1179237Sjb/*
2179237Sjb * CDDL HEADER START
3179237Sjb *
4179237Sjb * The contents of this file are subject to the terms of the
5179237Sjb * Common Development and Distribution License, Version 1.0 only
6179237Sjb * (the "License").  You may not use this file except in compliance
7179237Sjb * with the License.
8179237Sjb *
9179237Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10179237Sjb * or http://www.opensolaris.org/os/licensing.
11179237Sjb * See the License for the specific language governing permissions
12179237Sjb * and limitations under the License.
13179237Sjb *
14179237Sjb * When distributing Covered Code, include this CDDL HEADER in each
15179237Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16179237Sjb * If applicable, add the following below this CDDL HEADER, with the
17179237Sjb * fields enclosed by brackets "[]" replaced with your own identifying
18179237Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
19179237Sjb *
20179237Sjb * CDDL HEADER END
21179237Sjb *
22179237Sjb * $FreeBSD$
23179237Sjb */
24179237Sjb/*
25179237Sjb * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26179237Sjb * Use is subject to license terms.
27179237Sjb */
28179237Sjb
29179237Sjb/*	Copyright (c) 1988 AT&T	*/
30179237Sjb/*	  All Rights Reserved	*/
31179237Sjb
32179237Sjb
33179237Sjb#if defined(sun)
34179237Sjb#pragma ident	"@(#)instr_size.c	1.14	05/07/08 SMI"
35179237Sjb#endif
36179237Sjb
37179237Sjb#include <sys/types.h>
38179237Sjb#include <sys/param.h>
39179237Sjb#include <sys/proc.h>
40179237Sjb#if defined(sun)
41179237Sjb#include <sys/cmn_err.h>
42179237Sjb#include <sys/archsystm.h>
43179237Sjb#include <sys/copyops.h>
44179237Sjb#include <vm/seg_enum.h>
45179237Sjb#include <sys/privregs.h>
46179237Sjb#else
47179237Sjbtypedef	u_int			model_t;
48179237Sjb#define	DATAMODEL_NATIVE	0
49179237Sjbint dtrace_instr_size(uchar_t *);
50211607Srpauloint dtrace_instr_size_isa(uchar_t *, model_t, int *);
51179237Sjb#endif
52179237Sjb
53179237Sjb#include <dis_tables.h>
54179237Sjb
55179237Sjb/*
56179237Sjb * This subsystem (with the minor exception of the instr_size() function) is
57179237Sjb * is called from DTrace probe context.  This imposes several requirements on
58179237Sjb * the implementation:
59179237Sjb *
60179237Sjb * 1. External subsystems and functions may not be referenced.  The one current
61179237Sjb *    exception is for cmn_err, but only to signal the detection of table
62179237Sjb *    errors.  Assuming the tables are correct, no combination of input is to
63179237Sjb *    trigger a cmn_err call.
64179237Sjb *
65179237Sjb * 2. These functions can't be allowed to be traced.  To prevent this,
66179237Sjb *    all functions in the probe path (everything except instr_size()) must
67179237Sjb *    have names that begin with "dtrace_".
68179237Sjb */
69179237Sjb
70179237Sjbtypedef enum dis_isize {
71179237Sjb	DIS_ISIZE_INSTR,
72179237Sjb	DIS_ISIZE_OPERAND
73179237Sjb} dis_isize_t;
74179237Sjb
75179237Sjb
76179237Sjb/*
77179237Sjb * get a byte from instruction stream
78179237Sjb */
79179237Sjbstatic int
80179237Sjbdtrace_dis_get_byte(void *p)
81179237Sjb{
82179237Sjb	int ret;
83179237Sjb	uchar_t **instr = p;
84179237Sjb
85179237Sjb	ret = **instr;
86179237Sjb	*instr += 1;
87179237Sjb
88179237Sjb	return (ret);
89179237Sjb}
90179237Sjb
91179237Sjb/*
92179237Sjb * Returns either the size of a given instruction, in bytes, or the size of that
93179237Sjb * instruction's memory access (if any), depending on the value of `which'.
94179237Sjb * If a programming error in the tables is detected, the system will panic to
95179237Sjb * ease diagnosis.  Invalid instructions will not be flagged.  They will appear
96179237Sjb * to have an instruction size between 1 and the actual size, and will be
97179237Sjb * reported as having no memory impact.
98179237Sjb */
99179237Sjb/* ARGSUSED2 */
100179237Sjbstatic int
101179237Sjbdtrace_dis_isize(uchar_t *instr, dis_isize_t which, model_t model, int *rmindex)
102179237Sjb{
103179237Sjb	int sz;
104179237Sjb	dis86_t	x;
105179237Sjb	uint_t mode = SIZE64;
106179237Sjb
107179237Sjb#if defined(sun)
108179237Sjb	mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
109179237Sjb#endif
110179237Sjb
111179237Sjb	x.d86_data = (void **)&instr;
112179237Sjb	x.d86_get_byte = dtrace_dis_get_byte;
113179237Sjb	x.d86_check_func = NULL;
114179237Sjb
115179237Sjb	if (dtrace_disx86(&x, mode) != 0)
116179237Sjb		return (-1);
117179237Sjb
118179237Sjb	if (which == DIS_ISIZE_INSTR)
119179237Sjb		sz = x.d86_len;		/* length of the instruction */
120179237Sjb	else
121179237Sjb		sz = x.d86_memsize;	/* length of memory operand */
122179237Sjb
123179237Sjb	if (rmindex != NULL)
124179237Sjb		*rmindex = x.d86_rmindex;
125179237Sjb	return (sz);
126179237Sjb}
127179237Sjb
128179237Sjbint
129211607Srpaulodtrace_instr_size_isa(uchar_t *instr, model_t model, int *rmindex)
130211607Srpaulo{
131211607Srpaulo	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
132211607Srpaulo}
133211607Srpaulo
134211607Srpauloint
135179237Sjbdtrace_instr_size(uchar_t *instr)
136179237Sjb{
137179237Sjb	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
138179237Sjb	    NULL));
139179237Sjb}
140