Skip to content

Commit

Permalink
Refine the code quality of core components
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Jun 12, 2024
1 parent 0f287ad commit bdf5315
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 43 deletions.
3 changes: 0 additions & 3 deletions src/dev.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <dev.h>

#include <driver.h>
#include <errno.h>

isize dev_read(dev_t dev, u8 *buf, usize size) {
return dev.driver->read(dev.num, buf, size);
}
Expand Down
3 changes: 1 addition & 2 deletions src/dev_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <init.h>
#include <string.h>

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++) {
Expand Down
8 changes: 4 additions & 4 deletions src/intr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion src/kmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 11 additions & 11 deletions src/syscalls/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 5 additions & 3 deletions src/syscalls/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/syscalls/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 5 additions & 8 deletions src/syscalls/wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/syscalls/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 0 additions & 2 deletions src/timer.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include <timer.h>

#include <csr.h>
#include <errno.h>
#include <init.h>
#include <kalloc.h>
#include <sbi.h>

extern const u32 TIMER_FREQ;
Expand Down
4 changes: 1 addition & 3 deletions src/trap.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <trap.h>

#include <config.h>
#include <csr.h>
#include <errno.h>
#include <init.h>
Expand All @@ -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:
Expand Down

0 comments on commit bdf5315

Please sign in to comment.