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);
52 apic_done_servicing();
54 asm volatile("pushl %0\n"
55 "jmp switch_to\n" ::"r"(proc)); // kernel/asm/x86/interrupt.S
61 if (!sched_ctx.ptable_len) {
65 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
66 cpu_disable_interrupt();
67 struct proc_info* next;
68 int prev_ptr = sched_ctx.procs_index;
70 // round-robin scheduler
72 ptr = (ptr + 1) % sched_ctx.ptable_len;
73 next = &sched_ctx._procs[ptr];
74 } while (next->state != PROC_STOPPED && ptr != prev_ptr);
76 sched_ctx.procs_index = ptr;
82 proc_timer_callback(struct proc_info* proc)
85 proc->state = PROC_STOPPED;
88 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
90 // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
95 if (__current->timer) {
96 return __current->timer->counter / timer_context()->running_frequency;
99 struct lx_timer* timer =
100 timer_run_second(seconds, proc_timer_callback, __current, 0);
101 __current->timer = timer;
102 __current->intr_ctx.registers.eax = seconds;
103 __current->state = PROC_BLOCKED;
107 __DEFINE_LXSYSCALL1(void, exit, int, status)
109 terminate_proc(status);
112 __DEFINE_LXSYSCALL(void, yield)
118 _wait(pid_t wpid, int* status, int options);
120 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
122 return _wait(-1, status, 0);
125 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
127 return _wait(pid, status, options);
131 _wait(pid_t wpid, int* status, int options)
133 pid_t cur = __current->pid;
134 int status_flags = 0;
135 struct proc_info *proc, *n;
136 if (llist_empty(&__current->children)) {
140 wpid = wpid ? wpid : -__current->pgid;
141 cpu_enable_interrupt();
143 llist_for_each(proc, n, &__current->children, siblings)
145 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
146 if (proc->state == PROC_TERMNAT && !options) {
147 status_flags |= PROCTERM;
150 if (proc->state == PROC_STOPPED && (options & WUNTRACED)) {
151 status_flags |= PROCSTOP;
156 if ((options & WNOHANG)) {
164 cpu_disable_interrupt();
165 *status = (proc->exit_code & 0xffff) | status_flags;
166 return destroy_process(proc->pid);
174 i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY;
178 if (i == MAX_PROCESS) {
179 panick("Panic in Ponyville shimmer!");
185 push_process(struct proc_info* process)
187 int index = process->pid;
188 if (index < 0 || index > sched_ctx.ptable_len) {
189 __current->k_status = LXINVLDPID;
193 if (index == sched_ctx.ptable_len) {
194 sched_ctx.ptable_len++;
197 sched_ctx._procs[index] = *process;
199 process = &sched_ctx._procs[index];
201 // make sure the reference is relative to process table
202 llist_init_head(&process->children);
203 llist_init_head(&process->grp_member);
205 // every process is the child of first process (pid=1)
206 if (process->parent) {
207 llist_append(&process->parent->children, &process->siblings);
209 process->parent = &sched_ctx._procs[0];
212 process->state = PROC_STOPPED;
215 // from <kernel/process.c>
217 __del_pagetable(pid_t pid, uintptr_t mount_point);
220 destroy_process(pid_t pid)
223 if (index <= 0 || index > sched_ctx.ptable_len) {
224 __current->k_status = LXINVLDPID;
227 struct proc_info* proc = &sched_ctx._procs[index];
228 proc->state = PROC_DESTROY;
229 llist_delete(&proc->siblings);
231 if (proc->mm.regions) {
232 struct mm_region *pos, *n;
233 llist_for_each(pos, n, &proc->mm.regions->head, head)
239 vmm_mount_pd(PD_MOUNT_2, proc->page_table);
241 __del_pagetable(pid, PD_MOUNT_2);
243 vmm_unmount_pd(PD_MOUNT_2);
249 terminate_proc(int exit_code)
251 __current->state = PROC_TERMNAT;
252 __current->exit_code = exit_code;
258 get_process(pid_t pid)
261 if (index < 0 || index > sched_ctx.ptable_len) {
264 return &sched_ctx._procs[index];
268 orphaned_proc(pid_t pid)
272 if (pid >= sched_ctx.ptable_len)
274 struct proc_info* proc = &sched_ctx._procs[pid];
275 struct proc_info* parent = proc->parent;
277 // 如果其父进程的状态是terminated 或 destroy中的一种
278 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
279 return (parent->state & PROC_TERMMASK) || parent->created > proc->created;