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 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);\
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);\
37 #define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
39 if(strcmp(Fmt,_fmtstr)!=0)return 0;\
40 a0 = *(_t0*)Args;Args+=sizeof(_t0);\
41 a1 = *(_t1*)Args;Args+=sizeof(_t1);\
45 #define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
47 if(strcmp(Fmt,_fmtstr)!=0)return 0;\
48 a0 = *(_t0*)Args;Args+=sizeof(_t0);\
52 #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
53 if(strcmp(Fmt,"")!=0)return 0;\
58 int Syscall_Null(Uint*Errno, const char *Format, void *Args, int *Sizes)
63 SYSCALL1(Syscall_Exit, "i", int,
68 SYSCALL2(Syscall_Open, "si", const char *, int,
69 return VFS_Open(a0, a1|VFS_OPENFLAG_USER);
71 SYSCALL1(Syscall_Close, "i", int,
75 SYSCALL3(Syscall_Read, "iid", int, int, void *,
77 Log_Warning("Syscalls", "Read - %i < %i", Sizes[2], a1);
80 return VFS_Read(a0, a1, a2);
82 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
85 return VFS_Write(a0, a1, a2);
87 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
88 return VFS_Seek(a0, a1, a2);
90 SYSCALL1(Syscall_Tell, "i", int,
93 SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
94 return VFS_IOCtl(a0, a1, a2);
96 SYSCALL3(Syscall_FInfo, "idi", int, void *, int,
97 if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL))
99 return VFS_FInfo(a0, a1, a2);
101 SYSCALL2(Syscall_ReadDir, "id", int, char *,
104 return VFS_ReadDir(a0, a1);
106 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
107 return VFS_OpenChild(NULL, a0, a1, a2|VFS_OPENFLAG_USER);
109 SYSCALL2(Syscall_GetACL, "id", int, void *,
110 if(Sizes[1] < sizeof(tVFS_ACL))
112 return VFS_GetACL(a0, (void*)a1);
114 SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *,
115 return VFS_Mount(a0, a1, a2, a3);
117 SYSCALL1(Syscall_Chdir, "s", const char *,
118 return VFS_ChDir(a0);
120 SYSCALL0(Syscall_Sleep,
124 SYSCALL2(Syscall_WaitTID, "id", int, int *,
125 if(Sizes[1] < sizeof(int))
127 return Threads_WaitTID(a0, a1);
129 SYSCALL1(Syscall_SetUID, "i", int,
130 if(Sizes[0] < sizeof(int)) {
131 *Errno = -EINVAL; // TODO: Better message
134 return Threads_SetUID(Errno, a0);
136 SYSCALL1(Syscall_SetGID, "i", int,
137 if(Sizes[0] < sizeof(int)) {
138 *Errno = -EINVAL; // TODO: Better message
141 return Threads_SetGID(Errno, a0);
144 SYSCALL0(Syscall_Fork,
145 return Threads_Fork();
148 const tSyscallHandler caSyscalls[] = {
173 const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
175 * \brief Recieve a syscall structure from the server code
177 tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
179 char formatString[Request->NParams+1];
180 char *inData = (char*)&Request->Params[Request->NParams];
184 int retValueCount = 1;
185 int retDataLen = sizeof(Uint64);
186 void *returnData[Request->NParams];
187 int argSizes[Request->NParams];
191 if( Request->CallID >= ciNumSyscalls ) {
192 Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
196 if( !caSyscalls[Request->CallID] ) {
197 Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
201 // Get size of argument list
202 for( i = 0; i < Request->NParams; i ++ )
204 argSizes[i] = Request->Params[i].Length;
205 switch(Request->Params[i].Type)
208 formatString[i] = '-';
211 formatString[i] = 'i';
212 argListLen += sizeof(Uint32);
215 formatString[i] = 'I';
216 argListLen += sizeof(Uint64);
219 formatString[i] = 'd';
220 argListLen += sizeof(void*);
222 case ARG_TYPE_STRING:
223 formatString[i] = 's';
224 argListLen += sizeof(char*);
227 return NULL; // ERROR!
230 formatString[i] = '\0';
232 LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
235 char argListData[argListLen];
237 // Build argument list
238 for( i = 0; i < Request->NParams; i ++ )
240 returnData[i] = NULL;
241 switch(Request->Params[i].Type)
246 LOG("Syscalls", "%i INT32: 0x%x", i, *(Uint32*)inData);
247 *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
248 argListLen += sizeof(Uint32);
249 inData += sizeof(Uint32);
252 LOG("Syscalls", "%i INT64: 0x%llx", i, *(Uint64*)inData);
253 *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
254 argListLen += sizeof(Uint64);
255 inData += sizeof(Uint64);
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;
264 // Data gets special handling, because only it can be returned to the user
265 // (ARG_TYPE_DATA is a pointer)
267 // Prepare the return values
268 if( Request->Params[i].Flags & ARG_FLAG_RETURN )
270 retDataLen += Request->Params[i].Length;
274 // Check for non-resident data
275 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
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*);
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;
297 retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
300 // Allocate the return
301 ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
303 ret->ClientID = Request->ClientID;
304 ret->CallID = Request->CallID;
305 ret->NParams = retValueCount;
306 inData = (char*)&ret->Params[ ret->NParams ];
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);
315 Log_Debug("Syscalls", "Return 0x%llx", retVal);
318 for( i = 0; i < Request->NParams; i ++ )
320 if( Request->Params[i].Type != ARG_TYPE_DATA ) continue;
321 if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) ) continue;
323 ret->Params[retValueCount].Type = Request->Params[i].Type;
324 ret->Params[retValueCount].Flags = 0;
325 ret->Params[retValueCount].Length = Request->Params[i].Length;
327 LOG("Syscalls", "Ret %i: Type %i, Len %i",
328 i, Request->Params[i].Type, Request->Params[i].Length);
330 memcpy(inData, returnData[i], Request->Params[i].Length);
331 inData += Request->Params[i].Length;
333 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
334 free( returnData[i] ); // Free temp buffer from above
338 *ReturnLength = sizeof(tRequestHeader)
339 + retValueCount * sizeof(tRequestValue)