Usermode/ld-acess - Clean up loadlib.c, improved debug
[tpg/acess2.git] / KernelLand / Kernel / syscalls.c
1 /*
2  * AcessOS Microkernel Version
3  * syscalls.c
4  */
5 #define DEBUG   0
6
7 #include <acess.h>
8 #include <syscalls.h>
9 #include <proc.h>
10 #include <hal_proc.h>
11 #include <errno.h>
12 #include <threads.h>
13 #include <events.h>
14
15 #if 1
16 # define MERR(f,v...) Log_Debug("Syscalls", "0x%x "f, callNum ,## v)
17 #else
18 # define MERR(v...) do{}while(0)
19 #endif
20
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 {\
30          int    i;\
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*) ); \
36         }\
37         if(tmp[i]) break;\
38 } while(0)
39
40 #if BITS==64
41 #define ARG64(idx1,idx2)        ***ARG64 not used on 64-bit***
42 #else
43 #define ARG64(idx1, idx2)       (Regs->Arg##idx1|(((Uint64)Regs->Arg##idx2)<<32))
44 #endif
45
46 // === IMPORTS ===
47 extern Uint     Binary_Load(const char *file, Uint *entryPoint);
48
49 // === PROTOTYPES ===
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);
54
55 // === CODE ===
56 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
57 void SyscallHandler(tSyscallRegs *Regs)
58 {
59         Uint64  ret = 0;
60         Uint    err = -EOK;
61          int    callNum = Regs->Num;
62         
63         #if DEBUG < 2
64         if(callNum != SYS_READ && callNum != SYS_WRITE) {
65         #endif
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);
70         #if DEBUG < 2
71         }
72         #endif
73         
74         switch(Regs->Num)
75         {
76         // -- Exit the current thread
77         case SYS_EXIT:  Threads_Exit(0, Regs->Arg1);    break;
78         
79         // -- Put the current thread to sleep
80         case SYS_SLEEP: Threads_Sleep();        break;
81
82         case SYS_TIMEDSLEEP:
83                 Time_Delay(Regs->Arg1);
84                 break;
85         
86         // -- Yield current timeslice
87         case SYS_YIELD: Threads_Yield();        break;
88         
89         // -- Set Error Handler
90         case SYS_SETFAULTHANDLER:
91                 Threads_SetFaultHandler(Regs->Arg1);
92                 break;
93         
94         // -- Clone the current thread
95         case SYS_CLONE:
96                 // Call clone system call
97                 ret = Proc_Clone(Regs->Arg1);
98                 break;
99         
100         // -- Send a signal
101         case SYS_KILL:
102                 err = -ENOSYS;
103                 ret = -1;
104                 break;
105
106         // -- Wait fr an event  
107         case SYS_WAITEVENT:
108                 // Message mask
109                 ret = Threads_WaitEvents(Regs->Arg1);
110                 break;
111
112         // -- Wait for a thread
113         case SYS_WAITTID:
114                 // Sanity Check (Status can be NULL)
115                 CHECK_NUM_NULLOK( (int*)Regs->Arg2, sizeof(int) );
116                 // TID, *Status
117                 ret = Threads_WaitTID(Regs->Arg1, (int*)Regs->Arg2);
118                 break;
119         
120         // -- Get the physical address of a page
121         case SYS_GETPHYS:
122                 ret = MM_GetPhysAddr( (void*)Regs->Arg1 );
123                 break;
124         
125         // -- Map an address
126         case SYS_MAP:   MM_Map((void*)Regs->Arg1, Regs->Arg2);  break;
127         
128         // -- Allocate an address
129         case SYS_ALLOCATE:      ret = MM_Allocate((void*)Regs->Arg1);   break;
130         
131         // -- Unmap an address
132         case SYS_UNMAP:         MM_Deallocate((void*)Regs->Arg1);       break;
133         
134         // -- Change the protection on an address
135         case SYS_SETFLAGS:
136                 ret = Syscall_MM_SetFlags((void*)Regs->Arg1, Regs->Arg2, Regs->Arg3);
137                 break;
138
139         // -- Get Thread/Process IDs
140         case SYS_GETTID:        ret = Threads_GetTID(); break;
141         case SYS_GETPID:        ret = Threads_GetPID(); break;
142         
143         // -- Get User/Group IDs
144         case SYS_GETUID:        ret = Threads_GetUID(); break;
145         case SYS_GETGID:        ret = Threads_GetGID(); break;
146         
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;
150         
151         // -- Send Message
152         case SYS_SENDMSG:
153                 CHECK_NUM_NONULL( (void*)Regs->Arg3, Regs->Arg2 );
154                 // Destination, Size, *Data
155                 ret = Proc_SendMessage(Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
156                 break;
157         // -- Check for messages
158         case SYS_GETMSG:
159                 CHECK_NUM_NULLOK( (Uint*)Regs->Arg1, sizeof(Uint) );
160                 if( Regs->Arg3 != -1 )
161                         CHECK_NUM_NULLOK((void*)Regs->Arg3, Regs->Arg2);
162                 // *Source, *Data
163                 ret = Proc_GetMessage((Uint*)Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
164                 break;
165         
166         // -- Get the current timestamp
167         case SYS_GETTIME:
168                 ret = now();
169                 break;
170         
171         // -- Set the thread's name
172         case SYS_SETNAME:
173                 CHECK_STR_NONULL( (char*) Regs->Arg1);
174                 Threads_SetName( (char*)Regs->Arg1 );
175                 break;
176         
177         // ---
178         // Binary Control
179         // ---
180         // -- Create a new process
181         case SYS_SPAWN:
182                 CHECK_STR_NONULL((const char*)Regs->Arg1);
183                 CHECK_STR_ARRAY((const char**)Regs->Arg2);
184                 CHECK_STR_ARRAY((const char**)Regs->Arg3);
185                 if( Regs->Arg4 > 0 )
186                         CHECK_NUM_NONULL((void*)Regs->Arg5, Regs->Arg4*sizeof(int));
187                 ret = Proc_SysSpawn(
188                         (const char*)Regs->Arg1, (const char**)Regs->Arg2, (const char**)Regs->Arg3,
189                         Regs->Arg4, (int*)Regs->Arg5
190                         );
191                 break;
192         // -- Replace the current process with another
193         case SYS_EXECVE:
194                 CHECK_STR_NONULL((char*)Regs->Arg1);
195                 CHECK_STR_ARRAY( (char**)Regs->Arg2 );
196                 if( Regs->Arg3 )
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)
200                 ret = Proc_Execve(
201                         (const char*)Regs->Arg1, (const char**)Regs->Arg2, (const char**)Regs->Arg3,
202                         0
203                         );
204                 break;
205         // -- Load a binary into the current process
206         case SYS_LOADBIN:
207                 CHECK_STR_NONULL( (char*)Regs->Arg1 );
208                 CHECK_NUM_NONULL( (Uint*)Regs->Arg2, sizeof(Uint) );
209                 // Path, *Entrypoint
210                 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
211                 break;
212
213         // -- Load a kernel module
214         case SYS_LOADMOD:
215                 CHECK_STR_NONULL( (const char *)Regs->Arg1 );
216                 if( Threads_GetUID() != 0 ) {
217                         MERR("Not root");
218                         ret = EACCES;
219                 }
220                 else {
221                         LOG("Module_LoadFile(\"%s\", NULL)", (const char *)Regs->Arg1);
222                         ret = Module_LoadFile( (const char *)Regs->Arg1, NULL );
223                 }
224                 break;
225
226         // ---
227         // Virtual Filesystem
228         // ---
229         case SYS_OPEN:
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);
233                 break;
234         case SYS_REOPEN:
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);
238                 break;
239         
240         case SYS_CLOSE:
241                 LOG("VFS_Close(%i)", Regs->Arg1);
242                 VFS_Close( Regs->Arg1 );
243                 break;
244         
245         case SYS_COPYFD:
246                 LOG("VFS_DuplicateFD(%i,%i)", Regs->Arg1, Regs->Arg2);
247                 ret = VFS_DuplicateFD(Regs->Arg1, Regs->Arg2);
248                 break;
249
250         case SYS_FDCTL:
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);
253                 break;
254
255         case SYS_SEEK:
256                 #if BITS == 64
257                 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
258                 #else
259                 ret = VFS_Seek( Regs->Arg1, ARG64(2, 3), Regs->Arg4 );
260                 #endif
261                 break;
262                 
263         case SYS_TELL:
264                 ret = VFS_Tell( Regs->Arg1 );
265                 break;
266         
267         case SYS_WRITE:
268                 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
269                 ret = VFS_Write( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
270                 break;
271         case SYS_WRITEAT:
272                 #if BITS == 64
273                 CHECK_NUM_NONULL( (void*)Regs->Arg5, Regs->Arg3 );
274                 ret = VFS_WriteAt( Regs->Arg1, Regs->Arg2, Regs->Arg3, (void*)Regs->Arg4 );
275                 #else
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 );
280                 #endif
281                 break;
282         
283         case SYS_READ:
284                 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
285                 ret = VFS_Read( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
286                 break;
287         case SYS_READAT:
288                 CHECK_NUM_NONULL( (void*)Regs->Arg5, Regs->Arg2 );
289                 #if BITS == 64
290                 ret = VFS_ReadAt( Regs->Arg1, Regs->Arg2, Regs->Arg3, (void*)Regs->Arg4 );
291                 #else
292                 ret = VFS_ReadAt( Regs->Arg1, Regs->Arg2, ARG64(3, 4), (void*)Regs->Arg5 );
293                 #endif
294                 break;
295         
296         case SYS_TRUNCATE:
297                 ret = VFS_Truncate(
298                         Regs->Arg1,
299                         #if BITS == 32
300                         ARG64(2,3)
301                         #else
302                         Regs->Arg2
303                         #endif
304                         );
305                 break;
306         
307         case SYS_FINFO:
308                 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tFInfo) + Regs->Arg3*sizeof(tVFS_ACL) );
309                 // FP, Dest, MaxACLs
310                 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
311                 break;
312         
313         // Get ACL Value
314         case SYS_GETACL:
315                 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tVFS_ACL) );
316                 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
317                 break;
318         
319         // Read Directory
320         case SYS_READDIR:
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 );
325                 break;
326         
327         // Open a file that is a entry in an open directory
328         case SYS_OPENCHILD:
329                 CHECK_STR_NONULL( (char*)Regs->Arg2 );
330                 ret = VFS_OpenChild( Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3 | VFS_OPENFLAG_USER);
331                 break;
332         
333         // Change Directory
334         case SYS_CHDIR:
335                 CHECK_STR_NONULL( (const char*)Regs->Arg1 );
336                 ret = VFS_ChDir( (const char*)Regs->Arg1 );
337                 break;
338         
339         // IO Control
340         case SYS_IOCTL:
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;
345                 }
346                 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
347                 break;
348         
349         // Mount a filesystem
350         case SYS_MOUNT:
351                 // Only root can mount filesystems
352                 if(Threads_GetUID() != 0) {
353                         err = -EACCES;
354                         ret = -1;
355                         break;
356                 }
357                 
358                 if( !Regs->Arg1 )
359                 {
360                         if( !Syscall_ValidString((char*)Regs->Arg2) ) {
361                                 err = -EINVAL;
362                                 ret = -1;
363                                 break;
364                         }
365                         
366                         ret = VFS_Unmount((char*)Regs->Arg2);
367                 }
368                 else
369                 {
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);
375                         ret = VFS_Mount(
376                                 (char*)Regs->Arg1,      // Device
377                                 (char*)Regs->Arg2,      // Mount point
378                                 (char*)Regs->Arg3,      // Filesystem
379                                 (char*)Regs->Arg4       // Options
380                                 );
381                 }
382                 break;
383                 
384         // Wait on a set of handles
385         case SYS_SELECT:
386                 // Sanity checks
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)) )
391                 {
392                         err = -EINVAL;
393                         ret = -1;
394                         break;
395                 }
396                 // Perform the call
397                 ret = VFS_Select(
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
404                         0       // User handles
405                         );
406                 break;
407
408         case SYS_MMAP:
409                 ret = (tVAddr)VFS_MMap(
410                         (void*)Regs->Arg1, Regs->Arg2,
411                         Regs->Arg3&0xFFFF, Regs->Arg3>>16,
412                         Regs->Arg4,
413                         #if BITS == 32
414                         ARG64(5,6)
415                         #else
416                         Regs->Arg5
417                         #endif
418                         );
419                 break;
420         case SYS_MUNMAP:
421                 ret = VFS_MUnmap( (void*)Regs->Arg1, Regs->Arg2 );
422                 break;
423         
424         // Create a directory
425         case SYS_MKDIR:
426                 CHECK_STR_NONULL( (char*)Regs->Arg1 );
427                 ret = VFS_MkDir( (char*)Regs->Arg1 );
428                 break;
429         
430         case SYS_UNLINK:
431                 Log_Error("Syscalls", "TODO: Impliment SYS_UNLINK");
432                 break;
433         
434         case SYS_MARSHALFD:
435                 ret = VFS_MarshalHandle(Regs->Arg1);
436                 break;
437         case SYS_UNMARSHALFD:
438                 #if BITS == 64
439                 ret = VFS_UnmarshalHandle( Regs->Arg1 );
440                 #else
441                 ret = VFS_UnmarshalHandle( ARG64(1,2) );
442                 #endif
443                 break;
444
445         // -- Debug
446         //#if DEBUG_BUILD
447         case SYS_DEBUGS:
448                 CHECK_STR_NONULL( (char*)Regs->Arg1 );
449                 Log("Log: %08lli [%i] %s", now(), Threads_GetTID(), (const char*)Regs->Arg1);
450                 break;
451         case SYS_DEBUGF:
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);
456                 LogF("\r\n");
457                 break;
458         case SYS_DEBUGHEX:
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 );
462                 break;
463         //#endif
464         
465         // -- Default (Return Error)
466         default:
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]);
470                 err = -ENOSYS;
471                 ret = -1;
472                 break;
473         }
474
475         if(err == 0)    err = errno;
476         
477         if(err != 0) {
478                 LOG("ID: %i, Return errno = %i", Regs->Num, err);
479         }
480         
481         #if BITS < 64
482         Regs->Return = ret&0xFFFFFFFF;
483         Regs->RetHi = ret >> 32;
484         #else
485         Regs->Return = ret;
486         #endif
487         Regs->Error = err;
488         #if DEBUG
489         # if DEBUG < 2
490         if( callNum != SYS_READ && callNum != SYS_WRITE ) {
491         # endif
492         LOG("err = %i", err);
493         if( callNum == SYS_EXECVE )
494                 LOG("Actual %i", ret);
495         else
496                 LEAVE('x', ret);
497         # if DEBUG < 2
498         }
499         # endif
500         #endif
501 }
502
503 /**
504  * \fn int Syscall_ValidString(const char *Addr)
505  * \brief Checks if a memory address contains a valid string
506  */
507 int Syscall_ValidString(const char *Addr)
508 {
509         // Check if the memory is user memory
510         if(!MM_IsUser( (tVAddr) Addr))  return 0;
511         
512         return CheckString( Addr );
513 }
514
515 /**
516  * \fn int Syscall_Valid(int Size, const void *Addr)
517  * \brief Checks if a memory address is valid
518  */
519 int Syscall_Valid(int Size, const void *Addr)
520 {
521         if(!MM_IsUser( (tVAddr)Addr )) {
522                 Log_Debug("Syscalls", "Syscall_Valid - %p not user", Addr);
523                 return 0;
524         }
525         
526         return CheckMem( Addr, Size );
527 }
528
529 int Syscall_MM_SetFlags(void *Addr, Uint Flags, Uint Mask)
530 {
531         tPAddr  paddr = MM_GetPhysAddr(Addr);
532         Flags &= MM_PFLAG_RO|MM_PFLAG_EXEC;
533         Mask &= MM_PFLAG_RO|MM_PFLAG_EXEC;
534
535         //Log_Debug("Syscalls", "SYS_SETFLAGS: %p %x %x", Addr, Flags, Mask);   
536
537         // Enable write?
538         if( (Mask & MM_PFLAG_RO) && !(Flags & MM_PFLAG_RO) ) {
539                 void    *node;
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;
544                 }
545         }
546         MM_SetFlags(Addr, Flags, Mask);
547         return 0;
548 }

UCC git Repository :: git.ucc.asn.au