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>
8 #include <lunaix/kpreempt.h>
10 #include <usr/lunaix/threads.h>
13 #include <asm/mm_defs.h>
18 __alloc_user_thread_stack(struct proc_info* proc,
19 struct mm_region** stack_region, ptr_t vm_mnt)
21 ptr_t th_stack_top = (proc->thread_count + 1) * USR_STACK_SIZE_THREAD;
22 th_stack_top = ROUNDUP(USR_STACK_END - th_stack_top, PAGE_SIZE);
24 struct mm_region* vmr;
25 struct proc_mm* mm = vmspace(proc);
26 struct mmap_param param = { .vms_mnt = vm_mnt,
28 .mlen = USR_STACK_SIZE_THREAD,
29 .proct = PROT_READ | PROT_WRITE,
30 .flags = MAP_ANON | MAP_PRIVATE,
31 .type = REGION_TYPE_STACK };
35 errno = mmap_user((void**)&th_stack_top, &vmr, th_stack_top, NULL, ¶m);
37 WARN("failed to create user thread stack: %d", errno);
41 pte_t* guardp = mkptep_va(vm_mnt, vmr->start);
42 set_pte(guardp, guard_pte);
46 ptr_t stack_top = align_stack(th_stack_top + USR_STACK_SIZE_THREAD - 1);
51 __alloc_kernel_thread_stack(struct proc_info* proc, ptr_t vm_mnt)
53 pfn_t kstack_top = pfn(KSTACK_AREA_END);
54 pfn_t kstack_end = pfn(KSTACK_AREA);
55 pte_t* ptep = mkptep_pn(vm_mnt, kstack_top);
56 while (ptep_pfn(ptep) > kstack_end) {
59 pte_t pte = pte_at(ptep);
60 if (pte_isnull(pte)) {
67 WARN("failed to create kernel stack: max stack num reach\n");
71 unsigned int po = count_order(KSTACK_PAGES);
72 struct leaflet* leaflet = alloc_leaflet(po);
75 WARN("failed to create kernel stack: nomem\n");
79 set_pte(ptep++, guard_pte);
80 ptep_map_leaflet(ptep, mkpte_prot(KERNEL_DATA), leaflet);
83 return align_stack(ptep_va(ptep, LFT_SIZE) - 1);
87 thread_release_mem(struct thread* thread)
89 struct leaflet* leaflet;
90 struct proc_mm* mm = vmspace(thread->process);
91 ptr_t vm_mnt = mm->vm_mnt;
93 // Ensure we have mounted
96 pte_t* ptep = mkptep_va(vm_mnt, thread->kstack);
97 leaflet = pte_leaflet(*ptep);
100 set_pte(ptep, null_pte);
101 ptep_unmap_leaflet(ptep + 1, leaflet);
103 leaflet_return(leaflet);
105 if (thread->ustack) {
106 if ((thread->ustack->start & 0xfff)) {
107 fail("invalid ustack struct");
109 mem_unmap_region(vm_mnt, thread->ustack);
114 create_thread(struct proc_info* proc, bool with_ustack)
116 struct proc_mm* mm = vmspace(proc);
119 ptr_t vm_mnt = mm->vm_mnt;
120 struct mm_region* ustack_region = NULL;
122 !(__alloc_user_thread_stack(proc, &ustack_region, vm_mnt)))
127 ptr_t kstack = __alloc_kernel_thread_stack(proc, vm_mnt);
129 mem_unmap_region(vm_mnt, ustack_region);
133 struct thread* th = alloc_thread(proc);
139 th->ustack = ustack_region;
142 th->ustack_top = align_stack(ustack_region->end - 1);
149 start_thread(struct thread* th, ptr_t entry)
152 struct proc_mm* mm = vmspace(th->process);
156 struct hart_transition transition;
157 if (!kernel_addr(entry)) {
160 hart_user_transfer(&transition, th->kstack, th->ustack_top, entry);
163 hart_kernel_transfer(&transition, th->kstack, entry);
166 install_hart_transition(mm->vm_mnt, &transition);
167 th->hstate = (struct hart_state*)transition.inject;
173 exit_thread(void* val) {
174 terminate_current_thread((ptr_t)val);
179 thread_find(struct proc_info* proc, tid_t tid)
181 struct thread *pos, *n;
182 llist_for_each(pos, n, &proc->threads, proc_sibs) {
183 if (pos->tid == tid) {
192 thread_stats_update(bool inbound, bool voluntary)
194 struct thread_stats* stats;
197 now = clock_systime();
198 stats = ¤t_thread->stats;
200 stats->at_user = !kernel_context(current_thread->hstate);
203 if (kernel_process(current_thread->process) ||
206 // exiting to user or kernel (kernel thread only), how graceful
207 stats->last_leave = now;
210 // exiting to kernel, effectively reentry
211 stats->last_reentry = now;
214 stats->last_resume = now;
218 stats->last_reentry = now;
222 // entering from kernel, it is a kernel preempt
223 thread_stats_update_kpreempt();
227 // entering from user space, a clean entrance.
230 stats->entry_count_invol++;
233 stats->entry_count_vol++;
236 thread_stats_reset_kpreempt();
237 stats->last_entry = now;
240 __DEFINE_LXSYSCALL3(int, th_create, tid_t*, tid,
241 struct uthread_param*, thparam, void*, entry)
245 struct thread* th = create_thread(__current, true);
252 ustack_top = th->ustack_top;
253 ustack_top = align_stack(ustack_top - sizeof(*thparam));
255 memcpy((void*)ustack_top, thparam, sizeof(*thparam));
257 th->ustack_top = ustack_top;
258 start_thread(th, (ptr_t)entry);
267 __DEFINE_LXSYSCALL(tid_t, th_self)
269 return current_thread->tid;
272 __DEFINE_LXSYSCALL1(void, th_exit, void*, val)
277 __DEFINE_LXSYSCALL2(int, th_join, tid_t, tid, void**, val_ptr)
279 struct thread* th = thread_find(__current, tid);
284 if (th == current_thread) {
288 while (!proc_terminated(th)) {
293 *val_ptr = (void*)th->exit_val;
302 __DEFINE_LXSYSCALL1(int, th_detach, tid_t, tid)
304 // can not detach the only thread
305 if (__current->thread_count == 1) {
309 struct thread* th = thread_find(__current, tid);
318 __DEFINE_LXSYSCALL2(int, th_kill, tid_t, tid, int, signum)
320 struct thread* target = thread_find(__current, tid);
325 if (signum > _SIG_NUM) {
330 thread_setsignal(target, signum);