diff options
| author | Ada Christine <adachristine18@gmail.com> | 2022-02-15 15:00:53 +0000 |
|---|---|---|
| committer | Ada Christine <adachristine18@gmail.com> | 2022-02-15 15:00:53 +0000 |
| commit | 5fc386102380e6c15c81df4fd750f54641535eaa (patch) | |
| tree | c3aed0635aa10a80e1bbbe46a80e4e67f2be2c39 /kc | |
| parent | 0c1bc025700656ce17bbd71f7d149744d65b7fb6 (diff) | |
beginning to things out into cpu-dependent and cpu-independent code
Diffstat (limited to 'kc')
| -rw-r--r-- | kc/core/Makefile | 4 | ||||
| -rw-r--r-- | kc/core/cpu/irq.c | 24 | ||||
| -rw-r--r-- | kc/core/cpu/irq.h | 4 | ||||
| -rw-r--r-- | kc/core/cpu/mmu.c | 30 | ||||
| -rw-r--r-- | kc/core/cpu/mmu.h | 9 | ||||
| -rw-r--r-- | kc/core/memory.c | 11 | ||||
| -rw-r--r-- | kc/core/task.c | 15 |
7 files changed, 75 insertions, 22 deletions
diff --git a/kc/core/Makefile b/kc/core/Makefile index 49083bc..fd5c984 100644 --- a/kc/core/Makefile +++ b/kc/core/Makefile @@ -10,9 +10,9 @@ CPPFLAGS += -I../../api -I. CFLAGS += -Wno-unused-const-variable -Wno-unused-function -fvisibility=hidden -g SOBJS := -GOBJS := entry_x86_64.o reloc_x86_64.o kprint.o serial.o port.o \ +GOBJS := entry_x86_64.o reloc_x86_64.o kprint.o serial.o port.o mmu.o \ kc_main.o memory.o memset.o memcpy.o memmove.o memcmp.o cpu.o \ - vm_tree.o exceptions.o panic.o msr.o task.o cpu_task.o \ + vm_tree.o exceptions.o panic.o msr.o task.o cpu_task.o irq.o \ kcc_memory.o string.o pic8259.o pic8259_isr.o pit8253.o timer.o IOBJS := 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 <stdatomic.h> + +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 <kernel/memory/paging.h> + +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); + diff --git a/kc/core/memory.c b/kc/core/memory.c index 7571f0d..0319246 100644 --- a/kc/core/memory.c +++ b/kc/core/memory.c @@ -3,6 +3,7 @@ #include "panic.h" #include "vm_tree.h" #include "vm_object.h" +#include "cpu/mmu.h" #include <stdint.h> @@ -109,13 +110,7 @@ static size_t init_get_max_paddr(struct memory_range *ranges, int count) static phys_addr_t get_kernel_pm4_phys(void) { - phys_addr_t pm4_phys; - __asm__ ( - "mov %%cr3, %0\n\t" - : "=r"(pm4_phys) - ); - - return page_address(pm4_phys, 1); + return mmu_get_map(); } static phys_addr_t get_kernel_pm3_phys(void) @@ -382,7 +377,7 @@ void page_unmap(void *vaddr) { *pte = 0; } - __asm__ ("invlpg (%0)" :: "r"(vaddr)); + mmu_invalidate(vaddr); } phys_addr_t page_alloc(void) diff --git a/kc/core/task.c b/kc/core/task.c index 09156d4..79fae5f 100644 --- a/kc/core/task.c +++ b/kc/core/task.c @@ -4,13 +4,12 @@ #include "panic.h" #include "cpu.h" #include "kprint.h" +#include "cpu/irq.h" struct kc_thread *current_task = NULL; struct kc_thread *first_ready_task = NULL; struct kc_thread *last_ready_task = NULL; -static int schedule_lock_cnt = 0; - static void lock_scheduler(void); static void unlock_scheduler(void); static void set_thread_status(enum kc_thread_status status); @@ -49,20 +48,12 @@ void task_schedule(void) static void lock_scheduler(void) { - __asm__ volatile ("cli;"); - schedule_lock_cnt++; + irq_lock(); } static void unlock_scheduler(void) { - if (schedule_lock_cnt >= 1) - { - schedule_lock_cnt--; - } - if (schedule_lock_cnt == 0) - { - __asm__ volatile ("sti;"); - } + irq_unlock(); } static void unblock_thread(struct kc_thread *thread) |
