Fix paging to completion.

More debug messages, some things are slightly broken. We can recover.
This commit is contained in:
Curle 2021-03-18 20:33:15 +00:00
parent bd9f994648
commit 98f3786e8b
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
5 changed files with 48 additions and 32 deletions

View File

@ -35,6 +35,7 @@
"$root/chroma/system/memory/abstract_allocator.c", "$root/chroma/system/memory/abstract_allocator.c",
"$root/chroma/system/memory/physmem.c", "$root/chroma/system/memory/physmem.c",
"$root/chroma/system/memory/paging.c", "$root/chroma/system/memory/paging.c",
"$root/chroma/system/memory/liballoc.c",
"$root/chroma/system/drivers/keyboard.c", "$root/chroma/system/drivers/keyboard.c",
"$root/chroma/system/drivers/elf.c" "$root/chroma/system/drivers/elf.c"
], ],

View File

@ -108,34 +108,34 @@
* *
*/ */
#define MMIO_REGION 0xFFFFFFFFF8000000ull // Cannot move! #define MMIO_REGION 0xFFFFFFFFF8000000ull // Cannot move!
#define FB_REGION 0xFFFFFFFFFC000000ull // Cannot move! #define FB_REGION 0xFFFFFFFFFC000000ull // Cannot move!
#define FB_PHYSICAL 0x00000000FD000000ull // Physical location of the Framebuffer #define FB_PHYSICAL (size_t) bootldr.fb_ptr // 0x00000000FD000000ull - Physical location of the Framebuffer
#define KERNEL_REGION 0xFFFFFFFFFFE00000ull // -2MiB, from bootloader #define KERNEL_REGION 0xFFFFFFFFFFE00000ull // -2MiB, from bootloader
#define KERNEL_TEXT 0x0000000000002000ull // Offset of symbols from the kernel text #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_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 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 CORE_STACK_END 0x0000000000015000ull
#define STACK_TOP 0xFFFFFFFFFFFFF000ull // The start of the highest stack #define STACK_TOP 0xFFFFFFFFFFFFF000ull // The start of the highest stack
#define MEM_CEILING 0xFFFFFFFFFFFFFFFFull // The top of the stack in the map #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_REGION 0xFFFFE00000000000ull // Kernel Stack Space
#define KERNEL_STACK_END 0xFFFFE00040000000ull // End of 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_REGION 0xFFFFE00080000000ull // Kernel Object Space (kmalloc will allocate into this region)
#define KERNEL_HEAP_END 0xFFFFE000C0000000ull // End of Kernel Object Space #define KERNEL_HEAP_END 0xFFFFE000C0000000ull // End of Kernel Object Space
#define DIRECT_REGION 0xFFFF800000000000ull #define DIRECT_REGION 0xFFFF800000000000ull
#define LOWER_REGION 0x0000000100000000ull // Lower Memory cutoff - 4GB #define LOWER_REGION 0x0000000100000000ull // Lower Memory cutoff - 4GB
#define PAGE_SHIFT 12 #define PAGE_SHIFT 12

View File

@ -20,9 +20,11 @@ address_space_t KernelAddressSpace;
int Main(void) { int Main(void) {
KernelAddressSpace = (address_space_t) {0}; KernelAddressSpace = (address_space_t) {0};
SerialPrintf("\r\n[ boot] Booting Chroma..\r\n"); 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] 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 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)))); 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(); InitPaging();
InitPrint();
WriteString("Paging complete. System initialized.");
for(;;) { } for(;;) { }

View File

@ -34,6 +34,14 @@ void InitPaging() {
.PML4 = PhysAllocateZeroMem(4096) .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"); SerialPrintf("[ Mem] Identity mapping the entirety of physical memory\r\n");
for(size_t i = 0; i < MemorySize / PAGE_SIZE; i++) { for(size_t i = 0; i < MemorySize / PAGE_SIZE; i++) {
@ -43,15 +51,21 @@ void InitPaging() {
// TODO: Map kernel mem // 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 // This allows the code to actually run
SerialPrintf("[ Mem] Mapping kernel\r\n"); 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 < KERNEL_END; i += PAGE_SIZE) 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); MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - KERNEL_PHYSICAL) + KERNEL_REGION, 0x3);
// This allows us to write to the screen // This allows us to write to the screen
SerialPrintf("[ Mem] Mapping framebuffer\r\n"); 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) for(size_t i = FB_PHYSICAL; i < bootldr.fb_size + FB_PHYSICAL; i += PAGE_SIZE) {
MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - FB_PHYSICAL) + FB_REGION, 0x3); MapVirtualPageNoDirect(&KernelAddressSpace, i, i, 0x3); // FD000000 + (page)
MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - FB_PHYSICAL) + FB_REGION, 0x3); // FFFFFFFFFC000000 + (page)
}
// This allows us to call functions // This allows us to call functions
SerialPrintf("[ Mem] Mapping stack\r\n"); SerialPrintf("[ Mem] Mapping stack\r\n");
@ -59,16 +73,12 @@ void InitPaging() {
// Make sure everything is sane // Make sure everything is sane
SerialPrintf("[ Mem] Diagnostic: Querying existing page tables\r\n"); 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 BootloaderAddress = DecodeVirtualAddressNoDirect(&BootloaderAddressSpace, AddressToFind);
size_t KernelDisoveredAddress = DecodeVirtualAddressNoDirect(&KernelAddressSpace, 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: 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.\r\n", AddressToFind, KernelDisoveredAddress); 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.."); SerialPrintf("[ Mem] %s\r\n", BootloaderAddress == KernelDisoveredAddress ? "These match. Continuing." : "These do not match. Continuing with caution..");
//if(BootloaderAddress != KernelDisoveredAddress) //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); 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); WriteControlRegister(3, (size_t) KernelAddressSpace.PML4 & STACK_TOP);
SerialPrintf("[ Mem] Worked\r\n"); SerialPrintf("[ Mem] Worked\r\n");
for(;;) {}
} }

View File

@ -45,7 +45,7 @@ typedef struct {
uint32_t scrlMode; uint32_t scrlMode;
} PRINTINFO; } PRINTINFO;
PRINTINFO PrintInfo = {0}; static PRINTINFO PrintInfo = {0};
void InitPrint() { void InitPrint() {
PrintInfo.charHeight = 8; PrintInfo.charHeight = 8;