2 #include <sys/interrupts.h>
3 #include <sys/mm/mempart.h>
8 #include <lunaix/fs/taskfs.h>
9 #include <lunaix/mm/cake.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 #include <klibc/string.h>
24 volatile struct proc_info* __current;
26 static struct proc_info dummy_proc;
28 struct proc_info dummy;
30 struct scheduler sched_ctx;
32 struct cake_pile* proc_pile;
42 proc_pile = cake_new_pile("proc", sizeof(struct proc_info), 1, 0);
43 cake_set_constructor(proc_pile, cake_ctor_zeroing);
45 sched_ctx = (struct scheduler){ ._procs = vzalloc(PROC_TABLE_SIZE),
49 // TODO initialize dummy_proc
53 #define DUMMY_STACK_SIZE 2048
58 // This surely need to be simplified or encapsulated!
59 // It is a living nightmare!
61 extern void my_dummy();
62 static char dummy_stack[DUMMY_STACK_SIZE] __attribute__((aligned(16)));
64 ptr_t stktop = (ptr_t)dummy_stack + DUMMY_STACK_SIZE;
66 dummy_proc = (struct proc_info){};
68 proc_init_transfer(&dummy_proc, stktop, (ptr_t)my_dummy, TRANSFER_IE);
70 dummy_proc.page_table = cpu_ldvmspace();
71 dummy_proc.state = PS_READY;
72 dummy_proc.parent = &dummy_proc;
73 dummy_proc.pid = KERNEL_PID;
75 __current = &dummy_proc;
79 run(struct proc_info* proc)
81 proc->state = PS_RUNNING;
88 can_schedule(struct proc_info* proc)
94 struct sighail* sh = &proc->sigctx;
96 if ((proc->state & PS_PAUSED)) {
97 return !!(sh->sig_pending & ~1);
100 if (sigset_test(sh->sig_pending, _SIGCONT)) {
101 sigset_clear(sh->sig_pending, _SIGSTOP);
102 } else if (sigset_test(sh->sig_pending, _SIGSTOP)) {
103 // 如果进程受到SIGSTOP,则该进程不给予调度。
107 return (proc->state == PS_READY);
113 struct proc_info* leader = sched_ctx._procs[0];
114 struct proc_info *pos, *n;
115 time_t now = clock_systime();
116 llist_for_each(pos, n, &leader->sleep.sleepers, sleep.sleepers)
118 if (proc_terminated(pos)) {
122 time_t wtime = pos->sleep.wakeup_time;
123 time_t atime = pos->sleep.alarm_time;
125 if (wtime && now >= wtime) {
126 pos->sleep.wakeup_time = 0;
127 pos->state = PS_READY;
130 if (atime && now >= atime) {
131 pos->sleep.alarm_time = 0;
132 proc_setsignal(pos, _SIGALRM);
135 if (!wtime && !atime) {
137 llist_delete(&pos->sleep.sleepers);
145 if (!sched_ctx.ptable_len) {
149 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
150 cpu_disable_interrupt();
151 struct proc_info* next;
152 int prev_ptr = sched_ctx.procs_index;
156 if (!(__current->state & ~PS_RUNNING)) {
157 __current->state = PS_READY;
162 // round-robin scheduler
164 ptr = (ptr + 1) % sched_ctx.ptable_len;
165 next = sched_ctx._procs[ptr];
167 if (!(found = can_schedule(next))) {
168 if (ptr == prev_ptr) {
175 sched_ctx.procs_index = ptr;
184 cpu_enable_interrupt();
188 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
194 if (__current->sleep.wakeup_time) {
195 return (__current->sleep.wakeup_time - clock_systime()) / 1000U;
198 struct proc_info* root_proc = sched_ctx._procs[0];
199 __current->sleep.wakeup_time = clock_systime() + seconds * 1000;
201 if (llist_empty(&__current->sleep.sleepers)) {
202 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
205 store_retval(seconds);
213 // FIXME issue with alarm, paused parent process never got wake up, check what
214 // has been fucked up by refactoring.
216 __DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
218 time_t prev_ddl = __current->sleep.alarm_time;
219 time_t now = clock_systime();
221 __current->sleep.alarm_time = seconds ? now + seconds * 1000 : 0;
223 struct proc_info* root_proc = sched_ctx._procs[0];
224 if (llist_empty(&__current->sleep.sleepers)) {
225 llist_append(&root_proc->sleep.sleepers, &__current->sleep.sleepers);
228 return prev_ddl ? (prev_ddl - now) / 1000 : 0;
231 __DEFINE_LXSYSCALL1(void, exit, int, status)
233 terminate_proc(status);
237 __DEFINE_LXSYSCALL(void, yield)
243 _wait(pid_t wpid, int* status, int options);
245 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
247 return _wait(-1, status, 0);
250 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
252 return _wait(pid, status, options);
255 __DEFINE_LXSYSCALL(int, geterrno)
257 return __current->k_status;
261 _wait(pid_t wpid, int* status, int options)
263 pid_t cur = __current->pid;
264 int status_flags = 0;
265 struct proc_info *proc, *n;
266 if (llist_empty(&__current->children)) {
270 wpid = wpid ? wpid : -__current->pgid;
272 llist_for_each(proc, n, &__current->children, siblings)
274 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
275 if (proc->state == PS_TERMNAT && !options) {
276 status_flags |= PEXITTERM;
279 if (proc->state == PS_READY && (options & WUNTRACED)) {
280 status_flags |= PEXITSTOP;
285 if ((options & WNOHANG)) {
294 *status = proc->exit_code | status_flags;
296 return destroy_process(proc->pid);
303 for (; i < sched_ctx.ptable_len && sched_ctx._procs[i]; i++)
306 if (i == MAX_PROCESS) {
307 panick("Panic in Ponyville shimmer!");
310 if (i == sched_ctx.ptable_len) {
311 sched_ctx.ptable_len++;
314 struct proc_info* proc = cake_grab(proc_pile);
316 proc->state = PS_CREATED;
319 proc->created = clock_systime();
320 proc->pgid = proc->pid;
321 proc->fdtable = vzalloc(sizeof(struct v_fdtable));
323 llist_init_head(&proc->mm.regions);
324 llist_init_head(&proc->tasks);
325 llist_init_head(&proc->children);
326 llist_init_head(&proc->grp_member);
327 llist_init_head(&proc->sleep.sleepers);
328 waitq_init(&proc->waitqueue);
330 sched_ctx._procs[i] = proc;
336 commit_process(struct proc_info* process)
338 assert(process == sched_ctx._procs[process->pid]);
340 if (process->state != PS_CREATED) {
341 __current->k_status = EINVAL;
345 // every process is the child of first process (pid=1)
346 if (!process->parent) {
347 process->parent = sched_ctx._procs[1];
350 llist_append(&process->parent->children, &process->siblings);
351 llist_append(&sched_ctx._procs[0]->tasks, &process->tasks);
353 process->state = PS_READY;
356 // from <kernel/process.c>
358 __del_pagetable(pid_t pid, ptr_t mount_point);
361 destroy_process(pid_t pid)
364 if (index <= 0 || index > sched_ctx.ptable_len) {
365 __current->k_status = EINVAL;
369 struct proc_info* proc = sched_ctx._procs[index];
370 sched_ctx._procs[index] = 0;
372 llist_delete(&proc->siblings);
373 llist_delete(&proc->grp_member);
374 llist_delete(&proc->tasks);
375 llist_delete(&proc->sleep.sleepers);
377 taskfs_invalidate(pid);
380 vfs_unref_dnode(proc->cwd);
383 for (size_t i = 0; i < VFS_MAX_FD; i++) {
384 struct v_fd* fd = proc->fdtable->fds[i];
386 vfs_pclose(fd->file, pid);
391 vfree(proc->fdtable);
393 vmm_mount_pd(VMS_MOUNT_1, proc->page_table);
395 struct mm_region *pos, *n;
396 llist_for_each(pos, n, &proc->mm.regions, head)
398 mem_sync_pages(VMS_MOUNT_1, pos, pos->start, pos->end - pos->start, 0);
402 __del_pagetable(pid, VMS_MOUNT_1);
404 vmm_unmount_pd(VMS_MOUNT_1);
406 cake_release(proc_pile, proc);
412 terminate_proc(int exit_code)
414 __current->state = PS_TERMNAT;
415 __current->exit_code = exit_code;
417 proc_setsignal(__current->parent, _SIGCHLD);
421 get_process(pid_t pid)
424 if (index < 0 || index > sched_ctx.ptable_len) {
427 return sched_ctx._procs[index];
431 orphaned_proc(pid_t pid)
435 if (pid >= sched_ctx.ptable_len)
437 struct proc_info* proc = sched_ctx._procs[pid];
438 struct proc_info* parent = proc->parent;
440 // 如果其父进程的状态是terminated 或 destroy中的一种
441 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
442 return proc_terminated(parent) || parent->created > proc->created;