10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1710Sahl * Common Development and Distribution License (the "License"). 6*1710Sahl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211048Sraf 220Sstevel@tonic-gate /* 23*1710Sahl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/fasttrap_isa.h> 300Sstevel@tonic-gate #include <sys/fasttrap_impl.h> 310Sstevel@tonic-gate #include <sys/dtrace.h> 320Sstevel@tonic-gate #include <sys/dtrace_impl.h> 330Sstevel@tonic-gate #include <sys/cmn_err.h> 340Sstevel@tonic-gate #include <sys/frame.h> 350Sstevel@tonic-gate #include <sys/stack.h> 360Sstevel@tonic-gate #include <sys/sysmacros.h> 370Sstevel@tonic-gate #include <sys/trap.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include <v9/sys/machpcb.h> 400Sstevel@tonic-gate #include <v9/sys/privregs.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * Lossless User-Land Tracing on SPARC 440Sstevel@tonic-gate * ----------------------------------- 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * The Basic Idea 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * The most important design constraint is, of course, correct execution of 490Sstevel@tonic-gate * the user thread above all else. The next most important goal is rapid 500Sstevel@tonic-gate * execution. We combine execution of instructions in user-land with 510Sstevel@tonic-gate * emulation of certain instructions in the kernel to aim for complete 520Sstevel@tonic-gate * correctness and maximal performance. 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * We take advantage of the split PC/NPC architecture to speed up logical 550Sstevel@tonic-gate * single-stepping; when we copy an instruction out to the scratch space in 560Sstevel@tonic-gate * the ulwp_t structure (held in the %g7 register on SPARC), we can 570Sstevel@tonic-gate * effectively single step by setting the PC to our scratch space and leaving 580Sstevel@tonic-gate * the NPC alone. This executes the replaced instruction and then continues 590Sstevel@tonic-gate * on without having to reenter the kernel as with single- stepping. The 600Sstevel@tonic-gate * obvious caveat is for instructions whose execution is PC dependant -- 610Sstevel@tonic-gate * branches, call and link instructions (call and jmpl), and the rdpc 620Sstevel@tonic-gate * instruction. These instructions cannot be executed in the manner described 630Sstevel@tonic-gate * so they must be emulated in the kernel. 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * Emulation for this small set of instructions if fairly simple; the most 660Sstevel@tonic-gate * difficult part being emulating branch conditions. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * A Cache Heavy Portfolio 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * It's important to note at this time that copying an instruction out to the 720Sstevel@tonic-gate * ulwp_t scratch space in user-land is rather complicated. SPARC has 730Sstevel@tonic-gate * separate data and instruction caches so any writes to the D$ (using a 740Sstevel@tonic-gate * store instruction for example) aren't necessarily reflected in the I$. 750Sstevel@tonic-gate * The flush instruction can be used to synchronize the two and must be used 760Sstevel@tonic-gate * for any self-modifying code, but the flush instruction only applies to the 770Sstevel@tonic-gate * primary address space (the absence of a flusha analogue to the flush 780Sstevel@tonic-gate * instruction that accepts an ASI argument is an obvious omission from SPARC 790Sstevel@tonic-gate * v9 where the notion of the alternate address space was introduced on 800Sstevel@tonic-gate * SPARC). To correctly copy out the instruction we must use a block store 810Sstevel@tonic-gate * that doesn't allocate in the D$ and ensures synchronization with the I$; 820Sstevel@tonic-gate * see dtrace_blksuword32() for the implementation (this function uses 830Sstevel@tonic-gate * ASI_BLK_COMMIT_S to write a block through the secondary ASI in the manner 840Sstevel@tonic-gate * described). Refer to the UltraSPARC I/II manual for details on the 850Sstevel@tonic-gate * ASI_BLK_COMMIT_S ASI. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * 880Sstevel@tonic-gate * Return Subtleties 890Sstevel@tonic-gate * 900Sstevel@tonic-gate * When we're firing a return probe we need to expose the value returned by 910Sstevel@tonic-gate * the function being traced. Since the function can set the return value 920Sstevel@tonic-gate * in its last instruction, we need to fire the return probe only _after_ 930Sstevel@tonic-gate * the effects of the instruction are apparent. For instructions that we 940Sstevel@tonic-gate * emulate, we can call dtrace_probe() after we've performed the emulation; 950Sstevel@tonic-gate * for instructions that we execute after we return to user-land, we set 960Sstevel@tonic-gate * %pc to the instruction we copied out (as described above) and set %npc 970Sstevel@tonic-gate * to a trap instruction stashed in the ulwp_t structure. After the traced 980Sstevel@tonic-gate * instruction is executed, the trap instruction returns control to the 990Sstevel@tonic-gate * kernel where we can fire the return probe. 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * This need for a second trap in cases where we execute the traced 1020Sstevel@tonic-gate * instruction makes it all the more important to emulate the most common 1030Sstevel@tonic-gate * instructions to avoid the second trip in and out of the kernel. 1040Sstevel@tonic-gate * 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * Making it Fast 1070Sstevel@tonic-gate * 1080Sstevel@tonic-gate * Since copying out an instruction is neither simple nor inexpensive for the 1090Sstevel@tonic-gate * CPU, we should attempt to avoid doing it in as many cases as possible. 1100Sstevel@tonic-gate * Since function entry and return are usually the most interesting probe 1110Sstevel@tonic-gate * sites, we attempt to tune the performance of the fasttrap provider around 1120Sstevel@tonic-gate * instructions typically in those places. 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * Looking at a bunch of functions in libraries and executables reveals that 1150Sstevel@tonic-gate * most functions begin with either a save or a sethi (to setup a larger 1160Sstevel@tonic-gate * argument to the save) and end with a restore or an or (in the case of leaf 1170Sstevel@tonic-gate * functions). To try to improve performance, we emulate all of these 1180Sstevel@tonic-gate * instructions in the kernel. 1190Sstevel@tonic-gate * 1200Sstevel@tonic-gate * The save and restore instructions are a little tricky since they perform 1210Sstevel@tonic-gate * register window maniplulation. Rather than trying to tinker with the 1220Sstevel@tonic-gate * register windows from the kernel, we emulate the implicit add that takes 1230Sstevel@tonic-gate * place as part of those instructions and set the %pc to point to a simple 1240Sstevel@tonic-gate * save or restore we've hidden in the ulwp_t structure. If we're in a return 1250Sstevel@tonic-gate * probe so want to make it seem as though the tracepoint has been completely 1260Sstevel@tonic-gate * executed we need to remember that we've pulled this trick with restore and 1270Sstevel@tonic-gate * pull registers from the previous window (the one that we'll switch to once 1280Sstevel@tonic-gate * the simple store instruction is executed) rather than the current one. This 1290Sstevel@tonic-gate * is why in the case of emulating a restore we set the DTrace CPU flag 1300Sstevel@tonic-gate * CPU_DTRACE_FAKERESTORE before calling dtrace_probe() for the return probes 1310Sstevel@tonic-gate * (see fasttrap_return_common()). 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate #define OP(x) ((x) >> 30) 1350Sstevel@tonic-gate #define OP2(x) (((x) >> 22) & 0x07) 1360Sstevel@tonic-gate #define OP3(x) (((x) >> 19) & 0x3f) 1370Sstevel@tonic-gate #define RCOND(x) (((x) >> 25) & 0x07) 1380Sstevel@tonic-gate #define COND(x) (((x) >> 25) & 0x0f) 1390Sstevel@tonic-gate #define A(x) (((x) >> 29) & 0x01) 1400Sstevel@tonic-gate #define I(x) (((x) >> 13) & 0x01) 1410Sstevel@tonic-gate #define RD(x) (((x) >> 25) & 0x1f) 1420Sstevel@tonic-gate #define RS1(x) (((x) >> 14) & 0x1f) 1430Sstevel@tonic-gate #define RS2(x) (((x) >> 0) & 0x1f) 1440Sstevel@tonic-gate #define CC(x) (((x) >> 20) & 0x03) 1450Sstevel@tonic-gate #define DISP16(x) ((((x) >> 6) & 0xc000) | ((x) & 0x3fff)) 1460Sstevel@tonic-gate #define DISP22(x) ((x) & 0x3fffff) 1470Sstevel@tonic-gate #define DISP19(x) ((x) & 0x7ffff) 1480Sstevel@tonic-gate #define DISP30(x) ((x) & 0x3fffffff) 1490Sstevel@tonic-gate #define SW_TRAP(x) ((x) & 0x7f) 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate #define OP3_OR 0x02 1520Sstevel@tonic-gate #define OP3_RD 0x28 1530Sstevel@tonic-gate #define OP3_JMPL 0x38 1540Sstevel@tonic-gate #define OP3_RETURN 0x39 1550Sstevel@tonic-gate #define OP3_TCC 0x3a 1560Sstevel@tonic-gate #define OP3_SAVE 0x3c 1570Sstevel@tonic-gate #define OP3_RESTORE 0x3d 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate #define OP3_PREFETCH 0x2d 1600Sstevel@tonic-gate #define OP3_CASA 0x3c 1610Sstevel@tonic-gate #define OP3_PREFETCHA 0x3d 1620Sstevel@tonic-gate #define OP3_CASXA 0x3e 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate #define OP2_ILLTRAP 0x0 1650Sstevel@tonic-gate #define OP2_BPcc 0x1 1660Sstevel@tonic-gate #define OP2_Bicc 0x2 1670Sstevel@tonic-gate #define OP2_BPr 0x3 1680Sstevel@tonic-gate #define OP2_SETHI 0x4 1690Sstevel@tonic-gate #define OP2_FBPfcc 0x5 1700Sstevel@tonic-gate #define OP2_FBfcc 0x6 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate #define R_G0 0 1730Sstevel@tonic-gate #define R_O0 8 1740Sstevel@tonic-gate #define R_SP 14 1750Sstevel@tonic-gate #define R_I0 24 1760Sstevel@tonic-gate #define R_I1 25 1770Sstevel@tonic-gate #define R_I2 26 1780Sstevel@tonic-gate #define R_I3 27 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Check the comment in fasttrap.h when changing these offsets or adding 1820Sstevel@tonic-gate * new instructions. 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate #define FASTTRAP_OFF_SAVE 64 1850Sstevel@tonic-gate #define FASTTRAP_OFF_RESTORE 68 1860Sstevel@tonic-gate #define FASTTRAP_OFF_FTRET 72 1870Sstevel@tonic-gate #define FASTTRAP_OFF_RETURN 76 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate #define BREAKPOINT_INSTR 0x91d02001 /* ta 1 */ 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate /* 1920Sstevel@tonic-gate * Tunable to let users turn off the fancy save instruction optimization. 1930Sstevel@tonic-gate * If a program is non-ABI compliant, there's a possibility that the save 1940Sstevel@tonic-gate * instruction optimization could cause an error. 1950Sstevel@tonic-gate */ 1960Sstevel@tonic-gate int fasttrap_optimize_save = 1; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate static uint64_t 1990Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int argno) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate uint64_t value; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if (argno < 6) 2040Sstevel@tonic-gate return ((&rp->r_o0)[argno]); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_NATIVE) { 2070Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2100Sstevel@tonic-gate value = dtrace_fulword(&fr->fr_argd[argno]); 2110Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR | 2120Sstevel@tonic-gate CPU_DTRACE_BADALIGN); 2130Sstevel@tonic-gate } else { 2140Sstevel@tonic-gate struct frame32 *fr = (struct frame32 *)rp->r_sp; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2170Sstevel@tonic-gate value = dtrace_fuword32(&fr->fr_argd[argno]); 2180Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR | 2190Sstevel@tonic-gate CPU_DTRACE_BADALIGN); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate return (value); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t); 2260Sstevel@tonic-gate static void fasttrap_putreg(struct regs *, uint_t, ulong_t); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate int 2290Sstevel@tonic-gate fasttrap_probe(struct regs *rp) 2300Sstevel@tonic-gate { 2310Sstevel@tonic-gate dtrace_probe(fasttrap_probe_id, 2320Sstevel@tonic-gate rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3, rp->r_o4); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate rp->r_pc = rp->r_npc; 2350Sstevel@tonic-gate rp->r_npc = rp->r_pc + 4; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate return (0); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate static void 2410Sstevel@tonic-gate fasttrap_usdt_args(fasttrap_probe_t *probe, struct regs *rp, int argc, 2420Sstevel@tonic-gate uintptr_t *argv) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate int i, x, cap = MIN(argc, probe->ftp_nargs); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_NATIVE) { 2470Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS); 2480Sstevel@tonic-gate uintptr_t v; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate for (i = 0; i < cap; i++) { 2510Sstevel@tonic-gate x = probe->ftp_argmap[i]; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if (x < 6) 2540Sstevel@tonic-gate argv[i] = (&rp->r_o0)[x]; 2550Sstevel@tonic-gate else if (fasttrap_fulword(&fr->fr_argd[x], &v) != 0) 2560Sstevel@tonic-gate argv[i] = 0; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate } else { 2600Sstevel@tonic-gate struct frame32 *fr = (struct frame32 *)rp->r_sp; 2610Sstevel@tonic-gate uint32_t v; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate for (i = 0; i < cap; i++) { 2640Sstevel@tonic-gate x = probe->ftp_argmap[i]; 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (x < 6) 2670Sstevel@tonic-gate argv[i] = (&rp->r_o0)[x]; 2680Sstevel@tonic-gate else if (fasttrap_fuword32(&fr->fr_argd[x], &v) != 0) 2690Sstevel@tonic-gate argv[i] = 0; 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate for (; i < argc; i++) { 2740Sstevel@tonic-gate argv[i] = 0; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate static void 2790Sstevel@tonic-gate fasttrap_return_common(struct regs *rp, uintptr_t pc, pid_t pid, 2800Sstevel@tonic-gate uint_t fake_restore) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 2830Sstevel@tonic-gate fasttrap_bucket_t *bucket; 2840Sstevel@tonic-gate fasttrap_id_t *id; 2850Sstevel@tonic-gate kmutex_t *pid_mtx; 2860Sstevel@tonic-gate dtrace_icookie_t cookie; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock; 2890Sstevel@tonic-gate mutex_enter(pid_mtx); 2900Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 2930Sstevel@tonic-gate if (pid == tp->ftt_pid && pc == tp->ftt_pc && 294532Sahl !tp->ftt_proc->ftpc_defunct) 2950Sstevel@tonic-gate break; 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * Don't sweat it if we can't find the tracepoint again; unlike 3000Sstevel@tonic-gate * when we're in fasttrap_pid_probe(), finding the tracepoint here 3010Sstevel@tonic-gate * is not essential to the correct execution of the process. 3020Sstevel@tonic-gate */ 3030Sstevel@tonic-gate if (tp == NULL || tp->ftt_retids == NULL) { 3040Sstevel@tonic-gate mutex_exit(pid_mtx); 3050Sstevel@tonic-gate return; 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate for (id = tp->ftt_retids; id != NULL; id = id->fti_next) { 3090Sstevel@tonic-gate fasttrap_probe_t *probe = id->fti_probe; 3100Sstevel@tonic-gate 311*1710Sahl if (id->fti_ptype == DTFTP_POST_OFFSETS) { 3120Sstevel@tonic-gate if (probe->ftp_argmap == NULL) { 3130Sstevel@tonic-gate dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1, 3140Sstevel@tonic-gate rp->r_o2, rp->r_o3, rp->r_o4); 3150Sstevel@tonic-gate } else { 3160Sstevel@tonic-gate uintptr_t t[5]; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate fasttrap_usdt_args(probe, rp, 3190Sstevel@tonic-gate sizeof (t) / sizeof (t[0]), t); 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate dtrace_probe(probe->ftp_id, t[0], t[1], 3220Sstevel@tonic-gate t[2], t[3], t[4]); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate continue; 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate /* 3280Sstevel@tonic-gate * If this is only a possible return point, we must 3290Sstevel@tonic-gate * be looking at a potential tail call in leaf context. 3300Sstevel@tonic-gate * If the %npc is still within this function, then we 3310Sstevel@tonic-gate * must have misidentified a jmpl as a tail-call when it 3320Sstevel@tonic-gate * is, in fact, part of a jump table. It would be nice to 3330Sstevel@tonic-gate * remove this tracepoint, but this is neither the time 3340Sstevel@tonic-gate * nor the place. 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate if ((tp->ftt_flags & FASTTRAP_F_RETMAYBE) && 3370Sstevel@tonic-gate rp->r_npc - probe->ftp_faddr < probe->ftp_fsize) 3380Sstevel@tonic-gate continue; 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate /* 3410Sstevel@tonic-gate * It's possible for a function to branch to the delay slot 3420Sstevel@tonic-gate * of an instruction that we've identified as a return site. 3430Sstevel@tonic-gate * We can dectect this spurious return probe activation by 3440Sstevel@tonic-gate * observing that in this case %npc will be %pc + 4 and %npc 3450Sstevel@tonic-gate * will be inside the current function (unless the user is 3460Sstevel@tonic-gate * doing _crazy_ instruction picking in which case there's 3470Sstevel@tonic-gate * very little we can do). The second check is important 3480Sstevel@tonic-gate * in case the last instructions of a function make a tail- 3490Sstevel@tonic-gate * call to the function located immediately subsequent. 3500Sstevel@tonic-gate */ 3510Sstevel@tonic-gate if (rp->r_npc == rp->r_pc + 4 && 3520Sstevel@tonic-gate rp->r_npc - probe->ftp_faddr < probe->ftp_fsize) 3530Sstevel@tonic-gate continue; 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * The first argument is the offset of return tracepoint 3570Sstevel@tonic-gate * in the function; the remaining arguments are the return 3580Sstevel@tonic-gate * values. 3590Sstevel@tonic-gate * 3600Sstevel@tonic-gate * If fake_restore is set, we need to pull the return values 3610Sstevel@tonic-gate * out of the %i's rather than the %o's -- a little trickier. 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate if (!fake_restore) { 3640Sstevel@tonic-gate dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr, 3650Sstevel@tonic-gate rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3); 3660Sstevel@tonic-gate } else { 3670Sstevel@tonic-gate uintptr_t arg0 = fasttrap_getreg(rp, R_I0); 3680Sstevel@tonic-gate uintptr_t arg1 = fasttrap_getreg(rp, R_I1); 3690Sstevel@tonic-gate uintptr_t arg2 = fasttrap_getreg(rp, R_I2); 3700Sstevel@tonic-gate uintptr_t arg3 = fasttrap_getreg(rp, R_I3); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate cookie = dtrace_interrupt_disable(); 3730Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE); 3740Sstevel@tonic-gate dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr, 3750Sstevel@tonic-gate arg0, arg1, arg2, arg3); 3760Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE); 3770Sstevel@tonic-gate dtrace_interrupt_enable(cookie); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate mutex_exit(pid_mtx); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate int 3850Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate proc_t *p = curproc; 3880Sstevel@tonic-gate fasttrap_tracepoint_t *tp, tp_local; 3890Sstevel@tonic-gate fasttrap_id_t *id; 3900Sstevel@tonic-gate pid_t pid; 3910Sstevel@tonic-gate uintptr_t pc = rp->r_pc; 3920Sstevel@tonic-gate uintptr_t npc = rp->r_npc; 3930Sstevel@tonic-gate uintptr_t orig_pc = pc; 3940Sstevel@tonic-gate fasttrap_bucket_t *bucket; 3950Sstevel@tonic-gate kmutex_t *pid_mtx; 396*1710Sahl uint_t fake_restore = 0, is_enabled = 0; 3970Sstevel@tonic-gate dtrace_icookie_t cookie; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate /* 4000Sstevel@tonic-gate * It's possible that a user (in a veritable orgy of bad planning) 4010Sstevel@tonic-gate * could redirect this thread's flow of control before it reached the 4020Sstevel@tonic-gate * return probe fasttrap. In this case we need to kill the process 4030Sstevel@tonic-gate * since it's in a unrecoverable state. 4040Sstevel@tonic-gate */ 4050Sstevel@tonic-gate if (curthread->t_dtrace_step) { 4060Sstevel@tonic-gate ASSERT(curthread->t_dtrace_on); 4070Sstevel@tonic-gate fasttrap_sigtrap(p, curthread, pc); 4080Sstevel@tonic-gate return (0); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * Clear all user tracing flags. 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate curthread->t_dtrace_ft = 0; 4150Sstevel@tonic-gate curthread->t_dtrace_pc = 0; 4160Sstevel@tonic-gate curthread->t_dtrace_npc = 0; 4170Sstevel@tonic-gate curthread->t_dtrace_scrpc = 0; 4180Sstevel@tonic-gate curthread->t_dtrace_astpc = 0; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * Treat a child created by a call to vfork(2) as if it were its 4220Sstevel@tonic-gate * parent. We know that there's only one thread of control in such a 4230Sstevel@tonic-gate * process: this one. 4240Sstevel@tonic-gate */ 4250Sstevel@tonic-gate while (p->p_flag & SVFORK) { 4260Sstevel@tonic-gate p = p->p_parent; 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate pid = p->p_pid; 4300Sstevel@tonic-gate pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock; 4310Sstevel@tonic-gate mutex_enter(pid_mtx); 4320Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate /* 4350Sstevel@tonic-gate * Lookup the tracepoint that the process just hit. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 4380Sstevel@tonic-gate if (pid == tp->ftt_pid && pc == tp->ftt_pc && 439532Sahl !tp->ftt_proc->ftpc_defunct) 4400Sstevel@tonic-gate break; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * If we couldn't find a matching tracepoint, either a tracepoint has 4450Sstevel@tonic-gate * been inserted without using the pid<pid> ioctl interface (see 4460Sstevel@tonic-gate * fasttrap_ioctl), or somehow we have mislaid this tracepoint. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate if (tp == NULL) { 4490Sstevel@tonic-gate mutex_exit(pid_mtx); 4500Sstevel@tonic-gate return (-1); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate for (id = tp->ftt_ids; id != NULL; id = id->fti_next) { 4540Sstevel@tonic-gate fasttrap_probe_t *probe = id->fti_probe; 455*1710Sahl int isentry = (id->fti_ptype == DTFTP_ENTRY); 456*1710Sahl 457*1710Sahl if (id->fti_ptype == DTFTP_IS_ENABLED) { 458*1710Sahl is_enabled = 1; 459*1710Sahl continue; 460*1710Sahl } 461*1710Sahl 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * We note that this was an entry probe to help ustack() find 4640Sstevel@tonic-gate * the first caller. 4650Sstevel@tonic-gate */ 466*1710Sahl if (isentry) { 4670Sstevel@tonic-gate cookie = dtrace_interrupt_disable(); 4680Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1, rp->r_o2, 4710Sstevel@tonic-gate rp->r_o3, rp->r_o4); 4720Sstevel@tonic-gate if (isentry) { 4730Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY); 4740Sstevel@tonic-gate dtrace_interrupt_enable(cookie); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * We're about to do a bunch of work so we cache a local copy of 4800Sstevel@tonic-gate * the tracepoint to emulate the instruction, and then find the 4810Sstevel@tonic-gate * tracepoint again later if we need to light up any return probes. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate tp_local = *tp; 4840Sstevel@tonic-gate mutex_exit(pid_mtx); 4850Sstevel@tonic-gate tp = &tp_local; 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate /* 488*1710Sahl * If there's an is-enabled probe conntected to this tracepoint it 489*1710Sahl * means that there was a 'mov %g0, %o0' instruction that was placed 490*1710Sahl * there by DTrace when the binary was linked. As this probe is, in 491*1710Sahl * fact, enabled, we need to stuff 1 into %o0. Accordingly, we can 492*1710Sahl * bypass all the instruction emulation logic since we know the 493*1710Sahl * inevitable result. It's possible that a user could construct a 494*1710Sahl * scenario where the 'is-enabled' probe was on some other 495*1710Sahl * instruction, but that would be a rather exotic way to shoot oneself 496*1710Sahl * in the foot. 497*1710Sahl */ 498*1710Sahl if (is_enabled) { 499*1710Sahl rp->r_o0 = 1; 500*1710Sahl pc = rp->r_npc; 501*1710Sahl npc = pc + 4; 502*1710Sahl goto done; 503*1710Sahl } 504*1710Sahl 505*1710Sahl /* 506*1710Sahl * We emulate certain types of instructions to ensure correctness 5070Sstevel@tonic-gate * (in the case of position dependent instructions) or optimize 5080Sstevel@tonic-gate * common cases. The rest we have the thread execute back in user- 5090Sstevel@tonic-gate * land. 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate switch (tp->ftt_type) { 5120Sstevel@tonic-gate case FASTTRAP_T_SAVE: 5130Sstevel@tonic-gate { 5140Sstevel@tonic-gate int32_t imm; 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* 5170Sstevel@tonic-gate * This an optimization to let us handle function entry 5180Sstevel@tonic-gate * probes more efficiently. Many functions begin with a save 5190Sstevel@tonic-gate * instruction that follows the pattern: 5200Sstevel@tonic-gate * save %sp, <imm>, %sp 5210Sstevel@tonic-gate * 5220Sstevel@tonic-gate * Meanwhile, we've stashed the instruction: 5230Sstevel@tonic-gate * save %g1, %g0, %sp 5240Sstevel@tonic-gate * 5250Sstevel@tonic-gate * off of %g7, so all we have to do is stick the right value 5260Sstevel@tonic-gate * into %g1 and reset %pc to point to the instruction we've 5270Sstevel@tonic-gate * cleverly hidden (%npc should not be touched). 5280Sstevel@tonic-gate */ 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate imm = tp->ftt_instr << 19; 5310Sstevel@tonic-gate imm >>= 19; 5320Sstevel@tonic-gate rp->r_g1 = rp->r_sp + imm; 5330Sstevel@tonic-gate pc = rp->r_g7 + FASTTRAP_OFF_SAVE; 5340Sstevel@tonic-gate break; 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate case FASTTRAP_T_RESTORE: 5380Sstevel@tonic-gate { 5390Sstevel@tonic-gate ulong_t value; 5400Sstevel@tonic-gate uint_t rd; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate /* 5430Sstevel@tonic-gate * This is an optimization to let us handle function 5440Sstevel@tonic-gate * return probes more efficiently. Most non-leaf functions 5450Sstevel@tonic-gate * end with the sequence: 5460Sstevel@tonic-gate * ret 5470Sstevel@tonic-gate * restore <reg>, <reg_or_imm>, %oX 5480Sstevel@tonic-gate * 5490Sstevel@tonic-gate * We've stashed the instruction: 5500Sstevel@tonic-gate * restore %g0, %g0, %g0 5510Sstevel@tonic-gate * 5520Sstevel@tonic-gate * off of %g7 so we just need to place the correct value 5530Sstevel@tonic-gate * in the right %i register (since after our fake-o 5540Sstevel@tonic-gate * restore, the %i's will become the %o's) and set the %pc 5550Sstevel@tonic-gate * to point to our hidden restore. We also set fake_restore to 5560Sstevel@tonic-gate * let fasttrap_return_common() know that it will find the 5570Sstevel@tonic-gate * return values in the %i's rather than the %o's. 5580Sstevel@tonic-gate */ 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate if (I(tp->ftt_instr)) { 5610Sstevel@tonic-gate int32_t imm; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate imm = tp->ftt_instr << 19; 5640Sstevel@tonic-gate imm >>= 19; 5650Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm; 5660Sstevel@tonic-gate } else { 5670Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + 5680Sstevel@tonic-gate fasttrap_getreg(rp, RS2(tp->ftt_instr)); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate /* 5720Sstevel@tonic-gate * Convert %o's to %i's; leave %g's as they are. 5730Sstevel@tonic-gate */ 5740Sstevel@tonic-gate rd = RD(tp->ftt_instr); 5750Sstevel@tonic-gate fasttrap_putreg(rp, ((rd & 0x18) == 0x8) ? rd + 16 : rd, value); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate pc = rp->r_g7 + FASTTRAP_OFF_RESTORE; 5780Sstevel@tonic-gate fake_restore = 1; 5790Sstevel@tonic-gate break; 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate case FASTTRAP_T_RETURN: 5830Sstevel@tonic-gate { 5840Sstevel@tonic-gate uintptr_t target; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* 5870Sstevel@tonic-gate * A return instruction is like a jmpl (without the link 5880Sstevel@tonic-gate * part) that executes an implicit restore. We've stashed 5890Sstevel@tonic-gate * the instruction: 5900Sstevel@tonic-gate * return %o0 5910Sstevel@tonic-gate * 5920Sstevel@tonic-gate * off of %g7 so we just need to place the target in %o0 5930Sstevel@tonic-gate * and set the %pc to point to the stashed return instruction. 5940Sstevel@tonic-gate * We use %o0 since that register disappears after the return 5950Sstevel@tonic-gate * executes, erasing any evidence of this tampering. 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate if (I(tp->ftt_instr)) { 5980Sstevel@tonic-gate int32_t imm; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate imm = tp->ftt_instr << 19; 6010Sstevel@tonic-gate imm >>= 19; 6020Sstevel@tonic-gate target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm; 6030Sstevel@tonic-gate } else { 6040Sstevel@tonic-gate target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + 6050Sstevel@tonic-gate fasttrap_getreg(rp, RS2(tp->ftt_instr)); 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate fasttrap_putreg(rp, R_O0, target); 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate pc = rp->r_g7 + FASTTRAP_OFF_RETURN; 6110Sstevel@tonic-gate fake_restore = 1; 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate case FASTTRAP_T_OR: 6160Sstevel@tonic-gate { 6170Sstevel@tonic-gate ulong_t value; 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate if (I(tp->ftt_instr)) { 6200Sstevel@tonic-gate int32_t imm; 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate imm = tp->ftt_instr << 19; 6230Sstevel@tonic-gate imm >>= 19; 6240Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) | imm; 6250Sstevel@tonic-gate } else { 6260Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) | 6270Sstevel@tonic-gate fasttrap_getreg(rp, RS2(tp->ftt_instr)); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), value); 6310Sstevel@tonic-gate pc = rp->r_npc; 6320Sstevel@tonic-gate npc = pc + 4; 6330Sstevel@tonic-gate break; 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate case FASTTRAP_T_SETHI: 6370Sstevel@tonic-gate if (RD(tp->ftt_instr) != R_G0) { 6380Sstevel@tonic-gate uint32_t imm32 = tp->ftt_instr << 10; 6390Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), (ulong_t)imm32); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate pc = rp->r_npc; 6420Sstevel@tonic-gate npc = pc + 4; 6430Sstevel@tonic-gate break; 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate case FASTTRAP_T_CCR: 6460Sstevel@tonic-gate { 6470Sstevel@tonic-gate uint_t c, v, z, n, taken; 6480Sstevel@tonic-gate uint_t ccr = rp->r_tstate >> TSTATE_CCR_SHIFT; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate if (tp->ftt_cc != 0) 6510Sstevel@tonic-gate ccr >>= 4; 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate c = (ccr >> 0) & 1; 6540Sstevel@tonic-gate v = (ccr >> 1) & 1; 6550Sstevel@tonic-gate z = (ccr >> 2) & 1; 6560Sstevel@tonic-gate n = (ccr >> 3) & 1; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate switch (tp->ftt_code) { 6590Sstevel@tonic-gate case 0x0: /* BN */ 6600Sstevel@tonic-gate taken = 0; break; 6610Sstevel@tonic-gate case 0x1: /* BE */ 6620Sstevel@tonic-gate taken = z; break; 6630Sstevel@tonic-gate case 0x2: /* BLE */ 6640Sstevel@tonic-gate taken = z | (n ^ v); break; 6650Sstevel@tonic-gate case 0x3: /* BL */ 6660Sstevel@tonic-gate taken = n ^ v; break; 6670Sstevel@tonic-gate case 0x4: /* BLEU */ 6680Sstevel@tonic-gate taken = c | z; break; 6690Sstevel@tonic-gate case 0x5: /* BCS (BLU) */ 6700Sstevel@tonic-gate taken = c; break; 6710Sstevel@tonic-gate case 0x6: /* BNEG */ 6720Sstevel@tonic-gate taken = n; break; 6730Sstevel@tonic-gate case 0x7: /* BVS */ 6740Sstevel@tonic-gate taken = v; break; 6750Sstevel@tonic-gate case 0x8: /* BA */ 6760Sstevel@tonic-gate /* 6770Sstevel@tonic-gate * We handle the BA case differently since the annul 6780Sstevel@tonic-gate * bit means something slightly different. 6790Sstevel@tonic-gate */ 6800Sstevel@tonic-gate panic("fasttrap: mishandled a branch"); 6810Sstevel@tonic-gate taken = 1; break; 6820Sstevel@tonic-gate case 0x9: /* BNE */ 6830Sstevel@tonic-gate taken = ~z; break; 6840Sstevel@tonic-gate case 0xa: /* BG */ 6850Sstevel@tonic-gate taken = ~(z | (n ^ v)); break; 6860Sstevel@tonic-gate case 0xb: /* BGE */ 6870Sstevel@tonic-gate taken = ~(n ^ v); break; 6880Sstevel@tonic-gate case 0xc: /* BGU */ 6890Sstevel@tonic-gate taken = ~(c | z); break; 6900Sstevel@tonic-gate case 0xd: /* BCC (BGEU) */ 6910Sstevel@tonic-gate taken = ~c; break; 6920Sstevel@tonic-gate case 0xe: /* BPOS */ 6930Sstevel@tonic-gate taken = ~n; break; 6940Sstevel@tonic-gate case 0xf: /* BVC */ 6950Sstevel@tonic-gate taken = ~v; break; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate if (taken & 1) { 6990Sstevel@tonic-gate pc = rp->r_npc; 7000Sstevel@tonic-gate npc = tp->ftt_dest; 7010Sstevel@tonic-gate } else if (tp->ftt_flags & FASTTRAP_F_ANNUL) { 7020Sstevel@tonic-gate /* 7030Sstevel@tonic-gate * Untaken annulled branches don't execute the 7040Sstevel@tonic-gate * instruction in the delay slot. 7050Sstevel@tonic-gate */ 7060Sstevel@tonic-gate pc = rp->r_npc + 4; 7070Sstevel@tonic-gate npc = pc + 4; 7080Sstevel@tonic-gate } else { 7090Sstevel@tonic-gate pc = rp->r_npc; 7100Sstevel@tonic-gate npc = pc + 4; 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate break; 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate case FASTTRAP_T_FCC: 7160Sstevel@tonic-gate { 7170Sstevel@tonic-gate uint_t fcc; 7180Sstevel@tonic-gate uint_t taken; 7190Sstevel@tonic-gate uint64_t fsr; 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate dtrace_getfsr(&fsr); 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (tp->ftt_cc == 0) { 7240Sstevel@tonic-gate fcc = (fsr >> 10) & 0x3; 7250Sstevel@tonic-gate } else { 7260Sstevel@tonic-gate uint_t shift; 7270Sstevel@tonic-gate ASSERT(tp->ftt_cc <= 3); 7280Sstevel@tonic-gate shift = 30 + tp->ftt_cc * 2; 7290Sstevel@tonic-gate fcc = (fsr >> shift) & 0x3; 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate switch (tp->ftt_code) { 7330Sstevel@tonic-gate case 0x0: /* FBN */ 7340Sstevel@tonic-gate taken = (1 << fcc) & (0|0|0|0); break; 7350Sstevel@tonic-gate case 0x1: /* FBNE */ 7360Sstevel@tonic-gate taken = (1 << fcc) & (8|4|2|0); break; 7370Sstevel@tonic-gate case 0x2: /* FBLG */ 7380Sstevel@tonic-gate taken = (1 << fcc) & (0|4|2|0); break; 7390Sstevel@tonic-gate case 0x3: /* FBUL */ 7400Sstevel@tonic-gate taken = (1 << fcc) & (8|0|2|0); break; 7410Sstevel@tonic-gate case 0x4: /* FBL */ 7420Sstevel@tonic-gate taken = (1 << fcc) & (0|0|2|0); break; 7430Sstevel@tonic-gate case 0x5: /* FBUG */ 7440Sstevel@tonic-gate taken = (1 << fcc) & (8|4|0|0); break; 7450Sstevel@tonic-gate case 0x6: /* FBG */ 7460Sstevel@tonic-gate taken = (1 << fcc) & (0|4|0|0); break; 7470Sstevel@tonic-gate case 0x7: /* FBU */ 7480Sstevel@tonic-gate taken = (1 << fcc) & (8|0|0|0); break; 7490Sstevel@tonic-gate case 0x8: /* FBA */ 7500Sstevel@tonic-gate /* 7510Sstevel@tonic-gate * We handle the FBA case differently since the annul 7520Sstevel@tonic-gate * bit means something slightly different. 7530Sstevel@tonic-gate */ 7540Sstevel@tonic-gate panic("fasttrap: mishandled a branch"); 7550Sstevel@tonic-gate taken = (1 << fcc) & (8|4|2|1); break; 7560Sstevel@tonic-gate case 0x9: /* FBE */ 7570Sstevel@tonic-gate taken = (1 << fcc) & (0|0|0|1); break; 7580Sstevel@tonic-gate case 0xa: /* FBUE */ 7590Sstevel@tonic-gate taken = (1 << fcc) & (8|0|0|1); break; 7600Sstevel@tonic-gate case 0xb: /* FBGE */ 7610Sstevel@tonic-gate taken = (1 << fcc) & (0|4|0|1); break; 7620Sstevel@tonic-gate case 0xc: /* FBUGE */ 7630Sstevel@tonic-gate taken = (1 << fcc) & (8|4|0|1); break; 7640Sstevel@tonic-gate case 0xd: /* FBLE */ 7650Sstevel@tonic-gate taken = (1 << fcc) & (0|0|2|1); break; 7660Sstevel@tonic-gate case 0xe: /* FBULE */ 7670Sstevel@tonic-gate taken = (1 << fcc) & (8|0|2|1); break; 7680Sstevel@tonic-gate case 0xf: /* FBO */ 7690Sstevel@tonic-gate taken = (1 << fcc) & (0|4|2|1); break; 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate if (taken) { 7730Sstevel@tonic-gate pc = rp->r_npc; 7740Sstevel@tonic-gate npc = tp->ftt_dest; 7750Sstevel@tonic-gate } else if (tp->ftt_flags & FASTTRAP_F_ANNUL) { 7760Sstevel@tonic-gate /* 7770Sstevel@tonic-gate * Untaken annulled branches don't execute the 7780Sstevel@tonic-gate * instruction in the delay slot. 7790Sstevel@tonic-gate */ 7800Sstevel@tonic-gate pc = rp->r_npc + 4; 7810Sstevel@tonic-gate npc = pc + 4; 7820Sstevel@tonic-gate } else { 7830Sstevel@tonic-gate pc = rp->r_npc; 7840Sstevel@tonic-gate npc = pc + 4; 7850Sstevel@tonic-gate } 7860Sstevel@tonic-gate break; 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate case FASTTRAP_T_REG: 7900Sstevel@tonic-gate { 7910Sstevel@tonic-gate uint64_t value; 7920Sstevel@tonic-gate uint_t taken; 7930Sstevel@tonic-gate uint_t reg = RS1(tp->ftt_instr); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate /* 7960Sstevel@tonic-gate * An ILP32 process shouldn't be using a branch predicated on 7970Sstevel@tonic-gate * an %i or an %l since it would violate the ABI. It's a 7980Sstevel@tonic-gate * violation of the ABI because we can't ensure deterministic 7990Sstevel@tonic-gate * behavior. We should have identified this case when we 8000Sstevel@tonic-gate * enabled the probe. 8010Sstevel@tonic-gate */ 8020Sstevel@tonic-gate ASSERT(p->p_model == DATAMODEL_LP64 || reg < 16); 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate value = fasttrap_getreg(rp, reg); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate switch (tp->ftt_code) { 8070Sstevel@tonic-gate case 0x1: /* BRZ */ 8080Sstevel@tonic-gate taken = (value == 0); break; 8090Sstevel@tonic-gate case 0x2: /* BRLEZ */ 8100Sstevel@tonic-gate taken = (value <= 0); break; 8110Sstevel@tonic-gate case 0x3: /* BRLZ */ 8120Sstevel@tonic-gate taken = (value < 0); break; 8130Sstevel@tonic-gate case 0x5: /* BRNZ */ 8140Sstevel@tonic-gate taken = (value != 0); break; 8150Sstevel@tonic-gate case 0x6: /* BRGZ */ 8160Sstevel@tonic-gate taken = (value > 0); break; 8170Sstevel@tonic-gate case 0x7: /* BRGEZ */ 8180Sstevel@tonic-gate taken = (value <= 0); break; 8190Sstevel@tonic-gate default: 8200Sstevel@tonic-gate case 0x0: 8210Sstevel@tonic-gate case 0x4: 8220Sstevel@tonic-gate panic("fasttrap: mishandled a branch"); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (taken) { 8260Sstevel@tonic-gate pc = rp->r_npc; 8270Sstevel@tonic-gate npc = tp->ftt_dest; 8280Sstevel@tonic-gate } else if (tp->ftt_flags & FASTTRAP_F_ANNUL) { 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * Untaken annulled branches don't execute the 8310Sstevel@tonic-gate * instruction in the delay slot. 8320Sstevel@tonic-gate */ 8330Sstevel@tonic-gate pc = rp->r_npc + 4; 8340Sstevel@tonic-gate npc = pc + 4; 8350Sstevel@tonic-gate } else { 8360Sstevel@tonic-gate pc = rp->r_npc; 8370Sstevel@tonic-gate npc = pc + 4; 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate break; 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate case FASTTRAP_T_ALWAYS: 8430Sstevel@tonic-gate /* 8440Sstevel@tonic-gate * BAs, BA,As... 8450Sstevel@tonic-gate */ 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate if (tp->ftt_flags & FASTTRAP_F_ANNUL) { 8480Sstevel@tonic-gate /* 8490Sstevel@tonic-gate * Annulled branch always instructions never execute 8500Sstevel@tonic-gate * the instruction in the delay slot. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate pc = tp->ftt_dest; 8530Sstevel@tonic-gate npc = tp->ftt_dest + 4; 8540Sstevel@tonic-gate } else { 8550Sstevel@tonic-gate pc = rp->r_npc; 8560Sstevel@tonic-gate npc = tp->ftt_dest; 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate break; 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate case FASTTRAP_T_RDPC: 8610Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc); 8620Sstevel@tonic-gate pc = rp->r_npc; 8630Sstevel@tonic-gate npc = pc + 4; 8640Sstevel@tonic-gate break; 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate case FASTTRAP_T_CALL: 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * It's a call _and_ link remember... 8690Sstevel@tonic-gate */ 8700Sstevel@tonic-gate rp->r_o7 = rp->r_pc; 8710Sstevel@tonic-gate pc = rp->r_npc; 8720Sstevel@tonic-gate npc = tp->ftt_dest; 8730Sstevel@tonic-gate break; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate case FASTTRAP_T_JMPL: 8760Sstevel@tonic-gate pc = rp->r_npc; 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate if (I(tp->ftt_instr)) { 8790Sstevel@tonic-gate uint_t rs1 = RS1(tp->ftt_instr); 8800Sstevel@tonic-gate int32_t imm; 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate imm = tp->ftt_instr << 19; 8830Sstevel@tonic-gate imm >>= 19; 8840Sstevel@tonic-gate npc = fasttrap_getreg(rp, rs1) + imm; 8850Sstevel@tonic-gate } else { 8860Sstevel@tonic-gate uint_t rs1 = RS1(tp->ftt_instr); 8870Sstevel@tonic-gate uint_t rs2 = RS2(tp->ftt_instr); 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate npc = fasttrap_getreg(rp, rs1) + 8900Sstevel@tonic-gate fasttrap_getreg(rp, rs2); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate /* 8940Sstevel@tonic-gate * Do the link part of the jump-and-link instruction. 8950Sstevel@tonic-gate */ 8960Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc); 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate break; 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate case FASTTRAP_T_COMMON: 9010Sstevel@tonic-gate { 9020Sstevel@tonic-gate curthread->t_dtrace_scrpc = rp->r_g7; 9030Sstevel@tonic-gate curthread->t_dtrace_astpc = rp->r_g7 + FASTTRAP_OFF_FTRET; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* 9060Sstevel@tonic-gate * Copy the instruction to a reserved location in the 9070Sstevel@tonic-gate * user-land thread structure, then set the PC to that 9080Sstevel@tonic-gate * location and leave the NPC alone. We take pains to ensure 9090Sstevel@tonic-gate * consistency in the instruction stream (See SPARC 9100Sstevel@tonic-gate * Architecture Manual Version 9, sections 8.4.7, A.20, and 9110Sstevel@tonic-gate * H.1.6; UltraSPARC I/II User's Manual, sections 3.1.1.1, 9120Sstevel@tonic-gate * and 13.6.4) by using the ASI ASI_BLK_COMMIT_S to copy the 9130Sstevel@tonic-gate * instruction into the user's address space without 9140Sstevel@tonic-gate * bypassing the I$. There's no AS_USER version of this ASI 9150Sstevel@tonic-gate * (as exist for other ASIs) so we use the lofault 9160Sstevel@tonic-gate * mechanism to catch faults. 9170Sstevel@tonic-gate */ 9180Sstevel@tonic-gate if (dtrace_blksuword32(rp->r_g7, &tp->ftt_instr, 1) == -1) { 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * If the copyout fails, then the process's state 9210Sstevel@tonic-gate * is not consistent (the effects of the traced 9220Sstevel@tonic-gate * instruction will never be seen). This process 9230Sstevel@tonic-gate * cannot be allowed to continue execution. 9240Sstevel@tonic-gate */ 9250Sstevel@tonic-gate fasttrap_sigtrap(curproc, curthread, pc); 9260Sstevel@tonic-gate return (0); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate curthread->t_dtrace_pc = pc; 9300Sstevel@tonic-gate curthread->t_dtrace_npc = npc; 9310Sstevel@tonic-gate curthread->t_dtrace_on = 1; 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate pc = curthread->t_dtrace_scrpc; 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate if (tp->ftt_retids != NULL) { 9360Sstevel@tonic-gate curthread->t_dtrace_step = 1; 9370Sstevel@tonic-gate curthread->t_dtrace_ret = 1; 9380Sstevel@tonic-gate npc = curthread->t_dtrace_astpc; 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate break; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate default: 9440Sstevel@tonic-gate panic("fasttrap: mishandled an instruction"); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate /* 9480Sstevel@tonic-gate * This bit me in the ass a couple of times, so lets toss this 9490Sstevel@tonic-gate * in as a cursory sanity check. 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate ASSERT(pc != rp->r_g7 + 4); 9520Sstevel@tonic-gate ASSERT(pc != rp->r_g7 + 8); 9530Sstevel@tonic-gate 954*1710Sahl done: 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * If there were no return probes when we first found the tracepoint, 9570Sstevel@tonic-gate * we should feel no obligation to honor any return probes that were 9580Sstevel@tonic-gate * subsequently enabled -- they'll just have to wait until the next 9590Sstevel@tonic-gate * time around. 9600Sstevel@tonic-gate */ 9610Sstevel@tonic-gate if (tp->ftt_retids != NULL) { 9620Sstevel@tonic-gate /* 9630Sstevel@tonic-gate * We need to wait until the results of the instruction are 9640Sstevel@tonic-gate * apparent before invoking any return probes. If this 9650Sstevel@tonic-gate * instruction was emulated we can just call 9660Sstevel@tonic-gate * fasttrap_return_common(); if it needs to be executed, we 9670Sstevel@tonic-gate * need to wait until we return to the kernel. 9680Sstevel@tonic-gate */ 9690Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_COMMON) { 9700Sstevel@tonic-gate fasttrap_return_common(rp, orig_pc, pid, fake_restore); 9710Sstevel@tonic-gate } else { 9720Sstevel@tonic-gate ASSERT(curthread->t_dtrace_ret != 0); 9730Sstevel@tonic-gate ASSERT(curthread->t_dtrace_pc == orig_pc); 9740Sstevel@tonic-gate ASSERT(curthread->t_dtrace_scrpc == rp->r_g7); 9750Sstevel@tonic-gate ASSERT(npc == curthread->t_dtrace_astpc); 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate } 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate ASSERT(pc != 0); 9800Sstevel@tonic-gate rp->r_pc = pc; 9810Sstevel@tonic-gate rp->r_npc = npc; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate return (0); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate int 9870Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp) 9880Sstevel@tonic-gate { 9890Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 9900Sstevel@tonic-gate pid_t pid; 9910Sstevel@tonic-gate uintptr_t pc = curthread->t_dtrace_pc; 9920Sstevel@tonic-gate uintptr_t npc = curthread->t_dtrace_npc; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate curthread->t_dtrace_pc = 0; 9950Sstevel@tonic-gate curthread->t_dtrace_npc = 0; 9960Sstevel@tonic-gate curthread->t_dtrace_scrpc = 0; 9970Sstevel@tonic-gate curthread->t_dtrace_astpc = 0; 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate /* 10000Sstevel@tonic-gate * Treat a child created by a call to vfork(2) as if it were its 10010Sstevel@tonic-gate * parent. We know there's only one thread of control in such a 10020Sstevel@tonic-gate * process: this one. 10030Sstevel@tonic-gate */ 10040Sstevel@tonic-gate while (p->p_flag & SVFORK) { 10050Sstevel@tonic-gate p = p->p_parent; 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate /* 10090Sstevel@tonic-gate * We set the %pc and %npc to their values when the traced 10100Sstevel@tonic-gate * instruction was initially executed so that it appears to 10110Sstevel@tonic-gate * dtrace_probe() that we're on the original instruction, and so that 10120Sstevel@tonic-gate * the user can't easily detect our complex web of lies. 10130Sstevel@tonic-gate * dtrace_return_probe() (our caller) will correctly set %pc and %npc 10140Sstevel@tonic-gate * after we return. 10150Sstevel@tonic-gate */ 10160Sstevel@tonic-gate rp->r_pc = pc; 10170Sstevel@tonic-gate rp->r_npc = npc; 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate pid = p->p_pid; 10200Sstevel@tonic-gate fasttrap_return_common(rp, pc, pid, 0); 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate return (0); 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate int 10260Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp) 10270Sstevel@tonic-gate { 10280Sstevel@tonic-gate fasttrap_instr_t instr = FASTTRAP_INSTR; 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate if (uwrite(p, &instr, 4, tp->ftt_pc) != 0) 10310Sstevel@tonic-gate return (-1); 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate return (0); 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate int 10370Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp) 10380Sstevel@tonic-gate { 10390Sstevel@tonic-gate fasttrap_instr_t instr; 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate /* 10420Sstevel@tonic-gate * Distinguish between read or write failures and a changed 10430Sstevel@tonic-gate * instruction. 10440Sstevel@tonic-gate */ 10450Sstevel@tonic-gate if (uread(p, &instr, 4, tp->ftt_pc) != 0) 10460Sstevel@tonic-gate return (0); 10470Sstevel@tonic-gate if (instr != FASTTRAP_INSTR && instr != BREAKPOINT_INSTR) 10480Sstevel@tonic-gate return (0); 10490Sstevel@tonic-gate if (uwrite(p, &tp->ftt_instr, 4, tp->ftt_pc) != 0) 10500Sstevel@tonic-gate return (-1); 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate return (0); 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate int 1056*1710Sahl fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc, 1057*1710Sahl fasttrap_probe_type_t type) 10580Sstevel@tonic-gate { 10590Sstevel@tonic-gate uint32_t instr; 10600Sstevel@tonic-gate int32_t disp; 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate /* 10630Sstevel@tonic-gate * Read the instruction at the given address out of the process's 10640Sstevel@tonic-gate * address space. We don't have to worry about a debugger 10650Sstevel@tonic-gate * changing this instruction before we overwrite it with our trap 10660Sstevel@tonic-gate * instruction since P_PR_LOCK is set. 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate if (uread(p, &instr, 4, pc) != 0) 10690Sstevel@tonic-gate return (-1); 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* 10720Sstevel@tonic-gate * Decode the instruction to fill in the probe flags. We can have 10730Sstevel@tonic-gate * the process execute most instructions on its own using a pc/npc 10740Sstevel@tonic-gate * trick, but pc-relative control transfer present a problem since 10750Sstevel@tonic-gate * we're relocating the instruction. We emulate these instructions 10760Sstevel@tonic-gate * in the kernel. We assume a default type and over-write that as 10770Sstevel@tonic-gate * needed. 10780Sstevel@tonic-gate * 10790Sstevel@tonic-gate * pc-relative instructions must be emulated for correctness; 10800Sstevel@tonic-gate * other instructions (which represent a large set of commonly traced 10810Sstevel@tonic-gate * instructions) are emulated or otherwise optimized for performance. 10820Sstevel@tonic-gate */ 10830Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_COMMON; 10840Sstevel@tonic-gate if (OP(instr) == 1) { 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * Call instructions. 10870Sstevel@tonic-gate */ 10880Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CALL; 10890Sstevel@tonic-gate disp = DISP30(instr) << 2; 10900Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp; 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate } else if (OP(instr) == 0) { 10930Sstevel@tonic-gate /* 10940Sstevel@tonic-gate * Branch instructions. 10950Sstevel@tonic-gate * 10960Sstevel@tonic-gate * Unconditional branches need careful attention when they're 10970Sstevel@tonic-gate * annulled: annulled unconditional branches never execute 10980Sstevel@tonic-gate * the instruction in the delay slot. 10990Sstevel@tonic-gate */ 11000Sstevel@tonic-gate switch (OP2(instr)) { 11010Sstevel@tonic-gate case OP2_ILLTRAP: 11020Sstevel@tonic-gate case 0x7: 11030Sstevel@tonic-gate /* 11040Sstevel@tonic-gate * The compiler may place an illtrap after a call to 11050Sstevel@tonic-gate * a function that returns a structure. In the case of 11060Sstevel@tonic-gate * a returned structure, the compiler places an illtrap 11070Sstevel@tonic-gate * whose const22 field is the size of the returned 11080Sstevel@tonic-gate * structure immediately following the delay slot of 11090Sstevel@tonic-gate * the call. To stay out of the way, we refuse to 11100Sstevel@tonic-gate * place tracepoints on top of illtrap instructions. 11110Sstevel@tonic-gate * 11120Sstevel@tonic-gate * This is one of the dumbest architectural decisions 11130Sstevel@tonic-gate * I've ever had to work around. 11140Sstevel@tonic-gate * 11150Sstevel@tonic-gate * We also identify the only illegal op2 value (See 11160Sstevel@tonic-gate * SPARC Architecture Manual Version 9, E.2 table 31). 11170Sstevel@tonic-gate */ 11180Sstevel@tonic-gate return (-1); 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate case OP2_BPcc: 11210Sstevel@tonic-gate if (COND(instr) == 8) { 11220Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS; 11230Sstevel@tonic-gate } else { 11240Sstevel@tonic-gate /* 11250Sstevel@tonic-gate * Check for an illegal instruction. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate if (CC(instr) & 1) 11280Sstevel@tonic-gate return (-1); 11290Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CCR; 11300Sstevel@tonic-gate tp->ftt_cc = CC(instr); 11310Sstevel@tonic-gate tp->ftt_code = COND(instr); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate if (A(instr) != 0) 11350Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL; 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate disp = DISP19(instr); 11380Sstevel@tonic-gate disp <<= 13; 11390Sstevel@tonic-gate disp >>= 11; 11400Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp; 11410Sstevel@tonic-gate break; 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate case OP2_Bicc: 11440Sstevel@tonic-gate if (COND(instr) == 8) { 11450Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS; 11460Sstevel@tonic-gate } else { 11470Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CCR; 11480Sstevel@tonic-gate tp->ftt_cc = 0; 11490Sstevel@tonic-gate tp->ftt_code = COND(instr); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate if (A(instr) != 0) 11530Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL; 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate disp = DISP22(instr); 11560Sstevel@tonic-gate disp <<= 10; 11570Sstevel@tonic-gate disp >>= 8; 11580Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp; 11590Sstevel@tonic-gate break; 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate case OP2_BPr: 11620Sstevel@tonic-gate /* 11630Sstevel@tonic-gate * Check for an illegal instruction. 11640Sstevel@tonic-gate */ 11650Sstevel@tonic-gate if ((RCOND(instr) & 3) == 0) 11660Sstevel@tonic-gate return (-1); 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate /* 11690Sstevel@tonic-gate * It's a violation of the v8plus ABI to use a 11700Sstevel@tonic-gate * register-predicated branch in a 32-bit app if 11710Sstevel@tonic-gate * the register used is an %l or an %i (%gs and %os 11720Sstevel@tonic-gate * are legit because they're not saved to the stack 11730Sstevel@tonic-gate * in 32-bit words when we take a trap). 11740Sstevel@tonic-gate */ 11750Sstevel@tonic-gate if (p->p_model == DATAMODEL_ILP32 && RS1(instr) >= 16) 11760Sstevel@tonic-gate return (-1); 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_REG; 11790Sstevel@tonic-gate if (A(instr) != 0) 11800Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL; 11810Sstevel@tonic-gate disp = DISP16(instr); 11820Sstevel@tonic-gate disp <<= 16; 11830Sstevel@tonic-gate disp >>= 14; 11840Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp; 11850Sstevel@tonic-gate tp->ftt_code = RCOND(instr); 11860Sstevel@tonic-gate break; 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate case OP2_SETHI: 11890Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_SETHI; 11900Sstevel@tonic-gate break; 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate case OP2_FBPfcc: 11930Sstevel@tonic-gate if (COND(instr) == 8) { 11940Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS; 11950Sstevel@tonic-gate } else { 11960Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_FCC; 11970Sstevel@tonic-gate tp->ftt_cc = CC(instr); 11980Sstevel@tonic-gate tp->ftt_code = COND(instr); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate if (A(instr) != 0) 12020Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL; 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate disp = DISP19(instr); 12050Sstevel@tonic-gate disp <<= 13; 12060Sstevel@tonic-gate disp >>= 11; 12070Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp; 12080Sstevel@tonic-gate break; 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate case OP2_FBfcc: 12110Sstevel@tonic-gate if (COND(instr) == 8) { 12120Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS; 12130Sstevel@tonic-gate } else { 12140Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_FCC; 12150Sstevel@tonic-gate tp->ftt_cc = 0; 12160Sstevel@tonic-gate tp->ftt_code = COND(instr); 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate if (A(instr) != 0) 12200Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL; 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate disp = DISP22(instr); 12230Sstevel@tonic-gate disp <<= 10; 12240Sstevel@tonic-gate disp >>= 8; 12250Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp; 12260Sstevel@tonic-gate break; 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate } else if (OP(instr) == 2) { 12300Sstevel@tonic-gate switch (OP3(instr)) { 12310Sstevel@tonic-gate case OP3_RETURN: 12320Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RETURN; 12330Sstevel@tonic-gate break; 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate case OP3_JMPL: 12360Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMPL; 12370Sstevel@tonic-gate break; 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate case OP3_RD: 12400Sstevel@tonic-gate if (RS1(instr) == 5) 12410Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RDPC; 12420Sstevel@tonic-gate break; 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate case OP3_SAVE: 12450Sstevel@tonic-gate /* 12460Sstevel@tonic-gate * We optimize for save instructions at function 12470Sstevel@tonic-gate * entry; see the comment in fasttrap_pid_probe() 12480Sstevel@tonic-gate * (near FASTTRAP_T_SAVE) for details. 12490Sstevel@tonic-gate */ 12500Sstevel@tonic-gate if (fasttrap_optimize_save != 0 && 1251*1710Sahl type == DTFTP_ENTRY && 12520Sstevel@tonic-gate I(instr) == 1 && RD(instr) == R_SP) 12530Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_SAVE; 12540Sstevel@tonic-gate break; 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate case OP3_RESTORE: 12570Sstevel@tonic-gate /* 12580Sstevel@tonic-gate * We optimize restore instructions at function 12590Sstevel@tonic-gate * return; see the comment in fasttrap_pid_probe() 12600Sstevel@tonic-gate * (near FASTTRAP_T_RESTORE) for details. 12610Sstevel@tonic-gate * 12620Sstevel@tonic-gate * rd must be an %o or %g register. 12630Sstevel@tonic-gate */ 12640Sstevel@tonic-gate if ((RD(instr) & 0x10) == 0) 12650Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RESTORE; 12660Sstevel@tonic-gate break; 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate case OP3_OR: 12690Sstevel@tonic-gate /* 12700Sstevel@tonic-gate * A large proportion of instructions in the delay 12710Sstevel@tonic-gate * slot of retl instructions are or's so we emulate 12720Sstevel@tonic-gate * these downstairs as an optimization. 12730Sstevel@tonic-gate */ 12740Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_OR; 12750Sstevel@tonic-gate break; 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate case OP3_TCC: 12780Sstevel@tonic-gate /* 12790Sstevel@tonic-gate * Breakpoint instructions are effectively position- 12800Sstevel@tonic-gate * dependent since the debugger uses the %pc value 12810Sstevel@tonic-gate * to lookup which breakpoint was executed. As a 12820Sstevel@tonic-gate * result, we can't actually instrument breakpoints. 12830Sstevel@tonic-gate */ 12840Sstevel@tonic-gate if (SW_TRAP(instr) == ST_BREAKPOINT) 12850Sstevel@tonic-gate return (-1); 12860Sstevel@tonic-gate break; 12870Sstevel@tonic-gate 12880Sstevel@tonic-gate case 0x19: 12890Sstevel@tonic-gate case 0x1d: 12900Sstevel@tonic-gate case 0x29: 12910Sstevel@tonic-gate case 0x33: 12920Sstevel@tonic-gate case 0x3f: 12930Sstevel@tonic-gate /* 12940Sstevel@tonic-gate * Identify illegal instructions (See SPARC 12950Sstevel@tonic-gate * Architecture Manual Version 9, E.2 table 32). 12960Sstevel@tonic-gate */ 12970Sstevel@tonic-gate return (-1); 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate } else if (OP(instr) == 3) { 13000Sstevel@tonic-gate uint32_t op3 = OP3(instr); 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate /* 13030Sstevel@tonic-gate * Identify illegal instructions (See SPARC Architecture 13040Sstevel@tonic-gate * Manual Version 9, E.2 table 33). 13050Sstevel@tonic-gate */ 13060Sstevel@tonic-gate if ((op3 & 0x28) == 0x28) { 13070Sstevel@tonic-gate if (op3 != OP3_PREFETCH && op3 != OP3_CASA && 13080Sstevel@tonic-gate op3 != OP3_PREFETCHA && op3 != OP3_CASXA) 13090Sstevel@tonic-gate return (-1); 13100Sstevel@tonic-gate } else { 13110Sstevel@tonic-gate if ((op3 & 0x0f) == 0x0c || (op3 & 0x3b) == 0x31) 13120Sstevel@tonic-gate return (-1); 13130Sstevel@tonic-gate } 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate tp->ftt_instr = instr; 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate /* 13190Sstevel@tonic-gate * We don't know how this tracepoint is going to be used, but in case 13200Sstevel@tonic-gate * it's used as part of a function return probe, we need to indicate 13210Sstevel@tonic-gate * whether it's always a return site or only potentially a return 13220Sstevel@tonic-gate * site. If it's part of a return probe, it's always going to be a 13230Sstevel@tonic-gate * return from that function if it's a restore instruction or if 13240Sstevel@tonic-gate * the previous instruction was a return. If we could reliably 13250Sstevel@tonic-gate * distinguish jump tables from return sites, this wouldn't be 13260Sstevel@tonic-gate * necessary. 13270Sstevel@tonic-gate */ 13280Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_RESTORE && 13290Sstevel@tonic-gate (uread(p, &instr, 4, pc - sizeof (instr)) != 0 || 13300Sstevel@tonic-gate !(OP(instr) == 2 && OP3(instr) == OP3_RETURN))) 13310Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_RETMAYBE; 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate return (0); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate /*ARGSUSED*/ 13370Sstevel@tonic-gate uint64_t 13380Sstevel@tonic-gate fasttrap_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes) 13390Sstevel@tonic-gate { 13400Sstevel@tonic-gate return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno)); 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate /*ARGSUSED*/ 13440Sstevel@tonic-gate uint64_t 13450Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, 13460Sstevel@tonic-gate int aframes) 13470Sstevel@tonic-gate { 13480Sstevel@tonic-gate return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno)); 13490Sstevel@tonic-gate } 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate static uint64_t fasttrap_getreg_fast_cnt; 13520Sstevel@tonic-gate static uint64_t fasttrap_getreg_mpcb_cnt; 13530Sstevel@tonic-gate static uint64_t fasttrap_getreg_slow_cnt; 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate static ulong_t 13560Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg) 13570Sstevel@tonic-gate { 13580Sstevel@tonic-gate ulong_t value; 13590Sstevel@tonic-gate dtrace_icookie_t cookie; 13600Sstevel@tonic-gate struct machpcb *mpcb; 13610Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t); 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate /* 13640Sstevel@tonic-gate * We have the %os and %gs in our struct regs, but if we need to 13650Sstevel@tonic-gate * snag a %l or %i we need to go scrounging around in the process's 13660Sstevel@tonic-gate * address space. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate if (reg == 0) 13690Sstevel@tonic-gate return (0); 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate if (reg < 16) 13720Sstevel@tonic-gate return ((&rp->r_g1)[reg - 1]); 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate /* 13750Sstevel@tonic-gate * Before we look at the user's stack, we'll check the register 13760Sstevel@tonic-gate * windows to see if the information we want is in there. 13770Sstevel@tonic-gate */ 13780Sstevel@tonic-gate cookie = dtrace_interrupt_disable(); 13790Sstevel@tonic-gate if (dtrace_getotherwin() > 0) { 13800Sstevel@tonic-gate value = dtrace_getreg_win(reg, 1); 13810Sstevel@tonic-gate dtrace_interrupt_enable(cookie); 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_fast_cnt, 1); 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate return (value); 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate dtrace_interrupt_enable(cookie); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate /* 13900Sstevel@tonic-gate * First check the machpcb structure to see if we've already read 13910Sstevel@tonic-gate * in the register window we're looking for; if we haven't, (and 13920Sstevel@tonic-gate * we probably haven't) try to copy in the value of the register. 13930Sstevel@tonic-gate */ 13940Sstevel@tonic-gate mpcb = (struct machpcb *)((caddr_t)rp - REGOFF); 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 13970Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS); 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) { 14000Sstevel@tonic-gate struct rwindow *rwin = (void *)mpcb->mpcb_wbuf; 14010Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt; 14020Sstevel@tonic-gate do { 14030Sstevel@tonic-gate i--; 14040Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp) 14050Sstevel@tonic-gate continue; 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1); 14080Sstevel@tonic-gate return (rwin[i].rw_local[reg - 16]); 14090Sstevel@tonic-gate } while (i > 0); 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate if (fasttrap_fulword(&fr->fr_local[reg - 16], &value) != 0) 14130Sstevel@tonic-gate goto err; 14140Sstevel@tonic-gate } else { 14151048Sraf struct frame32 *fr = 14161048Sraf (struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp; 14170Sstevel@tonic-gate uint32_t *v32 = (uint32_t *)&value; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) { 14200Sstevel@tonic-gate struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf; 14210Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt; 14220Sstevel@tonic-gate do { 14230Sstevel@tonic-gate i--; 14240Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp) 14250Sstevel@tonic-gate continue; 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1); 14280Sstevel@tonic-gate return (rwin[i].rw_local[reg - 16]); 14290Sstevel@tonic-gate } while (i > 0); 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate if (fasttrap_fuword32(&fr->fr_local[reg - 16], &v32[1]) != 0) 14330Sstevel@tonic-gate goto err; 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate v32[0] = 0; 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_slow_cnt, 1); 14390Sstevel@tonic-gate return (value); 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate err: 14420Sstevel@tonic-gate /* 14430Sstevel@tonic-gate * If the copy in failed, the process will be in a irrecoverable 14440Sstevel@tonic-gate * state, and we have no choice but to kill it. 14450Sstevel@tonic-gate */ 14460Sstevel@tonic-gate psignal(ttoproc(curthread), SIGILL); 14470Sstevel@tonic-gate return (0); 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate static uint64_t fasttrap_putreg_fast_cnt; 14510Sstevel@tonic-gate static uint64_t fasttrap_putreg_mpcb_cnt; 14520Sstevel@tonic-gate static uint64_t fasttrap_putreg_slow_cnt; 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate static void 14550Sstevel@tonic-gate fasttrap_putreg(struct regs *rp, uint_t reg, ulong_t value) 14560Sstevel@tonic-gate { 14570Sstevel@tonic-gate dtrace_icookie_t cookie; 14580Sstevel@tonic-gate struct machpcb *mpcb; 14590Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t); 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate if (reg == 0) 14620Sstevel@tonic-gate return; 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate if (reg < 16) { 14650Sstevel@tonic-gate (&rp->r_g1)[reg - 1] = value; 14660Sstevel@tonic-gate return; 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate /* 14700Sstevel@tonic-gate * If the user process is still using some register windows, we 14710Sstevel@tonic-gate * can just place the value in the correct window. 14720Sstevel@tonic-gate */ 14730Sstevel@tonic-gate cookie = dtrace_interrupt_disable(); 14740Sstevel@tonic-gate if (dtrace_getotherwin() > 0) { 14750Sstevel@tonic-gate dtrace_putreg_win(reg, value); 14760Sstevel@tonic-gate dtrace_interrupt_enable(cookie); 14770Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_fast_cnt, 1); 14780Sstevel@tonic-gate return; 14790Sstevel@tonic-gate } 14800Sstevel@tonic-gate dtrace_interrupt_enable(cookie); 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate /* 14830Sstevel@tonic-gate * First see if there's a copy of the register window in the 14840Sstevel@tonic-gate * machpcb structure that we can modify; if there isn't try to 14850Sstevel@tonic-gate * copy out the value. If that fails, we try to create a new 14860Sstevel@tonic-gate * register window in the machpcb structure. While this isn't 14870Sstevel@tonic-gate * _precisely_ the intended use of the machpcb structure, it 14880Sstevel@tonic-gate * can't cause any problems since we know at this point in the 14890Sstevel@tonic-gate * code that all of the user's data have been flushed out of the 14900Sstevel@tonic-gate * register file (since %otherwin is 0). 14910Sstevel@tonic-gate */ 14920Sstevel@tonic-gate mpcb = (struct machpcb *)((caddr_t)rp - REGOFF); 14930Sstevel@tonic-gate 14940Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 14950Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS); 14960Sstevel@tonic-gate struct rwindow *rwin = (struct rwindow *)mpcb->mpcb_wbuf; 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) { 14990Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt; 15000Sstevel@tonic-gate do { 15010Sstevel@tonic-gate i--; 15020Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp) 15030Sstevel@tonic-gate continue; 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate rwin[i].rw_local[reg - 16] = value; 15060Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1); 15070Sstevel@tonic-gate return; 15080Sstevel@tonic-gate } while (i > 0); 15090Sstevel@tonic-gate } 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate if (fasttrap_sulword(&fr->fr_local[reg - 16], value) != 0) { 15120Sstevel@tonic-gate if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr, 15130Sstevel@tonic-gate &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0) 15140Sstevel@tonic-gate goto err; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = value; 15170Sstevel@tonic-gate mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp; 15180Sstevel@tonic-gate mpcb->mpcb_wbcnt++; 15190Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1); 15200Sstevel@tonic-gate return; 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate } else { 15231048Sraf struct frame32 *fr = 15241048Sraf (struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp; 15250Sstevel@tonic-gate struct rwindow32 *rwin = (struct rwindow32 *)mpcb->mpcb_wbuf; 15260Sstevel@tonic-gate uint32_t v32 = (uint32_t)value; 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) { 15290Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt; 15300Sstevel@tonic-gate do { 15310Sstevel@tonic-gate i--; 15320Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp) 15330Sstevel@tonic-gate continue; 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate rwin[i].rw_local[reg - 16] = v32; 15360Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1); 15370Sstevel@tonic-gate return; 15380Sstevel@tonic-gate } while (i > 0); 15390Sstevel@tonic-gate } 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate if (fasttrap_suword32(&fr->fr_local[reg - 16], v32) != 0) { 15420Sstevel@tonic-gate if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr, 15430Sstevel@tonic-gate &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0) 15440Sstevel@tonic-gate goto err; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = v32; 15470Sstevel@tonic-gate mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp; 15480Sstevel@tonic-gate mpcb->mpcb_wbcnt++; 15490Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1); 15500Sstevel@tonic-gate return; 15510Sstevel@tonic-gate } 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_slow_cnt, 1); 15550Sstevel@tonic-gate return; 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate err: 15580Sstevel@tonic-gate /* 15590Sstevel@tonic-gate * If we couldn't record this register's value, the process is in an 15600Sstevel@tonic-gate * irrecoverable state and we have no choice but to euthanize it. 15610Sstevel@tonic-gate */ 15620Sstevel@tonic-gate psignal(ttoproc(curthread), SIGILL); 15630Sstevel@tonic-gate } 1564