1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 
3 /*---------------------------------------------------------------*/
4 /*--- begin                                     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-2017
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 "s390_defs.h"        // S390_MAX_MNEMONIC_LEN
38 #include "s390_disasm.h"
39 
40 
41 /* Return the mnemonic padded with blanks to its right */
42 static const HChar *
mnemonic(const HChar * mnm)43 mnemonic(const HChar *mnm)
44 {
45    vassert(vex_strlen(mnm) <= S390_MAX_MNEMONIC_LEN);
46 
47    static HChar buf[S390_MAX_MNEMONIC_LEN + 1];
48 
49    vex_sprintf(buf, "%-*s", S390_MAX_MNEMONIC_LEN, mnm);
50 
51    return buf;
52 }
53 
54 
55 /* Return the name of a general purpose register for dis-assembly purposes. */
56 static const HChar *
gpr_operand(UInt archreg)57 gpr_operand(UInt archreg)
58 {
59    static const HChar names[16][5] = {
60       "%r0", "%r1", "%r2", "%r3",
61       "%r4", "%r5", "%r6", "%r7",
62       "%r8", "%r9", "%r10", "%r11",
63       "%r12", "%r13", "%r14", "%r15",
64    };
65 
66    vassert(archreg < 16);
67 
68    return names[archreg];
69 }
70 
71 
72 /* Return the name of a floating point register for dis-assembly purposes. */
73 static const HChar *
fpr_operand(UInt archreg)74 fpr_operand(UInt archreg)
75 {
76    static const HChar names[16][5] = {
77       "%f0", "%f1", "%f2", "%f3",
78       "%f4", "%f5", "%f6", "%f7",
79       "%f8", "%f9", "%f10", "%f11",
80       "%f12", "%f13", "%f14", "%f15",
81    };
82 
83    vassert(archreg < 16);
84 
85    return names[archreg];
86 }
87 
88 
89 /* Return the name of an access register for dis-assembly purposes. */
90 static const HChar *
ar_operand(UInt archreg)91 ar_operand(UInt archreg)
92 {
93    static const HChar names[16][5] = {
94       "%a0", "%a1", "%a2", "%a3",
95       "%a4", "%a5", "%a6", "%a7",
96       "%a8", "%a9", "%a10", "%a11",
97       "%a12", "%a13", "%a14", "%a15",
98    };
99 
100    vassert(archreg < 16);
101 
102    return names[archreg];
103 }
104 
105 
106 /* Build and return the extended mnemonic for the compare and branch
107    opcodes as introduced by z10. See also the opcodes in file
108    opcodes/s390-opc.txt (from binutils) that have a '$' in their name. */
109 static const HChar *
cab_operand(const HChar * base,UInt mask)110 cab_operand(const HChar *base, UInt mask)
111 {
112    HChar *to;
113    const HChar *from;
114 
115    static HChar buf[S390_MAX_MNEMONIC_LEN + 1];
116 
117    static const HChar suffix[8][3] = {
118       "", "h", "l", "ne", "e", "nl", "nh", ""
119    };
120 
121    /* Guard against buffer overflow */
122    vassert(vex_strlen(base) + sizeof suffix[0] <= sizeof buf);
123 
124    /* strcpy(buf, from); */
125    for (from = base, to = buf; *from; ++from, ++to) {
126       *to = *from;
127    }
128    /* strcat(buf, suffix); */
129    for (from = suffix[mask >> 1]; *from; ++from, ++to) {
130       *to = *from;
131    }
132    *to = '\0';
133 
134    return buf;
135 }
136 
137 
138 /* Return the name of a vector register for dis-assembly purposes. */
139 static const HChar *
vr_operand(UInt archreg)140 vr_operand(UInt archreg)
141 {
142    static const HChar names[32][5] = {
143       "%v0", "%v1", "%v2", "%v3",
144       "%v4", "%v5", "%v6", "%v7",
145       "%v8", "%v9", "%v10", "%v11",
146       "%v16", "%v17", "%v18", "%v19",
147       "%v20", "%v21", "%v22", "%v23",
148       "%v24", "%v25", "%v26", "%v27",
149       "%v28", "%v29", "%v30", "%v31",
150    };
151 
152    vassert(archreg < 32);
153 
154    return names[archreg];
155 }
156 
157 
158 /* Common function used to construct a mnemonic based on a condition code
159    mask. */
160 static const HChar *
construct_mnemonic(const HChar * prefix,const HChar * suffix,UInt mask)161 construct_mnemonic(const HChar *prefix, const HChar *suffix, UInt mask)
162 {
163    HChar *to;
164    const HChar *from;
165 
166    static HChar buf[S390_MAX_MNEMONIC_LEN + 1];
167 
168    static HChar mask_id[16][4] = {
169       "", /* 0 -> unused */
170       "o", "h", "nle", "l", "nhe", "lh", "ne",
171       "e", "nlh", "he", "nl", "le", "nh", "no",
172       ""  /* 15 -> unused */
173    };
174 
175    /* Guard against buffer overflow */
176    vassert(vex_strlen(prefix) + vex_strlen(suffix) +
177            sizeof mask_id[0] <= sizeof buf);
178 
179    /* strcpy(buf, prefix); */
180    for (from = prefix, to = buf; *from; ++from, ++to) {
181       *to = *from;
182    }
183    /* strcat(buf, mask_id); */
184    for (from = mask_id[mask]; *from; ++from, ++to) {
185       *to = *from;
186    }
187    /* strcat(buf, suffix); */
188    for (from = suffix; *from; ++from, ++to) {
189       *to = *from;
190    }
191    *to = '\0';
192 
193    return buf;
194 }
195 
196 
197 /* Return the special mnemonic for the BCR opcode */
198 static const HChar *
bcr_operand(UInt m1)199 bcr_operand(UInt m1)
200 {
201    if (m1 ==  0) return "nopr";
202    if (m1 == 15) return "br";
203 
204    return construct_mnemonic("b", "r", m1);
205 }
206 
207 
208 /* Return the special mnemonic for the BC opcode */
209 static const HChar *
bc_operand(UInt m1)210 bc_operand(UInt m1)
211 {
212    if (m1 ==  0) return "nop";
213    if (m1 == 15) return "b";
214 
215    return construct_mnemonic("b", "", m1);
216 }
217 
218 
219 /* Return the special mnemonic for the BRC opcode */
220 static const HChar *
brc_operand(UInt m1)221 brc_operand(UInt m1)
222 {
223    if (m1 == 0)  return "brc";
224    if (m1 == 15) return "j";
225 
226    return construct_mnemonic("j", "", m1);
227 }
228 
229 
230 /* Return the special mnemonic for the BRCL opcode */
231 static const HChar *
brcl_operand(UInt m1)232 brcl_operand(UInt m1)
233 {
234    if (m1 == 0)  return "brcl";
235    if (m1 == 15) return "jg";
236 
237    return construct_mnemonic("jg", "", m1);
238 }
239 
240 
241 /* Return the special mnemonic for a conditional load/store  opcode */
242 static const HChar *
cls_operand(Int kind,UInt mask)243 cls_operand(Int kind, UInt mask)
244 {
245    const HChar *prefix;
246 
247    switch (kind) {
248    case S390_XMNM_LOCR:   prefix = "locr";  break;
249    case S390_XMNM_LOCGR:  prefix = "locgr"; break;
250    case S390_XMNM_LOC:    prefix = "loc";   break;
251    case S390_XMNM_LOCG:   prefix = "locg";  break;
252    case S390_XMNM_STOC:   prefix = "stoc";  break;
253    case S390_XMNM_STOCG:  prefix = "stocg"; break;
254    case S390_XMNM_STOCFH: prefix = "stocfh"; break;
255    case S390_XMNM_LOCFH:  prefix = "locgh"; break;
256    case S390_XMNM_LOCFHR: prefix = "locghr"; break;
257    case S390_XMNM_LOCHI:  prefix = "lochi"; break;
258    case S390_XMNM_LOCGHI: prefix = "locghi"; break;
259    case S390_XMNM_LOCHHI: prefix = "lochhi"; break;
260    default:
261       vpanic("cls_operand");
262    }
263 
264    return construct_mnemonic(prefix, "", mask);
265 }
266 
267 
268 /* An operand with a base register, an index register, and a displacement.
269    If the displacement is signed, the rightmost 20 bit of D need to be
270    sign extended */
271 static HChar *
dxb_operand(HChar * p,UInt d,UInt x,UInt b,Bool displacement_is_signed)272 dxb_operand(HChar *p, UInt d, UInt x, UInt b, Bool displacement_is_signed)
273 {
274    if (displacement_is_signed) {
275       Int displ = (Int)(d << 12) >> 12;  /* sign extend */
276 
277       p += vex_sprintf(p, "%d", displ);
278    } else {
279       p += vex_sprintf(p, "%u", d);
280    }
281    if (x != 0) {
282       p += vex_sprintf(p, "(%s", gpr_operand(x));
283       if (b != 0) {
284          p += vex_sprintf(p, ",%s", gpr_operand(b));
285       }
286       p += vex_sprintf(p, ")");
287    } else {
288       if (b != 0) {
289          p += vex_sprintf(p, "(%s)", gpr_operand(b));
290       }
291    }
292 
293    return p;
294 }
295 
296 
297 /* An operand with base register, unsigned length, and a 12-bit
298    unsigned displacement */
299 static HChar *
udlb_operand(HChar * p,UInt d,UInt length,UInt b)300 udlb_operand(HChar *p, UInt d, UInt length, UInt b)
301 {
302    p += vex_sprintf(p, "%u", d);
303    p += vex_sprintf(p, "(%u", length + 1);  // actual length is +1
304    if (b != 0) {
305       p += vex_sprintf(p, ",%s", gpr_operand(b));
306    }
307    p += vex_sprintf(p, ")");
308 
309    return p;
310 }
311 
312 
313 /* An operand with a base register, an vector register, and a displacement.
314    If the displacement is signed, the rightmost 20 bit of D need to be
315    sign extended */
316 static HChar *
dvb_operand(HChar * p,UInt d,UInt v,UInt b,Bool displacement_is_signed)317 dvb_operand(HChar *p, UInt d, UInt v, UInt b, Bool displacement_is_signed)
318 {
319    if (displacement_is_signed) {
320       Int displ = (Int)(d << 12) >> 12;  /* sign extend */
321 
322       p += vex_sprintf(p, "%d", displ);
323    } else {
324       p += vex_sprintf(p, "%u", d);
325    }
326    if (v != 0) {
327       p += vex_sprintf(p, "(%s", vr_operand(v));
328       if (b != 0) {
329          p += vex_sprintf(p, ",%s", gpr_operand(b));
330       }
331       p += vex_sprintf(p, ")");
332    } else {
333       if (b != 0) {
334          p += vex_sprintf(p, "(%s)", gpr_operand(b));
335       }
336    }
337 
338    return p;
339 }
340 
341 
342 /* The first argument is the command that says how to write the disassembled
343    insn. It is understood that the mnemonic comes first and that arguments
344    are separated by a ','. The command holds the arguments. Each argument is
345    encoded using a 4-bit S390_ARG_xyz value. The first argument is placed
346    in the least significant bits of the command and so on. There are at most
347    7 arguments in an insn and a sentinel (S390_ARG_DONE) is needed to identify
348    the end of the argument list. 8 * 4 = 32 bits are required for the
349    command. */
350 void
s390_disasm(UInt command,...)351 s390_disasm(UInt command, ...)
352 {
353    va_list  args;
354    UInt argkind;
355    HChar buf[128];  /* holds the disassembled insn */
356    HChar *p;
357    HChar separator;
358    Int mask_suffix = -1;
359 
360    va_start(args, command);
361 
362    p = buf;
363    separator = 0;
364 
365    while (42) {
366       argkind = command & 0xF;
367       command >>= 4;
368 
369       if (argkind == S390_ARG_DONE) goto done;
370 
371       if (argkind == S390_ARG_CABM) separator = 0;  /* optional */
372 
373       /* Write out the separator */
374       if (separator) *p++ = separator;
375 
376       /* argument */
377       switch (argkind) {
378       case S390_ARG_MNM:
379          p += vex_sprintf(p, "%s", mnemonic(va_arg(args, HChar *)));
380          separator = ' ';
381          continue;
382 
383       case S390_ARG_XMNM: {
384          UInt mask, kind;
385          const HChar *mnm;
386 
387          kind = va_arg(args, UInt);
388 
389          separator = ' ';
390          switch (kind) {
391          case S390_XMNM_BC:
392          case S390_XMNM_BCR:
393             mask = va_arg(args, UInt);
394             mnm = kind == S390_XMNM_BCR ? bcr_operand(mask) : bc_operand(mask);
395             p  += vex_sprintf(p, "%s", mnemonic(mnm));
396             /* mask == 0 is a NOP and has no argument */
397             if (mask == 0) goto done;
398             break;
399 
400          case S390_XMNM_BRC:
401          case S390_XMNM_BRCL:
402             mask = va_arg(args, UInt);
403             mnm = kind == S390_XMNM_BRC ? brc_operand(mask) : brcl_operand(mask);
404             p  += vex_sprintf(p, "%s", mnemonic(mnm));
405 
406             /* mask == 0 has no special mnemonic */
407             if (mask == 0) {
408                p += vex_sprintf(p, " 0");
409                separator = ',';
410             }
411             break;
412 
413          case S390_XMNM_CAB:
414             mnm  = va_arg(args, HChar *);
415             mask = va_arg(args, UInt);
416             p  += vex_sprintf(p, "%s", mnemonic(cab_operand(mnm, mask)));
417             break;
418 
419          case S390_XMNM_LOCR:
420          case S390_XMNM_LOCGR:
421          case S390_XMNM_LOC:
422          case S390_XMNM_LOCG:
423          case S390_XMNM_STOC:
424          case S390_XMNM_STOCG:
425          case S390_XMNM_STOCFH:
426          case S390_XMNM_LOCFH:
427          case S390_XMNM_LOCFHR:
428          case S390_XMNM_LOCHI:
429          case S390_XMNM_LOCGHI:
430          case S390_XMNM_LOCHHI:
431             mask = va_arg(args, UInt);
432             mnm = cls_operand(kind, mask);
433             p  += vex_sprintf(p, "%s", mnemonic(mnm));
434             /* There are no special opcodes when mask == 0 or 15. In that case
435                the integer mask is appended as the final operand */
436             if (mask == 0 || mask == 15) mask_suffix = mask;
437             break;
438          }
439       }
440       continue;
441 
442       case S390_ARG_GPR:
443          p += vex_sprintf(p, "%s", gpr_operand(va_arg(args, UInt)));
444          break;
445 
446       case S390_ARG_FPR:
447          p += vex_sprintf(p, "%s", fpr_operand(va_arg(args, UInt)));
448          break;
449 
450       case S390_ARG_AR:
451          p += vex_sprintf(p, "%s", ar_operand(va_arg(args, UInt)));
452          break;
453 
454       case S390_ARG_UINT:
455          p += vex_sprintf(p, "%u", va_arg(args, UInt));
456          break;
457 
458       case S390_ARG_INT:
459          p += vex_sprintf(p, "%d", (Int)(va_arg(args, UInt)));
460          break;
461 
462       case S390_ARG_PCREL: {
463          Long offset = va_arg(args, Int);
464 
465          /* Convert # halfwords to # bytes */
466          offset <<= 1;
467 
468          if (offset < 0) {
469             p += vex_sprintf(p, ".%lld", offset);
470          } else {
471             p += vex_sprintf(p, ".+%lld", offset);
472          }
473          break;
474       }
475 
476       case S390_ARG_SDXB: {
477          UInt dh, dl, x, b;
478 
479          dh = va_arg(args, UInt);
480          dl = va_arg(args, UInt);
481          x  = va_arg(args, UInt);
482          b  = va_arg(args, UInt);
483 
484          p = dxb_operand(p, (dh << 12) | dl, x, b, 1 /* signed_displacement */);
485          break;
486       }
487 
488       case S390_ARG_UDXB: {
489          UInt d, x, b;
490 
491          d = va_arg(args, UInt);
492          x = va_arg(args, UInt);
493          b = va_arg(args, UInt);
494 
495          p = dxb_operand(p, d, x, b, 0 /* signed_displacement */);
496          break;
497       }
498 
499       case S390_ARG_UDLB: {
500          UInt d, l, b;
501 
502          d = va_arg(args, UInt);
503          l = va_arg(args, UInt);
504          b = va_arg(args, UInt);
505 
506          p = udlb_operand(p, d, l, b);
507          break;
508       }
509 
510       case S390_ARG_CABM: {
511          UInt mask;
512 
513          mask = va_arg(args, UInt) & 0xE;
514          if (mask == 0 || mask == 14) {
515             p += vex_sprintf(p, ",%u", mask);
516          }
517          break;
518       }
519 
520       case S390_ARG_VR:
521          p += vex_sprintf(p, "%s", vr_operand(va_arg(args, UInt)));
522          break;
523 
524       case S390_ARG_UDVB: {
525          UInt d, v, b;
526 
527          d = va_arg(args, UInt);
528          v = va_arg(args, UInt);
529          b = va_arg(args, UInt);
530 
531          p = dvb_operand(p, d, v, b, 0 /* signed_displacement */);
532          break;
533          }
534       }
535 
536       separator = ',';
537    }
538 
539  done:
540    va_end(args);
541 
542    if (mask_suffix != -1)
543       p += vex_sprintf(p, ",%d", mask_suffix);
544    *p = '\0';
545 
546    vassert(p < buf + sizeof buf);  /* detect buffer overwrite */
547 
548    /* Finally, write out the disassembled insn */
549    vex_printf("%s\n", buf);
550 }
551 
552 /*---------------------------------------------------------------*/
553 /*--- end                                       s390_disasm.c ---*/
554 /*---------------------------------------------------------------*/
555