mem: begin work on pager
This commit is contained in:
parent
2b9d76d015
commit
ffddd592da
10 changed files with 184 additions and 28 deletions
2
Makefile
2
Makefile
|
|
@ -66,7 +66,7 @@ override HEADER_DEPS := $(CFILES:.c=.d) $(ASFILES:.S=.d)
|
||||||
all: $(KERNEL)
|
all: $(KERNEL)
|
||||||
|
|
||||||
# Link rules for the final kernel executable.
|
# Link rules for the final kernel executable.
|
||||||
$(KERNEL): $(OBJ)
|
$(KERNEL): $(OBJ) linker.ld
|
||||||
@$(LD) $(OBJ) $(LDFLAGS) -o $@
|
@$(LD) $(OBJ) $(LDFLAGS) -o $@
|
||||||
@echo " LD $@"
|
@echo " LD $@"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/acpi.h>
|
#include <kernel/acpi.h>
|
||||||
|
#include <kernel/mem.h>
|
||||||
#include <dev/apic.h>
|
#include <dev/apic.h>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
@ -92,5 +93,9 @@ void apic_init()
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ioapic_addr += hhdm_start;
|
||||||
|
printf(LOG_INFO "IOAPIC ADDR: %X\n", ioapic_addr);
|
||||||
|
|
||||||
|
|
||||||
printf(LOG_SUCCESS "APIC initialized\n");
|
printf(LOG_SUCCESS "APIC initialized\n");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define PACKED __attribute__((packed))
|
#define PACKED __attribute__((packed))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
|
|
||||||
#ifndef __ASM__
|
#define HHDM_START 0xffffffff80000000
|
||||||
|
|
||||||
#define HHDM_START 0xFFFF800000000000
|
#ifndef __ASM__
|
||||||
|
|
||||||
#define ALIGN(_a) __attribute__((aligned(_a)))
|
#define ALIGN(_a) __attribute__((aligned(_a)))
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ struct page { /* I would rather refer to this as a struct */
|
||||||
uint64_t ignored : 1;
|
uint64_t ignored : 1;
|
||||||
uint64_t zero : 1;
|
uint64_t zero : 1;
|
||||||
uint64_t ignored_2 : 4;
|
uint64_t ignored_2 : 4;
|
||||||
uint64_t pdpt_addr : 36;
|
uint64_t addr : 36;
|
||||||
uint64_t reserved : 4;
|
uint64_t reserved : 4;
|
||||||
uint64_t ignored_3 : 11;
|
uint64_t ignored_3 : 11;
|
||||||
uint64_t xd : 1;
|
uint64_t xd : 1;
|
||||||
|
|
@ -31,15 +31,34 @@ struct page { /* I would rather refer to this as a struct */
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct page pml4[512] ALIGN(0x1000);
|
struct cr3 {
|
||||||
extern struct page pdpt[512] ALIGN(0x1000);
|
union {
|
||||||
extern struct page pdt[512] ALIGN(0x1000);
|
struct {
|
||||||
extern struct page pt[512] ALIGN(0x1000);
|
uint64_t ignored : 3;
|
||||||
|
uint64_t pwt : 1;
|
||||||
|
uint64_t pcd : 1;
|
||||||
|
uint64_t ignored_2 : 7;
|
||||||
|
uint64_t pml4_addr : 52;
|
||||||
|
};
|
||||||
|
uint64_t val;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
extern uintptr_t __hhdm_start;
|
||||||
|
extern uintptr_t __data_end;
|
||||||
|
extern uintptr_t __bss_start;
|
||||||
|
extern uintptr_t __bss_end;
|
||||||
|
|
||||||
|
extern uintptr_t hhdm_start;
|
||||||
|
extern uintptr_t data_end;
|
||||||
|
extern uintptr_t bss_start;
|
||||||
|
extern uintptr_t bss_end;
|
||||||
|
|
||||||
int page_map(uintptr_t virt, uintptr_t phys);
|
int page_map(uintptr_t virt, uintptr_t phys);
|
||||||
uint64_t cr3_get();
|
uint64_t cr3_read();
|
||||||
|
void cr3_write(uint64_t cr3);
|
||||||
|
|
||||||
void pfa_init();
|
void pfa_init(char *mem, size_t len);
|
||||||
|
|
||||||
#endif /* __ASM__ */
|
#endif /* __ASM__ */
|
||||||
#endif /* _MEM_H_ */
|
#endif /* _MEM_H_ */
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t num);
|
void *memcpy(void *dest, const void *src, size_t num);
|
||||||
int memcmp(const void *aa, const void *bb, size_t num);
|
int memcmp(const void *aa, const void *bb, size_t num);
|
||||||
|
void *memset(void *str, int c, size_t n);
|
||||||
char *strcpy(char *dest, const char *src);
|
char *strcpy(char *dest, const char *src);
|
||||||
|
|
||||||
#endif /* __ASM__ */
|
#endif /* __ASM__ */
|
||||||
|
|
|
||||||
25
init/main.c
25
init/main.c
|
|
@ -10,6 +10,11 @@
|
||||||
|
|
||||||
#include <limine/limine.h>
|
#include <limine/limine.h>
|
||||||
|
|
||||||
|
uintptr_t hhdm_start;
|
||||||
|
uintptr_t data_end;
|
||||||
|
uintptr_t bss_start;
|
||||||
|
uintptr_t bss_end;
|
||||||
|
|
||||||
static void done(void)
|
static void done(void)
|
||||||
{
|
{
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
@ -19,13 +24,25 @@ static void done(void)
|
||||||
|
|
||||||
void kmain(void)
|
void kmain(void)
|
||||||
{
|
{
|
||||||
|
hhdm_start = (uintptr_t)(&__hhdm_start);
|
||||||
|
data_end = (uintptr_t)(&__data_end);
|
||||||
|
bss_start = (uintptr_t)(&__bss_start);
|
||||||
|
bss_end = (uintptr_t)(&__bss_end);
|
||||||
|
|
||||||
|
serial_init();
|
||||||
gdt_init();
|
gdt_init();
|
||||||
idt_init();
|
idt_init();
|
||||||
serial_init();
|
|
||||||
acpi_init();
|
|
||||||
apic_init();
|
|
||||||
|
|
||||||
pfa_init();
|
acpi_init();
|
||||||
|
|
||||||
|
/* Initialize early kernel memory array */
|
||||||
|
const uintptr_t one_gb = 0x40000000;
|
||||||
|
char kmem[one_gb] ALIGN(0x1000);
|
||||||
|
printf(LOG_WARN "kmem: %Xh\n", kmem);
|
||||||
|
|
||||||
|
pfa_init(kmem, one_gb);
|
||||||
|
|
||||||
|
apic_init();
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
lib/string.c
10
lib/string.c
|
|
@ -28,3 +28,13 @@ int memcmp(const void *aa, const void *bb, size_t num)
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *memset(void *str, int c, size_t n)
|
||||||
|
{
|
||||||
|
unsigned char *s = (unsigned char *)str;
|
||||||
|
unsigned char j = (unsigned char)c;
|
||||||
|
for (size_t i = 0; i < n; i++) {
|
||||||
|
s[i] = j;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ SECTIONS
|
||||||
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
|
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
|
||||||
/* that is the beginning of the region. */
|
/* that is the beginning of the region. */
|
||||||
. = 0xffffffff80000000;
|
. = 0xffffffff80000000;
|
||||||
|
__hhdm_start = 0xffffffff80000000;
|
||||||
|
|
||||||
.text : {
|
.text : {
|
||||||
*(.text .text.*)
|
*(.text .text.*)
|
||||||
|
|
@ -29,15 +30,16 @@ SECTIONS
|
||||||
/* Move to the next memory page for .rodata */
|
/* Move to the next memory page for .rodata */
|
||||||
. += CONSTANT(MAXPAGESIZE);
|
. += CONSTANT(MAXPAGESIZE);
|
||||||
|
|
||||||
.rodata : {
|
.rodata ALIGN (0x1000) : {
|
||||||
*(.rodata .rodata.*)
|
*(.rodata .rodata.*)
|
||||||
} :rodata
|
} :rodata
|
||||||
|
|
||||||
/* Move to the next memory page for .data */
|
/* Move to the next memory page for .data */
|
||||||
. += CONSTANT(MAXPAGESIZE);
|
. += CONSTANT(MAXPAGESIZE);
|
||||||
|
|
||||||
.data : {
|
.data ALIGN (0x1000) : {
|
||||||
*(.data .data.*)
|
*(.data .data.*)
|
||||||
|
__data_end = .;
|
||||||
} :data
|
} :data
|
||||||
|
|
||||||
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
|
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
|
||||||
|
|
@ -45,8 +47,10 @@ SECTIONS
|
||||||
/* If you need, for example, .init_array and .fini_array, those should be placed */
|
/* If you need, for example, .init_array and .fini_array, those should be placed */
|
||||||
/* above this. */
|
/* above this. */
|
||||||
.bss : {
|
.bss : {
|
||||||
|
__bss_start = .;
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
*(.bss .bss.*)
|
*(.bss .bss.*)
|
||||||
|
__bss_end = .;
|
||||||
} :data
|
} :data
|
||||||
|
|
||||||
/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
|
/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,10 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
.global cr3_get
|
.global cr3_read
|
||||||
cr3_get:
|
cr3_read:
|
||||||
movq %cr3, %rax
|
movq %cr3, %rax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
.global cr3_write
|
||||||
|
cr3_write:
|
||||||
|
movq %rdi, %cr3
|
||||||
|
ret
|
||||||
|
|
|
||||||
120
mem/pfa.c
120
mem/pfa.c
|
|
@ -5,22 +5,116 @@
|
||||||
#include <dev/apic.h>
|
#include <dev/apic.h>
|
||||||
#include <limine/limine.h>
|
#include <limine/limine.h>
|
||||||
|
|
||||||
struct page pml4[512] ALIGN(0x1000);
|
struct limine_memmap_request map_req = { .id = LIMINE_MEMMAP_REQUEST,
|
||||||
struct page pdpt[512] ALIGN(0x1000);
|
.revision = 0 };
|
||||||
struct page pdt[512] ALIGN(0x1000);
|
|
||||||
struct page pt[512] ALIGN(0x1000);
|
|
||||||
|
|
||||||
static void pfa_gen_tables()
|
static char *kmem;
|
||||||
|
static size_t kmem_len;
|
||||||
|
static uintptr_t hwm = 0;
|
||||||
|
static struct page *pml4;
|
||||||
|
|
||||||
|
static const char *map_names[] = { "USABLE ", "RESERVED ",
|
||||||
|
"ACPI_RECLAIM", "ACPI_NVS ",
|
||||||
|
"BAD_MEM ", "BOOT_RECLAIM",
|
||||||
|
"KERNEL ", "FRAMEBUFFER " };
|
||||||
|
|
||||||
|
void *alloc_page()
|
||||||
{
|
{
|
||||||
|
void *r = (void *)(hwm + kmem);
|
||||||
|
if (hwm >= kmem_len)
|
||||||
|
printf(LOG_ERROR "Out of early kmem\n");
|
||||||
|
hwm += 0x1000;
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pfa_init()
|
uint64_t *next_level(uint64_t *this_level, size_t next_num)
|
||||||
{
|
{
|
||||||
uintptr_t pml4_t = (cr3_get() & 0xFFFFFFFFFFFFF000);
|
uint64_t *r;
|
||||||
pml4_t += HHDM_START;
|
if (!(this_level[next_num] & 1)) {
|
||||||
|
r = alloc_page();
|
||||||
uint64_t *pml4 = (uint64_t *)pml4_t;
|
memset(r, 0, 4096);
|
||||||
|
this_level[next_num] = (uintptr_t)r | 3;
|
||||||
printf(LOG_WARN "PML4 Entry 0: %Xh\n", *pml4);
|
} else
|
||||||
printf(LOG_INFO "PML4 ADDRESS: %Xh\n", pml4);
|
r = (void *)(this_level[next_num] & -4096ull);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void kmap(uint64_t paddr, uint64_t vaddr, size_t len, uint64_t attr)
|
||||||
|
{
|
||||||
|
uint64_t *pdpt;
|
||||||
|
uint64_t *pdt;
|
||||||
|
uint64_t *pt;
|
||||||
|
|
||||||
|
size_t pdpn;
|
||||||
|
size_t pdn;
|
||||||
|
size_t ptn;
|
||||||
|
size_t pn;
|
||||||
|
|
||||||
|
if (pml4 == NULL) {
|
||||||
|
pml4 = alloc_page();
|
||||||
|
memset(pml4, 0, 0x1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
len += paddr & 4095;
|
||||||
|
paddr &= -4096ull;
|
||||||
|
vaddr &= -4096ull;
|
||||||
|
|
||||||
|
len = (len + 4095) & -4096ull;
|
||||||
|
|
||||||
|
for (; len; len -= 4096, vaddr += 4096, paddr += 4096) {
|
||||||
|
pn = (vaddr >> 12) & 0x1FF;
|
||||||
|
ptn = (vaddr >> 21) & 0x1FF;
|
||||||
|
pdn = (vaddr >> 30) & 0x1FF;
|
||||||
|
pdpn = (vaddr >> 39) & 0x1FF;
|
||||||
|
|
||||||
|
pdpt = next_level((uint64_t *)pml4, pdpn);
|
||||||
|
pdt = next_level(pdpt, pdn);
|
||||||
|
pt = next_level(pdt, ptn);
|
||||||
|
pt[pn] = paddr | attr | 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pfa_init(char *mem, size_t len)
|
||||||
|
{
|
||||||
|
kmem = mem;
|
||||||
|
kmem_len = len;
|
||||||
|
|
||||||
|
uint64_t kernel_base = 0;
|
||||||
|
size_t kernel_size = 0;
|
||||||
|
|
||||||
|
/* Print limine early memory map */
|
||||||
|
struct limine_memmap_response *res = map_req.response;
|
||||||
|
for (unsigned int i = 0; i < res->entry_count; i++) {
|
||||||
|
struct limine_memmap_entry *entry = res->entries[i];
|
||||||
|
printf(LOG_INFO "MEM REGION %s BASE: %X, SIZE: %X\n",
|
||||||
|
map_names[entry->type], entry->base, entry->length);
|
||||||
|
|
||||||
|
switch (entry->type) {
|
||||||
|
case LIMINE_MEMMAP_KERNEL_AND_MODULES:
|
||||||
|
kernel_base = entry->base;
|
||||||
|
kernel_size = entry->length;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Need to make these linker script values usable */
|
||||||
|
|
||||||
|
printf(LOG_WARN "__hhdm_start %X\n", hhdm_start);
|
||||||
|
printf(LOG_WARN "__data_end %X\n", data_end);
|
||||||
|
printf(LOG_WARN "__bss_start %X\n", bss_start);
|
||||||
|
printf(LOG_WARN "__bss_end %X\n", bss_end);
|
||||||
|
printf(LOG_WARN "sizeof size_t %d\n", sizeof(size_t));
|
||||||
|
|
||||||
|
struct page attrs;
|
||||||
|
attrs.val = 0;
|
||||||
|
attrs.rw = 1;
|
||||||
|
attrs.xd = 0;
|
||||||
|
kmap(kernel_base, hhdm_start, kernel_size, attrs.val);
|
||||||
|
|
||||||
|
struct cr3 cr3 = { .pml4_addr = ((uintptr_t)pml4 >> 12) };
|
||||||
|
cr3_write(cr3.val);
|
||||||
|
|
||||||
|
printf(LOG_SUCCESS "Early memory map initialized\n");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue