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;
41 proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
42 thread_pile = cake_new_pile("thread", sizeof(struct thread), 1, 0);
43 cake_set_constructor(proc_pile, cake_ctor_zeroing);
44 cake_set_constructor(thread_pile, cake_ctor_zeroing);
46 sched_ctx = (struct scheduler){
47 .procs = vzalloc(PROC_TABLE_SIZE), .ptable_len = 0, .procs_index = 0};
49 llist_init_head(&sched_ctx.sleepers);
53 run(struct thread* thread)
55 thread->state = PS_RUNNING;
56 thread->process->state = PS_RUNNING;
57 thread->process->th_active = thread;
59 procvm_mount_self(vmspace(thread->process));
60 set_current_executing(thread);
63 fail("unexpected return from switching");
67 Currently, we do not allow self-destorying thread, doing
68 so will eliminate current kernel stack which is disaster.
69 A compromise solution is to perform a regular scan and
70 clean-up on these thread, in the preemptible kernel thread.
74 cleanup_detached_threads() {
75 ensure_preempt_caller();
77 // XXX may be a lock on sched_context will ben the most appropriate?
78 cpu_disable_interrupt();
81 struct thread *pos, *n;
82 llist_for_each(pos, n, sched_ctx.threads, sched_sibs) {
83 if (likely(!proc_terminated(pos) || !thread_detached(pos))) {
87 struct proc_mm* mm = vmspace(pos->process);
97 INFO("cleaned %d terminated detached thread(s)", i);
100 cpu_enable_interrupt();
104 can_schedule(struct thread* thread)
110 if (unlikely(kernel_process(thread->process))) {
111 // a kernel process is always runnable
112 return thread->state == PS_READY;
115 struct sigctx* sh = &thread->sigctx;
117 if ((thread->state & PS_PAUSED)) {
118 return !!(sh->sig_pending & ~1);
120 if ((thread->state & PS_BLOCKED)) {
121 return sigset_test(sh->sig_pending, _SIGINT);
124 if (sigset_test(sh->sig_pending, _SIGSTOP)) {
125 // If one thread is experiencing SIGSTOP, then we know
126 // all other threads are also SIGSTOP (as per POSIX-2008.1)
127 // In which case, the entire process is stopped.
128 thread->state = PS_STOPPED;
131 if (sigset_test(sh->sig_pending, _SIGCONT)) {
132 thread->state = PS_READY;
135 return (thread->state == PS_READY) \
136 && proc_runnable(thread->process);
142 struct thread *pos, *n;
143 time_t now = clock_systime() / 1000;
145 llist_for_each(pos, n, &sched_ctx.sleepers, sleep.sleepers)
147 if (proc_terminated(pos)) {
151 time_t wtime = pos->sleep.wakeup_time;
152 time_t atime = pos->sleep.alarm_time;
154 if (wtime && now >= wtime) {
155 pos->sleep.wakeup_time = 0;
156 pos->state = PS_READY;
159 if (atime && now >= atime) {
160 pos->sleep.alarm_time = 0;
161 thread_setsignal(pos, _SIGALRM);
164 if (!wtime && !atime) {
166 llist_delete(&pos->sleep.sleepers);
174 assert(sched_ctx.ptable_len && sched_ctx.ttable_len);
176 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
177 cpu_disable_interrupt();
179 if (!(current_thread->state & ~PS_RUNNING)) {
180 current_thread->state = PS_READY;
181 __current->state = PS_READY;
185 procvm_unmount_self(vmspace(__current));
188 // round-robin scheduler
190 struct thread* current = current_thread;
191 struct thread* to_check = current;
194 to_check = list_next(to_check, struct thread, sched_sibs);
196 if (can_schedule(to_check)) {
200 if (to_check == current) {
201 // FIXME do something less leathal here
202 fail("Ran out of threads!")
208 sched_ctx.procs_index = to_check->process->pid;
214 fail("unexpected return from scheduler");
220 cpu_enable_interrupt();
224 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
230 time_t systime = clock_systime() / 1000;
231 struct haybed* bed = ¤t_thread->sleep;
233 if (bed->wakeup_time) {
234 return (bed->wakeup_time - systime);
237 bed->wakeup_time = systime + seconds;
239 if (llist_empty(&bed->sleepers)) {
240 llist_append(&sched_ctx.sleepers, &bed->sleepers);
243 store_retval(seconds);
245 block_current_thread();
251 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
253 struct haybed* bed = ¤t_thread->sleep;
254 time_t prev_ddl = bed->alarm_time;
255 time_t now = clock_systime() / 1000;
257 bed->alarm_time = seconds ? now + seconds : 0;
259 struct proc_info* root_proc = sched_ctx.procs[0];
260 if (llist_empty(&bed->sleepers)) {
261 llist_append(&sched_ctx.sleepers, &bed->sleepers);
264 return prev_ddl ? (prev_ddl - now) : 0;
267 __DEFINE_LXSYSCALL1(void, exit, int, status)
269 terminate_current(status);
273 __DEFINE_LXSYSCALL(void, yield)
279 _wait(pid_t wpid, int* status, int options);
281 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
283 return _wait(-1, status, 0);
286 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
288 return _wait(pid, status, options);
291 __DEFINE_LXSYSCALL(int, geterrno)
293 return current_thread->syscall_ret;
297 _wait(pid_t wpid, int* status, int options)
299 pid_t cur = __current->pid;
300 int status_flags = 0;
301 struct proc_info *proc, *n;
302 if (llist_empty(&__current->children)) {
306 wpid = wpid ? wpid : -__current->pgid;
308 llist_for_each(proc, n, &__current->children, siblings)
310 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
311 if (proc->state == PS_TERMNAT && !options) {
312 status_flags |= PEXITTERM;
315 if (proc->state == PS_READY && (options & WUNTRACED)) {
316 status_flags |= PEXITSTOP;
321 if ((options & WNOHANG)) {
330 *status = proc->exit_code | status_flags;
332 return destroy_process(proc->pid);
339 for (; i < sched_ctx.ptable_len && sched_ctx.procs[i]; i++)
342 if (unlikely(i == MAX_PROCESS)) {
343 panick("Panic in Ponyville shimmer!");
350 alloc_thread(struct proc_info* process) {
351 if (process->thread_count >= MAX_THREAD_PP) {
355 struct thread* th = cake_grab(thread_pile);
357 th->process = process;
358 th->created = clock_systime();
360 // FIXME we need a better tid allocation method!
361 th->tid = th->created;
362 th->tid = (th->created ^ ((ptr_t)th)) % MAX_THREAD_PP;
364 th->state = PS_CREATED;
366 llist_init_head(&th->sleep.sleepers);
367 llist_init_head(&th->sched_sibs);
368 llist_init_head(&th->proc_sibs);
369 waitq_init(&th->waitqueue);
377 pid_t i = get_free_pid();
379 if (i == sched_ctx.ptable_len) {
380 sched_ctx.ptable_len++;
383 struct proc_info* proc = cake_grab(proc_pile);
388 proc->state = PS_CREATED;
390 proc->created = clock_systime();
391 proc->pgid = proc->pid;
393 proc->sigreg = vzalloc(sizeof(struct sigregistry));
394 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
396 proc->mm = procvm_create(proc);
398 llist_init_head(&proc->tasks);
399 llist_init_head(&proc->children);
400 llist_init_head(&proc->grp_member);
401 llist_init_head(&proc->threads);
403 iopoll_init(&proc->pollctx);
405 sched_ctx.procs[i] = proc;
411 commit_thread(struct thread* thread) {
412 struct proc_info* process = thread->process;
414 assert(process && !proc_terminated(process));
416 llist_append(&process->threads, &thread->proc_sibs);
418 if (sched_ctx.threads) {
419 llist_append(sched_ctx.threads, &thread->sched_sibs);
421 sched_ctx.threads = &thread->sched_sibs;
424 sched_ctx.ttable_len++;
425 process->thread_count++;
426 thread->state = PS_READY;
430 commit_process(struct proc_info* process)
432 assert(process == sched_ctx.procs[process->pid]);
433 assert(process->state == PS_CREATED);
435 // every process is the child of first process (pid=1)
436 if (!process->parent) {
437 if (likely(!kernel_process(process))) {
438 process->parent = sched_ctx.procs[1];
440 process->parent = process;
443 assert(!proc_terminated(process->parent));
446 if (sched_ctx.proc_list) {
447 llist_append(sched_ctx.proc_list, &process->tasks);
449 sched_ctx.proc_list = &process->tasks;
452 llist_append(&process->parent->children, &process->siblings);
454 process->state = PS_READY;
458 destory_thread(struct thread* thread)
460 cake_ensure_valid(thread);
462 struct proc_info* proc = thread->process;
464 llist_delete(&thread->sched_sibs);
465 llist_delete(&thread->proc_sibs);
466 llist_delete(&thread->sleep.sleepers);
467 waitq_cancel_wait(&thread->waitqueue);
469 thread_release_mem(thread);
471 proc->thread_count--;
472 sched_ctx.ttable_len--;
474 cake_release(thread_pile, thread);
478 delete_process(struct proc_info* proc)
480 pid_t pid = proc->pid;
481 struct proc_mm* mm = vmspace(proc);
483 assert(pid); // long live the pid0 !!
485 sched_ctx.procs[pid] = NULL;
487 llist_delete(&proc->siblings);
488 llist_delete(&proc->grp_member);
489 llist_delete(&proc->tasks);
493 taskfs_invalidate(pid);
496 vfs_unref_dnode(proc->cwd);
503 for (size_t i = 0; i < VFS_MAX_FD; i++) {
504 struct v_fd* fd = proc->fdtable->fds[i];
506 vfs_pclose(fd->file, pid);
511 vfree(proc->fdtable);
513 signal_free_registry(proc->sigreg);
517 struct thread *pos, *n;
518 llist_for_each(pos, n, &proc->threads, proc_sibs) {
519 // terminate and destory all thread unconditionally
523 procvm_unmount_release(mm);
525 cake_release(proc_pile, proc);
529 destroy_process(pid_t pid)
532 if (index <= 0 || index > sched_ctx.ptable_len) {
533 syscall_result(EINVAL);
537 struct proc_info* proc = sched_ctx.procs[index];
538 delete_process(proc);
544 terminate_proc_only(struct proc_info* proc, int exit_code) {
545 proc->state = PS_TERMNAT;
546 proc->exit_code = exit_code;
548 proc_setsignal(proc->parent, _SIGCHLD);
552 terminate_thread(struct thread* thread, ptr_t val) {
553 thread->exit_val = val;
554 thread->state = PS_TERMNAT;
556 struct proc_info* proc = thread->process;
557 if (proc->thread_count == 1) {
558 terminate_proc_only(thread->process, 0);
563 terminate_current_thread(ptr_t val) {
564 terminate_thread(current_thread, val);
568 terminate_proccess(struct proc_info* proc, int exit_code) {
569 assert(!kernel_process(proc));
571 if (proc->pid == 1) {
572 panick("Attempt to kill init");
575 terminate_proc_only(proc, exit_code);
577 struct thread *pos, *n;
578 llist_for_each(pos, n, &__current->threads, proc_sibs) {
579 pos->state = PS_TERMNAT;
584 terminate_current(int exit_code)
586 terminate_proccess(__current, exit_code);
590 get_process(pid_t pid)
593 if (index < 0 || index > sched_ctx.ptable_len) {
596 return sched_ctx.procs[index];
600 orphaned_proc(pid_t pid)
604 if (pid >= sched_ctx.ptable_len)
606 struct proc_info* proc = sched_ctx.procs[pid];
607 struct proc_info* parent = proc->parent;
609 // 如果其父进程的状态是terminated 或 destroy中的一种
610 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
611 return proc_terminated(parent) || parent->created > proc->created;