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