2 * AcessOS Microkernel Version
12 #define CHECK_NUM_NULLOK(v,size) \
13 if((v)&&!Syscall_Valid((size),(Uint)(v))){ret=-1;err=-EINVAL;break;}
14 #define CHECK_STR_NULLOK(v) \
15 if((v)&&!Syscall_ValidString((Uint)(v))){ret=-1;err=-EINVAL;break;}
16 #define CHECK_NUM_NONULL(v,size) \
17 if(!(v)||!Syscall_Valid((size),(Uint)(v))){ret=-1;err=-EINVAL;break;}
18 #define CHECK_STR_NONULL(v) \
19 if(!(v)||!Syscall_ValidString((Uint)(v))){ret=-1;err=-EINVAL;break;}
22 extern int Proc_Clone(Uint *Err, Uint Flags);
23 extern int Threads_WaitTID(int TID, int *status);
24 extern Uint Proc_SendMessage(Uint *Err, Uint Dest, Uint Length, void *Data);
25 extern int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer);
26 extern int Proc_Execve(char *File, char **ArgV, char **EnvP);
27 extern Uint Binary_Load(char *file, Uint *entryPoint);
28 extern int Threads_SetName(char *NewName);
29 extern int Threads_SetUID(Uint *errno, tUID ID);
30 extern int Threads_SetGID(Uint *errno, tGID ID);
31 extern int Threads_SetFaultHandler(Uint Handler);
34 int Syscall_ValidString(Uint Addr);
35 int Syscall_Valid(int Size, Uint Addr);
38 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
39 void SyscallHandler(tSyscallRegs *Regs)
44 ENTER("iThread iNum", Threads_GetTID(), Regs->Num);
45 if(Regs->Num < NUM_SYSCALLS)
46 LOG("Syscall %s", cSYSCALL_NAMES[Regs->Num]);
47 LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x, Arg4: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3, Regs->Arg4);
51 // -- Exit the current thread
52 case SYS_EXIT: Threads_Exit(0, Regs->Arg1); break;
54 // -- Put the current thread to sleep
55 case SYS_SLEEP: Threads_Sleep(); break;
57 // -- Yield current timeslice
58 case SYS_YIELD: Threads_Yield(); break;
60 // -- Set Error Handler
61 case SYS_SETFAULTHANDLER:
62 Threads_SetFaultHandler(Regs->Arg1);
65 // -- Clone the current thread
67 // Call clone system call
68 ret = Proc_Clone(&err, Regs->Arg1);
69 // Change user stack if a new stack address is passed
70 if(ret == 0 && Regs->Arg2)
71 Regs->StackPointer = Regs->Arg2;
80 // -- Wait for a thread
82 // Sanity Check (Status can be NULL)
83 CHECK_NUM_NULLOK( Regs->Arg2, sizeof(int) );
85 ret = Threads_WaitTID(Regs->Arg1, (void*)Regs->Arg2);
88 // -- Get the physical address of a page
90 ret = MM_GetPhysAddr(Regs->Arg1);
94 case SYS_MAP: MM_Map(Regs->Arg1, Regs->Arg2); break;
96 // -- Allocate an address
97 case SYS_ALLOCATE: ret = MM_Allocate(Regs->Arg1); break;
99 // -- Unmap an address
100 case SYS_UNMAP: MM_Deallocate(Regs->Arg1); break;
102 // -- Get Thread/Process IDs
103 case SYS_GETTID: ret = Threads_GetTID(); break;
104 case SYS_GETPID: ret = Threads_GetPID(); break;
106 // -- Get User/Group IDs
107 case SYS_GETUID: ret = Threads_GetUID(); break;
108 case SYS_GETGID: ret = Threads_GetGID(); break;
110 // -- Set User/Group IDs
111 case SYS_SETUID: ret = Threads_SetUID(&err, Regs->Arg1); break;
112 case SYS_SETGID: ret = Threads_SetGID(&err, Regs->Arg1); break;
116 CHECK_NUM_NONULL(Regs->Arg3, Regs->Arg2);
117 // Destination, Size, *Data
118 ret = Proc_SendMessage(&err, Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
120 // -- Check for messages
122 CHECK_NUM_NULLOK(Regs->Arg1, sizeof(Uint));
123 if( Regs->Arg2 && Regs->Arg2 != -1 && !MM_IsUser(Regs->Arg2) )
125 err = -EINVAL; ret = -1; break;
128 ret = Proc_GetMessage(&err, (Uint*)Regs->Arg1, (void*)Regs->Arg2);
131 // -- Set the thread's name
133 CHECK_STR_NONULL(Regs->Arg1);
134 Threads_SetName( (char*)Regs->Arg1 );
141 CHECK_STR_NONULL(Regs->Arg1);
142 //Log(" Regs = {Arg2: %x, Arg3: %x}", Regs->Arg2, Regs->Arg3);
145 char **tmp = (char**)Regs->Arg2;
146 // Check ArgV (traverse array checking all string pointers)
147 CHECK_NUM_NONULL( tmp, sizeof(char**) );
148 //Log("tmp = %p", tmp);
149 for(i=0;tmp[i];i++) {
150 CHECK_NUM_NONULL( &tmp[i], sizeof(char*) );
151 CHECK_STR_NONULL( tmp[i] );
154 // - EnvP can be NULL
157 tmp = (char**)Regs->Arg3;
158 //Log("tmp = %p", tmp);
159 for(i=0;tmp[i];i++) {
160 CHECK_NUM_NULLOK( &tmp[i], sizeof(char*) );
161 CHECK_STR_NONULL( tmp[i] );
165 LEAVE('s', "Assuming 0");
166 // Path, **Argv, **Envp
167 ret = Proc_Execve((char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3);
170 if( !Syscall_ValidString(Regs->Arg1)
171 || !Syscall_Valid(sizeof(Uint), Regs->Arg2) ) {
177 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
181 // Virtual Filesystem
184 if( !Syscall_ValidString(Regs->Arg1) ) {
189 ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
193 VFS_Close( Regs->Arg1 );
198 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
200 ret = VFS_Seek( Regs->Arg1, Regs->Arg2|(((Uint64)Regs->Arg3)<<32), Regs->Arg4 );
205 ret = VFS_Tell( Regs->Arg1 );
209 CHECK_NUM_NONULL( Regs->Arg3, Regs->Arg2 );
210 ret = VFS_Write( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
214 CHECK_NUM_NONULL( Regs->Arg3, Regs->Arg2 );
215 ret = VFS_Read( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
219 CHECK_NUM_NONULL( Regs->Arg2, sizeof(tFInfo) + Regs->Arg3*sizeof(tVFS_ACL) );
221 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
226 if( !Syscall_Valid(sizeof(tVFS_ACL), Regs->Arg1) ) {
231 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
236 if( !Syscall_Valid(8, Regs->Arg2) ) {
241 ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
244 // Open a file that is a entry in an open directory
246 if( !Syscall_ValidString(Regs->Arg2) ) {
251 ret = VFS_OpenChild( &err, Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3);
256 if( !Syscall_ValidString(Regs->Arg1) ) {
261 ret = VFS_ChDir( (void*)Regs->Arg1 );
266 // All sanity checking should be done by the driver
267 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
270 // Mount a filesystem
272 // Only root can mount filesystems
273 if(Threads_GetUID() != 0) {
278 // Sanity check the paths
279 if(!Syscall_ValidString(Regs->Arg1)
280 || !Syscall_ValidString(Regs->Arg2)
281 || !Syscall_ValidString(Regs->Arg3)
282 || !Syscall_ValidString(Regs->Arg4) ) {
288 (char*)Regs->Arg1, // Device
289 (char*)Regs->Arg2, // Mount point
290 (char*)Regs->Arg3, // Filesystem
291 (char*)Regs->Arg4 // Options
298 Log((char*)Regs->Arg1,
299 Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6);
303 // -- Default (Return Error)
305 Warning("SyscallHandler: Unknown System Call %i", Regs->Num);
306 if(Regs->Num < NUM_SYSCALLS)
307 Warning(" Syscall '%s'", cSYSCALL_NAMES[Regs->Num]);
314 LOG("ID: %i, Return errno = %i", Regs->Num, err);
318 Regs->Return = ret&0xFFFFFFFF;
319 Regs->RetHi = ret >> 32;
325 LOG("err = %i", err);
326 if(Regs->Num != SYS_EXECVE)
329 LOG("Actual %i", ret);
334 * \fn int Syscall_ValidString(Uint Addr)
335 * \brief Checks if a memory address contains a valid string
337 int Syscall_ValidString(Uint Addr)
339 // Check if the memory is user memory
340 if(!MM_IsUser(Addr)) return 0;
342 return CheckString( (char*)Addr );
346 * \fn int Syscall_Valid(int Size, Uint Addr)
347 * \brief Checks if a memory address is valid
349 int Syscall_Valid(int Size, Uint Addr)
351 if(!MM_IsUser(Addr)) return 0;
353 return CheckMem( (void*)Addr, Size );