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