intercept.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_lctg(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_lctg++;
  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. reg = reg1;
  35. VCPU_EVENT(vcpu, 5, "lctg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
  36. disp2);
  37. do {
  38. rc = get_guest_u64(vcpu, useraddr,
  39. &vcpu->arch.sie_block->gcr[reg]);
  40. if (rc == -EFAULT) {
  41. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  42. break;
  43. }
  44. useraddr += 8;
  45. if (reg == reg3)
  46. break;
  47. reg = (reg + 1) % 16;
  48. } while (1);
  49. return 0;
  50. }
  51. static int handle_lctl(struct kvm_vcpu *vcpu)
  52. {
  53. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  54. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  55. int base2 = vcpu->arch.sie_block->ipb >> 28;
  56. int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  57. u64 useraddr;
  58. u32 val = 0;
  59. int reg, rc;
  60. vcpu->stat.instruction_lctl++;
  61. useraddr = disp2;
  62. if (base2)
  63. useraddr += vcpu->arch.guest_gprs[base2];
  64. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
  65. disp2);
  66. reg = reg1;
  67. do {
  68. rc = get_guest_u32(vcpu, useraddr, &val);
  69. if (rc == -EFAULT) {
  70. kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  71. break;
  72. }
  73. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  74. vcpu->arch.sie_block->gcr[reg] |= val;
  75. useraddr += 4;
  76. if (reg == reg3)
  77. break;
  78. reg = (reg + 1) % 16;
  79. } while (1);
  80. return 0;
  81. }
  82. static intercept_handler_t instruction_handlers[256] = {
  83. [0x83] = kvm_s390_handle_diag,
  84. [0xae] = kvm_s390_handle_sigp,
  85. [0xb2] = kvm_s390_handle_priv,
  86. [0xb7] = handle_lctl,
  87. [0xeb] = handle_lctg,
  88. };
  89. static int handle_noop(struct kvm_vcpu *vcpu)
  90. {
  91. switch (vcpu->arch.sie_block->icptcode) {
  92. case 0x0:
  93. vcpu->stat.exit_null++;
  94. break;
  95. case 0x10:
  96. vcpu->stat.exit_external_request++;
  97. break;
  98. case 0x14:
  99. vcpu->stat.exit_external_interrupt++;
  100. break;
  101. default:
  102. break; /* nothing */
  103. }
  104. return 0;
  105. }
  106. static int handle_stop(struct kvm_vcpu *vcpu)
  107. {
  108. int rc;
  109. vcpu->stat.exit_stop_request++;
  110. atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
  111. spin_lock_bh(&vcpu->arch.local_int.lock);
  112. if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
  113. vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
  114. rc = __kvm_s390_vcpu_store_status(vcpu,
  115. KVM_S390_STORE_STATUS_NOADDR);
  116. if (rc >= 0)
  117. rc = -ENOTSUPP;
  118. }
  119. if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
  120. vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
  121. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  122. rc = -ENOTSUPP;
  123. } else
  124. rc = 0;
  125. spin_unlock_bh(&vcpu->arch.local_int.lock);
  126. return rc;
  127. }
  128. static int handle_validity(struct kvm_vcpu *vcpu)
  129. {
  130. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  131. vcpu->stat.exit_validity++;
  132. if (viwhy == 0x37) {
  133. fault_in_pages_writeable((char __user *)
  134. vcpu->kvm->arch.guest_origin +
  135. vcpu->arch.sie_block->prefix,
  136. PAGE_SIZE);
  137. return 0;
  138. }
  139. VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
  140. viwhy);
  141. return -ENOTSUPP;
  142. }
  143. static int handle_instruction(struct kvm_vcpu *vcpu)
  144. {
  145. intercept_handler_t handler;
  146. vcpu->stat.exit_instruction++;
  147. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  148. if (handler)
  149. return handler(vcpu);
  150. return -ENOTSUPP;
  151. }
  152. static int handle_prog(struct kvm_vcpu *vcpu)
  153. {
  154. vcpu->stat.exit_program_interruption++;
  155. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  156. }
  157. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  158. {
  159. int rc, rc2;
  160. vcpu->stat.exit_instr_and_program++;
  161. rc = handle_instruction(vcpu);
  162. rc2 = handle_prog(vcpu);
  163. if (rc == -ENOTSUPP)
  164. vcpu->arch.sie_block->icptcode = 0x04;
  165. if (rc)
  166. return rc;
  167. return rc2;
  168. }
  169. static const intercept_handler_t intercept_funcs[0x48 >> 2] = {
  170. [0x00 >> 2] = handle_noop,
  171. [0x04 >> 2] = handle_instruction,
  172. [0x08 >> 2] = handle_prog,
  173. [0x0C >> 2] = handle_instruction_and_prog,
  174. [0x10 >> 2] = handle_noop,
  175. [0x14 >> 2] = handle_noop,
  176. [0x1C >> 2] = kvm_s390_handle_wait,
  177. [0x20 >> 2] = handle_validity,
  178. [0x28 >> 2] = handle_stop,
  179. };
  180. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  181. {
  182. intercept_handler_t func;
  183. u8 code = vcpu->arch.sie_block->icptcode;
  184. if (code & 3 || code > 0x48)
  185. return -ENOTSUPP;
  186. func = intercept_funcs[code >> 2];
  187. if (func)
  188. return func(vcpu);
  189. return -ENOTSUPP;
  190. }