feat: support ioctl() syscall for direct control to devices
[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     if (__current->cwd) {
200         pcb->cwd = __current->cwd;
201         vfs_ref_dnode(pcb->cwd);
202     }
203
204     __copy_fdtable(pcb);
205     region_copy(&__current->mm.regions, &pcb->mm.regions);
206
207     setup_proc_mem(pcb, PD_REFERENCED);
208
209     // 根据 mm_region 进一步配置页表
210
211     struct mm_region *pos, *n;
212     llist_for_each(pos, n, &pcb->mm.regions.head, head)
213     {
214         // 如果写共享,则不作处理。
215         if ((pos->attr & REGION_WSHARED)) {
216             continue;
217         }
218
219         uintptr_t start_vpn = pos->start >> 12;
220         uintptr_t end_vpn = pos->end >> 12;
221         __mark_region(start_vpn, end_vpn, pos->attr);
222     }
223
224     vmm_unmount_pd(PD_MOUNT_1);
225
226     // 正如同fork,返回两次。
227     pcb->intr_ctx.registers.eax = 0;
228
229     commit_process(pcb);
230
231     return pcb->pid;
232 }
233
234 extern void __kernel_end;
235
236 void
237 setup_proc_mem(struct proc_info* proc, uintptr_t usedMnt)
238 {
239     // copy the entire kernel page table
240     pid_t pid = proc->pid;
241     void* pt_copy = __dup_pagetable(pid, usedMnt);
242
243     vmm_mount_pd(PD_MOUNT_1, pt_copy); // 将新进程的页表挂载到挂载点#2
244
245     // copy the kernel stack
246     for (size_t i = KSTACK_START >> 12; i <= KSTACK_TOP >> 12; i++) {
247         volatile x86_pte_t* ppte = &PTE_MOUNTED(PD_MOUNT_1, i);
248
249         /*
250             This is a fucking nightmare, the TLB caching keep the rewrite to PTE
251            from updating. Even the Nightmare Moon the Evil is far less nasty
252            than this. It took me hours of debugging to figure this out.
253
254             In the name of Celestia our glorious goddess, I will fucking HATE
255            the TLB for the rest of my LIFE!
256         */
257         cpu_invplg(ppte);
258
259         x86_pte_t p = *ppte;
260         void* ppa = vmm_dup_page(pid, PG_ENTRY_ADDR(p));
261         pmm_free_page(pid, PG_ENTRY_ADDR(p));
262         *ppte = (p & 0xfff) | (uintptr_t)ppa;
263     }
264
265     // 我们不需要分配内核的区域,因为所有的内核代码和数据段只能通过系统调用来访问,任何非法的访问
266     // 都会导致eip落在区域外面,从而segmentation fault.
267
268     // 至于其他的区域我们暂时没有办法知道,因为那需要知道用户程序的信息。我们留到之后在处理。
269     proc->page_table = pt_copy;
270 }