intercept.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * intercept.c - in-kernel handling for sie intercepts
  3. *
  4. * Copyright IBM Corp. 2008
  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 -ENOTSUPP;
  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. [0xeb] = handle_lctlg,
  92. };
  93. static int handle_noop(struct kvm_vcpu *vcpu)
  94. {
  95. switch (vcpu->arch.sie_block->icptcode) {
  96. case 0x0:
  97. vcpu->stat.exit_null++;
  98. break;
  99. case 0x10:
  100. vcpu->stat.exit_external_request++;
  101. break;
  102. case 0x14:
  103. vcpu->stat.exit_external_interrupt++;
  104. break;
  105. default:
  106. break; /* nothing */
  107. }
  108. return 0;
  109. }
  110. static int handle_stop(struct kvm_vcpu *vcpu)
  111. {
  112. int rc;
  113. vcpu->stat.exit_stop_request++;
  114. atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
  115. spin_lock_bh(&vcpu->arch.local_int.lock);
  116. if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
  117. vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
  118. rc = __kvm_s390_vcpu_store_status(vcpu,
  119. KVM_S390_STORE_STATUS_NOADDR);
  120. if (rc >= 0)
  121. rc = -ENOTSUPP;
  122. }
  123. if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
  124. vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
  125. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  126. rc = -ENOTSUPP;
  127. } else
  128. rc = 0;
  129. spin_unlock_bh(&vcpu->arch.local_int.lock);
  130. return rc;
  131. }
  132. static int handle_validity(struct kvm_vcpu *vcpu)
  133. {
  134. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  135. vcpu->stat.exit_validity++;
  136. if (viwhy == 0x37) {
  137. fault_in_pages_writeable((char __user *)
  138. vcpu->kvm->arch.guest_origin +
  139. vcpu->arch.sie_block->prefix,
  140. PAGE_SIZE);
  141. return 0;
  142. }
  143. VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
  144. viwhy);
  145. return -ENOTSUPP;
  146. }
  147. static int handle_instruction(struct kvm_vcpu *vcpu)
  148. {
  149. intercept_handler_t handler;
  150. vcpu->stat.exit_instruction++;
  151. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  152. if (handler)
  153. return handler(vcpu);
  154. return -ENOTSUPP;
  155. }
  156. static int handle_prog(struct kvm_vcpu *vcpu)
  157. {
  158. vcpu->stat.exit_program_interruption++;
  159. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  160. }
  161. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  162. {
  163. int rc, rc2;
  164. vcpu->stat.exit_instr_and_program++;
  165. rc = handle_instruction(vcpu);
  166. rc2 = handle_prog(vcpu);
  167. if (rc == -ENOTSUPP)
  168. vcpu->arch.sie_block->icptcode = 0x04;
  169. if (rc)
  170. return rc;
  171. return rc2;
  172. }
  173. static const intercept_handler_t intercept_funcs[0x48 >> 2] = {
  174. [0x00 >> 2] = handle_noop,
  175. [0x04 >> 2] = handle_instruction,
  176. [0x08 >> 2] = handle_prog,
  177. [0x0C >> 2] = handle_instruction_and_prog,
  178. [0x10 >> 2] = handle_noop,
  179. [0x14 >> 2] = handle_noop,
  180. [0x1C >> 2] = kvm_s390_handle_wait,
  181. [0x20 >> 2] = handle_validity,
  182. [0x28 >> 2] = handle_stop,
  183. };
  184. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  185. {
  186. intercept_handler_t func;
  187. u8 code = vcpu->arch.sie_block->icptcode;
  188. if (code & 3 || code > 0x48)
  189. return -ENOTSUPP;
  190. func = intercept_funcs[code >> 2];
  191. if (func)
  192. return func(vcpu);
  193. return -ENOTSUPP;
  194. }