diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 08abd0dc..466cbe5e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -60,4 +60,4 @@ jobs: shared/libshared.a shared/*.h shared/**/*.h - fs/redos/user/*.elf \ No newline at end of file + fs/* \ No newline at end of file diff --git a/kernel/Makefile b/kernel/Makefile index eb2511d8..00f5d147 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -50,5 +50,6 @@ $(BUILD_DIR)/%.o: %.cpp clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET) + $(RM) -r $(BUILD_DIR) -include $(DEP) diff --git a/kernel/exceptions/exception_vectors_as.S b/kernel/exceptions/exception_vectors_as.S index ac68f059..e22f7e77 100644 --- a/kernel/exceptions/exception_vectors_as.S +++ b/kernel/exceptions/exception_vectors_as.S @@ -1,3 +1,5 @@ +#include "process/context_save.S" + .section .vectors, "a", %progbits .align 11 .global exception_vectors @@ -31,32 +33,15 @@ exception_vectors: .global irq_el1_asm_handler irq_el1_asm_handler: - msr daifset, #2 - mrs x10, spsr_el1 - lsr x18, x10, #2 - and x18, x18, #0b11 - - cmp x18, #1 - b.eq 1f - cmp x18, #0 - b.eq 2f - - b 3f - -1: mov x11, sp - b 3f - -2: mrs x11, sp_el0 - ldr x18, =ksp - //TODO: in order to ensure we don't ovewrite any stack data, save it to its own global variable - mov sp, x18 - -3: - mov x15, x0 - mov x14, x1 - mov x9, x2 - mov x16, x3 - mov x13, x29 - mov x12, x30 + save_ctx + b irq_el1_handler + eret + +.global sync_el0_handler_as +//TODO: Rethink the registers used to be sequential both here and in context_switch and exception_vectors_as +sync_el0_handler_as: + save_ctx + + b sync_el0_handler_c eret \ No newline at end of file diff --git a/kernel/exceptions/irq.c b/kernel/exceptions/irq.c index 7936e841..5166b083 100644 --- a/kernel/exceptions/irq.c +++ b/kernel/exceptions/irq.c @@ -73,7 +73,6 @@ void disable_interrupt(){ } void irq_el1_handler() { - save_context_registers(); save_return_address_interrupt(); syscall_depth++; uint32_t irq; diff --git a/kernel/process/context_save.S b/kernel/process/context_save.S new file mode 100644 index 00000000..53338073 --- /dev/null +++ b/kernel/process/context_save.S @@ -0,0 +1,51 @@ +.macro save_ctx +msr daifset, #2 + mrs x18, spsr_el1 + lsr x18, x18, #2 + and x18, x18, #0b11 + + cmp x18, #1 + b.eq 1f + cmp x18, #0 + b.eq 2f + + b 3f + +1: mov x17, sp + b 3f + +2: mrs x17, sp_el0 + ldr x18, =ksp + //TODO: in order to ensure we don't ovewrite any stack data, save it to its own global variable + mov sp, x18 + +3: + +ldr x18, =cpec +ldr x18, [x18] +// Save general-purpose registers x1-x30 +stp x0, x1, [x18, #(8 * 0)] +stp x2, x3, [x18, #(8 * 2)] +stp x4, x5, [x18, #(8 * 4)] +stp x6, x7, [x18, #(8 * 6)] +stp x8, x9, [x18, #(8 * 8)] +stp x10, x11, [x18, #(8 * 10)] +stp x12, x13, [x18, #(8 * 12)] +stp x14, x15, [x18, #(8 * 14)] +str x16, [x18, #(8 * 16)] +str x19, [x18, #(8 * 19)] +stp x20, x21, [x18, #(8 * 20)] +stp x22, x23, [x18, #(8 * 22)] +stp x24, x25, [x18, #(8 * 24)] +stp x26, x27, [x18, #(8 * 26)] +stp x28, x29, [x18, #(8 * 28)] +str x30, [x18,#(8 * 30)] + +// SP +str x17, [x18, #(8 * 31)] + +//Status bits +mrs x17, spsr_el1 +str x17, [x18, #(8 * 33)] + +.endm \ No newline at end of file diff --git a/kernel/process/context_switch.S b/kernel/process/context_switch.S index e03b73cb..39eaa0d6 100644 --- a/kernel/process/context_switch.S +++ b/kernel/process/context_switch.S @@ -1,33 +1,3 @@ -.global save_context -save_context: - // x0: pointer to process_t - - // Save general-purpose registers x1-x30 (excluding x0 which holds the pointer) - stp x1, x2, [x0, #(8 * 0)] - stp x3, x4, [x0, #(8 * 2)] - stp x5, x6, [x0, #(8 * 4)] - stp x7, x8, [x0, #(8 * 6)] - stp x9, x10, [x0, #(8 * 8)] - stp x11, x12, [x0, #(8 * 10)] - stp x13, x14, [x0, #(8 * 12)] - stp x15, x16, [x0, #(8 * 14)] - stp x17, x18, [x0, #(8 * 16)] - stp x19, x20, [x0, #(8 * 18)] - stp x21, x22, [x0, #(8 * 20)] - stp x23, x24, [x0, #(8 * 22)] - stp x25, x26, [x0, #(8 * 24)] - stp x27, x28, [x0, #(8 * 26)] - stp x29, x30, [x0, #(8 * 28)] - - // SP - str x11, [x0, #(8 * 31)] - - //Status bits - and x10, x10, 0xFFF - str x10, [x0, #(8 * 33)] - - ret - .global save_pc_interrupt save_pc_interrupt: mrs x1, elr_el1 @@ -37,50 +7,51 @@ save_pc_interrupt: .global restore_context restore_context: + ldr x18, =cpec + ldr x18, [x18] // Restore general-purpose registers - ldp x1, x2, [x0, #(8 * 0)] - ldp x3, x4, [x0, #(8 * 2)] - ldp x5, x6, [x0, #(8 * 4)] - ldp x7, x8, [x0, #(8 * 6)] - ldp x9, x10, [x0, #(8 * 8)] - ldp x11, x12, [x0, #(8 * 10)] - ldp x13, x14, [x0, #(8 * 12)] - ldp x15, x16, [x0, #(8 * 14)] - ldp x17, x18, [x0, #(8 * 16)] - ldp x19, x20, [x0, #(8 * 18)] - ldp x21, x22, [x0, #(8 * 20)] - ldp x23, x24, [x0, #(8 * 22)] - ldp x25, x26, [x0, #(8 * 24)] - ldp x27, x28, [x0, #(8 * 26)] - ldp x29, x30, [x0, #(8 * 28)] - - ldr x11, [x0, #(8 * 31)] - ldr x10, [x0, #(8 * 33)] - msr spsr_el1, x10 - lsr x18, x10, #2 - and x18, x18, #0b11 - - cmp x18, #1 + ldp x0, x1, [x18, #(8 * 0)] + ldp x2, x3, [x18, #(8 * 2)] + ldp x4, x5, [x18, #(8 * 4)] + ldp x6, x7, [x18, #(8 * 6)] + ldp x8, x9, [x18, #(8 * 8)] + ldp x10, x11, [x18, #(8 * 10)] + ldp x12, x13, [x18, #(8 * 12)] + ldp x14, x15, [x18, #(8 * 14)] + ldr x16, [x18, #(8 * 16)] + ldr x19, [x18, #(8 * 19)] + ldp x20, x21, [x18, #(8 * 20)] + ldp x22, x23, [x18, #(8 * 22)] + ldp x24, x25, [x18, #(8 * 24)] + ldp x26, x27, [x18, #(8 * 26)] + ldp x28, x29, [x18, #(8 * 28)] + ldr x30, [x18, #(8 * 30)] + + ldr x17, [x18, #(8 * 33)] + msr spsr_el1, x17 + lsr x17, x17, #2 + and x17, x17, #0b11 + + cmp x17, #1 b.eq 1f - cmp x18, #0 + cmp x17, #0 b.eq 2f b 3f -1: mov sp, x11 +1: ldr x17, [x18, #(8 * 31)] + mov sp, x17 b 3f -2: msr sp_el0, x11 +2: ldr x17, [x18, #(8 * 31)] + msr sp_el0, x17 3: - mov x29, x11 - ldr x11, [x0, #(8 * 32)] - msr elr_el1, x11 - - mov x0, x15 - mov x1, x14 - mov x30, x12 - mov x2, x9 - mov x3, x16 + mov x29, x17 + ldr x17, [x18, #(8 * 32)] + msr elr_el1, x17 + + mov x17, #0 + mov x18, #0 eret \ No newline at end of file diff --git a/kernel/process/process.h b/kernel/process/process.h index 28d964fb..1ce05785 100644 --- a/kernel/process/process.h +++ b/kernel/process/process.h @@ -62,11 +62,11 @@ typedef struct { } process_t; //Helper functions for accessing registers mapped to scratch regs -#define PROC_X0 regs[14] -#define PROC_X1 regs[13] -#define PROC_X2 regs[8] -#define PROC_X3 regs[15] -#define PROC_X4 regs[3] +#define PROC_X0 regs[0] +#define PROC_X1 regs[1] +#define PROC_X2 regs[2] +#define PROC_X3 regs[3] +#define PROC_X4 regs[4] // #define PROC_FP regs[12] // #define PROC_LR regs[11] // #define PROC_SP regs[10] diff --git a/kernel/process/scheduler.c b/kernel/process/scheduler.c index 0062c847..8cd539df 100644 --- a/kernel/process/scheduler.c +++ b/kernel/process/scheduler.c @@ -15,9 +15,8 @@ #include "memory/mmu.h" #include "process/syscall.h" -extern void save_context(process_t* proc); -extern void save_pc_interrupt(process_t* proc); -extern void restore_context(process_t* proc); +extern void save_pc_interrupt(uintptr_t ptr); +extern void restore_context(uintptr_t ptr); //TODO: use queues, eliminate the max procs limitation process_t processes[MAX_PROCS]; @@ -48,12 +47,8 @@ typedef struct proc_open_file { void* proc_page; -void save_context_registers(){ - save_context(&processes[current_proc]); -} - void save_return_address_interrupt(){ - save_pc_interrupt(&processes[current_proc]); + save_pc_interrupt(cpec); } void switch_proc(ProcSwitchReason reason) { @@ -66,6 +61,7 @@ void switch_proc(ProcSwitchReason reason) { } current_proc = next_proc; + cpec = (uintptr_t)&processes[current_proc]; timer_reset(processes[current_proc].priority); process_restore(); } @@ -76,7 +72,7 @@ void save_syscall_return(uint64_t value){ void process_restore(){ syscall_depth--; - restore_context(&processes[current_proc]); + restore_context(cpec); } bool start_scheduler(){ @@ -159,6 +155,7 @@ void reset_process(process_t *proc){ void init_main_process(){ proc_page = palloc(0x1000, MEM_PRIV_KERNEL, MEM_RW, false); process_t* proc = &processes[0]; + cpec = (uintptr_t)&processes[0]; proc->id = next_proc_index++; proc->state = BLOCKED; proc->heap = (uintptr_t)palloc(0x1000, MEM_PRIV_KERNEL, MEM_RW, false); diff --git a/kernel/process/scheduler.h b/kernel/process/scheduler.h index b62267d0..8ea84f2e 100644 --- a/kernel/process/scheduler.h +++ b/kernel/process/scheduler.h @@ -18,7 +18,6 @@ typedef enum { void switch_proc(ProcSwitchReason reason); bool start_scheduler(); -void save_context_registers(); void save_return_address_interrupt(); void init_main_process(); process_t* init_process(); diff --git a/kernel/process/syscall.c b/kernel/process/syscall.c index c3491513..b464164c 100644 --- a/kernel/process/syscall.c +++ b/kernel/process/syscall.c @@ -21,6 +21,7 @@ #include "bin/bin_mod.h" int syscall_depth = 0; +uintptr_t cpec; //TODO: What happens if we pass another process' data in here? //TODO: make indexmap in c and it can be used here @@ -260,7 +261,6 @@ void coredump(uint64_t esr, uint64_t elr, uint64_t far){ } void sync_el0_handler_c(){ - save_context_registers(); save_return_address_interrupt(); syscall_depth++; diff --git a/kernel/process/syscall.h b/kernel/process/syscall.h index 42515e59..61c24fdc 100644 --- a/kernel/process/syscall.h +++ b/kernel/process/syscall.h @@ -2,4 +2,5 @@ #include "types.h" -extern int syscall_depth; \ No newline at end of file +extern int syscall_depth; +extern uintptr_t cpec; \ No newline at end of file diff --git a/kernel/process/syscall_as.S b/kernel/process/syscall_as.S deleted file mode 100644 index b6399480..00000000 --- a/kernel/process/syscall_as.S +++ /dev/null @@ -1,33 +0,0 @@ -.global sync_el0_handler_as - -//TODO: Rethink the registers used to be sequential both here and in context_switch and exception_vectors_as -sync_el0_handler_as: - msr daifset, #2 - mrs x10, spsr_el1 - lsr x18, x10, #2 - and x18, x18, #0b11 - - cmp x18, #1 - b.eq 1f - cmp x18, #0 - b.eq 2f - - b 3f - -1: mov x11, sp - b 3f - -2: mrs x11, sp_el0 - ldr x18, =ksp - //TODO: in order to ensure we don't ovewrite any stack data, save it to its own global variable - mov sp, x18 - -3: - mov x15, x0 - mov x14, x1 - mov x9, x2 - mov x16, x3 - mov x13, x29 - mov x12, x30 - b sync_el0_handler_c - eret \ No newline at end of file diff --git a/shared/Makefile b/shared/Makefile index 96d80111..593e7db5 100644 --- a/shared/Makefile +++ b/shared/Makefile @@ -40,8 +40,8 @@ $(BUILD_DIR)/%.o: %.cpp @mkdir -p $(dir $@) $(VCXX) $(CXXFLAGS) -c -MMD -MP $< -o $@ -# Might want to also delete the directories created inside BUILD_DIR clean: $(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(TARGET) + $(RM) -r $(BUILD_DIR) -include $(DEP)