intercept.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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->arch.guest_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->arch.guest_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. atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
  116. spin_lock_bh(&vcpu->arch.local_int.lock);
  117. if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
  118. vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
  119. rc = kvm_s390_vcpu_store_status(vcpu,
  120. KVM_S390_STORE_STATUS_NOADDR);
  121. if (rc >= 0)
  122. rc = -EOPNOTSUPP;
  123. }
  124. if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
  125. vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
  126. rc = SIE_INTERCEPT_RERUNVCPU;
  127. vcpu->run->exit_reason = KVM_EXIT_INTR;
  128. }
  129. if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
  130. vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
  131. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  132. rc = -EOPNOTSUPP;
  133. }
  134. spin_unlock_bh(&vcpu->arch.local_int.lock);
  135. return rc;
  136. }
  137. static int handle_validity(struct kvm_vcpu *vcpu)
  138. {
  139. unsigned long vmaddr;
  140. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  141. int rc;
  142. vcpu->stat.exit_validity++;
  143. if (viwhy == 0x37) {
  144. vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
  145. vcpu->arch.gmap);
  146. if (IS_ERR_VALUE(vmaddr)) {
  147. rc = -EOPNOTSUPP;
  148. goto out;
  149. }
  150. rc = fault_in_pages_writeable((char __user *) vmaddr,
  151. PAGE_SIZE);
  152. if (rc) {
  153. /* user will receive sigsegv, exit to user */
  154. rc = -EOPNOTSUPP;
  155. goto out;
  156. }
  157. vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
  158. vcpu->arch.gmap);
  159. if (IS_ERR_VALUE(vmaddr)) {
  160. rc = -EOPNOTSUPP;
  161. goto out;
  162. }
  163. rc = fault_in_pages_writeable((char __user *) vmaddr,
  164. PAGE_SIZE);
  165. if (rc) {
  166. /* user will receive sigsegv, exit to user */
  167. rc = -EOPNOTSUPP;
  168. goto out;
  169. }
  170. } else
  171. rc = -EOPNOTSUPP;
  172. out:
  173. if (rc)
  174. VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
  175. viwhy);
  176. return rc;
  177. }
  178. static int handle_instruction(struct kvm_vcpu *vcpu)
  179. {
  180. intercept_handler_t handler;
  181. vcpu->stat.exit_instruction++;
  182. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  183. if (handler)
  184. return handler(vcpu);
  185. return -EOPNOTSUPP;
  186. }
  187. static int handle_prog(struct kvm_vcpu *vcpu)
  188. {
  189. vcpu->stat.exit_program_interruption++;
  190. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  191. }
  192. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  193. {
  194. int rc, rc2;
  195. vcpu->stat.exit_instr_and_program++;
  196. rc = handle_instruction(vcpu);
  197. rc2 = handle_prog(vcpu);
  198. if (rc == -EOPNOTSUPP)
  199. vcpu->arch.sie_block->icptcode = 0x04;
  200. if (rc)
  201. return rc;
  202. return rc2;
  203. }
  204. static const intercept_handler_t intercept_funcs[] = {
  205. [0x00 >> 2] = handle_noop,
  206. [0x04 >> 2] = handle_instruction,
  207. [0x08 >> 2] = handle_prog,
  208. [0x0C >> 2] = handle_instruction_and_prog,
  209. [0x10 >> 2] = handle_noop,
  210. [0x14 >> 2] = handle_noop,
  211. [0x1C >> 2] = kvm_s390_handle_wait,
  212. [0x20 >> 2] = handle_validity,
  213. [0x28 >> 2] = handle_stop,
  214. };
  215. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  216. {
  217. intercept_handler_t func;
  218. u8 code = vcpu->arch.sie_block->icptcode;
  219. if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
  220. return -EOPNOTSUPP;
  221. func = intercept_funcs[code >> 2];
  222. if (func)
  223. return func(vcpu);
  224. return -EOPNOTSUPP;
  225. }