From 5fc386102380e6c15c81df4fd750f54641535eaa Mon Sep 17 00:00:00 2001 From: Ada Christine Date: Tue, 15 Feb 2022 15:00:53 +0000 Subject: beginning to things out into cpu-dependent and cpu-independent code --- kc/core/cpu/irq.c | 24 ++++++++++++++++++++++++ kc/core/cpu/irq.h | 4 ++++ kc/core/cpu/mmu.c | 30 ++++++++++++++++++++++++++++++ kc/core/cpu/mmu.h | 9 +++++++++ 4 files changed, 67 insertions(+) create mode 100644 kc/core/cpu/irq.c create mode 100644 kc/core/cpu/mmu.c create mode 100644 kc/core/cpu/mmu.h (limited to 'kc/core/cpu') diff --git a/kc/core/cpu/irq.c b/kc/core/cpu/irq.c new file mode 100644 index 0000000..639aca3 --- /dev/null +++ b/kc/core/cpu/irq.c @@ -0,0 +1,24 @@ +#include "irq.h" + +#include + +static atomic_uint_fast64_t irq_lock_count = 0; + +void irq_lock(void) +{ + __asm__ volatile ("cli;"); +} + +void irq_unlock(void) +{ + if (irq_lock_count >= 1) + { + irq_lock_count--; + } + + if (irq_lock_count == 0) + { + __asm__ volatile ("sti;"); + } +} + diff --git a/kc/core/cpu/irq.h b/kc/core/cpu/irq.h index 9874761..9734860 100644 --- a/kc/core/cpu/irq.h +++ b/kc/core/cpu/irq.h @@ -3,6 +3,10 @@ #include "interrupt.h" #ifndef __ASSEMBLER__ + +void irq_lock(void); +void irq_unlock(void); + #else .macro IRQ_DECLARE name:req handler:req, irq:req, offset:req diff --git a/kc/core/cpu/mmu.c b/kc/core/cpu/mmu.c new file mode 100644 index 0000000..60bee0f --- /dev/null +++ b/kc/core/cpu/mmu.c @@ -0,0 +1,30 @@ +#include "mmu.h" +#include "irq.h" + +#include + +void mmu_set_map(phys_addr_t map) +{ + irq_lock(); + __asm__ volatile ("mov %0, %%cr3" :: "r"(page_address(map, 1))); + irq_unlock(); +} + +phys_addr_t mmu_get_map(void) +{ + phys_addr_t pm4_phys; + irq_lock(); + __asm__ volatile + ( + "mov %%cr3, %0\n\t" + : "=r"(pm4_phys) + ); + irq_unlock(); + return page_address(pm4_phys, 1); +} + +void mmu_invalidate(void *addr) +{ + __asm__ volatile ("invlpg (%0)" :: "r"(addr)); +} + diff --git a/kc/core/cpu/mmu.h b/kc/core/cpu/mmu.h new file mode 100644 index 0000000..2021a2e --- /dev/null +++ b/kc/core/cpu/mmu.h @@ -0,0 +1,9 @@ +#pragma once + +#include "memory.h" + +void mmu_set_map(phys_addr_t map); +phys_addr_t mmu_get_map(void); + +void mmu_invalidate(void *addr); + -- cgit v1.3.1-1-g115d