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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*151Sahl * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/cmn_err.h>
360Sstevel@tonic-gate #include <sys/archsystm.h>
370Sstevel@tonic-gate #include <sys/copyops.h>
380Sstevel@tonic-gate #include <vm/seg_enum.h>
390Sstevel@tonic-gate #include <sys/privregs.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <dis_tables.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * This subsystem (with the minor exception of the instr_size() function) is
450Sstevel@tonic-gate * is called from DTrace probe context. This imposes several requirements on
460Sstevel@tonic-gate * the implementation:
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * 1. External subsystems and functions may not be referenced. The one current
490Sstevel@tonic-gate * exception is for cmn_err, but only to signal the detection of table
500Sstevel@tonic-gate * errors. Assuming the tables are correct, no combination of input is to
510Sstevel@tonic-gate * trigger a cmn_err call.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * 2. These functions can't be allowed to be traced. To prevent this,
540Sstevel@tonic-gate * all functions in the probe path (everything except instr_size()) must
550Sstevel@tonic-gate * have names that begin with "dtrace_".
560Sstevel@tonic-gate */
570Sstevel@tonic-gate
580Sstevel@tonic-gate typedef enum dis_isize {
590Sstevel@tonic-gate DIS_ISIZE_INSTR,
600Sstevel@tonic-gate DIS_ISIZE_OPERAND
610Sstevel@tonic-gate } dis_isize_t;
620Sstevel@tonic-gate
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * get a byte from instruction stream
660Sstevel@tonic-gate */
670Sstevel@tonic-gate static int
dtrace_dis_get_byte(void * p)680Sstevel@tonic-gate dtrace_dis_get_byte(void *p)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate int ret;
710Sstevel@tonic-gate uchar_t **instr = p;
720Sstevel@tonic-gate
730Sstevel@tonic-gate ret = **instr;
740Sstevel@tonic-gate *instr += 1;
750Sstevel@tonic-gate
760Sstevel@tonic-gate return (ret);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate * Returns either the size of a given instruction, in bytes, or the size of that
810Sstevel@tonic-gate * instruction's memory access (if any), depending on the value of `which'.
820Sstevel@tonic-gate * If a programming error in the tables is detected, the system will panic to
830Sstevel@tonic-gate * ease diagnosis. Invalid instructions will not be flagged. They will appear
840Sstevel@tonic-gate * to have an instruction size between 1 and the actual size, and will be
850Sstevel@tonic-gate * reported as having no memory impact.
860Sstevel@tonic-gate */
870Sstevel@tonic-gate /* ARGSUSED2 */
880Sstevel@tonic-gate static int
dtrace_dis_isize(uchar_t * instr,dis_isize_t which,model_t model,int * rmindex)890Sstevel@tonic-gate dtrace_dis_isize(uchar_t *instr, dis_isize_t which, model_t model, int *rmindex)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate int sz;
920Sstevel@tonic-gate dis86_t x;
930Sstevel@tonic-gate uint_t mode = SIZE32;
940Sstevel@tonic-gate
950Sstevel@tonic-gate mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
960Sstevel@tonic-gate
970Sstevel@tonic-gate x.d86_data = (void **)&instr;
980Sstevel@tonic-gate x.d86_get_byte = dtrace_dis_get_byte;
990Sstevel@tonic-gate x.d86_check_func = NULL;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (dtrace_disx86(&x, mode) != 0)
1020Sstevel@tonic-gate return (-1);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if (which == DIS_ISIZE_INSTR)
1050Sstevel@tonic-gate sz = x.d86_len; /* length of the instruction */
1060Sstevel@tonic-gate else
1070Sstevel@tonic-gate sz = x.d86_memsize; /* length of memory operand */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (rmindex != NULL)
1100Sstevel@tonic-gate *rmindex = x.d86_rmindex;
1110Sstevel@tonic-gate return (sz);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate int
dtrace_instr_size_isa(uchar_t * instr,model_t model,int * rmindex)1150Sstevel@tonic-gate dtrace_instr_size_isa(uchar_t *instr, model_t model, int *rmindex)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate int
dtrace_instr_size(uchar_t * instr)1210Sstevel@tonic-gate dtrace_instr_size(uchar_t *instr)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
1240Sstevel@tonic-gate NULL));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /*ARGSUSED*/
1280Sstevel@tonic-gate int
instr_size(struct regs * rp,caddr_t * addrp,enum seg_rw rw)1290Sstevel@tonic-gate instr_size(struct regs *rp, caddr_t *addrp, enum seg_rw rw)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate uchar_t instr[16]; /* maximum size instruction */
1320Sstevel@tonic-gate caddr_t pc = (caddr_t)rp->r_pc;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate (void) copyin_nowatch(pc, (caddr_t)instr, sizeof (instr));
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate return (dtrace_dis_isize(instr,
1370Sstevel@tonic-gate rw == S_EXEC ? DIS_ISIZE_INSTR : DIS_ISIZE_OPERAND,
138*151Sahl curproc->p_model, NULL));
1390Sstevel@tonic-gate }
140