sched: implement user-mode task and thread creation

This commit is contained in:
2026-02-08 13:11:17 +00:00
parent d2f303680d
commit ee82097017
4 changed files with 129 additions and 9 deletions

View File

@@ -29,7 +29,7 @@ struct thread *thread_alloc(void)
return t;
}
kern_status_t thread_init(struct thread *thr, uintptr_t ip)
kern_status_t thread_init_kernel(struct thread *thr, virt_addr_t ip)
{
thr->tr_id = thr->tr_parent->t_next_thread_id++;
@@ -46,7 +46,51 @@ kern_status_t thread_init(struct thread *thr, uintptr_t ip)
+ vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
thr->tr_bp = thr->tr_sp;
prepare_stack(ip, &thr->tr_sp);
ml_thread_prepare_kernel_context(ip, &thr->tr_sp);
return KERN_OK;
}
kern_status_t thread_init_user(
struct thread *thr,
virt_addr_t ip,
virt_addr_t sp)
{
thr->tr_id = thr->tr_parent->t_next_thread_id++;
thr->tr_prio = PRIO_NORMAL;
thr->tr_state = THREAD_READY;
thr->tr_quantum_target = default_quantum();
thr->tr_kstack = vm_page_alloc(THREAD_KSTACK_ORDER, VM_NORMAL);
if (!thr->tr_kstack) {
return KERN_NO_MEMORY;
}
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack)
+ vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
thr->tr_bp = thr->tr_sp;
thr->tr_cpu_kernel_sp = thr->tr_sp;
/* the new thread needs two contextx:
* 1) to get the thread running in kernel mode, so that it can
* execute ml_thread_switch_user
* 2) to allow ml_thread_switch_user to jump to the correct place
* in usermode (and with the correct stack).
*
* these two contexts are constructed on the thread's kernel stack
* in reverse order.
*/
/* this context will be used by ml_user_return to jump to userspace
* with the specified instruction pointer and user stack */
ml_thread_prepare_user_context(ip, sp, &thr->tr_sp);
/* this context will be used by the scheduler and ml_thread_switch to
* jump to ml_user_return in kernel mode with the thread's kernel stack.
*/
ml_thread_prepare_kernel_context(
(uintptr_t)ml_thread_switch_user,
&thr->tr_sp);
return KERN_OK;
}
@@ -89,7 +133,7 @@ struct thread *create_kernel_thread(void (*fn)(void))
thr->tr_state = THREAD_READY;
thr->tr_quantum_target = default_quantum();
thread_init(thr, (uintptr_t)fn);
thread_init_kernel(thr, (uintptr_t)fn);
unsigned long flags;
task_lock_irqsave(kernel, &flags);