intercept.c 6.7 KB

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