Loading...
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
10#ifndef __KVM_S390_GACCESS_H
11#define __KVM_S390_GACCESS_H
12
13#include <linux/compiler.h>
14#include <linux/kvm_host.h>
15#include <linux/uaccess.h>
16#include <linux/ptrace.h>
17#include "kvm-s390.h"
18
19/**
20 * kvm_s390_real_to_abs - convert guest real address to guest absolute address
21 * @prefix - guest prefix
22 * @gra - guest real address
23 *
24 * Returns the guest absolute address that corresponds to the passed guest real
25 * address @gra of by applying the given prefix.
26 */
27static inline unsigned long _kvm_s390_real_to_abs(u32 prefix, unsigned long gra)
28{
29 if (gra < 2 * PAGE_SIZE)
30 gra += prefix;
31 else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)
32 gra -= prefix;
33 return gra;
34}
35
36/**
37 * kvm_s390_real_to_abs - convert guest real address to guest absolute address
38 * @vcpu - guest virtual cpu
39 * @gra - guest real address
40 *
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.
43 */
44static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
45 unsigned long gra)
46{
47 return _kvm_s390_real_to_abs(kvm_s390_get_prefix(vcpu), gra);
48}
49
50/**
51 * _kvm_s390_logical_to_effective - convert guest logical to effective address
52 * @psw: psw of the guest
53 * @ga: guest logical address
54 *
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).
58 *
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.
62 */
63static inline unsigned long _kvm_s390_logical_to_effective(psw_t *psw,
64 unsigned long ga)
65{
66 if (psw_bits(*psw).eaba == PSW_BITS_AMODE_64BIT)
67 return ga;
68 if (psw_bits(*psw).eaba == PSW_BITS_AMODE_31BIT)
69 return ga & ((1UL << 31) - 1);
70 return ga & ((1UL << 24) - 1);
71}
72
73/**
74 * kvm_s390_logical_to_effective - convert guest logical to effective address
75 * @vcpu: guest virtual cpu
76 * @ga: guest logical address
77 *
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).
81 *
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.
85 */
86static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,
87 unsigned long ga)
88{
89 return _kvm_s390_logical_to_effective(&vcpu->arch.sie_block->gpsw, ga);
90}
91
92/*
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.
101 */
102
103/**
104 * put_guest_lc - write a simple variable to a guest vcpu's lowcore
105 * @vcpu: virtual cpu
106 * @x: value to copy to guest
107 * @gra: vcpu's destination guest real address
108 *
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.
112 *
113 * Returns zero on success or -EFAULT on error.
114 *
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.
119 */
120#define put_guest_lc(vcpu, x, gra) \
121({ \
122 struct kvm_vcpu *__vcpu = (vcpu); \
123 __typeof__(*(gra)) __x = (x); \
124 unsigned long __gpa; \
125 \
126 __gpa = (unsigned long)(gra); \
127 __gpa += kvm_s390_get_prefix(__vcpu); \
128 kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \
129})
130
131/**
132 * write_guest_lc - copy data from kernel space to guest vcpu's lowcore
133 * @vcpu: virtual cpu
134 * @gra: vcpu's source guest real address
135 * @data: source address in kernel space
136 * @len: number of bytes to copy
137 *
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.
140 *
141 * Returns zero on success or -EFAULT on error.
142 *
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.
147 */
148static inline __must_check
149int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
150 unsigned long len)
151{
152 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
153
154 return kvm_write_guest(vcpu->kvm, gpa, data, len);
155}
156
157/**
158 * read_guest_lc - copy data from guest vcpu's lowcore to kernel space
159 * @vcpu: virtual cpu
160 * @gra: vcpu's source guest real address
161 * @data: destination address in kernel space
162 * @len: number of bytes to copy
163 *
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.
166 *
167 * Returns zero on success or -EFAULT on error.
168 *
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.
173 */
174static inline __must_check
175int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
176 unsigned long len)
177{
178 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
179
180 return kvm_read_guest(vcpu->kvm, gpa, data, len);
181}
182
183enum gacc_mode {
184 GACC_FETCH,
185 GACC_STORE,
186 GACC_IFETCH,
187};
188
189int guest_translate_address_with_key(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
190 unsigned long *gpa, enum gacc_mode mode,
191 u8 access_key);
192
193int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
194 unsigned long length, enum gacc_mode mode, u8 access_key);
195
196int check_gpa_range(struct kvm *kvm, unsigned long gpa, unsigned long length,
197 enum gacc_mode mode, u8 access_key);
198
199int access_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, void *data,
200 unsigned long len, enum gacc_mode mode, u8 access_key);
201
202int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
203 void *data, unsigned long len, enum gacc_mode mode,
204 u8 access_key);
205
206int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
207 void *data, unsigned long len, enum gacc_mode mode);
208
209int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len, __uint128_t *old,
210 __uint128_t new, u8 access_key, bool *success);
211
212/**
213 * write_guest_with_key - copy data from kernel space to guest space
214 * @vcpu: virtual cpu
215 * @ga: guest address
216 * @ar: access register
217 * @data: source address in kernel space
218 * @len: number of bytes to copy
219 * @access_key: access key the storage key needs to match
220 *
221 * Copy @len bytes from @data (kernel space) to @ga (guest address).
222 * In order to copy data to guest space the PSW of the vcpu is inspected:
223 * If DAT is off data will be copied to guest real or absolute memory.
224 * If DAT is on data will be copied to the address space as specified by
225 * the address space bits of the PSW:
226 * Primary, secondary, home space or access register mode.
227 * The addressing mode of the PSW is also inspected, so that address wrap
228 * around is taken into account for 24-, 31- and 64-bit addressing mode,
229 * if the to be copied data crosses page boundaries in guest address space.
230 * In addition low address, DAT and key protection checks are performed before
231 * copying any data.
232 *
233 * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
234 * In case of an access exception (e.g. protection exception) pgm will contain
235 * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
236 * will inject a correct exception into the guest.
237 * If no access exception happened, the contents of pgm are undefined when
238 * this function returns.
239 *
240 * Returns: - zero on success
241 * - a negative value if e.g. the guest mapping is broken or in
242 * case of out-of-memory. In this case the contents of pgm are
243 * undefined. Also parts of @data may have been copied to guest
244 * space.
245 * - a positive value if an access exception happened. In this case
246 * the returned value is the program interruption code and the
247 * contents of pgm may be used to inject an exception into the
248 * guest. No data has been copied to guest space.
249 *
250 * Note: in case an access exception is recognized no data has been copied to
251 * guest space (this is also true, if the to be copied data would cross
252 * one or more page boundaries in guest space).
253 * Therefore this function may be used for nullifying and suppressing
254 * instruction emulation.
255 * It may also be used for terminating instructions, if it is undefined
256 * if data has been changed in guest space in case of an exception.
257 */
258static inline __must_check
259int write_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
260 void *data, unsigned long len, u8 access_key)
261{
262 return access_guest_with_key(vcpu, ga, ar, data, len, GACC_STORE,
263 access_key);
264}
265
266/**
267 * write_guest - copy data from kernel space to guest space
268 * @vcpu: virtual cpu
269 * @ga: guest address
270 * @ar: access register
271 * @data: source address in kernel space
272 * @len: number of bytes to copy
273 *
274 * The behaviour of write_guest is identical to write_guest_with_key, except
275 * that the PSW access key is used instead of an explicit argument.
276 */
277static inline __must_check
278int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
279 unsigned long len)
280{
281 u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
282
283 return write_guest_with_key(vcpu, ga, ar, data, len, access_key);
284}
285
286/**
287 * read_guest_with_key - copy data from guest space to kernel space
288 * @vcpu: virtual cpu
289 * @ga: guest address
290 * @ar: access register
291 * @data: destination address in kernel space
292 * @len: number of bytes to copy
293 * @access_key: access key the storage key needs to match
294 *
295 * Copy @len bytes from @ga (guest address) to @data (kernel space).
296 *
297 * The behaviour of read_guest_with_key is identical to write_guest_with_key,
298 * except that data will be copied from guest space to kernel space.
299 */
300static inline __must_check
301int read_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
302 void *data, unsigned long len, u8 access_key)
303{
304 return access_guest_with_key(vcpu, ga, ar, data, len, GACC_FETCH,
305 access_key);
306}
307
308/**
309 * read_guest - copy data from guest space to kernel space
310 * @vcpu: virtual cpu
311 * @ga: guest address
312 * @ar: access register
313 * @data: destination address in kernel space
314 * @len: number of bytes to copy
315 *
316 * Copy @len bytes from @ga (guest address) to @data (kernel space).
317 *
318 * The behaviour of read_guest is identical to read_guest_with_key, except
319 * that the PSW access key is used instead of an explicit argument.
320 */
321static inline __must_check
322int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
323 unsigned long len)
324{
325 u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
326
327 return read_guest_with_key(vcpu, ga, ar, data, len, access_key);
328}
329
330/**
331 * read_guest_instr - copy instruction data from guest space to kernel space
332 * @vcpu: virtual cpu
333 * @ga: guest address
334 * @data: destination address in kernel space
335 * @len: number of bytes to copy
336 *
337 * Copy @len bytes from the given address (guest space) to @data (kernel
338 * space).
339 *
340 * The behaviour of read_guest_instr is identical to read_guest, except that
341 * instruction data will be read from primary space when in home-space or
342 * address-space mode.
343 */
344static inline __must_check
345int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
346 unsigned long len)
347{
348 u8 access_key = psw_bits(vcpu->arch.sie_block->gpsw).key;
349
350 return access_guest_with_key(vcpu, ga, 0, data, len, GACC_IFETCH,
351 access_key);
352}
353
354/**
355 * write_guest_abs - copy data from kernel space to guest space absolute
356 * @vcpu: virtual cpu
357 * @gpa: guest physical (absolute) address
358 * @data: source address in kernel space
359 * @len: number of bytes to copy
360 *
361 * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
362 * It is up to the caller to ensure that the entire guest memory range is
363 * valid memory before calling this function.
364 * Guest low address and key protection are not checked.
365 *
366 * Returns zero on success or -EFAULT on error.
367 *
368 * If an error occurs data may have been copied partially to guest memory.
369 */
370static inline __must_check
371int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
372 unsigned long len)
373{
374 return kvm_write_guest(vcpu->kvm, gpa, data, len);
375}
376
377/**
378 * read_guest_abs - copy data from guest space absolute to kernel space
379 * @vcpu: virtual cpu
380 * @gpa: guest physical (absolute) address
381 * @data: destination address in kernel space
382 * @len: number of bytes to copy
383 *
384 * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
385 * It is up to the caller to ensure that the entire guest memory range is
386 * valid memory before calling this function.
387 * Guest key protection is not checked.
388 *
389 * Returns zero on success or -EFAULT on error.
390 *
391 * If an error occurs data may have been copied partially to kernel space.
392 */
393static inline __must_check
394int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
395 unsigned long len)
396{
397 return kvm_read_guest(vcpu->kvm, gpa, data, len);
398}
399
400/**
401 * write_guest_real - copy data from kernel space to guest space real
402 * @vcpu: virtual cpu
403 * @gra: guest real address
404 * @data: source address in kernel space
405 * @len: number of bytes to copy
406 *
407 * Copy @len bytes from @data (kernel space) to @gra (guest real address).
408 * Guest low address and key protection are not checked.
409 *
410 * Returns zero on success, -EFAULT when copying from @data failed, or
411 * PGM_ADRESSING in case @gra is outside a memslot. In this case, pgm check info
412 * is also stored to allow injecting into the guest (if applicable) using
413 * kvm_s390_inject_prog_cond().
414 *
415 * If an error occurs data may have been copied partially to guest memory.
416 */
417static inline __must_check
418int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
419 unsigned long len)
420{
421 return access_guest_real(vcpu, gra, data, len, 1);
422}
423
424/**
425 * read_guest_real - copy data from guest space real to kernel space
426 * @vcpu: virtual cpu
427 * @gra: guest real address
428 * @data: destination address in kernel space
429 * @len: number of bytes to copy
430 *
431 * Copy @len bytes from @gra (guest real address) to @data (kernel space).
432 * Guest key protection is not checked.
433 *
434 * Returns zero on success, -EFAULT when copying to @data failed, or
435 * PGM_ADRESSING in case @gra is outside a memslot. In this case, pgm check info
436 * is also stored to allow injecting into the guest (if applicable) using
437 * kvm_s390_inject_prog_cond().
438 *
439 * If an error occurs data may have been copied partially to kernel space.
440 */
441static inline __must_check
442int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
443 unsigned long len)
444{
445 return access_guest_real(vcpu, gra, data, len, 0);
446}
447
448void ipte_lock(struct kvm *kvm);
449void ipte_unlock(struct kvm *kvm);
450int ipte_lock_held(struct kvm *kvm);
451int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);
452
453/* MVPG PEI indication bits */
454#define PEI_DAT_PROT 2
455#define PEI_NOT_PTE 4
456
457int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,
458 unsigned long saddr, unsigned long *datptr);
459
460#endif /* __KVM_S390_GACCESS_H */
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
10#ifndef __KVM_S390_GACCESS_H
11#define __KVM_S390_GACCESS_H
12
13#include <linux/compiler.h>
14#include <linux/kvm_host.h>
15#include <linux/uaccess.h>
16#include <linux/ptrace.h>
17#include "kvm-s390.h"
18
19/**
20 * kvm_s390_real_to_abs - convert guest real address to guest absolute address
21 * @vcpu - guest virtual cpu
22 * @gra - guest real address
23 *
24 * Returns the guest absolute address that corresponds to the passed guest real
25 * address @gra of a virtual guest cpu by applying its prefix.
26 */
27static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
28 unsigned long gra)
29{
30 unsigned long prefix = kvm_s390_get_prefix(vcpu);
31
32 if (gra < 2 * PAGE_SIZE)
33 gra += prefix;
34 else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)
35 gra -= prefix;
36 return gra;
37}
38
39/**
40 * kvm_s390_logical_to_effective - convert guest logical to effective address
41 * @vcpu: guest virtual cpu
42 * @ga: guest logical address
43 *
44 * Convert a guest vcpu logical address to a guest vcpu effective address by
45 * applying the rules of the vcpu's addressing mode defined by PSW bits 31
46 * and 32 (extendended/basic addressing mode).
47 *
48 * Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing
49 * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode)
50 * of @ga will be zeroed and the remaining bits will be returned.
51 */
52static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,
53 unsigned long ga)
54{
55 psw_t *psw = &vcpu->arch.sie_block->gpsw;
56
57 if (psw_bits(*psw).eaba == PSW_BITS_AMODE_64BIT)
58 return ga;
59 if (psw_bits(*psw).eaba == PSW_BITS_AMODE_31BIT)
60 return ga & ((1UL << 31) - 1);
61 return ga & ((1UL << 24) - 1);
62}
63
64/*
65 * put_guest_lc, read_guest_lc and write_guest_lc are guest access functions
66 * which shall only be used to access the lowcore of a vcpu.
67 * These functions should be used for e.g. interrupt handlers where no
68 * guest memory access protection facilities, like key or low address
69 * protection, are applicable.
70 * At a later point guest vcpu lowcore access should happen via pinned
71 * prefix pages, so that these pages can be accessed directly via the
72 * kernel mapping. All of these *_lc functions can be removed then.
73 */
74
75/**
76 * put_guest_lc - write a simple variable to a guest vcpu's lowcore
77 * @vcpu: virtual cpu
78 * @x: value to copy to guest
79 * @gra: vcpu's destination guest real address
80 *
81 * Copies a simple value from kernel space to a guest vcpu's lowcore.
82 * The size of the variable may be 1, 2, 4 or 8 bytes. The destination
83 * must be located in the vcpu's lowcore. Otherwise the result is undefined.
84 *
85 * Returns zero on success or -EFAULT on error.
86 *
87 * Note: an error indicates that either the kernel is out of memory or
88 * the guest memory mapping is broken. In any case the best solution
89 * would be to terminate the guest.
90 * It is wrong to inject a guest exception.
91 */
92#define put_guest_lc(vcpu, x, gra) \
93({ \
94 struct kvm_vcpu *__vcpu = (vcpu); \
95 __typeof__(*(gra)) __x = (x); \
96 unsigned long __gpa; \
97 \
98 __gpa = (unsigned long)(gra); \
99 __gpa += kvm_s390_get_prefix(__vcpu); \
100 kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \
101})
102
103/**
104 * write_guest_lc - copy data from kernel space to guest vcpu's lowcore
105 * @vcpu: virtual cpu
106 * @gra: vcpu's source guest real address
107 * @data: source address in kernel space
108 * @len: number of bytes to copy
109 *
110 * Copy data from kernel space to guest vcpu's lowcore. The entire range must
111 * be located within the vcpu's lowcore, otherwise the result is undefined.
112 *
113 * Returns zero on success or -EFAULT on error.
114 *
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.
119 */
120static inline __must_check
121int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
122 unsigned long len)
123{
124 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
125
126 return kvm_write_guest(vcpu->kvm, gpa, data, len);
127}
128
129/**
130 * read_guest_lc - copy data from guest vcpu's lowcore to kernel space
131 * @vcpu: virtual cpu
132 * @gra: vcpu's source guest real address
133 * @data: destination address in kernel space
134 * @len: number of bytes to copy
135 *
136 * Copy data from guest vcpu's lowcore to kernel space. The entire range must
137 * be located within the vcpu's lowcore, otherwise the result is undefined.
138 *
139 * Returns zero on success or -EFAULT on error.
140 *
141 * Note: an error indicates that either the kernel is out of memory or
142 * the guest memory mapping is broken. In any case the best solution
143 * would be to terminate the guest.
144 * It is wrong to inject a guest exception.
145 */
146static inline __must_check
147int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
148 unsigned long len)
149{
150 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
151
152 return kvm_read_guest(vcpu->kvm, gpa, data, len);
153}
154
155enum gacc_mode {
156 GACC_FETCH,
157 GACC_STORE,
158 GACC_IFETCH,
159};
160
161int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva,
162 u8 ar, unsigned long *gpa, enum gacc_mode mode);
163int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar,
164 unsigned long length, enum gacc_mode mode);
165
166int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
167 unsigned long len, enum gacc_mode mode);
168
169int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
170 void *data, unsigned long len, enum gacc_mode mode);
171
172/**
173 * write_guest - copy data from kernel space to guest space
174 * @vcpu: virtual cpu
175 * @ga: guest address
176 * @ar: access register
177 * @data: source address in kernel space
178 * @len: number of bytes to copy
179 *
180 * Copy @len bytes from @data (kernel space) to @ga (guest address).
181 * In order to copy data to guest space the PSW of the vcpu is inspected:
182 * If DAT is off data will be copied to guest real or absolute memory.
183 * If DAT is on data will be copied to the address space as specified by
184 * the address space bits of the PSW:
185 * Primary, secondary, home space or access register mode.
186 * The addressing mode of the PSW is also inspected, so that address wrap
187 * around is taken into account for 24-, 31- and 64-bit addressing mode,
188 * if the to be copied data crosses page boundaries in guest address space.
189 * In addition also low address and DAT protection are inspected before
190 * copying any data (key protection is currently not implemented).
191 *
192 * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
193 * In case of an access exception (e.g. protection exception) pgm will contain
194 * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
195 * will inject a correct exception into the guest.
196 * If no access exception happened, the contents of pgm are undefined when
197 * this function returns.
198 *
199 * Returns: - zero on success
200 * - a negative value if e.g. the guest mapping is broken or in
201 * case of out-of-memory. In this case the contents of pgm are
202 * undefined. Also parts of @data may have been copied to guest
203 * space.
204 * - a positive value if an access exception happened. In this case
205 * the returned value is the program interruption code and the
206 * contents of pgm may be used to inject an exception into the
207 * guest. No data has been copied to guest space.
208 *
209 * Note: in case an access exception is recognized no data has been copied to
210 * guest space (this is also true, if the to be copied data would cross
211 * one or more page boundaries in guest space).
212 * Therefore this function may be used for nullifying and suppressing
213 * instruction emulation.
214 * It may also be used for terminating instructions, if it is undefined
215 * if data has been changed in guest space in case of an exception.
216 */
217static inline __must_check
218int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
219 unsigned long len)
220{
221 return access_guest(vcpu, ga, ar, data, len, GACC_STORE);
222}
223
224/**
225 * read_guest - copy data from guest space to kernel space
226 * @vcpu: virtual cpu
227 * @ga: guest address
228 * @ar: access register
229 * @data: destination address in kernel space
230 * @len: number of bytes to copy
231 *
232 * Copy @len bytes from @ga (guest address) to @data (kernel space).
233 *
234 * The behaviour of read_guest is identical to write_guest, except that
235 * data will be copied from guest space to kernel space.
236 */
237static inline __must_check
238int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data,
239 unsigned long len)
240{
241 return access_guest(vcpu, ga, ar, data, len, GACC_FETCH);
242}
243
244/**
245 * read_guest_instr - copy instruction data from guest space to kernel space
246 * @vcpu: virtual cpu
247 * @ga: guest address
248 * @data: destination address in kernel space
249 * @len: number of bytes to copy
250 *
251 * Copy @len bytes from the given address (guest space) to @data (kernel
252 * space).
253 *
254 * The behaviour of read_guest_instr is identical to read_guest, except that
255 * instruction data will be read from primary space when in home-space or
256 * address-space mode.
257 */
258static inline __must_check
259int read_guest_instr(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
260 unsigned long len)
261{
262 return access_guest(vcpu, ga, 0, data, len, GACC_IFETCH);
263}
264
265/**
266 * write_guest_abs - copy data from kernel space to guest space absolute
267 * @vcpu: virtual cpu
268 * @gpa: guest physical (absolute) address
269 * @data: source address in kernel space
270 * @len: number of bytes to copy
271 *
272 * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
273 * It is up to the caller to ensure that the entire guest memory range is
274 * valid memory before calling this function.
275 * Guest low address and key protection are not checked.
276 *
277 * Returns zero on success or -EFAULT on error.
278 *
279 * If an error occurs data may have been copied partially to guest memory.
280 */
281static inline __must_check
282int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
283 unsigned long len)
284{
285 return kvm_write_guest(vcpu->kvm, gpa, data, len);
286}
287
288/**
289 * read_guest_abs - copy data from guest space absolute to kernel space
290 * @vcpu: virtual cpu
291 * @gpa: guest physical (absolute) address
292 * @data: destination address in kernel space
293 * @len: number of bytes to copy
294 *
295 * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
296 * It is up to the caller to ensure that the entire guest memory range is
297 * valid memory before calling this function.
298 * Guest key protection is not checked.
299 *
300 * Returns zero on success or -EFAULT on error.
301 *
302 * If an error occurs data may have been copied partially to kernel space.
303 */
304static inline __must_check
305int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
306 unsigned long len)
307{
308 return kvm_read_guest(vcpu->kvm, gpa, data, len);
309}
310
311/**
312 * write_guest_real - copy data from kernel space to guest space real
313 * @vcpu: virtual cpu
314 * @gra: guest real address
315 * @data: source address in kernel space
316 * @len: number of bytes to copy
317 *
318 * Copy @len bytes from @data (kernel space) to @gra (guest real address).
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 low address and key protection are 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 guest memory.
326 */
327static inline __must_check
328int write_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, 1);
332}
333
334/**
335 * read_guest_real - copy data from guest space real to kernel space
336 * @vcpu: virtual cpu
337 * @gra: guest real address
338 * @data: destination address in kernel space
339 * @len: number of bytes to copy
340 *
341 * Copy @len bytes from @gra (guest real address) to @data (kernel space).
342 * It is up to the caller to ensure that the entire guest memory range is
343 * valid memory before calling this function.
344 * Guest key protection is not checked.
345 *
346 * Returns zero on success or -EFAULT on error.
347 *
348 * If an error occurs data may have been copied partially to kernel space.
349 */
350static inline __must_check
351int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
352 unsigned long len)
353{
354 return access_guest_real(vcpu, gra, data, len, 0);
355}
356
357void ipte_lock(struct kvm_vcpu *vcpu);
358void ipte_unlock(struct kvm_vcpu *vcpu);
359int ipte_lock_held(struct kvm_vcpu *vcpu);
360int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra);
361
362int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *shadow,
363 unsigned long saddr);
364
365#endif /* __KVM_S390_GACCESS_H */