refactor: cut off some bloats in intr_ctx
[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     // 注册用户栈区域
146     region_add(
147       &pcb->mm.regions, USTACK_END, USTACK_TOP, REGION_RW | REGION_RSHARED);
148
149     // 预留地址空间,具体物理页将由Page Fault Handler按需分配。
150     for (uintptr_t i = PG_ALIGN(USTACK_END); i < USTACK_TOP; i += PG_SIZE) {
151         vmm_set_mapping(PD_MOUNT_1, i, 0, PG_ALLOW_USER | PG_WRITE, VMAP_NULL);
152     }
153
154     // TODO other uspace initialization stuff
155
156     vmm_unmount_pd(PD_MOUNT_1);
157 }
158
159 void
160 __mark_region(uintptr_t start_vpn, uintptr_t end_vpn, int attr)
161 {
162     for (size_t i = start_vpn; i <= end_vpn; i++) {
163         x86_pte_t* curproc = &PTE_MOUNTED(PD_REFERENCED, i);
164         x86_pte_t* newproc = &PTE_MOUNTED(PD_MOUNT_1, i);
165         cpu_invplg(newproc);
166
167         if ((attr & REGION_MODE_MASK) == REGION_RSHARED) {
168             // 如果读共享,则将两者的都标注为只读,那么任何写入都将会应用COW策略。
169             cpu_invplg(curproc);
170             cpu_invplg(i << 12);
171             *curproc = *curproc & ~PG_WRITE;
172             *newproc = *newproc & ~PG_WRITE;
173         } else {
174             // 如果是私有页,则将该页从新进程中移除。
175             *newproc = 0;
176         }
177     }
178 }
179
180 void
181 __copy_fdtable(struct proc_info* pcb)
182 {
183     for (size_t i = 0; i < VFS_MAX_FD; i++) {
184         struct v_fd* fd = __current->fdtable->fds[i];
185         if (!fd)
186             continue;
187         vfs_dup_fd(fd, &pcb->fdtable->fds[i]);
188     }
189 }
190
191 pid_t
192 dup_proc()
193 {
194     struct proc_info* pcb = alloc_process();
195     pcb->mm.u_heap = __current->mm.u_heap;
196     pcb->intr_ctx = __current->intr_ctx;
197     pcb->parent = __current;
198
199     memcpy(pcb->fxstate, __current->fxstate, 512);
200
201     if (__current->cwd) {
202         pcb->cwd = __current->cwd;
203         vfs_ref_dnode(pcb->cwd);
204     }
205
206     __copy_fdtable(pcb);
207     region_copy(&__current->mm.regions, &pcb->mm.regions);
208
209     setup_proc_mem(pcb, PD_REFERENCED);
210
211     // 根据 mm_region 进一步配置页表
212
213     struct mm_region *pos, *n;
214     llist_for_each(pos, n, &pcb->mm.regions.head, head)
215     {
216         // 如果写共享,则不作处理。
217         if ((pos->attr & REGION_WSHARED)) {
218             continue;
219         }
220
221         uintptr_t start_vpn = pos->start >> 12;
222         uintptr_t end_vpn = pos->end >> 12;
223         __mark_region(start_vpn, end_vpn, pos->attr);
224     }
225
226     vmm_unmount_pd(PD_MOUNT_1);
227
228     // 正如同fork,返回两次。
229     pcb->intr_ctx.registers.eax = 0;
230
231     commit_process(pcb);
232
233     return pcb->pid;
234 }
235
236 extern void __kernel_end;
237
238 void
239 setup_proc_mem(struct proc_info* proc, uintptr_t usedMnt)
240 {
241     // copy the entire kernel page table
242     pid_t pid = proc->pid;
243     void* pt_copy = __dup_pagetable(pid, usedMnt);
244
245     vmm_mount_pd(PD_MOUNT_1, pt_copy); // 将新进程的页表挂载到挂载点#2
246
247     // copy the kernel stack
248     for (size_t i = KSTACK_START >> 12; i <= KSTACK_TOP >> 12; i++) {
249         volatile x86_pte_t* ppte = &PTE_MOUNTED(PD_MOUNT_1, i);
250
251         /*
252             This is a fucking nightmare, the TLB caching keep the rewrite to PTE
253            from updating. Even the Nightmare Moon the Evil is far less nasty
254            than this. It took me hours of debugging to figure this out.
255
256             In the name of Celestia our glorious goddess, I will fucking HATE
257            the TLB for the rest of my LIFE!
258         */
259         cpu_invplg(ppte);
260
261         x86_pte_t p = *ppte;
262         void* ppa = vmm_dup_page(pid, PG_ENTRY_ADDR(p));
263         pmm_free_page(pid, PG_ENTRY_ADDR(p));
264         *ppte = (p & 0xfff) | (uintptr_t)ppa;
265     }
266
267     // 我们不需要分配内核的区域,因为所有的内核代码和数据段只能通过系统调用来访问,任何非法的访问
268     // 都会导致eip落在区域外面,从而segmentation fault.
269
270     // 至于其他的区域我们暂时没有办法知道,因为那需要知道用户程序的信息。我们留到之后在处理。
271     proc->page_table = pt_copy;
272 }