3 * - Acess kernel emulation on another OS using SDL and UDP
10 #include "../syscalls.h"
13 extern int Threads_Fork(void); // AcessNative only function
16 typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes);
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);\
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);\
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);\
51 #define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
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);\
60 #define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
62 if(strcmp(Fmt,_fmtstr)!=0)return 0;\
63 a0 = *(_t0*)Args;Args+=sizeof(_t0);\
64 LOG("SYSCALL1 '%s' %p", Fmt,(intptr_t)a0);\
68 #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
69 if(strcmp(Fmt,"")!=0)return 0;\
75 int Syscall_Null(Uint*Errno, const char *Format, void *Args, int *Sizes)
80 SYSCALL1(Syscall_Exit, "i", int,
85 SYSCALL2(Syscall_Open, "si", const char *, int,
86 return VFS_Open(a0, a1|VFS_OPENFLAG_USER);
88 SYSCALL1(Syscall_Close, "i", int,
92 SYSCALL3(Syscall_Read, "iid", int, int, void *,
94 Log_Warning("Syscalls", "Read - %i < %i", Sizes[2], a1);
97 return VFS_Read(a0, a1, a2);
99 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
102 return VFS_Write(a0, a1, a2);
104 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
105 return VFS_Seek(a0, a1, a2);
107 SYSCALL1(Syscall_Tell, "i", int,
110 SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
111 return VFS_IOCtl(a0, a1, a2);
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));
120 return VFS_FInfo(a0, a1, a2);
122 SYSCALL2(Syscall_ReadDir, "id", int, char *,
125 return VFS_ReadDir(a0, a1);
127 SYSCALL5(Syscall_select, "idddd", int, fd_set *, fd_set *, fd_set *, time_t *,
128 return VFS_Select(a0, a1, a2, a3, a4, 0);
130 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
131 return VFS_OpenChild(a0, a1, a2|VFS_OPENFLAG_USER);
133 SYSCALL2(Syscall_GetACL, "id", int, void *,
134 if(Sizes[1] < sizeof(tVFS_ACL))
136 return VFS_GetACL(a0, (void*)a1);
138 SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *,
139 return VFS_Mount(a0, a1, a2, a3);
141 SYSCALL1(Syscall_Chdir, "s", const char *,
142 return VFS_ChDir(a0);
144 SYSCALL0(Syscall_Sleep,
148 SYSCALL2(Syscall_WaitTID, "id", int, int *,
149 if(Sizes[1] < sizeof(int))
151 return Threads_WaitTID(a0, a1);
153 SYSCALL1(Syscall_SetUID, "i", int,
154 if(Sizes[0] < sizeof(int)) {
155 *Errno = -EINVAL; // TODO: Better message
158 return Threads_SetUID(Errno, a0);
160 SYSCALL1(Syscall_SetGID, "i", int,
161 if(Sizes[0] < sizeof(int)) {
162 *Errno = -EINVAL; // TODO: Better message
165 return Threads_SetGID(Errno, a0);
168 SYSCALL1(Syscall_Fork, "d", int *,
169 if(Sizes[0] < sizeof(int))
171 *a0 = Threads_Fork();
175 const tSyscallHandler caSyscalls[] = {
204 const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
206 * \brief Recieve a syscall structure from the server code
208 tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
210 char formatString[Request->NParams+1];
211 char *inData = (char*)&Request->Params[Request->NParams];
215 int retValueCount = 1;
216 int retDataLen = sizeof(Uint64);
217 void *returnData[Request->NParams];
218 int argSizes[Request->NParams];
222 if( Request->CallID >= ciNumSyscalls ) {
223 Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
227 if( !caSyscalls[Request->CallID] ) {
228 Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
232 // Get size of argument list
233 for( i = 0; i < Request->NParams; i ++ )
235 argSizes[i] = Request->Params[i].Length;
236 switch(Request->Params[i].Type)
239 formatString[i] = '-';
242 formatString[i] = 'i';
243 argListLen += sizeof(Uint32);
246 formatString[i] = 'I';
247 argListLen += sizeof(Uint64);
250 formatString[i] = 'd';
251 argListLen += sizeof(void*);
253 case ARG_TYPE_STRING:
254 formatString[i] = 's';
255 argListLen += sizeof(char*);
258 return NULL; // ERROR!
261 formatString[i] = '\0';
263 LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
266 char argListData[argListLen];
268 // Build argument list
269 for( i = 0; i < Request->NParams; i ++ )
271 returnData[i] = NULL;
272 switch(Request->Params[i].Type)
277 LOG("%i INT32: 0x%x", i, *(Uint32*)inData);
278 *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
279 argListLen += sizeof(Uint32);
280 inData += sizeof(Uint32);
283 LOG("%i INT64: 0x%llx", i, *(Uint64*)inData);
284 *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
285 argListLen += sizeof(Uint64);
286 inData += sizeof(Uint64);
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;
295 // Data gets special handling, because only it can be returned to the user
296 // (ARG_TYPE_DATA is a pointer)
298 // Prepare the return values
299 if( Request->Params[i].Flags & ARG_FLAG_RETURN )
301 retDataLen += Request->Params[i].Length;
305 // Check for non-resident data
306 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
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*);
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;
328 retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
331 // Allocate the return
332 ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
334 ret->ClientID = Request->ClientID;
335 ret->CallID = Request->CallID;
336 ret->NParams = retValueCount;
337 inData = (char*)&ret->Params[ ret->NParams ];
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);
346 Log_Debug("Syscalls", "Return 0x%llx", retVal);
349 for( i = 0; i < Request->NParams; i ++ )
351 if( Request->Params[i].Type != ARG_TYPE_DATA ) continue;
352 if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) ) continue;
354 ret->Params[retValueCount].Type = Request->Params[i].Type;
355 ret->Params[retValueCount].Flags = 0;
356 ret->Params[retValueCount].Length = Request->Params[i].Length;
358 LOG("Syscalls", "Ret %i: Type %i, Len %i",
359 i, Request->Params[i].Type, Request->Params[i].Length);
361 memcpy(inData, returnData[i], Request->Params[i].Length);
362 inData += Request->Params[i].Length;
364 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
365 free( returnData[i] ); // Free temp buffer from above
369 *ReturnLength = sizeof(tRequestHeader)
370 + retValueCount * sizeof(tRequestValue)