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;
21 th_stack_top = ROUNDUP(USR_STACK_END - th_stack_top, MEM_PAGE);
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,
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 set_pte(mkptep_va(vm_mnt, vmr->start), guard_pte);
44 ptr_t stack_top = align_stack(th_stack_top + USR_STACK_SIZE - 1);
49 __alloc_kernel_thread_stack(struct proc_info* proc, ptr_t vm_mnt)
51 pfn_t kstack_top = leaf_count(KSTACK_AREA_END);
52 pfn_t kstack_end = pfn(KSTACK_AREA);
53 pte_t* ptep = mkptep_pn(vm_mnt, kstack_top);
54 while (ptep_pfn(ptep) > kstack_end) {
57 // first page in the kernel stack is guardian page
58 pte_t pte = *(ptep + 1);
59 if (pte_isnull(pte)) {
64 WARN("failed to create kernel stack: max stack num reach\n");
68 // KSTACK_PAGES = 3, removal one guardian pte, give order 1 page
69 struct leaflet* leaflet = alloc_leaflet(1);
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;
142 start_thread(struct thread* th, ptr_t entry)
145 struct proc_mm* mm = vmspace(th->process);
149 struct hart_transition transition;
150 if (!kernel_addr(entry)) {
153 ptr_t ustack_top = align_stack(th->ustack->end - 1);
154 ustack_top -= 16; // pre_allocate a 16 byte for inject parameter
155 hart_user_transfer(&transition, th->kstack, ustack_top, entry);
157 th->ustack_top = ustack_top;
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_LXSYSCALL4(int, th_create, tid_t*, tid, struct uthread_info*, thinfo,
189 void*, entry, void*, param)
191 struct thread* th = create_thread(__current, true);
196 start_thread(th, (ptr_t)entry);
198 ptr_t ustack_top = th->ustack_top;
199 *((void**)ustack_top) = param;
201 thinfo->th_stack_sz = region_size(th->ustack);
202 thinfo->th_stack_top = (void*)ustack_top;
211 __DEFINE_LXSYSCALL(tid_t, th_self)
213 return current_thread->tid;
216 __DEFINE_LXSYSCALL1(void, th_exit, void*, val)
221 __DEFINE_LXSYSCALL2(int, th_join, tid_t, tid, void**, val_ptr)
223 struct thread* th = thread_find(__current, tid);
228 if (th == current_thread) {
232 while (!proc_terminated(th)) {
237 *val_ptr = (void*)th->exit_val;
245 __DEFINE_LXSYSCALL1(int, th_detach, tid_t, tid)
247 // can not detach the only thread
248 if (__current->thread_count == 1) {
252 struct thread* th = thread_find(__current, tid);
261 __DEFINE_LXSYSCALL2(int, th_kill, tid_t, tid, int, signum)
263 struct thread* target = thread_find(__current, tid);
268 if (signum > _SIG_NUM) {
273 thread_setsignal(target, signum);