1 #include <lunaix/process.h>
2 #include <lunaix/sched.h>
3 #include <lunaix/mm/vmm.h>
4 #include <lunaix/mm/kalloc.h>
6 #include <arch/x86/interrupts.h>
7 #include <arch/x86/tss.h>
10 #include <lunaix/spike.h>
11 #include <lunaix/status.h>
12 #include <lunaix/syslog.h>
13 #include <lunaix/syscall.h>
15 #define MAX_PROCESS 512
17 volatile struct proc_info* __current;
19 struct proc_info dummy;
21 extern void __proc_table;
23 struct scheduler sched_ctx;
28 size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
30 vmm_alloc_pages(KERNEL_PID, &__proc_table, pg_size, PG_PREM_RW, PP_FGPERSIST),
31 "Fail to allocate proc table"
34 sched_ctx = (struct scheduler) {
35 ._procs = (struct proc_info*) &__proc_table,
41 void run(struct proc_info* proc) {
42 if (!(__current->state & ~PROC_RUNNING)) {
43 __current->state = PROC_STOPPED;
45 proc->state = PROC_RUNNING;
48 // tss_update_esp(__current->intr_ctx.esp);
50 if (__current->page_table != proc->page_table) {
52 cpu_lcr3(__current->page_table);
53 // from now on, the we are in the kstack of another process
59 apic_done_servicing();
63 "jmp soft_iret\n"::"r"(&__current->intr_ctx): "memory");
67 if (!sched_ctx.ptable_len) {
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 static void proc_timer_callback(struct proc_info* proc) {
88 proc->state = PROC_STOPPED;
91 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) {
92 // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
96 if (__current->timer) {
97 return __current->timer->counter / timer_context()->running_frequency;
100 struct lx_timer* timer = 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) {
108 terminate_proc(status);
111 __DEFINE_LXSYSCALL(void, yield) {
115 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status) {
116 pid_t cur = __current->pid;
117 struct proc_info *proc, *n;
118 if (llist_empty(&__current->children)) {
122 llist_for_each(proc, n, &__current->children, siblings) {
123 if (proc->state == PROC_TERMNAT) {
127 // FIXME: 除了循环,也许有更高效的办法…… (在这里进行schedule,需要重写context switch!)
131 *status = proc->exit_code;
132 return destroy_process(proc->pid);
137 for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY; i++);
139 if (i == MAX_PROCESS) {
140 panick("Process table is full");
145 void push_process(struct proc_info* process) {
146 int index = process->pid - 1;
147 if (index < 0 || index > sched_ctx.ptable_len) {
148 __current->k_status = LXINVLDPID;
152 if (index == sched_ctx.ptable_len) {
153 sched_ctx.ptable_len++;
156 sched_ctx._procs[index] = *process;
158 process = &sched_ctx._procs[index];
160 // make sure the address is in the range of process table
161 llist_init_head(&process->children);
162 // every process is the child of first process (pid=1)
163 if (process->parent) {
164 llist_append(&process->parent->children, &process->siblings);
167 process->parent = &sched_ctx._procs[0];
170 process->state = PROC_STOPPED;
173 // from <kernel/process.c>
174 extern void __del_pagetable(pid_t pid, uintptr_t mount_point);
176 pid_t destroy_process(pid_t pid) {
178 if (index <= 0 || index > sched_ctx.ptable_len) {
179 __current->k_status = LXINVLDPID;
182 struct proc_info *proc = &sched_ctx._procs[index];
183 proc->state = PROC_DESTROY;
184 llist_delete(&proc->siblings);
186 if (proc->mm.regions) {
187 struct mm_region *pos, *n;
188 llist_for_each(pos, n, &proc->mm.regions->head, head) {
193 vmm_mount_pd(PD_MOUNT_2, proc->page_table);
195 __del_pagetable(pid, PD_MOUNT_2);
197 vmm_unmount_pd(PD_MOUNT_2);
202 void terminate_proc(int exit_code) {
203 __current->state = PROC_TERMNAT;
204 __current->exit_code = exit_code;
209 struct proc_info* get_process(pid_t pid) {
211 if (index < 0 || index > sched_ctx.ptable_len) {
214 return &sched_ctx._procs[index];
217 int orphaned_proc(pid_t pid) {
219 if(pid >= sched_ctx.ptable_len) return 0;
220 struct proc_info* proc = &sched_ctx._procs[pid-1];
221 struct proc_info* parent = proc->parent;
223 // 如果其父进程的状态是terminated 或 destroy中的一种
224 // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
225 return (parent->state & PROC_TERMMASK) || parent->created > proc->created;