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