/* SPDX-License-Identifier: GPL-2.0 */1/*2* access guest memory3*4* Copyright IBM Corp. 2008, 20145*6* Author(s): Carsten Otte <[email protected]>7*/89#ifndef __KVM_S390_GACCESS_H10#define __KVM_S390_GACCESS_H1112#include <linux/compiler.h>13#include <linux/kvm_host.h>14#include <linux/uaccess.h>15#include <linux/ptrace.h>16#include "kvm-s390.h"1718/**19* kvm_s390_real_to_abs - convert guest real address to guest absolute address20* @prefix - guest prefix21* @gra - guest real address22*23* Returns the guest absolute address that corresponds to the passed guest real24* address @gra of by applying the given prefix.25*/26static inline unsigned long _kvm_s390_real_to_abs(u32 prefix, unsigned long gra)27{28if (gra < 2 * PAGE_SIZE)29gra += prefix;30else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)31gra -= prefix;32return gra;33}3435/**36* kvm_s390_real_to_abs - convert guest real address to guest absolute address37* @vcpu - guest virtual cpu38* @gra - guest real address39*40* Returns the guest absolute address that corresponds to the passed guest real41* address @gra of a virtual guest cpu by applying its prefix.42*/43static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,44unsigned long gra)45{46return _kvm_s390_real_to_abs(kvm_s390_get_prefix(vcpu), gra);47}4849/**50* _kvm_s390_logical_to_effective - convert guest logical to effective address51* @psw: psw of the guest52* @ga: guest logical address53*54* Convert a guest logical address to an effective address by applying the55* rules of the addressing mode defined by bits 31 and 32 of the given PSW56* (extendended/basic addressing mode).57*58* Depending on the addressing mode, the upper 40 bits (24 bit addressing59* mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing60* mode) of @ga will be zeroed and the remaining bits will be returned.61*/62static inline unsigned long _kvm_s390_logical_to_effective(psw_t *psw,63unsigned long ga)64{65if (psw_bits(*psw).eaba == PSW_BITS_AMODE_64BIT)66return ga;67if (psw_bits(*psw).eaba == PSW_BITS_AMODE_31BIT)68return ga & ((1UL << 31) - 1);69return ga & ((1UL << 24) - 1);70}7172/**73* kvm_s390_logical_to_effective - convert guest logical to effective address74* @vcpu: guest virtual cpu75* @ga: guest logical address76*77* Convert a guest vcpu logical address to a guest vcpu effective address by78* applying the rules of the vcpu's addressing mode defined by PSW bits 3179* and 32 (extendended/basic addressing mode).80*81* Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing82* mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode)83* of @ga will be zeroed and the remaining bits will be returned.84*/85static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,86unsigned long ga)87{88return _kvm_s390_logical_to_effective(&vcpu->arch.sie_block->gpsw, ga);89}9091/*92* put_guest_lc, read_guest_lc and write_guest_lc are guest access functions93* which shall only be used to access the lowcore of a vcpu.94* These functions should be used for e.g. interrupt handlers where no95* guest memory access protection facilities, like key or low address96* protection, are applicable.97* At a later point guest vcpu lowcore access should happen via pinned98* prefix pages, so that these pages can be accessed directly via the99* kernel mapping. All of these *_lc functions can be removed then.100*/101102/**103* put_guest_lc - write a simple variable to a guest vcpu's lowcore104* @vcpu: virtual cpu105* @x: value to copy to guest106* @gra: vcpu's destination guest real address107*108* Copies a simple value from kernel space to a guest vcpu's lowcore.109* The size of the variable may be 1, 2, 4 or 8 bytes. The destination110* must be located in the vcpu's lowcore. Otherwise the result is undefined.111*112* Returns zero on success or -EFAULT on error.113*114* Note: an error indicates that either the kernel is out of memory or115* the guest memory mapping is broken. In any case the best solution116* would be to terminate the guest.117* It is wrong to inject a guest exception.118*/119#define put_guest_lc(vcpu, x, gra) \120({ \121struct kvm_vcpu *__vcpu = (vcpu); \122__typeof__(*(gra)) __x = (x); \123unsigned long __gpa; \124\125__gpa = (unsigned long)(gra); \126__gpa += kvm_s390_get_prefix(__vcpu); \127kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \128})129130/**131* write_guest_lc - copy data from kernel space to guest vcpu's lowcore132* @vcpu: virtual cpu133* @gra: vcpu's source guest real address134* @data: source address in kernel space135* @len: number of bytes to copy136*137* Copy data from kernel space to guest vcpu's lowcore. The entire range must138* be located within the vcpu's lowcore, otherwise the result is undefined.139*140* Returns zero on success or -EFAULT on error.141*142* Note: an error indicates that either the kernel is out of memory or143* the guest memory mapping is broken. In any case the best solution144* would be to terminate the guest.145* It is wrong to inject a guest exception.146*/147static inline __must_check148int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,149unsigned long len)150{151unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);152153return kvm_write_guest(vcpu->kvm, gpa, data, len);154}155156/**157* read_guest_lc - copy data from guest vcpu's lowcore to kernel space158* @vcpu: virtual cpu159* @gra: vcpu's source guest real address160* @data: destination address in kernel space161* @len: number of bytes to copy162*163* Copy data from guest vcpu's lowcore to kernel space. The entire range must164* be located within the vcpu's lowcore, otherwise the result is undefined.165*166* Returns zero on success or -EFAULT on error.167*168* Note: an error indicates that either the kernel is out of memory or169* the guest memory mapping is broken. In any case the best solution170* would be to terminate the guest.171* It is wrong to inject a guest exception.172*/173static inline __must_check174int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,175unsigned long len)176{177unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);178179return kvm_read_guest(vcpu->kvm, gpa, data, len);180}181182enum gacc_mode {183GACC_FETCH,184GACC_STORE,185GACC_IFETCH,186};187188int guest_translate_address_with_key(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,189unsigned long *gpa, enum gacc_mode mode,190u8 access_key);191192int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,193unsigned long length, enum gacc_mode mode, u8 access_key);194195int check_gpa_range(struct kvm *kvm, unsigned long gpa, unsigned long length,196enum gacc_mode mode, u8 access_key);197198int access_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, void *data,199unsigned long len, enum gacc_mode mode, u8 access_key);200201int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,202void *data, unsigned long len, enum gacc_mode mode,203u8 access_key);204205int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,206void *data, unsigned long len, enum gacc_mode mode);207208int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len, __uint128_t *old,209__uint128_t new, u8 access_key, bool *success);210211/**212* write_guest_with_key - copy data from kernel space to guest space213* @vcpu: virtual cpu214* @ga: guest address215* @ar: access register216* @data: source address in kernel space217* @len: number of bytes to copy218* @access_key: access key the storage key needs to match219*220* Copy @len bytes from @data (kernel space) to @ga (guest address).221* In order to copy data to guest space the PSW of the vcpu is inspected:222* If DAT is off data will be copied to guest real or absolute memory.223* If DAT is on data will be copied to the address space as specified by224* the address space bits of the PSW:225* Primary, secondary, home space or access register mode.226* The addressing mode of the PSW is also inspected, so that address wrap227* around is taken into account for 24-, 31- and 64-bit addressing mode,228* if the to be copied data crosses page boundaries in guest address space.229* In addition low address, DAT and key protection checks are performed before230* copying any data.231*232* This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.233* In case of an access exception (e.g. protection exception) pgm will contain234* all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'235* will inject a correct exception into the guest.236* If no access exception happened, the contents of pgm are undefined when237* this function returns.238*239* Returns: - zero on success240* - a negative value if e.g. the guest mapping is broken or in241* case of out-of-memory. In this case the contents of pgm are242* undefined. Also parts of @data may have been copied to guest243* space.244* - a positive value if an access exception happened. In this case245* the returned value is the program interruption code and the246* contents of pgm may be used to inject an exception into the247* guest. No data has been copied to guest space.248*249* Note: in case an access exception is recognized no data has been copied to250* guest space (this is also true, if the to be copied data would cross251* one or more page boundaries in guest space).252* Therefore this function may be used for nullifying and suppressing253* instruction emulation.254* It may also be used for terminating instructions, if it is undefined255* if data has been changed in guest space in case of an exception.256*/257static inline __must_check258int write_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,259void *data, unsigned long len, u8 access_key)260{261return access_guest_with_key(vcpu, ga, ar, data, len, GACC_STORE,262access_key);263}264265/**266* write_guest - copy data from kernel space to guest space267* @vcpu: virtual cpu268* @ga: guest address269* @ar: access register270* @data: source address in kernel space271* @len: number of bytes to copy272*273* The behaviour of write_guest is identical to write_guest_with_key, except274* that the PSW access key is used instead of an explicit argument.275*/276static inline __must_check277int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,278unsigned long len)279{280u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;281282return write_guest_with_key(vcpu, ga, ar, data, len, access_key);283}284285/**286* read_guest_with_key - copy data from guest space to kernel space287* @vcpu: virtual cpu288* @ga: guest address289* @ar: access register290* @data: destination address in kernel space291* @len: number of bytes to copy292* @access_key: access key the storage key needs to match293*294* Copy @len bytes from @ga (guest address) to @data (kernel space).295*296* The behaviour of read_guest_with_key is identical to write_guest_with_key,297* except that data will be copied from guest space to kernel space.298*/299static inline __must_check300int read_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,301void *data, unsigned long len, u8 access_key)302{303return access_guest_with_key(vcpu, ga, ar, data, len, GACC_FETCH,304access_key);305}306307/**308* read_guest - copy data from guest space to kernel space309* @vcpu: virtual cpu310* @ga: guest address311* @ar: access register312* @data: destination address in kernel space313* @len: number of bytes to copy314*315* Copy @len bytes from @ga (guest address) to @data (kernel space).316*317* The behaviour of read_guest is identical to read_guest_with_key, except318* that the PSW access key is used instead of an explicit argument.319*/320static inline __must_check321int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,322unsigned long len)323{324u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;325326return read_guest_with_key(vcpu, ga, ar, data, len, access_key);327}328329/**330* read_guest_instr - copy instruction data from guest space to kernel space331* @vcpu: virtual cpu332* @ga: guest address333* @data: destination address in kernel space334* @len: number of bytes to copy335*336* Copy @len bytes from the given address (guest space) to @data (kernel337* space).338*339* The behaviour of read_guest_instr is identical to read_guest, except that340* instruction data will be read from primary space when in home-space or341* address-space mode.342*/343static inline __must_check344int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,345unsigned long len)346{347u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;348349return access_guest_with_key(vcpu, ga, 0, data, len, GACC_IFETCH,350access_key);351}352353/**354* write_guest_abs - copy data from kernel space to guest space absolute355* @vcpu: virtual cpu356* @gpa: guest physical (absolute) address357* @data: source address in kernel space358* @len: number of bytes to copy359*360* Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).361* It is up to the caller to ensure that the entire guest memory range is362* valid memory before calling this function.363* Guest low address and key protection are not checked.364*365* Returns zero on success or -EFAULT on error.366*367* If an error occurs data may have been copied partially to guest memory.368*/369static inline __must_check370int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,371unsigned long len)372{373return kvm_write_guest(vcpu->kvm, gpa, data, len);374}375376/**377* read_guest_abs - copy data from guest space absolute to kernel space378* @vcpu: virtual cpu379* @gpa: guest physical (absolute) address380* @data: destination address in kernel space381* @len: number of bytes to copy382*383* Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).384* It is up to the caller to ensure that the entire guest memory range is385* valid memory before calling this function.386* Guest key protection is not checked.387*388* Returns zero on success or -EFAULT on error.389*390* If an error occurs data may have been copied partially to kernel space.391*/392static inline __must_check393int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,394unsigned long len)395{396return kvm_read_guest(vcpu->kvm, gpa, data, len);397}398399/**400* write_guest_real - copy data from kernel space to guest space real401* @vcpu: virtual cpu402* @gra: guest real address403* @data: source address in kernel space404* @len: number of bytes to copy405*406* Copy @len bytes from @data (kernel space) to @gra (guest real address).407* Guest low address and key protection are not checked.408*409* Returns zero on success, -EFAULT when copying from @data failed, or410* PGM_ADRESSING in case @gra is outside a memslot. In this case, pgm check info411* is also stored to allow injecting into the guest (if applicable) using412* kvm_s390_inject_prog_cond().413*414* If an error occurs data may have been copied partially to guest memory.415*/416static inline __must_check417int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,418unsigned long len)419{420return access_guest_real(vcpu, gra, data, len, 1);421}422423/**424* read_guest_real - copy data from guest space real to kernel space425* @vcpu: virtual cpu426* @gra: guest real address427* @data: destination address in kernel space428* @len: number of bytes to copy429*430* Copy @len bytes from @gra (guest real address) to @data (kernel space).431* Guest key protection is not checked.432*433* Returns zero on success, -EFAULT when copying to @data failed, or434* PGM_ADRESSING in case @gra is outside a memslot. In this case, pgm check info435* is also stored to allow injecting into the guest (if applicable) using436* kvm_s390_inject_prog_cond().437*438* If an error occurs data may have been copied partially to kernel space.439*/440static inline __must_check441int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,442unsigned long len)443{444return access_guest_real(vcpu, gra, data, len, 0);445}446447void ipte_lock(struct kvm *kvm);448void ipte_unlock(struct kvm *kvm);449int ipte_lock_held(struct kvm *kvm);450int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);451452/* MVPG PEI indication bits */453#define PEI_DAT_PROT 2454#define PEI_NOT_PTE 4455456int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,457unsigned long saddr, unsigned long *datptr);458459#endif /* __KVM_S390_GACCESS_H */460461462