Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ jobs:
shared/libshared.a
shared/*.h
shared/**/*.h
fs/redos/user/*.elf
fs/*
1 change: 1 addition & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ $(BUILD_DIR)/%.o: %.cpp

clean:
$(RM) $(CLEAN_OBJS) $(CLEAN_DEPS) $(ELF) $(TARGET)
$(RM) -r $(BUILD_DIR)

-include $(DEP)
39 changes: 12 additions & 27 deletions kernel/exceptions/exception_vectors_as.S
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "process/context_save.S"

.section .vectors, "a", %progbits
.align 11
.global exception_vectors
Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion kernel/exceptions/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void disable_interrupt(){
}

void irq_el1_handler() {
save_context_registers();
save_return_address_interrupt();
syscall_depth++;
uint32_t irq;
Expand Down
51 changes: 51 additions & 0 deletions kernel/process/context_save.S
Original file line number Diff line number Diff line change
@@ -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
101 changes: 36 additions & 65 deletions kernel/process/context_switch.S
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
10 changes: 5 additions & 5 deletions kernel/process/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
15 changes: 6 additions & 9 deletions kernel/process/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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(){
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion kernel/process/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion kernel/process/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++;
Expand Down
3 changes: 2 additions & 1 deletion kernel/process/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "types.h"

extern int syscall_depth;
extern int syscall_depth;
extern uintptr_t cpec;
33 changes: 0 additions & 33 deletions kernel/process/syscall_as.S

This file was deleted.

2 changes: 1 addition & 1 deletion shared/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)