intercept.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * 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. #include "trace.h"
  20. #include "trace-s390.h"
  21. static const intercept_handler_t instruction_handlers[256] = {
  22. [0x01] = kvm_s390_handle_01,
  23. [0x82] = kvm_s390_handle_lpsw,
  24. [0x83] = kvm_s390_handle_diag,
  25. [0xae] = kvm_s390_handle_sigp,
  26. [0xb2] = kvm_s390_handle_b2,
  27. [0xb7] = kvm_s390_handle_lctl,
  28. [0xb9] = kvm_s390_handle_b9,
  29. [0xe5] = kvm_s390_handle_e5,
  30. [0xeb] = kvm_s390_handle_eb,
  31. };
  32. static int handle_noop(struct kvm_vcpu *vcpu)
  33. {
  34. switch (vcpu->arch.sie_block->icptcode) {
  35. case 0x0:
  36. vcpu->stat.exit_null++;
  37. break;
  38. case 0x10:
  39. vcpu->stat.exit_external_request++;
  40. break;
  41. case 0x14:
  42. vcpu->stat.exit_external_interrupt++;
  43. break;
  44. default:
  45. break; /* nothing */
  46. }
  47. return 0;
  48. }
  49. static int handle_stop(struct kvm_vcpu *vcpu)
  50. {
  51. int rc = 0;
  52. vcpu->stat.exit_stop_request++;
  53. spin_lock_bh(&vcpu->arch.local_int.lock);
  54. trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
  55. if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
  56. vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
  57. rc = SIE_INTERCEPT_RERUNVCPU;
  58. vcpu->run->exit_reason = KVM_EXIT_INTR;
  59. }
  60. if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
  61. atomic_set_mask(CPUSTAT_STOPPED,
  62. &vcpu->arch.sie_block->cpuflags);
  63. vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
  64. VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
  65. rc = -EOPNOTSUPP;
  66. }
  67. if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
  68. vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
  69. /* store status must be called unlocked. Since local_int.lock
  70. * only protects local_int.* and not guest memory we can give
  71. * up the lock here */
  72. spin_unlock_bh(&vcpu->arch.local_int.lock);
  73. rc = kvm_s390_vcpu_store_status(vcpu,
  74. KVM_S390_STORE_STATUS_NOADDR);
  75. if (rc >= 0)
  76. rc = -EOPNOTSUPP;
  77. } else
  78. spin_unlock_bh(&vcpu->arch.local_int.lock);
  79. return rc;
  80. }
  81. static int handle_validity(struct kvm_vcpu *vcpu)
  82. {
  83. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  84. vcpu->stat.exit_validity++;
  85. trace_kvm_s390_intercept_validity(vcpu, viwhy);
  86. WARN_ONCE(true, "kvm: unhandled validity intercept 0x%x\n", viwhy);
  87. return -EOPNOTSUPP;
  88. }
  89. static int handle_instruction(struct kvm_vcpu *vcpu)
  90. {
  91. intercept_handler_t handler;
  92. vcpu->stat.exit_instruction++;
  93. trace_kvm_s390_intercept_instruction(vcpu,
  94. vcpu->arch.sie_block->ipa,
  95. vcpu->arch.sie_block->ipb);
  96. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  97. if (handler)
  98. return handler(vcpu);
  99. return -EOPNOTSUPP;
  100. }
  101. static int handle_prog(struct kvm_vcpu *vcpu)
  102. {
  103. vcpu->stat.exit_program_interruption++;
  104. trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
  105. return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
  106. }
  107. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  108. {
  109. int rc, rc2;
  110. vcpu->stat.exit_instr_and_program++;
  111. rc = handle_instruction(vcpu);
  112. rc2 = handle_prog(vcpu);
  113. if (rc == -EOPNOTSUPP)
  114. vcpu->arch.sie_block->icptcode = 0x04;
  115. if (rc)
  116. return rc;
  117. return rc2;
  118. }
  119. static const intercept_handler_t intercept_funcs[] = {
  120. [0x00 >> 2] = handle_noop,
  121. [0x04 >> 2] = handle_instruction,
  122. [0x08 >> 2] = handle_prog,
  123. [0x0C >> 2] = handle_instruction_and_prog,
  124. [0x10 >> 2] = handle_noop,
  125. [0x14 >> 2] = handle_noop,
  126. [0x18 >> 2] = handle_noop,
  127. [0x1C >> 2] = kvm_s390_handle_wait,
  128. [0x20 >> 2] = handle_validity,
  129. [0x28 >> 2] = handle_stop,
  130. };
  131. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  132. {
  133. intercept_handler_t func;
  134. u8 code = vcpu->arch.sie_block->icptcode;
  135. if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
  136. return -EOPNOTSUPP;
  137. func = intercept_funcs[code >> 2];
  138. if (func)
  139. return func(vcpu);
  140. return -EOPNOTSUPP;
  141. }