2 #include <sys/mm/mempart.h>
6 #include <lunaix/fs/taskfs.h>
7 #include <lunaix/mm/cake.h>
8 #include <lunaix/mm/mmap.h>
9 #include <lunaix/mm/pmm.h>
10 #include <lunaix/mm/valloc.h>
11 #include <lunaix/mm/vmm.h>
12 #include <lunaix/mm/procvm.h>
13 #include <lunaix/process.h>
14 #include <lunaix/sched.h>
15 #include <lunaix/signal.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/status.h>
18 #include <lunaix/syscall.h>
19 #include <lunaix/syslog.h>
20 #include <lunaix/hart_state.h>
21 #include <lunaix/kpreempt.h>
23 #include <lunaix/generic/isrm.h>
25 #include <klibc/string.h>
27 struct thread empty_thread_obj;
29 volatile struct proc_info* __current;
30 volatile struct thread* current_thread = &empty_thread_obj;
32 struct scheduler sched_ctx;
34 struct cake_pile *proc_pile ,*thread_pile;
36 #define root_process (sched_ctx.procs[1])
43 proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
44 thread_pile = cake_new_pile("thread", sizeof(struct thread), 1, 0);
45 cake_set_constructor(proc_pile, cake_ctor_zeroing);
46 cake_set_constructor(thread_pile, cake_ctor_zeroing);
48 sched_ctx = (struct scheduler){
49 .procs = vzalloc(PROC_TABLE_SIZE), .ptable_len = 0, .procs_index = 0};
51 llist_init_head(&sched_ctx.sleepers);
55 run(struct thread* thread)
57 thread->state = PS_RUNNING;
58 thread->process->state = PS_RUNNING;
59 thread->process->th_active = thread;
61 procvm_mount_self(vmspace(thread->process));
62 set_current_executing(thread);
65 fail("unexpected return from switching");
69 Currently, we do not allow self-destorying thread, doing
70 so will eliminate current kernel stack which is disaster.
71 A compromise solution is to perform a regular scan and
72 clean-up on these thread, in the preemptible kernel thread.
76 cleanup_detached_threads() {
77 ensure_preempt_caller();
79 // XXX may be a lock on sched_context will ben the most appropriate?
80 cpu_disable_interrupt();
83 struct thread *pos, *n;
84 llist_for_each(pos, n, sched_ctx.threads, sched_sibs) {
85 if (likely(!proc_terminated(pos) || !thread_detached(pos))) {
89 struct proc_mm* mm = vmspace(pos->process);
99 INFO("cleaned %d terminated detached thread(s)", i);
102 cpu_enable_interrupt();
106 can_schedule(struct thread* thread)
112 if (unlikely(kernel_process(thread->process))) {
113 // a kernel process is always runnable
114 return thread->state == PS_READY;
117 struct sigctx* sh = &thread->sigctx;
119 if ((thread->state & PS_PAUSED)) {
120 return !!(sh->sig_pending & ~1);
122 if ((thread->state & PS_BLOCKED)) {
123 return sigset_test(sh->sig_pending, _SIGINT);
126 if (sigset_test(sh->sig_pending, _SIGSTOP)) {
127 // If one thread is experiencing SIGSTOP, then we know
128 // all other threads are also SIGSTOP (as per POSIX-2008.1)
129 // In which case, the entire process is stopped.
130 thread->state = PS_STOPPED;
133 if (sigset_test(sh->sig_pending, _SIGCONT)) {
134 thread->state = PS_READY;
137 return (thread->state == PS_READY) \
138 && proc_runnable(thread->process);
144 struct thread *pos, *n;
145 time_t now = clock_systime() / 1000;
147 llist_for_each(pos, n, &sched_ctx.sleepers, sleep.sleepers)
149 if (proc_terminated(pos)) {
153 time_t wtime = pos->sleep.wakeup_time;
154 time_t atime = pos->sleep.alarm_time;
156 if (wtime && now >= wtime) {
157 pos->sleep.wakeup_time = 0;
158 pos->state = PS_READY;
161 if (atime && now >= atime) {
162 pos->sleep.alarm_time = 0;
163 thread_setsignal(pos, _SIGALRM);
166 if (!wtime && !atime) {
168 llist_delete(&pos->sleep.sleepers);
176 assert(sched_ctx.ptable_len && sched_ctx.ttable_len);
178 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
179 cpu_disable_interrupt();
181 if (!(current_thread->state & ~PS_RUNNING)) {
182 current_thread->state = PS_READY;
183 __current->state = PS_READY;
187 procvm_unmount_self(vmspace(__current));
190 // round-robin scheduler
192 struct thread* current = current_thread;
193 struct thread* to_check = current;
196 to_check = list_next(to_check, struct thread, sched_sibs);
198 if (can_schedule(to_check)) {
202 if (to_check == current) {
203 // FIXME do something less leathal here
204 fail("Ran out of threads!")
210 sched_ctx.procs_index = to_check->process->pid;
216 fail("unexpected return from scheduler");
222 cpu_enable_interrupt();
226 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
232 time_t systime = clock_systime() / 1000;
233 struct haybed* bed = ¤t_thread->sleep;
235 if (bed->wakeup_time) {
236 return (bed->wakeup_time - systime);
239 bed->wakeup_time = systime + seconds;
241 if (llist_empty(&bed->sleepers)) {
242 llist_append(&sched_ctx.sleepers, &bed->sleepers);
245 store_retval(seconds);
247 block_current_thread();
253 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
255 struct haybed* bed = ¤t_thread->sleep;
256 time_t prev_ddl = bed->alarm_time;
257 time_t now = clock_systime() / 1000;
259 bed->alarm_time = seconds ? now + seconds : 0;
261 if (llist_empty(&bed->sleepers)) {
262 llist_append(&sched_ctx.sleepers, &bed->sleepers);
265 return prev_ddl ? (prev_ddl - now) : 0;
268 __DEFINE_LXSYSCALL1(void, exit, int, status)
270 terminate_current(status);
274 __DEFINE_LXSYSCALL(void, yield)
280 _wait(pid_t wpid, int* status, int options);
282 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
284 return _wait(-1, status, 0);
287 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
289 return _wait(pid, status, options);
292 __DEFINE_LXSYSCALL(int, geterrno)
294 return current_thread->syscall_ret;
298 _wait(pid_t wpid, int* status, int options)
300 pid_t cur = __current->pid;
301 int status_flags = 0;
302 struct proc_info *proc, *n;
303 if (llist_empty(&__current->children)) {
307 wpid = wpid ? wpid : -__current->pgid;
309 llist_for_each(proc, n, &__current->children, siblings)
311 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
312 if (proc->state == PS_TERMNAT && !options) {
313 status_flags |= PEXITTERM;
316 if (proc->state == PS_READY && (options & WUNTRACED)) {
317 status_flags |= PEXITSTOP;
322 if ((options & WNOHANG)) {
331 *status = proc->exit_code | status_flags;
333 return destroy_process(proc->pid);
340 for (; i < sched_ctx.ptable_len && sched_ctx.procs[i]; i++)
343 if (unlikely(i == MAX_PROCESS)) {
344 panick("Panic in Ponyville shimmer!");
351 alloc_thread(struct proc_info* process) {
352 if (process->thread_count >= MAX_THREAD_PP) {
356 struct thread* th = cake_grab(thread_pile);
358 th->process = process;
359 th->created = clock_systime();
361 // FIXME we need a better tid allocation method!
362 th->tid = th->created;
363 th->tid = (th->created ^ ((ptr_t)th)) % MAX_THREAD_PP;
365 th->state = PS_CREATED;
367 llist_init_head(&th->sleep.sleepers);
368 llist_init_head(&th->sched_sibs);
369 llist_init_head(&th->proc_sibs);
370 waitq_init(&th->waitqueue);
378 pid_t i = get_free_pid();
380 if (i == sched_ctx.ptable_len) {
381 sched_ctx.ptable_len++;
384 struct proc_info* proc = cake_grab(proc_pile);
389 proc->state = PS_CREATED;
391 proc->created = clock_systime();
392 proc->pgid = proc->pid;
394 proc->sigreg = vzalloc(sizeof(struct sigregistry));
395 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
397 proc->mm = procvm_create(proc);
399 llist_init_head(&proc->tasks);
400 llist_init_head(&proc->children);
401 llist_init_head(&proc->grp_member);
402 llist_init_head(&proc->threads);
404 iopoll_init(&proc->pollctx);
406 sched_ctx.procs[i] = proc;
412 commit_thread(struct thread* thread) {
413 struct proc_info* process = thread->process;
415 assert(process && !proc_terminated(process));
417 llist_append(&process->threads, &thread->proc_sibs);
419 if (sched_ctx.threads) {
420 llist_append(sched_ctx.threads, &thread->sched_sibs);
422 sched_ctx.threads = &thread->sched_sibs;
425 sched_ctx.ttable_len++;
426 process->thread_count++;
427 thread->state = PS_READY;
431 commit_process(struct proc_info* process)
433 assert(process == sched_ctx.procs[process->pid]);
434 assert(process->state == PS_CREATED);
436 // every process is the child of first process (pid=1)
437 if (!process->parent) {
438 if (likely(!kernel_process(process))) {
439 process->parent = root_process;
441 process->parent = process;
444 assert(!proc_terminated(process->parent));
447 if (sched_ctx.proc_list) {
448 llist_append(sched_ctx.proc_list, &process->tasks);
450 sched_ctx.proc_list = &process->tasks;
453 llist_append(&process->parent->children, &process->siblings);
455 process->state = PS_READY;
459 destory_thread(struct thread* thread)
461 cake_ensure_valid(thread);
463 struct proc_info* proc = thread->process;
465 llist_delete(&thread->sched_sibs);
466 llist_delete(&thread->proc_sibs);
467 llist_delete(&thread->sleep.sleepers);
468 waitq_cancel_wait(&thread->waitqueue);
470 thread_release_mem(thread);
472 proc->thread_count--;
473 sched_ctx.ttable_len--;
475 cake_release(thread_pile, thread);
479 orphan_children(struct proc_info* proc)
481 struct proc_info *root;
482 struct proc_info *pos, *n;
486 llist_for_each(pos, n, &proc->children, siblings) {
488 llist_append(&root->children, &pos->siblings);
493 delete_process(struct proc_info* proc)
495 pid_t pid = proc->pid;
496 struct proc_mm* mm = vmspace(proc);
498 assert(pid); // long live the pid0 !!
500 sched_ctx.procs[pid] = NULL;
502 llist_delete(&proc->siblings);
503 llist_delete(&proc->grp_member);
504 llist_delete(&proc->tasks);
508 taskfs_invalidate(pid);
511 vfs_unref_dnode(proc->cwd);
518 for (size_t i = 0; i < VFS_MAX_FD; i++) {
519 struct v_fd* fd = proc->fdtable->fds[i];
521 vfs_pclose(fd->file, pid);
526 vfree(proc->fdtable);
528 signal_free_registry(proc->sigreg);
532 struct thread *pos, *n;
533 llist_for_each(pos, n, &proc->threads, proc_sibs) {
534 // terminate and destory all thread unconditionally
538 orphan_children(proc);
540 procvm_unmount_release(mm);
542 cake_release(proc_pile, proc);
546 destroy_process(pid_t pid)
549 if (index <= 0 || index > sched_ctx.ptable_len) {
550 syscall_result(EINVAL);
554 struct proc_info* proc = sched_ctx.procs[index];
555 delete_process(proc);
561 terminate_proc_only(struct proc_info* proc, int exit_code) {
562 proc->state = PS_TERMNAT;
563 proc->exit_code = exit_code;
565 proc_setsignal(proc->parent, _SIGCHLD);
569 terminate_thread(struct thread* thread, ptr_t val) {
570 thread->exit_val = val;
571 thread->state = PS_TERMNAT;
573 struct proc_info* proc = thread->process;
574 if (proc->thread_count == 1) {
575 terminate_proc_only(thread->process, 0);
580 terminate_current_thread(ptr_t val) {
581 terminate_thread(current_thread, val);
585 terminate_proccess(struct proc_info* proc, int exit_code) {
586 assert(!kernel_process(proc));
588 if (proc->pid == 1) {
589 panick("Attempt to kill init");
592 terminate_proc_only(proc, exit_code);
594 struct thread *pos, *n;
595 llist_for_each(pos, n, &__current->threads, proc_sibs) {
596 pos->state = PS_TERMNAT;
601 terminate_current(int exit_code)
603 terminate_proccess(__current, exit_code);
607 get_process(pid_t pid)
610 if (index < 0 || index > sched_ctx.ptable_len) {
613 return sched_ctx.procs[index];
617 orphaned_proc(pid_t pid)
621 if (pid >= sched_ctx.ptable_len)
623 struct proc_info* proc = sched_ctx.procs[pid];
624 struct proc_info* parent = proc->parent;
626 // 如果其父进程的状态是terminated 或 destroy中的一种
627 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
628 return proc_terminated(parent) || parent->created > proc->created;