#include "opt_cputypes.h"
#include "opt_ddb.h"
#include "opt_dtrace.h"
#include "opt_kgdb.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: undefined.c,v 1.75 2023/10/05 19:41:03 ad Exp $");
#include <sys/param.h>
#include <sys/cpu.h>
#ifdef KGDB
#include <sys/kgdb.h>
#endif
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/signal.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <arm/locore.h>
#include <arm/undefined.h>
#include <machine/pcb.h>
#include <machine/trap.h>
#ifdef VERBOSE_ARM32
#include <arch/arm/arm/disassem.h>
#endif
#ifdef DDB
#include <ddb/db_output.h>
#include <machine/db_machdep.h>
#endif
static int gdb_trapper(u_int, u_int, struct trapframe *, int);
LIST_HEAD(, undefined_handler) undefined_handlers[NUM_UNKNOWN_HANDLERS];
void *
install_coproc_handler(int coproc, undef_handler_t handler)
{
struct undefined_handler *uh;
KASSERT(coproc >= 0 && coproc < NUM_UNKNOWN_HANDLERS);
KASSERT(handler != NULL);
uh = kmem_alloc(sizeof(*uh), KM_SLEEP);
uh->uh_handler = handler;
install_coproc_handler_static(coproc, uh);
return uh;
}
void
replace_coproc_handler(int coproc, undef_handler_t handler)
{
LIST_INIT(&undefined_handlers[coproc]);
install_coproc_handler(coproc, handler);
}
void
install_coproc_handler_static(int coproc, struct undefined_handler *uh)
{
LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
}
void
remove_coproc_handler(void *cookie)
{
struct undefined_handler *uh = cookie;
LIST_REMOVE(uh, uh_link);
kmem_free(uh, sizeof(*uh));
}
static int
cp15_trapper(u_int addr, u_int insn, struct trapframe *tf, int code)
{
struct lwp * const l = curlwp;
#if defined(THUMB_CODE) && !defined(CPU_ARMV7)
if (tf->tf_spsr & PSR_T_bit)
return 1;
#endif
if (code != FAULT_USER)
return 1;
const u_int regno = (insn >> 12) & 15;
if (regno == 13 || regno == 15)
return 1;
register_t * const regp = &tf->tf_r0 + regno;
if ((insn & 0xffff0fff) == 0xee1d0f70) {
*regp = (uintptr_t)l->l_private;
tf->tf_pc += INSN_SIZE;
curcpu()->ci_und_cp15_ev.ev_count++;
return 0;
}
if ((insn & 0xffef0fff) == 0xee0d0f50) {
struct pcb * const pcb = lwp_getpcb(l);
if (insn & 0x00100000)
*regp = pcb->pcb_user_pid_rw;
else
pcb->pcb_user_pid_rw = *regp;
tf->tf_pc += INSN_SIZE;
curcpu()->ci_und_cp15_ev.ev_count++;
return 0;
}
return 1;
}
static int
gdb_trapper(u_int addr, u_int insn, struct trapframe *tf, int code)
{
struct lwp * const l = curlwp;
#ifdef THUMB_CODE
if (tf->tf_spsr & PSR_T_bit) {
if (insn == GDB_THUMB_BREAKPOINT)
goto bkpt;
}
else
#endif
{
if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
#ifdef THUMB_CODE
bkpt:
#endif
if (code == FAULT_USER) {
ksiginfo_t ksi;
KSI_INIT_TRAP(&ksi);
ksi.ksi_signo = SIGTRAP;
ksi.ksi_code = TRAP_BRKPT;
ksi.ksi_addr = (uint32_t *)addr;
ksi.ksi_trap = 0;
trapsignal(l, &ksi);
return 0;
}
#ifdef KGDB
return !kgdb_trap(T_BREAKPOINT, tf);
#endif
}
}
return 1;
}
#ifdef FPU_VFP
static int
vfp_test(u_int address, u_int insn, trapframe_t *frame, int fault_code)
{
struct cpu_info * const ci = curcpu();
frame->tf_pc += INSN_SIZE;
ci->ci_vfp_id = 0;
return 0;
}
#endif
static struct undefined_handler cp15_uh = {
.uh_handler = cp15_trapper,
};
static struct undefined_handler gdb_uh = {
.uh_handler = gdb_trapper,
};
#ifdef THUMB_CODE
static struct undefined_handler gdb_uh_thumb = {
.uh_handler = gdb_trapper,
};
#endif
#ifdef FPU_VFP
struct undefined_handler vfptest_uh = {
.uh_handler = vfp_test,
};
#endif
#ifdef KDTRACE_HOOKS
#include <sys/dtrace_bsd.h>
dtrace_doubletrap_func_t dtrace_doubletrap_func = NULL;
dtrace_trap_func_t dtrace_trap_func = NULL;
int (* dtrace_invop_jump_addr)(struct trapframe *);
static int
dtrace_trapper(u_int addr, struct trapframe *frame)
{
u_int insn = read_insn(addr, false);
if (dtrace_invop_jump_addr == NULL)
return 1;
if (!DTRACE_IS_BREAKPOINT(insn))
return 1;
if (!arm_cond_ok_p(__SHIFTIN(insn, INSN_COND_MASK), frame->tf_spsr)) {
frame->tf_pc += INSN_SIZE;
return 0;
}
dtrace_invop_jump_addr(frame);
return 0;
}
#endif
void
undefined_init(void)
{
int loop;
for (loop = 0; loop < NUM_UNKNOWN_HANDLERS; ++loop)
LIST_INIT(&undefined_handlers[loop]);
install_coproc_handler_static(SYSTEM_COPROC, &cp15_uh);
install_coproc_handler_static(CORE_UNKNOWN_HANDLER, &gdb_uh);
#ifdef THUMB_CODE
install_coproc_handler_static(THUMB_UNKNOWN_HANDLER, &gdb_uh_thumb);
#endif
#ifdef FPU_VFP
install_coproc_handler_static(VFP_COPROC, &vfptest_uh);
#endif
}
void
undefinedinstruction(trapframe_t *tf)
{
struct lwp *l;
vaddr_t fault_pc;
int fault_instruction;
int fault_code;
int coprocessor;
int user;
struct undefined_handler *uh;
curcpu()->ci_und_ev.ev_count++;
#ifdef KDTRACE_HOOKS
if ((tf->tf_spsr & PSR_MODE) != PSR_USR32_MODE) {
tf->tf_pc -= INSN_SIZE;
if (dtrace_trapper(tf->tf_pc, tf) == 0)
return;
tf->tf_pc += INSN_SIZE;
}
#endif
restore_interrupts(tf->tf_spsr & IF32_bits);
#ifdef THUMB_CODE
if (tf->tf_spsr & PSR_T_bit)
tf->tf_pc -= THUMB_INSN_SIZE;
else
#endif
{
tf->tf_pc -= INSN_SIZE;
}
fault_pc = tf->tf_pc;
l = curlwp;
if ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
user = 1;
} else
user = 0;
#ifdef THUMB_CODE
if (tf->tf_spsr & PSR_T_bit) {
fault_instruction = read_thumb_insn(fault_pc, user);
if (fault_instruction >= 0xe000) {
fault_instruction = (fault_instruction << 16)
| read_thumb_insn(fault_pc + 2, user);
}
}
else
#endif
{
if (__predict_false((fault_pc & 3) != 0)) {
ksiginfo_t ksi;
KSI_INIT_TRAP(&ksi);
ksi.ksi_signo = SIGILL;
ksi.ksi_code = ILL_ILLOPC;
ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc;
trapsignal(l, &ksi);
userret(l);
return;
}
fault_instruction = read_insn(fault_pc, user);
}
curcpu()->ci_data.cpu_ntrap++;
#ifdef THUMB_CODE
if ((tf->tf_spsr & PSR_T_bit) && !CPU_IS_ARMV7_P()) {
coprocessor = THUMB_UNKNOWN_HANDLER;
}
else
#endif
{
if ((fault_instruction & (1 << 27)) != 0
&& (fault_instruction & 0xf0000000) != 0xf0000000) {
coprocessor = (fault_instruction >> 8) & 0x0f;
#ifdef THUMB_CODE
} else if ((tf->tf_spsr & PSR_T_bit) && !CPU_IS_ARMV7_P()) {
coprocessor = THUMB_UNKNOWN_HANDLER;
#endif
} else {
coprocessor = CORE_UNKNOWN_HANDLER;
}
}
if (user) {
fault_code = FAULT_USER;
KASSERTMSG(tf == lwp_trapframe(l), "tf %p vs %p", tf,
lwp_trapframe(l));
} else
fault_code = 0;
LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) {
int ret = uh->uh_handler(fault_pc, fault_instruction, tf,
fault_code);
if (ret == 0)
break;
}
if (uh == NULL) {
ksiginfo_t ksi;
#ifdef VERBOSE_ARM32
int s = spltty();
if ((fault_instruction & 0x0f000010) == 0x0e000000) {
printf("CDP\n");
disassemble(fault_pc);
} else if ((fault_instruction & 0x0e000000) == 0x0c000000) {
printf("LDC/STC\n");
disassemble(fault_pc);
} else if ((fault_instruction & 0x0f000010) == 0x0e000010) {
printf("MRC/MCR\n");
disassemble(fault_pc);
} else if ((fault_instruction & ~INSN_COND_MASK)
!= (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) {
printf("Undefined instruction\n");
disassemble(fault_pc);
}
splx(s);
#endif
if ((fault_code & FAULT_USER) == 0) {
#ifdef DDB
db_printf("Undefined instruction %#x in kernel at %#lx (LR %#x SP %#x)\n",
fault_instruction, fault_pc, tf->tf_svc_lr, tf->tf_svc_sp);
kdb_trap(T_FAULT, tf);
#else
panic("undefined instruction %#x in kernel at %#lx", fault_instruction, fault_pc);
#endif
}
KSI_INIT_TRAP(&ksi);
ksi.ksi_signo = SIGILL;
ksi.ksi_code = ILL_ILLOPC;
ksi.ksi_addr = (uint32_t *)fault_pc;
ksi.ksi_trap = fault_instruction;
trapsignal(l, &ksi);
}
if ((fault_code & FAULT_USER) == 0)
return;
userret(l);
}