84eb416fd7f99e50e77c00f725b86b494e3ae413
[tpg/acess2.git] / AcessNative / acesskernel_src / syscalls.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * Syscall Distribution
6  */
7 #define DEBUG   1
8 #include <acess.h>
9 #include <threads.h>
10 #include "../syscalls.h"
11
12 // === IMPORTS ===
13 extern int      Threads_Fork(void);     // AcessNative only function
14
15 // === TYPES ===
16 typedef int     (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes);
17
18 // === MACROS ===
19 #define SYSCALL5(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
20         _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;\
21         if(strcmp(Fmt,_fmtstr)!=0)return 0;\
22         a0 = *(_t0*)Args;Args+=sizeof(_t0);\
23         a1 = *(_t1*)Args;Args+=sizeof(_t1);\
24         a2 = *(_t2*)Args;Args+=sizeof(_t2);\
25         a3 = *(_t3*)Args;Args+=sizeof(_t3);\
26         a4 = *(_t4*)Args;Args+=sizeof(_t4);\
27         LOG("SYSCALL5 '%s' %p %p %p %p %p", Fmt, (intptr_t)a0,(intptr_t)a1,(intptr_t)a2,(intptr_t)a3,(intptr_t)a4);\
28         _call\
29 }
30 #define SYSCALL4(_name, _fmtstr, _t0, _t1, _t2, _t3, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
31         _t0 a0;_t1 a1;_t2 a2;_t3 a3;\
32         if(strcmp(Fmt,_fmtstr)!=0)return 0;\
33         a0 = *(_t0*)Args;Args+=sizeof(_t0);\
34         a1 = *(_t1*)Args;Args+=sizeof(_t1);\
35         a2 = *(_t2*)Args;Args+=sizeof(_t2);\
36         a3 = *(_t3*)Args;Args+=sizeof(_t3);\
37         LOG("SYSCALL4 '%s' %p %p %p %p", Fmt, (intptr_t)a0,(intptr_t)a1,(intptr_t)a2,(intptr_t)a3);\
38         _call\
39 }
40
41 #define SYSCALL3(_name, _fmtstr, _t0, _t1, _t2, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
42         _t0 a0;_t1 a1;_t2 a2;\
43         if(strcmp(Fmt,_fmtstr)!=0)return 0;\
44         a0 = *(_t0*)Args;Args+=sizeof(_t0);\
45         a1 = *(_t1*)Args;Args+=sizeof(_t1);\
46         a2 = *(_t2*)Args;Args+=sizeof(_t2);\
47         LOG("SYSCALL3 '%s' %p %p %p", Fmt, (intptr_t)a0,(intptr_t)a1,(intptr_t)a2);\
48         _call\
49 }
50
51 #define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
52         _t0 a0;_t1 a1;\
53         if(strcmp(Fmt,_fmtstr)!=0)return 0;\
54         a0 = *(_t0*)Args;Args+=sizeof(_t0);\
55         a1 = *(_t1*)Args;Args+=sizeof(_t1);\
56         LOG("SYSCALL2 '%s' %p %p", Fmt, (intptr_t)a0,(intptr_t)a1);\
57         _call;\
58 }
59
60 #define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
61         _t0 a0;\
62         if(strcmp(Fmt,_fmtstr)!=0)return 0;\
63         a0 = *(_t0*)Args;Args+=sizeof(_t0);\
64         LOG("SYSCALL1 '%s' %p", Fmt,(intptr_t)a0);\
65         _call;\
66 }
67
68 #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
69         if(strcmp(Fmt,"")!=0)return 0;\
70         LOG("SYSCALL0");\
71         _call;\
72 }
73
74 // === CODE ===
75 int Syscall_Null(Uint*Errno, const char *Format, void *Args, int *Sizes)
76 {
77         return 0;
78 }
79
80 SYSCALL1(Syscall_Exit, "i", int,
81         Threads_Exit(0, a0);
82         return 0;
83 );
84
85 SYSCALL2(Syscall_Open, "si", const char *, int,
86         return VFS_Open(a0, a1|VFS_OPENFLAG_USER);
87 );
88 SYSCALL1(Syscall_Close, "i", int,
89         VFS_Close(a0);
90         return 0;
91 );
92 SYSCALL3(Syscall_Read, "iid", int, int, void *,
93         if( Sizes[2] < a1 ) {
94                 Log_Warning("Syscalls", "Read - %i < %i", Sizes[2], a1);
95                 return -1;
96         }
97         return VFS_Read(a0, a1, a2);
98 );
99 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
100         if( Sizes[2] < a1 )
101                 return -1;
102         return VFS_Write(a0, a1, a2);
103 );
104 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
105         return VFS_Seek(a0, a1, a2);
106 );
107 SYSCALL1(Syscall_Tell, "i", int,
108         return VFS_Tell(a0);
109 );
110 SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
111         return VFS_IOCtl(a0, a1, a2);
112 );
113 SYSCALL3(Syscall_FInfo, "idi", int, void *, int,
114         if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL)) {
115                 LOG("offsetof(size) = %i", offsetof(tFInfo, size));
116                 LOG("Bad size %i < %i", Sizes[1], sizeof(tFInfo)+a2*sizeof(tVFS_ACL));
117                 *Errno = -EINVAL;
118                 return -1;
119         }
120         return VFS_FInfo(a0, a1, a2);
121 );
122 SYSCALL2(Syscall_ReadDir, "id", int, char *,
123         if(Sizes[1] < 255)
124                 return -1;
125         return VFS_ReadDir(a0, a1);
126 );
127 SYSCALL5(Syscall_select, "idddd", int, fd_set *, fd_set *, fd_set *, time_t *,
128         return VFS_Select(a0, a1, a2, a3, a4, 0);
129 );
130 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
131         return VFS_OpenChild(a0, a1, a2|VFS_OPENFLAG_USER);
132 );
133 SYSCALL2(Syscall_GetACL, "id", int, void *,
134         if(Sizes[1] < sizeof(tVFS_ACL))
135                 return -1;
136         return VFS_GetACL(a0, (void*)a1);
137 );
138 SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *,
139         return VFS_Mount(a0, a1, a2, a3);
140 );
141 SYSCALL1(Syscall_Chdir, "s", const char *,
142         return VFS_ChDir(a0);
143 );
144 SYSCALL0(Syscall_Sleep,
145         Threads_Sleep();
146         return 0;
147 );
148 SYSCALL2(Syscall_WaitTID, "id", int, int *,
149         if(Sizes[1] < sizeof(int))
150                 return -1;
151         return Threads_WaitTID(a0, a1);
152 );
153 SYSCALL1(Syscall_SetUID, "i", int,
154         if(Sizes[0] < sizeof(int)) {
155                 *Errno = -EINVAL;       // TODO: Better message
156                 return -1;
157         }
158         return Threads_SetUID(Errno, a0);
159 );
160 SYSCALL1(Syscall_SetGID, "i", int,
161         if(Sizes[0] < sizeof(int)) {
162                 *Errno = -EINVAL;       // TODO: Better message
163                 return -1;
164         }
165         return Threads_SetGID(Errno, a0);
166 );
167
168 SYSCALL1(Syscall_Fork, "d", int *,
169         if(Sizes[0] < sizeof(int))
170                 return -1;
171         *a0 = Threads_Fork();
172         return *a0;
173 );
174
175 const tSyscallHandler   caSyscalls[] = {
176         Syscall_Null,
177         Syscall_Exit,
178         Syscall_Open,
179         Syscall_Close,
180         Syscall_Read,
181         Syscall_Write,
182         Syscall_Seek,
183         Syscall_Tell,
184         Syscall_IOCtl,
185         Syscall_FInfo,
186         Syscall_ReadDir,
187         Syscall_OpenChild,
188         Syscall_GetACL,
189         Syscall_Mount,
190         NULL,   // SYS_REOPEN
191         Syscall_Chdir,
192         
193         Syscall_WaitTID,
194         Syscall_SetUID,
195         Syscall_SetGID,
196         
197         Syscall_Sleep,
198         Syscall_Fork,
199
200         NULL,
201         NULL,
202         Syscall_select
203 };
204 const int       ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
205 /**
206  * \brief Recieve a syscall structure from the server code
207  */
208 tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
209 {
210         char    formatString[Request->NParams+1];
211         char    *inData = (char*)&Request->Params[Request->NParams];
212          int    argListLen = 0;
213          int    i, retVal;
214         tRequestHeader  *ret;
215          int    retValueCount = 1;
216          int    retDataLen = sizeof(Uint64);
217         void    *returnData[Request->NParams];
218          int    argSizes[Request->NParams];
219         Uint    ret_errno = 0;
220         
221         // Sanity check
222         if( Request->CallID >= ciNumSyscalls ) {
223                 Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
224                 return NULL;
225         }
226         
227         if( !caSyscalls[Request->CallID] ) {
228                 Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
229                 return NULL;
230         }
231         
232         // Get size of argument list
233         for( i = 0; i < Request->NParams; i ++ )
234         {
235                 argSizes[i] = Request->Params[i].Length;
236                 switch(Request->Params[i].Type)
237                 {
238                 case ARG_TYPE_VOID:
239                         formatString[i] = '-';
240                         break;
241                 case ARG_TYPE_INT32:
242                         formatString[i] = 'i';
243                         argListLen += sizeof(Uint32);
244                         break;
245                 case ARG_TYPE_INT64:
246                         formatString[i] = 'I';
247                         argListLen += sizeof(Uint64);
248                         break;
249                 case ARG_TYPE_DATA:
250                         formatString[i] = 'd';
251                         argListLen += sizeof(void*);
252                         break;
253                 case ARG_TYPE_STRING:
254                         formatString[i] = 's';
255                         argListLen += sizeof(char*);
256                         break;
257                 default:
258                         return NULL;    // ERROR!
259                 }
260         }
261         formatString[i] = '\0';
262         
263         LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
264         
265         {
266                 char    argListData[argListLen];
267                 argListLen = 0;
268                 // Build argument list
269                 for( i = 0; i < Request->NParams; i ++ )
270                 {
271                         returnData[i] = NULL;
272                         switch(Request->Params[i].Type)
273                         {
274                         case ARG_TYPE_VOID:
275                                 break;
276                         case ARG_TYPE_INT32:
277                                 LOG("%i INT32: 0x%x", i, *(Uint32*)inData);
278                                 *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
279                                 argListLen += sizeof(Uint32);
280                                 inData += sizeof(Uint32);
281                                 break;
282                         case ARG_TYPE_INT64:
283                                 LOG("%i INT64: 0x%llx", i, *(Uint64*)inData);
284                                 *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
285                                 argListLen += sizeof(Uint64);
286                                 inData += sizeof(Uint64);
287                                 break;
288                         case ARG_TYPE_STRING:
289                                 LOG("%i STR: '%s'", i, (char*)inData);
290                                 *(char**)&argListData[argListLen] = (char*)inData;
291                                 argListLen += sizeof(void*);
292                                 inData += Request->Params[i].Length;
293                                 break;
294                         
295                         // Data gets special handling, because only it can be returned to the user
296                         // (ARG_TYPE_DATA is a pointer)
297                         case ARG_TYPE_DATA:
298                                 // Prepare the return values
299                                 if( Request->Params[i].Flags & ARG_FLAG_RETURN )
300                                 {
301                                         retDataLen += Request->Params[i].Length;
302                                         retValueCount ++;
303                                 }
304                                 
305                                 // Check for non-resident data
306                                 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
307                                 {
308                                         // Allocate and zero the buffer
309                                         returnData[i] = calloc(1, Request->Params[i].Length);
310                                         LOG("%i ZDAT: %i %p", i,
311                                                 Request->Params[i].Length, returnData[i]);
312                                         *(void**)&argListData[argListLen] = returnData[i];
313                                         argListLen += sizeof(void*);
314                                 }
315                                 else
316                                 {
317                                         returnData[i] = (void*)inData;
318                                         LOG("%i DATA: %i %p", i,
319                                                 Request->Params[i].Length, returnData[i]);
320                                         *(void**)&argListData[argListLen] = (void*)inData;
321                                         argListLen += sizeof(void*);
322                                         inData += Request->Params[i].Length;
323                                 }
324                                 break;
325                         }
326                 }
327                 
328                 retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
329         }
330         
331         // Allocate the return
332         ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
333                 + retDataLen);
334         ret->ClientID = Request->ClientID;
335         ret->CallID = Request->CallID;
336         ret->NParams = retValueCount;
337         inData = (char*)&ret->Params[ ret->NParams ];
338         
339         // Static Uint64 return value
340         ret->Params[0].Type = ARG_TYPE_INT64;
341         ret->Params[0].Flags = 0;
342         ret->Params[0].Length = sizeof(Uint64);
343         *(Uint64*)inData = retVal;
344         inData += sizeof(Uint64);
345         
346         Log_Debug("Syscalls", "Return 0x%llx", retVal);
347         
348         retValueCount = 1;
349         for( i = 0; i < Request->NParams; i ++ )
350         {
351                 if( Request->Params[i].Type != ARG_TYPE_DATA )  continue;
352                 if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) )     continue;
353                 
354                 ret->Params[retValueCount].Type = Request->Params[i].Type;
355                 ret->Params[retValueCount].Flags = 0;
356                 ret->Params[retValueCount].Length = Request->Params[i].Length;
357                 
358                 LOG("Syscalls", "Ret %i: Type %i, Len %i",
359                         i, Request->Params[i].Type, Request->Params[i].Length);
360                 
361                 memcpy(inData, returnData[i], Request->Params[i].Length);
362                 inData += Request->Params[i].Length;
363                 
364                 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
365                         free( returnData[i] );  // Free temp buffer from above
366                 retValueCount ++;
367         }
368         
369         *ReturnLength = sizeof(tRequestHeader)
370                 + retValueCount * sizeof(tRequestValue)
371                 + retDataLen;
372         
373         return ret;
374 }

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