From 45db1523a02730a172564ca8c2be55422d5d2f0b Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 22 Aug 2020 01:10:59 +0200 Subject: Fixed some TODOs --- apps/wm.c | 1 + kernel/features/event.c | 31 ++++++++++++++++--------------- kernel/features/proc.c | 8 ++++---- kernel/features/syscall.c | 4 ++-- kernel/inc/event.h | 4 ++-- libc/inc/assert.h | 9 +++++++-- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/apps/wm.c b/apps/wm.c index 88c0451..fa7f6de 100644 --- a/apps/wm.c +++ b/apps/wm.c @@ -33,6 +33,7 @@ int main(int argc, char **argv) printf("onkey: %x\n", onkey); map(EVENT_KEYBOARD, onkey); + map(EVENT_KEYBOARD, onkey); while (1) { }; diff --git a/kernel/features/event.c b/kernel/features/event.c index 9fd6da2..8a2c904 100644 --- a/kernel/features/event.c +++ b/kernel/features/event.c @@ -10,40 +10,41 @@ struct list *event_table[] = { [EVENT_KEYBOARD] = NULL, [EVENT_MOUSE] = NULL }; -u32 event_map(enum event id, u32 *func) +u32 event_map(enum event id, struct proc *proc, u32 *func) { - // TODO: Check if function is already mapped - if (id >= sizeof(event_table) / sizeof(*event_table)) - return -1; + assert(id < sizeof(event_table) / sizeof(*event_table)); if (event_table[id] == NULL) event_table[id] = (struct list *)list_new(); + struct node *iterator = event_table[id]->head; + do { + assert(((struct event_descriptor *)iterator->data)->func != func); + } while ((iterator = iterator->next) != NULL); + struct event_descriptor *desc = malloc(sizeof(*desc)); desc->func = func; - desc->proc = proc_current(); + desc->proc = proc; list_add((struct list *)event_table[id], (void *)desc); return 0; } -// TODO: Fix unmap -void event_unmap(enum event id, u32 *func) +void event_unmap(enum event id, struct proc *proc, u32 *func) { + assert(id < sizeof(event_table) / sizeof(*event_table)); + struct list *list = ((struct list *)event_table[id]); struct event_descriptor *desc = malloc(sizeof(*desc)); desc->func = func; - desc->proc = proc_current(); + desc->proc = proc; struct node *iterator = list->head; - while (memcmp(iterator->data, (void *)desc, sizeof(*desc)) == 0) { - iterator = iterator->next; - if (!iterator) - return; - } - - list_remove(list, iterator); + do { + if (memcmp(iterator->data, desc, sizeof(*desc)) == 0) + list_remove(list, iterator); + } while ((iterator = iterator->next) != NULL); } u32 event_trigger(enum event id, u32 *data) diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 3e99e35..e4219b0 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -38,7 +38,6 @@ void scheduler(struct regs *regs) } } - /* proc_print(); */ memcpy(regs, &((struct proc *)current->data)->regs, sizeof(struct regs)); if (((struct proc *)current->data)->event) { @@ -77,7 +76,10 @@ void proc_print() struct proc *proc_current() { - return (struct proc *)current->data; + if (current) + return (struct proc *)current->data; + else + return NULL; } void proc_exit(struct proc *proc, int status) @@ -92,7 +94,6 @@ void proc_exit(struct proc *proc, int status) break; } } while ((iterator = iterator->next) != NULL); - proc_print(); } struct proc *proc_make() @@ -121,7 +122,6 @@ void proc_init() struct node *new = list_add(proc_list, proc_make()); bin_load("/init", new->data); - proc_print(); _eip = ((struct proc *)new->data)->regs.eip; _esp = ((struct proc *)new->data)->regs.useresp; diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index 2704690..e96cb65 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -68,12 +68,12 @@ void syscall_handler(struct regs *r) } case SYS_MAP: { printf("map\n"); - event_map(r->ebx, (u32 *)r->ecx); + event_map(r->ebx, proc_current(), (u32 *)r->ecx); break; } case SYS_UNMAP: { printf("unmap\n"); - event_unmap(r->ebx, (u32 *)r->ecx); + event_unmap(r->ebx, proc_current(), (u32 *)r->ecx); break; } default: { diff --git a/kernel/inc/event.h b/kernel/inc/event.h index d0fe4fc..d14fdb0 100644 --- a/kernel/inc/event.h +++ b/kernel/inc/event.h @@ -12,8 +12,8 @@ struct event_descriptor { struct proc *proc; }; -u32 event_map(enum event id, u32 *func); -void event_unmap(enum event id, u32 *func); +u32 event_map(enum event id, struct proc *proc, u32 *func); +void event_unmap(enum event id, struct proc *proc, u32 *func); u32 event_trigger(enum event id, u32 *data); #endif diff --git a/libc/inc/assert.h b/libc/inc/assert.h index a553444..758c91b 100644 --- a/libc/inc/assert.h +++ b/libc/inc/assert.h @@ -6,17 +6,22 @@ #include #ifdef kernel +#include #define assert(exp) \ if (!(exp)) { \ printf("%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp); \ - __asm__ volatile("cli\nhlt"); \ + struct proc *proc = proc_current(); \ + if (proc) \ + proc_exit(proc, 1); \ + else \ + __asm__ volatile("cli\nhlt"); \ } #elif defined(userspace) #include #define assert(exp) \ if (!(exp)) { \ printf("%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp); \ - sys0(SYS_LOOP); \ + sys1(SYS_EXIT, 1); \ } #else #error "No lib target specified. Please use -Dkernel or -Duserspace" -- cgit v1.2.3