intercept.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * in-kernel handling for sie intercepts
  3. *
  4. * Copyright IBM Corp. 2008, 2009
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Carsten Otte <cotte@de.ibm.com>
  11. * Christian Borntraeger <borntraeger@de.ibm.com>
  12. */
  13. #include <linux/kvm_host.h>
  14. #include <linux/errno.h>
  15. #include <linux/pagemap.h>
  16. #include <asm/kvm_host.h>
  17. #include "kvm-s390.h"
  18. #include "gaccess.h"
  19. #include "trace.h"
  20. #include "trace-s390.h"
  21. static int handle_lctlg(struct kvm_vcpu *vcpu)
  22. {
  23. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  24. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  25. int base2 = vcpu->arch.sie_block->ipb >> 28;
  26. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
  27. ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
  28. u64 useraddr;
  29. int reg, rc;
  30. vcpu->stat.instruction_lctlg++;
  31. if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
  32. return -EOPNOTSUPP;
  33. useraddr = disp2;
  34. if (base2)
  35. useraddr += vcpu->run->s.regs.gprs[base2];
  36. if (useraddr & 7)
  37. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  38. reg = reg1;
  39. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
  40. disp2);
  41. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
  42. do {
  43. rc = get_guest_u64(vcpu, useraddr,
  44. &vcpu->arch.sie_block->gcr[reg]);
  45. if (rc == -EFAULT) {
  46. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  47. break;
  48. }
  49. useraddr += 8;
  50. if (reg == reg3)
  51. break;
  52. reg = (reg + 1) % 16;
  53. } while (1);
  54. return 0;
  55. }
  56. static int handle_lctl(struct kvm_vcpu *vcpu)
  57. {
  58. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  59. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  60. int base2 = vcpu->arch.sie_block->ipb >> 28;
  61. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  62. u64 useraddr;
  63. u32 val = 0;
  64. int reg, rc;
  65. vcpu->stat.instruction_lctl++;
  66. useraddr = disp2;
  67. if (base2)
  68. useraddr += vcpu->run->s.regs.gprs[base2];
  69. if (useraddr & 3)
  70. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  71. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
  72. disp2);
  73. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
  74. reg = reg1;
  75. do {
  76. rc = get_guest_u32(vcpu, useraddr, &val);
  77. if (rc == -EFAULT) {
  78. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  79. break;
  80. }
  81. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  82. vcpu->arch.sie_block->gcr[reg] |= val;
  83. useraddr += 4;
  84. if (reg == reg3)
  85. break;
  86. reg = (reg + 1) % 16;
  87. } while (1);
  88. return 0;
  89. }
  90. static intercept_handler_t instruction_handlers[256] = {
  91. [0x01] = kvm_s390_handle_01,
  92. [0x83] = kvm_s390_handle_diag,
  93. [0xae] = kvm_s390_handle_sigp,
  94. [0xb2] = kvm_s390_handle_b2,
  95. [0xb7] = handle_lctl,
  96. [0xe5] = kvm_s390_handle_e5,
  97. [0xeb] = handle_lctlg,
  98. };
  99. static int handle_noop(struct kvm_vcpu *vcpu)
  100. {
  101. switch (vcpu->arch.sie_block->icptcode) {
  102. case 0x0:
  103. vcpu->stat.exit_null++;
  104. break;
  105. case 0x10:
  106. vcpu->stat.exit_external_request++;
  107. break;
  108. case 0x14:
  109. vcpu->stat.exit_external_interrupt++;
  110. break;
  111. default:
  112. break; /* nothing */
  113. }
  114. return 0;
  115. }
  116. static int handle_stop(struct kvm_vcpu *vcpu)
  117. {
  118. int rc = 0;
  119. vcpu->stat.exit_stop_request++;
  120. spin_lock_bh(&vcpu->arch.local_int.lock);
  121. trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
  122. if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
  123. vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
  124. rc = SIE_INTERCEPT_RERUNVCPU;
  125. vcpu->run->exit_reason = KVM_EXIT_INTR;
  126. }
  127. if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
  128. atomic_set_mask(CPUSTAT_STOPPED,
  129. &vcpu->arch.sie_block->cpuflags);
  130. vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
  131. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  132. rc = -EOPNOTSUPP;
  133. }
  134. if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
  135. vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
  136. /* store status must be called unlocked. Since local_int.lock
  137. * only protects local_int.* and not guest memory we can give
  138. * up the lock here */
  139. spin_unlock_bh(&vcpu->arch.local_int.lock);
  140. rc = kvm_s390_vcpu_store_status(vcpu,
  141. KVM_S390_STORE_STATUS_NOADDR);
  142. if (rc >= 0)
  143. rc = -EOPNOTSUPP;
  144. } else
  145. spin_unlock_bh(&vcpu->arch.local_int.lock);
  146. return rc;
  147. }
  148. static int handle_validity(struct kvm_vcpu *vcpu)
  149. {
  150. unsigned long vmaddr;
  151. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  152. int rc;
  153. vcpu->stat.exit_validity++;
  154. trace_kvm_s390_intercept_validity(vcpu, viwhy);
  155. if (viwhy == 0x37) {
  156. vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
  157. vcpu->arch.gmap);
  158. if (IS_ERR_VALUE(vmaddr)) {
  159. rc = -EOPNOTSUPP;
  160. goto out;
  161. }
  162. rc = fault_in_pages_writeable((char __user *) vmaddr,
  163. PAGE_SIZE);
  164. if (rc) {
  165. /* user will receive sigsegv, exit to user */
  166. rc = -EOPNOTSUPP;
  167. goto out;
  168. }
  169. vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
  170. vcpu->arch.gmap);
  171. if (IS_ERR_VALUE(vmaddr)) {
  172. rc = -EOPNOTSUPP;
  173. goto out;
  174. }
  175. rc = fault_in_pages_writeable((char __user *) vmaddr,
  176. PAGE_SIZE);
  177. if (rc) {
  178. /* user will receive sigsegv, exit to user */
  179. rc = -EOPNOTSUPP;
  180. goto out;
  181. }
  182. } else
  183. rc = -EOPNOTSUPP;
  184. out:
  185. if (rc)
  186. VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
  187. viwhy);
  188. return rc;
  189. }
  190. static int handle_instruction(struct kvm_vcpu *vcpu)
  191. {
  192. intercept_handler_t handler;
  193. vcpu->stat.exit_instruction++;
  194. trace_kvm_s390_intercept_instruction(vcpu,
  195. vcpu->arch.sie_block->ipa,
  196. vcpu->arch.sie_block->ipb);
  197. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  198. if (handler)
  199. return handler(vcpu);
  200. return -EOPNOTSUPP;
  201. }
  202. static int handle_prog(struct kvm_vcpu *vcpu)
  203. {
  204. vcpu->stat.exit_program_interruption++;
  205. trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
  206. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  207. }
  208. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  209. {
  210. int rc, rc2;
  211. vcpu->stat.exit_instr_and_program++;
  212. rc = handle_instruction(vcpu);
  213. rc2 = handle_prog(vcpu);
  214. if (rc == -EOPNOTSUPP)
  215. vcpu->arch.sie_block->icptcode = 0x04;
  216. if (rc)
  217. return rc;
  218. return rc2;
  219. }
  220. static const intercept_handler_t intercept_funcs[] = {
  221. [0x00 >> 2] = handle_noop,
  222. [0x04 >> 2] = handle_instruction,
  223. [0x08 >> 2] = handle_prog,
  224. [0x0C >> 2] = handle_instruction_and_prog,
  225. [0x10 >> 2] = handle_noop,
  226. [0x14 >> 2] = handle_noop,
  227. [0x1C >> 2] = kvm_s390_handle_wait,
  228. [0x20 >> 2] = handle_validity,
  229. [0x28 >> 2] = handle_stop,
  230. };
  231. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  232. {
  233. intercept_handler_t func;
  234. u8 code = vcpu->arch.sie_block->icptcode;
  235. if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
  236. return -EOPNOTSUPP;
  237. func = intercept_funcs[code >> 2];
  238. if (func)
  239. return func(vcpu);
  240. return -EOPNOTSUPP;
  241. }