AcessKernel - cleaning up debug messages
[tpg/acess2.git] / AcessNative / ld-acess_src / syscalls.c
index 50352f8..3fdbc72 100644 (file)
@@ -1,32 +1,46 @@
 /*
  */
-#include "../../Usermode/include/acess/sys.h"
+#define DONT_INCLUDE_SYSCALL_NAMES 1
 #include "common.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdarg.h>
 #include <string.h>
+#include <stddef.h>
+#include <unistd.h>
 #include "request.h"
-#include "../syscalls.h"
+
+#if SYSCALL_TRACE
+#define DEBUG(str, x...)       Debug(str, x)
+#else
+#define DEBUG(...)     do{}while(0)
+#endif
+
+#define        MAX_FPS 16
 
 // === Types ===
 
 // === IMPORTS ===
 
+// === GLOBALS ===
+FILE   *gaSyscall_LocalFPs[MAX_FPS];
+
 // === CODE ===
-const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list Args)
+const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
 {
        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)
        {
@@ -48,11 +62,11 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
        case 'i':
                
                if( direction != 1 ) {
-                       fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
+                       Warning("ReadEntry: Recieving an integer is not defined");
                        return NULL;
                }
                
-               val32 = va_arg(Args, uint32_t);
+               val32 = va_arg(*Args, uint32_t);
                
                Dest->Type = ARG_TYPE_INT32;
                Dest->Length = sizeof(uint32_t);
@@ -69,7 +83,7 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                        return NULL;
                }
                
-               val64 = va_arg(Args, uint64_t);
+               val64 = va_arg(*Args, uint64_t);
                
                Dest->Type = ARG_TYPE_INT64;
                Dest->Length = sizeof(uint64_t);
@@ -85,7 +99,7 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                        return NULL;
                }
                
-               str = va_arg(Args, char*);
+               str = va_arg(*Args, char*);
                
                Dest->Type = ARG_TYPE_STRING;
                Dest->Length = strlen(str) + 1;
@@ -98,15 +112,15 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                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*);
                
                // Save the pointer for later
                if( PtrDest )   *PtrDest = str;
                
                // Create parameter block
-               Dest->Type = ARG_TYPE_INT64;
-               Dest->Length = sizeof(uint64_t);
+               Dest->Type = ARG_TYPE_DATA;
+               Dest->Length = str ? len : 0;
                Dest->Flags = 0;
                if( direction & 2 )
                        Dest->Flags |= ARG_FLAG_RETURN;
@@ -114,7 +128,7 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                // Has data?
                if( direction & 1 )
                {
-                       if( DataDest )
+                       if( DataDest && str )
                                memcpy(DataDest, str, len);
                }
                else
@@ -154,6 +168,11 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        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;
@@ -163,7 +182,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        {
                tRequestValue   tmpVal;
                
-               str = ReadEntry(&tmpVal, NULL, NULL, str, args);
+               str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
                if( !str ) {
                        fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
                        exit(127);
@@ -191,6 +210,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        req->ClientID = 0;      //< Filled later
        req->CallID = SyscallID;
        req->NParams = paramCount;
+       req->MessageLength = dataLength;
        dataPtr = &req->Params[paramCount];
        
        // Fill `output` and `input`
@@ -201,7 +221,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        retCount = 0;
        while(*str)
        {               
-               str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, args);
+               str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
                if( !str )      break;
                
                if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
@@ -214,7 +234,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        va_end(args);
        
        // Send syscall request
-       if( SendRequest(req, dataLength) ) {
+       if( SendRequest(req, dataLength, retLength) < 0 ) {
                fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
                exit(127);
        }
@@ -222,7 +242,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        // Parse return value
        dataPtr = &req->Params[req->NParams];
        retValue = 0;
-       if( req->NParams > 1 )
+       if( req->NParams >= 1 )
        {
                switch(req->Params[0].Type)
                {
@@ -238,110 +258,80 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        }
        
        // Write changes to buffers
-       va_start(args, ArgTypes);
+       if( req->NParams - 1 != 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 = 1; i < req->NParams; i ++ )
        {
-               memcpy( retPtrs[i-1], dataPtr, req->Params[i].Length );
+               #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
+               memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
                dataPtr += req->Params[i].Length;
        }
-       va_end(args);
        
        free( req );
+       free( retPtrs );
        
-       return 0;
-}
-
-// --- VFS Calls
-int open(const char *Path, int Flags) {
-       return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
-}
-
-void close(int FD) {
-       _Syscall(SYS_CLOSE, ">i", FD);
-}
-
-size_t read(int FD, size_t Bytes, void *Dest) {
-       return _Syscall(SYS_READ, "<i >i >i <d", FD, Bytes, Bytes, Dest);
+       DEBUG(": %i 0x%llx", SyscallID, retValue);
+       
+       return retValue;
 }
 
-size_t write(int FD, size_t Bytes, void *Src) {
-       return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
-}
 
-int seek(int FD, int64_t Ofs, int Dir) {
-       return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
+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) {
-       return _Syscall(SYS_TELL, ">i", FD);
+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 >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 <d >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 <d", &ret, fd, 256, dest);
-       return ret;
+size_t native_write(int FD, const void *Src, size_t Bytes)
+{
+       return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
 }
 
-int _SysOpenChild(int fd, char *name, int flags) {
-        int    ret = 0;
-       _Syscall(SYS_OPENCHILD, "<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 >i <d", &ret, fd, sizeof(t_sysACL), dest);
-       return ret;
+uint64_t native_tell(int FD)
+{
+       return ftell( gaSyscall_LocalFPs[FD] );
 }
 
-int _SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
-        int    ret = 0;
-       _Syscall(SYS_MOUNT, "<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},
-       
-       DEFSYM(open),
-       DEFSYM(close),
-       DEFSYM(read),
-       DEFSYM(write),
-       DEFSYM(seek),
-       DEFSYM(tell),
-       DEFSYM(ioctl),
-       DEFSYM(finfo),
-       DEFSYM(readdir),
-       DEFSYM(_SysOpenChild),
-       DEFSYM(_SysGetACL),
-       DEFSYM(_SysMount),
-       
-       {"_SysSetFaultHandler", _SysSetFaultHandler}
-};
-
-const int      ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);
-

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