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