gaccess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <cotte@de.ibm.com>
  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(struct kvm_vcpu *vcpu, unsigned long gva,
  176. u8 ar, unsigned long *gpa, enum gacc_mode mode);
  177. int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
  178. unsigned long length, enum gacc_mode mode);
  179. int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  180. unsigned long len, enum gacc_mode mode);
  181. int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
  182. void *data, unsigned long len, enum gacc_mode mode);
  183. /**
  184. * write_guest - copy data from kernel space to guest space
  185. * @vcpu: virtual cpu
  186. * @ga: guest address
  187. * @ar: access register
  188. * @data: source address in kernel space
  189. * @len: number of bytes to copy
  190. *
  191. * Copy @len bytes from @data (kernel space) to @ga (guest address).
  192. * In order to copy data to guest space the PSW of the vcpu is inspected:
  193. * If DAT is off data will be copied to guest real or absolute memory.
  194. * If DAT is on data will be copied to the address space as specified by
  195. * the address space bits of the PSW:
  196. * Primary, secondary, home space or access register mode.
  197. * The addressing mode of the PSW is also inspected, so that address wrap
  198. * around is taken into account for 24-, 31- and 64-bit addressing mode,
  199. * if the to be copied data crosses page boundaries in guest address space.
  200. * In addition also low address and DAT protection are inspected before
  201. * copying any data (key protection is currently not implemented).
  202. *
  203. * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
  204. * In case of an access exception (e.g. protection exception) pgm will contain
  205. * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
  206. * will inject a correct exception into the guest.
  207. * If no access exception happened, the contents of pgm are undefined when
  208. * this function returns.
  209. *
  210. * Returns: - zero on success
  211. * - a negative value if e.g. the guest mapping is broken or in
  212. * case of out-of-memory. In this case the contents of pgm are
  213. * undefined. Also parts of @data may have been copied to guest
  214. * space.
  215. * - a positive value if an access exception happened. In this case
  216. * the returned value is the program interruption code and the
  217. * contents of pgm may be used to inject an exception into the
  218. * guest. No data has been copied to guest space.
  219. *
  220. * Note: in case an access exception is recognized no data has been copied to
  221. * guest space (this is also true, if the to be copied data would cross
  222. * one or more page boundaries in guest space).
  223. * Therefore this function may be used for nullifying and suppressing
  224. * instruction emulation.
  225. * It may also be used for terminating instructions, if it is undefined
  226. * if data has been changed in guest space in case of an exception.
  227. */
  228. static inline __must_check
  229. int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  230. unsigned long len)
  231. {
  232. return access_guest(vcpu, ga, ar, data, len, GACC_STORE);
  233. }
  234. /**
  235. * read_guest - copy data from guest space to kernel space
  236. * @vcpu: virtual cpu
  237. * @ga: guest address
  238. * @ar: access register
  239. * @data: destination address in kernel space
  240. * @len: number of bytes to copy
  241. *
  242. * Copy @len bytes from @ga (guest address) to @data (kernel space).
  243. *
  244. * The behaviour of read_guest is identical to write_guest, except that
  245. * data will be copied from guest space to kernel space.
  246. */
  247. static inline __must_check
  248. int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
  249. unsigned long len)
  250. {
  251. return access_guest(vcpu, ga, ar, data, len, GACC_FETCH);
  252. }
  253. /**
  254. * read_guest_instr - copy instruction data from guest space to kernel space
  255. * @vcpu: virtual cpu
  256. * @ga: guest address
  257. * @data: destination address in kernel space
  258. * @len: number of bytes to copy
  259. *
  260. * Copy @len bytes from the given address (guest space) to @data (kernel
  261. * space).
  262. *
  263. * The behaviour of read_guest_instr is identical to read_guest, except that
  264. * instruction data will be read from primary space when in home-space or
  265. * address-space mode.
  266. */
  267. static inline __must_check
  268. int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
  269. unsigned long len)
  270. {
  271. return access_guest(vcpu, ga, 0, data, len, GACC_IFETCH);
  272. }
  273. /**
  274. * write_guest_abs - copy data from kernel space to guest space absolute
  275. * @vcpu: virtual cpu
  276. * @gpa: guest physical (absolute) address
  277. * @data: source address in kernel space
  278. * @len: number of bytes to copy
  279. *
  280. * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
  281. * It is up to the caller to ensure that the entire guest memory range is
  282. * valid memory before calling this function.
  283. * Guest low address and key protection are not checked.
  284. *
  285. * Returns zero on success or -EFAULT on error.
  286. *
  287. * If an error occurs data may have been copied partially to guest memory.
  288. */
  289. static inline __must_check
  290. int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
  291. unsigned long len)
  292. {
  293. return kvm_write_guest(vcpu->kvm, gpa, data, len);
  294. }
  295. /**
  296. * read_guest_abs - copy data from guest space absolute to kernel space
  297. * @vcpu: virtual cpu
  298. * @gpa: guest physical (absolute) address
  299. * @data: destination address in kernel space
  300. * @len: number of bytes to copy
  301. *
  302. * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
  303. * It is up to the caller to ensure that the entire guest memory range is
  304. * valid memory before calling this function.
  305. * Guest key protection is not checked.
  306. *
  307. * Returns zero on success or -EFAULT on error.
  308. *
  309. * If an error occurs data may have been copied partially to kernel space.
  310. */
  311. static inline __must_check
  312. int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
  313. unsigned long len)
  314. {
  315. return kvm_read_guest(vcpu->kvm, gpa, data, len);
  316. }
  317. /**
  318. * write_guest_real - copy data from kernel space to guest space real
  319. * @vcpu: virtual cpu
  320. * @gra: guest real address
  321. * @data: source address in kernel space
  322. * @len: number of bytes to copy
  323. *
  324. * Copy @len bytes from @data (kernel space) to @gra (guest real address).
  325. * It is up to the caller to ensure that the entire guest memory range is
  326. * valid memory before calling this function.
  327. * Guest low address and key protection are not checked.
  328. *
  329. * Returns zero on success or -EFAULT on error.
  330. *
  331. * If an error occurs data may have been copied partially to guest memory.
  332. */
  333. static inline __must_check
  334. int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
  335. unsigned long len)
  336. {
  337. return access_guest_real(vcpu, gra, data, len, 1);
  338. }
  339. /**
  340. * read_guest_real - copy data from guest space real to kernel space
  341. * @vcpu: virtual cpu
  342. * @gra: guest real address
  343. * @data: destination address in kernel space
  344. * @len: number of bytes to copy
  345. *
  346. * Copy @len bytes from @gra (guest real address) to @data (kernel space).
  347. * It is up to the caller to ensure that the entire guest memory range is
  348. * valid memory before calling this function.
  349. * Guest key protection is not checked.
  350. *
  351. * Returns zero on success or -EFAULT on error.
  352. *
  353. * If an error occurs data may have been copied partially to kernel space.
  354. */
  355. static inline __must_check
  356. int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
  357. unsigned long len)
  358. {
  359. return access_guest_real(vcpu, gra, data, len, 0);
  360. }
  361. void ipte_lock(struct kvm_vcpu *vcpu);
  362. void ipte_unlock(struct kvm_vcpu *vcpu);
  363. int ipte_lock_held(struct kvm_vcpu *vcpu);
  364. int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);
  365. /* MVPG PEI indication bits */
  366. #define PEI_DAT_PROT 2
  367. #define PEI_NOT_PTE 4
  368. int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,
  369. unsigned long saddr, unsigned long *datptr);
  370. #endif /* __KVM_S390_GACCESS_H */