1 /* $OpenBSD: vm_machdep.c,v 1.9 2023/04/11 00:45:08 jsg Exp $ */
2
3 /*-
4 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
5 * Copyright (c) 1982, 1986 The Regents of the University of California.
6 * Copyright (c) 1989, 1990 William Jolitz
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department, and William Jolitz.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
38 */
39
40 /*
41 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
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/vnode.h>
49 #include <sys/buf.h>
50 #include <sys/user.h>
51 #include <sys/exec.h>
52 #include <sys/ptrace.h>
53 #include <sys/signalvar.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #include <machine/cpu.h>
58 #include <machine/fpu.h>
59 #include <machine/reg.h>
60
61 /*
62 * Finish a fork operation, with process p2 nearly set up.
63 * Copy and update the kernel stack and pcb, making the child
64 * ready to run, and marking it so that it can return differently
65 * than the parent. Returns 1 in the child process, 0 in the parent.
66 */
67 void
cpu_fork(struct proc * p1,struct proc * p2,void * stack,void * tcb,void (* func)(void *),void * arg)68 cpu_fork(struct proc *p1, struct proc *p2, void *stack, void *tcb,
69 void (*func)(void *), void *arg)
70 {
71 struct pcb *pcb = &p2->p_addr->u_pcb;
72 struct pcb *pcb1 = &p1->p_addr->u_pcb;
73 struct trapframe *tf;
74 struct callframe *cf;
75 struct switchframe *sf;
76 register_t kstack;
77
78 /* Save FPU state to PCB if necessary. */
79 if (pcb1->pcb_flags & (PCB_FPU|PCB_VEC|PCB_VSX) &&
80 p1->p_md.md_regs->srr1 & (PSL_FPU|PSL_VEC|PSL_VSX)) {
81 p1->p_md.md_regs->srr1 &= ~(PSL_FPU|PSL_VEC|PSL_VSX);
82 save_vsx(p1);
83 }
84
85 /* Copy the pcb. */
86 *pcb = p1->p_addr->u_pcb;
87
88 /* XXX This should not be necessary but things explode without it. */
89 memset(&pcb->pcb_slb, 0, sizeof(pcb->pcb_slb));
90
91 pmap_extract(pmap_kernel(), (vaddr_t)&pcb->pcb_slb,
92 &p2->p_md.md_user_slb_pa);
93 pmap_activate(p2);
94
95 kstack = (register_t)p2->p_addr + USPACE - FRAMELEN -
96 (arc4random() & PAGE_MASK & ~_STACKALIGNBYTES);
97 p2->p_md.md_regs = tf = (struct trapframe *)kstack;
98 *tf = *p1->p_md.md_regs;
99
100 if (stack != NULL)
101 tf->fixreg[1] = STACKALIGN(stack);
102 if (tcb != NULL)
103 tf->fixreg[13] = (register_t)tcb;
104
105 cf = (struct callframe *)tf - 1;
106 memset(cf, 0, sizeof(*cf));
107 cf->cf_lr = (register_t)&proc_trampoline;
108
109 sf = (struct switchframe *)cf - 1;
110 memset(sf, 0, sizeof(*sf));
111 sf->sf_r31 = (register_t)func;
112 sf->sf_r30 = (register_t)arg;
113 pcb->pcb_sp = (register_t)sf;
114 }
115
116 /*
117 * cpu_exit is called as the last action during exit.
118 *
119 * We clean up a little and then call sched_exit() with the old proc as an
120 * argument.
121 */
122 void
cpu_exit(struct proc * p)123 cpu_exit(struct proc *p)
124 {
125 pmap_deactivate(p);
126 sched_exit(p);
127 }
128
129 struct kmem_va_mode kv_physwait = {
130 .kv_map = &phys_map,
131 .kv_wait = 1,
132 };
133
134 /*
135 * Map a user I/O request into kernel virtual address space.
136 * Note: the pages are already locked by uvm_vslock(), so we
137 * do not need to pass an access_type to pmap_enter().
138 */
139 void
vmapbuf(struct buf * bp,vsize_t len)140 vmapbuf(struct buf *bp, vsize_t len)
141 {
142 vaddr_t faddr, taddr, off;
143 paddr_t fpa;
144
145 if ((bp->b_flags & B_PHYS) == 0)
146 panic("vmapbuf");
147 faddr = trunc_page((vaddr_t)(bp->b_saveaddr = bp->b_data));
148 off = (vaddr_t)bp->b_data - faddr;
149 len = round_page(off + len);
150 taddr = (vaddr_t)km_alloc(len, &kv_physwait, &kp_none, &kd_waitok);
151 bp->b_data = (caddr_t)(taddr + off);
152 /*
153 * The region is locked, so we expect that pmap_pte() will return
154 * non-NULL.
155 * XXX: unwise to expect this in a multithreaded environment.
156 * anything can happen to a pmap between the time we lock a
157 * region, release the pmap lock, and then relock it for
158 * the pmap_extract().
159 *
160 * no need to flush TLB since we expect nothing to be mapped
161 * where we just allocated (TLB will be flushed when our
162 * mapping is removed).
163 */
164 while (len) {
165 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
166 faddr, &fpa);
167 pmap_kenter_pa(taddr, fpa, PROT_READ | PROT_WRITE);
168 faddr += PAGE_SIZE;
169 taddr += PAGE_SIZE;
170 len -= PAGE_SIZE;
171 }
172 pmap_update(pmap_kernel());
173 }
174
175 /*
176 * Unmap a previously-mapped user I/O request.
177 */
178 void
vunmapbuf(struct buf * bp,vsize_t len)179 vunmapbuf(struct buf *bp, vsize_t len)
180 {
181 vaddr_t addr, off;
182
183 if ((bp->b_flags & B_PHYS) == 0)
184 panic("vunmapbuf");
185 addr = trunc_page((vaddr_t)bp->b_data);
186 off = (vaddr_t)bp->b_data - addr;
187 len = round_page(off + len);
188 pmap_kremove(addr, len);
189 pmap_update(pmap_kernel());
190 km_free((void *)addr, len, &kv_physwait, &kp_none);
191 bp->b_data = bp->b_saveaddr;
192 bp->b_saveaddr = NULL;
193 }
194