AcessNative - Spawn in libacess-native (with AN_GETPATH syscall)
[tpg/acess2.git] / AcessNative / acesskernel_src / syscalls.c
index b945df8..344745f 100644 (file)
@@ -8,30 +8,42 @@
 #include <acess.h>
 #include <threads.h>
 #include <events.h>
+#if DEBUG == 0
+# define DONT_INCLUDE_SYSCALL_NAMES
+#endif
 #include "../syscalls.h"
 
 // === IMPORTS ===
 extern int     Threads_Fork(void);     // AcessNative only function
+extern int     Threads_Spawn(int nFD, int FDs[], const void *info);
+extern int     Syscall_AN_GetPath_Real(char *Dest, size_t DstLen, const char *Path);
 
 // === TYPES ===
 typedef int    (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes);
 
 // === MACROS ===
+#define _SYSCALL_CHKFMT(_name,_fmtstr,Fmt) do{ \
+       if(strcmp(Fmt,_fmtstr) != 0) {\
+               *Errno = EINVAL;\
+               Log_Error("Syscalls", "Call %s takes args '%s', given '%s'", #_name, _fmtstr, Fmt);\
+               return -1;\
+       }\
+} while(0)
 #define SYSCALL6(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _t5, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
        _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;_t5 a5;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        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);\
        a5 = *(_t5*)Args;Args+=sizeof(_t5);\
-       LOG("SYSCALL5 '%s' %p %p %p %p %p %p", Fmt, (intptr_t)a0,(intptr_t)a1,(intptr_t)a2,(intptr_t)a3,(intptr_t)a4,(intptr_t)a5);\
+       LOG("SYSCALL6 '%s' %p %p %p %p %p %p", Fmt, (intptr_t)a0,(intptr_t)a1,(intptr_t)a2,(intptr_t)a3,(intptr_t)a4,(intptr_t)a5);\
        _call\
 }
 #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;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -42,7 +54,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 }
 #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;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -53,7 +65,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 
 #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;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -63,7 +75,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 
 #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;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        LOG("SYSCALL2 '%s' %p %p", Fmt, (intptr_t)a0,(intptr_t)a1);\
@@ -72,20 +84,20 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 
 #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;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        LOG("SYSCALL1 '%s' %p", Fmt,(intptr_t)a0);\
        _call;\
 }
 
 #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
-       if(strcmp(Fmt,"")!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,"",Fmt);\
        LOG("SYSCALL0");\
        _call;\
 }
 
 // === CODE ===
-int Syscall_Null(Uint*Errno, const char *Format, void *Args, int *Sizes)
+int Syscall_Null(Uint *Errno, const char *Format, void *Args, int *Sizes)
 {
        return 0;
 }
@@ -96,7 +108,9 @@ SYSCALL1(Syscall_Exit, "i", int,
 );
 
 SYSCALL2(Syscall_Open, "si", const char *, int,
-       return VFS_Open(a0, a1|VFS_OPENFLAG_USER);
+       int rv = VFS_Open(a0, a1|VFS_OPENFLAG_USER);
+       if(rv == -1)    *Errno = errno;
+       return rv;
 );
 SYSCALL1(Syscall_Close, "i", int,
        VFS_Close(a0);
@@ -107,12 +121,19 @@ SYSCALL3(Syscall_Read, "iid", int, int, void *,
                Log_Warning("Syscalls", "Read - %i < %i", Sizes[2], a1);
                return -1;
        }
-       return VFS_Read(a0, a1, a2);
+       size_t rv = VFS_Read(a0, a1, a2);
+       if(rv == -1)    *Errno = errno;
+       return rv;
 );
 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
-       if( Sizes[2] < a1 )
+       if( Sizes[2] < a1 ) {
+               Log_Warning("Syscalls", "Write - %x < %x", (int)Sizes[2], (int)a1);
+               *Errno = EINVAL;
                return -1;
-       return VFS_Write(a0, a1, a2);
+       }
+       size_t rv = VFS_Write(a0, a1, a2);
+       if(rv == -1)    *Errno = errno;
+       return rv;
 );
 SYSCALL3(Syscall_Seek, "iIi", int, int64_t, int,
        return VFS_Seek(a0, a1, a2);
@@ -125,7 +146,7 @@ SYSCALL3(Syscall_IOCtl, "iid", int, int, void *,
 );
 SYSCALL3(Syscall_FInfo, "idi", int, void *, int,
        if( Sizes[1] < sizeof(tFInfo)+a2*sizeof(tVFS_ACL)) {
-               LOG("offsetof(size) = %i", offsetof(tFInfo, size));
+               //LOG("offsetof(size) = %i", offsetof(tFInfo, size));
                LOG("Bad size %i < %i", Sizes[1], sizeof(tFInfo)+a2*sizeof(tVFS_ACL));
                *Errno = -EINVAL;
                return -1;
@@ -137,7 +158,7 @@ SYSCALL2(Syscall_ReadDir, "id", int, char *,
                return -1;
        return VFS_ReadDir(a0, a1);
 );
-SYSCALL6(Syscall_select, "iddddi", int, fd_set *, fd_set *, fd_set *, time_t *, unsigned int,
+SYSCALL6(Syscall_select, "iddddi", int, fd_set *, fd_set *, fd_set *, tTime *, unsigned int,
        return VFS_Select(a0, a1, a2, a3, a4, a5, 0);
 );
 SYSCALL3(Syscall_OpenChild, "isi", int, const char *, int,
@@ -154,6 +175,11 @@ SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const
 SYSCALL1(Syscall_Chdir, "s", const char *,
        return VFS_ChDir(a0);
 );
+
+SYSCALL2(Syscall_AN_Getpath, "ds", char *, const char *,
+       return Syscall_AN_GetPath_Real(a0, Sizes[0], a1);
+);
+
 SYSCALL0(Syscall_Sleep,
        Threads_Sleep();
        return 0;
@@ -190,6 +216,13 @@ SYSCALL1(Syscall_AN_Fork, "d", int *,
        return *a0;
 );
 
+SYSCALL3(Syscall_AN_Spawn, "ddd", int *, int *, void *,
+       if(Sizes[0] < sizeof(int))
+               return -1;
+       *a0 = Threads_Spawn(Sizes[1] / sizeof(int), a1, a2);
+       return *a0;
+);
+
 SYSCALL2(Syscall_SendMessage, "id", int, void *,
        return Proc_SendMessage(a0, Sizes[1], a1);
 );
@@ -203,11 +236,11 @@ SYSCALL2(Syscall_GetMessage, "dd", uint32_t *, void *,
        Uint    tmp;
         int    rv;
        if( a0 ) {
-               rv = Proc_GetMessage(&tmp, a1);
+               rv = Proc_GetMessage(&tmp, Sizes[1], a1);
                *a0 = tmp;
        }
        else
-               rv = Proc_GetMessage(NULL, a1);
+               rv = Proc_GetMessage(NULL, Sizes[1], a1);
        return rv;
 );
 
@@ -216,26 +249,29 @@ SYSCALL1(Syscall_WaitEvent, "i", int,
 );
 
 const tSyscallHandler  caSyscalls[] = {
-       Syscall_Null,
-       Syscall_Exit,
-       Syscall_Open,
-       Syscall_Close,
-       Syscall_Read,
-       Syscall_Write,
-       Syscall_Seek,
-       Syscall_Tell,
-       Syscall_IOCtl,
-       Syscall_FInfo,
-       Syscall_ReadDir,
-       Syscall_OpenChild,
-       Syscall_GetACL,
-       Syscall_Mount,
-       NULL,   // SYS_REOPEN
-       Syscall_Chdir,
+       [SYS_NULL]      = Syscall_Null,
+       [SYS_EXIT]      = Syscall_Exit,
+       [SYS_OPEN]      = Syscall_Open,
+       [SYS_CLOSE]     = Syscall_Close,
+       [SYS_COPYFD]    = NULL,
+       [SYS_FDFLAGS]   = NULL,
+       [SYS_READ]      = Syscall_Read,
+       [SYS_WRITE]     = Syscall_Write,
+       [SYS_SEEK]      = Syscall_Seek,
+       [SYS_TELL]      = Syscall_Tell,
+       [SYS_IOCTL]     = Syscall_IOCtl,
+       [SYS_FINFO]     = Syscall_FInfo,
+       [SYS_READDIR]   = Syscall_ReadDir,
+       [SYS_OPENCHILD] = Syscall_OpenChild,
+       [SYS_GETACL]    = Syscall_GetACL,
+       [SYS_MOUNT]     = Syscall_Mount,
+       [SYS_REOPEN]    = NULL, // SYS_REOPEN
+       [SYS_CHDIR]     = Syscall_Chdir,
+       [SYS_AN_GETPATH] = Syscall_AN_Getpath,
        
-       Syscall_WaitTID,
-       Syscall_SetUID,
-       Syscall_SetGID,
+       [SYS_WAITTID]   = Syscall_WaitTID,
+       [SYS_SETUID]    = Syscall_SetUID,
+       [SYS_SETGID]    = Syscall_SetGID,
        
        Syscall_GetTID,
        Syscall_GetPID,
@@ -243,7 +279,8 @@ const tSyscallHandler       caSyscalls[] = {
        Syscall_GetGID,
 
        Syscall_Sleep,
-       Syscall_AN_Fork,
+       [SYS_AN_FORK]  = Syscall_AN_Fork,
+       [SYS_AN_SPAWN] = Syscall_AN_Spawn,
 
        Syscall_SendMessage,
        Syscall_GetMessage,
@@ -254,19 +291,22 @@ const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
 /**
  * \brief Recieve a syscall structure from the server code
  */
-tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
+tRequestHeader *SyscallRecieve(tRequestHeader *Request, size_t *ReturnLength)
 {
        char    formatString[Request->NParams+1];
        char    *inData = (char*)&Request->Params[Request->NParams];
         int    argListLen = 0;
-        int    i, retVal;
+        int    retVal;
        tRequestHeader  *ret;
-        int    retValueCount = 1;
-        int    retDataLen = sizeof(Uint64);
+        int    retValueCount;
+        int    retDataLen;
        void    *returnData[Request->NParams];
         int    argSizes[Request->NParams];
        Uint    ret_errno = 0;
        
+       // Clear errno (Acess verson) at the start of the request
+       errno = 0;
+       
        // Sanity check
        if( Request->CallID >= ciNumSyscalls ) {
                Log_Notice("Syscalls", "Unknown syscall number %i", Request->CallID);
@@ -277,9 +317,13 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                Log_Notice("Syscalls", "Unimplemented syscall %i", Request->CallID);
                return NULL;
        }
-       
+
+       // Init return count/size
+       retValueCount = 2;
+       retDataLen = sizeof(Uint64) + sizeof(Uint32);   
+
        // Get size of argument list
-       for( i = 0; i < Request->NParams; i ++ )
+       for( int i = 0; i < Request->NParams; i ++ )
        {
                argSizes[i] = Request->Params[i].Length;
                switch(Request->Params[i].Type)
@@ -298,16 +342,23 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                case ARG_TYPE_DATA:
                        formatString[i] = 'd';
                        argListLen += sizeof(void*);
+                       // Prepare the return values
+                       if( Request->Params[i].Flags & ARG_FLAG_RETURN )
+                       {
+                               retDataLen += Request->Params[i].Length;
+                               retValueCount ++;
+                       }
                        break;
                case ARG_TYPE_STRING:
                        formatString[i] = 's';
                        argListLen += sizeof(char*);
                        break;
                default:
+                       Log_Error("Syscalls", "Unknown param type %i", Request->Params[i].Type);
                        return NULL;    // ERROR!
                }
        }
-       formatString[i] = '\0';
+       formatString[Request->NParams] = '\0';
        
        LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
        
@@ -315,7 +366,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                char    argListData[argListLen];
                argListLen = 0;
                // Build argument list
-               for( i = 0; i < Request->NParams; i ++ )
+               for( int i = 0; i < Request->NParams; i ++ )
                {
                        returnData[i] = NULL;
                        switch(Request->Params[i].Type)
@@ -344,13 +395,6 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                        // Data gets special handling, because only it can be returned to the user
                        // (ARG_TYPE_DATA is a pointer)
                        case ARG_TYPE_DATA:
-                               // Prepare the return values
-                               if( Request->Params[i].Flags & ARG_FLAG_RETURN )
-                               {
-                                       retDataLen += Request->Params[i].Length;
-                                       retValueCount ++;
-                               }
-                               
                                // Check for non-resident data
                                if( Request->Params[i].Length == 0 )
                                {
@@ -380,15 +424,24 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                        }
                }
                
+               // --- Perform request
                retVal = caSyscalls[Request->CallID](&ret_errno, formatString, argListData, argSizes);
        }
        
+       // ---------- Return
+       
+       if( ret_errno == 0 && errno != 0 ) {
+               ret_errno = errno;
+               LOG("errno = %i", errno);
+       }
+       
        // Allocate the return
-       ret = malloc(sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue)
-               + retDataLen);
+       size_t  msglen = sizeof(tRequestHeader) + retValueCount * sizeof(tRequestValue) + retDataLen;
+       ret = malloc(msglen);
        ret->ClientID = Request->ClientID;
        ret->CallID = Request->CallID;
        ret->NParams = retValueCount;
+       ret->MessageLength = msglen;
        inData = (char*)&ret->Params[ ret->NParams ];
        
        // Static Uint64 return value
@@ -398,10 +451,19 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
        *(Uint64*)inData = retVal;
        inData += sizeof(Uint64);
        
-       Log_Debug("Syscalls", "Return 0x%llx", retVal);
+       // Static Uint32 errno value
+       ret->Params[1].Type = ARG_TYPE_INT32;
+       ret->Params[1].Flags = 0;
+       ret->Params[1].Length = sizeof(Uint32);
+       *(Uint32*)inData = ret_errno;
+       inData += sizeof(Uint32);
+
+       LOG("Ret: %llx, errno=%i", retVal, ret_errno);  
+
+       //Log_Debug("Syscalls", "Return 0x%llx", retVal);
        
-       retValueCount = 1;
-       for( i = 0; i < Request->NParams; i ++ )
+       retValueCount = 2;
+       for( int i = 0; i < Request->NParams; i ++ )
        {
                if( Request->Params[i].Type != ARG_TYPE_DATA )  continue;
                if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) )     continue;
@@ -421,9 +483,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                retValueCount ++;
        }
        
-       *ReturnLength = sizeof(tRequestHeader)
-               + retValueCount * sizeof(tRequestValue)
-               + retDataLen;
+       *ReturnLength = ret->MessageLength;
        
        return ret;
 }

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