2008-03-26 01:47:20 +08:00
|
|
|
/*
|
2012-07-20 17:15:04 +08:00
|
|
|
* access guest memory
|
2008-03-26 01:47:20 +08:00
|
|
|
*
|
2012-07-20 17:15:04 +08:00
|
|
|
* Copyright IBM Corp. 2008, 2009
|
2008-03-26 01:47:20 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License (version 2 only)
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* Author(s): Carsten Otte <cotte@de.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __KVM_S390_GACCESS_H
|
|
|
|
#define __KVM_S390_GACCESS_H
|
|
|
|
|
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <asm/uaccess.h>
|
2009-05-25 19:40:51 +08:00
|
|
|
#include "kvm-s390.h"
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2013-03-05 20:14:44 +08:00
|
|
|
static inline void *__gptr_to_uptr(struct kvm_vcpu *vcpu, void *gptr)
|
2008-03-26 01:47:20 +08:00
|
|
|
{
|
2008-07-25 21:51:00 +08:00
|
|
|
unsigned long prefix = vcpu->arch.sie_block->prefix;
|
2013-03-05 20:14:44 +08:00
|
|
|
unsigned long gaddr = (unsigned long) gptr;
|
|
|
|
unsigned long uaddr;
|
|
|
|
|
|
|
|
if (gaddr < 2 * PAGE_SIZE)
|
|
|
|
gaddr += prefix;
|
|
|
|
else if ((gaddr >= prefix) && (gaddr < prefix + 2 * PAGE_SIZE))
|
|
|
|
gaddr -= prefix;
|
|
|
|
uaddr = gmap_fault(gaddr, vcpu->arch.gmap);
|
|
|
|
if (IS_ERR_VALUE(uaddr))
|
|
|
|
uaddr = -EFAULT;
|
|
|
|
return (void *)uaddr;
|
2008-03-26 01:47:20 +08:00
|
|
|
}
|
|
|
|
|
2013-03-05 20:14:44 +08:00
|
|
|
#define get_guest(vcpu, x, gptr) \
|
|
|
|
({ \
|
|
|
|
__typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr); \
|
|
|
|
int __mask = sizeof(__typeof__(*(gptr))) - 1; \
|
|
|
|
int __ret = PTR_RET(__uptr); \
|
|
|
|
\
|
|
|
|
if (!__ret) { \
|
|
|
|
BUG_ON((unsigned long)__uptr & __mask); \
|
|
|
|
__ret = get_user(x, __uptr); \
|
|
|
|
} \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define put_guest(vcpu, x, gptr) \
|
|
|
|
({ \
|
|
|
|
__typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr); \
|
|
|
|
int __mask = sizeof(__typeof__(*(gptr))) - 1; \
|
|
|
|
int __ret = PTR_RET(__uptr); \
|
|
|
|
\
|
|
|
|
if (!__ret) { \
|
|
|
|
BUG_ON((unsigned long)__uptr & __mask); \
|
|
|
|
__ret = put_user(x, __uptr); \
|
|
|
|
} \
|
|
|
|
__ret; \
|
|
|
|
})
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2008-07-25 21:51:00 +08:00
|
|
|
static inline int __copy_to_guest_slow(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long guestdest,
|
2011-07-24 16:48:22 +08:00
|
|
|
void *from, unsigned long n)
|
2008-03-26 01:47:20 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned long i;
|
2011-07-24 16:48:22 +08:00
|
|
|
u8 *data = from;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2013-03-05 20:14:44 +08:00
|
|
|
rc = put_guest(vcpu, *(data++), (u8 *)guestdest++);
|
2008-03-26 01:47:20 +08:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
static inline int __copy_to_guest_fast(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long guestdest,
|
|
|
|
void *from, unsigned long n)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
void __user *uptr;
|
|
|
|
unsigned long size;
|
|
|
|
|
|
|
|
if (guestdest + n < guestdest)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* simple case: all within one segment table entry? */
|
|
|
|
if ((guestdest & PMD_MASK) == ((guestdest+n) & PMD_MASK)) {
|
|
|
|
uptr = (void __user *) gmap_fault(guestdest, vcpu->arch.gmap);
|
|
|
|
|
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
|
|
|
|
|
|
|
r = copy_to_user(uptr, from, n);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
r = -EFAULT;
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy first segment */
|
|
|
|
uptr = (void __user *)gmap_fault(guestdest, vcpu->arch.gmap);
|
|
|
|
|
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
|
|
|
|
|
|
|
size = PMD_SIZE - (guestdest & ~PMD_MASK);
|
|
|
|
|
|
|
|
r = copy_to_user(uptr, from, size);
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
r = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
from += size;
|
|
|
|
n -= size;
|
|
|
|
guestdest += size;
|
|
|
|
|
|
|
|
/* copy full segments */
|
|
|
|
while (n >= PMD_SIZE) {
|
|
|
|
uptr = (void __user *)gmap_fault(guestdest, vcpu->arch.gmap);
|
|
|
|
|
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
|
|
|
|
|
|
|
r = copy_to_user(uptr, from, PMD_SIZE);
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
r = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
from += PMD_SIZE;
|
|
|
|
n -= PMD_SIZE;
|
|
|
|
guestdest += PMD_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the tail segment */
|
|
|
|
if (n) {
|
|
|
|
uptr = (void __user *)gmap_fault(guestdest, vcpu->arch.gmap);
|
|
|
|
|
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
|
|
|
|
|
|
|
r = copy_to_user(uptr, from, n);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
r = -EFAULT;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int copy_to_guest_absolute(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long guestdest,
|
|
|
|
void *from, unsigned long n)
|
|
|
|
{
|
|
|
|
return __copy_to_guest_fast(vcpu, guestdest, from, n);
|
|
|
|
}
|
|
|
|
|
2008-07-25 21:51:00 +08:00
|
|
|
static inline int copy_to_guest(struct kvm_vcpu *vcpu, unsigned long guestdest,
|
2011-07-24 16:48:22 +08:00
|
|
|
void *from, unsigned long n)
|
2008-03-26 01:47:20 +08:00
|
|
|
{
|
2008-07-25 21:51:00 +08:00
|
|
|
unsigned long prefix = vcpu->arch.sie_block->prefix;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
|
|
|
if ((guestdest < 2 * PAGE_SIZE) && (guestdest + n > 2 * PAGE_SIZE))
|
|
|
|
goto slowpath;
|
|
|
|
|
|
|
|
if ((guestdest < prefix) && (guestdest + n > prefix))
|
|
|
|
goto slowpath;
|
|
|
|
|
|
|
|
if ((guestdest < prefix + 2 * PAGE_SIZE)
|
|
|
|
&& (guestdest + n > prefix + 2 * PAGE_SIZE))
|
|
|
|
goto slowpath;
|
|
|
|
|
|
|
|
if (guestdest < 2 * PAGE_SIZE)
|
|
|
|
guestdest += prefix;
|
|
|
|
else if ((guestdest >= prefix) && (guestdest < prefix + 2 * PAGE_SIZE))
|
|
|
|
guestdest -= prefix;
|
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
return __copy_to_guest_fast(vcpu, guestdest, from, n);
|
2008-03-26 01:47:20 +08:00
|
|
|
slowpath:
|
|
|
|
return __copy_to_guest_slow(vcpu, guestdest, from, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int __copy_from_guest_slow(struct kvm_vcpu *vcpu, void *to,
|
2008-07-25 21:51:00 +08:00
|
|
|
unsigned long guestsrc,
|
|
|
|
unsigned long n)
|
2008-03-26 01:47:20 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned long i;
|
|
|
|
u8 *data = to;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2013-03-05 20:14:44 +08:00
|
|
|
rc = get_guest(vcpu, *(data++), (u8 *)guestsrc++);
|
2008-03-26 01:47:20 +08:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
static inline int __copy_from_guest_fast(struct kvm_vcpu *vcpu, void *to,
|
|
|
|
unsigned long guestsrc,
|
|
|
|
unsigned long n)
|
2008-03-26 01:47:20 +08:00
|
|
|
{
|
2011-07-24 16:48:22 +08:00
|
|
|
int r;
|
|
|
|
void __user *uptr;
|
|
|
|
unsigned long size;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if (guestsrc + n < guestsrc)
|
|
|
|
return -EFAULT;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
/* simple case: all within one segment table entry? */
|
|
|
|
if ((guestsrc & PMD_MASK) == ((guestsrc+n) & PMD_MASK)) {
|
|
|
|
uptr = (void __user *) gmap_fault(guestsrc, vcpu->arch.gmap);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
r = copy_from_user(to, uptr, n);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if (r)
|
|
|
|
r = -EFAULT;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
/* copy first segment */
|
|
|
|
uptr = (void __user *)gmap_fault(guestsrc, vcpu->arch.gmap);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
size = PMD_SIZE - (guestsrc & ~PMD_MASK);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
r = copy_from_user(to, uptr, size);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if (r) {
|
|
|
|
r = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
to += size;
|
|
|
|
n -= size;
|
|
|
|
guestsrc += size;
|
|
|
|
|
|
|
|
/* copy full segments */
|
|
|
|
while (n >= PMD_SIZE) {
|
|
|
|
uptr = (void __user *)gmap_fault(guestsrc, vcpu->arch.gmap);
|
|
|
|
|
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
|
|
|
|
|
|
|
r = copy_from_user(to, uptr, PMD_SIZE);
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
r = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
to += PMD_SIZE;
|
|
|
|
n -= PMD_SIZE;
|
|
|
|
guestsrc += PMD_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the tail segment */
|
|
|
|
if (n) {
|
|
|
|
uptr = (void __user *)gmap_fault(guestsrc, vcpu->arch.gmap);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if (IS_ERR((void __force *) uptr))
|
|
|
|
return PTR_ERR((void __force *) uptr);
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
r = copy_from_user(to, uptr, n);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
r = -EFAULT;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return r;
|
2008-03-26 01:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int copy_from_guest_absolute(struct kvm_vcpu *vcpu, void *to,
|
2008-07-25 21:51:00 +08:00
|
|
|
unsigned long guestsrc,
|
|
|
|
unsigned long n)
|
2008-03-26 01:47:20 +08:00
|
|
|
{
|
2011-07-24 16:48:22 +08:00
|
|
|
return __copy_from_guest_fast(vcpu, to, guestsrc, n);
|
|
|
|
}
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
static inline int copy_from_guest(struct kvm_vcpu *vcpu, void *to,
|
|
|
|
unsigned long guestsrc, unsigned long n)
|
|
|
|
{
|
|
|
|
unsigned long prefix = vcpu->arch.sie_block->prefix;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if ((guestsrc < 2 * PAGE_SIZE) && (guestsrc + n > 2 * PAGE_SIZE))
|
|
|
|
goto slowpath;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
if ((guestsrc < prefix) && (guestsrc + n > prefix))
|
|
|
|
goto slowpath;
|
|
|
|
|
|
|
|
if ((guestsrc < prefix + 2 * PAGE_SIZE)
|
|
|
|
&& (guestsrc + n > prefix + 2 * PAGE_SIZE))
|
|
|
|
goto slowpath;
|
|
|
|
|
|
|
|
if (guestsrc < 2 * PAGE_SIZE)
|
|
|
|
guestsrc += prefix;
|
|
|
|
else if ((guestsrc >= prefix) && (guestsrc < prefix + 2 * PAGE_SIZE))
|
|
|
|
guestsrc -= prefix;
|
2008-03-26 01:47:20 +08:00
|
|
|
|
2011-07-24 16:48:22 +08:00
|
|
|
return __copy_from_guest_fast(vcpu, to, guestsrc, n);
|
|
|
|
slowpath:
|
|
|
|
return __copy_from_guest_slow(vcpu, to, guestsrc, n);
|
2008-03-26 01:47:20 +08:00
|
|
|
}
|
|
|
|
#endif
|