Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * access guest memory
0004  *
0005  * Copyright IBM Corp. 2008, 2014
0006  *
0007  *    Author(s): Carsten Otte <cotte@de.ibm.com>
0008  */
0009 
0010 #ifndef __KVM_S390_GACCESS_H
0011 #define __KVM_S390_GACCESS_H
0012 
0013 #include <linux/compiler.h>
0014 #include <linux/kvm_host.h>
0015 #include <linux/uaccess.h>
0016 #include <linux/ptrace.h>
0017 #include "kvm-s390.h"
0018 
0019 /**
0020  * kvm_s390_real_to_abs - convert guest real address to guest absolute address
0021  * @prefix - guest prefix
0022  * @gra - guest real address
0023  *
0024  * Returns the guest absolute address that corresponds to the passed guest real
0025  * address @gra of by applying the given prefix.
0026  */
0027 static inline unsigned long _kvm_s390_real_to_abs(u32 prefix, unsigned long gra)
0028 {
0029     if (gra < 2 * PAGE_SIZE)
0030         gra += prefix;
0031     else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)
0032         gra -= prefix;
0033     return gra;
0034 }
0035 
0036 /**
0037  * kvm_s390_real_to_abs - convert guest real address to guest absolute address
0038  * @vcpu - guest virtual cpu
0039  * @gra - guest real address
0040  *
0041  * Returns the guest absolute address that corresponds to the passed guest real
0042  * address @gra of a virtual guest cpu by applying its prefix.
0043  */
0044 static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
0045                          unsigned long gra)
0046 {
0047     return _kvm_s390_real_to_abs(kvm_s390_get_prefix(vcpu), gra);
0048 }
0049 
0050 /**
0051  * _kvm_s390_logical_to_effective - convert guest logical to effective address
0052  * @psw: psw of the guest
0053  * @ga: guest logical address
0054  *
0055  * Convert a guest logical address to an effective address by applying the
0056  * rules of the addressing mode defined by bits 31 and 32 of the given PSW
0057  * (extendended/basic addressing mode).
0058  *
0059  * Depending on the addressing mode, the upper 40 bits (24 bit addressing
0060  * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing
0061  * mode) of @ga will be zeroed and the remaining bits will be returned.
0062  */
0063 static inline unsigned long _kvm_s390_logical_to_effective(psw_t *psw,
0064                                unsigned long ga)
0065 {
0066     if (psw_bits(*psw).eaba == PSW_BITS_AMODE_64BIT)
0067         return ga;
0068     if (psw_bits(*psw).eaba == PSW_BITS_AMODE_31BIT)
0069         return ga & ((1UL << 31) - 1);
0070     return ga & ((1UL << 24) - 1);
0071 }
0072 
0073 /**
0074  * kvm_s390_logical_to_effective - convert guest logical to effective address
0075  * @vcpu: guest virtual cpu
0076  * @ga: guest logical address
0077  *
0078  * Convert a guest vcpu logical address to a guest vcpu effective address by
0079  * applying the rules of the vcpu's addressing mode defined by PSW bits 31
0080  * and 32 (extendended/basic addressing mode).
0081  *
0082  * Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing
0083  * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode)
0084  * of @ga will be zeroed and the remaining bits will be returned.
0085  */
0086 static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,
0087                               unsigned long ga)
0088 {
0089     return _kvm_s390_logical_to_effective(&vcpu->arch.sie_block->gpsw, ga);
0090 }
0091 
0092 /*
0093  * put_guest_lc, read_guest_lc and write_guest_lc are guest access functions
0094  * which shall only be used to access the lowcore of a vcpu.
0095  * These functions should be used for e.g. interrupt handlers where no
0096  * guest memory access protection facilities, like key or low address
0097  * protection, are applicable.
0098  * At a later point guest vcpu lowcore access should happen via pinned
0099  * prefix pages, so that these pages can be accessed directly via the
0100  * kernel mapping. All of these *_lc functions can be removed then.
0101  */
0102 
0103 /**
0104  * put_guest_lc - write a simple variable to a guest vcpu's lowcore
0105  * @vcpu: virtual cpu
0106  * @x: value to copy to guest
0107  * @gra: vcpu's destination guest real address
0108  *
0109  * Copies a simple value from kernel space to a guest vcpu's lowcore.
0110  * The size of the variable may be 1, 2, 4 or 8 bytes. The destination
0111  * must be located in the vcpu's lowcore. Otherwise the result is undefined.
0112  *
0113  * Returns zero on success or -EFAULT on error.
0114  *
0115  * Note: an error indicates that either the kernel is out of memory or
0116  *   the guest memory mapping is broken. In any case the best solution
0117  *   would be to terminate the guest.
0118  *   It is wrong to inject a guest exception.
0119  */
0120 #define put_guest_lc(vcpu, x, gra)              \
0121 ({                              \
0122     struct kvm_vcpu *__vcpu = (vcpu);           \
0123     __typeof__(*(gra)) __x = (x);               \
0124     unsigned long __gpa;                    \
0125                                 \
0126     __gpa = (unsigned long)(gra);               \
0127     __gpa += kvm_s390_get_prefix(__vcpu);           \
0128     kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \
0129 })
0130 
0131 /**
0132  * write_guest_lc - copy data from kernel space to guest vcpu's lowcore
0133  * @vcpu: virtual cpu
0134  * @gra: vcpu's source guest real address
0135  * @data: source address in kernel space
0136  * @len: number of bytes to copy
0137  *
0138  * Copy data from kernel space to guest vcpu's lowcore. The entire range must
0139  * be located within the vcpu's lowcore, otherwise the result is undefined.
0140  *
0141  * Returns zero on success or -EFAULT on error.
0142  *
0143  * Note: an error indicates that either the kernel is out of memory or
0144  *   the guest memory mapping is broken. In any case the best solution
0145  *   would be to terminate the guest.
0146  *   It is wrong to inject a guest exception.
0147  */
0148 static inline __must_check
0149 int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
0150            unsigned long len)
0151 {
0152     unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
0153 
0154     return kvm_write_guest(vcpu->kvm, gpa, data, len);
0155 }
0156 
0157 /**
0158  * read_guest_lc - copy data from guest vcpu's lowcore to kernel space
0159  * @vcpu: virtual cpu
0160  * @gra: vcpu's source guest real address
0161  * @data: destination address in kernel space
0162  * @len: number of bytes to copy
0163  *
0164  * Copy data from guest vcpu's lowcore to kernel space. The entire range must
0165  * be located within the vcpu's lowcore, otherwise the result is undefined.
0166  *
0167  * Returns zero on success or -EFAULT on error.
0168  *
0169  * Note: an error indicates that either the kernel is out of memory or
0170  *   the guest memory mapping is broken. In any case the best solution
0171  *   would be to terminate the guest.
0172  *   It is wrong to inject a guest exception.
0173  */
0174 static inline __must_check
0175 int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
0176           unsigned long len)
0177 {
0178     unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
0179 
0180     return kvm_read_guest(vcpu->kvm, gpa, data, len);
0181 }
0182 
0183 enum gacc_mode {
0184     GACC_FETCH,
0185     GACC_STORE,
0186     GACC_IFETCH,
0187 };
0188 
0189 int guest_translate_address_with_key(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
0190                      unsigned long *gpa, enum gacc_mode mode,
0191                      u8 access_key);
0192 
0193 int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
0194             unsigned long length, enum gacc_mode mode, u8 access_key);
0195 
0196 int check_gpa_range(struct kvm *kvm, unsigned long gpa, unsigned long length,
0197             enum gacc_mode mode, u8 access_key);
0198 
0199 int access_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, void *data,
0200                   unsigned long len, enum gacc_mode mode, u8 access_key);
0201 
0202 int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
0203               void *data, unsigned long len, enum gacc_mode mode,
0204               u8 access_key);
0205 
0206 int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
0207               void *data, unsigned long len, enum gacc_mode mode);
0208 
0209 /**
0210  * write_guest_with_key - copy data from kernel space to guest space
0211  * @vcpu: virtual cpu
0212  * @ga: guest address
0213  * @ar: access register
0214  * @data: source address in kernel space
0215  * @len: number of bytes to copy
0216  * @access_key: access key the storage key needs to match
0217  *
0218  * Copy @len bytes from @data (kernel space) to @ga (guest address).
0219  * In order to copy data to guest space the PSW of the vcpu is inspected:
0220  * If DAT is off data will be copied to guest real or absolute memory.
0221  * If DAT is on data will be copied to the address space as specified by
0222  * the address space bits of the PSW:
0223  * Primary, secondary, home space or access register mode.
0224  * The addressing mode of the PSW is also inspected, so that address wrap
0225  * around is taken into account for 24-, 31- and 64-bit addressing mode,
0226  * if the to be copied data crosses page boundaries in guest address space.
0227  * In addition low address, DAT and key protection checks are performed before
0228  * copying any data.
0229  *
0230  * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
0231  * In case of an access exception (e.g. protection exception) pgm will contain
0232  * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
0233  * will inject a correct exception into the guest.
0234  * If no access exception happened, the contents of pgm are undefined when
0235  * this function returns.
0236  *
0237  * Returns:  - zero on success
0238  *       - a negative value if e.g. the guest mapping is broken or in
0239  *         case of out-of-memory. In this case the contents of pgm are
0240  *         undefined. Also parts of @data may have been copied to guest
0241  *         space.
0242  *       - a positive value if an access exception happened. In this case
0243  *         the returned value is the program interruption code and the
0244  *         contents of pgm may be used to inject an exception into the
0245  *         guest. No data has been copied to guest space.
0246  *
0247  * Note: in case an access exception is recognized no data has been copied to
0248  *   guest space (this is also true, if the to be copied data would cross
0249  *   one or more page boundaries in guest space).
0250  *   Therefore this function may be used for nullifying and suppressing
0251  *   instruction emulation.
0252  *   It may also be used for terminating instructions, if it is undefined
0253  *   if data has been changed in guest space in case of an exception.
0254  */
0255 static inline __must_check
0256 int write_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
0257              void *data, unsigned long len, u8 access_key)
0258 {
0259     return access_guest_with_key(vcpu, ga, ar, data, len, GACC_STORE,
0260                      access_key);
0261 }
0262 
0263 /**
0264  * write_guest - copy data from kernel space to guest space
0265  * @vcpu: virtual cpu
0266  * @ga: guest address
0267  * @ar: access register
0268  * @data: source address in kernel space
0269  * @len: number of bytes to copy
0270  *
0271  * The behaviour of write_guest is identical to write_guest_with_key, except
0272  * that the PSW access key is used instead of an explicit argument.
0273  */
0274 static inline __must_check
0275 int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
0276         unsigned long len)
0277 {
0278     u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
0279 
0280     return write_guest_with_key(vcpu, ga, ar, data, len, access_key);
0281 }
0282 
0283 /**
0284  * read_guest_with_key - copy data from guest space to kernel space
0285  * @vcpu: virtual cpu
0286  * @ga: guest address
0287  * @ar: access register
0288  * @data: destination address in kernel space
0289  * @len: number of bytes to copy
0290  * @access_key: access key the storage key needs to match
0291  *
0292  * Copy @len bytes from @ga (guest address) to @data (kernel space).
0293  *
0294  * The behaviour of read_guest_with_key is identical to write_guest_with_key,
0295  * except that data will be copied from guest space to kernel space.
0296  */
0297 static inline __must_check
0298 int read_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
0299             void *data, unsigned long len, u8 access_key)
0300 {
0301     return access_guest_with_key(vcpu, ga, ar, data, len, GACC_FETCH,
0302                      access_key);
0303 }
0304 
0305 /**
0306  * read_guest - copy data from guest space to kernel space
0307  * @vcpu: virtual cpu
0308  * @ga: guest address
0309  * @ar: access register
0310  * @data: destination address in kernel space
0311  * @len: number of bytes to copy
0312  *
0313  * Copy @len bytes from @ga (guest address) to @data (kernel space).
0314  *
0315  * The behaviour of read_guest is identical to read_guest_with_key, except
0316  * that the PSW access key is used instead of an explicit argument.
0317  */
0318 static inline __must_check
0319 int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
0320            unsigned long len)
0321 {
0322     u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
0323 
0324     return read_guest_with_key(vcpu, ga, ar, data, len, access_key);
0325 }
0326 
0327 /**
0328  * read_guest_instr - copy instruction data from guest space to kernel space
0329  * @vcpu: virtual cpu
0330  * @ga: guest address
0331  * @data: destination address in kernel space
0332  * @len: number of bytes to copy
0333  *
0334  * Copy @len bytes from the given address (guest space) to @data (kernel
0335  * space).
0336  *
0337  * The behaviour of read_guest_instr is identical to read_guest, except that
0338  * instruction data will be read from primary space when in home-space or
0339  * address-space mode.
0340  */
0341 static inline __must_check
0342 int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
0343              unsigned long len)
0344 {
0345     u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
0346 
0347     return access_guest_with_key(vcpu, ga, 0, data, len, GACC_IFETCH,
0348                      access_key);
0349 }
0350 
0351 /**
0352  * write_guest_abs - copy data from kernel space to guest space absolute
0353  * @vcpu: virtual cpu
0354  * @gpa: guest physical (absolute) address
0355  * @data: source address in kernel space
0356  * @len: number of bytes to copy
0357  *
0358  * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
0359  * It is up to the caller to ensure that the entire guest memory range is
0360  * valid memory before calling this function.
0361  * Guest low address and key protection are not checked.
0362  *
0363  * Returns zero on success or -EFAULT on error.
0364  *
0365  * If an error occurs data may have been copied partially to guest memory.
0366  */
0367 static inline __must_check
0368 int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
0369             unsigned long len)
0370 {
0371     return kvm_write_guest(vcpu->kvm, gpa, data, len);
0372 }
0373 
0374 /**
0375  * read_guest_abs - copy data from guest space absolute to kernel space
0376  * @vcpu: virtual cpu
0377  * @gpa: guest physical (absolute) address
0378  * @data: destination address in kernel space
0379  * @len: number of bytes to copy
0380  *
0381  * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
0382  * It is up to the caller to ensure that the entire guest memory range is
0383  * valid memory before calling this function.
0384  * Guest key protection is not checked.
0385  *
0386  * Returns zero on success or -EFAULT on error.
0387  *
0388  * If an error occurs data may have been copied partially to kernel space.
0389  */
0390 static inline __must_check
0391 int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
0392            unsigned long len)
0393 {
0394     return kvm_read_guest(vcpu->kvm, gpa, data, len);
0395 }
0396 
0397 /**
0398  * write_guest_real - copy data from kernel space to guest space real
0399  * @vcpu: virtual cpu
0400  * @gra: guest real address
0401  * @data: source address in kernel space
0402  * @len: number of bytes to copy
0403  *
0404  * Copy @len bytes from @data (kernel space) to @gra (guest real address).
0405  * It is up to the caller to ensure that the entire guest memory range is
0406  * valid memory before calling this function.
0407  * Guest low address and key protection are not checked.
0408  *
0409  * Returns zero on success or -EFAULT on error.
0410  *
0411  * If an error occurs data may have been copied partially to guest memory.
0412  */
0413 static inline __must_check
0414 int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
0415              unsigned long len)
0416 {
0417     return access_guest_real(vcpu, gra, data, len, 1);
0418 }
0419 
0420 /**
0421  * read_guest_real - copy data from guest space real to kernel space
0422  * @vcpu: virtual cpu
0423  * @gra: guest real address
0424  * @data: destination address in kernel space
0425  * @len: number of bytes to copy
0426  *
0427  * Copy @len bytes from @gra (guest real address) to @data (kernel space).
0428  * It is up to the caller to ensure that the entire guest memory range is
0429  * valid memory before calling this function.
0430  * Guest key protection is not checked.
0431  *
0432  * Returns zero on success or -EFAULT on error.
0433  *
0434  * If an error occurs data may have been copied partially to kernel space.
0435  */
0436 static inline __must_check
0437 int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
0438             unsigned long len)
0439 {
0440     return access_guest_real(vcpu, gra, data, len, 0);
0441 }
0442 
0443 void ipte_lock(struct kvm *kvm);
0444 void ipte_unlock(struct kvm *kvm);
0445 int ipte_lock_held(struct kvm *kvm);
0446 int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);
0447 
0448 /* MVPG PEI indication bits */
0449 #define PEI_DAT_PROT 2
0450 #define PEI_NOT_PTE 4
0451 
0452 int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,
0453               unsigned long saddr, unsigned long *datptr);
0454 
0455 #endif /* __KVM_S390_GACCESS_H */