1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright IBM Corp. 2008, 2014
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
10 #ifndef __KVM_S390_GACCESS_H
11 #define __KVM_S390_GACCESS_H
13 #include <linux/compiler.h>
14 #include <linux/kvm_host.h>
15 #include <linux/uaccess.h>
16 #include <linux/ptrace.h>
20 * kvm_s390_real_to_abs - convert guest real address to guest absolute address
21 * @prefix - guest prefix
22 * @gra - guest real address
24 * Returns the guest absolute address that corresponds to the passed guest real
25 * address @gra of by applying the given prefix.
27 static inline unsigned long _kvm_s390_real_to_abs(u32 prefix, unsigned long gra)
29 if (gra < 2 * PAGE_SIZE)
31 else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)
37 * kvm_s390_real_to_abs - convert guest real address to guest absolute address
38 * @vcpu - guest virtual cpu
39 * @gra - guest real address
41 * Returns the guest absolute address that corresponds to the passed guest real
42 * address @gra of a virtual guest cpu by applying its prefix.
44 static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
47 return _kvm_s390_real_to_abs(kvm_s390_get_prefix(vcpu), gra);
51 * _kvm_s390_logical_to_effective - convert guest logical to effective address
52 * @psw: psw of the guest
53 * @ga: guest logical address
55 * Convert a guest logical address to an effective address by applying the
56 * rules of the addressing mode defined by bits 31 and 32 of the given PSW
57 * (extendended/basic addressing mode).
59 * Depending on the addressing mode, the upper 40 bits (24 bit addressing
60 * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing
61 * mode) of @ga will be zeroed and the remaining bits will be returned.
63 static inline unsigned long _kvm_s390_logical_to_effective(psw_t *psw,
66 if (psw_bits(*psw).eaba == PSW_BITS_AMODE_64BIT)
68 if (psw_bits(*psw).eaba == PSW_BITS_AMODE_31BIT)
69 return ga & ((1UL << 31) - 1);
70 return ga & ((1UL << 24) - 1);
74 * kvm_s390_logical_to_effective - convert guest logical to effective address
75 * @vcpu: guest virtual cpu
76 * @ga: guest logical address
78 * Convert a guest vcpu logical address to a guest vcpu effective address by
79 * applying the rules of the vcpu's addressing mode defined by PSW bits 31
80 * and 32 (extendended/basic addressing mode).
82 * Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing
83 * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode)
84 * of @ga will be zeroed and the remaining bits will be returned.
86 static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,
89 return _kvm_s390_logical_to_effective(&vcpu->arch.sie_block->gpsw, ga);
93 * put_guest_lc, read_guest_lc and write_guest_lc are guest access functions
94 * which shall only be used to access the lowcore of a vcpu.
95 * These functions should be used for e.g. interrupt handlers where no
96 * guest memory access protection facilities, like key or low address
97 * protection, are applicable.
98 * At a later point guest vcpu lowcore access should happen via pinned
99 * prefix pages, so that these pages can be accessed directly via the
100 * kernel mapping. All of these *_lc functions can be removed then.
104 * put_guest_lc - write a simple variable to a guest vcpu's lowcore
106 * @x: value to copy to guest
107 * @gra: vcpu's destination guest real address
109 * Copies a simple value from kernel space to a guest vcpu's lowcore.
110 * The size of the variable may be 1, 2, 4 or 8 bytes. The destination
111 * must be located in the vcpu's lowcore. Otherwise the result is undefined.
113 * Returns zero on success or -EFAULT on error.
115 * Note: an error indicates that either the kernel is out of memory or
116 * the guest memory mapping is broken. In any case the best solution
117 * would be to terminate the guest.
118 * It is wrong to inject a guest exception.
120 #define put_guest_lc(vcpu, x, gra) \
122 struct kvm_vcpu *__vcpu = (vcpu); \
123 __typeof__(*(gra)) __x = (x); \
124 unsigned long __gpa; \
126 __gpa = (unsigned long)(gra); \
127 __gpa += kvm_s390_get_prefix(__vcpu); \
128 kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \
132 * write_guest_lc - copy data from kernel space to guest vcpu's lowcore
134 * @gra: vcpu's source guest real address
135 * @data: source address in kernel space
136 * @len: number of bytes to copy
138 * Copy data from kernel space to guest vcpu's lowcore. The entire range must
139 * be located within the vcpu's lowcore, otherwise the result is undefined.
141 * Returns zero on success or -EFAULT on error.
143 * Note: an error indicates that either the kernel is out of memory or
144 * the guest memory mapping is broken. In any case the best solution
145 * would be to terminate the guest.
146 * It is wrong to inject a guest exception.
148 static inline __must_check
149 int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
152 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
154 return kvm_write_guest(vcpu->kvm, gpa, data, len);
158 * read_guest_lc - copy data from guest vcpu's lowcore to kernel space
160 * @gra: vcpu's source guest real address
161 * @data: destination address in kernel space
162 * @len: number of bytes to copy
164 * Copy data from guest vcpu's lowcore to kernel space. The entire range must
165 * be located within the vcpu's lowcore, otherwise the result is undefined.
167 * Returns zero on success or -EFAULT on error.
169 * Note: an error indicates that either the kernel is out of memory or
170 * the guest memory mapping is broken. In any case the best solution
171 * would be to terminate the guest.
172 * It is wrong to inject a guest exception.
174 static inline __must_check
175 int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
178 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
180 return kvm_read_guest(vcpu->kvm, gpa, data, len);
189 int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva,
190 u8 ar, unsigned long *gpa, enum gacc_mode mode);
191 int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
192 unsigned long length, enum gacc_mode mode);
194 int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
195 unsigned long len, enum gacc_mode mode);
197 int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
198 void *data, unsigned long len, enum gacc_mode mode);
201 * write_guest - copy data from kernel space to guest space
204 * @ar: access register
205 * @data: source address in kernel space
206 * @len: number of bytes to copy
208 * Copy @len bytes from @data (kernel space) to @ga (guest address).
209 * In order to copy data to guest space the PSW of the vcpu is inspected:
210 * If DAT is off data will be copied to guest real or absolute memory.
211 * If DAT is on data will be copied to the address space as specified by
212 * the address space bits of the PSW:
213 * Primary, secondary, home space or access register mode.
214 * The addressing mode of the PSW is also inspected, so that address wrap
215 * around is taken into account for 24-, 31- and 64-bit addressing mode,
216 * if the to be copied data crosses page boundaries in guest address space.
217 * In addition also low address and DAT protection are inspected before
218 * copying any data (key protection is currently not implemented).
220 * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
221 * In case of an access exception (e.g. protection exception) pgm will contain
222 * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
223 * will inject a correct exception into the guest.
224 * If no access exception happened, the contents of pgm are undefined when
225 * this function returns.
227 * Returns: - zero on success
228 * - a negative value if e.g. the guest mapping is broken or in
229 * case of out-of-memory. In this case the contents of pgm are
230 * undefined. Also parts of @data may have been copied to guest
232 * - a positive value if an access exception happened. In this case
233 * the returned value is the program interruption code and the
234 * contents of pgm may be used to inject an exception into the
235 * guest. No data has been copied to guest space.
237 * Note: in case an access exception is recognized no data has been copied to
238 * guest space (this is also true, if the to be copied data would cross
239 * one or more page boundaries in guest space).
240 * Therefore this function may be used for nullifying and suppressing
241 * instruction emulation.
242 * It may also be used for terminating instructions, if it is undefined
243 * if data has been changed in guest space in case of an exception.
245 static inline __must_check
246 int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
249 return access_guest(vcpu, ga, ar, data, len, GACC_STORE);
253 * read_guest - copy data from guest space to kernel space
256 * @ar: access register
257 * @data: destination address in kernel space
258 * @len: number of bytes to copy
260 * Copy @len bytes from @ga (guest address) to @data (kernel space).
262 * The behaviour of read_guest is identical to write_guest, except that
263 * data will be copied from guest space to kernel space.
265 static inline __must_check
266 int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
269 return access_guest(vcpu, ga, ar, data, len, GACC_FETCH);
273 * read_guest_instr - copy instruction data from guest space to kernel space
276 * @data: destination address in kernel space
277 * @len: number of bytes to copy
279 * Copy @len bytes from the given address (guest space) to @data (kernel
282 * The behaviour of read_guest_instr is identical to read_guest, except that
283 * instruction data will be read from primary space when in home-space or
284 * address-space mode.
286 static inline __must_check
287 int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
290 return access_guest(vcpu, ga, 0, data, len, GACC_IFETCH);
294 * write_guest_abs - copy data from kernel space to guest space absolute
296 * @gpa: guest physical (absolute) address
297 * @data: source address in kernel space
298 * @len: number of bytes to copy
300 * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
301 * It is up to the caller to ensure that the entire guest memory range is
302 * valid memory before calling this function.
303 * Guest low address and key protection are not checked.
305 * Returns zero on success or -EFAULT on error.
307 * If an error occurs data may have been copied partially to guest memory.
309 static inline __must_check
310 int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
313 return kvm_write_guest(vcpu->kvm, gpa, data, len);
317 * read_guest_abs - copy data from guest space absolute to kernel space
319 * @gpa: guest physical (absolute) address
320 * @data: destination address in kernel space
321 * @len: number of bytes to copy
323 * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
324 * It is up to the caller to ensure that the entire guest memory range is
325 * valid memory before calling this function.
326 * Guest key protection is not checked.
328 * Returns zero on success or -EFAULT on error.
330 * If an error occurs data may have been copied partially to kernel space.
332 static inline __must_check
333 int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
336 return kvm_read_guest(vcpu->kvm, gpa, data, len);
340 * write_guest_real - copy data from kernel space to guest space real
342 * @gra: guest real address
343 * @data: source address in kernel space
344 * @len: number of bytes to copy
346 * Copy @len bytes from @data (kernel space) to @gra (guest real address).
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 low address and key protection are not checked.
351 * Returns zero on success or -EFAULT on error.
353 * If an error occurs data may have been copied partially to guest memory.
355 static inline __must_check
356 int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
359 return access_guest_real(vcpu, gra, data, len, 1);
363 * read_guest_real - copy data from guest space real to kernel space
365 * @gra: guest real address
366 * @data: destination address in kernel space
367 * @len: number of bytes to copy
369 * Copy @len bytes from @gra (guest real address) to @data (kernel space).
370 * It is up to the caller to ensure that the entire guest memory range is
371 * valid memory before calling this function.
372 * Guest key protection is not checked.
374 * Returns zero on success or -EFAULT on error.
376 * If an error occurs data may have been copied partially to kernel space.
378 static inline __must_check
379 int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
382 return access_guest_real(vcpu, gra, data, len, 0);
385 void ipte_lock(struct kvm_vcpu *vcpu);
386 void ipte_unlock(struct kvm_vcpu *vcpu);
387 int ipte_lock_held(struct kvm_vcpu *vcpu);
388 int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);
390 int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,
391 unsigned long saddr);
393 #endif /* __KVM_S390_GACCESS_H */