dis_sparc.c revision 702941cd97e30d600c2c1275823bc58231b2c361
9e2cd38c103ae52a41b09823a11c9b5c059555f0eh * CDDL HEADER START
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The contents of this file are subject to the terms of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Common Development and Distribution License (the "License").
bb5e3b2f129cc39517b925419c22f69a378ec023eh * You may not use this file except in compliance with the License.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
bb5e3b2f129cc39517b925419c22f69a378ec023eh * See the License for the specific language governing permissions
bb5e3b2f129cc39517b925419c22f69a378ec023eh * and limitations under the License.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * When distributing Covered Code, include this CDDL HEADER in each
bb5e3b2f129cc39517b925419c22f69a378ec023eh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If applicable, add the following below this CDDL HEADER, with the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * fields enclosed by brackets "[]" replaced with your own identifying
bb5e3b2f129cc39517b925419c22f69a378ec023eh * information: Portions Copyright [yyyy] [name of copyright owner]
bb5e3b2f129cc39517b925419c22f69a378ec023eh * CDDL HEADER END
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Use is subject to license terms.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Copyright 2007 Jason King. All rights reserved.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Use is subject to license terms.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The sparc disassembler is mostly straightforward, each instruction is
bb5e3b2f129cc39517b925419c22f69a378ec023eh * represented by an inst_t structure. The inst_t definitions are organized
bb5e3b2f129cc39517b925419c22f69a378ec023eh * into tables. The tables are correspond to the opcode maps documented in the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * various sparc architecture manuals. Each table defines the bit range of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instruction whose value act as an index into the array of instructions. A
bb5e3b2f129cc39517b925419c22f69a378ec023eh * table can also refer to another table if needed. Each table also contains
bb5e3b2f129cc39517b925419c22f69a378ec023eh * a function pointer of type format_fcn that knows how to output the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instructions in the table, as well as handle any synthetic instructions
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instructions, they sometimes renamed or just reused the same instruction to
bb5e3b2f129cc39517b925419c22f69a378ec023eh * do different operations (i.e. the sparcv8 coprocessor instructions). To
bb5e3b2f129cc39517b925419c22f69a378ec023eh * accommodate this, each table can define an overlay table. The overlay table
bb5e3b2f129cc39517b925419c22f69a378ec023eh * is a list of (table index, architecture, new instruction definition) values.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Traversal starts with the first table,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * get index value from the instruction
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * if an relevant overlay entry exists for this index,
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * grab the overlay definition
bb5e3b2f129cc39517b925419c22f69a378ec023eh * grab the definition from the array (corresponding to the index value)
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the entry is an instruction,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * call print function of instruction.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the entry is a pointer to another table
bb5e3b2f129cc39517b925419c22f69a378ec023eh * traverse the table
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If not valid,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * return an error
bb5e3b2f129cc39517b925419c22f69a378ec023eh * To keep dis happy, for sparc, instead of actually returning an error, if
bb5e3b2f129cc39517b925419c22f69a378ec023eh * the instruction cannot be disassembled, we instead merely place the value
bb5e3b2f129cc39517b925419c22f69a378ec023eh * of the instruction into the output buffer.
193974072f41a843678abf5f61979c748687e66bSherry Moore * Adding new instructions:
bb5e3b2f129cc39517b925419c22f69a378ec023eh * With the above information, it hopefully makes it clear how to add support
bb5e3b2f129cc39517b925419c22f69a378ec023eh * for decoding new instructions. Presumably, with new instructions will come
bb5e3b2f129cc39517b925419c22f69a378ec023eh * a new dissassembly mode (I.e. DIS_SPARC_V8, DIS_SPARC_V9, etc.).
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the dissassembled format does not correspond to one of the existing
bb5e3b2f129cc39517b925419c22f69a378ec023eh * formats, a new formatter will have to be written. The 'flags' value of
bb5e3b2f129cc39517b925419c22f69a378ec023eh * inst_t is intended to instruct the corresponding formatter about how to
bb5e3b2f129cc39517b925419c22f69a378ec023eh * output the instruction.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the corresponding entry in the correct table is currently unoccupied,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * simply replace the INVALID entry with the correct definition. The INST and
bb5e3b2f129cc39517b925419c22f69a378ec023eh * TABLE macros are suggested to be used for this. If there is already an
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instruction defined, then the entry must be placed in an overlay table. If
bb5e3b2f129cc39517b925419c22f69a378ec023eh * no overlay table exists for the instruction table, one will need to be
bb5e3b2f129cc39517b925419c22f69a378ec023eh * created.
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI)) == 0) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((dhp = dis_zalloc(sizeof (struct dis_handle))) == NULL) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* ARGSUSED */
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (4);
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The dis_i386.c comment for this says it returns the previous instruction,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * however, I'm fairly sure it's actually returning the _address_ of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * nth previous instruction.
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* ARGSUSED */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (n <= 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (pc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (pc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* ARGSUSED */
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (4);
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (-1);
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* this allows sparc code to be tested on x86 */
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* CONSTCOND */
bb5e3b2f129cc39517b925419c22f69a378ec023eh while (1) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (0);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ((dhp->dh_flags & DIS_OCTAL) != 0) ? "0%011lo" : "0x%08lx",
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (0);
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic const inst_t *
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_get_overlay(dis_handle_t *dhp, const table_t *tp, uint32_t idx)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (ip);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (ip);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic void
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */