kvm-s390.h 10 KB

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