intercept.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. u64 useraddr;
  26. int reg, rc;
  27. vcpu->stat.instruction_lctlg++;
  28. useraddr = kvm_s390_get_base_disp_rsy(vcpu);
  29. if (useraddr & 7)
  30. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  31. reg = reg1;
  32. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
  33. useraddr);
  34. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
  35. do {
  36. rc = get_guest_u64(vcpu, useraddr,
  37. &vcpu->arch.sie_block->gcr[reg]);
  38. if (rc == -EFAULT) {
  39. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  40. break;
  41. }
  42. useraddr += 8;
  43. if (reg == reg3)
  44. break;
  45. reg = (reg + 1) % 16;
  46. } while (1);
  47. return 0;
  48. }
  49. static int handle_lctl(struct kvm_vcpu *vcpu)
  50. {
  51. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  52. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  53. u64 useraddr;
  54. u32 val = 0;
  55. int reg, rc;
  56. vcpu->stat.instruction_lctl++;
  57. useraddr = kvm_s390_get_base_disp_rs(vcpu);
  58. if (useraddr & 3)
  59. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  60. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
  61. useraddr);
  62. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
  63. reg = reg1;
  64. do {
  65. rc = get_guest_u32(vcpu, useraddr, &val);
  66. if (rc == -EFAULT) {
  67. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  68. break;
  69. }
  70. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  71. vcpu->arch.sie_block->gcr[reg] |= val;
  72. useraddr += 4;
  73. if (reg == reg3)
  74. break;
  75. reg = (reg + 1) % 16;
  76. } while (1);
  77. return 0;
  78. }
  79. static const intercept_handler_t eb_handlers[256] = {
  80. [0x2f] = handle_lctlg,
  81. [0x8a] = kvm_s390_handle_priv_eb,
  82. };
  83. static int handle_eb(struct kvm_vcpu *vcpu)
  84. {
  85. intercept_handler_t handler;
  86. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  87. if (handler)
  88. return handler(vcpu);
  89. return -EOPNOTSUPP;
  90. }
  91. static const intercept_handler_t instruction_handlers[256] = {
  92. [0x01] = kvm_s390_handle_01,
  93. [0x82] = kvm_s390_handle_lpsw,
  94. [0x83] = kvm_s390_handle_diag,
  95. [0xae] = kvm_s390_handle_sigp,
  96. [0xb2] = kvm_s390_handle_b2,
  97. [0xb7] = handle_lctl,
  98. [0xb9] = kvm_s390_handle_b9,
  99. [0xe5] = kvm_s390_handle_e5,
  100. [0xeb] = handle_eb,
  101. };
  102. static int handle_noop(struct kvm_vcpu *vcpu)
  103. {
  104. switch (vcpu->arch.sie_block->icptcode) {
  105. case 0x0:
  106. vcpu->stat.exit_null++;
  107. break;
  108. case 0x10:
  109. vcpu->stat.exit_external_request++;
  110. break;
  111. case 0x14:
  112. vcpu->stat.exit_external_interrupt++;
  113. break;
  114. default:
  115. break; /* nothing */
  116. }
  117. return 0;
  118. }
  119. static int handle_stop(struct kvm_vcpu *vcpu)
  120. {
  121. int rc = 0;
  122. vcpu->stat.exit_stop_request++;
  123. spin_lock_bh(&vcpu->arch.local_int.lock);
  124. trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
  125. if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
  126. vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
  127. rc = SIE_INTERCEPT_RERUNVCPU;
  128. vcpu->run->exit_reason = KVM_EXIT_INTR;
  129. }
  130. if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
  131. atomic_set_mask(CPUSTAT_STOPPED,
  132. &vcpu->arch.sie_block->cpuflags);
  133. vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
  134. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  135. rc = -EOPNOTSUPP;
  136. }
  137. if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
  138. vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
  139. /* store status must be called unlocked. Since local_int.lock
  140. * only protects local_int.* and not guest memory we can give
  141. * up the lock here */
  142. spin_unlock_bh(&vcpu->arch.local_int.lock);
  143. rc = kvm_s390_vcpu_store_status(vcpu,
  144. KVM_S390_STORE_STATUS_NOADDR);
  145. if (rc >= 0)
  146. rc = -EOPNOTSUPP;
  147. } else
  148. spin_unlock_bh(&vcpu->arch.local_int.lock);
  149. return rc;
  150. }
  151. static int handle_validity(struct kvm_vcpu *vcpu)
  152. {
  153. unsigned long vmaddr;
  154. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  155. int rc;
  156. vcpu->stat.exit_validity++;
  157. trace_kvm_s390_intercept_validity(vcpu, viwhy);
  158. if (viwhy == 0x37) {
  159. vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
  160. vcpu->arch.gmap);
  161. if (IS_ERR_VALUE(vmaddr)) {
  162. rc = -EOPNOTSUPP;
  163. goto out;
  164. }
  165. rc = fault_in_pages_writeable((char __user *) vmaddr,
  166. PAGE_SIZE);
  167. if (rc) {
  168. /* user will receive sigsegv, exit to user */
  169. rc = -EOPNOTSUPP;
  170. goto out;
  171. }
  172. vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
  173. vcpu->arch.gmap);
  174. if (IS_ERR_VALUE(vmaddr)) {
  175. rc = -EOPNOTSUPP;
  176. goto out;
  177. }
  178. rc = fault_in_pages_writeable((char __user *) vmaddr,
  179. PAGE_SIZE);
  180. if (rc) {
  181. /* user will receive sigsegv, exit to user */
  182. rc = -EOPNOTSUPP;
  183. goto out;
  184. }
  185. } else
  186. rc = -EOPNOTSUPP;
  187. out:
  188. if (rc)
  189. VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
  190. viwhy);
  191. return rc;
  192. }
  193. static int handle_instruction(struct kvm_vcpu *vcpu)
  194. {
  195. intercept_handler_t handler;
  196. vcpu->stat.exit_instruction++;
  197. trace_kvm_s390_intercept_instruction(vcpu,
  198. vcpu->arch.sie_block->ipa,
  199. vcpu->arch.sie_block->ipb);
  200. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  201. if (handler)
  202. return handler(vcpu);
  203. return -EOPNOTSUPP;
  204. }
  205. static int handle_prog(struct kvm_vcpu *vcpu)
  206. {
  207. vcpu->stat.exit_program_interruption++;
  208. trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
  209. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  210. }
  211. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  212. {
  213. int rc, rc2;
  214. vcpu->stat.exit_instr_and_program++;
  215. rc = handle_instruction(vcpu);
  216. rc2 = handle_prog(vcpu);
  217. if (rc == -EOPNOTSUPP)
  218. vcpu->arch.sie_block->icptcode = 0x04;
  219. if (rc)
  220. return rc;
  221. return rc2;
  222. }
  223. static const intercept_handler_t intercept_funcs[] = {
  224. [0x00 >> 2] = handle_noop,
  225. [0x04 >> 2] = handle_instruction,
  226. [0x08 >> 2] = handle_prog,
  227. [0x0C >> 2] = handle_instruction_and_prog,
  228. [0x10 >> 2] = handle_noop,
  229. [0x14 >> 2] = handle_noop,
  230. [0x18 >> 2] = handle_noop,
  231. [0x1C >> 2] = kvm_s390_handle_wait,
  232. [0x20 >> 2] = handle_validity,
  233. [0x28 >> 2] = handle_stop,
  234. };
  235. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  236. {
  237. intercept_handler_t func;
  238. u8 code = vcpu->arch.sie_block->icptcode;
  239. if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
  240. return -EOPNOTSUPP;
  241. func = intercept_funcs[code >> 2];
  242. if (func)
  243. return func(vcpu);
  244. return -EOPNOTSUPP;
  245. }