1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <inttypes.h>
31#include <string.h>
32
33struct disasm_table_entry_t
34{
35    uint32_t       mask;
36    uint32_t       value;
37    const char*    instr_template;
38};
39
40
41static disasm_table_entry_t disasm_table[] =
42{
43    {0xff000000, 0x91000000, "add <xd|sp>, <xn|sp>, #<imm1>, <shift1>"},
44    {0xff000000, 0xd1000000, "sub <xd|sp>, <xn|sp>, #<imm1>, <shift1>"},
45    {0xff200000, 0x8b000000, "add <xd>, <xn>, <xm>, <shift2> #<amt1>"},
46    {0xff200000, 0x0b000000, "add <wd>, <wn>, <wm>, <shift2> #<amt1>"},
47    {0xff200000, 0x4b000000, "sub <wd>, <wn>, <wm>, <shift2> #<amt1>"},
48    {0xff200000, 0x6b000000, "subs <wd>, <wn>, <wm>, <shift2> #<amt1>"},
49    {0xff200000, 0x0a000000, "and <wd>, <wn>, <wm>, <shift2> #<amt1>"},
50    {0xff200000, 0x2a000000, "orr <wd>, <wn>, <wm>, <shift2> #<amt1>"},
51    {0xff200000, 0x2a200000, "orn <wd>, <wn>, <wm>, <shift2> #<amt1>"},
52    {0xff800000, 0x72800000, "movk <wd>, #<imm2>, lsl #<shift3>"},
53    {0xff800000, 0x52800000, "movz <wd>, #<imm2>, lsl #<shift3>"},
54    {0xff800000, 0xd2800000, "movz <xd>, #<imm2>, lsl #<shift3>"},
55    {0xffe00c00, 0x1a800000, "csel <wd>, <wn>, <wm>, <cond1>"},
56    {0xffe00c00, 0x9a800000, "csel <xd>, <xn>, <xm>, <cond1>"},
57    {0xffe00c00, 0x5a800000, "csinv <wd>, <wn>, <wm>, <cond1>"},
58    {0xffe08000, 0x1b000000, "madd <wd>, <wn>, <wm>, <wa>"},
59    {0xffe08000, 0x9b200000, "smaddl <xd>, <wn>, <wm>, <xa>"},
60    {0xffe04c00, 0xb8604800, "ldr <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt2>]"},
61    {0xffe04c00, 0xb8204800, "str <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt2>]"},
62    {0xffe04c00, 0xf8604800, "ldr <xt>, [<xn|sp>, <r1><m1>, <ext1> #<amt3>]"},
63    {0xffe04c00, 0xf8204800, "str <xt>, [<xn|sp>, <r1><m1>, <ext1> #<amt3>]"},
64    {0xffe04c00, 0x38604800, "ldrb <wt>, [<xn|sp>, <r1><m1>, <ext1> <amt5>]"},
65    {0xffe04c00, 0x38204800, "strb <wt>, [<xn|sp>, <r1><m1>, <ext1> <amt5>]"},
66    {0xffe04c00, 0x78604800, "ldrh <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt6>]"},
67    {0xffe04c00, 0x78204800, "strh <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt6>]"},
68    {0xffe00c00, 0xb8400400, "ldr <wt>, [<xn|sp>], #<simm1>"},
69    {0xffe00c00, 0xb8000c00, "str <wt>, [<xn|sp>, #<simm1>]!"},
70    {0xffc00000, 0x13000000, "sbfm <wd>, <wn>, #<immr1>, #<imms1>"},
71    {0xffc00000, 0x53000000, "ubfm <wd>, <wn>, #<immr1>, #<imms1>"},
72    {0xffc00000, 0xd3400000, "ubfm <xd>, <xn>, #<immr1>, #<imms1>"},
73    {0xffe00000, 0x13800000, "extr <wd>, <wn>, <wm>, #<lsb1>"},
74    {0xff000000, 0x54000000, "b.<cond2> <label1>"},
75    {0xfffffc1f, 0xd65f0000, "ret <xn>"},
76    {0xffe00000, 0x8b200000, "add <xd|sp>, <xn|sp>, <r2><m1>, <ext2> #<amt4>"},
77    {0xffe00000, 0xcb200000, "sub <xd|sp>, <xn|sp>, <r2><m1>, <ext2> #<amt4>"}
78};
79
80static int32_t bits_signed(uint32_t instr, uint32_t msb, uint32_t lsb)
81{
82    int32_t value;
83    value   = ((int32_t)instr) << (31 - msb);
84    value >>= (31 - msb);
85    value >>= lsb;
86    return value;
87}
88static uint32_t bits_unsigned(uint32_t instr, uint32_t msb, uint32_t lsb)
89{
90    uint32_t width = msb - lsb + 1;
91    uint32_t mask  = (1 << width) - 1;
92    return ((instr >> lsb) & mask);
93}
94
95static void get_token(const char *instr, uint32_t index, char *token)
96{
97    uint32_t i, j;
98    for(i = index, j = 0; i < strlen(instr); ++i)
99    {
100        if(instr[index] == '<' && instr[i] == '>')
101        {
102            token[j++] = instr[i];
103            break;
104        }
105        else if(instr[index] != '<' && instr[i] == '<')
106        {
107            break;
108        }
109        else
110        {
111            token[j++] = instr[i];
112        }
113    }
114    token[j] = '\0';
115    return;
116}
117
118
119static const char * token_cc_table[] =
120{
121    "eq", "ne", "cs", "cc", "mi",
122    "pl", "vs", "vc", "hi", "ls",
123    "ge", "lt", "gt", "le", "al", "nv"
124};
125
126static void decode_rx_zr_token(uint32_t reg, const char *prefix, char *instr_part)
127{
128    if(reg == 31)
129        sprintf(instr_part, "%s%s", prefix, "zr");
130    else
131        sprintf(instr_part, "%s%d", prefix, reg);
132}
133
134static void decode_token(uint32_t code, char *token, char *instr_part)
135{
136    if(strcmp(token, "<imm1>") == 0)
137        sprintf(instr_part, "0x%x", bits_unsigned(code, 21,10));
138    else if(strcmp(token, "<imm2>") == 0)
139        sprintf(instr_part, "0x%x", bits_unsigned(code, 20,5));
140    else if(strcmp(token, "<shift1>") == 0)
141        sprintf(instr_part, "lsl #%d", bits_unsigned(code, 23,22) * 12);
142    else if(strcmp(token, "<shift2>") == 0)
143    {
144        static const char * shift2_table[] = { "lsl", "lsr", "asr", "ror"};
145        sprintf(instr_part, "%s", shift2_table[bits_unsigned(code, 23,22)]);
146    }
147    else if(strcmp(token, "<shift3>") == 0)
148        sprintf(instr_part, "%d", bits_unsigned(code, 22,21) * 16);
149    else if(strcmp(token, "<amt1>") == 0)
150        sprintf(instr_part, "%d", bits_unsigned(code, 15,10));
151    else if(strcmp(token, "<amt2>") == 0)
152        sprintf(instr_part, "%d", bits_unsigned(code, 12,12) * 2);
153    else if(strcmp(token, "<amt3>") == 0)
154        sprintf(instr_part, "%d", bits_unsigned(code, 12,12) * 3);
155    else if(strcmp(token, "<amt4>") == 0)
156        sprintf(instr_part, "%d", bits_unsigned(code, 12,10));
157    else if(strcmp(token, "<amt5>") == 0)
158    {
159        static const char * amt5_table[] = {"", "#0"};
160        sprintf(instr_part, "%s", amt5_table[bits_unsigned(code, 12,12)]);
161    }
162    else if(strcmp(token, "<amt6>") == 0)
163        sprintf(instr_part, "%d", bits_unsigned(code, 12,12));
164    else if(strcmp(token, "<simm1>") == 0)
165        sprintf(instr_part, "%d", bits_signed(code, 20,12));
166    else if(strcmp(token, "<immr1>") == 0)
167        sprintf(instr_part, "%d", bits_unsigned(code, 21,16));
168    else if(strcmp(token, "<imms1>") == 0)
169        sprintf(instr_part, "%d", bits_unsigned(code, 15,10));
170    else if(strcmp(token, "<lsb1>") == 0)
171        sprintf(instr_part, "%d", bits_unsigned(code, 15,10));
172    else if(strcmp(token, "<cond1>") == 0)
173        sprintf(instr_part, "%s", token_cc_table[bits_unsigned(code, 15,12)]);
174    else if(strcmp(token, "<cond2>") == 0)
175        sprintf(instr_part, "%s", token_cc_table[bits_unsigned(code, 4,0)]);
176    else if(strcmp(token, "<r1>") == 0)
177    {
178        const char * token_r1_table[] =
179        {
180            "reserved", "reserved", "w", "x",
181            "reserved", "reserved", "w", "x"
182        };
183        sprintf(instr_part, "%s", token_r1_table[bits_unsigned(code, 15,13)]);
184    }
185    else if(strcmp(token, "<r2>") == 0)
186    {
187        static const char * token_r2_table[] =
188        {
189                "w","w","w", "x", "w", "w", "w", "x"
190        };
191        sprintf(instr_part, "%s", token_r2_table[bits_unsigned(code, 15,13)]);
192    }
193    else if(strcmp(token, "<m1>") == 0)
194    {
195        uint32_t reg = bits_unsigned(code, 20,16);
196        if(reg == 31)
197            sprintf(instr_part, "%s", "zr");
198        else
199            sprintf(instr_part, "%d", reg);
200    }
201    else if(strcmp(token, "<ext1>") == 0)
202    {
203        static const char * token_ext1_table[] =
204        {
205             "reserved","reserved","uxtw", "lsl",
206             "reserved","reserved", "sxtw", "sxtx"
207        };
208        sprintf(instr_part, "%s", token_ext1_table[bits_unsigned(code, 15,13)]);
209    }
210    else if(strcmp(token, "<ext2>") == 0)
211    {
212        static const char * token_ext2_table[] =
213        {
214                "uxtb","uxth","uxtw","uxtx",
215                "sxtb","sxth","sxtw","sxtx"
216        };
217        sprintf(instr_part, "%s", token_ext2_table[bits_unsigned(code, 15,13)]);
218    }
219    else if (strcmp(token, "<label1>") == 0)
220    {
221        int32_t offset = bits_signed(code, 23,5) * 4;
222        if(offset > 0)
223            sprintf(instr_part, "#.+%d", offset);
224        else
225            sprintf(instr_part, "#.-%d", -offset);
226    }
227    else if (strcmp(token, "<xn|sp>") == 0)
228    {
229        uint32_t reg = bits_unsigned(code, 9, 5);
230        if(reg == 31)
231            sprintf(instr_part, "%s", "sp");
232        else
233            sprintf(instr_part, "x%d", reg);
234    }
235    else if (strcmp(token, "<xd|sp>") == 0)
236    {
237        uint32_t reg = bits_unsigned(code, 4, 0);
238        if(reg == 31)
239            sprintf(instr_part, "%s", "sp");
240        else
241            sprintf(instr_part, "x%d", reg);
242    }
243    else if (strcmp(token, "<xn>") == 0)
244        decode_rx_zr_token(bits_unsigned(code, 9, 5), "x", instr_part);
245    else if (strcmp(token, "<xd>") == 0)
246        decode_rx_zr_token(bits_unsigned(code, 4, 0), "x", instr_part);
247    else if (strcmp(token, "<xm>") == 0)
248        decode_rx_zr_token(bits_unsigned(code, 20, 16), "x", instr_part);
249    else if (strcmp(token, "<xa>") == 0)
250        decode_rx_zr_token(bits_unsigned(code, 14, 10), "x", instr_part);
251    else if (strcmp(token, "<xt>") == 0)
252        decode_rx_zr_token(bits_unsigned(code, 4, 0), "x", instr_part);
253    else if (strcmp(token, "<wn>") == 0)
254        decode_rx_zr_token(bits_unsigned(code, 9, 5), "w", instr_part);
255    else if (strcmp(token, "<wd>") == 0)
256        decode_rx_zr_token(bits_unsigned(code, 4, 0), "w", instr_part);
257    else if (strcmp(token, "<wm>") == 0)
258        decode_rx_zr_token(bits_unsigned(code, 20, 16), "w", instr_part);
259    else if (strcmp(token, "<wa>") == 0)
260        decode_rx_zr_token(bits_unsigned(code, 14, 10), "w", instr_part);
261    else if (strcmp(token, "<wt>") == 0)
262        decode_rx_zr_token(bits_unsigned(code, 4, 0), "w", instr_part);
263    else
264    {
265        sprintf(instr_part, "error");
266    }
267    return;
268}
269
270int arm64_disassemble(uint32_t code, char* instr)
271{
272    uint32_t i;
273    char token[256];
274    char instr_part[256];
275
276    if(instr == NULL)
277        return -1;
278
279    bool matched = false;
280    disasm_table_entry_t *entry = NULL;
281    for(i = 0; i < sizeof(disasm_table)/sizeof(disasm_table_entry_t); ++i)
282    {
283        entry = &disasm_table[i];
284        if((code & entry->mask) == entry->value)
285        {
286            matched = true;
287            break;
288        }
289    }
290    if(matched == false)
291    {
292        strcpy(instr, "Unknown Instruction");
293        return -1;
294    }
295    else
296    {
297        uint32_t index = 0;
298        uint32_t length = strlen(entry->instr_template);
299        instr[0] = '\0';
300        do
301        {
302            get_token(entry->instr_template, index, token);
303            if(token[0] == '<')
304            {
305                decode_token(code, token, instr_part);
306                strcat(instr, instr_part);
307            }
308            else
309            {
310                strcat(instr, token);
311            }
312            index += strlen(token);
313        }while(index < length);
314        return 0;
315    }
316}
317