#include <sys/fasttrap_isa.h>
#include <sys/fasttrap_impl.h>
#include <sys/dtrace.h>
#include <sys/dtrace_impl.h>
#include <sys/cmn_err.h>
#include <sys/frame.h>
#include <sys/stack.h>
#include <sys/sysmacros.h>
#include <sys/trap.h>
#include <v9/sys/machpcb.h>
#include <v9/sys/privregs.h>
#define OP(x) ((x) >> 30)
#define OP2(x) (((x) >> 22) & 0x07)
#define OP3(x) (((x) >> 19) & 0x3f)
#define RCOND(x) (((x) >> 25) & 0x07)
#define COND(x) (((x) >> 25) & 0x0f)
#define A(x) (((x) >> 29) & 0x01)
#define I(x) (((x) >> 13) & 0x01)
#define RD(x) (((x) >> 25) & 0x1f)
#define RS1(x) (((x) >> 14) & 0x1f)
#define RS2(x) (((x) >> 0) & 0x1f)
#define CC(x) (((x) >> 20) & 0x03)
#define DISP16(x) ((((x) >> 6) & 0xc000) | ((x) & 0x3fff))
#define DISP22(x) ((x) & 0x3fffff)
#define DISP19(x) ((x) & 0x7ffff)
#define DISP30(x) ((x) & 0x3fffffff)
#define SW_TRAP(x) ((x) & 0x7f)
#define OP3_OR 0x02
#define OP3_RD 0x28
#define OP3_JMPL 0x38
#define OP3_RETURN 0x39
#define OP3_TCC 0x3a
#define OP3_SAVE 0x3c
#define OP3_RESTORE 0x3d
#define OP3_PREFETCH 0x2d
#define OP3_CASA 0x3c
#define OP3_PREFETCHA 0x3d
#define OP3_CASXA 0x3e
#define OP2_ILLTRAP 0x0
#define OP2_BPcc 0x1
#define OP2_Bicc 0x2
#define OP2_BPr 0x3
#define OP2_SETHI 0x4
#define OP2_FBPfcc 0x5
#define OP2_FBfcc 0x6
#define R_G0 0
#define R_O0 8
#define R_SP 14
#define R_I0 24
#define R_I1 25
#define R_I2 26
#define R_I3 27
#define R_I4 28
#define FASTTRAP_OFF_SAVE 64
#define FASTTRAP_OFF_RESTORE 68
#define FASTTRAP_OFF_FTRET 72
#define FASTTRAP_OFF_RETURN 76
#define BREAKPOINT_INSTR 0x91d02001
int fasttrap_optimize_save = 1;
static uint64_t
fasttrap_anarg(struct regs *rp, int argno)
{
uint64_t value;
if (argno < 6)
return ((&rp->r_o0)[argno]);
if (curproc->p_model == DATAMODEL_NATIVE) {
struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
value = dtrace_fulword(&fr->fr_argd[argno]);
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
CPU_DTRACE_BADALIGN);
} else {
struct frame32 *fr = (struct frame32 *)rp->r_sp;
DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
value = dtrace_fuword32(&fr->fr_argd[argno]);
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
CPU_DTRACE_BADALIGN);
}
return (value);
}
static ulong_t fasttrap_getreg(struct regs *, uint_t);
static void fasttrap_putreg(struct regs *, uint_t, ulong_t);
static void
fasttrap_usdt_args(fasttrap_probe_t *probe, struct regs *rp,
uint_t fake_restore, int argc, uintptr_t *argv)
{
int i, x, cap = MIN(argc, probe->ftp_nargs);
int inc = (fake_restore ? 16 : 0);
if (curproc->p_model == DATAMODEL_NATIVE) {
struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
uintptr_t v;
for (i = 0; i < cap; i++) {
x = probe->ftp_argmap[i];
if (x < 6)
argv[i] = fasttrap_getreg(rp, R_O0 + x + inc);
else if (fasttrap_fulword(&fr->fr_argd[x], &v) != 0)
argv[i] = 0;
}
} else {
struct frame32 *fr = (struct frame32 *)rp->r_sp;
uint32_t v;
for (i = 0; i < cap; i++) {
x = probe->ftp_argmap[i];
if (x < 6)
argv[i] = fasttrap_getreg(rp, R_O0 + x + inc);
else if (fasttrap_fuword32(&fr->fr_argd[x], &v) != 0)
argv[i] = 0;
}
}
for (; i < argc; i++) {
argv[i] = 0;
}
}
static void
fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid,
uint_t fake_restore)
{
fasttrap_tracepoint_t *tp;
fasttrap_bucket_t *bucket;
fasttrap_id_t *id;
kmutex_t *pid_mtx;
dtrace_icookie_t cookie;
pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
mutex_enter(pid_mtx);
bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
tp->ftt_proc->ftpc_acount != 0)
break;
}
if (tp == NULL || tp->ftt_retids == NULL) {
mutex_exit(pid_mtx);
return;
}
for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
fasttrap_probe_t *probe = id->fti_probe;
if (id->fti_ptype == DTFTP_POST_OFFSETS) {
if (probe->ftp_argmap != NULL && fake_restore) {
uintptr_t t[5];
fasttrap_usdt_args(probe, rp, fake_restore,
sizeof (t) / sizeof (t[0]), t);
cookie = dtrace_interrupt_disable();
DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
dtrace_probe(probe->ftp_id, t[0], t[1],
t[2], t[3], t[4]);
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
dtrace_interrupt_enable(cookie);
} else if (probe->ftp_argmap != NULL) {
uintptr_t t[5];
fasttrap_usdt_args(probe, rp, fake_restore,
sizeof (t) / sizeof (t[0]), t);
dtrace_probe(probe->ftp_id, t[0], t[1],
t[2], t[3], t[4]);
} else if (fake_restore) {
uintptr_t arg0 = fasttrap_getreg(rp, R_I0);
uintptr_t arg1 = fasttrap_getreg(rp, R_I1);
uintptr_t arg2 = fasttrap_getreg(rp, R_I2);
uintptr_t arg3 = fasttrap_getreg(rp, R_I3);
uintptr_t arg4 = fasttrap_getreg(rp, R_I4);
cookie = dtrace_interrupt_disable();
DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
dtrace_probe(probe->ftp_id, arg0, arg1,
arg2, arg3, arg4);
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
dtrace_interrupt_enable(cookie);
} else {
dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1,
rp->r_o2, rp->r_o3, rp->r_o4);
}
continue;
}
if ((tp->ftt_flags & FASTTRAP_F_RETMAYBE) &&
rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
continue;
if (rp->r_npc == rp->r_pc + 4 &&
rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
continue;
if (!fake_restore) {
dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3);
} else {
uintptr_t arg0 = fasttrap_getreg(rp, R_I0);
uintptr_t arg1 = fasttrap_getreg(rp, R_I1);
uintptr_t arg2 = fasttrap_getreg(rp, R_I2);
uintptr_t arg3 = fasttrap_getreg(rp, R_I3);
cookie = dtrace_interrupt_disable();
DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
arg0, arg1, arg2, arg3);
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
dtrace_interrupt_enable(cookie);
}
}
mutex_exit(pid_mtx);
}
int
fasttrap_pid_probe(struct regs *rp)
{
proc_t *p = curproc;
fasttrap_tracepoint_t *tp, tp_local;
fasttrap_id_t *id;
pid_t pid;
uintptr_t pc = rp->r_pc;
uintptr_t npc = rp->r_npc;
uintptr_t orig_pc = pc;
fasttrap_bucket_t *bucket;
kmutex_t *pid_mtx;
uint_t fake_restore = 0, is_enabled = 0;
dtrace_icookie_t cookie;
if (curthread->t_dtrace_step) {
ASSERT(curthread->t_dtrace_on);
fasttrap_sigtrap(p, curthread, pc);
return (0);
}
curthread->t_dtrace_ft = 0;
curthread->t_dtrace_pc = 0;
curthread->t_dtrace_npc = 0;
curthread->t_dtrace_scrpc = 0;
curthread->t_dtrace_astpc = 0;
while (p->p_flag & SVFORK) {
p = p->p_parent;
}
pid = p->p_pid;
pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
mutex_enter(pid_mtx);
bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
tp->ftt_proc->ftpc_acount != 0)
break;
}
if (tp == NULL) {
mutex_exit(pid_mtx);
return (-1);
}
for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
fasttrap_probe_t *probe = id->fti_probe;
int isentry = (id->fti_ptype == DTFTP_ENTRY);
if (id->fti_ptype == DTFTP_IS_ENABLED) {
is_enabled = 1;
continue;
}
if (isentry) {
cookie = dtrace_interrupt_disable();
DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
}
dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1, rp->r_o2,
rp->r_o3, rp->r_o4);
if (isentry) {
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
dtrace_interrupt_enable(cookie);
}
}
tp_local = *tp;
mutex_exit(pid_mtx);
tp = &tp_local;
if (is_enabled) {
rp->r_o0 = 1;
pc = rp->r_npc;
npc = pc + 4;
goto done;
}
switch (tp->ftt_type) {
case FASTTRAP_T_SAVE:
{
int32_t imm;
imm = tp->ftt_instr << 19;
imm >>= 19;
rp->r_g1 = rp->r_sp + imm;
pc = rp->r_g7 + FASTTRAP_OFF_SAVE;
break;
}
case FASTTRAP_T_RESTORE:
{
ulong_t value;
uint_t rd;
if (I(tp->ftt_instr)) {
int32_t imm;
imm = tp->ftt_instr << 19;
imm >>= 19;
value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
} else {
value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
fasttrap_getreg(rp, RS2(tp->ftt_instr));
}
rd = RD(tp->ftt_instr);
fasttrap_putreg(rp, ((rd & 0x18) == 0x8) ? rd + 16 : rd, value);
pc = rp->r_g7 + FASTTRAP_OFF_RESTORE;
fake_restore = 1;
break;
}
case FASTTRAP_T_RETURN:
{
uintptr_t target;
if (I(tp->ftt_instr)) {
int32_t imm;
imm = tp->ftt_instr << 19;
imm >>= 19;
target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
} else {
target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
fasttrap_getreg(rp, RS2(tp->ftt_instr));
}
fasttrap_putreg(rp, R_O0, target);
pc = rp->r_g7 + FASTTRAP_OFF_RETURN;
fake_restore = 1;
break;
}
case FASTTRAP_T_OR:
{
ulong_t value;
if (I(tp->ftt_instr)) {
int32_t imm;
imm = tp->ftt_instr << 19;
imm >>= 19;
value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) | imm;
} else {
value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) |
fasttrap_getreg(rp, RS2(tp->ftt_instr));
}
fasttrap_putreg(rp, RD(tp->ftt_instr), value);
pc = rp->r_npc;
npc = pc + 4;
break;
}
case FASTTRAP_T_SETHI:
if (RD(tp->ftt_instr) != R_G0) {
uint32_t imm32 = tp->ftt_instr << 10;
fasttrap_putreg(rp, RD(tp->ftt_instr), (ulong_t)imm32);
}
pc = rp->r_npc;
npc = pc + 4;
break;
case FASTTRAP_T_CCR:
{
uint_t c, v, z, n, taken;
uint_t ccr = rp->r_tstate >> TSTATE_CCR_SHIFT;
if (tp->ftt_cc != 0)
ccr >>= 4;
c = (ccr >> 0) & 1;
v = (ccr >> 1) & 1;
z = (ccr >> 2) & 1;
n = (ccr >> 3) & 1;
switch (tp->ftt_code) {
case 0x0:
taken = 0; break;
case 0x1:
taken = z; break;
case 0x2:
taken = z | (n ^ v); break;
case 0x3:
taken = n ^ v; break;
case 0x4:
taken = c | z; break;
case 0x5:
taken = c; break;
case 0x6:
taken = n; break;
case 0x7:
taken = v; break;
case 0x8:
panic("fasttrap: mishandled a branch");
taken = 1; break;
case 0x9:
taken = ~z; break;
case 0xa:
taken = ~(z | (n ^ v)); break;
case 0xb:
taken = ~(n ^ v); break;
case 0xc:
taken = ~(c | z); break;
case 0xd:
taken = ~c; break;
case 0xe:
taken = ~n; break;
case 0xf:
taken = ~v; break;
}
if (taken & 1) {
pc = rp->r_npc;
npc = tp->ftt_dest;
} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
pc = rp->r_npc + 4;
npc = pc + 4;
} else {
pc = rp->r_npc;
npc = pc + 4;
}
break;
}
case FASTTRAP_T_FCC:
{
uint_t fcc;
uint_t taken;
uint64_t fsr;
dtrace_getfsr(&fsr);
if (tp->ftt_cc == 0) {
fcc = (fsr >> 10) & 0x3;
} else {
uint_t shift;
ASSERT(tp->ftt_cc <= 3);
shift = 30 + tp->ftt_cc * 2;
fcc = (fsr >> shift) & 0x3;
}
switch (tp->ftt_code) {
case 0x0:
taken = (1 << fcc) & (0|0|0|0); break;
case 0x1:
taken = (1 << fcc) & (8|4|2|0); break;
case 0x2:
taken = (1 << fcc) & (0|4|2|0); break;
case 0x3:
taken = (1 << fcc) & (8|0|2|0); break;
case 0x4:
taken = (1 << fcc) & (0|0|2|0); break;
case 0x5:
taken = (1 << fcc) & (8|4|0|0); break;
case 0x6:
taken = (1 << fcc) & (0|4|0|0); break;
case 0x7:
taken = (1 << fcc) & (8|0|0|0); break;
case 0x8:
panic("fasttrap: mishandled a branch");
taken = (1 << fcc) & (8|4|2|1); break;
case 0x9:
taken = (1 << fcc) & (0|0|0|1); break;
case 0xa:
taken = (1 << fcc) & (8|0|0|1); break;
case 0xb:
taken = (1 << fcc) & (0|4|0|1); break;
case 0xc:
taken = (1 << fcc) & (8|4|0|1); break;
case 0xd:
taken = (1 << fcc) & (0|0|2|1); break;
case 0xe:
taken = (1 << fcc) & (8|0|2|1); break;
case 0xf:
taken = (1 << fcc) & (0|4|2|1); break;
}
if (taken) {
pc = rp->r_npc;
npc = tp->ftt_dest;
} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
pc = rp->r_npc + 4;
npc = pc + 4;
} else {
pc = rp->r_npc;
npc = pc + 4;
}
break;
}
case FASTTRAP_T_REG:
{
int64_t value;
uint_t taken;
uint_t reg = RS1(tp->ftt_instr);
ASSERT(p->p_model == DATAMODEL_LP64 || reg < 16);
value = (int64_t)fasttrap_getreg(rp, reg);
switch (tp->ftt_code) {
case 0x1:
taken = (value == 0); break;
case 0x2:
taken = (value <= 0); break;
case 0x3:
taken = (value < 0); break;
case 0x5:
taken = (value != 0); break;
case 0x6:
taken = (value > 0); break;
case 0x7:
taken = (value >= 0); break;
default:
case 0x0:
case 0x4:
panic("fasttrap: mishandled a branch");
}
if (taken) {
pc = rp->r_npc;
npc = tp->ftt_dest;
} else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
pc = rp->r_npc + 4;
npc = pc + 4;
} else {
pc = rp->r_npc;
npc = pc + 4;
}
break;
}
case FASTTRAP_T_ALWAYS:
if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
pc = tp->ftt_dest;
npc = tp->ftt_dest + 4;
} else {
pc = rp->r_npc;
npc = tp->ftt_dest;
}
break;
case FASTTRAP_T_RDPC:
fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
pc = rp->r_npc;
npc = pc + 4;
break;
case FASTTRAP_T_CALL:
rp->r_o7 = rp->r_pc;
pc = rp->r_npc;
npc = tp->ftt_dest;
break;
case FASTTRAP_T_JMPL:
pc = rp->r_npc;
if (I(tp->ftt_instr)) {
uint_t rs1 = RS1(tp->ftt_instr);
int32_t imm;
imm = tp->ftt_instr << 19;
imm >>= 19;
npc = fasttrap_getreg(rp, rs1) + imm;
} else {
uint_t rs1 = RS1(tp->ftt_instr);
uint_t rs2 = RS2(tp->ftt_instr);
npc = fasttrap_getreg(rp, rs1) +
fasttrap_getreg(rp, rs2);
}
fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
break;
case FASTTRAP_T_COMMON:
{
curthread->t_dtrace_scrpc = rp->r_g7;
curthread->t_dtrace_astpc = rp->r_g7 + FASTTRAP_OFF_FTRET;
if (dtrace_blksuword32(rp->r_g7, &tp->ftt_instr, 1) == -1) {
fasttrap_sigtrap(curproc, curthread, pc);
return (0);
}
curthread->t_dtrace_pc = pc;
curthread->t_dtrace_npc = npc;
curthread->t_dtrace_on = 1;
pc = curthread->t_dtrace_scrpc;
if (tp->ftt_retids != NULL) {
curthread->t_dtrace_step = 1;
curthread->t_dtrace_ret = 1;
npc = curthread->t_dtrace_astpc;
}
break;
}
default:
panic("fasttrap: mishandled an instruction");
}
ASSERT(pc != rp->r_g7 + 4);
ASSERT(pc != rp->r_g7 + 8);
done:
if (tp->ftt_retids != NULL) {
if (tp->ftt_type != FASTTRAP_T_COMMON) {
fasttrap_return_common(rp, orig_pc, pid, fake_restore);
} else {
ASSERT(curthread->t_dtrace_ret != 0);
ASSERT(curthread->t_dtrace_pc == orig_pc);
ASSERT(curthread->t_dtrace_scrpc == rp->r_g7);
ASSERT(npc == curthread->t_dtrace_astpc);
}
}
ASSERT(pc != 0);
rp->r_pc = pc;
rp->r_npc = npc;
return (0);
}
int
fasttrap_return_probe(struct regs *rp)
{
proc_t *p = ttoproc(curthread);
pid_t pid;
uintptr_t pc = curthread->t_dtrace_pc;
uintptr_t npc = curthread->t_dtrace_npc;
curthread->t_dtrace_pc = 0;
curthread->t_dtrace_npc = 0;
curthread->t_dtrace_scrpc = 0;
curthread->t_dtrace_astpc = 0;
while (p->p_flag & SVFORK) {
p = p->p_parent;
}
rp->r_pc = pc;
rp->r_npc = npc;
pid = p->p_pid;
fasttrap_return_common(rp, pc, pid, 0);
return (0);
}
int
fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
{
fasttrap_instr_t instr = FASTTRAP_INSTR;
if (uwrite(p, &instr, 4, tp->ftt_pc) != 0)
return (-1);
return (0);
}
int
fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
{
fasttrap_instr_t instr;
if (uread(p, &instr, 4, tp->ftt_pc) != 0)
return (0);
if (instr != FASTTRAP_INSTR && instr != BREAKPOINT_INSTR)
return (0);
if (uwrite(p, &tp->ftt_instr, 4, tp->ftt_pc) != 0)
return (-1);
return (0);
}
int
fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
fasttrap_probe_type_t type)
{
uint32_t instr;
int32_t disp;
if (uread(p, &instr, 4, pc) != 0)
return (-1);
tp->ftt_type = FASTTRAP_T_COMMON;
if (OP(instr) == 1) {
tp->ftt_type = FASTTRAP_T_CALL;
disp = DISP30(instr) << 2;
tp->ftt_dest = pc + (intptr_t)disp;
} else if (OP(instr) == 0) {
switch (OP2(instr)) {
case OP2_ILLTRAP:
case 0x7:
return (-1);
case OP2_BPcc:
if (COND(instr) == 8) {
tp->ftt_type = FASTTRAP_T_ALWAYS;
} else {
if (CC(instr) & 1)
return (-1);
tp->ftt_type = FASTTRAP_T_CCR;
tp->ftt_cc = CC(instr);
tp->ftt_code = COND(instr);
}
if (A(instr) != 0)
tp->ftt_flags |= FASTTRAP_F_ANNUL;
disp = DISP19(instr);
disp <<= 13;
disp >>= 11;
tp->ftt_dest = pc + (intptr_t)disp;
break;
case OP2_Bicc:
if (COND(instr) == 8) {
tp->ftt_type = FASTTRAP_T_ALWAYS;
} else {
tp->ftt_type = FASTTRAP_T_CCR;
tp->ftt_cc = 0;
tp->ftt_code = COND(instr);
}
if (A(instr) != 0)
tp->ftt_flags |= FASTTRAP_F_ANNUL;
disp = DISP22(instr);
disp <<= 10;
disp >>= 8;
tp->ftt_dest = pc + (intptr_t)disp;
break;
case OP2_BPr:
if ((RCOND(instr) & 3) == 0)
return (-1);
if (p->p_model == DATAMODEL_ILP32 && RS1(instr) >= 16)
return (-1);
tp->ftt_type = FASTTRAP_T_REG;
if (A(instr) != 0)
tp->ftt_flags |= FASTTRAP_F_ANNUL;
disp = DISP16(instr);
disp <<= 16;
disp >>= 14;
tp->ftt_dest = pc + (intptr_t)disp;
tp->ftt_code = RCOND(instr);
break;
case OP2_SETHI:
tp->ftt_type = FASTTRAP_T_SETHI;
break;
case OP2_FBPfcc:
if (COND(instr) == 8) {
tp->ftt_type = FASTTRAP_T_ALWAYS;
} else {
tp->ftt_type = FASTTRAP_T_FCC;
tp->ftt_cc = CC(instr);
tp->ftt_code = COND(instr);
}
if (A(instr) != 0)
tp->ftt_flags |= FASTTRAP_F_ANNUL;
disp = DISP19(instr);
disp <<= 13;
disp >>= 11;
tp->ftt_dest = pc + (intptr_t)disp;
break;
case OP2_FBfcc:
if (COND(instr) == 8) {
tp->ftt_type = FASTTRAP_T_ALWAYS;
} else {
tp->ftt_type = FASTTRAP_T_FCC;
tp->ftt_cc = 0;
tp->ftt_code = COND(instr);
}
if (A(instr) != 0)
tp->ftt_flags |= FASTTRAP_F_ANNUL;
disp = DISP22(instr);
disp <<= 10;
disp >>= 8;
tp->ftt_dest = pc + (intptr_t)disp;
break;
}
} else if (OP(instr) == 2) {
switch (OP3(instr)) {
case OP3_RETURN:
tp->ftt_type = FASTTRAP_T_RETURN;
break;
case OP3_JMPL:
tp->ftt_type = FASTTRAP_T_JMPL;
break;
case OP3_RD:
if (RS1(instr) == 5)
tp->ftt_type = FASTTRAP_T_RDPC;
break;
case OP3_SAVE:
if (fasttrap_optimize_save != 0 &&
type == DTFTP_ENTRY &&
I(instr) == 1 && RD(instr) == R_SP)
tp->ftt_type = FASTTRAP_T_SAVE;
break;
case OP3_RESTORE:
if ((RD(instr) & 0x10) == 0)
tp->ftt_type = FASTTRAP_T_RESTORE;
break;
case OP3_OR:
tp->ftt_type = FASTTRAP_T_OR;
break;
case OP3_TCC:
if (SW_TRAP(instr) == ST_BREAKPOINT)
return (-1);
break;
case 0x19:
case 0x1d:
case 0x29:
case 0x33:
case 0x3f:
return (-1);
}
} else if (OP(instr) == 3) {
uint32_t op3 = OP3(instr);
if ((op3 & 0x28) == 0x28) {
if (op3 != OP3_PREFETCH && op3 != OP3_CASA &&
op3 != OP3_PREFETCHA && op3 != OP3_CASXA)
return (-1);
} else {
if ((op3 & 0x0f) == 0x0c || (op3 & 0x3b) == 0x31)
return (-1);
}
}
tp->ftt_instr = instr;
if (tp->ftt_type != FASTTRAP_T_RESTORE &&
(uread(p, &instr, 4, pc - sizeof (instr)) != 0 ||
!(OP(instr) == 2 && OP3(instr) == OP3_RETURN)))
tp->ftt_flags |= FASTTRAP_F_RETMAYBE;
return (0);
}
uint64_t
fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
int aframes)
{
return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
}
uint64_t
fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
int aframes)
{
return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
}
static uint64_t fasttrap_getreg_fast_cnt;
static uint64_t fasttrap_getreg_mpcb_cnt;
static uint64_t fasttrap_getreg_slow_cnt;
static ulong_t
fasttrap_getreg(struct regs *rp, uint_t reg)
{
ulong_t value;
dtrace_icookie_t cookie;
struct machpcb *mpcb;
extern ulong_t dtrace_getreg_win(uint_t, uint_t);
if (reg == 0)
return (0);
if (reg < 16)
return ((&rp->r_g1)[reg - 1]);
cookie = dtrace_interrupt_disable();
if (dtrace_getotherwin() > 0) {
value = dtrace_getreg_win(reg, 1);
dtrace_interrupt_enable(cookie);
atomic_inc_64(&fasttrap_getreg_fast_cnt);
return (value);
}
dtrace_interrupt_enable(cookie);
mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
if (get_udatamodel() == DATAMODEL_NATIVE) {
struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
if (mpcb->mpcb_wbcnt > 0) {
struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
int i = mpcb->mpcb_wbcnt;
do {
i--;
if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
continue;
atomic_inc_64(&fasttrap_getreg_mpcb_cnt);
return (rwin[i].rw_local[reg - 16]);
} while (i > 0);
}
if (fasttrap_fulword(&fr->fr_local[reg - 16], &value) != 0)
goto err;
} else {
struct frame32 *fr =
(struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp;
uint32_t *v32 = (uint32_t *)&value;
if (mpcb->mpcb_wbcnt > 0) {
struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
int i = mpcb->mpcb_wbcnt;
do {
i--;
if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
continue;
atomic_inc_64(&fasttrap_getreg_mpcb_cnt);
return (rwin[i].rw_local[reg - 16]);
} while (i > 0);
}
if (fasttrap_fuword32(&fr->fr_local[reg - 16], &v32[1]) != 0)
goto err;
v32[0] = 0;
}
atomic_inc_64(&fasttrap_getreg_slow_cnt);
return (value);
err:
psignal(ttoproc(curthread), SIGILL);
return (0);
}
static uint64_t fasttrap_putreg_fast_cnt;
static uint64_t fasttrap_putreg_mpcb_cnt;
static uint64_t fasttrap_putreg_slow_cnt;
static void
fasttrap_putreg(struct regs *rp, uint_t reg, ulong_t value)
{
dtrace_icookie_t cookie;
struct machpcb *mpcb;
extern void dtrace_putreg_win(uint_t, ulong_t);
if (reg == 0)
return;
if (reg < 16) {
(&rp->r_g1)[reg - 1] = value;
return;
}
cookie = dtrace_interrupt_disable();
if (dtrace_getotherwin() > 0) {
dtrace_putreg_win(reg, value);
dtrace_interrupt_enable(cookie);
atomic_inc_64(&fasttrap_putreg_fast_cnt);
return;
}
dtrace_interrupt_enable(cookie);
mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
if (get_udatamodel() == DATAMODEL_NATIVE) {
struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
struct rwindow *rwin = (struct rwindow *)mpcb->mpcb_wbuf;
if (mpcb->mpcb_wbcnt > 0) {
int i = mpcb->mpcb_wbcnt;
do {
i--;
if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
continue;
rwin[i].rw_local[reg - 16] = value;
atomic_inc_64(&fasttrap_putreg_mpcb_cnt);
return;
} while (i > 0);
}
if (fasttrap_sulword(&fr->fr_local[reg - 16], value) != 0) {
if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
&rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
goto err;
rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = value;
mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
mpcb->mpcb_wbcnt++;
atomic_inc_64(&fasttrap_putreg_mpcb_cnt);
return;
}
} else {
struct frame32 *fr =
(struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp;
struct rwindow32 *rwin = (struct rwindow32 *)mpcb->mpcb_wbuf;
uint32_t v32 = (uint32_t)value;
if (mpcb->mpcb_wbcnt > 0) {
int i = mpcb->mpcb_wbcnt;
do {
i--;
if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
continue;
rwin[i].rw_local[reg - 16] = v32;
atomic_inc_64(&fasttrap_putreg_mpcb_cnt);
return;
} while (i > 0);
}
if (fasttrap_suword32(&fr->fr_local[reg - 16], v32) != 0) {
if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
&rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
goto err;
rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = v32;
mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
mpcb->mpcb_wbcnt++;
atomic_inc_64(&fasttrap_putreg_mpcb_cnt);
return;
}
}
atomic_inc_64(&fasttrap_putreg_slow_cnt);
return;
err:
psignal(ttoproc(curthread), SIGILL);
}