xref: /illumos-gate/usr/src/uts/i86pc/os/instr_size.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 #include <sys/proc.h>
32 #include <sys/param.h>
33 #include <sys/cmn_err.h>
34 #include <sys/archsystm.h>
35 #include <sys/copyops.h>
36 #include <vm/seg_enum.h>
37 #include <sys/privregs.h>
38 
39 #include <dis_tables.h>
40 
41 /*
42  * This subsystem (with the minor exception of the instr_size() function) is
43  * is called from DTrace probe context.  This imposes several requirements on
44  * the implementation:
45  *
46  * 1. External subsystems and functions may not be referenced.  The one current
47  *    exception is for cmn_err, but only to signal the detection of table
48  *    errors.  Assuming the tables are correct, no combination of input is to
49  *    trigger a cmn_err call.
50  *
51  * 2. These functions can't be allowed to be traced.  To prevent this,
52  *    all functions in the probe path (everything except instr_size()) must
53  *    have names that begin with "dtrace_".
54  */
55 
56 typedef enum dis_isize {
57 	DIS_ISIZE_INSTR,
58 	DIS_ISIZE_OPERAND
59 } dis_isize_t;
60 
61 
62 /*
63  * get a byte from instruction stream
64  */
65 static int
66 dtrace_dis_get_byte(void *p)
67 {
68 	int ret;
69 	uchar_t **instr = p;
70 
71 	ret = **instr;
72 	*instr += 1;
73 
74 	return (ret);
75 }
76 
77 /*
78  * Returns either the size of a given instruction, in bytes, or the size of that
79  * instruction's memory access (if any), depending on the value of `which'.
80  * If a programming error in the tables is detected, the system will panic to
81  * ease diagnosis.  Invalid instructions will not be flagged.  They will appear
82  * to have an instruction size between 1 and the actual size, and will be
83  * reported as having no memory impact.
84  */
85 /* ARGSUSED2 */
86 static int
87 dtrace_dis_isize(uchar_t *instr, dis_isize_t which, model_t model, int *rmindex)
88 {
89 	int sz;
90 	dis86_t	x;
91 	uint_t mode = SIZE32;
92 
93 	mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
94 
95 	x.d86_data = (void **)&instr;
96 	x.d86_get_byte = dtrace_dis_get_byte;
97 	x.d86_check_func = NULL;
98 
99 	if (dtrace_disx86(&x, mode) != 0)
100 		return (-1);
101 
102 	if (which == DIS_ISIZE_INSTR)
103 		sz = x.d86_len;		/* length of the instruction */
104 	else
105 		sz = x.d86_memsize;	/* length of memory operand */
106 
107 	if (rmindex != NULL)
108 		*rmindex = x.d86_rmindex;
109 	return (sz);
110 }
111 
112 int
113 dtrace_instr_size_isa(uchar_t *instr, model_t model, int *rmindex)
114 {
115 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
116 }
117 
118 int
119 dtrace_instr_size(uchar_t *instr)
120 {
121 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
122 	    NULL));
123 }
124 
125 /*ARGSUSED*/
126 int
127 instr_size(struct regs *rp, caddr_t *addrp, enum seg_rw rw)
128 {
129 	uchar_t instr[16];	/* maximum size instruction */
130 	caddr_t pc = (caddr_t)rp->r_pc;
131 
132 	(void) copyin_nowatch(pc, (caddr_t)instr, sizeof (instr));
133 
134 	return (dtrace_dis_isize(instr,
135 	    rw == S_EXEC ? DIS_ISIZE_INSTR : DIS_ISIZE_OPERAND,
136 	    curproc->p_model, NULL));
137 }
138