3 * - Acess kernel emulation on another OS using SDL and UDP
9 #include "../syscalls.h"
12 extern int Threads_Fork(void); // AcessNative only function
15 typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes);
18 #define SYSCALL5(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
19 _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;\
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 a4 = *(_t4*)Args;Args+=sizeof(_t4);\
28 #define SYSCALL4(_name, _fmtstr, _t0, _t1, _t2, _t3, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
29 _t0 a0;_t1 a1;_t2 a2;_t3 a3;\
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 a3 = *(_t3*)Args;Args+=sizeof(_t3);\
38 #define SYSCALL3(_name, _fmtstr, _t0, _t1, _t2, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
39 _t0 a0;_t1 a1;_t2 a2;\
40 if(strcmp(Fmt,_fmtstr)!=0)return 0;\
41 a0 = *(_t0*)Args;Args+=sizeof(_t0);\
42 a1 = *(_t1*)Args;Args+=sizeof(_t1);\
43 a2 = *(_t2*)Args;Args+=sizeof(_t2);\
47 #define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
49 if(strcmp(Fmt,_fmtstr)!=0)return 0;\
50 a0 = *(_t0*)Args;Args+=sizeof(_t0);\
51 a1 = *(_t1*)Args;Args+=sizeof(_t1);\
55 #define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
57 if(strcmp(Fmt,_fmtstr)!=0)return 0;\
58 a0 = *(_t0*)Args;Args+=sizeof(_t0);\
62 #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
63 if(strcmp(Fmt,"")!=0)return 0;\
68 int Syscall_Null(Uint*Errno, const char *Format, void *Args, int *Sizes)
73 SYSCALL1(Syscall_Exit, "i", int,
78 SYSCALL2(Syscall_Open, "si", const char *, int,
79 return VFS_Open(a0, a1|VFS_OPENFLAG_USER);
81 SYSCALL1(Syscall_Close, "i", int,
85 SYSCALL3(Syscall_Read, "iid", int, int, void *,
87 Log_Warning("Syscalls", "Read - %i < %i", Sizes[2], a1);
90 return VFS_Read(a0, a1, a2);
92 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
95 return VFS_Write(a0, a1, a2);
97 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
98 return VFS_Seek(a0, a1, a2);
100 SYSCALL1(Syscall_Tell, "i", int,
103 SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
104 return VFS_IOCtl(a0, a1, a2);
106 SYSCALL3(Syscall_FInfo, "idi", int, void *, int,
107 if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL))
109 return VFS_FInfo(a0, a1, a2);
111 SYSCALL2(Syscall_ReadDir, "id", int, char *,
114 return VFS_ReadDir(a0, a1);
116 SYSCALL5(Syscall_select, "idddd", int, fd_set *, fd_set *, fd_set *, time_t *,
117 return VFS_Select(a0, a1, a2, a3, a4, 0);
119 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
120 return VFS_OpenChild(a0, a1, a2|VFS_OPENFLAG_USER);
122 SYSCALL2(Syscall_GetACL, "id", int, void *,
123 if(Sizes[1] < sizeof(tVFS_ACL))
125 return VFS_GetACL(a0, (void*)a1);
127 SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *,
128 return VFS_Mount(a0, a1, a2, a3);
130 SYSCALL1(Syscall_Chdir, "s", const char *,
131 return VFS_ChDir(a0);
133 SYSCALL0(Syscall_Sleep,
137 SYSCALL2(Syscall_WaitTID, "id", int, int *,
138 if(Sizes[1] < sizeof(int))
140 return Threads_WaitTID(a0, a1);
142 SYSCALL1(Syscall_SetUID, "i", int,
143 if(Sizes[0] < sizeof(int)) {
144 *Errno = -EINVAL; // TODO: Better message
147 return Threads_SetUID(Errno, a0);
149 SYSCALL1(Syscall_SetGID, "i", int,
150 if(Sizes[0] < sizeof(int)) {
151 *Errno = -EINVAL; // TODO: Better message
154 return Threads_SetGID(Errno, a0);
157 SYSCALL1(Syscall_Fork, "d", int *,
158 if(Sizes[0] < sizeof(int))
160 *a0 = Threads_Fork();
164 const tSyscallHandler caSyscalls[] = {
193 const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
195 * \brief Recieve a syscall structure from the server code
197 tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
199 char formatString[Request->NParams+1];
200 char *inData = (char*)&Request->Params[Request->NParams];
204 int retValueCount = 1;
205 int retDataLen = sizeof(Uint64);
206 void *returnData[Request->NParams];
207 int argSizes[Request->NParams];
211 if( Request->CallID >= ciNumSyscalls ) {
212 Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
216 if( !caSyscalls[Request->CallID] ) {
217 Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
221 // Get size of argument list
222 for( i = 0; i < Request->NParams; i ++ )
224 argSizes[i] = Request->Params[i].Length;
225 switch(Request->Params[i].Type)
228 formatString[i] = '-';
231 formatString[i] = 'i';
232 argListLen += sizeof(Uint32);
235 formatString[i] = 'I';
236 argListLen += sizeof(Uint64);
239 formatString[i] = 'd';
240 argListLen += sizeof(void*);
242 case ARG_TYPE_STRING:
243 formatString[i] = 's';
244 argListLen += sizeof(char*);
247 return NULL; // ERROR!
250 formatString[i] = '\0';
252 LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
255 char argListData[argListLen];
257 // Build argument list
258 for( i = 0; i < Request->NParams; i ++ )
260 returnData[i] = NULL;
261 switch(Request->Params[i].Type)
266 LOG("Syscalls", "%i INT32: 0x%x", i, *(Uint32*)inData);
267 *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
268 argListLen += sizeof(Uint32);
269 inData += sizeof(Uint32);
272 LOG("Syscalls", "%i INT64: 0x%llx", i, *(Uint64*)inData);
273 *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
274 argListLen += sizeof(Uint64);
275 inData += sizeof(Uint64);
277 case ARG_TYPE_STRING:
278 LOG("Syscalls", "%i STR: '%s'", i, (char*)inData);
279 *(char**)&argListData[argListLen] = (char*)inData;
280 argListLen += sizeof(void*);
281 inData += Request->Params[i].Length;
284 // Data gets special handling, because only it can be returned to the user
285 // (ARG_TYPE_DATA is a pointer)
287 // Prepare the return values
288 if( Request->Params[i].Flags & ARG_FLAG_RETURN )
290 retDataLen += Request->Params[i].Length;
294 // Check for non-resident data
295 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
297 // Allocate and zero the buffer
298 returnData[i] = calloc(1, Request->Params[i].Length);
299 LOG("Syscalls", "%i ZDAT: %i %p", i,
300 Request->Params[i].Length, returnData[i]);
301 *(void**)&argListData[argListLen] = returnData[i];
302 argListLen += sizeof(void*);
306 returnData[i] = (void*)inData;
307 LOG("Syscalls", "%i DATA: %i %p", i,
308 Request->Params[i].Length, returnData[i]);
309 *(void**)&argListData[argListLen] = (void*)inData;
310 argListLen += sizeof(void*);
311 inData += Request->Params[i].Length;
317 retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
320 // Allocate the return
321 ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
323 ret->ClientID = Request->ClientID;
324 ret->CallID = Request->CallID;
325 ret->NParams = retValueCount;
326 inData = (char*)&ret->Params[ ret->NParams ];
328 // Static Uint64 return value
329 ret->Params[0].Type = ARG_TYPE_INT64;
330 ret->Params[0].Flags = 0;
331 ret->Params[0].Length = sizeof(Uint64);
332 *(Uint64*)inData = retVal;
333 inData += sizeof(Uint64);
335 Log_Debug("Syscalls", "Return 0x%llx", retVal);
338 for( i = 0; i < Request->NParams; i ++ )
340 if( Request->Params[i].Type != ARG_TYPE_DATA ) continue;
341 if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) ) continue;
343 ret->Params[retValueCount].Type = Request->Params[i].Type;
344 ret->Params[retValueCount].Flags = 0;
345 ret->Params[retValueCount].Length = Request->Params[i].Length;
347 LOG("Syscalls", "Ret %i: Type %i, Len %i",
348 i, Request->Params[i].Type, Request->Params[i].Length);
350 memcpy(inData, returnData[i], Request->Params[i].Length);
351 inData += Request->Params[i].Length;
353 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
354 free( returnData[i] ); // Free temp buffer from above
358 *ReturnLength = sizeof(tRequestHeader)
359 + retValueCount * sizeof(tRequestValue)