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