1 #include <arch/x86/interrupts.h>
2 #include <arch/x86/tss.h>
7 #include <lunaix/fs/taskfs.h>
8 #include <lunaix/mm/cake.h>
9 #include <lunaix/mm/kalloc.h>
10 #include <lunaix/mm/mmap.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/valloc.h>
13 #include <lunaix/mm/vmm.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>
22 volatile struct proc_info* __current;
24 static struct proc_info dummy_proc;
26 struct proc_info dummy;
28 struct scheduler sched_ctx;
30 struct cake_pile* proc_pile;
40 proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
41 cake_set_constructor(proc_pile, cake_ctor_zeroing);
43 sched_ctx = (struct scheduler){ ._procs = vzalloc(PROC_TABLE_SIZE),
47 // TODO initialize dummy_proc
51 #define DUMMY_STACK_SIZE 2048
56 // This surely need to be simplified or encapsulated!
57 // It is a living nightmare!
59 extern void my_dummy();
60 static char dummy_stack[DUMMY_STACK_SIZE] __attribute__((aligned(16)));
63 dummy_proc = (struct proc_info){};
64 dummy_proc.intr_ctx = (isr_param){
65 .registers = { .ds = KDATA_SEG,
69 .esp = (void*)dummy_stack + DUMMY_STACK_SIZE - 20 },
71 .eip = (void*)my_dummy,
73 .eflags = cpu_reflags() | 0x0200
76 *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 4]) = dummy_proc.intr_ctx.eflags;
77 *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 8]) = KCODE_SEG;
78 *(u32_t*)(&dummy_stack[DUMMY_STACK_SIZE - 12]) = dummy_proc.intr_ctx.eip;
80 dummy_proc.page_table = cpu_rcr3();
81 dummy_proc.state = PS_READY;
82 dummy_proc.parent = &dummy_proc;
83 dummy_proc.pid = KERNEL_PID;
85 __current = &dummy_proc;
89 run(struct proc_info* proc)
91 proc->state = PS_RUNNING;
94 将tss.esp0设置为上次调度前的esp值。
95 当处理信号时,上下文信息是不会恢复的,而是保存在用户栈中,然后直接跳转进位于用户空间的sig_wrapper进行
96 信号的处理。当用户自定义的信号处理函数返回时,sigreturn的系统调用才开始进行上下文的恢复(或者说是进行
98 由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
99 这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
101 tss_update_esp(proc->intr_ctx.registers.esp);
103 apic_done_servicing();
105 asm volatile("pushl %0\n"
106 "jmp switch_to\n" ::"r"(proc)
107 : "memory"); // kernel/asm/x86/interrupt.S
111 can_schedule(struct proc_info* proc)
113 if (__SIGTEST(proc->sig_pending, _SIGCONT)) {
114 __SIGCLEAR(proc->sig_pending, _SIGSTOP);
115 } else if (__SIGTEST(proc->sig_pending, _SIGSTOP)) {
116 // 如果进程受到SIGSTOP,则该进程不给予调度。
126 struct proc_info* leader = sched_ctx._procs[0];
127 struct proc_info *pos, *n;
128 time_t now = clock_systime();
129 llist_for_each(pos, n, &leader->sleep.sleepers, sleep.sleepers)
131 if (PROC_TERMINATED(pos->state)) {
135 time_t wtime = pos->sleep.wakeup_time;
136 time_t atime = pos->sleep.alarm_time;
138 if (wtime && now >= wtime) {
139 pos->sleep.wakeup_time = 0;
140 pos->state = PS_READY;
143 if (atime && now >= atime) {
144 pos->sleep.alarm_time = 0;
145 __SIGSET(pos->sig_pending, _SIGALRM);
148 if (!wtime && !atime) {
150 llist_delete(&pos->sleep.sleepers);
158 if (!sched_ctx.ptable_len) {
162 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
163 cpu_disable_interrupt();
164 struct proc_info* next;
165 int prev_ptr = sched_ctx.procs_index;
168 if (!(__current->state & ~PS_RUNNING)) {
169 __current->state = PS_READY;
174 // round-robin scheduler
177 ptr = (ptr + 1) % sched_ctx.ptable_len;
178 next = sched_ctx._procs[ptr];
179 } while (!next || (next->state != PS_READY && ptr != prev_ptr));
181 sched_ctx.procs_index = ptr;
183 if (next->state != PS_READY) {
184 // schedule the dummy process if we're out of choice
189 if (!can_schedule(next)) {
190 // 如果该进程不给予调度,则尝试重新选择
201 cpu_enable_interrupt();
202 cpu_int(LUNAIX_SCHED);
205 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
211 if (__current->sleep.wakeup_time) {
212 return (__current->sleep.wakeup_time - clock_systime()) / 1000U;
215 struct proc_info* root_proc = sched_ctx._procs[0];
216 __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
217 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
219 __current->intr_ctx.registers.eax = seconds;
225 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
227 time_t prev_ddl = __current->sleep.alarm_time;
228 time_t now = clock_systime();
230 __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0;
232 struct proc_info* root_proc = sched_ctx._procs[0];
233 if (llist_empty(&__current->sleep.sleepers)) {
234 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
237 return prev_ddl ? (prev_ddl - now) / 1000 : 0;
240 __DEFINE_LXSYSCALL1(void, exit, int, status)
242 terminate_proc(status);
246 __DEFINE_LXSYSCALL(void, yield)
252 _wait(pid_t wpid, int* status, int options);
254 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
256 return _wait(-1, status, 0);
259 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
261 return _wait(pid, status, options);
264 __DEFINE_LXSYSCALL(int, geterrno)
266 return __current->k_status;
270 _wait(pid_t wpid, int* status, int options)
272 pid_t cur = __current->pid;
273 int status_flags = 0;
274 struct proc_info *proc, *n;
275 if (llist_empty(&__current->children)) {
279 wpid = wpid ? wpid : -__current->pgid;
281 llist_for_each(proc, n, &__current->children, siblings)
283 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
284 if (proc->state == PS_TERMNAT && !options) {
285 status_flags |= PEXITTERM;
288 if (proc->state == PS_READY && (options & WUNTRACED)) {
289 status_flags |= PEXITSTOP;
294 if ((options & WNOHANG)) {
302 status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
304 *status = proc->exit_code | status_flags;
306 return destroy_process(proc->pid);
313 for (; i < sched_ctx.ptable_len && sched_ctx._procs[i]; i++)
316 if (i == MAX_PROCESS) {
317 panick("Panic in Ponyville shimmer!");
320 if (i == sched_ctx.ptable_len) {
321 sched_ctx.ptable_len++;
324 struct proc_info* proc = cake_grab(proc_pile);
326 proc->state = PS_CREATED;
328 proc->created = clock_systime();
329 proc->pgid = proc->pid;
330 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
332 vzalloc_dma(512); // FXSAVE需要十六位对齐地址,使用DMA块(128位对齐)
334 llist_init_head(&proc->mm.regions);
335 llist_init_head(&proc->tasks);
336 llist_init_head(&proc->children);
337 llist_init_head(&proc->grp_member);
338 llist_init_head(&proc->sleep.sleepers);
339 waitq_init(&proc->waitqueue);
341 sched_ctx._procs[i] = proc;
347 commit_process(struct proc_info* process)
349 assert(process == sched_ctx._procs[process->pid]);
351 if (process->state != PS_CREATED) {
352 __current->k_status = EINVAL;
356 // every process is the child of first process (pid=1)
357 if (!process->parent) {
358 process->parent = sched_ctx._procs[1];
361 llist_append(&process->parent->children, &process->siblings);
362 llist_append(&sched_ctx._procs[0]->tasks, &process->tasks);
364 process->state = PS_READY;
367 // from <kernel/process.c>
369 __del_pagetable(pid_t pid, uintptr_t mount_point);
372 destroy_process(pid_t pid)
375 if (index <= 0 || index > sched_ctx.ptable_len) {
376 __current->k_status = EINVAL;
379 struct proc_info* proc = sched_ctx._procs[index];
380 sched_ctx._procs[index] = 0;
382 llist_delete(&proc->siblings);
383 llist_delete(&proc->grp_member);
384 llist_delete(&proc->tasks);
385 llist_delete(&proc->sleep.sleepers);
387 taskfs_invalidate(pid);
390 vfs_unref_dnode(proc->cwd);
393 for (size_t i = 0; i < VFS_MAX_FD; i++) {
394 struct v_fd* fd = proc->fdtable->fds[i];
396 vfs_pclose(fd->file, pid);
401 vfree(proc->fdtable);
402 vfree_dma(proc->fxstate);
404 vmm_mount_pd(VMS_MOUNT_1, proc->page_table);
406 struct mm_region *pos, *n;
407 llist_for_each(pos, n, &proc->mm.regions, head)
409 mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0);
413 __del_pagetable(pid, VMS_MOUNT_1);
415 vmm_unmount_pd(VMS_MOUNT_1);
417 cake_release(proc_pile, proc);
423 terminate_proc(int exit_code)
425 __current->state = PS_TERMNAT;
426 __current->exit_code = exit_code;
428 __SIGSET(__current->parent->sig_pending, _SIGCHLD);
432 get_process(pid_t pid)
435 if (index < 0 || index > sched_ctx.ptable_len) {
438 return sched_ctx._procs[index];
442 orphaned_proc(pid_t pid)
446 if (pid >= sched_ctx.ptable_len)
448 struct proc_info* proc = sched_ctx._procs[pid];
449 struct proc_info* parent = proc->parent;
451 // 如果其父进程的状态是terminated 或 destroy中的一种
452 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
453 return PROC_TERMINATED(parent->state) || parent->created > proc->created;