gaccess.h 13 KB

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