From b7f014bb53b817944bb14640ebf8cfc755d4ce56 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 30 Sep 2012 23:02:48 +0800 Subject: [PATCH] Kernel - x86_64 booting again (with some hackery) --- KernelLand/Kernel/arch/x86_64/Makefile | 2 +- KernelLand/Kernel/arch/x86_64/desctab.asm | 15 +++++---- KernelLand/Kernel/arch/x86_64/lib.c | 3 ++ KernelLand/Kernel/arch/x86_64/main.c | 40 ++++++++++++++++++++++- KernelLand/Kernel/arch/x86_64/mm_virt.c | 6 ++-- KernelLand/Kernel/bin/elf.c | 6 ++-- KernelLand/Kernel/binary.c | 3 ++ KernelLand/Kernel/modules.c | 4 +-- KernelLand/Kernel/system.c | 2 +- Usermode/Libraries/ld-acess.so_src/elf.c | 8 ++--- 10 files changed, 68 insertions(+), 21 deletions(-) diff --git a/KernelLand/Kernel/arch/x86_64/Makefile b/KernelLand/Kernel/arch/x86_64/Makefile index 4c030a58..17adec90 100644 --- a/KernelLand/Kernel/arch/x86_64/Makefile +++ b/KernelLand/Kernel/arch/x86_64/Makefile @@ -26,7 +26,7 @@ endif A_OBJ := start32.ao start64.ao desctab.ao proc.ao A_OBJ += main.o lib.o proc.o mm_virt.o mm_phys.o A_OBJ += kernelpanic.o errors.o time.o pci.o -A_OBJ += vm8086.o +A_OBJ += vm8086.o vpci.o A_OBJ += ../x86/mboot.o # rme.o diff --git a/KernelLand/Kernel/arch/x86_64/desctab.asm b/KernelLand/Kernel/arch/x86_64/desctab.asm index 6e8aa639..22b41be0 100644 --- a/KernelLand/Kernel/arch/x86_64/desctab.asm +++ b/KernelLand/Kernel/arch/x86_64/desctab.asm @@ -5,6 +5,7 @@ [BITS 64] [extern Log] +[extern Log_Debug] [extern gGDTPtr] [extern gGDT] @@ -167,11 +168,11 @@ IRQ_AddHandler: push rax push rdx sub rsp, 8 - mov rcx, rdi ; IRQ Number - mov rdx, rsi ; Callback - mov rsi, rax ; Pointer - mov rdi, csIRQ_Assigned - call Log + mov rcx, rsi ; IRQ Number + mov rdx, rdi ; Callback + mov rsi, csIRQ_Assigned + mov rdi, csIRQ_Tag + call Log_Debug add rsp, 8 pop rdx pop rax @@ -189,9 +190,11 @@ IRQ_AddHandler: [section .rodata] csIRQ_Assigned: - db "IRQ %p := %p (IRQ %i)",0 + db "IRQ %i .= %p",0 csIRQ_Fired: db "IRQ %i fired",0 +csIRQ_Tag: + db "IRQ",0 [section .text] %macro ISR_NOERRNO 1 diff --git a/KernelLand/Kernel/arch/x86_64/lib.c b/KernelLand/Kernel/arch/x86_64/lib.c index 5b9609a6..385b677a 100644 --- a/KernelLand/Kernel/arch/x86_64/lib.c +++ b/KernelLand/Kernel/arch/x86_64/lib.c @@ -373,3 +373,6 @@ Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem) return ret; } +EXPORT(memcpy); +EXPORT(memset); + diff --git a/KernelLand/Kernel/arch/x86_64/main.c b/KernelLand/Kernel/arch/x86_64/main.c index 0348de68..a855a474 100644 --- a/KernelLand/Kernel/arch/x86_64/main.c +++ b/KernelLand/Kernel/arch/x86_64/main.c @@ -14,6 +14,7 @@ // === CONSTANTS === #define KERNEL_LOAD 0x100000 #define MAX_PMEMMAP_ENTS 16 +#define MAX_ARGSTR_POS (0x200000-0x2000) // === IMPORTS === extern void Desctab_Init(void); @@ -28,6 +29,8 @@ void kmain(Uint MbMagic, void *MbInfoPtr); // === GLOBALS == char *gsBootCmdLine = NULL; +tBootModule *gaArch_BootModules; + int giArch_NumBootModules = 0; // === CODE === void kmain(Uint MbMagic, void *MbInfoPtr) @@ -50,6 +53,7 @@ void kmain(Uint MbMagic, void *MbInfoPtr) // Adjust Multiboot structure address mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE ); gsBootCmdLine = (char*)( (Uint)mbInfo->CommandLine + KERNEL_BASE); + // TODO: ref above? nPMemMapEnts = Multiboot_LoadMemoryMap(mbInfo, KERNEL_BASE, pmemmap, MAX_PMEMMAP_ENTS, KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_BASE ); @@ -63,6 +67,13 @@ void kmain(Uint MbMagic, void *MbInfoPtr) MM_InitPhys( nPMemMapEnts, pmemmap ); // Set up physical memory manager Log("gsBootCmdLine = '%s'", gsBootCmdLine); + switch(MbMagic) + { + case MULTIBOOT_MAGIC: + MM_RefPhys( mbInfo->CommandLine ); + break; + } + *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'D'; Heap_Install(); @@ -76,6 +87,8 @@ void kmain(Uint MbMagic, void *MbInfoPtr) Log_Log("Arch", "Starting VFS..."); VFS_Init(); + gaArch_BootModules = Multiboot_LoadModules(mbInfo, KERNEL_BASE, &giArch_NumBootModules); + *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'Z'; // Pass on to Independent Loader @@ -89,7 +102,32 @@ void kmain(Uint MbMagic, void *MbInfoPtr) void Arch_LoadBootModules(void) { - + int i, j, numPages; + Log("gsBootCmdLine = '%s'", gsBootCmdLine); + for( i = 0; i < giArch_NumBootModules; i ++ ) + { + Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString); + + if( !Module_LoadMem( gaArch_BootModules[i].Base, + gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString + ) ) + { + Log_Warning("Arch", "Unable to load module"); + } + + // Unmap and free + numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12; + MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages ); + + for( j = 0; j < numPages; j++ ) + MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) ); + + if( (tVAddr) gaArch_BootModules[i].ArgString < KERNEL_BASE ) + MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 ); + } + Log_Log("Arch", "Boot modules loaded"); + if( gaArch_BootModules ) + free( gaArch_BootModules ); } void StartupPrint(const char *String) diff --git a/KernelLand/Kernel/arch/x86_64/mm_virt.c b/KernelLand/Kernel/arch/x86_64/mm_virt.c index 14117f48..0953cbf8 100644 --- a/KernelLand/Kernel/arch/x86_64/mm_virt.c +++ b/KernelLand/Kernel/arch/x86_64/mm_virt.c @@ -340,8 +340,8 @@ void MM_DumpTables(tVAddr Start, tVAddr End) expected |= expected_pml4 & PF_NX; expected |= expected_pdp & PF_NX; expected |= expected_pd & PF_NX; - Log("expected (pml4 = %x, pdp = %x, pd = %x)", - expected_pml4, expected_pdp, expected_pd); +// Log("expected (pml4 = %x, pdp = %x, pd = %x)", +// expected_pml4, expected_pdp, expected_pd); // Dump MM_int_DumpTablesEnt( rangeStart, curPos - rangeStart, expected ); expected = CHANGEABLE_BITS; @@ -1070,8 +1070,6 @@ tVAddr MM_NewWorkerStack(void *StackData, size_t StackSize) tmp_addr = MM_MapTemp(phys); dest = (char*)tmp_addr + (0x1000 - StackSize); memcpy( dest, StackData, StackSize ); - Log_Debug("MM", "MM_NewWorkerStack: %p->%p %i bytes (i=%i)", StackData, dest, StackSize, i); - Log_Debug("MM", "MM_NewWorkerStack: ret = %p", ret); MM_FreeTemp(tmp_addr); } diff --git a/KernelLand/Kernel/bin/elf.c b/KernelLand/Kernel/bin/elf.c index b0d96cac..76724bc2 100644 --- a/KernelLand/Kernel/bin/elf.c +++ b/KernelLand/Kernel/bin/elf.c @@ -8,7 +8,9 @@ #define _COMMON_H #define SysDebug(v...) LOG(v) -#define DISABLE_ELF64 +#if BITS <= 32 +# define DISABLE_ELF64 +#endif void *GetSymbol(const char *Name, size_t *Size); void *GetSymbol(const char *Name, size_t *Size) { Uint val; Binary_GetSymbol(Name, &val); if(Size)*Size=0; return (void*)val; }; #define AddLoaded(a,b) do{}while(0) @@ -291,7 +293,7 @@ tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header) int Elf_Relocate(void *Base) { - return ElfRelocate(Base, (char**){NULL}, "") != NULL; + return ElfRelocate(Base, (char**){NULL}, "") != NULL; } int Elf_GetSymbol(void *Base, const char *Name, Uint *ret) { diff --git a/KernelLand/Kernel/binary.c b/KernelLand/Kernel/binary.c index de94c6f6..92baa737 100644 --- a/KernelLand/Kernel/binary.c +++ b/KernelLand/Kernel/binary.c @@ -822,9 +822,12 @@ Uint Binary_GetSymbolEx(const char *Name, Uint *Value) tKernelBin *pKBin; int numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol); + LOG("numKSyms = %i", numKSyms); + // Scan Kernel for( i = 0; i < numKSyms; i++ ) { + LOG("KSym %s = %p", gKernelSymbols[i].Name, gKernelSymbols[i].Value); if(strcmp(Name, gKernelSymbols[i].Name) == 0) { *Value = gKernelSymbols[i].Value; return 1; diff --git a/KernelLand/Kernel/modules.c b/KernelLand/Kernel/modules.c index 544e8661..c5e7f1bc 100644 --- a/KernelLand/Kernel/modules.c +++ b/KernelLand/Kernel/modules.c @@ -366,7 +366,7 @@ int Module_LoadFile(const char *Path, const char *ArgString) // TODO: I need a way of relocating the dependencies before everything else, so // they can be resolved before any other link errors if( !Binary_Relocate(base) ) { - Log_Warning("Relocation of module %s failed", Path); + Log_Warning("Module", "Relocation of module %s failed", Path); Binary_Unload(base); return 0; } @@ -389,7 +389,7 @@ int Module_LoadFile(const char *Path, const char *ArgString) } if( !Module_int_ResolveDeps(info) ) { - Log_Warning("Dependencies not met for '%s'", Path); + Log_Warning("Module", "Dependencies not met for '%s'", Path); Binary_Unload(base); return 0; } diff --git a/KernelLand/Kernel/system.c b/KernelLand/Kernel/system.c index aa1cad82..9224db55 100644 --- a/KernelLand/Kernel/system.c +++ b/KernelLand/Kernel/system.c @@ -4,7 +4,7 @@ * system.c * - Architecture Independent System Init */ -#define DEBUG 0 +#define DEBUG 1 #include #include diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index 92237eaa..4b081bcb 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -528,7 +528,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) int i; Elf64_Ehdr *hdr = Base; Elf64_Phdr *phtab; - Elf64_Dyn *dyntab; + Elf64_Dyn *dyntab = NULL; Elf64_Addr compiledBase = -1, baseDiff; Elf64_Sym *symtab = NULL; char *strtab = NULL; @@ -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 = Base + hdr->e_phoff; + phtab = (void*)((uintptr_t)Base + hdr->e_phoff); for( i = 0; i < hdr->e_phnum; i ++ ) { if(phtab[i].p_type == PT_DYNAMIC) @@ -677,8 +677,8 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) break; case R_X86_64_COPY: { size_t size; - void *sym = GetSymbol(symname, &size); - memcpy(ptr, sym, size); + void *symptr = GetSymbol(symname, &size); + memcpy(ptr, symptr, size); } break; case R_X86_64_GLOB_DAT: *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL); -- 2.20.1