fixed process handling & refactor
This commit is contained in:
21
kernel.c
21
kernel.c
@@ -5,6 +5,12 @@ extern char __bss[], __bss_end[], __stack_top[];
|
|||||||
extern char __free_ram[], __free_ram_end[];
|
extern char __free_ram[], __free_ram_end[];
|
||||||
extern char __kernel_base[];
|
extern char __kernel_base[];
|
||||||
|
|
||||||
|
struct process procs[PROCS_MAX];
|
||||||
|
struct process *current_proc;
|
||||||
|
struct process *idle_proc;
|
||||||
|
struct process *proc_a;
|
||||||
|
struct process *proc_b;
|
||||||
|
|
||||||
paddr_t alloc_pages(uint32_t n) {
|
paddr_t alloc_pages(uint32_t n) {
|
||||||
static paddr_t next_paddr = (paddr_t) __free_ram;
|
static paddr_t next_paddr = (paddr_t) __free_ram;
|
||||||
paddr_t paddr = next_paddr;
|
paddr_t paddr = next_paddr;
|
||||||
@@ -192,8 +198,6 @@ __attribute__((naked)) void switch_context(uint32_t *prev_sp,
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct process procs[PROCS_MAX];
|
|
||||||
|
|
||||||
struct process *create_process(uint32_t pc) {
|
struct process *create_process(uint32_t pc) {
|
||||||
// Find an unused proccess control structure
|
// Find an unused proccess control structure
|
||||||
struct process *proc = NULL;
|
struct process *proc = NULL;
|
||||||
@@ -243,9 +247,6 @@ void delay(void) {
|
|||||||
__asm__ __volatile__("nop"); // do nothing
|
__asm__ __volatile__("nop"); // do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
struct process *current_proc;
|
|
||||||
struct process *idle_proc;
|
|
||||||
|
|
||||||
void yield(void) {
|
void yield(void) {
|
||||||
// Search for a runnable process
|
// Search for a runnable process
|
||||||
struct process *next = idle_proc;
|
struct process *next = idle_proc;
|
||||||
@@ -261,6 +262,9 @@ void yield(void) {
|
|||||||
if (next == current_proc)
|
if (next == current_proc)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
struct process *prev = current_proc;
|
||||||
|
current_proc = next;
|
||||||
|
|
||||||
__asm__ __volatile__(
|
__asm__ __volatile__(
|
||||||
"sfence.vma\n"
|
"sfence.vma\n"
|
||||||
"csrw satp, %[satp]\n"
|
"csrw satp, %[satp]\n"
|
||||||
@@ -270,15 +274,10 @@ void yield(void) {
|
|||||||
: [satp] "r" (SATP_SV32 | ((uint32_t) next->page_table / PAGE_SIZE)),
|
: [satp] "r" (SATP_SV32 | ((uint32_t) next->page_table / PAGE_SIZE)),
|
||||||
[sscratch] "r" ((uint32_t) &next->stack[sizeof(next->stack)])
|
[sscratch] "r" ((uint32_t) &next->stack[sizeof(next->stack)])
|
||||||
);
|
);
|
||||||
// Context switch
|
|
||||||
struct process *prev = current_proc;
|
|
||||||
current_proc = next;
|
|
||||||
switch_context(&prev->sp, &next->sp);
|
switch_context(&prev->sp, &next->sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct process *proc_a;
|
|
||||||
struct process *proc_b;
|
|
||||||
|
|
||||||
void proc_a_entry(void) {
|
void proc_a_entry(void) {
|
||||||
printf("Start process A\n");
|
printf("Start process A\n");
|
||||||
while (1) {
|
while (1) {
|
||||||
|
Reference in New Issue
Block a user