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 | |
Thomas Huth | 732e563 | 2013-09-12 10:33:47 +0200 | [diff] [blame] | 21 | /* Convert real to absolute address by applying the prefix of the CPU */ |
| 22 | static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu, |
| 23 | unsigned long gaddr) |
| 24 | { |
| 25 | unsigned long prefix = vcpu->arch.sie_block->prefix; |
| 26 | if (gaddr < 2 * PAGE_SIZE) |
| 27 | gaddr += prefix; |
| 28 | else if (gaddr >= prefix && gaddr < prefix + 2 * PAGE_SIZE) |
| 29 | gaddr -= prefix; |
| 30 | return gaddr; |
| 31 | } |
| 32 | |
Heiko Carstens | 072c987 | 2014-01-01 16:21:47 +0100 | [diff] [blame^] | 33 | /** |
| 34 | * kvm_s390_logical_to_effective - convert guest logical to effective address |
| 35 | * @vcpu: guest virtual cpu |
| 36 | * @ga: guest logical address |
| 37 | * |
| 38 | * Convert a guest vcpu logical address to a guest vcpu effective address by |
| 39 | * applying the rules of the vcpu's addressing mode defined by PSW bits 31 |
| 40 | * and 32 (extendended/basic addressing mode). |
| 41 | * |
| 42 | * Depending on the vcpu's addressing mode the upper 40 bits (24 bit addressing |
| 43 | * mode), 33 bits (31 bit addressing mode) or no bits (64 bit addressing mode) |
| 44 | * of @ga will be zeroed and the remaining bits will be returned. |
| 45 | */ |
| 46 | static inline unsigned long kvm_s390_logical_to_effective(struct kvm_vcpu *vcpu, |
| 47 | unsigned long ga) |
| 48 | { |
| 49 | psw_t *psw = &vcpu->arch.sie_block->gpsw; |
| 50 | |
| 51 | if (psw_bits(*psw).eaba == PSW_AMODE_64BIT) |
| 52 | return ga; |
| 53 | if (psw_bits(*psw).eaba == PSW_AMODE_31BIT) |
| 54 | return ga & ((1UL << 31) - 1); |
| 55 | return ga & ((1UL << 24) - 1); |
| 56 | } |
| 57 | |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 58 | static inline void __user *__gptr_to_uptr(struct kvm_vcpu *vcpu, |
| 59 | void __user *gptr, |
| 60 | int prefixing) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 61 | { |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 62 | unsigned long gaddr = (unsigned long) gptr; |
| 63 | unsigned long uaddr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 64 | |
Thomas Huth | 732e563 | 2013-09-12 10:33:47 +0200 | [diff] [blame] | 65 | if (prefixing) |
| 66 | gaddr = kvm_s390_real_to_abs(vcpu, gaddr); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 67 | uaddr = gmap_fault(gaddr, vcpu->arch.gmap); |
| 68 | if (IS_ERR_VALUE(uaddr)) |
| 69 | uaddr = -EFAULT; |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 70 | return (void __user *)uaddr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 73 | #define get_guest(vcpu, x, gptr) \ |
| 74 | ({ \ |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 75 | __typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr, 1);\ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 76 | int __mask = sizeof(__typeof__(*(gptr))) - 1; \ |
Rusty Russell | 228b822 | 2013-07-15 11:21:32 +0930 | [diff] [blame] | 77 | int __ret; \ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 78 | \ |
Rusty Russell | 228b822 | 2013-07-15 11:21:32 +0930 | [diff] [blame] | 79 | if (IS_ERR((void __force *)__uptr)) { \ |
| 80 | __ret = PTR_ERR((void __force *)__uptr); \ |
| 81 | } else { \ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 82 | BUG_ON((unsigned long)__uptr & __mask); \ |
| 83 | __ret = get_user(x, __uptr); \ |
| 84 | } \ |
| 85 | __ret; \ |
| 86 | }) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 87 | |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 88 | #define put_guest(vcpu, x, gptr) \ |
| 89 | ({ \ |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 90 | __typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr, 1);\ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 91 | int __mask = sizeof(__typeof__(*(gptr))) - 1; \ |
Rusty Russell | 228b822 | 2013-07-15 11:21:32 +0930 | [diff] [blame] | 92 | int __ret; \ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 93 | \ |
Rusty Russell | 228b822 | 2013-07-15 11:21:32 +0930 | [diff] [blame] | 94 | if (IS_ERR((void __force *)__uptr)) { \ |
| 95 | __ret = PTR_ERR((void __force *)__uptr); \ |
| 96 | } else { \ |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 97 | BUG_ON((unsigned long)__uptr & __mask); \ |
| 98 | __ret = put_user(x, __uptr); \ |
| 99 | } \ |
| 100 | __ret; \ |
| 101 | }) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 102 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 103 | static inline int __copy_guest(struct kvm_vcpu *vcpu, unsigned long to, |
| 104 | unsigned long from, unsigned long len, |
| 105 | int to_guest, int prefixing) |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 106 | { |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 107 | unsigned long _len, rc; |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 108 | void __user *uptr; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 109 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 110 | while (len) { |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 111 | uptr = to_guest ? (void __user *)to : (void __user *)from; |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 112 | uptr = __gptr_to_uptr(vcpu, uptr, prefixing); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 113 | if (IS_ERR((void __force *)uptr)) |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 114 | return -EFAULT; |
| 115 | _len = PAGE_SIZE - ((unsigned long)uptr & (PAGE_SIZE - 1)); |
| 116 | _len = min(_len, len); |
| 117 | if (to_guest) |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 118 | rc = copy_to_user((void __user *) uptr, (void *)from, _len); |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 119 | else |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 120 | rc = copy_from_user((void *)to, (void __user *)uptr, _len); |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 121 | if (rc) |
| 122 | return -EFAULT; |
| 123 | len -= _len; |
| 124 | from += _len; |
| 125 | to += _len; |
Heiko Carstens | b0c632d | 2008-03-25 18:47:20 +0100 | [diff] [blame] | 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 130 | #define copy_to_guest(vcpu, to, from, size) \ |
| 131 | __copy_guest(vcpu, to, (unsigned long)from, size, 1, 1) |
| 132 | #define copy_from_guest(vcpu, to, from, size) \ |
| 133 | __copy_guest(vcpu, (unsigned long)to, from, size, 0, 1) |
| 134 | #define copy_to_guest_absolute(vcpu, to, from, size) \ |
| 135 | __copy_guest(vcpu, to, (unsigned long)from, size, 1, 0) |
| 136 | #define copy_from_guest_absolute(vcpu, to, from, size) \ |
| 137 | __copy_guest(vcpu, (unsigned long)to, from, size, 0, 0) |
Carsten Otte | 092670c | 2011-07-24 10:48:22 +0200 | [diff] [blame] | 138 | |
Heiko Carstens | f9dc72e | 2013-03-05 13:14:45 +0100 | [diff] [blame] | 139 | #endif /* __KVM_S390_GACCESS_H */ |