1 /* $OpenBSD: vm_machdep.c,v 1.55 2023/04/11 00:45:07 jsg Exp $ */
2 /* $NetBSD: vm_machdep.c,v 1.1 1996/09/30 16:34:57 ws Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1996 TooLs GmbH.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/exec.h>
37 #include <sys/pool.h>
38 #include <sys/proc.h>
39 #include <sys/signalvar.h>
40 #include <sys/user.h>
41 #include <sys/vnode.h>
42 #include <sys/ptrace.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <machine/pcb.h>
47 #include <machine/fpu.h>
48
49 /*
50 * Finish a fork operation, with process p2 nearly set up.
51 */
52 void
cpu_fork(struct proc * p1,struct proc * p2,void * stack,void * tcb,void (* func)(void *),void * arg)53 cpu_fork(struct proc *p1, struct proc *p2, void *stack, void *tcb,
54 void (*func)(void *), void *arg)
55 {
56 struct trapframe *tf;
57 struct callframe *cf;
58 struct switchframe *sf;
59 caddr_t stktop1, stktop2;
60 extern void proc_trampoline(void);
61 struct pcb *pcb = &p2->p_addr->u_pcb;
62 struct cpu_info *ci = curcpu();
63
64 if (p1 == ci->ci_fpuproc)
65 save_fpu();
66 *pcb = p1->p_addr->u_pcb;
67
68 #ifdef ALTIVEC
69 if (p1->p_addr->u_pcb.pcb_vr != NULL) {
70 if (p1 == ci->ci_vecproc)
71 save_vec(p1);
72 pcb->pcb_vr = pool_get(&ppc_vecpl, PR_WAITOK);
73 *pcb->pcb_vr = *p1->p_addr->u_pcb.pcb_vr;
74 } else
75 pcb->pcb_vr = NULL;
76
77 #endif /* ALTIVEC */
78
79 pcb->pcb_pm = p2->p_vmspace->vm_map.pmap;
80
81 pmap_extract(pmap_kernel(),
82 (vaddr_t)pcb->pcb_pm, (paddr_t *)&pcb->pcb_pmreal);
83
84 /*
85 * Setup the trap frame for the new process
86 */
87 stktop1 = (caddr_t)trapframe(p1);
88 stktop2 = (caddr_t)trapframe(p2);
89 bcopy(stktop1, stktop2, sizeof(struct trapframe));
90
91 /*
92 * If specified, give the child a different stack.
93 */
94 if (stack != NULL) {
95 tf = trapframe(p2);
96 tf->fixreg[1] = (register_t)stack;
97 }
98 if (tcb != NULL) {
99 tf = trapframe(p2);
100 tf->fixreg[2] = (register_t)tcb;
101 }
102
103 stktop2 = (caddr_t)((u_long)stktop2 & ~15); /* Align stack pointer */
104
105 /*
106 * There happens to be a callframe, too.
107 */
108 cf = (struct callframe *)stktop2;
109 cf->lr = (int)proc_trampoline;
110
111 /*
112 * Below the trap frame, there is another call frame:
113 */
114 stktop2 -= 16;
115 cf = (struct callframe *)stktop2;
116 cf->r31 = (register_t)func;
117 cf->r30 = (register_t)arg;
118
119 /*
120 * Below that, we allocate the switch frame:
121 */
122 /* must match SFRAMELEN in genassym */
123 stktop2 -= roundup(sizeof *sf, 16);
124
125 sf = (struct switchframe *)stktop2;
126 bzero((void *)sf, sizeof *sf); /* just in case */
127 sf->sp = (int)cf;
128 sf->user_sr = pmap_kernel()->pm_sr[PPC_USER_SR]; /* just in case */
129 pcb->pcb_sp = (int)stktop2;
130 }
131
132 /*
133 * cpu_exit is called as the last action during exit.
134 * We release the address space and machine-dependent resources,
135 * including the memory for the user structure and kernel stack.
136 *
137 * Since we don't have curproc anymore, we cannot sleep, and therefore
138 * this is at least incorrect for the multiprocessor version.
139 * Not sure whether we can get away with this in the single proc version. XXX
140 */
141 void
cpu_exit(struct proc * p)142 cpu_exit(struct proc *p)
143 {
144 struct cpu_info *ci = curcpu();
145 #ifdef ALTIVEC
146 struct pcb *pcb = &p->p_addr->u_pcb;
147 #endif /* ALTIVEC */
148
149 /* XXX What if the state is on the other cpu? */
150 if (p == ci->ci_fpuproc) /* release the fpu */
151 ci->ci_fpuproc = NULL;
152
153 #ifdef ALTIVEC
154 /* XXX What if the state is on the other cpu? */
155 if (p == ci->ci_vecproc)
156 ci->ci_vecproc = NULL; /* release the Altivec Unit */
157 if (pcb->pcb_vr != NULL)
158 pool_put(&ppc_vecpl, pcb->pcb_vr);
159 #endif /* ALTIVEC */
160
161 pmap_deactivate(p);
162 sched_exit(p);
163 }
164
165 struct kmem_va_mode kv_physwait = {
166 .kv_map = &phys_map,
167 .kv_wait = 1,
168 };
169
170 /*
171 * Map a user I/O request into kernel virtual address space.
172 * Note: the pages are already locked by uvm_vslock(), so we
173 * do not need to pass an access_type to pmap_enter().
174 */
175 void
vmapbuf(struct buf * bp,vsize_t len)176 vmapbuf(struct buf *bp, vsize_t len)
177 {
178 vaddr_t faddr, taddr, off;
179 paddr_t fpa;
180
181 if ((bp->b_flags & B_PHYS) == 0)
182 panic("vmapbuf");
183 faddr = trunc_page((vaddr_t)(bp->b_saveaddr = bp->b_data));
184 off = (vaddr_t)bp->b_data - faddr;
185 len = round_page(off + len);
186 taddr = (vaddr_t)km_alloc(len, &kv_physwait, &kp_none, &kd_waitok);
187 bp->b_data = (caddr_t)(taddr + off);
188 /*
189 * The region is locked, so we expect that pmap_pte() will return
190 * non-NULL.
191 * XXX: unwise to expect this in a multithreaded environment.
192 * anything can happen to a pmap between the time we lock a
193 * region, release the pmap lock, and then relock it for
194 * the pmap_extract().
195 *
196 * no need to flush TLB since we expect nothing to be mapped
197 * where we just allocated (TLB will be flushed when our
198 * mapping is removed).
199 */
200 while (len) {
201 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
202 faddr, &fpa);
203 pmap_kenter_pa(taddr, fpa, PROT_READ | PROT_WRITE);
204 faddr += PAGE_SIZE;
205 taddr += PAGE_SIZE;
206 len -= PAGE_SIZE;
207 }
208 pmap_update(pmap_kernel());
209 }
210
211 /*
212 * Unmap a previously-mapped user I/O request.
213 */
214 void
vunmapbuf(struct buf * bp,vsize_t len)215 vunmapbuf(struct buf *bp, vsize_t len)
216 {
217 vaddr_t addr, off;
218
219 if ((bp->b_flags & B_PHYS) == 0)
220 panic("vunmapbuf");
221 addr = trunc_page((vaddr_t)bp->b_data);
222 off = (vaddr_t)bp->b_data - addr;
223 len = round_page(off + len);
224 pmap_kremove(addr, len);
225 pmap_update(pmap_kernel());
226 km_free((void *)addr, len, &kv_physwait, &kp_none);
227 bp->b_data = bp->b_saveaddr;
228 bp->b_saveaddr = NULL;
229 }
230