2 #include <sys/mm/mempart.h>
7 #include <lunaix/fs/taskfs.h>
8 #include <lunaix/mm/cake.h>
9 #include <lunaix/mm/mmap.h>
10 #include <lunaix/mm/pmm.h>
11 #include <lunaix/mm/valloc.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/mm/procvm.h>
14 #include <lunaix/process.h>
15 #include <lunaix/sched.h>
16 #include <lunaix/signal.h>
17 #include <lunaix/spike.h>
18 #include <lunaix/status.h>
19 #include <lunaix/syscall.h>
20 #include <lunaix/syslog.h>
21 #include <lunaix/pcontext.h>
22 #include <lunaix/kpreempt.h>
24 #include <klibc/string.h>
26 struct thread empty_thread_obj;
28 volatile struct proc_info* __current;
29 volatile struct thread* current_thread = &empty_thread_obj;
31 struct scheduler sched_ctx;
33 struct cake_pile *proc_pile ,*thread_pile;
40 proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
41 thread_pile = cake_new_pile("thread", sizeof(struct thread), 1, 0);
42 cake_set_constructor(proc_pile, cake_ctor_zeroing);
43 cake_set_constructor(thread_pile, cake_ctor_zeroing);
45 sched_ctx = (struct scheduler){
46 .procs = vzalloc(PROC_TABLE_SIZE), .ptable_len = 0, .procs_index = 0};
48 llist_init_head(&sched_ctx.sleepers);
52 run(struct thread* thread)
54 thread->state = PS_RUNNING;
55 thread->process->state = PS_RUNNING;
56 thread->process->th_active = thread;
58 procvm_mount_self(vmspace(thread->process));
59 set_current_executing(thread);
62 fail("unexpected return from switching");
66 Currently, we do not allow self-destorying thread, doing
67 so will eliminate current kernel stack which is disaster.
68 A compromise solution is to perform a regular scan and
69 clean-up on these thread, in the preemptible kernel thread.
73 cleanup_detached_threads() {
74 ensure_preempt_caller();
76 // XXX may be a lock on sched_context will ben the most appropriate?
77 cpu_disable_interrupt();
80 struct thread *pos, *n;
81 llist_for_each(pos, n, sched_ctx.threads, sched_sibs) {
82 if (likely(!proc_terminated(pos) || !thread_detached(pos))) {
86 struct proc_mm* mm = vmspace(pos->process);
96 INFO("cleaned %d terminated detached thread(s)", i);
99 cpu_enable_interrupt();
103 can_schedule(struct thread* thread)
109 if (unlikely(kernel_process(thread->process))) {
110 // a kernel process is always runnable
111 return thread->state == PS_READY;
114 struct sigctx* sh = &thread->sigctx;
116 if ((thread->state & PS_PAUSED)) {
117 return !!(sh->sig_pending & ~1);
119 if ((thread->state & PS_BLOCKED)) {
120 return sigset_test(sh->sig_pending, _SIGINT);
123 if (sigset_test(sh->sig_pending, _SIGSTOP)) {
124 // If one thread is experiencing SIGSTOP, then we know
125 // all other threads are also SIGSTOP (as per POSIX-2008.1)
126 // In which case, the entire process is stopped.
127 thread->state = PS_STOPPED;
130 if (sigset_test(sh->sig_pending, _SIGCONT)) {
131 thread->state = PS_READY;
134 return (thread->state == PS_READY) \
135 && proc_runnable(thread->process);
141 struct thread *pos, *n;
142 time_t now = clock_systime() / 1000;
144 llist_for_each(pos, n, &sched_ctx.sleepers, sleep.sleepers)
146 if (proc_terminated(pos)) {
150 time_t wtime = pos->sleep.wakeup_time;
151 time_t atime = pos->sleep.alarm_time;
153 if (wtime && now >= wtime) {
154 pos->sleep.wakeup_time = 0;
155 pos->state = PS_READY;
158 if (atime && now >= atime) {
159 pos->sleep.alarm_time = 0;
160 thread_setsignal(pos, _SIGALRM);
163 if (!wtime && !atime) {
165 llist_delete(&pos->sleep.sleepers);
173 assert(sched_ctx.ptable_len && sched_ctx.ttable_len);
175 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
176 cpu_disable_interrupt();
178 if (!(current_thread->state & ~PS_RUNNING)) {
179 current_thread->state = PS_READY;
180 __current->state = PS_READY;
184 procvm_unmount_self(vmspace(__current));
187 // round-robin scheduler
189 struct thread* current = current_thread;
190 struct thread* to_check = current;
193 to_check = list_next(to_check, struct thread, sched_sibs);
195 if (can_schedule(to_check)) {
199 if (to_check == current) {
200 // FIXME do something less leathal here
201 fail("Ran out of threads!")
207 sched_ctx.procs_index = to_check->process->pid;
213 fail("unexpected return from scheduler");
219 cpu_enable_interrupt();
223 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
229 time_t systime = clock_systime() / 1000;
230 struct haybed* bed = ¤t_thread->sleep;
232 if (bed->wakeup_time) {
233 return (bed->wakeup_time - systime);
236 bed->wakeup_time = systime + seconds;
238 if (llist_empty(&bed->sleepers)) {
239 llist_append(&sched_ctx.sleepers, &bed->sleepers);
242 store_retval(seconds);
244 block_current_thread();
250 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
252 struct haybed* bed = ¤t_thread->sleep;
253 time_t prev_ddl = bed->alarm_time;
254 time_t now = clock_systime() / 1000;
256 bed->alarm_time = seconds ? now + seconds : 0;
258 struct proc_info* root_proc = sched_ctx.procs[0];
259 if (llist_empty(&bed->sleepers)) {
260 llist_append(&sched_ctx.sleepers, &bed->sleepers);
263 return prev_ddl ? (prev_ddl - now) : 0;
266 __DEFINE_LXSYSCALL1(void, exit, int, status)
268 terminate_current(status);
272 __DEFINE_LXSYSCALL(void, yield)
278 _wait(pid_t wpid, int* status, int options);
280 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
282 return _wait(-1, status, 0);
285 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
287 return _wait(pid, status, options);
290 __DEFINE_LXSYSCALL(int, geterrno)
292 return current_thread->syscall_ret;
296 _wait(pid_t wpid, int* status, int options)
298 pid_t cur = __current->pid;
299 int status_flags = 0;
300 struct proc_info *proc, *n;
301 if (llist_empty(&__current->children)) {
305 wpid = wpid ? wpid : -__current->pgid;
307 llist_for_each(proc, n, &__current->children, siblings)
309 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
310 if (proc->state == PS_TERMNAT && !options) {
311 status_flags |= PEXITTERM;
314 if (proc->state == PS_READY && (options & WUNTRACED)) {
315 status_flags |= PEXITSTOP;
320 if ((options & WNOHANG)) {
329 *status = proc->exit_code | status_flags;
331 return destroy_process(proc->pid);
338 for (; i < sched_ctx.ptable_len && sched_ctx.procs[i]; i++)
341 if (unlikely(i == MAX_PROCESS)) {
342 panick("Panic in Ponyville shimmer!");
349 alloc_thread(struct proc_info* process) {
350 if (process->thread_count >= MAX_THREAD_PP) {
354 struct thread* th = cake_grab(thread_pile);
356 th->process = process;
357 th->created = clock_systime();
359 // FIXME we need a better tid allocation method!
360 th->tid = th->created;
361 th->tid = (th->created ^ ((ptr_t)th)) % MAX_THREAD_PP;
363 th->state = PS_CREATED;
365 llist_init_head(&th->sleep.sleepers);
366 llist_init_head(&th->sched_sibs);
367 llist_init_head(&th->proc_sibs);
368 waitq_init(&th->waitqueue);
376 pid_t i = get_free_pid();
378 if (i == sched_ctx.ptable_len) {
379 sched_ctx.ptable_len++;
382 struct proc_info* proc = cake_grab(proc_pile);
387 proc->state = PS_CREATED;
389 proc->created = clock_systime();
390 proc->pgid = proc->pid;
392 proc->sigreg = vzalloc(sizeof(struct sigregister));
393 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
395 proc->mm = procvm_create(proc);
397 llist_init_head(&proc->tasks);
398 llist_init_head(&proc->children);
399 llist_init_head(&proc->grp_member);
400 llist_init_head(&proc->threads);
402 iopoll_init(&proc->pollctx);
404 sched_ctx.procs[i] = proc;
410 commit_thread(struct thread* thread) {
411 struct proc_info* process = thread->process;
413 assert(process && !proc_terminated(process));
415 llist_append(&process->threads, &thread->proc_sibs);
417 if (sched_ctx.threads) {
418 llist_append(sched_ctx.threads, &thread->sched_sibs);
420 sched_ctx.threads = &thread->sched_sibs;
423 sched_ctx.ttable_len++;
424 process->thread_count++;
425 thread->state = PS_READY;
429 commit_process(struct proc_info* process)
431 assert(process == sched_ctx.procs[process->pid]);
432 assert(process->state == PS_CREATED);
434 // every process is the child of first process (pid=1)
435 if (!process->parent) {
436 if (likely(!kernel_process(process))) {
437 process->parent = sched_ctx.procs[1];
439 process->parent = process;
442 assert(!proc_terminated(process->parent));
445 if (sched_ctx.proc_list) {
446 llist_append(sched_ctx.proc_list, &process->tasks);
448 sched_ctx.proc_list = &process->tasks;
451 llist_append(&process->parent->children, &process->siblings);
453 process->state = PS_READY;
457 destory_thread(struct thread* thread)
459 cake_ensure_valid(thread);
461 struct proc_info* proc = thread->process;
463 llist_delete(&thread->sched_sibs);
464 llist_delete(&thread->proc_sibs);
465 llist_delete(&thread->sleep.sleepers);
466 waitq_cancel_wait(&thread->waitqueue);
468 thread_release_mem(thread);
470 proc->thread_count--;
471 sched_ctx.ttable_len--;
473 cake_release(thread_pile, thread);
477 delete_process(struct proc_info* proc)
479 pid_t pid = proc->pid;
480 struct proc_mm* mm = vmspace(proc);
482 assert(pid); // long live the pid0 !!
484 sched_ctx.procs[pid] = NULL;
486 llist_delete(&proc->siblings);
487 llist_delete(&proc->grp_member);
488 llist_delete(&proc->tasks);
492 taskfs_invalidate(pid);
495 vfs_unref_dnode(proc->cwd);
502 for (size_t i = 0; i < VFS_MAX_FD; i++) {
503 struct v_fd* fd = proc->fdtable->fds[i];
505 vfs_pclose(fd->file, pid);
510 vfree(proc->fdtable);
512 signal_free_registers(proc->sigreg);
516 struct thread *pos, *n;
517 llist_for_each(pos, n, &proc->threads, proc_sibs) {
518 // terminate and destory all thread unconditionally
522 procvm_unmount_release(mm);
524 cake_release(proc_pile, proc);
528 destroy_process(pid_t pid)
531 if (index <= 0 || index > sched_ctx.ptable_len) {
532 syscall_result(EINVAL);
536 struct proc_info* proc = sched_ctx.procs[index];
537 delete_process(proc);
543 terminate_proc_only(struct proc_info* proc, int exit_code) {
544 proc->state = PS_TERMNAT;
545 proc->exit_code = exit_code;
547 proc_setsignal(proc->parent, _SIGCHLD);
551 terminate_thread(struct thread* thread, ptr_t val) {
552 thread->exit_val = val;
553 thread->state = PS_TERMNAT;
555 struct proc_info* proc = thread->process;
556 if (proc->thread_count == 1) {
557 terminate_proc_only(thread->process, 0);
562 terminate_current_thread(ptr_t val) {
563 terminate_thread(current_thread, val);
567 terminate_proccess(struct proc_info* proc, int exit_code) {
568 assert(!kernel_process(proc));
570 if (proc->pid == 1) {
571 panick("Attempt to kill init");
574 terminate_proc_only(proc, exit_code);
576 struct thread *pos, *n;
577 llist_for_each(pos, n, &__current->threads, proc_sibs) {
578 pos->state = PS_TERMNAT;
583 terminate_current(int exit_code)
585 terminate_proccess(__current, exit_code);
589 get_process(pid_t pid)
592 if (index < 0 || index > sched_ctx.ptable_len) {
595 return sched_ctx.procs[index];
599 orphaned_proc(pid_t pid)
603 if (pid >= sched_ctx.ptable_len)
605 struct proc_info* proc = sched_ctx.procs[pid];
606 struct proc_info* parent = proc->parent;
608 // 如果其父进程的状态是terminated 或 destroy中的一种
609 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
610 return proc_terminated(parent) || parent->created > proc->created;