xref: /titanic_52/usr/src/lib/libdisasm/common/dis_i386.c (revision cfc41e72a4f85ff48430f435531264fca458f55d)
1f7184619SJoshua M. Clulow /*
2f7184619SJoshua M. Clulow  * CDDL HEADER START
3f7184619SJoshua M. Clulow  *
4f7184619SJoshua M. Clulow  * The contents of this file are subject to the terms of the
5f7184619SJoshua M. Clulow  * Common Development and Distribution License (the "License").
6f7184619SJoshua M. Clulow  * You may not use this file except in compliance with the License.
7f7184619SJoshua M. Clulow  *
8f7184619SJoshua M. Clulow  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f7184619SJoshua M. Clulow  * or http://www.opensolaris.org/os/licensing.
10f7184619SJoshua M. Clulow  * See the License for the specific language governing permissions
11f7184619SJoshua M. Clulow  * and limitations under the License.
12f7184619SJoshua M. Clulow  *
13f7184619SJoshua M. Clulow  * When distributing Covered Code, include this CDDL HEADER in each
14f7184619SJoshua M. Clulow  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f7184619SJoshua M. Clulow  * If applicable, add the following below this CDDL HEADER, with the
16f7184619SJoshua M. Clulow  * fields enclosed by brackets "[]" replaced with your own identifying
17f7184619SJoshua M. Clulow  * information: Portions Copyright [yyyy] [name of copyright owner]
18f7184619SJoshua M. Clulow  *
19f7184619SJoshua M. Clulow  * CDDL HEADER END
20f7184619SJoshua M. Clulow  */
21f7184619SJoshua M. Clulow 
22f7184619SJoshua M. Clulow /*
23f7184619SJoshua M. Clulow  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24f7184619SJoshua M. Clulow  * Use is subject to license terms.
25f7184619SJoshua M. Clulow  * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
26f7184619SJoshua M. Clulow  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
27f7184619SJoshua M. Clulow  */
28f7184619SJoshua M. Clulow 
29f7184619SJoshua M. Clulow #include <libdisasm.h>
30f7184619SJoshua M. Clulow 
31f7184619SJoshua M. Clulow #include "dis_tables.h"
32f7184619SJoshua M. Clulow #include "libdisasm_impl.h"
33f7184619SJoshua M. Clulow 
34f7184619SJoshua M. Clulow typedef struct dis_handle_i386 {
35f7184619SJoshua M. Clulow 	int		dhx_mode;
36f7184619SJoshua M. Clulow 	dis86_t		dhx_dis;
37f7184619SJoshua M. Clulow 	uint64_t	dhx_end;
38f7184619SJoshua M. Clulow } dis_handle_i386_t;
39f7184619SJoshua M. Clulow 
40f7184619SJoshua M. Clulow /*
41f7184619SJoshua M. Clulow  * Returns true if we are near the end of a function.  This is a cheap hack at
42f7184619SJoshua M. Clulow  * detecting NULL padding between functions.  If we're within a few bytes of the
43f7184619SJoshua M. Clulow  * next function, or past the start, then return true.
44f7184619SJoshua M. Clulow  */
45f7184619SJoshua M. Clulow static int
46f7184619SJoshua M. Clulow check_func(void *data)
47f7184619SJoshua M. Clulow {
48f7184619SJoshua M. Clulow 	dis_handle_t *dhp = data;
49f7184619SJoshua M. Clulow 	uint64_t start;
50f7184619SJoshua M. Clulow 	size_t len;
51f7184619SJoshua M. Clulow 
52f7184619SJoshua M. Clulow 	if (dhp->dh_lookup(dhp->dh_data, dhp->dh_addr, NULL, 0, &start, &len)
53f7184619SJoshua M. Clulow 	    != 0)
54f7184619SJoshua M. Clulow 		return (0);
55f7184619SJoshua M. Clulow 
56f7184619SJoshua M. Clulow 	if (start < dhp->dh_addr)
57f7184619SJoshua M. Clulow 		return (dhp->dh_addr > start + len - 0x10);
58f7184619SJoshua M. Clulow 
59f7184619SJoshua M. Clulow 	return (1);
60f7184619SJoshua M. Clulow }
61f7184619SJoshua M. Clulow 
62f7184619SJoshua M. Clulow static int
63f7184619SJoshua M. Clulow get_byte(void *data)
64f7184619SJoshua M. Clulow {
65f7184619SJoshua M. Clulow 	uchar_t byte;
66f7184619SJoshua M. Clulow 	dis_handle_t *dhp = data;
67f7184619SJoshua M. Clulow 
68f7184619SJoshua M. Clulow 	if (dhp->dh_read(dhp->dh_data, dhp->dh_addr, &byte, sizeof (byte)) !=
69f7184619SJoshua M. Clulow 	    sizeof (byte))
70f7184619SJoshua M. Clulow 		return (-1);
71f7184619SJoshua M. Clulow 
72f7184619SJoshua M. Clulow 	dhp->dh_addr++;
73f7184619SJoshua M. Clulow 
74f7184619SJoshua M. Clulow 	return ((int)byte);
75f7184619SJoshua M. Clulow }
76f7184619SJoshua M. Clulow 
77f7184619SJoshua M. Clulow static int
78f7184619SJoshua M. Clulow do_lookup(void *data, uint64_t addr, char *buf, size_t buflen)
79f7184619SJoshua M. Clulow {
80f7184619SJoshua M. Clulow 	dis_handle_t *dhp = data;
81f7184619SJoshua M. Clulow 
82f7184619SJoshua M. Clulow 	return (dhp->dh_lookup(dhp->dh_data, addr, buf, buflen, NULL, NULL));
83f7184619SJoshua M. Clulow }
84f7184619SJoshua M. Clulow 
85f7184619SJoshua M. Clulow static void
86f7184619SJoshua M. Clulow dis_i386_handle_detach(dis_handle_t *dhp)
87f7184619SJoshua M. Clulow {
88f7184619SJoshua M. Clulow 	dis_free(dhp->dh_arch_private, sizeof (dis_handle_i386_t));
89f7184619SJoshua M. Clulow 	dhp->dh_arch_private = NULL;
90f7184619SJoshua M. Clulow }
91f7184619SJoshua M. Clulow 
92f7184619SJoshua M. Clulow static int
93f7184619SJoshua M. Clulow dis_i386_handle_attach(dis_handle_t *dhp)
94f7184619SJoshua M. Clulow {
95f7184619SJoshua M. Clulow 	dis_handle_i386_t *dhx;
96f7184619SJoshua M. Clulow 
97f7184619SJoshua M. Clulow 	/*
98f7184619SJoshua M. Clulow 	 * Validate architecture flags
99f7184619SJoshua M. Clulow 	 */
100f7184619SJoshua M. Clulow 	if (dhp->dh_flags & ~(DIS_X86_SIZE16 | DIS_X86_SIZE32 | DIS_X86_SIZE64 |
101f7184619SJoshua M. Clulow 	    DIS_OCTAL | DIS_NOIMMSYM)) {
102f7184619SJoshua M. Clulow 		(void) dis_seterrno(E_DIS_INVALFLAG);
103f7184619SJoshua M. Clulow 		return (-1);
104f7184619SJoshua M. Clulow 	}
105f7184619SJoshua M. Clulow 
106f7184619SJoshua M. Clulow 	/*
107f7184619SJoshua M. Clulow 	 * Create and initialize the internal structure
108f7184619SJoshua M. Clulow 	 */
109f7184619SJoshua M. Clulow 	if ((dhx = dis_zalloc(sizeof (dis_handle_i386_t))) == NULL) {
110f7184619SJoshua M. Clulow 		(void) dis_seterrno(E_DIS_NOMEM);
111f7184619SJoshua M. Clulow 		return (-1);
112f7184619SJoshua M. Clulow 	}
113f7184619SJoshua M. Clulow 	dhp->dh_arch_private = dhx;
114f7184619SJoshua M. Clulow 
115f7184619SJoshua M. Clulow 	/*
116f7184619SJoshua M. Clulow 	 * Initialize x86-specific architecture structure
117f7184619SJoshua M. Clulow 	 */
118f7184619SJoshua M. Clulow 	if (dhp->dh_flags & DIS_X86_SIZE16)
119f7184619SJoshua M. Clulow 		dhx->dhx_mode = SIZE16;
120f7184619SJoshua M. Clulow 	else if (dhp->dh_flags & DIS_X86_SIZE64)
121f7184619SJoshua M. Clulow 		dhx->dhx_mode = SIZE64;
122f7184619SJoshua M. Clulow 	else
123f7184619SJoshua M. Clulow 		dhx->dhx_mode = SIZE32;
124f7184619SJoshua M. Clulow 
125f7184619SJoshua M. Clulow 	if (dhp->dh_flags & DIS_OCTAL)
126f7184619SJoshua M. Clulow 		dhx->dhx_dis.d86_flags = DIS_F_OCTAL;
127f7184619SJoshua M. Clulow 
128f7184619SJoshua M. Clulow 	dhx->dhx_dis.d86_sprintf_func = dis_snprintf;
129f7184619SJoshua M. Clulow 	dhx->dhx_dis.d86_get_byte = get_byte;
130f7184619SJoshua M. Clulow 	dhx->dhx_dis.d86_sym_lookup = do_lookup;
131f7184619SJoshua M. Clulow 	dhx->dhx_dis.d86_check_func = check_func;
132f7184619SJoshua M. Clulow 
133f7184619SJoshua M. Clulow 	dhx->dhx_dis.d86_data = dhp;
134f7184619SJoshua M. Clulow 
135f7184619SJoshua M. Clulow 	return (0);
136f7184619SJoshua M. Clulow }
137f7184619SJoshua M. Clulow 
138f7184619SJoshua M. Clulow static int
139f7184619SJoshua M. Clulow dis_i386_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf,
140f7184619SJoshua M. Clulow     size_t buflen)
141f7184619SJoshua M. Clulow {
142f7184619SJoshua M. Clulow 	dis_handle_i386_t *dhx = dhp->dh_arch_private;
143f7184619SJoshua M. Clulow 	dhp->dh_addr = addr;
144f7184619SJoshua M. Clulow 
145f7184619SJoshua M. Clulow 	/* DIS_NOIMMSYM might not be set until now, so update */
146f7184619SJoshua M. Clulow 	if (dhp->dh_flags & DIS_NOIMMSYM)
147f7184619SJoshua M. Clulow 		dhx->dhx_dis.d86_flags |= DIS_F_NOIMMSYM;
148f7184619SJoshua M. Clulow 	else
149f7184619SJoshua M. Clulow 		dhx->dhx_dis.d86_flags &= ~DIS_F_NOIMMSYM;
150f7184619SJoshua M. Clulow 
151f7184619SJoshua M. Clulow 	if (dtrace_disx86(&dhx->dhx_dis, dhx->dhx_mode) != 0)
152f7184619SJoshua M. Clulow 		return (-1);
153f7184619SJoshua M. Clulow 
154f7184619SJoshua M. Clulow 	if (buf != NULL)
155f7184619SJoshua M. Clulow 		dtrace_disx86_str(&dhx->dhx_dis, dhx->dhx_mode, addr, buf,
156f7184619SJoshua M. Clulow 		    buflen);
157f7184619SJoshua M. Clulow 
158f7184619SJoshua M. Clulow 	return (0);
159f7184619SJoshua M. Clulow }
160f7184619SJoshua M. Clulow 
161f7184619SJoshua M. Clulow /* ARGSUSED */
162f7184619SJoshua M. Clulow static int
163f7184619SJoshua M. Clulow dis_i386_max_instrlen(dis_handle_t *dhp)
164f7184619SJoshua M. Clulow {
165f7184619SJoshua M. Clulow 	return (15);
166f7184619SJoshua M. Clulow }
167f7184619SJoshua M. Clulow 
168f7184619SJoshua M. Clulow /* ARGSUSED */
169f7184619SJoshua M. Clulow static int
170f7184619SJoshua M. Clulow dis_i386_min_instrlen(dis_handle_t *dhp)
171f7184619SJoshua M. Clulow {
172f7184619SJoshua M. Clulow 	return (1);
173f7184619SJoshua M. Clulow }
174f7184619SJoshua M. Clulow 
175f7184619SJoshua M. Clulow static int
176f7184619SJoshua M. Clulow dis_i386_supports_flags(int flags)
177f7184619SJoshua M. Clulow {
178f7184619SJoshua M. Clulow 	int archflags = flags & DIS_ARCH_MASK;
179f7184619SJoshua M. Clulow 
180f7184619SJoshua M. Clulow 	if (archflags == DIS_X86_SIZE16 || archflags == DIS_X86_SIZE32 ||
181f7184619SJoshua M. Clulow 	    archflags == DIS_X86_SIZE64)
182f7184619SJoshua M. Clulow 		return (1);
183f7184619SJoshua M. Clulow 
184f7184619SJoshua M. Clulow 	return (0);
185f7184619SJoshua M. Clulow }
186f7184619SJoshua M. Clulow 
187f7184619SJoshua M. Clulow dis_arch_t dis_arch_i386 = {
188*b3457a09SJosef 'Jeff' Sipek 	.da_supports_flags	= dis_i386_supports_flags,
189*b3457a09SJosef 'Jeff' Sipek 	.da_handle_attach	= dis_i386_handle_attach,
190*b3457a09SJosef 'Jeff' Sipek 	.da_handle_detach	= dis_i386_handle_detach,
191*b3457a09SJosef 'Jeff' Sipek 	.da_disassemble		= dis_i386_disassemble,
192*b3457a09SJosef 'Jeff' Sipek 	.da_min_instrlen	= dis_i386_min_instrlen,
193*b3457a09SJosef 'Jeff' Sipek 	.da_max_instrlen	= dis_i386_max_instrlen,
194f7184619SJoshua M. Clulow };
195