trap.c revision 1.38 1 /* $NetBSD: trap.c,v 1.38 2006/10/09 21:32:29 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /* $OpenBSD: trap.c,v 1.30 2001/09/19 20:50:56 mickey Exp $ */
40
41 /*
42 * Copyright (c) 1998-2000 Michael Shalayeff
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Michael Shalayeff.
56 * 4. The name of the author may not be used to endorse or promote products
57 * derived from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.38 2006/10/09 21:32:29 skrll Exp $");
73
74 /* #define INTRDEBUG */
75 /* #define TRAPDEBUG */
76 /* #define USERTRACE */
77
78 #include "opt_kgdb.h"
79 #include "opt_ktrace.h"
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/syscall.h>
85 #include <sys/sa.h>
86 #include <sys/savar.h>
87 #ifdef KTRACE
88 #include <sys/ktrace.h>
89 #endif
90 #include <sys/proc.h>
91 #include <sys/signalvar.h>
92 #include <sys/user.h>
93 #include <sys/acct.h>
94 #include <sys/signal.h>
95 #include <sys/device.h>
96 #include <sys/pool.h>
97 #include <sys/userret.h>
98
99 #include <net/netisr.h>
100
101 #ifdef KGDB
102 #include <sys/kgdb.h>
103 #endif
104
105 #include <uvm/uvm.h>
106
107 #include <machine/iomod.h>
108 #include <machine/cpufunc.h>
109 #include <machine/reg.h>
110 #include <machine/autoconf.h>
111
112 #include <machine/db_machdep.h>
113
114 #include <hppa/hppa/machdep.h>
115
116 #include <ddb/db_output.h>
117 #include <ddb/db_interface.h>
118
119 #if defined(DEBUG) || defined(DIAGNOSTIC)
120 /*
121 * 0x6fc1000 is a stwm r1, d(sr0, sp), which is the last
122 * instruction in the function prologue that gcc -O0 uses.
123 * When we have this instruction we know the relationship
124 * between the stack pointer and the gcc -O0 frame pointer
125 * (in r3, loaded with the initial sp) for the body of a
126 * function.
127 *
128 * If the given instruction is a stwm r1, d(sr0, sp) where
129 * d > 0, we evaluate to d, else we evaluate to zero.
130 */
131 #define STWM_R1_D_SR0_SP(inst) \
132 (((inst) & 0xffffc001) == 0x6fc10000 ? (((inst) & 0x00003ff) >> 1) : 0)
133 #endif /* DEBUG || DIAGNOSTIC */
134
135 const char *trap_type[] = {
136 "invalid",
137 "HPMC",
138 "power failure",
139 "recovery counter",
140 "external interrupt",
141 "LPMC",
142 "ITLB miss fault",
143 "instruction protection",
144 "Illegal instruction",
145 "break instruction",
146 "privileged operation",
147 "privileged register",
148 "overflow",
149 "conditional",
150 "assist exception",
151 "DTLB miss",
152 "ITLB non-access miss",
153 "DTLB non-access miss",
154 "data protection/rights/alignment",
155 "data break",
156 "TLB dirty",
157 "page reference",
158 "assist emulation",
159 "higher-priv transfer",
160 "lower-priv transfer",
161 "taken branch",
162 "data access rights",
163 "data protection",
164 "unaligned data ref",
165 };
166 int trap_types = sizeof(trap_type)/sizeof(trap_type[0]);
167
168 uint8_t fpopmap[] = {
169 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
170 0x00, 0x0c, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177 };
178
179 int want_resched;
180 volatile int astpending;
181
182 void pmap_hptdump(void);
183 void syscall(struct trapframe *, int *);
184
185 #ifdef USERTRACE
186 /*
187 * USERTRACE is a crude facility that traces the PC of
188 * a single user process. This tracing is normally
189 * activated by the dispatching of a certain syscall
190 * with certain arguments - see the activation code in
191 * syscall().
192 */
193 u_int rctr_next_iioq;
194 #endif
195
196 static inline void
197 userret(struct lwp *l, register_t pc, u_quad_t oticks)
198 {
199 struct proc *p = l->l_proc;
200
201 l->l_priority = l->l_usrpri;
202 if (want_resched) {
203 preempt(0);
204 }
205
206 mi_userret(l);
207
208 /*
209 * If profiling, charge recent system time to the trapped pc.
210 */
211 if (p->p_flag & P_PROFIL) {
212 extern int psratio;
213
214 addupc_task(p, pc, (int)(p->p_sticks - oticks) * psratio);
215 }
216
217 curcpu()->ci_schedstate.spc_curpriority = l->l_priority;
218 }
219
220 /*
221 * This handles some messy kernel debugger details.
222 * It dispatches into either kgdb or DDB, and knows
223 * about some special things to do, like skipping over
224 * break instructions and how to really set up for
225 * a single-step.
226 */
227 #if defined(KGDB) || defined(DDB)
228 static int
229 trap_kdebug(int type, int code, struct trapframe *frame)
230 {
231 int handled;
232 u_int tf_iioq_head_old;
233 u_int tf_iioq_tail_old;
234
235 for(;;) {
236
237 /* This trap has not been handled. */
238 handled = 0;
239
240 /* Remember the instruction offset queue. */
241 tf_iioq_head_old = frame->tf_iioq_head;
242 tf_iioq_tail_old = frame->tf_iioq_tail;
243
244 #ifdef KGDB
245 /* Let KGDB handle it (if connected) */
246 if (!handled)
247 handled = kgdb_trap(type, frame);
248 #endif
249 #ifdef DDB
250 /* Let DDB handle it. */
251 if (!handled)
252 handled = kdb_trap(type, code, frame);
253 #endif
254
255 /* If this trap wasn't handled, return now. */
256 if (!handled)
257 return(0);
258
259 /*
260 * If the instruction offset queue head changed,
261 * but the offset queue tail didn't, assume that
262 * the user wants to jump to the head offset, and
263 * adjust the tail accordingly. This should fix
264 * the kgdb `jump' command, and can help DDB users
265 * who `set' the offset head but forget the tail.
266 */
267 if (frame->tf_iioq_head != tf_iioq_head_old &&
268 frame->tf_iioq_tail == tf_iioq_tail_old)
269 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
270
271 /*
272 * This is some single-stepping support.
273 * If we're trying to step through a nullified
274 * instruction, just advance by hand and trap
275 * again. Otherwise, load the recovery counter
276 * with zero.
277 */
278 if (frame->tf_ipsw & PSW_R) {
279 #ifdef TRAPDEBUG
280 printf("(single stepping at head 0x%x tail 0x%x)\n", frame->tf_iioq_head, frame->tf_iioq_tail);
281 #endif
282 if (frame->tf_ipsw & PSW_N) {
283 #ifdef TRAPDEBUG
284 printf("(single stepping past nullified)\n");
285 #endif
286
287 /* Advance the program counter. */
288 frame->tf_iioq_head = frame->tf_iioq_tail;
289 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
290
291 /* Clear flags. */
292 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
293
294 /* Simulate another trap. */
295 type = T_RECOVERY;
296 continue;
297 }
298 frame->tf_rctr = 0;
299 }
300
301 /* We handled this trap. */
302 return (1);
303 }
304 /* NOTREACHED */
305 }
306 #else /* !KGDB && !DDB */
307 #define trap_kdebug(t, c, f) (0)
308 #endif /* !KGDB && !DDB */
309
310 #if defined(DEBUG) || defined(USERTRACE)
311 /*
312 * These functions give a crude usermode backtrace. They
313 * really only work when code has been compiled without
314 * optimization, as they assume a certain function prologue
315 * sets up a frame pointer and stores the return pointer
316 * and arguments in it.
317 */
318 static void user_backtrace_raw(u_int, u_int);
319 static void
320 user_backtrace_raw(u_int pc, u_int fp)
321 {
322 int frame_number;
323 int arg_number;
324
325 for (frame_number = 0;
326 frame_number < 100 && pc > HPPA_PC_PRIV_MASK && fp;
327 frame_number++) {
328
329 printf("%3d: pc=%08x%s fp=0x%08x", frame_number,
330 pc & ~HPPA_PC_PRIV_MASK, USERMODE(pc) ? "" : "**", fp);
331 for(arg_number = 0; arg_number < 4; arg_number++)
332 printf(" arg%d=0x%08x", arg_number,
333 (int) fuword(HPPA_FRAME_CARG(arg_number, fp)));
334 printf("\n");
335 pc = fuword(((register_t *) fp) - 5); /* fetch rp */
336 if (pc == -1) {
337 printf(" fuword for pc failed\n");
338 break;
339 }
340 fp = fuword(((register_t *) fp) + 0); /* fetch previous fp */
341 if (fp == -1) {
342 printf(" fuword for fp failed\n");
343 break;
344 }
345 }
346 printf(" backtrace stopped with pc %08x fp 0x%08x\n", pc, fp);
347 }
348
349 static void user_backtrace(struct trapframe *, struct lwp *, int);
350 static void
351 user_backtrace(struct trapframe *tf, struct lwp *l, int type)
352 {
353 struct proc *p = l->l_proc;
354 u_int pc, fp, inst;
355
356 /*
357 * Display any trap type that we have.
358 */
359 if (type >= 0)
360 printf("pid %d (%s) trap #%d\n",
361 p->p_pid, p->p_comm, type & ~T_USER);
362
363 /*
364 * Assuming that the frame pointer in r3 is valid,
365 * dump out a stack trace.
366 */
367 fp = tf->tf_r3;
368 printf("pid %d (%s) backtrace, starting with fp 0x%08x\n",
369 p->p_pid, p->p_comm, fp);
370 user_backtrace_raw(tf->tf_iioq_head, fp);
371
372 /*
373 * In case the frame pointer in r3 is not valid,
374 * assuming the stack pointer is valid and the
375 * faulting function is a non-leaf, if we can
376 * find its prologue we can recover its frame
377 * pointer.
378 */
379 pc = tf->tf_iioq_head;
380 fp = tf->tf_sp - HPPA_FRAME_SIZE;
381 printf("pid %d (%s) backtrace, starting with sp 0x%08x pc 0x%08x\n",
382 p->p_pid, p->p_comm, tf->tf_sp, pc);
383 for(pc &= ~HPPA_PC_PRIV_MASK; pc > 0; pc -= sizeof(inst)) {
384 inst = fuword((register_t *) pc);
385 if (inst == -1) {
386 printf(" fuword for inst at pc %08x failed\n", pc);
387 break;
388 }
389 /* Check for the prologue instruction that sets sp. */
390 if (STWM_R1_D_SR0_SP(inst)) {
391 fp = tf->tf_sp - STWM_R1_D_SR0_SP(inst);
392 printf(" sp from fp at pc %08x: %08x\n", pc, inst);
393 break;
394 }
395 }
396 user_backtrace_raw(tf->tf_iioq_head, fp);
397 }
398 #endif /* DEBUG || USERTRACE */
399
400 #ifdef DEBUG
401 /*
402 * This sanity-checks a trapframe. It is full of various
403 * assumptions about what a healthy CPU state should be,
404 * with some documented elsewhere, some not.
405 */
406 struct trapframe *sanity_frame;
407 struct lwp *sanity_lwp;
408 int sanity_checked = 0;
409 void frame_sanity_check(int, int, struct trapframe *, struct lwp *);
410 void
411 frame_sanity_check(int where, int type, struct trapframe *tf, struct lwp *l)
412 {
413 extern int kernel_text;
414 extern int etext;
415 extern register_t kpsw;
416 extern vaddr_t hpt_base;
417 extern vsize_t hpt_mask;
418 vsize_t uspace_size;
419 #define SANITY(e) \
420 do { \
421 if (sanity_frame == NULL && !(e)) { \
422 sanity_frame = tf; \
423 sanity_lwp = l; \
424 sanity_checked = __LINE__; \
425 } \
426 } while (/* CONSTCOND */ 0)
427
428 SANITY((tf->tf_ipsw & kpsw) == kpsw);
429 SANITY(tf->tf_hptm == hpt_mask && tf->tf_vtop == hpt_base);
430 SANITY((kpsw & PSW_I) == 0 || tf->tf_eiem != 0);
431 if (tf->tf_iisq_head == HPPA_SID_KERNEL) {
432 /*
433 * If the trap happened in the gateway
434 * page, we take the easy way out and
435 * assume that the trapframe is okay.
436 */
437 if ((tf->tf_iioq_head & ~PAGE_MASK) != SYSCALLGATE) {
438 SANITY(!USERMODE(tf->tf_iioq_head));
439 SANITY(!USERMODE(tf->tf_iioq_tail));
440 SANITY(tf->tf_iioq_head >= (u_int) &kernel_text);
441 SANITY(tf->tf_iioq_head < (u_int) &etext);
442 SANITY(tf->tf_iioq_tail >= (u_int) &kernel_text);
443 SANITY(tf->tf_iioq_tail < (u_int) &etext);
444 #ifdef HPPA_REDZONE
445 uspace_size = HPPA_REDZONE;
446 #else
447 uspace_size = USPACE;
448 #endif
449 SANITY(l == NULL ||
450 ((tf->tf_sp >= (u_int)(l->l_addr) + PAGE_SIZE &&
451 tf->tf_sp < (u_int)(l->l_addr) + uspace_size)));
452 }
453 } else {
454 SANITY(USERMODE(tf->tf_iioq_head));
455 SANITY(USERMODE(tf->tf_iioq_tail));
456 SANITY(l != NULL && tf->tf_cr30 == kvtop((caddr_t)l->l_addr));
457 }
458 #undef SANITY
459 if (sanity_frame == tf) {
460 printf("insanity: where 0x%x type 0x%x tf %p lwp %p line %d "
461 "sp 0x%x pc 0x%x\n",
462 where, type, sanity_frame, sanity_lwp, sanity_checked,
463 tf->tf_sp, tf->tf_iioq_head);
464 (void) trap_kdebug(T_IBREAK, 0, tf);
465 sanity_frame = NULL;
466 sanity_lwp = NULL;
467 sanity_checked = 0;
468 }
469 }
470 #endif /* DEBUG */
471
472 void
473 trap(int type, struct trapframe *frame)
474 {
475 struct lwp *l;
476 struct proc *p;
477 struct pcb *pcbp;
478 vaddr_t va;
479 struct vm_map *map;
480 struct vmspace *vm;
481 vm_prot_t vftype;
482 pa_space_t space;
483 ksiginfo_t ksi;
484 u_int opcode, onfault;
485 int ret;
486 const char *tts;
487 int type_raw;
488 #ifdef DIAGNOSTIC
489 extern int emergency_stack_start, emergency_stack_end;
490 #endif
491
492 type_raw = type & ~T_USER;
493 opcode = frame->tf_iir;
494 if (type_raw == T_ITLBMISS || type_raw == T_ITLBMISSNA) {
495 va = frame->tf_iioq_head;
496 space = frame->tf_iisq_head;
497 vftype = VM_PROT_EXECUTE;
498 } else {
499 va = frame->tf_ior;
500 space = frame->tf_isr;
501 vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
502 }
503
504 l = curlwp;
505 p = l ? l->l_proc : NULL;
506 if ((type & T_USER) != 0)
507 LWP_CACHE_CREDS(l, p);
508
509 tts = (type & ~T_USER) > trap_types ? "reserved" :
510 trap_type[type & ~T_USER];
511
512 #ifdef DIAGNOSTIC
513 /*
514 * If we are on the emergency stack, then we either got
515 * a fault on the kernel stack, or we're just handling
516 * a trap for the machine check handler (which also
517 * runs on the emergency stack).
518 *
519 * We *very crudely* differentiate between the two cases
520 * by checking the faulting instruction: if it is the
521 * function prologue instruction that stores the old
522 * frame pointer and updates the stack pointer, we assume
523 * that we faulted on the kernel stack.
524 *
525 * In this case, not completing that instruction will
526 * probably confuse backtraces in kgdb/ddb. Completing
527 * it would be difficult, because we already faulted on
528 * that part of the stack, so instead we fix up the
529 * frame as if the function called has just returned.
530 * This has peculiar knowledge about what values are in
531 * what registers during the "normal gcc -g" prologue.
532 */
533 if (&type >= &emergency_stack_start &&
534 &type < &emergency_stack_end &&
535 type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
536 /* Restore the caller's frame pointer. */
537 frame->tf_r3 = frame->tf_r1;
538 /* Restore the caller's instruction offsets. */
539 frame->tf_iioq_head = frame->tf_rp;
540 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
541 goto dead_end;
542 }
543 #endif /* DIAGNOSTIC */
544
545 #ifdef DEBUG
546 frame_sanity_check(0xdead01, type, frame, l);
547 #endif /* DEBUG */
548
549 /* If this is a trap, not an interrupt, reenable interrupts. */
550 if (type_raw != T_INTERRUPT)
551 mtctl(frame->tf_eiem, CR_EIEM);
552
553 if (frame->tf_flags & TFF_LAST)
554 l->l_md.md_regs = frame;
555
556 #ifdef TRAPDEBUG
557 if (type_raw != T_INTERRUPT && type_raw != T_IBREAK)
558 printf("trap: %d, %s for %x:%x at %x:%x, fp=%p, rp=%x\n",
559 type, tts, space, (u_int)va, frame->tf_iisq_head,
560 frame->tf_iioq_head, frame, frame->tf_rp);
561 else if (type_raw == T_IBREAK)
562 printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
563 break5(opcode), break13(opcode),
564 frame->tf_iisq_head, frame->tf_iioq_head, frame);
565
566 {
567 extern int etext;
568 if (frame < (struct trapframe *)&etext) {
569 printf("trap: bogus frame ptr %p\n", frame);
570 goto dead_end;
571 }
572 }
573 #endif
574 switch (type) {
575 case T_NONEXIST:
576 case T_NONEXIST|T_USER:
577 #if !defined(DDB) && !defined(KGDB)
578 /* we've got screwed up by the central scrutinizer */
579 panic ("trap: elvis has just left the building!");
580 break;
581 #else
582 goto dead_end;
583 #endif
584 case T_RECOVERY|T_USER:
585 #ifdef USERTRACE
586 for(;;) {
587 if (frame->tf_iioq_head != rctr_next_iioq)
588 printf("-%08x\nr %08x",
589 rctr_next_iioq - 4,
590 frame->tf_iioq_head);
591 rctr_next_iioq = frame->tf_iioq_head + 4;
592 if (frame->tf_ipsw & PSW_N) {
593 /* Advance the program counter. */
594 frame->tf_iioq_head = frame->tf_iioq_tail;
595 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
596 /* Clear flags. */
597 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
598 /* Simulate another trap. */
599 continue;
600 }
601 break;
602 }
603 frame->tf_rctr = 0;
604 break;
605 #endif /* USERTRACE */
606 case T_RECOVERY:
607 #if !defined(DDB) && !defined(KGDB)
608 /* XXX will implement later */
609 printf ("trap: handicapped");
610 break;
611 #else
612 goto dead_end;
613 #endif
614
615 case T_EMULATION | T_USER:
616 #ifdef FPEMUL
617 hppa_fpu_emulate(frame, l, opcode);
618 #else /* !FPEMUL */
619 /*
620 * We don't have FPU emulation, so signal the
621 * process with a SIGFPE.
622 */
623
624 KSI_INIT_TRAP(&ksi);
625 ksi.ksi_signo = SIGFPE;
626 ksi.ksi_code = SI_NOINFO;
627 ksi.ksi_trap = type;
628 ksi.ksi_addr = (void *)frame->tf_iioq_head;
629 trapsignal(l, &ksi);
630 #endif /* !FPEMUL */
631 break;
632
633 case T_DATALIGN:
634 if (l->l_addr->u_pcb.pcb_onfault) {
635 do_onfault:
636 pcbp = &l->l_addr->u_pcb;
637 frame->tf_iioq_tail = 4 +
638 (frame->tf_iioq_head =
639 pcbp->pcb_onfault);
640 pcbp->pcb_onfault = 0;
641 break;
642 }
643 /*FALLTHROUGH*/
644
645 #ifdef DIAGNOSTIC
646 /* these just can't happen ever */
647 case T_PRIV_OP:
648 case T_PRIV_REG:
649 /* these just can't make it to the trap() ever */
650 case T_HPMC:
651 case T_HPMC | T_USER:
652 case T_EMULATION:
653 case T_EXCEPTION:
654 #endif
655 case T_IBREAK:
656 case T_DBREAK:
657 dead_end:
658 if (type & T_USER) {
659 #ifdef DEBUG
660 user_backtrace(frame, l, type);
661 #endif
662 KSI_INIT_TRAP(&ksi);
663 ksi.ksi_signo = SIGILL;
664 ksi.ksi_code = ILL_ILLTRP;
665 ksi.ksi_trap = type;
666 ksi.ksi_addr = (void *)frame->tf_iioq_head;
667 trapsignal(l, &ksi);
668 break;
669 }
670 if (trap_kdebug(type, va, frame))
671 return;
672 else if (type == T_DATALIGN)
673 panic ("trap: %s at 0x%x", tts, (u_int) va);
674 else
675 panic ("trap: no debugger for \"%s\" (%d)", tts, type);
676 break;
677
678 case T_IBREAK | T_USER:
679 case T_DBREAK | T_USER:
680 /* pass to user debugger */
681 break;
682
683 case T_EXCEPTION | T_USER: { /* co-proc assist trap */
684 uint64_t *fpp;
685 uint32_t *pex, ex, inst;
686 int i;
687
688 hppa_fpu_flush(l);
689 fpp = l->l_addr->u_pcb.pcb_fpregs;
690 pex = (uint32_t *)&fpp[1];
691 for (i = 1; i < 8 && !*pex; i++, pex++)
692 ;
693 KASSERT(i < 8);
694 ex = *pex;
695 *pex = 0;
696
697 /* reset the trap flag, as if there was none */
698 fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
699
700 /* emulate the instruction */
701 inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
702 hppa_fpu_emulate(frame, l, inst);
703 }
704 break;
705
706 case T_OVERFLOW | T_USER:
707 KSI_INIT_TRAP(&ksi);
708 ksi.ksi_signo = SIGFPE;
709 ksi.ksi_code = SI_NOINFO;
710 ksi.ksi_trap = type;
711 ksi.ksi_addr = (void *)va;
712 trapsignal(l, &ksi);
713 break;
714
715 case T_CONDITION | T_USER:
716 KSI_INIT_TRAP(&ksi);
717 ksi.ksi_signo = SIGFPE;
718 ksi.ksi_code = FPE_INTDIV;
719 ksi.ksi_trap = type;
720 ksi.ksi_addr = (void *)va;
721 trapsignal(l, &ksi);
722 break;
723
724 case T_ILLEGAL | T_USER:
725 #ifdef DEBUG
726 user_backtrace(frame, l, type);
727 #endif
728 KSI_INIT_TRAP(&ksi);
729 ksi.ksi_signo = SIGILL;
730 ksi.ksi_code = ILL_ILLOPC;
731 ksi.ksi_trap = type;
732 ksi.ksi_addr = (void *)va;
733 trapsignal(l, &ksi);
734 break;
735
736 case T_PRIV_OP | T_USER:
737 #ifdef DEBUG
738 user_backtrace(frame, l, type);
739 #endif
740 KSI_INIT_TRAP(&ksi);
741 ksi.ksi_signo = SIGILL;
742 ksi.ksi_code = ILL_PRVOPC;
743 ksi.ksi_trap = type;
744 ksi.ksi_addr = (void *)va;
745 trapsignal(l, &ksi);
746 break;
747
748 case T_PRIV_REG | T_USER:
749 #ifdef DEBUG
750 user_backtrace(frame, l, type);
751 #endif
752 KSI_INIT_TRAP(&ksi);
753 ksi.ksi_signo = SIGILL;
754 ksi.ksi_code = ILL_PRVREG;
755 ksi.ksi_trap = type;
756 ksi.ksi_addr = (void *)va;
757 trapsignal(l, &ksi);
758 break;
759
760 /* these should never got here */
761 case T_HIGHERPL | T_USER:
762 case T_LOWERPL | T_USER:
763 KSI_INIT_TRAP(&ksi);
764 ksi.ksi_signo = SIGSEGV;
765 ksi.ksi_code = SEGV_ACCERR;
766 ksi.ksi_trap = type;
767 ksi.ksi_addr = (void *)va;
768 trapsignal(l, &ksi);
769 break;
770
771 case T_IPROT | T_USER:
772 case T_DPROT | T_USER:
773 KSI_INIT_TRAP(&ksi);
774 ksi.ksi_signo = SIGSEGV;
775 ksi.ksi_code = SEGV_ACCERR;
776 ksi.ksi_trap = type;
777 ksi.ksi_addr = (void *)va;
778 trapsignal(l, &ksi);
779 break;
780
781 case T_DATACC: case T_USER | T_DATACC:
782 case T_ITLBMISS: case T_USER | T_ITLBMISS:
783 case T_DTLBMISS: case T_USER | T_DTLBMISS:
784 case T_ITLBMISSNA: case T_USER | T_ITLBMISSNA:
785 case T_DTLBMISSNA: case T_USER | T_DTLBMISSNA:
786 case T_TLB_DIRTY: case T_USER | T_TLB_DIRTY:
787 vm = p->p_vmspace;
788
789 if (!vm) {
790 #ifdef TRAPDEBUG
791 printf("trap: no vm, p=%p\n", p);
792 #endif
793 goto dead_end;
794 }
795
796 /*
797 * it could be a kernel map for exec_map faults
798 */
799 if (!(type & T_USER) && space == HPPA_SID_KERNEL)
800 map = kernel_map;
801 else {
802 map = &vm->vm_map;
803 if (l->l_flag & L_SA) {
804 l->l_savp->savp_faultaddr = va;
805 l->l_flag |= L_SA_PAGEFAULT;
806 }
807 }
808
809 va = hppa_trunc_page(va);
810
811 if (map->pmap->pmap_space != space) {
812 #ifdef TRAPDEBUG
813 printf("trap: space mismatch %d != %d\n",
814 space, map->pmap->pmap_space);
815 #endif
816 /* actually dump the user, crap the kernel */
817 goto dead_end;
818 }
819
820 /* Never call uvm_fault in interrupt context. */
821 KASSERT(hppa_intr_depth == 0);
822
823 onfault = l->l_addr->u_pcb.pcb_onfault;
824 l->l_addr->u_pcb.pcb_onfault = 0;
825 ret = uvm_fault(map, va, vftype);
826 l->l_addr->u_pcb.pcb_onfault = onfault;
827
828 #ifdef TRAPDEBUG
829 printf("uvm_fault(%p, %x, %d)=%d\n",
830 map, (u_int)va, vftype, ret);
831 #endif
832
833 if (map != kernel_map)
834 l->l_flag &= ~L_SA_PAGEFAULT;
835
836 /*
837 * If this was a stack access we keep track of the maximum
838 * accessed stack size. Also, if uvm_fault gets a protection
839 * failure it is due to accessing the stack region outside
840 * the current limit and we need to reflect that as an access
841 * error.
842 */
843 if (va >= (vaddr_t)vm->vm_maxsaddr + vm->vm_ssize) {
844 if (ret == 0) {
845 vsize_t nss = btoc(va - USRSTACK + PAGE_SIZE);
846 if (nss > vm->vm_ssize)
847 vm->vm_ssize = nss;
848 } else if (ret == EACCES)
849 ret = EFAULT;
850 }
851
852 if (ret != 0) {
853 if (type & T_USER) {
854 #ifdef DEBUG
855 user_backtrace(frame, l, type);
856 #endif
857 KSI_INIT_TRAP(&ksi);
858 ksi.ksi_signo = SIGSEGV;
859 ksi.ksi_code = (ret == EACCES ?
860 SEGV_ACCERR : SEGV_MAPERR);
861 ksi.ksi_trap = type;
862 ksi.ksi_addr = (void *)va;
863 trapsignal(l, &ksi);
864 } else {
865 if (l->l_addr->u_pcb.pcb_onfault) {
866 goto do_onfault;
867 }
868 panic("trap: uvm_fault(%p, %lx, %d): %d",
869 map, va, vftype, ret);
870 }
871 }
872 break;
873
874 case T_DATALIGN | T_USER:
875 #ifdef DEBUG
876 user_backtrace(frame, l, type);
877 #endif
878 KSI_INIT_TRAP(&ksi);
879 ksi.ksi_signo = SIGBUS;
880 ksi.ksi_code = BUS_ADRALN;
881 ksi.ksi_trap = type;
882 ksi.ksi_addr = (void *)va;
883 trapsignal(l, &ksi);
884 break;
885
886 case T_INTERRUPT:
887 case T_INTERRUPT|T_USER:
888 hppa_intr(frame);
889 mtctl(frame->tf_eiem, CR_EIEM);
890 break;
891
892 case T_LOWERPL:
893 case T_DPROT:
894 case T_IPROT:
895 case T_OVERFLOW:
896 case T_CONDITION:
897 case T_ILLEGAL:
898 case T_HIGHERPL:
899 case T_TAKENBR:
900 case T_POWERFAIL:
901 case T_LPMC:
902 case T_PAGEREF:
903 case T_DATAPID: case T_DATAPID | T_USER:
904 if (0 /* T-chip */) {
905 break;
906 }
907 /* FALLTHROUGH to unimplemented */
908 default:
909 panic ("trap: unimplemented \'%s\' (%d)", tts, type);
910 }
911
912 if (type & T_USER)
913 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
914
915 #ifdef DEBUG
916 frame_sanity_check(0xdead02, type, frame, l);
917 if (frame->tf_flags & TFF_LAST && curlwp != NULL)
918 frame_sanity_check(0xdead03, type, curlwp->l_md.md_regs,
919 curlwp);
920 #endif /* DEBUG */
921 }
922
923 void
924 child_return(void *arg)
925 {
926 struct lwp *l = arg;
927 #ifdef KTRACE
928 struct proc *p = l->l_proc;
929 #endif
930
931 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
932 #ifdef KTRACE
933 if (KTRPOINT(p, KTR_SYSRET))
934 ktrsysret(l, SYS_fork, 0, 0);
935 #endif
936 #ifdef DEBUG
937 frame_sanity_check(0xdead04, 0, l->l_md.md_regs, l);
938 #endif /* DEBUG */
939 }
940
941 /*
942 * call actual syscall routine
943 * from the low-level syscall handler:
944 * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
945 * our stack, this wins compared to copyin just needed amount anyway
946 * - register args are copied onto stack too
947 */
948 void
949 syscall(struct trapframe *frame, int *args)
950 {
951 struct lwp *l;
952 struct proc *p;
953 const struct sysent *callp;
954 int nsys, code, argsize, error;
955 int tmp;
956 int rval[2];
957
958 uvmexp.syscalls++;
959
960 #ifdef DEBUG
961 frame_sanity_check(0xdead04, 0, frame, curlwp);
962 #endif /* DEBUG */
963
964 if (!USERMODE(frame->tf_iioq_head))
965 panic("syscall");
966
967 l = curlwp;
968 p = l->l_proc;
969 l->l_md.md_regs = frame;
970 nsys = p->p_emul->e_nsysent;
971 callp = p->p_emul->e_sysent;
972 code = frame->tf_t1;
973 LWP_CACHE_CREDS(l, p);
974
975 /*
976 * Restarting a system call is touchy on the HPPA,
977 * because syscall arguments are passed in registers
978 * and the program counter of the syscall "point"
979 * isn't easily divined.
980 *
981 * We handle the first problem by assuming that we
982 * will have to restart this system call, so we
983 * stuff the first four words of the original arguments
984 * back into the frame as arg0...arg3, which is where
985 * we found them in the first place. Any further
986 * arguments are (still) on the user's stack and the
987 * syscall code will fetch them from there (again).
988 *
989 * The program counter problem is addressed below.
990 */
991 frame->tf_arg0 = args[0];
992 frame->tf_arg1 = args[1];
993 frame->tf_arg2 = args[2];
994 frame->tf_arg3 = args[3];
995
996 /*
997 * Some special handling for the syscall(2) and
998 * __syscall(2) system calls.
999 */
1000 switch (code) {
1001 case SYS_syscall:
1002 code = *args;
1003 args += 1;
1004 break;
1005 case SYS___syscall:
1006 if (callp != sysent)
1007 break;
1008 /*
1009 * NB: even though __syscall(2) takes a quad_t
1010 * containing the system call number, because
1011 * our argument copying word-swaps 64-bit arguments,
1012 * the least significant word of that quad_t
1013 * is the first word in the argument array.
1014 */
1015 code = *args;
1016 args += 2;
1017 }
1018
1019 /*
1020 * Stacks growing from lower addresses to higher
1021 * addresses are not really such a good idea, because
1022 * it makes it impossible to overlay a struct on top
1023 * of C stack arguments (the arguments appear in
1024 * reversed order).
1025 *
1026 * You can do the obvious thing (as locore.S does) and
1027 * copy argument words one by one, laying them out in
1028 * the "right" order in the destination buffer, but this
1029 * ends up word-swapping multi-word arguments (like off_t).
1030 *
1031 * To compensate, we have some automatically-generated
1032 * code that word-swaps these multi-word arguments.
1033 * Right now the script that generates this code is
1034 * in Perl, because I don't know awk.
1035 *
1036 * FIXME - this works only on native binaries and
1037 * will probably screw up any and all emulation.
1038 */
1039 switch (code) {
1040 /*
1041 * BEGIN automatically generated
1042 * by /home/fredette/project/hppa/makescargfix.pl
1043 * do not edit!
1044 */
1045 case SYS_pread:
1046 /*
1047 * syscallarg(int) fd;
1048 * syscallarg(void *) buf;
1049 * syscallarg(size_t) nbyte;
1050 * syscallarg(int) pad;
1051 * syscallarg(off_t) offset;
1052 */
1053 tmp = args[4];
1054 args[4] = args[4 + 1];
1055 args[4 + 1] = tmp;
1056 break;
1057 case SYS_pwrite:
1058 /*
1059 * syscallarg(int) fd;
1060 * syscallarg(const void *) buf;
1061 * syscallarg(size_t) nbyte;
1062 * syscallarg(int) pad;
1063 * syscallarg(off_t) offset;
1064 */
1065 tmp = args[4];
1066 args[4] = args[4 + 1];
1067 args[4 + 1] = tmp;
1068 break;
1069 case SYS_mmap:
1070 /*
1071 * syscallarg(void *) addr;
1072 * syscallarg(size_t) len;
1073 * syscallarg(int) prot;
1074 * syscallarg(int) flags;
1075 * syscallarg(int) fd;
1076 * syscallarg(long) pad;
1077 * syscallarg(off_t) pos;
1078 */
1079 tmp = args[6];
1080 args[6] = args[6 + 1];
1081 args[6 + 1] = tmp;
1082 break;
1083 case SYS_lseek:
1084 /*
1085 * syscallarg(int) fd;
1086 * syscallarg(int) pad;
1087 * syscallarg(off_t) offset;
1088 */
1089 tmp = args[2];
1090 args[2] = args[2 + 1];
1091 args[2 + 1] = tmp;
1092 break;
1093 case SYS_truncate:
1094 /*
1095 * syscallarg(const char *) path;
1096 * syscallarg(int) pad;
1097 * syscallarg(off_t) length;
1098 */
1099 tmp = args[2];
1100 args[2] = args[2 + 1];
1101 args[2 + 1] = tmp;
1102 break;
1103 case SYS_ftruncate:
1104 /*
1105 * syscallarg(int) fd;
1106 * syscallarg(int) pad;
1107 * syscallarg(off_t) length;
1108 */
1109 tmp = args[2];
1110 args[2] = args[2 + 1];
1111 args[2 + 1] = tmp;
1112 break;
1113 case SYS_preadv:
1114 /*
1115 * syscallarg(int) fd;
1116 * syscallarg(const struct iovec *) iovp;
1117 * syscallarg(int) iovcnt;
1118 * syscallarg(int) pad;
1119 * syscallarg(off_t) offset;
1120 */
1121 tmp = args[4];
1122 args[4] = args[4 + 1];
1123 args[4 + 1] = tmp;
1124 break;
1125 case SYS_pwritev:
1126 /*
1127 * syscallarg(int) fd;
1128 * syscallarg(const struct iovec *) iovp;
1129 * syscallarg(int) iovcnt;
1130 * syscallarg(int) pad;
1131 * syscallarg(off_t) offset;
1132 */
1133 tmp = args[4];
1134 args[4] = args[4 + 1];
1135 args[4 + 1] = tmp;
1136 break;
1137 default:
1138 break;
1139 /*
1140 * END automatically generated
1141 * by /home/fredette/project/hppa/makescargfix.pl
1142 * do not edit!
1143 */
1144 }
1145
1146 #ifdef USERTRACE
1147 if (0) {
1148 user_backtrace(frame, l, -1);
1149 frame->tf_ipsw |= PSW_R;
1150 frame->tf_rctr = 0;
1151 printf("r %08x", frame->tf_iioq_head);
1152 rctr_next_iioq = frame->tf_iioq_head + 4;
1153 }
1154 #endif
1155
1156 if (code < 0 || code >= nsys)
1157 callp += p->p_emul->e_nosys; /* bad syscall # */
1158 else
1159 callp += code;
1160 argsize = callp->sy_argsize;
1161
1162 if ((error = trace_enter(l, code, code, NULL, args)) != 0)
1163 goto out;
1164
1165 rval[0] = 0;
1166 rval[1] = 0;
1167 error = (*callp->sy_call)(l, args, rval);
1168 out:
1169 switch (error) {
1170 case 0:
1171 l = curlwp; /* changes on exec() */
1172 frame = l->l_md.md_regs;
1173 frame->tf_ret0 = rval[0];
1174 frame->tf_ret1 = rval[1];
1175 frame->tf_t1 = 0;
1176 break;
1177 case ERESTART:
1178 /*
1179 * Now we have to wind back the instruction
1180 * offset queue to the point where the system
1181 * call will be made again. This is inherently
1182 * tied to the SYSCALL macro.
1183 *
1184 * Currently, the part of the SYSCALL macro
1185 * that we want to rerun reads as:
1186 *
1187 * ldil L%SYSCALLGATE, r1
1188 * ble 4(sr7, r1)
1189 * ldi __CONCAT(SYS_,x), t1
1190 * ldw HPPA_FRAME_ERP(sr0,sp), rp
1191 *
1192 * And our offset queue head points to the
1193 * final ldw instruction. So we need to
1194 * subtract twelve to reach the ldil.
1195 */
1196 frame->tf_iioq_head -= 12;
1197 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
1198 break;
1199 case EJUSTRETURN:
1200 p = curproc;
1201 break;
1202 default:
1203 if (p->p_emul->e_errno)
1204 error = p->p_emul->e_errno[error];
1205 frame->tf_t1 = error;
1206 break;
1207 }
1208
1209 trace_exit(l, code, args, rval, error);
1210
1211 userret(l, frame->tf_iioq_head, 0);
1212 #ifdef DEBUG
1213 frame_sanity_check(0xdead05, 0, frame, l);
1214 #endif /* DEBUG */
1215 }
1216
1217 /*
1218 * Start a new LWP
1219 */
1220 void
1221 startlwp(void *arg)
1222 {
1223 int err;
1224 ucontext_t *uc = arg;
1225 struct lwp *l = curlwp;
1226
1227 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
1228 #if DIAGNOSTIC
1229 if (err) {
1230 printf("Error %d from cpu_setmcontext.", err);
1231 }
1232 #endif
1233 pool_put(&lwp_uc_pool, uc);
1234
1235 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1236 }
1237
1238 /*
1239 * XXX This is a terrible name.
1240 */
1241 void
1242 upcallret(struct lwp *l)
1243 {
1244 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1245 }
1246