diff --git a/src/dev.c b/src/dev.c index 09d3fcb..e94a77d 100644 --- a/src/dev.c +++ b/src/dev.c @@ -1,8 +1,5 @@ #include -#include -#include - isize dev_read(dev_t dev, u8 *buf, usize size) { return dev.driver->read(dev.num, buf, size); } diff --git a/src/dev_table.c b/src/dev_table.c index f56ea13..12da046 100644 --- a/src/dev_table.c +++ b/src/dev_table.c @@ -3,8 +3,7 @@ #include #include -extern dev_init_t _dev_init_start; -extern dev_init_t _dev_init_end; +extern dev_init_t _dev_init_start, _dev_init_end; static i32 init(void) { for (usize i = 0; DEV_TABLE[i].name; i++) { diff --git a/src/intr.c b/src/intr.c index 532213a..c14cbf0 100644 --- a/src/intr.c +++ b/src/intr.c @@ -15,10 +15,10 @@ static u32 nr_isrs = 0; i32 intr_register_isr(irq_t irq, void (*isr)(void *data), void *data) { if (nr_isrs == INTR_ISR_CAPACITY) return -EAGAIN; - isrs[nr_isrs].irq = irq; - isrs[nr_isrs].isr = isr; - isrs[nr_isrs].data = data; - nr_isrs++; + u32 idx = nr_isrs++; + isrs[idx].irq = irq; + isrs[idx].isr = isr; + isrs[idx].data = data; return 0; } diff --git a/src/kmain.c b/src/kmain.c index 99ede6d..bd76209 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -11,7 +11,6 @@ void init(void); proc_init(init_proc, init, 0, nullptr, 0, nullptr); sched_update_state(init_proc, PROC_STATE_READY); - sched_start(); unreachable(); diff --git a/src/syscalls/exit.c b/src/syscalls/exit.c index 00959b3..96e96db 100644 --- a/src/syscalls/exit.c +++ b/src/syscalls/exit.c @@ -4,30 +4,30 @@ [[noreturn]] isize sys_exit(const trapframe_t *frame) { i32 status = frame->a0; - proc_t *proc = curr_proc; - proc->exit_status = status; + curr_proc->exit_status = status; - while (!list_empty(&proc->children)) { - proc_t *child = list_first_entry(&proc->children, proc_t, tree_node); + while (!list_empty(&curr_proc->children)) { + proc_t *child = + list_first_entry(&curr_proc->children, proc_t, tree_node); proc_adopt(proc_table[1], child); } - while (!list_empty(&proc->zombie_children)) { + while (!list_empty(&curr_proc->zombie_children)) { proc_t *child = - list_first_entry(&proc->zombie_children, proc_t, tree_node); + list_first_entry(&curr_proc->zombie_children, proc_t, tree_node); proc_adopt(proc_table[1], child); } - proc_t *parent = proc->parent; - list_remove(&proc->tree_node); - list_push_back(&parent->zombie_children, &proc->tree_node); + proc_t *parent = curr_proc->parent; + list_remove(&curr_proc->tree_node); + list_push_back(&parent->zombie_children, &curr_proc->tree_node); if (parent->state == PROC_STATE_WAIT_CHILD) { - parent->proc_waiting = proc; + parent->proc_waiting = curr_proc; sched_update_state(parent, PROC_STATE_READY); } - sched_update_state(proc, PROC_STATE_ZOMBIE); + sched_update_state(curr_proc, PROC_STATE_ZOMBIE); unreachable(); } diff --git a/src/syscalls/proc.c b/src/syscalls/proc.c index d5eb36a..c5ab7d6 100644 --- a/src/syscalls/proc.c +++ b/src/syscalls/proc.c @@ -9,9 +9,11 @@ isize sys_proc(const trapframe_t *frame) { char **argv = (char **) frame->a1; if (!entry || !argv) return -EFAULT; - usize argc; - for (argc = 0; argv[argc]; argc++) - ; + + usize argc = 0; + while (argv[argc]) { + argc++; + } proc_t *proc = (proc_t *) kmalloc(sizeof(proc_t)); if (!proc) diff --git a/src/syscalls/read.c b/src/syscalls/read.c index 8f8e9e5..4da010f 100644 --- a/src/syscalls/read.c +++ b/src/syscalls/read.c @@ -7,10 +7,9 @@ isize sys_read(const trapframe_t *frame) { i32 fd = frame->a0; u8 *buf = (u8 *) frame->a1; usize size = frame->a2; - if (proc_is_bad_fd(curr_proc, fd)) + if (proc_is_bad_fd(curr_proc, fd) && + !(curr_proc->fds[fd].flags & FD_FLAG_READABLE)) return -EBADF; - if (!(curr_proc->fds[fd].flags & FD_FLAG_READABLE)) - return -EINVAL; if (!buf) return -EFAULT; diff --git a/src/syscalls/wait.c b/src/syscalls/wait.c index 3b7be09..d46603e 100644 --- a/src/syscalls/wait.c +++ b/src/syscalls/wait.c @@ -11,19 +11,16 @@ isize sys_wait(const trapframe_t *frame) { list_empty(&curr_proc->zombie_children)) return -ECHILD; + proc_t *proc; if (!list_empty(&curr_proc->zombie_children)) { - proc_t *proc = - list_first_entry(&curr_proc->zombie_children, proc_t, tree_node); - if (status) - *status = proc->exit_status & 0xFF; - proc_deinit(proc); - kfree(proc); - return proc->pid; + proc = list_first_entry(&curr_proc->zombie_children, proc_t, tree_node); + goto quit; } sched_update_state(curr_proc, PROC_STATE_WAIT_CHILD); + proc = curr_proc->proc_waiting; - proc_t *proc = curr_proc->proc_waiting; +quit: if (status) *status = proc->exit_status & 0xFF; proc_deinit(proc); diff --git a/src/syscalls/write.c b/src/syscalls/write.c index 2fddcff..945923a 100644 --- a/src/syscalls/write.c +++ b/src/syscalls/write.c @@ -7,10 +7,9 @@ isize sys_write(const trapframe_t *frame) { i32 fd = frame->a0; const u8 *buf = (const u8 *) frame->a1; usize size = frame->a2; - if (proc_is_bad_fd(curr_proc, fd)) + if (proc_is_bad_fd(curr_proc, fd) && + !(curr_proc->fds[fd].flags & FD_FLAG_WRITABLE)) return -EBADF; - if (!(curr_proc->fds[fd].flags & FD_FLAG_WRITABLE)) - return -EINVAL; if (!buf) return -EFAULT; diff --git a/src/timer.c b/src/timer.c index 495336b..149bdfa 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,9 +1,7 @@ #include #include -#include #include -#include #include extern const u32 TIMER_FREQ; diff --git a/src/trap.c b/src/trap.c index 35bb129..90e6c5b 100644 --- a/src/trap.c +++ b/src/trap.c @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -23,11 +22,10 @@ void trap_handler(trapframe_t *frame) { case CSR_SCAUSE_TIMER_INTR: timer_isr(); break; - case CSR_SCAUSE_EXT_INTR: { + case CSR_SCAUSE_EXT_INTR: intr_isr(); break; } - } } else { switch (frame->scause) { case CSR_SCAUSE_ECALL_U: