diff --git a/build.json b/build.json index d30b074..4a961c9 100644 --- a/build.json +++ b/build.json @@ -35,6 +35,7 @@ "$root/chroma/system/memory/abstract_allocator.c", "$root/chroma/system/memory/physmem.c", "$root/chroma/system/memory/paging.c", + "$root/chroma/system/memory/liballoc.c", "$root/chroma/system/drivers/keyboard.c", "$root/chroma/system/drivers/elf.c" ], diff --git a/chroma/inc/kernel/system/memory.h b/chroma/inc/kernel/system/memory.h index 830df48..3888e57 100644 --- a/chroma/inc/kernel/system/memory.h +++ b/chroma/inc/kernel/system/memory.h @@ -108,34 +108,34 @@ * */ -#define MMIO_REGION 0xFFFFFFFFF8000000ull // Cannot move! -#define FB_REGION 0xFFFFFFFFFC000000ull // Cannot move! -#define FB_PHYSICAL 0x00000000FD000000ull // Physical location of the Framebuffer -#define KERNEL_REGION 0xFFFFFFFFFFE00000ull // -2MiB, from bootloader -#define KERNEL_TEXT 0x0000000000002000ull // Offset of symbols from the kernel text +#define MMIO_REGION 0xFFFFFFFFF8000000ull // Cannot move! +#define FB_REGION 0xFFFFFFFFFC000000ull // Cannot move! +#define FB_PHYSICAL (size_t) bootldr.fb_ptr // 0x00000000FD000000ull - Physical location of the Framebuffer +#define KERNEL_REGION 0xFFFFFFFFFFE00000ull // -2MiB, from bootloader +#define KERNEL_TEXT 0x0000000000002000ull // Offset of symbols from the kernel text -#define KERNEL_PHYSICAL KernelLocation // The located kernel from the bootstrap process +#define KERNEL_PHYSICAL KernelLocation // The located kernel from the bootstrap process #define KERNEL_END KernelLocation + (KernelEnd - KernelAddr) -#define KERNEL_OFFSET 0x0000000000039000ull // KERNEL_PHYSICAL -> KERNEL_PHYSICAL + KERNEL_OFFSET + KERNEL_REGION +#define KERNEL_OFFSET 0x0000000000039000ull // KERNEL_PHYSICAL -> KERNEL_PHYSICAL + KERNEL_OFFSET + KERNEL_REGION -#define CODE_STACK_PHYSICAL 0x0000000000006C00ull // The base of the stack running the C code we enter with +#define CODE_STACK_PHYSICAL 0x0000000000006C00ull // The base of the stack running the C code we enter with #define CODE_STACK_END 0x0000000000007C00ull -#define CORE_STACK_PHYSICAL 0x0000000000014000ull // The first CPU core's stack +#define CORE_STACK_PHYSICAL 0x0000000000014000ull // The first CPU core's stack #define CORE_STACK_END 0x0000000000015000ull -#define STACK_TOP 0xFFFFFFFFFFFFF000ull // The start of the highest stack -#define MEM_CEILING 0xFFFFFFFFFFFFFFFFull // The top of the stack in the map +#define STACK_TOP 0xFFFFFFFFFFFFF000ull // The start of the highest stack +#define MEM_CEILING 0xFFFFFFFFFFFFFFFFull // The top of the stack in the map -#define USER_REGION 0x00007FFFFFFFFFFFull // Not needed yet, but we're higher half so we might as well be thorough +#define USER_REGION 0x00007FFFFFFFFFFFull // Not needed yet, but we're higher half so we might as well be thorough -#define KERNEL_STACK_REGION 0xFFFFE00000000000ull // Kernel Stack Space -#define KERNEL_STACK_END 0xFFFFE00040000000ull // End of Kernel Stack Space +#define KERNEL_STACK_REGION 0xFFFFE00000000000ull // Kernel Stack Space +#define KERNEL_STACK_END 0xFFFFE00040000000ull // End of Kernel Stack Space -#define KERNEL_HEAP_REGION 0xFFFFE00080000000ull // Kernel Object Space (kmalloc will allocate into this region) -#define KERNEL_HEAP_END 0xFFFFE000C0000000ull // End of Kernel Object Space +#define KERNEL_HEAP_REGION 0xFFFFE00080000000ull // Kernel Object Space (kmalloc will allocate into this region) +#define KERNEL_HEAP_END 0xFFFFE000C0000000ull // End of Kernel Object Space #define DIRECT_REGION 0xFFFF800000000000ull -#define LOWER_REGION 0x0000000100000000ull // Lower Memory cutoff - 4GB +#define LOWER_REGION 0x0000000100000000ull // Lower Memory cutoff - 4GB #define PAGE_SHIFT 12 diff --git a/chroma/kernel.c b/chroma/kernel.c index 4aa0cdb..a6bfba8 100644 --- a/chroma/kernel.c +++ b/chroma/kernel.c @@ -20,9 +20,11 @@ address_space_t KernelAddressSpace; int Main(void) { KernelAddressSpace = (address_space_t) {0}; - + SerialPrintf("\r\n[ boot] Booting Chroma..\r\n"); + SerialPrintf("[ boot] Bootloader data structure at 0x%p\r\n", (size_t) &bootldr); SerialPrintf("[ boot] Kernel loaded at 0x%p, ends at 0x%p, is %d bytes long.\r\n", KernelAddr, KernelEnd, KernelEnd - KernelAddr); + SerialPrintf("[ boot] Framebuffer at 0x%p / 0x%p, is %dx%d, 0x%x bytes long.\r\n", bootldr.fb_ptr, (size_t) &fb, bootldr.fb_width, bootldr.fb_height, bootldr.fb_size); SerialPrintf("[ boot] Initrd is physically at 0x%p, and is %d bytes long.\r\n", bootldr.initrd_ptr, bootldr.initrd_size); SerialPrintf("[ boot] Initrd's header is 0x%p\r\n", FIXENDIAN32(*((volatile uint32_t*)(bootldr.initrd_ptr)))); @@ -48,6 +50,10 @@ int Main(void) { InitPaging(); + InitPrint(); + + WriteString("Paging complete. System initialized."); + for(;;) { } diff --git a/chroma/system/memory/paging.c b/chroma/system/memory/paging.c index 1ab92d5..869c9ec 100644 --- a/chroma/system/memory/paging.c +++ b/chroma/system/memory/paging.c @@ -34,6 +34,14 @@ void InitPaging() { .PML4 = PhysAllocateZeroMem(4096) }; + address_space_t BootloaderAddressSpace = (address_space_t) { + .Lock = {0}, + .PML4 = (size_t*) ReadControlRegister(3) + }; + + size_t AddressToFind = (size_t) &fb; + size_t BootldrAddress = 0x8000; + SerialPrintf("[ Mem] Identity mapping the entirety of physical memory\r\n"); for(size_t i = 0; i < MemorySize / PAGE_SIZE; i++) { @@ -43,15 +51,21 @@ void InitPaging() { // TODO: Map kernel mem } + SerialPrintf("[ Mem] Mapping 0x%x bytes of bootloader structure, starting at 0x%p\r\n", bootldr.size, BootldrAddress); + for(size_t i = BootldrAddress; i < (BootldrAddress + bootldr.size); i += PAGE_SIZE) + MapVirtualPageNoDirect(&KernelAddressSpace, i, KERNEL_REGION + (i - BootldrAddress), 0x3); + // This allows the code to actually run - SerialPrintf("[ Mem] Mapping kernel\r\n"); - for(size_t i = KERNEL_PHYSICAL + KERNEL_TEXT; i < KERNEL_END; i += PAGE_SIZE) + SerialPrintf("[ Mem] Mapping 0x%x bytes of kernel, starting at 0x%p\r\n", KernelEnd - KernelAddr, KERNEL_PHYSICAL + KERNEL_TEXT); + for(size_t i = KERNEL_PHYSICAL + KERNEL_TEXT; i < (KernelEnd - KernelAddr) + KERNEL_PHYSICAL; i += PAGE_SIZE) MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - KERNEL_PHYSICAL) + KERNEL_REGION, 0x3); // This allows us to write to the screen - SerialPrintf("[ Mem] Mapping framebuffer\r\n"); - for(size_t i = FB_PHYSICAL; i < bootldr.fb_size + FB_PHYSICAL; i += PAGE_SIZE) - MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - FB_PHYSICAL) + FB_REGION, 0x3); + SerialPrintf("[ Mem] Mapping 0x%x bytes of framebuffer, starting at 0x%p\r\n", bootldr.fb_size, FB_PHYSICAL); + for(size_t i = FB_PHYSICAL; i < bootldr.fb_size + FB_PHYSICAL; i += PAGE_SIZE) { + MapVirtualPageNoDirect(&KernelAddressSpace, i, i, 0x3); // FD000000 + (page) + MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - FB_PHYSICAL) + FB_REGION, 0x3); // FFFFFFFFFC000000 + (page) + } // This allows us to call functions SerialPrintf("[ Mem] Mapping stack\r\n"); @@ -59,16 +73,12 @@ void InitPaging() { // Make sure everything is sane SerialPrintf("[ Mem] Diagnostic: Querying existing page tables\r\n"); - address_space_t BootloaderAddressSpace = (address_space_t) { - .Lock = {0}, - .PML4 = (size_t*) ReadControlRegister(3) - }; - size_t AddressToFind = 0xffffffffffffff58; + AddressToFind = (size_t) &(bootldr); size_t BootloaderAddress = DecodeVirtualAddressNoDirect(&BootloaderAddressSpace, AddressToFind); size_t KernelDisoveredAddress = DecodeVirtualAddressNoDirect(&KernelAddressSpace, AddressToFind); - SerialPrintf("[ Mem] Diagnostic: Existing pagetables put 0x%p at 0x%p.\r\n", AddressToFind, BootloaderAddress); - SerialPrintf("[ Mem] Diagnostic: Our pagetables put 0x%p at 0x%p.\r\n", AddressToFind, KernelDisoveredAddress); + SerialPrintf("[ Mem] Diagnostic: Existing pagetables put 0x%p at 0x%p + 0x%p.\r\n", AddressToFind, BootloaderAddress, AddressToFind & ~STACK_TOP); + SerialPrintf("[ Mem] Diagnostic: Our pagetables put 0x%p at 0x%p + 0x%p.\r\n", AddressToFind, KernelDisoveredAddress, AddressToFind & ~STACK_TOP); SerialPrintf("[ Mem] %s\r\n", BootloaderAddress == KernelDisoveredAddress ? "These match. Continuing." : "These do not match. Continuing with caution.."); //if(BootloaderAddress != KernelDisoveredAddress) @@ -77,7 +87,6 @@ void InitPaging() { SerialPrintf("[ Mem] Attempting to jump into our new pagetables: 0x%p\r\n", (size_t) KernelAddressSpace.PML4); WriteControlRegister(3, (size_t) KernelAddressSpace.PML4 & STACK_TOP); SerialPrintf("[ Mem] Worked\r\n"); - for(;;) {} } diff --git a/chroma/video/draw.c b/chroma/video/draw.c index 4ea38d6..9575548 100644 --- a/chroma/video/draw.c +++ b/chroma/video/draw.c @@ -45,7 +45,7 @@ typedef struct { uint32_t scrlMode; } PRINTINFO; -PRINTINFO PrintInfo = {0}; +static PRINTINFO PrintInfo = {0}; void InitPrint() { PrintInfo.charHeight = 8;