1 #include <arch/x86/interrupts.h>
2 #include <arch/x86/tss.h>
7 #include <lunaix/mm/kalloc.h>
8 #include <lunaix/mm/pmm.h>
9 #include <lunaix/mm/valloc.h>
10 #include <lunaix/mm/vmm.h>
11 #include <lunaix/process.h>
12 #include <lunaix/sched.h>
13 #include <lunaix/signal.h>
14 #include <lunaix/spike.h>
15 #include <lunaix/status.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
19 #define MAX_PROCESS 512
21 volatile struct proc_info* __current;
23 struct proc_info dummy;
25 struct scheduler sched_ctx;
32 size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
34 for (size_t i = 0; i <= pg_size; i += 4096) {
35 uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
37 PD_REFERENCED, PROC_START + i, pa, PG_PREM_RW, VMAP_NULL);
40 sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)PROC_START,
46 run(struct proc_info* proc)
48 proc->state = PS_RUNNING;
51 将tss.esp0设置为上次调度前的esp值。
52 当处理信号时,上下文信息是不会恢复的,而是保存在用户栈中,然后直接跳转进位于用户空间的sig_wrapper进行
53 信号的处理。当用户自定义的信号处理函数返回时,sigreturn的系统调用才开始进行上下文的恢复(或者说是进行
55 由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
56 这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
58 tss_update_esp(proc->intr_ctx.registers.esp);
60 apic_done_servicing();
62 asm volatile("pushl %0\n"
63 "jmp switch_to\n" ::"r"(proc)
64 : "memory"); // kernel/asm/x86/interrupt.S
68 can_schedule(struct proc_info* proc)
70 if (__SIGTEST(proc->sig_pending, _SIGCONT)) {
71 __SIGCLEAR(proc->sig_pending, _SIGSTOP);
72 } else if (__SIGTEST(proc->sig_pending, _SIGSTOP)) {
73 // 如果进程受到SIGSTOP,则该进程不给予调度。
83 struct proc_info* leader = &sched_ctx._procs[0];
84 struct proc_info *pos, *n;
85 time_t now = clock_systime();
86 llist_for_each(pos, n, &leader->sleep.sleepers, sleep.sleepers)
88 if (PROC_TERMINATED(pos->state)) {
92 time_t wtime = pos->sleep.wakeup_time;
93 time_t atime = pos->sleep.alarm_time;
95 if (wtime && now >= wtime) {
96 pos->sleep.wakeup_time = 0;
97 pos->state = PS_READY;
100 if (atime && now >= atime) {
101 pos->sleep.alarm_time = 0;
102 __SIGSET(pos->sig_pending, _SIGALRM);
105 if (!wtime && !atime) {
107 llist_delete(&pos->sleep.sleepers);
115 if (!sched_ctx.ptable_len) {
119 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
120 cpu_disable_interrupt();
121 struct proc_info* next;
122 int prev_ptr = sched_ctx.procs_index;
125 if (!(__current->state & ~PS_RUNNING)) {
126 __current->state = PS_READY;
131 // round-robin scheduler
134 ptr = (ptr + 1) % sched_ctx.ptable_len;
135 next = &sched_ctx._procs[ptr];
136 } while (next->state != PS_READY && ptr != prev_ptr);
138 sched_ctx.procs_index = ptr;
140 if (!can_schedule(next)) {
141 // 如果该进程不给予调度,则尝试重新选择
151 cpu_enable_interrupt();
152 cpu_int(LUNAIX_SCHED);
155 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
161 if (__current->sleep.wakeup_time) {
162 return (__current->sleep.wakeup_time - clock_systime()) / 1000U;
165 __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
166 llist_append(&sched_ctx._procs[0].sleep.sleepers,
167 &__current->sleep.sleepers);
169 __current->intr_ctx.registers.eax = seconds;
170 __current->state = PS_BLOCKED;
174 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
176 time_t prev_ddl = __current->sleep.alarm_time;
177 time_t now = clock_systime();
179 __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0;
181 if (llist_empty(&__current->sleep.sleepers)) {
182 llist_append(&sched_ctx._procs[0].sleep.sleepers,
183 &__current->sleep.sleepers);
186 return prev_ddl ? (prev_ddl - now) / 1000 : 0;
189 __DEFINE_LXSYSCALL1(void, exit, int, status)
191 terminate_proc(status);
195 __DEFINE_LXSYSCALL(void, yield)
201 _wait(pid_t wpid, int* status, int options);
203 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
205 return _wait(-1, status, 0);
208 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
210 return _wait(pid, status, options);
213 __DEFINE_LXSYSCALL(int, geterrno)
215 return __current->k_status;
219 _wait(pid_t wpid, int* status, int options)
221 pid_t cur = __current->pid;
222 int status_flags = 0;
223 struct proc_info *proc, *n;
224 if (llist_empty(&__current->children)) {
228 wpid = wpid ? wpid : -__current->pgid;
230 llist_for_each(proc, n, &__current->children, siblings)
232 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
233 if (proc->state == PS_TERMNAT && !options) {
234 status_flags |= PEXITTERM;
237 if (proc->state == PS_READY && (options & WUNTRACED)) {
238 status_flags |= PEXITSTOP;
243 if ((options & WNOHANG)) {
251 status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
253 *status = proc->exit_code | status_flags;
255 return destroy_process(proc->pid);
262 for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PS_DESTROY;
266 if (i == MAX_PROCESS) {
267 panick("Panic in Ponyville shimmer!");
270 if (i == sched_ctx.ptable_len) {
271 sched_ctx.ptable_len++;
274 struct proc_info* proc = &sched_ctx._procs[i];
275 memset(proc, 0, sizeof(*proc));
277 proc->state = PS_CREATED;
279 proc->created = clock_systime();
280 proc->pgid = proc->pid;
281 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
283 llist_init_head(&proc->mm.regions.head);
284 llist_init_head(&proc->children);
285 llist_init_head(&proc->grp_member);
286 llist_init_head(&proc->sleep.sleepers);
287 waitq_init(&proc->waitqueue);
293 commit_process(struct proc_info* process)
295 assert(process == &sched_ctx._procs[process->pid]);
297 if (process->state != PS_CREATED) {
298 __current->k_status = EINVAL;
302 // every process is the child of first process (pid=1)
303 if (!process->parent) {
304 process->parent = &sched_ctx._procs[1];
307 llist_append(&process->parent->children, &process->siblings);
309 process->state = PS_READY;
312 // from <kernel/process.c>
314 __del_pagetable(pid_t pid, uintptr_t mount_point);
317 destroy_process(pid_t pid)
320 if (index <= 0 || index > sched_ctx.ptable_len) {
321 __current->k_status = EINVAL;
324 struct proc_info* proc = &sched_ctx._procs[index];
325 proc->state = PS_DESTROY;
326 llist_delete(&proc->siblings);
328 for (size_t i = 0; i < VFS_MAX_FD; i++) {
329 struct v_fd* fd = proc->fdtable->fds[i];
334 vfree(proc->fdtable);
336 struct mm_region *pos, *n;
337 llist_for_each(pos, n, &proc->mm.regions.head, head)
342 vmm_mount_pd(PD_MOUNT_1, proc->page_table);
344 __del_pagetable(pid, PD_MOUNT_1);
346 vmm_unmount_pd(PD_MOUNT_1);
352 terminate_proc(int exit_code)
354 __current->state = PS_TERMNAT;
355 __current->exit_code = exit_code;
357 __SIGSET(__current->parent->sig_pending, _SIGCHLD);
361 get_process(pid_t pid)
364 if (index < 0 || index > sched_ctx.ptable_len) {
367 return &sched_ctx._procs[index];
371 orphaned_proc(pid_t pid)
375 if (pid >= sched_ctx.ptable_len)
377 struct proc_info* proc = &sched_ctx._procs[pid];
378 struct proc_info* parent = proc->parent;
380 // 如果其父进程的状态是terminated 或 destroy中的一种
381 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
382 return PROC_TERMINATED(parent->state) || parent->created > proc->created;