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