AcessNative - Huge changes, cleaning up and getting it to work
[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                 return -1;
78         return VFS_Read(a0, a1, a2);
79 );
80 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
81         if( Sizes[2] < a1 )
82                 return -1;
83         return VFS_Write(a0, a1, a2);
84 );
85 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
86         return VFS_Seek(a0, a1, a2);
87 );
88 SYSCALL1(Syscall_Tell, "i", int,
89         return VFS_Tell(a0);
90 );
91 SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
92         return VFS_IOCtl(a0, a1, a2);
93 );
94 SYSCALL3(Syscall_FInfo, "idi", int, void *, int,
95         if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL))
96                 return -1;
97         return VFS_FInfo(a0, a1, a2);
98 );
99 SYSCALL2(Syscall_ReadDir, "id", int, char *,
100         if(Sizes[1] < 255)
101                 return -1;
102         return VFS_ReadDir(a0, a1);
103 );
104 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
105         return VFS_OpenChild(NULL, a0, a1, a2|VFS_OPENFLAG_USER);
106 );
107 SYSCALL2(Syscall_GetACL, "id", int, void *,
108         if(Sizes[1] < sizeof(tVFS_ACL))
109                 return -1;
110         return VFS_GetACL(a0, (void*)a1);
111 );
112 SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *,
113         return VFS_Mount(a0, a1, a2, a3);
114 );
115 SYSCALL0(Syscall_Sleep,
116         Threads_Sleep();
117         return 0;
118 );
119 SYSCALL2(Syscall_WaitTID, "id", int, int *,
120         if(Sizes[1] < sizeof(int))
121                 return -1;
122         return Threads_WaitTID(a0, a1);
123 );
124 SYSCALL1(Syscall_SetUID, "i", int,
125         if(Sizes[0] < sizeof(int)) {
126                 *Errno = -EINVAL;       // TODO: Better message
127                 return -1;
128         }
129         return Threads_SetUID(Errno, a0);
130 );
131 SYSCALL1(Syscall_SetGID, "i", int,
132         if(Sizes[0] < sizeof(int)) {
133                 *Errno = -EINVAL;       // TODO: Better message
134                 return -1;
135         }
136         return Threads_SetGID(Errno, a0);
137 );
138
139 SYSCALL0(Syscall_Fork,
140         return Threads_Fork();
141 );
142
143 const tSyscallHandler   caSyscalls[] = {
144         Syscall_Null,
145         Syscall_Exit,
146         Syscall_Open,
147         Syscall_Close,
148         Syscall_Read,
149         Syscall_Write,
150         Syscall_Seek,
151         Syscall_Tell,
152         Syscall_IOCtl,
153         Syscall_FInfo,
154         Syscall_ReadDir,
155         Syscall_OpenChild,
156         Syscall_GetACL,
157         Syscall_Mount,
158         NULL,   // SYS_REOPEN
159         
160         Syscall_WaitTID,
161         Syscall_SetUID,
162         Syscall_SetGID,
163         
164         Syscall_Sleep,
165         Syscall_Fork
166 };
167 const int       ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
168 /**
169  * \brief Recieve a syscall structure from the server code
170  */
171 tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
172 {
173         char    formatString[Request->NParams+1];
174         char    *inData = (char*)&Request->Params[Request->NParams];
175          int    argListLen = 0;
176          int    i, retVal;
177         tRequestHeader  *ret;
178          int    retValueCount = 1;
179          int    retDataLen = sizeof(Uint64);
180         void    *returnData[Request->NParams];
181          int    argSizes[Request->NParams];
182         Uint    ret_errno = 0;
183         
184         // Sanity check
185         if( Request->CallID >= ciNumSyscalls ) {
186                 Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
187                 return NULL;
188         }
189         
190         if( !caSyscalls[Request->CallID] ) {
191                 Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
192                 return NULL;
193         }
194         
195         // Get size of argument list
196         for( i = 0; i < Request->NParams; i ++ )
197         {
198                 argSizes[i] = Request->Params[i].Length;
199                 switch(Request->Params[i].Type)
200                 {
201                 case ARG_TYPE_VOID:
202                         formatString[i] = '-';
203                         break;
204                 case ARG_TYPE_INT32:
205                         formatString[i] = 'i';
206                         argListLen += sizeof(Uint32);
207                         break;
208                 case ARG_TYPE_INT64:
209                         formatString[i] = 'I';
210                         argListLen += sizeof(Uint64);
211                         break;
212                 case ARG_TYPE_DATA:
213                         formatString[i] = 'd';
214                         argListLen += sizeof(void*);
215                         break;
216                 case ARG_TYPE_STRING:
217                         formatString[i] = 's';
218                         argListLen += sizeof(char*);
219                         break;
220                 default:
221                         return NULL;    // ERROR!
222                 }
223         }
224         formatString[i] = '\0';
225         
226         LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
227         
228         {
229                 char    argListData[argListLen];
230                 argListLen = 0;
231                 // Build argument list
232                 for( i = 0; i < Request->NParams; i ++ )
233                 {
234                         returnData[i] = NULL;
235                         switch(Request->Params[i].Type)
236                         {
237                         case ARG_TYPE_VOID:
238                                 break;
239                         case ARG_TYPE_INT32:
240                                 LOG("Syscalls", "%i INT32: 0x%x", i, *(Uint32*)inData);
241                                 *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
242                                 argListLen += sizeof(Uint32);
243                                 inData += sizeof(Uint32);
244                                 break;
245                         case ARG_TYPE_INT64:
246                                 LOG("Syscalls", "%i INT64: 0x%llx", i, *(Uint64*)inData);
247                                 *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
248                                 argListLen += sizeof(Uint64);
249                                 inData += sizeof(Uint64);
250                                 break;
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;
256                                 break;
257                         
258                         // Data gets special handling, because only it can be returned to the user
259                         // (ARG_TYPE_DATA is a pointer)
260                         case ARG_TYPE_DATA:
261                                 // Prepare the return values
262                                 if( Request->Params[i].Flags & ARG_FLAG_RETURN )
263                                 {
264                                         retDataLen += Request->Params[i].Length;
265                                         retValueCount ++;
266                                 }
267                                 
268                                 // Check for non-resident data
269                                 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
270                                 {
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*);
277                                 }
278                                 else
279                                 {
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;
286                                 }
287                                 break;
288                         }
289                 }
290                 
291                 retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
292         }
293         
294         // Allocate the return
295         ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
296                 + retDataLen);
297         ret->ClientID = Request->ClientID;
298         ret->CallID = Request->CallID;
299         ret->NParams = retValueCount;
300         inData = (char*)&ret->Params[ ret->NParams ];
301         
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);
308         
309         LOG("Syscalls", "Return 0x%llx", retVal);
310         
311         retValueCount = 1;
312         for( i = 0; i < Request->NParams; i ++ )
313         {
314                 if( Request->Params[i].Type != ARG_TYPE_DATA )  continue;
315                 if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) )     continue;
316                 
317                 ret->Params[retValueCount].Type = Request->Params[i].Type;
318                 ret->Params[retValueCount].Flags = 0;
319                 ret->Params[retValueCount].Length = Request->Params[i].Length;
320                 
321                 LOG("Syscalls", "Ret %i: Type %i, Len %i",
322                         i, Request->Params[i].Type, Request->Params[i].Length);
323                 
324                 memcpy(inData, returnData[i], Request->Params[i].Length);
325                 inData += Request->Params[i].Length;
326                 
327                 if( Request->Params[i].Flags & ARG_FLAG_ZEROED )
328                         free( returnData[i] );  // Free temp buffer from above
329                 retValueCount ++;
330         }
331         
332         *ReturnLength = sizeof(tRequestHeader)
333                 + retValueCount * sizeof(tRequestValue)
334                 + retDataLen;
335         
336         return ret;
337 }

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