AcessNative - Bugfixes 'r' us, GUI can start and render (partially)
[tpg/acess2.git] / AcessNative / ld-acess_src / exports.c
index 947e0d3..cd1924b 100644 (file)
@@ -5,12 +5,14 @@
  * - Exported functions
  */
 #define DONT_INCLUDE_SYSCALL_NAMES 1
-#include "../../Usermode/include/acess/sys.h"
+#include "../../Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h"
 #include "../syscalls.h"
 #include "exports.h"
 #include <stdarg.h>
+#include <stddef.h>
 
 #define DEBUG(v...)    Debug(v)
+#define PAGE_SIZE      4096
 
 typedef struct sFILE   FILE;
 
@@ -22,13 +24,20 @@ extern int  sprintf(char *,const char *, ...);
 extern int     vprintf(const char *, va_list);
 extern int     strncmp(const char *, const char *, size_t);
 
+extern int     gSocket;
 extern int     giSyscall_ClientID;     // Needed for execve
+extern void    _InitSyscalls(void);
+extern void    _CloseSyscalls(void);
+
 extern void    Debug(const char *Format, ...);
 extern int     AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
 
 // === CONSTANTS ===
 #define NATIVE_FILE_MASK       0x40000000
 
+// === GLOBALS ===
+int    acess__errno;
+
 // === CODE ===
 // --- VFS Calls
 int acess_chdir(const char *Path)
@@ -62,14 +71,16 @@ int acess_reopen(int FD, const char *Path, int Flags) {
 size_t acess_read(int FD, void *Dest, size_t Bytes) {
        if(FD & NATIVE_FILE_MASK)
                return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
-       DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
+//     if( FD > 2 )
+               DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
        return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
 }
 
 size_t acess_write(int FD, const void *Src, size_t Bytes) {
        if(FD & NATIVE_FILE_MASK)
                return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
-       DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
+//     if( FD > 2 )
+               DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
        return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
 }
 
@@ -84,16 +95,22 @@ int acess_seek(int FD, int64_t Ofs, int Dir) {
 uint64_t acess_tell(int FD) {
        if(FD & NATIVE_FILE_MASK)
                return native_tell( FD & (NATIVE_FILE_MASK-1) );
+       DEBUG("tell(0x%x)", FD);
        return _Syscall(SYS_TELL, ">i", FD);
 }
 
 int acess_ioctl(int fd, int id, void *data) {
-       // NOTE: 1024 byte size is a hack
+        int    len;
        DEBUG("ioctl(%i, %i, %p)", fd, id, data);
-       return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, 1024, data);
+       // NOTE: The length here is hacky and could break
+       if( data == NULL )
+               len = 0;
+       else
+               len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
+       return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
 }
 int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
-       DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
+//     DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
        DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
        return _Syscall(SYS_FINFO, ">i <d >i",
                fd,
@@ -102,31 +119,41 @@ int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
                );
 }
 
-int acess_readdir(int fd, char *dest) {
-       DEBUG("readdir(%i, %p)", fd, dest);
+int acess_SysReadDir(int fd, char *dest) {
+       DEBUG("SysReadDir(%i, %p)", fd, dest);
        return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
 }
 
-int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
+int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
 {
-       DEBUG("select(%i, %p, %p, %p, %p)", nfds, read, write, error, timeout);
-       return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d", nfds,
+       DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
+       return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
                read ? (nfds+7)/8 : 0, read,
                write ? (nfds+7)/8 : 0, write,
                error ? (nfds+7)/8 : 0, error,
-               sizeof(*timeout), timeout
+               sizeof(*timeout), timeout,
+               events
                );
 }
 
+int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
+{
+       return acess__SysSelect(nfds, read, write, error, timeout, 0);
+}
+
+
 int acess__SysOpenChild(int fd, char *name, int flags) {
+       DEBUG("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
        return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
 }
 
 int acess__SysGetACL(int fd, t_sysACL *dest) {
+       DEBUG("%s(0x%x, %p)", __func__, fd, dest);
        return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
 }
 
 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
+       DEBUG("%s('%s', '%s', '%s', '%s')", __func__, Device, Directory, Type, Options);
        return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
 }
 
@@ -152,15 +179,19 @@ int acess_clone(int flags, void *stack)
        extern int fork(void);
        if(flags & CLONE_VM) {
                 int    ret, newID, kernel_tid=0;
-               printf("fork()");
+               Debug("USERSIDE fork()");
                
-               newID = _Syscall(SYS_FORK, "<d", sizeof(int), &kernel_tid);
+               newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
                ret = fork();
-               if(ret < 0)     return ret;
+               if(ret < 0) {
+                       return ret;
+               }
                
                if(ret == 0)
                {
+                       _CloseSyscalls();
                        giSyscall_ClientID = newID;
+                       _InitSyscalls();
                        return 0;
                }
                
@@ -183,20 +214,24 @@ int acess_execve(char *path, char **argv, char **envp)
        // Get argument count
        for( argc = 0; argv[argc]; argc ++ ) ;
        DEBUG(" acess_execve: argc = %i", argc);
-       
-       char    *new_argv[5+argc+1];
-       char    key[11];
-       sprintf(key, "%i", giSyscall_ClientID);
+
+       char    *new_argv[7+argc+1];
+       char    client_id_str[11];
+       char    socket_fd_str[11];
+       sprintf(client_id_str, "%i", giSyscall_ClientID);
+       sprintf(socket_fd_str, "%i", gSocket);
        new_argv[0] = "ld-acess";       // TODO: Get path to ld-acess executable
-       new_argv[1] = "--key";  // Set socket/client ID for Request.c
-       new_argv[2] = key;
-       new_argv[3] = "--binary";       // Set the binary path (instead of using argv[0])
-       new_argv[4] = path;
-       for( i = 0; i < argc; i ++ )    new_argv[5+i] = argv[i];
-       new_argv[5+i] = NULL;
+       new_argv[1] = "--key";          // Set client ID for Request.c
+       new_argv[2] = client_id_str;
+       new_argv[3] = "--socket";       // Socket
+       new_argv[4] = socket_fd_str;
+       new_argv[5] = "--binary";       // Set the binary path (instead of using argv[0])
+       new_argv[6] = path;
+       for( i = 0; i < argc; i ++ )    new_argv[7+i] = argv[i];
+       new_argv[7+i] = NULL;
        
        #if 1
-       argc += 5;
+       argc += 7;
        for( i = 0; i < argc; i ++ )
                printf("\"%s\" ", new_argv[i]);
        printf("\n");
@@ -215,37 +250,44 @@ int acess_execve(char *path, char **argv, char **envp)
 
 void acess_sleep(void)
 {
+       DEBUG("%s()", __func__);
        _Syscall(SYS_SLEEP, "");
 }
 
 int acess_waittid(int TID, int *ExitStatus)
 {
+       DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
        return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
 }
 
-int acess_setuid(int ID)
-{
-       return _Syscall(SYS_SETUID, ">i", ID);
-}
-
-int acess_setgid(int ID)
-{
-       return _Syscall(SYS_SETGID, ">i", ID);
-}
+int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
+int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
+int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
+int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
+int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
+int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
 
 int acess_SysSendMessage(int DestTID, int Length, void *Data)
 {
+       DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
        return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
 }
 
-int acess_SysGetMessage(int *SourceTID, void *Data)
+int acess_SysGetMessage(int *SourceTID, int BufLen, void *Data)
 {
+       DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
        return _Syscall(SYS_GETMSG, "<d <d",
-               SourceTID ? sizeof(int) : 0, SourceTID,
-               Data ? 4096 : 0, Data
+               SourceTID ? sizeof(uint32_t) : 0, SourceTID,
+               BufLen, Data
                );
 }
 
+int acess__SysWaitEvent(int Mask)
+{
+       DEBUG("%s(%x)", __func__, Mask);
+       return _Syscall(SYS_WAITEVENT, ">i", Mask);
+}
+
 // --- Logging
 void acess__SysDebug(const char *Format, ...)
 {
@@ -253,7 +295,7 @@ void acess__SysDebug(const char *Format, ...)
        
        va_start(args, Format);
        
-       printf("[_SysDebug %i]", giSyscall_ClientID);
+       printf("[_SysDebug %i] ", giSyscall_ClientID);
        vprintf(Format, args);
        printf("\n");
        
@@ -269,7 +311,7 @@ void acess__exit(int Status)
 
 
 // === Symbol List ===
-#define DEFSYM(name)   {#name, acess_##name}
+#define DEFSYM(name)   {#name, &acess_##name}
 const tSym     caBuiltinSymbols[] = {
        DEFSYM(_exit),
        
@@ -283,11 +325,12 @@ const tSym        caBuiltinSymbols[] = {
        DEFSYM(tell),
        DEFSYM(ioctl),
        DEFSYM(finfo),
-       DEFSYM(readdir),
+       DEFSYM(SysReadDir),
        DEFSYM(select),
        DEFSYM(_SysOpenChild),
        DEFSYM(_SysGetACL),
        DEFSYM(_SysMount),
+       DEFSYM(_SysSelect),
        
        DEFSYM(clone),
        DEFSYM(execve),
@@ -296,13 +339,17 @@ const tSym        caBuiltinSymbols[] = {
        DEFSYM(waittid),
        DEFSYM(setuid),
        DEFSYM(setgid),
+       DEFSYM(gettid),
 
        DEFSYM(SysSendMessage),
        DEFSYM(SysGetMessage),
        
        DEFSYM(_SysAllocate),
        DEFSYM(_SysDebug),
-       DEFSYM(_SysSetFaultHandler)
+       DEFSYM(_SysSetFaultHandler),
+       DEFSYM(_SysWaitEvent),
+       
+       DEFSYM(_errno)
 };
 
 const int      ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);

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