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