Mailing List Archive

[PATCH v4 4/6] xen: introduce XEN_GUEST_HANDLE_PARAM
XEN_GUEST_HANDLE_PARAM is going to be used to distinguish guest pointers
stored in memory from guest pointers as hypercall parameters.

guest_handle_* macros default to XEN_GUEST_HANDLE_PARAM as return type.
Two new guest_handle_to_param and guest_handle_from_param macros are
introduced to do conversions.

Changes in v2:

- add 2 missing #define _XEN_GUEST_HANDLE_PARAM for the compilation of
the compat code.

Changes in v3:

- move the guest_handle_cast change into this patch;
- add a clear comment on top of guest_handle_cast;
- also s/XEN_GUEST_HANDLE/XEN_GUEST_HANDLE_PARAM in
guest_handle_from_ptr and const_guest_handle_from_ptr;
- introduce guest_handle_from_param and guest_handle_to_param.

Changes in v4:

- make both XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM unions on ARM;
- simplify set_xen_guest_handle_raw on ARM;
- add type checking in guest_handle_to_param and guest_handle_from_param
trivial.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
xen/include/asm-arm/guest_access.h | 32 ++++++++++++++++++++++++++++----
xen/include/asm-x86/guest_access.h | 29 +++++++++++++++++++++++++----
xen/include/public/arch-arm.h | 26 ++++++++++++++++++++++----
xen/include/public/arch-ia64.h | 9 +++++++++
xen/include/public/arch-x86/xen.h | 9 +++++++++
xen/include/xen/xencomm.h | 22 +++++++++++++++++++++-
6 files changed, 114 insertions(+), 13 deletions(-)

diff --git a/xen/include/asm-arm/guest_access.h b/xen/include/asm-arm/guest_access.h
index 0fceae6..5686217 100644
--- a/xen/include/asm-arm/guest_access.h
+++ b/xen/include/asm-arm/guest_access.h
@@ -27,16 +27,40 @@ unsigned long raw_clear_guest(void *to, unsigned len);
#define guest_handle_add_offset(hnd, nr) ((hnd).p += (nr))
#define guest_handle_subtract_offset(hnd, nr) ((hnd).p -= (nr))

-/* Cast a guest handle to the specified type of handle. */
+/* Cast a guest handle (either XEN_GUEST_HANDLE or XEN_GUEST_HANDLE_PARAM)
+ * to the specified type of XEN_GUEST_HANDLE_PARAM. */
#define guest_handle_cast(hnd, type) ({ \
type *_x = (hnd).p; \
- (XEN_GUEST_HANDLE(type)) { _x }; \
+ (XEN_GUEST_HANDLE_PARAM(type)) { _x }; \
+})
+
+/* Cast a XEN_GUEST_HANDLE to XEN_GUEST_HANDLE_PARAM */
+#define guest_handle_to_param(hnd, type) ({ \
+ typeof((hnd).p) _x = (hnd).p; \
+ XEN_GUEST_HANDLE_PARAM(type) _y = { _x }; \
+ /* type checking: make sure that the pointers inside \
+ * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \
+ * the same type, then return hnd */ \
+ (void)(&_x == &_y.p); \
+ _y; \
+})
+
+
+/* Cast a XEN_GUEST_HANDLE_PARAM to XEN_GUEST_HANDLE */
+#define guest_handle_from_param(hnd, type) ({ \
+ typeof((hnd).p) _x = (hnd).p; \
+ XEN_GUEST_HANDLE(type) _y = { _x }; \
+ /* type checking: make sure that the pointers inside \
+ * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \
+ * the same type, then return hnd */ \
+ (void)(&_x == &_y.p); \
+ _y; \
})

#define guest_handle_from_ptr(ptr, type) \
- ((XEN_GUEST_HANDLE(type)) { (type *)ptr })
+ ((XEN_GUEST_HANDLE_PARAM(type)) { (type *)ptr })
#define const_guest_handle_from_ptr(ptr, type) \
- ((XEN_GUEST_HANDLE(const_##type)) { (const type *)ptr })
+ ((XEN_GUEST_HANDLE_PARAM(const_##type)) { (const type *)ptr })

/*
* Copy an array of objects to guest context via a guest handle,
diff --git a/xen/include/asm-x86/guest_access.h b/xen/include/asm-x86/guest_access.h
index 2b429c2..e67ed82 100644
--- a/xen/include/asm-x86/guest_access.h
+++ b/xen/include/asm-x86/guest_access.h
@@ -45,16 +45,37 @@
#define guest_handle_add_offset(hnd, nr) ((hnd).p += (nr))
#define guest_handle_subtract_offset(hnd, nr) ((hnd).p -= (nr))

-/* Cast a guest handle to the specified type of handle. */
+/* Cast a guest handle (either XEN_GUEST_HANDLE or XEN_GUEST_HANDLE_PARAM)
+ * to the specified type of XEN_GUEST_HANDLE_PARAM. */
#define guest_handle_cast(hnd, type) ({ \
type *_x = (hnd).p; \
- (XEN_GUEST_HANDLE(type)) { _x }; \
+ (XEN_GUEST_HANDLE_PARAM(type)) { _x }; \
+})
+
+/* Cast a XEN_GUEST_HANDLE to XEN_GUEST_HANDLE_PARAM */
+#define guest_handle_to_param(hnd, type) ({ \
+ /* type checking: make sure that the pointers inside \
+ * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \
+ * the same type, then return hnd */ \
+ (void)((typeof(&(hnd).p)) 0 == \
+ (typeof(&((XEN_GUEST_HANDLE_PARAM(type)) {}).p)) 0); \
+ (hnd); \
+})
+
+/* Cast a XEN_GUEST_HANDLE_PARAM to XEN_GUEST_HANDLE */
+#define guest_handle_from_param(hnd, type) ({ \
+ /* type checking: make sure that the pointers inside \
+ * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \
+ * the same type, then return hnd */ \
+ (void)((typeof(&(hnd).p)) 0 == \
+ (typeof(&((XEN_GUEST_HANDLE_PARAM(type)) {}).p)) 0); \
+ (hnd); \
})

#define guest_handle_from_ptr(ptr, type) \
- ((XEN_GUEST_HANDLE(type)) { (type *)ptr })
+ ((XEN_GUEST_HANDLE_PARAM(type)) { (type *)ptr })
#define const_guest_handle_from_ptr(ptr, type) \
- ((XEN_GUEST_HANDLE(const_##type)) { (const type *)ptr })
+ ((XEN_GUEST_HANDLE_PARAM(const_##type)) { (const type *)ptr })

/*
* Copy an array of objects to guest context via a guest handle,
diff --git a/xen/include/public/arch-arm.h b/xen/include/public/arch-arm.h
index 2ae6548..4c6d607 100644
--- a/xen/include/public/arch-arm.h
+++ b/xen/include/public/arch-arm.h
@@ -51,18 +51,36 @@

#define XEN_HYPERCALL_TAG 0XEA1

+#define uint64_aligned_t uint64_t __attribute__((aligned(8)))

#ifndef __ASSEMBLY__
-#define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
- typedef struct { type *p; } __guest_handle_ ## name
+#define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
+ typedef union { type *p; unsigned long q; } \
+ __guest_handle_ ## name; \
+ typedef union { type *p; uint64_aligned_t q; } \
+ __guest_handle_64_ ## name;

+/*
+ * XEN_GUEST_HANDLE represents a guest pointer, when passed as a field
+ * in a struct in memory. On ARM is always 8 bytes sizes and 8 bytes
+ * aligned.
+ * XEN_GUEST_HANDLE_PARAM represent a guest pointer, when passed as an
+ * hypercall argument. It is 4 bytes on aarch and 8 bytes on aarch64.
+ */
#define __DEFINE_XEN_GUEST_HANDLE(name, type) \
___DEFINE_XEN_GUEST_HANDLE(name, type); \
___DEFINE_XEN_GUEST_HANDLE(const_##name, const type)
#define DEFINE_XEN_GUEST_HANDLE(name) __DEFINE_XEN_GUEST_HANDLE(name, name)
-#define __XEN_GUEST_HANDLE(name) __guest_handle_ ## name
+#define __XEN_GUEST_HANDLE(name) __guest_handle_64_ ## name
#define XEN_GUEST_HANDLE(name) __XEN_GUEST_HANDLE(name)
-#define set_xen_guest_handle_raw(hnd, val) do { (hnd).p = val; } while (0)
+/* this is going to be changes on 64 bit */
+#define XEN_GUEST_HANDLE_PARAM(name) __guest_handle_ ## name
+#define set_xen_guest_handle_raw(hnd, val) \
+ do { \
+ typeof(&(hnd)) t = &(hnd); \
+ _t->q = 0; \
+ _t->p = val; \
+ } while ( 0 )
#ifdef __XEN_TOOLS__
#define get_xen_guest_handle(val, hnd) do { val = (hnd).p; } while (0)
#endif
diff --git a/xen/include/public/arch-ia64.h b/xen/include/public/arch-ia64.h
index c9da5d4..e4e9688 100644
--- a/xen/include/public/arch-ia64.h
+++ b/xen/include/public/arch-ia64.h
@@ -45,8 +45,17 @@
___DEFINE_XEN_GUEST_HANDLE(name, type); \
___DEFINE_XEN_GUEST_HANDLE(const_##name, const type)

+/*
+ * XEN_GUEST_HANDLE represents a guest pointer, when passed as a field
+ * in a struct in memory.
+ * XEN_GUEST_HANDLE_PARAM represent a guest pointer, when passed as an
+ * hypercall argument.
+ * XEN_GUEST_HANDLE_PARAM and XEN_GUEST_HANDLE are the same on ia64 but
+ * they might not be on other architectures.
+ */
#define DEFINE_XEN_GUEST_HANDLE(name) __DEFINE_XEN_GUEST_HANDLE(name, name)
#define XEN_GUEST_HANDLE(name) __guest_handle_ ## name
+#define XEN_GUEST_HANDLE_PARAM(name) XEN_GUEST_HANDLE(name)
#define XEN_GUEST_HANDLE_64(name) XEN_GUEST_HANDLE(name)
#define uint64_aligned_t uint64_t
#define set_xen_guest_handle_raw(hnd, val) do { (hnd).p = val; } while (0)
diff --git a/xen/include/public/arch-x86/xen.h b/xen/include/public/arch-x86/xen.h
index 1c186d7..0e10260 100644
--- a/xen/include/public/arch-x86/xen.h
+++ b/xen/include/public/arch-x86/xen.h
@@ -38,12 +38,21 @@
typedef type * __guest_handle_ ## name
#endif

+/*
+ * XEN_GUEST_HANDLE represents a guest pointer, when passed as a field
+ * in a struct in memory.
+ * XEN_GUEST_HANDLE_PARAM represent a guest pointer, when passed as an
+ * hypercall argument.
+ * XEN_GUEST_HANDLE_PARAM and XEN_GUEST_HANDLE are the same on X86 but
+ * they might not be on other architectures.
+ */
#define __DEFINE_XEN_GUEST_HANDLE(name, type) \
___DEFINE_XEN_GUEST_HANDLE(name, type); \
___DEFINE_XEN_GUEST_HANDLE(const_##name, const type)
#define DEFINE_XEN_GUEST_HANDLE(name) __DEFINE_XEN_GUEST_HANDLE(name, name)
#define __XEN_GUEST_HANDLE(name) __guest_handle_ ## name
#define XEN_GUEST_HANDLE(name) __XEN_GUEST_HANDLE(name)
+#define XEN_GUEST_HANDLE_PARAM(name) XEN_GUEST_HANDLE(name)
#define set_xen_guest_handle_raw(hnd, val) do { (hnd).p = val; } while (0)
#ifdef __XEN_TOOLS__
#define get_xen_guest_handle(val, hnd) do { val = (hnd).p; } while (0)
diff --git a/xen/include/xen/xencomm.h b/xen/include/xen/xencomm.h
index 730da7c..3426b8a 100644
--- a/xen/include/xen/xencomm.h
+++ b/xen/include/xen/xencomm.h
@@ -66,11 +66,31 @@ static inline unsigned long xencomm_inline_addr(const void *handle)
/* Cast a guest handle to the specified type of handle. */
#define guest_handle_cast(hnd, type) ({ \
type *_x = (hnd).p; \
- XEN_GUEST_HANDLE(type) _y; \
+ XEN_GUEST_HANDLE_PARAM(type) _y; \
set_xen_guest_handle(_y, _x); \
_y; \
})

+/* Cast a XEN_GUEST_HANDLE to XEN_GUEST_HANDLE_PARAM */
+#define guest_handle_to_param(hnd, type) ({ \
+ /* type checking: make sure that the pointers inside \
+ * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \
+ * the same type, then return hnd */ \
+ (void)((typeof(&(hnd).p)) 0 == \
+ (typeof(&((XEN_GUEST_HANDLE_PARAM(type)) {}).p)) 0); \
+ (hnd); \
+})
+
+/* Cast a XEN_GUEST_HANDLE_PARAM to XEN_GUEST_HANDLE */
+#define guest_handle_from_param(hnd, type) ({ \
+ /* type checking: make sure that the pointers inside \
+ * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \
+ * the same type, then return hnd */ \
+ (void)((typeof(&(hnd).p)) 0 == \
+ (typeof(&((XEN_GUEST_HANDLE_PARAM(type)) {}).p)) 0); \
+ (hnd); \
+})
+
/* Since we run in real mode, we can safely access all addresses. That also
* means our __routines are identical to our "normal" routines. */
#define guest_handle_okay(hnd, nr) 1
--
1.7.2.5


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel
Re: [PATCH v4 4/6] xen: introduce XEN_GUEST_HANDLE_PARAM [ In reply to ]
On Wed, 2012-08-22 at 12:08 +0100, Stefano Stabellini wrote:
> +#define set_xen_guest_handle_raw(hnd, val) \
> + do { \
> + typeof(&(hnd)) t = &(hnd); \
> + _t->q = 0; \
> + _t->p = val; \

You have a mix of t and _t here...

Since this macro is used in the tools it would be useful to avoid any
potentially clashing names (e.g. so -Wshadow doesn't moan).

_sxghr_tmp?



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel
Re: [PATCH v4 4/6] xen: introduce XEN_GUEST_HANDLE_PARAM [ In reply to ]
On Wed, 2012-08-22 at 12:08 +0100, Stefano Stabellini wrote:
> diff --git a/xen/include/asm-arm/guest_access.h b/xen/include/asm-arm/guest_access.h
> index 0fceae6..5686217 100644
> --- a/xen/include/asm-arm/guest_access.h
> +++ b/xen/include/asm-arm/guest_access.h
> @@ -27,16 +27,40 @@ unsigned long raw_clear_guest(void *to, unsigned len);
> #define guest_handle_add_offset(hnd, nr) ((hnd).p += (nr))
> #define guest_handle_subtract_offset(hnd, nr) ((hnd).p -= (nr))
>
> -/* Cast a guest handle to the specified type of handle. */
> +/* Cast a guest handle (either XEN_GUEST_HANDLE or XEN_GUEST_HANDLE_PARAM)
> + * to the specified type of XEN_GUEST_HANDLE_PARAM. */
> #define guest_handle_cast(hnd, type) ({ \
> type *_x = (hnd).p; \
> - (XEN_GUEST_HANDLE(type)) { _x }; \
> + (XEN_GUEST_HANDLE_PARAM(type)) { _x }; \
[...]
> #define guest_handle_from_ptr(ptr, type) \
> - ((XEN_GUEST_HANDLE(type)) { (type *)ptr })
> + ((XEN_GUEST_HANDLE_PARAM(type)) { (type *)ptr })
> #define const_guest_handle_from_ptr(ptr, type) \
> - ((XEN_GUEST_HANDLE(const_##type)) { (const type *)ptr })
> + ((XEN_GUEST_HANDLE_PARAM(const_##type)) { (const type *)ptr })

These little bits cause build breakage if you only apply to this point
in the series (i.e. breaks bisectability):
grant_table.c: In function ‘do_grant_table_op’:
grant_table.c:2449:13: error: invalid initializer
grant_table.c:2456:17: error: incompatible types when assigning to type ‘__guest_handle_64_void’ from type ‘__guest_handle_void’
[lots more of the same]

I think this is because you have changed guest_handle_cast but you
haven't yet changed the type of the parameter.

I haven't tried x86 but I can't see why the same problem wouldn't exist
there also.

The obvious answer is to move these hunks into the next patch, but I
think because of the split into patches 5/6 and 6/6 this will still
cause problems. I'll try it though but I suspect we might end up having
to squash 5+6 together?

Alternatively I've broken something during rebasing and this used to
work fine and you know what to do ;-)...

Ian.


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel
Re: [PATCH v4 4/6] xen: introduce XEN_GUEST_HANDLE_PARAM [ In reply to ]
On Fri, 5 Oct 2012, Ian Campbell wrote:
> On Wed, 2012-08-22 at 12:08 +0100, Stefano Stabellini wrote:
> > diff --git a/xen/include/asm-arm/guest_access.h b/xen/include/asm-arm/guest_access.h
> > index 0fceae6..5686217 100644
> > --- a/xen/include/asm-arm/guest_access.h
> > +++ b/xen/include/asm-arm/guest_access.h
> > @@ -27,16 +27,40 @@ unsigned long raw_clear_guest(void *to, unsigned len);
> > #define guest_handle_add_offset(hnd, nr) ((hnd).p += (nr))
> > #define guest_handle_subtract_offset(hnd, nr) ((hnd).p -= (nr))
> >
> > -/* Cast a guest handle to the specified type of handle. */
> > +/* Cast a guest handle (either XEN_GUEST_HANDLE or XEN_GUEST_HANDLE_PARAM)
> > + * to the specified type of XEN_GUEST_HANDLE_PARAM. */
> > #define guest_handle_cast(hnd, type) ({ \
> > type *_x = (hnd).p; \
> > - (XEN_GUEST_HANDLE(type)) { _x }; \
> > + (XEN_GUEST_HANDLE_PARAM(type)) { _x }; \
> [...]
> > #define guest_handle_from_ptr(ptr, type) \
> > - ((XEN_GUEST_HANDLE(type)) { (type *)ptr })
> > + ((XEN_GUEST_HANDLE_PARAM(type)) { (type *)ptr })
> > #define const_guest_handle_from_ptr(ptr, type) \
> > - ((XEN_GUEST_HANDLE(const_##type)) { (const type *)ptr })
> > + ((XEN_GUEST_HANDLE_PARAM(const_##type)) { (const type *)ptr })
>
> These little bits cause build breakage if you only apply to this point
> in the series (i.e. breaks bisectability):
> grant_table.c: In function ‘do_grant_table_op’:
> grant_table.c:2449:13: error: invalid initializer
> grant_table.c:2456:17: error: incompatible types when assigning to type ‘__guest_handle_64_void’ from type ‘__guest_handle_void’
> [lots more of the same]
>
> I think this is because you have changed guest_handle_cast but you
> haven't yet changed the type of the parameter.
>
> I haven't tried x86 but I can't see why the same problem wouldn't exist
> there also.
>
> The obvious answer is to move these hunks into the next patch, but I
> think because of the split into patches 5/6 and 6/6 this will still
> cause problems. I'll try it though but I suspect we might end up having
> to squash 5+6 together?

You are right, I realize now that the series is not bisectable :(
It makes sense to squash 4, 5 and 6 together, the only reason why I kept
them separate is for readability.