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