summaryrefslogtreecommitdiff
path: root/src/arch
diff options
context:
space:
mode:
Diffstat (limited to 'src/arch')
-rw-r--r--src/arch/amd64/cpu/backtrace.c6
-rw-r--r--src/arch/amd64/cpu/debugger.c5
-rw-r--r--src/arch/amd64/cpu/idt.c24
-rw-r--r--src/arch/amd64/drivers/bochs.c2
-rw-r--r--src/arch/amd64/mboot.c43
-rw-r--r--src/arch/amd64/mboot.h2
-rw-r--r--src/arch/amd64/paging.c153
-rw-r--r--src/arch/amd64/shim.c5
8 files changed, 124 insertions, 116 deletions
diff --git a/src/arch/amd64/cpu/backtrace.c b/src/arch/amd64/cpu/backtrace.c
index 9e1c9d7..b1d2a2c 100644
--- a/src/arch/amd64/cpu/backtrace.c
+++ b/src/arch/amd64/cpu/backtrace.c
@@ -8,13 +8,13 @@ struct stackframe {
size_t backtrace(void **dst, size_t len) {
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);
}
size_t backtrace_ex(void **dst, size_t len, void *ip, void *bp) {
struct stackframe *frame = bp;
- __asm__ volatile ("mov %%rbp, %0" : "=r"(frame));
+ __asm__ ("mov %%rbp, %0" : "=r"(frame));
if (len > 0) {
dst[0] = ip;
}
@@ -28,7 +28,7 @@ size_t backtrace_ex(void **dst, size_t len, void *ip, void *bp) {
void log_backtrace() {
struct stackframe *rbp;
- __asm__ volatile ("mov %%rbp, %0" : "=r"(rbp));
+ __asm__ ("mov %%rbp, %0" : "=r"(rbp));
log_backtrace_ex(rbp->rip, rbp->rbp);
}
diff --git a/src/arch/amd64/cpu/debugger.c b/src/arch/amd64/cpu/debugger.c
index 4d9d3c3..0f48b84 100644
--- a/src/arch/amd64/cpu/debugger.c
+++ b/src/arch/amd64/cpu/debugger.c
@@ -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;
__asm__ volatile ("mov %%dr6, %0" : "=r"(dr6));
@@ -398,7 +398,8 @@ void debugger(struct isr_regs *state, int cause) {
dbg_steps = 0;
dbg_continue = 0;
- ((struct rflags *)&state->rflags)->tf = 0;
+ struct rflags *rflags = (struct rflags *) &state->rflags;
+ rflags->tf = 0;
if (dr6.b0) {
state->rip += bkps[0].instr_len;
diff --git a/src/arch/amd64/cpu/idt.c b/src/arch/amd64/cpu/idt.c
index c286199..3e98b55 100644
--- a/src/arch/amd64/cpu/idt.c
+++ b/src/arch/amd64/cpu/idt.c
@@ -5,6 +5,7 @@
#include "idt.h"
#include "backtrace.h"
#include "debugger.h"
+#include "../paging.h"
#include "../bindings.h"
#include "../drivers/pic.h"
@@ -72,6 +73,10 @@ void idt_init(void) {
__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
char *EXCEPTIONS[] = {
"Division Error",
@@ -109,27 +114,30 @@ char *EXCEPTIONS[] = {
};
void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *state) {
+ uint64_t cr2;
+
switch (exception) {
- case 0x01: // debug
+ case EX_DEBUG: // debug
debugger(state, DEBUG_DBG);
return;
- case 0x03: // breakpoint
+ case EX_BREAKPOINT: // breakpoint
debugger(state, DEBUG_INT3);
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");
kprintf("0x%02lX %s\n", exception, EXCEPTIONS[exception]);
kprintf("Error code 0x%lX\n", code);
-
- // page faults store the offending address in cr2
- if (exception == 0x0E) {
- uint64_t cr2;
- __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
+ if (exception == EX_PAGE_FAULT) {
kprintf("Page fault address: 0x%lX\n", cr2);
}
-
+
kputs("\n");
log_backtrace_ex((void *)state->rip, (void *)state->rbp);
diff --git a/src/arch/amd64/drivers/bochs.c b/src/arch/amd64/drivers/bochs.c
index 5383213..1abb927 100644
--- a/src/arch/amd64/drivers/bochs.c
+++ b/src/arch/amd64/drivers/bochs.c
@@ -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));
}
-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);
diff --git a/src/arch/amd64/mboot.c b/src/arch/amd64/mboot.c
index 34424a0..771e911 100644
--- a/src/arch/amd64/mboot.c
+++ b/src/arch/amd64/mboot.c
@@ -68,13 +68,13 @@ struct mboot_tag_mmap {
struct mboot_tag_old_rsdp {
uint32_t type;
uint32_t size;
- uint8_t rsdp[];
+ char rsdp[];
};
struct mboot_tag_new_rsdp {
uint32_t type;
uint32_t size;
- uint8_t rsdp[];
+ char rsdp[];
};
struct mboot_tag_cmdline {
@@ -96,11 +96,10 @@ struct mboot_tag_framebuffer {
};
static void read_symbols(
- struct boot_info *shim_info,
- struct mboot_tag_elf_sections *sections
+ volatile struct boot_info *shim_info,
+ volatile struct mboot_tag_elf_sections *sections
) {
-
- shim_info->symbol_table = sections->sections;
+ shim_info->symbol_table = (void * volatile) sections->sections;
// struct mboot_elf_section_header *section =
// (struct mboot_elf_section_header *) (layout->elf_section_headers);
@@ -136,19 +135,19 @@ static void read_symbols(
}
static void read_cmdline(
- struct boot_info *shim_info,
- struct mboot_tag_cmdline *cmdline
+ volatile struct boot_info *shim_info,
+ volatile struct mboot_tag_cmdline *cmdline
) {
uint32_t size = cmdline->size - 8;
if (size >= CMDLINE_MAX)
size = CMDLINE_MAX; // truncate :(
- memcpy(shim_info->cmdline, cmdline->cmdline, size);
+ memcpyv(shim_info->cmdline, cmdline->cmdline, size);
shim_info->cmdline[size] = '\0';
}
static void read_framebuffer(
- struct boot_info *shim_info,
- struct mboot_tag_framebuffer *framebuffer
+ volatile struct boot_info *shim_info,
+ volatile struct mboot_tag_framebuffer *framebuffer
) {
shim_info->fb.addr = framebuffer->framebuffer_addr;
shim_info->fb.width = framebuffer->framebuffer_width;
@@ -168,8 +167,8 @@ static const char *segment_type[] = {
};
static void read_memory_map(
- struct boot_info *shim_info,
- struct mboot_tag_mmap *map
+ volatile struct boot_info *shim_info,
+ volatile struct mboot_tag_mmap *map
) {
int idx = 0;
uintptr_t i = (uintptr_t)map->entries;
@@ -200,31 +199,31 @@ static void read_memory_map(
}
static void read_old_rsdp(
- struct boot_info *shim_info,
- struct mboot_tag_old_rsdp *rsdp
+ volatile struct boot_info *shim_info,
+ volatile struct mboot_tag_old_rsdp *rsdp
) {
if (shim_info->acpi_table != NULL)
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(
- struct boot_info *shim_info,
- struct mboot_tag_new_rsdp *rsdp
+ volatile struct boot_info *shim_info,
+ 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(
long mboot_magic,
- const void *mboot_data_ptr,
- struct boot_info *shim_info
+ volatile const void *mboot_data_ptr,
+ volatile struct boot_info *shim_info
) {
if (mboot_magic != MBOOT_HEADER_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;
const char *mboot_end = ((char *) mboot_info) + mboot_info->total_size;
diff --git a/src/arch/amd64/mboot.h b/src/arch/amd64/mboot.h
index b9c647b..d1ca1a0 100644
--- a/src/arch/amd64/mboot.h
+++ b/src/arch/amd64/mboot.h
@@ -8,4 +8,4 @@
* @param mboot_info - the pointer passed from multiboot2
* @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);
diff --git a/src/arch/amd64/paging.c b/src/arch/amd64/paging.c
index 1c1c56c..fd24af4 100644
--- a/src/arch/amd64/paging.c
+++ b/src/arch/amd64/paging.c
@@ -54,34 +54,34 @@ struct pte {
};
// bss segment, can write to
-extern struct pml4e kernel_pml4[512];
-extern struct pdpte kernel_pdpt_0[512];
-extern struct pde kernel_pd_0[512];
-extern 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 pml4e kernel_pml4[512];
+extern volatile struct pdpte kernel_pdpt_0[512];
+extern volatile struct pde kernel_pd_0[512];
+extern volatile struct pte bootstrap_pt[512];
+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
// the structures are not gurenteed to be ident mapped
// map them here with map_<type>(phys_addr) before useing structures
-void *addr_mapped = (void *) (uintptr_t) 0x204000;
-static struct pml4e *pml4_mapped = (void *) (uintptr_t) 0x200000;
-static struct pdpte *pdpt_mapped = (void *) (uintptr_t) 0x201000;
-static struct pde *pd_mapped = (void *) (uintptr_t) 0x202000;
-static struct pte *pt_mapped = (void *) (uintptr_t) 0x203000;
+void volatile *addr_mapped = (void *) (uintptr_t) 0x204000;
+static volatile struct pml4e *pml4_mapped = (void *) (uintptr_t) 0x200000;
+static volatile struct pdpte *pdpt_mapped = (void *) (uintptr_t) 0x201000;
+static volatile struct pde *pd_mapped = (void *) (uintptr_t) 0x202000;
+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");
}
-static void load_addr(void *phys_addr) {
- static struct pte *pt = &paging_pt[4];
+static void load_addr(volatile void *phys_addr) {
+ static volatile struct pte *pt = &paging_pt[4];
pt->address = (uint64_t)phys_addr >> 12;
pt->flags = F_PRESENT | F_WRITEABLE;
invlpg(addr_mapped);
}
-static void load_pml4(void *phys) {
- static struct pte *pt = &paging_pt[0];
+static void load_pml4(volatile void *phys) {
+ static volatile struct pte *pt = &paging_pt[0];
if ((uint64_t)phys >> 12 == pt->address)
return;
pt->address = (uint64_t)phys >> 12;
@@ -89,8 +89,8 @@ static void load_pml4(void *phys) {
invlpg(pml4_mapped);
}
-static void load_pdpt(void *phys) {
- static struct pte *pt = &paging_pt[1];
+static void load_pdpt(volatile void *phys) {
+ static volatile struct pte *pt = &paging_pt[1];
if ((uint64_t)phys >> 12 == pt->address)
return;
pt->address = (uint64_t)phys >> 12;
@@ -98,8 +98,8 @@ static void load_pdpt(void *phys) {
invlpg(pdpt_mapped);
}
-static void load_pd(void *phys) {
- static struct pte *pt = &paging_pt[2];
+static void load_pd(volatile void *phys) {
+ static volatile struct pte *pt = &paging_pt[2];
if ((uint64_t)phys >> 12 == pt->address)
return;
pt->address = (uint64_t)phys >> 12;
@@ -107,8 +107,8 @@ static void load_pd(void *phys) {
invlpg(pd_mapped);
}
-static void load_pt(void *phys) {
- static struct pte *pt = &paging_pt[3];
+static void load_pt(volatile void *phys) {
+ static volatile struct pte *pt = &paging_pt[3];
if ((uint64_t)phys >> 12 == pt->address)
return;
pt->address = (uint64_t)phys >> 12;
@@ -123,13 +123,13 @@ static void load_pt(void *phys) {
static int select_pdpt(
void *virt,
unsigned int flags,
- struct pml4e *root,
- struct pdpte **res,
+ volatile struct pml4e *root,
+ volatile struct pdpte **res,
bool create
) {
load_pml4(root);
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 (!create) {
return PAG_NOT_PRESENT;
@@ -139,26 +139,26 @@ static int select_pdpt(
return PAG_CANNOT_ALLOC;
}
load_addr(new_page);
- memset(addr_mapped, 0, PAGE_SIZE);
+ memsetv(addr_mapped, 0, PAGE_SIZE);
pml4e->address = ((uint64_t)new_page) >> 12;
pml4e->flags = F_PRESENT;
}
if (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;
}
static int select_pd(
void *virt,
unsigned int flags,
- struct pdpte *pdpt,
- struct pde **res,
+ volatile struct pdpte *pdpt,
+ volatile struct pde **res,
bool create
) {
load_pdpt(pdpt);
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 (!create) {
return PAG_NOT_PRESENT;
@@ -168,26 +168,26 @@ static int select_pd(
return PAG_CANNOT_ALLOC;
}
load_addr(new_page);
- memset(addr_mapped, 0, PAGE_SIZE);
+ memsetv(addr_mapped, 0, PAGE_SIZE);
pdpte->address = ((uint64_t)new_page) >> 12;
pdpte->flags = F_PRESENT;
}
if (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;
}
static int select_pt(
void *virt,
unsigned int flags,
- struct pde *pd,
- struct pte **res,
+ volatile struct pde *pd,
+ volatile struct pte **res,
bool create
) {
load_pd(pd);
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 (!create) {
return PAG_NOT_PRESENT;
@@ -197,24 +197,24 @@ static int select_pt(
return PAG_CANNOT_ALLOC;
}
load_addr(new_page);
- memset(addr_mapped, 0, PAGE_SIZE);
+ memsetv(addr_mapped, 0, PAGE_SIZE);
pde->address = ((uint64_t)new_page) >> 12;
pde->flags = F_PRESENT;
}
if (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;
}
static void select_page(
void *virt,
- struct pte *pt,
- struct pte **res
+ volatile struct pte *pt,
+ volatile struct pte **res
) {
load_pt(pt);
uint64_t offset = ((uint64_t)virt >> 12) & 0x1ff;
- struct pte *page = &pt_mapped[offset];
+ volatile struct pte *page = &pt_mapped[offset];
*res = page;
return;
}
@@ -275,13 +275,13 @@ static inline void try_unmap_pt(void) {
}
static void unmap_page(
- struct pml4e *root,
+ volatile struct pml4e *root,
void *virt
) {
- struct pdpte *pdpt;
- struct pde *pd;
- struct pte *pt;
- struct pte *page;
+ volatile struct pdpte *pdpt;
+ volatile struct pde *pd;
+ volatile struct pte *pt;
+ volatile struct pte *page;
unsigned int df = 0;
@@ -308,7 +308,7 @@ static void unmap_page(
}
static void unmap_pages(
- struct pml4e *root,
+ volatile struct pml4e *root,
void *virt_start,
long page_count
) {
@@ -320,10 +320,10 @@ static void unmap_pages(
pdpt_n,
pd_n;
- struct pdpte *pdpt = NULL;
- struct pde *pd = NULL;
- struct pte *pt = NULL;
- struct pte *page = NULL;
+ volatile struct pdpte *pdpt = NULL;
+ volatile struct pde *pd = NULL;
+ volatile struct pte *pt = NULL;
+ volatile struct pte *page = NULL;
unsigned int df = 0;
@@ -372,14 +372,14 @@ static void unmap_pages(
return;
}
-static struct pte *get_page(
- struct pml4e *root,
+static volatile struct pte *get_page(
+ volatile struct pml4e *root,
void *virt
) {
- struct pdpte *pdpt;
- struct pde *pd;
- struct pte *pt;
- struct pte *page;
+ volatile struct pdpte *pdpt;
+ volatile struct pde *pd;
+ volatile struct pte *pt;
+ volatile struct pte *page;
unsigned int df = 0;
@@ -398,15 +398,15 @@ static struct pte *get_page(
}
static int map_page(
- struct pml4e *root,
+ volatile struct pml4e *root,
void *virt,
void *phys,
unsigned int flags
) {
- struct pdpte *pdpt;
- struct pde *pd;
- struct pte *pt;
- struct pte *page;
+ volatile struct pdpte *pdpt;
+ volatile struct pde *pd;
+ volatile struct pte *pt;
+ volatile struct pte *page;
unsigned int df = F_WRITEABLE;
@@ -439,7 +439,7 @@ static int map_page(
}
static int map_pages(
- struct pml4e *root,
+ volatile struct pml4e *root,
void *virt_start,
void *phys_start,
unsigned int flags,
@@ -453,10 +453,10 @@ static int map_pages(
pdpt_n,
pd_n;
- struct pdpte *pdpt = NULL;
- struct pde *pd = NULL;
- struct pte *pt = NULL;
- struct pte *page = NULL;
+ volatile struct pdpte *pdpt = NULL;
+ volatile struct pde *pd = NULL;
+ volatile struct pte *pt = NULL;
+ volatile struct pte *page = NULL;
void *virt, *phys;
@@ -533,8 +533,8 @@ void paging_init(void) {
kernel_pd_0[2].flags = F_PRESENT | F_WRITEABLE;
kernel_pd_0[2].address = (uint64_t)(&bootstrap_pt) >> 12;
- memset(&paging_pt, 0, 4096);
- memset(&bootstrap_pt, 0, 4096);
+ memsetv(&paging_pt, 0, 4096);
+ memsetv(&bootstrap_pt, 0, 4096);
}
static inline void *page_align(void *addr) {
@@ -577,15 +577,17 @@ void *alloc_pages(int count) {
void *virt = virtaddr_alloc(count);
if (virt == NULL)
return NULL;
- void *phys = alloc_phys_pages(count);
- if (phys == NULL) {
- virtaddr_free(virt);
- return NULL;
- }
+ //void *phys = alloc_phys_pages(count);
+ //if (phys == NULL) {
+ // virtaddr_free(virt);
+ // return NULL;
+ //}
if (map_pages(
kernel_pml4,
virt,
- phys,
+ //phys,
+ //F_WRITEABLE,
+ NULL,
F_WRITEABLE,
count
)) {
@@ -625,7 +627,7 @@ int kunmap_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)
return -1;
if (page->loaded)
@@ -636,8 +638,7 @@ int kload_page(void *virt_addr) {
return -2;
page->loaded = 1;
page->address = (uint64_t)phys >> 12;
- page->flags |= F_PRESENT | F_WRITEABLE;
+ page->flags |= F_PRESENT;
invlpg(virt_addr);
- __asm__ volatile ("movq %cr3, %rax; movq %rax, %cr3;");
return 0;
}
diff --git a/src/arch/amd64/shim.c b/src/arch/amd64/shim.c
index b84167c..b055fed 100644
--- a/src/arch/amd64/shim.c
+++ b/src/arch/amd64/shim.c
@@ -11,9 +11,10 @@
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();
paging_init();
+ serial_out_str("aaaa\n");
pic_remap();
idt_init();
@@ -21,5 +22,3 @@ void *amd64_shim(long mboot_magic, void *mboot_data_ptr) {
return &boot_info;
}
-
-