kvm-s390.h 13 KB

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