xref: /freebsd-13-stable/sys/cddl/dev/dtrace/x86/instr_size.c (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 
31 #ifdef illumos
32 #pragma ident	"@(#)instr_size.c	1.14	05/07/08 SMI"
33 #endif
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #ifdef illumos
39 #include <sys/cmn_err.h>
40 #include <sys/archsystm.h>
41 #include <sys/copyops.h>
42 #include <vm/seg_enum.h>
43 #include <sys/privregs.h>
44 #else
45 #include <sys/cred.h>
46 #include <cddl/dev/dtrace/dtrace_cddl.h>
47 
48 typedef	u_int			model_t;
49 #define	DATAMODEL_NATIVE	0
50 int dtrace_instr_size(uint8_t *);
51 int dtrace_instr_size_isa(uint8_t *, model_t, int *);
52 #endif
53 
54 #include <dis_tables.h>
55 
56 /*
57  * This subsystem (with the minor exception of the instr_size() function) is
58  * is called from DTrace probe context.  This imposes several requirements on
59  * the implementation:
60  *
61  * 1. External subsystems and functions may not be referenced.  The one current
62  *    exception is for cmn_err, but only to signal the detection of table
63  *    errors.  Assuming the tables are correct, no combination of input is to
64  *    trigger a cmn_err call.
65  *
66  * 2. These functions can't be allowed to be traced.  To prevent this,
67  *    all functions in the probe path (everything except instr_size()) must
68  *    have names that begin with "dtrace_".
69  */
70 
71 typedef enum dis_isize {
72 	DIS_ISIZE_INSTR,
73 	DIS_ISIZE_OPERAND
74 } dis_isize_t;
75 
76 
77 /*
78  * get a byte from instruction stream
79  */
80 static int
dtrace_dis_get_byte(void * p)81 dtrace_dis_get_byte(void *p)
82 {
83 	int ret;
84 	uint8_t **instr = p;
85 
86 	ret = **instr;
87 	*instr += 1;
88 
89 	return (ret);
90 }
91 
92 /*
93  * Returns either the size of a given instruction, in bytes, or the size of that
94  * instruction's memory access (if any), depending on the value of `which'.
95  * If a programming error in the tables is detected, the system will panic to
96  * ease diagnosis.  Invalid instructions will not be flagged.  They will appear
97  * to have an instruction size between 1 and the actual size, and will be
98  * reported as having no memory impact.
99  */
100 /* ARGSUSED2 */
101 static int
dtrace_dis_isize(uint8_t * instr,dis_isize_t which,model_t model,int * rmindex)102 dtrace_dis_isize(uint8_t *instr, dis_isize_t which, model_t model, int *rmindex)
103 {
104 	int sz;
105 	dis86_t	x;
106 	uint_t mode = SIZE32;
107 
108 	mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
109 
110 	x.d86_data = (void **)&instr;
111 	x.d86_get_byte = dtrace_dis_get_byte;
112 	x.d86_check_func = NULL;
113 
114 	if (dtrace_disx86(&x, mode) != 0)
115 		return (-1);
116 
117 	if (which == DIS_ISIZE_INSTR)
118 		sz = x.d86_len;		/* length of the instruction */
119 	else
120 		sz = x.d86_memsize;	/* length of memory operand */
121 
122 	if (rmindex != NULL)
123 		*rmindex = x.d86_rmindex;
124 	return (sz);
125 }
126 
127 int
dtrace_instr_size_isa(uint8_t * instr,model_t model,int * rmindex)128 dtrace_instr_size_isa(uint8_t *instr, model_t model, int *rmindex)
129 {
130 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
131 }
132 
133 int
dtrace_instr_size(uint8_t * instr)134 dtrace_instr_size(uint8_t *instr)
135 {
136 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
137 	    NULL));
138 }
139