1 /* $OpenBSD: trap.c,v 1.19 1998/09/30 12:40:41 pefo Exp $ */
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department and Ralph Campbell.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: Utah Hdr: trap.c 1.32 91/04/06
38 * from: @(#)trap.c 8.5 (Berkeley) 1/11/94
39 * JNPR: trap.c,v 1.13.2.2 2007/08/29 10:03:49 girish
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include "opt_compat.h"
46 #include "opt_ktrace.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/sysent.h>
52 #include <sys/kernel.h>
53 #include <sys/signalvar.h>
54 #include <sys/syscall.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_param.h>
62 #include <sys/vmmeter.h>
63 #include <sys/ptrace.h>
66 #include <sys/vnode.h>
67 #include <sys/pioctl.h>
68 #include <sys/sysctl.h>
69 #include <sys/syslog.h>
72 #include <sys/ktrace.h>
74 #include <net/netisr.h>
76 #include <machine/trap.h>
77 #include <machine/cpu.h>
78 #include <machine/pte.h>
79 #include <machine/pmap.h>
80 #include <machine/md_var.h>
81 #include <machine/mips_opcode.h>
82 #include <machine/frame.h>
83 #include <machine/regnum.h>
84 #include <machine/tls.h>
87 #include <machine/db_machdep.h>
88 #include <ddb/db_sym.h>
94 #include <sys/dtrace_bsd.h>
99 SYSCTL_INT(_machdep, OID_AUTO, trap_debug, CTLFLAG_RW,
100 &trap_debug, 0, "Debug information on all traps");
103 #define lbu_macro(data, addr) \
104 __asm __volatile ("lbu %0, 0x0(%1)" \
105 : "=r" (data) /* outputs */ \
106 : "r" (addr)); /* inputs */
108 #define lb_macro(data, addr) \
109 __asm __volatile ("lb %0, 0x0(%1)" \
110 : "=r" (data) /* outputs */ \
111 : "r" (addr)); /* inputs */
113 #define lwl_macro(data, addr) \
114 __asm __volatile ("lwl %0, 0x0(%1)" \
115 : "=r" (data) /* outputs */ \
116 : "r" (addr)); /* inputs */
118 #define lwr_macro(data, addr) \
119 __asm __volatile ("lwr %0, 0x0(%1)" \
120 : "=r" (data) /* outputs */ \
121 : "r" (addr)); /* inputs */
123 #define ldl_macro(data, addr) \
124 __asm __volatile ("ldl %0, 0x0(%1)" \
125 : "=r" (data) /* outputs */ \
126 : "r" (addr)); /* inputs */
128 #define ldr_macro(data, addr) \
129 __asm __volatile ("ldr %0, 0x0(%1)" \
130 : "=r" (data) /* outputs */ \
131 : "r" (addr)); /* inputs */
133 #define sb_macro(data, addr) \
134 __asm __volatile ("sb %0, 0x0(%1)" \
136 : "r" (data), "r" (addr)); /* inputs */
138 #define swl_macro(data, addr) \
139 __asm __volatile ("swl %0, 0x0(%1)" \
141 : "r" (data), "r" (addr)); /* inputs */
143 #define swr_macro(data, addr) \
144 __asm __volatile ("swr %0, 0x0(%1)" \
146 : "r" (data), "r" (addr)); /* inputs */
148 #define sdl_macro(data, addr) \
149 __asm __volatile ("sdl %0, 0x0(%1)" \
151 : "r" (data), "r" (addr)); /* inputs */
153 #define sdr_macro(data, addr) \
154 __asm __volatile ("sdr %0, 0x0(%1)" \
156 : "r" (data), "r" (addr)); /* inputs */
158 static void log_illegal_instruction(const char *, struct trapframe *);
159 static void log_bad_page_fault(char *, struct trapframe *, int);
160 static void log_frame_dump(struct trapframe *frame);
161 static void get_mapping_info(vm_offset_t, pd_entry_t **, pt_entry_t **);
163 int (*dtrace_invop_jump_addr)(struct trapframe *);
166 static void trap_frame_dump(struct trapframe *frame);
169 void (*machExceptionTable[]) (void)= {
171 * The kernel exception handlers.
173 MipsKernIntr, /* external interrupt */
174 MipsKernGenException, /* TLB modification */
175 MipsTLBInvalidException,/* TLB miss (load or instr. fetch) */
176 MipsTLBInvalidException,/* TLB miss (store) */
177 MipsKernGenException, /* address error (load or I-fetch) */
178 MipsKernGenException, /* address error (store) */
179 MipsKernGenException, /* bus error (I-fetch) */
180 MipsKernGenException, /* bus error (load or store) */
181 MipsKernGenException, /* system call */
182 MipsKernGenException, /* breakpoint */
183 MipsKernGenException, /* reserved instruction */
184 MipsKernGenException, /* coprocessor unusable */
185 MipsKernGenException, /* arithmetic overflow */
186 MipsKernGenException, /* trap exception */
187 MipsKernGenException, /* virtual coherence exception inst */
188 MipsKernGenException, /* floating point exception */
189 MipsKernGenException, /* reserved */
190 MipsKernGenException, /* reserved */
191 MipsKernGenException, /* reserved */
192 MipsKernGenException, /* reserved */
193 MipsKernGenException, /* reserved */
194 MipsKernGenException, /* reserved */
195 MipsKernGenException, /* reserved */
196 MipsKernGenException, /* watch exception */
197 MipsKernGenException, /* reserved */
198 MipsKernGenException, /* reserved */
199 MipsKernGenException, /* reserved */
200 MipsKernGenException, /* reserved */
201 MipsKernGenException, /* reserved */
202 MipsKernGenException, /* reserved */
203 MipsKernGenException, /* reserved */
204 MipsKernGenException, /* virtual coherence exception data */
206 * The user exception handlers.
208 MipsUserIntr, /* 0 */
209 MipsUserGenException, /* 1 */
210 MipsTLBInvalidException,/* 2 */
211 MipsTLBInvalidException,/* 3 */
212 MipsUserGenException, /* 4 */
213 MipsUserGenException, /* 5 */
214 MipsUserGenException, /* 6 */
215 MipsUserGenException, /* 7 */
216 MipsUserGenException, /* 8 */
217 MipsUserGenException, /* 9 */
218 MipsUserGenException, /* 10 */
219 MipsUserGenException, /* 11 */
220 MipsUserGenException, /* 12 */
221 MipsUserGenException, /* 13 */
222 MipsUserGenException, /* 14 */
223 MipsUserGenException, /* 15 */
224 MipsUserGenException, /* 16 */
225 MipsUserGenException, /* 17 */
226 MipsUserGenException, /* 18 */
227 MipsUserGenException, /* 19 */
228 MipsUserGenException, /* 20 */
229 MipsUserGenException, /* 21 */
230 MipsUserGenException, /* 22 */
231 MipsUserGenException, /* 23 */
232 MipsUserGenException, /* 24 */
233 MipsUserGenException, /* 25 */
234 MipsUserGenException, /* 26 */
235 MipsUserGenException, /* 27 */
236 MipsUserGenException, /* 28 */
237 MipsUserGenException, /* 29 */
238 MipsUserGenException, /* 20 */
239 MipsUserGenException, /* 31 */
242 char *trap_type[] = {
243 "external interrupt",
245 "TLB miss (load or instr. fetch)",
247 "address error (load or I-fetch)",
248 "address error (store)",
249 "bus error (I-fetch)",
250 "bus error (load or store)",
253 "reserved instruction",
254 "coprocessor unusable",
255 "arithmetic overflow",
257 "virtual coherency instruction",
274 "virtual coherency data",
277 #if !defined(SMP) && (defined(DDB) || defined(DEBUG))
278 struct trapdebug trapdebug[TRAPSIZE], *trp = trapdebug;
281 #define KERNLAND(x) ((vm_offset_t)(x) >= VM_MIN_KERNEL_ADDRESS && (vm_offset_t)(x) < VM_MAX_KERNEL_ADDRESS)
282 #define DELAYBRANCH(x) ((int)(x) < 0)
285 * MIPS load/store access type
298 char *access_name[] = {
299 "Load Halfword Unsigned",
301 "Load Word Unsigned",
310 #include <machine/octeon_cop2.h>
313 static int allow_unaligned_acc = 1;
315 SYSCTL_INT(_vm, OID_AUTO, allow_unaligned_acc, CTLFLAG_RW,
316 &allow_unaligned_acc, 0, "Allow unaligned accesses");
319 * FP emulation is assumed to work on O32, but the code is outdated and crufty
320 * enough that it's a more sensible default to have it disabled when using
321 * other ABIs. At the very least, it needs a lot of help in using
322 * type-semantic ABI-oblivious macros for everything it does.
324 #if defined(__mips_o32)
325 static int emulate_fp = 1;
327 static int emulate_fp = 0;
329 SYSCTL_INT(_machdep, OID_AUTO, emulate_fp, CTLFLAG_RW,
330 &emulate_fp, 0, "Emulate unimplemented FPU instructions");
332 static int emulate_unaligned_access(struct trapframe *frame, int mode);
334 extern void fswintrberr(void); /* XXX */
337 cpu_fetch_syscall_args(struct thread *td)
339 struct trapframe *locr0;
340 struct sysentvec *se;
341 struct syscall_args *sa;
344 locr0 = td->td_frame;
347 bzero(sa->args, sizeof(sa->args));
349 /* compute next PC after syscall instruction */
350 td->td_pcb->pcb_tpc = sa->trapframe->pc; /* Remember if restart */
351 if (DELAYBRANCH(sa->trapframe->cause)) /* Check BD bit */
352 locr0->pc = MipsEmulateBranch(locr0, sa->trapframe->pc, 0, 0);
354 locr0->pc += sizeof(int);
355 sa->code = locr0->v0;
361 * This is an indirect syscall, in which the code is the first argument.
363 #if (!defined(__mips_n32) && !defined(__mips_n64)) || defined(COMPAT_FREEBSD32)
364 if (sa->code == SYS___syscall && SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
366 * Like syscall, but code is a quad, so as to maintain alignment
367 * for the rest of the arguments.
369 if (_QUAD_LOWWORD == 0)
370 sa->code = locr0->a0;
372 sa->code = locr0->a1;
373 sa->args[0] = locr0->a2;
374 sa->args[1] = locr0->a3;
380 * This is either not a quad syscall, or is a quad syscall with a
381 * new ABI in which quads fit in a single register.
383 sa->code = locr0->a0;
384 sa->args[0] = locr0->a1;
385 sa->args[1] = locr0->a2;
386 sa->args[2] = locr0->a3;
388 #if defined(__mips_n32) || defined(__mips_n64)
389 #ifdef COMPAT_FREEBSD32
390 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
393 * Non-o32 ABIs support more arguments in registers.
395 sa->args[3] = locr0->a4;
396 sa->args[4] = locr0->a5;
397 sa->args[5] = locr0->a6;
398 sa->args[6] = locr0->a7;
400 #ifdef COMPAT_FREEBSD32
407 * A direct syscall, arguments are just parameters to the syscall.
409 sa->args[0] = locr0->a0;
410 sa->args[1] = locr0->a1;
411 sa->args[2] = locr0->a2;
412 sa->args[3] = locr0->a3;
414 #if defined (__mips_n32) || defined(__mips_n64)
415 #ifdef COMPAT_FREEBSD32
416 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
419 * Non-o32 ABIs support more arguments in registers.
421 sa->args[4] = locr0->a4;
422 sa->args[5] = locr0->a5;
423 sa->args[6] = locr0->a6;
424 sa->args[7] = locr0->a7;
426 #ifdef COMPAT_FREEBSD32
435 printf("SYSCALL #%d pid:%u\n", sa->code, td->td_proc->p_pid);
438 se = td->td_proc->p_sysent;
441 * Shouldn't this go before switching on the code?
444 sa->code &= se->sv_mask;
446 if (sa->code >= se->sv_size)
447 sa->callp = &se->sv_table[0];
449 sa->callp = &se->sv_table[sa->code];
451 sa->narg = sa->callp->sy_narg;
453 if (sa->narg > nsaved) {
454 #if defined(__mips_n32) || defined(__mips_n64)
457 * Is this right for new ABIs? I think the 4 there
458 * should be 8, size there are 8 registers to skip,
459 * not 4, but I'm not certain.
461 #ifdef COMPAT_FREEBSD32
462 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32))
464 printf("SYSCALL #%u pid:%u, narg (%u) > nsaved (%u).\n",
465 sa->code, td->td_proc->p_pid, sa->narg, nsaved);
467 #if (defined(__mips_n32) || defined(__mips_n64)) && defined(COMPAT_FREEBSD32)
468 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
472 error = 0; /* XXX GCC is awful. */
473 for (i = nsaved; i < sa->narg; i++) {
474 error = copyin((caddr_t)(intptr_t)(locr0->sp +
475 (4 + (i - nsaved)) * sizeof(int32_t)),
476 (caddr_t)&arg, sizeof arg);
483 error = copyin((caddr_t)(intptr_t)(locr0->sp +
484 4 * sizeof(register_t)), (caddr_t)&sa->args[nsaved],
485 (u_int)(sa->narg - nsaved) * sizeof(register_t));
494 td->td_retval[0] = 0;
495 td->td_retval[1] = locr0->v1;
503 #include "../../kern/subr_syscall.c"
506 * Handle an exception.
507 * Called from MipsKernGenException() or MipsUserGenException()
508 * when a processor trap occurs.
509 * In the case of a kernel trap, we return the pc where to resume if
510 * p->p_addr->u_pcb.pcb_onfault is set, otherwise, return old pc.
513 trap(struct trapframe *trapframe)
518 struct thread *td = curthread;
519 struct proc *p = curproc;
528 register_t *frame_regs;
530 trapdebug_enter(trapframe, 0);
532 type = (trapframe->cause & MIPS_CR_EXC_CODE) >> MIPS_CR_EXC_CODE_SHIFT;
533 if (TRAPF_USERMODE(trapframe)) {
541 * Enable hardware interrupts if they were on before the trap. If it
542 * was off disable all so we don't accidently enable it when doing a
543 * return to userland.
545 if (trapframe->sr & MIPS_SR_INT_IE) {
546 set_intr_mask(trapframe->sr & MIPS_SR_INT_MASK);
554 static vm_offset_t last_badvaddr = 0;
555 static vm_offset_t this_badvaddr = 0;
556 static int count = 0;
559 printf("trap type %x (%s - ", type,
560 trap_type[type & (~T_USER)]);
563 printf("user mode)\n");
565 printf("kernel mode)\n");
568 printf("cpuid = %d\n", PCPU_GET(cpuid));
570 pid = mips_rd_entryhi() & TLBHI_ASID_MASK;
571 printf("badaddr = %#jx, pc = %#jx, ra = %#jx, sp = %#jx, sr = %jx, pid = %d, ASID = %u\n",
572 (intmax_t)trapframe->badvaddr, (intmax_t)trapframe->pc, (intmax_t)trapframe->ra,
573 (intmax_t)trapframe->sp, (intmax_t)trapframe->sr,
574 (curproc ? curproc->p_pid : -1), pid);
576 switch (type & ~T_USER) {
582 this_badvaddr = trapframe->badvaddr;
585 this_badvaddr = trapframe->ra;
588 this_badvaddr = trapframe->pc;
591 if ((last_badvaddr == this_badvaddr) &&
592 ((type & ~T_USER) != T_SYSCALL) &&
593 ((type & ~T_USER) != T_COP_UNUSABLE)) {
595 trap_frame_dump(trapframe);
596 panic("too many faults at %p\n", (void *)last_badvaddr);
599 last_badvaddr = this_badvaddr;
607 * A trap can occur while DTrace executes a probe. Before
608 * executing the probe, DTrace blocks re-scheduling and sets
609 * a flag in its per-cpu flags to indicate that it doesn't
610 * want to fault. On returning from the probe, the no-fault
611 * flag is cleared and finally re-scheduling is enabled.
613 * If the DTrace kernel module has registered a trap handler,
614 * call it and if it returns non-zero, assume that it has
615 * handled the trap and modified the trap frame so that this
616 * function can return normally.
619 * XXXDTRACE: add pid probe handler here (if ever)
622 if (dtrace_trap_func != NULL &&
623 (*dtrace_trap_func)(trapframe, type) != 0)
624 return (trapframe->pc);
631 kdb_trap(type, 0, trapframe);
636 /* check for kernel address */
637 if (KERNLAND(trapframe->badvaddr)) {
638 if (pmap_emulate_modified(kernel_pmap,
639 trapframe->badvaddr) != 0) {
640 ftype = VM_PROT_WRITE;
643 return (trapframe->pc);
647 case T_TLB_MOD + T_USER:
648 pmap = &p->p_vmspace->vm_pmap;
649 if (pmap_emulate_modified(pmap, trapframe->badvaddr) != 0) {
650 ftype = VM_PROT_WRITE;
654 return (trapframe->pc);
659 ftype = (type == T_TLB_ST_MISS) ? VM_PROT_WRITE : VM_PROT_READ;
660 /* check for kernel address */
661 if (KERNLAND(trapframe->badvaddr)) {
666 va = trunc_page((vm_offset_t)trapframe->badvaddr);
667 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
668 if (rv == KERN_SUCCESS)
669 return (trapframe->pc);
670 if (td->td_pcb->pcb_onfault != NULL) {
671 pc = (register_t)(intptr_t)td->td_pcb->pcb_onfault;
672 td->td_pcb->pcb_onfault = NULL;
679 * It is an error for the kernel to access user space except
680 * through the copyin/copyout routines.
682 if (td->td_pcb->pcb_onfault == NULL)
685 /* check for fuswintr() or suswintr() getting a page fault */
686 /* XXX There must be a nicer way to do this. */
687 if (td->td_pcb->pcb_onfault == fswintrberr) {
688 pc = (register_t)(intptr_t)td->td_pcb->pcb_onfault;
689 td->td_pcb->pcb_onfault = NULL;
695 case T_TLB_LD_MISS + T_USER:
696 ftype = VM_PROT_READ;
699 case T_TLB_ST_MISS + T_USER:
700 ftype = VM_PROT_WRITE;
710 va = trunc_page((vm_offset_t)trapframe->badvaddr);
711 if (KERNLAND(trapframe->badvaddr)) {
713 * Don't allow user-mode faults in kernel
719 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
721 * XXXDTRACE: add dtrace_doubletrap_func here?
724 printf("vm_fault(%p (pmap %p), %p (%p), %x, %d) -> %x at pc %p\n",
725 map, &vm->vm_pmap, (void *)va, (void *)(intptr_t)trapframe->badvaddr,
726 ftype, VM_FAULT_NORMAL, rv, (void *)(intptr_t)trapframe->pc);
729 if (rv == KERN_SUCCESS) {
731 return (trapframe->pc);
737 if (td->td_pcb->pcb_onfault != NULL) {
738 pc = (register_t)(intptr_t)td->td_pcb->pcb_onfault;
739 td->td_pcb->pcb_onfault = NULL;
745 if (rv == KERN_PROTECTION_FAILURE)
749 addr = trapframe->pc;
751 msg = "BAD_PAGE_FAULT";
752 log_bad_page_fault(msg, trapframe, type);
757 case T_ADDR_ERR_LD + T_USER: /* misaligned or kseg access */
758 case T_ADDR_ERR_ST + T_USER: /* misaligned or kseg access */
759 if (trapframe->badvaddr < 0 ||
760 trapframe->badvaddr >= VM_MAXUSER_ADDRESS) {
761 msg = "ADDRESS_SPACE_ERR";
762 } else if (allow_unaligned_acc) {
765 if (type == (T_ADDR_ERR_LD + T_USER))
768 mode = VM_PROT_WRITE;
770 access_type = emulate_unaligned_access(trapframe, mode);
771 if (access_type != 0)
773 msg = "ALIGNMENT_FIX_ERR";
780 case T_BUS_ERR_IFETCH + T_USER: /* BERR asserted to cpu */
781 case T_BUS_ERR_LD_ST + T_USER: /* BERR asserted to cpu */
782 ucode = 0; /* XXX should be VM_PROT_something */
784 addr = trapframe->pc;
787 log_bad_page_fault(msg, trapframe, type);
790 case T_SYSCALL + T_USER:
794 td->td_sa.trapframe = trapframe;
795 error = syscallenter(td);
797 #if !defined(SMP) && (defined(DDB) || defined(DEBUG))
798 if (trp == trapdebug)
799 trapdebug[TRAPSIZE - 1].code = td->td_sa.code;
801 trp[-1].code = td->td_sa.code;
803 trapdebug_enter(td->td_frame, -td->td_sa.code);
806 * The sync'ing of I & D caches for SYS_ptrace() is
807 * done by procfs_domem() through procfs_rwmem()
808 * instead of being done here under a special check
811 syscallret(td, error);
812 return (trapframe->pc);
815 #if defined(KDTRACE_HOOKS) || defined(DDB)
818 if (!usermode && dtrace_invop_jump_addr != 0) {
819 dtrace_invop_jump_addr(trapframe);
820 return (trapframe->pc);
824 kdb_trap(type, 0, trapframe);
825 return (trapframe->pc);
829 case T_BREAK + T_USER:
834 /* compute address of break instruction */
836 if (DELAYBRANCH(trapframe->cause))
839 /* read break instruction */
840 instr = fuword32((caddr_t)va);
842 printf("trap: %s (%d) breakpoint %x at %x: (adr %x ins %x)\n",
843 p->p_comm, p->p_pid, instr, trapframe->pc,
844 p->p_md.md_ss_addr, p->p_md.md_ss_instr); /* XXX */
846 if (td->td_md.md_ss_addr != va ||
847 instr != MIPS_BREAK_SSTEP) {
849 addr = trapframe->pc;
853 * The restoration of the original instruction and
854 * the clearing of the breakpoint will be done later
855 * by the call to ptrace_clear_single_step() in
856 * issignal() when SIGTRAP is processed.
858 addr = trapframe->pc;
863 case T_IWATCH + T_USER:
864 case T_DWATCH + T_USER:
868 /* compute address of trapped instruction */
870 if (DELAYBRANCH(trapframe->cause))
872 printf("watch exception @ %p\n", (void *)va);
878 case T_TRAP + T_USER:
882 struct trapframe *locr0 = td->td_frame;
884 /* compute address of trap instruction */
886 if (DELAYBRANCH(trapframe->cause))
888 /* read break instruction */
889 instr = fuword32((caddr_t)va);
891 if (DELAYBRANCH(trapframe->cause)) { /* Check BD bit */
892 locr0->pc = MipsEmulateBranch(locr0, trapframe->pc, 0,
895 locr0->pc += sizeof(int);
898 i = SIGEMT; /* Stuff it with something for now */
902 case T_RES_INST + T_USER:
905 inst = *(InstFmt *)(intptr_t)trapframe->pc;
906 switch (inst.RType.op) {
908 switch (inst.RType.func) {
910 /* Register 29 used for TLS */
911 if (inst.RType.rd == 29) {
912 frame_regs = &(trapframe->zero);
913 frame_regs[inst.RType.rt] = (register_t)(intptr_t)td->td_md.md_tls;
914 frame_regs[inst.RType.rt] += td->td_md.md_tls_tcb_offset;
915 trapframe->pc += sizeof(int);
923 log_illegal_instruction("RES_INST", trapframe);
925 addr = trapframe->pc;
934 cop = (trapframe->cause & MIPS_CR_COP_ERR) >> MIPS_CR_COP_ERR_SHIFT;
935 /* Handle only COP2 exception */
939 addr = trapframe->pc;
940 /* save userland cop2 context if it has been touched */
941 if ((td->td_md.md_flags & MDTD_COP2USED) &&
942 (td->td_md.md_cop2owner == COP2_OWNER_USERLAND)) {
943 if (td->td_md.md_ucop2)
944 octeon_cop2_save(td->td_md.md_ucop2);
946 panic("COP2 was used in user mode but md_ucop2 is NULL");
949 if (td->td_md.md_cop2 == NULL) {
950 td->td_md.md_cop2 = octeon_cop2_alloc_ctx();
951 if (td->td_md.md_cop2 == NULL)
952 panic("Failed to allocate COP2 context");
953 memset(td->td_md.md_cop2, 0, sizeof(*td->td_md.md_cop2));
956 octeon_cop2_restore(td->td_md.md_cop2);
958 /* Make userland re-request its context */
959 td->td_frame->sr &= ~MIPS_SR_COP_2_BIT;
960 td->td_md.md_flags |= MDTD_COP2USED;
961 td->td_md.md_cop2owner = COP2_OWNER_KERNEL;
962 /* Enable COP2, it will be disabled in cpu_switch */
963 mips_wr_status(mips_rd_status() | MIPS_SR_COP_2_BIT);
964 return (trapframe->pc);
970 case T_COP_UNUSABLE + T_USER:
971 cop = (trapframe->cause & MIPS_CR_COP_ERR) >> MIPS_CR_COP_ERR_SHIFT;
973 #if !defined(CPU_HAVEFPU)
974 /* FP (COP1) instruction */
975 log_illegal_instruction("COP1_UNUSABLE", trapframe);
979 addr = trapframe->pc;
980 MipsSwitchFPState(PCPU_GET(fpcurthread), td->td_frame);
981 PCPU_SET(fpcurthread, td);
982 #if defined(__mips_n64)
983 td->td_frame->sr |= MIPS_SR_COP_1_BIT | MIPS_SR_FR;
985 td->td_frame->sr |= MIPS_SR_COP_1_BIT;
987 td->td_md.md_flags |= MDTD_FPUSED;
993 addr = trapframe->pc;
994 if ((td->td_md.md_flags & MDTD_COP2USED) &&
995 (td->td_md.md_cop2owner == COP2_OWNER_KERNEL)) {
996 if (td->td_md.md_cop2)
997 octeon_cop2_save(td->td_md.md_cop2);
999 panic("COP2 was used in kernel mode but md_cop2 is NULL");
1002 if (td->td_md.md_ucop2 == NULL) {
1003 td->td_md.md_ucop2 = octeon_cop2_alloc_ctx();
1004 if (td->td_md.md_ucop2 == NULL)
1005 panic("Failed to allocate userland COP2 context");
1006 memset(td->td_md.md_ucop2, 0, sizeof(*td->td_md.md_ucop2));
1009 octeon_cop2_restore(td->td_md.md_ucop2);
1011 td->td_frame->sr |= MIPS_SR_COP_2_BIT;
1012 td->td_md.md_flags |= MDTD_COP2USED;
1013 td->td_md.md_cop2owner = COP2_OWNER_USERLAND;
1018 log_illegal_instruction("COPn_UNUSABLE", trapframe);
1019 i = SIGILL; /* only FPU instructions allowed */
1024 #if !defined(SMP) && (defined(DDB) || defined(DEBUG))
1027 printf("FPU Trap: PC %#jx CR %x SR %x\n",
1028 (intmax_t)trapframe->pc, (unsigned)trapframe->cause, (unsigned)trapframe->sr);
1032 case T_FPE + T_USER:
1035 addr = trapframe->pc;
1038 MipsFPTrap(trapframe->sr, trapframe->cause, trapframe->pc);
1041 case T_OVFLOW + T_USER:
1043 addr = trapframe->pc;
1046 case T_ADDR_ERR_LD: /* misaligned access */
1047 case T_ADDR_ERR_ST: /* misaligned access */
1050 printf("+++ ADDR_ERR: type = %d, badvaddr = %#jx\n", type,
1051 (intmax_t)trapframe->badvaddr);
1054 /* Only allow emulation on a user address */
1055 if (allow_unaligned_acc &&
1056 ((vm_offset_t)trapframe->badvaddr < VM_MAXUSER_ADDRESS)) {
1059 if (type == T_ADDR_ERR_LD)
1060 mode = VM_PROT_READ;
1062 mode = VM_PROT_WRITE;
1064 access_type = emulate_unaligned_access(trapframe, mode);
1065 if (access_type != 0)
1066 return (trapframe->pc);
1070 case T_BUS_ERR_LD_ST: /* BERR asserted to cpu */
1071 if (td->td_pcb->pcb_onfault != NULL) {
1072 pc = (register_t)(intptr_t)td->td_pcb->pcb_onfault;
1073 td->td_pcb->pcb_onfault = NULL;
1082 #if !defined(SMP) && defined(DEBUG)
1086 printf("cpu:%d-", PCPU_GET(cpuid));
1088 printf("Trap cause = %d (%s - ", type,
1089 trap_type[type & (~T_USER)]);
1092 printf("user mode)\n");
1094 printf("kernel mode)\n");
1098 printf("badvaddr = %#jx, pc = %#jx, ra = %#jx, sr = %#jxx\n",
1099 (intmax_t)trapframe->badvaddr, (intmax_t)trapframe->pc, (intmax_t)trapframe->ra,
1100 (intmax_t)trapframe->sr);
1104 if (debugger_on_panic || kdb_active) {
1105 kdb_trap(type, 0, trapframe);
1110 td->td_frame->pc = trapframe->pc;
1111 td->td_frame->cause = trapframe->cause;
1112 td->td_frame->badvaddr = trapframe->badvaddr;
1113 ksiginfo_init_trap(&ksi);
1115 ksi.ksi_code = ucode;
1116 ksi.ksi_addr = (void *)addr;
1117 ksi.ksi_trapno = type;
1118 trapsignal(td, &ksi);
1122 * Note: we should only get here if returning to user mode.
1124 userret(td, trapframe);
1125 return (trapframe->pc);
1128 #if !defined(SMP) && (defined(DDB) || defined(DEBUG))
1136 printf("trapDump(%s)\n", msg);
1137 for (i = 0; i < TRAPSIZE; i++) {
1138 if (trp == trapdebug) {
1139 trp = &trapdebug[TRAPSIZE - 1];
1144 if (trp->cause == 0)
1147 printf("%s: ADR %jx PC %jx CR %jx SR %jx\n",
1148 trap_type[(trp->cause & MIPS_CR_EXC_CODE) >>
1149 MIPS_CR_EXC_CODE_SHIFT],
1150 (intmax_t)trp->vadr, (intmax_t)trp->pc,
1151 (intmax_t)trp->cause, (intmax_t)trp->status);
1153 printf(" RA %jx SP %jx code %d\n", (intmax_t)trp->ra,
1154 (intmax_t)trp->sp, (int)trp->code);
1162 * Return the resulting PC as if the branch was executed.
1165 MipsEmulateBranch(struct trapframe *framePtr, uintptr_t instPC, int fpcCSR,
1169 register_t *regsPtr = (register_t *) framePtr;
1170 uintptr_t retAddr = 0;
1173 #define GetBranchDest(InstPtr, inst) \
1174 (InstPtr + 4 + ((short)inst.IType.imm << 2))
1178 if (instptr < MIPS_KSEG0_START)
1179 inst.word = fuword32((void *)instptr);
1181 inst = *(InstFmt *) instptr;
1183 if ((vm_offset_t)instPC < MIPS_KSEG0_START)
1184 inst.word = fuword32((void *)instPC);
1186 inst = *(InstFmt *) instPC;
1189 switch ((int)inst.JType.op) {
1191 switch ((int)inst.RType.func) {
1194 retAddr = regsPtr[inst.RType.rs];
1198 retAddr = instPC + 4;
1204 switch ((int)inst.IType.rt) {
1209 if ((int)(regsPtr[inst.RType.rs]) < 0)
1210 retAddr = GetBranchDest(instPC, inst);
1212 retAddr = instPC + 8;
1219 if ((int)(regsPtr[inst.RType.rs]) >= 0)
1220 retAddr = GetBranchDest(instPC, inst);
1222 retAddr = instPC + 8;
1231 retAddr = instPC + 4; /* Like syscall... */
1235 panic("MipsEmulateBranch: Bad branch cond");
1241 retAddr = (inst.JType.target << 2) |
1242 ((unsigned)(instPC + 4) & 0xF0000000);
1247 if (regsPtr[inst.RType.rs] == regsPtr[inst.RType.rt])
1248 retAddr = GetBranchDest(instPC, inst);
1250 retAddr = instPC + 8;
1255 if (regsPtr[inst.RType.rs] != regsPtr[inst.RType.rt])
1256 retAddr = GetBranchDest(instPC, inst);
1258 retAddr = instPC + 8;
1263 if ((int)(regsPtr[inst.RType.rs]) <= 0)
1264 retAddr = GetBranchDest(instPC, inst);
1266 retAddr = instPC + 8;
1271 if ((int)(regsPtr[inst.RType.rs]) > 0)
1272 retAddr = GetBranchDest(instPC, inst);
1274 retAddr = instPC + 8;
1278 switch (inst.RType.rs) {
1281 if ((inst.RType.rt & COPz_BC_TF_MASK) == COPz_BC_TRUE)
1282 condition = fpcCSR & MIPS_FPU_COND_BIT;
1284 condition = !(fpcCSR & MIPS_FPU_COND_BIT);
1286 retAddr = GetBranchDest(instPC, inst);
1288 retAddr = instPC + 8;
1292 retAddr = instPC + 4;
1297 retAddr = instPC + 4;
1303 log_frame_dump(struct trapframe *frame)
1305 log(LOG_ERR, "Trapframe Register Dump:\n");
1306 log(LOG_ERR, "\tzero: %#jx\tat: %#jx\tv0: %#jx\tv1: %#jx\n",
1307 (intmax_t)0, (intmax_t)frame->ast, (intmax_t)frame->v0, (intmax_t)frame->v1);
1309 log(LOG_ERR, "\ta0: %#jx\ta1: %#jx\ta2: %#jx\ta3: %#jx\n",
1310 (intmax_t)frame->a0, (intmax_t)frame->a1, (intmax_t)frame->a2, (intmax_t)frame->a3);
1312 #if defined(__mips_n32) || defined(__mips_n64)
1313 log(LOG_ERR, "\ta4: %#jx\ta5: %#jx\ta6: %#jx\ta6: %#jx\n",
1314 (intmax_t)frame->a4, (intmax_t)frame->a5, (intmax_t)frame->a6, (intmax_t)frame->a7);
1316 log(LOG_ERR, "\tt0: %#jx\tt1: %#jx\tt2: %#jx\tt3: %#jx\n",
1317 (intmax_t)frame->t0, (intmax_t)frame->t1, (intmax_t)frame->t2, (intmax_t)frame->t3);
1319 log(LOG_ERR, "\tt0: %#jx\tt1: %#jx\tt2: %#jx\tt3: %#jx\n",
1320 (intmax_t)frame->t0, (intmax_t)frame->t1, (intmax_t)frame->t2, (intmax_t)frame->t3);
1322 log(LOG_ERR, "\tt4: %#jx\tt5: %#jx\tt6: %#jx\tt7: %#jx\n",
1323 (intmax_t)frame->t4, (intmax_t)frame->t5, (intmax_t)frame->t6, (intmax_t)frame->t7);
1325 log(LOG_ERR, "\tt8: %#jx\tt9: %#jx\ts0: %#jx\ts1: %#jx\n",
1326 (intmax_t)frame->t8, (intmax_t)frame->t9, (intmax_t)frame->s0, (intmax_t)frame->s1);
1328 log(LOG_ERR, "\ts2: %#jx\ts3: %#jx\ts4: %#jx\ts5: %#jx\n",
1329 (intmax_t)frame->s2, (intmax_t)frame->s3, (intmax_t)frame->s4, (intmax_t)frame->s5);
1331 log(LOG_ERR, "\ts6: %#jx\ts7: %#jx\tk0: %#jx\tk1: %#jx\n",
1332 (intmax_t)frame->s6, (intmax_t)frame->s7, (intmax_t)frame->k0, (intmax_t)frame->k1);
1334 log(LOG_ERR, "\tgp: %#jx\tsp: %#jx\ts8: %#jx\tra: %#jx\n",
1335 (intmax_t)frame->gp, (intmax_t)frame->sp, (intmax_t)frame->s8, (intmax_t)frame->ra);
1337 log(LOG_ERR, "\tsr: %#jx\tmullo: %#jx\tmulhi: %#jx\tbadvaddr: %#jx\n",
1338 (intmax_t)frame->sr, (intmax_t)frame->mullo, (intmax_t)frame->mulhi, (intmax_t)frame->badvaddr);
1340 log(LOG_ERR, "\tcause: %#jx\tpc: %#jx\n",
1341 (intmax_t)frame->cause, (intmax_t)frame->pc);
1346 trap_frame_dump(struct trapframe *frame)
1348 printf("Trapframe Register Dump:\n");
1349 printf("\tzero: %#jx\tat: %#jx\tv0: %#jx\tv1: %#jx\n",
1350 (intmax_t)0, (intmax_t)frame->ast, (intmax_t)frame->v0, (intmax_t)frame->v1);
1352 printf("\ta0: %#jx\ta1: %#jx\ta2: %#jx\ta3: %#jx\n",
1353 (intmax_t)frame->a0, (intmax_t)frame->a1, (intmax_t)frame->a2, (intmax_t)frame->a3);
1354 #if defined(__mips_n32) || defined(__mips_n64)
1355 printf("\ta4: %#jx\ta5: %#jx\ta6: %#jx\ta7: %#jx\n",
1356 (intmax_t)frame->a4, (intmax_t)frame->a5, (intmax_t)frame->a6, (intmax_t)frame->a7);
1358 printf("\tt0: %#jx\tt1: %#jx\tt2: %#jx\tt3: %#jx\n",
1359 (intmax_t)frame->t0, (intmax_t)frame->t1, (intmax_t)frame->t2, (intmax_t)frame->t3);
1361 printf("\tt0: %#jx\tt1: %#jx\tt2: %#jx\tt3: %#jx\n",
1362 (intmax_t)frame->t0, (intmax_t)frame->t1, (intmax_t)frame->t2, (intmax_t)frame->t3);
1364 printf("\tt4: %#jx\tt5: %#jx\tt6: %#jx\tt7: %#jx\n",
1365 (intmax_t)frame->t4, (intmax_t)frame->t5, (intmax_t)frame->t6, (intmax_t)frame->t7);
1367 printf("\tt8: %#jx\tt9: %#jx\ts0: %#jx\ts1: %#jx\n",
1368 (intmax_t)frame->t8, (intmax_t)frame->t9, (intmax_t)frame->s0, (intmax_t)frame->s1);
1370 printf("\ts2: %#jx\ts3: %#jx\ts4: %#jx\ts5: %#jx\n",
1371 (intmax_t)frame->s2, (intmax_t)frame->s3, (intmax_t)frame->s4, (intmax_t)frame->s5);
1373 printf("\ts6: %#jx\ts7: %#jx\tk0: %#jx\tk1: %#jx\n",
1374 (intmax_t)frame->s6, (intmax_t)frame->s7, (intmax_t)frame->k0, (intmax_t)frame->k1);
1376 printf("\tgp: %#jx\tsp: %#jx\ts8: %#jx\tra: %#jx\n",
1377 (intmax_t)frame->gp, (intmax_t)frame->sp, (intmax_t)frame->s8, (intmax_t)frame->ra);
1379 printf("\tsr: %#jx\tmullo: %#jx\tmulhi: %#jx\tbadvaddr: %#jx\n",
1380 (intmax_t)frame->sr, (intmax_t)frame->mullo, (intmax_t)frame->mulhi, (intmax_t)frame->badvaddr);
1382 printf("\tcause: %#jx\tpc: %#jx\n",
1383 (intmax_t)frame->cause, (intmax_t)frame->pc);
1390 get_mapping_info(vm_offset_t va, pd_entry_t **pdepp, pt_entry_t **ptepp)
1394 struct proc *p = curproc;
1396 pdep = (&(p->p_vmspace->vm_pmap.pm_segtab[(va >> SEGSHIFT) & (NPDEPG - 1)]));
1398 ptep = pmap_pte(&p->p_vmspace->vm_pmap, va);
1400 ptep = (pt_entry_t *)0;
1407 log_illegal_instruction(const char *msg, struct trapframe *frame)
1420 printf("cpuid = %d\n", PCPU_GET(cpuid));
1422 pc = frame->pc + (DELAYBRANCH(frame->cause) ? 4 : 0);
1423 log(LOG_ERR, "%s: pid %d tid %ld (%s), uid %d: pc %#jx ra %#jx\n",
1424 msg, p->p_pid, (long)td->td_tid, p->p_comm,
1425 p->p_ucred ? p->p_ucred->cr_uid : -1,
1427 (intmax_t)frame->ra);
1429 /* log registers in trap frame */
1430 log_frame_dump(frame);
1432 get_mapping_info((vm_offset_t)pc, &pdep, &ptep);
1435 * Dump a few words around faulting instruction, if the addres is
1439 useracc((caddr_t)(intptr_t)pc, sizeof(int) * 4, VM_PROT_READ)) {
1440 /* dump page table entry for faulting instruction */
1441 log(LOG_ERR, "Page table info for pc address %#jx: pde = %p, pte = %#jx\n",
1442 (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
1444 addr = (unsigned int *)(intptr_t)pc;
1445 log(LOG_ERR, "Dumping 4 words starting at pc address %p: \n",
1447 log(LOG_ERR, "%08x %08x %08x %08x\n",
1448 addr[0], addr[1], addr[2], addr[3]);
1450 log(LOG_ERR, "pc address %#jx is inaccessible, pde = %p, pte = %#jx\n",
1451 (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
1456 log_bad_page_fault(char *msg, struct trapframe *frame, int trap_type)
1463 char *read_or_write;
1466 trap_type &= ~T_USER;
1472 printf("cpuid = %d\n", PCPU_GET(cpuid));
1474 switch (trap_type) {
1478 read_or_write = "write";
1482 case T_BUS_ERR_IFETCH:
1483 read_or_write = "read";
1486 read_or_write = "unknown";
1489 pc = frame->pc + (DELAYBRANCH(frame->cause) ? 4 : 0);
1490 log(LOG_ERR, "%s: pid %d tid %ld (%s), uid %d: pc %#jx got a %s fault "
1491 "(type %#x) at %#jx\n",
1492 msg, p->p_pid, (long)td->td_tid, p->p_comm,
1493 p->p_ucred ? p->p_ucred->cr_uid : -1,
1497 (intmax_t)frame->badvaddr);
1499 /* log registers in trap frame */
1500 log_frame_dump(frame);
1502 get_mapping_info((vm_offset_t)pc, &pdep, &ptep);
1505 * Dump a few words around faulting instruction, if the addres is
1508 if (!(pc & 3) && (pc != frame->badvaddr) &&
1509 (trap_type != T_BUS_ERR_IFETCH) &&
1510 useracc((caddr_t)(intptr_t)pc, sizeof(int) * 4, VM_PROT_READ)) {
1511 /* dump page table entry for faulting instruction */
1512 log(LOG_ERR, "Page table info for pc address %#jx: pde = %p, pte = %#jx\n",
1513 (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
1515 addr = (unsigned int *)(intptr_t)pc;
1516 log(LOG_ERR, "Dumping 4 words starting at pc address %p: \n",
1518 log(LOG_ERR, "%08x %08x %08x %08x\n",
1519 addr[0], addr[1], addr[2], addr[3]);
1521 log(LOG_ERR, "pc address %#jx is inaccessible, pde = %p, pte = %#jx\n",
1522 (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
1525 get_mapping_info((vm_offset_t)frame->badvaddr, &pdep, &ptep);
1526 log(LOG_ERR, "Page table info for bad address %#jx: pde = %p, pte = %#jx\n",
1527 (intmax_t)frame->badvaddr, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
1532 * Unaligned load/store emulation
1535 mips_unaligned_load_store(struct trapframe *frame, int mode, register_t addr, register_t pc)
1537 register_t *reg = (register_t *) frame;
1538 u_int32_t inst = *((u_int32_t *)(intptr_t)pc);
1539 register_t value_msb, value;
1543 * ADDR_ERR faults have higher priority than TLB
1544 * Miss faults. Therefore, it is necessary to
1545 * verify that the faulting address is a valid
1546 * virtual address within the process' address space
1547 * before trying to emulate the unaligned access.
1549 switch (MIPS_INST_OPCODE(inst)) {
1550 case OP_LHU: case OP_LH:
1554 case OP_LWU: case OP_LW:
1563 printf("%s: unhandled opcode in address error: %#x\n", __func__, MIPS_INST_OPCODE(inst));
1567 if (!useracc((void *)rounddown2((vm_offset_t)addr, size), size * 2, mode))
1572 * Handle LL/SC LLD/SCD.
1574 switch (MIPS_INST_OPCODE(inst)) {
1576 KASSERT(mode == VM_PROT_READ, ("access mode must be read for load instruction."));
1577 lbu_macro(value_msb, addr);
1579 lbu_macro(value, addr);
1580 value |= value_msb << 8;
1581 reg[MIPS_INST_RT(inst)] = value;
1582 return (MIPS_LHU_ACCESS);
1585 KASSERT(mode == VM_PROT_READ, ("access mode must be read for load instruction."));
1586 lb_macro(value_msb, addr);
1588 lbu_macro(value, addr);
1589 value |= value_msb << 8;
1590 reg[MIPS_INST_RT(inst)] = value;
1591 return (MIPS_LH_ACCESS);
1594 KASSERT(mode == VM_PROT_READ, ("access mode must be read for load instruction."));
1595 lwl_macro(value, addr);
1597 lwr_macro(value, addr);
1598 value &= 0xffffffff;
1599 reg[MIPS_INST_RT(inst)] = value;
1600 return (MIPS_LWU_ACCESS);
1603 KASSERT(mode == VM_PROT_READ, ("access mode must be read for load instruction."));
1604 lwl_macro(value, addr);
1606 lwr_macro(value, addr);
1607 reg[MIPS_INST_RT(inst)] = value;
1608 return (MIPS_LW_ACCESS);
1610 #if defined(__mips_n32) || defined(__mips_n64)
1612 KASSERT(mode == VM_PROT_READ, ("access mode must be read for load instruction."));
1613 ldl_macro(value, addr);
1615 ldr_macro(value, addr);
1616 reg[MIPS_INST_RT(inst)] = value;
1617 return (MIPS_LD_ACCESS);
1621 KASSERT(mode == VM_PROT_WRITE, ("access mode must be write for store instruction."));
1622 value = reg[MIPS_INST_RT(inst)];
1623 value_msb = value >> 8;
1624 sb_macro(value_msb, addr);
1626 sb_macro(value, addr);
1627 return (MIPS_SH_ACCESS);
1630 KASSERT(mode == VM_PROT_WRITE, ("access mode must be write for store instruction."));
1631 value = reg[MIPS_INST_RT(inst)];
1632 swl_macro(value, addr);
1634 swr_macro(value, addr);
1635 return (MIPS_SW_ACCESS);
1637 #if defined(__mips_n32) || defined(__mips_n64)
1639 KASSERT(mode == VM_PROT_WRITE, ("access mode must be write for store instruction."));
1640 value = reg[MIPS_INST_RT(inst)];
1641 sdl_macro(value, addr);
1643 sdr_macro(value, addr);
1644 return (MIPS_SD_ACCESS);
1647 panic("%s: should not be reached.", __func__);
1654 static struct timeval unaligned_lasterr;
1655 static int unaligned_curerr;
1657 static int unaligned_pps_log_limit = 4;
1659 SYSCTL_INT(_machdep, OID_AUTO, unaligned_log_pps_limit, CTLFLAG_RWTUN,
1660 &unaligned_pps_log_limit, 0,
1661 "limit number of userland unaligned log messages per second");
1664 emulate_unaligned_access(struct trapframe *frame, int mode)
1667 int access_type = 0;
1668 struct thread *td = curthread;
1669 struct proc *p = curproc;
1671 pc = frame->pc + (DELAYBRANCH(frame->cause) ? 4 : 0);
1674 * Fall through if it's instruction fetch exception
1676 if (!((pc & 3) || (pc == frame->badvaddr))) {
1679 * Handle unaligned load and store
1683 * Return access type if the instruction was emulated.
1684 * Otherwise restore pc and fall through.
1686 access_type = mips_unaligned_load_store(frame,
1687 mode, frame->badvaddr, pc);
1690 if (DELAYBRANCH(frame->cause))
1691 frame->pc = MipsEmulateBranch(frame, frame->pc,
1696 if (ppsratecheck(&unaligned_lasterr,
1697 &unaligned_curerr, unaligned_pps_log_limit)) {
1698 /* XXX TODO: keep global/tid/pid counters? */
1700 "Unaligned %s: pid=%ld (%s), tid=%ld, "
1701 "pc=%#jx, badvaddr=%#jx\n",
1702 access_name[access_type - 1],
1707 (intmax_t)frame->badvaddr);