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

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