Division bug hidden, now only shows when 64-bit division is needed
[tpg/acess2.git] / 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 <errno.h>
11
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;}
20
21 // === IMPORTS ===
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);
32
33 // === PROTOTYPES ===
34  int    Syscall_ValidString(Uint Addr);
35  int    Syscall_Valid(int Size, Uint Addr);
36
37 // === CODE ===
38 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
39 void SyscallHandler(tSyscallRegs *Regs)
40 {
41         Uint64  ret = 0;
42         Uint    err = -EOK;
43         
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);
48         
49         switch(Regs->Num)
50         {
51         // -- Exit the current thread
52         case SYS_EXIT:  Threads_Exit(0, Regs->Arg1);    break;
53         
54         // -- Put the current thread to sleep
55         case SYS_SLEEP: Threads_Sleep();        break;
56         
57         // -- Yield current timeslice
58         case SYS_YIELD: Threads_Yield();        break;
59         
60         // -- Set Error Handler
61         case SYS_SETFAULTHANDLER:
62                 Threads_SetFaultHandler(Regs->Arg1);
63                 break;
64         
65         // -- Clone the current thread
66         case SYS_CLONE:
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;
72                 break;
73         
74         // -- Send a signal
75         case SYS_KILL:
76                 err = -ENOSYS;
77                 ret = -1;
78                 break;
79         
80         // -- Wait for a thread
81         case SYS_WAITTID:
82                 // Sanity Check (Status can be NULL)
83                 CHECK_NUM_NULLOK( Regs->Arg2, sizeof(int) );
84                 // TID, *Status
85                 ret = Threads_WaitTID(Regs->Arg1, (void*)Regs->Arg2);
86                 break;
87         
88         // -- Get the physical address of a page
89         case SYS_GETPHYS:
90                 ret = MM_GetPhysAddr(Regs->Arg1);
91                 break;
92         
93         // -- Map an address
94         case SYS_MAP:   MM_Map(Regs->Arg1, Regs->Arg2); break;
95         
96         // -- Allocate an address
97         case SYS_ALLOCATE:      ret = MM_Allocate(Regs->Arg1);  break;
98         
99         // -- Unmap an address
100         case SYS_UNMAP:         MM_Deallocate(Regs->Arg1);      break;
101         
102         // -- Get Thread/Process IDs
103         case SYS_GETTID:        ret = Threads_GetTID(); break;
104         case SYS_GETPID:        ret = Threads_GetPID(); break;
105         
106         // -- Get User/Group IDs
107         case SYS_GETUID:        ret = Threads_GetUID(); break;
108         case SYS_GETGID:        ret = Threads_GetGID(); break;
109         
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;
113         
114         // -- Send Message
115         case SYS_SENDMSG:
116                 CHECK_NUM_NONULL(Regs->Arg3, Regs->Arg2);
117                 // Destination, Size, *Data
118                 ret = Proc_SendMessage(&err, Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
119                 break;
120         // -- Check for messages
121         case SYS_GETMSG:
122                 CHECK_NUM_NULLOK(Regs->Arg1, sizeof(Uint));
123                 if( Regs->Arg2 && Regs->Arg2 != -1 && !MM_IsUser(Regs->Arg2) )
124                 {
125                         err = -EINVAL;  ret = -1;       break;
126                 }
127                 // *Source, *Data
128                 ret = Proc_GetMessage(&err, (Uint*)Regs->Arg1, (void*)Regs->Arg2);
129                 break;
130         
131         // -- Set the thread's name
132         case SYS_SETNAME:
133                 CHECK_STR_NONULL(Regs->Arg1);
134                 Threads_SetName( (char*)Regs->Arg1 );
135                 break;
136         
137         // ---
138         // Binary Control
139         // ---
140         case SYS_EXECVE:
141                 CHECK_STR_NONULL(Regs->Arg1);
142                 //Log(" Regs = {Arg2: %x, Arg3: %x}", Regs->Arg2, Regs->Arg3);
143                 {
144                          int    i;
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] );
152                         }
153                         // Check EnvP also
154                         // - EnvP can be NULL
155                         if( Regs->Arg3 )
156                         {
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] );
162                                 }
163                         }
164                 }
165                 LEAVE('s', "Assuming 0");
166                 // Path, **Argv, **Envp
167                 ret = Proc_Execve((char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3);
168                 break;
169         case SYS_LOADBIN:
170                 if( !Syscall_ValidString(Regs->Arg1)
171                 ||  !Syscall_Valid(sizeof(Uint), Regs->Arg2) ) {
172                         err = -EINVAL;
173                         ret = 0;
174                         break;
175                 }
176                 // Path, *Entrypoint
177                 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
178                 break;
179         
180         // ---
181         // Virtual Filesystem
182         // ---
183         case SYS_OPEN:
184                 if( !Syscall_ValidString(Regs->Arg1) ) {
185                         err = -EINVAL;
186                         ret = -1;
187                         break;
188                 }
189                 ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
190                 break;
191         
192         case SYS_CLOSE:
193                 VFS_Close( Regs->Arg1 );
194                 break;
195         
196         case SYS_SEEK:
197                 #if BITS == 64
198                 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
199                 #else
200                 ret = VFS_Seek( Regs->Arg1, Regs->Arg2|(((Uint64)Regs->Arg3)<<32), Regs->Arg4 );
201                 #endif
202                 break;
203                 
204         case SYS_TELL:
205                 ret = VFS_Tell( Regs->Arg1 );
206                 break;
207         
208         case SYS_WRITE:
209                 CHECK_NUM_NONULL( Regs->Arg3, Regs->Arg2 );
210                 ret = VFS_Write( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
211                 break;
212         
213         case SYS_READ:
214                 CHECK_NUM_NONULL( Regs->Arg3, Regs->Arg2 );
215                 ret = VFS_Read( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
216                 break;
217         
218         case SYS_FINFO:
219                 CHECK_NUM_NONULL( Regs->Arg2, sizeof(tFInfo) + Regs->Arg3*sizeof(tVFS_ACL) );
220                 // FP, Dest, MaxACLs
221                 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
222                 break;
223         
224         // Get ACL Value
225         case SYS_GETACL:
226                 if( !Syscall_Valid(sizeof(tVFS_ACL), Regs->Arg1) ) {
227                         err = -EINVAL;
228                         ret = -1;
229                         break;
230                 }
231                 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
232                 break;
233         
234         // Read Directory
235         case SYS_READDIR:
236                 if( !Syscall_Valid(8, Regs->Arg2) ) {
237                         err = -EINVAL;
238                         ret = -1;
239                         break;
240                 }
241                 ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
242                 break;
243         
244         // Open a file that is a entry in an open directory
245         case SYS_OPENCHILD:
246                 if( !Syscall_ValidString(Regs->Arg2) ) {
247                         err = -EINVAL;
248                         ret = -1;
249                         break;
250                 }
251                 ret = VFS_OpenChild( &err, Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3);
252                 break;
253         
254         // Change Directory
255         case SYS_CHDIR:
256                 if( !Syscall_ValidString(Regs->Arg1) ) {
257                         err = -EINVAL;
258                         ret = -1;
259                         break;
260                 }
261                 ret = VFS_ChDir( (void*)Regs->Arg1 );
262                 break;
263         
264         // IO Control
265         case SYS_IOCTL:
266                 // All sanity checking should be done by the driver
267                 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
268                 break;
269         
270         // Mount a filesystem
271         case SYS_MOUNT:
272                 // Only root can mount filesystems
273                 if(Threads_GetUID() != 0) {
274                         err = -EACCES;
275                         ret = -1;
276                         break;
277                 }
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) ) {
283                         err = -EINVAL;
284                         ret = -1;
285                         break;
286                 }
287                 ret = VFS_Mount(
288                         (char*)Regs->Arg1,      // Device
289                         (char*)Regs->Arg2,      // Mount point
290                         (char*)Regs->Arg3,      // Filesystem
291                         (char*)Regs->Arg4       // Options
292                         );
293                 break;
294         
295         // -- Debug
296         //#if DEBUG_BUILD
297         case SYS_DEBUG:
298                 Log((char*)Regs->Arg1,
299                         Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6);
300                 break;
301         //#endif
302         
303         // -- Default (Return Error)
304         default:
305                 Warning("SyscallHandler: Unknown System Call %i", Regs->Num);
306                 if(Regs->Num < NUM_SYSCALLS)
307                         Warning(" Syscall '%s'", cSYSCALL_NAMES[Regs->Num]);
308                 err = -ENOSYS;
309                 ret = -1;
310                 break;
311         }
312         
313         if(err != 0) {
314                 LOG("ID: %i, Return errno = %i", Regs->Num, err);
315         }
316         
317         #if BITS < 64
318         Regs->Return = ret&0xFFFFFFFF;
319         Regs->RetHi = ret >> 32;
320         #else
321         Regs->Return = ret;
322         #endif
323         Regs->Error = err;
324         #if DEBUG
325         LOG("err = %i", err);
326         if(Regs->Num != SYS_EXECVE)
327                 LEAVE('x', ret);
328         else
329                 LOG("Actual %i", ret);
330         #endif
331 }
332
333 /**
334  * \fn int Syscall_ValidString(Uint Addr)
335  * \brief Checks if a memory address contains a valid string
336  */
337 int Syscall_ValidString(Uint Addr)
338 {
339         // Check if the memory is user memory
340         if(!MM_IsUser(Addr))    return 0;
341         
342         return CheckString( (char*)Addr );
343 }
344
345 /**
346  * \fn int Syscall_Valid(int Size, Uint Addr)
347  * \brief Checks if a memory address is valid
348  */
349 int Syscall_Valid(int Size, Uint Addr)
350 {
351         if(!MM_IsUser(Addr))    return 0;
352         
353         return CheckMem( (void*)Addr, Size );
354 }

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