From 510431249326d5a44fc502ed5cf798b79a442ce8 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 6 Oct 2012 19:50:38 +0800 Subject: [PATCH] AcessNative - Changes to allow _compiling_ on windows. - NOTE: Windows does not support fork(), time to fix my process creation code. --- AcessNative/acesskernel_src/Makefile | 3 +- AcessNative/acesskernel_src/include/arch.h | 8 +- .../acesskernel_src/include/threads_glue.h | 15 +++ AcessNative/acesskernel_src/main.c | 19 +--- AcessNative/acesskernel_src/server.c | 29 +++--- AcessNative/acesskernel_src/syscalls.c | 2 +- AcessNative/acesskernel_src/threads.c | 97 ++++-------------- AcessNative/acesskernel_src/threads_glue.c | 99 +++++++++++++++++++ AcessNative/ld-acess_src/Makefile | 9 +- AcessNative/ld-acess_src/elf.c | 4 +- AcessNative/ld-acess_src/elf32.h | 2 +- AcessNative/ld-acess_src/elf64.h | 2 +- AcessNative/ld-acess_src/elf_load.c | 5 +- AcessNative/ld-acess_src/exports.c | 13 ++- AcessNative/ld-acess_src/exports.h | 2 +- AcessNative/ld-acess_src/memory.c | 15 +-- AcessNative/ld-acess_src/syscalls.c | 2 +- KernelLand/Kernel/debug.c | 2 +- KernelLand/Kernel/include/acess.h | 58 +---------- KernelLand/Kernel/include/logdebug.h | 69 +++++++++++++ Usermode/Libraries/ld-acess.so_src/elf.c | 4 +- 21 files changed, 272 insertions(+), 187 deletions(-) create mode 100644 AcessNative/acesskernel_src/include/threads_glue.h create mode 100644 AcessNative/acesskernel_src/threads_glue.c create mode 100644 KernelLand/Kernel/include/logdebug.h diff --git a/AcessNative/acesskernel_src/Makefile b/AcessNative/acesskernel_src/Makefile index fddfbcca..611509b1 100644 --- a/AcessNative/acesskernel_src/Makefile +++ b/AcessNative/acesskernel_src/Makefile @@ -25,7 +25,7 @@ N_OBJ := main.o BUILDINFO_OBJ := obj-$(PLATFORM)/buildinfo.o BUILDINFO_SRC := $(BUILDINFO_OBJ:%.o=%.c) -OBJ := helpers.o threads.o server.o syscalls.o time.o +OBJ := helpers.o threads.o threads_glue.o server.o syscalls.o time.o OBJ += video.o keyboard.o mouse.o nativefs.o vfs_handle.o ui_sdl.o OBJ := $(addprefix obj-$(PLATFORM)/,$(OBJ)) N_OBJ := $(addprefix obj-$(PLATFORM)/,$(N_OBJ)) @@ -40,6 +40,7 @@ LDFLAGS += -lSDL -lSDLmain -g -Wl,--defsym,__buildnum=$(BUILD_NUM) ifeq ($(PLATFORM),win) BIN := ../AcessKernel.exe + LDFLAGS += -lws2_32 -lpthread endif ifeq ($(PLATFORM),lin) BIN := ../AcessKernel diff --git a/AcessNative/acesskernel_src/include/arch.h b/AcessNative/acesskernel_src/include/arch.h index 03474cf1..1307e0c6 100644 --- a/AcessNative/acesskernel_src/include/arch.h +++ b/AcessNative/acesskernel_src/include/arch.h @@ -5,7 +5,6 @@ #include //#include -#include #undef CLONE_VM #define _MODULE_NAME_ "NativeKernel" @@ -29,10 +28,13 @@ typedef intptr_t tPAddr; typedef int BOOL; +#include +#undef NULL +#undef offsetof + struct sShortSpinlock { - int IsValid; - pthread_mutex_t Mutex; + void *Mutex; }; #define SHORTLOCK(...) diff --git a/AcessNative/acesskernel_src/include/threads_glue.h b/AcessNative/acesskernel_src/include/threads_glue.h new file mode 100644 index 00000000..61346bd3 --- /dev/null +++ b/AcessNative/acesskernel_src/include/threads_glue.h @@ -0,0 +1,15 @@ +/* + */ +#ifndef _ACESSNATIVE__THREADS_GLUE_H_ +#define _ACESSNATIVE__THREADS_GLUE_H_ + + +extern void Threads_Glue_Yield(void); +extern void Threads_Glue_AcquireMutex(void **Lock); +extern void Threads_Glue_ReleaseMutex(void **Lock); +extern void Threads_Glue_SemInit(void **Ptr, int Val); +extern int Threads_Glue_SemWait(void *Ptr, int Max); +extern int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd ); + +#endif + diff --git a/AcessNative/acesskernel_src/main.c b/AcessNative/acesskernel_src/main.c index 609ea2ad..e5d8baba 100644 --- a/AcessNative/acesskernel_src/main.c +++ b/AcessNative/acesskernel_src/main.c @@ -13,6 +13,7 @@ #endif #include #include +#include "../../KernelLand/Kernel/include/logdebug.h" #define VALGRIND_CLIENT 0 @@ -66,7 +67,9 @@ int main(int argc, char *argv[]) UI_Initialise(800, 480); // - Ignore SIGUSR1 (used to wake threads) + #ifdef SIGUSR1 signal(SIGUSR1, SIG_IGN); + #endif // Initialise VFS VFS_Init(); @@ -101,7 +104,7 @@ int main(int argc, char *argv[]) { int pid; int argcount = 0; - char *args[7+rootapp_argc+1+1]; + const char *args[7+rootapp_argc+1+1]; #if VALGRIND_CLIENT args[argcount++] = "valgrind"; @@ -113,23 +116,11 @@ int main(int argc, char *argv[]) for( i = 0; i < rootapp_argc; i ++ ) args[argcount+i] = rootapp[i]; args[argcount+rootapp_argc] = NULL; - - pid = fork(); + pid = spawnv(P_NOWAIT, "./ld-acess", args); if(pid < 0) { perror("Starting root application [fork(2)]"); return 1; } - if(pid == 0) - { - #ifdef __LINUX__ - prctl(PR_SET_PDEATHSIG, SIGHUP); // LINUX ONLY! - #endif - #if VALGRIND_CLIENT - execv("valgrind", args); - #else - execv("./ld-acess", args); - #endif - } printf("Root application running as PID %i\n", pid); } diff --git a/AcessNative/acesskernel_src/server.c b/AcessNative/acesskernel_src/server.c index 1c03d307..82d4e700 100644 --- a/AcessNative/acesskernel_src/server.c +++ b/AcessNative/acesskernel_src/server.c @@ -9,16 +9,22 @@ #include #include #ifdef __WIN32__ +# define _WIN32_WINNT 0x0501 # include -# include +# include +# include +# define close(fd) closesocket(fd) +typedef int socklen_t; #else # include # include # include # include // inet_ntop +# include "include/windows_helpers.h" #endif +#define DONT_INCLUDE_SYSCALL_NAMES #include "../syscalls.h" -//#include +#include // acess but std #include #define USE_TCP 1 @@ -42,10 +48,8 @@ typedef struct { extern tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength); extern int Threads_CreateRootProcess(void); extern void Threads_SetThread(int TID); -// HACK: Should have these in a header -extern void Log_Debug(const char *Subsys, const char *Message, ...); -extern void Log_Notice(const char *Subsys, const char *Message, ...); -extern void Log_Warning(const char *Subsys, const char *Message, ...); +extern void *Threads_GetThread(int TID); +extern void Threads_PostEvent(void *Thread, uint32_t Event); // === PROTOTYPES === tClient *Server_GetClient(int ClientID); @@ -157,7 +161,7 @@ int Server_WorkerThread(void *ClientPtr) const int ciMaxParamCount = 6; char lbuf[sizeof(tRequestHeader) + ciMaxParamCount*sizeof(tRequestValue)]; tRequestHeader *hdr = (void*)lbuf; - size_t len = recv(Client->Socket, hdr, sizeof(*hdr), 0); + size_t len = recv(Client->Socket, (void*)hdr, sizeof(*hdr), 0); Log_Debug("Server", "%i bytes of header", len); if( len == 0 ) break; if( len == -1 ) { @@ -181,7 +185,7 @@ int Server_WorkerThread(void *ClientPtr) if( hdr->NParams > 0 ) { - len = recv(Client->Socket, hdr->Params, hdr->NParams*sizeof(tRequestValue), 0); + len = recv(Client->Socket, (void*)hdr->Params, hdr->NParams*sizeof(tRequestValue), 0); Log_Debug("Server", "%i bytes of params", len); if( len != hdr->NParams*sizeof(tRequestValue) ) { // Oops. @@ -244,7 +248,7 @@ int Server_WorkerThread(void *ClientPtr) continue ; } - send(Client->Socket, retHeader, retlen, 0); + send(Client->Socket, (void*)retHeader, retlen, 0); // Clean up free(hdr); @@ -415,13 +419,14 @@ int Server_ListenThread(void *Unused) } char addrstr[4*8+8+1]; - inet_ntop(clientaddr.sin_family, &clientaddr.sin_addr, addrstr, sizeof(addrstr)); + getnameinfo((struct sockaddr*)&clientaddr, sizeof(clientaddr), + addrstr, sizeof(addrstr), NULL, 0, NI_NUMERICHOST); Log_Debug("Server", "Client connection %s:%i", addrstr, ntohs(clientaddr.sin_port)); // Perform auth size_t len; tRequestAuthHdr authhdr; - len = recv(clientSock, &authhdr, sizeof(authhdr), 0); + len = recv(clientSock, (void*)&authhdr, sizeof(authhdr), 0); if( len != sizeof(authhdr) ) { // Some form of error? Log_Warning("Server", "Client auth block bad size (%i != exp %i)", @@ -461,7 +466,7 @@ int Server_ListenThread(void *Unused) } Log_Debug("Server", "Client given PID %i - info %p", authhdr.pid, client); - len = send(clientSock, &authhdr, sizeof(authhdr), 0); + len = send(clientSock, (void*)&authhdr, sizeof(authhdr), 0); if( len != sizeof(authhdr) ) { // Ok, this is an error perror("Sending auth reply"); diff --git a/AcessNative/acesskernel_src/syscalls.c b/AcessNative/acesskernel_src/syscalls.c index b636d81a..446da3de 100644 --- a/AcessNative/acesskernel_src/syscalls.c +++ b/AcessNative/acesskernel_src/syscalls.c @@ -137,7 +137,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, diff --git a/AcessNative/acesskernel_src/threads.c b/AcessNative/acesskernel_src/threads.c index 9de6140b..9400d72d 100644 --- a/AcessNative/acesskernel_src/threads.c +++ b/AcessNative/acesskernel_src/threads.c @@ -5,29 +5,16 @@ * threads.c * - Thread and process handling */ -#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) -#define off_t _acess_off_t #include -#undef NULL // Remove acess definition #include #include -#include +#include "../../KernelLand/Kernel/include/semaphore.h" #include #include #include - -#undef CLONE_VM // Such a hack -#undef off_t - -// - Native headers -#include -#include -#include -#include "/usr/include/signal.h" -#include +#include +#include "include/threads_glue.h" #define THREAD_EVENT_WAKEUP 0x80000000 @@ -35,14 +22,6 @@ void VFS_CloneHandleList(int PID); // === STRUCTURES === -#if 0 -typedef struct sState -{ - void *CurState; - Uint SP, BP, IP; -} tState; -#endif - // === PROTOTYPES === int Threads_Wake(tThread *Thread); @@ -119,10 +98,7 @@ tThread *Threads_CloneTCB(tThread *TemplateThread) ret->TID = giThreads_NextThreadID ++; ret->ThreadName = strdup(TemplateThread->ThreadName); - ret->EventSem = SDL_CreateSemaphore(0); - if( !ret->EventSem ) { - Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError()); - } + Threads_Glue_SemInit( &ret->EventSem, 0 ); ret->WaitingThreads = NULL; ret->WaitingThreadsEnd = NULL; @@ -223,7 +199,7 @@ tTID Threads_WaitTID(int TID, int *Status) void Threads_Sleep(void) { // TODO: Add to a sleeping queue - pause(); + //pause(); } void Threads_Yield(void) @@ -244,7 +220,7 @@ void Threads_Exit(int TID, int Status) { // Wait for the thread to be waited upon while( gpCurrentThread->WaitingThreads == NULL ) - SDL_Delay(10); + Threads_Glue_Yield(); } #endif @@ -255,7 +231,7 @@ void Threads_Exit(int TID, int Status) Threads_Wake(toWake); while(gpCurrentThread->WaitingThreads == toWake) - SDL_Delay(10); + Threads_Glue_Yield(); } } @@ -300,17 +276,13 @@ int Threads_Fork(void) // -------------------------------------------------------------------- int Mutex_Acquire(tMutex *Mutex) { - if(!Mutex->Protector.IsValid) { - pthread_mutex_init( &Mutex->Protector.Mutex, NULL ); - Mutex->Protector.IsValid = 1; - } - pthread_mutex_lock( &Mutex->Protector.Mutex ); + Threads_Glue_AcquireMutex(&Mutex->Protector.Mutex); return 0; } void Mutex_Release(tMutex *Mutex) { - pthread_mutex_unlock( &Mutex->Protector.Mutex ); + Threads_Glue_ReleaseMutex(&Mutex->Protector.Mutex); } // -------------------------------------------------------------------- @@ -320,50 +292,19 @@ void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Mo { memset(Sem, 0, sizeof(tSemaphore)); // HACK: Use `Sem->Protector` as space for the semaphore pointer - *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue); + Threads_Glue_SemInit( &Sem->Protector.Mutex, InitValue ); } int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) { - SDL_SemWait( *(void**)(&Sem->Protector) ); - return 1; + return Threads_Glue_SemWait( Sem->Protector.Mutex, MaxToTake ); } int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd) { - int i; - for( i = 0; i < AmmountToAdd; i ++ ) - SDL_SemPost( *(void**)(&Sem->Protector) ); - return AmmountToAdd; -} - -// -------------------------------------------------------------------- -// Event handling -// -------------------------------------------------------------------- -int RWLock_AcquireRead(tRWLock *Lock) -{ - if( !Lock->ReaderWaiting ) { - Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t)); - pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 ); - } - pthread_rwlock_rdlock( (void*)Lock->ReaderWaiting ); - return 0; -} -int RWLock_AcquireWrite(tRWLock *Lock) -{ - if( !Lock->ReaderWaiting ) { - Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t)); - pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 ); - } - pthread_rwlock_wrlock( (void*)Lock->ReaderWaiting ); - return 0; -} -void RWLock_Release(tRWLock *Lock) -{ - pthread_rwlock_unlock( (void*)Lock->ReaderWaiting ); + return Threads_Glue_SemSignal( Sem->Protector.Mutex, AmmountToAdd ); } - // -------------------------------------------------------------------- // Event handling // -------------------------------------------------------------------- @@ -376,14 +317,10 @@ Uint32 Threads_WaitEvents(Uint32 Mask) gpCurrentThread->WaitMask = Mask; if( !(gpCurrentThread->Events & Mask) ) { - do { - if( SDL_SemWait( gpCurrentThread->EventSem ) == -1 ) { - Log_Warning("Threads", "Wait on eventsem of %p, %p failed", - gpCurrentThread, gpCurrentThread->EventSem); - break; - } - } while(SDL_SemValue(gpCurrentThread->EventSem)); - // NOTE: While loop catches multiple event occurances + if( Threads_Glue_SemWait(gpCurrentThread->EventSem, INT_MAX) == -1 ) { + Log_Warning("Threads", "Wait on eventsem of %p, %p failed", + gpCurrentThread, gpCurrentThread->EventSem); + } Log_Debug("Threads", "Woken from nap (%i here)", SDL_SemValue(gpCurrentThread->EventSem)); } rv = gpCurrentThread->Events & Mask; @@ -401,7 +338,7 @@ void Threads_PostEvent(tThread *Thread, Uint32 Events) Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events); if( Events == 0 || Thread->WaitMask & Events ) { - SDL_SemPost( Thread->EventSem ); + Threads_Glue_SemSignal( Thread->EventSem, 1 ); // Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName); } } diff --git a/AcessNative/acesskernel_src/threads_glue.c b/AcessNative/acesskernel_src/threads_glue.c new file mode 100644 index 00000000..ba407b81 --- /dev/null +++ b/AcessNative/acesskernel_src/threads_glue.c @@ -0,0 +1,99 @@ +/* + * Acess2 Native Kernel + * - Acess kernel emulation on another OS using SDL and UDP + * + * threads.c + * - Thread and process handling + */ +#include "include/threads_glue.h" + +#define _ACESS_H +typedef void **tShortSpinlock; +#include + +// - Native headers +#include +#include +#include +//#include "/usr/include/signal.h" +#include +#include +#include + +#include // Kernel land, but uses standards + +// === CODE === +void Threads_Glue_Yield(void) +{ + SDL_Delay(10); +} + +void Threads_Glue_AcquireMutex(void **Lock) +{ + if(!*Lock) { + *Lock = malloc( sizeof(pthread_mutex_t) ); + pthread_mutex_init( *Lock, NULL ); + } + pthread_mutex_lock( *Lock ); +} + +void Threads_Glue_ReleaseMutex(void **Lock) +{ + pthread_mutex_unlock( *Lock ); +} + +void Threads_Glue_SemInit(void **Ptr, int Val) +{ + *Ptr = SDL_CreateSemaphore(Val); + if( !*Ptr ) { + Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError()); + } +} + +int Threads_Glue_SemWait(void *Ptr, int Max) +{ + int have = 0; + assert(Ptr); + do { + if( SDL_SemWait( Ptr ) == -1 ) { + return -1; + } + have ++; + } while( SDL_SemValue(Ptr) && have < Max ); + return have; +} + +int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd ) +{ + int i; + for( i = 0; i < AmmountToAdd; i ++ ) + SDL_SemPost( Ptr ); + return AmmountToAdd; +} + +// -------------------------------------------------------------------- +// Event handling +// -------------------------------------------------------------------- +int RWLock_AcquireRead(tRWLock *Lock) +{ + if( !Lock->ReaderWaiting ) { + Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t)); + pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 ); + } + pthread_rwlock_rdlock( (void*)Lock->ReaderWaiting ); + return 0; +} +int RWLock_AcquireWrite(tRWLock *Lock) +{ + if( !Lock->ReaderWaiting ) { + Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t)); + pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 ); + } + pthread_rwlock_wrlock( (void*)Lock->ReaderWaiting ); + return 0; +} +void RWLock_Release(tRWLock *Lock) +{ + pthread_rwlock_unlock( (void*)Lock->ReaderWaiting ); +} + diff --git a/AcessNative/ld-acess_src/Makefile b/AcessNative/ld-acess_src/Makefile index 805b3a5f..bbaef1de 100644 --- a/AcessNative/ld-acess_src/Makefile +++ b/AcessNative/ld-acess_src/Makefile @@ -11,6 +11,7 @@ OBJ := $(addprefix obj-$(PLATFORM)/,$(OBJ)) ifeq ($(PLATFORM),win) BIN := ../ld-acess.exe + LDFLAGS += -lws2_32 endif ifeq ($(PLATFORM),lin) BIN := ../ld-acess @@ -35,7 +36,7 @@ clean: $(RM) $(BIN) $(OBJ) $(DEPFILES) obj-$(PLATFORM)/link.ld $(BIN): obj-$(PLATFORM)/link.ld $(OBJ) - $(CC) $(LDFLAGS) -o $@ $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) objdump -S $@ > $@.dsm obj-$(PLATFORM)/%.o: %.c @@ -50,5 +51,11 @@ obj-lin/link.ld: Makefile @echo "Making Linker Script ($@)" $(LD) -g --verbose | awk '{ if( substr($$1,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/\b0x[048][0-9]*\b/$(LINKADDR)/g' | sed 's/CONSTANT (MAXPAGESIZE)/0x1000/g' > $@ +# Modify the default makefile to put the executable at 1MB instead +obj-win/link.ld: Makefile + @mkdir -p $(dir $@) + @echo "Making Linker Script ($@)" + $(LD) -g --verbose | awk '{ if( substr($$1,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/\b0x[048][0-9]*\b/$(LINKADDR)/g' | sed 's/CONSTANT (MAXPAGESIZE)/0x1000/g' > $@ + -include $(DEPFILES) diff --git a/AcessNative/ld-acess_src/elf.c b/AcessNative/ld-acess_src/elf.c index f7aa1a5b..3c15fb9b 120000 --- a/AcessNative/ld-acess_src/elf.c +++ b/AcessNative/ld-acess_src/elf.c @@ -1 +1,3 @@ -../../Usermode/Libraries/ld-acess.so_src/elf.c \ No newline at end of file +#include "common.h" +#define _COMMON_H // stops real ld-acess.so common.h being included +#include "../../Usermode/Libraries/ld-acess.so_src/elf.c" diff --git a/AcessNative/ld-acess_src/elf32.h b/AcessNative/ld-acess_src/elf32.h index a693e8d6..dfecd714 120000 --- a/AcessNative/ld-acess_src/elf32.h +++ b/AcessNative/ld-acess_src/elf32.h @@ -1 +1 @@ -../../Usermode/Libraries/ld-acess.so_src/elf32.h \ No newline at end of file +#include "../../Usermode/Libraries/ld-acess.so_src/elf32.h" diff --git a/AcessNative/ld-acess_src/elf64.h b/AcessNative/ld-acess_src/elf64.h index 272e8de0..6edb566a 120000 --- a/AcessNative/ld-acess_src/elf64.h +++ b/AcessNative/ld-acess_src/elf64.h @@ -1 +1 @@ -../../Usermode/Libraries/ld-acess.so_src/elf64.h \ No newline at end of file +#include "../../Usermode/Libraries/ld-acess.so_src/elf64.h" diff --git a/AcessNative/ld-acess_src/elf_load.c b/AcessNative/ld-acess_src/elf_load.c index 909c24a2..d661622e 100644 --- a/AcessNative/ld-acess_src/elf_load.c +++ b/AcessNative/ld-acess_src/elf_load.c @@ -8,6 +8,7 @@ #include #include #include +#include // PRIx64 #include "common.h" #include "elf32.h" #include "elf64.h" @@ -288,8 +289,8 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr) addr = phtab[i].p_vaddr + baseDiff; if( AllocateMemory( addr, phtab[i].p_memsz ) ) { - fprintf(stderr, "Elf_Load: Unable to map memory at %llx (0x%llx bytes)\n", - (long long)addr, (long long)phtab[i].p_memsz); + fprintf(stderr, "Elf_Load: Unable to map memory at %"PRIx64" (0x%"PRIx64" bytes)\n", + (uint64_t)addr, (uint64_t)phtab[i].p_memsz); free( phtab ); return NULL; } diff --git a/AcessNative/ld-acess_src/exports.c b/AcessNative/ld-acess_src/exports.c index cd1924b2..47f741f1 100644 --- a/AcessNative/ld-acess_src/exports.c +++ b/AcessNative/ld-acess_src/exports.c @@ -16,7 +16,6 @@ typedef struct sFILE FILE; -extern FILE *stderr; extern void exit(int) __attribute__ ((noreturn)); extern int printf(const char *, ...); extern int fprintf(FILE *,const char *, ...); @@ -29,6 +28,7 @@ extern int giSyscall_ClientID; // Needed for execve extern void _InitSyscalls(void); extern void _CloseSyscalls(void); +extern void Warning(const char *Format, ...); extern void Debug(const char *Format, ...); extern int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount); @@ -176,6 +176,10 @@ uint64_t acess__SysAllocate(uint vaddr) // --- Process Management --- int acess_clone(int flags, void *stack) { + #ifdef __WIN32__ + Warning("Win32 does not support anything like fork(2), cannot emulate"); + exit(-1); + #else extern int fork(void); if(flags & CLONE_VM) { int ret, newID, kernel_tid=0; @@ -200,12 +204,13 @@ int acess_clone(int flags, void *stack) } else { - fprintf(stderr, "ERROR: Threads currently unsupported\n"); + Warning("ERROR: Threads currently unsupported\n"); exit(-1); } + #endif } -int acess_execve(char *path, char **argv, char **envp) +int acess_execve(char *path, char **argv, const char **envp) { int i, argc; @@ -215,7 +220,7 @@ int acess_execve(char *path, char **argv, char **envp) for( argc = 0; argv[argc]; argc ++ ) ; DEBUG(" acess_execve: argc = %i", argc); - char *new_argv[7+argc+1]; + const char *new_argv[7+argc+1]; char client_id_str[11]; char socket_fd_str[11]; sprintf(client_id_str, "%i", giSyscall_ClientID); diff --git a/AcessNative/ld-acess_src/exports.h b/AcessNative/ld-acess_src/exports.h index 121a3aed..880e74ca 100644 --- a/AcessNative/ld-acess_src/exports.h +++ b/AcessNative/ld-acess_src/exports.h @@ -20,7 +20,7 @@ extern size_t native_write(int FD, const void *Src, size_t Bytes); extern int native_seek(int FD, int64_t Offset, int Dir); extern uint64_t native_tell(int FD); -extern int native_execve(const char *filename, char *const argv[], char *const envp[]); +extern int native_execve(const char *filename, const char *const argv[], const char *const envp[]); // Syscalls used by the linker extern int acess_open(const char *Path, int Flags); diff --git a/AcessNative/ld-acess_src/memory.c b/AcessNative/ld-acess_src/memory.c index 2e9612c3..3f0091d3 100644 --- a/AcessNative/ld-acess_src/memory.c +++ b/AcessNative/ld-acess_src/memory.c @@ -21,9 +21,9 @@ int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount) size_t size = (VirtAddr & 0xFFF) + ByteCount; void *tmp; #if __WIN32__ - tmp = VirtualAlloc(base, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + tmp = VirtualAlloc((void*)base, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if( tmp == NULL ) { - printf("ERROR: Unable to allocate memory (%i)\n", GetLastError()); + printf("ERROR: Unable to allocate memory (0x%x)\n", (int)GetLastError()); return -1; } #else @@ -41,9 +41,6 @@ int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount) uintptr_t FindFreeRange(size_t ByteCount, int MaxBits) { - #if __WIN32__ - # error "Windows FindFreeRange() unimplemented" - #else uintptr_t base, ofs, size; uintptr_t end = -1; static const int PAGE_SIZE = 0x1000; @@ -59,15 +56,21 @@ uintptr_t FindFreeRange(size_t ByteCount, int MaxBits) for( base = end - size + 1; base > 0; base -= PAGE_SIZE ) { for( ofs = 0; ofs < size; ofs += PAGE_SIZE ) { + #if __WIN32__ + MEMORY_BASIC_INFORMATION info; + VirtualQuery( (void*)(base + ofs), &info, sizeof(info) ); + if( info.State != MEM_FREE ) + break; + #else if( msync( (void*)(base+ofs), 1, 0) == 0 ) break; if( errno != ENOMEM ) perror("FindFreeRange, msync"); + #endif } if( ofs >= size ) { return base; } } return 0; - #endif } diff --git a/AcessNative/ld-acess_src/syscalls.c b/AcessNative/ld-acess_src/syscalls.c index 91b8de62..66801863 100644 --- a/AcessNative/ld-acess_src/syscalls.c +++ b/AcessNative/ld-acess_src/syscalls.c @@ -318,7 +318,7 @@ uint64_t native_tell(int FD) return ftell( gaSyscall_LocalFPs[FD] ); } -int native_execve(const char *filename, char *const argv[], char *const envp[]) +int native_execve(const char *filename, const char *const argv[], const char *const envp[]) { int ret; ret = execve(filename, argv, envp); diff --git a/KernelLand/Kernel/debug.c b/KernelLand/Kernel/debug.c index 31d3b399..ab3dd672 100644 --- a/KernelLand/Kernel/debug.c +++ b/KernelLand/Kernel/debug.c @@ -385,7 +385,7 @@ void Debug_Leave(const char *FuncName, char RetType, ...) #endif } -void Debug_HexDump(const char *Header, const void *Data, Uint Length) +void Debug_HexDump(const char *Header, const void *Data, size_t Length) { const Uint8 *cdat = Data; Uint pos = 0; diff --git a/KernelLand/Kernel/include/acess.h b/KernelLand/Kernel/include/acess.h index 41854a96..27dbb2a6 100644 --- a/KernelLand/Kernel/include/acess.h +++ b/KernelLand/Kernel/include/acess.h @@ -9,6 +9,8 @@ * \brief Acess2 Kernel API Core */ +#include + //! NULL Pointer #define NULL ((void*)0) //! Pack a structure @@ -34,7 +36,6 @@ * \} */ -#include #include #include "errno.h" @@ -144,60 +145,7 @@ extern void IRQ_RemHandler(int Handle); * \} */ -// --- Logging --- -/** - * \name Logging to kernel ring buffer - * \{ - */ -extern void Log_KernelPanic(const char *Ident, const char *Message, ...); -extern void Log_Panic(const char *Ident, const char *Message, ...); -extern void Log_Error(const char *Ident, const char *Message, ...); -extern void Log_Warning(const char *Ident, const char *Message, ...); -extern void Log_Notice(const char *Ident, const char *Message, ...); -extern void Log_Log(const char *Ident, const char *Message, ...); -extern void Log_Debug(const char *Ident, const char *Message, ...); -/** - * \} - */ - -// --- Debug --- -/** - * \name Debugging and Errors - * \{ - */ -extern void Debug_KernelPanic(void); //!< Initiate a kernel panic -extern void Panic(const char *Msg, ...); //!< Print a panic message (initiates a kernel panic) -extern void Warning(const char *Msg, ...); //!< Print a warning message -extern void LogF(const char *Fmt, ...); //!< Print a log message without a trailing newline -extern void Log(const char *Fmt, ...); //!< Print a log message -extern void Debug(const char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm) -extern void LogV(const char *Fmt, va_list Args); //!< va_list Log message -extern void Debug_Enter(const char *FuncName, const char *ArgTypes, ...); -extern void Debug_Log(const char *FuncName, const char *Fmt, ...); -extern void Debug_Leave(const char *FuncName, char RetType, ...); -extern void Debug_HexDump(const char *Header, const void *Data, Uint Length); -#define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__) -#if DEBUG -# define ENTER(_types...) Debug_Enter((char*)__func__, _types) -# define LOG(_fmt...) Debug_Log((char*)__func__, _fmt) -# define LEAVE(_t...) Debug_Leave((char*)__func__, _t) -# define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0) -# define LEAVE_RET0() do{LEAVE('-');return;}while(0) -#else -# define ENTER(...) -# define LOG(...) -# define LEAVE(...) -# define LEAVE_RET(_t,_v...) return (_v) -# define LEAVE_RET0() return -#endif -#if SANITY -# define ASSERT(expr) do{if(!(expr))Panic("%s:%i - %s: Assertion '"#expr"' failed",__FILE__,__LINE__,(char*)__func__);}while(0) -#else -# define ASSERT(expr) -#endif -/** - * \} - */ +#include "logdebug.h" // --- IO --- #if NO_IO_BUS diff --git a/KernelLand/Kernel/include/logdebug.h b/KernelLand/Kernel/include/logdebug.h new file mode 100644 index 00000000..13d3b82b --- /dev/null +++ b/KernelLand/Kernel/include/logdebug.h @@ -0,0 +1,69 @@ +/* + * Acess2 Kernel + * - By John Hodge (thePowersGang) + * + * logdebug.h + * - Logging / Debug printing functions + */ +#ifndef _KERNEL__LOGDEBUG_H_ +#define _KERNEL__LOGDEBUG_H_ + +#include + +// --- Logging --- +/** + * \name Logging to kernel ring buffer + * \{ + */ +extern void Log_KernelPanic(const char *Ident, const char *Message, ...); +extern void Log_Panic(const char *Ident, const char *Message, ...); +extern void Log_Error(const char *Ident, const char *Message, ...); +extern void Log_Warning(const char *Ident, const char *Message, ...); +extern void Log_Notice(const char *Ident, const char *Message, ...); +extern void Log_Log(const char *Ident, const char *Message, ...); +extern void Log_Debug(const char *Ident, const char *Message, ...); +/** + * \} + */ + +// --- Debug --- +/** + * \name Debugging and Errors + * \{ + */ +extern void Debug_KernelPanic(void); //!< Initiate a kernel panic +extern void Panic(const char *Msg, ...); //!< Print a panic message (initiates a kernel panic) +extern void Warning(const char *Msg, ...); //!< Print a warning message +extern void LogF(const char *Fmt, ...); //!< Print a log message without a trailing newline +extern void Log(const char *Fmt, ...); //!< Print a log message +extern void Debug(const char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm) +extern void LogV(const char *Fmt, va_list Args); //!< va_list Log message +extern void Debug_Enter(const char *FuncName, const char *ArgTypes, ...); +extern void Debug_Log(const char *FuncName, const char *Fmt, ...); +extern void Debug_Leave(const char *FuncName, char RetType, ...); +extern void Debug_HexDump(const char *Header, const void *Data, size_t Length); +#define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__) +#if DEBUG +# define ENTER(_types...) Debug_Enter((char*)__func__, _types) +# define LOG(_fmt...) Debug_Log((char*)__func__, _fmt) +# define LEAVE(_t...) Debug_Leave((char*)__func__, _t) +# define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0) +# define LEAVE_RET0() do{LEAVE('-');return;}while(0) +#else +# define ENTER(...) +# define LOG(...) +# define LEAVE(...) +# define LEAVE_RET(_t,_v...) return (_v) +# define LEAVE_RET0() return +#endif +#if SANITY +# define ASSERT(expr) do{if(!(expr))Panic("%s:%i - %s: Assertion '"#expr"' failed",__FILE__,__LINE__,(char*)__func__);}while(0) +#else +# define ASSERT(expr) +#endif +/** + * \} + */ + +#endif + diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index 4b081bcb..dc9dca36 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -554,7 +554,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum); // Scan for the dynamic table (and find the compiled base) - phtab = (void*)((uintptr_t)Base + hdr->e_phoff); + phtab = (void*)((uintptr_t)Base + (uintptr_t)hdr->e_phoff); for( i = 0; i < hdr->e_phnum; i ++ ) { if(phtab[i].p_type == PT_DYNAMIC) @@ -771,7 +771,7 @@ int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size) int j; // Locate the tables - phtab = (void*)( (intptr_t)Base + hdr->e_phoff ); + phtab = (void*)( (intptr_t)Base + (uintptr_t)hdr->e_phoff ); for( i = 0; i < hdr->e_phnum; i ++ ) { if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr) -- 2.20.1