changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > plan9front / sys/src/libmach/7db.c

changeset 7186: 4dd5e2428c1d
child: f49283fe4c5f
author: cinap_lenrek@felloff.net
date: Fri, 03 May 2019 21:00:17 +0200
permissions: -rw-r--r--
description: libmach: initial arm64 support
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <mach.h>
5 
6 typedef struct Opcode Opcode;
7 struct Opcode
8 {
9  char *p;
10  char *o;
11  char *a;
12 };
13 
14 typedef struct Instr Instr;
15 struct Instr
16 {
17  Opcode *op;
18  Map *map;
19  uvlong addr;
20  ulong w;
21 
22  char *curr; /* fill point in buffer */
23  char *end; /* end of buffer */
24 };
25 
26 static void format(char*, Instr*, char*);
27 static char FRAMENAME[] = ".frame";
28 
29 /*
30  * Arm64-specific debugger interface
31  */
32 static char* arm64excep(Map*, Rgetter);
33 static int arm64foll(Map*, uvlong, Rgetter, uvlong*);
34 static int arm64inst(Map*, uvlong, char, char*, int);
35 static int arm64das(Map*, uvlong, char*, int);
36 static int arm64instlen(Map*, uvlong);
37 
38 /*
39  * Debugger interface
40  */
41 Machdata arm64mach =
42 {
43  {0x00, 0x00, 0x20, 0xD4}, /* break point 0xD4200000 */
44  4, /* break point size */
45  leswab, /* short to local byte order */
46  leswal, /* long to local byte order */
47  leswav, /* long to local byte order */
48  risctrace, /* C traceback */
49  riscframe, /* Frame finder */
50  arm64excep, /* print exception */
51  0, /* breakpoint fixup */
52  0, /* single precision float printer */
53  0, /* double precision float printer */
54  arm64foll, /* following addresses */
55  arm64inst, /* print instruction */
56  arm64das, /* dissembler */
57  arm64instlen, /* instruction size */
58 };
59 
60 static Opcode opcodes[] =
61 {
62  "0AA10000AAAAAAAAAAAAAAAAAAAddddd", "ADR", "$%A,R%d",
63  "1PP10000PPPPPPPPPPPPPPPPPPPddddd", "ADRP", "$%P,R%d",
64  "00011000lllllllllllllllllllddddd", "MOVWU", "%l,R%d",
65  "01011000LLLLLLLLLLLLLLLLLLLddddd", "MOV", "%L,R%d",
66  "10011000lllllllllllllllllllddddd", "MOVW", "%l,R%d",
67  "11011000lllllllllllllllllllddddd", "PRFM", "%l,$%d",
68  "1111100100uuuuuuuuuuuu11111ddddd", "MOV", "R%d,%u(SP)",
69  "1111100100uuuuuuuuuuuunnnnnddddd", "MOV", "R%d,%u(R%n)",
70  "WW11100100uuuuuuuuuuuu11111ddddd", "MOV%WU", "R%d,%u(SP)",
71  "WW11100100uuuuuuuuuuuunnnnnddddd", "MOV%WU", "R%d,%u(R%n)",
72  "1111100101uuuuuuuuuuuu11111ddddd", "MOV", "%u(SP),R%d",
73  "1111100101uuuuuuuuuuuunnnnnddddd", "MOV", "%u(R%n),R%d",
74  "WW11100101uuuuuuuuuuuu11111ddddd", "MOV%WU", "%u(SP),R%d",
75  "WW11100101uuuuuuuuuuuunnnnnddddd", "MOV%WU", "%u(R%n),R%d",
76  "WW11100110uuuuuuuuuuuu11111ddddd", "MOV%W", "%u(SP),R%d",
77  "WW11100110uuuuuuuuuuuunnnnnddddd", "MOV%W", "%u(R%n),R%d",
78  "11111000000ooooooooo0011111ddddd", "MOV", "R%d,%o(SP)",
79  "11111000000ooooooooo00nnnnnddddd", "MOV", "R%d,%o(R%n)",
80  "WW111000000ooooooooo0011111ddddd", "MOV%W", "R%d,%o(SP)",
81  "WW111000000ooooooooo00nnnnnddddd", "MOV%W", "R%d,%o(R%n)",
82  "11111000010ooooooooo0011111ddddd", "MOV", "%o(SP),R%d",
83  "11111000010ooooooooo00nnnnnddddd", "MOV", "%o(R%n),R%d",
84  "WW111000010ooooooooo0011111ddddd", "MOV%WU", "%o(SP),R%d",
85  "WW111000010ooooooooo00nnnnnddddd", "MOV%WU", "%o(R%n),R%d",
86  "WW111000100ooooooooo0011111ddddd", "MOV%W", "%o(SP),R%d",
87  "WW111000100ooooooooo00nnnnnddddd", "MOV%W", "%o(R%n),R%d",
88  "11111000000ooooooooo0111111ddddd", "MOV", "R%d,(SP)%o!",
89  "WW111000000ooooooooo0111111ddddd", "MOV%WU", "R%d,(SP)%o!",
90  "WW111000000ooooooooo01nnnnnddddd", "MOV%WU", "R%d,(R%n)%o!",
91  "11111000000ooooooooo1111111ddddd", "MOV", "R%d,%o(SP)!",
92  "WW111000000ooooooooo1111111ddddd", "MOV%WU", "R%d,%o(SP)!",
93  "WW111000000ooooooooo11nnnnnddddd", "MOV%WU", "R%d,%o(R%n)!",
94  "11111000010ooooooooo0111111ddddd", "MOV", "(SP)%o!,R%d",
95  "11111000010ooooooooo01nnnnnddddd", "MOV", "(R%n)%o!,R%d",
96  "WW111000010ooooooooo0111111ddddd", "MOV%WU", "(SP)%o!,R%d",
97  "WW111000010ooooooooo01nnnnnddddd", "MOV%WU", "(R%n)%o!,R%d",
98  "WW111000100ooooooooo0111111ddddd", "MOV%W", "(SP)%o!,R%d",
99  "WW111000100ooooooooo01nnnnnddddd", "MOV%W", "(R%n)%o!,R%d",
100  "11111000010ooooooooo1111111ddddd", "MOV", "%o(SP)!,R%d",
101  "11111000010ooooooooo11nnnnnddddd", "MOV", "%o(R%n)!,R%d",
102  "WW111000010ooooooooo1111111ddddd", "MOV%WU", "%o(SP)!,R%d",
103  "WW111000010ooooooooo11nnnnnddddd", "MOV%WU", "%o(R%n)!,R%d",
104  "WW111000100ooooooooo1111111ddddd", "MOV%W", "%o(SP)!,R%d",
105  "WW111000100ooooooooo11nnnnnddddd", "MOV%W", "%o(R%n)!,R%d",
106  "11111000001mmmmmeeei10nnnnnddddd", "MOV", "R%d,(R%n)(R%m%e)",
107  "11111000111mmmmmeeei10nnnnnddddd", "MOV", "(R%n)(R%m%e),R%d",
108  "WW111000001mmmmmeeei10nnnnnddddd", "MOV%W", "R%d,(R%n)(R%m%e)",
109  "WW111000011mmmmmeeei10nnnnnddddd", "MOV%WU", "(R%n)(R%m%e),R%d",
110  "WW111000101mmmmmeeei10nnnnnddddd", "MOV%W", "(R%n)(R%m%e),R%d",
111  "WW111000111mmmmmeeei10nnnnnddddd", "MOV%WW", "(R%n)(R%m%e),R%d",
112  "W00100101ssKKKKKKKKKKKKKKKKddddd", "MOVN%W", "$%K,R%d",
113  "W10100101ssKKKKKKKKKKKKKKKKddddd", "MOVZ%W", "$%K,R%d",
114  "W11100101ssKKKKKKKKKKKKKKKKddddd", "MOVK%W", "$%K,R%d",
115  "W0010001--00000000000011111ddddd", "MOV%W", "SP,R%d",
116  "W0010001--000000000000nnnnn11111", "MOV%W", "R%n,SP",
117  "W0010001ssIIIIIIIIIIII1111111111", "ADD%W", "$%I,SP,SP",
118  "W0010001ssIIIIIIIIIIII11111ddddd", "ADD%W", "$%I,SP,R%d",
119  "W0010001ssIIIIIIIIIIIInnnnn11111", "ADD%W", "$%I,R%n,SP",
120  "W0110001ssIIIIIIIIIIII1111111111", "ADDS%W", "$%I,SP,SP",
121  "W0110001ssIIIIIIIIIIII11111ddddd", "ADDS%W", "$%I,SP,R%d",
122  "W0110001ssIIIIIIIIIIIInnnnn11111", "ADDS%W", "$%I,R%n,SP",
123  "W1010001ssIIIIIIIIIIII1111111111", "SUB%W", "$%I,SP,SP",
124  "W1010001ssIIIIIIIIIIII11111ddddd", "SUB%W", "$%I,SP,R%d",
125  "W1010001ssIIIIIIIIIIIInnnnn11111", "SUB%W", "$%I,R%n,SP",
126  "W1110001ssIIIIIIIIIIII1111111111", "CMP%W", "$%I,SP",
127  "W1110001ssIIIIIIIIIIIInnnnn11111", "CMP%W", "$%I,R%n",
128  "W1110001ssIIIIIIIIIIII11111ddddd", "SUBS%W", "$%I,SP,R%d",
129  "W0010001ssIIIIIIIIIIIInnnnnddddd", "ADD%W", "$%I,R%n,R%d",
130  "W0110001ssIIIIIIIIIIIInnnnnddddd", "ADDS%W", "$%I,R%n,R%d",
131  "W1010001ssIIIIIIIIIIIInnnnnddddd", "SUB%W", "$%I,R%n,R%d",
132  "W1110001ssIIIIIIIIIIIInnnnnddddd", "SUBS%W", "$%I,R%n,R%d",
133  "W00100100MMMMMMMMMMMMMnnnnn11111", "AND%W", "$%M,R%n,SP",
134  "W01100100MMMMMMMMMMMMMnnnnn11111", "ORR%W", "$%M,R%n,SP",
135  "W10100100MMMMMMMMMMMMMnnnnn11111", "EOR%W", "$%M,R%n,SP",
136  "W11100100MMMMMMMMMMMMMnnnnn11111", "ANDS%W", "$%M,R%n,SP",
137  "W00100100MMMMMMMMMMMMMnnnnnddddd", "AND%W", "$%M,R%n,R%d",
138  "W01100100MMMMMMMMMMMMMnnnnnddddd", "ORR%W", "$%M,R%n,R%d",
139  "W10100100MMMMMMMMMMMMMnnnnnddddd", "EOR%W", "$%M,R%n,R%d",
140  "W11100100MMMMMMMMMMMMMnnnnnddddd", "ANDS%W", "$%M,R%n,R%d",
141  "1001001101000000011111nnnnnddddd", "SXTW", "R%n,R%d",
142  "0101001100iiiiii011111nnnnnddddd", "LSRW", "$%i,R%n,R%d",
143  "1101001101iiiiii111111nnnnnddddd", "LSR", "$%i,R%n,R%d",
144  "W00100110-iiiiiijjjjjjnnnnnddddd", "SBFM%W", "$%i,$%j,R%n,R%d",
145  "W01100110-iiiiiijjjjjjnnnnnddddd", "BFM%W", "$%i,$%j,R%n,R%d",
146  "W10100110-iiiiiijjjjjjnnnnnddddd", "UBFM%W", "$%i,$%j,R%n,R%d",
147  "W1011010000mmmmm00000011111ddddd", "NGC%W", "R%m,R%d",
148  "W1111010000mmmmm00000011111ddddd", "NGCS%W", "R%m,R%d",
149  "W0011010000mmmmm000000nnnnnddddd", "ADC%W", "R%m,R%n,R%d",
150  "W0111010000mmmmm000000nnnnnddddd", "ADCS%W", "R%m,R%n,R%d",
151  "W1011010000mmmmm000000nnnnnddddd", "SBC%W", "R%m,R%n,R%d",
152  "W1111010000mmmmm000000nnnnnddddd", "SBCS%W", "R%m,R%n,R%d",
153  "W0101011ss0mmmmmiiiiiinnnnn11111", "CMN%W", "R%m%s,R%n",
154  "W1101011ss0mmmmmiiiiiinnnnn11111", "CMP%W", "R%m%s,R%n",
155  "W1001011ss0mmmmmiiiiii11111ddddd", "NEG%W", "R%m%s,R%d",
156  "W1101011ss0mmmmmiiiiii11111ddddd", "NEGS%W", "R%m%s,R%d",
157  "W0001011ss0mmmmmiiiiiinnnnnddddd", "ADD%W", "R%m%s,R%n,R%d",
158  "W0101011ss0mmmmmiiiiiinnnnnddddd", "ADDS%W", "R%m%s,R%n,R%d",
159  "W1001011ss0mmmmmiiiiiinnnnnddddd", "SUB%W", "R%m%s,R%n,R%d",
160  "W1101011ss0mmmmmiiiiiinnnnnddddd", "SUBS%W", "R%m%s,R%n,R%d",
161  "W0001011001mmmmmeeeiii1111111111", "ADD%W", "R%m%e,SP,SP",
162  "W0001011001mmmmmeeeiii11111ddddd", "ADD%W", "R%m%e,SP,R%d",
163  "W0001011001mmmmmeeeiiinnnnn11111", "ADD%W", "R%m%e,R%n,SP",
164  "W0101011001mmmmmeeeiii1111111111", "ADDS%W", "R%m%e,SP,SP",
165  "W0101011001mmmmmeeeiii11111ddddd", "ADDS%W", "R%m%e,SP,R%d",
166  "W0101011001mmmmmeeeiiinnnnn11111", "ADDS%W", "R%m%e,R%n,SP",
167  "W1001011001mmmmmeeeiii1111111111", "SUB%W", "R%m%e,SP,SP",
168  "W1001011001mmmmmeeeiii11111ddddd", "SUB%W", "R%m%e,SP,R%d",
169  "W1001011001mmmmmeeeiiinnnnn11111", "SUB%W", "R%m%e,R%n,SP",
170  "W1101011001mmmmmeeeiii1111111111", "SUBS%W", "R%m%e,SP,SP",
171  "W1101011001mmmmmeeeiii11111ddddd", "SUBS%W", "R%m%e,SP,R%d",
172  "W1101011001mmmmmeeeiiinnnnn11111", "SUBS%W", "R%m%e,R%n,SP",
173  "W0001011001mmmmmeeeiiinnnnnddddd", "ADD%W", "R%m%e,R%n,R%d",
174  "W0101011001mmmmmeeeiiinnnnnddddd", "ADDS%W", "R%m%e,R%n,R%d",
175  "W1001011001mmmmmeeeiiinnnnnddddd", "SUB%W", "R%m%e,R%n,R%d",
176  "W1101011001mmmmmeeeiiinnnnnddddd", "SUBS%W", "R%m%e,R%n,R%d",
177  "W0101010000mmmmm-0000011111ddddd", "MOV%W", "R%m,R%d",
178  "W0101010ss1mmmmmiiiiii11111ddddd", "NVM%W", "R%m%s,R%d",
179  "W1101010ss0mmmmmiiiiiinnnnn11111", "TST%W", "R%m%s,R%n",
180  "W0001010ss0mmmmmiiiiiinnnnnddddd", "AND%W", "R%m%s,R%n,R%d",
181  "W1101010ss0mmmmmiiiiiinnnnnddddd", "ANDS%W", "R%m%s,R%n,R%d",
182  "W0001010ss1mmmmmiiiiiinnnnnddddd", "BIC%W", "R%m%s,R%n,R%d",
183  "W1101010ss1mmmmmiiiiiinnnnnddddd", "BICS%W", "R%m%s,R%n,R%d",
184  "W1001010ss0mmmmmiiiiiinnnnnddddd", "EOR%W", "R%m%s,R%n,R%d",
185  "W1001010ss1mmmmmiiiiiinnnnnddddd", "EON%W", "R%m%s,R%n,R%d",
186  "W0101010ss0mmmmmiiiiiinnnnnddddd", "ORR%W", "R%m%s,R%n,R%d",
187  "W0101010ss1mmmmmiiiiiinnnnnddddd", "ORN%W", "R%m%s,R%n,R%d",
188  "W0011010110mmmmm001000nnnnnddddd", "LSL%W", "R%m,R%n,R%d",
189  "W0011010110mmmmm001001nnnnnddddd", "LSR%W", "R%m,R%n,R%d",
190  "W0011010110mmmmm001010nnnnnddddd", "ASR%W", "R%m,R%n,R%d",
191  "W0011010110mmmmm001011nnnnnddddd", "ROR%W", "R%m,R%n,R%d",
192  "W0011010110mmmmm000010nnnnnddddd", "UDIV%W", "R%m,R%n,R%d",
193  "W0011010110mmmmm000011nnnnnddddd", "SDIV%W", "R%m,R%n,R%d",
194  "W0011011000mmmmm011111nnnnnddddd", "MUL%W", "R%m,R%n,R%d",
195  "W0011011000mmmmm111111nnnnnddddd", "MNEG%W", "R%m,R%n,R%d",
196  "W0011011000mmmmm0aaaaannnnnddddd", "MADD%W", "R%m,R%n,R%a,R%d",
197  "W0011011000mmmmm1aaaaannnnnddddd", "MSUB%W", "R%m,R%n,R%a,R%d",
198  "10011011001mmmmm011111nnnnnddddd", "SMULL", "R%m,R%n,R%d",
199  "10011011001mmmmm111111nnnnnddddd", "SMNEGL", "R%m,R%n,R%d",
200  "10011011001mmmmm0aaaaannnnnddddd", "SMADDL", "R%m,R%n,R%a,R%d",
201  "10011011001mmmmm1aaaaannnnnddddd", "SMSUBL", "R%m,R%n,R%a,R%d",
202  "10011011101mmmmm011111nnnnnddddd", "UMULL", "R%m,R%n,R%d",
203  "10011011101mmmmm111111nnnnnddddd", "UMNEGL", "R%m,R%n,R%d",
204  "10011011101mmmmm0aaaaannnnnddddd", "UMADDL", "R%m,R%n,R%a,R%d",
205  "10011011101mmmmm1aaaaannnnnddddd", "UMSUBL", "R%m,R%n,R%a,R%d",
206  "W0110100TTTTTTTTTTTTTTTTTTTddddd", "CBZ%W", "R%d,%T",
207  "W0110101TTTTTTTTTTTTTTTTTTTddddd", "CBNZ%W", "R%d,%T",
208  "01010100TTTTTTTTTTTTTTTTTTT0CCCC", "B%C", "%T",
209  "000101TTTTTTTTTTTTTTTTTTTTTTTTTT", "B", "%T",
210  "100101TTTTTTTTTTTTTTTTTTTTTTTTTT", "BL", "%T",
211  "1101011000011111000000nnnnn00000", "BR", "R%n",
212  "1101011000111111000000nnnnn00000", "BLR", "R%n",
213  "11010110010111110000001111000000", "RETURN", nil,
214  "1101011001011111000000nnnnn00000", "RET", "R%n",
215  "11010110100111110000001111100000", "ERET", nil,
216  "11010110101111110000001111100000", "DRPS", nil,
217  "11010100000iiiiiiiiiiiiiiii00001", "SVC", "$%i",
218  "11010100000iiiiiiiiiiiiiiii00010", "HVC", "$%i",
219  "11010100000iiiiiiiiiiiiiiii00011", "SMC", "$%i",
220  "11010100001iiiiiiiiiiiiiiii00000", "BRK", "$%i",
221  "11010100010iiiiiiiiiiiiiiii00000", "HLT", "$%i",
222  "11010100101iiiiiiiiiiiiiiii00001", "DCPS1", "$%i",
223  "11010100101iiiiiiiiiiiiiiii00010", "DCPS2", "$%i",
224  "11010100101iiiiiiiiiiiiiiii00011", "DCPS3", "$%i",
225  "11010101000000110010000000011111", "NOP", nil,
226  "11010101000000110010000000111111", "YIELD", nil,
227  "11010101000000110010000001011111", "WFE", nil,
228  "11010101000000110010000001111111", "WFI", nil,
229  "11010101000000110010000010011111", "SEV", nil,
230  "11010101000000110010000010111111", "SEVL", nil,
231  "11010101000000110011xxxx01011111", "CLREX", "$%x",
232  "11010101000000110011xxxx10011111", "DSB", "$%x",
233  "11010101000000110011xxxx10111111", "DMB", "$%x",
234  "11010101000000110011xxxx11011111", "ISB", "$%x",
235  "1101010100001YYYYYYYYYYYYYY11111", "SYS", "%Y",
236  "1101010100001YYYYYYYYYYYYYYddddd", "SYS", "R%d,%Y",
237  "1101010100101YYYYYYYYYYYYYYddddd", "SYSL", "%Y,R%d",
238  "11010101000000000100xxxx10111111", "MSR", "$%x,SP",
239  "11010101000000110100xxxx11011111", "MSR", "$%x,DAIFSet",
240  "11010101000000110100xxxx11111111", "MSR", "$%x,DAIFClr",
241  "11010101000YYYYYYYYYYYYYYYYddddd", "MSR", "R%d,%Y",
242  "11010101001YYYYYYYYYYYYYYYYddddd", "MRS", "%Y,R%d",
243  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "WORD", "$%x",
244 };
245 
246 #define SYSARG5(op0,op1,Cn,Cm,op2) ((op0)<<19|(op1)<<16|(Cn)<<12|(Cm)<<8|(op2)<<5)
247 
248 static ulong
249 smask(char *s, char c)
250 {
251  ulong m;
252  int i;
253 
254  m = 0;
255  for(i=0; i<32 && *s != '\0'; i++, s++)
256  m |= (*s == c)<<(31-i);
257  return m;
258 }
259 
260 static int
261 nbits(ulong v)
262 {
263  int n = 0;
264  while(v != 0){
265  v &= v-1;
266  n++;
267  }
268  return n;
269 }
270 
271 static int
272 nones(ulong v)
273 {
274  int n = 0;
275  while(v & 1){
276  v >>= 1;
277  n++;
278  }
279  return n;
280 }
281 
282 static int
283 nshift(ulong m)
284 {
285  if(m == 0 || m == ~0UL)
286  return 0;
287  return nones(~m);
288 }
289 
290 static ulong
291 unshift(ulong w, ulong m)
292 {
293  int s = nshift(m);
294  w >>= s, m >>= s;
295  if((m+1 & m) != 0){ // 0bxxx0000yyyyyy -> 0byyyyyyxxx
296  ulong b = (1UL<<nones(m))-1;
297  return ((w & b) << nbits(m & ~b)) | unshift(w, m & ~b);
298  }
299  return w & m;
300 }
301 
302 static long
303 sext(ulong u, int n)
304 {
305  long l = (long)u;
306  if(n > 0){
307  l <<= sizeof(l)*8 - n;
308  l >>= sizeof(l)*8 - n;
309  }
310  return l;
311 }
312 
313 static char*
314 arm64excep(Map *, Rgetter)
315 {
316 // uvlong c = (*rget)(map, "TYPE");
317  return "???";
318 }
319 
320 static int
321 decode(Map *map, uvlong pc, Instr *i)
322 {
323  static ulong tab[2*nelem(opcodes)];
324  static int once;
325  ulong w, j;
326 
327  if(!once){
328  Opcode *o;
329 
330  /* precalculate value/mask table */
331  for(j=0, o=opcodes; j<nelem(tab); j+=2, o++){
332  tab[j] = smask(o->p, '1');
333  tab[j|1] = tab[j] | smask(o->p, '0');
334  }
335 
336  once = 1;
337  }
338 
339  if(get4(map, pc, &w) < 0) {
340  werrstr("can't read instruction: %r");
341  return -1;
342  }
343  i->addr = pc;
344  i->map = map;
345  i->w = w;
346 
347  for(j=0; j<nelem(tab); j+=2){
348  if((w & tab[j|1]) == tab[j]){
349  i->op = &opcodes[j/2];
350  return 1;
351  }
352  }
353 
354  /* should not happen */
355  return 0;
356 }
357 
358 #pragma varargck argpos bprint 2
359 
360 static void
361 bprint(Instr *i, char *fmt, ...)
362 {
363  va_list arg;
364 
365  va_start(arg, fmt);
366  i->curr = vseprint(i->curr, i->end, fmt, arg);
367  va_end(arg);
368 }
369 
370 static
371 char* shtype[4] =
372 {
373  "<<", ">>", "->", "@>"
374 };
375 
376 static
377 char* rextype[8] =
378 {
379  "UB", "UH", "UW", "UX",
380  "SB", "SH", "SW", "SX"
381 };
382 
383 static
384 char* scond[16] =
385 {
386  "EQ", "NE", "HS", "LO",
387  "MI", "PL", "VS", "VC",
388  "HI", "LS", "GE", "LT",
389  "GT", "LE", "", "NV",
390 };
391 
392 static uvlong
393 decodebitmask(int n, int s, int r)
394 {
395  uvlong w;
396  int e;
397 
398  if(n)
399  n = 6;
400  else {
401  for(n = 5; n >= 1 && ((~s & 0x3F) & (1<<n)) == 0; n--)
402  ;
403  }
404  e = 1 << n;
405  s &= e-1;
406  r &= e-1;
407 
408  w = 1ULL << s;
409  w |= w-1;
410 
411  if(r != 0){
412  w = (w >> r) | (w << (e-r));
413  if(e < 64)
414  w &= (1ULL << e)-1;
415  }
416  while(e < 64){
417  w |= w << e;
418  e <<= 1;
419  }
420  return w;
421 }
422 
423 static void
424 format(char *mnemonic, Instr *i, char *f)
425 {
426  Symbol s;
427  uvlong v;
428  ulong w, u, m;
429 
430  if(mnemonic)
431  format(0, i, mnemonic);
432  if(f == 0)
433  return;
434  if(mnemonic)
435  if(i->curr < i->end)
436  *i->curr++ = '\t';
437  for ( ; *f && i->curr < i->end; f++) {
438  if(*f != '%') {
439  *i->curr++ = *f;
440  continue;
441  }
442  m = smask(i->op->p, *++f);
443  u = unshift(i->w, m);
444  switch (*f) {
445  case 'C': // Condition
446  bprint(i, "%s", scond[u & 15]);
447  break;
448 
449  case 'W': // Width
450  if(nbits(m) == 1) u += 2;
451  u &= 3;
452  if(u < 3)
453  *i->curr++ = "BHW"[u];
454  break;
455 
456  case 'd': // Register Numbers
457  case 'n':
458  case 'a':
459  case 'm':
460  bprint(i, "%lud", u);
461  break;
462 
463  case 's': // Register shift
464  w = unshift(i->w, smask(i->op->p, 'i'));
465  if(w != 0)
466  bprint(i, "%s%lud", shtype[u & 3], w);
467  break;
468 
469  case 'e': // Register extension
470  u &= 7;
471  bprint(i, ".%s", rextype[u]);
472  w = unshift(i->w, smask(i->op->p, 'i'));
473  if(w != 0 && u == 2+(i->w>>31))
474  bprint(i, "<<%lud", w);
475  break;
476 
477  case 'M': // Bitmask
478  v = decodebitmask((u>>12)&1, u&0x3F, (u>>6)&0x3F);
479  if((i->w & (1<<31)) == 0)
480  v &= 0xFFFFFFFF;
481  bprint(i, "%llux", v);
482  break;
483 
484  case 'I': // Shifted Immediate (12 bit)
485  case 'K': // Shifted Immediate (16 bit)
486  w = unshift(i->w, smask(i->op->p, 's'));
487  if(u != 0 && w != 0)
488  bprint(i, "(%lux<<%ld)", u, w*(*f == 'I' ? 12 : 16));
489  else
490  bprint(i, "%lud", u);
491  break;
492 
493  case 'o': // Signed byte offset
494  bprint(i, "%ld", sext(u, nbits(m)));
495  break;
496  case 'u': // Unsigned offset
497  u <<= (i->w >> 30)&3;
498  /* wet floor */
499  case 'i': // Decimal
500  case 'j':
501  bprint(i, "%lud", u);
502  break;
503 
504  case 'x': // Hex
505  bprint(i, "%lux", u);
506  break;
507 
508  case 'l': // 32-bit Literal
509  if(get4(i->map, i->addr + sext(u, nbits(m))*4, &w) < 0)
510  goto Ptext;
511  bprint(i, "$%lux", w);
512  break;
513  case 'L': // 64-bit Literal
514  if(get8(i->map, i->addr + sext(u, nbits(m))*4, &v) < 0)
515  goto Ptext;
516  bprint(i, "$%llux", v);
517  break;
518  case 'T': // Text address (PC relative)
519  Ptext:
520  v = i->addr + sext(u, nbits(m))*4;
521  if(findsym(v, CTEXT, &s)){
522  bprint(i, "%s", s.name);
523  if(v < s.value)
524  bprint(i, "%llx", v - s.value);
525  else if(v > s.value)
526  bprint(i, "+%llx", v - s.value);
527  bprint(i, "(SB)");
528  break;
529  }
530  bprint(i, "%llux(SB)", v);
531  break;
532  case 'A': // Data address (PC relative)
533  v = i->addr + sext(u, nbits(m));
534  goto Pdata;
535  case 'P': // Page address (PC relative)
536  v = i->addr + ((vlong)sext(u, nbits(m)) << 12);
537  Pdata:
538  if(findsym(v, CANY, &s)){
539  bprint(i, "%s", s.name);
540  if(v < s.value)
541  bprint(i, "%llx", v - s.value);
542  else if(v > s.value)
543  bprint(i, "+%llx", v - s.value);
544  bprint(i, "(SB)");
545  break;
546  }
547  bprint(i, "%llux(SB)", v);
548  break;
549 
550  case 'Y':
551  if(nbits(m) == 14){ // SYS/SYSL operands
552  bprint(i, "%lud,%lud,%lud,%lud",
553  (u>>(4+4+3))&7, // op1
554  (u>>(4+3))&15, // CRn
555  (u>>3)&15, // CRm
556  (u)&7); // op2
557  break;
558  }
559  /* see /sys/src/cmd/7c/7.out.h */
560  switch(i->w & m){
561  case SYSARG5(3,3,4,2,1): bprint(i, "DAIF"); break;
562  case SYSARG5(3,3,4,2,0): bprint(i, "NZCV"); break;
563  case SYSARG5(3,3,4,4,1): bprint(i, "FPSR"); break;
564  case SYSARG5(3,3,4,4,0): bprint(i, "FPCR"); break;
565  case SYSARG5(3,0,4,0,0): bprint(i, "SPSR_EL1"); break;
566  case SYSARG5(3,0,4,0,1): bprint(i, "ELR_EL1"); break;
567  case SYSARG5(3,4,4,0,0): bprint(i, "SPSR_EL2"); break;
568  case SYSARG5(3,4,4,0,1): bprint(i, "ELR_EL2"); break;
569  case SYSARG5(3,0,4,2,2): bprint(i, "CurrentEL"); break;
570  case SYSARG5(3,0,4,1,0): bprint(i, "SP_EL0"); break;
571  case SYSARG5(3,0,4,2,0): bprint(i, "SPSel"); break;
572  default: bprint(i, "SPR(%lux)", i->w & m);
573  }
574  break;
575 
576  case '\0':
577  *i->curr++ = '%';
578  return;
579 
580  default:
581  bprint(i, "%%%c", *f);
582  break;
583  }
584  }
585  *i->curr = 0;
586 }
587 
588 static int
589 printins(Map *map, uvlong pc, char *buf, int n)
590 {
591  Instr i[1];
592 
593  i->curr = buf;
594  i->end = buf+n-1;
595  if(decode(map, pc, i) < 0)
596  return -1;
597  format(i->op->o, i, i->op->a);
598  return 4;
599 }
600 
601 static int
602 arm64inst(Map *map, uvlong pc, char modifier, char *buf, int n)
603 {
604  USED(modifier);
605  return printins(map, pc, buf, n);
606 }
607 
608 static int
609 arm64das(Map *map, uvlong pc, char *buf, int n)
610 {
611  Instr i[1];
612 
613  i->curr = buf;
614  i->end = buf+n;
615  if(decode(map, pc, i) < 0)
616  return -1;
617  if(i->end-i->curr > 8)
618  i->curr = _hexify(buf, i->w, 7);
619  *i->curr = 0;
620  return 4;
621 }
622 
623 static int
624 arm64instlen(Map*, uvlong)
625 {
626  return 4;
627 }
628 
629 static uvlong
630 readreg(Instr *i, Rgetter rget, int rc)
631 {
632  ulong m;
633  uvlong v;
634  char reg[4];
635  snprint(reg, sizeof(reg), "R%lud", unshift(i->w, smask(i->op->p, rc)));
636  v = (*rget)(i->map, reg);
637  m = smask(i->op->p, 'W');
638  if(m != 0 && unshift(i->w, m) == 0)
639  v &= 0xFFFFFFFFULL;
640  return v;
641 }
642 
643 static int
644 passcond(Instr *i, Rgetter rget)
645 {
646  uvlong psr;
647  uchar n;
648  uchar z;
649  uchar c;
650  uchar v;
651 
652  psr = (*rget)(i->map, "PSR");
653  n = (psr >> 31) & 1;
654  z = (psr >> 30) & 1;
655  c = (psr >> 29) & 1;
656  v = (psr >> 28) & 1;
657 
658  switch(unshift(i->w, smask(i->op->p, 'C'))) {
659  default:
660  case 0: return z;
661  case 1: return !z;
662  case 2: return c;
663  case 3: return !c;
664  case 4: return n;
665  case 5: return !n;
666  case 6: return v;
667  case 7: return !v;
668  case 8: return c && !z;
669  case 9: return !c || z;
670  case 10: return n == v;
671  case 11: return n != v;
672  case 12: return !z && (n == v);
673  case 13: return z || (n != v);
674  case 14: return 1;
675  case 15: return 0;
676  }
677 }
678 
679 static uvlong
680 jumptarg(Instr *i)
681 {
682  ulong m = smask(i->op->p, 'T');
683  return i->addr + sext(unshift(i->w, m), m)*4;
684 }
685 
686 static int
687 arm64foll(Map *map, uvlong pc, Rgetter rget, uvlong *foll)
688 {
689  Instr i[1];
690  char *o;
691 
692  if(decode(map, pc, i) < 0)
693  return -1;
694 
695  o = i->op->o;
696  if(strcmp(o, "ERET") == 0)
697  return -1;
698 
699  if(strcmp(o, "RET") == 0 || strcmp(o, "BR") == 0 || strcmp(o, "BLR") == 0){
700  foll[0] = readreg(i, rget, 'n');
701  return 1;
702  }
703  if(strcmp(o, "B") == 0 || strcmp(o, "BL") == 0){
704  foll[0] = jumptarg(i);
705  return 1;
706  }
707  if(strcmp(o, "B%C") == 0){
708  if(passcond(i, rget)){
709  foll[0] = jumptarg(i);
710  return 1;
711  }
712  }
713  if(strcmp(o, "CBZ%W") == 0){
714  if(readreg(i, rget, 'd') == 0){
715  foll[0] = jumptarg(i);
716  return 1;
717  }
718  }
719  if(strcmp(o, "CBNZ%W") == 0){
720  if(readreg(i, rget, 'd') != 0){
721  foll[0] = jumptarg(i);
722  return 1;
723  }
724  }
725 
726  foll[0] = i->addr+4;
727  return 1;
728 }