intercept.c 6.3 KB

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