dis.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Disassemble s390 instructions.
  4. *
  5. * Copyright IBM Corp. 2007
  6. * Author(s): Martin Schwidefsky ([email protected]),
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/errno.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/timer.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/delay.h>
  19. #include <linux/export.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/reboot.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/kdebug.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/atomic.h>
  26. #include <asm/dis.h>
  27. #include <asm/io.h>
  28. #include <asm/cpcmd.h>
  29. #include <asm/lowcore.h>
  30. #include <asm/debug.h>
  31. #include <asm/irq.h>
  32. /* Type of operand */
  33. #define OPERAND_GPR 0x1 /* Operand printed as %rx */
  34. #define OPERAND_FPR 0x2 /* Operand printed as %fx */
  35. #define OPERAND_AR 0x4 /* Operand printed as %ax */
  36. #define OPERAND_CR 0x8 /* Operand printed as %cx */
  37. #define OPERAND_VR 0x10 /* Operand printed as %vx */
  38. #define OPERAND_DISP 0x20 /* Operand printed as displacement */
  39. #define OPERAND_BASE 0x40 /* Operand printed as base register */
  40. #define OPERAND_INDEX 0x80 /* Operand printed as index register */
  41. #define OPERAND_PCREL 0x100 /* Operand printed as pc-relative symbol */
  42. #define OPERAND_SIGNED 0x200 /* Operand printed as signed value */
  43. #define OPERAND_LENGTH 0x400 /* Operand printed as length (+1) */
  44. struct s390_operand {
  45. unsigned char bits; /* The number of bits in the operand. */
  46. unsigned char shift; /* The number of bits to shift. */
  47. unsigned short flags; /* One bit syntax flags. */
  48. };
  49. struct s390_insn {
  50. union {
  51. const char name[5];
  52. struct {
  53. unsigned char zero;
  54. unsigned int offset;
  55. } __packed;
  56. };
  57. unsigned char opfrag;
  58. unsigned char format;
  59. };
  60. struct s390_opcode_offset {
  61. unsigned char opcode;
  62. unsigned char mask;
  63. unsigned char byte;
  64. unsigned short offset;
  65. unsigned short count;
  66. } __packed;
  67. enum {
  68. UNUSED,
  69. A_8, /* Access reg. starting at position 8 */
  70. A_12, /* Access reg. starting at position 12 */
  71. A_24, /* Access reg. starting at position 24 */
  72. A_28, /* Access reg. starting at position 28 */
  73. B_16, /* Base register starting at position 16 */
  74. B_32, /* Base register starting at position 32 */
  75. C_8, /* Control reg. starting at position 8 */
  76. C_12, /* Control reg. starting at position 12 */
  77. D20_20, /* 20 bit displacement starting at 20 */
  78. D_20, /* Displacement starting at position 20 */
  79. D_36, /* Displacement starting at position 36 */
  80. F_8, /* FPR starting at position 8 */
  81. F_12, /* FPR starting at position 12 */
  82. F_16, /* FPR starting at position 16 */
  83. F_24, /* FPR starting at position 24 */
  84. F_28, /* FPR starting at position 28 */
  85. F_32, /* FPR starting at position 32 */
  86. I8_8, /* 8 bit signed value starting at 8 */
  87. I8_32, /* 8 bit signed value starting at 32 */
  88. I16_16, /* 16 bit signed value starting at 16 */
  89. I16_32, /* 16 bit signed value starting at 32 */
  90. I32_16, /* 32 bit signed value starting at 16 */
  91. J12_12, /* 12 bit PC relative offset at 12 */
  92. J16_16, /* 16 bit PC relative offset at 16 */
  93. J16_32, /* 16 bit PC relative offset at 32 */
  94. J24_24, /* 24 bit PC relative offset at 24 */
  95. J32_16, /* 32 bit PC relative offset at 16 */
  96. L4_8, /* 4 bit length starting at position 8 */
  97. L4_12, /* 4 bit length starting at position 12 */
  98. L8_8, /* 8 bit length starting at position 8 */
  99. R_8, /* GPR starting at position 8 */
  100. R_12, /* GPR starting at position 12 */
  101. R_16, /* GPR starting at position 16 */
  102. R_24, /* GPR starting at position 24 */
  103. R_28, /* GPR starting at position 28 */
  104. U4_8, /* 4 bit unsigned value starting at 8 */
  105. U4_12, /* 4 bit unsigned value starting at 12 */
  106. U4_16, /* 4 bit unsigned value starting at 16 */
  107. U4_20, /* 4 bit unsigned value starting at 20 */
  108. U4_24, /* 4 bit unsigned value starting at 24 */
  109. U4_28, /* 4 bit unsigned value starting at 28 */
  110. U4_32, /* 4 bit unsigned value starting at 32 */
  111. U4_36, /* 4 bit unsigned value starting at 36 */
  112. U8_8, /* 8 bit unsigned value starting at 8 */
  113. U8_16, /* 8 bit unsigned value starting at 16 */
  114. U8_24, /* 8 bit unsigned value starting at 24 */
  115. U8_28, /* 8 bit unsigned value starting at 28 */
  116. U8_32, /* 8 bit unsigned value starting at 32 */
  117. U12_16, /* 12 bit unsigned value starting at 16 */
  118. U16_16, /* 16 bit unsigned value starting at 16 */
  119. U16_32, /* 16 bit unsigned value starting at 32 */
  120. U32_16, /* 32 bit unsigned value starting at 16 */
  121. VX_12, /* Vector index register starting at position 12 */
  122. V_8, /* Vector reg. starting at position 8 */
  123. V_12, /* Vector reg. starting at position 12 */
  124. V_16, /* Vector reg. starting at position 16 */
  125. V_32, /* Vector reg. starting at position 32 */
  126. X_12, /* Index register starting at position 12 */
  127. };
  128. static const struct s390_operand operands[] = {
  129. [UNUSED] = { 0, 0, 0 },
  130. [A_8] = { 4, 8, OPERAND_AR },
  131. [A_12] = { 4, 12, OPERAND_AR },
  132. [A_24] = { 4, 24, OPERAND_AR },
  133. [A_28] = { 4, 28, OPERAND_AR },
  134. [B_16] = { 4, 16, OPERAND_BASE | OPERAND_GPR },
  135. [B_32] = { 4, 32, OPERAND_BASE | OPERAND_GPR },
  136. [C_8] = { 4, 8, OPERAND_CR },
  137. [C_12] = { 4, 12, OPERAND_CR },
  138. [D20_20] = { 20, 20, OPERAND_DISP | OPERAND_SIGNED },
  139. [D_20] = { 12, 20, OPERAND_DISP },
  140. [D_36] = { 12, 36, OPERAND_DISP },
  141. [F_8] = { 4, 8, OPERAND_FPR },
  142. [F_12] = { 4, 12, OPERAND_FPR },
  143. [F_16] = { 4, 16, OPERAND_FPR },
  144. [F_24] = { 4, 24, OPERAND_FPR },
  145. [F_28] = { 4, 28, OPERAND_FPR },
  146. [F_32] = { 4, 32, OPERAND_FPR },
  147. [I8_8] = { 8, 8, OPERAND_SIGNED },
  148. [I8_32] = { 8, 32, OPERAND_SIGNED },
  149. [I16_16] = { 16, 16, OPERAND_SIGNED },
  150. [I16_32] = { 16, 32, OPERAND_SIGNED },
  151. [I32_16] = { 32, 16, OPERAND_SIGNED },
  152. [J12_12] = { 12, 12, OPERAND_PCREL },
  153. [J16_16] = { 16, 16, OPERAND_PCREL },
  154. [J16_32] = { 16, 32, OPERAND_PCREL },
  155. [J24_24] = { 24, 24, OPERAND_PCREL },
  156. [J32_16] = { 32, 16, OPERAND_PCREL },
  157. [L4_8] = { 4, 8, OPERAND_LENGTH },
  158. [L4_12] = { 4, 12, OPERAND_LENGTH },
  159. [L8_8] = { 8, 8, OPERAND_LENGTH },
  160. [R_8] = { 4, 8, OPERAND_GPR },
  161. [R_12] = { 4, 12, OPERAND_GPR },
  162. [R_16] = { 4, 16, OPERAND_GPR },
  163. [R_24] = { 4, 24, OPERAND_GPR },
  164. [R_28] = { 4, 28, OPERAND_GPR },
  165. [U4_8] = { 4, 8, 0 },
  166. [U4_12] = { 4, 12, 0 },
  167. [U4_16] = { 4, 16, 0 },
  168. [U4_20] = { 4, 20, 0 },
  169. [U4_24] = { 4, 24, 0 },
  170. [U4_28] = { 4, 28, 0 },
  171. [U4_32] = { 4, 32, 0 },
  172. [U4_36] = { 4, 36, 0 },
  173. [U8_8] = { 8, 8, 0 },
  174. [U8_16] = { 8, 16, 0 },
  175. [U8_24] = { 8, 24, 0 },
  176. [U8_28] = { 8, 28, 0 },
  177. [U8_32] = { 8, 32, 0 },
  178. [U12_16] = { 12, 16, 0 },
  179. [U16_16] = { 16, 16, 0 },
  180. [U16_32] = { 16, 32, 0 },
  181. [U32_16] = { 32, 16, 0 },
  182. [VX_12] = { 4, 12, OPERAND_INDEX | OPERAND_VR },
  183. [V_8] = { 4, 8, OPERAND_VR },
  184. [V_12] = { 4, 12, OPERAND_VR },
  185. [V_16] = { 4, 16, OPERAND_VR },
  186. [V_32] = { 4, 32, OPERAND_VR },
  187. [X_12] = { 4, 12, OPERAND_INDEX | OPERAND_GPR },
  188. };
  189. static const unsigned char formats[][6] = {
  190. [INSTR_E] = { 0, 0, 0, 0, 0, 0 },
  191. [INSTR_IE_UU] = { U4_24, U4_28, 0, 0, 0, 0 },
  192. [INSTR_MII_UPP] = { U4_8, J12_12, J24_24 },
  193. [INSTR_RIE_R0IU] = { R_8, I16_16, U4_32, 0, 0, 0 },
  194. [INSTR_RIE_R0UU] = { R_8, U16_16, U4_32, 0, 0, 0 },
  195. [INSTR_RIE_RRI0] = { R_8, R_12, I16_16, 0, 0, 0 },
  196. [INSTR_RIE_RRP] = { R_8, R_12, J16_16, 0, 0, 0 },
  197. [INSTR_RIE_RRPU] = { R_8, R_12, U4_32, J16_16, 0, 0 },
  198. [INSTR_RIE_RRUUU] = { R_8, R_12, U8_16, U8_24, U8_32, 0 },
  199. [INSTR_RIE_RUI0] = { R_8, I16_16, U4_12, 0, 0, 0 },
  200. [INSTR_RIE_RUPI] = { R_8, I8_32, U4_12, J16_16, 0, 0 },
  201. [INSTR_RIE_RUPU] = { R_8, U8_32, U4_12, J16_16, 0, 0 },
  202. [INSTR_RIL_RI] = { R_8, I32_16, 0, 0, 0, 0 },
  203. [INSTR_RIL_RP] = { R_8, J32_16, 0, 0, 0, 0 },
  204. [INSTR_RIL_RU] = { R_8, U32_16, 0, 0, 0, 0 },
  205. [INSTR_RIL_UP] = { U4_8, J32_16, 0, 0, 0, 0 },
  206. [INSTR_RIS_RURDI] = { R_8, I8_32, U4_12, D_20, B_16, 0 },
  207. [INSTR_RIS_RURDU] = { R_8, U8_32, U4_12, D_20, B_16, 0 },
  208. [INSTR_RI_RI] = { R_8, I16_16, 0, 0, 0, 0 },
  209. [INSTR_RI_RP] = { R_8, J16_16, 0, 0, 0, 0 },
  210. [INSTR_RI_RU] = { R_8, U16_16, 0, 0, 0, 0 },
  211. [INSTR_RI_UP] = { U4_8, J16_16, 0, 0, 0, 0 },
  212. [INSTR_RRE_00] = { 0, 0, 0, 0, 0, 0 },
  213. [INSTR_RRE_AA] = { A_24, A_28, 0, 0, 0, 0 },
  214. [INSTR_RRE_AR] = { A_24, R_28, 0, 0, 0, 0 },
  215. [INSTR_RRE_F0] = { F_24, 0, 0, 0, 0, 0 },
  216. [INSTR_RRE_FF] = { F_24, F_28, 0, 0, 0, 0 },
  217. [INSTR_RRE_FR] = { F_24, R_28, 0, 0, 0, 0 },
  218. [INSTR_RRE_R0] = { R_24, 0, 0, 0, 0, 0 },
  219. [INSTR_RRE_RA] = { R_24, A_28, 0, 0, 0, 0 },
  220. [INSTR_RRE_RF] = { R_24, F_28, 0, 0, 0, 0 },
  221. [INSTR_RRE_RR] = { R_24, R_28, 0, 0, 0, 0 },
  222. [INSTR_RRF_0UFF] = { F_24, F_28, U4_20, 0, 0, 0 },
  223. [INSTR_RRF_0URF] = { R_24, F_28, U4_20, 0, 0, 0 },
  224. [INSTR_RRF_F0FF] = { F_16, F_24, F_28, 0, 0, 0 },
  225. [INSTR_RRF_F0FF2] = { F_24, F_16, F_28, 0, 0, 0 },
  226. [INSTR_RRF_F0FR] = { F_24, F_16, R_28, 0, 0, 0 },
  227. [INSTR_RRF_FFRU] = { F_24, F_16, R_28, U4_20, 0, 0 },
  228. [INSTR_RRF_FUFF] = { F_24, F_16, F_28, U4_20, 0, 0 },
  229. [INSTR_RRF_FUFF2] = { F_24, F_28, F_16, U4_20, 0, 0 },
  230. [INSTR_RRF_R0RR] = { R_24, R_16, R_28, 0, 0, 0 },
  231. [INSTR_RRF_R0RR2] = { R_24, R_28, R_16, 0, 0, 0 },
  232. [INSTR_RRF_RURR] = { R_24, R_28, R_16, U4_20, 0, 0 },
  233. [INSTR_RRF_RURR2] = { R_24, R_16, R_28, U4_20, 0, 0 },
  234. [INSTR_RRF_U0FF] = { F_24, U4_16, F_28, 0, 0, 0 },
  235. [INSTR_RRF_U0RF] = { R_24, U4_16, F_28, 0, 0, 0 },
  236. [INSTR_RRF_U0RR] = { R_24, R_28, U4_16, 0, 0, 0 },
  237. [INSTR_RRF_URR] = { R_24, R_28, U8_16, 0, 0, 0 },
  238. [INSTR_RRF_UUFF] = { F_24, U4_16, F_28, U4_20, 0, 0 },
  239. [INSTR_RRF_UUFR] = { F_24, U4_16, R_28, U4_20, 0, 0 },
  240. [INSTR_RRF_UURF] = { R_24, U4_16, F_28, U4_20, 0, 0 },
  241. [INSTR_RRS_RRRDU] = { R_8, R_12, U4_32, D_20, B_16 },
  242. [INSTR_RR_FF] = { F_8, F_12, 0, 0, 0, 0 },
  243. [INSTR_RR_R0] = { R_8, 0, 0, 0, 0, 0 },
  244. [INSTR_RR_RR] = { R_8, R_12, 0, 0, 0, 0 },
  245. [INSTR_RR_U0] = { U8_8, 0, 0, 0, 0, 0 },
  246. [INSTR_RR_UR] = { U4_8, R_12, 0, 0, 0, 0 },
  247. [INSTR_RSI_RRP] = { R_8, R_12, J16_16, 0, 0, 0 },
  248. [INSTR_RSL_LRDFU] = { F_32, D_20, L8_8, B_16, U4_36, 0 },
  249. [INSTR_RSL_R0RD] = { D_20, L4_8, B_16, 0, 0, 0 },
  250. [INSTR_RSY_AARD] = { A_8, A_12, D20_20, B_16, 0, 0 },
  251. [INSTR_RSY_CCRD] = { C_8, C_12, D20_20, B_16, 0, 0 },
  252. [INSTR_RSY_RDRU] = { R_8, D20_20, B_16, U4_12, 0, 0 },
  253. [INSTR_RSY_RRRD] = { R_8, R_12, D20_20, B_16, 0, 0 },
  254. [INSTR_RSY_RURD] = { R_8, U4_12, D20_20, B_16, 0, 0 },
  255. [INSTR_RSY_RURD2] = { R_8, D20_20, B_16, U4_12, 0, 0 },
  256. [INSTR_RS_AARD] = { A_8, A_12, D_20, B_16, 0, 0 },
  257. [INSTR_RS_CCRD] = { C_8, C_12, D_20, B_16, 0, 0 },
  258. [INSTR_RS_R0RD] = { R_8, D_20, B_16, 0, 0, 0 },
  259. [INSTR_RS_RRRD] = { R_8, R_12, D_20, B_16, 0, 0 },
  260. [INSTR_RS_RURD] = { R_8, U4_12, D_20, B_16, 0, 0 },
  261. [INSTR_RXE_FRRD] = { F_8, D_20, X_12, B_16, 0, 0 },
  262. [INSTR_RXE_RRRDU] = { R_8, D_20, X_12, B_16, U4_32, 0 },
  263. [INSTR_RXF_FRRDF] = { F_32, F_8, D_20, X_12, B_16, 0 },
  264. [INSTR_RXY_FRRD] = { F_8, D20_20, X_12, B_16, 0, 0 },
  265. [INSTR_RXY_RRRD] = { R_8, D20_20, X_12, B_16, 0, 0 },
  266. [INSTR_RXY_URRD] = { U4_8, D20_20, X_12, B_16, 0, 0 },
  267. [INSTR_RX_FRRD] = { F_8, D_20, X_12, B_16, 0, 0 },
  268. [INSTR_RX_RRRD] = { R_8, D_20, X_12, B_16, 0, 0 },
  269. [INSTR_RX_URRD] = { U4_8, D_20, X_12, B_16, 0, 0 },
  270. [INSTR_SIL_RDI] = { D_20, B_16, I16_32, 0, 0, 0 },
  271. [INSTR_SIL_RDU] = { D_20, B_16, U16_32, 0, 0, 0 },
  272. [INSTR_SIY_IRD] = { D20_20, B_16, I8_8, 0, 0, 0 },
  273. [INSTR_SIY_RD] = { D20_20, B_16, 0, 0, 0, 0 },
  274. [INSTR_SIY_URD] = { D20_20, B_16, U8_8, 0, 0, 0 },
  275. [INSTR_SI_RD] = { D_20, B_16, 0, 0, 0, 0 },
  276. [INSTR_SI_URD] = { D_20, B_16, U8_8, 0, 0, 0 },
  277. [INSTR_SMI_U0RDP] = { U4_8, J16_32, D_20, B_16, 0, 0 },
  278. [INSTR_SSE_RDRD] = { D_20, B_16, D_36, B_32, 0, 0 },
  279. [INSTR_SSF_RRDRD] = { D_20, B_16, D_36, B_32, R_8, 0 },
  280. [INSTR_SSF_RRDRD2] = { R_8, D_20, B_16, D_36, B_32, 0 },
  281. [INSTR_SS_L0RDRD] = { D_20, L8_8, B_16, D_36, B_32, 0 },
  282. [INSTR_SS_L2RDRD] = { D_20, B_16, D_36, L8_8, B_32, 0 },
  283. [INSTR_SS_LIRDRD] = { D_20, L4_8, B_16, D_36, B_32, U4_12 },
  284. [INSTR_SS_LLRDRD] = { D_20, L4_8, B_16, D_36, L4_12, B_32 },
  285. [INSTR_SS_RRRDRD] = { D_20, R_8, B_16, D_36, B_32, R_12 },
  286. [INSTR_SS_RRRDRD2] = { R_8, D_20, B_16, R_12, D_36, B_32 },
  287. [INSTR_SS_RRRDRD3] = { R_8, R_12, D_20, B_16, D_36, B_32 },
  288. [INSTR_S_00] = { 0, 0, 0, 0, 0, 0 },
  289. [INSTR_S_RD] = { D_20, B_16, 0, 0, 0, 0 },
  290. [INSTR_VRI_V0IU] = { V_8, I16_16, U4_32, 0, 0, 0 },
  291. [INSTR_VRI_V0U] = { V_8, U16_16, 0, 0, 0, 0 },
  292. [INSTR_VRI_V0UU2] = { V_8, U16_16, U4_32, 0, 0, 0 },
  293. [INSTR_VRI_V0UUU] = { V_8, U8_16, U8_24, U4_32, 0, 0 },
  294. [INSTR_VRI_VR0UU] = { V_8, R_12, U8_28, U4_24, 0, 0 },
  295. [INSTR_VRI_VVUU] = { V_8, V_12, U16_16, U4_32, 0, 0 },
  296. [INSTR_VRI_VVUUU] = { V_8, V_12, U12_16, U4_32, U4_28, 0 },
  297. [INSTR_VRI_VVUUU2] = { V_8, V_12, U8_28, U8_16, U4_24, 0 },
  298. [INSTR_VRI_VVV0U] = { V_8, V_12, V_16, U8_24, 0, 0 },
  299. [INSTR_VRI_VVV0UU] = { V_8, V_12, V_16, U8_24, U4_32, 0 },
  300. [INSTR_VRI_VVV0UU2] = { V_8, V_12, V_16, U8_28, U4_24, 0 },
  301. [INSTR_VRR_0V] = { V_12, 0, 0, 0, 0, 0 },
  302. [INSTR_VRR_0VV0U] = { V_12, V_16, U4_24, 0, 0, 0 },
  303. [INSTR_VRR_RV0UU] = { R_8, V_12, U4_24, U4_28, 0, 0 },
  304. [INSTR_VRR_VRR] = { V_8, R_12, R_16, 0, 0, 0 },
  305. [INSTR_VRR_VV] = { V_8, V_12, 0, 0, 0, 0 },
  306. [INSTR_VRR_VV0U] = { V_8, V_12, U4_32, 0, 0, 0 },
  307. [INSTR_VRR_VV0U0U] = { V_8, V_12, U4_32, U4_24, 0, 0 },
  308. [INSTR_VRR_VV0U2] = { V_8, V_12, U4_24, 0, 0, 0 },
  309. [INSTR_VRR_VV0UU2] = { V_8, V_12, U4_32, U4_28, 0, 0 },
  310. [INSTR_VRR_VV0UUU] = { V_8, V_12, U4_32, U4_28, U4_24, 0 },
  311. [INSTR_VRR_VVV] = { V_8, V_12, V_16, 0, 0, 0 },
  312. [INSTR_VRR_VVV0U] = { V_8, V_12, V_16, U4_32, 0, 0 },
  313. [INSTR_VRR_VVV0U0] = { V_8, V_12, V_16, U4_24, 0, 0 },
  314. [INSTR_VRR_VVV0U0U] = { V_8, V_12, V_16, U4_32, U4_24, 0 },
  315. [INSTR_VRR_VVV0UU] = { V_8, V_12, V_16, U4_32, U4_28, 0 },
  316. [INSTR_VRR_VVV0UUU] = { V_8, V_12, V_16, U4_32, U4_28, U4_24 },
  317. [INSTR_VRR_VVV0V] = { V_8, V_12, V_16, V_32, 0, 0 },
  318. [INSTR_VRR_VVVU0UV] = { V_8, V_12, V_16, V_32, U4_28, U4_20 },
  319. [INSTR_VRR_VVVU0V] = { V_8, V_12, V_16, V_32, U4_20, 0 },
  320. [INSTR_VRR_VVVUU0V] = { V_8, V_12, V_16, V_32, U4_20, U4_24 },
  321. [INSTR_VRS_RRDV] = { V_32, R_12, D_20, B_16, 0, 0 },
  322. [INSTR_VRS_RVRDU] = { R_8, V_12, D_20, B_16, U4_32, 0 },
  323. [INSTR_VRS_VRRD] = { V_8, R_12, D_20, B_16, 0, 0 },
  324. [INSTR_VRS_VRRDU] = { V_8, R_12, D_20, B_16, U4_32, 0 },
  325. [INSTR_VRS_VVRDU] = { V_8, V_12, D_20, B_16, U4_32, 0 },
  326. [INSTR_VRV_VVXRDU] = { V_8, D_20, VX_12, B_16, U4_32, 0 },
  327. [INSTR_VRX_VRRDU] = { V_8, D_20, X_12, B_16, U4_32, 0 },
  328. [INSTR_VRX_VV] = { V_8, V_12, 0, 0, 0, 0 },
  329. [INSTR_VSI_URDV] = { V_32, D_20, B_16, U8_8, 0, 0 },
  330. };
  331. static char long_insn_name[][7] = LONG_INSN_INITIALIZER;
  332. static struct s390_insn opcode[] = OPCODE_TABLE_INITIALIZER;
  333. static struct s390_opcode_offset opcode_offset[] = OPCODE_OFFSET_INITIALIZER;
  334. /* Extracts an operand value from an instruction. */
  335. static unsigned int extract_operand(unsigned char *code,
  336. const struct s390_operand *operand)
  337. {
  338. unsigned char *cp;
  339. unsigned int val;
  340. int bits;
  341. /* Extract fragments of the operand byte for byte. */
  342. cp = code + operand->shift / 8;
  343. bits = (operand->shift & 7) + operand->bits;
  344. val = 0;
  345. do {
  346. val <<= 8;
  347. val |= (unsigned int) *cp++;
  348. bits -= 8;
  349. } while (bits > 0);
  350. val >>= -bits;
  351. val &= ((1U << (operand->bits - 1)) << 1) - 1;
  352. /* Check for special long displacement case. */
  353. if (operand->bits == 20 && operand->shift == 20)
  354. val = (val & 0xff) << 12 | (val & 0xfff00) >> 8;
  355. /* Check for register extensions bits for vector registers. */
  356. if (operand->flags & OPERAND_VR) {
  357. if (operand->shift == 8)
  358. val |= (code[4] & 8) << 1;
  359. else if (operand->shift == 12)
  360. val |= (code[4] & 4) << 2;
  361. else if (operand->shift == 16)
  362. val |= (code[4] & 2) << 3;
  363. else if (operand->shift == 32)
  364. val |= (code[4] & 1) << 4;
  365. }
  366. /* Sign extend value if the operand is signed or pc relative. */
  367. if ((operand->flags & (OPERAND_SIGNED | OPERAND_PCREL)) &&
  368. (val & (1U << (operand->bits - 1))))
  369. val |= (-1U << (operand->bits - 1)) << 1;
  370. /* Double value if the operand is pc relative. */
  371. if (operand->flags & OPERAND_PCREL)
  372. val <<= 1;
  373. /* Length x in an instructions has real length x + 1. */
  374. if (operand->flags & OPERAND_LENGTH)
  375. val++;
  376. return val;
  377. }
  378. struct s390_insn *find_insn(unsigned char *code)
  379. {
  380. struct s390_opcode_offset *entry;
  381. struct s390_insn *insn;
  382. unsigned char opfrag;
  383. int i;
  384. /* Search the opcode offset table to find an entry which
  385. * matches the beginning of the opcode. If there is no match
  386. * the last entry will be used, which is the default entry for
  387. * unknown instructions as well as 1-byte opcode instructions.
  388. */
  389. for (i = 0; i < ARRAY_SIZE(opcode_offset); i++) {
  390. entry = &opcode_offset[i];
  391. if (entry->opcode == code[0])
  392. break;
  393. }
  394. opfrag = *(code + entry->byte) & entry->mask;
  395. insn = &opcode[entry->offset];
  396. for (i = 0; i < entry->count; i++) {
  397. if (insn->opfrag == opfrag)
  398. return insn;
  399. insn++;
  400. }
  401. return NULL;
  402. }
  403. static int print_insn(char *buffer, unsigned char *code, unsigned long addr)
  404. {
  405. struct s390_insn *insn;
  406. const unsigned char *ops;
  407. const struct s390_operand *operand;
  408. unsigned int value;
  409. char separator;
  410. char *ptr;
  411. int i;
  412. ptr = buffer;
  413. insn = find_insn(code);
  414. if (insn) {
  415. if (insn->zero == 0)
  416. ptr += sprintf(ptr, "%.7s\t",
  417. long_insn_name[insn->offset]);
  418. else
  419. ptr += sprintf(ptr, "%.5s\t", insn->name);
  420. /* Extract the operands. */
  421. separator = 0;
  422. for (ops = formats[insn->format], i = 0;
  423. *ops != 0 && i < 6; ops++, i++) {
  424. operand = operands + *ops;
  425. value = extract_operand(code, operand);
  426. if ((operand->flags & OPERAND_INDEX) && value == 0)
  427. continue;
  428. if ((operand->flags & OPERAND_BASE) &&
  429. value == 0 && separator == '(') {
  430. separator = ',';
  431. continue;
  432. }
  433. if (separator)
  434. ptr += sprintf(ptr, "%c", separator);
  435. if (operand->flags & OPERAND_GPR)
  436. ptr += sprintf(ptr, "%%r%i", value);
  437. else if (operand->flags & OPERAND_FPR)
  438. ptr += sprintf(ptr, "%%f%i", value);
  439. else if (operand->flags & OPERAND_AR)
  440. ptr += sprintf(ptr, "%%a%i", value);
  441. else if (operand->flags & OPERAND_CR)
  442. ptr += sprintf(ptr, "%%c%i", value);
  443. else if (operand->flags & OPERAND_VR)
  444. ptr += sprintf(ptr, "%%v%i", value);
  445. else if (operand->flags & OPERAND_PCREL) {
  446. void *pcrel = (void *)((int)value + addr);
  447. ptr += sprintf(ptr, "%px", pcrel);
  448. } else if (operand->flags & OPERAND_SIGNED)
  449. ptr += sprintf(ptr, "%i", value);
  450. else
  451. ptr += sprintf(ptr, "%u", value);
  452. if (operand->flags & OPERAND_DISP)
  453. separator = '(';
  454. else if (operand->flags & OPERAND_BASE) {
  455. ptr += sprintf(ptr, ")");
  456. separator = ',';
  457. } else
  458. separator = ',';
  459. }
  460. } else
  461. ptr += sprintf(ptr, "unknown");
  462. return (int) (ptr - buffer);
  463. }
  464. static int copy_from_regs(struct pt_regs *regs, void *dst, void *src, int len)
  465. {
  466. if (user_mode(regs)) {
  467. if (copy_from_user(dst, (char __user *)src, len))
  468. return -EFAULT;
  469. } else {
  470. if (copy_from_kernel_nofault(dst, src, len))
  471. return -EFAULT;
  472. }
  473. return 0;
  474. }
  475. void show_code(struct pt_regs *regs)
  476. {
  477. char *mode = user_mode(regs) ? "User" : "Krnl";
  478. unsigned char code[64];
  479. char buffer[128], *ptr;
  480. unsigned long addr;
  481. int start, end, opsize, hops, i;
  482. /* Get a snapshot of the 64 bytes surrounding the fault address. */
  483. for (start = 32; start && regs->psw.addr >= 34 - start; start -= 2) {
  484. addr = regs->psw.addr - 34 + start;
  485. if (copy_from_regs(regs, code + start - 2, (void *)addr, 2))
  486. break;
  487. }
  488. for (end = 32; end < 64; end += 2) {
  489. addr = regs->psw.addr + end - 32;
  490. if (copy_from_regs(regs, code + end, (void *)addr, 2))
  491. break;
  492. }
  493. /* Code snapshot useable ? */
  494. if ((regs->psw.addr & 1) || start >= end) {
  495. printk("%s Code: Bad PSW.\n", mode);
  496. return;
  497. }
  498. /* Find a starting point for the disassembly. */
  499. while (start < 32) {
  500. for (i = 0, hops = 0; start + i < 32 && hops < 3; hops++) {
  501. if (!find_insn(code + start + i))
  502. break;
  503. i += insn_length(code[start + i]);
  504. }
  505. if (start + i == 32)
  506. /* Looks good, sequence ends at PSW. */
  507. break;
  508. start += 2;
  509. }
  510. /* Decode the instructions. */
  511. ptr = buffer;
  512. ptr += sprintf(ptr, "%s Code:", mode);
  513. hops = 0;
  514. while (start < end && hops < 8) {
  515. opsize = insn_length(code[start]);
  516. if (start + opsize == 32)
  517. *ptr++ = '#';
  518. else if (start == 32)
  519. *ptr++ = '>';
  520. else
  521. *ptr++ = ' ';
  522. addr = regs->psw.addr + start - 32;
  523. ptr += sprintf(ptr, "%px: ", (void *)addr);
  524. if (start + opsize >= end)
  525. break;
  526. for (i = 0; i < opsize; i++)
  527. ptr += sprintf(ptr, "%02x", code[start + i]);
  528. *ptr++ = '\t';
  529. if (i < 6)
  530. *ptr++ = '\t';
  531. ptr += print_insn(ptr, code + start, addr);
  532. start += opsize;
  533. pr_cont("%s", buffer);
  534. ptr = buffer;
  535. ptr += sprintf(ptr, "\n ");
  536. hops++;
  537. }
  538. pr_cont("\n");
  539. }
  540. void print_fn_code(unsigned char *code, unsigned long len)
  541. {
  542. char buffer[128], *ptr;
  543. int opsize, i;
  544. while (len) {
  545. ptr = buffer;
  546. opsize = insn_length(*code);
  547. if (opsize > len)
  548. break;
  549. ptr += sprintf(ptr, "%px: ", code);
  550. for (i = 0; i < opsize; i++)
  551. ptr += sprintf(ptr, "%02x", code[i]);
  552. *ptr++ = '\t';
  553. if (i < 4)
  554. *ptr++ = '\t';
  555. ptr += print_insn(ptr, code, (unsigned long) code);
  556. *ptr++ = '\n';
  557. *ptr++ = 0;
  558. printk("%s", buffer);
  559. code += opsize;
  560. len -= opsize;
  561. }
  562. }