kvm-s390.h 13 KB

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