mem: add page entry type

This commit is contained in:
Michael Shaw 2023-03-01 14:20:13 -05:00
commit 2b9d76d015
7 changed files with 100 additions and 9 deletions

View file

@ -3,11 +3,12 @@
#include <kernel/acpi.h>
#include <dev/apic.h>
#include <stdint.h>
#include <stdio.h>
static void *madt_entries[0x20];
static int n_madt_entries = 0;
static uint32_t ioapic_addr = 0;
static uintptr_t ioapic_addr = 0;
static const char *madt_entry_types[] = { "LAPIC",
"IOAPIC",
@ -20,18 +21,16 @@ static const char *madt_entry_types[] = { "LAPIC",
"INVALID",
"CPU_LOCAL_X2_APIC" };
uint32_t ioapic_read(void *addr, uint32_t reg)
uint32_t ioapic_read(uintptr_t addr, uint8_t reg)
{
uint32_t volatile *ioapic = (uint32_t volatile *)addr;
ioapic[0] = (reg & 0xFF);
return ioapic[4];
*(volatile uint32_t *)(addr) = reg;
return *(volatile uint32_t *)(addr + 0x10);
}
void ioapic_write(void *addr, uint32_t reg, uint32_t value)
void ioapic_write(uintptr_t addr, uint8_t reg, uint32_t value)
{
uint32_t volatile *ioapic = (uint32_t volatile *)addr;
ioapic[0] = (reg & 0xFF);
ioapic[4] = value;
*(volatile uint32_t *)(addr) = reg;
*(volatile uint32_t *)(addr + 0x10) = value;
}
int madt_parse_next_entry(int offset)
@ -50,6 +49,8 @@ int madt_parse_next_entry(int offset)
struct madt_ioapic *m_ioapic = (struct madt_ioapic *)madt;
switch (type) {
case MADT_LAPIC:
break;
case MADT_IOAPIC:
ioapic_addr = m_ioapic->ioapic_addr;
printf(LOG_INFO "IOAPIC address: %xh\n", m_ioapic->ioapic_addr);

View file

@ -7,6 +7,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#define PACKED __attribute__((packed))

45
include/kernel/mem.h Normal file
View file

@ -0,0 +1,45 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _MEM_H_
#define _MEM_H_
#include <kernel/common.h>
#ifndef __ASM__
#define HHDM_START 0xFFFF800000000000
#define ALIGN(_a) __attribute__((aligned(_a)))
struct page { /* I would rather refer to this as a struct */
union {
struct {
uint64_t present : 1;
uint64_t rw : 1;
uint64_t usermode : 1;
uint64_t pwt : 1;
uint64_t pcd : 1;
uint64_t accessed : 1;
uint64_t ignored : 1;
uint64_t zero : 1;
uint64_t ignored_2 : 4;
uint64_t pdpt_addr : 36;
uint64_t reserved : 4;
uint64_t ignored_3 : 11;
uint64_t xd : 1;
};
uint64_t val;
};
};
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);
int page_map(uintptr_t virt, uintptr_t phys);
uint64_t cr3_get();
void pfa_init();
#endif /* __ASM__ */
#endif /* _MEM_H_ */

View file

@ -3,6 +3,7 @@
#include <kernel/acpi.h>
#include <kernel/gdt.h>
#include <kernel/idt.h>
#include <kernel/mem.h>
#include <dev/apic.h>
#include <dev/serial.h>
@ -23,6 +24,8 @@ void kmain(void)
serial_init();
acpi_init();
apic_init();
pfa_init();
done();
}

View file

@ -26,6 +26,7 @@ int printf(const char *restrict fmt, ...)
char c;
char *s;
int i;
long l;
int n;
@ -82,6 +83,15 @@ int printf(const char *restrict fmt, ...)
ret++;
}
break;
case 'X':
l = va_arg(args, long);
for (uint64_t j = 0; j < (2 * sizeof(l)); j++) {
int sel = ((sizeof(l) * 2) - j - 1) * 4;
int num = (l >> sel) & 0xF;
printf_out(convert[num]);
ret++;
}
break;
default:
printf_out(c);
ret++;

5
mem/cr3.S Normal file
View file

@ -0,0 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0-only */
.global cr3_get
cr3_get:
movq %cr3, %rax
ret

26
mem/pfa.c Normal file
View file

@ -0,0 +1,26 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/mem.h>
#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);
static void pfa_gen_tables()
{
}
void pfa_init()
{
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);
}