X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Facesskernel_src%2Fsyscalls.c;h=b41e3544fa354f38c67e2be3679b48e02f4e705e;hb=1474ce5c1ba164bbccfefa411883805d12a0dc62;hp=85a82325a5d11a9702c605095380c89d61ef9674;hpb=d599a063b3b453b705925c58180c93e1f9cb3d79;p=tpg%2Facess2.git diff --git a/AcessNative/acesskernel_src/syscalls.c b/AcessNative/acesskernel_src/syscalls.c index 85a82325..b41e3544 100644 --- a/AcessNative/acesskernel_src/syscalls.c +++ b/AcessNative/acesskernel_src/syscalls.c @@ -5,55 +5,190 @@ * Syscall Distribution */ #include +#include #include "../syscalls.h" +// === IMPORTS === +extern int Threads_Fork(void); // AcessNative only function + // === TYPES === -typedef int (*tSyscallHandler)(const char *Format, void *Args); +typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes); // === MACROS === -#define SYSCALL3(_name, _call, _fmtstr, _t1, _t2, _t3) int _name(const char *fmt,void*args){\ - _t1 a1;_t2 a2;_t3 a3;\ - if(strcmp(fmt,_fmtstr)!=0)return 0;\ - a1 = *(_t1*)args;args+=sizeof(_t1);\ - a2 = *(_t2*)args;args+=sizeof(_t2);\ - a3 = *(_t3*)args;args+=sizeof(_t3);\ - return _call(a1,a2,a3);\ +#define SYSCALL5(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ + _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;\ + if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + a0 = *(_t0*)Args;Args+=sizeof(_t0);\ + a1 = *(_t1*)Args;Args+=sizeof(_t1);\ + a2 = *(_t2*)Args;Args+=sizeof(_t2);\ + a3 = *(_t3*)Args;Args+=sizeof(_t3);\ + a4 = *(_t4*)Args;Args+=sizeof(_t4);\ + _call\ +} +#define SYSCALL4(_name, _fmtstr, _t0, _t1, _t2, _t3, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ + _t0 a0;_t1 a1;_t2 a2;_t3 a3;\ + if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + a0 = *(_t0*)Args;Args+=sizeof(_t0);\ + a1 = *(_t1*)Args;Args+=sizeof(_t1);\ + a2 = *(_t2*)Args;Args+=sizeof(_t2);\ + a3 = *(_t3*)Args;Args+=sizeof(_t3);\ + _call\ +} + +#define SYSCALL3(_name, _fmtstr, _t0, _t1, _t2, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ + _t0 a0;_t1 a1;_t2 a2;\ + if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + a0 = *(_t0*)Args;Args+=sizeof(_t0);\ + a1 = *(_t1*)Args;Args+=sizeof(_t1);\ + a2 = *(_t2*)Args;Args+=sizeof(_t2);\ + _call\ +} + +#define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\ + _t0 a0;_t1 a1;\ + if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + a0 = *(_t0*)Args;Args+=sizeof(_t0);\ + a1 = *(_t1*)Args;Args+=sizeof(_t1);\ + _call;\ } -#define SYSCALL2(_name, _call, _fmtstr, _t1, _t2) int _name(const char *fmt,void*args){\ - _t1 a1;_t2 a2;\ - if(strcmp(fmt,_fmtstr)!=0)return 0;\ - a1 = *(_t1*)args;args+=sizeof(_t1);\ - a2 = *(_t2*)args;args+=sizeof(_t2);\ - return _call(a1,a2);\ +#define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\ + _t0 a0;\ + if(strcmp(Fmt,_fmtstr)!=0)return 0;\ + a0 = *(_t0*)Args;Args+=sizeof(_t0);\ + _call;\ } -#define SYSCALL1V(_name, _call, _fmtstr, _t1) int _name(const char *fmt, void*args){\ - _t1 a1;\ - if(strcmp(fmt,_fmtstr)!=0)return 0;\ - a1 = *(_t1*)args;args+=sizeof(_t1);\ - _call(a1);\ - return 0;\ +#define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\ + if(strcmp(Fmt,"")!=0)return 0;\ + _call;\ } // === CODE === -int Syscall_Null(const char *Format, void *Args) +int Syscall_Null(Uint*Errno, const char *Format, void *Args, int *Sizes) { return 0; } -SYSCALL2(Syscall_Open, VFS_Open, "si", const char *, int); -SYSCALL1V(Syscall_Close, VFS_Close, "i", int); -SYSCALL3(Syscall_Read, VFS_Read, "iid", int, int, void *); -SYSCALL3(Syscall_Write, VFS_Write, "iid", int, int, const void *); +SYSCALL1(Syscall_Exit, "i", int, + Threads_Exit(0, a0); + return 0; +); +SYSCALL2(Syscall_Open, "si", const char *, int, + return VFS_Open(a0, a1|VFS_OPENFLAG_USER); +); +SYSCALL1(Syscall_Close, "i", int, + VFS_Close(a0); + return 0; +); +SYSCALL3(Syscall_Read, "iid", int, int, void *, + if( Sizes[2] < a1 ) { + Log_Warning("Syscalls", "Read - %i < %i", Sizes[2], a1); + return -1; + } + return VFS_Read(a0, a1, a2); +); +SYSCALL3(Syscall_Write, "iid", int, int, const void *, + if( Sizes[2] < a1 ) + return -1; + return VFS_Write(a0, a1, a2); +); +SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int, + return VFS_Seek(a0, a1, a2); +); +SYSCALL1(Syscall_Tell, "i", int, + return VFS_Tell(a0); +); +SYSCALL3(Syscall_IOCtl, "iid", int, int, void *, + return VFS_IOCtl(a0, a1, a2); +); +SYSCALL3(Syscall_FInfo, "idi", int, void *, int, + if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL)) + return -1; + return VFS_FInfo(a0, a1, a2); +); +SYSCALL2(Syscall_ReadDir, "id", int, char *, + if(Sizes[1] < 255) + return -1; + return VFS_ReadDir(a0, a1); +); +SYSCALL5(Syscall_select, "idddd", int, fd_set *, fd_set *, fd_set *, time_t *, + return VFS_Select(a0, a1, a2, a3, a4, 0); +); +SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int, + return VFS_OpenChild(NULL, a0, a1, a2|VFS_OPENFLAG_USER); +); +SYSCALL2(Syscall_GetACL, "id", int, void *, + if(Sizes[1] < sizeof(tVFS_ACL)) + return -1; + return VFS_GetACL(a0, (void*)a1); +); +SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const char *, + return VFS_Mount(a0, a1, a2, a3); +); +SYSCALL1(Syscall_Chdir, "s", const char *, + return VFS_ChDir(a0); +); +SYSCALL0(Syscall_Sleep, + Threads_Sleep(); + return 0; +); +SYSCALL2(Syscall_WaitTID, "id", int, int *, + if(Sizes[1] < sizeof(int)) + return -1; + return Threads_WaitTID(a0, a1); +); +SYSCALL1(Syscall_SetUID, "i", int, + if(Sizes[0] < sizeof(int)) { + *Errno = -EINVAL; // TODO: Better message + return -1; + } + return Threads_SetUID(Errno, a0); +); +SYSCALL1(Syscall_SetGID, "i", int, + if(Sizes[0] < sizeof(int)) { + *Errno = -EINVAL; // TODO: Better message + return -1; + } + return Threads_SetGID(Errno, a0); +); + +SYSCALL1(Syscall_Fork, "d", int *, + if(Sizes[0] < sizeof(int)) + return -1; + *a0 = Threads_Fork(); + return *a0; +); const tSyscallHandler caSyscalls[] = { Syscall_Null, + Syscall_Exit, Syscall_Open, Syscall_Close, Syscall_Read, - Syscall_Write + Syscall_Write, + Syscall_Seek, + Syscall_Tell, + Syscall_IOCtl, + Syscall_FInfo, + Syscall_ReadDir, + Syscall_OpenChild, + Syscall_GetACL, + Syscall_Mount, + NULL, // SYS_REOPEN + Syscall_Chdir, + + Syscall_WaitTID, + Syscall_SetUID, + Syscall_SetGID, + + Syscall_Sleep, + Syscall_Fork, + + NULL, + NULL, + Syscall_select }; const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]); /** @@ -68,16 +203,25 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) tRequestHeader *ret; int retValueCount = 1; int retDataLen = sizeof(Uint64); - void *mallocdData[Request->NParams]; + void *returnData[Request->NParams]; + int argSizes[Request->NParams]; + Uint ret_errno = 0; // Sanity check - if( Request->CallID > ciNumSyscalls ) { + if( Request->CallID >= ciNumSyscalls ) { + Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID); + return NULL; + } + + if( !caSyscalls[Request->CallID] ) { + Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID); return NULL; } // Get size of argument list for( i = 0; i < Request->NParams; i ++ ) { + argSizes[i] = Request->Params[i].Length; switch(Request->Params[i].Type) { case ARG_TYPE_VOID: @@ -103,6 +247,9 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) return NULL; // ERROR! } } + formatString[i] = '\0'; + + LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString); { char argListData[argListLen]; @@ -110,22 +257,26 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) // Build argument list for( i = 0; i < Request->NParams; i ++ ) { + returnData[i] = NULL; switch(Request->Params[i].Type) { case ARG_TYPE_VOID: break; case ARG_TYPE_INT32: + LOG("Syscalls", "%i INT32: 0x%x", i, *(Uint32*)inData); *(Uint32*)&argListData[argListLen] = *(Uint32*)inData; argListLen += sizeof(Uint32); inData += sizeof(Uint32); break; case ARG_TYPE_INT64: + LOG("Syscalls", "%i INT64: 0x%llx", i, *(Uint64*)inData); *(Uint64*)&argListData[argListLen] = *(Uint64*)inData; argListLen += sizeof(Uint64); inData += sizeof(Uint64); break; case ARG_TYPE_STRING: - *(void**)&argListData[argListLen] = *(void**)inData; + LOG("Syscalls", "%i STR: '%s'", i, (char*)inData); + *(char**)&argListData[argListLen] = (char*)inData; argListLen += sizeof(void*); inData += Request->Params[i].Length; break; @@ -144,12 +295,17 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) if( Request->Params[i].Flags & ARG_FLAG_ZEROED ) { // Allocate and zero the buffer - mallocdData[i] = calloc(1, Request->Params[i].Length); - *(void**)&argListData[argListLen] = mallocdData[i]; + returnData[i] = calloc(1, Request->Params[i].Length); + LOG("Syscalls", "%i ZDAT: %i %p", i, + Request->Params[i].Length, returnData[i]); + *(void**)&argListData[argListLen] = returnData[i]; argListLen += sizeof(void*); } else { + returnData[i] = (void*)inData; + LOG("Syscalls", "%i DATA: %i %p", i, + Request->Params[i].Length, returnData[i]); *(void**)&argListData[argListLen] = (void*)inData; argListLen += sizeof(void*); inData += Request->Params[i].Length; @@ -158,7 +314,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) } } - retVal = caSyscalls[Request->CallID](formatString, argListData); + retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes); } // Allocate the return @@ -167,7 +323,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) ret->ClientID = Request->ClientID; ret->CallID = Request->CallID; ret->NParams = retValueCount; - inData = &ret->Params[ ret->NParams ]; + inData = (char*)&ret->Params[ ret->NParams ]; // Static Uint64 return value ret->Params[0].Type = ARG_TYPE_INT64; @@ -176,20 +332,32 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) *(Uint64*)inData = retVal; inData += sizeof(Uint64); + Log_Debug("Syscalls", "Return 0x%llx", retVal); + + retValueCount = 1; for( i = 0; i < Request->NParams; i ++ ) { if( Request->Params[i].Type != ARG_TYPE_DATA ) continue; if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) ) continue; - ret->Params[1 + i].Type = Request->Params[i].Type; - ret->Params[1 + i].Flags = 0; - ret->Params[1 + i].Length = Request->Params[i].Length; + ret->Params[retValueCount].Type = Request->Params[i].Type; + ret->Params[retValueCount].Flags = 0; + ret->Params[retValueCount].Length = Request->Params[i].Length; - memcpy(inData, mallocdData[i], Request->Params[i].Length); + LOG("Syscalls", "Ret %i: Type %i, Len %i", + i, Request->Params[i].Type, Request->Params[i].Length); + + memcpy(inData, returnData[i], Request->Params[i].Length); inData += Request->Params[i].Length; - free( mallocdData[i] ); // Free temp buffer from above + if( Request->Params[i].Flags & ARG_FLAG_ZEROED ) + free( returnData[i] ); // Free temp buffer from above + retValueCount ++; } + *ReturnLength = sizeof(tRequestHeader) + + retValueCount * sizeof(tRequestValue) + + retDataLen; + return ret; }