2 * AcessOS Microkernel Version
14 #define CHECK_NUM_NULLOK(v,size) \
15 if((v)&&!Syscall_Valid((size),(v))){ret=-1;err=-EINVAL;break;}
16 #define CHECK_STR_NULLOK(v) \
17 if((v)&&!Syscall_ValidString((v))){ret=-1;err=-EINVAL;break;}
18 #define CHECK_NUM_NONULL(v,size) \
19 if(!(v)||!Syscall_Valid((size),(v))){ret=-1;err=-EINVAL;break;}
20 #define CHECK_STR_NONULL(v) \
21 if(!(v)||!Syscall_ValidString((v))){ret=-1;err=-EINVAL;break;}
24 extern int Proc_Execve(char *File, char **ArgV, char **EnvP);
25 extern Uint Binary_Load(const char *file, Uint *entryPoint);
28 void SyscallHandler(tSyscallRegs *Regs);
29 int Syscall_ValidString(const char *Addr);
30 int Syscall_Valid(int Size, const void *Addr);
33 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
34 void SyscallHandler(tSyscallRegs *Regs)
38 int callNum = Regs->Num;
41 if(callNum != SYS_READ && callNum != SYS_WRITE) {
43 ENTER("iThread iNum", Threads_GetTID(), callNum);
44 if(callNum < NUM_SYSCALLS)
45 LOG("Syscall %s", cSYSCALL_NAMES[callNum]);
46 LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x, Arg4: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3, Regs->Arg4);
53 // -- Exit the current thread
54 case SYS_EXIT: Threads_Exit(0, Regs->Arg1); break;
56 // -- Put the current thread to sleep
57 case SYS_SLEEP: Threads_Sleep(); break;
59 // -- Yield current timeslice
60 case SYS_YIELD: Threads_Yield(); break;
62 // -- Set Error Handler
63 case SYS_SETFAULTHANDLER:
64 Threads_SetFaultHandler(Regs->Arg1);
67 // -- Clone the current thread
69 // Call clone system call
70 ret = Proc_Clone(Regs->Arg1);
79 // -- Wait for a thread
81 // Sanity Check (Status can be NULL)
82 CHECK_NUM_NULLOK( (int*)Regs->Arg2, sizeof(int) );
84 ret = Threads_WaitTID(Regs->Arg1, (int*)Regs->Arg2);
87 // -- Get the physical address of a page
89 ret = MM_GetPhysAddr(Regs->Arg1);
93 case SYS_MAP: MM_Map(Regs->Arg1, Regs->Arg2); break;
95 // -- Allocate an address
96 case SYS_ALLOCATE: ret = MM_Allocate(Regs->Arg1); break;
98 // -- Unmap an address
99 case SYS_UNMAP: MM_Deallocate(Regs->Arg1); break;
101 // -- Get Thread/Process IDs
102 case SYS_GETTID: ret = Threads_GetTID(); break;
103 case SYS_GETPID: ret = Threads_GetPID(); break;
105 // -- Get User/Group IDs
106 case SYS_GETUID: ret = Threads_GetUID(); break;
107 case SYS_GETGID: ret = Threads_GetGID(); break;
109 // -- Set User/Group IDs
110 case SYS_SETUID: ret = Threads_SetUID(&err, Regs->Arg1); break;
111 case SYS_SETGID: ret = Threads_SetGID(&err, Regs->Arg1); break;
115 CHECK_NUM_NONULL( (void*)Regs->Arg3, Regs->Arg2 );
116 // Destination, Size, *Data
117 ret = Proc_SendMessage(&err, Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
119 // -- Check for messages
121 CHECK_NUM_NULLOK( (Uint*)Regs->Arg1, sizeof(Uint) );
122 // NOTE: Can't do range checking as we don't know the size
123 // - Should be done by Proc_GetMessage
124 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 // -- Get the current timestamp
136 // -- Set the thread's name
138 CHECK_STR_NONULL( (char*) Regs->Arg1);
139 Threads_SetName( (char*)Regs->Arg1 );
145 // -- Replace the current process with another
147 CHECK_STR_NONULL((char*)Regs->Arg1);
148 // Check the argument arrays
151 char **tmp = (char**)Regs->Arg2;
152 // Check ArgV (traverse array checking all string pointers)
153 CHECK_NUM_NONULL( tmp, sizeof(char**) );
154 //Log("tmp = %p", tmp);
155 for(i=0;tmp[i];i++) {
156 CHECK_NUM_NONULL( &tmp[i], sizeof(char*) );
157 CHECK_STR_NONULL( tmp[i] );
161 // - EnvP can be NULL
164 tmp = (char**)Regs->Arg3;
165 CHECK_NUM_NONULL(tmp, sizeof(char**));
166 for(i=0;tmp[i];i++) {
167 CHECK_NUM_NONULL( &tmp[i], sizeof(char*) );
168 CHECK_STR_NONULL( tmp[i] );
173 LEAVE('s', "Assuming 0");
174 // Path, **Argv, **Envp
175 ret = Proc_Execve( (char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3 );
177 // -- Load a binary into the current process
179 if( !Syscall_ValidString( (char*) Regs->Arg1)
180 || !Syscall_Valid(sizeof(Uint), (Uint*)Regs->Arg2) ) {
186 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
190 // Virtual Filesystem
193 CHECK_STR_NONULL( (char*)Regs->Arg1 );
194 LOG("VFS_Open(\"%s\", 0x%x)", (char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
195 ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
199 LOG("VFS_Close(%i)", Regs->Arg1);
200 VFS_Close( Regs->Arg1 );
205 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
207 ret = VFS_Seek( Regs->Arg1, Regs->Arg2|(((Uint64)Regs->Arg3)<<32), Regs->Arg4 );
212 ret = VFS_Tell( Regs->Arg1 );
216 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
217 ret = VFS_Write( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
221 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
222 ret = VFS_Read( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
226 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tFInfo) + Regs->Arg3*sizeof(tVFS_ACL) );
228 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
233 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tVFS_ACL) );
234 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
239 // TODO: What if the filename is longer?
240 // Maybe force it to be a 256 byte buffer
241 CHECK_NUM_NONULL( (void*)Regs->Arg2, 256 );
242 ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
245 // Open a file that is a entry in an open directory
247 CHECK_STR_NONULL( (char*)Regs->Arg2 );
248 ret = VFS_OpenChild( Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3);
253 CHECK_STR_NONULL( (const char*)Regs->Arg1 );
254 ret = VFS_ChDir( (const char*)Regs->Arg1 );
259 // All sanity checking should be done by the driver
260 if( Regs->Arg3 && !MM_IsUser(Regs->Arg3) ) {
261 err = -EINVAL; ret = -1; break;
263 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
266 // Mount a filesystem
268 // Only root can mount filesystems
269 if(Threads_GetUID() != 0) {
274 // Sanity check the paths
275 if(!Syscall_ValidString((char*)Regs->Arg1)
276 || !Syscall_ValidString((char*)Regs->Arg2)
277 || !Syscall_ValidString((char*)Regs->Arg3)
278 || !Syscall_ValidString((char*)Regs->Arg4) ) {
284 (char*)Regs->Arg1, // Device
285 (char*)Regs->Arg2, // Mount point
286 (char*)Regs->Arg3, // Filesystem
287 (char*)Regs->Arg4 // Options
291 // Wait on a set of handles
294 if( (Regs->Arg2 && !Syscall_Valid(sizeof(fd_set), (void*)Regs->Arg2))
295 || (Regs->Arg3 && !Syscall_Valid(sizeof(fd_set), (void*)Regs->Arg3))
296 || (Regs->Arg4 && !Syscall_Valid(sizeof(fd_set), (void*)Regs->Arg4))
297 || (Regs->Arg5 && !Syscall_Valid(sizeof(tTime), (void*)Regs->Arg5)) )
305 Regs->Arg1, // Max handle
306 (fd_set *)Regs->Arg2, // Read
307 (fd_set *)Regs->Arg3, // Write
308 (fd_set *)Regs->Arg4, // Errors
309 (tTime *)Regs->Arg5, // Timeout
317 CHECK_STR_NONULL( (char*)Regs->Arg1 );
318 LogF("Log: %08lli [%i] ", now(), Threads_GetTID());
319 LogF((const char*)Regs->Arg1,
320 Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6);
325 // -- Default (Return Error)
327 Log_Warning("Syscalls", "Unknown System Call %i", Regs->Num);
328 if(Regs->Num < NUM_SYSCALLS)
329 Log_Warning("Syscall", " named '%s'", cSYSCALL_NAMES[Regs->Num]);
335 if(err == 0) err = errno;
338 LOG("ID: %i, Return errno = %i", Regs->Num, err);
342 Regs->Return = ret&0xFFFFFFFF;
343 Regs->RetHi = ret >> 32;
350 if( callNum != SYS_READ && callNum != SYS_WRITE ) {
352 LOG("err = %i", err);
353 if( callNum == SYS_EXECVE )
354 LOG("Actual %i", ret);
364 * \fn int Syscall_ValidString(const char *Addr)
365 * \brief Checks if a memory address contains a valid string
367 int Syscall_ValidString(const char *Addr)
369 // Check if the memory is user memory
370 if(!MM_IsUser( (tVAddr) Addr)) return 0;
372 return CheckString( Addr );
376 * \fn int Syscall_Valid(int Size, const void *Addr)
377 * \brief Checks if a memory address is valid
379 int Syscall_Valid(int Size, const void *Addr)
381 if(!MM_IsUser( (tVAddr)Addr )) return 0;
383 return CheckMem( Addr, Size );