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)
|
||||
|
||||
# Link rules for the final kernel executable.
|
||||
$(KERNEL): $(OBJ)
|
||||
$(KERNEL): $(OBJ) linker.ld
|
||||
@$(LD) $(OBJ) $(LDFLAGS) -o $@
|
||||
@echo " LD $@"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/acpi.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <dev/apic.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");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PACKED __attribute__((packed))
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@
|
|||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#ifndef __ASM__
|
||||
#define HHDM_START 0xffffffff80000000
|
||||
|
||||
#define HHDM_START 0xFFFF800000000000
|
||||
#ifndef __ASM__
|
||||
|
||||
#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 zero : 1;
|
||||
uint64_t ignored_2 : 4;
|
||||
uint64_t pdpt_addr : 36;
|
||||
uint64_t addr : 36;
|
||||
uint64_t reserved : 4;
|
||||
uint64_t ignored_3 : 11;
|
||||
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);
|
||||
extern struct page pdpt[512] ALIGN(0x1000);
|
||||
extern struct page pdt[512] ALIGN(0x1000);
|
||||
extern struct page pt[512] ALIGN(0x1000);
|
||||
struct cr3 {
|
||||
union {
|
||||
struct {
|
||||
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);
|
||||
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 /* _MEM_H_ */
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
void *memcpy(void *dest, const void *src, 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);
|
||||
|
||||
#endif /* __ASM__ */
|
||||
|
|
|
|||
23
init/main.c
23
init/main.c
|
|
@ -10,6 +10,11 @@
|
|||
|
||||
#include <limine/limine.h>
|
||||
|
||||
uintptr_t hhdm_start;
|
||||
uintptr_t data_end;
|
||||
uintptr_t bss_start;
|
||||
uintptr_t bss_end;
|
||||
|
||||
static void done(void)
|
||||
{
|
||||
for (;;) {
|
||||
|
|
@ -19,13 +24,25 @@ static void done(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();
|
||||
idt_init();
|
||||
serial_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();
|
||||
|
||||
pfa_init();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 */
|
||||
/* that is the beginning of the region. */
|
||||
. = 0xffffffff80000000;
|
||||
__hhdm_start = 0xffffffff80000000;
|
||||
|
||||
.text : {
|
||||
*(.text .text.*)
|
||||
|
|
@ -29,15 +30,16 @@ SECTIONS
|
|||
/* Move to the next memory page for .rodata */
|
||||
. += CONSTANT(MAXPAGESIZE);
|
||||
|
||||
.rodata : {
|
||||
.rodata ALIGN (0x1000) : {
|
||||
*(.rodata .rodata.*)
|
||||
} :rodata
|
||||
|
||||
/* Move to the next memory page for .data */
|
||||
. += CONSTANT(MAXPAGESIZE);
|
||||
|
||||
.data : {
|
||||
.data ALIGN (0x1000) : {
|
||||
*(.data .data.*)
|
||||
__data_end = .;
|
||||
} :data
|
||||
|
||||
/* 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 */
|
||||
/* above this. */
|
||||
.bss : {
|
||||
__bss_start = .;
|
||||
*(COMMON)
|
||||
*(.bss .bss.*)
|
||||
__bss_end = .;
|
||||
} :data
|
||||
|
||||
/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
|
||||
|
|
|
|||
|
|
@ -1,5 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
.global cr3_get
|
||||
cr3_get:
|
||||
.global cr3_read
|
||||
cr3_read:
|
||||
movq %cr3, %rax
|
||||
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 <limine/limine.h>
|
||||
|
||||
struct page pml4[512] ALIGN(0x1000);
|
||||
struct page pdpt[512] ALIGN(0x1000);
|
||||
struct page pdt[512] ALIGN(0x1000);
|
||||
struct page pt[512] ALIGN(0x1000);
|
||||
struct limine_memmap_request map_req = { .id = LIMINE_MEMMAP_REQUEST,
|
||||
.revision = 0 };
|
||||
|
||||
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);
|
||||
pml4_t += HHDM_START;
|
||||
|
||||
uint64_t *pml4 = (uint64_t *)pml4_t;
|
||||
|
||||
printf(LOG_WARN "PML4 Entry 0: %Xh\n", *pml4);
|
||||
printf(LOG_INFO "PML4 ADDRESS: %Xh\n", pml4);
|
||||
uint64_t *r;
|
||||
if (!(this_level[next_num] & 1)) {
|
||||
r = alloc_page();
|
||||
memset(r, 0, 4096);
|
||||
this_level[next_num] = (uintptr_t)r | 3;
|
||||
} else
|
||||
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