Kernel mapping
Posted: Sun Jun 24, 2018 3:07 pm
To describe the title more accurately:
My kernel linker script uses the VMA 0xffffffff80200000. I'm using the separate loader approach from OSDev tutorials, loading a 64-bit kernel by jumping to it from a 32-bit separate loader.
I recently found a file from kernel.org which lists the different virtual memory map entries using the PML4 page table (https://www.kernel.org/doc/Documentatio ... _64/mm.txt).
In there I tried to adhere to the standard mapping, especially both the phys memory mapping:
with 1GiB of RAM -> 0xffff880000000000 to 0xffff880040000000 on 2MiB pages, and the kernel text mapping:
with 512MiB of RAM, just as stated in the file, on 4KiB pages.
However, I am currently mapping the kernel elf file directly, by only mapping the size of the kernel itself (in my current case, 0xf030 bytes), from 0xffffffff80200000 to 0xffffffff8020f030.
The problem seems to be clear: the first mapping of 512 MB completely surrounds the second mapping. Meaning, the virtual addresses overlap:
elf64_ehdr is the header from the kernel elf.
While the first mapping is from the standard of virtual memory layout, the second mapping is needed to make the OSDev tutorial work.
How do I deal with this issue? It should be noted that I want to make the virtual memory layout adhere to the one listed in the file. I thought about using either one of these approaches:
While the second mapping (the mapping of the kernel elf) is my current approach and it works perfectly fine, I am trying to find reasons to change it to the layout in the link. By changing to the other approach (kernel text mapping, 512MiB), I would need to rewrite my code so that the kernel entry point is still reachable.
My kernel linker script uses the VMA 0xffffffff80200000. I'm using the separate loader approach from OSDev tutorials, loading a 64-bit kernel by jumping to it from a 32-bit separate loader.
I recently found a file from kernel.org which lists the different virtual memory map entries using the PML4 page table (https://www.kernel.org/doc/Documentatio ... _64/mm.txt).
In there I tried to adhere to the standard mapping, especially both the phys memory mapping:
Code: Select all
ffff880000000000 - ffffc7ffffffffff (=64 TB) direct mapping of all phys. memory
Code: Select all
ffffffff80000000 - ffffffff9fffffff (=512 MB) kernel text mapping, from phys 0
However, I am currently mapping the kernel elf file directly, by only mapping the size of the kernel itself (in my current case, 0xf030 bytes), from 0xffffffff80200000 to 0xffffffff8020f030.
The problem seems to be clear: the first mapping of 512 MB completely surrounds the second mapping. Meaning, the virtual addresses overlap:
Code: Select all
void bootstrap_map_kernel(elf64_ehdr_t *elf64_ehdr)
{
elf64_phdr_t *elf64_phdr = get_elf64_phdr(elf64_ehdr);
for(uint32_t i = 0; i < elf64_ehdr->e_phnum; i++) {
elf64_phdr_t *elf64_phdr_segment = &elf64_phdr[i];
uint64_t vaddr = elf64_phdr_segment->p_vaddr;
uint64_t paddr = void_ptrtu32(elf64_ehdr) + elf64_phdr_segment->p_offset;
int64_t memsz = elf64_phdr_segment->p_memsz & LONG_MASK;
mmap_4kb(vaddr, paddr, X86_64_PTE_FLAG_PRESENT_RW, memsz);
}
}
While the first mapping is from the standard of virtual memory layout, the second mapping is needed to make the OSDev tutorial work.
How do I deal with this issue? It should be noted that I want to make the virtual memory layout adhere to the one listed in the file. I thought about using either one of these approaches:
While the second mapping (the mapping of the kernel elf) is my current approach and it works perfectly fine, I am trying to find reasons to change it to the layout in the link. By changing to the other approach (kernel text mapping, 512MiB), I would need to rewrite my code so that the kernel entry point is still reachable.