trap.c revision 1.53 1 1.53 rmind /* $NetBSD: trap.c,v 1.53 2009/11/21 17:40:29 rmind Exp $ */
2 1.1 simonb
3 1.1 simonb /*
4 1.1 simonb * Copyright 2001 Wasabi Systems, Inc.
5 1.1 simonb * All rights reserved.
6 1.1 simonb *
7 1.1 simonb * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 1.1 simonb *
9 1.1 simonb * Redistribution and use in source and binary forms, with or without
10 1.1 simonb * modification, are permitted provided that the following conditions
11 1.1 simonb * are met:
12 1.1 simonb * 1. Redistributions of source code must retain the above copyright
13 1.1 simonb * notice, this list of conditions and the following disclaimer.
14 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 simonb * notice, this list of conditions and the following disclaimer in the
16 1.1 simonb * documentation and/or other materials provided with the distribution.
17 1.1 simonb * 3. All advertising materials mentioning features or use of this software
18 1.1 simonb * must display the following acknowledgement:
19 1.1 simonb * This product includes software developed for the NetBSD Project by
20 1.1 simonb * Wasabi Systems, Inc.
21 1.1 simonb * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 simonb * or promote products derived from this software without specific prior
23 1.1 simonb * written permission.
24 1.1 simonb *
25 1.1 simonb * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 simonb * POSSIBILITY OF SUCH DAMAGE.
36 1.1 simonb */
37 1.1 simonb
38 1.1 simonb /*
39 1.1 simonb * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 1.1 simonb * Copyright (C) 1995, 1996 TooLs GmbH.
41 1.1 simonb * All rights reserved.
42 1.1 simonb *
43 1.1 simonb * Redistribution and use in source and binary forms, with or without
44 1.1 simonb * modification, are permitted provided that the following conditions
45 1.1 simonb * are met:
46 1.1 simonb * 1. Redistributions of source code must retain the above copyright
47 1.1 simonb * notice, this list of conditions and the following disclaimer.
48 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 simonb * notice, this list of conditions and the following disclaimer in the
50 1.1 simonb * documentation and/or other materials provided with the distribution.
51 1.1 simonb * 3. All advertising materials mentioning features or use of this software
52 1.1 simonb * must display the following acknowledgement:
53 1.1 simonb * This product includes software developed by TooLs GmbH.
54 1.1 simonb * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 1.1 simonb * derived from this software without specific prior written permission.
56 1.1 simonb *
57 1.1 simonb * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 1.1 simonb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 1.1 simonb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 1.1 simonb * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 1.1 simonb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 1.1 simonb * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 1.1 simonb * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 1.1 simonb * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 1.1 simonb * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 1.1 simonb * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 1.1 simonb */
68 1.14 lukem
69 1.14 lukem #include <sys/cdefs.h>
70 1.53 rmind __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.53 2009/11/21 17:40:29 rmind Exp $");
71 1.1 simonb
72 1.1 simonb #include "opt_altivec.h"
73 1.1 simonb #include "opt_ddb.h"
74 1.44 garbled #include "opt_kgdb.h"
75 1.1 simonb
76 1.1 simonb #include <sys/param.h>
77 1.1 simonb #include <sys/proc.h>
78 1.1 simonb #include <sys/reboot.h>
79 1.1 simonb #include <sys/syscall.h>
80 1.1 simonb #include <sys/systm.h>
81 1.10 thorpej #include <sys/pool.h>
82 1.49 wrstuden #include <sys/sa.h>
83 1.49 wrstuden #include <sys/savar.h>
84 1.20 cl #include <sys/userret.h>
85 1.34 yamt #include <sys/kauth.h>
86 1.1 simonb
87 1.44 garbled #if defined(KGDB)
88 1.44 garbled #include <sys/kgdb.h>
89 1.44 garbled #endif
90 1.44 garbled
91 1.1 simonb #include <uvm/uvm_extern.h>
92 1.1 simonb
93 1.1 simonb #include <dev/cons.h>
94 1.1 simonb
95 1.1 simonb #include <machine/cpu.h>
96 1.1 simonb #include <machine/db_machdep.h>
97 1.1 simonb #include <machine/fpu.h>
98 1.1 simonb #include <machine/frame.h>
99 1.1 simonb #include <machine/pcb.h>
100 1.1 simonb #include <machine/psl.h>
101 1.1 simonb #include <machine/trap.h>
102 1.1 simonb
103 1.1 simonb #include <powerpc/spr.h>
104 1.1 simonb #include <powerpc/ibm4xx/pmap.h>
105 1.1 simonb #include <powerpc/ibm4xx/tlb.h>
106 1.1 simonb #include <powerpc/fpu/fpu_extern.h>
107 1.1 simonb
108 1.1 simonb /* These definitions should probably be somewhere else XXX */
109 1.1 simonb #define FIRSTARG 3 /* first argument is in reg 3 */
110 1.1 simonb #define NARGREG 8 /* 8 args are in registers */
111 1.40 christos #define MOREARGS(sp) ((void *)((int)(sp) + 8)) /* more args go here */
112 1.1 simonb
113 1.50 dsl static int fix_unaligned(struct lwp *l, struct trapframe *frame);
114 1.1 simonb
115 1.50 dsl void trap(struct trapframe *); /* Called from locore / trap_subr */
116 1.1 simonb /* Why are these not defined in a header? */
117 1.50 dsl int badaddr(void *, size_t);
118 1.50 dsl int badaddr_read(void *, size_t, int *);
119 1.50 dsl int ctx_setup(int, int);
120 1.1 simonb
121 1.1 simonb #ifdef DEBUG
122 1.1 simonb #define TDB_ALL 0x1
123 1.1 simonb int trapdebug = /* TDB_ALL */ 0;
124 1.1 simonb #define DBPRINTF(x, y) if (trapdebug & (x)) printf y
125 1.1 simonb #else
126 1.1 simonb #define DBPRINTF(x, y)
127 1.1 simonb #endif
128 1.1 simonb
129 1.1 simonb void
130 1.1 simonb trap(struct trapframe *frame)
131 1.1 simonb {
132 1.10 thorpej struct lwp *l = curlwp;
133 1.10 thorpej struct proc *p = l ? l->l_proc : NULL;
134 1.53 rmind struct pcb *pcb;
135 1.1 simonb int type = frame->exc;
136 1.1 simonb int ftype, rv;
137 1.18 eeh ksiginfo_t ksi;
138 1.1 simonb
139 1.53 rmind KASSERT(l == NULL || (l->l_stat == LSONPROC));
140 1.1 simonb
141 1.35 ad if (frame->srr1 & PSL_PR) {
142 1.35 ad LWP_CACHE_CREDS(l, p);
143 1.1 simonb type |= EXC_USER;
144 1.35 ad }
145 1.1 simonb
146 1.1 simonb ftype = VM_PROT_READ;
147 1.1 simonb
148 1.13 simonb DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
149 1.13 simonb type, frame->srr0, frame, &frame));
150 1.1 simonb
151 1.1 simonb switch (type) {
152 1.1 simonb case EXC_DEBUG|EXC_USER:
153 1.13 simonb {
154 1.13 simonb int srr2, srr3;
155 1.13 simonb
156 1.28 perry __asm volatile("mfspr %0,0x3f0" :
157 1.13 simonb "=r" (rv), "=r" (srr2), "=r" (srr3) :);
158 1.13 simonb printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2,
159 1.13 simonb srr3);
160 1.13 simonb /* XXX fall through or break here?! */
161 1.13 simonb }
162 1.1 simonb /*
163 1.1 simonb * DEBUG intr -- probably single-step.
164 1.1 simonb */
165 1.1 simonb case EXC_TRC|EXC_USER:
166 1.17 matt frame->srr1 &= ~PSL_SE;
167 1.19 thorpej KSI_INIT_TRAP(&ksi);
168 1.17 matt ksi.ksi_signo = SIGTRAP;
169 1.17 matt ksi.ksi_trap = EXC_TRC;
170 1.17 matt ksi.ksi_addr = (void *)frame->srr0;
171 1.17 matt trapsignal(l, &ksi);
172 1.1 simonb break;
173 1.7 simonb
174 1.13 simonb /*
175 1.13 simonb * If we could not find and install appropriate TLB entry, fall through.
176 1.13 simonb */
177 1.7 simonb
178 1.1 simonb case EXC_DSI:
179 1.1 simonb /* FALLTHROUGH */
180 1.1 simonb case EXC_DTMISS:
181 1.1 simonb {
182 1.1 simonb struct vm_map *map;
183 1.1 simonb vaddr_t va;
184 1.11 matt struct faultbuf *fb = NULL;
185 1.1 simonb
186 1.11 matt va = frame->dar;
187 1.11 matt if (frame->tf_xtra[TF_PID] == KERNEL_PID) {
188 1.1 simonb map = kernel_map;
189 1.1 simonb } else {
190 1.1 simonb map = &p->p_vmspace->vm_map;
191 1.49 wrstuden if ((l->l_flag & LW_SA)
192 1.49 wrstuden && (~l->l_pflag & LP_SA_NOBLOCK)) {
193 1.49 wrstuden l->l_savp->savp_faultaddr = va;
194 1.49 wrstuden l->l_pflag |= LP_SA_PAGEFAULT;
195 1.49 wrstuden }
196 1.1 simonb }
197 1.1 simonb
198 1.11 matt if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
199 1.3 chs ftype = VM_PROT_WRITE;
200 1.1 simonb
201 1.13 simonb DBPRINTF(TDB_ALL,
202 1.13 simonb ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
203 1.13 simonb frame->srr0,
204 1.13 simonb (ftype & VM_PROT_WRITE) ? "write" : "read",
205 1.13 simonb (void *)va, frame->tf_xtra[TF_ESR]));
206 1.32 drochner rv = uvm_fault(map, trunc_page(va), ftype);
207 1.49 wrstuden if (map != kernel_map) {
208 1.49 wrstuden l->l_pflag &= ~LP_SA_PAGEFAULT;
209 1.49 wrstuden }
210 1.1 simonb if (rv == 0)
211 1.1 simonb goto done;
212 1.53 rmind pcb = lwp_getpcb(l);
213 1.53 rmind if ((fb = pcb->pcb_onfault) != NULL) {
214 1.11 matt frame->tf_xtra[TF_PID] = KERNEL_PID;
215 1.11 matt frame->srr0 = fb->fb_pc;
216 1.1 simonb frame->srr1 |= PSL_IR; /* Re-enable IMMU */
217 1.11 matt frame->fixreg[1] = fb->fb_sp;
218 1.11 matt frame->fixreg[2] = fb->fb_r2;
219 1.1 simonb frame->fixreg[3] = 1; /* Return TRUE */
220 1.11 matt frame->cr = fb->fb_cr;
221 1.11 matt memcpy(&frame->fixreg[13], fb->fb_fixreg,
222 1.11 matt sizeof(fb->fb_fixreg));
223 1.1 simonb goto done;
224 1.1 simonb }
225 1.1 simonb }
226 1.1 simonb goto brain_damage;
227 1.7 simonb
228 1.1 simonb case EXC_DSI|EXC_USER:
229 1.1 simonb /* FALLTHROUGH */
230 1.1 simonb case EXC_DTMISS|EXC_USER:
231 1.11 matt if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
232 1.3 chs ftype = VM_PROT_WRITE;
233 1.1 simonb
234 1.13 simonb DBPRINTF(TDB_ALL,
235 1.13 simonb ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
236 1.13 simonb frame->srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
237 1.13 simonb frame->dar, frame->tf_xtra[TF_ESR]));
238 1.13 simonb KASSERT(l == curlwp && (l->l_stat == LSONPROC));
239 1.49 wrstuden if (l->l_flag & LW_SA) {
240 1.49 wrstuden l->l_savp->savp_faultaddr = (vaddr_t)frame->dar;
241 1.49 wrstuden l->l_pflag |= LP_SA_PAGEFAULT;
242 1.49 wrstuden }
243 1.13 simonb rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->dar),
244 1.32 drochner ftype);
245 1.1 simonb if (rv == 0) {
246 1.49 wrstuden l->l_pflag &= ~LP_SA_PAGEFAULT;
247 1.13 simonb break;
248 1.1 simonb }
249 1.19 thorpej KSI_INIT_TRAP(&ksi);
250 1.17 matt ksi.ksi_signo = SIGSEGV;
251 1.17 matt ksi.ksi_trap = EXC_DSI;
252 1.17 matt ksi.ksi_addr = (void *)frame->dar;
253 1.1 simonb if (rv == ENOMEM) {
254 1.10 thorpej printf("UVM: pid %d (%s) lid %d, uid %d killed: "
255 1.13 simonb "out of swap\n",
256 1.13 simonb p->p_pid, p->p_comm, l->l_lid,
257 1.36 ad l->l_cred ?
258 1.36 ad kauth_cred_geteuid(l->l_cred) : -1);
259 1.17 matt ksi.ksi_signo = SIGKILL;
260 1.1 simonb }
261 1.17 matt trapsignal(l, &ksi);
262 1.49 wrstuden l->l_pflag &= ~LP_SA_PAGEFAULT;
263 1.1 simonb break;
264 1.15 chs
265 1.1 simonb case EXC_ITMISS|EXC_USER:
266 1.1 simonb case EXC_ISI|EXC_USER:
267 1.49 wrstuden if (l->l_flag & LW_SA) {
268 1.49 wrstuden l->l_savp->savp_faultaddr = (vaddr_t)frame->srr0;
269 1.49 wrstuden l->l_pflag |= LP_SA_PAGEFAULT;
270 1.49 wrstuden }
271 1.15 chs ftype = VM_PROT_EXECUTE;
272 1.13 simonb DBPRINTF(TDB_ALL,
273 1.15 chs ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
274 1.13 simonb frame->srr0, frame));
275 1.13 simonb rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0),
276 1.32 drochner ftype);
277 1.1 simonb if (rv == 0) {
278 1.49 wrstuden l->l_pflag &= ~LP_SA_PAGEFAULT;
279 1.13 simonb break;
280 1.1 simonb }
281 1.19 thorpej KSI_INIT_TRAP(&ksi);
282 1.17 matt ksi.ksi_signo = SIGSEGV;
283 1.17 matt ksi.ksi_trap = EXC_ISI;
284 1.17 matt ksi.ksi_addr = (void *)frame->srr0;
285 1.21 drochner ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
286 1.17 matt trapsignal(l, &ksi);
287 1.49 wrstuden l->l_pflag &= ~LP_SA_PAGEFAULT;
288 1.1 simonb break;
289 1.1 simonb
290 1.1 simonb case EXC_AST|EXC_USER:
291 1.11 matt curcpu()->ci_astpending = 0; /* we are about to do it */
292 1.1 simonb uvmexp.softs++;
293 1.39 ad if (l->l_pflag & LP_OWEUPC) {
294 1.39 ad l->l_pflag &= ~LP_OWEUPC;
295 1.39 ad ADDUPROF(l);
296 1.1 simonb }
297 1.1 simonb /* Check whether we are being preempted. */
298 1.42 rjs if (curcpu()->ci_want_resched)
299 1.38 ad preempt();
300 1.1 simonb break;
301 1.1 simonb
302 1.1 simonb
303 1.1 simonb case EXC_ALI|EXC_USER:
304 1.17 matt if (fix_unaligned(l, frame) != 0) {
305 1.19 thorpej KSI_INIT_TRAP(&ksi);
306 1.17 matt ksi.ksi_signo = SIGBUS;
307 1.17 matt ksi.ksi_trap = EXC_ALI;
308 1.17 matt ksi.ksi_addr = (void *)frame->dar;
309 1.17 matt trapsignal(l, &ksi);
310 1.17 matt } else
311 1.1 simonb frame->srr0 += 4;
312 1.1 simonb break;
313 1.1 simonb
314 1.1 simonb case EXC_PGM|EXC_USER:
315 1.7 simonb /*
316 1.7 simonb * Illegal insn:
317 1.1 simonb *
318 1.7 simonb * let's try to see if it's FPU and can be emulated.
319 1.1 simonb */
320 1.24 simonb uvmexp.traps++;
321 1.53 rmind pcb = lwp_getpcb(l);
322 1.53 rmind
323 1.53 rmind if (!(pcb->pcb_flags & PCB_FPU)) {
324 1.53 rmind memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
325 1.53 rmind pcb->pcb_flags |= PCB_FPU;
326 1.1 simonb }
327 1.1 simonb
328 1.53 rmind if ((rv = fpu_emulate(frame, (struct fpreg *)&pcb->pcb_fpu))) {
329 1.19 thorpej KSI_INIT_TRAP(&ksi);
330 1.17 matt ksi.ksi_signo = rv;
331 1.17 matt ksi.ksi_trap = EXC_PGM;
332 1.17 matt ksi.ksi_addr = (void *)frame->srr0;
333 1.17 matt trapsignal(l, &ksi);
334 1.1 simonb }
335 1.1 simonb break;
336 1.1 simonb
337 1.1 simonb case EXC_MCHK:
338 1.1 simonb {
339 1.11 matt struct faultbuf *fb;
340 1.1 simonb
341 1.53 rmind pcb = lwp_getpcb(l);
342 1.53 rmind if ((fb = pcb->pcb_onfault) != NULL) {
343 1.11 matt frame->tf_xtra[TF_PID] = KERNEL_PID;
344 1.11 matt frame->srr0 = fb->fb_pc;
345 1.1 simonb frame->srr1 |= PSL_IR; /* Re-enable IMMU */
346 1.11 matt frame->fixreg[1] = fb->fb_sp;
347 1.11 matt frame->fixreg[2] = fb->fb_r2;
348 1.1 simonb frame->fixreg[3] = 1; /* Return TRUE */
349 1.11 matt frame->cr = fb->fb_cr;
350 1.11 matt memcpy(&frame->fixreg[13], fb->fb_fixreg,
351 1.11 matt sizeof(fb->fb_fixreg));
352 1.1 simonb goto done;
353 1.1 simonb }
354 1.1 simonb }
355 1.1 simonb goto brain_damage;
356 1.1 simonb default:
357 1.13 simonb brain_damage:
358 1.11 matt printf("trap type 0x%x at 0x%lx\n", type, frame->srr0);
359 1.44 garbled #if defined(DDB) || defined(KGDB)
360 1.1 simonb if (kdb_trap(type, frame))
361 1.1 simonb goto done;
362 1.1 simonb #endif
363 1.1 simonb #ifdef TRAP_PANICWAIT
364 1.1 simonb printf("Press a key to panic.\n");
365 1.1 simonb cngetc();
366 1.1 simonb #endif
367 1.1 simonb panic("trap");
368 1.1 simonb }
369 1.1 simonb
370 1.20 cl /* Invoke MI userret code */
371 1.20 cl mi_userret(l);
372 1.13 simonb done:
373 1.9 thorpej return;
374 1.1 simonb }
375 1.1 simonb
376 1.1 simonb int
377 1.1 simonb ctx_setup(int ctx, int srr1)
378 1.1 simonb {
379 1.1 simonb volatile struct pmap *pm;
380 1.1 simonb
381 1.1 simonb /* Update PID if we're returning to user mode. */
382 1.1 simonb if (srr1 & PSL_PR) {
383 1.1 simonb pm = curproc->p_vmspace->vm_map.pmap;
384 1.1 simonb if (!pm->pm_ctx) {
385 1.26 scw ctx_alloc(__UNVOLATILE(pm));
386 1.1 simonb }
387 1.1 simonb ctx = pm->pm_ctx;
388 1.1 simonb if (srr1 & PSL_SE) {
389 1.1 simonb int dbreg, mask = 0x48000000;
390 1.1 simonb /*
391 1.1 simonb * Set the Internal Debug and
392 1.1 simonb * Instruction Completion bits of
393 1.1 simonb * the DBCR0 register.
394 1.1 simonb *
395 1.1 simonb * XXX this is also used by jtag debuggers...
396 1.1 simonb */
397 1.28 perry __asm volatile("mfspr %0,0x3f2;"
398 1.13 simonb "or %0,%0,%1;"
399 1.13 simonb "mtspr 0x3f2,%0;" :
400 1.13 simonb "=&r" (dbreg) : "r" (mask));
401 1.1 simonb }
402 1.1 simonb }
403 1.1 simonb else if (!ctx) {
404 1.1 simonb ctx = KERNEL_PID;
405 1.1 simonb }
406 1.1 simonb return (ctx);
407 1.1 simonb }
408 1.1 simonb
409 1.1 simonb /*
410 1.1 simonb * Used by copyin()/copyout()
411 1.1 simonb */
412 1.50 dsl extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
413 1.50 dsl extern void vunmaprange(vaddr_t, vsize_t);
414 1.50 dsl static int bigcopyin(const void *, void *, size_t );
415 1.50 dsl static int bigcopyout(const void *, void *, size_t );
416 1.1 simonb
417 1.1 simonb int
418 1.1 simonb copyin(const void *udaddr, void *kaddr, size_t len)
419 1.1 simonb {
420 1.1 simonb struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
421 1.46 hpeyerl int msr, pid, tmp, ctx, count=0;
422 1.11 matt struct faultbuf env;
423 1.1 simonb
424 1.1 simonb /* For bigger buffers use the faster copy */
425 1.46 hpeyerl if (len > 1024)
426 1.25 simonb return (bigcopyin(udaddr, kaddr, len));
427 1.1 simonb
428 1.11 matt if (setfault(&env)) {
429 1.1 simonb curpcb->pcb_onfault = 0;
430 1.1 simonb return EFAULT;
431 1.1 simonb }
432 1.1 simonb
433 1.1 simonb if (!(ctx = pm->pm_ctx)) {
434 1.1 simonb /* No context -- assign it one */
435 1.1 simonb ctx_alloc(pm);
436 1.1 simonb ctx = pm->pm_ctx;
437 1.1 simonb }
438 1.1 simonb
439 1.46 hpeyerl __asm volatile(
440 1.46 hpeyerl " mfmsr %[msr];" /* Save MSR */
441 1.46 hpeyerl " li %[pid],0x20; "
442 1.46 hpeyerl " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */
443 1.46 hpeyerl " mfpid %[pid];" /* Save old PID */
444 1.46 hpeyerl " sync; isync;"
445 1.46 hpeyerl
446 1.46 hpeyerl " srwi. %[count],%[len],0x2;" /* How many words? */
447 1.46 hpeyerl " beq- 2f;" /* No words. Go do bytes */
448 1.46 hpeyerl " mtctr %[count];"
449 1.46 hpeyerl "1: mtpid %[ctx]; sync;"
450 1.47 simonb " lswi %[tmp],%[udaddr],4;" /* Load user word */
451 1.46 hpeyerl " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */
452 1.46 hpeyerl " sync; isync;"
453 1.46 hpeyerl " mtpid %[pid];sync;"
454 1.47 simonb " stswi %[tmp],%[kaddr],4;" /* Store kernel word */
455 1.46 hpeyerl " dcbf 0,%[kaddr];" /* flush cache */
456 1.46 hpeyerl " addi %[kaddr],%[kaddr],0x4;" /* next udaddr word */
457 1.46 hpeyerl " sync; isync;"
458 1.46 hpeyerl " bdnz 1b;" /* repeat */
459 1.46 hpeyerl
460 1.46 hpeyerl "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */
461 1.46 hpeyerl " addi %[count],%[count],0x1;"
462 1.46 hpeyerl " mtctr %[count];"
463 1.46 hpeyerl "3: bdz 10f;" /* while count */
464 1.46 hpeyerl " mtpid %[ctx];sync;"
465 1.46 hpeyerl " lbz %[tmp],0(%[udaddr]);" /* Load user byte */
466 1.46 hpeyerl " addi %[udaddr],%[udaddr],0x1;" /* next udaddr byte */
467 1.46 hpeyerl " sync; isync;"
468 1.46 hpeyerl " mtpid %[pid]; sync;"
469 1.46 hpeyerl " stb %[tmp],0(%[kaddr]);" /* Store kernel byte */
470 1.46 hpeyerl " dcbf 0,%[kaddr];" /* flush cache */
471 1.46 hpeyerl " addi %[kaddr],%[kaddr],0x1;"
472 1.46 hpeyerl " sync; isync;"
473 1.46 hpeyerl " b 3b;"
474 1.46 hpeyerl "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
475 1.46 hpeyerl : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
476 1.46 hpeyerl : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
477 1.1 simonb
478 1.1 simonb curpcb->pcb_onfault = 0;
479 1.1 simonb return 0;
480 1.1 simonb }
481 1.1 simonb
482 1.1 simonb static int
483 1.1 simonb bigcopyin(const void *udaddr, void *kaddr, size_t len)
484 1.1 simonb {
485 1.1 simonb const char *up;
486 1.1 simonb char *kp = kaddr;
487 1.10 thorpej struct lwp *l = curlwp;
488 1.10 thorpej struct proc *p;
489 1.1 simonb int error;
490 1.1 simonb
491 1.10 thorpej if (!l) {
492 1.1 simonb return EFAULT;
493 1.1 simonb }
494 1.1 simonb
495 1.10 thorpej p = l->l_proc;
496 1.10 thorpej
497 1.1 simonb /*
498 1.7 simonb * Stolen from physio():
499 1.1 simonb */
500 1.37 chs error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
501 1.1 simonb if (error) {
502 1.1 simonb return EFAULT;
503 1.1 simonb }
504 1.1 simonb up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
505 1.1 simonb
506 1.2 wiz memcpy(kp, up, len);
507 1.1 simonb vunmaprange((vaddr_t)up, len);
508 1.37 chs uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
509 1.1 simonb
510 1.7 simonb return 0;
511 1.1 simonb }
512 1.1 simonb
513 1.1 simonb int
514 1.1 simonb copyout(const void *kaddr, void *udaddr, size_t len)
515 1.1 simonb {
516 1.1 simonb struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
517 1.46 hpeyerl int msr, pid, tmp, ctx, count=0;
518 1.11 matt struct faultbuf env;
519 1.1 simonb
520 1.1 simonb /* For big copies use more efficient routine */
521 1.46 hpeyerl if (len > 1024)
522 1.25 simonb return (bigcopyout(kaddr, udaddr, len));
523 1.1 simonb
524 1.11 matt if (setfault(&env)) {
525 1.1 simonb curpcb->pcb_onfault = 0;
526 1.1 simonb return EFAULT;
527 1.1 simonb }
528 1.1 simonb
529 1.1 simonb if (!(ctx = pm->pm_ctx)) {
530 1.1 simonb /* No context -- assign it one */
531 1.1 simonb ctx_alloc(pm);
532 1.1 simonb ctx = pm->pm_ctx;
533 1.1 simonb }
534 1.1 simonb
535 1.46 hpeyerl __asm volatile(
536 1.46 hpeyerl " mfmsr %[msr];" /* Save MSR */ \
537 1.46 hpeyerl " li %[pid],0x20; " \
538 1.46 hpeyerl " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */ \
539 1.46 hpeyerl " mfpid %[pid];" /* Save old PID */ \
540 1.46 hpeyerl " sync; isync;"
541 1.46 hpeyerl
542 1.46 hpeyerl " srwi. %[count],%[len],0x2;" /* How many words? */
543 1.46 hpeyerl " beq- 2f;" /* No words. Go do bytes */
544 1.46 hpeyerl " mtctr %[count];"
545 1.46 hpeyerl "1: mtpid %[pid];sync;"
546 1.47 simonb " lswi %[tmp],%[kaddr],4;" /* Load kernel word */
547 1.46 hpeyerl " addi %[kaddr],%[kaddr],0x4;" /* next kaddr word */
548 1.46 hpeyerl " sync; isync;"
549 1.46 hpeyerl " mtpid %[ctx]; sync;"
550 1.47 simonb " stswi %[tmp],%[udaddr],4;" /* Store user word */
551 1.46 hpeyerl " dcbf 0,%[udaddr];" /* flush cache */
552 1.46 hpeyerl " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */
553 1.46 hpeyerl " sync; isync;"
554 1.46 hpeyerl " bdnz 1b;" /* repeat */
555 1.46 hpeyerl
556 1.46 hpeyerl "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */
557 1.46 hpeyerl " addi %[count],%[count],0x1;"
558 1.46 hpeyerl " mtctr %[count];"
559 1.46 hpeyerl "3: bdz 10f;" /* while count */
560 1.46 hpeyerl " mtpid %[pid];sync;"
561 1.46 hpeyerl " lbz %[tmp],0(%[kaddr]);" /* Load kernel byte */
562 1.46 hpeyerl " addi %[kaddr],%[kaddr],0x1;" /* next kaddr byte */
563 1.46 hpeyerl " sync; isync;"
564 1.46 hpeyerl " mtpid %[ctx]; sync;"
565 1.46 hpeyerl " stb %[tmp],0(%[udaddr]);" /* Store user byte */
566 1.46 hpeyerl " dcbf 0,%[udaddr];" /* flush cache */
567 1.46 hpeyerl " addi %[udaddr],%[udaddr],0x1;"
568 1.46 hpeyerl " sync; isync;"
569 1.46 hpeyerl " b 3b;"
570 1.46 hpeyerl "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
571 1.46 hpeyerl : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
572 1.46 hpeyerl : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
573 1.1 simonb
574 1.1 simonb curpcb->pcb_onfault = 0;
575 1.1 simonb return 0;
576 1.1 simonb }
577 1.1 simonb
578 1.1 simonb static int
579 1.1 simonb bigcopyout(const void *kaddr, void *udaddr, size_t len)
580 1.1 simonb {
581 1.1 simonb char *up;
582 1.26 scw const char *kp = (const char *)kaddr;
583 1.10 thorpej struct lwp *l = curlwp;
584 1.10 thorpej struct proc *p;
585 1.1 simonb int error;
586 1.1 simonb
587 1.10 thorpej if (!l) {
588 1.1 simonb return EFAULT;
589 1.1 simonb }
590 1.1 simonb
591 1.10 thorpej p = l->l_proc;
592 1.10 thorpej
593 1.1 simonb /*
594 1.7 simonb * Stolen from physio():
595 1.1 simonb */
596 1.37 chs error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
597 1.1 simonb if (error) {
598 1.1 simonb return EFAULT;
599 1.1 simonb }
600 1.7 simonb up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
601 1.13 simonb VM_PROT_READ | VM_PROT_WRITE);
602 1.1 simonb
603 1.2 wiz memcpy(up, kp, len);
604 1.1 simonb vunmaprange((vaddr_t)up, len);
605 1.37 chs uvm_vsunlock(p->p_vmspace, udaddr, len);
606 1.1 simonb
607 1.7 simonb return 0;
608 1.1 simonb }
609 1.1 simonb
610 1.1 simonb /*
611 1.1 simonb * kcopy(const void *src, void *dst, size_t len);
612 1.1 simonb *
613 1.1 simonb * Copy len bytes from src to dst, aborting if we encounter a fatal
614 1.1 simonb * page fault.
615 1.1 simonb *
616 1.1 simonb * kcopy() _must_ save and restore the old fault handler since it is
617 1.1 simonb * called by uiomove(), which may be in the path of servicing a non-fatal
618 1.1 simonb * page fault.
619 1.1 simonb */
620 1.1 simonb int
621 1.1 simonb kcopy(const void *src, void *dst, size_t len)
622 1.1 simonb {
623 1.11 matt struct faultbuf env, *oldfault;
624 1.1 simonb
625 1.1 simonb oldfault = curpcb->pcb_onfault;
626 1.11 matt if (setfault(&env)) {
627 1.1 simonb curpcb->pcb_onfault = oldfault;
628 1.1 simonb return EFAULT;
629 1.1 simonb }
630 1.1 simonb
631 1.2 wiz memcpy(dst, src, len);
632 1.1 simonb
633 1.1 simonb curpcb->pcb_onfault = oldfault;
634 1.1 simonb return 0;
635 1.1 simonb }
636 1.1 simonb
637 1.1 simonb int
638 1.1 simonb badaddr(void *addr, size_t size)
639 1.1 simonb {
640 1.1 simonb
641 1.1 simonb return badaddr_read(addr, size, NULL);
642 1.1 simonb }
643 1.1 simonb
644 1.1 simonb int
645 1.1 simonb badaddr_read(void *addr, size_t size, int *rptr)
646 1.1 simonb {
647 1.11 matt struct faultbuf env;
648 1.1 simonb int x;
649 1.1 simonb
650 1.1 simonb /* Get rid of any stale machine checks that have been waiting. */
651 1.28 perry __asm volatile ("sync; isync");
652 1.1 simonb
653 1.11 matt if (setfault(&env)) {
654 1.1 simonb curpcb->pcb_onfault = 0;
655 1.28 perry __asm volatile ("sync");
656 1.1 simonb return 1;
657 1.1 simonb }
658 1.1 simonb
659 1.28 perry __asm volatile ("sync");
660 1.1 simonb
661 1.1 simonb switch (size) {
662 1.1 simonb case 1:
663 1.1 simonb x = *(volatile int8_t *)addr;
664 1.1 simonb break;
665 1.1 simonb case 2:
666 1.1 simonb x = *(volatile int16_t *)addr;
667 1.1 simonb break;
668 1.1 simonb case 4:
669 1.1 simonb x = *(volatile int32_t *)addr;
670 1.1 simonb break;
671 1.1 simonb default:
672 1.1 simonb panic("badaddr: invalid size (%d)", size);
673 1.1 simonb }
674 1.1 simonb
675 1.1 simonb /* Make sure we took the machine check, if we caused one. */
676 1.28 perry __asm volatile ("sync; isync");
677 1.1 simonb
678 1.1 simonb curpcb->pcb_onfault = 0;
679 1.28 perry __asm volatile ("sync"); /* To be sure. */
680 1.1 simonb
681 1.1 simonb /* Use the value to avoid reorder. */
682 1.1 simonb if (rptr)
683 1.1 simonb *rptr = x;
684 1.1 simonb
685 1.1 simonb return 0;
686 1.1 simonb }
687 1.1 simonb
688 1.1 simonb /*
689 1.1 simonb * For now, this only deals with the particular unaligned access case
690 1.1 simonb * that gcc tends to generate. Eventually it should handle all of the
691 1.1 simonb * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
692 1.1 simonb */
693 1.1 simonb
694 1.1 simonb static int
695 1.10 thorpej fix_unaligned(struct lwp *l, struct trapframe *frame)
696 1.1 simonb {
697 1.1 simonb
698 1.1 simonb return -1;
699 1.10 thorpej }
700 1.10 thorpej
701 1.10 thorpej /*
702 1.10 thorpej * Start a new LWP
703 1.10 thorpej */
704 1.10 thorpej void
705 1.51 dsl startlwp(void *arg)
706 1.10 thorpej {
707 1.10 thorpej int err;
708 1.10 thorpej ucontext_t *uc = arg;
709 1.10 thorpej struct lwp *l = curlwp;
710 1.10 thorpej
711 1.10 thorpej err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
712 1.10 thorpej #if DIAGNOSTIC
713 1.10 thorpej if (err) {
714 1.10 thorpej printf("Error %d from cpu_setmcontext.", err);
715 1.10 thorpej }
716 1.10 thorpej #endif
717 1.10 thorpej pool_put(&lwp_uc_pool, uc);
718 1.10 thorpej
719 1.49 wrstuden upcallret(l);
720 1.49 wrstuden }
721 1.49 wrstuden
722 1.49 wrstuden /*
723 1.49 wrstuden * XXX This is a terrible name.
724 1.49 wrstuden */
725 1.49 wrstuden void
726 1.51 dsl upcallret(struct lwp *l)
727 1.49 wrstuden {
728 1.49 wrstuden
729 1.20 cl /* Invoke MI userret code */
730 1.20 cl mi_userret(l);
731 1.1 simonb }
732