xref: /illumos-gate/usr/src/uts/i86pc/os/instr_size.c (revision 2d6eb4a5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*40c00cd7Sahl  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/proc.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
357c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
367c478bd9Sstevel@tonic-gate #include <vm/seg_enum.h>
377c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <dis_tables.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * This subsystem (with the minor exception of the instr_size() function) is
437c478bd9Sstevel@tonic-gate  * is called from DTrace probe context.  This imposes several requirements on
447c478bd9Sstevel@tonic-gate  * the implementation:
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * 1. External subsystems and functions may not be referenced.  The one current
477c478bd9Sstevel@tonic-gate  *    exception is for cmn_err, but only to signal the detection of table
487c478bd9Sstevel@tonic-gate  *    errors.  Assuming the tables are correct, no combination of input is to
497c478bd9Sstevel@tonic-gate  *    trigger a cmn_err call.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * 2. These functions can't be allowed to be traced.  To prevent this,
527c478bd9Sstevel@tonic-gate  *    all functions in the probe path (everything except instr_size()) must
537c478bd9Sstevel@tonic-gate  *    have names that begin with "dtrace_".
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate typedef enum dis_isize {
577c478bd9Sstevel@tonic-gate 	DIS_ISIZE_INSTR,
587c478bd9Sstevel@tonic-gate 	DIS_ISIZE_OPERAND
597c478bd9Sstevel@tonic-gate } dis_isize_t;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * get a byte from instruction stream
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static int
dtrace_dis_get_byte(void * p)667c478bd9Sstevel@tonic-gate dtrace_dis_get_byte(void *p)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	int ret;
697c478bd9Sstevel@tonic-gate 	uchar_t **instr = p;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	ret = **instr;
727c478bd9Sstevel@tonic-gate 	*instr += 1;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	return (ret);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Returns either the size of a given instruction, in bytes, or the size of that
797c478bd9Sstevel@tonic-gate  * instruction's memory access (if any), depending on the value of `which'.
807c478bd9Sstevel@tonic-gate  * If a programming error in the tables is detected, the system will panic to
817c478bd9Sstevel@tonic-gate  * ease diagnosis.  Invalid instructions will not be flagged.  They will appear
827c478bd9Sstevel@tonic-gate  * to have an instruction size between 1 and the actual size, and will be
837c478bd9Sstevel@tonic-gate  * reported as having no memory impact.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
867c478bd9Sstevel@tonic-gate static int
dtrace_dis_isize(uchar_t * instr,dis_isize_t which,model_t model,int * rmindex)877c478bd9Sstevel@tonic-gate dtrace_dis_isize(uchar_t *instr, dis_isize_t which, model_t model, int *rmindex)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	int sz;
907c478bd9Sstevel@tonic-gate 	dis86_t	x;
917c478bd9Sstevel@tonic-gate 	uint_t mode = SIZE32;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	mode = (model == DATAMODEL_LP64) ? SIZE64 : SIZE32;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	x.d86_data = (void **)&instr;
967c478bd9Sstevel@tonic-gate 	x.d86_get_byte = dtrace_dis_get_byte;
977c478bd9Sstevel@tonic-gate 	x.d86_check_func = NULL;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (dtrace_disx86(&x, mode) != 0)
1007c478bd9Sstevel@tonic-gate 		return (-1);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (which == DIS_ISIZE_INSTR)
1037c478bd9Sstevel@tonic-gate 		sz = x.d86_len;		/* length of the instruction */
1047c478bd9Sstevel@tonic-gate 	else
1057c478bd9Sstevel@tonic-gate 		sz = x.d86_memsize;	/* length of memory operand */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (rmindex != NULL)
1087c478bd9Sstevel@tonic-gate 		*rmindex = x.d86_rmindex;
1097c478bd9Sstevel@tonic-gate 	return (sz);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate int
dtrace_instr_size_isa(uchar_t * instr,model_t model,int * rmindex)1137c478bd9Sstevel@tonic-gate dtrace_instr_size_isa(uchar_t *instr, model_t model, int *rmindex)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, model, rmindex));
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
dtrace_instr_size(uchar_t * instr)1197c478bd9Sstevel@tonic-gate dtrace_instr_size(uchar_t *instr)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	return (dtrace_dis_isize(instr, DIS_ISIZE_INSTR, DATAMODEL_NATIVE,
1227c478bd9Sstevel@tonic-gate 	    NULL));
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1267c478bd9Sstevel@tonic-gate int
instr_size(struct regs * rp,caddr_t * addrp,enum seg_rw rw)1277c478bd9Sstevel@tonic-gate instr_size(struct regs *rp, caddr_t *addrp, enum seg_rw rw)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	uchar_t instr[16];	/* maximum size instruction */
1307c478bd9Sstevel@tonic-gate 	caddr_t pc = (caddr_t)rp->r_pc;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	(void) copyin_nowatch(pc, (caddr_t)instr, sizeof (instr));
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (dtrace_dis_isize(instr,
1357c478bd9Sstevel@tonic-gate 	    rw == S_EXEC ? DIS_ISIZE_INSTR : DIS_ISIZE_OPERAND,
136*40c00cd7Sahl 	    curproc->p_model, NULL));
1377c478bd9Sstevel@tonic-gate }
138