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 extern void __proc_table;
26 struct scheduler sched_ctx;
33 size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
35 for (size_t i = 0; i <= pg_size; i += 4096) {
36 uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
38 PD_REFERENCED, &__proc_table + i, pa, PG_PREM_RW, VMAP_NULL);
41 sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)&__proc_table,
47 run(struct proc_info* proc)
49 if (!(__current->state & ~PROC_RUNNING)) {
50 __current->state = PROC_STOPPED;
52 proc->state = PROC_RUNNING;
55 // tss_update_esp(__current->intr_ctx.esp);
56 apic_done_servicing();
58 asm volatile("pushl %0\n"
59 "jmp switch_to\n" ::"r"(proc)); // kernel/asm/x86/interrupt.S
65 if (!sched_ctx.ptable_len) {
69 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
70 cpu_disable_interrupt();
71 struct proc_info* next;
72 int prev_ptr = sched_ctx.procs_index;
74 // round-robin scheduler
76 ptr = (ptr + 1) % sched_ctx.ptable_len;
77 next = &sched_ctx._procs[ptr];
78 } while (next->state != PROC_STOPPED && ptr != prev_ptr);
80 sched_ctx.procs_index = ptr;
86 proc_timer_callback(struct proc_info* proc)
89 proc->state = PROC_STOPPED;
92 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
94 // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
99 if (__current->timer) {
100 return __current->timer->counter / timer_context()->running_frequency;
103 struct lx_timer* timer =
104 timer_run_second(seconds, proc_timer_callback, __current, 0);
105 __current->timer = timer;
106 __current->intr_ctx.registers.eax = seconds;
107 __current->state = PROC_BLOCKED;
111 __DEFINE_LXSYSCALL1(void, exit, int, status)
113 terminate_proc(status);
116 __DEFINE_LXSYSCALL(void, yield)
122 _wait(pid_t wpid, int* status, int options);
124 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
126 return _wait(-1, status, 0);
129 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
131 return _wait(pid, status, options);
135 _wait(pid_t wpid, int* status, int options)
137 pid_t cur = __current->pid;
138 int status_flags = 0;
139 struct proc_info *proc, *n;
140 if (llist_empty(&__current->children)) {
144 wpid = wpid ? wpid : -__current->pgid;
145 cpu_enable_interrupt();
147 llist_for_each(proc, n, &__current->children, siblings)
149 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
150 if (proc->state == PROC_TERMNAT && !options) {
151 status_flags |= PROCTERM;
154 if (proc->state == PROC_STOPPED && (options & WUNTRACED)) {
155 status_flags |= PROCSTOP;
160 if ((options & WNOHANG)) {
168 cpu_disable_interrupt();
169 *status = (proc->exit_code & 0xffff) | status_flags;
170 return destroy_process(proc->pid);
178 i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY;
182 if (i == MAX_PROCESS) {
183 panick("Panic in Ponyville shimmer!");
189 push_process(struct proc_info* process)
191 int index = process->pid;
192 if (index < 0 || index > sched_ctx.ptable_len) {
193 __current->k_status = LXINVLDPID;
197 if (index == sched_ctx.ptable_len) {
198 sched_ctx.ptable_len++;
201 sched_ctx._procs[index] = *process;
203 process = &sched_ctx._procs[index];
205 // make sure the reference is relative to process table
206 llist_init_head(&process->children);
207 llist_init_head(&process->grp_member);
209 // every process is the child of first process (pid=1)
210 if (process->parent) {
211 llist_append(&process->parent->children, &process->siblings);
213 process->parent = &sched_ctx._procs[0];
216 process->state = PROC_STOPPED;
219 // from <kernel/process.c>
221 __del_pagetable(pid_t pid, uintptr_t mount_point);
224 destroy_process(pid_t pid)
227 if (index <= 0 || index > sched_ctx.ptable_len) {
228 __current->k_status = LXINVLDPID;
231 struct proc_info* proc = &sched_ctx._procs[index];
232 proc->state = PROC_DESTROY;
233 llist_delete(&proc->siblings);
235 if (proc->mm.regions) {
236 struct mm_region *pos, *n;
237 llist_for_each(pos, n, &proc->mm.regions->head, head)
243 vmm_mount_pd(PD_MOUNT_2, proc->page_table);
245 __del_pagetable(pid, PD_MOUNT_2);
247 vmm_unmount_pd(PD_MOUNT_2);
253 terminate_proc(int exit_code)
255 __current->state = PROC_TERMNAT;
256 __current->exit_code = exit_code;
262 get_process(pid_t pid)
265 if (index < 0 || index > sched_ctx.ptable_len) {
268 return &sched_ctx._procs[index];
272 orphaned_proc(pid_t pid)
276 if (pid >= sched_ctx.ptable_len)
278 struct proc_info* proc = &sched_ctx._procs[pid];
279 struct proc_info* parent = proc->parent;
281 // 如果其父进程的状态是terminated 或 destroy中的一种
282 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
283 return (parent->state & PROC_TERMMASK) || parent->created > proc->created;