Usermode/ld-acess - Fixed bug with ARM nested functions
[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 <hal_proc.h>
11 #include <errno.h>
12 #include <threads.h>
13
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;}
22
23 // === IMPORTS ===
24 extern int      Proc_Execve(char *File, char **ArgV, char **EnvP);
25 extern Uint     Binary_Load(const char *file, Uint *entryPoint);
26
27 // === PROTOTYPES ===
28 void    SyscallHandler(tSyscallRegs *Regs);
29  int    Syscall_ValidString(const char *Addr);
30  int    Syscall_Valid(int Size, const void *Addr);
31
32 // === CODE ===
33 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
34 void SyscallHandler(tSyscallRegs *Regs)
35 {
36         Uint64  ret = 0;
37         Uint    err = -EOK;
38          int    callNum = Regs->Num;
39         
40         #if DEBUG < 2
41         if(callNum != SYS_READ && callNum != SYS_WRITE) {
42         #endif
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);
47         #if DEBUG < 2
48         }
49         #endif
50         
51         switch(Regs->Num)
52         {
53         // -- Exit the current thread
54         case SYS_EXIT:  Threads_Exit(0, Regs->Arg1);    break;
55         
56         // -- Put the current thread to sleep
57         case SYS_SLEEP: Threads_Sleep();        break;
58         
59         // -- Yield current timeslice
60         case SYS_YIELD: Threads_Yield();        break;
61         
62         // -- Set Error Handler
63         case SYS_SETFAULTHANDLER:
64                 Threads_SetFaultHandler(Regs->Arg1);
65                 break;
66         
67         // -- Clone the current thread
68         case SYS_CLONE:
69                 // Call clone system call
70                 ret = Proc_Clone(Regs->Arg1);
71                 break;
72         
73         // -- Send a signal
74         case SYS_KILL:
75                 err = -ENOSYS;
76                 ret = -1;
77                 break;
78         
79         // -- Wait for a thread
80         case SYS_WAITTID:
81                 // Sanity Check (Status can be NULL)
82                 CHECK_NUM_NULLOK( (int*)Regs->Arg2, sizeof(int) );
83                 // TID, *Status
84                 ret = Threads_WaitTID(Regs->Arg1, (int*)Regs->Arg2);
85                 break;
86         
87         // -- Get the physical address of a page
88         case SYS_GETPHYS:
89                 ret = MM_GetPhysAddr(Regs->Arg1);
90                 break;
91         
92         // -- Map an address
93         case SYS_MAP:   MM_Map(Regs->Arg1, Regs->Arg2); break;
94         
95         // -- Allocate an address
96         case SYS_ALLOCATE:      ret = MM_Allocate(Regs->Arg1);  break;
97         
98         // -- Unmap an address
99         case SYS_UNMAP:         MM_Deallocate(Regs->Arg1);      break;
100         
101         // -- Get Thread/Process IDs
102         case SYS_GETTID:        ret = Threads_GetTID(); break;
103         case SYS_GETPID:        ret = Threads_GetPID(); break;
104         
105         // -- Get User/Group IDs
106         case SYS_GETUID:        ret = Threads_GetUID(); break;
107         case SYS_GETGID:        ret = Threads_GetGID(); break;
108         
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;
112         
113         // -- Send Message
114         case SYS_SENDMSG:
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);
118                 break;
119         // -- Check for messages
120         case SYS_GETMSG:
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;
126                 }
127                 // *Source, *Data
128                 ret = Proc_GetMessage(&err, (Uint*)Regs->Arg1, (void*)Regs->Arg2);
129                 break;
130         
131         // -- Get the current timestamp
132         case SYS_GETTIME:
133                 ret = now();
134                 break;
135         
136         // -- Set the thread's name
137         case SYS_SETNAME:
138                 CHECK_STR_NONULL( (char*) Regs->Arg1);
139                 Threads_SetName( (char*)Regs->Arg1 );
140                 break;
141         
142         // ---
143         // Binary Control
144         // ---
145         // -- Replace the current process with another
146         case SYS_EXECVE:
147                 CHECK_STR_NONULL((char*)Regs->Arg1);
148                 // Check the argument arrays
149                 {
150                          int    i;
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] );
158                         }
159                         if(ret == -1) break;
160                         // Check EnvP also
161                         // - EnvP can be NULL
162                         if( Regs->Arg3 )
163                         {
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] );
169                                 }
170                                 if(ret == -1) break;
171                         }
172                 }
173                 LEAVE('s', "Assuming 0");
174                 // Path, **Argv, **Envp
175                 ret = Proc_Execve( (char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3 );
176                 break;
177         // -- Load a binary into the current process
178         case SYS_LOADBIN:
179                 if( !Syscall_ValidString( (char*) Regs->Arg1)
180                 ||  !Syscall_Valid(sizeof(Uint), (Uint*)Regs->Arg2) ) {
181                         err = -EINVAL;
182                         ret = 0;
183                         break;
184                 }
185                 // Path, *Entrypoint
186                 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
187                 break;
188         
189         // ---
190         // Virtual Filesystem
191         // ---
192         case SYS_OPEN:
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);
196                 break;
197         
198         case SYS_CLOSE:
199                 LOG("VFS_Close(%i)", Regs->Arg1);
200                 VFS_Close( Regs->Arg1 );
201                 break;
202         
203         case SYS_SEEK:
204                 #if BITS == 64
205                 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3 );
206                 #else
207                 ret = VFS_Seek( Regs->Arg1, Regs->Arg2|(((Uint64)Regs->Arg3)<<32), Regs->Arg4 );
208                 #endif
209                 break;
210                 
211         case SYS_TELL:
212                 ret = VFS_Tell( Regs->Arg1 );
213                 break;
214         
215         case SYS_WRITE:
216                 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
217                 ret = VFS_Write( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
218                 break;
219         
220         case SYS_READ:
221                 CHECK_NUM_NONULL( (void*)Regs->Arg2, Regs->Arg3 );
222                 ret = VFS_Read( Regs->Arg1, Regs->Arg3, (void*)Regs->Arg2 );
223                 break;
224         
225         case SYS_FINFO:
226                 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tFInfo) + Regs->Arg3*sizeof(tVFS_ACL) );
227                 // FP, Dest, MaxACLs
228                 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
229                 break;
230         
231         // Get ACL Value
232         case SYS_GETACL:
233                 CHECK_NUM_NONULL( (void*)Regs->Arg2, sizeof(tVFS_ACL) );
234                 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
235                 break;
236         
237         // Read Directory
238         case SYS_READDIR:
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 );
243                 break;
244         
245         // Open a file that is a entry in an open directory
246         case SYS_OPENCHILD:
247                 CHECK_STR_NONULL( (char*)Regs->Arg2 );
248                 ret = VFS_OpenChild( Regs->Arg1, (char*)Regs->Arg2, Regs->Arg3);
249                 break;
250         
251         // Change Directory
252         case SYS_CHDIR:
253                 CHECK_STR_NONULL( (const char*)Regs->Arg1 );
254                 ret = VFS_ChDir( (const char*)Regs->Arg1 );
255                 break;
256         
257         // IO Control
258         case SYS_IOCTL:
259                 // All sanity checking should be done by the driver
260                 if( Regs->Arg3 && !MM_IsUser(Regs->Arg3) ) {
261                         err = -EINVAL;  ret = -1;       break;
262                 }
263                 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
264                 break;
265         
266         // Mount a filesystem
267         case SYS_MOUNT:
268                 // Only root can mount filesystems
269                 if(Threads_GetUID() != 0) {
270                         err = -EACCES;
271                         ret = -1;
272                         break;
273                 }
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) ) {
279                         err = -EINVAL;
280                         ret = -1;
281                         break;
282                 }
283                 ret = VFS_Mount(
284                         (char*)Regs->Arg1,      // Device
285                         (char*)Regs->Arg2,      // Mount point
286                         (char*)Regs->Arg3,      // Filesystem
287                         (char*)Regs->Arg4       // Options
288                         );
289                 break;
290                 
291         // Wait on a set of handles
292         case SYS_SELECT:
293                 // Sanity checks
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)) )
298                 {
299                         err = -EINVAL;
300                         ret = -1;
301                         break;
302                 }
303                 // Perform the call
304                 ret = VFS_Select(
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
310                         0       // User handles
311                         );
312                 break;
313         
314         // -- Debug
315         //#if DEBUG_BUILD
316         case SYS_DEBUG:
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);
321                 LogF("\r\n");
322                 break;
323         //#endif
324         
325         // -- Default (Return Error)
326         default:
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]);
330                 err = -ENOSYS;
331                 ret = -1;
332                 break;
333         }
334
335         if(err == 0)    err = errno;
336         
337         if(err != 0) {
338                 LOG("ID: %i, Return errno = %i", Regs->Num, err);
339         }
340         
341         #if BITS < 64
342         Regs->Return = ret&0xFFFFFFFF;
343         Regs->RetHi = ret >> 32;
344         #else
345         Regs->Return = ret;
346         #endif
347         Regs->Error = err;
348         #if DEBUG
349         # if DEBUG < 2
350         if( callNum != SYS_READ && callNum != SYS_WRITE ) {
351         # endif
352         LOG("err = %i", err);
353         if( callNum == SYS_EXECVE )
354                 LOG("Actual %i", ret);
355         else
356                 LEAVE('x', ret);
357         # if DEBUG < 2
358         }
359         # endif
360         #endif
361 }
362
363 /**
364  * \fn int Syscall_ValidString(const char *Addr)
365  * \brief Checks if a memory address contains a valid string
366  */
367 int Syscall_ValidString(const char *Addr)
368 {
369         // Check if the memory is user memory
370         if(!MM_IsUser( (tVAddr) Addr))  return 0;
371         
372         return CheckString( Addr );
373 }
374
375 /**
376  * \fn int Syscall_Valid(int Size, const void *Addr)
377  * \brief Checks if a memory address is valid
378  */
379 int Syscall_Valid(int Size, const void *Addr)
380 {
381         if(!MM_IsUser( (tVAddr)Addr ))  return 0;
382         
383         return CheckMem( Addr, Size );
384 }

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