brl_emu.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Emulation of the "brl" instruction for IA64 processors that
  4. * don't support it in hardware.
  5. * Author: Stephan Zeisset, Intel Corp. <Stephan.Zeisset@intel.com>
  6. *
  7. * 02/22/02 D. Mosberger Clear si_flgs, si_isr, and si_imm to avoid
  8. * leaking kernel bits.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/uaccess.h>
  13. #include <asm/processor.h>
  14. extern char ia64_set_b1, ia64_set_b2, ia64_set_b3, ia64_set_b4, ia64_set_b5;
  15. struct illegal_op_return {
  16. unsigned long fkt, arg1, arg2, arg3;
  17. };
  18. /*
  19. * The unimplemented bits of a virtual address must be set
  20. * to the value of the most significant implemented bit.
  21. * unimpl_va_mask includes all unimplemented bits and
  22. * the most significant implemented bit, so the result
  23. * of an and operation with the mask must be all 0's
  24. * or all 1's for the address to be valid.
  25. */
  26. #define unimplemented_virtual_address(va) ( \
  27. ((va) & local_cpu_data->unimpl_va_mask) != 0 && \
  28. ((va) & local_cpu_data->unimpl_va_mask) != local_cpu_data->unimpl_va_mask \
  29. )
  30. /*
  31. * The unimplemented bits of a physical address must be 0.
  32. * unimpl_pa_mask includes all unimplemented bits, so the result
  33. * of an and operation with the mask must be all 0's for the
  34. * address to be valid.
  35. */
  36. #define unimplemented_physical_address(pa) ( \
  37. ((pa) & local_cpu_data->unimpl_pa_mask) != 0 \
  38. )
  39. /*
  40. * Handle an illegal operation fault that was caused by an
  41. * unimplemented "brl" instruction.
  42. * If we are not successful (e.g because the illegal operation
  43. * wasn't caused by a "brl" after all), we return -1.
  44. * If we are successful, we return either 0 or the address
  45. * of a "fixup" function for manipulating preserved register
  46. * state.
  47. */
  48. struct illegal_op_return
  49. ia64_emulate_brl (struct pt_regs *regs, unsigned long ar_ec)
  50. {
  51. unsigned long bundle[2];
  52. unsigned long opcode, btype, qp, offset, cpl;
  53. unsigned long next_ip;
  54. struct siginfo siginfo;
  55. struct illegal_op_return rv;
  56. long tmp_taken, unimplemented_address;
  57. clear_siginfo(&siginfo);
  58. rv.fkt = (unsigned long) -1;
  59. /*
  60. * Decode the instruction bundle.
  61. */
  62. if (copy_from_user(bundle, (void *) (regs->cr_iip), sizeof(bundle)))
  63. return rv;
  64. next_ip = (unsigned long) regs->cr_iip + 16;
  65. /* "brl" must be in slot 2. */
  66. if (ia64_psr(regs)->ri != 1) return rv;
  67. /* Must be "mlx" template */
  68. if ((bundle[0] & 0x1e) != 0x4) return rv;
  69. opcode = (bundle[1] >> 60);
  70. btype = ((bundle[1] >> 29) & 0x7);
  71. qp = ((bundle[1] >> 23) & 0x3f);
  72. offset = ((bundle[1] & 0x0800000000000000L) << 4)
  73. | ((bundle[1] & 0x00fffff000000000L) >> 32)
  74. | ((bundle[1] & 0x00000000007fffffL) << 40)
  75. | ((bundle[0] & 0xffff000000000000L) >> 24);
  76. tmp_taken = regs->pr & (1L << qp);
  77. switch(opcode) {
  78. case 0xC:
  79. /*
  80. * Long Branch.
  81. */
  82. if (btype != 0) return rv;
  83. rv.fkt = 0;
  84. if (!(tmp_taken)) {
  85. /*
  86. * Qualifying predicate is 0.
  87. * Skip instruction.
  88. */
  89. regs->cr_iip = next_ip;
  90. ia64_psr(regs)->ri = 0;
  91. return rv;
  92. }
  93. break;
  94. case 0xD:
  95. /*
  96. * Long Call.
  97. */
  98. rv.fkt = 0;
  99. if (!(tmp_taken)) {
  100. /*
  101. * Qualifying predicate is 0.
  102. * Skip instruction.
  103. */
  104. regs->cr_iip = next_ip;
  105. ia64_psr(regs)->ri = 0;
  106. return rv;
  107. }
  108. /*
  109. * BR[btype] = IP+16
  110. */
  111. switch(btype) {
  112. case 0:
  113. regs->b0 = next_ip;
  114. break;
  115. case 1:
  116. rv.fkt = (unsigned long) &ia64_set_b1;
  117. break;
  118. case 2:
  119. rv.fkt = (unsigned long) &ia64_set_b2;
  120. break;
  121. case 3:
  122. rv.fkt = (unsigned long) &ia64_set_b3;
  123. break;
  124. case 4:
  125. rv.fkt = (unsigned long) &ia64_set_b4;
  126. break;
  127. case 5:
  128. rv.fkt = (unsigned long) &ia64_set_b5;
  129. break;
  130. case 6:
  131. regs->b6 = next_ip;
  132. break;
  133. case 7:
  134. regs->b7 = next_ip;
  135. break;
  136. }
  137. rv.arg1 = next_ip;
  138. /*
  139. * AR[PFS].pfm = CFM
  140. * AR[PFS].pec = AR[EC]
  141. * AR[PFS].ppl = PSR.cpl
  142. */
  143. cpl = ia64_psr(regs)->cpl;
  144. regs->ar_pfs = ((regs->cr_ifs & 0x3fffffffff)
  145. | (ar_ec << 52) | (cpl << 62));
  146. /*
  147. * CFM.sof -= CFM.sol
  148. * CFM.sol = 0
  149. * CFM.sor = 0
  150. * CFM.rrb.gr = 0
  151. * CFM.rrb.fr = 0
  152. * CFM.rrb.pr = 0
  153. */
  154. regs->cr_ifs = ((regs->cr_ifs & 0xffffffc00000007f)
  155. - ((regs->cr_ifs >> 7) & 0x7f));
  156. break;
  157. default:
  158. /*
  159. * Unknown opcode.
  160. */
  161. return rv;
  162. }
  163. regs->cr_iip += offset;
  164. ia64_psr(regs)->ri = 0;
  165. if (ia64_psr(regs)->it == 0)
  166. unimplemented_address = unimplemented_physical_address(regs->cr_iip);
  167. else
  168. unimplemented_address = unimplemented_virtual_address(regs->cr_iip);
  169. if (unimplemented_address) {
  170. /*
  171. * The target address contains unimplemented bits.
  172. */
  173. printk(KERN_DEBUG "Woah! Unimplemented Instruction Address Trap!\n");
  174. siginfo.si_signo = SIGILL;
  175. siginfo.si_errno = 0;
  176. siginfo.si_flags = 0;
  177. siginfo.si_isr = 0;
  178. siginfo.si_imm = 0;
  179. siginfo.si_code = ILL_BADIADDR;
  180. force_sig_info(SIGILL, &siginfo, current);
  181. } else if (ia64_psr(regs)->tb) {
  182. /*
  183. * Branch Tracing is enabled.
  184. * Force a taken branch signal.
  185. */
  186. siginfo.si_signo = SIGTRAP;
  187. siginfo.si_errno = 0;
  188. siginfo.si_code = TRAP_BRANCH;
  189. siginfo.si_flags = 0;
  190. siginfo.si_isr = 0;
  191. siginfo.si_addr = 0;
  192. siginfo.si_imm = 0;
  193. force_sig_info(SIGTRAP, &siginfo, current);
  194. } else if (ia64_psr(regs)->ss) {
  195. /*
  196. * Single Step is enabled.
  197. * Force a trace signal.
  198. */
  199. siginfo.si_signo = SIGTRAP;
  200. siginfo.si_errno = 0;
  201. siginfo.si_code = TRAP_TRACE;
  202. siginfo.si_flags = 0;
  203. siginfo.si_isr = 0;
  204. siginfo.si_addr = 0;
  205. siginfo.si_imm = 0;
  206. force_sig_info(SIGTRAP, &siginfo, current);
  207. }
  208. return rv;
  209. }