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/vmm.h>
10 #include <lunaix/process.h>
11 #include <lunaix/sched.h>
12 #include <lunaix/signal.h>
13 #include <lunaix/spike.h>
14 #include <lunaix/status.h>
15 #include <lunaix/syscall.h>
16 #include <lunaix/syslog.h>
18 #define MAX_PROCESS 512
20 volatile struct proc_info* __current;
22 struct proc_info dummy;
24 struct scheduler sched_ctx;
31 size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
33 for (size_t i = 0; i <= pg_size; i += 4096) {
34 uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
36 PD_REFERENCED, PROC_START + i, pa, PG_PREM_RW, VMAP_NULL);
39 sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)PROC_START,
45 run(struct proc_info* proc)
47 if (!(__current->state & ~PROC_RUNNING)) {
48 __current->state = PROC_STOPPED;
50 proc->state = PROC_RUNNING;
53 // tss_update_esp(__current->intr_ctx.esp);
54 apic_done_servicing();
56 asm volatile("pushl %0\n"
57 "jmp switch_to\n" ::"r"(proc)); // kernel/asm/x86/interrupt.S
63 if (!sched_ctx.ptable_len) {
67 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
68 cpu_disable_interrupt();
69 struct proc_info* next;
70 int prev_ptr = sched_ctx.procs_index;
72 // round-robin scheduler
74 ptr = (ptr + 1) % sched_ctx.ptable_len;
75 next = &sched_ctx._procs[ptr];
76 } while (next->state != PROC_STOPPED && ptr != prev_ptr);
78 sched_ctx.procs_index = ptr;
84 proc_timer_callback(struct proc_info* proc)
87 proc->state = PROC_STOPPED;
90 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
92 // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
97 if (__current->timer) {
98 return __current->timer->counter / timer_context()->running_frequency;
101 struct lx_timer* timer =
102 timer_run_second(seconds, proc_timer_callback, __current, 0);
103 __current->timer = timer;
104 __current->intr_ctx.registers.eax = seconds;
105 __current->state = PROC_BLOCKED;
109 __DEFINE_LXSYSCALL1(void, exit, int, status)
111 terminate_proc(status);
114 __DEFINE_LXSYSCALL(void, yield)
120 _wait(pid_t wpid, int* status, int options);
122 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
124 return _wait(-1, status, 0);
127 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
129 return _wait(pid, status, options);
133 _wait(pid_t wpid, int* status, int options)
135 pid_t cur = __current->pid;
136 int status_flags = 0;
137 struct proc_info *proc, *n;
138 if (llist_empty(&__current->children)) {
142 wpid = wpid ? wpid : -__current->pgid;
143 cpu_enable_interrupt();
145 llist_for_each(proc, n, &__current->children, siblings)
147 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
148 if (proc->state == PROC_TERMNAT && !options) {
149 status_flags |= PROCTERM;
152 if (proc->state == PROC_STOPPED && (options & WUNTRACED)) {
153 status_flags |= PROCSTOP;
158 if ((options & WNOHANG)) {
166 cpu_disable_interrupt();
167 *status = (proc->exit_code & 0xffff) | status_flags;
168 return destroy_process(proc->pid);
176 i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY;
180 if (i == MAX_PROCESS) {
181 panick("Panic in Ponyville shimmer!");
187 push_process(struct proc_info* process)
189 int index = process->pid;
190 if (index < 0 || index > sched_ctx.ptable_len) {
191 __current->k_status = LXINVLDPID;
195 if (index == sched_ctx.ptable_len) {
196 sched_ctx.ptable_len++;
199 sched_ctx._procs[index] = *process;
201 process = &sched_ctx._procs[index];
203 // make sure the reference is relative to process table
204 llist_init_head(&process->children);
205 llist_init_head(&process->grp_member);
207 // every process is the child of first process (pid=1)
208 if (process->parent) {
209 llist_append(&process->parent->children, &process->siblings);
211 process->parent = &sched_ctx._procs[0];
214 process->state = PROC_STOPPED;
217 // from <kernel/process.c>
219 __del_pagetable(pid_t pid, uintptr_t mount_point);
222 destroy_process(pid_t pid)
225 if (index <= 0 || index > sched_ctx.ptable_len) {
226 __current->k_status = LXINVLDPID;
229 struct proc_info* proc = &sched_ctx._procs[index];
230 proc->state = PROC_DESTROY;
231 llist_delete(&proc->siblings);
233 if (proc->mm.regions) {
234 struct mm_region *pos, *n;
235 llist_for_each(pos, n, &proc->mm.regions->head, head)
241 vmm_mount_pd(PD_MOUNT_1, proc->page_table);
243 __del_pagetable(pid, PD_MOUNT_1);
245 vmm_unmount_pd(PD_MOUNT_1);
251 terminate_proc(int exit_code)
253 __current->state = PROC_TERMNAT;
254 __current->exit_code = exit_code;
260 get_process(pid_t pid)
263 if (index < 0 || index > sched_ctx.ptable_len) {
266 return &sched_ctx._procs[index];
270 orphaned_proc(pid_t pid)
274 if (pid >= sched_ctx.ptable_len)
276 struct proc_info* proc = &sched_ctx._procs[pid];
277 struct proc_info* parent = proc->parent;
279 // 如果其父进程的状态是terminated 或 destroy中的一种
280 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
281 return (parent->state & PROC_TERMMASK) || parent->created > proc->created;