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