AcessKernel - cleaning up debug messages
[tpg/acess2.git] / AcessNative / ld-acess_src / syscalls.c
index ff29997..3fdbc72 100644 (file)
@@ -1,19 +1,31 @@
 /*
  */
-#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)
 {
@@ -22,11 +34,13 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
         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,12 +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);
-               printf("val32 = 0x%x\n", val32);
                
                Dest->Type = ARG_TYPE_INT32;
                Dest->Length = sizeof(uint32_t);
@@ -71,7 +84,6 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                }
                
                val64 = va_arg(*Args, uint64_t);
-               printf("val64 = 0x%llx\n", val64);
                
                Dest->Type = ARG_TYPE_INT64;
                Dest->Length = sizeof(uint64_t);
@@ -88,7 +100,6 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                }
                
                str = va_arg(*Args, char*);
-               printf("str = %p '%s'\n", str, str);
                
                Dest->Type = ARG_TYPE_STRING;
                Dest->Length = strlen(str) + 1;
@@ -101,17 +112,15 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                break;
        // Data (special handling)
        case 'd':
-               len = va_arg(*Args, int);
+               len = va_arg(*Args, size_t);
                str = va_arg(*Args, char*);
                
-               printf("len = %i, str = %p\n", len, str);
-               
                // Save the pointer for later
                if( PtrDest )   *PtrDest = str;
                
                // Create parameter block
                Dest->Type = ARG_TYPE_DATA;
-               Dest->Length = len;
+               Dest->Length = str ? len : 0;
                Dest->Flags = 0;
                if( direction & 2 )
                        Dest->Flags |= ARG_FLAG_RETURN;
@@ -119,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
@@ -159,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;
@@ -196,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`
@@ -219,7 +234,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        va_end(args);
        
        // Send syscall request
-       if( SendRequest(req, dataLength) < 0 ) {
+       if( SendRequest(req, dataLength, retLength) < 0 ) {
                fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
                exit(127);
        }
@@ -227,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)
                {
@@ -243,98 +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);
-}
-
-uint64_t tell(int FD) {
-       return _Syscall(SYS_TELL, ">i", FD);
+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;
 }
 
-int ioctl(int fd, int id, void *data) {
-       // NOTE: 1024 byte size is a hack
-       return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, 1024, data);
-}
-int finfo(int fd, t_sysFInfo *info, int maxacls) {
-       return _Syscall(SYS_FINFO, ">i <d >i",
-               fd,
-               sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
-               maxacls);
+void native_close(int FD)
+{
+       fclose( gaSyscall_LocalFPs[FD] );
+       gaSyscall_LocalFPs[FD] = NULL;
 }
 
-int readdir(int fd, char *dest) {
-       return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
+size_t native_read(int FD, void *Dest, size_t Bytes)
+{
+       return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
 }
 
-int _SysOpenChild(int fd, char *name, int flags) {
-       return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
+size_t native_write(int FD, const void *Src, size_t Bytes)
+{
+       return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
 }
 
-int _SysGetACL(int fd, t_sysACL *dest) {
-       return _Syscall(SYS_GETACL, "<i >i <d", fd, sizeof(t_sysACL), dest);
+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 _SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
-       return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
+uint64_t native_tell(int FD)
+{
+       return ftell( gaSyscall_LocalFPs[FD] );
 }
 
-
-// --- Error Handler
-int    _SysSetFaultHandler(int (*Handler)(int)) {
-       return 0;
+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;
 }
-
-
-// === 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