kvm-s390.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /* implemented in interrupt.c */
  148. int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
  149. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu);
  150. enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
  151. int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
  152. void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
  153. void kvm_s390_clear_float_irqs(struct kvm *kvm);
  154. int __must_check kvm_s390_inject_vm(struct kvm *kvm,
  155. struct kvm_s390_interrupt *s390int);
  156. int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
  157. struct kvm_s390_irq *irq);
  158. static inline int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
  159. struct kvm_s390_pgm_info *pgm_info)
  160. {
  161. struct kvm_s390_irq irq = {
  162. .type = KVM_S390_PROGRAM_INT,
  163. .u.pgm = *pgm_info,
  164. };
  165. return kvm_s390_inject_vcpu(vcpu, &irq);
  166. }
  167. static inline int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
  168. {
  169. struct kvm_s390_irq irq = {
  170. .type = KVM_S390_PROGRAM_INT,
  171. .u.pgm.code = code,
  172. };
  173. return kvm_s390_inject_vcpu(vcpu, &irq);
  174. }
  175. struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
  176. u64 isc_mask, u32 schid);
  177. int kvm_s390_reinject_io_int(struct kvm *kvm,
  178. struct kvm_s390_interrupt_info *inti);
  179. int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked);
  180. /* implemented in intercept.c */
  181. void kvm_s390_rewind_psw(struct kvm_vcpu *vcpu, int ilc);
  182. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
  183. /* implemented in priv.c */
  184. int is_valid_psw(psw_t *psw);
  185. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu);
  186. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu);
  187. int kvm_s390_handle_01(struct kvm_vcpu *vcpu);
  188. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu);
  189. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu);
  190. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu);
  191. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu);
  192. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
  193. /* implemented in sigp.c */
  194. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
  195. int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
  196. /* implemented in kvm-s390.c */
  197. void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod);
  198. long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable);
  199. int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
  200. int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
  201. unsigned long addr);
  202. int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
  203. int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr);
  204. void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu);
  205. void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu);
  206. void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu);
  207. void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu);
  208. void exit_sie(struct kvm_vcpu *vcpu);
  209. void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu);
  210. int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);
  211. void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu);
  212. unsigned long kvm_s390_fac_list_mask_size(void);
  213. extern unsigned long kvm_s390_fac_list_mask[];
  214. /* implemented in diag.c */
  215. int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
  216. static inline void kvm_s390_vcpu_block_all(struct kvm *kvm)
  217. {
  218. int i;
  219. struct kvm_vcpu *vcpu;
  220. WARN_ON(!mutex_is_locked(&kvm->lock));
  221. kvm_for_each_vcpu(i, vcpu, kvm)
  222. kvm_s390_vcpu_block(vcpu);
  223. }
  224. static inline void kvm_s390_vcpu_unblock_all(struct kvm *kvm)
  225. {
  226. int i;
  227. struct kvm_vcpu *vcpu;
  228. kvm_for_each_vcpu(i, vcpu, kvm)
  229. kvm_s390_vcpu_unblock(vcpu);
  230. }
  231. static inline u64 kvm_s390_get_tod_clock_fast(struct kvm *kvm)
  232. {
  233. u64 rc;
  234. preempt_disable();
  235. rc = get_tod_clock_fast() + kvm->arch.epoch;
  236. preempt_enable();
  237. return rc;
  238. }
  239. /**
  240. * kvm_s390_inject_prog_cond - conditionally inject a program check
  241. * @vcpu: virtual cpu
  242. * @rc: original return/error code
  243. *
  244. * This function is supposed to be used after regular guest access functions
  245. * failed, to conditionally inject a program check to a vcpu. The typical
  246. * pattern would look like
  247. *
  248. * rc = write_guest(vcpu, addr, data, len);
  249. * if (rc)
  250. * return kvm_s390_inject_prog_cond(vcpu, rc);
  251. *
  252. * A negative return code from guest access functions implies an internal error
  253. * like e.g. out of memory. In these cases no program check should be injected
  254. * to the guest.
  255. * A positive value implies that an exception happened while accessing a guest's
  256. * memory. In this case all data belonging to the corresponding program check
  257. * has been stored in vcpu->arch.pgm and can be injected with
  258. * kvm_s390_inject_prog_irq().
  259. *
  260. * Returns: - the original @rc value if @rc was negative (internal error)
  261. * - zero if @rc was already zero
  262. * - zero or error code from injecting if @rc was positive
  263. * (program check injected to @vcpu)
  264. */
  265. static inline int kvm_s390_inject_prog_cond(struct kvm_vcpu *vcpu, int rc)
  266. {
  267. if (rc <= 0)
  268. return rc;
  269. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  270. }
  271. int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
  272. struct kvm_s390_irq *s390irq);
  273. /* implemented in interrupt.c */
  274. int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop);
  275. int psw_extint_disabled(struct kvm_vcpu *vcpu);
  276. void kvm_s390_destroy_adapters(struct kvm *kvm);
  277. int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu);
  278. extern struct kvm_device_ops kvm_flic_ops;
  279. int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu);
  280. void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu);
  281. int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu,
  282. void __user *buf, int len);
  283. int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu,
  284. __u8 __user *buf, int len);
  285. /* implemented in guestdbg.c */
  286. void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
  287. void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu);
  288. void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu);
  289. int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
  290. struct kvm_guest_debug *dbg);
  291. void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu);
  292. void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu);
  293. void kvm_s390_handle_per_event(struct kvm_vcpu *vcpu);
  294. #endif