2 #include <arch/x86/interrupts.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/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>
21 #include <klibc/string.h>
23 volatile struct proc_info* __current;
25 static struct proc_info dummy_proc;
27 struct proc_info dummy;
29 struct scheduler sched_ctx;
31 struct cake_pile* proc_pile;
41 proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
42 cake_set_constructor(proc_pile, cake_ctor_zeroing);
44 sched_ctx = (struct scheduler){ ._procs = vzalloc(PROC_TABLE_SIZE),
48 // TODO initialize dummy_proc
52 #define DUMMY_STACK_SIZE 2048
57 // This surely need to be simplified or encapsulated!
58 // It is a living nightmare!
60 extern void my_dummy();
61 static char dummy_stack[DUMMY_STACK_SIZE] __attribute__((aligned(16)));
63 struct exec_param* execp =
64 (void*)dummy_stack + DUMMY_STACK_SIZE - sizeof(struct exec_param);
66 isr_param* isrp = (void*)execp - sizeof(isr_param);
68 *execp = (struct exec_param){
70 .eflags = cpu_reflags() | 0x0200,
71 .eip = (ptr_t)my_dummy,
75 *isrp = (isr_param){ .registers = { .ds = KDATA_SEG,
82 dummy_proc = (struct proc_info){};
83 dummy_proc.intr_ctx = isrp;
85 dummy_proc.page_table = cpu_rcr3();
86 dummy_proc.state = PS_READY;
87 dummy_proc.parent = &dummy_proc;
88 dummy_proc.pid = KERNEL_PID;
90 __current = &dummy_proc;
94 run(struct proc_info* proc)
96 proc->state = PS_RUNNING;
99 将tss.esp0设置为上次调度前的esp值。
100 当处理信号时,上下文信息是不会恢复的,而是保存在用户栈中,然后直接跳转进位于用户空间的sig_wrapper进行
101 信号的处理。当用户自定义的信号处理函数返回时,sigreturn的系统调用才开始进行上下文的恢复(或者说是进行
103 由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
104 这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
107 apic_done_servicing();
109 asm volatile("pushl %0\n"
110 "jmp switch_to\n" ::"r"(proc)
111 : "memory"); // kernel/asm/x86/interrupt.S
115 can_schedule(struct proc_info* proc)
121 struct sighail* sh = &proc->sigctx;
123 if ((proc->state & PS_PAUSED)) {
124 return !!(sh->sig_pending & ~1);
127 if (sigset_test(sh->sig_pending, _SIGCONT)) {
128 sigset_clear(sh->sig_pending, _SIGSTOP);
129 } else if (sigset_test(sh->sig_pending, _SIGSTOP)) {
130 // 如果进程受到SIGSTOP,则该进程不给予调度。
134 return (proc->state == PS_READY);
140 struct proc_info* leader = sched_ctx._procs[0];
141 struct proc_info *pos, *n;
142 time_t now = clock_systime();
143 llist_for_each(pos, n, &leader->sleep.sleepers, sleep.sleepers)
145 if (proc_terminated(pos)) {
149 time_t wtime = pos->sleep.wakeup_time;
150 time_t atime = pos->sleep.alarm_time;
152 if (wtime && now >= wtime) {
153 pos->sleep.wakeup_time = 0;
154 pos->state = PS_READY;
157 if (atime && now >= atime) {
158 pos->sleep.alarm_time = 0;
159 proc_setsignal(pos, _SIGALRM);
162 if (!wtime && !atime) {
164 llist_delete(&pos->sleep.sleepers);
172 if (!sched_ctx.ptable_len) {
176 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
177 cpu_disable_interrupt();
178 struct proc_info* next;
179 int prev_ptr = sched_ctx.procs_index;
183 if (!(__current->state & ~PS_RUNNING)) {
184 __current->state = PS_READY;
189 // round-robin scheduler
191 ptr = (ptr + 1) % sched_ctx.ptable_len;
192 next = sched_ctx._procs[ptr];
194 if (!(found = can_schedule(next))) {
195 if (ptr == prev_ptr) {
202 sched_ctx.procs_index = ptr;
211 cpu_enable_interrupt();
212 cpu_int(LUNAIX_SCHED);
215 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
221 if (__current->sleep.wakeup_time) {
222 return (__current->sleep.wakeup_time - clock_systime()) / 1000U;
225 struct proc_info* root_proc = sched_ctx._procs[0];
226 __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
228 if (llist_empty(&__current->sleep.sleepers)) {
229 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
232 store_retval(seconds);
240 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
242 time_t prev_ddl = __current->sleep.alarm_time;
243 time_t now = clock_systime();
245 __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0;
247 struct proc_info* root_proc = sched_ctx._procs[0];
248 if (llist_empty(&__current->sleep.sleepers)) {
249 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
252 return prev_ddl ? (prev_ddl - now) / 1000 : 0;
255 __DEFINE_LXSYSCALL1(void, exit, int, status)
257 terminate_proc(status);
261 __DEFINE_LXSYSCALL(void, yield)
267 _wait(pid_t wpid, int* status, int options);
269 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
271 return _wait(-1, status, 0);
274 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
276 return _wait(pid, status, options);
279 __DEFINE_LXSYSCALL(int, geterrno)
281 return __current->k_status;
285 _wait(pid_t wpid, int* status, int options)
287 pid_t cur = __current->pid;
288 int status_flags = 0;
289 struct proc_info *proc, *n;
290 if (llist_empty(&__current->children)) {
294 wpid = wpid ? wpid : -__current->pgid;
296 llist_for_each(proc, n, &__current->children, siblings)
298 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
299 if (proc->state == PS_TERMNAT && !options) {
300 status_flags |= PEXITTERM;
303 if (proc->state == PS_READY && (options & WUNTRACED)) {
304 status_flags |= PEXITSTOP;
309 if ((options & WNOHANG)) {
318 *status = proc->exit_code | status_flags;
320 return destroy_process(proc->pid);
327 for (; i < sched_ctx.ptable_len && sched_ctx._procs[i]; i++)
330 if (i == MAX_PROCESS) {
331 panick("Panic in Ponyville shimmer!");
334 if (i == sched_ctx.ptable_len) {
335 sched_ctx.ptable_len++;
338 struct proc_info* proc = cake_grab(proc_pile);
340 proc->state = PS_CREATED;
343 proc->created = clock_systime();
344 proc->pgid = proc->pid;
345 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
347 llist_init_head(&proc->mm.regions);
348 llist_init_head(&proc->tasks);
349 llist_init_head(&proc->children);
350 llist_init_head(&proc->grp_member);
351 llist_init_head(&proc->sleep.sleepers);
352 waitq_init(&proc->waitqueue);
354 sched_ctx._procs[i] = proc;
360 commit_process(struct proc_info* process)
362 assert(process == sched_ctx._procs[process->pid]);
364 if (process->state != PS_CREATED) {
365 __current->k_status = EINVAL;
369 // every process is the child of first process (pid=1)
370 if (!process->parent) {
371 process->parent = sched_ctx._procs[1];
374 llist_append(&process->parent->children, &process->siblings);
375 llist_append(&sched_ctx._procs[0]->tasks, &process->tasks);
377 process->state = PS_READY;
380 // from <kernel/process.c>
382 __del_pagetable(pid_t pid, ptr_t mount_point);
385 destroy_process(pid_t pid)
388 if (index <= 0 || index > sched_ctx.ptable_len) {
389 __current->k_status = EINVAL;
393 struct proc_info* proc = sched_ctx._procs[index];
394 sched_ctx._procs[index] = 0;
396 llist_delete(&proc->siblings);
397 llist_delete(&proc->grp_member);
398 llist_delete(&proc->tasks);
399 llist_delete(&proc->sleep.sleepers);
401 taskfs_invalidate(pid);
404 vfs_unref_dnode(proc->cwd);
407 for (size_t i = 0; i < VFS_MAX_FD; i++) {
408 struct v_fd* fd = proc->fdtable->fds[i];
410 vfs_pclose(fd->file, pid);
415 vfree(proc->fdtable);
417 vmm_mount_pd(VMS_MOUNT_1, proc->page_table);
419 struct mm_region *pos, *n;
420 llist_for_each(pos, n, &proc->mm.regions, head)
422 mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0);
426 __del_pagetable(pid, VMS_MOUNT_1);
428 vmm_unmount_pd(VMS_MOUNT_1);
430 cake_release(proc_pile, proc);
436 terminate_proc(int exit_code)
438 __current->state = PS_TERMNAT;
439 __current->exit_code = exit_code;
441 proc_setsignal(__current->parent, _SIGCHLD);
445 get_process(pid_t pid)
448 if (index < 0 || index > sched_ctx.ptable_len) {
451 return sched_ctx._procs[index];
455 orphaned_proc(pid_t pid)
459 if (pid >= sched_ctx.ptable_len)
461 struct proc_info* proc = sched_ctx._procs[pid];
462 struct proc_info* parent = proc->parent;
464 // 如果其父进程的状态是terminated 或 destroy中的一种
465 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
466 return proc_terminated(parent) || parent->created > proc->created;