gaccess.h 3.1 KB
Newer Older
1
/*
2
 * access guest memory
3
 *
4
 * Copyright IBM Corp. 2008, 2009
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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>
19
#include "kvm-s390.h"
20

21 22 23 24 25 26 27 28 29 30 31 32
/* Convert real to absolute address by applying the prefix of the CPU */
static inline unsigned long kvm_s390_real_to_abs(struct kvm_vcpu *vcpu,
						 unsigned long gaddr)
{
	unsigned long prefix  = vcpu->arch.sie_block->prefix;
	if (gaddr < 2 * PAGE_SIZE)
		gaddr += prefix;
	else if (gaddr >= prefix && gaddr < prefix + 2 * PAGE_SIZE)
		gaddr -= prefix;
	return gaddr;
}

33 34 35
static inline void __user *__gptr_to_uptr(struct kvm_vcpu *vcpu,
					  void __user *gptr,
					  int prefixing)
36
{
37 38 39
	unsigned long gaddr = (unsigned long) gptr;
	unsigned long uaddr;

40 41
	if (prefixing)
		gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
42 43 44
	uaddr = gmap_fault(gaddr, vcpu->arch.gmap);
	if (IS_ERR_VALUE(uaddr))
		uaddr = -EFAULT;
45
	return (void __user *)uaddr;
46 47
}

48 49
#define get_guest(vcpu, x, gptr)				\
({								\
50
	__typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr, 1);\
51
	int __mask = sizeof(__typeof__(*(gptr))) - 1;		\
52
	int __ret;						\
53
								\
54 55 56
	if (IS_ERR((void __force *)__uptr)) {			\
		__ret = PTR_ERR((void __force *)__uptr);	\
	} else {						\
57 58 59 60 61 62 63 64
		BUG_ON((unsigned long)__uptr & __mask);		\
		__ret = get_user(x, __uptr);			\
	}							\
	__ret;							\
})

#define put_guest(vcpu, x, gptr)				\
({								\
65
	__typeof__(gptr) __uptr = __gptr_to_uptr(vcpu, gptr, 1);\
66
	int __mask = sizeof(__typeof__(*(gptr))) - 1;		\
67
	int __ret;						\
68
								\
69 70 71
	if (IS_ERR((void __force *)__uptr)) {			\
		__ret = PTR_ERR((void __force *)__uptr);	\
	} else {						\
72 73 74 75 76
		BUG_ON((unsigned long)__uptr & __mask);		\
		__ret = put_user(x, __uptr);			\
	}							\
	__ret;							\
})
77

78 79 80
static inline int __copy_guest(struct kvm_vcpu *vcpu, unsigned long to,
			       unsigned long from, unsigned long len,
			       int to_guest, int prefixing)
81
{
82
	unsigned long _len, rc;
83
	void __user *uptr;
84 85

	while (len) {
86
		uptr = to_guest ? (void __user *)to : (void __user *)from;
87
		uptr = __gptr_to_uptr(vcpu, uptr, prefixing);
88
		if (IS_ERR((void __force *)uptr))
89 90 91 92
			return -EFAULT;
		_len = PAGE_SIZE - ((unsigned long)uptr & (PAGE_SIZE - 1));
		_len = min(_len, len);
		if (to_guest)
93
			rc = copy_to_user((void __user *) uptr, (void *)from, _len);
94
		else
95
			rc = copy_from_user((void *)to, (void __user *)uptr, _len);
96 97 98 99 100
		if (rc)
			return -EFAULT;
		len -= _len;
		from += _len;
		to += _len;
101 102 103 104
	}
	return 0;
}

105 106 107 108 109 110 111 112
#define copy_to_guest(vcpu, to, from, size) \
	__copy_guest(vcpu, to, (unsigned long)from, size, 1, 1)
#define copy_from_guest(vcpu, to, from, size) \
	__copy_guest(vcpu, (unsigned long)to, from, size, 0, 1)
#define copy_to_guest_absolute(vcpu, to, from, size) \
	__copy_guest(vcpu, to, (unsigned long)from, size, 1, 0)
#define copy_from_guest_absolute(vcpu, to, from, size) \
	__copy_guest(vcpu, (unsigned long)to, from, size, 0, 0)
113

114
#endif /* __KVM_S390_GACCESS_H */