From: John Hodge Date: Sun, 19 Feb 2012 08:07:35 +0000 (+0800) Subject: AcessNative - Fixes, now can run again (and spawn/kill new processes) X-Git-Tag: rel0.15~762 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=6d32a3c22e659994d7ae6164ba3722ab12d11421;p=tpg%2Facess2.git AcessNative - Fixes, now can run again (and spawn/kill new processes) --- diff --git a/AcessNative/RunTest b/AcessNative/RunTest new file mode 100755 index 00000000..9137ff94 --- /dev/null +++ b/AcessNative/RunTest @@ -0,0 +1,5 @@ +#!/bin/sh +trap '' 2 +./AcessKernel --rootapp /Acess/SBin/login +trap 2 +killall ld-acess diff --git a/AcessNative/acesskernel_src/nativefs.c b/AcessNative/acesskernel_src/nativefs.c index 8490dd13..864a24bc 100644 --- a/AcessNative/acesskernel_src/nativefs.c +++ b/AcessNative/acesskernel_src/nativefs.c @@ -6,8 +6,10 @@ * - Host filesystem access */ #define DEBUG 1 +#define off_t _acess_off_t #include // Acess #include // Acess +#undef off_t #include // Posix #include // Posix #include // Posix diff --git a/AcessNative/acesskernel_src/server.c b/AcessNative/acesskernel_src/server.c index 953722c0..7bb50336 100644 --- a/AcessNative/acesskernel_src/server.c +++ b/AcessNative/acesskernel_src/server.c @@ -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 diff --git a/AcessNative/acesskernel_src/syscalls.c b/AcessNative/acesskernel_src/syscalls.c index d5c051b0..7932cca2 100644 --- a/AcessNative/acesskernel_src/syscalls.c +++ b/AcessNative/acesskernel_src/syscalls.c @@ -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, diff --git a/AcessNative/acesskernel_src/threads.c b/AcessNative/acesskernel_src/threads.c index a39fb5e4..a2a58981 100644 --- a/AcessNative/acesskernel_src/threads.c +++ b/AcessNative/acesskernel_src/threads.c @@ -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 #undef NULL // Remove acess definition #include #include #include +#include + +#undef CLONE_VM // Such a hack +#undef off_t + +// - Native headers #include #include #include #include "/usr/include/signal.h" #include +#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); + } } diff --git a/AcessNative/acesskernel_src/ui_sdl.c b/AcessNative/acesskernel_src/ui_sdl.c index 9f463568..520ac009 100644 --- a/AcessNative/acesskernel_src/ui_sdl.c +++ b/AcessNative/acesskernel_src/ui_sdl.c @@ -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 ); } diff --git a/AcessNative/ld-acess_src/binary.c b/AcessNative/ld-acess_src/binary.c index 08ae6e9e..e9408ef9 100644 --- a/AcessNative/ld-acess_src/binary.c +++ b/AcessNative/ld-acess_src/binary.c @@ -1,12 +1,13 @@ /* * AcessNative */ +#define DEBUG 1 #include "common.h" #include #include #include -#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; diff --git a/AcessNative/ld-acess_src/exports.c b/AcessNative/ld-acess_src/exports.c index 5f97bb27..7131e632 100644 --- a/AcessNative/ld-acess_src/exports.c +++ b/AcessNative/ld-acess_src/exports.c @@ -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, " // === 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]; diff --git a/AcessNative/ld-acess_src/request.c b/AcessNative/ld-acess_src/request.c index 7d80a45b..2117591d 100644 --- a/AcessNative/ld-acess_src/request.c +++ b/AcessNative/ld-acess_src/request.c @@ -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 ) diff --git a/AcessNative/syscalls.h b/AcessNative/syscalls.h index 078944d1..0adb51f0 100644 --- a/AcessNative/syscalls.h +++ b/AcessNative/syscalls.h @@ -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