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);
55 apic_done_servicing();
57 asm volatile("pushl %0\n"
58 "jmp switch_to\n" ::"r"(proc)); // kernel/asm/x86/interrupt.S
64 if (!sched_ctx.ptable_len) {
68 // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
69 cpu_disable_interrupt();
70 struct proc_info* next;
71 int prev_ptr = sched_ctx.procs_index;
73 // round-robin scheduler
75 ptr = (ptr + 1) % sched_ctx.ptable_len;
76 next = &sched_ctx._procs[ptr];
77 } while (next->state != PROC_STOPPED && ptr != prev_ptr);
79 sched_ctx.procs_index = ptr;
85 proc_timer_callback(struct proc_info* proc)
88 proc->state = PROC_STOPPED;
91 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
93 // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
98 if (__current->timer) {
99 return __current->timer->counter / timer_context()->running_frequency;
102 struct lx_timer* timer =
103 timer_run_second(seconds, proc_timer_callback, __current, 0);
104 __current->timer = timer;
105 __current->intr_ctx.registers.eax = seconds;
106 __current->state = PROC_BLOCKED;
110 __DEFINE_LXSYSCALL1(void, exit, int, status)
112 terminate_proc(status);
115 __DEFINE_LXSYSCALL(void, yield)
121 _wait(pid_t wpid, int* status, int options);
123 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
125 return _wait(-1, status, 0);
128 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
130 return _wait(pid, status, options);
134 _wait(pid_t wpid, int* status, int options)
136 pid_t cur = __current->pid;
137 int status_flags = 0;
138 struct proc_info *proc, *n;
139 if (llist_empty(&__current->children)) {
143 wpid = wpid ? wpid : -__current->pgid;
144 cpu_enable_interrupt();
146 llist_for_each(proc, n, &__current->children, siblings)
148 if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
149 if (proc->state == PROC_TERMNAT && !options) {
150 status_flags |= PROCTERM;
153 if (proc->state == PROC_STOPPED && (options & WUNTRACED)) {
154 status_flags |= PROCSTOP;
159 if ((options & WNOHANG)) {
167 cpu_disable_interrupt();
168 *status = (proc->exit_code & 0xffff) | status_flags;
169 return destroy_process(proc->pid);
177 i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY;
181 if (i == MAX_PROCESS) {
182 panick("Panic in Ponyville shimmer!");
185 if (i == sched_ctx.ptable_len) {
186 sched_ctx.ptable_len++;
189 struct proc_info* proc = &sched_ctx._procs[i];
190 memset(proc, 0, sizeof(*proc));
192 proc->state = PROC_CREATED;
194 proc->created = clock_systime();
195 proc->pgid = proc->pid;
197 llist_init_head(&proc->mm.regions);
198 llist_init_head(&proc->children);
199 llist_init_head(&proc->grp_member);
205 commit_process(struct proc_info* process)
207 assert(process == &sched_ctx._procs[process->pid]);
209 if (process->state != PROC_CREATED) {
210 __current->k_status = LXINVL;
214 // every process is the child of first process (pid=1)
215 if (process->parent) {
216 llist_append(&process->parent->children, &process->siblings);
218 process->parent = &sched_ctx._procs[0];
221 process->state = PROC_STOPPED;
224 // from <kernel/process.c>
226 __del_pagetable(pid_t pid, uintptr_t mount_point);
229 destroy_process(pid_t pid)
232 if (index <= 0 || index > sched_ctx.ptable_len) {
233 __current->k_status = LXINVLDPID;
236 struct proc_info* proc = &sched_ctx._procs[index];
237 proc->state = PROC_DESTROY;
238 llist_delete(&proc->siblings);
240 struct mm_region *pos, *n;
241 llist_for_each(pos, n, &proc->mm.regions.head, head)
246 vmm_mount_pd(PD_MOUNT_1, proc->page_table);
248 __del_pagetable(pid, PD_MOUNT_1);
250 vmm_unmount_pd(PD_MOUNT_1);
256 terminate_proc(int exit_code)
258 __current->state = PROC_TERMNAT;
259 __current->exit_code = exit_code;
265 get_process(pid_t pid)
268 if (index < 0 || index > sched_ctx.ptable_len) {
271 return &sched_ctx._procs[index];
275 orphaned_proc(pid_t pid)
279 if (pid >= sched_ctx.ptable_len)
281 struct proc_info* proc = &sched_ctx._procs[pid];
282 struct proc_info* parent = proc->parent;
284 // 如果其父进程的状态是terminated 或 destroy中的一种
285 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
286 return (parent->state & PROC_TERMMASK) || parent->created > proc->created;