feat: No more kernel page table switching upon interrupt.
[lunaix-os.git] / lunaix-os / kernel / sched.c
1 #include <lunaix/process.h>
2 #include <lunaix/sched.h>
3 #include <lunaix/mm/vmm.h>
4 #include <lunaix/mm/kalloc.h>
5 #include <hal/cpu.h>
6 #include <arch/x86/interrupts.h>
7 #include <arch/x86/tss.h>
8 #include <hal/apic.h>
9
10 #include <lunaix/spike.h>
11 #include <lunaix/status.h>
12 #include <lunaix/syslog.h>
13 #include <lunaix/syscall.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 sched_init() {
28     size_t pg_size = ROUNDUP(sizeof(struct proc_info) * MAX_PROCESS, 0x1000);
29     assert_msg(
30         vmm_alloc_pages(KERNEL_PID, &__proc_table, pg_size, PG_PREM_RW, PP_FGPERSIST), 
31         "Fail to allocate proc table"
32     );
33     
34     sched_ctx = (struct scheduler) {
35         ._procs = (struct proc_info*) &__proc_table,
36         .ptable_len = 0,
37         .procs_index = 0
38     };
39 }
40
41 void run(struct proc_info* proc) {
42     if (!(__current->state & ~PROC_RUNNING)) {
43         __current->state = PROC_STOPPED;
44     }
45     proc->state = PROC_RUNNING;
46     
47     // FIXME: 这里还是得再考虑一下。
48     // tss_update_esp(__current->intr_ctx.esp);
49
50     if (__current->page_table != proc->page_table) {
51         __current = proc;
52         cpu_lcr3(__current->page_table);
53         // from now on, the we are in the kstack of another process
54     }
55     else {
56         __current = proc;
57     }
58
59     apic_done_servicing();
60
61     asm volatile (
62         "pushl %0\n"
63         "jmp soft_iret\n"::"r"(&__current->intr_ctx): "memory");
64 }
65
66 void schedule() {
67     if (!sched_ctx.ptable_len) {
68         return;
69     }
70
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
83     run(next);
84 }
85
86 static void proc_timer_callback(struct proc_info* proc) {
87     proc->timer = NULL;
88     proc->state = PROC_STOPPED;
89 }
90
91 __DEFINE_LXSYSCALL1(unsigned int, sleep, unsigned int, seconds) {
92     // FIXME: sleep的实现或许需要改一下。专门绑一个计时器好像没有必要……
93     if (!seconds) {
94         return 0;
95     }
96     if (__current->timer) {
97         return __current->timer->counter / timer_context()->running_frequency;
98     }
99
100     struct lx_timer* timer = timer_run_second(seconds, proc_timer_callback, __current, 0);
101     __current->timer = timer;
102     __current->intr_ctx.registers.eax = seconds;
103     __current->state = PROC_BLOCKED;
104     schedule();
105 }
106
107 __DEFINE_LXSYSCALL1(void, exit, int, status) {
108     terminate_proc(status);
109 }
110
111 __DEFINE_LXSYSCALL(void, yield) {
112     schedule();
113 }
114
115 __DEFINE_LXSYSCALL1(pid_t, wait, int*, status) {
116     pid_t cur = __current->pid;
117     struct proc_info *proc, *n;
118     if (llist_empty(&__current->children)) {
119         return -1;
120     }
121 repeat:
122     llist_for_each(proc, n, &__current->children, siblings) {
123         if (proc->state == PROC_TERMNAT) {
124             goto done;
125         }
126     }
127     // FIXME: 除了循环,也许有更高效的办法…… (在这里进行schedule,需要重写context switch!)
128     goto repeat;
129
130 done:
131     *status = proc->exit_code;
132     return destroy_process(proc->pid);
133 }
134
135 pid_t alloc_pid() {
136     pid_t i = 0;
137     for (; i < sched_ctx.ptable_len && sched_ctx._procs[i].state != PROC_DESTROY; i++);
138
139     if (i == MAX_PROCESS) {
140         panick("Process table is full");
141     }
142     return i + 1;
143 }
144
145 void push_process(struct proc_info* process) {
146     int index = process->pid - 1;
147     if (index < 0 || index > sched_ctx.ptable_len) {
148         __current->k_status = LXINVLDPID;
149         return;
150     }
151     
152     if (index == sched_ctx.ptable_len) {
153         sched_ctx.ptable_len++;
154     }
155     
156     sched_ctx._procs[index] = *process;
157
158     process = &sched_ctx._procs[index];
159
160     // make sure the address is in the range of process table
161     llist_init_head(&process->children);
162     // every process is the child of first process (pid=1)
163     if (process->parent) {
164         llist_append(&process->parent->children, &process->siblings);
165     }
166     else {
167         process->parent = &sched_ctx._procs[0];
168     }
169
170     process->state = PROC_STOPPED;    
171 }
172
173 // from <kernel/process.c>
174 extern void __del_pagetable(pid_t pid, uintptr_t mount_point);
175
176 pid_t destroy_process(pid_t pid) {
177     int index = pid - 1;
178     if (index <= 0 || index > sched_ctx.ptable_len) {
179         __current->k_status = LXINVLDPID;
180         return;
181     }
182     struct proc_info *proc = &sched_ctx._procs[index];
183     proc->state = PROC_DESTROY;
184     llist_delete(&proc->siblings);
185
186     if (proc->mm.regions) {
187         struct mm_region *pos, *n;
188         llist_for_each(pos, n, &proc->mm.regions->head, head) {
189             lxfree(pos);
190         }
191     }
192
193     vmm_mount_pd(PD_MOUNT_2, proc->page_table);
194
195     __del_pagetable(pid, PD_MOUNT_2);
196
197     vmm_unmount_pd(PD_MOUNT_2);
198
199     return pid;
200 }
201
202 void terminate_proc(int exit_code) {
203     __current->state = PROC_TERMNAT;
204     __current->exit_code = exit_code;
205
206     schedule();
207 }
208
209 struct proc_info* get_process(pid_t pid) {
210     int index = pid - 1;
211     if (index < 0 || index > sched_ctx.ptable_len) {
212         return NULL;
213     }
214     return &sched_ctx._procs[index];
215 }
216
217 int orphaned_proc(pid_t pid) {
218     if(!pid) return 0;
219     if(pid >= sched_ctx.ptable_len) return 0;
220     struct proc_info* proc = &sched_ctx._procs[pid-1];
221     struct proc_info* parent = proc->parent;
222     
223     // 如果其父进程的状态是terminated 或 destroy中的一种
224     // 或者其父进程是在该进程之后创建的,那么该进程为孤儿进程
225     return (parent->state & PROC_TERMMASK) || parent->created > proc->created;
226 }