diff --git a/include/aos/threads.h b/include/aos/threads.h index f615146..4e6d2c9 100644 --- a/include/aos/threads.h +++ b/include/aos/threads.h @@ -122,6 +122,8 @@ static inline void thread_once(thread_once_t *control, void (*func)(void)) { */ void thread_set_status(int status); +void thread_allocate_stack(struct thread *thread); + __END_DECLS #endif // LIBBARRELFISH_THREADS_H diff --git a/lib/aos/threads.c b/lib/aos/threads.c index d730fab..7845b4b 100644 --- a/lib/aos/threads.c +++ b/lib/aos/threads.c @@ -347,9 +347,8 @@ struct thread *thread_create_unrunnable(thread_func_t start_func, void *arg, // allocate stack // debug_printf("[thread_create_unrunnable] stacksize = %x\n", stacksize); assert((stacksize % sizeof(uintptr_t)) == 0); - stacksize += BASE_PAGE_SIZE; size_t bytes_allocated; - void *stack = paging_malloc(stacksize, &bytes_allocated, BASE_PAGE_SIZE); + void *stack = paging_malloc(stacksize + BASE_PAGE_SIZE, &bytes_allocated, BASE_PAGE_SIZE) + BASE_PAGE_SIZE; if (stack == NULL) { return NULL; } @@ -362,7 +361,7 @@ struct thread *thread_create_unrunnable(thread_func_t start_func, void *arg, release_spinlock(&thread_slabs_spinlock); // thread_mutex_unlock(&thread_slabs_mutex); if (space == NULL) { - free(stack); + // free(stack); return NULL; } @@ -1463,3 +1462,13 @@ void thread_deliver_exception_disabled(dispatcher_handle_t handle, disp_resume(handle, &thread->regs); } + +/** + * \brief Allocate all pages of the thread stack. + */ +void thread_allocate_stack(struct thread *thread) +{ + for (volatile uint8_t *p = thread->stack; (void*)p < thread->stack_top; p += BASE_PAGE_SIZE) { + *p; // cause a page fault + } +} diff --git a/usr/init/main.c b/usr/init/main.c index ee825c0..72f37ed 100644 --- a/usr/init/main.c +++ b/usr/init/main.c @@ -230,8 +230,6 @@ app_main(int argc, char *argv[]) { // - grading_test_early(); // - grading_test_late(); - // Grading - grading_setup_app_init(bi); // TODO: initialize mem allocator, vspace management here err = cap_retype(cap_selfep, cap_dispatcher, 0, ObjType_EndPointLMP, 0, 1); @@ -248,22 +246,36 @@ app_main(int argc, char *argv[]) { ram_alloc_set(ram_alloc_remote_core); - thread_create(urpc_server, &urpc_to_app_server); + struct thread *urpc_server_thread = thread_create(urpc_server, &urpc_to_app_server); + // Allocate all pages of the thread stack now. + // We can't have page fault while the thread is running, + // because URPC calls may only be done from the main thread. + thread_allocate_stack(urpc_server_thread); - // Grading - grading_test_early(); // TODO: Spawn system processes etc. here struct bootinfo_serialized * bi_ser; err = aos_urpc_get_bootinfo(&urpc_to_bsp, &bi_ser); - if (err_is_fail(err)) return err; + if (err_is_fail(err)) { + DEBUG_ERR(err, "in aos_urpc_get_bootinfo"); + abort(); + } err = deserialize_bootinfo(bi_ser, &bi); + if (err_is_fail(err)) { + DEBUG_ERR(err, "in deserialize_bootinfo"); + abort(); + } free(bi_ser); - if (err_is_fail(err)) return err; debug_printf("[app_main]: received bootinfo with %d regions\n", bi->regions_length); + // Grading + grading_setup_app_init(bi); + + // Grading + grading_test_early(); + // Grading grading_test_late();