refactor: separate syscall interfaces from kernel space, into posix compliant structure.
[lunaix-os.git] / lunaix-os / kernel / process / process.c
1 #include <klibc/string.h>
2 #include <lunaix/clock.h>
3 #include <lunaix/common.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/region.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/process.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/status.h>
11 #include <lunaix/syscall.h>
12 #include <lunaix/syslog.h>
13
14 LOG_MODULE("PROC")
15
16 void*
17 __dup_pagetable(pid_t pid, uintptr_t mount_point)
18 {
19     void* ptd_pp = pmm_alloc_page(pid, PP_FGPERSIST);
20     vmm_set_mapping(PD_REFERENCED, PG_MOUNT_1, ptd_pp, PG_PREM_RW, VMAP_NULL);
21
22     x86_page_table* ptd = PG_MOUNT_1;
23     x86_page_table* pptd = (x86_page_table*)(mount_point | (0x3FF << 12));
24
25     size_t kspace_l1inx = L1_INDEX(KERNEL_MM_BASE);
26
27     for (size_t i = 0; i < PG_MAX_ENTRIES - 1; i++) {
28
29         x86_pte_t ptde = pptd->entry[i];
30         // 空或者是未在内存中的L1页表项直接照搬过去。
31         // 内核地址空间直接共享过去。
32         if (!ptde || i >= kspace_l1inx || !(ptde & PG_PRESENT)) {
33             ptd->entry[i] = ptde;
34             continue;
35         }
36
37         // 复制L2页表
38         void* pt_pp = pmm_alloc_page(pid, PP_FGPERSIST);
39         vmm_set_mapping(
40           PD_REFERENCED, PG_MOUNT_2, pt_pp, PG_PREM_RW, VMAP_NULL);
41
42         x86_page_table* ppt = (x86_page_table*)(mount_point | (i << 12));
43         x86_page_table* pt = PG_MOUNT_2;
44
45         for (size_t j = 0; j < PG_MAX_ENTRIES; j++) {
46             x86_pte_t pte = ppt->entry[j];
47             pmm_ref_page(pid, PG_ENTRY_ADDR(pte));
48             pt->entry[j] = pte;
49         }
50
51         ptd->entry[i] = (uintptr_t)pt_pp | PG_ENTRY_FLAGS(ptde);
52     }
53
54     ptd->entry[PG_MAX_ENTRIES - 1] = NEW_L1_ENTRY(T_SELF_REF_PERM, ptd_pp);
55
56     return ptd_pp;
57 }
58
59 void
60 __del_pagetable(pid_t pid, uintptr_t mount_point)
61 {
62     x86_page_table* pptd = (x86_page_table*)(mount_point | (0x3FF << 12));
63
64     // only remove user address space
65     for (size_t i = 0; i < L1_INDEX(KERNEL_MM_BASE); i++) {
66         x86_pte_t ptde = pptd->entry[i];
67         if (!ptde || !(ptde & PG_PRESENT)) {
68             continue;
69         }
70
71         x86_page_table* ppt = (x86_page_table*)(mount_point | (i << 12));
72
73         for (size_t j = 0; j < PG_MAX_ENTRIES; j++) {
74             x86_pte_t pte = ppt->entry[j];
75             // free the 4KB data page
76             if ((pte & PG_PRESENT)) {
77                 pmm_free_page(pid, PG_ENTRY_ADDR(pte));
78             }
79         }
80         // free the L2 page table
81         pmm_free_page(pid, PG_ENTRY_ADDR(ptde));
82     }
83     // free the L1 directory
84     pmm_free_page(pid, PG_ENTRY_ADDR(pptd->entry[PG_MAX_ENTRIES - 1]));
85 }
86
87 void*
88 vmm_dup_vmspace(pid_t pid)
89 {
90     return __dup_pagetable(pid, PD_REFERENCED);
91 }
92
93 __DEFINE_LXSYSCALL(pid_t, fork)
94 {
95     return dup_proc();
96 }
97
98 __DEFINE_LXSYSCALL(pid_t, getpid)
99 {
100     return __current->pid;
101 }
102
103 __DEFINE_LXSYSCALL(pid_t, getppid)
104 {
105     return __current->parent->pid;
106 }
107
108 __DEFINE_LXSYSCALL(pid_t, getpgid)
109 {
110     return __current->pgid;
111 }
112
113 __DEFINE_LXSYSCALL2(int, setpgid, pid_t, pid, pid_t, pgid)
114 {
115     struct proc_info* proc = pid ? get_process(pid) : __current;
116
117     if (!proc) {
118         __current->k_status = EINVAL;
119         return -1;
120     }
121
122     pgid = pgid ? pgid : proc->pid;
123
124     struct proc_info* gruppenfuhrer = get_process(pgid);
125
126     if (!gruppenfuhrer || proc->pgid == gruppenfuhrer->pid) {
127         __current->k_status = EINVAL;
128         return -1;
129     }
130
131     llist_delete(&proc->grp_member);
132     llist_append(&gruppenfuhrer->grp_member, &proc->grp_member);
133
134     proc->pgid = pgid;
135     return 0;
136 }
137
138 void
139 init_proc_user_space(struct proc_info* pcb)
140 {
141     vmm_mount_pd(PD_MOUNT_1, pcb->page_table);
142
143     /*---  分配用户栈  ---*/
144
145     struct mm_region* stack_vm;
146
147     stack_vm = region_create(
148       USTACK_END, USTACK_TOP, REGION_RW | REGION_RSHARED | REGION_ANON);
149     // 注册用户栈区域
150     region_add(&pcb->mm.regions, stack_vm);
151
152     // 预留地址空间,具体物理页将由Page Fault Handler按需分配。
153     for (uintptr_t i = PG_ALIGN(USTACK_END); i < USTACK_TOP; i += PG_SIZE) {
154         vmm_set_mapping(PD_MOUNT_1, i, 0, PG_ALLOW_USER | PG_WRITE, VMAP_NULL);
155     }
156
157     // TODO other uspace initialization stuff
158
159     vmm_unmount_pd(PD_MOUNT_1);
160 }
161
162 void
163 __mark_region(uintptr_t start_vpn, uintptr_t end_vpn, int attr)
164 {
165     for (size_t i = start_vpn; i <= end_vpn; i++) {
166         x86_pte_t* curproc = &PTE_MOUNTED(PD_REFERENCED, i);
167         x86_pte_t* newproc = &PTE_MOUNTED(PD_MOUNT_1, i);
168         cpu_invplg(newproc);
169
170         if ((attr & REGION_MODE_MASK) == REGION_RSHARED) {
171             // 如果读共享,则将两者的都标注为只读,那么任何写入都将会应用COW策略。
172             cpu_invplg(curproc);
173             cpu_invplg(i << 12);
174             *curproc = *curproc & ~PG_WRITE;
175             *newproc = *newproc & ~PG_WRITE;
176         } else {
177             // 如果是私有页,则将该页从新进程中移除。
178             *newproc = 0;
179         }
180     }
181 }
182
183 void
184 __copy_fdtable(struct proc_info* pcb)
185 {
186     for (size_t i = 0; i < VFS_MAX_FD; i++) {
187         struct v_fd* fd = __current->fdtable->fds[i];
188         if (!fd)
189             continue;
190         vfs_dup_fd(fd, &pcb->fdtable->fds[i]);
191     }
192 }
193
194 pid_t
195 dup_proc()
196 {
197     struct proc_info* pcb = alloc_process();
198     pcb->mm.u_heap = __current->mm.u_heap;
199     pcb->intr_ctx = __current->intr_ctx;
200     pcb->parent = __current;
201
202     memcpy(pcb->fxstate, __current->fxstate, 512);
203
204     if (__current->cwd) {
205         pcb->cwd = __current->cwd;
206         vfs_ref_dnode(pcb->cwd);
207     }
208
209     __copy_fdtable(pcb);
210     region_copy(&__current->mm.regions, &pcb->mm.regions);
211
212     setup_proc_mem(pcb, PD_REFERENCED);
213
214     // 根据 mm_region 进一步配置页表
215
216     struct mm_region *pos, *n;
217     llist_for_each(pos, n, &pcb->mm.regions, head)
218     {
219         // 如果写共享,则不作处理。
220         if ((pos->attr & REGION_WSHARED)) {
221             continue;
222         }
223
224         uintptr_t start_vpn = pos->start >> 12;
225         uintptr_t end_vpn = pos->end >> 12;
226         __mark_region(start_vpn, end_vpn, pos->attr);
227     }
228
229     vmm_unmount_pd(PD_MOUNT_1);
230
231     // 正如同fork,返回两次。
232     pcb->intr_ctx.registers.eax = 0;
233
234     commit_process(pcb);
235
236     return pcb->pid;
237 }
238
239 extern void __kernel_end;
240
241 void
242 setup_proc_mem(struct proc_info* proc, uintptr_t usedMnt)
243 {
244     // copy the entire kernel page table
245     pid_t pid = proc->pid;
246     void* pt_copy = __dup_pagetable(pid, usedMnt);
247
248     vmm_mount_pd(PD_MOUNT_1, pt_copy); // 将新进程的页表挂载到挂载点#2
249
250     // copy the kernel stack
251     for (size_t i = KSTACK_START >> 12; i <= KSTACK_TOP >> 12; i++) {
252         volatile x86_pte_t* ppte = &PTE_MOUNTED(PD_MOUNT_1, i);
253
254         /*
255             This is a fucking nightmare, the TLB caching keep the rewrite to PTE
256            from updating. Even the Nightmare Moon the Evil is far less nasty
257            than this. It took me hours of debugging to figure this out.
258
259             In the name of Celestia our glorious goddess, I will fucking HATE
260            the TLB for the rest of my LIFE!
261         */
262         cpu_invplg(ppte);
263
264         x86_pte_t p = *ppte;
265         void* ppa = vmm_dup_page(pid, PG_ENTRY_ADDR(p));
266         pmm_free_page(pid, PG_ENTRY_ADDR(p));
267         *ppte = (p & 0xfff) | (uintptr_t)ppa;
268     }
269
270     // 我们不需要分配内核的区域,因为所有的内核代码和数据段只能通过系统调用来访问,任何非法的访问
271     // 都会导致eip落在区域外面,从而segmentation fault.
272
273     // 至于其他的区域我们暂时没有办法知道,因为那需要知道用户程序的信息。我们留到之后在处理。
274     proc->page_table = pt_copy;
275 }