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