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/vmm.h>
7 #include <lunaix/mm/pmm.h>
8 #include <lunaix/syslog.h>
10 #include <usr/lunaix/threads.h>
13 #include <sys/mm/mm_defs.h>
18 __alloc_user_thread_stack(struct proc_info* proc, 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 };
32 int errno = mmap_user((void**)&th_stack_top, &vmr, th_stack_top, NULL, ¶m);
35 WARN("failed to create user thread stack: %d", errno);
39 set_pte(mkptep_va(vm_mnt, vmr->start), guard_pte);
43 ptr_t stack_top = align_stack(th_stack_top + USR_STACK_SIZE - 1);
48 __alloc_kernel_thread_stack(struct proc_info* proc, ptr_t vm_mnt)
50 pfn_t kstack_top = leaf_count(KSTACK_AREA_END);
51 pfn_t kstack_end = pfn(KSTACK_AREA);
52 pte_t* ptep = mkptep_pn(vm_mnt, kstack_top);
53 while (ptep_pfn(ptep) > kstack_end) {
56 // first page in the kernel stack is guardian page
57 pte_t pte = *(ptep + 1);
58 if (pte_isnull(pte)) {
63 WARN("failed to create kernel stack: max stack num reach\n");
67 ptr_t pa = pmm_alloc_cpage(KSTACK_PAGES - 1, 0);
70 WARN("failed to create kernel stack: nomem\n");
74 set_pte(ptep, guard_pte);
76 pte_t pte = mkpte(pa, KERNEL_DATA);
77 vmm_set_ptes_contig(ptep + 1, pte, LFT_SIZE, KSTACK_PAGES - 1);
80 return align_stack(ptep_va(ptep, LFT_SIZE) - 1);
84 thread_release_mem(struct thread* thread)
86 struct proc_mm* mm = vmspace(thread->process);
87 ptr_t vm_mnt = mm->vm_mnt;
89 // Ensure we have mounted
92 pte_t* ptep = mkptep_va(vm_mnt, thread->kstack);
94 ptep -= KSTACK_PAGES - 1;
95 vmm_unset_ptes(ptep, KSTACK_PAGES);
98 if ((thread->ustack->start & 0xfff)) {
99 fail("invalid ustack struct");
101 mem_unmap_region(vm_mnt, thread->ustack);
106 create_thread(struct proc_info* proc, bool with_ustack)
108 struct proc_mm* mm = vmspace(proc);
111 ptr_t vm_mnt = mm->vm_mnt;
112 struct mm_region* ustack_region = NULL;
114 !(__alloc_user_thread_stack(proc, &ustack_region, vm_mnt)))
119 ptr_t kstack = __alloc_kernel_thread_stack(proc, vm_mnt);
121 mem_unmap_region(vm_mnt, ustack_region);
125 struct thread* th = alloc_thread(proc);
131 th->ustack = ustack_region;
137 start_thread(struct thread* th, ptr_t entry)
140 struct proc_mm* mm = vmspace(th->process);
144 struct transfer_context transfer;
145 if (!kernel_addr(entry)) {
148 ptr_t ustack_top = align_stack(th->ustack->end - 1);
149 ustack_top -= 16; // pre_allocate a 16 byte for inject parameter
150 thread_create_user_transfer(&transfer, th->kstack, ustack_top, entry);
152 th->ustack_top = ustack_top;
155 thread_create_kernel_transfer(&transfer, th->kstack, entry);
158 inject_transfer_context(mm->vm_mnt, &transfer);
159 th->intr_ctx = (isr_param*)transfer.inject;
165 exit_thread(void* val) {
166 terminate_current_thread((ptr_t)val);
171 thread_find(struct proc_info* proc, tid_t tid)
173 struct thread *pos, *n;
174 llist_for_each(pos, n, &proc->threads, proc_sibs) {
175 if (pos->tid == tid) {
183 __DEFINE_LXSYSCALL4(int, th_create, tid_t*, tid, struct uthread_info*, thinfo,
184 void*, entry, void*, param)
186 struct thread* th = create_thread(__current, true);
191 start_thread(th, (ptr_t)entry);
193 ptr_t ustack_top = th->ustack_top;
194 *((void**)ustack_top) = param;
196 thinfo->th_stack_sz = region_size(th->ustack);
197 thinfo->th_stack_top = (void*)ustack_top;
206 __DEFINE_LXSYSCALL(tid_t, th_self)
208 return current_thread->tid;
211 __DEFINE_LXSYSCALL1(void, th_exit, void*, val)
216 __DEFINE_LXSYSCALL2(int, th_join, tid_t, tid, void**, val_ptr)
218 struct thread* th = thread_find(__current, tid);
223 if (th == current_thread) {
227 while (!proc_terminated(th)) {
232 *val_ptr = (void*)th->exit_val;
240 __DEFINE_LXSYSCALL1(int, th_detach, tid_t, tid)
242 // can not detach the only thread
243 if (__current->thread_count == 1) {
247 struct thread* th = thread_find(__current, tid);
256 __DEFINE_LXSYSCALL2(int, th_kill, tid_t, tid, int, signum)
258 struct thread* target = thread_find(__current, tid);
263 if (signum > _SIG_NUM) {
268 thread_setsignal(target, signum);