Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * access guest memory |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2008, 2009 |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 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 <asm/uaccess.h> |
Christian Ehrhardt | 628eb9b | 2009-05-25 13:40:51 +0200 | [diff] [blame] | 19 | #include "kvm-s390.h" |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 20 | |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 21 | static inline void __user *__gptr_to_uptr(struct kvm_vcpu *vcpu, |
| 22 | void __user *gptr, |
| 23 | int prefixing) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 24 | { |
Martin Schwidefsky | 0096369 | 2008-07-25 15:51:00 +0200 | [diff] [blame] | 25 | unsigned long prefix = vcpu->arch.sie_block->prefix; |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 26 | unsigned long gaddr = (unsigned long) gptr; |
| 27 | unsigned long uaddr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 28 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 29 | if (prefixing) { |
| 30 | if (gaddr < 2 * PAGE_SIZE) |
| 31 | gaddr += prefix; |
| 32 | else if ((gaddr >= prefix) && (gaddr < prefix + 2 * PAGE_SIZE)) |
| 33 | gaddr -= prefix; |
| 34 | } |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 35 | uaddr = gmap_fault(gaddr, vcpu->arch.gmap); |
| 36 | if (IS_ERR_VALUE(uaddr)) |
| 37 | uaddr = -EFAULT; |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 38 | return (void __user *)uaddr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 41 | #define get_guest(vcpu, x, gptr) \ |
| 42 | ({ \ |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 43 | __typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr, 1);\ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 44 | int __mask = sizeof(__typeof__(*(gptr))) - 1; \ |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 45 | int __ret = PTR_RET((void __force *)__uptr); \ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 46 | \ |
| 47 | if (!__ret) { \ |
| 48 | BUG_ON((unsigned long)__uptr & __mask); \ |
| 49 | __ret = get_user(x, __uptr); \ |
| 50 | } \ |
| 51 | __ret; \ |
| 52 | }) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 53 | |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 54 | #define put_guest(vcpu, x, gptr) \ |
| 55 | ({ \ |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 56 | __typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr, 1);\ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 57 | int __mask = sizeof(__typeof__(*(gptr))) - 1; \ |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 58 | int __ret = PTR_RET((void __force *)__uptr); \ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 59 | \ |
| 60 | if (!__ret) { \ |
| 61 | BUG_ON((unsigned long)__uptr & __mask); \ |
| 62 | __ret = put_user(x, __uptr); \ |
| 63 | } \ |
| 64 | __ret; \ |
| 65 | }) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 66 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 67 | static inline int __copy_guest(struct kvm_vcpu *vcpu, unsigned long to, |
| 68 | unsigned long from, unsigned long len, |
| 69 | int to_guest, int prefixing) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 70 | { |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 71 | unsigned long _len, rc; |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 72 | void __user *uptr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 73 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 74 | while (len) { |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 75 | uptr = to_guest ? (void __user *)to : (void __user *)from; |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 76 | uptr = __gptr_to_uptr(vcpu, uptr, prefixing); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 77 | if (IS_ERR((void __force *)uptr)) |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 78 | return -EFAULT; |
| 79 | _len = PAGE_SIZE - ((unsigned long)uptr & (PAGE_SIZE - 1)); |
| 80 | _len = min(_len, len); |
| 81 | if (to_guest) |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 82 | rc = copy_to_user((void __user *) uptr, (void *)from, _len); |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 83 | else |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 84 | rc = copy_from_user((void *)to, (void __user *)uptr, _len); |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 85 | if (rc) |
| 86 | return -EFAULT; |
| 87 | len -= _len; |
| 88 | from += _len; |
| 89 | to += _len; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 94 | #define copy_to_guest(vcpu, to, from, size) \ |
| 95 | __copy_guest(vcpu, to, (unsigned long)from, size, 1, 1) |
| 96 | #define copy_from_guest(vcpu, to, from, size) \ |
| 97 | __copy_guest(vcpu, (unsigned long)to, from, size, 0, 1) |
| 98 | #define copy_to_guest_absolute(vcpu, to, from, size) \ |
| 99 | __copy_guest(vcpu, to, (unsigned long)from, size, 1, 0) |
| 100 | #define copy_from_guest_absolute(vcpu, to, from, size) \ |
| 101 | __copy_guest(vcpu, (unsigned long)to, from, size, 0, 0) |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 102 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 103 | #endif /* __KVM_S390_GACCESS_H */ |