Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ARM A64 disassembly output wrapper to libvixl |
| 3 | * Copyright (c) 2013 Linaro Limited |
| 4 | * Written by Claudio Fontana |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Peter Maydell | e78490c | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Markus Armbruster | 3979fca | 2019-04-17 21:18:04 +0200 | [diff] [blame] | 21 | #include "disas/dis-asm.h" |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 22 | |
Peter Maydell | e78490c | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 23 | #include "vixl/a64/disasm-a64.h" |
| 24 | |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 25 | using namespace vixl; |
| 26 | |
| 27 | static Decoder *vixl_decoder = NULL; |
| 28 | static Disassembler *vixl_disasm = NULL; |
| 29 | |
| 30 | /* We don't use libvixl's PrintDisassembler because its output |
| 31 | * is a little unhelpful (trailing newlines, for example). |
| 32 | * Instead we use our own very similar variant so we have |
| 33 | * control over the format. |
| 34 | */ |
| 35 | class QEMUDisassembler : public Disassembler { |
| 36 | public: |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 37 | QEMUDisassembler() : printf_(NULL), stream_(NULL) { } |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 38 | ~QEMUDisassembler() { } |
| 39 | |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 40 | void SetStream(FILE *stream) { |
| 41 | stream_ = stream; |
| 42 | } |
| 43 | |
Stefan Weil | 57b7309 | 2015-07-21 11:18:45 +0100 | [diff] [blame] | 44 | void SetPrintf(fprintf_function printf_fn) { |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 45 | printf_ = printf_fn; |
| 46 | } |
| 47 | |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 48 | protected: |
Peter Maydell | 6aea44f | 2014-10-24 12:19:11 +0100 | [diff] [blame] | 49 | virtual void ProcessOutput(const Instruction *instr) { |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 50 | printf_(stream_, "%08" PRIx32 " %s", |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 51 | instr->InstructionBits(), GetOutput()); |
| 52 | } |
| 53 | |
| 54 | private: |
Stefan Weil | 57b7309 | 2015-07-21 11:18:45 +0100 | [diff] [blame] | 55 | fprintf_function printf_; |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 56 | FILE *stream_; |
| 57 | }; |
| 58 | |
| 59 | static int vixl_is_initialized(void) |
| 60 | { |
| 61 | return vixl_decoder != NULL; |
| 62 | } |
| 63 | |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 64 | static void vixl_init() { |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 65 | vixl_decoder = new Decoder(); |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 66 | vixl_disasm = new QEMUDisassembler(); |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 67 | vixl_decoder->AppendVisitor(vixl_disasm); |
| 68 | } |
| 69 | |
| 70 | #define INSN_SIZE 4 |
| 71 | |
| 72 | /* Disassemble ARM A64 instruction. This is our only entry |
| 73 | * point from QEMU's C code. |
| 74 | */ |
| 75 | int print_insn_arm_a64(uint64_t addr, disassemble_info *info) |
| 76 | { |
| 77 | uint8_t bytes[INSN_SIZE]; |
Peter Maydell | 8d298be | 2015-02-05 13:37:25 +0000 | [diff] [blame] | 78 | uint32_t instrval; |
| 79 | const Instruction *instr; |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 80 | int status; |
| 81 | |
| 82 | status = info->read_memory_func(addr, bytes, INSN_SIZE, info); |
| 83 | if (status != 0) { |
| 84 | info->memory_error_func(status, addr, info); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | if (!vixl_is_initialized()) { |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 89 | vixl_init(); |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Peter Crosthwaite | fb200d5 | 2015-06-23 20:57:34 -0700 | [diff] [blame] | 92 | ((QEMUDisassembler *)vixl_disasm)->SetPrintf(info->fprintf_func); |
| 93 | ((QEMUDisassembler *)vixl_disasm)->SetStream(info->stream); |
| 94 | |
Peter Maydell | 8d298be | 2015-02-05 13:37:25 +0000 | [diff] [blame] | 95 | instrval = bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24; |
| 96 | instr = reinterpret_cast<const Instruction *>(&instrval); |
| 97 | vixl_disasm->MapCodeAddress(addr, instr); |
| 98 | vixl_decoder->Decode(instr); |
Claudio Fontana | 999b53e | 2014-02-05 17:27:28 +0000 | [diff] [blame] | 99 | |
| 100 | return INSN_SIZE; |
| 101 | } |