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/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 *execp = (struct exec_param){
68 .eflags = cpu_reflags() | 0x0200,
69 .eip = (void*)my_dummy,
74 dummy_proc = (struct proc_info){};
75 dummy_proc.intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
81 dummy_proc.page_table = cpu_rcr3();
82 dummy_proc.state = PS_READY;
83 dummy_proc.parent = &dummy_proc;
84 dummy_proc.pid = KERNEL_PID;
86 __current = &dummy_proc;
90 run(struct proc_info* proc)
92 proc->state = PS_RUNNING;
95 将tss.esp0设置为上次调度前的esp值。
96 当处理信号时,上下文信息是不会恢复的,而是保存在用户栈中,然后直接跳转进位于用户空间的sig_wrapper进行
97 信号的处理。当用户自定义的信号处理函数返回时,sigreturn的系统调用才开始进行上下文的恢复(或者说是进行
99 由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
100 这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
102 tss_update_esp(proc->intr_ctx.esp);
104 apic_done_servicing();
106 asm volatile("pushl %0\n"
107 "jmp switch_to\n" ::"r"(proc)
108 : "memory"); // kernel/asm/x86/interrupt.S
112 can_schedule(struct proc_info* proc)
114 if (__SIGTEST(proc->sig_pending, _SIGCONT)) {
115 __SIGCLEAR(proc->sig_pending, _SIGSTOP);
116 } else if (__SIGTEST(proc->sig_pending, _SIGSTOP)) {
117 // 如果进程受到SIGSTOP,则该进程不给予调度。
127 struct proc_info* leader = sched_ctx._procs[0];
128 struct proc_info *pos, *n;
129 time_t now = clock_systime();
130 llist_for_each(pos, n, &leader->sleep.sleepers, sleep.sleepers)
132 if (PROC_TERMINATED(pos->state)) {
136 time_t wtime = pos->sleep.wakeup_time;
137 time_t atime = pos->sleep.alarm_time;
139 if (wtime && now >= wtime) {
140 pos->sleep.wakeup_time = 0;
141 pos->state = PS_READY;
144 if (atime && now >= atime) {
145 pos->sleep.alarm_time = 0;
146 __SIGSET(pos->sig_pending, _SIGALRM);
149 if (!wtime && !atime) {
151 llist_delete(&pos->sleep.sleepers);
159 if (!sched_ctx.ptable_len) {
163 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
164 cpu_disable_interrupt();
165 struct proc_info* next;
166 int prev_ptr = sched_ctx.procs_index;
169 if (!(__current->state & ~PS_RUNNING)) {
170 __current->state = PS_READY;
175 // round-robin scheduler
178 ptr = (ptr + 1) % sched_ctx.ptable_len;
179 next = sched_ctx._procs[ptr];
180 } while (!next || (next->state != PS_READY && ptr != prev_ptr));
182 sched_ctx.procs_index = ptr;
184 if (next->state != PS_READY) {
185 // schedule the dummy process if we're out of choice
190 if (!can_schedule(next)) {
191 // 如果该进程不给予调度,则尝试重新选择
202 cpu_enable_interrupt();
203 cpu_int(LUNAIX_SCHED);
206 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
212 if (__current->sleep.wakeup_time) {
213 return (__current->sleep.wakeup_time - clock_systime()) / 1000U;
216 struct proc_info* root_proc = sched_ctx._procs[0];
217 __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
218 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
220 __current->intr_ctx.registers.eax = seconds;
226 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
228 time_t prev_ddl = __current->sleep.alarm_time;
229 time_t now = clock_systime();
231 __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0;
233 struct proc_info* root_proc = sched_ctx._procs[0];
234 if (llist_empty(&__current->sleep.sleepers)) {
235 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
238 return prev_ddl ? (prev_ddl - now) / 1000 : 0;
241 __DEFINE_LXSYSCALL1(void, exit, int, status)
243 terminate_proc(status);
247 __DEFINE_LXSYSCALL(void, yield)
253 _wait(pid_t wpid, int* status, int options);
255 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
257 return _wait(-1, status, 0);
260 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
262 return _wait(pid, status, options);
265 __DEFINE_LXSYSCALL(int, geterrno)
267 return __current->k_status;
271 _wait(pid_t wpid, int* status, int options)
273 pid_t cur = __current->pid;
274 int status_flags = 0;
275 struct proc_info *proc, *n;
276 if (llist_empty(&__current->children)) {
280 wpid = wpid ? wpid : -__current->pgid;
282 llist_for_each(proc, n, &__current->children, siblings)
284 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
285 if (proc->state == PS_TERMNAT && !options) {
286 status_flags |= PEXITTERM;
289 if (proc->state == PS_READY && (options & WUNTRACED)) {
290 status_flags |= PEXITSTOP;
295 if ((options & WNOHANG)) {
303 status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
305 *status = proc->exit_code | status_flags;
307 return destroy_process(proc->pid);
314 for (; i < sched_ctx.ptable_len && sched_ctx._procs[i]; i++)
317 if (i == MAX_PROCESS) {
318 panick("Panic in Ponyville shimmer!");
321 if (i == sched_ctx.ptable_len) {
322 sched_ctx.ptable_len++;
325 struct proc_info* proc = cake_grab(proc_pile);
327 proc->state = PS_CREATED;
330 proc->created = clock_systime();
331 proc->pgid = proc->pid;
332 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
334 vzalloc_dma(512); // FXSAVE需要十六位对齐地址,使用DMA块(128位对齐)
336 llist_init_head(&proc->mm.regions);
337 llist_init_head(&proc->tasks);
338 llist_init_head(&proc->children);
339 llist_init_head(&proc->grp_member);
340 llist_init_head(&proc->sleep.sleepers);
341 waitq_init(&proc->waitqueue);
343 sched_ctx._procs[i] = proc;
349 commit_process(struct proc_info* process)
351 assert(process == sched_ctx._procs[process->pid]);
353 if (process->state != PS_CREATED) {
354 __current->k_status = EINVAL;
358 // every process is the child of first process (pid=1)
359 if (!process->parent) {
360 process->parent = sched_ctx._procs[1];
363 llist_append(&process->parent->children, &process->siblings);
364 llist_append(&sched_ctx._procs[0]->tasks, &process->tasks);
366 process->state = PS_READY;
369 // from <kernel/process.c>
371 __del_pagetable(pid_t pid, uintptr_t mount_point);
374 destroy_process(pid_t pid)
377 if (index <= 0 || index > sched_ctx.ptable_len) {
378 __current->k_status = EINVAL;
381 struct proc_info* proc = sched_ctx._procs[index];
382 sched_ctx._procs[index] = 0;
384 llist_delete(&proc->siblings);
385 llist_delete(&proc->grp_member);
386 llist_delete(&proc->tasks);
387 llist_delete(&proc->sleep.sleepers);
389 taskfs_invalidate(pid);
392 vfs_unref_dnode(proc->cwd);
395 for (size_t i = 0; i < VFS_MAX_FD; i++) {
396 struct v_fd* fd = proc->fdtable->fds[i];
398 vfs_pclose(fd->file, pid);
403 vfree(proc->fdtable);
404 vfree_dma(proc->fxstate);
406 vmm_mount_pd(VMS_MOUNT_1, proc->page_table);
408 struct mm_region *pos, *n;
409 llist_for_each(pos, n, &proc->mm.regions, head)
411 mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0);
415 __del_pagetable(pid, VMS_MOUNT_1);
417 vmm_unmount_pd(VMS_MOUNT_1);
419 cake_release(proc_pile, proc);
425 terminate_proc(int exit_code)
427 __current->state = PS_TERMNAT;
428 __current->exit_code = exit_code;
430 __SIGSET(__current->parent->sig_pending, _SIGCHLD);
434 get_process(pid_t pid)
437 if (index < 0 || index > sched_ctx.ptable_len) {
440 return sched_ctx._procs[index];
444 orphaned_proc(pid_t pid)
448 if (pid >= sched_ctx.ptable_len)
450 struct proc_info* proc = sched_ctx._procs[pid];
451 struct proc_info* parent = proc->parent;
453 // 如果其父进程的状态是terminated 或 destroy中的一种
454 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
455 return PROC_TERMINATED(parent->state) || parent->created > proc->created;