Skip to content

Commit

Permalink
PT init refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
djphoenix committed May 19, 2019
1 parent 6e998c8 commit bcb17c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
8 changes: 4 additions & 4 deletions ld/system.ld
Expand Up @@ -20,7 +20,7 @@ SECTIONS
.reloc 0x400FE0 (NOLOAD) : ALIGN(1) {
LONG(0) LONG(10) SHORT(0)
} :text
.text 0x401000 : ALIGN(1) {
.text 0x401000 : ALIGN(0x1000) {
__text_start__ = . ;
*boot.a:bootstrap.o(.text .text.*)
*(.text .text.*)
Expand All @@ -29,15 +29,15 @@ SECTIONS
. = ALIGN(64);
__text_end__ = . ;
} :text
.rodata : ALIGN(1) {
.rodata : ALIGN(0x1000) {
. = ALIGN(8);
*(.modules)

. = ALIGN(8);
__data_start__ = . ;
*(.rodata .rodata.*)
} :rodata
.data : ALIGN(1) {
.data : ALIGN(0x1000) {
. = ALIGN(8);
__VTABLE_START__ = . ;
__CTOR_LIST__ = . ; QUAD(0) *(.init_array*) *(.ctor*) QUAD(0)
Expand All @@ -49,7 +49,7 @@ SECTIONS
*(.data .data.*)
__data_end__ = . ;
} :data
.bss (NOLOAD) : ALIGN(1) {
.bss (NOLOAD) : ALIGN(0x1000) {
__bss_start__ = . ;
*(.bss .bss.*)
*(COMMON)
Expand Down
35 changes: 17 additions & 18 deletions src/platform/pagetable.cpp
Expand Up @@ -19,7 +19,7 @@ static void fillPages(uintptr_t low, uintptr_t top, PTE *pagetable, uint8_t flag
*PTE::find(low, pagetable) = PTE(low, flags);
}

static inline void fillPages(void *low, void *top, PTE *pagetable, uint8_t flags = 3) {
static inline void fillPages(const void *low, const void *top, PTE *pagetable, uint8_t flags = 3) {
fillPages(uintptr_t(low), uintptr_t(top), pagetable, flags);
}

Expand Down Expand Up @@ -77,22 +77,19 @@ static void efiMapPage(PTE *pagetable, const void *page,
}

void Pagetable::init() {
char *stack_start, *stack_top;
char *text_start, *data_top;
char *bss_start, *bss_top;
static const size_t stack_size = 0x1000;

extern const char __stack_end__[];
const char *__stack_start__ = __stack_end__ - stack_size;
extern const char __text_start__[], __text_end__[];
extern const char __modules_start__[], __modules_end__[];
extern const char __data_start__[], __data_end__[];
extern const char __bss_start__[], __bss_end__[];

const struct EFI::SystemTable *ST = EFI::getSystemTable();
Multiboot::Payload *multiboot = Multiboot::getPayload();

static const size_t stack_size = 0x1000;

asm volatile("mov %%cr3, %q0":"=r"(pagetable));
asm volatile("lea __stack_end__(%%rip), %q0":"=r"(stack_top));
stack_start = stack_top - stack_size;
asm volatile("lea __text_start__(%%rip), %q0":"=r"(text_start));
asm volatile("lea __data_end__(%%rip), %q0":"=r"(data_top));
asm volatile("lea __bss_start__(%%rip), %q0":"=r"(bss_start));
asm volatile("lea __bss_end__(%%rip), %q0":"=r"(bss_top));

// Initialization of pagetables

Expand Down Expand Up @@ -153,9 +150,11 @@ void Pagetable::init() {
fillPages(0x0C8000, 0x0F0000, newpt); // Reserved for many systems
fillPages(0x0F0000, 0x100000, newpt); // BIOS Code

fillPages(stack_start, stack_top, newpt); // PXOS Stack
fillPages(text_start, data_top, newpt); // PXOS Code & Data
fillPages(bss_start, bss_top, newpt); // PXOS BSS
fillPages(__stack_start__, __stack_end__, newpt); // PXOS Stack
fillPages(__text_start__, __text_end__, newpt, 1); // PXOS Code
fillPages(__modules_start__, __modules_end__, newpt, 1); // PXOS Built-in modules
fillPages(__data_start__, __data_end__, newpt); // PXOS Data
fillPages(__bss_start__, __bss_end__, newpt); // PXOS BSS

if (multiboot)
*PTE::find(multiboot, newpt) = PTE { multiboot, 3 };
Expand All @@ -167,13 +166,13 @@ void Pagetable::init() {
if (multiboot) {
if (multiboot->flags & Multiboot::MB_FLAG_CMDLINE) {
if (multiboot->pcmdline < 0x80000)
multiboot->pcmdline += uintptr_t(bss_top);
multiboot->pcmdline += uintptr_t(__bss_end__);
map(reinterpret_cast<void*>(uintptr_t(multiboot->pcmdline)));
}

if (multiboot->flags & Multiboot::MB_FLAG_MODS) {
if (multiboot->pmods_addr < 0x80000)
multiboot->pmods_addr += uintptr_t(bss_top);
multiboot->pmods_addr += uintptr_t(__bss_end__);

uintptr_t low = uintptr_t(multiboot->pmods_addr) & 0xFFFFFFFFFFFFF000;
uintptr_t top = klib::__align(
Expand All @@ -195,7 +194,7 @@ void Pagetable::init() {

if (multiboot->flags & Multiboot::MB_FLAG_MEMMAP) {
if (multiboot->pmmap_addr < 0x80000)
multiboot->pmmap_addr += uintptr_t(bss_top);
multiboot->pmmap_addr += uintptr_t(__bss_end__);

const char *mmap = reinterpret_cast<const char*>(uintptr_t(multiboot->pmmap_addr));
const char *mmap_top = mmap + multiboot->mmap_length;
Expand Down

0 comments on commit bcb17c1

Please sign in to comment.