3 #define DONT_INCLUDE_SYSCALL_NAMES 1
13 # include <spawn.h> // posix_spawn
17 #define assert(cnd) do{ \
19 fprintf(stderr, "%s:%i - assert failed - " #cnd"\n", __FILE__, __LINE__);\
29 extern int gbSyscallDebugEnabled;
32 FILE *gaSyscall_LocalFPs[MAX_FPS];
35 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
39 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
44 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
45 if( *ArgTypes == '\0' ) return ArgTypes;
47 // DEBUG("ArgTypes = '%s'", ArgTypes);
52 default: // Defaults to output
53 case '>': direction = 1; break;
54 case '<': direction = 2; break;
55 case '?': direction = 3; break;
60 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
61 if( *ArgTypes == '\0' ) return ArgTypes;
69 if( direction != 1 ) {
70 Warning("ReadEntry: Recieving an integer is not defined");
74 val32 = va_arg(*Args, uint32_t);
76 Dest->Type = ARG_TYPE_INT32;
77 Dest->Length = sizeof(uint32_t);
81 *(uint32_t*)DataDest = val32;
86 if( direction != 1 ) {
87 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
91 val64 = va_arg(*Args, uint64_t);
93 Dest->Type = ARG_TYPE_INT64;
94 Dest->Length = sizeof(uint64_t);
97 *(uint64_t*)DataDest = val64;
101 // Input string makes no sense!
102 if( direction != 1 ) {
103 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
107 str = va_arg(*Args, char*);
109 Dest->Type = ARG_TYPE_STRING;
110 Dest->Length = strlen(str) + 1;
115 memcpy(DataDest, str, Dest->Length);
118 // Data (special handling)
120 len = va_arg(*Args, size_t);
121 str = va_arg(*Args, char*);
123 // Save the pointer for later
124 if( PtrDest ) *PtrDest = str;
126 // Create parameter block
127 Dest->Type = ARG_TYPE_DATA;
128 Dest->Length = str ? len : 0;
131 Dest->Flags |= ARG_FLAG_RETURN;
136 if( DataDest && str )
137 memcpy(DataDest, str, len);
140 Dest->Flags |= ARG_FLAG_ZEROED;
154 * Whitespace is ignored
155 * >i: Input Integer (32-bits)
156 * >I: Input Long Integer (64-bits)
158 * >d: Input Buffer (Preceded by valid size)
159 * <I: Output long integer
160 * <d: Output Buffer (Preceded by valid size)
161 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
164 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
167 int paramCount, dataLength;
168 int retCount = 2, retLength = sizeof(uint64_t) + sizeof(uint32_t);
169 void **retPtrs; // Pointers to return buffers
177 // printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
178 // printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags));
179 // printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length));
182 va_start(args, ArgTypes);
188 tRequestValue tmpVal;
190 str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
192 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
196 if( !(tmpVal.Flags & ARG_FLAG_ZEROED) )
197 dataLength += tmpVal.Length;
199 if( tmpVal.Flags & ARG_FLAG_RETURN ) {
200 retLength += tmpVal.Length;
206 dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue);
207 retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue);
210 retPtrs = malloc( sizeof(void*) * (retCount+1) );
211 if( dataLength > retLength)
212 req = malloc( dataLength );
214 req = malloc( retLength );
215 req->ClientID = 0; //< Filled later
216 req->CallID = SyscallID;
217 req->NParams = paramCount;
218 req->MessageLength = dataLength;
219 dataPtr = &req->Params[paramCount];
221 // Fill `output` and `input`
222 va_start(args, ArgTypes);
224 // - re-zero so they can be used as indicies
229 str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
232 if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
233 dataPtr += req->Params[paramCount].Length;
234 if( req->Params[paramCount].Flags & ARG_FLAG_RETURN )
241 // --- Send syscall request
242 if( SendRequest(req, dataLength, retLength) < 0 ) {
243 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
247 dataPtr = (void*)&req->Params[req->NParams];
248 assert(req->NParams >= 2);
250 assert(req->Params[0].Type == ARG_TYPE_INT64);
251 assert(req->Params[0].Length == sizeof(uint64_t));
252 retValue = *(uint64_t*)dataPtr;
253 dataPtr += sizeof(uint64_t);
255 assert(req->Params[1].Type == ARG_TYPE_INT32);
256 assert(req->Params[1].Length == sizeof(uint32_t));
257 acess__errno = *(uint32_t*)dataPtr;
258 dataPtr += sizeof(uint32_t);
260 // Write changes to buffers
261 if( req->NParams - 2 != retCount ) {
262 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
263 req->NParams, retCount, SyscallID);
267 for( i = 2; i < req->NParams; i ++ )
271 printf("Return Data %i: (%i)", i, req->Params[i].Length);
272 for( j = 0; j < req->Params[i].Length; j ++ )
273 printf(" %02x", ((uint8_t*)dataPtr)[j]);
276 assert( req->Params[i].Type == ARG_TYPE_DATA );
277 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
278 dataPtr += req->Params[i].Length;
284 if( gbSyscallDebugEnabled ) {
285 SYSTRACE(": %i 0x%llx", SyscallID, retValue);
292 int native_open(const char *Path, int Flags)
295 for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
296 if(ret == MAX_FPS) return -1;
297 // TODO: Handle directories
298 gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
299 if(!gaSyscall_LocalFPs[ret]) return -1;
303 void native_close(int FD)
305 fclose( gaSyscall_LocalFPs[FD] );
306 gaSyscall_LocalFPs[FD] = NULL;
309 size_t native_read(int FD, void *Dest, size_t Bytes)
311 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
314 size_t native_write(int FD, const void *Src, size_t Bytes)
316 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
319 int native_seek(int FD, int64_t Ofs, int Dir)
322 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
324 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
326 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
329 uint64_t native_tell(int FD)
331 return ftell( gaSyscall_LocalFPs[FD] );
334 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
337 ret = execve(filename, (void*)argv, (void*)envp);
338 perror("native_execve");
342 int native_spawn(const char *filename, const char *const argv[], const char *const envp[])
346 fprintf(stderr, "native_spawn('%s')\n", filename);
349 rv = _spawnve(_P_NOWAIT, filename, argv, envp);
351 rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp);
355 perror("native_spawn");