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 *,
78 return VFS_Read(a0, a1, a2);
80 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
83 return VFS_Write(a0, a1, a2);
85 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
86 return VFS_Seek(a0, a1, a2);
88 SYSCALL1(Syscall_Tell, "i", int,
91 SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
92 return VFS_IOCtl(a0, a1, a2);
94 SYSCALL3(Syscall_FInfo, "idi", int, void *, int,
95 if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL))
97 return VFS_FInfo(a0, a1, a2);
99 SYSCALL2(Syscall_ReadDir, "id", int, char *,
102 return VFS_ReadDir(a0, a1);
104 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
105 return VFS_OpenChild(NULL, a0, a1, a2|VFS_OPENFLAG_USER);
107 SYSCALL2(Syscall_GetACL, "id", int, void *,
108 if(Sizes[1] < sizeof(tVFS_ACL))
110 return VFS_GetACL(a0, (void*)a1);
112 SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *,
113 return VFS_Mount(a0, a1, a2, a3);
115 SYSCALL0(Syscall_Sleep,
119 SYSCALL2(Syscall_WaitTID, "id", int, int *,
120 if(Sizes[1] < sizeof(int))
122 return Threads_WaitTID(a0, a1);
124 SYSCALL1(Syscall_SetUID, "i", int,
125 if(Sizes[0] < sizeof(int)) {
126 *Errno = -EINVAL; // TODO: Better message
129 return Threads_SetUID(Errno, a0);
131 SYSCALL1(Syscall_SetGID, "i", int,
132 if(Sizes[0] < sizeof(int)) {
133 *Errno = -EINVAL; // TODO: Better message
136 return Threads_SetGID(Errno, a0);
139 SYSCALL0(Syscall_Fork,
140 return Threads_Fork();
143 const tSyscallHandler caSyscalls[] = {
167 const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
169 * \brief Recieve a syscall structure from the server code
171 tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
173 char formatString[Request->NParams+1];
174 char *inData = (char*)&Request->Params[Request->NParams];
178 int retValueCount = 1;
179 int retDataLen = sizeof(Uint64);
180 void *returnData[Request->NParams];
181 int argSizes[Request->NParams];
185 if( Request->CallID >= ciNumSyscalls ) {
186 Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
190 if( !caSyscalls[Request->CallID] ) {
191 Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
195 // Get size of argument list
196 for( i = 0; i < Request->NParams; i ++ )
198 argSizes[i] = Request->Params[i].Length;
199 switch(Request->Params[i].Type)
202 formatString[i] = '-';
205 formatString[i] = 'i';
206 argListLen += sizeof(Uint32);
209 formatString[i] = 'I';
210 argListLen += sizeof(Uint64);
213 formatString[i] = 'd';
214 argListLen += sizeof(void*);
216 case ARG_TYPE_STRING:
217 formatString[i] = 's';
218 argListLen += sizeof(char*);
221 return NULL; // ERROR!
224 formatString[i] = '\0';
226 LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
229 char argListData[argListLen];
231 // Build argument list
232 for( i = 0; i < Request->NParams; i ++ )
234 returnData[i] = NULL;
235 switch(Request->Params[i].Type)
240 LOG("Syscalls", "%i INT32: 0x%x", i, *(Uint32*)inData);
241 *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
242 argListLen += sizeof(Uint32);
243 inData += sizeof(Uint32);
246 LOG("Syscalls", "%i INT64: 0x%llx", i, *(Uint64*)inData);
247 *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
248 argListLen += sizeof(Uint64);
249 inData += sizeof(Uint64);
251 case ARG_TYPE_STRING:
252 LOG("Syscalls", "%i STR: '%s'", i, (char*)inData);
253 *(char**)&argListData[argListLen] = (char*)inData;
254 argListLen += sizeof(void*);
255 inData += Request->Params[i].Length;
258 // Data gets special handling, because only it can be returned to the user
259 // (ARG_TYPE_DATA is a pointer)
261 // Prepare the return values
262 if( Request->Params[i].Flags & ARG_FLAG_RETURN )
264 retDataLen += Request->Params[i].Length;
268 // Check for non-resident data
269 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
271 // Allocate and zero the buffer
272 returnData[i] = calloc(1, Request->Params[i].Length);
273 LOG("Syscalls", "%i ZDAT: %i %p", i,
274 Request->Params[i].Length, returnData[i]);
275 *(void**)&argListData[argListLen] = returnData[i];
276 argListLen += sizeof(void*);
280 returnData[i] = (void*)inData;
281 LOG("Syscalls", "%i DATA: %i %p", i,
282 Request->Params[i].Length, returnData[i]);
283 *(void**)&argListData[argListLen] = (void*)inData;
284 argListLen += sizeof(void*);
285 inData += Request->Params[i].Length;
291 retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
294 // Allocate the return
295 ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
297 ret->ClientID = Request->ClientID;
298 ret->CallID = Request->CallID;
299 ret->NParams = retValueCount;
300 inData = (char*)&ret->Params[ ret->NParams ];
302 // Static Uint64 return value
303 ret->Params[0].Type = ARG_TYPE_INT64;
304 ret->Params[0].Flags = 0;
305 ret->Params[0].Length = sizeof(Uint64);
306 *(Uint64*)inData = retVal;
307 inData += sizeof(Uint64);
309 LOG("Syscalls", "Return 0x%llx", retVal);
312 for( i = 0; i < Request->NParams; i ++ )
314 if( Request->Params[i].Type != ARG_TYPE_DATA ) continue;
315 if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) ) continue;
317 ret->Params[retValueCount].Type = Request->Params[i].Type;
318 ret->Params[retValueCount].Flags = 0;
319 ret->Params[retValueCount].Length = Request->Params[i].Length;
321 LOG("Syscalls", "Ret %i: Type %i, Len %i",
322 i, Request->Params[i].Type, Request->Params[i].Length);
324 memcpy(inData, returnData[i], Request->Params[i].Length);
325 inData += Request->Params[i].Length;
327 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
328 free( returnData[i] ); // Free temp buffer from above
332 *ReturnLength = sizeof(tRequestHeader)
333 + retValueCount * sizeof(tRequestValue)