X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=AcessNative%2Fld-acess_src%2Fsyscalls.c;h=34132c62a67b8fcff968252965992e3a8829f32a;hb=336ba8b200f6ca1d5d25f09a9d7ddac2e59b26d9;hp=9bdde9b27e7dfade28832df5ced2e5841652975e;hpb=02cbaac1233be9c5228973a787431fa5e0aa178e;p=tpg%2Facess2.git diff --git a/AcessNative/ld-acess_src/syscalls.c b/AcessNative/ld-acess_src/syscalls.c index 9bdde9b2..34132c62 100644 --- a/AcessNative/ld-acess_src/syscalls.c +++ b/AcessNative/ld-acess_src/syscalls.c @@ -1,41 +1,57 @@ /* */ -#include "../../Usermode/include/acess/sys.h" +#define DONT_INCLUDE_SYSCALL_NAMES 1 #include "common.h" #include #include #include #include #include +#include +#include +#ifndef __WIN32__ +# include // posix_spawn +#endif #include "request.h" -#include "../syscalls.h" + +#define assert(cnd) do{ \ + if( !(cnd) ) { \ + fprintf(stderr, "%s:%i - assert failed - " #cnd"\n", __FILE__, __LINE__);\ + exit(-1); \ + } \ +}while(0) + +#define MAX_FPS 16 // === Types === // === IMPORTS === +// === GLOBALS === +FILE *gaSyscall_LocalFPs[MAX_FPS]; + // === CODE === -const char *ReadEntry(tOutValue **OutDest, tInValue **InDest, - int *Direction, const char *ArgTypes, va_list Args) +const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args) { - uint64_t val64, *ptr64; - uint32_t val32, *ptr32; + uint64_t val64; + uint32_t val32; int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out char *str; int len; - + // Eat whitespace while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++; if( *ArgTypes == '\0' ) return ArgTypes; +// DEBUG("ArgTypes = '%s'", ArgTypes); + // Get direction switch(*ArgTypes) { + default: // Defaults to output case '>': direction = 1; break; case '<': direction = 2; break; case '?': direction = 3; break; - default: - return NULL; } ArgTypes ++; @@ -43,99 +59,90 @@ const char *ReadEntry(tOutValue **OutDest, tInValue **InDest, while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++; if( *ArgTypes == '\0' ) return ArgTypes; - // Internal helper macro - #define MAKE_OUT(_dest,_typeChar,_typeName,_value) do{if((_dest)){\ - *(_dest) = (tOutValue*)malloc(sizeof(tOutValue)+sizeof(_typeName));\ - (*(_dest))->Type=(_typeChar);(*(_dest))->Length=sizeof(_typeName);\ - *(_typeName*)((*(_dest))->Data) = (_value);\ - }}while(0) - #define MAKE_IN(_dest,_typeChar,_typeName,_value) do{if((_dest)){\ - *(_dest) = (tInValue*)malloc(sizeof(tInValue));\ - (*(_dest))->Type=(_typeChar);(*(_dest))->Length=sizeof(_typeName);\ - (*(_dest))->Data = (_value);\ - }}while(0) - // Get type switch(*ArgTypes) { - case 'i': // 32-bit integer - // Input? - if( direction & 2 ) - { - ptr32 = va_arg(Args, uint32_t*); - MAKE_IN(InDest, 'i', uint32_t*, ptr32); - if( direction & 1 ) - MAKE_OUT(OutDest, 'i', uint32_t, *ptr32); - } - else - { - val32 = va_arg(Args, uint32_t); - MAKE_OUT(OutDest, 'i', uint32_t, val32); + // 32-bit integer + case 'i': + + if( direction != 1 ) { + Warning("ReadEntry: Recieving an integer is not defined"); + return NULL; } + + val32 = va_arg(*Args, uint32_t); + + Dest->Type = ARG_TYPE_INT32; + Dest->Length = sizeof(uint32_t); + Dest->Flags = 0; + + if( DataDest ) + *(uint32_t*)DataDest = val32; break; - case 'I': // 64-bit integer - // Input? - if( direction & 2 ) - { - ptr64 = va_arg(Args, uint64_t*); - MAKE_IN(InDest, 'I', uint64_t*, ptr64); - if( direction & 1 ) - MAKE_OUT(OutDest, 'I', uint64_t, *ptr64); - } - else - { - val64 = va_arg(Args, uint64_t); - MAKE_OUT(OutDest, 'I', uint64_t, val64); + // 64-bit integer + case 'I': + + if( direction != 1 ) { + fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n"); + return NULL; } + + val64 = va_arg(*Args, uint64_t); + + Dest->Type = ARG_TYPE_INT64; + Dest->Length = sizeof(uint64_t); + Dest->Flags = 0; + if( DataDest ) + *(uint64_t*)DataDest = val64; break; + // String case 's': // Input string makes no sense! - if( direction & 2 ) { - fprintf(stderr, "ReadEntry: Incoming string is not defined\n"); + if( direction != 1 ) { + fprintf(stderr, "ReadEntry: Recieving a string is not defined\n"); return NULL; } - str = va_arg(Args, char*); - if( OutDest ) + str = va_arg(*Args, char*); + + Dest->Type = ARG_TYPE_STRING; + Dest->Length = strlen(str) + 1; + Dest->Flags = 0; + + if( DataDest ) { - int len = strlen(str) + 1; - *OutDest = malloc( sizeof(tOutValue) + len ); - (*OutDest)->Type = 's'; - (*OutDest)->Length = len; - memcpy((*OutDest)->Data, str, len); + memcpy(DataDest, str, Dest->Length); } break; - + // Data (special handling) case 'd': - len = va_arg(Args, int); - str = va_arg(Args, char*); + len = va_arg(*Args, size_t); + str = va_arg(*Args, char*); - // Input ? - if( (direction & 2) && InDest ) - { - *InDest = (tInValue*)malloc( sizeof(tInValue) ); - (*InDest)->Type = 'd'; - (*InDest)->Length = len; - (*InDest)->Data = str; - } + // Save the pointer for later + if( PtrDest ) *PtrDest = str; - // Output ? - if( (direction & 1) && InDest ) + // Create parameter block + Dest->Type = ARG_TYPE_DATA; + Dest->Length = str ? len : 0; + Dest->Flags = 0; + if( direction & 2 ) + Dest->Flags |= ARG_FLAG_RETURN; + + // Has data? + if( direction & 1 ) { - *OutDest = (tOutValue*)malloc( sizeof(tOutValue) + len ); - (*OutDest)->Type = 'd'; - (*OutDest)->Length = len; - memcpy((*OutDest)->Data, str, len); + if( DataDest && str ) + memcpy(DataDest, str, len); } + else + Dest->Flags |= ARG_FLAG_ZEROED; break; default: return NULL; } ArgTypes ++; - #undef MAKE_ASSIGN - - *Direction = direction; return ArgTypes; } @@ -153,178 +160,191 @@ const char *ReadEntry(tOutValue **OutDest, tInValue **InDest, * ?d: Bi-directional buffer (Preceded by valid size), buffer contents * are returned */ -void _Syscall(int SyscallID, const char *ArgTypes, ...) +uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...) { va_list args; - int outCount; - int inCount; + int paramCount, dataLength; + int retCount = 2, retLength = sizeof(uint64_t) + sizeof(uint32_t); + void **retPtrs; // Pointers to return buffers const char *str; - tOutValue **output; - tInValue **input; + tRequestHeader *req; + void *dataPtr; + uint64_t retValue; + int i; + + // DEBUG! +// printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params)); +// printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags)); +// printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length)); // Get data size va_start(args, ArgTypes); str = ArgTypes; - outCount = 0; - inCount = 0; + paramCount = 0; + dataLength = 0; while(*str) { - int dir; + tRequestValue tmpVal; - str = ReadEntry(NULL, NULL, &dir, str, args); + str = ReadEntry(&tmpVal, NULL, NULL, str, &args); if( !str ) { fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID); exit(127); } + paramCount ++; + if( !(tmpVal.Flags & ARG_FLAG_ZEROED) ) + dataLength += tmpVal.Length; - // Out! - if( dir & 1 ) outCount ++; - - // and.. In! - if( dir & 2 ) inCount ++; + if( tmpVal.Flags & ARG_FLAG_RETURN ) { + retLength += tmpVal.Length; + retCount ++; + } } va_end(args); + dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue); + retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue); + // Allocate buffers - output = malloc( outCount*sizeof(tOutValue*) ); - input = malloc( inCount*sizeof(tInValue*) ); + retPtrs = malloc( sizeof(void*) * (retCount+1) ); + if( dataLength > retLength) + req = malloc( dataLength ); + else + req = malloc( retLength ); + req->ClientID = 0; //< Filled later + req->CallID = SyscallID; + req->NParams = paramCount; + req->MessageLength = dataLength; + dataPtr = &req->Params[paramCount]; // Fill `output` and `input` va_start(args, ArgTypes); str = ArgTypes; // - re-zero so they can be used as indicies - outCount = 0; - inCount = 0; + paramCount = 0; + retCount = 0; while(*str) - { - tOutValue *outParam; - tInValue *inParam; - int dir; - - str = ReadEntry(&outParam, &inParam, &dir, str, args); + { + str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args); if( !str ) break; - if( dir & 1 ) - output[outCount++] = outParam; - if( dir & 2 ) - input[inCount++] = inParam; + if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) ) + dataPtr += req->Params[paramCount].Length; + if( req->Params[paramCount].Flags & ARG_FLAG_RETURN ) + retCount ++; + + paramCount ++; } va_end(args); - // Send syscall request - if( SendRequest(SyscallID, outCount, output, inCount, input) ) { + // --- Send syscall request + if( SendRequest(req, dataLength, retLength) < 0 ) { fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID); exit(127); } - // Clean up - while(outCount--) free(output[outCount]); - free(output); - while(inCount--) free(input[inCount]); - free(input); -} - -// --- VFS Calls -int open(const char *Path, int Flags) { - int ret = 0; - _Syscall(SYS_OPEN, "s >i", &ret, Path, Flags); - return ret; -} - -void close(int FD) { - _Syscall(SYS_CLOSE, ">i", FD); -} - -size_t read(int FD, size_t Bytes, void *Dest) { - int ret = 0; - _Syscall(SYS_READ, "i >i Params[req->NParams]; + assert(req->NParams >= 2); + // return + assert(req->Params[0].Type == ARG_TYPE_INT64); + assert(req->Params[0].Length == sizeof(uint64_t)); + retValue = *(uint64_t*)dataPtr; + dataPtr += sizeof(uint64_t); + // errno + assert(req->Params[1].Type == ARG_TYPE_INT32); + assert(req->Params[1].Length == sizeof(uint32_t)); + acess__errno = *(uint32_t*)dataPtr; + dataPtr += sizeof(uint32_t); + + // Write changes to buffers + if( req->NParams - 2 != retCount ) { + fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n", + req->NParams, retCount, SyscallID); + exit(127); + } + retCount = 0; + for( i = 2; i < req->NParams; i ++ ) + { + #if 0 + int j; + printf("Return Data %i: (%i)", i, req->Params[i].Length); + for( j = 0; j < req->Params[i].Length; j ++ ) + printf(" %02x", ((uint8_t*)dataPtr)[j]); + printf("\n"); + #endif + assert( req->Params[i].Type == ARG_TYPE_DATA ); + memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length ); + dataPtr += req->Params[i].Length; + } + + free( req ); + free( retPtrs ); + + SYSTRACE(": %i 0x%llx", SyscallID, retValue); + + return retValue; } -size_t write(int FD, size_t Bytes, void *Src) { - int ret = 0; - _Syscall(SYS_WRITE, "i >i >d", &ret, FD, Bytes, Bytes, Src); - return ret; -} -int seek(int FD, int64_t Ofs, int Dir) { - int ret = 0; - _Syscall(SYS_SEEK, "i >I >i", &ret, FD, Ofs, Dir); - return ret; +int native_open(const char *Path, int Flags) +{ + int ret; + for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ; + if(ret == MAX_FPS) return -1; + // TODO: Handle directories + gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+"); + if(!gaSyscall_LocalFPs[ret]) return -1; + return ret; } -uint64_t tell(int FD) { - uint64_t ret; - _Syscall(SYS_TELL, "i", &ret, FD); - return ret; +void native_close(int FD) +{ + fclose( gaSyscall_LocalFPs[FD] ); + gaSyscall_LocalFPs[FD] = NULL; } -int ioctl(int fd, int id, void *data) { - int ret = 0; - // NOTE: 1024 byte size is a hack - _Syscall(SYS_IOCTL, "i >i ?d", &ret, fd, id, 1024, data); - return ret; -} -int finfo(int fd, t_sysFInfo *info, int maxacls) { - int ret = 0; - _Syscall(SYS_FINFO, "i i", - &ret, fd, - sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info, - maxacls); - return ret; +size_t native_read(int FD, void *Dest, size_t Bytes) +{ + return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] ); } -int readdir(int fd, char *dest) { - int ret = 0; - _Syscall(SYS_READDIR, "i i >s >i", &ret, fd, name, flags); - return ret; +int native_seek(int FD, int64_t Ofs, int Dir) +{ + if(Dir == 0) + return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR ); + else if(Dir > 0) + return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET ); + else + return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END ); } -int _SysGetACL(int fd, t_sysACL *dest) { - int ret = 0; - _Syscall(SYS_GETACL, "i s >s >s >s", &ret, Device, Directory, Type, Options); +int native_execve(const char *filename, const char *const argv[], const char *const envp[]) +{ + int ret; + ret = execve(filename, (void*)argv, (void*)envp); + perror("native_execve"); return ret; } - -// --- Error Handler -int _SysSetFaultHandler(int (*Handler)(int)) { - return 0; -} - - -// === Symbol List === -#define DEFSYM(name) {#name, name} -const tSym caBuiltinSymbols[] = { - {"_exit", exit}, +int native_spawn(const char *filename, const char *const argv[], const char *const envp[]) +{ + int rv; - DEFSYM(open), - DEFSYM(close), - DEFSYM(read), - DEFSYM(write), - DEFSYM(seek), - DEFSYM(tell), - DEFSYM(ioctl), - DEFSYM(finfo), - DEFSYM(readdir), - DEFSYM(_SysOpenChild), - DEFSYM(_SysGetACL), - DEFSYM(_SysMount), + #if __WIN32__ + rv = _spawnve(_P_NOWAIT, filename, argv, envp); + #else + rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp); + #endif - {"_SysSetFaultHandler", _SysSetFaultHandler} -}; - -const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]); - + return rv; +}