• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 
3 /*---------------------------------------------------------------*/
4 /*--- begin                                host_s390_disasm.c ---*/
5 /*---------------------------------------------------------------*/
6 
7 /*
8    This file is part of Valgrind, a dynamic binary instrumentation
9    framework.
10 
11    Copyright IBM Corp. 2010-2011
12 
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of the
16    License, or (at your option) any later version.
17 
18    This program is distributed in the hope that it will be useful, but
19    WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26    02110-1301, USA.
27 
28    The GNU General Public License is contained in the file COPYING.
29 */
30 
31 /* Contributed by Florian Krohm */
32 
33 #include <stdarg.h>
34 #include "libvex_basictypes.h"
35 #include "main_util.h"        // vassert
36 #include "main_globals.h"     // vex_traceflags
37 #include "host_s390_disasm.h"
38 
39 /* The format that is used to write out a mnemonic.
40    These should be declared as 'const HChar' but vex_printf needs
41    to be changed for that first */
42 static HChar s390_mnm_fmt[] = "%-8s";
43 
44 
45 /* Return the name of a general purpose register for dis-assembly purposes. */
46 static const HChar *
gpr_operand(UInt archreg)47 gpr_operand(UInt archreg)
48 {
49    static const HChar names[16][5] = {
50       "%r0", "%r1", "%r2", "%r3",
51       "%r4", "%r5", "%r6", "%r7",
52       "%r8", "%r9", "%r10", "%r11",
53       "%r12", "%r13", "%r14", "%r15",
54    };
55 
56    vassert(archreg < 16);
57 
58    return names[archreg];
59 }
60 
61 
62 /* Return the name of a floating point register for dis-assembly purposes. */
63 static const HChar *
fpr_operand(UInt archreg)64 fpr_operand(UInt archreg)
65 {
66    static const HChar names[16][5] = {
67       "%f0", "%f1", "%f2", "%f3",
68       "%f4", "%f5", "%f6", "%f7",
69       "%f8", "%f9", "%f10", "%f11",
70       "%f12", "%f13", "%f14", "%f15",
71    };
72 
73    vassert(archreg < 16);
74 
75    return names[archreg];
76 }
77 
78 
79 /* Return the name of an access register for dis-assembly purposes. */
80 static const HChar *
ar_operand(UInt archreg)81 ar_operand(UInt archreg)
82 {
83    static const HChar names[16][5] = {
84       "%a0", "%a1", "%a2", "%a3",
85       "%a4", "%a5", "%a6", "%a7",
86       "%a8", "%a9", "%a10", "%a11",
87       "%a12", "%a13", "%a14", "%a15",
88    };
89 
90    vassert(archreg < 16);
91 
92    return names[archreg];
93 }
94 
95 
96 /* Build and return the extended mnemonic for the compare and branch
97    opcodes as introduced by z10. See also the opcodes in file
98    opcodes/s390-opc.txt (from binutils) that have a '$' in their name. */
99 static const HChar *
cab_operand(const HChar * base,UInt mask)100 cab_operand(const HChar *base, UInt mask)
101 {
102    HChar *to;
103    const HChar *from;
104 
105    static HChar buf[10];   /* Maximum is 6 + 2 */
106 
107    static HChar *suffix[] = {
108       "", "h", "l", "ne", "e", "nl", "nh", ""
109    };
110 
111    /* strcpy(buf, from); */
112    for (from = base, to = buf; *from; ++from, ++to) {
113       *to = *from;
114    }
115    /* strcat(buf, suffix); */
116    for (from = suffix[mask >> 1]; *from; ++from, ++to) {
117       *to = *from;
118    }
119    *to = '\0';
120 
121    return buf;
122 }
123 
124 /* Common function used to construct a mnemonic based on a condition code
125    mask. */
126 static const HChar *
construct_mnemonic(const HChar * prefix,const HChar * suffix,UInt mask)127 construct_mnemonic(const HChar *prefix, const HChar *suffix, UInt mask)
128 {
129    HChar *to;
130    const HChar *from;
131 
132    static HChar buf[10];
133 
134    static HChar mask_id[16][4] = {
135       "", /* 0 -> unused */
136       "o", "h", "nle", "l", "nhe", "lh", "ne",
137       "e", "nlh", "he", "nl", "le", "nh", "no",
138       ""  /* 15 -> unused */
139    };
140 
141    /* Guard against buffer overflow */
142    vassert(vex_strlen(prefix) + vex_strlen(suffix) + sizeof mask_id[0] <= sizeof buf);
143 
144    /* strcpy(buf, prefix); */
145    for (from = prefix, to = buf; *from; ++from, ++to) {
146       *to = *from;
147    }
148    /* strcat(buf, mask_id); */
149    for (from = mask_id[mask]; *from; ++from, ++to) {
150       *to = *from;
151    }
152    /* strcat(buf, suffix); */
153    for (from = suffix; *from; ++from, ++to) {
154       *to = *from;
155    }
156    *to = '\0';
157 
158    return buf;
159 }
160 
161 
162 /* Return the special mnemonic for the BCR opcode */
163 static const HChar *
bcr_operand(UInt m1)164 bcr_operand(UInt m1)
165 {
166    if (m1 ==  0) return "nopr";
167    if (m1 == 15) return "br";
168 
169    return construct_mnemonic("b", "r", m1);
170 }
171 
172 
173 /* Return the special mnemonic for the BC opcode */
174 static const HChar *
bc_operand(UInt m1)175 bc_operand(UInt m1)
176 {
177    if (m1 ==  0) return "nop";
178    if (m1 == 15) return "b";
179 
180    return construct_mnemonic("b", "", m1);
181 }
182 
183 
184 /* Return the special mnemonic for the BRC opcode */
185 static const HChar *
brc_operand(UInt m1)186 brc_operand(UInt m1)
187 {
188    if (m1 == 0)  return "brc";
189    if (m1 == 15) return "j";
190 
191    return construct_mnemonic("j", "", m1);
192 }
193 
194 
195 /* Return the special mnemonic for the BRCL opcode */
196 static const HChar *
brcl_operand(UInt m1)197 brcl_operand(UInt m1)
198 {
199    if (m1 == 0)  return "brcl";
200    if (m1 == 15) return "jg";
201 
202    return construct_mnemonic("jg", "", m1);
203 }
204 
205 
206 /* Return the special mnemonic for a conditional load/store  opcode */
207 static const HChar *
cls_operand(Int kind,UInt mask)208 cls_operand(Int kind, UInt mask)
209 {
210    HChar *prefix;
211 
212    switch (kind) {
213    case S390_XMNM_LOCR:   prefix = "locr";  break;
214    case S390_XMNM_LOCGR:  prefix = "locgr"; break;
215    case S390_XMNM_LOC:    prefix = "loc";   break;
216    case S390_XMNM_LOCG:   prefix = "locg";  break;
217    case S390_XMNM_STOC:   prefix = "stoc";  break;
218    case S390_XMNM_STOCG:  prefix = "stocg"; break;
219    default:
220       vpanic("cls_operand");
221    }
222 
223    return construct_mnemonic(prefix, "", mask);
224 }
225 
226 
227 /* An operand with a base register, an index register, and a displacement.
228    If the displacement is signed, the rightmost 20 bit of D need to be
229    sign extended */
230 static HChar *
dxb_operand(HChar * p,UInt d,UInt x,UInt b,Bool displacement_is_signed)231 dxb_operand(HChar *p, UInt d, UInt x, UInt b, Bool displacement_is_signed)
232 {
233    if (displacement_is_signed) {
234       Int displ = ((Int)d << 12) >> 12;  /* sign extend */
235 
236       p += vex_sprintf(p, "%d", displ);
237    } else {
238       p += vex_sprintf(p, "%u", d);
239    }
240    if (x != 0) {
241       p += vex_sprintf(p, "(%s", gpr_operand(x));
242       if (b != 0) {
243          p += vex_sprintf(p, ",%s", gpr_operand(b));
244       }
245       p += vex_sprintf(p, ")");
246    } else {
247       if (b != 0) {
248          p += vex_sprintf(p, "(%s)", gpr_operand(b));
249       }
250    }
251 
252    return p;
253 }
254 
255 
256 /* An operand with base register, unsigned length, and a 12-bit
257    unsigned displacement */
258 static HChar *
udlb_operand(HChar * p,UInt d,UInt length,UInt b)259 udlb_operand(HChar *p, UInt d, UInt length, UInt b)
260 {
261    p += vex_sprintf(p, "%u", d);
262    p += vex_sprintf(p, "(%u", length + 1);  // actual length is +1
263    if (b != 0) {
264       p += vex_sprintf(p, ",%s", gpr_operand(b));
265    }
266    p += vex_sprintf(p, ")");
267 
268    return p;
269 }
270 
271 
272 /* The first argument is the command that says how to write the disassembled
273    insn. It is understood that the mnemonic comes first and that arguments
274    are separated by a ','. The command holds the arguments. Each argument is
275    encoded using a 4-bit S390_ARG_xyz value. The first argument is placed
276    in the least significant bits of the command and so on. There are at most
277    5 arguments in an insn and a sentinel (S390_ARG_DONE) is needed to identify
278    the end of the argument list. 6 * 4 = 24 bits are required for the
279    command. */
280 void
s390_disasm(UInt command,...)281 s390_disasm(UInt command, ...)
282 {
283    va_list  args;
284    unsigned argkind;
285    HChar buf[128];  /* holds the disassembled insn */
286    HChar *p;
287    HChar separator;
288    Int mask_suffix = -1;
289 
290    va_start(args, command);
291 
292    p = buf;
293    separator = 0;
294 
295    while (42) {
296       argkind = command & 0xF;
297       command >>= 4;
298 
299       if (argkind == S390_ARG_DONE) goto done;
300 
301       if (argkind == S390_ARG_CABM) separator = 0;  /* optional */
302 
303       /* Write out the separator */
304       if (separator) *p++ = separator;
305 
306       /* argument */
307       switch (argkind) {
308       case S390_ARG_MNM:
309          p += vex_sprintf(p, s390_mnm_fmt, va_arg(args, HChar *));
310          separator = ' ';
311          continue;
312 
313       case S390_ARG_XMNM: {
314          UInt mask, kind;
315          const HChar *mnm;
316 
317          kind = va_arg(args, UInt);
318 
319          separator = ' ';
320          switch (kind) {
321          case S390_XMNM_BC:
322          case S390_XMNM_BCR:
323             mask = va_arg(args, UInt);
324             mnm = kind == S390_XMNM_BCR ? bcr_operand(mask) : bc_operand(mask);
325             p  += vex_sprintf(p, s390_mnm_fmt, mnm);
326             /* mask == 0 is a NOP and has no argument */
327             if (mask == 0) goto done;
328             break;
329 
330          case S390_XMNM_BRC:
331          case S390_XMNM_BRCL:
332             mask = va_arg(args, UInt);
333             mnm = kind == S390_XMNM_BRC ? brc_operand(mask) : brcl_operand(mask);
334             p  += vex_sprintf(p, s390_mnm_fmt, mnm);
335 
336             /* mask == 0 has no special mnemonic */
337             if (mask == 0) {
338                p += vex_sprintf(p, " 0");
339                separator = ',';
340             }
341             break;
342 
343          case S390_XMNM_CAB:
344             mnm  = va_arg(args, HChar *);
345             mask = va_arg(args, UInt);
346             p  += vex_sprintf(p, s390_mnm_fmt, cab_operand(mnm, mask));
347             break;
348 
349          case S390_XMNM_LOCR:
350          case S390_XMNM_LOCGR:
351          case S390_XMNM_LOC:
352          case S390_XMNM_LOCG:
353          case S390_XMNM_STOC:
354          case S390_XMNM_STOCG:
355             mask = va_arg(args, UInt);
356             mnm = cls_operand(kind, mask);
357             p  += vex_sprintf(p, s390_mnm_fmt, mnm);
358             /* There are no special opcodes when mask == 0 or 15. In that case
359                the integer mask is appended as the final operand */
360             if (mask == 0 || mask == 15) mask_suffix = mask;
361             break;
362          }
363       }
364       continue;
365 
366       case S390_ARG_GPR:
367          p += vex_sprintf(p, "%s", gpr_operand(va_arg(args, UInt)));
368          break;
369 
370       case S390_ARG_FPR:
371          p += vex_sprintf(p, "%s", fpr_operand(va_arg(args, UInt)));
372          break;
373 
374       case S390_ARG_AR:
375          p += vex_sprintf(p, "%s", ar_operand(va_arg(args, UInt)));
376          break;
377 
378       case S390_ARG_UINT:
379          p += vex_sprintf(p, "%u", va_arg(args, UInt));
380          break;
381 
382       case S390_ARG_INT:
383          p += vex_sprintf(p, "%d", (Int)(va_arg(args, UInt)));
384          break;
385 
386       case S390_ARG_PCREL: {
387          Int offset = (Int)(va_arg(args, UInt));
388 
389          /* Convert # halfwords to # bytes */
390          offset <<= 1;
391 
392          if (offset < 0) {
393             p += vex_sprintf(p, ".%d", offset);
394          } else {
395             p += vex_sprintf(p, ".+%u", offset);
396          }
397          break;
398       }
399 
400       case S390_ARG_SDXB: {
401          UInt dh, dl, x, b;
402 
403          dh = va_arg(args, UInt);
404          dl = va_arg(args, UInt);
405          x  = va_arg(args, UInt);
406          b  = va_arg(args, UInt);
407 
408          p = dxb_operand(p, (dh << 12) | dl, x, b, 1 /* signed_displacement */);
409          break;
410       }
411 
412       case S390_ARG_UDXB: {
413          UInt d, x, b;
414 
415          d = va_arg(args, UInt);
416          x = va_arg(args, UInt);
417          b = va_arg(args, UInt);
418 
419          p = dxb_operand(p, d, x, b, 0 /* signed_displacement */);
420          break;
421       }
422 
423       case S390_ARG_UDLB: {
424          UInt d, l, b;
425 
426          d = va_arg(args, UInt);
427          l = va_arg(args, UInt);
428          b = va_arg(args, UInt);
429 
430          p = udlb_operand(p, d, l, b);
431          break;
432       }
433 
434       case S390_ARG_CABM: {
435          UInt mask;
436 
437          mask = va_arg(args, UInt) & 0xE;
438          if (mask == 0 || mask == 14) {
439             p += vex_sprintf(p, ",%u", mask);
440          }
441          break;
442       }
443       }
444 
445       separator = ',';
446    }
447 
448  done:
449    va_end(args);
450 
451    if (mask_suffix != -1)
452       p += vex_sprintf(p, ",%d", mask_suffix);
453    *p = '\0';
454 
455    vassert(p < buf + sizeof buf);  /* detect buffer overwrite */
456 
457    /* Finally, write out the disassembled insn */
458    vex_printf("%s\n", buf);
459 }
460 
461 /*---------------------------------------------------------------*/
462 /*--- end                                  host_s390_disasm.c ---*/
463 /*---------------------------------------------------------------*/
464