2 * AcessOS Microkernel Version
16 # define MERR(f,v...) Log_Debug("Syscalls", "0x%x "f, callNum ,## v)
18 # define MERR(v...) do{}while(0)
21 #define CHECK_NUM_NULLOK(v,size) \
22 if((v)&&!Syscall_Valid((size),(v))){MERR("CHECK_NUM_NULLOK: %p(%x) FAIL",v,size);ret=-1;err=-EINVAL;break;}
23 #define CHECK_STR_NULLOK(v) \
24 if((v)&&!Syscall_ValidString((v))){MERR("CHECK_STR_NULLOK: %p FAIL",v);ret=-1;err=-EINVAL;break;}
25 #define CHECK_NUM_NONULL(v,size) \
26 if(!(v)||!Syscall_Valid((size),(v))){MERR("CHECK_NUM_NONULL: %p(%x) FAIL",v,size);ret=-1;err=-EINVAL;break;}
27 #define CHECK_STR_NONULL(v) \
28 if(!(v)||!Syscall_ValidString((v))){MERR("CHECK_STR_NONULL: %p FAIL",v);ret=-1;err=-EINVAL;break;}
29 #define CHECK_STR_ARRAY(arr) do {\
31 char **tmp = (char**)arr; \
32 CHECK_NUM_NULLOK( tmp, sizeof(char**) ); \
33 for(i=0;tmp&&tmp[i];i++) { \
34 CHECK_STR_NONULL( tmp[i] ); \
35 CHECK_NUM_NONULL( &tmp[i+1], sizeof(char*) ); \
41 #define ARG64(idx1,idx2) ***ARG64 not used on 64-bit***
43 #define ARG64(idx1, idx2) (Regs->Arg##idx1|(((Uint64)Regs->Arg##idx2)<<32))
47 extern Uint Binary_Load(const char *file, Uint *entryPoint);
50 void SyscallHandler(tSyscallRegs *Regs);
51 int Syscall_ValidString(const char *Addr);
52 int Syscall_Valid(int Size, const void *Addr);
53 int Syscall_MM_SetFlags(void *Addr, Uint Flags, Uint Mask);
56 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
57 void SyscallHandler(tSyscallRegs *Regs)
61 int callNum = Regs->Num;
64 if(callNum != SYS_READ && callNum != SYS_WRITE) {
66 ENTER("iThread iNum", Threads_GetTID(), callNum);
67 if(callNum < NUM_SYSCALLS)
68 LOG("Syscall %s", cSYSCALL_NAMES[callNum]);
69 LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x, Arg4: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3, Regs->Arg4);
76 // -- Exit the current thread
77 case SYS_EXIT: Threads_Exit(0, Regs->Arg1); break;
79 // -- Put the current thread to sleep
80 case SYS_SLEEP: Threads_Sleep(); break;
83 Time_Delay(Regs->Arg1);
86 // -- Yield current timeslice
87 case SYS_YIELD: Threads_Yield(); break;
89 // -- Set Error Handler
90 case SYS_SETFAULTHANDLER:
91 Threads_SetFaultHandler(Regs->Arg1);
94 // -- Clone the current thread
96 // Call clone system call
97 ret = Proc_Clone(Regs->Arg1);
106 // -- Wait fr an event
109 ret = Threads_WaitEvents(Regs->Arg1);
112 // -- Wait for a thread
114 // Sanity Check (Status can be NULL)
115 CHECK_NUM_NULLOK( (int*)Regs->Arg2, sizeof(int) );
117 ret = Threads_WaitTID(Regs->Arg1, (int*)Regs->Arg2);
120 // -- Get the physical address of a page
122 ret = MM_GetPhysAddr( (void*)Regs->Arg1 );
126 case SYS_MAP: MM_Map((void*)Regs->Arg1, Regs->Arg2); break;
128 // -- Allocate an address
129 case SYS_ALLOCATE: ret = MM_Allocate((void*)Regs->Arg1); break;
131 // -- Unmap an address
132 case SYS_UNMAP: MM_Deallocate((void*)Regs->Arg1); break;
134 // -- Change the protection on an address
136 ret = Syscall_MM_SetFlags((void*)Regs->Arg1, Regs->Arg2, Regs->Arg3);
139 // -- Get Thread/Process IDs
140 case SYS_GETTID: ret = Threads_GetTID(); break;
141 case SYS_GETPID: ret = Threads_GetPID(); break;
143 // -- Get User/Group IDs
144 case SYS_GETUID: ret = Threads_GetUID(); break;
145 case SYS_GETGID: ret = Threads_GetGID(); break;
147 // -- Set User/Group IDs
148 case SYS_SETUID: ret = Threads_SetUID(Regs->Arg1); break;
149 case SYS_SETGID: ret = Threads_SetGID(Regs->Arg1); break;
153 CHECK_NUM_NONULL( (void*)Regs->Arg3, Regs->Arg2 );
154 // Destination, Size, *Data
155 ret = Proc_SendMessage(Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
157 // -- Check for messages
159 CHECK_NUM_NULLOK( (Uint*)Regs->Arg1, sizeof(Uint) );
160 if( Regs->Arg3 != -1 )
161 CHECK_NUM_NULLOK((void*)Regs->Arg3, Regs->Arg2);
163 ret = Proc_GetMessage((Uint*)Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
166 // -- Get the current timestamp
171 // -- Set the thread's name
173 CHECK_STR_NONULL( (char*) Regs->Arg1);
174 Threads_SetName( (char*)Regs->Arg1 );
180 // -- Create a new process
182 CHECK_STR_NONULL((const char*)Regs->Arg1);
183 CHECK_STR_ARRAY((const char**)Regs->Arg2);
184 CHECK_STR_ARRAY((const char**)Regs->Arg3);
186 CHECK_NUM_NONULL((void*)Regs->Arg5, Regs->Arg4*sizeof(int));
188 (const char*)Regs->Arg1, (const char**)Regs->Arg2, (const char**)Regs->Arg3,
189 Regs->Arg4, (int*)Regs->Arg5
192 // -- Replace the current process with another
194 CHECK_STR_NONULL((char*)Regs->Arg1);
195 CHECK_STR_ARRAY( (char**)Regs->Arg2 );
197 CHECK_STR_ARRAY( (char**)Regs->Arg3 );
198 LEAVE('s', "Assuming 0");
199 // Path, **Argv, **Envp, DataSize (=0 to tell it to create a copy)
201 (const char*)Regs->Arg1, (const char**)Regs->Arg2, (const char**)Regs->Arg3,
205 // -- Load a binary into the current process
207 CHECK_STR_NONULL( (char*)Regs->Arg1 );
208 CHECK_NUM_NONULL( (Uint*)Regs->Arg2, sizeof(Uint) );
210 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
213 // -- Load a kernel module
215 CHECK_STR_NONULL( (const char *)Regs->Arg1 );
216 if( Threads_GetUID() != 0 ) {
221 LOG("Module_LoadFile(\"%s\", NULL)", (const char *)Regs->Arg1);
222 ret = Module_LoadFile( (const char *)Regs->Arg1, NULL );
227 // Virtual Filesystem
230 CHECK_STR_NONULL( (char*)Regs->Arg1 );
231 LOG("VFS_Open(\"%s\", 0x%x)", (char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
232 ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
235 CHECK_STR_NONULL( (char*)Regs->Arg2 );
236 LOG("VFS_Reopen(%i, \"%s\", 0x%x)", Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3 | VFS_OPENFLAG_USER);
237 ret = VFS_Reopen(Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3 | VFS_OPENFLAG_USER);
241 LOG("VFS_Close(%i)", Regs->Arg1);
242 VFS_Close( Regs->Arg1 );
246 LOG("VFS_DuplicateFD(%i,%i)", Regs->Arg1, Regs->Arg2);
247 ret = VFS_DuplicateFD(Regs->Arg1, Regs->Arg2);
251 LOG("VFS_SetFDFlags(%i,0%o,0%o)", Regs->Arg1, Regs->Arg2, Regs->Arg3);
252 ret = VFS_SetFDFlags(Regs->Arg1, Regs->Arg2, Regs->Arg3);
257 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
259 ret = VFS_Seek( Regs->Arg1, ARG64(2, 3), Regs->Arg4 );
264 ret = VFS_Tell( Regs->Arg1 );
268 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
269 ret = VFS_Write( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
273 CHECK_NUM_NONULL( (void*)Regs->Arg5, Regs->Arg3 );
274 ret = VFS_WriteAt( Regs->Arg1, Regs->Arg2, Regs->Arg3, (void*)Regs->Arg4 );
276 CHECK_NUM_NONULL( (void*)Regs->Arg5, Regs->Arg4 );
277 Debug("VFS_WriteAt(%i, %lli, %i, %p)",
278 Regs->Arg1, ARG64(2, 3), Regs->Arg4, (void*)Regs->Arg5);
279 ret = VFS_WriteAt( Regs->Arg1, ARG64(2, 3), Regs->Arg4, (void*)Regs->Arg5 );
284 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
285 ret = VFS_Read( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
288 CHECK_NUM_NONULL( (void*)Regs->Arg5, Regs->Arg2 );
290 ret = VFS_ReadAt( Regs->Arg1, Regs->Arg2, Regs->Arg3, (void*)Regs->Arg4 );
292 ret = VFS_ReadAt( Regs->Arg1, Regs->Arg2, ARG64(3, 4), (void*)Regs->Arg5 );
308 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tFInfo) + Regs->Arg3*sizeof(tVFS_ACL) );
310 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
315 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tVFS_ACL) );
316 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
321 // TODO: What if the filename is longer?
322 // Maybe force it to be a 256 byte buffer
323 CHECK_NUM_NONULL( (void*)Regs->Arg2, 256 );
324 ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
327 // Open a file that is a entry in an open directory
329 CHECK_STR_NONULL( (char*)Regs->Arg2 );
330 ret = VFS_OpenChild( Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3 | VFS_OPENFLAG_USER);
335 CHECK_STR_NONULL( (const char*)Regs->Arg1 );
336 ret = VFS_ChDir( (const char*)Regs->Arg1 );
341 // All sanity checking should be done by the driver
342 if( Regs->Arg3 && !MM_IsUser(Regs->Arg3) ) {
343 MERR("IOCtl Invalid arg %p", Regs->Arg3);
344 err = -EINVAL; ret = -1; break;
346 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
349 // Mount a filesystem
351 // Only root can mount filesystems
352 if(Threads_GetUID() != 0) {
360 if( !Syscall_ValidString((char*)Regs->Arg2) ) {
366 ret = VFS_Unmount((char*)Regs->Arg2);
370 // Sanity check the paths
371 CHECK_STR_NONULL((char*)Regs->Arg1);
372 CHECK_STR_NONULL((char*)Regs->Arg2);
373 CHECK_STR_NULLOK((char*)Regs->Arg3);
374 CHECK_STR_NONULL((char*)Regs->Arg4);
376 (char*)Regs->Arg1, // Device
377 (char*)Regs->Arg2, // Mount point
378 (char*)Regs->Arg3, // Filesystem
379 (char*)Regs->Arg4 // Options
384 // Wait on a set of handles
387 if( (Regs->Arg2 && !Syscall_Valid(sizeof(fd_set), (void*)Regs->Arg2))
388 || (Regs->Arg3 && !Syscall_Valid(sizeof(fd_set), (void*)Regs->Arg3))
389 || (Regs->Arg4 && !Syscall_Valid(sizeof(fd_set), (void*)Regs->Arg4))
390 || (Regs->Arg5 && !Syscall_Valid(sizeof(tTime), (void*)Regs->Arg5)) )
398 Regs->Arg1, // Max handle
399 (fd_set *)Regs->Arg2, // Read
400 (fd_set *)Regs->Arg3, // Write
401 (fd_set *)Regs->Arg4, // Errors
402 (tTime *)Regs->Arg5, // Timeout
403 (Uint32)Regs->Arg6, // Extra wakeup events
409 ret = (tVAddr)VFS_MMap(
410 (void*)Regs->Arg1, Regs->Arg2,
411 Regs->Arg3&0xFFFF, Regs->Arg3>>16,
421 ret = VFS_MUnmap( (void*)Regs->Arg1, Regs->Arg2 );
424 // Create a directory
426 CHECK_STR_NONULL( (char*)Regs->Arg1 );
427 ret = VFS_MkDir( (char*)Regs->Arg1 );
431 Log_Error("Syscalls", "TODO: Impliment SYS_UNLINK");
435 ret = VFS_MarshalHandle(Regs->Arg1);
437 case SYS_UNMARSHALFD:
439 ret = VFS_UnmarshalHandle( Regs->Arg1 );
441 ret = VFS_UnmarshalHandle( ARG64(1,2) );
448 CHECK_STR_NONULL( (char*)Regs->Arg1 );
449 Log("Log: %08lli [%i] %s", now(), Threads_GetTID(), (const char*)Regs->Arg1);
452 CHECK_STR_NONULL( (char*)Regs->Arg1 );
453 LogF("Log: %08lli [%i] ", now(), Threads_GetTID());
454 LogF((const char*)Regs->Arg1,
455 Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6);
459 CHECK_STR_NONULL( (char*)Regs->Arg1 );
460 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
461 Debug_HexDump( (const char*)Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
465 // -- Default (Return Error)
467 Log_Warning("Syscalls", "Unknown System Call %i", Regs->Num);
468 if(Regs->Num < NUM_SYSCALLS)
469 Log_Warning("Syscall", " named '%s'", cSYSCALL_NAMES[Regs->Num]);
475 if(err == 0) err = errno;
478 LOG("ID: %i, Return errno = %i", Regs->Num, err);
482 Regs->Return = ret&0xFFFFFFFF;
483 Regs->RetHi = ret >> 32;
490 if( callNum != SYS_READ && callNum != SYS_WRITE ) {
492 LOG("err = %i", err);
493 if( callNum == SYS_EXECVE )
494 LOG("Actual %i", ret);
504 * \fn int Syscall_ValidString(const char *Addr)
505 * \brief Checks if a memory address contains a valid string
507 int Syscall_ValidString(const char *Addr)
509 // Check if the memory is user memory
510 if(!MM_IsUser( (tVAddr) Addr)) return 0;
512 return CheckString( Addr );
516 * \fn int Syscall_Valid(int Size, const void *Addr)
517 * \brief Checks if a memory address is valid
519 int Syscall_Valid(int Size, const void *Addr)
521 if(!MM_IsUser( (tVAddr)Addr )) {
522 Log_Debug("Syscalls", "Syscall_Valid - %p not user", Addr);
526 return CheckMem( Addr, Size );
529 int Syscall_MM_SetFlags(void *Addr, Uint Flags, Uint Mask)
531 tPAddr paddr = MM_GetPhysAddr(Addr);
532 Flags &= MM_PFLAG_RO|MM_PFLAG_EXEC;
533 Mask &= MM_PFLAG_RO|MM_PFLAG_EXEC;
535 //Log_Debug("Syscalls", "SYS_SETFLAGS: %p %x %x", Addr, Flags, Mask);
538 if( (Mask & MM_PFLAG_RO) && !(Flags & MM_PFLAG_RO) ) {
540 // HACK - Assume that RO mmap'd files are immutable
541 if( MM_GetPageNode(paddr, &node) == 0 && node ) {
542 Flags |= MM_PFLAG_COW;
543 Mask |= MM_PFLAG_COW;
546 MM_SetFlags(Addr, Flags, Mask);