alloc on write paging, -O3 compile works, 'volatile' is the story of my life

This commit is contained in:
Freya Murphy 2024-02-03 14:36:26 -05:00
parent 5bec5d9fbd
commit 8e74123683
Signed by: freya
GPG key ID: 744AB800E383AE52
15 changed files with 180 additions and 134 deletions

View file

@ -6,10 +6,12 @@ CC=cc
LD=ld LD=ld
AS=nasm AS=nasm
CFLAGS += -std=c2x -ffreestanding -fno-stack-protector -g -Wall -Wextra -pedantic -lgcc -isystem include CFLAGS += -std=c2x -ffreestanding -lgcc -isystem include
CFLAGS += -Wall -Wextra -pedantic
CFLAGS += -O3 -pipe -g -fno-stack-protector
CFLAGS += -DPAGE_SIZE=4096 CFLAGS += -DPAGE_SIZE=4096
LDFLAGS += -nmagic -nostdlib LDFLAGS += -nmagic --no-warn-rwx-segments -nostdlib
H_SRC = $(shell find src include -type f -name "*.h") H_SRC = $(shell find src include -type f -name "*.h")
@ -24,6 +26,8 @@ A_OBJ = $(patsubst %.S,build/%.S.o,$(A_SRC))
all: build/$(ISO) all: build/$(ISO)
build: build/$(ISO)
clean: clean:
@printf "\033[31m RM \033[0m%s\n" build @printf "\033[31m RM \033[0m%s\n" build
@rm -rf build/* @rm -rf build/*
@ -41,7 +45,7 @@ $(C_OBJ): build/%.o : %.c
build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC) build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC)
@mkdir -p $(@D) @mkdir -p $(@D)
@printf "\033[32m LD \033[0m%s\n" $@ @printf "\033[32m LD \033[0m%s\n" $@
@$(LD) $(LDFLAGS) --no-warn-rwx-segments -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ) @$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ)
build/$(ISO): build/$(KERNEL) grub.cfg build/$(ISO): build/$(KERNEL) grub.cfg
@mkdir -p $(@D) @mkdir -p $(@D)

View file

@ -8,4 +8,4 @@
* @param width - the width of the screen * @param width - the width of the screen
* @param height - the height of the screen * @param height - the height of the screen
*/ */
uint32_t *bochs_init(uint16_t w, uint16_t h, uint8_t b); volatile uint32_t *bochs_init(uint16_t w, uint16_t h, uint8_t b);

View file

@ -10,11 +10,17 @@
int memcmp(const void *restrict s1, const void *restrict s2, unsigned long n); int memcmp(const void *restrict s1, const void *restrict s2, unsigned long n);
/** /**
* The memcpy() function copies n bytes from memory area src to memory area dest. * the memcpy() function copies n bytes from memory area src to memory area dest.
* The memory areas must not overlap. * the memory areas must not overlap.
*/ */
void *memcpy(void *restrict dest, const void *restrict src, unsigned long n); void *memcpy(void *restrict dest, const void *restrict src, unsigned long n);
/**
* the memcpy() function copies n bytes from memory area src to memory area dest.
* the memory areas must not overlap.
*/
volatile void *memcpyv(volatile void *dest, const volatile void *src, unsigned long n);
/** /**
* The memmove() function copies n bytes from memory area src to memory area dest. The memory areas * The memmove() function copies n bytes from memory area src to memory area dest. The memory areas
* may overlap: copying takes place as though the bytes in src are first copied into a temporary array * may overlap: copying takes place as though the bytes in src are first copied into a temporary array
@ -28,6 +34,12 @@ void *memmove(void *dest, const void *src, unsigned long n);
*/ */
void *memset(void *restrict dest, int c, unsigned long n); void *memset(void *restrict dest, int c, unsigned long n);
/**
* The memset() function fills the first n bytes of the memory area pointed to by s with the constant
* byte c.
*/
volatile void *memsetv(volatile void *dest, int c, unsigned long n);
/** /**
* The strcmp() function compares the two strings s1 and s2. The locale is not taken into account * The strcmp() function compares the two strings s1 and s2. The locale is not taken into account
* (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters. * (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters.

View file

@ -8,13 +8,13 @@ struct stackframe {
size_t backtrace(void **dst, size_t len) { size_t backtrace(void **dst, size_t len) {
struct stackframe *rbp; struct stackframe *rbp;
__asm__ volatile ("mov %%rbp, %0" : "=r"(rbp)); __asm__ ("mov %%rbp, %0" : "=r"(rbp));
return backtrace_ex(dst, len, rbp->rip, rbp->rbp); return backtrace_ex(dst, len, rbp->rip, rbp->rbp);
} }
size_t backtrace_ex(void **dst, size_t len, void *ip, void *bp) { size_t backtrace_ex(void **dst, size_t len, void *ip, void *bp) {
struct stackframe *frame = bp; struct stackframe *frame = bp;
__asm__ volatile ("mov %%rbp, %0" : "=r"(frame)); __asm__ ("mov %%rbp, %0" : "=r"(frame));
if (len > 0) { if (len > 0) {
dst[0] = ip; dst[0] = ip;
} }
@ -28,7 +28,7 @@ size_t backtrace_ex(void **dst, size_t len, void *ip, void *bp) {
void log_backtrace() { void log_backtrace() {
struct stackframe *rbp; struct stackframe *rbp;
__asm__ volatile ("mov %%rbp, %0" : "=r"(rbp)); __asm__ ("mov %%rbp, %0" : "=r"(rbp));
log_backtrace_ex(rbp->rip, rbp->rbp); log_backtrace_ex(rbp->rip, rbp->rbp);
} }

View file

@ -376,7 +376,7 @@ static int debugger_prompt(struct isr_regs *state) {
} }
} }
void debugger(struct isr_regs *state, int cause) { void debugger(struct isr_regs *volatile state, int cause) {
struct dr6 dr6; struct dr6 dr6;
__asm__ volatile ("mov %%dr6, %0" : "=r"(dr6)); __asm__ volatile ("mov %%dr6, %0" : "=r"(dr6));
@ -398,7 +398,8 @@ void debugger(struct isr_regs *state, int cause) {
dbg_steps = 0; dbg_steps = 0;
dbg_continue = 0; dbg_continue = 0;
((struct rflags *)&state->rflags)->tf = 0; struct rflags *rflags = (struct rflags *) &state->rflags;
rflags->tf = 0;
if (dr6.b0) { if (dr6.b0) {
state->rip += bkps[0].instr_len; state->rip += bkps[0].instr_len;

View file

@ -5,6 +5,7 @@
#include "idt.h" #include "idt.h"
#include "backtrace.h" #include "backtrace.h"
#include "debugger.h" #include "debugger.h"
#include "../paging.h"
#include "../bindings.h" #include "../bindings.h"
#include "../drivers/pic.h" #include "../drivers/pic.h"
@ -72,6 +73,10 @@ void idt_init(void) {
__asm__ volatile ("lidt %0" : : "m"(idtr)); __asm__ volatile ("lidt %0" : : "m"(idtr));
} }
#define EX_DEBUG 0x01
#define EX_BREAKPOINT 0x03
#define EX_PAGE_FAULT 0x0e
// Intel manual vol 3 ch 6.3.1 // Intel manual vol 3 ch 6.3.1
char *EXCEPTIONS[] = { char *EXCEPTIONS[] = {
"Division Error", "Division Error",
@ -109,27 +114,30 @@ char *EXCEPTIONS[] = {
}; };
void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *state) { void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *state) {
uint64_t cr2;
switch (exception) { switch (exception) {
case 0x01: // debug case EX_DEBUG: // debug
debugger(state, DEBUG_DBG); debugger(state, DEBUG_DBG);
return; return;
case 0x03: // breakpoint case EX_BREAKPOINT: // breakpoint
debugger(state, DEBUG_INT3); debugger(state, DEBUG_INT3);
return; return;
case EX_PAGE_FAULT:
// page faults store the offending address in cr2
__asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
if (!kload_page((void *)cr2))
return;
} }
kputs("\n\n!!! EXCEPTION !!!\n"); kputs("\n\n!!! EXCEPTION !!!\n");
kprintf("0x%02lX %s\n", exception, EXCEPTIONS[exception]); kprintf("0x%02lX %s\n", exception, EXCEPTIONS[exception]);
kprintf("Error code 0x%lX\n", code); kprintf("Error code 0x%lX\n", code);
if (exception == EX_PAGE_FAULT) {
// page faults store the offending address in cr2
if (exception == 0x0E) {
uint64_t cr2;
__asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
kprintf("Page fault address: 0x%lX\n", cr2); kprintf("Page fault address: 0x%lX\n", cr2);
} }
kputs("\n"); kputs("\n");
log_backtrace_ex((void *)state->rip, (void *)state->rbp); log_backtrace_ex((void *)state->rip, (void *)state->rbp);

View file

@ -53,7 +53,7 @@ static void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, int lf
(clear ? 0 : DATA_NO_CLEAR_MEM)); (clear ? 0 : DATA_NO_CLEAR_MEM));
} }
uint32_t* bochs_init(uint16_t width, uint16_t height, uint8_t bit_depth) { volatile uint32_t* bochs_init(uint16_t width, uint16_t height, uint8_t bit_depth) {
set_mode(width, height, bit_depth, true, true); set_mode(width, height, bit_depth, true, true);

View file

@ -68,13 +68,13 @@ struct mboot_tag_mmap {
struct mboot_tag_old_rsdp { struct mboot_tag_old_rsdp {
uint32_t type; uint32_t type;
uint32_t size; uint32_t size;
uint8_t rsdp[]; char rsdp[];
}; };
struct mboot_tag_new_rsdp { struct mboot_tag_new_rsdp {
uint32_t type; uint32_t type;
uint32_t size; uint32_t size;
uint8_t rsdp[]; char rsdp[];
}; };
struct mboot_tag_cmdline { struct mboot_tag_cmdline {
@ -96,11 +96,10 @@ struct mboot_tag_framebuffer {
}; };
static void read_symbols( static void read_symbols(
struct boot_info *shim_info, volatile struct boot_info *shim_info,
struct mboot_tag_elf_sections *sections volatile struct mboot_tag_elf_sections *sections
) { ) {
shim_info->symbol_table = (void * volatile) sections->sections;
shim_info->symbol_table = sections->sections;
// struct mboot_elf_section_header *section = // struct mboot_elf_section_header *section =
// (struct mboot_elf_section_header *) (layout->elf_section_headers); // (struct mboot_elf_section_header *) (layout->elf_section_headers);
@ -136,19 +135,19 @@ static void read_symbols(
} }
static void read_cmdline( static void read_cmdline(
struct boot_info *shim_info, volatile struct boot_info *shim_info,
struct mboot_tag_cmdline *cmdline volatile struct mboot_tag_cmdline *cmdline
) { ) {
uint32_t size = cmdline->size - 8; uint32_t size = cmdline->size - 8;
if (size >= CMDLINE_MAX) if (size >= CMDLINE_MAX)
size = CMDLINE_MAX; // truncate :( size = CMDLINE_MAX; // truncate :(
memcpy(shim_info->cmdline, cmdline->cmdline, size); memcpyv(shim_info->cmdline, cmdline->cmdline, size);
shim_info->cmdline[size] = '\0'; shim_info->cmdline[size] = '\0';
} }
static void read_framebuffer( static void read_framebuffer(
struct boot_info *shim_info, volatile struct boot_info *shim_info,
struct mboot_tag_framebuffer *framebuffer volatile struct mboot_tag_framebuffer *framebuffer
) { ) {
shim_info->fb.addr = framebuffer->framebuffer_addr; shim_info->fb.addr = framebuffer->framebuffer_addr;
shim_info->fb.width = framebuffer->framebuffer_width; shim_info->fb.width = framebuffer->framebuffer_width;
@ -168,8 +167,8 @@ static const char *segment_type[] = {
}; };
static void read_memory_map( static void read_memory_map(
struct boot_info *shim_info, volatile struct boot_info *shim_info,
struct mboot_tag_mmap *map volatile struct mboot_tag_mmap *map
) { ) {
int idx = 0; int idx = 0;
uintptr_t i = (uintptr_t)map->entries; uintptr_t i = (uintptr_t)map->entries;
@ -200,31 +199,31 @@ static void read_memory_map(
} }
static void read_old_rsdp( static void read_old_rsdp(
struct boot_info *shim_info, volatile struct boot_info *shim_info,
struct mboot_tag_old_rsdp *rsdp volatile struct mboot_tag_old_rsdp *rsdp
) { ) {
if (shim_info->acpi_table != NULL) if (shim_info->acpi_table != NULL)
return; // xsdp is newer and has been loaded return; // xsdp is newer and has been loaded
shim_info->acpi_table = rsdp->rsdp; shim_info->acpi_table = (void *volatile) rsdp->rsdp;
} }
static void read_new_rsdp( static void read_new_rsdp(
struct boot_info *shim_info, volatile struct boot_info *shim_info,
struct mboot_tag_new_rsdp *rsdp volatile struct mboot_tag_new_rsdp *rsdp
) { ) {
shim_info->acpi_table = rsdp->rsdp; shim_info->acpi_table = (void *volatile) rsdp->rsdp;
} }
void mboot_load_info( void mboot_load_info(
long mboot_magic, long mboot_magic,
const void *mboot_data_ptr, volatile const void *mboot_data_ptr,
struct boot_info *shim_info volatile struct boot_info *shim_info
) { ) {
if (mboot_magic != MBOOT_HEADER_MAGIC) if (mboot_magic != MBOOT_HEADER_MAGIC)
panic("invalid multiboot magic"); panic("invalid multiboot magic");
memset(shim_info, 0, sizeof(struct boot_info)); memsetv(shim_info, 0, sizeof(struct boot_info));
struct mboot_info *mboot_info = (struct mboot_info *) mboot_data_ptr; struct mboot_info *mboot_info = (struct mboot_info *) mboot_data_ptr;
const char *mboot_end = ((char *) mboot_info) + mboot_info->total_size; const char *mboot_end = ((char *) mboot_info) + mboot_info->total_size;

View file

@ -8,4 +8,4 @@
* @param mboot_info - the pointer passed from multiboot2 * @param mboot_info - the pointer passed from multiboot2
* @param shim_info - the info to be collected by shim * @param shim_info - the info to be collected by shim
*/ */
void mboot_load_info(long mboot_magic, const void *mboot_info, struct boot_info *shim_info); void mboot_load_info(long mboot_magic, const volatile void *mboot_data_ptr, volatile struct boot_info *shim_info);

View file

@ -54,34 +54,34 @@ struct pte {
}; };
// bss segment, can write to // bss segment, can write to
extern struct pml4e kernel_pml4[512]; extern volatile struct pml4e kernel_pml4[512];
extern struct pdpte kernel_pdpt_0[512]; extern volatile struct pdpte kernel_pdpt_0[512];
extern struct pde kernel_pd_0[512]; extern volatile struct pde kernel_pd_0[512];
extern struct pte bootstrap_pt[512]; extern volatile struct pte bootstrap_pt[512];
extern struct pte paging_pt[512]; // paging_pt should NEVER be outside of this file, NEVER i say extern volatile struct pte paging_pt[512]; // paging_pt should NEVER be outside of this file, NEVER i say
// paged address to read page tables // paged address to read page tables
// the structures are not gurenteed to be ident mapped // the structures are not gurenteed to be ident mapped
// map them here with map_<type>(phys_addr) before useing structures // map them here with map_<type>(phys_addr) before useing structures
void *addr_mapped = (void *) (uintptr_t) 0x204000; void volatile *addr_mapped = (void *) (uintptr_t) 0x204000;
static struct pml4e *pml4_mapped = (void *) (uintptr_t) 0x200000; static volatile struct pml4e *pml4_mapped = (void *) (uintptr_t) 0x200000;
static struct pdpte *pdpt_mapped = (void *) (uintptr_t) 0x201000; static volatile struct pdpte *pdpt_mapped = (void *) (uintptr_t) 0x201000;
static struct pde *pd_mapped = (void *) (uintptr_t) 0x202000; static volatile struct pde *pd_mapped = (void *) (uintptr_t) 0x202000;
static struct pte *pt_mapped = (void *) (uintptr_t) 0x203000; static volatile struct pte *pt_mapped = (void *) (uintptr_t) 0x203000;
static inline void invlpg(void *addr) { static inline void invlpg(volatile void *addr) {
__asm__ volatile("invlpg (%0)" ::"r" (addr) : "memory"); __asm__ volatile("invlpg (%0)" ::"r" (addr) : "memory");
} }
static void load_addr(void *phys_addr) { static void load_addr(volatile void *phys_addr) {
static struct pte *pt = &paging_pt[4]; static volatile struct pte *pt = &paging_pt[4];
pt->address = (uint64_t)phys_addr >> 12; pt->address = (uint64_t)phys_addr >> 12;
pt->flags = F_PRESENT | F_WRITEABLE; pt->flags = F_PRESENT | F_WRITEABLE;
invlpg(addr_mapped); invlpg(addr_mapped);
} }
static void load_pml4(void *phys) { static void load_pml4(volatile void *phys) {
static struct pte *pt = &paging_pt[0]; static volatile struct pte *pt = &paging_pt[0];
if ((uint64_t)phys >> 12 == pt->address) if ((uint64_t)phys >> 12 == pt->address)
return; return;
pt->address = (uint64_t)phys >> 12; pt->address = (uint64_t)phys >> 12;
@ -89,8 +89,8 @@ static void load_pml4(void *phys) {
invlpg(pml4_mapped); invlpg(pml4_mapped);
} }
static void load_pdpt(void *phys) { static void load_pdpt(volatile void *phys) {
static struct pte *pt = &paging_pt[1]; static volatile struct pte *pt = &paging_pt[1];
if ((uint64_t)phys >> 12 == pt->address) if ((uint64_t)phys >> 12 == pt->address)
return; return;
pt->address = (uint64_t)phys >> 12; pt->address = (uint64_t)phys >> 12;
@ -98,8 +98,8 @@ static void load_pdpt(void *phys) {
invlpg(pdpt_mapped); invlpg(pdpt_mapped);
} }
static void load_pd(void *phys) { static void load_pd(volatile void *phys) {
static struct pte *pt = &paging_pt[2]; static volatile struct pte *pt = &paging_pt[2];
if ((uint64_t)phys >> 12 == pt->address) if ((uint64_t)phys >> 12 == pt->address)
return; return;
pt->address = (uint64_t)phys >> 12; pt->address = (uint64_t)phys >> 12;
@ -107,8 +107,8 @@ static void load_pd(void *phys) {
invlpg(pd_mapped); invlpg(pd_mapped);
} }
static void load_pt(void *phys) { static void load_pt(volatile void *phys) {
static struct pte *pt = &paging_pt[3]; static volatile struct pte *pt = &paging_pt[3];
if ((uint64_t)phys >> 12 == pt->address) if ((uint64_t)phys >> 12 == pt->address)
return; return;
pt->address = (uint64_t)phys >> 12; pt->address = (uint64_t)phys >> 12;
@ -123,13 +123,13 @@ static void load_pt(void *phys) {
static int select_pdpt( static int select_pdpt(
void *virt, void *virt,
unsigned int flags, unsigned int flags,
struct pml4e *root, volatile struct pml4e *root,
struct pdpte **res, volatile struct pdpte **res,
bool create bool create
) { ) {
load_pml4(root); load_pml4(root);
uint64_t offset = (uint64_t)virt >> 39; uint64_t offset = (uint64_t)virt >> 39;
struct pml4e *pml4e = &pml4_mapped[offset]; volatile struct pml4e *pml4e = &pml4_mapped[offset];
if (!(pml4e->flags & F_PRESENT)) { if (!(pml4e->flags & F_PRESENT)) {
if (!create) { if (!create) {
return PAG_NOT_PRESENT; return PAG_NOT_PRESENT;
@ -139,26 +139,26 @@ static int select_pdpt(
return PAG_CANNOT_ALLOC; return PAG_CANNOT_ALLOC;
} }
load_addr(new_page); load_addr(new_page);
memset(addr_mapped, 0, PAGE_SIZE); memsetv(addr_mapped, 0, PAGE_SIZE);
pml4e->address = ((uint64_t)new_page) >> 12; pml4e->address = ((uint64_t)new_page) >> 12;
pml4e->flags = F_PRESENT; pml4e->flags = F_PRESENT;
} }
if (flags) if (flags)
pml4e->flags = F_PRESENT | flags; pml4e->flags = F_PRESENT | flags;
*res = (struct pdpte *)(uintptr_t)(pml4e->address << 12); *res = (volatile struct pdpte *)(uintptr_t)(pml4e->address << 12);
return PAG_SUCCESS; return PAG_SUCCESS;
} }
static int select_pd( static int select_pd(
void *virt, void *virt,
unsigned int flags, unsigned int flags,
struct pdpte *pdpt, volatile struct pdpte *pdpt,
struct pde **res, volatile struct pde **res,
bool create bool create
) { ) {
load_pdpt(pdpt); load_pdpt(pdpt);
uint64_t offset = ((uint64_t)virt >> 30) & 0x1ff; uint64_t offset = ((uint64_t)virt >> 30) & 0x1ff;
struct pdpte *pdpte = &pdpt_mapped[offset]; volatile struct pdpte *pdpte = &pdpt_mapped[offset];
if (!(pdpte->flags & F_PRESENT)) { if (!(pdpte->flags & F_PRESENT)) {
if (!create) { if (!create) {
return PAG_NOT_PRESENT; return PAG_NOT_PRESENT;
@ -168,26 +168,26 @@ static int select_pd(
return PAG_CANNOT_ALLOC; return PAG_CANNOT_ALLOC;
} }
load_addr(new_page); load_addr(new_page);
memset(addr_mapped, 0, PAGE_SIZE); memsetv(addr_mapped, 0, PAGE_SIZE);
pdpte->address = ((uint64_t)new_page) >> 12; pdpte->address = ((uint64_t)new_page) >> 12;
pdpte->flags = F_PRESENT; pdpte->flags = F_PRESENT;
} }
if (flags) if (flags)
pdpte->flags = F_PRESENT | flags; pdpte->flags = F_PRESENT | flags;
*res = (struct pde *)(uintptr_t)(pdpte->address << 12); *res = (volatile struct pde *)(uintptr_t)(pdpte->address << 12);
return PAG_SUCCESS; return PAG_SUCCESS;
} }
static int select_pt( static int select_pt(
void *virt, void *virt,
unsigned int flags, unsigned int flags,
struct pde *pd, volatile struct pde *pd,
struct pte **res, volatile struct pte **res,
bool create bool create
) { ) {
load_pd(pd); load_pd(pd);
uint64_t offset = ((uint64_t)virt >> 21) & 0x1ff; uint64_t offset = ((uint64_t)virt >> 21) & 0x1ff;
struct pde *pde = &pd_mapped[offset]; volatile struct pde *pde = &pd_mapped[offset];
if (!(pde->flags & F_PRESENT)) { if (!(pde->flags & F_PRESENT)) {
if (!create) { if (!create) {
return PAG_NOT_PRESENT; return PAG_NOT_PRESENT;
@ -197,24 +197,24 @@ static int select_pt(
return PAG_CANNOT_ALLOC; return PAG_CANNOT_ALLOC;
} }
load_addr(new_page); load_addr(new_page);
memset(addr_mapped, 0, PAGE_SIZE); memsetv(addr_mapped, 0, PAGE_SIZE);
pde->address = ((uint64_t)new_page) >> 12; pde->address = ((uint64_t)new_page) >> 12;
pde->flags = F_PRESENT; pde->flags = F_PRESENT;
} }
if (flags) if (flags)
pde->flags = F_PRESENT | flags; pde->flags = F_PRESENT | flags;
*res = (struct pte *)(uintptr_t)(pde->address << 12); *res = (volatile struct pte *)(uintptr_t)(pde->address << 12);
return PAG_SUCCESS; return PAG_SUCCESS;
} }
static void select_page( static void select_page(
void *virt, void *virt,
struct pte *pt, volatile struct pte *pt,
struct pte **res volatile struct pte **res
) { ) {
load_pt(pt); load_pt(pt);
uint64_t offset = ((uint64_t)virt >> 12) & 0x1ff; uint64_t offset = ((uint64_t)virt >> 12) & 0x1ff;
struct pte *page = &pt_mapped[offset]; volatile struct pte *page = &pt_mapped[offset];
*res = page; *res = page;
return; return;
} }
@ -275,13 +275,13 @@ static inline void try_unmap_pt(void) {
} }
static void unmap_page( static void unmap_page(
struct pml4e *root, volatile struct pml4e *root,
void *virt void *virt
) { ) {
struct pdpte *pdpt; volatile struct pdpte *pdpt;
struct pde *pd; volatile struct pde *pd;
struct pte *pt; volatile struct pte *pt;
struct pte *page; volatile struct pte *page;
unsigned int df = 0; unsigned int df = 0;
@ -308,7 +308,7 @@ static void unmap_page(
} }
static void unmap_pages( static void unmap_pages(
struct pml4e *root, volatile struct pml4e *root,
void *virt_start, void *virt_start,
long page_count long page_count
) { ) {
@ -320,10 +320,10 @@ static void unmap_pages(
pdpt_n, pdpt_n,
pd_n; pd_n;
struct pdpte *pdpt = NULL; volatile struct pdpte *pdpt = NULL;
struct pde *pd = NULL; volatile struct pde *pd = NULL;
struct pte *pt = NULL; volatile struct pte *pt = NULL;
struct pte *page = NULL; volatile struct pte *page = NULL;
unsigned int df = 0; unsigned int df = 0;
@ -372,14 +372,14 @@ static void unmap_pages(
return; return;
} }
static struct pte *get_page( static volatile struct pte *get_page(
struct pml4e *root, volatile struct pml4e *root,
void *virt void *virt
) { ) {
struct pdpte *pdpt; volatile struct pdpte *pdpt;
struct pde *pd; volatile struct pde *pd;
struct pte *pt; volatile struct pte *pt;
struct pte *page; volatile struct pte *page;
unsigned int df = 0; unsigned int df = 0;
@ -398,15 +398,15 @@ static struct pte *get_page(
} }
static int map_page( static int map_page(
struct pml4e *root, volatile struct pml4e *root,
void *virt, void *virt,
void *phys, void *phys,
unsigned int flags unsigned int flags
) { ) {
struct pdpte *pdpt; volatile struct pdpte *pdpt;
struct pde *pd; volatile struct pde *pd;
struct pte *pt; volatile struct pte *pt;
struct pte *page; volatile struct pte *page;
unsigned int df = F_WRITEABLE; unsigned int df = F_WRITEABLE;
@ -439,7 +439,7 @@ static int map_page(
} }
static int map_pages( static int map_pages(
struct pml4e *root, volatile struct pml4e *root,
void *virt_start, void *virt_start,
void *phys_start, void *phys_start,
unsigned int flags, unsigned int flags,
@ -453,10 +453,10 @@ static int map_pages(
pdpt_n, pdpt_n,
pd_n; pd_n;
struct pdpte *pdpt = NULL; volatile struct pdpte *pdpt = NULL;
struct pde *pd = NULL; volatile struct pde *pd = NULL;
struct pte *pt = NULL; volatile struct pte *pt = NULL;
struct pte *page = NULL; volatile struct pte *page = NULL;
void *virt, *phys; void *virt, *phys;
@ -533,8 +533,8 @@ void paging_init(void) {
kernel_pd_0[2].flags = F_PRESENT | F_WRITEABLE; kernel_pd_0[2].flags = F_PRESENT | F_WRITEABLE;
kernel_pd_0[2].address = (uint64_t)(&bootstrap_pt) >> 12; kernel_pd_0[2].address = (uint64_t)(&bootstrap_pt) >> 12;
memset(&paging_pt, 0, 4096); memsetv(&paging_pt, 0, 4096);
memset(&bootstrap_pt, 0, 4096); memsetv(&bootstrap_pt, 0, 4096);
} }
static inline void *page_align(void *addr) { static inline void *page_align(void *addr) {
@ -577,15 +577,17 @@ void *alloc_pages(int count) {
void *virt = virtaddr_alloc(count); void *virt = virtaddr_alloc(count);
if (virt == NULL) if (virt == NULL)
return NULL; return NULL;
void *phys = alloc_phys_pages(count); //void *phys = alloc_phys_pages(count);
if (phys == NULL) { //if (phys == NULL) {
virtaddr_free(virt); // virtaddr_free(virt);
return NULL; // return NULL;
} //}
if (map_pages( if (map_pages(
kernel_pml4, kernel_pml4,
virt, virt,
phys, //phys,
//F_WRITEABLE,
NULL,
F_WRITEABLE, F_WRITEABLE,
count count
)) { )) {
@ -625,7 +627,7 @@ int kunmap_page(void *virt_addr) {
} }
int kload_page(void *virt_addr) { int kload_page(void *virt_addr) {
struct pte *page = get_page(kernel_pml4, virt_addr); volatile struct pte *page = get_page(kernel_pml4, virt_addr);
if (page == NULL) if (page == NULL)
return -1; return -1;
if (page->loaded) if (page->loaded)
@ -636,8 +638,7 @@ int kload_page(void *virt_addr) {
return -2; return -2;
page->loaded = 1; page->loaded = 1;
page->address = (uint64_t)phys >> 12; page->address = (uint64_t)phys >> 12;
page->flags |= F_PRESENT | F_WRITEABLE; page->flags |= F_PRESENT;
invlpg(virt_addr); invlpg(virt_addr);
__asm__ volatile ("movq %cr3, %rax; movq %rax, %cr3;");
return 0; return 0;
} }

View file

@ -11,9 +11,10 @@
static struct boot_info boot_info; static struct boot_info boot_info;
void *amd64_shim(long mboot_magic, void *mboot_data_ptr) { void *amd64_shim(long mboot_magic, volatile void *mboot_data_ptr) {
serial_init(); serial_init();
paging_init(); paging_init();
serial_out_str("aaaa\n");
pic_remap(); pic_remap();
idt_init(); idt_init();
@ -21,5 +22,3 @@ void *amd64_shim(long mboot_magic, void *mboot_data_ptr) {
return &boot_info; return &boot_info;
} }

View file

@ -17,6 +17,7 @@ void kmain(struct boot_info *info) {
kprintf("enterd kmain\n"); kprintf("enterd kmain\n");
// pages are allocated on write :3
char *test = kalloc(5); char *test = kalloc(5);
*test = 1; *test = 1;

View file

@ -15,6 +15,13 @@ void *memcpy(void *restrict dest, const void *restrict src, unsigned long n) {
return dest; return dest;
} }
volatile void *memcpyv(volatile void *dest, const volatile void *src, unsigned long n) {
volatile char *d = dest;
const volatile char *s = src;
for (; n; n--) *d++ = *s++;
return dest;
}
void *memmove(void *dest, const void *src, unsigned long n) { void *memmove(void *dest, const void *src, unsigned long n) {
char *d = dest; char *d = dest;
const char *s = src; const char *s = src;
@ -38,6 +45,14 @@ void *memset(void *restrict dest, int c, unsigned long n) {
return dest; return dest;
} }
volatile void *memsetv(volatile void *dest, int c, unsigned long n) {
volatile unsigned char *d = dest;
for (; n; n--) {
*d++ = c;
};
return dest;
}
int strncmp(const char *restrict lhs, const char *restrict rhs, unsigned long n) { int strncmp(const char *restrict lhs, const char *restrict rhs, unsigned long n) {
const unsigned char *l=(void *)lhs, *r=(void *)rhs; const unsigned char *l=(void *)lhs, *r=(void *)rhs;
if (!n--) return 0; if (!n--) return 0;

View file

@ -51,7 +51,6 @@ void *kalloc_new(size_t size) {
node = 0; node = 0;
} }
memcpy(addr, 0, pages * PAGE_SIZE);
struct page_header *header = addr; struct page_header *header = addr;
header->magic = 0xBEEFCAFE; header->magic = 0xBEEFCAFE;
header->used = size; header->used = size;

View file

@ -20,7 +20,7 @@ struct hsv {
uint16_t v; uint16_t v;
}; };
static uint32_t *buffer = NULL; static volatile uint32_t *buffer = NULL;
static uint32_t width, height; static uint32_t width, height;
static uint8_t bit_depth; static uint8_t bit_depth;
@ -39,7 +39,12 @@ static float fabs(float f) {
return f > 0 ? f : -f; return f > 0 ? f : -f;
} }
static struct rgb htor(struct hsv hsv) { static struct rgb gethsv(uint16_t a, uint16_t xp, uint16_t yp) {
struct hsv hsv = {
.h = a,
.s = xp * 100.0 / width,
.v = (height - yp) * 100.0 / height,
};
struct rgb rgb; struct rgb rgb;
float s = hsv.s / 100.0; float s = hsv.s / 100.0;
float v = hsv.v / 100.0; float v = hsv.v / 100.0;
@ -69,14 +74,16 @@ static struct rgb htor(struct hsv hsv) {
return rgb; return rgb;
} }
static struct rgb get_color(uint16_t a, uint16_t x, uint16_t y) { //static struct rgb getsrgb(uint16_t x, uint16_t y) {
struct hsv hsv = { // struct rgb color;// = get_color(angle, x, height - y);
.h = a, // double _x = (double) x / width;
.s = x * 100.0 / width, // double _y = (double) y / height;
.v = y * 100.0 / height, // double _z = (2 - _x - _y) / 2;
}; // color.r = _x * 255;
return htor(hsv); // color.g = _y * 255;
} // color.b = _z * 255;
// return color;
//}
void screen_init(void) { void screen_init(void) {
angle = 0; angle = 0;
@ -89,7 +96,7 @@ void screen_init(void) {
void screen_redraw(void) { void screen_redraw(void) {
for (uint16_t y = 0; y < height; y++) { for (uint16_t y = 0; y < height; y++) {
for (uint16_t x = 0; x < width; x++) { for (uint16_t x = 0; x < width; x++) {
struct rgb color = get_color(angle, x, height - y); struct rgb color = gethsv(angle, x, y);
set_pixel(color, x, y); set_pixel(color, x, y);
} }
} }