instr_size.c revision 297077
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: stable/10/sys/cddl/dev/dtrace/amd64/instr_size.c 297077 2016-03-20 20:00:25Z mav $
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
47typedef	u_int			model_t;
48#define	DATAMODEL_NATIVE	0
49int dtrace_instr_size(uchar_t *);
50int dtrace_instr_size_isa(uchar_t *, model_t, int *);
51#endif
52
53#include <dis_tables.h>
54
55/*
56 * This subsystem (with the minor exception of the instr_size() function) is
57 * is called from DTrace probe context.  This imposes several requirements on
58 * the implementation:
59 *
60 * 1. External subsystems and functions may not be referenced.  The one current
61 *    exception is for cmn_err, but only to signal the detection of table
62 *    errors.  Assuming the tables are correct, no combination of input is to
63 *    trigger a cmn_err call.
64 *
65 * 2. These functions can't be allowed to be traced.  To prevent this,
66 *    all functions in the probe path (everything except instr_size()) must
67 *    have names that begin with "dtrace_".
68 */
69
70typedef enum dis_isize {
71	DIS_ISIZE_INSTR,
72	DIS_ISIZE_OPERAND
73} dis_isize_t;
74
75
76/*
77 * get a byte from instruction stream
78 */
79static int
80dtrace_dis_get_byte(void *p)
81{
82	int ret;
83	uchar_t **instr = p;
84
85	ret = **instr;
86	*instr += 1;
87
88	return (ret);
89}
90
91/*
92 * Returns either the size of a given instruction, in bytes, or the size of that
93 * instruction's memory access (if any), depending on the value of `which'.
94 * If a programming error in the tables is detected, the system will panic to
95 * ease diagnosis.  Invalid instructions will not be flagged.  They will appear
96 * to have an instruction size between 1 and the actual size, and will be
97 * reported as having no memory impact.
98 */
99/* ARGSUSED2 */
100static int
101dtrace_dis_isize(uchar_t *instr, dis_isize_t which, model_t model, int *rmindex)
102{
103	int sz;
104	dis86_t	x;
105	uint_t mode = SIZE64;
106
107#ifdef illumos
108	mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
109#endif
110
111	x.d86_data = (void **)&instr;
112	x.d86_get_byte = dtrace_dis_get_byte;
113	x.d86_check_func = NULL;
114
115	if (dtrace_disx86(&x, mode) != 0)
116		return (-1);
117
118	if (which == DIS_ISIZE_INSTR)
119		sz = x.d86_len;		/* length of the instruction */
120	else
121		sz = x.d86_memsize;	/* length of memory operand */
122
123	if (rmindex != NULL)
124		*rmindex = x.d86_rmindex;
125	return (sz);
126}
127
128int
129dtrace_instr_size_isa(uchar_t *instr, model_t model, int *rmindex)
130{
131	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
132}
133
134int
135dtrace_instr_size(uchar_t *instr)
136{
137	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
138	    NULL));
139}
140