1 /* $OpenBSD: vm_machdep.c,v 1.42 2022/05/22 16:54:17 kettenis Exp $ */
2 /*
3 * Copyright (c) 1988 University of Utah.
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department and Ralph Campbell.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * from: Utah Hdr: vm_machdep.c 1.21 91/04/06
40 *
41 * from: @(#)vm_machdep.c 8.3 (Berkeley) 1/4/94
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 #include <sys/buf.h>
49 #include <sys/vnode.h>
50 #include <sys/user.h>
51 #include <sys/exec.h>
52 #include <sys/signalvar.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <machine/cpu.h>
57 #include <mips64/mips_cpu.h>
58
59 extern void proc_trampoline(void);
60 /*
61 * Finish a fork operation, with process p2 nearly set up.
62 */
63 void
cpu_fork(struct proc * p1,struct proc * p2,void * stack,void * tcb,void (* func)(void *),void * arg)64 cpu_fork(struct proc *p1, struct proc *p2, void *stack, void *tcb,
65 void (*func)(void *), void *arg)
66 {
67 struct cpu_info *ci = curcpu();
68 struct pcb *pcb;
69 #if UPAGES == 1
70 paddr_t pa;
71
72 /* replace p_addr with a direct translation address */
73 p2->p_md.md_uarea = (vaddr_t)p2->p_addr;
74 pmap_extract(pmap_kernel(), p2->p_md.md_uarea, &pa);
75 p2->p_addr = (void *)PHYS_TO_XKPHYS(pa, CCA_CACHED);
76 #endif
77 pcb = &p2->p_addr->u_pcb;
78
79 /*
80 * If we own the FPU, save its state before copying the PCB.
81 */
82 if (p1 == ci->ci_fpuproc)
83 save_fpu();
84
85 p2->p_md.md_flags = p1->p_md.md_flags & MDP_FORKSAVE;
86 #ifdef FPUEMUL
87 if (!CPU_HAS_FPU(ci)) {
88 p2->p_md.md_fppgva = p1->p_md.md_fppgva;
89 KASSERT((p2->p_md.md_flags & MDP_FPUSED) == 0);
90 }
91 #endif
92
93 /* Copy pcb from p1 to p2 */
94 if (p1 == curproc) {
95 /* Sync the PCB before we copy it. */
96 savectx(p1->p_addr, 0);
97 }
98 #ifdef DIAGNOSTIC
99 else if (p1 != &proc0)
100 panic("cpu_fork: curproc");
101 #endif
102 *pcb = p1->p_addr->u_pcb;
103 p2->p_md.md_regs = &p2->p_addr->u_pcb.pcb_regs;
104
105 /*
106 * If specified, give the child a different stack and/or TCB.
107 */
108 if (stack != NULL)
109 p2->p_md.md_regs->sp = (u_int64_t)stack;
110 p2->p_md.md_tcb = tcb != NULL ? tcb : p1->p_md.md_tcb;
111
112 /*
113 * Copy the process control block to the new proc and
114 * create a clean stack for exit through trampoline.
115 * pcb_context has s0-s7, sp, s8, ra, sr.
116 */
117 if (p1 != curproc) {
118 pcb->pcb_context.val[11] = (pcb->pcb_regs.sr & ~SR_INT_MASK) |
119 (idle_mask & SR_INT_MASK);
120 }
121 pcb->pcb_context.val[10] = (register_t)proc_trampoline;
122 pcb->pcb_context.val[8] = (register_t)pcb +
123 ((USPACE - sizeof(struct trapframe)) & ~_STACKALIGNBYTES);
124 pcb->pcb_context.val[1] = (register_t)arg;
125 pcb->pcb_context.val[0] = (register_t)func;
126 }
127
128 /*
129 * cpu_exit is called as the last action during exit.
130 */
131 void
cpu_exit(struct proc * p)132 cpu_exit(struct proc *p)
133 {
134 struct cpu_info *ci = curcpu();
135
136 if (ci->ci_fpuproc == p)
137 ci->ci_fpuproc = NULL;
138
139 pmap_deactivate(p);
140 #if UPAGES == 1
141 /* restore p_addr for proper deallocation */
142 p->p_addr = (void *)p->p_md.md_uarea;
143 #endif
144 sched_exit(p);
145 }
146
147 struct kmem_va_mode kv_physwait = {
148 .kv_map = &phys_map,
149 .kv_wait = 1,
150 };
151
152 /*
153 * Map an IO request into kernel virtual address space.
154 */
155 void
vmapbuf(struct buf * bp,vsize_t len)156 vmapbuf(struct buf *bp, vsize_t len)
157 {
158 struct kmem_dyn_mode kd_prefer = { .kd_waitok = 1 };
159 struct pmap *pm = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
160 vaddr_t kva, uva;
161 vsize_t size, off;
162
163 #ifdef DIAGNOSTIC
164 if ((bp->b_flags & B_PHYS) == 0)
165 panic("vmapbuf");
166 #endif
167 bp->b_saveaddr = bp->b_data;
168 uva = trunc_page((vaddr_t)bp->b_data);
169 off = (vaddr_t)bp->b_data - uva;
170 size = round_page(off + len);
171
172 kd_prefer.kd_prefer = uva;
173 kva = (vaddr_t)km_alloc(size, &kv_physwait, &kp_none, &kd_prefer);
174 bp->b_data = (caddr_t)(kva + off);
175 while (size > 0) {
176 paddr_t pa;
177
178 if (pmap_extract(pm, uva, &pa) == FALSE)
179 panic("vmapbuf: null page frame");
180 else
181 pmap_kenter_pa(kva, pa, PROT_READ | PROT_WRITE);
182 uva += PAGE_SIZE;
183 kva += PAGE_SIZE;
184 size -= PAGE_SIZE;
185 }
186 pmap_update(pmap_kernel());
187 }
188
189 /*
190 * Unmap IO request from the kernel virtual address space.
191 */
192 void
vunmapbuf(struct buf * bp,vsize_t len)193 vunmapbuf(struct buf *bp, vsize_t len)
194 {
195 vaddr_t addr, off;
196
197 #ifdef DIAGNOSTIC
198 if ((bp->b_flags & B_PHYS) == 0)
199 panic("vunmapbuf");
200 #endif
201 addr = trunc_page((vaddr_t)bp->b_data);
202 off = (vaddr_t)bp->b_data - addr;
203 len = round_page(off + len);
204 pmap_kremove(addr, len);
205 pmap_update(pmap_kernel());
206 km_free((void *)addr, len, &kv_physwait, &kp_none);
207 bp->b_data = bp->b_saveaddr;
208 bp->b_saveaddr = NULL;
209 }
210