intercept.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. [0xb2] = kvm_s390_handle_priv,
  84. [0xb7] = handle_lctl,
  85. [0xeb] = handle_lctg,
  86. };
  87. static int handle_noop(struct kvm_vcpu *vcpu)
  88. {
  89. switch (vcpu->arch.sie_block->icptcode) {
  90. case 0x10:
  91. vcpu->stat.exit_external_request++;
  92. break;
  93. case 0x14:
  94. vcpu->stat.exit_external_interrupt++;
  95. break;
  96. default:
  97. break; /* nothing */
  98. }
  99. return 0;
  100. }
  101. static int handle_stop(struct kvm_vcpu *vcpu)
  102. {
  103. vcpu->stat.exit_stop_request++;
  104. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  105. atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
  106. return -ENOTSUPP;
  107. }
  108. static int handle_validity(struct kvm_vcpu *vcpu)
  109. {
  110. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  111. vcpu->stat.exit_validity++;
  112. if (viwhy == 0x37) {
  113. fault_in_pages_writeable((char __user *)
  114. vcpu->kvm->arch.guest_origin +
  115. vcpu->arch.sie_block->prefix,
  116. PAGE_SIZE);
  117. return 0;
  118. }
  119. VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
  120. viwhy);
  121. return -ENOTSUPP;
  122. }
  123. static int handle_instruction(struct kvm_vcpu *vcpu)
  124. {
  125. intercept_handler_t handler;
  126. vcpu->stat.exit_instruction++;
  127. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  128. if (handler)
  129. return handler(vcpu);
  130. return -ENOTSUPP;
  131. }
  132. static int handle_prog(struct kvm_vcpu *vcpu)
  133. {
  134. vcpu->stat.exit_program_interruption++;
  135. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  136. }
  137. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  138. {
  139. int rc, rc2;
  140. vcpu->stat.exit_instr_and_program++;
  141. rc = handle_instruction(vcpu);
  142. rc2 = handle_prog(vcpu);
  143. if (rc == -ENOTSUPP)
  144. vcpu->arch.sie_block->icptcode = 0x04;
  145. if (rc)
  146. return rc;
  147. return rc2;
  148. }
  149. static const intercept_handler_t intercept_funcs[0x48 >> 2] = {
  150. [0x00 >> 2] = handle_noop,
  151. [0x04 >> 2] = handle_instruction,
  152. [0x08 >> 2] = handle_prog,
  153. [0x0C >> 2] = handle_instruction_and_prog,
  154. [0x10 >> 2] = handle_noop,
  155. [0x14 >> 2] = handle_noop,
  156. [0x1C >> 2] = kvm_s390_handle_wait,
  157. [0x20 >> 2] = handle_validity,
  158. [0x28 >> 2] = handle_stop,
  159. };
  160. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  161. {
  162. intercept_handler_t func;
  163. u8 code = vcpu->arch.sie_block->icptcode;
  164. if (code & 3 || code > 0x48)
  165. return -ENOTSUPP;
  166. func = intercept_funcs[code >> 2];
  167. if (func)
  168. return func(vcpu);
  169. return -ENOTSUPP;
  170. }