xref: /freebsd/sys/cddl/dev/dtrace/x86/instr_size.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1e86e17afSMark Johnston /*
2e86e17afSMark Johnston  * CDDL HEADER START
3e86e17afSMark Johnston  *
4e86e17afSMark Johnston  * The contents of this file are subject to the terms of the
5e86e17afSMark Johnston  * Common Development and Distribution License, Version 1.0 only
6e86e17afSMark Johnston  * (the "License").  You may not use this file except in compliance
7e86e17afSMark Johnston  * with the License.
8e86e17afSMark Johnston  *
9e86e17afSMark Johnston  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10e86e17afSMark Johnston  * or http://www.opensolaris.org/os/licensing.
11e86e17afSMark Johnston  * See the License for the specific language governing permissions
12e86e17afSMark Johnston  * and limitations under the License.
13e86e17afSMark Johnston  *
14e86e17afSMark Johnston  * When distributing Covered Code, include this CDDL HEADER in each
15e86e17afSMark Johnston  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16e86e17afSMark Johnston  * If applicable, add the following below this CDDL HEADER, with the
17e86e17afSMark Johnston  * fields enclosed by brackets "[]" replaced with your own identifying
18e86e17afSMark Johnston  * information: Portions Copyright [yyyy] [name of copyright owner]
19e86e17afSMark Johnston  *
20e86e17afSMark Johnston  * CDDL HEADER END
21e86e17afSMark Johnston  */
22e86e17afSMark Johnston /*
23e86e17afSMark Johnston  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24e86e17afSMark Johnston  * Use is subject to license terms.
25e86e17afSMark Johnston  */
26e86e17afSMark Johnston 
27e86e17afSMark Johnston /*	Copyright (c) 1988 AT&T	*/
28e86e17afSMark Johnston /*	  All Rights Reserved	*/
29e86e17afSMark Johnston 
30e86e17afSMark Johnston #include <sys/types.h>
31e86e17afSMark Johnston #include <sys/param.h>
32e86e17afSMark Johnston #include <sys/proc.h>
33e86e17afSMark Johnston #ifdef illumos
34e86e17afSMark Johnston #include <sys/cmn_err.h>
35e86e17afSMark Johnston #include <sys/archsystm.h>
36e86e17afSMark Johnston #include <sys/copyops.h>
37e86e17afSMark Johnston #include <vm/seg_enum.h>
38e86e17afSMark Johnston #include <sys/privregs.h>
39e86e17afSMark Johnston #else
40e86e17afSMark Johnston #include <sys/cred.h>
41e86e17afSMark Johnston #include <cddl/dev/dtrace/dtrace_cddl.h>
42e86e17afSMark Johnston 
43e86e17afSMark Johnston typedef	u_int			model_t;
44e86e17afSMark Johnston #define	DATAMODEL_NATIVE	0
45*cef25655SChristos Margiolis int dtrace_dis_get_byte(void *);
461a149d65SChristos Margiolis int dtrace_instr_size(uint8_t *);
471a149d65SChristos Margiolis int dtrace_instr_size_isa(uint8_t *, model_t, int *);
48e86e17afSMark Johnston #endif
49e86e17afSMark Johnston 
50e86e17afSMark Johnston #include <dis_tables.h>
51e86e17afSMark Johnston 
52e86e17afSMark Johnston /*
53e86e17afSMark Johnston  * This subsystem (with the minor exception of the instr_size() function) is
54e86e17afSMark Johnston  * is called from DTrace probe context.  This imposes several requirements on
55e86e17afSMark Johnston  * the implementation:
56e86e17afSMark Johnston  *
57e86e17afSMark Johnston  * 1. External subsystems and functions may not be referenced.  The one current
58e86e17afSMark Johnston  *    exception is for cmn_err, but only to signal the detection of table
59e86e17afSMark Johnston  *    errors.  Assuming the tables are correct, no combination of input is to
60e86e17afSMark Johnston  *    trigger a cmn_err call.
61e86e17afSMark Johnston  *
62e86e17afSMark Johnston  * 2. These functions can't be allowed to be traced.  To prevent this,
63e86e17afSMark Johnston  *    all functions in the probe path (everything except instr_size()) must
64e86e17afSMark Johnston  *    have names that begin with "dtrace_".
65e86e17afSMark Johnston  */
66e86e17afSMark Johnston 
67e86e17afSMark Johnston typedef enum dis_isize {
68e86e17afSMark Johnston 	DIS_ISIZE_INSTR,
69e86e17afSMark Johnston 	DIS_ISIZE_OPERAND
70e86e17afSMark Johnston } dis_isize_t;
71e86e17afSMark Johnston 
72e86e17afSMark Johnston 
73e86e17afSMark Johnston /*
74e86e17afSMark Johnston  * get a byte from instruction stream
75e86e17afSMark Johnston  */
76*cef25655SChristos Margiolis int
dtrace_dis_get_byte(void * p)77e86e17afSMark Johnston dtrace_dis_get_byte(void *p)
78e86e17afSMark Johnston {
79e86e17afSMark Johnston 	int ret;
801a149d65SChristos Margiolis 	uint8_t **instr = p;
81e86e17afSMark Johnston 
82e86e17afSMark Johnston 	ret = **instr;
83e86e17afSMark Johnston 	*instr += 1;
84e86e17afSMark Johnston 
85e86e17afSMark Johnston 	return (ret);
86e86e17afSMark Johnston }
87e86e17afSMark Johnston 
88e86e17afSMark Johnston /*
89e86e17afSMark Johnston  * Returns either the size of a given instruction, in bytes, or the size of that
90e86e17afSMark Johnston  * instruction's memory access (if any), depending on the value of `which'.
91e86e17afSMark Johnston  * If a programming error in the tables is detected, the system will panic to
92e86e17afSMark Johnston  * ease diagnosis.  Invalid instructions will not be flagged.  They will appear
93e86e17afSMark Johnston  * to have an instruction size between 1 and the actual size, and will be
94e86e17afSMark Johnston  * reported as having no memory impact.
95e86e17afSMark Johnston  */
96e86e17afSMark Johnston /* ARGSUSED2 */
97e86e17afSMark Johnston static int
dtrace_dis_isize(uint8_t * instr,dis_isize_t which,model_t model,int * rmindex)981a149d65SChristos Margiolis dtrace_dis_isize(uint8_t *instr, dis_isize_t which, model_t model, int *rmindex)
99e86e17afSMark Johnston {
100e86e17afSMark Johnston 	int sz;
101e86e17afSMark Johnston 	dis86_t	x;
102e86e17afSMark Johnston 	uint_t mode = SIZE32;
103e86e17afSMark Johnston 
104e86e17afSMark Johnston 	mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
105e86e17afSMark Johnston 
106e86e17afSMark Johnston 	x.d86_data = (void **)&instr;
107e86e17afSMark Johnston 	x.d86_get_byte = dtrace_dis_get_byte;
108e86e17afSMark Johnston 	x.d86_check_func = NULL;
109e86e17afSMark Johnston 
110e86e17afSMark Johnston 	if (dtrace_disx86(&x, mode) != 0)
111e86e17afSMark Johnston 		return (-1);
112e86e17afSMark Johnston 
113e86e17afSMark Johnston 	if (which == DIS_ISIZE_INSTR)
114e86e17afSMark Johnston 		sz = x.d86_len;		/* length of the instruction */
115e86e17afSMark Johnston 	else
116e86e17afSMark Johnston 		sz = x.d86_memsize;	/* length of memory operand */
117e86e17afSMark Johnston 
118e86e17afSMark Johnston 	if (rmindex != NULL)
119e86e17afSMark Johnston 		*rmindex = x.d86_rmindex;
120e86e17afSMark Johnston 	return (sz);
121e86e17afSMark Johnston }
122e86e17afSMark Johnston 
123e86e17afSMark Johnston int
dtrace_instr_size_isa(uint8_t * instr,model_t model,int * rmindex)1241a149d65SChristos Margiolis dtrace_instr_size_isa(uint8_t *instr, model_t model, int *rmindex)
125e86e17afSMark Johnston {
126e86e17afSMark Johnston 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
127e86e17afSMark Johnston }
128e86e17afSMark Johnston 
129e86e17afSMark Johnston int
dtrace_instr_size(uint8_t * instr)1301a149d65SChristos Margiolis dtrace_instr_size(uint8_t *instr)
131e86e17afSMark Johnston {
132e86e17afSMark Johnston 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
133e86e17afSMark Johnston 	    NULL));
134e86e17afSMark Johnston }
135