brl_emu.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. rv.fkt = (unsigned long) -1;
  58. /*
  59. * Decode the instruction bundle.
  60. */
  61. if (copy_from_user(bundle, (void *) (regs->cr_iip), sizeof(bundle)))
  62. return rv;
  63. next_ip = (unsigned long) regs->cr_iip + 16;
  64. /* "brl" must be in slot 2. */
  65. if (ia64_psr(regs)->ri != 1) return rv;
  66. /* Must be "mlx" template */
  67. if ((bundle[0] & 0x1e) != 0x4) return rv;
  68. opcode = (bundle[1] >> 60);
  69. btype = ((bundle[1] >> 29) & 0x7);
  70. qp = ((bundle[1] >> 23) & 0x3f);
  71. offset = ((bundle[1] & 0x0800000000000000L) << 4)
  72. | ((bundle[1] & 0x00fffff000000000L) >> 32)
  73. | ((bundle[1] & 0x00000000007fffffL) << 40)
  74. | ((bundle[0] & 0xffff000000000000L) >> 24);
  75. tmp_taken = regs->pr & (1L << qp);
  76. switch(opcode) {
  77. case 0xC:
  78. /*
  79. * Long Branch.
  80. */
  81. if (btype != 0) return rv;
  82. rv.fkt = 0;
  83. if (!(tmp_taken)) {
  84. /*
  85. * Qualifying predicate is 0.
  86. * Skip instruction.
  87. */
  88. regs->cr_iip = next_ip;
  89. ia64_psr(regs)->ri = 0;
  90. return rv;
  91. }
  92. break;
  93. case 0xD:
  94. /*
  95. * Long Call.
  96. */
  97. rv.fkt = 0;
  98. if (!(tmp_taken)) {
  99. /*
  100. * Qualifying predicate is 0.
  101. * Skip instruction.
  102. */
  103. regs->cr_iip = next_ip;
  104. ia64_psr(regs)->ri = 0;
  105. return rv;
  106. }
  107. /*
  108. * BR[btype] = IP+16
  109. */
  110. switch(btype) {
  111. case 0:
  112. regs->b0 = next_ip;
  113. break;
  114. case 1:
  115. rv.fkt = (unsigned long) &ia64_set_b1;
  116. break;
  117. case 2:
  118. rv.fkt = (unsigned long) &ia64_set_b2;
  119. break;
  120. case 3:
  121. rv.fkt = (unsigned long) &ia64_set_b3;
  122. break;
  123. case 4:
  124. rv.fkt = (unsigned long) &ia64_set_b4;
  125. break;
  126. case 5:
  127. rv.fkt = (unsigned long) &ia64_set_b5;
  128. break;
  129. case 6:
  130. regs->b6 = next_ip;
  131. break;
  132. case 7:
  133. regs->b7 = next_ip;
  134. break;
  135. }
  136. rv.arg1 = next_ip;
  137. /*
  138. * AR[PFS].pfm = CFM
  139. * AR[PFS].pec = AR[EC]
  140. * AR[PFS].ppl = PSR.cpl
  141. */
  142. cpl = ia64_psr(regs)->cpl;
  143. regs->ar_pfs = ((regs->cr_ifs & 0x3fffffffff)
  144. | (ar_ec << 52) | (cpl << 62));
  145. /*
  146. * CFM.sof -= CFM.sol
  147. * CFM.sol = 0
  148. * CFM.sor = 0
  149. * CFM.rrb.gr = 0
  150. * CFM.rrb.fr = 0
  151. * CFM.rrb.pr = 0
  152. */
  153. regs->cr_ifs = ((regs->cr_ifs & 0xffffffc00000007f)
  154. - ((regs->cr_ifs >> 7) & 0x7f));
  155. break;
  156. default:
  157. /*
  158. * Unknown opcode.
  159. */
  160. return rv;
  161. }
  162. regs->cr_iip += offset;
  163. ia64_psr(regs)->ri = 0;
  164. if (ia64_psr(regs)->it == 0)
  165. unimplemented_address = unimplemented_physical_address(regs->cr_iip);
  166. else
  167. unimplemented_address = unimplemented_virtual_address(regs->cr_iip);
  168. if (unimplemented_address) {
  169. /*
  170. * The target address contains unimplemented bits.
  171. */
  172. printk(KERN_DEBUG "Woah! Unimplemented Instruction Address Trap!\n");
  173. siginfo.si_signo = SIGILL;
  174. siginfo.si_errno = 0;
  175. siginfo.si_flags = 0;
  176. siginfo.si_isr = 0;
  177. siginfo.si_imm = 0;
  178. siginfo.si_code = ILL_BADIADDR;
  179. force_sig_info(SIGILL, &siginfo, current);
  180. } else if (ia64_psr(regs)->tb) {
  181. /*
  182. * Branch Tracing is enabled.
  183. * Force a taken branch signal.
  184. */
  185. siginfo.si_signo = SIGTRAP;
  186. siginfo.si_errno = 0;
  187. siginfo.si_code = TRAP_BRANCH;
  188. siginfo.si_flags = 0;
  189. siginfo.si_isr = 0;
  190. siginfo.si_addr = 0;
  191. siginfo.si_imm = 0;
  192. force_sig_info(SIGTRAP, &siginfo, current);
  193. } else if (ia64_psr(regs)->ss) {
  194. /*
  195. * Single Step is enabled.
  196. * Force a trace signal.
  197. */
  198. siginfo.si_signo = SIGTRAP;
  199. siginfo.si_errno = 0;
  200. siginfo.si_code = TRAP_TRACE;
  201. siginfo.si_flags = 0;
  202. siginfo.si_isr = 0;
  203. siginfo.si_addr = 0;
  204. siginfo.si_imm = 0;
  205. force_sig_info(SIGTRAP, &siginfo, current);
  206. }
  207. return rv;
  208. }