AcessNative - Changes to allow _compiling_ on windows.
authorJohn Hodge <[email protected]>
Sat, 6 Oct 2012 11:50:38 +0000 (19:50 +0800)
committerJohn Hodge <[email protected]>
Sat, 6 Oct 2012 11:50:38 +0000 (19:50 +0800)
- NOTE: Windows does not support fork(), time to fix my process creation code.

21 files changed:
AcessNative/acesskernel_src/Makefile
AcessNative/acesskernel_src/include/arch.h
AcessNative/acesskernel_src/include/threads_glue.h [new file with mode: 0644]
AcessNative/acesskernel_src/main.c
AcessNative/acesskernel_src/server.c
AcessNative/acesskernel_src/syscalls.c
AcessNative/acesskernel_src/threads.c
AcessNative/acesskernel_src/threads_glue.c [new file with mode: 0644]
AcessNative/ld-acess_src/Makefile
AcessNative/ld-acess_src/elf.c
AcessNative/ld-acess_src/elf32.h
AcessNative/ld-acess_src/elf64.h
AcessNative/ld-acess_src/elf_load.c
AcessNative/ld-acess_src/exports.c
AcessNative/ld-acess_src/exports.h
AcessNative/ld-acess_src/memory.c
AcessNative/ld-acess_src/syscalls.c
KernelLand/Kernel/debug.c
KernelLand/Kernel/include/acess.h
KernelLand/Kernel/include/logdebug.h [new file with mode: 0644]
Usermode/Libraries/ld-acess.so_src/elf.c

index fddfbcc..611509b 100644 (file)
@@ -25,7 +25,7 @@ N_OBJ := main.o
 BUILDINFO_OBJ := obj-$(PLATFORM)/buildinfo.o\r
 BUILDINFO_SRC := $(BUILDINFO_OBJ:%.o=%.c)\r
 \r
-OBJ := helpers.o threads.o server.o syscalls.o time.o\r
+OBJ := helpers.o threads.o threads_glue.o server.o syscalls.o time.o\r
 OBJ += video.o keyboard.o mouse.o nativefs.o vfs_handle.o ui_sdl.o\r
 OBJ := $(addprefix obj-$(PLATFORM)/,$(OBJ))\r
 N_OBJ := $(addprefix obj-$(PLATFORM)/,$(N_OBJ))\r
@@ -40,6 +40,7 @@ LDFLAGS += -lSDL -lSDLmain -g -Wl,--defsym,__buildnum=$(BUILD_NUM)
 \r
 ifeq ($(PLATFORM),win)\r
        BIN := ../AcessKernel.exe\r
+       LDFLAGS += -lws2_32 -lpthread\r
 endif\r
 ifeq ($(PLATFORM),lin)\r
        BIN := ../AcessKernel\r
index 03474cf..1307e0c 100644 (file)
@@ -5,7 +5,6 @@
 
 #include <stdint.h>
 //#include <stdlib.h>
-#include <pthread.h>
 #undef CLONE_VM
 #define        _MODULE_NAME_   "NativeKernel"
 
@@ -29,10 +28,13 @@ typedef intptr_t    tPAddr;
 
 typedef        int     BOOL;
 
+#include <stddef.h>
+#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 (file)
index 0000000..61346bd
--- /dev/null
@@ -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
+
index 609ea2a..e5d8bab 100644 (file)
@@ -13,6 +13,7 @@
 #endif
 #include <unistd.h>
 #include <string.h>
+#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);
        }
 
index 1c03d30..82d4e70 100644 (file)
@@ -9,16 +9,22 @@
 #include <string.h>
 #include <SDL/SDL.h>
 #ifdef __WIN32__
+# define _WIN32_WINNT 0x0501
 # include <windows.h>
-# include <winsock.h>
+# include <winsock2.h>
+# include <ws2tcpip.h>
+# define close(fd)     closesocket(fd)
+typedef int    socklen_t;
 #else
 # include <unistd.h>
 # include <sys/socket.h>
 # include <netinet/in.h>
 # include <arpa/inet.h>        // inet_ntop
+# include "include/windows_helpers.h"
 #endif
+#define DONT_INCLUDE_SYSCALL_NAMES
 #include "../syscalls.h"
-//#include <debug.h>
+#include <logdebug.h>  // acess but std
 #include <errno.h>
 
 #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");
index b636d81..446da3d 100644 (file)
@@ -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,
index 9de6140..9400d72 100644 (file)
@@ -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 <arch.h>
-#undef NULL    // Remove acess definition
 #include <acess.h>
 #include <mutex.h>
-#include <semaphore.h>
+#include "../../KernelLand/Kernel/include/semaphore.h"
 #include <rwlock.h>
 #include <events.h>
 #include <threads_int.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>
+#include <limits.h>
+#include "include/threads_glue.h"
 
 #define THREAD_EVENT_WAKEUP    0x80000000
 
 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 (file)
index 0000000..ba407b8
--- /dev/null
@@ -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 <rwlock.h>
+
+// - Native headers
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdint.h>
+//#include "/usr/include/signal.h"
+#include <SDL/SDL.h>
+#include <pthread.h>
+#include <assert.h>
+
+#include <logdebug.h>  // 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 );
+}
+
index 805b3a5..bbaef1d 100644 (file)
@@ -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 $@ > [email protected]
 
 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)
 
index f7aa1a5..3c15fb9 120000 (symlink)
@@ -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"
index a693e8d..dfecd71 120000 (symlink)
@@ -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"
index 272e8de..6edb566 120000 (symlink)
@@ -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"
index 909c24a..d661622 100644 (file)
@@ -8,6 +8,7 @@
 #include <stdio.h>\r
 #include <string.h>\r
 #include <unistd.h>\r
+#include <inttypes.h>  // PRIx64\r
 #include "common.h"\r
 #include "elf32.h"\r
 #include "elf64.h"\r
@@ -288,8 +289,8 @@ void *Elf64Load(int FD, Elf64_Ehdr *hdr)
                addr = phtab[i].p_vaddr + baseDiff;\r
 \r
                if( AllocateMemory( addr, phtab[i].p_memsz ) ) {\r
-                       fprintf(stderr, "Elf_Load: Unable to map memory at %llx (0x%llx bytes)\n",\r
-                               (long long)addr, (long long)phtab[i].p_memsz);\r
+                       fprintf(stderr, "Elf_Load: Unable to map memory at %"PRIx64" (0x%"PRIx64" bytes)\n",\r
+                               (uint64_t)addr, (uint64_t)phtab[i].p_memsz);\r
                        free( phtab );\r
                        return NULL;\r
                }\r
index cd1924b..47f741f 100644 (file)
@@ -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);
index 121a3ae..880e74c 100644 (file)
@@ -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);
index 2e9612c..3f0091d 100644 (file)
@@ -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
 }
index 91b8de6..6680186 100644 (file)
@@ -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);
index 31d3b39..ab3dd67 100644 (file)
@@ -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;
index 41854a9..27dbb2a 100644 (file)
@@ -9,6 +9,8 @@
  * \brief Acess2 Kernel API Core
  */
 
+#include <arch.h>
+
 //! NULL Pointer
 #define NULL   ((void*)0)
 //! Pack a structure
@@ -34,7 +36,6 @@
  * \}
  */
 
-#include <arch.h>
 #include <stdarg.h>
 #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 (file)
index 0000000..13d3b82
--- /dev/null
@@ -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 <stdarg.h>
+
+// --- 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
+
index 4b081bc..dc9dca3 100644 (file)
@@ -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)

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