1 #include <lunaix/process.h>
2 #include <lunaix/sched.h>
3 #include <lunaix/syscall.h>
4 #include <lunaix/syscall_utils.h>
5 #include <lunaix/mm/mmap.h>
6 #include <lunaix/mm/page.h>
7 #include <lunaix/syslog.h>
9 #include <usr/lunaix/threads.h>
12 #include <sys/mm/mm_defs.h>
17 __alloc_user_thread_stack(struct proc_info* proc,
18 struct mm_region** stack_region, ptr_t vm_mnt)
20 ptr_t th_stack_top = (proc->thread_count + 1) * USR_STACK_SIZE_THREAD;
21 th_stack_top = ROUNDUP(USR_STACK_END - th_stack_top, PAGE_SIZE);
23 struct mm_region* vmr;
24 struct proc_mm* mm = vmspace(proc);
25 struct mmap_param param = { .vms_mnt = vm_mnt,
27 .mlen = USR_STACK_SIZE_THREAD,
28 .proct = PROT_READ | PROT_WRITE,
29 .flags = MAP_ANON | MAP_PRIVATE,
30 .type = REGION_TYPE_STACK };
34 errno = mmap_user((void**)&th_stack_top, &vmr, th_stack_top, NULL, ¶m);
36 WARN("failed to create user thread stack: %d", errno);
40 pte_t* guardp = mkptep_va(vm_mnt, vmr->start);
41 set_pte(guardp, guard_pte);
45 ptr_t stack_top = align_stack(th_stack_top + USR_STACK_SIZE_THREAD - 1);
50 __alloc_kernel_thread_stack(struct proc_info* proc, ptr_t vm_mnt)
52 pfn_t kstack_top = leaf_count(KSTACK_AREA_END);
53 pfn_t kstack_end = pfn(KSTACK_AREA);
54 pte_t* ptep = mkptep_pn(vm_mnt, kstack_top);
55 while (ptep_pfn(ptep) > kstack_end) {
56 ptep -= KSTACK_PAGES + 1;
58 pte_t pte = pte_at(ptep + 1);
59 if (pte_isnull(pte)) {
64 WARN("failed to create kernel stack: max stack num reach\n");
68 unsigned int po = count_order(KSTACK_PAGES);
69 struct leaflet* leaflet = alloc_leaflet(po);
72 WARN("failed to create kernel stack: nomem\n");
76 set_pte(ptep, guard_pte);
77 ptep_map_leaflet(ptep + 1, mkpte_prot(KERNEL_DATA), leaflet);
80 return align_stack(ptep_va(ptep, LFT_SIZE) - 1);
84 thread_release_mem(struct thread* thread)
86 struct leaflet* leaflet;
87 struct proc_mm* mm = vmspace(thread->process);
88 ptr_t vm_mnt = mm->vm_mnt;
90 // Ensure we have mounted
93 pte_t* ptep = mkptep_va(vm_mnt, thread->kstack);
94 leaflet = pte_leaflet(*ptep);
96 ptep -= KSTACK_PAGES - 1;
97 set_pte(ptep, null_pte);
98 ptep_unmap_leaflet(ptep + 1, leaflet);
100 leaflet_return(leaflet);
102 if (thread->ustack) {
103 if ((thread->ustack->start & 0xfff)) {
104 fail("invalid ustack struct");
106 mem_unmap_region(vm_mnt, thread->ustack);
111 create_thread(struct proc_info* proc, bool with_ustack)
113 struct proc_mm* mm = vmspace(proc);
116 ptr_t vm_mnt = mm->vm_mnt;
117 struct mm_region* ustack_region = NULL;
119 !(__alloc_user_thread_stack(proc, &ustack_region, vm_mnt)))
124 ptr_t kstack = __alloc_kernel_thread_stack(proc, vm_mnt);
126 mem_unmap_region(vm_mnt, ustack_region);
130 struct thread* th = alloc_thread(proc);
136 th->ustack = ustack_region;
139 th->ustack_top = align_stack(ustack_region->end - 1);
146 start_thread(struct thread* th, ptr_t entry)
149 struct proc_mm* mm = vmspace(th->process);
153 struct hart_transition transition;
154 if (!kernel_addr(entry)) {
157 hart_user_transfer(&transition, th->kstack, th->ustack_top, entry);
160 hart_kernel_transfer(&transition, th->kstack, entry);
163 install_hart_transition(mm->vm_mnt, &transition);
164 th->hstate = (struct hart_state*)transition.inject;
170 exit_thread(void* val) {
171 terminate_current_thread((ptr_t)val);
176 thread_find(struct proc_info* proc, tid_t tid)
178 struct thread *pos, *n;
179 llist_for_each(pos, n, &proc->threads, proc_sibs) {
180 if (pos->tid == tid) {
188 __DEFINE_LXSYSCALL3(int, th_create, tid_t*, tid,
189 struct uthread_param*, thparam, void*, entry)
191 struct thread* th = create_thread(__current, true);
198 ustack_top = th->ustack_top;
199 ustack_top = align_stack(ustack_top - sizeof(*thparam));
201 memcpy((void*)ustack_top, thparam, sizeof(*thparam));
203 th->ustack_top = ustack_top;
204 start_thread(th, (ptr_t)entry);
213 __DEFINE_LXSYSCALL(tid_t, th_self)
215 return current_thread->tid;
218 __DEFINE_LXSYSCALL1(void, th_exit, void*, val)
223 __DEFINE_LXSYSCALL2(int, th_join, tid_t, tid, void**, val_ptr)
225 struct thread* th = thread_find(__current, tid);
230 if (th == current_thread) {
234 while (!proc_terminated(th)) {
239 *val_ptr = (void*)th->exit_val;
247 __DEFINE_LXSYSCALL1(int, th_detach, tid_t, tid)
249 // can not detach the only thread
250 if (__current->thread_count == 1) {
254 struct thread* th = thread_find(__current, tid);
263 __DEFINE_LXSYSCALL2(int, th_kill, tid_t, tid, int, signum)
265 struct thread* target = thread_find(__current, tid);
270 if (signum > _SIG_NUM) {
275 thread_setsignal(target, signum);