feat: User mode support.
[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 struct scheduler sched_ctx;
25
26 LOG_MODULE("SCHED")
27
28 void
29 sched_init()
30 {
31     size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
32
33     for (size_t i = 0; i <= pg_size; i += 4096) {
34         uintptr_t pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
35         vmm_set_mapping(
36           PD_REFERENCED, PROC_START + i, pa, PG_PREM_RW, VMAP_NULL);
37     }
38
39     sched_ctx = (struct scheduler){ ._procs = (struct proc_info*)PROC_START,
40                                     .ptable_len = 0,
41                                     .procs_index = 0 };
42 }
43
44 void
45 run(struct proc_info* proc)
46 {
47     if (!(__current->state & ~PROC_RUNNING)) {
48         __current->state = PROC_STOPPED;
49     }
50     proc->state = PROC_RUNNING;
51
52     // XXX: 我们需要这一步吗?
53     // tss_update_esp(__current->intr_ctx.esp);
54
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 struct proc_info*
173 alloc_process()
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
185     if (i == sched_ctx.ptable_len) {
186         sched_ctx.ptable_len++;
187     }
188
189     struct proc_info* proc = &sched_ctx._procs[i];
190     memset(proc, 0, sizeof(*proc));
191
192     proc->state = PROC_CREATED;
193     proc->pid = i;
194     proc->created = clock_systime();
195     proc->pgid = proc->pid;
196
197     llist_init_head(&proc->mm.regions);
198     llist_init_head(&proc->children);
199     llist_init_head(&proc->grp_member);
200
201     return proc;
202 }
203
204 void
205 commit_process(struct proc_info* process)
206 {
207     assert(process == &sched_ctx._procs[process->pid]);
208
209     if (process->state != PROC_CREATED) {
210         __current->k_status = LXINVL;
211         return;
212     }
213
214     // every process is the child of first process (pid=1)
215     if (process->parent) {
216         llist_append(&process->parent->children, &process->siblings);
217     } else {
218         process->parent = &sched_ctx._procs[0];
219     }
220
221     process->state = PROC_STOPPED;
222 }
223
224 // from <kernel/process.c>
225 extern void
226 __del_pagetable(pid_t pid, uintptr_t mount_point);
227
228 pid_t
229 destroy_process(pid_t pid)
230 {
231     int index = pid;
232     if (index <= 0 || index > sched_ctx.ptable_len) {
233         __current->k_status = LXINVLDPID;
234         return;
235     }
236     struct proc_info* proc = &sched_ctx._procs[index];
237     proc->state = PROC_DESTROY;
238     llist_delete(&proc->siblings);
239
240     struct mm_region *pos, *n;
241     llist_for_each(pos, n, &proc->mm.regions.head, head)
242     {
243         lxfree(pos);
244     }
245
246     vmm_mount_pd(PD_MOUNT_1, proc->page_table);
247
248     __del_pagetable(pid, PD_MOUNT_1);
249
250     vmm_unmount_pd(PD_MOUNT_1);
251
252     return pid;
253 }
254
255 void
256 terminate_proc(int exit_code)
257 {
258     __current->state = PROC_TERMNAT;
259     __current->exit_code = exit_code;
260
261     schedule();
262 }
263
264 struct proc_info*
265 get_process(pid_t pid)
266 {
267     int index = pid;
268     if (index < 0 || index > sched_ctx.ptable_len) {
269         return NULL;
270     }
271     return &sched_ctx._procs[index];
272 }
273
274 int
275 orphaned_proc(pid_t pid)
276 {
277     if (!pid)
278         return 0;
279     if (pid >= sched_ctx.ptable_len)
280         return 0;
281     struct proc_info* proc = &sched_ctx._procs[pid];
282     struct proc_info* parent = proc->parent;
283
284     // 如果其父进程的状态是terminated 或 destroy中的一种
285     // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
286     return (parent->state & PROC_TERMMASK) || parent->created > proc->created;
287 }