kvm-s390.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * definition for kvm on s390
  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. * Christian Ehrhardt <ehrhardt@de.ibm.com>
  13. */
  14. #ifndef ARCH_S390_KVM_S390_H
  15. #define ARCH_S390_KVM_S390_H
  16. #include <linux/hrtimer.h>
  17. #include <linux/kvm.h>
  18. #include <linux/kvm_host.h>
  19. typedef int (*intercept_handler_t)(struct kvm_vcpu *vcpu);
  20. /* declare vfacilities extern */
  21. extern unsigned long *vfacilities;
  22. /* Transactional Memory Execution related macros */
  23. #define IS_TE_ENABLED(vcpu) ((vcpu->arch.sie_block->ecb & 0x10))
  24. #define TDB_FORMAT1 1
  25. #define IS_ITDB_VALID(vcpu) ((*(char *)vcpu->arch.sie_block->itdba == TDB_FORMAT1))
  26. #define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
  27. do { \
  28. debug_sprintf_event(d_kvm->arch.dbf, d_loglevel, d_string "\n", \
  29. d_args); \
  30. } while (0)
  31. #define VCPU_EVENT(d_vcpu, d_loglevel, d_string, d_args...)\
  32. do { \
  33. debug_sprintf_event(d_vcpu->kvm->arch.dbf, d_loglevel, \
  34. "%02d[%016lx-%016lx]: " d_string "\n", d_vcpu->vcpu_id, \
  35. d_vcpu->arch.sie_block->gpsw.mask, d_vcpu->arch.sie_block->gpsw.addr,\
  36. d_args); \
  37. } while (0)
  38. static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
  39. {
  40. return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED;
  41. }
  42. static inline int kvm_is_ucontrol(struct kvm *kvm)
  43. {
  44. #ifdef CONFIG_KVM_S390_UCONTROL
  45. if (kvm->arch.gmap)
  46. return 0;
  47. return 1;
  48. #else
  49. return 0;
  50. #endif
  51. }
  52. #define GUEST_PREFIX_SHIFT 13
  53. static inline u32 kvm_s390_get_prefix(struct kvm_vcpu *vcpu)
  54. {
  55. return vcpu->arch.sie_block->prefix << GUEST_PREFIX_SHIFT;
  56. }
  57. static inline void kvm_s390_set_prefix(struct kvm_vcpu *vcpu, u32 prefix)
  58. {
  59. vcpu->arch.sie_block->prefix = prefix >> GUEST_PREFIX_SHIFT;
  60. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  61. kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
  62. }
  63. static inline u64 kvm_s390_get_base_disp_s(struct kvm_vcpu *vcpu)
  64. {
  65. u32 base2 = vcpu->arch.sie_block->ipb >> 28;
  66. u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  67. return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
  68. }
  69. static inline void kvm_s390_get_base_disp_sse(struct kvm_vcpu *vcpu,
  70. u64 *address1, u64 *address2)
  71. {
  72. u32 base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
  73. u32 disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
  74. u32 base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
  75. u32 disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
  76. *address1 = (base1 ? vcpu->run->s.regs.gprs[base1] : 0) + disp1;
  77. *address2 = (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
  78. }
  79. static inline void kvm_s390_get_regs_rre(struct kvm_vcpu *vcpu, int *r1, int *r2)
  80. {
  81. if (r1)
  82. *r1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 20;
  83. if (r2)
  84. *r2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
  85. }
  86. static inline u64 kvm_s390_get_base_disp_rsy(struct kvm_vcpu *vcpu)
  87. {
  88. u32 base2 = vcpu->arch.sie_block->ipb >> 28;
  89. u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
  90. ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
  91. /* The displacement is a 20bit _SIGNED_ value */
  92. if (disp2 & 0x80000)
  93. disp2+=0xfff00000;
  94. return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + (long)(int)disp2;
  95. }
  96. static inline u64 kvm_s390_get_base_disp_rs(struct kvm_vcpu *vcpu)
  97. {
  98. u32 base2 = vcpu->arch.sie_block->ipb >> 28;
  99. u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  100. return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
  101. }
  102. /* Set the condition code in the guest program status word */
  103. static inline void kvm_s390_set_psw_cc(struct kvm_vcpu *vcpu, unsigned long cc)
  104. {
  105. vcpu->arch.sie_block->gpsw.mask &= ~(3UL << 44);
  106. vcpu->arch.sie_block->gpsw.mask |= cc << 44;
  107. }
  108. /* are cpu states controlled by user space */
  109. static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm)
  110. {
  111. return kvm->arch.user_cpu_state_ctrl != 0;
  112. }
  113. int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
  114. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu);
  115. enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
  116. int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
  117. void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
  118. void kvm_s390_clear_float_irqs(struct kvm *kvm);
  119. int __must_check kvm_s390_inject_vm(struct kvm *kvm,
  120. struct kvm_s390_interrupt *s390int);
  121. int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
  122. struct kvm_s390_irq *irq);
  123. int __must_check kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code);
  124. struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
  125. u64 cr6, u64 schid);
  126. void kvm_s390_reinject_io_int(struct kvm *kvm,
  127. struct kvm_s390_interrupt_info *inti);
  128. int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked);
  129. /* implemented in intercept.c */
  130. void kvm_s390_rewind_psw(struct kvm_vcpu *vcpu, int ilc);
  131. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
  132. /* implemented in priv.c */
  133. int is_valid_psw(psw_t *psw);
  134. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu);
  135. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu);
  136. int kvm_s390_handle_01(struct kvm_vcpu *vcpu);
  137. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu);
  138. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu);
  139. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu);
  140. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu);
  141. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
  142. /* implemented in sigp.c */
  143. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
  144. int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
  145. /* implemented in kvm-s390.c */
  146. long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable);
  147. int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
  148. int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
  149. void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu);
  150. void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu);
  151. void s390_vcpu_block(struct kvm_vcpu *vcpu);
  152. void s390_vcpu_unblock(struct kvm_vcpu *vcpu);
  153. void exit_sie(struct kvm_vcpu *vcpu);
  154. void exit_sie_sync(struct kvm_vcpu *vcpu);
  155. int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);
  156. void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu);
  157. /* is cmma enabled */
  158. bool kvm_s390_cmma_enabled(struct kvm *kvm);
  159. int test_vfacility(unsigned long nr);
  160. /* implemented in diag.c */
  161. int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
  162. /* implemented in interrupt.c */
  163. int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
  164. struct kvm_s390_pgm_info *pgm_info);
  165. /**
  166. * kvm_s390_inject_prog_cond - conditionally inject a program check
  167. * @vcpu: virtual cpu
  168. * @rc: original return/error code
  169. *
  170. * This function is supposed to be used after regular guest access functions
  171. * failed, to conditionally inject a program check to a vcpu. The typical
  172. * pattern would look like
  173. *
  174. * rc = write_guest(vcpu, addr, data, len);
  175. * if (rc)
  176. * return kvm_s390_inject_prog_cond(vcpu, rc);
  177. *
  178. * A negative return code from guest access functions implies an internal error
  179. * like e.g. out of memory. In these cases no program check should be injected
  180. * to the guest.
  181. * A positive value implies that an exception happened while accessing a guest's
  182. * memory. In this case all data belonging to the corresponding program check
  183. * has been stored in vcpu->arch.pgm and can be injected with
  184. * kvm_s390_inject_prog_irq().
  185. *
  186. * Returns: - the original @rc value if @rc was negative (internal error)
  187. * - zero if @rc was already zero
  188. * - zero or error code from injecting if @rc was positive
  189. * (program check injected to @vcpu)
  190. */
  191. static inline int kvm_s390_inject_prog_cond(struct kvm_vcpu *vcpu, int rc)
  192. {
  193. if (rc <= 0)
  194. return rc;
  195. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  196. }
  197. int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
  198. struct kvm_s390_irq *s390irq);
  199. /* implemented in interrupt.c */
  200. int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
  201. int psw_extint_disabled(struct kvm_vcpu *vcpu);
  202. void kvm_s390_destroy_adapters(struct kvm *kvm);
  203. int kvm_s390_si_ext_call_pending(struct kvm_vcpu *vcpu);
  204. extern struct kvm_device_ops kvm_flic_ops;
  205. /* implemented in guestdbg.c */
  206. void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
  207. void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu);
  208. void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu);
  209. int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
  210. struct kvm_guest_debug *dbg);
  211. void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu);
  212. void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu);
  213. void kvm_s390_handle_per_event(struct kvm_vcpu *vcpu);
  214. #endif