kvm-s390.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * definition for kvm on s390
  4. *
  5. * Copyright IBM Corp. 2008, 2009
  6. *
  7. * Author(s): Carsten Otte <cotte@de.ibm.com>
  8. * Christian Borntraeger <borntraeger@de.ibm.com>
  9. * Christian Ehrhardt <ehrhardt@de.ibm.com>
  10. */
  11. #ifndef ARCH_S390_KVM_S390_H
  12. #define ARCH_S390_KVM_S390_H
  13. #include <linux/hrtimer.h>
  14. #include <linux/kvm.h>
  15. #include <linux/kvm_host.h>
  16. #include <asm/facility.h>
  17. #include <asm/processor.h>
  18. #include <asm/sclp.h>
  19. /* Transactional Memory Execution related macros */
  20. #define IS_TE_ENABLED(vcpu) ((vcpu->arch.sie_block->ecb & ECB_TE))
  21. #define TDB_FORMAT1 1
  22. #define IS_ITDB_VALID(vcpu) ((*(char *)vcpu->arch.sie_block->itdba == TDB_FORMAT1))
  23. extern debug_info_t *kvm_s390_dbf;
  24. #define KVM_EVENT(d_loglevel, d_string, d_args...)\
  25. do { \
  26. debug_sprintf_event(kvm_s390_dbf, d_loglevel, d_string "\n", \
  27. d_args); \
  28. } while (0)
  29. #define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
  30. do { \
  31. debug_sprintf_event(d_kvm->arch.dbf, d_loglevel, d_string "\n", \
  32. d_args); \
  33. } while (0)
  34. #define VCPU_EVENT(d_vcpu, d_loglevel, d_string, d_args...)\
  35. do { \
  36. debug_sprintf_event(d_vcpu->kvm->arch.dbf, d_loglevel, \
  37. "%02d[%016lx-%016lx]: " d_string "\n", d_vcpu->vcpu_id, \
  38. d_vcpu->arch.sie_block->gpsw.mask, d_vcpu->arch.sie_block->gpsw.addr,\
  39. d_args); \
  40. } while (0)
  41. static inline void kvm_s390_set_cpuflags(struct kvm_vcpu *vcpu, u32 flags)
  42. {
  43. atomic_or(flags, &vcpu->arch.sie_block->cpuflags);
  44. }
  45. static inline void kvm_s390_clear_cpuflags(struct kvm_vcpu *vcpu, u32 flags)
  46. {
  47. atomic_andnot(flags, &vcpu->arch.sie_block->cpuflags);
  48. }
  49. static inline bool kvm_s390_test_cpuflags(struct kvm_vcpu *vcpu, u32 flags)
  50. {
  51. return (atomic_read(&vcpu->arch.sie_block->cpuflags) & flags) == flags;
  52. }
  53. static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
  54. {
  55. return kvm_s390_test_cpuflags(vcpu, CPUSTAT_STOPPED);
  56. }
  57. static inline int is_vcpu_idle(struct kvm_vcpu *vcpu)
  58. {
  59. return test_bit(vcpu->vcpu_id, vcpu->kvm->arch.float_int.idle_mask);
  60. }
  61. static inline int kvm_is_ucontrol(struct kvm *kvm)
  62. {
  63. #ifdef CONFIG_KVM_S390_UCONTROL
  64. if (kvm->arch.gmap)
  65. return 0;
  66. return 1;
  67. #else
  68. return 0;
  69. #endif
  70. }
  71. #define GUEST_PREFIX_SHIFT 13
  72. static inline u32 kvm_s390_get_prefix(struct kvm_vcpu *vcpu)
  73. {
  74. return vcpu->arch.sie_block->prefix << GUEST_PREFIX_SHIFT;
  75. }
  76. static inline void kvm_s390_set_prefix(struct kvm_vcpu *vcpu, u32 prefix)
  77. {
  78. VCPU_EVENT(vcpu, 3, "set prefix of cpu %03u to 0x%x", vcpu->vcpu_id,
  79. prefix);
  80. vcpu->arch.sie_block->prefix = prefix >> GUEST_PREFIX_SHIFT;
  81. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  82. kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
  83. }
  84. static inline u64 kvm_s390_get_base_disp_s(struct kvm_vcpu *vcpu, u8 *ar)
  85. {
  86. u32 base2 = vcpu->arch.sie_block->ipb >> 28;
  87. u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  88. if (ar)
  89. *ar = base2;
  90. return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
  91. }
  92. static inline void kvm_s390_get_base_disp_sse(struct kvm_vcpu *vcpu,
  93. u64 *address1, u64 *address2,
  94. u8 *ar_b1, u8 *ar_b2)
  95. {
  96. u32 base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
  97. u32 disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
  98. u32 base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
  99. u32 disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
  100. *address1 = (base1 ? vcpu->run->s.regs.gprs[base1] : 0) + disp1;
  101. *address2 = (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
  102. if (ar_b1)
  103. *ar_b1 = base1;
  104. if (ar_b2)
  105. *ar_b2 = base2;
  106. }
  107. static inline void kvm_s390_get_regs_rre(struct kvm_vcpu *vcpu, int *r1, int *r2)
  108. {
  109. if (r1)
  110. *r1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 20;
  111. if (r2)
  112. *r2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
  113. }
  114. static inline u64 kvm_s390_get_base_disp_rsy(struct kvm_vcpu *vcpu, u8 *ar)
  115. {
  116. u32 base2 = vcpu->arch.sie_block->ipb >> 28;
  117. u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
  118. ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
  119. /* The displacement is a 20bit _SIGNED_ value */
  120. if (disp2 & 0x80000)
  121. disp2+=0xfff00000;
  122. if (ar)
  123. *ar = base2;
  124. return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + (long)(int)disp2;
  125. }
  126. static inline u64 kvm_s390_get_base_disp_rs(struct kvm_vcpu *vcpu, u8 *ar)
  127. {
  128. u32 base2 = vcpu->arch.sie_block->ipb >> 28;
  129. u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
  130. if (ar)
  131. *ar = base2;
  132. return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
  133. }
  134. /* Set the condition code in the guest program status word */
  135. static inline void kvm_s390_set_psw_cc(struct kvm_vcpu *vcpu, unsigned long cc)
  136. {
  137. vcpu->arch.sie_block->gpsw.mask &= ~(3UL << 44);
  138. vcpu->arch.sie_block->gpsw.mask |= cc << 44;
  139. }
  140. /* test availability of facility in a kvm instance */
  141. static inline int test_kvm_facility(struct kvm *kvm, unsigned long nr)
  142. {
  143. return __test_facility(nr, kvm->arch.model.fac_mask) &&
  144. __test_facility(nr, kvm->arch.model.fac_list);
  145. }
  146. static inline int set_kvm_facility(u64 *fac_list, unsigned long nr)
  147. {
  148. unsigned char *ptr;
  149. if (nr >= MAX_FACILITY_BIT)
  150. return -EINVAL;
  151. ptr = (unsigned char *) fac_list + (nr >> 3);
  152. *ptr |= (0x80UL >> (nr & 7));
  153. return 0;
  154. }
  155. static inline int test_kvm_cpu_feat(struct kvm *kvm, unsigned long nr)
  156. {
  157. WARN_ON_ONCE(nr >= KVM_S390_VM_CPU_FEAT_NR_BITS);
  158. return test_bit_inv(nr, kvm->arch.cpu_feat);
  159. }
  160. /* are cpu states controlled by user space */
  161. static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm)
  162. {
  163. return kvm->arch.user_cpu_state_ctrl != 0;
  164. }
  165. /* implemented in interrupt.c */
  166. int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
  167. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu);
  168. enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
  169. int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
  170. void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
  171. void kvm_s390_clear_float_irqs(struct kvm *kvm);
  172. int __must_check kvm_s390_inject_vm(struct kvm *kvm,
  173. struct kvm_s390_interrupt *s390int);
  174. int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
  175. struct kvm_s390_irq *irq);
  176. static inline int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
  177. struct kvm_s390_pgm_info *pgm_info)
  178. {
  179. struct kvm_s390_irq irq = {
  180. .type = KVM_S390_PROGRAM_INT,
  181. .u.pgm = *pgm_info,
  182. };
  183. return kvm_s390_inject_vcpu(vcpu, &irq);
  184. }
  185. static inline int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
  186. {
  187. struct kvm_s390_irq irq = {
  188. .type = KVM_S390_PROGRAM_INT,
  189. .u.pgm.code = code,
  190. };
  191. return kvm_s390_inject_vcpu(vcpu, &irq);
  192. }
  193. struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
  194. u64 isc_mask, u32 schid);
  195. int kvm_s390_reinject_io_int(struct kvm *kvm,
  196. struct kvm_s390_interrupt_info *inti);
  197. int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked);
  198. /* implemented in intercept.c */
  199. u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu);
  200. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
  201. static inline void kvm_s390_rewind_psw(struct kvm_vcpu *vcpu, int ilen)
  202. {
  203. struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
  204. sie_block->gpsw.addr = __rewind_psw(sie_block->gpsw, ilen);
  205. }
  206. static inline void kvm_s390_forward_psw(struct kvm_vcpu *vcpu, int ilen)
  207. {
  208. kvm_s390_rewind_psw(vcpu, -ilen);
  209. }
  210. static inline void kvm_s390_retry_instr(struct kvm_vcpu *vcpu)
  211. {
  212. /* don't inject PER events if we re-execute the instruction */
  213. vcpu->arch.sie_block->icptstatus &= ~0x02;
  214. kvm_s390_rewind_psw(vcpu, kvm_s390_get_ilen(vcpu));
  215. }
  216. int handle_sthyi(struct kvm_vcpu *vcpu);
  217. /* implemented in priv.c */
  218. int is_valid_psw(psw_t *psw);
  219. int kvm_s390_handle_aa(struct kvm_vcpu *vcpu);
  220. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu);
  221. int kvm_s390_handle_e3(struct kvm_vcpu *vcpu);
  222. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu);
  223. int kvm_s390_handle_01(struct kvm_vcpu *vcpu);
  224. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu);
  225. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu);
  226. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu);
  227. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu);
  228. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
  229. int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu);
  230. /* implemented in vsie.c */
  231. int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu);
  232. void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu);
  233. void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
  234. unsigned long end);
  235. void kvm_s390_vsie_init(struct kvm *kvm);
  236. void kvm_s390_vsie_destroy(struct kvm *kvm);
  237. /* implemented in sigp.c */
  238. int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
  239. int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
  240. /* implemented in kvm-s390.c */
  241. void kvm_s390_set_tod_clock(struct kvm *kvm,
  242. const struct kvm_s390_vm_tod_clock *gtod);
  243. long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable);
  244. int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
  245. int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
  246. void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu);
  247. void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu);
  248. void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu);
  249. void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu);
  250. bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu);
  251. void exit_sie(struct kvm_vcpu *vcpu);
  252. void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu);
  253. int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);
  254. void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu);
  255. void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm);
  256. __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu);
  257. /* implemented in diag.c */
  258. int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
  259. static inline void kvm_s390_vcpu_block_all(struct kvm *kvm)
  260. {
  261. int i;
  262. struct kvm_vcpu *vcpu;
  263. WARN_ON(!mutex_is_locked(&kvm->lock));
  264. kvm_for_each_vcpu(i, vcpu, kvm)
  265. kvm_s390_vcpu_block(vcpu);
  266. }
  267. static inline void kvm_s390_vcpu_unblock_all(struct kvm *kvm)
  268. {
  269. int i;
  270. struct kvm_vcpu *vcpu;
  271. kvm_for_each_vcpu(i, vcpu, kvm)
  272. kvm_s390_vcpu_unblock(vcpu);
  273. }
  274. static inline u64 kvm_s390_get_tod_clock_fast(struct kvm *kvm)
  275. {
  276. u64 rc;
  277. preempt_disable();
  278. rc = get_tod_clock_fast() + kvm->arch.epoch;
  279. preempt_enable();
  280. return rc;
  281. }
  282. /**
  283. * kvm_s390_inject_prog_cond - conditionally inject a program check
  284. * @vcpu: virtual cpu
  285. * @rc: original return/error code
  286. *
  287. * This function is supposed to be used after regular guest access functions
  288. * failed, to conditionally inject a program check to a vcpu. The typical
  289. * pattern would look like
  290. *
  291. * rc = write_guest(vcpu, addr, data, len);
  292. * if (rc)
  293. * return kvm_s390_inject_prog_cond(vcpu, rc);
  294. *
  295. * A negative return code from guest access functions implies an internal error
  296. * like e.g. out of memory. In these cases no program check should be injected
  297. * to the guest.
  298. * A positive value implies that an exception happened while accessing a guest's
  299. * memory. In this case all data belonging to the corresponding program check
  300. * has been stored in vcpu->arch.pgm and can be injected with
  301. * kvm_s390_inject_prog_irq().
  302. *
  303. * Returns: - the original @rc value if @rc was negative (internal error)
  304. * - zero if @rc was already zero
  305. * - zero or error code from injecting if @rc was positive
  306. * (program check injected to @vcpu)
  307. */
  308. static inline int kvm_s390_inject_prog_cond(struct kvm_vcpu *vcpu, int rc)
  309. {
  310. if (rc <= 0)
  311. return rc;
  312. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  313. }
  314. int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
  315. struct kvm_s390_irq *s390irq);
  316. /* implemented in interrupt.c */
  317. int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop);
  318. int psw_extint_disabled(struct kvm_vcpu *vcpu);
  319. void kvm_s390_destroy_adapters(struct kvm *kvm);
  320. int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu);
  321. extern struct kvm_device_ops kvm_flic_ops;
  322. int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu);
  323. void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu);
  324. int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu,
  325. void __user *buf, int len);
  326. int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu,
  327. __u8 __user *buf, int len);
  328. void kvm_s390_gisa_init(struct kvm *kvm);
  329. void kvm_s390_gisa_clear(struct kvm *kvm);
  330. void kvm_s390_gisa_destroy(struct kvm *kvm);
  331. /* implemented in guestdbg.c */
  332. void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
  333. void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu);
  334. void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu);
  335. int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
  336. struct kvm_guest_debug *dbg);
  337. void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu);
  338. void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu);
  339. int kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu *vcpu);
  340. int kvm_s390_handle_per_event(struct kvm_vcpu *vcpu);
  341. /* support for Basic/Extended SCA handling */
  342. static inline union ipte_control *kvm_s390_get_ipte_control(struct kvm *kvm)
  343. {
  344. struct bsca_block *sca = kvm->arch.sca; /* SCA version doesn't matter */
  345. return &sca->ipte_control;
  346. }
  347. static inline int kvm_s390_use_sca_entries(void)
  348. {
  349. /*
  350. * Without SIGP interpretation, only SRS interpretation (if available)
  351. * might use the entries. By not setting the entries and keeping them
  352. * invalid, hardware will not access them but intercept.
  353. */
  354. return sclp.has_sigpif;
  355. }
  356. void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
  357. struct mcck_volatile_info *mcck_info);
  358. /**
  359. * kvm_s390_vcpu_crypto_reset_all
  360. *
  361. * Reset the crypto attributes for each vcpu. This can be done while the vcpus
  362. * are running as each vcpu will be removed from SIE before resetting the crypt
  363. * attributes and restored to SIE afterward.
  364. *
  365. * Note: The kvm->lock must be held while calling this function
  366. *
  367. * @kvm: the KVM guest
  368. */
  369. void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm);
  370. #endif