AcessNative - Fixes, now can run again (and spawn/kill new processes)
authorJohn Hodge <[email protected]>
Sun, 19 Feb 2012 08:07:35 +0000 (16:07 +0800)
committerJohn Hodge <[email protected]>
Sun, 19 Feb 2012 08:07:35 +0000 (16:07 +0800)
AcessNative/RunTest [new file with mode: 0755]
AcessNative/acesskernel_src/nativefs.c
AcessNative/acesskernel_src/server.c
AcessNative/acesskernel_src/syscalls.c
AcessNative/acesskernel_src/threads.c
AcessNative/acesskernel_src/ui_sdl.c
AcessNative/ld-acess_src/binary.c
AcessNative/ld-acess_src/exports.c
AcessNative/ld-acess_src/main.c
AcessNative/ld-acess_src/request.c
AcessNative/syscalls.h

diff --git a/AcessNative/RunTest b/AcessNative/RunTest
new file mode 100755 (executable)
index 0000000..9137ff9
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/sh
+trap '' 2
+./AcessKernel --rootapp /Acess/SBin/login
+trap 2
+killall ld-acess
index 8490dd1..864a24b 100644 (file)
@@ -6,8 +6,10 @@
  * - Host filesystem access\r
  */\r
 #define DEBUG  1\r
+#define off_t  _acess_off_t\r
 #include <acess.h>     // Acess\r
 #include <vfs.h>       // Acess\r
+#undef off_t\r
 #include <dirent.h>    // Posix\r
 #include <sys/stat.h>  // Posix\r
 #include <stdio.h>     // Posix\r
index 953722c..7bb5033 100644 (file)
@@ -139,8 +139,13 @@ int Server_WorkerThread(void *ClientPtr)
                // Get the response
                retHeader = SyscallRecieve(Client->CurrentRequest, &retSize);
 
-               Log_Debug("AcessSrv", "Client %i request %i",
-                       Client->ClientID, Client->CurrentRequest->CallID);
+               {
+                       int     callid = Client->CurrentRequest->CallID;
+                       Log_Debug("AcessSrv", "Client %i request %i %s",
+                               Client->ClientID, callid,
+                               callid < N_SYSCALLS ? casSYSCALL_NAMES[callid] : "UNK"
+                               );
+               }
                
                
                if( !retHeader ) {
@@ -156,13 +161,14 @@ int Server_WorkerThread(void *ClientPtr)
                retHeader->ClientID = Client->ClientID;
                
                // Mark the thread as ready for another job
+               free(Client->CurrentRequest);
                Client->CurrentRequest = 0;
                
-               Log_Debug("AcessSrv", "Sending %i to %x:%i (Client %i)",
-                       retSize, ntohl(Client->ClientAddr.sin_addr.s_addr),
-                       ntohs(Client->ClientAddr.sin_port),
-                       Client->ClientID
-                       );
+//             Log_Debug("AcessSrv", "Sending %i to %x:%i (Client %i)",
+//                     retSize, ntohl(Client->ClientAddr.sin_addr.s_addr),
+//                     ntohs(Client->ClientAddr.sin_port),
+//                     Client->ClientID
+//                     );
                
                // Return the data
                sentSize = sendto(gSocket, retHeader, retSize, 0,
@@ -272,8 +278,8 @@ int Server_ListenThread(void *Unused)
                
                // Hand off to a worker thread
                // - TODO: Actually have worker threads
-               printf("%i bytes from %x:%i\n", length,
-                       ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port));
+//             Log_Debug("Server", "%i bytes from %x:%i", length,
+//                     ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port));
                
                client = Server_GetClient(req->ClientID);
                // NOTE: Hack - Should check if all zero
@@ -298,7 +304,10 @@ int Server_ListenThread(void *Unused)
                
                Log_Debug("AcessSrv", "Message from Client %i (%p)",
                        client->ClientID, client);
-               
+
+               // Make a copy of the request data      
+               req = malloc(length);
+               memcpy(req, data, length);
                client->CurrentRequest = req;
                SDL_CondSignal(client->WaitFlag);
                #endif
index d5c051b..7932cca 100644 (file)
@@ -177,7 +177,7 @@ SYSCALL1(Syscall_SetGID, "i", int,
        return Threads_SetGID(a0);
 );
 
-SYSCALL1(Syscall_Fork, "d", int *,
+SYSCALL1(Syscall_AN_Fork, "d", int *,
        if(Sizes[0] < sizeof(int))
                return -1;
        *a0 = Threads_Fork();
@@ -207,7 +207,7 @@ const tSyscallHandler       caSyscalls[] = {
        Syscall_SetGID,
        
        Syscall_Sleep,
-       Syscall_Fork,
+       Syscall_AN_Fork,
 
        NULL,
        NULL,
index a39fb5e..a2a5898 100644 (file)
@@ -8,18 +8,27 @@
 #define _SIGNAL_H_     // Stop the acess signal.h being used
 #define _HEAP_H_       // Stop heap.h being imported (collides with stdlib heap)
 #define _VFS_EXT_H     // Stop vfs_ext.h being imported (collides with fd_set)
-#undef CLONE_VM        // Such a hack
+
+#define off_t  _acess_off_t
 #include <arch.h>
 #undef NULL    // Remove acess definition
 #include <acess.h>
 #include <mutex.h>
 #include <semaphore.h>
+#include <events.h>
+
+#undef CLONE_VM        // Such a hack
+#undef off_t   
+
+// - Native headers
 #include <unistd.h>
 #include <sys/types.h>
 #include <stdint.h>
 #include "/usr/include/signal.h"
 #include <SDL/SDL.h>
 
+#define THREAD_EVENT_WAKEUP    0x80000000
+
 // === IMPORTS ===
 void   VFS_CloneHandleList(int PID);
 
@@ -150,6 +159,7 @@ tThread *Threads_CloneTCB(tThread *TemplateThread)
        ret->TID = giThreads_NextThreadID ++;
        
        ret->ThreadName = strdup(TemplateThread->ThreadName);
+       ret->EventSem = SDL_CreateSemaphore(0);
        
        ret->WaitingThreads = NULL;
        ret->WaitingThreadsEnd = NULL;
@@ -167,10 +177,10 @@ tGID Threads_GetGID() { return gpCurrentThread->GID; }
 tTID Threads_GetTID() { return gpCurrentThread->TID; }
 tPID Threads_GetPID() { return gpCurrentThread->PID; }
 
-int Threads_SetUID(int *Errno, tUID NewUID)
+int Threads_SetUID(tUID NewUID)
 {
        if(Threads_GetUID() != 0) {
-               if(Errno)       *Errno = -EACCES;
+               errno = EACCES;
                return -1;
        }
        
@@ -178,10 +188,10 @@ int Threads_SetUID(int *Errno, tUID NewUID)
        return 0;
 }
 
-int Threads_SetGID(int *Errno, tGID NewGID)
+int Threads_SetGID(tGID NewGID)
 {
        if(Threads_GetUID() != 0) {
-               if(Errno)       *Errno = -EACCES;
+               errno = -EACCES;
                return -1;
        }
        
@@ -194,7 +204,7 @@ char **Threads_GetCWD(void) { return &gpCurrentThread->Process->CWD; }
 char **Threads_GetChroot(void) { return &gpCurrentThread->Process->Chroot; }
 int *Threads_GetMaxFD(void) { return &gpCurrentThread->Process->MaxFD; };
 
-int Threads_WaitTID(int TID, int *Status)
+tTID Threads_WaitTID(int TID, int *Status)
 {
        // Any Child
        if(TID == -1) {
@@ -235,11 +245,7 @@ int Threads_WaitTID(int TID, int *Status)
                        thread->WaitingThreadsEnd = us;
                }
                
-               while(thread->State != 0)
-               {
-                       pause();
-                       Log_Debug("Threads", "Huh?... state = %i", thread->State);
-               }
+               Threads_WaitEvents( THREAD_EVENT_WAKEUP );
                
                if(Status)      *Status = thread->ExitStatus;
                thread->WaitingThreads = thread->WaitingThreads->Next;
@@ -293,7 +299,7 @@ void Threads_Exit(int TID, int Status)
 int Threads_Wake(tThread *Thread)
 {
        Thread->State = 0;
-       kill( Thread->KernelTID, SIGUSR1 );
+       Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
        return 0;
 }
 
@@ -365,20 +371,29 @@ int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
 Uint32 Threads_WaitEvents(Uint32 Mask)
 {
        Uint32  rv;
+
+       Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events);        
+
        gpCurrentThread->WaitMask = Mask;
        if( !(gpCurrentThread->Events & Mask) )
+       {
                SDL_SemWait( gpCurrentThread->EventSem );
+       }
        rv = gpCurrentThread->Events & Mask;
        gpCurrentThread->Events &= ~Mask;
        gpCurrentThread->WaitMask = -1;
+       
        return rv;
 }
 
 void Threads_PostEvent(tThread *Thread, Uint32 Events)
 {
        Thread->Events |= Events;
+       Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events);
        
-       if( Thread->WaitMask & Events )
-               SDL_SemPost( gpCurrentThread->EventSem );
+       if( Thread->WaitMask & Events ) {
+               SDL_SemPost( Thread->EventSem );
+//             Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
+       }
 }
 
index 9f46356..520ac00 100644 (file)
@@ -38,7 +38,7 @@ int UI_Initialise(int MaxWidth, int MaxHeight)
        // Set up video
        SDL_Init(SDL_INIT_VIDEO);
        printf("UI attempting %ix%i %ibpp\n", giUI_Width, giUI_Height, 32);
-       gScreen = SDL_SetVideoMode(giUI_Width, giUI_Height, 32, 0);
+       gScreen = SDL_SetVideoMode(giUI_Width, giUI_Height, 32, SDL_DOUBLEBUF);
        if( !gScreen ) {
                fprintf(stderr, "Couldn't set %ix%i video mode: %s\n", giUI_Width, giUI_Height, SDL_GetError());
                SDL_Quit();
@@ -146,7 +146,12 @@ void UI_MainLoop(void)
                                        gUI_KeyboardCallback(KEY_ACTION_RELEASE|acess_sym);
                                }
                                break;
-                       
+
+                       case SDL_USEREVENT:
+                               SDL_UpdateRect(gScreen, 0, 0, giUI_Width, giUI_Height);
+                               SDL_Flip(gScreen);
+                               break;          
+       
                        default:
                                break;
                        }
@@ -202,5 +207,13 @@ void UI_FillBitmap(int X, int Y, int W, int H, Uint32 Value)
 void UI_Redraw(void)
 {
        // TODO: Keep track of changed rectangle
-       SDL_UpdateRect(gScreen, 0, 0, giUI_Width, giUI_Height);
+//     SDL_UpdateRect(gScreen, 0, 0, giUI_Width, giUI_Height);
+       SDL_Event       e;
+
+       e.type = SDL_USEREVENT;
+       e.user.code = 0;
+       e.user.data1 = 0;
+       e.user.data2 = 0;       
+
+       SDL_PushEvent( &e );
 }
index 08ae6e9..e9408ef 100644 (file)
@@ -1,12 +1,13 @@
 /*
  * AcessNative
  */
+#define DEBUG  1
 #include "common.h"
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 
-#define LIBRARY_PATH   "$$$$../Usermode/Output/x86/Libs"
+#define LIBRARY_PATH   "$$$$../Usermode/Output/x86_64/Libs"
 
 // === TYPES ===
 typedef struct sBinary {
@@ -169,12 +170,12 @@ void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
        }
        
        #if DEBUG
-       printf("fmt->Load(%i)...\n", fd);
+       printf("fmt->Load(0x%x)...\n", fd);
        #endif
        ret = fmt->Load(fd);
        acess_close(fd);
        #if DEBUG
-       printf("fmt->Load(%p): %p\n", fd, ret);
+       printf("fmt->Load(0x%x): %p\n", fd, ret);
        #endif
        if( !ret ) {
                return NULL;
index 5f97bb2..7131e63 100644 (file)
@@ -23,7 +23,11 @@ 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);
 
@@ -153,14 +157,18 @@ int acess_clone(int flags, void *stack)
        extern int fork(void);
        if(flags & CLONE_VM) {
                 int    ret, newID, kernel_tid=0;
-               printf("fork()");
+               printf("USERSIDE fork()\n");
                
-               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();
+                       _InitSyscalls();
                        giSyscall_ClientID = newID;
                        return 0;
                }
@@ -184,20 +192,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");
index cd2b815..ae13021 100644 (file)
@@ -7,8 +7,10 @@
 #include <string.h>
 
 // === IMPORTS ===
+extern int     gSocket;
 extern int     giSyscall_ClientID;
 extern void    acess__exit(int Status);
+extern void    Request_Preinit(void);
 
 // === PROTOTYPES ===
 void   CallUser(void *Entry, int argc, char *argv[], char **envp) __attribute__((noreturn));
@@ -24,6 +26,8 @@ int main(int argc, char *argv[], char **envp)
        void    *base;
         int    rv;
        
+       Request_Preinit();
+
 //      int    syscall_handle = -1;
        
        for( i = 1; i < argc; i ++ )
@@ -32,6 +36,11 @@ int main(int argc, char *argv[], char **envp)
                        giSyscall_ClientID = atoi(argv[++i]);
                        continue ;
                }
+
+               if(strcmp(argv[i], "--socket") == 0) {
+                       gSocket = atoi(argv[++i]);
+                       continue ;
+               }
                
                if(strcmp(argv[i], "--binary") == 0) {
                        appPath = argv[++i];
index 7d80a45..2117591 100644 (file)
@@ -44,9 +44,16 @@ SOCKET       gSocket = INVALID_SOCKET;
 struct sockaddr_in     gSyscall_ServerAddr;
 
 // === CODE ===
-int _InitSyscalls()
+void Request_Preinit(void)
+{
+       // Set server address
+       memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
+       gSyscall_ServerAddr.sin_family = AF_INET;
+       gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
+}
+
+int _InitSyscalls(void)
 {
-       
        #ifdef __WIN32__
        /* Open windows connection */
        if (WSAStartup(0x0101, &gWinsock) != 0)
@@ -72,12 +79,6 @@ int _InitSyscalls()
                exit(0);
        }
        
-       // Set server address
-       memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
-       gSyscall_ServerAddr.sin_family = AF_INET;
-       gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
-       gSyscall_ServerAddr.sin_addr.s_addr = htonl(0x7F000001);
-       
        #if 0
        // Set client address
        memset((void *)&client, '\0', sizeof(struct sockaddr_in));
@@ -100,7 +101,6 @@ int _InitSyscalls()
                #endif
                exit(0);
        }
-       giSyscall_ClientID = gSocket;   // A bit of a hack really :(
        #endif
        
        #if 0
@@ -143,6 +143,20 @@ int _InitSyscalls()
        return 0;
 }
 
+/**
+ * \brief Close the syscall socket
+ * \note Used in acess_fork to get a different port number
+ */
+void _CloseSyscalls(void)
+{
+       #if __WIN32__
+       closesocket(gSocket);
+       WSACleanup();
+       #else
+       close(gSocket);
+       #endif
+}
+
 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
 {
        if( gSocket == INVALID_SOCKET )
index 078944d..0adb51f 100644 (file)
@@ -55,7 +55,7 @@ enum eSyscalls {
        
        // IPC
        SYS_SLEEP,
-       SYS_FORK,
+       SYS_AN_FORK,
        SYS_SENDMSG,
        SYS_GETMSG,
        SYS_SELECT,
@@ -82,9 +82,18 @@ static const char * casSYSCALL_NAMES[] = {
        "SYS_GETACL",
        "SYS_MOUNT",
        "SYS_REOPEN",
+       "SYS_CHDIR",
+       
+       "SYS_WAITTID",
+       "SYS_SETUID",
+       "SYS_SETGID",
        
        // IPC
-       "SYS_SLEEP"
+       "SYS_SLEEP",
+       "SYS_AN_FORK",
+       "SYS_SENDMSG",
+       "SYS_GETMSG",
+       "SYS_SELECT"
 };
 #endif
 

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