8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libdisasm / common / dis_sparc.c
blob224fe9042decb2f8c4de5f0312004134147d854e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright 2007 Jason King. All rights reserved.
29 * Use is subject to license terms.
30 * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
34 * The sparc disassembler is mostly straightforward, each instruction is
35 * represented by an inst_t structure. The inst_t definitions are organized
36 * into tables. The tables are correspond to the opcode maps documented in the
37 * various sparc architecture manuals. Each table defines the bit range of the
38 * instruction whose value act as an index into the array of instructions. A
39 * table can also refer to another table if needed. Each table also contains
40 * a function pointer of type format_fcn that knows how to output the
41 * instructions in the table, as well as handle any synthetic instructions
43 * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
44 * instructions, they sometimes renamed or just reused the same instruction to
45 * do different operations (i.e. the sparcv8 coprocessor instructions). To
46 * accommodate this, each table can define an overlay table. The overlay table
47 * is a list of (table index, architecture, new instruction definition) values.
50 * Traversal starts with the first table,
51 * get index value from the instruction
52 * if an relevant overlay entry exists for this index,
53 * grab the overlay definition
54 * else
55 * grab the definition from the array (corresponding to the index value)
57 * If the entry is an instruction,
58 * call print function of instruction.
59 * If the entry is a pointer to another table
60 * traverse the table
61 * If not valid,
62 * return an error
65 * To keep dis happy, for sparc, instead of actually returning an error, if
66 * the instruction cannot be disassembled, we instead merely place the value
67 * of the instruction into the output buffer.
69 * Adding new instructions:
71 * With the above information, it hopefully makes it clear how to add support
72 * for decoding new instructions. Presumably, with new instructions will come
73 * a new dissassembly mode (I.e. DIS_SPARC_V8, DIS_SPARC_V9, etc.).
75 * If the dissassembled format does not correspond to one of the existing
76 * formats, a new formatter will have to be written. The 'flags' value of
77 * inst_t is intended to instruct the corresponding formatter about how to
78 * output the instruction.
80 * If the corresponding entry in the correct table is currently unoccupied,
81 * simply replace the INVALID entry with the correct definition. The INST and
82 * TABLE macros are suggested to be used for this. If there is already an
83 * instruction defined, then the entry must be placed in an overlay table. If
84 * no overlay table exists for the instruction table, one will need to be
85 * created.
88 #include <libdisasm.h>
89 #include <stdlib.h>
90 #include <stdio.h>
91 #include <sys/types.h>
92 #include <sys/byteorder.h>
93 #include <string.h>
95 #include "libdisasm_impl.h"
96 #include "dis_sparc.h"
98 static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
99 uint32_t);
100 static uint32_t dis_get_bits(uint32_t, int, int);
102 #if !defined(DIS_STANDALONE)
103 static void do_binary(uint32_t);
104 #endif /* DIS_STANDALONE */
106 static void
107 dis_sparc_handle_detach(dis_handle_t *dhp)
109 dis_free(dhp->dh_arch_private, sizeof (dis_handle_sparc_t));
110 dhp->dh_arch_private = NULL;
113 static int
114 dis_sparc_handle_attach(dis_handle_t *dhp)
116 dis_handle_sparc_t *dhx;
118 #if !defined(DIS_STANDALONE)
119 char *opt = NULL;
120 char *opt2, *save, *end;
121 #endif
123 /* Validate architecture flags */
124 if ((dhp->dh_flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI))
125 == 0) {
126 (void) dis_seterrno(E_DIS_INVALFLAG);
127 return (-1);
130 if ((dhx = dis_zalloc(sizeof (dis_handle_sparc_t))) == NULL) {
131 (void) dis_seterrno(E_DIS_NOMEM);
132 return (NULL);
134 dhx->dhx_debug = DIS_DEBUG_COMPAT;
135 dhp->dh_arch_private = dhx;
137 #if !defined(DIS_STANDALONE)
139 opt = getenv("_LIBDISASM_DEBUG");
140 if (opt == NULL)
141 return (0);
143 opt2 = strdup(opt);
144 if (opt2 == NULL) {
145 dis_handle_destroy(dhp);
146 dis_free(dhx, sizeof (dis_handle_sparc_t));
147 (void) dis_seterrno(E_DIS_NOMEM);
148 return (-1);
150 save = opt2;
152 while (opt2 != NULL) {
153 end = strchr(opt2, ',');
155 if (end != 0)
156 *end++ = '\0';
158 if (strcasecmp("synth-all", opt2) == 0)
159 dhx->dhx_debug |= DIS_DEBUG_SYN_ALL;
161 if (strcasecmp("compat", opt2) == 0)
162 dhx->dhx_debug |= DIS_DEBUG_COMPAT;
164 if (strcasecmp("synth-none", opt2) == 0)
165 dhx->dhx_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
167 if (strcasecmp("binary", opt2) == 0)
168 dhx->dhx_debug |= DIS_DEBUG_PRTBIN;
170 if (strcasecmp("format", opt2) == 0)
171 dhx->dhx_debug |= DIS_DEBUG_PRTFMT;
173 if (strcasecmp("all", opt2) == 0)
174 dhx->dhx_debug = DIS_DEBUG_ALL;
176 if (strcasecmp("none", opt2) == 0)
177 dhx->dhx_debug = DIS_DEBUG_NONE;
179 opt2 = end;
181 free(save);
182 #endif /* DIS_STANDALONE */
183 return (0);
186 /* ARGSUSED */
187 static int
188 dis_sparc_max_instrlen(dis_handle_t *dhp)
190 return (4);
193 /* ARGSUSED */
194 static int
195 dis_sparc_min_instrlen(dis_handle_t *dhp)
197 return (4);
200 /* ARGSUSED */
201 static uint64_t
202 dis_sparc_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
204 if (n <= 0)
205 return (pc);
207 if (pc < n)
208 return (pc);
210 return (pc - n*4);
213 /* ARGSUSED */
214 static int
215 dis_sparc_instrlen(dis_handle_t *dhp, uint64_t pc)
217 return (4);
220 static int
221 dis_sparc_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf,
222 size_t buflen)
224 dis_handle_sparc_t *dhx = dhp->dh_arch_private;
225 const table_t *tp = &initial_table;
226 const inst_t *inp = NULL;
228 uint32_t instr;
229 uint32_t idx = 0;
231 if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
232 sizeof (instr))
233 return (-1);
235 dhx->dhx_buf = buf;
236 dhx->dhx_buflen = buflen;
237 dhp->dh_addr = addr;
239 buf[0] = '\0';
241 /* this allows sparc code to be tested on x86 */
242 #if !defined(DIS_STANDALONE)
243 instr = BE_32(instr);
244 #endif /* DIS_STANDALONE */
246 #if !defined(DIS_STANDALONE)
247 if ((dhx->dhx_debug & DIS_DEBUG_PRTBIN) != 0)
248 do_binary(instr);
249 #endif /* DIS_STANDALONE */
251 /* CONSTCOND */
252 while (1) {
253 idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
254 inp = &tp->tbl_inp[idx];
256 inp = dis_get_overlay(dhp, tp, idx);
258 if ((inp->in_type == INST_NONE) ||
259 ((inp->in_arch & dhp->dh_flags) == 0))
260 goto error;
262 if (inp->in_type == INST_TBL) {
263 tp = inp->in_data.in_tbl;
264 continue;
267 break;
270 if (tp->tbl_fmt(dhp, instr, inp, idx) == 0)
271 return (0);
273 error:
275 (void) dis_snprintf(buf, buflen,
276 ((dhp->dh_flags & DIS_OCTAL) != 0) ? "0%011lo" : "0x%08lx",
277 instr);
279 return (0);
282 static uint32_t
283 dis_get_bits(uint32_t instr, int offset, int length)
285 uint32_t mask, val;
286 int i;
288 for (i = 0, mask = 0; i < length; ++i)
289 mask |= (1UL << i);
291 mask = mask << (offset - length + 1);
293 val = instr & mask;
295 val = val >> (offset - length + 1);
297 return (val);
300 static const inst_t *
301 dis_get_overlay(dis_handle_t *dhp, const table_t *tp, uint32_t idx)
303 const inst_t *ip = &tp->tbl_inp[idx];
304 int i;
306 if (tp->tbl_ovp == NULL)
307 return (ip);
309 for (i = 0; tp->tbl_ovp[i].ov_idx != -1; ++i) {
310 if (tp->tbl_ovp[i].ov_idx != idx)
311 continue;
313 if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
314 continue;
316 ip = &tp->tbl_ovp[i].ov_inst;
317 break;
320 return (ip);
323 #if !defined(DIS_STANDALONE)
324 static void
325 do_binary(uint32_t instr)
327 (void) fprintf(stderr, "DISASM: ");
328 prt_binary(instr, 32);
329 (void) fprintf(stderr, "\n");
331 #endif /* DIS_STANDALONE */
333 static int
334 dis_sparc_supports_flags(int flags)
336 int archflags = flags & DIS_ARCH_MASK;
338 if (archflags == DIS_SPARC_V8 ||
339 (archflags & (DIS_SPARC_V9 | DIS_SPARC_V8)) == DIS_SPARC_V9)
340 return (1);
342 return (0);
345 const dis_arch_t dis_arch_sparc = {
346 .da_supports_flags = dis_sparc_supports_flags,
347 .da_handle_attach = dis_sparc_handle_attach,
348 .da_handle_detach = dis_sparc_handle_detach,
349 .da_disassemble = dis_sparc_disassemble,
350 .da_previnstr = dis_sparc_previnstr,
351 .da_min_instrlen = dis_sparc_min_instrlen,
352 .da_max_instrlen = dis_sparc_max_instrlen,
353 .da_instrlen = dis_sparc_instrlen