gaccess.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * access guest memory
  4. *
  5. * Copyright IBM Corp. 2008, 2014
  6. *
  7. * Author(s): Carsten Otte <[email protected]>
  8. */
  9. #ifndef __KVM_S390_GACCESS_H
  10. #define __KVM_S390_GACCESS_H
  11. #include <linux/compiler.h>
  12. #include <linux/kvm_host.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/ptrace.h>
  15. #include "kvm-s390.h"
  16. /**
  17. * kvm_s390_real_to_abs - convert guest real address to guest absolute address
  18. * @prefix - guest prefix
  19. * @gra - guest real address
  20. *
  21. * Returns the guest absolute address that corresponds to the passed guest real
  22. * address @gra of by applying the given prefix.
  23. */
  24. static inline unsigned long _kvm_s390_real_to_abs(u32 prefix, unsigned long gra)
  25. {
  26. if (gra < 2 * PAGE_SIZE)
  27. gra += prefix;
  28. else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)
  29. gra -= prefix;
  30. return gra;
  31. }
  32. /**
  33. * kvm_s390_real_to_abs - convert guest real address to guest absolute address
  34. * @vcpu - guest virtual cpu
  35. * @gra - guest real address
  36. *
  37. * Returns the guest absolute address that corresponds to the passed guest real
  38. * address @gra of a virtual guest cpu by applying its prefix.
  39. */
  40. static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
  41. unsigned long gra)
  42. {
  43. return _kvm_s390_real_to_abs(kvm_s390_get_prefix(vcpu), gra);
  44. }
  45. /**
  46. * _kvm_s390_logical_to_effective - convert guest logical to effective address
  47. * @psw: psw of the guest
  48. * @ga: guest logical address
  49. *
  50. * Convert a guest logical address to an effective address by applying the
  51. * rules of the addressing mode defined by bits 31 and 32 of the given PSW
  52. * (extendended/basic addressing mode).
  53. *
  54. * Depending on the addressing mode, the upper 40 bits (24 bit addressing
  55. * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing
  56. * mode) of @ga will be zeroed and the remaining bits will be returned.
  57. */
  58. static inline unsigned long _kvm_s390_logical_to_effective(psw_t *psw,
  59. unsigned long ga)
  60. {
  61. if (psw_bits(*psw).eaba == PSW_BITS_AMODE_64BIT)
  62. return ga;
  63. if (psw_bits(*psw).eaba == PSW_BITS_AMODE_31BIT)
  64. return ga & ((1UL << 31) - 1);
  65. return ga & ((1UL << 24) - 1);
  66. }
  67. /**
  68. * kvm_s390_logical_to_effective - convert guest logical to effective address
  69. * @vcpu: guest virtual cpu
  70. * @ga: guest logical address
  71. *
  72. * Convert a guest vcpu logical address to a guest vcpu effective address by
  73. * applying the rules of the vcpu's addressing mode defined by PSW bits 31
  74. * and 32 (extendended/basic addressing mode).
  75. *
  76. * Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing
  77. * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode)
  78. * of @ga will be zeroed and the remaining bits will be returned.
  79. */
  80. static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,
  81. unsigned long ga)
  82. {
  83. return _kvm_s390_logical_to_effective(&vcpu->arch.sie_block->gpsw, ga);
  84. }
  85. /*
  86. * put_guest_lc, read_guest_lc and write_guest_lc are guest access functions
  87. * which shall only be used to access the lowcore of a vcpu.
  88. * These functions should be used for e.g. interrupt handlers where no
  89. * guest memory access protection facilities, like key or low address
  90. * protection, are applicable.
  91. * At a later point guest vcpu lowcore access should happen via pinned
  92. * prefix pages, so that these pages can be accessed directly via the
  93. * kernel mapping. All of these *_lc functions can be removed then.
  94. */
  95. /**
  96. * put_guest_lc - write a simple variable to a guest vcpu's lowcore
  97. * @vcpu: virtual cpu
  98. * @x: value to copy to guest
  99. * @gra: vcpu's destination guest real address
  100. *
  101. * Copies a simple value from kernel space to a guest vcpu's lowcore.
  102. * The size of the variable may be 1, 2, 4 or 8 bytes. The destination
  103. * must be located in the vcpu's lowcore. Otherwise the result is undefined.
  104. *
  105. * Returns zero on success or -EFAULT on error.
  106. *
  107. * Note: an error indicates that either the kernel is out of memory or
  108. * the guest memory mapping is broken. In any case the best solution
  109. * would be to terminate the guest.
  110. * It is wrong to inject a guest exception.
  111. */
  112. #define put_guest_lc(vcpu, x, gra) \
  113. ({ \
  114. struct kvm_vcpu *__vcpu = (vcpu); \
  115. __typeof__(*(gra)) __x = (x); \
  116. unsigned long __gpa; \
  117. \
  118. __gpa = (unsigned long)(gra); \
  119. __gpa += kvm_s390_get_prefix(__vcpu); \
  120. kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \
  121. })
  122. /**
  123. * write_guest_lc - copy data from kernel space to guest vcpu's lowcore
  124. * @vcpu: virtual cpu
  125. * @gra: vcpu's source guest real address
  126. * @data: source address in kernel space
  127. * @len: number of bytes to copy
  128. *
  129. * Copy data from kernel space to guest vcpu's lowcore. The entire range must
  130. * be located within the vcpu's lowcore, otherwise the result is undefined.
  131. *
  132. * Returns zero on success or -EFAULT on error.
  133. *
  134. * Note: an error indicates that either the kernel is out of memory or
  135. * the guest memory mapping is broken. In any case the best solution
  136. * would be to terminate the guest.
  137. * It is wrong to inject a guest exception.
  138. */
  139. static inline __must_check
  140. int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
  141. unsigned long len)
  142. {
  143. unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
  144. return kvm_write_guest(vcpu->kvm, gpa, data, len);
  145. }
  146. /**
  147. * read_guest_lc - copy data from guest vcpu's lowcore to kernel space
  148. * @vcpu: virtual cpu
  149. * @gra: vcpu's source guest real address
  150. * @data: destination address in kernel space
  151. * @len: number of bytes to copy
  152. *
  153. * Copy data from guest vcpu's lowcore to kernel space. The entire range must
  154. * be located within the vcpu's lowcore, otherwise the result is undefined.
  155. *
  156. * Returns zero on success or -EFAULT on error.
  157. *
  158. * Note: an error indicates that either the kernel is out of memory or
  159. * the guest memory mapping is broken. In any case the best solution
  160. * would be to terminate the guest.
  161. * It is wrong to inject a guest exception.
  162. */
  163. static inline __must_check
  164. int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
  165. unsigned long len)
  166. {
  167. unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
  168. return kvm_read_guest(vcpu->kvm, gpa, data, len);
  169. }
  170. enum gacc_mode {
  171. GACC_FETCH,
  172. GACC_STORE,
  173. GACC_IFETCH,
  174. };
  175. int guest_translate_address_with_key(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
  176. unsigned long *gpa, enum gacc_mode mode,
  177. u8 access_key);
  178. int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
  179. unsigned long length, enum gacc_mode mode, u8 access_key);
  180. int check_gpa_range(struct kvm *kvm, unsigned long gpa, unsigned long length,
  181. enum gacc_mode mode, u8 access_key);
  182. int access_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, void *data,
  183. unsigned long len, enum gacc_mode mode, u8 access_key);
  184. int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
  185. void *data, unsigned long len, enum gacc_mode mode,
  186. u8 access_key);
  187. int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
  188. void *data, unsigned long len, enum gacc_mode mode);
  189. /**
  190. * write_guest_with_key - copy data from kernel space to guest space
  191. * @vcpu: virtual cpu
  192. * @ga: guest address
  193. * @ar: access register
  194. * @data: source address in kernel space
  195. * @len: number of bytes to copy
  196. * @access_key: access key the storage key needs to match
  197. *
  198. * Copy @len bytes from @data (kernel space) to @ga (guest address).
  199. * In order to copy data to guest space the PSW of the vcpu is inspected:
  200. * If DAT is off data will be copied to guest real or absolute memory.
  201. * If DAT is on data will be copied to the address space as specified by
  202. * the address space bits of the PSW:
  203. * Primary, secondary, home space or access register mode.
  204. * The addressing mode of the PSW is also inspected, so that address wrap
  205. * around is taken into account for 24-, 31- and 64-bit addressing mode,
  206. * if the to be copied data crosses page boundaries in guest address space.
  207. * In addition low address, DAT and key protection checks are performed before
  208. * copying any data.
  209. *
  210. * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
  211. * In case of an access exception (e.g. protection exception) pgm will contain
  212. * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
  213. * will inject a correct exception into the guest.
  214. * If no access exception happened, the contents of pgm are undefined when
  215. * this function returns.
  216. *
  217. * Returns: - zero on success
  218. * - a negative value if e.g. the guest mapping is broken or in
  219. * case of out-of-memory. In this case the contents of pgm are
  220. * undefined. Also parts of @data may have been copied to guest
  221. * space.
  222. * - a positive value if an access exception happened. In this case
  223. * the returned value is the program interruption code and the
  224. * contents of pgm may be used to inject an exception into the
  225. * guest. No data has been copied to guest space.
  226. *
  227. * Note: in case an access exception is recognized no data has been copied to
  228. * guest space (this is also true, if the to be copied data would cross
  229. * one or more page boundaries in guest space).
  230. * Therefore this function may be used for nullifying and suppressing
  231. * instruction emulation.
  232. * It may also be used for terminating instructions, if it is undefined
  233. * if data has been changed in guest space in case of an exception.
  234. */
  235. static inline __must_check
  236. int write_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
  237. void *data, unsigned long len, u8 access_key)
  238. {
  239. return access_guest_with_key(vcpu, ga, ar, data, len, GACC_STORE,
  240. access_key);
  241. }
  242. /**
  243. * write_guest - copy data from kernel space to guest space
  244. * @vcpu: virtual cpu
  245. * @ga: guest address
  246. * @ar: access register
  247. * @data: source address in kernel space
  248. * @len: number of bytes to copy
  249. *
  250. * The behaviour of write_guest is identical to write_guest_with_key, except
  251. * that the PSW access key is used instead of an explicit argument.
  252. */
  253. static inline __must_check
  254. int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  255. unsigned long len)
  256. {
  257. u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
  258. return write_guest_with_key(vcpu, ga, ar, data, len, access_key);
  259. }
  260. /**
  261. * read_guest_with_key - copy data from guest space to kernel space
  262. * @vcpu: virtual cpu
  263. * @ga: guest address
  264. * @ar: access register
  265. * @data: destination address in kernel space
  266. * @len: number of bytes to copy
  267. * @access_key: access key the storage key needs to match
  268. *
  269. * Copy @len bytes from @ga (guest address) to @data (kernel space).
  270. *
  271. * The behaviour of read_guest_with_key is identical to write_guest_with_key,
  272. * except that data will be copied from guest space to kernel space.
  273. */
  274. static inline __must_check
  275. int read_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
  276. void *data, unsigned long len, u8 access_key)
  277. {
  278. return access_guest_with_key(vcpu, ga, ar, data, len, GACC_FETCH,
  279. access_key);
  280. }
  281. /**
  282. * read_guest - copy data from guest space to kernel space
  283. * @vcpu: virtual cpu
  284. * @ga: guest address
  285. * @ar: access register
  286. * @data: destination address in kernel space
  287. * @len: number of bytes to copy
  288. *
  289. * Copy @len bytes from @ga (guest address) to @data (kernel space).
  290. *
  291. * The behaviour of read_guest is identical to read_guest_with_key, except
  292. * that the PSW access key is used instead of an explicit argument.
  293. */
  294. static inline __must_check
  295. int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  296. unsigned long len)
  297. {
  298. u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
  299. return read_guest_with_key(vcpu, ga, ar, data, len, access_key);
  300. }
  301. /**
  302. * read_guest_instr - copy instruction data from guest space to kernel space
  303. * @vcpu: virtual cpu
  304. * @ga: guest address
  305. * @data: destination address in kernel space
  306. * @len: number of bytes to copy
  307. *
  308. * Copy @len bytes from the given address (guest space) to @data (kernel
  309. * space).
  310. *
  311. * The behaviour of read_guest_instr is identical to read_guest, except that
  312. * instruction data will be read from primary space when in home-space or
  313. * address-space mode.
  314. */
  315. static inline __must_check
  316. int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
  317. unsigned long len)
  318. {
  319. u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
  320. return access_guest_with_key(vcpu, ga, 0, data, len, GACC_IFETCH,
  321. access_key);
  322. }
  323. /**
  324. * write_guest_abs - copy data from kernel space to guest space absolute
  325. * @vcpu: virtual cpu
  326. * @gpa: guest physical (absolute) address
  327. * @data: source address in kernel space
  328. * @len: number of bytes to copy
  329. *
  330. * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
  331. * It is up to the caller to ensure that the entire guest memory range is
  332. * valid memory before calling this function.
  333. * Guest low address and key protection are not checked.
  334. *
  335. * Returns zero on success or -EFAULT on error.
  336. *
  337. * If an error occurs data may have been copied partially to guest memory.
  338. */
  339. static inline __must_check
  340. int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
  341. unsigned long len)
  342. {
  343. return kvm_write_guest(vcpu->kvm, gpa, data, len);
  344. }
  345. /**
  346. * read_guest_abs - copy data from guest space absolute to kernel space
  347. * @vcpu: virtual cpu
  348. * @gpa: guest physical (absolute) address
  349. * @data: destination address in kernel space
  350. * @len: number of bytes to copy
  351. *
  352. * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
  353. * It is up to the caller to ensure that the entire guest memory range is
  354. * valid memory before calling this function.
  355. * Guest key protection is not checked.
  356. *
  357. * Returns zero on success or -EFAULT on error.
  358. *
  359. * If an error occurs data may have been copied partially to kernel space.
  360. */
  361. static inline __must_check
  362. int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
  363. unsigned long len)
  364. {
  365. return kvm_read_guest(vcpu->kvm, gpa, data, len);
  366. }
  367. /**
  368. * write_guest_real - copy data from kernel space to guest space real
  369. * @vcpu: virtual cpu
  370. * @gra: guest real address
  371. * @data: source address in kernel space
  372. * @len: number of bytes to copy
  373. *
  374. * Copy @len bytes from @data (kernel space) to @gra (guest real address).
  375. * It is up to the caller to ensure that the entire guest memory range is
  376. * valid memory before calling this function.
  377. * Guest low address and key protection are not checked.
  378. *
  379. * Returns zero on success or -EFAULT on error.
  380. *
  381. * If an error occurs data may have been copied partially to guest memory.
  382. */
  383. static inline __must_check
  384. int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
  385. unsigned long len)
  386. {
  387. return access_guest_real(vcpu, gra, data, len, 1);
  388. }
  389. /**
  390. * read_guest_real - copy data from guest space real to kernel space
  391. * @vcpu: virtual cpu
  392. * @gra: guest real address
  393. * @data: destination address in kernel space
  394. * @len: number of bytes to copy
  395. *
  396. * Copy @len bytes from @gra (guest real address) to @data (kernel space).
  397. * It is up to the caller to ensure that the entire guest memory range is
  398. * valid memory before calling this function.
  399. * Guest key protection is not checked.
  400. *
  401. * Returns zero on success or -EFAULT on error.
  402. *
  403. * If an error occurs data may have been copied partially to kernel space.
  404. */
  405. static inline __must_check
  406. int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
  407. unsigned long len)
  408. {
  409. return access_guest_real(vcpu, gra, data, len, 0);
  410. }
  411. void ipte_lock(struct kvm *kvm);
  412. void ipte_unlock(struct kvm *kvm);
  413. int ipte_lock_held(struct kvm *kvm);
  414. int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);
  415. /* MVPG PEI indication bits */
  416. #define PEI_DAT_PROT 2
  417. #define PEI_NOT_PTE 4
  418. int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,
  419. unsigned long saddr, unsigned long *datptr);
  420. #endif /* __KVM_S390_GACCESS_H */