refactor: simplify the vmm design, single responsibility. But using it should with...
[lunaix-os.git] / lunaix-os / kernel / sched.c
1 #include <arch/x86/interrupts.h>
2 #include <arch/x86/tss.h>
3
4 #include <hal/apic.h>
5 #include <hal/cpu.h>
6
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>
17
18 #define MAX_PROCESS 512
19
20 volatile struct proc_info* __current;
21
22 struct proc_info dummy;
23
24 extern void __proc_table;
25
26 struct scheduler sched_ctx;
27
28 LOG_MODULE("SCHED")
29
30 void
31 sched_init()
32 {
33     size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
34
35     for (size_t i = 0; i <= pg_size; i += 4096) {
36         uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
37         vmm_set_mapping(PD_REFERENCED, &__proc_table + i, pa, PG_PREM_RW);
38     }
39
40     sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)&__proc_table,
41                                     .ptable_len = 0,
42                                     .procs_index = 0 };
43 }
44
45 void
46 run(struct proc_info* proc)
47 {
48     if (!(__current->state & ~PROC_RUNNING)) {
49         __current->state = PROC_STOPPED;
50     }
51     proc->state = PROC_RUNNING;
52
53     // FIXME: 这里还是得再考虑一下。
54     // tss_update_esp(__current->intr_ctx.esp);
55     apic_done_servicing();
56
57     asm volatile("pushl %0\n"
58                  "jmp switch_to\n" ::"r"(proc)); // kernel/asm/x86/interrupt.S
59 }
60
61 void
62 schedule()
63 {
64     if (!sched_ctx.ptable_len) {
65         return;
66     }
67
68     // 上下文切换相当的敏感!我们不希望任何的中断打乱栈的顺序……
69     cpu_disable_interrupt();
70     struct proc_info* next;
71     int prev_ptr = sched_ctx.procs_index;
72     int ptr = prev_ptr;
73     // round-robin scheduler
74     do {
75         ptr = (ptr + 1) % sched_ctx.ptable_len;
76         next = &sched_ctx._procs[ptr];
77     } while (next->state != PROC_STOPPED && ptr != prev_ptr);
78
79     sched_ctx.procs_index = ptr;
80
81     run(next);
82 }
83
84 static void
85 proc_timer_callback(struct proc_info* proc)
86 {
87     proc->timer = NULL;
88     proc->state = PROC_STOPPED;
89 }
90
91 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds)
92 {
93     // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
94     if (!seconds) {
95         return 0;
96     }
97
98     if (__current->timer) {
99         return __current->timer->counter / timer_context()->running_frequency;
100     }
101
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;
107     schedule();
108 }
109
110 __DEFINE_LXSYSCALL1(void, exit, int, status)
111 {
112     terminate_proc(status);
113 }
114
115 __DEFINE_LXSYSCALL(void, yield)
116 {
117     schedule();
118 }
119
120 pid_t
121 _wait(pid_t wpid, int* status, int options);
122
123 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status)
124 {
125     return _wait(-1, status, 0);
126 }
127
128 __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
129 {
130     return _wait(pid, status, options);
131 }
132
133 pid_t
134 _wait(pid_t wpid, int* status, int options)
135 {
136     pid_t cur = __current->pid;
137     int status_flags = 0;
138     struct proc_info *proc, *n;
139     if (llist_empty(&__current->children)) {
140         return -1;
141     }
142
143     wpid = wpid ? wpid : -__current->pgid;
144     cpu_enable_interrupt();
145 repeat:
146     llist_for_each(proc, n, &__current->children, siblings)
147     {
148         if (!~wpid || proc->pid == wpid || proc->pgid == -wpid) {
149             if (proc->state == PROC_TERMNAT && !options) {
150                 status_flags |= PROCTERM;
151                 goto done;
152             }
153             if (proc->state == PROC_STOPPED && (options & WUNTRACED)) {
154                 status_flags |= PROCSTOP;
155                 goto done;
156             }
157         }
158     }
159     if ((options & WNOHANG)) {
160         return 0;
161     }
162     // 放弃当前的运行机会
163     sched_yield();
164     goto repeat;
165
166 done:
167     cpu_disable_interrupt();
168     *status = (proc->exit_code & 0xffff) | status_flags;
169     return destroy_process(proc->pid);
170 }
171
172 pid_t
173 alloc_pid()
174 {
175     pid_t i = 0;
176     for (;
177          i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY;
178          i++)
179         ;
180
181     if (i == MAX_PROCESS) {
182         panick("Panic in Ponyville shimmer!");
183     }
184     return i;
185 }
186
187 void
188 push_process(struct proc_info* process)
189 {
190     int index = process->pid;
191     if (index < 0 || index > sched_ctx.ptable_len) {
192         __current->k_status = LXINVLDPID;
193         return;
194     }
195
196     if (index == sched_ctx.ptable_len) {
197         sched_ctx.ptable_len++;
198     }
199
200     sched_ctx._procs[index] = *process;
201
202     process = &sched_ctx._procs[index];
203
204     // make sure the reference is relative to process table
205     llist_init_head(&process->children);
206     llist_init_head(&process->grp_member);
207
208     // every process is the child of first process (pid=1)
209     if (process->parent) {
210         llist_append(&process->parent->children, &process->siblings);
211     } else {
212         process->parent = &sched_ctx._procs[0];
213     }
214
215     process->state = PROC_STOPPED;
216 }
217
218 // from <kernel/process.c>
219 extern void
220 __del_pagetable(pid_t pid, uintptr_t mount_point);
221
222 pid_t
223 destroy_process(pid_t pid)
224 {
225     int index = pid;
226     if (index <= 0 || index > sched_ctx.ptable_len) {
227         __current->k_status = LXINVLDPID;
228         return;
229     }
230     struct proc_info* proc = &sched_ctx._procs[index];
231     proc->state = PROC_DESTROY;
232     llist_delete(&proc->siblings);
233
234     if (proc->mm.regions) {
235         struct mm_region *pos, *n;
236         llist_for_each(pos, n, &proc->mm.regions->head, head)
237         {
238             lxfree(pos);
239         }
240     }
241
242     vmm_mount_pd(PD_MOUNT_2, proc->page_table);
243
244     __del_pagetable(pid, PD_MOUNT_2);
245
246     vmm_unmount_pd(PD_MOUNT_2);
247
248     return pid;
249 }
250
251 void
252 terminate_proc(int exit_code)
253 {
254     __current->state = PROC_TERMNAT;
255     __current->exit_code = exit_code;
256
257     schedule();
258 }
259
260 struct proc_info*
261 get_process(pid_t pid)
262 {
263     int index = pid;
264     if (index < 0 || index > sched_ctx.ptable_len) {
265         return NULL;
266     }
267     return &sched_ctx._procs[index];
268 }
269
270 int
271 orphaned_proc(pid_t pid)
272 {
273     if (!pid)
274         return 0;
275     if (pid >= sched_ctx.ptable_len)
276         return 0;
277     struct proc_info* proc = &sched_ctx._procs[pid];
278     struct proc_info* parent = proc->parent;
279
280     // 如果其父进程的状态是terminated 或 destroy中的一种
281     // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
282     return (parent->state & PROC_TERMMASK) || parent->created > proc->created;
283 }