AcessNative - Little fixes to get it running on 64-bit
authorJohn Hodge <[email protected]>
Tue, 14 Feb 2012 07:33:32 +0000 (15:33 +0800)
committerJohn Hodge <[email protected]>
Tue, 14 Feb 2012 07:33:32 +0000 (15:33 +0800)
- Still broken though :)

AcessNative/acesskernel_src/main.c
AcessNative/acesskernel_src/syscalls.c
AcessNative/ld-acess_src/Makefile
AcessNative/ld-acess_src/elf_load.c
AcessNative/ld-acess_src/exports.c
AcessNative/ld-acess_src/main.c

index f3ab196..fbb457c 100644 (file)
@@ -31,7 +31,7 @@ extern const char     gsGitHash[];
 extern int     giBuildNumber;
 
 // === GLOBALS ===
-const char     *gsAcessDir = "../Usermode/Output/x86";
+const char     *gsAcessDir = "../Usermode/Output/x86_64";
 
 // === CODE ===
 int main(int argc, char *argv[])
@@ -68,8 +68,12 @@ int main(int argc, char *argv[])
        // Initialise VFS
        VFS_Init();
        // - Start IO Drivers
-       Video_Install(NULL);
-       NativeKeyboard_Install(NULL);
+       if( Video_Install(NULL) ) {
+               Log_Error("Init", "Unable to load NativeVideo");
+       }
+       if( NativeKeyboard_Install(NULL) ) {
+               Log_Error("Init", "Unable to load NativeKeyboard");
+       }
        NativeFS_Install(NULL);
        // - Start VTerm
        {
index 4d85e8c..d5c051b 100644 (file)
@@ -272,7 +272,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
        }
        formatString[i] = '\0';
        
-       LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
+       //LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
        
        {
                char    argListData[argListLen];
@@ -286,19 +286,19 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                        case ARG_TYPE_VOID:
                                break;
                        case ARG_TYPE_INT32:
-                               LOG("%i INT32: 0x%x", i, *(Uint32*)inData);
+                               //LOG("%i INT32: 0x%x", i, *(Uint32*)inData);
                                *(Uint32*)&argListData[argListLen] = *(Uint32*)inData;
                                argListLen += sizeof(Uint32);
                                inData += sizeof(Uint32);
                                break;
                        case ARG_TYPE_INT64:
-                               LOG("%i INT64: 0x%llx", i, *(Uint64*)inData);
+                               //LOG("%i INT64: 0x%llx", i, *(Uint64*)inData);
                                *(Uint64*)&argListData[argListLen] = *(Uint64*)inData;
                                argListLen += sizeof(Uint64);
                                inData += sizeof(Uint64);
                                break;
                        case ARG_TYPE_STRING:
-                               LOG("%i STR: '%s'", i, (char*)inData);
+                               //LOG("%i STR: '%s'", i, (char*)inData);
                                *(char**)&argListData[argListLen] = (char*)inData;
                                argListLen += sizeof(void*);
                                inData += Request->Params[i].Length;
@@ -319,16 +319,16 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                                {
                                        // Allocate and zero the buffer
                                        returnData[i] = calloc(1, Request->Params[i].Length);
-                                       LOG("%i ZDAT: %i %p", i,
-                                               Request->Params[i].Length, returnData[i]);
+                                       //LOG("%i ZDAT: %i %p", i,
+                                       //      Request->Params[i].Length, returnData[i]);
                                        *(void**)&argListData[argListLen] = returnData[i];
                                        argListLen += sizeof(void*);
                                }
                                else
                                {
                                        returnData[i] = (void*)inData;
-                                       LOG("%i DATA: %i %p", i,
-                                               Request->Params[i].Length, returnData[i]);
+                                       //LOG("%i DATA: %i %p", i,
+                                       //      Request->Params[i].Length, returnData[i]);
                                        *(void**)&argListData[argListLen] = (void*)inData;
                                        argListLen += sizeof(void*);
                                        inData += Request->Params[i].Length;
index bbce43d..7d82132 100644 (file)
@@ -14,10 +14,14 @@ ifeq ($(PLATFORM),win)
 endif
 ifeq ($(PLATFORM),lin)
        BIN := ../ld-acess
-       LD += -m elf_i386
+#      LD += -m elf_i386
 endif
 
-CFLAGS += -Wall -Werror -g -m32
+CFLAGS   += -Wall
+CFLAGS   += -Werror
+CFLAGS   += -g
+CPPFLAGS += -DARCHDIR_is_x86_64=1
+LDFLAGS  += -g -Wl,-T,obj-$(PLATFORM)/link.ld
 
 DEPFILES  = $(filter %.o,$(OBJ))
 DEPFILES := $(DEPFILES:%=%.dep)
@@ -27,10 +31,10 @@ DEPFILES := $(DEPFILES:%=%.dep)
 all: $(BIN)
 
 clean:
-       $(RM) $(BIN) $(OBJ) $(DEPFILES)
+       $(RM) $(BIN) $(OBJ) $(DEPFILES) obj-$(PLATFORM)/link.ld
 
 $(BIN): obj-$(PLATFORM)/link.ld $(OBJ)
-       $(CC) -g -o $@ $(OBJ) -m32 -Wl,-T,obj-$(PLATFORM)/link.ld
+       $(CC) $(LDFLAGS) -o $@ $(OBJ)
 
 obj-$(PLATFORM)/%.o: %.c
        @mkdir -p $(dir $@)
@@ -42,7 +46,7 @@ obj-$(PLATFORM)/%.o: %.c
 obj-lin/link.ld:
        @mkdir -p $(dir $@)
        @echo "Making Linker Script ($@)"
-       @$(LD) --verbose | awk '{ if( substr($$0,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/\b0x0[08][0-9]*\b/0x00100000/g' > $@
+       $(LD) --verbose | awk '{ if( substr($$0,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/\b0x[048][0-9]*\b/0x00200000/g' > $@
 
 -include $(DEPFILES)
 
index 9e5a032..590da37 100644 (file)
@@ -1,5 +1,6 @@
 /*\r
- * Acess v0.1\r
+ * Acess2 - AcessNative\r
+ *\r
  * ELF Executable Loader Code\r
  */\r
 #define DEBUG  1\r
@@ -9,6 +10,7 @@
 #include <unistd.h>\r
 #include "common.h"\r
 #include "elf32.h"\r
+#include "elf64.h"\r
 \r
 #define DEBUG_WARN     1\r
 \r
@@ -20,7 +22,7 @@
 # define ENTER(...)    printf("%s: ---- ENTER ----\n", __func__);\r
 # define LOG(s, ...)   printf("%s: " s, __func__, __VA_ARGS__)\r
 # define LOGS(s)       printf("%s: " s, __func__)\r
-# define LEAVE(...)\r
+# define LEAVE(...)    printf("%s: ---- LEAVE ----\n", __func__);\r
 #else\r
 # define ENTER(...)\r
 # define LOG(...)\r
 // === PROTOTYPES ===\r
 void   *Elf_Load(int FD);\r
 void   *Elf32Load(int FD, Elf32_Ehdr *hdr);\r
+void   *Elf64Load(int FD, Elf64_Ehdr *hdr);\r
 \r
 // === CODE ===\r
 void *Elf_Load(int FD)\r
 {\r
-       Elf32_Ehdr      hdr;\r
+       Elf64_Ehdr      hdr;\r
        \r
        // Read ELF Header\r
        acess_read(FD, &hdr, sizeof(hdr));\r
@@ -49,11 +52,15 @@ void *Elf_Load(int FD)
        switch(hdr.e_ident[4])\r
        {\r
        case ELFCLASS32:\r
-               return Elf32Load(FD, &hdr);\r
+               return Elf32Load(FD, (void*)&hdr);\r
+       case ELFCLASS64:\r
+               return Elf64Load(FD, &hdr);\r
        default:\r
+               Warning("Unknown ELF class (%i)", hdr.e_ident[4]);\r
                return NULL;\r
        }\r
 }\r
+\r
 void *Elf32Load(int FD, Elf32_Ehdr *hdr)\r
 {\r
        Elf32_Phdr      *phtab;\r
@@ -122,8 +129,8 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr)
        LOG("base = %08x, max = %08x\n", base, max);\r
 \r
        if( base == 0 ) {\r
-               // Find a nice space (31 address bits allowed)\r
-               base = FindFreeRange( max, 31 );\r
+               // Find a nice space (47 address bits allowed)\r
+               base = FindFreeRange( max, 47 );\r
                LOG("new base = %08x\n", base);\r
                if( base == 0 ) return NULL;\r
                baseDiff = base;\r
@@ -171,3 +178,131 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr)
        LEAVE('p', base);\r
        return PTRMK(void, base);\r
 }\r
+\r
+void *Elf64Load(int FD, Elf64_Ehdr *hdr)\r
+{\r
+       Elf64_Phdr      *phtab;\r
+        int    i;\r
+        int    iPageCount;\r
+       uint64_t        max, base;\r
+       uint64_t        addr;\r
+       uint64_t        baseDiff = 0;\r
+       \r
+       ENTER("iFD", FD);\r
+       \r
+       #if BITS <= 32\r
+       Warning("ELF64 being loaded in 32-bit env, this may not work");\r
+       #endif\r
+\r
+       // Check for a program header\r
+       if(hdr->e_phoff == 0) {\r
+               #if DEBUG_WARN\r
+               Warning("ELF File does not contain a program header\n");\r
+               #endif\r
+               LEAVE('n');\r
+               return NULL;\r
+       }\r
+       \r
+       // Read Program Header Table\r
+       phtab = malloc( sizeof(Elf64_Phdr) * hdr->e_phnum );\r
+       if( !phtab ) {\r
+               LEAVE('n');\r
+               return NULL;\r
+       }\r
+       LOG("hdr.phoff = 0x%08llx\n", (long long)hdr->e_phoff);\r
+       acess_seek(FD, hdr->e_phoff, ACESS_SEEK_SET);\r
+       acess_read(FD, phtab, sizeof(Elf64_Phdr) * hdr->e_phnum);\r
+       \r
+       // Count Pages\r
+       iPageCount = 0;\r
+       LOG("hdr.phentcount = %i\n", hdr->e_phnum);\r
+       for( i = 0; i < hdr->e_phnum; i++ )\r
+       {\r
+               // Ignore Non-LOAD types\r
+               if(phtab[i].p_type != PT_LOAD)\r
+                       continue;\r
+               iPageCount += ((phtab[i].p_vaddr&0xFFF) + phtab[i].p_memsz + 0xFFF) >> 12;\r
+               LOG("phtab[%i] = {VAddr:0x%llx, MemSize:0x%llx}\n",\r
+                       i, (long long)phtab[i].p_vaddr, (long long)phtab[i].p_memsz);\r
+       }\r
+       \r
+       LOG("iPageCount = %i\n", iPageCount);\r
+       \r
+       // Allocate Information Structure\r
+       //ret = malloc( sizeof(tBinary) + sizeof(tBinaryPage)*iPageCount );\r
+       // Fill Info Struct\r
+       //ret->Entry = hdr.entrypoint;\r
+       //ret->Base = -1;               // Set Base to maximum value\r
+       //ret->NumPages = iPageCount;\r
+       //ret->Interpreter = NULL;\r
+\r
+       // Prescan for base and size\r
+       max = 0;\r
+       base = 0xFFFFFFFF;\r
+       for( i = 0; i < hdr->e_phnum; i ++)\r
+       {\r
+               if( phtab[i].p_type != PT_LOAD )\r
+                       continue;\r
+               if( phtab[i].p_vaddr < base )\r
+                       base = phtab[i].p_vaddr;\r
+               if( phtab[i].p_vaddr + phtab[i].p_memsz > max )\r
+                       max = phtab[i].p_vaddr + phtab[i].p_memsz;\r
+       }\r
+\r
+       LOG("base = %08lx, max = %08lx\n", base, max);\r
+\r
+       if( base == 0 ) {\r
+               // Find a nice space (31 address bits allowed)\r
+               base = FindFreeRange( max, 31 );\r
+               LOG("new base = %08lx\n", base);\r
+               if( base == 0 ) return NULL;\r
+               baseDiff = base;\r
+       }\r
+       \r
+       // Load Pages\r
+       for( i = 0; i < hdr->e_phnum; i++ )\r
+       {\r
+               // Get Interpreter Name\r
+               if( phtab[i].p_type == PT_INTERP )\r
+               {\r
+                       char *tmp;\r
+                       //if(ret->Interpreter)  continue;\r
+                       tmp = malloc(phtab[i].p_filesz+1);\r
+                       tmp[ phtab[i].p_filesz ] = 0;\r
+                       acess_seek(FD, phtab[i].p_offset, ACESS_SEEK_SET);\r
+                       acess_read(FD, tmp, phtab[i].p_filesz);\r
+                       //ret->Interpreter = Binary_RegInterp(tmp);\r
+                       LOG("Interpreter '%s'\n", tmp);\r
+                       free(tmp);\r
+                       continue;\r
+               }\r
+               // Ignore non-LOAD types\r
+               if(phtab[i].p_type != PT_LOAD)  continue;\r
+               \r
+               LOG("phtab[%i] = PT_LOAD {Adj VAddr:0x%llx, Offset:0x%llx, FileSize:0x%llx, MemSize:0x%llx}\n",\r
+                       i,\r
+                       (long long)phtab[i].p_vaddr+baseDiff, (long long)phtab[i].p_offset,\r
+                       (long long)phtab[i].p_filesz, (long long)phtab[i].p_memsz\r
+                       );\r
+               \r
+               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
+                       free( phtab );\r
+                       return NULL;\r
+               }\r
+               \r
+               acess_seek(FD, phtab[i].p_offset, ACESS_SEEK_SET);\r
+               acess_read(FD, PTRMK(void, addr), phtab[i].p_filesz);\r
+               memset( PTRMK(char, addr) + phtab[i].p_filesz, 0, phtab[i].p_memsz - phtab[i].p_filesz );\r
+       }\r
+       \r
+       // Clean Up\r
+       free(phtab);\r
+       // Return\r
+       LEAVE('p', base);\r
+       return PTRMK(void, base);\r
+}\r
+\r
index 947e0d3..5f97bb2 100644 (file)
@@ -9,6 +9,7 @@
 #include "../syscalls.h"
 #include "exports.h"
 #include <stdarg.h>
+#include <stddef.h>
 
 #define DEBUG(v...)    Debug(v)
 
@@ -93,7 +94,7 @@ int acess_ioctl(int fd, int id, void *data) {
        return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, 1024, data);
 }
 int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
-       DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
+//     DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
        DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
        return _Syscall(SYS_FINFO, ">i <d >i",
                fd,
index cbe0ac1..cd2b815 100644 (file)
@@ -20,7 +20,7 @@ int main(int argc, char *argv[], char **envp)
         int    appArgc;
        char    **appArgv;
        char    *appPath = NULL;
-        int    (*appMain)(int, char *[], char **) __attribute__((cdecl));
+        int    (*appMain)(int, char *[], char **);
        void    *base;
         int    rv;
        

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