10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright 2007 Jason King. All rights reserved.
29 * Use is subject to license terms.
30 */
31
32
33 #pragma ident "%Z%%M% %I% %E% SMI"
34
35 /*
36 * The sparc disassembler is mostly straightforward, each instruction is
37 * represented by an inst_t structure. The inst_t definitions are organized
38 * into tables. The tables are correspond to the opcode maps documented in the
39 * various sparc architecture manuals. Each table defines the bit range of the
40 * instruction whose value act as an index into the array of instructions. A
41 * table can also refer to another table if needed. Each table also contains
42 * a function pointer of type format_fcn that knows how to output the
43 * instructions in the table, as well as handle any synthetic instructions
44 *
45 * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
46 * instructions, they sometimes renamed or just reused the same instruction to
47 * do different operations (i.e. the sparcv8 coprocessor instructions). To
48 * accommodate this, each table can define an overlay table. The overlay table
49 * is a list of (table index, architecture, new instruction definition) values.
50 *
51 *
52 * Traversal starts with the first table,
53 * get index value from the instruction
54 * if an relevant overlay entry exists for this index,
88 */
89
90 #include <libdisasm.h>
91 #include <stdlib.h>
92 #include <stdio.h>
93 #include <sys/types.h>
94 #include <sys/byteorder.h>
95 #include <string.h>
96
97 #include "libdisasm_impl.h"
98 #include "dis_sparc.h"
99
100 static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
101 uint32_t);
102 static uint32_t dis_get_bits(uint32_t, int, int);
103
104 #if !defined(DIS_STANDALONE)
105 static void do_binary(uint32_t);
106 #endif /* DIS_STANDALONE */
107
108 dis_handle_t *
109 dis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
110 dis_read_f read_func)
111 {
112
113 #if !defined(DIS_STANDALONE)
114 char *opt = NULL;
115 char *opt2, *save, *end;
116 #endif
117 dis_handle_t *dhp;
118
119 if ((flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI)) == 0) {
120 (void) dis_seterrno(E_DIS_INVALFLAG);
121 return (NULL);
122 }
123
124 if ((dhp = dis_zalloc(sizeof (struct dis_handle))) == NULL) {
125 (void) dis_seterrno(E_DIS_NOMEM);
126 return (NULL);
127 }
128
129 dhp->dh_lookup = lookup_func;
130 dhp->dh_read = read_func;
131 dhp->dh_flags = flags;
132 dhp->dh_data = data;
133 dhp->dh_debug = DIS_DEBUG_COMPAT;
134
135 #if !defined(DIS_STANDALONE)
136
137 opt = getenv("_LIBDISASM_DEBUG");
138 if (opt == NULL)
139 return (dhp);
140
141 opt2 = strdup(opt);
142 if (opt2 == NULL) {
143 dis_handle_destroy(dhp);
144 (void) dis_seterrno(E_DIS_NOMEM);
145 return (NULL);
146 }
147 save = opt2;
148
149 while (opt2 != NULL) {
150 end = strchr(opt2, ',');
151
152 if (end != 0)
153 *end++ = '\0';
154
155 if (strcasecmp("synth-all", opt2) == 0)
156 dhp->dh_debug |= DIS_DEBUG_SYN_ALL;
157
158 if (strcasecmp("compat", opt2) == 0)
159 dhp->dh_debug |= DIS_DEBUG_COMPAT;
160
161 if (strcasecmp("synth-none", opt2) == 0)
162 dhp->dh_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
163
164 if (strcasecmp("binary", opt2) == 0)
165 dhp->dh_debug |= DIS_DEBUG_PRTBIN;
166
167 if (strcasecmp("format", opt2) == 0)
168 dhp->dh_debug |= DIS_DEBUG_PRTFMT;
169
170 if (strcasecmp("all", opt2) == 0)
171 dhp->dh_debug = DIS_DEBUG_ALL;
172
173 if (strcasecmp("none", opt2) == 0)
174 dhp->dh_debug = DIS_DEBUG_NONE;
175
176 opt2 = end;
177 }
178 free(save);
179 #endif /* DIS_STANDALONE */
180 return (dhp);
181 }
182
183 void
184 dis_handle_destroy(dis_handle_t *dhp)
185 {
186 dis_free(dhp, sizeof (dis_handle_t));
187 }
188
189 void
190 dis_set_data(dis_handle_t *dhp, void *data)
191 {
192 dhp->dh_data = data;
193 }
194
195 void
196 dis_flags_set(dis_handle_t *dhp, int f)
197 {
198 dhp->dh_flags |= f;
199 }
200
201 void
202 dis_flags_clear(dis_handle_t *dhp, int f)
203 {
204 dhp->dh_flags &= ~f;
205 }
206
207 /* ARGSUSED */
208 int
209 dis_max_instrlen(dis_handle_t *dhp)
210 {
211 return (4);
212 }
213
214 /*
215 * The dis_i386.c comment for this says it returns the previous instruction,
216 * however, I'm fairly sure it's actually returning the _address_ of the
217 * nth previous instruction.
218 */
219 /* ARGSUSED */
220 uint64_t
221 dis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
222 {
223 if (n <= 0)
224 return (pc);
225
226 if (pc < n)
227 return (pc);
228
229 return (pc - n*4);
230 }
231
232 int
233 dis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
234 {
235 const table_t *tp = &initial_table;
236 const inst_t *inp = NULL;
237
238 uint32_t instr;
239 uint32_t idx = 0;
240
241 if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
242 sizeof (instr))
243 return (-1);
244
245 dhp->dh_buf = buf;
246 dhp->dh_buflen = buflen;
247 dhp->dh_addr = addr;
248
249 buf[0] = '\0';
250
251 /* this allows sparc code to be tested on x86 */
252 instr = BE_32(instr);
253
254 #if !defined(DIS_STANDALONE)
255 if ((dhp->dh_debug & DIS_DEBUG_PRTBIN) != 0)
256 do_binary(instr);
257 #endif /* DIS_STANDALONE */
258
259 /* CONSTCOND */
260 while (1) {
261 idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
262 inp = &tp->tbl_inp[idx];
263
264 inp = dis_get_overlay(dhp, tp, idx);
265
266 if ((inp->in_type == INST_NONE) ||
267 ((inp->in_arch & dhp->dh_flags) == 0))
268 goto error;
269
270 if (inp->in_type == INST_TBL) {
271 tp = inp->in_data.in_tbl;
272 continue;
273 }
274
275 break;
320
321 if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
322 continue;
323
324 ip = &tp->tbl_ovp[i].ov_inst;
325 break;
326 }
327
328 return (ip);
329 }
330
331 #if !defined(DIS_STANDALONE)
332 static void
333 do_binary(uint32_t instr)
334 {
335 (void) fprintf(stderr, "DISASM: ");
336 prt_binary(instr, 32);
337 (void) fprintf(stderr, "\n");
338 }
339 #endif /* DIS_STANDALONE */
|
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright 2007 Jason King. All rights reserved.
29 * Use is subject to license terms.
30 * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
31 */
32
33
34 /*
35 * The sparc disassembler is mostly straightforward, each instruction is
36 * represented by an inst_t structure. The inst_t definitions are organized
37 * into tables. The tables are correspond to the opcode maps documented in the
38 * various sparc architecture manuals. Each table defines the bit range of the
39 * instruction whose value act as an index into the array of instructions. A
40 * table can also refer to another table if needed. Each table also contains
41 * a function pointer of type format_fcn that knows how to output the
42 * instructions in the table, as well as handle any synthetic instructions
43 *
44 * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
45 * instructions, they sometimes renamed or just reused the same instruction to
46 * do different operations (i.e. the sparcv8 coprocessor instructions). To
47 * accommodate this, each table can define an overlay table. The overlay table
48 * is a list of (table index, architecture, new instruction definition) values.
49 *
50 *
51 * Traversal starts with the first table,
52 * get index value from the instruction
53 * if an relevant overlay entry exists for this index,
87 */
88
89 #include <libdisasm.h>
90 #include <stdlib.h>
91 #include <stdio.h>
92 #include <sys/types.h>
93 #include <sys/byteorder.h>
94 #include <string.h>
95
96 #include "libdisasm_impl.h"
97 #include "dis_sparc.h"
98
99 static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
100 uint32_t);
101 static uint32_t dis_get_bits(uint32_t, int, int);
102
103 #if !defined(DIS_STANDALONE)
104 static void do_binary(uint32_t);
105 #endif /* DIS_STANDALONE */
106
107 static void
108 dis_sparc_handle_detach(dis_handle_t *dhp)
109 {
110 dis_free(dhp->dh_arch_private, sizeof (dis_handle_sparc_t));
111 dhp->dh_arch_private = NULL;
112 }
113
114 static int
115 dis_sparc_handle_attach(dis_handle_t *dhp)
116 {
117 dis_handle_sparc_t *dhx;
118
119 #if !defined(DIS_STANDALONE)
120 char *opt = NULL;
121 char *opt2, *save, *end;
122 #endif
123
124 /* Validate architecture flags */
125 if ((dhp->dh_flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI))
126 == 0) {
127 (void) dis_seterrno(E_DIS_INVALFLAG);
128 return (-1);
129 }
130
131 if ((dhx = dis_zalloc(sizeof (dis_handle_sparc_t))) == NULL) {
132 (void) dis_seterrno(E_DIS_NOMEM);
133 return (NULL);
134 }
135 dhx->dhx_debug = DIS_DEBUG_COMPAT;
136 dhp->dh_arch_private = dhx;
137
138 #if !defined(DIS_STANDALONE)
139
140 opt = getenv("_LIBDISASM_DEBUG");
141 if (opt == NULL)
142 return (0);
143
144 opt2 = strdup(opt);
145 if (opt2 == NULL) {
146 dis_handle_destroy(dhp);
147 dis_free(dhx, sizeof (dis_handle_sparc_t));
148 (void) dis_seterrno(E_DIS_NOMEM);
149 return (-1);
150 }
151 save = opt2;
152
153 while (opt2 != NULL) {
154 end = strchr(opt2, ',');
155
156 if (end != 0)
157 *end++ = '\0';
158
159 if (strcasecmp("synth-all", opt2) == 0)
160 dhx->dhx_debug |= DIS_DEBUG_SYN_ALL;
161
162 if (strcasecmp("compat", opt2) == 0)
163 dhx->dhx_debug |= DIS_DEBUG_COMPAT;
164
165 if (strcasecmp("synth-none", opt2) == 0)
166 dhx->dhx_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
167
168 if (strcasecmp("binary", opt2) == 0)
169 dhx->dhx_debug |= DIS_DEBUG_PRTBIN;
170
171 if (strcasecmp("format", opt2) == 0)
172 dhx->dhx_debug |= DIS_DEBUG_PRTFMT;
173
174 if (strcasecmp("all", opt2) == 0)
175 dhx->dhx_debug = DIS_DEBUG_ALL;
176
177 if (strcasecmp("none", opt2) == 0)
178 dhx->dhx_debug = DIS_DEBUG_NONE;
179
180 opt2 = end;
181 }
182 free(save);
183 #endif /* DIS_STANDALONE */
184 return (0);
185 }
186
187 /* ARGSUSED */
188 static int
189 dis_sparc_max_instrlen(dis_handle_t *dhp)
190 {
191 return (4);
192 }
193
194 /* ARGSUSED */
195 static int
196 dis_sparc_min_instrlen(dis_handle_t *dhp)
197 {
198 return (4);
199 }
200
201 /*
202 * The dis_i386.c comment for this says it returns the previous instruction,
203 * however, I'm fairly sure it's actually returning the _address_ of the
204 * nth previous instruction.
205 */
206 /* ARGSUSED */
207 static uint64_t
208 dis_sparc_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
209 {
210 if (n <= 0)
211 return (pc);
212
213 if (pc < n)
214 return (pc);
215
216 return (pc - n*4);
217 }
218
219 static int
220 dis_sparc_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf,
221 size_t buflen)
222 {
223 dis_handle_sparc_t *dhx = dhp->dh_arch_private;
224 const table_t *tp = &initial_table;
225 const inst_t *inp = NULL;
226
227 uint32_t instr;
228 uint32_t idx = 0;
229
230 if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
231 sizeof (instr))
232 return (-1);
233
234 dhx->dhx_buf = buf;
235 dhx->dhx_buflen = buflen;
236 dhp->dh_addr = addr;
237
238 buf[0] = '\0';
239
240 /* this allows sparc code to be tested on x86 */
241 #if !defined(DIS_STANDALONE)
242 instr = BE_32(instr);
243 #endif /* DIS_STANDALONE */
244
245 #if !defined(DIS_STANDALONE)
246 if ((dhx->dhx_debug & DIS_DEBUG_PRTBIN) != 0)
247 do_binary(instr);
248 #endif /* DIS_STANDALONE */
249
250 /* CONSTCOND */
251 while (1) {
252 idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
253 inp = &tp->tbl_inp[idx];
254
255 inp = dis_get_overlay(dhp, tp, idx);
256
257 if ((inp->in_type == INST_NONE) ||
258 ((inp->in_arch & dhp->dh_flags) == 0))
259 goto error;
260
261 if (inp->in_type == INST_TBL) {
262 tp = inp->in_data.in_tbl;
263 continue;
264 }
265
266 break;
311
312 if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
313 continue;
314
315 ip = &tp->tbl_ovp[i].ov_inst;
316 break;
317 }
318
319 return (ip);
320 }
321
322 #if !defined(DIS_STANDALONE)
323 static void
324 do_binary(uint32_t instr)
325 {
326 (void) fprintf(stderr, "DISASM: ");
327 prt_binary(instr, 32);
328 (void) fprintf(stderr, "\n");
329 }
330 #endif /* DIS_STANDALONE */
331
332 static int
333 dis_sparc_supports_flags(int flags)
334 {
335 int archflags = flags & DIS_ARCH_MASK;
336
337 if (archflags == DIS_SPARC_V8 ||
338 (archflags & (DIS_SPARC_V9 | DIS_SPARC_V8)) == DIS_SPARC_V9)
339 return (1);
340
341 return (0);
342 }
343
344 const dis_arch_t dis_arch_sparc = {
345 dis_sparc_supports_flags,
346 dis_sparc_handle_attach,
347 dis_sparc_handle_detach,
348 dis_sparc_disassemble,
349 dis_sparc_previnstr,
350 dis_sparc_min_instrlen,
351 dis_sparc_max_instrlen
352 };
|