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
51710Sahl * Common Development and Distribution License (the "License").
61710Sahl * 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 /*
233944Sahl * Copyright 2007 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
1794685Sahl #define R_I4 28
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * Check the comment in fasttrap.h when changing these offsets or adding
1830Sstevel@tonic-gate * new instructions.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate #define FASTTRAP_OFF_SAVE 64
1860Sstevel@tonic-gate #define FASTTRAP_OFF_RESTORE 68
1870Sstevel@tonic-gate #define FASTTRAP_OFF_FTRET 72
1880Sstevel@tonic-gate #define FASTTRAP_OFF_RETURN 76
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate #define BREAKPOINT_INSTR 0x91d02001 /* ta 1 */
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate * Tunable to let users turn off the fancy save instruction optimization.
1940Sstevel@tonic-gate * If a program is non-ABI compliant, there's a possibility that the save
1950Sstevel@tonic-gate * instruction optimization could cause an error.
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate int fasttrap_optimize_save = 1;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate static uint64_t
fasttrap_anarg(struct regs * rp,int argno)2000Sstevel@tonic-gate fasttrap_anarg(struct regs *rp, int argno)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate uint64_t value;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate if (argno < 6)
2050Sstevel@tonic-gate return ((&rp->r_o0)[argno]);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_NATIVE) {
2080Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2110Sstevel@tonic-gate value = dtrace_fulword(&fr->fr_argd[argno]);
2120Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
2130Sstevel@tonic-gate CPU_DTRACE_BADALIGN);
2140Sstevel@tonic-gate } else {
2150Sstevel@tonic-gate struct frame32 *fr = (struct frame32 *)rp->r_sp;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
2180Sstevel@tonic-gate value = dtrace_fuword32(&fr->fr_argd[argno]);
2190Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR |
2200Sstevel@tonic-gate CPU_DTRACE_BADALIGN);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate return (value);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate static ulong_t fasttrap_getreg(struct regs *, uint_t);
2270Sstevel@tonic-gate static void fasttrap_putreg(struct regs *, uint_t, ulong_t);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate static void
fasttrap_usdt_args(fasttrap_probe_t * probe,struct regs * rp,uint_t fake_restore,int argc,uintptr_t * argv)2304685Sahl fasttrap_usdt_args(fasttrap_probe_t *probe, struct regs *rp,
2314685Sahl uint_t fake_restore, int argc, uintptr_t *argv)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate int i, x, cap = MIN(argc, probe->ftp_nargs);
2344685Sahl int inc = (fake_restore ? 16 : 0);
2354685Sahl
2364685Sahl /*
2374685Sahl * The only way we'll hit the fake_restore case is if a USDT probe is
2384685Sahl * invoked as a tail-call. While it wouldn't be incorrect, we can
2394685Sahl * avoid a call to fasttrap_getreg(), and safely use rp->r_sp
2404685Sahl * directly since a tail-call can't be made if the invoked function
2414685Sahl * would use the argument dump space (i.e. if there were more than
2424685Sahl * 6 arguments). We take this shortcut because unconditionally rooting
2434685Sahl * around for R_FP (R_SP + 16) would be unnecessarily painful.
2444685Sahl */
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)
2544685Sahl argv[i] = fasttrap_getreg(rp, R_O0 + x + inc);
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)
2674685Sahl argv[i] = fasttrap_getreg(rp, R_O0 + x + inc);
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
fasttrap_return_common(struct regs * rp,uintptr_t pc,pid_t pid,uint_t fake_restore)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 &&
294*4821Sahl tp->ftt_proc->ftpc_acount != 0)
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
3111710Sahl if (id->fti_ptype == DTFTP_POST_OFFSETS) {
3124685Sahl if (probe->ftp_argmap != NULL && fake_restore) {
3130Sstevel@tonic-gate uintptr_t t[5];
3140Sstevel@tonic-gate
3154685Sahl fasttrap_usdt_args(probe, rp, fake_restore,
3164685Sahl sizeof (t) / sizeof (t[0]), t);
3174685Sahl
3184685Sahl cookie = dtrace_interrupt_disable();
3194685Sahl DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
3204685Sahl dtrace_probe(probe->ftp_id, t[0], t[1],
3214685Sahl t[2], t[3], t[4]);
3224685Sahl DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
3234685Sahl dtrace_interrupt_enable(cookie);
3244685Sahl
3254685Sahl } else if (probe->ftp_argmap != NULL) {
3264685Sahl uintptr_t t[5];
3274685Sahl
3284685Sahl fasttrap_usdt_args(probe, rp, fake_restore,
3290Sstevel@tonic-gate sizeof (t) / sizeof (t[0]), t);
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate dtrace_probe(probe->ftp_id, t[0], t[1],
3320Sstevel@tonic-gate t[2], t[3], t[4]);
3334685Sahl
3344685Sahl } else if (fake_restore) {
3354685Sahl uintptr_t arg0 = fasttrap_getreg(rp, R_I0);
3364685Sahl uintptr_t arg1 = fasttrap_getreg(rp, R_I1);
3374685Sahl uintptr_t arg2 = fasttrap_getreg(rp, R_I2);
3384685Sahl uintptr_t arg3 = fasttrap_getreg(rp, R_I3);
3394685Sahl uintptr_t arg4 = fasttrap_getreg(rp, R_I4);
3404685Sahl
3414685Sahl cookie = dtrace_interrupt_disable();
3424685Sahl DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
3434685Sahl dtrace_probe(probe->ftp_id, arg0, arg1,
3444685Sahl arg2, arg3, arg4);
3454685Sahl DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
3464685Sahl dtrace_interrupt_enable(cookie);
3474685Sahl
3484685Sahl } else {
3494685Sahl dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1,
3504685Sahl rp->r_o2, rp->r_o3, rp->r_o4);
3510Sstevel@tonic-gate }
3524685Sahl
3530Sstevel@tonic-gate continue;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * If this is only a possible return point, we must
3580Sstevel@tonic-gate * be looking at a potential tail call in leaf context.
3590Sstevel@tonic-gate * If the %npc is still within this function, then we
3600Sstevel@tonic-gate * must have misidentified a jmpl as a tail-call when it
3610Sstevel@tonic-gate * is, in fact, part of a jump table. It would be nice to
3620Sstevel@tonic-gate * remove this tracepoint, but this is neither the time
3630Sstevel@tonic-gate * nor the place.
3640Sstevel@tonic-gate */
3650Sstevel@tonic-gate if ((tp->ftt_flags & FASTTRAP_F_RETMAYBE) &&
3660Sstevel@tonic-gate rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
3670Sstevel@tonic-gate continue;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate * It's possible for a function to branch to the delay slot
3710Sstevel@tonic-gate * of an instruction that we've identified as a return site.
3720Sstevel@tonic-gate * We can dectect this spurious return probe activation by
3730Sstevel@tonic-gate * observing that in this case %npc will be %pc + 4 and %npc
3740Sstevel@tonic-gate * will be inside the current function (unless the user is
3750Sstevel@tonic-gate * doing _crazy_ instruction picking in which case there's
3760Sstevel@tonic-gate * very little we can do). The second check is important
3770Sstevel@tonic-gate * in case the last instructions of a function make a tail-
3780Sstevel@tonic-gate * call to the function located immediately subsequent.
3790Sstevel@tonic-gate */
3800Sstevel@tonic-gate if (rp->r_npc == rp->r_pc + 4 &&
3810Sstevel@tonic-gate rp->r_npc - probe->ftp_faddr < probe->ftp_fsize)
3820Sstevel@tonic-gate continue;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate * The first argument is the offset of return tracepoint
3860Sstevel@tonic-gate * in the function; the remaining arguments are the return
3870Sstevel@tonic-gate * values.
3880Sstevel@tonic-gate *
3890Sstevel@tonic-gate * If fake_restore is set, we need to pull the return values
3900Sstevel@tonic-gate * out of the %i's rather than the %o's -- a little trickier.
3910Sstevel@tonic-gate */
3920Sstevel@tonic-gate if (!fake_restore) {
3930Sstevel@tonic-gate dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
3940Sstevel@tonic-gate rp->r_o0, rp->r_o1, rp->r_o2, rp->r_o3);
3950Sstevel@tonic-gate } else {
3960Sstevel@tonic-gate uintptr_t arg0 = fasttrap_getreg(rp, R_I0);
3970Sstevel@tonic-gate uintptr_t arg1 = fasttrap_getreg(rp, R_I1);
3980Sstevel@tonic-gate uintptr_t arg2 = fasttrap_getreg(rp, R_I2);
3990Sstevel@tonic-gate uintptr_t arg3 = fasttrap_getreg(rp, R_I3);
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate cookie = dtrace_interrupt_disable();
4020Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_FAKERESTORE);
4030Sstevel@tonic-gate dtrace_probe(probe->ftp_id, pc - probe->ftp_faddr,
4040Sstevel@tonic-gate arg0, arg1, arg2, arg3);
4050Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_FAKERESTORE);
4060Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate mutex_exit(pid_mtx);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate int
fasttrap_pid_probe(struct regs * rp)4140Sstevel@tonic-gate fasttrap_pid_probe(struct regs *rp)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate proc_t *p = curproc;
4170Sstevel@tonic-gate fasttrap_tracepoint_t *tp, tp_local;
4180Sstevel@tonic-gate fasttrap_id_t *id;
4190Sstevel@tonic-gate pid_t pid;
4200Sstevel@tonic-gate uintptr_t pc = rp->r_pc;
4210Sstevel@tonic-gate uintptr_t npc = rp->r_npc;
4220Sstevel@tonic-gate uintptr_t orig_pc = pc;
4230Sstevel@tonic-gate fasttrap_bucket_t *bucket;
4240Sstevel@tonic-gate kmutex_t *pid_mtx;
4251710Sahl uint_t fake_restore = 0, is_enabled = 0;
4260Sstevel@tonic-gate dtrace_icookie_t cookie;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate * It's possible that a user (in a veritable orgy of bad planning)
4300Sstevel@tonic-gate * could redirect this thread's flow of control before it reached the
4310Sstevel@tonic-gate * return probe fasttrap. In this case we need to kill the process
4320Sstevel@tonic-gate * since it's in a unrecoverable state.
4330Sstevel@tonic-gate */
4340Sstevel@tonic-gate if (curthread->t_dtrace_step) {
4350Sstevel@tonic-gate ASSERT(curthread->t_dtrace_on);
4360Sstevel@tonic-gate fasttrap_sigtrap(p, curthread, pc);
4370Sstevel@tonic-gate return (0);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate * Clear all user tracing flags.
4420Sstevel@tonic-gate */
4430Sstevel@tonic-gate curthread->t_dtrace_ft = 0;
4440Sstevel@tonic-gate curthread->t_dtrace_pc = 0;
4450Sstevel@tonic-gate curthread->t_dtrace_npc = 0;
4460Sstevel@tonic-gate curthread->t_dtrace_scrpc = 0;
4470Sstevel@tonic-gate curthread->t_dtrace_astpc = 0;
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate * Treat a child created by a call to vfork(2) as if it were its
4510Sstevel@tonic-gate * parent. We know that there's only one thread of control in such a
4520Sstevel@tonic-gate * process: this one.
4530Sstevel@tonic-gate */
4540Sstevel@tonic-gate while (p->p_flag & SVFORK) {
4550Sstevel@tonic-gate p = p->p_parent;
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate pid = p->p_pid;
4590Sstevel@tonic-gate pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
4600Sstevel@tonic-gate mutex_enter(pid_mtx);
4610Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * Lookup the tracepoint that the process just hit.
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
4670Sstevel@tonic-gate if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
468*4821Sahl tp->ftt_proc->ftpc_acount != 0)
4690Sstevel@tonic-gate break;
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate * If we couldn't find a matching tracepoint, either a tracepoint has
4740Sstevel@tonic-gate * been inserted without using the pid<pid> ioctl interface (see
4750Sstevel@tonic-gate * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
4760Sstevel@tonic-gate */
4770Sstevel@tonic-gate if (tp == NULL) {
4780Sstevel@tonic-gate mutex_exit(pid_mtx);
4790Sstevel@tonic-gate return (-1);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
4830Sstevel@tonic-gate fasttrap_probe_t *probe = id->fti_probe;
4841710Sahl int isentry = (id->fti_ptype == DTFTP_ENTRY);
4851710Sahl
4861710Sahl if (id->fti_ptype == DTFTP_IS_ENABLED) {
4871710Sahl is_enabled = 1;
4881710Sahl continue;
4891710Sahl }
4901710Sahl
4910Sstevel@tonic-gate /*
4920Sstevel@tonic-gate * We note that this was an entry probe to help ustack() find
4930Sstevel@tonic-gate * the first caller.
4940Sstevel@tonic-gate */
4951710Sahl if (isentry) {
4960Sstevel@tonic-gate cookie = dtrace_interrupt_disable();
4970Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate dtrace_probe(probe->ftp_id, rp->r_o0, rp->r_o1, rp->r_o2,
5000Sstevel@tonic-gate rp->r_o3, rp->r_o4);
5010Sstevel@tonic-gate if (isentry) {
5020Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
5030Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * We're about to do a bunch of work so we cache a local copy of
5090Sstevel@tonic-gate * the tracepoint to emulate the instruction, and then find the
5100Sstevel@tonic-gate * tracepoint again later if we need to light up any return probes.
5110Sstevel@tonic-gate */
5120Sstevel@tonic-gate tp_local = *tp;
5130Sstevel@tonic-gate mutex_exit(pid_mtx);
5140Sstevel@tonic-gate tp = &tp_local;
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate /*
5171710Sahl * If there's an is-enabled probe conntected to this tracepoint it
5181710Sahl * means that there was a 'mov %g0, %o0' instruction that was placed
5191710Sahl * there by DTrace when the binary was linked. As this probe is, in
5201710Sahl * fact, enabled, we need to stuff 1 into %o0. Accordingly, we can
5211710Sahl * bypass all the instruction emulation logic since we know the
5221710Sahl * inevitable result. It's possible that a user could construct a
5231710Sahl * scenario where the 'is-enabled' probe was on some other
5241710Sahl * instruction, but that would be a rather exotic way to shoot oneself
5251710Sahl * in the foot.
5261710Sahl */
5271710Sahl if (is_enabled) {
5281710Sahl rp->r_o0 = 1;
5291710Sahl pc = rp->r_npc;
5301710Sahl npc = pc + 4;
5311710Sahl goto done;
5321710Sahl }
5331710Sahl
5341710Sahl /*
5351710Sahl * We emulate certain types of instructions to ensure correctness
5360Sstevel@tonic-gate * (in the case of position dependent instructions) or optimize
5370Sstevel@tonic-gate * common cases. The rest we have the thread execute back in user-
5380Sstevel@tonic-gate * land.
5390Sstevel@tonic-gate */
5400Sstevel@tonic-gate switch (tp->ftt_type) {
5410Sstevel@tonic-gate case FASTTRAP_T_SAVE:
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate int32_t imm;
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * This an optimization to let us handle function entry
5470Sstevel@tonic-gate * probes more efficiently. Many functions begin with a save
5480Sstevel@tonic-gate * instruction that follows the pattern:
5490Sstevel@tonic-gate * save %sp, <imm>, %sp
5500Sstevel@tonic-gate *
5510Sstevel@tonic-gate * Meanwhile, we've stashed the instruction:
5520Sstevel@tonic-gate * save %g1, %g0, %sp
5530Sstevel@tonic-gate *
5540Sstevel@tonic-gate * off of %g7, so all we have to do is stick the right value
5550Sstevel@tonic-gate * into %g1 and reset %pc to point to the instruction we've
5560Sstevel@tonic-gate * cleverly hidden (%npc should not be touched).
5570Sstevel@tonic-gate */
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate imm = tp->ftt_instr << 19;
5600Sstevel@tonic-gate imm >>= 19;
5610Sstevel@tonic-gate rp->r_g1 = rp->r_sp + imm;
5620Sstevel@tonic-gate pc = rp->r_g7 + FASTTRAP_OFF_SAVE;
5630Sstevel@tonic-gate break;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate case FASTTRAP_T_RESTORE:
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate ulong_t value;
5690Sstevel@tonic-gate uint_t rd;
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate * This is an optimization to let us handle function
5730Sstevel@tonic-gate * return probes more efficiently. Most non-leaf functions
5740Sstevel@tonic-gate * end with the sequence:
5750Sstevel@tonic-gate * ret
5760Sstevel@tonic-gate * restore <reg>, <reg_or_imm>, %oX
5770Sstevel@tonic-gate *
5780Sstevel@tonic-gate * We've stashed the instruction:
5790Sstevel@tonic-gate * restore %g0, %g0, %g0
5800Sstevel@tonic-gate *
5810Sstevel@tonic-gate * off of %g7 so we just need to place the correct value
5820Sstevel@tonic-gate * in the right %i register (since after our fake-o
5830Sstevel@tonic-gate * restore, the %i's will become the %o's) and set the %pc
5840Sstevel@tonic-gate * to point to our hidden restore. We also set fake_restore to
5850Sstevel@tonic-gate * let fasttrap_return_common() know that it will find the
5860Sstevel@tonic-gate * return values in the %i's rather than the %o's.
5870Sstevel@tonic-gate */
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate if (I(tp->ftt_instr)) {
5900Sstevel@tonic-gate int32_t imm;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate imm = tp->ftt_instr << 19;
5930Sstevel@tonic-gate imm >>= 19;
5940Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
5950Sstevel@tonic-gate } else {
5960Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
5970Sstevel@tonic-gate fasttrap_getreg(rp, RS2(tp->ftt_instr));
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate * Convert %o's to %i's; leave %g's as they are.
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate rd = RD(tp->ftt_instr);
6040Sstevel@tonic-gate fasttrap_putreg(rp, ((rd & 0x18) == 0x8) ? rd + 16 : rd, value);
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate pc = rp->r_g7 + FASTTRAP_OFF_RESTORE;
6070Sstevel@tonic-gate fake_restore = 1;
6080Sstevel@tonic-gate break;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate case FASTTRAP_T_RETURN:
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate uintptr_t target;
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate * A return instruction is like a jmpl (without the link
6170Sstevel@tonic-gate * part) that executes an implicit restore. We've stashed
6180Sstevel@tonic-gate * the instruction:
6190Sstevel@tonic-gate * return %o0
6200Sstevel@tonic-gate *
6210Sstevel@tonic-gate * off of %g7 so we just need to place the target in %o0
6220Sstevel@tonic-gate * and set the %pc to point to the stashed return instruction.
6230Sstevel@tonic-gate * We use %o0 since that register disappears after the return
6240Sstevel@tonic-gate * executes, erasing any evidence of this tampering.
6250Sstevel@tonic-gate */
6260Sstevel@tonic-gate if (I(tp->ftt_instr)) {
6270Sstevel@tonic-gate int32_t imm;
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate imm = tp->ftt_instr << 19;
6300Sstevel@tonic-gate imm >>= 19;
6310Sstevel@tonic-gate target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) + imm;
6320Sstevel@tonic-gate } else {
6330Sstevel@tonic-gate target = fasttrap_getreg(rp, RS1(tp->ftt_instr)) +
6340Sstevel@tonic-gate fasttrap_getreg(rp, RS2(tp->ftt_instr));
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate fasttrap_putreg(rp, R_O0, target);
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate pc = rp->r_g7 + FASTTRAP_OFF_RETURN;
6400Sstevel@tonic-gate fake_restore = 1;
6410Sstevel@tonic-gate break;
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate case FASTTRAP_T_OR:
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate ulong_t value;
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate if (I(tp->ftt_instr)) {
6490Sstevel@tonic-gate int32_t imm;
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate imm = tp->ftt_instr << 19;
6520Sstevel@tonic-gate imm >>= 19;
6530Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) | imm;
6540Sstevel@tonic-gate } else {
6550Sstevel@tonic-gate value = fasttrap_getreg(rp, RS1(tp->ftt_instr)) |
6560Sstevel@tonic-gate fasttrap_getreg(rp, RS2(tp->ftt_instr));
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), value);
6600Sstevel@tonic-gate pc = rp->r_npc;
6610Sstevel@tonic-gate npc = pc + 4;
6620Sstevel@tonic-gate break;
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate case FASTTRAP_T_SETHI:
6660Sstevel@tonic-gate if (RD(tp->ftt_instr) != R_G0) {
6670Sstevel@tonic-gate uint32_t imm32 = tp->ftt_instr << 10;
6680Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), (ulong_t)imm32);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate pc = rp->r_npc;
6710Sstevel@tonic-gate npc = pc + 4;
6720Sstevel@tonic-gate break;
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate case FASTTRAP_T_CCR:
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate uint_t c, v, z, n, taken;
6770Sstevel@tonic-gate uint_t ccr = rp->r_tstate >> TSTATE_CCR_SHIFT;
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate if (tp->ftt_cc != 0)
6800Sstevel@tonic-gate ccr >>= 4;
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate c = (ccr >> 0) & 1;
6830Sstevel@tonic-gate v = (ccr >> 1) & 1;
6840Sstevel@tonic-gate z = (ccr >> 2) & 1;
6850Sstevel@tonic-gate n = (ccr >> 3) & 1;
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate switch (tp->ftt_code) {
6880Sstevel@tonic-gate case 0x0: /* BN */
6890Sstevel@tonic-gate taken = 0; break;
6900Sstevel@tonic-gate case 0x1: /* BE */
6910Sstevel@tonic-gate taken = z; break;
6920Sstevel@tonic-gate case 0x2: /* BLE */
6930Sstevel@tonic-gate taken = z | (n ^ v); break;
6940Sstevel@tonic-gate case 0x3: /* BL */
6950Sstevel@tonic-gate taken = n ^ v; break;
6960Sstevel@tonic-gate case 0x4: /* BLEU */
6970Sstevel@tonic-gate taken = c | z; break;
6980Sstevel@tonic-gate case 0x5: /* BCS (BLU) */
6990Sstevel@tonic-gate taken = c; break;
7000Sstevel@tonic-gate case 0x6: /* BNEG */
7010Sstevel@tonic-gate taken = n; break;
7020Sstevel@tonic-gate case 0x7: /* BVS */
7030Sstevel@tonic-gate taken = v; break;
7040Sstevel@tonic-gate case 0x8: /* BA */
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate * We handle the BA case differently since the annul
7070Sstevel@tonic-gate * bit means something slightly different.
7080Sstevel@tonic-gate */
7090Sstevel@tonic-gate panic("fasttrap: mishandled a branch");
7100Sstevel@tonic-gate taken = 1; break;
7110Sstevel@tonic-gate case 0x9: /* BNE */
7120Sstevel@tonic-gate taken = ~z; break;
7130Sstevel@tonic-gate case 0xa: /* BG */
7140Sstevel@tonic-gate taken = ~(z | (n ^ v)); break;
7150Sstevel@tonic-gate case 0xb: /* BGE */
7160Sstevel@tonic-gate taken = ~(n ^ v); break;
7170Sstevel@tonic-gate case 0xc: /* BGU */
7180Sstevel@tonic-gate taken = ~(c | z); break;
7190Sstevel@tonic-gate case 0xd: /* BCC (BGEU) */
7200Sstevel@tonic-gate taken = ~c; break;
7210Sstevel@tonic-gate case 0xe: /* BPOS */
7220Sstevel@tonic-gate taken = ~n; break;
7230Sstevel@tonic-gate case 0xf: /* BVC */
7240Sstevel@tonic-gate taken = ~v; break;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate
7270Sstevel@tonic-gate if (taken & 1) {
7280Sstevel@tonic-gate pc = rp->r_npc;
7290Sstevel@tonic-gate npc = tp->ftt_dest;
7300Sstevel@tonic-gate } else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
7310Sstevel@tonic-gate /*
7320Sstevel@tonic-gate * Untaken annulled branches don't execute the
7330Sstevel@tonic-gate * instruction in the delay slot.
7340Sstevel@tonic-gate */
7350Sstevel@tonic-gate pc = rp->r_npc + 4;
7360Sstevel@tonic-gate npc = pc + 4;
7370Sstevel@tonic-gate } else {
7380Sstevel@tonic-gate pc = rp->r_npc;
7390Sstevel@tonic-gate npc = pc + 4;
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate break;
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate case FASTTRAP_T_FCC:
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate uint_t fcc;
7470Sstevel@tonic-gate uint_t taken;
7480Sstevel@tonic-gate uint64_t fsr;
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate dtrace_getfsr(&fsr);
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate if (tp->ftt_cc == 0) {
7530Sstevel@tonic-gate fcc = (fsr >> 10) & 0x3;
7540Sstevel@tonic-gate } else {
7550Sstevel@tonic-gate uint_t shift;
7560Sstevel@tonic-gate ASSERT(tp->ftt_cc <= 3);
7570Sstevel@tonic-gate shift = 30 + tp->ftt_cc * 2;
7580Sstevel@tonic-gate fcc = (fsr >> shift) & 0x3;
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate switch (tp->ftt_code) {
7620Sstevel@tonic-gate case 0x0: /* FBN */
7630Sstevel@tonic-gate taken = (1 << fcc) & (0|0|0|0); break;
7640Sstevel@tonic-gate case 0x1: /* FBNE */
7650Sstevel@tonic-gate taken = (1 << fcc) & (8|4|2|0); break;
7660Sstevel@tonic-gate case 0x2: /* FBLG */
7670Sstevel@tonic-gate taken = (1 << fcc) & (0|4|2|0); break;
7680Sstevel@tonic-gate case 0x3: /* FBUL */
7690Sstevel@tonic-gate taken = (1 << fcc) & (8|0|2|0); break;
7700Sstevel@tonic-gate case 0x4: /* FBL */
7710Sstevel@tonic-gate taken = (1 << fcc) & (0|0|2|0); break;
7720Sstevel@tonic-gate case 0x5: /* FBUG */
7730Sstevel@tonic-gate taken = (1 << fcc) & (8|4|0|0); break;
7740Sstevel@tonic-gate case 0x6: /* FBG */
7750Sstevel@tonic-gate taken = (1 << fcc) & (0|4|0|0); break;
7760Sstevel@tonic-gate case 0x7: /* FBU */
7770Sstevel@tonic-gate taken = (1 << fcc) & (8|0|0|0); break;
7780Sstevel@tonic-gate case 0x8: /* FBA */
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate * We handle the FBA case differently since the annul
7810Sstevel@tonic-gate * bit means something slightly different.
7820Sstevel@tonic-gate */
7830Sstevel@tonic-gate panic("fasttrap: mishandled a branch");
7840Sstevel@tonic-gate taken = (1 << fcc) & (8|4|2|1); break;
7850Sstevel@tonic-gate case 0x9: /* FBE */
7860Sstevel@tonic-gate taken = (1 << fcc) & (0|0|0|1); break;
7870Sstevel@tonic-gate case 0xa: /* FBUE */
7880Sstevel@tonic-gate taken = (1 << fcc) & (8|0|0|1); break;
7890Sstevel@tonic-gate case 0xb: /* FBGE */
7900Sstevel@tonic-gate taken = (1 << fcc) & (0|4|0|1); break;
7910Sstevel@tonic-gate case 0xc: /* FBUGE */
7920Sstevel@tonic-gate taken = (1 << fcc) & (8|4|0|1); break;
7930Sstevel@tonic-gate case 0xd: /* FBLE */
7940Sstevel@tonic-gate taken = (1 << fcc) & (0|0|2|1); break;
7950Sstevel@tonic-gate case 0xe: /* FBULE */
7960Sstevel@tonic-gate taken = (1 << fcc) & (8|0|2|1); break;
7970Sstevel@tonic-gate case 0xf: /* FBO */
7980Sstevel@tonic-gate taken = (1 << fcc) & (0|4|2|1); break;
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate if (taken) {
8020Sstevel@tonic-gate pc = rp->r_npc;
8030Sstevel@tonic-gate npc = tp->ftt_dest;
8040Sstevel@tonic-gate } else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate * Untaken annulled branches don't execute the
8070Sstevel@tonic-gate * instruction in the delay slot.
8080Sstevel@tonic-gate */
8090Sstevel@tonic-gate pc = rp->r_npc + 4;
8100Sstevel@tonic-gate npc = pc + 4;
8110Sstevel@tonic-gate } else {
8120Sstevel@tonic-gate pc = rp->r_npc;
8130Sstevel@tonic-gate npc = pc + 4;
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate break;
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate case FASTTRAP_T_REG:
8190Sstevel@tonic-gate {
8203944Sahl int64_t value;
8210Sstevel@tonic-gate uint_t taken;
8220Sstevel@tonic-gate uint_t reg = RS1(tp->ftt_instr);
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate * An ILP32 process shouldn't be using a branch predicated on
8260Sstevel@tonic-gate * an %i or an %l since it would violate the ABI. It's a
8270Sstevel@tonic-gate * violation of the ABI because we can't ensure deterministic
8280Sstevel@tonic-gate * behavior. We should have identified this case when we
8290Sstevel@tonic-gate * enabled the probe.
8300Sstevel@tonic-gate */
8310Sstevel@tonic-gate ASSERT(p->p_model == DATAMODEL_LP64 || reg < 16);
8320Sstevel@tonic-gate
8333944Sahl value = (int64_t)fasttrap_getreg(rp, reg);
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate switch (tp->ftt_code) {
8360Sstevel@tonic-gate case 0x1: /* BRZ */
8370Sstevel@tonic-gate taken = (value == 0); break;
8380Sstevel@tonic-gate case 0x2: /* BRLEZ */
8390Sstevel@tonic-gate taken = (value <= 0); break;
8400Sstevel@tonic-gate case 0x3: /* BRLZ */
8410Sstevel@tonic-gate taken = (value < 0); break;
8420Sstevel@tonic-gate case 0x5: /* BRNZ */
8430Sstevel@tonic-gate taken = (value != 0); break;
8440Sstevel@tonic-gate case 0x6: /* BRGZ */
8450Sstevel@tonic-gate taken = (value > 0); break;
8460Sstevel@tonic-gate case 0x7: /* BRGEZ */
8473944Sahl taken = (value >= 0); break;
8480Sstevel@tonic-gate default:
8490Sstevel@tonic-gate case 0x0:
8500Sstevel@tonic-gate case 0x4:
8510Sstevel@tonic-gate panic("fasttrap: mishandled a branch");
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate if (taken) {
8550Sstevel@tonic-gate pc = rp->r_npc;
8560Sstevel@tonic-gate npc = tp->ftt_dest;
8570Sstevel@tonic-gate } else if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
8580Sstevel@tonic-gate /*
8590Sstevel@tonic-gate * Untaken annulled branches don't execute the
8600Sstevel@tonic-gate * instruction in the delay slot.
8610Sstevel@tonic-gate */
8620Sstevel@tonic-gate pc = rp->r_npc + 4;
8630Sstevel@tonic-gate npc = pc + 4;
8640Sstevel@tonic-gate } else {
8650Sstevel@tonic-gate pc = rp->r_npc;
8660Sstevel@tonic-gate npc = pc + 4;
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate break;
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate case FASTTRAP_T_ALWAYS:
8720Sstevel@tonic-gate /*
8730Sstevel@tonic-gate * BAs, BA,As...
8740Sstevel@tonic-gate */
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate if (tp->ftt_flags & FASTTRAP_F_ANNUL) {
8770Sstevel@tonic-gate /*
8780Sstevel@tonic-gate * Annulled branch always instructions never execute
8790Sstevel@tonic-gate * the instruction in the delay slot.
8800Sstevel@tonic-gate */
8810Sstevel@tonic-gate pc = tp->ftt_dest;
8820Sstevel@tonic-gate npc = tp->ftt_dest + 4;
8830Sstevel@tonic-gate } else {
8840Sstevel@tonic-gate pc = rp->r_npc;
8850Sstevel@tonic-gate npc = tp->ftt_dest;
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate break;
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate case FASTTRAP_T_RDPC:
8900Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
8910Sstevel@tonic-gate pc = rp->r_npc;
8920Sstevel@tonic-gate npc = pc + 4;
8930Sstevel@tonic-gate break;
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate case FASTTRAP_T_CALL:
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate * It's a call _and_ link remember...
8980Sstevel@tonic-gate */
8990Sstevel@tonic-gate rp->r_o7 = rp->r_pc;
9000Sstevel@tonic-gate pc = rp->r_npc;
9010Sstevel@tonic-gate npc = tp->ftt_dest;
9020Sstevel@tonic-gate break;
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate case FASTTRAP_T_JMPL:
9050Sstevel@tonic-gate pc = rp->r_npc;
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate if (I(tp->ftt_instr)) {
9080Sstevel@tonic-gate uint_t rs1 = RS1(tp->ftt_instr);
9090Sstevel@tonic-gate int32_t imm;
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate imm = tp->ftt_instr << 19;
9120Sstevel@tonic-gate imm >>= 19;
9130Sstevel@tonic-gate npc = fasttrap_getreg(rp, rs1) + imm;
9140Sstevel@tonic-gate } else {
9150Sstevel@tonic-gate uint_t rs1 = RS1(tp->ftt_instr);
9160Sstevel@tonic-gate uint_t rs2 = RS2(tp->ftt_instr);
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate npc = fasttrap_getreg(rp, rs1) +
9190Sstevel@tonic-gate fasttrap_getreg(rp, rs2);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate * Do the link part of the jump-and-link instruction.
9240Sstevel@tonic-gate */
9250Sstevel@tonic-gate fasttrap_putreg(rp, RD(tp->ftt_instr), rp->r_pc);
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate break;
9280Sstevel@tonic-gate
9290Sstevel@tonic-gate case FASTTRAP_T_COMMON:
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate curthread->t_dtrace_scrpc = rp->r_g7;
9320Sstevel@tonic-gate curthread->t_dtrace_astpc = rp->r_g7 + FASTTRAP_OFF_FTRET;
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate /*
9350Sstevel@tonic-gate * Copy the instruction to a reserved location in the
9360Sstevel@tonic-gate * user-land thread structure, then set the PC to that
9370Sstevel@tonic-gate * location and leave the NPC alone. We take pains to ensure
9380Sstevel@tonic-gate * consistency in the instruction stream (See SPARC
9390Sstevel@tonic-gate * Architecture Manual Version 9, sections 8.4.7, A.20, and
9400Sstevel@tonic-gate * H.1.6; UltraSPARC I/II User's Manual, sections 3.1.1.1,
9410Sstevel@tonic-gate * and 13.6.4) by using the ASI ASI_BLK_COMMIT_S to copy the
9420Sstevel@tonic-gate * instruction into the user's address space without
9430Sstevel@tonic-gate * bypassing the I$. There's no AS_USER version of this ASI
9440Sstevel@tonic-gate * (as exist for other ASIs) so we use the lofault
9450Sstevel@tonic-gate * mechanism to catch faults.
9460Sstevel@tonic-gate */
9470Sstevel@tonic-gate if (dtrace_blksuword32(rp->r_g7, &tp->ftt_instr, 1) == -1) {
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate * If the copyout fails, then the process's state
9500Sstevel@tonic-gate * is not consistent (the effects of the traced
9510Sstevel@tonic-gate * instruction will never be seen). This process
9520Sstevel@tonic-gate * cannot be allowed to continue execution.
9530Sstevel@tonic-gate */
9540Sstevel@tonic-gate fasttrap_sigtrap(curproc, curthread, pc);
9550Sstevel@tonic-gate return (0);
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate
9580Sstevel@tonic-gate curthread->t_dtrace_pc = pc;
9590Sstevel@tonic-gate curthread->t_dtrace_npc = npc;
9600Sstevel@tonic-gate curthread->t_dtrace_on = 1;
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate pc = curthread->t_dtrace_scrpc;
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate if (tp->ftt_retids != NULL) {
9650Sstevel@tonic-gate curthread->t_dtrace_step = 1;
9660Sstevel@tonic-gate curthread->t_dtrace_ret = 1;
9670Sstevel@tonic-gate npc = curthread->t_dtrace_astpc;
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate break;
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate default:
9730Sstevel@tonic-gate panic("fasttrap: mishandled an instruction");
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate
9760Sstevel@tonic-gate /*
9770Sstevel@tonic-gate * This bit me in the ass a couple of times, so lets toss this
9780Sstevel@tonic-gate * in as a cursory sanity check.
9790Sstevel@tonic-gate */
9800Sstevel@tonic-gate ASSERT(pc != rp->r_g7 + 4);
9810Sstevel@tonic-gate ASSERT(pc != rp->r_g7 + 8);
9820Sstevel@tonic-gate
9831710Sahl done:
9840Sstevel@tonic-gate /*
9850Sstevel@tonic-gate * If there were no return probes when we first found the tracepoint,
9860Sstevel@tonic-gate * we should feel no obligation to honor any return probes that were
9870Sstevel@tonic-gate * subsequently enabled -- they'll just have to wait until the next
9880Sstevel@tonic-gate * time around.
9890Sstevel@tonic-gate */
9900Sstevel@tonic-gate if (tp->ftt_retids != NULL) {
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate * We need to wait until the results of the instruction are
9930Sstevel@tonic-gate * apparent before invoking any return probes. If this
9940Sstevel@tonic-gate * instruction was emulated we can just call
9950Sstevel@tonic-gate * fasttrap_return_common(); if it needs to be executed, we
9960Sstevel@tonic-gate * need to wait until we return to the kernel.
9970Sstevel@tonic-gate */
9980Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_COMMON) {
9990Sstevel@tonic-gate fasttrap_return_common(rp, orig_pc, pid, fake_restore);
10000Sstevel@tonic-gate } else {
10010Sstevel@tonic-gate ASSERT(curthread->t_dtrace_ret != 0);
10020Sstevel@tonic-gate ASSERT(curthread->t_dtrace_pc == orig_pc);
10030Sstevel@tonic-gate ASSERT(curthread->t_dtrace_scrpc == rp->r_g7);
10040Sstevel@tonic-gate ASSERT(npc == curthread->t_dtrace_astpc);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate ASSERT(pc != 0);
10090Sstevel@tonic-gate rp->r_pc = pc;
10100Sstevel@tonic-gate rp->r_npc = npc;
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate return (0);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate int
fasttrap_return_probe(struct regs * rp)10160Sstevel@tonic-gate fasttrap_return_probe(struct regs *rp)
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
10190Sstevel@tonic-gate pid_t pid;
10200Sstevel@tonic-gate uintptr_t pc = curthread->t_dtrace_pc;
10210Sstevel@tonic-gate uintptr_t npc = curthread->t_dtrace_npc;
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate curthread->t_dtrace_pc = 0;
10240Sstevel@tonic-gate curthread->t_dtrace_npc = 0;
10250Sstevel@tonic-gate curthread->t_dtrace_scrpc = 0;
10260Sstevel@tonic-gate curthread->t_dtrace_astpc = 0;
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate /*
10290Sstevel@tonic-gate * Treat a child created by a call to vfork(2) as if it were its
10300Sstevel@tonic-gate * parent. We know there's only one thread of control in such a
10310Sstevel@tonic-gate * process: this one.
10320Sstevel@tonic-gate */
10330Sstevel@tonic-gate while (p->p_flag & SVFORK) {
10340Sstevel@tonic-gate p = p->p_parent;
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate /*
10380Sstevel@tonic-gate * We set the %pc and %npc to their values when the traced
10390Sstevel@tonic-gate * instruction was initially executed so that it appears to
10400Sstevel@tonic-gate * dtrace_probe() that we're on the original instruction, and so that
10410Sstevel@tonic-gate * the user can't easily detect our complex web of lies.
10420Sstevel@tonic-gate * dtrace_return_probe() (our caller) will correctly set %pc and %npc
10430Sstevel@tonic-gate * after we return.
10440Sstevel@tonic-gate */
10450Sstevel@tonic-gate rp->r_pc = pc;
10460Sstevel@tonic-gate rp->r_npc = npc;
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate pid = p->p_pid;
10490Sstevel@tonic-gate fasttrap_return_common(rp, pc, pid, 0);
10500Sstevel@tonic-gate
10510Sstevel@tonic-gate return (0);
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate int
fasttrap_tracepoint_install(proc_t * p,fasttrap_tracepoint_t * tp)10550Sstevel@tonic-gate fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
10560Sstevel@tonic-gate {
10570Sstevel@tonic-gate fasttrap_instr_t instr = FASTTRAP_INSTR;
10580Sstevel@tonic-gate
10590Sstevel@tonic-gate if (uwrite(p, &instr, 4, tp->ftt_pc) != 0)
10600Sstevel@tonic-gate return (-1);
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate return (0);
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate int
fasttrap_tracepoint_remove(proc_t * p,fasttrap_tracepoint_t * tp)10660Sstevel@tonic-gate fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate fasttrap_instr_t instr;
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate * Distinguish between read or write failures and a changed
10720Sstevel@tonic-gate * instruction.
10730Sstevel@tonic-gate */
10740Sstevel@tonic-gate if (uread(p, &instr, 4, tp->ftt_pc) != 0)
10750Sstevel@tonic-gate return (0);
10760Sstevel@tonic-gate if (instr != FASTTRAP_INSTR && instr != BREAKPOINT_INSTR)
10770Sstevel@tonic-gate return (0);
10780Sstevel@tonic-gate if (uwrite(p, &tp->ftt_instr, 4, tp->ftt_pc) != 0)
10790Sstevel@tonic-gate return (-1);
10800Sstevel@tonic-gate
10810Sstevel@tonic-gate return (0);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate int
fasttrap_tracepoint_init(proc_t * p,fasttrap_tracepoint_t * tp,uintptr_t pc,fasttrap_probe_type_t type)10851710Sahl fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc,
10861710Sahl fasttrap_probe_type_t type)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate uint32_t instr;
10890Sstevel@tonic-gate int32_t disp;
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate /*
10920Sstevel@tonic-gate * Read the instruction at the given address out of the process's
10930Sstevel@tonic-gate * address space. We don't have to worry about a debugger
10940Sstevel@tonic-gate * changing this instruction before we overwrite it with our trap
10950Sstevel@tonic-gate * instruction since P_PR_LOCK is set.
10960Sstevel@tonic-gate */
10970Sstevel@tonic-gate if (uread(p, &instr, 4, pc) != 0)
10980Sstevel@tonic-gate return (-1);
10990Sstevel@tonic-gate
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate * Decode the instruction to fill in the probe flags. We can have
11020Sstevel@tonic-gate * the process execute most instructions on its own using a pc/npc
11030Sstevel@tonic-gate * trick, but pc-relative control transfer present a problem since
11040Sstevel@tonic-gate * we're relocating the instruction. We emulate these instructions
11050Sstevel@tonic-gate * in the kernel. We assume a default type and over-write that as
11060Sstevel@tonic-gate * needed.
11070Sstevel@tonic-gate *
11080Sstevel@tonic-gate * pc-relative instructions must be emulated for correctness;
11090Sstevel@tonic-gate * other instructions (which represent a large set of commonly traced
11100Sstevel@tonic-gate * instructions) are emulated or otherwise optimized for performance.
11110Sstevel@tonic-gate */
11120Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_COMMON;
11130Sstevel@tonic-gate if (OP(instr) == 1) {
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate * Call instructions.
11160Sstevel@tonic-gate */
11170Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CALL;
11180Sstevel@tonic-gate disp = DISP30(instr) << 2;
11190Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp;
11200Sstevel@tonic-gate
11210Sstevel@tonic-gate } else if (OP(instr) == 0) {
11220Sstevel@tonic-gate /*
11230Sstevel@tonic-gate * Branch instructions.
11240Sstevel@tonic-gate *
11250Sstevel@tonic-gate * Unconditional branches need careful attention when they're
11260Sstevel@tonic-gate * annulled: annulled unconditional branches never execute
11270Sstevel@tonic-gate * the instruction in the delay slot.
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate switch (OP2(instr)) {
11300Sstevel@tonic-gate case OP2_ILLTRAP:
11310Sstevel@tonic-gate case 0x7:
11320Sstevel@tonic-gate /*
11330Sstevel@tonic-gate * The compiler may place an illtrap after a call to
11340Sstevel@tonic-gate * a function that returns a structure. In the case of
11350Sstevel@tonic-gate * a returned structure, the compiler places an illtrap
11360Sstevel@tonic-gate * whose const22 field is the size of the returned
11370Sstevel@tonic-gate * structure immediately following the delay slot of
11380Sstevel@tonic-gate * the call. To stay out of the way, we refuse to
11390Sstevel@tonic-gate * place tracepoints on top of illtrap instructions.
11400Sstevel@tonic-gate *
11410Sstevel@tonic-gate * This is one of the dumbest architectural decisions
11420Sstevel@tonic-gate * I've ever had to work around.
11430Sstevel@tonic-gate *
11440Sstevel@tonic-gate * We also identify the only illegal op2 value (See
11450Sstevel@tonic-gate * SPARC Architecture Manual Version 9, E.2 table 31).
11460Sstevel@tonic-gate */
11470Sstevel@tonic-gate return (-1);
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate case OP2_BPcc:
11500Sstevel@tonic-gate if (COND(instr) == 8) {
11510Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS;
11520Sstevel@tonic-gate } else {
11530Sstevel@tonic-gate /*
11540Sstevel@tonic-gate * Check for an illegal instruction.
11550Sstevel@tonic-gate */
11560Sstevel@tonic-gate if (CC(instr) & 1)
11570Sstevel@tonic-gate return (-1);
11580Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CCR;
11590Sstevel@tonic-gate tp->ftt_cc = CC(instr);
11600Sstevel@tonic-gate tp->ftt_code = COND(instr);
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate if (A(instr) != 0)
11640Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL;
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate disp = DISP19(instr);
11670Sstevel@tonic-gate disp <<= 13;
11680Sstevel@tonic-gate disp >>= 11;
11690Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp;
11700Sstevel@tonic-gate break;
11710Sstevel@tonic-gate
11720Sstevel@tonic-gate case OP2_Bicc:
11730Sstevel@tonic-gate if (COND(instr) == 8) {
11740Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS;
11750Sstevel@tonic-gate } else {
11760Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_CCR;
11770Sstevel@tonic-gate tp->ftt_cc = 0;
11780Sstevel@tonic-gate tp->ftt_code = COND(instr);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate if (A(instr) != 0)
11820Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL;
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate disp = DISP22(instr);
11850Sstevel@tonic-gate disp <<= 10;
11860Sstevel@tonic-gate disp >>= 8;
11870Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp;
11880Sstevel@tonic-gate break;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate case OP2_BPr:
11910Sstevel@tonic-gate /*
11920Sstevel@tonic-gate * Check for an illegal instruction.
11930Sstevel@tonic-gate */
11940Sstevel@tonic-gate if ((RCOND(instr) & 3) == 0)
11950Sstevel@tonic-gate return (-1);
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate * It's a violation of the v8plus ABI to use a
11990Sstevel@tonic-gate * register-predicated branch in a 32-bit app if
12000Sstevel@tonic-gate * the register used is an %l or an %i (%gs and %os
12010Sstevel@tonic-gate * are legit because they're not saved to the stack
12020Sstevel@tonic-gate * in 32-bit words when we take a trap).
12030Sstevel@tonic-gate */
12040Sstevel@tonic-gate if (p->p_model == DATAMODEL_ILP32 && RS1(instr) >= 16)
12050Sstevel@tonic-gate return (-1);
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_REG;
12080Sstevel@tonic-gate if (A(instr) != 0)
12090Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL;
12100Sstevel@tonic-gate disp = DISP16(instr);
12110Sstevel@tonic-gate disp <<= 16;
12120Sstevel@tonic-gate disp >>= 14;
12130Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp;
12140Sstevel@tonic-gate tp->ftt_code = RCOND(instr);
12150Sstevel@tonic-gate break;
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate case OP2_SETHI:
12180Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_SETHI;
12190Sstevel@tonic-gate break;
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate case OP2_FBPfcc:
12220Sstevel@tonic-gate if (COND(instr) == 8) {
12230Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS;
12240Sstevel@tonic-gate } else {
12250Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_FCC;
12260Sstevel@tonic-gate tp->ftt_cc = CC(instr);
12270Sstevel@tonic-gate tp->ftt_code = COND(instr);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate if (A(instr) != 0)
12310Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL;
12320Sstevel@tonic-gate
12330Sstevel@tonic-gate disp = DISP19(instr);
12340Sstevel@tonic-gate disp <<= 13;
12350Sstevel@tonic-gate disp >>= 11;
12360Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp;
12370Sstevel@tonic-gate break;
12380Sstevel@tonic-gate
12390Sstevel@tonic-gate case OP2_FBfcc:
12400Sstevel@tonic-gate if (COND(instr) == 8) {
12410Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_ALWAYS;
12420Sstevel@tonic-gate } else {
12430Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_FCC;
12440Sstevel@tonic-gate tp->ftt_cc = 0;
12450Sstevel@tonic-gate tp->ftt_code = COND(instr);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate
12480Sstevel@tonic-gate if (A(instr) != 0)
12490Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_ANNUL;
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate disp = DISP22(instr);
12520Sstevel@tonic-gate disp <<= 10;
12530Sstevel@tonic-gate disp >>= 8;
12540Sstevel@tonic-gate tp->ftt_dest = pc + (intptr_t)disp;
12550Sstevel@tonic-gate break;
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate } else if (OP(instr) == 2) {
12590Sstevel@tonic-gate switch (OP3(instr)) {
12600Sstevel@tonic-gate case OP3_RETURN:
12610Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RETURN;
12620Sstevel@tonic-gate break;
12630Sstevel@tonic-gate
12640Sstevel@tonic-gate case OP3_JMPL:
12650Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_JMPL;
12660Sstevel@tonic-gate break;
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate case OP3_RD:
12690Sstevel@tonic-gate if (RS1(instr) == 5)
12700Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RDPC;
12710Sstevel@tonic-gate break;
12720Sstevel@tonic-gate
12730Sstevel@tonic-gate case OP3_SAVE:
12740Sstevel@tonic-gate /*
12750Sstevel@tonic-gate * We optimize for save instructions at function
12760Sstevel@tonic-gate * entry; see the comment in fasttrap_pid_probe()
12770Sstevel@tonic-gate * (near FASTTRAP_T_SAVE) for details.
12780Sstevel@tonic-gate */
12790Sstevel@tonic-gate if (fasttrap_optimize_save != 0 &&
12801710Sahl type == DTFTP_ENTRY &&
12810Sstevel@tonic-gate I(instr) == 1 && RD(instr) == R_SP)
12820Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_SAVE;
12830Sstevel@tonic-gate break;
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate case OP3_RESTORE:
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate * We optimize restore instructions at function
12880Sstevel@tonic-gate * return; see the comment in fasttrap_pid_probe()
12890Sstevel@tonic-gate * (near FASTTRAP_T_RESTORE) for details.
12900Sstevel@tonic-gate *
12910Sstevel@tonic-gate * rd must be an %o or %g register.
12920Sstevel@tonic-gate */
12930Sstevel@tonic-gate if ((RD(instr) & 0x10) == 0)
12940Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_RESTORE;
12950Sstevel@tonic-gate break;
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate case OP3_OR:
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate * A large proportion of instructions in the delay
13000Sstevel@tonic-gate * slot of retl instructions are or's so we emulate
13010Sstevel@tonic-gate * these downstairs as an optimization.
13020Sstevel@tonic-gate */
13030Sstevel@tonic-gate tp->ftt_type = FASTTRAP_T_OR;
13040Sstevel@tonic-gate break;
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate case OP3_TCC:
13070Sstevel@tonic-gate /*
13080Sstevel@tonic-gate * Breakpoint instructions are effectively position-
13090Sstevel@tonic-gate * dependent since the debugger uses the %pc value
13100Sstevel@tonic-gate * to lookup which breakpoint was executed. As a
13110Sstevel@tonic-gate * result, we can't actually instrument breakpoints.
13120Sstevel@tonic-gate */
13130Sstevel@tonic-gate if (SW_TRAP(instr) == ST_BREAKPOINT)
13140Sstevel@tonic-gate return (-1);
13150Sstevel@tonic-gate break;
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate case 0x19:
13180Sstevel@tonic-gate case 0x1d:
13190Sstevel@tonic-gate case 0x29:
13200Sstevel@tonic-gate case 0x33:
13210Sstevel@tonic-gate case 0x3f:
13220Sstevel@tonic-gate /*
13230Sstevel@tonic-gate * Identify illegal instructions (See SPARC
13240Sstevel@tonic-gate * Architecture Manual Version 9, E.2 table 32).
13250Sstevel@tonic-gate */
13260Sstevel@tonic-gate return (-1);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate } else if (OP(instr) == 3) {
13290Sstevel@tonic-gate uint32_t op3 = OP3(instr);
13300Sstevel@tonic-gate
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate * Identify illegal instructions (See SPARC Architecture
13330Sstevel@tonic-gate * Manual Version 9, E.2 table 33).
13340Sstevel@tonic-gate */
13350Sstevel@tonic-gate if ((op3 & 0x28) == 0x28) {
13360Sstevel@tonic-gate if (op3 != OP3_PREFETCH && op3 != OP3_CASA &&
13370Sstevel@tonic-gate op3 != OP3_PREFETCHA && op3 != OP3_CASXA)
13380Sstevel@tonic-gate return (-1);
13390Sstevel@tonic-gate } else {
13400Sstevel@tonic-gate if ((op3 & 0x0f) == 0x0c || (op3 & 0x3b) == 0x31)
13410Sstevel@tonic-gate return (-1);
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate tp->ftt_instr = instr;
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate /*
13480Sstevel@tonic-gate * We don't know how this tracepoint is going to be used, but in case
13490Sstevel@tonic-gate * it's used as part of a function return probe, we need to indicate
13500Sstevel@tonic-gate * whether it's always a return site or only potentially a return
13510Sstevel@tonic-gate * site. If it's part of a return probe, it's always going to be a
13520Sstevel@tonic-gate * return from that function if it's a restore instruction or if
13530Sstevel@tonic-gate * the previous instruction was a return. If we could reliably
13540Sstevel@tonic-gate * distinguish jump tables from return sites, this wouldn't be
13550Sstevel@tonic-gate * necessary.
13560Sstevel@tonic-gate */
13570Sstevel@tonic-gate if (tp->ftt_type != FASTTRAP_T_RESTORE &&
13580Sstevel@tonic-gate (uread(p, &instr, 4, pc - sizeof (instr)) != 0 ||
13590Sstevel@tonic-gate !(OP(instr) == 2 && OP3(instr) == OP3_RETURN)))
13600Sstevel@tonic-gate tp->ftt_flags |= FASTTRAP_F_RETMAYBE;
13610Sstevel@tonic-gate
13620Sstevel@tonic-gate return (0);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate /*ARGSUSED*/
13660Sstevel@tonic-gate uint64_t
fasttrap_pid_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)13672179Sahl fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
13682179Sahl int aframes)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate
13730Sstevel@tonic-gate /*ARGSUSED*/
13740Sstevel@tonic-gate uint64_t
fasttrap_usdt_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)13750Sstevel@tonic-gate fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
13760Sstevel@tonic-gate int aframes)
13770Sstevel@tonic-gate {
13780Sstevel@tonic-gate return (fasttrap_anarg(ttolwp(curthread)->lwp_regs, argno));
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate static uint64_t fasttrap_getreg_fast_cnt;
13820Sstevel@tonic-gate static uint64_t fasttrap_getreg_mpcb_cnt;
13830Sstevel@tonic-gate static uint64_t fasttrap_getreg_slow_cnt;
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate static ulong_t
fasttrap_getreg(struct regs * rp,uint_t reg)13860Sstevel@tonic-gate fasttrap_getreg(struct regs *rp, uint_t reg)
13870Sstevel@tonic-gate {
13880Sstevel@tonic-gate ulong_t value;
13890Sstevel@tonic-gate dtrace_icookie_t cookie;
13900Sstevel@tonic-gate struct machpcb *mpcb;
13910Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t);
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate /*
13940Sstevel@tonic-gate * We have the %os and %gs in our struct regs, but if we need to
13950Sstevel@tonic-gate * snag a %l or %i we need to go scrounging around in the process's
13960Sstevel@tonic-gate * address space.
13970Sstevel@tonic-gate */
13980Sstevel@tonic-gate if (reg == 0)
13990Sstevel@tonic-gate return (0);
14000Sstevel@tonic-gate
14010Sstevel@tonic-gate if (reg < 16)
14020Sstevel@tonic-gate return ((&rp->r_g1)[reg - 1]);
14030Sstevel@tonic-gate
14040Sstevel@tonic-gate /*
14050Sstevel@tonic-gate * Before we look at the user's stack, we'll check the register
14060Sstevel@tonic-gate * windows to see if the information we want is in there.
14070Sstevel@tonic-gate */
14080Sstevel@tonic-gate cookie = dtrace_interrupt_disable();
14090Sstevel@tonic-gate if (dtrace_getotherwin() > 0) {
14100Sstevel@tonic-gate value = dtrace_getreg_win(reg, 1);
14110Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
14120Sstevel@tonic-gate
14130Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_fast_cnt, 1);
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate return (value);
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
14180Sstevel@tonic-gate
14190Sstevel@tonic-gate /*
14200Sstevel@tonic-gate * First check the machpcb structure to see if we've already read
14210Sstevel@tonic-gate * in the register window we're looking for; if we haven't, (and
14220Sstevel@tonic-gate * we probably haven't) try to copy in the value of the register.
14230Sstevel@tonic-gate */
14243944Sahl /* LINTED - alignment */
14250Sstevel@tonic-gate mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
14260Sstevel@tonic-gate
14270Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
14280Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
14290Sstevel@tonic-gate
14300Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) {
14310Sstevel@tonic-gate struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
14320Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt;
14330Sstevel@tonic-gate do {
14340Sstevel@tonic-gate i--;
14350Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
14360Sstevel@tonic-gate continue;
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1);
14390Sstevel@tonic-gate return (rwin[i].rw_local[reg - 16]);
14400Sstevel@tonic-gate } while (i > 0);
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate if (fasttrap_fulword(&fr->fr_local[reg - 16], &value) != 0)
14440Sstevel@tonic-gate goto err;
14450Sstevel@tonic-gate } else {
14461048Sraf struct frame32 *fr =
14471048Sraf (struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp;
14480Sstevel@tonic-gate uint32_t *v32 = (uint32_t *)&value;
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) {
14510Sstevel@tonic-gate struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
14520Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt;
14530Sstevel@tonic-gate do {
14540Sstevel@tonic-gate i--;
14550Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
14560Sstevel@tonic-gate continue;
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_mpcb_cnt, 1);
14590Sstevel@tonic-gate return (rwin[i].rw_local[reg - 16]);
14600Sstevel@tonic-gate } while (i > 0);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate
14630Sstevel@tonic-gate if (fasttrap_fuword32(&fr->fr_local[reg - 16], &v32[1]) != 0)
14640Sstevel@tonic-gate goto err;
14650Sstevel@tonic-gate
14660Sstevel@tonic-gate v32[0] = 0;
14670Sstevel@tonic-gate }
14680Sstevel@tonic-gate
14690Sstevel@tonic-gate atomic_add_64(&fasttrap_getreg_slow_cnt, 1);
14700Sstevel@tonic-gate return (value);
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate err:
14730Sstevel@tonic-gate /*
14740Sstevel@tonic-gate * If the copy in failed, the process will be in a irrecoverable
14750Sstevel@tonic-gate * state, and we have no choice but to kill it.
14760Sstevel@tonic-gate */
14770Sstevel@tonic-gate psignal(ttoproc(curthread), SIGILL);
14780Sstevel@tonic-gate return (0);
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate static uint64_t fasttrap_putreg_fast_cnt;
14820Sstevel@tonic-gate static uint64_t fasttrap_putreg_mpcb_cnt;
14830Sstevel@tonic-gate static uint64_t fasttrap_putreg_slow_cnt;
14840Sstevel@tonic-gate
14850Sstevel@tonic-gate static void
fasttrap_putreg(struct regs * rp,uint_t reg,ulong_t value)14860Sstevel@tonic-gate fasttrap_putreg(struct regs *rp, uint_t reg, ulong_t value)
14870Sstevel@tonic-gate {
14880Sstevel@tonic-gate dtrace_icookie_t cookie;
14890Sstevel@tonic-gate struct machpcb *mpcb;
14900Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t);
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate if (reg == 0)
14930Sstevel@tonic-gate return;
14940Sstevel@tonic-gate
14950Sstevel@tonic-gate if (reg < 16) {
14960Sstevel@tonic-gate (&rp->r_g1)[reg - 1] = value;
14970Sstevel@tonic-gate return;
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate
15000Sstevel@tonic-gate /*
15010Sstevel@tonic-gate * If the user process is still using some register windows, we
15020Sstevel@tonic-gate * can just place the value in the correct window.
15030Sstevel@tonic-gate */
15040Sstevel@tonic-gate cookie = dtrace_interrupt_disable();
15050Sstevel@tonic-gate if (dtrace_getotherwin() > 0) {
15060Sstevel@tonic-gate dtrace_putreg_win(reg, value);
15070Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
15080Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_fast_cnt, 1);
15090Sstevel@tonic-gate return;
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate dtrace_interrupt_enable(cookie);
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate /*
15140Sstevel@tonic-gate * First see if there's a copy of the register window in the
15150Sstevel@tonic-gate * machpcb structure that we can modify; if there isn't try to
15160Sstevel@tonic-gate * copy out the value. If that fails, we try to create a new
15170Sstevel@tonic-gate * register window in the machpcb structure. While this isn't
15180Sstevel@tonic-gate * _precisely_ the intended use of the machpcb structure, it
15190Sstevel@tonic-gate * can't cause any problems since we know at this point in the
15200Sstevel@tonic-gate * code that all of the user's data have been flushed out of the
15210Sstevel@tonic-gate * register file (since %otherwin is 0).
15220Sstevel@tonic-gate */
15233944Sahl /* LINTED - alignment */
15240Sstevel@tonic-gate mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
15250Sstevel@tonic-gate
15260Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
15270Sstevel@tonic-gate struct frame *fr = (struct frame *)(rp->r_sp + STACK_BIAS);
15283944Sahl /* LINTED - alignment */
15290Sstevel@tonic-gate struct rwindow *rwin = (struct rwindow *)mpcb->mpcb_wbuf;
15300Sstevel@tonic-gate
15310Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) {
15320Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt;
15330Sstevel@tonic-gate do {
15340Sstevel@tonic-gate i--;
15350Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
15360Sstevel@tonic-gate continue;
15370Sstevel@tonic-gate
15380Sstevel@tonic-gate rwin[i].rw_local[reg - 16] = value;
15390Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15400Sstevel@tonic-gate return;
15410Sstevel@tonic-gate } while (i > 0);
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate
15440Sstevel@tonic-gate if (fasttrap_sulword(&fr->fr_local[reg - 16], value) != 0) {
15450Sstevel@tonic-gate if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
15460Sstevel@tonic-gate &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
15470Sstevel@tonic-gate goto err;
15480Sstevel@tonic-gate
15490Sstevel@tonic-gate rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = value;
15500Sstevel@tonic-gate mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
15510Sstevel@tonic-gate mpcb->mpcb_wbcnt++;
15520Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15530Sstevel@tonic-gate return;
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate } else {
15561048Sraf struct frame32 *fr =
15571048Sraf (struct frame32 *)(uintptr_t)(caddr32_t)rp->r_sp;
15583944Sahl /* LINTED - alignment */
15590Sstevel@tonic-gate struct rwindow32 *rwin = (struct rwindow32 *)mpcb->mpcb_wbuf;
15600Sstevel@tonic-gate uint32_t v32 = (uint32_t)value;
15610Sstevel@tonic-gate
15620Sstevel@tonic-gate if (mpcb->mpcb_wbcnt > 0) {
15630Sstevel@tonic-gate int i = mpcb->mpcb_wbcnt;
15640Sstevel@tonic-gate do {
15650Sstevel@tonic-gate i--;
15660Sstevel@tonic-gate if ((long)mpcb->mpcb_spbuf[i] != rp->r_sp)
15670Sstevel@tonic-gate continue;
15680Sstevel@tonic-gate
15690Sstevel@tonic-gate rwin[i].rw_local[reg - 16] = v32;
15700Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15710Sstevel@tonic-gate return;
15720Sstevel@tonic-gate } while (i > 0);
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate
15750Sstevel@tonic-gate if (fasttrap_suword32(&fr->fr_local[reg - 16], v32) != 0) {
15760Sstevel@tonic-gate if (mpcb->mpcb_wbcnt >= MAXWIN || copyin(fr,
15770Sstevel@tonic-gate &rwin[mpcb->mpcb_wbcnt], sizeof (*rwin)) != 0)
15780Sstevel@tonic-gate goto err;
15790Sstevel@tonic-gate
15800Sstevel@tonic-gate rwin[mpcb->mpcb_wbcnt].rw_local[reg - 16] = v32;
15810Sstevel@tonic-gate mpcb->mpcb_spbuf[mpcb->mpcb_wbcnt] = (caddr_t)rp->r_sp;
15820Sstevel@tonic-gate mpcb->mpcb_wbcnt++;
15830Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_mpcb_cnt, 1);
15840Sstevel@tonic-gate return;
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate
15880Sstevel@tonic-gate atomic_add_64(&fasttrap_putreg_slow_cnt, 1);
15890Sstevel@tonic-gate return;
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate err:
15920Sstevel@tonic-gate /*
15930Sstevel@tonic-gate * If we couldn't record this register's value, the process is in an
15940Sstevel@tonic-gate * irrecoverable state and we have no choice but to euthanize it.
15950Sstevel@tonic-gate */
15960Sstevel@tonic-gate psignal(ttoproc(curthread), SIGILL);
15970Sstevel@tonic-gate }
1598