1 #include <arch/x86/interrupts.h>
2 #include <arch/x86/tss.h>
7 #include <lunaix/mm/kalloc.h>
8 #include <lunaix/mm/vmm.h>
9 #include <lunaix/process.h>
10 #include <lunaix/sched.h>
11 #include <lunaix/signal.h>
12 #include <lunaix/spike.h>
13 #include <lunaix/status.h>
14 #include <lunaix/syscall.h>
15 #include <lunaix/syslog.h>
17 #define MAX_PROCESS 512
19 volatile struct proc_info* __current;
21 struct proc_info dummy;
23 extern void __proc_table;
25 struct scheduler sched_ctx;
32 size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
33 assert_msg(vmm_alloc_pages(
34 KERNEL_PID, &__proc_table, pg_size, PG_PREM_RW, PP_FGPERSIST),
35 "Fail to allocate proc table");
37 sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)&__proc_table,
43 run(struct proc_info* proc)
45 if (!(__current->state & ~PROC_RUNNING)) {
46 __current->state = PROC_STOPPED;
48 proc->state = PROC_RUNNING;
51 // tss_update_esp(__current->intr_ctx.esp);
53 if (__current->page_table != proc->page_table) {
55 cpu_lcr3(__current->page_table);
56 // from now on, the we are in the kstack of another process
61 apic_done_servicing();
65 asm volatile("movl %0, %%eax\n"
66 "jmp soft_iret\n" ::"r"(&__current->intr_ctx)
73 if (!sched_ctx.ptable_len) {
77 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
78 cpu_disable_interrupt();
79 struct proc_info* next;
80 int prev_ptr = sched_ctx.procs_index;
82 // round-robin scheduler
84 ptr = (ptr + 1) % sched_ctx.ptable_len;
85 next = &sched_ctx._procs[ptr];
86 } while (next->state != PROC_STOPPED && ptr != prev_ptr);
88 sched_ctx.procs_index = ptr;
94 proc_timer_callback(struct proc_info* proc)
97 proc->state = PROC_STOPPED;
100 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
102 // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
107 if (__current->timer) {
108 return __current->timer->counter / timer_context()->running_frequency;
111 struct lx_timer* timer =
112 timer_run_second(seconds, proc_timer_callback, __current, 0);
113 __current->timer = timer;
114 __current->intr_ctx.registers.eax = seconds;
115 __current->state = PROC_BLOCKED;
119 __DEFINE_LXSYSCALL1(void, exit, int, status)
121 terminate_proc(status);
124 __DEFINE_LXSYSCALL(void, yield)
130 _wait(pid_t wpid, int* status, int options);
132 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
134 return _wait(-1, status, 0);
137 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
139 return _wait(pid, status, options);
143 _wait(pid_t wpid, int* status, int options)
145 pid_t cur = __current->pid;
146 int status_flags = 0;
147 struct proc_info *proc, *n;
148 if (llist_empty(&__current->children)) {
152 wpid = wpid ? wpid : -__current->pgid;
153 cpu_enable_interrupt();
155 llist_for_each(proc, n, &__current->children, siblings)
157 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
158 if (proc->state == PROC_TERMNAT && !options) {
159 status_flags |= PROCTERM;
162 if (proc->state == PROC_STOPPED && (options & WUNTRACED)) {
163 status_flags |= PROCSTOP;
168 if ((options & WNOHANG)) {
176 cpu_disable_interrupt();
177 *status = (proc->exit_code & 0xffff) | status_flags;
178 return destroy_process(proc->pid);
186 i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY;
190 if (i == MAX_PROCESS) {
191 panick("Panic in Ponyville shimmer!");
197 push_process(struct proc_info* process)
199 int index = process->pid;
200 if (index < 0 || index > sched_ctx.ptable_len) {
201 __current->k_status = LXINVLDPID;
205 if (index == sched_ctx.ptable_len) {
206 sched_ctx.ptable_len++;
209 sched_ctx._procs[index] = *process;
211 process = &sched_ctx._procs[index];
213 // make sure the reference is relative to process table
214 llist_init_head(&process->children);
215 llist_init_head(&process->grp_member);
217 // every process is the child of first process (pid=1)
218 if (process->parent) {
219 llist_append(&process->parent->children, &process->siblings);
221 process->parent = &sched_ctx._procs[0];
224 process->state = PROC_STOPPED;
227 // from <kernel/process.c>
229 __del_pagetable(pid_t pid, uintptr_t mount_point);
232 destroy_process(pid_t pid)
235 if (index <= 0 || index > sched_ctx.ptable_len) {
236 __current->k_status = LXINVLDPID;
239 struct proc_info* proc = &sched_ctx._procs[index];
240 proc->state = PROC_DESTROY;
241 llist_delete(&proc->siblings);
243 if (proc->mm.regions) {
244 struct mm_region *pos, *n;
245 llist_for_each(pos, n, &proc->mm.regions->head, head)
251 vmm_mount_pd(PD_MOUNT_2, proc->page_table);
253 __del_pagetable(pid, PD_MOUNT_2);
255 vmm_unmount_pd(PD_MOUNT_2);
261 terminate_proc(int exit_code)
263 __current->state = PROC_TERMNAT;
264 __current->exit_code = exit_code;
270 get_process(pid_t pid)
273 if (index < 0 || index > sched_ctx.ptable_len) {
276 return &sched_ctx._procs[index];
280 orphaned_proc(pid_t pid)
284 if (pid >= sched_ctx.ptable_len)
286 struct proc_info* proc = &sched_ctx._procs[pid];
287 struct proc_info* parent = proc->parent;
289 // 如果其父进程的状态是terminated 或 destroy中的一种
290 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
291 return (parent->state & PROC_TERMMASK) || parent->created > proc->created;