#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/signal.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/syslog.h>
#include <sys/vmmeter.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/signalvar.h>
#include <sys/ptrace.h>
#include <sys/vmmeter.h>
#ifdef KDB
#include <sys/kdb.h>
#endif
#include <vm/vm.h>
#include <vm/vm_extern.h>
#include <machine/armreg.h>
#include <machine/asm.h>
#include <machine/cpu.h>
#include <machine/frame.h>
#include <machine/undefined.h>
#include <machine/trap.h>
#include <machine/disassem.h>
#ifdef DDB
#include <ddb/db_output.h>
#endif
#ifdef KDB
#include <machine/db_machdep.h>
#endif
#define ARM_COPROC_INSN(insn) (((insn) & (1 << 27)) != 0)
#define ARM_VFP_INSN(insn) ((((insn) & 0xfe000000) == 0xf2000000) || \
(((insn) & 0xff100000) == 0xf4000000))
#define ARM_COPROC(insn) (((insn) >> 8) & 0xf)
#define THUMB_32BIT_INSN(insn) ((((insn) & 0xe000) == 0xe000) && \
(((insn) & 0x1800) != 0))
#define THUMB_COPROC_INSN(insn) (((insn) & (3 << 26)) == (3 << 26))
#define THUMB_VFP_INSN(insn) (((insn) & (0x1F1 << 20)) == (0x190 << 20))
#define THUMB_COPROC(insn) (((insn) >> 8) & 0xf)
#define COPROC_VFP 10
static int gdb_trapper(u_int, u_int, struct trapframe *, int);
LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
void *
install_coproc_handler(int coproc, undef_handler_t handler)
{
struct undefined_handler *uh;
KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
KASSERT(handler != NULL, ("handler is NULL"));
uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
uh->uh_handler = handler;
install_coproc_handler_static(coproc, uh);
return uh;
}
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);
free(uh, M_TEMP);
}
static int
gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
{
struct thread *td;
ksiginfo_t ksi;
int error;
td = (curthread == NULL) ? &thread0 : curthread;
if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
if (code == FAULT_USER) {
ksiginfo_init_trap(&ksi);
ksi.ksi_signo = SIGTRAP;
ksi.ksi_code = TRAP_BRKPT;
ksi.ksi_addr = (u_int32_t *)addr;
trapsignal(td, &ksi);
return 0;
}
#if 0
#ifdef KGDB
return !kgdb_trap(T_BREAKPOINT, frame);
#endif
#endif
}
if (code == FAULT_USER) {
if ((frame->tf_spsr & PSR_T) == 0 &&
insn == PTRACE_BREAKPOINT) {
PROC_LOCK(td->td_proc);
_PHOLD(td->td_proc);
error = ptrace_clear_single_step(td);
_PRELE(td->td_proc);
PROC_UNLOCK(td->td_proc);
if (error == 0) {
ksiginfo_init_trap(&ksi);
ksi.ksi_signo = SIGTRAP;
ksi.ksi_code = TRAP_TRACE;
ksi.ksi_addr = (u_int32_t *)addr;
trapsignal(td, &ksi);
return (0);
}
}
}
return 1;
}
static struct undefined_handler gdb_uh;
void
undefined_init(void)
{
int loop;
for (loop = 0; loop < MAX_COPROCS; ++loop)
LIST_INIT(&undefined_handlers[loop]);
gdb_uh.uh_handler = gdb_trapper;
install_coproc_handler_static(0, &gdb_uh);
}
void
undefinedinstruction(struct trapframe *frame)
{
struct thread *td;
u_int fault_pc;
int fault_instruction;
int fault_code;
int coprocessor;
struct undefined_handler *uh;
#ifdef VERBOSE_ARM32
int s;
#endif
ksiginfo_t ksi;
if (__predict_true(frame->tf_spsr & PSR_I) == 0)
enable_interrupts(PSR_I);
VM_CNT_INC(v_trap);
fault_pc = frame->tf_pc;
td = curthread == NULL ? &thread0 : curthread;
coprocessor = 0;
if ((frame->tf_spsr & PSR_T) == 0) {
if (__predict_false((fault_pc & 3) != 0)) {
ksiginfo_init_trap(&ksi);
ksi.ksi_signo = SIGILL;
ksi.ksi_code = ILL_ILLADR;
ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
trapsignal(td, &ksi);
userret(td, frame);
return;
}
fault_instruction = *(u_int32_t *)fault_pc;
if (ARM_COPROC_INSN(fault_instruction))
coprocessor = ARM_COPROC(fault_instruction);
else {
if (ARM_VFP_INSN(fault_instruction))
coprocessor = COPROC_VFP;
}
} else {
fault_instruction = *(uint16_t *)fault_pc;
if (THUMB_32BIT_INSN(fault_instruction)) {
fault_instruction <<= 16;
fault_instruction |= *(uint16_t *)(fault_pc + 2);
if (THUMB_COPROC_INSN(fault_instruction))
coprocessor = THUMB_COPROC(fault_instruction);
else {
if (THUMB_VFP_INSN(fault_instruction))
coprocessor = COPROC_VFP;
}
}
}
if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
fault_code = FAULT_USER;
td->td_frame = frame;
} else
fault_code = 0;
LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
if (uh->uh_handler(fault_pc, fault_instruction, frame,
fault_code) == 0)
break;
if (uh == NULL && (fault_code & FAULT_USER)) {
ksiginfo_init_trap(&ksi);
ksi.ksi_signo = SIGILL;
ksi.ksi_code = ILL_ILLOPC;
ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
trapsignal(td, &ksi);
}
if ((fault_code & FAULT_USER) == 0) {
if (fault_instruction == KERNEL_BREAKPOINT) {
#ifdef KDB
kdb_trap(T_BREAKPOINT, 0, frame);
#else
printf("No debugger in kernel.\n");
#endif
} else if (uh == NULL) {
panic("Undefined instruction in kernel (0x%08x)",
fault_instruction);
}
return;
}
userret(td, frame);
}