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