kvm-s390.h 9.6 KB

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