blob: a07ee08ac478cbe77aab0c8118b808a0cc2b6c15 [file] [log] [blame]
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * access guest memory
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003 *
Heiko Carstensd95fb122014-01-01 16:23:29 +01004 * Copyright IBM Corp. 2008, 2014
Heiko Carstensb0c632d2008-03-25 18:47:20 +01005 *
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>
Heiko Carstensd95fb122014-01-01 16:23:29 +010018#include <linux/uaccess.h>
19#include <linux/ptrace.h>
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +020020#include "kvm-s390.h"
Heiko Carstensb0c632d2008-03-25 18:47:20 +010021
Heiko Carstense497a962014-01-01 16:19:55 +010022/**
23 * kvm_s390_real_to_abs - convert guest real address to guest absolute address
24 * @vcpu - guest virtual cpu
25 * @gra - guest real address
26 *
27 * Returns the guest absolute address that corresponds to the passed guest real
28 * address @gra of a virtual guest cpu by applying its prefix.
29 */
Thomas Huth732e5632013-09-12 10:33:47 +020030static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
Heiko Carstense497a962014-01-01 16:19:55 +010031 unsigned long gra)
Thomas Huth732e5632013-09-12 10:33:47 +020032{
Michael Muellerfda902c2014-05-13 16:58:30 +020033 unsigned long prefix = kvm_s390_get_prefix(vcpu);
Heiko Carstense497a962014-01-01 16:19:55 +010034
35 if (gra < 2 * PAGE_SIZE)
36 gra += prefix;
37 else if (gra >= prefix && gra < prefix + 2 * PAGE_SIZE)
38 gra -= prefix;
39 return gra;
Thomas Huth732e5632013-09-12 10:33:47 +020040}
41
Heiko Carstens072c9872014-01-01 16:21:47 +010042/**
43 * kvm_s390_logical_to_effective - convert guest logical to effective address
44 * @vcpu: guest virtual cpu
45 * @ga: guest logical address
46 *
47 * Convert a guest vcpu logical address to a guest vcpu effective address by
48 * applying the rules of the vcpu's addressing mode defined by PSW bits 31
49 * and 32 (extendended/basic addressing mode).
50 *
51 * Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing
52 * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode)
53 * of @ga will be zeroed and the remaining bits will be returned.
54 */
55static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu,
56 unsigned long ga)
57{
58 psw_t *psw = &vcpu->arch.sie_block->gpsw;
59
60 if (psw_bits(*psw).eaba == PSW_AMODE_64BIT)
61 return ga;
62 if (psw_bits(*psw).eaba == PSW_AMODE_31BIT)
63 return ga & ((1UL << 31) - 1);
64 return ga & ((1UL << 24) - 1);
65}
66
Heiko Carstensd95fb122014-01-01 16:23:29 +010067/*
68 * put_guest_lc, read_guest_lc and write_guest_lc are guest access functions
69 * which shall only be used to access the lowcore of a vcpu.
70 * These functions should be used for e.g. interrupt handlers where no
71 * guest memory access protection facilities, like key or low address
72 * protection, are applicable.
73 * At a later point guest vcpu lowcore access should happen via pinned
74 * prefix pages, so that these pages can be accessed directly via the
75 * kernel mapping. All of these *_lc functions can be removed then.
76 */
77
78/**
79 * put_guest_lc - write a simple variable to a guest vcpu's lowcore
80 * @vcpu: virtual cpu
81 * @x: value to copy to guest
82 * @gra: vcpu's destination guest real address
83 *
84 * Copies a simple value from kernel space to a guest vcpu's lowcore.
85 * The size of the variable may be 1, 2, 4 or 8 bytes. The destination
86 * must be located in the vcpu's lowcore. Otherwise the result is undefined.
87 *
88 * Returns zero on success or -EFAULT on error.
89 *
90 * Note: an error indicates that either the kernel is out of memory or
91 * the guest memory mapping is broken. In any case the best solution
92 * would be to terminate the guest.
93 * It is wrong to inject a guest exception.
94 */
95#define put_guest_lc(vcpu, x, gra) \
96({ \
97 struct kvm_vcpu *__vcpu = (vcpu); \
98 __typeof__(*(gra)) __x = (x); \
99 unsigned long __gpa; \
100 \
101 __gpa = (unsigned long)(gra); \
Michael Muellerfda902c2014-05-13 16:58:30 +0200102 __gpa += kvm_s390_get_prefix(__vcpu); \
Heiko Carstensd95fb122014-01-01 16:23:29 +0100103 kvm_write_guest(__vcpu->kvm, __gpa, &__x, sizeof(__x)); \
104})
105
106/**
107 * write_guest_lc - copy data from kernel space to guest vcpu's lowcore
108 * @vcpu: virtual cpu
109 * @gra: vcpu's source guest real address
110 * @data: source address in kernel space
111 * @len: number of bytes to copy
112 *
113 * Copy data from kernel space to guest vcpu's lowcore. The entire range must
114 * be located within 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 */
123static inline __must_check
124int write_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
125 unsigned long len)
126{
Michael Muellerfda902c2014-05-13 16:58:30 +0200127 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
Heiko Carstensd95fb122014-01-01 16:23:29 +0100128
129 return kvm_write_guest(vcpu->kvm, gpa, data, len);
130}
131
132/**
133 * read_guest_lc - copy data from guest vcpu's lowcore to kernel space
134 * @vcpu: virtual cpu
135 * @gra: vcpu's source guest real address
136 * @data: destination address in kernel space
137 * @len: number of bytes to copy
138 *
139 * Copy data from guest vcpu's lowcore to kernel space. The entire range must
140 * be located within the vcpu's lowcore, otherwise the result is undefined.
141 *
142 * Returns zero on success or -EFAULT on error.
143 *
144 * Note: an error indicates that either the kernel is out of memory or
145 * the guest memory mapping is broken. In any case the best solution
146 * would be to terminate the guest.
147 * It is wrong to inject a guest exception.
148 */
149static inline __must_check
150int read_guest_lc(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
151 unsigned long len)
152{
Michael Muellerfda902c2014-05-13 16:58:30 +0200153 unsigned long gpa = gra + kvm_s390_get_prefix(vcpu);
Heiko Carstensd95fb122014-01-01 16:23:29 +0100154
155 return kvm_read_guest(vcpu->kvm, gpa, data, len);
156}
Heiko Carstens22938972014-01-01 16:26:52 +0100157
158int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
159 unsigned long len, int write);
160
161int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
162 void *data, unsigned long len, int write);
163
164/**
165 * write_guest - copy data from kernel space to guest space
166 * @vcpu: virtual cpu
167 * @ga: guest address
168 * @data: source address in kernel space
169 * @len: number of bytes to copy
170 *
171 * Copy @len bytes from @data (kernel space) to @ga (guest address).
172 * In order to copy data to guest space the PSW of the vcpu is inspected:
173 * If DAT is off data will be copied to guest real or absolute memory.
174 * If DAT is on data will be copied to the address space as specified by
175 * the address space bits of the PSW:
176 * Primary, secondory or home space (access register mode is currently not
177 * implemented).
178 * The addressing mode of the PSW is also inspected, so that address wrap
179 * around is taken into account for 24-, 31- and 64-bit addressing mode,
180 * if the to be copied data crosses page boundaries in guest address space.
181 * In addition also low address and DAT protection are inspected before
182 * copying any data (key protection is currently not implemented).
183 *
184 * This function modifies the 'struct kvm_s390_pgm_info pgm' member of @vcpu.
185 * In case of an access exception (e.g. protection exception) pgm will contain
186 * all data necessary so that a subsequent call to 'kvm_s390_inject_prog_vcpu()'
187 * will inject a correct exception into the guest.
188 * If no access exception happened, the contents of pgm are undefined when
189 * this function returns.
190 *
191 * Returns: - zero on success
192 * - a negative value if e.g. the guest mapping is broken or in
193 * case of out-of-memory. In this case the contents of pgm are
194 * undefined. Also parts of @data may have been copied to guest
195 * space.
196 * - a positive value if an access exception happened. In this case
197 * the returned value is the program interruption code and the
198 * contents of pgm may be used to inject an exception into the
199 * guest. No data has been copied to guest space.
200 *
201 * Note: in case an access exception is recognized no data has been copied to
202 * guest space (this is also true, if the to be copied data would cross
203 * one or more page boundaries in guest space).
204 * Therefore this function may be used for nullifying and suppressing
205 * instruction emulation.
206 * It may also be used for terminating instructions, if it is undefined
207 * if data has been changed in guest space in case of an exception.
208 */
209static inline __must_check
210int write_guest(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
211 unsigned long len)
212{
213 return access_guest(vcpu, ga, data, len, 1);
214}
215
216/**
217 * read_guest - copy data from guest space to kernel space
218 * @vcpu: virtual cpu
219 * @ga: guest address
220 * @data: destination address in kernel space
221 * @len: number of bytes to copy
222 *
223 * Copy @len bytes from @ga (guest address) to @data (kernel space).
224 *
225 * The behaviour of read_guest is identical to write_guest, except that
226 * data will be copied from guest space to kernel space.
227 */
228static inline __must_check
229int read_guest(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
230 unsigned long len)
231{
232 return access_guest(vcpu, ga, data, len, 0);
233}
234
235/**
236 * write_guest_abs - copy data from kernel space to guest space absolute
237 * @vcpu: virtual cpu
238 * @gpa: guest physical (absolute) address
239 * @data: source address in kernel space
240 * @len: number of bytes to copy
241 *
242 * Copy @len bytes from @data (kernel space) to @gpa (guest absolute address).
243 * It is up to the caller to ensure that the entire guest memory range is
244 * valid memory before calling this function.
245 * Guest low address and key protection are not checked.
246 *
247 * Returns zero on success or -EFAULT on error.
248 *
249 * If an error occurs data may have been copied partially to guest memory.
250 */
251static inline __must_check
252int write_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
253 unsigned long len)
254{
255 return kvm_write_guest(vcpu->kvm, gpa, data, len);
256}
257
258/**
259 * read_guest_abs - copy data from guest space absolute to kernel space
260 * @vcpu: virtual cpu
261 * @gpa: guest physical (absolute) address
262 * @data: destination address in kernel space
263 * @len: number of bytes to copy
264 *
265 * Copy @len bytes from @gpa (guest absolute address) to @data (kernel space).
266 * It is up to the caller to ensure that the entire guest memory range is
267 * valid memory before calling this function.
268 * Guest key protection is not checked.
269 *
270 * Returns zero on success or -EFAULT on error.
271 *
272 * If an error occurs data may have been copied partially to kernel space.
273 */
274static inline __must_check
275int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
276 unsigned long len)
277{
278 return kvm_read_guest(vcpu->kvm, gpa, data, len);
279}
280
281/**
282 * write_guest_real - copy data from kernel space to guest space real
283 * @vcpu: virtual cpu
284 * @gra: guest real address
285 * @data: source address in kernel space
286 * @len: number of bytes to copy
287 *
288 * Copy @len bytes from @data (kernel space) to @gra (guest real address).
289 * It is up to the caller to ensure that the entire guest memory range is
290 * valid memory before calling this function.
291 * Guest low address and key protection are not checked.
292 *
293 * Returns zero on success or -EFAULT on error.
294 *
295 * If an error occurs data may have been copied partially to guest memory.
296 */
297static inline __must_check
298int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
299 unsigned long len)
300{
301 return access_guest_real(vcpu, gra, data, len, 1);
302}
303
304/**
305 * read_guest_real - copy data from guest space real to kernel space
306 * @vcpu: virtual cpu
307 * @gra: guest real address
308 * @data: destination address in kernel space
309 * @len: number of bytes to copy
310 *
311 * Copy @len bytes from @gra (guest real address) to @data (kernel space).
312 * It is up to the caller to ensure that the entire guest memory range is
313 * valid memory before calling this function.
314 * Guest key protection is not checked.
315 *
316 * Returns zero on success or -EFAULT on error.
317 *
318 * If an error occurs data may have been copied partially to kernel space.
319 */
320static inline __must_check
321int read_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
322 unsigned long len)
323{
324 return access_guest_real(vcpu, gra, data, len, 0);
325}
326
Heiko Carstens8a2422342014-01-10 14:33:28 +0100327int ipte_lock_held(struct kvm_vcpu *vcpu);
Thomas Huthf8232c82014-03-03 23:34:42 +0100328int kvm_s390_check_low_addr_protection(struct kvm_vcpu *vcpu, unsigned long ga);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100329
Heiko Carstensf9dc72e2013-03-05 13:14:45 +0100330#endif /* __KVM_S390_GACCESS_H */