Skip to contentSkip to conversation

FYI: userland <-> hypervisor parameter passing

PATCH Hollis Blanchard 2 participants 4 messages Nov 16, 2005 – Nov 18, 2005

lore
Share & tools
lore
#0 lore raw

This is the changeset I just committed to the PowerPC tree to pass parameters between userland and Xen on PPC. If you'll recall, the problem is that the userland tools (libxc) pass virtual addresses all the way down to the hypervisor. These pointers are worthless when userspace and the hypervisor run in separate address spaces.

The kernel could easily perform the virtual->machine address translation using the pfn2mfn arrays, except that these data structures contain virtual pointers to still *other* structures. It is not feasible for the kernel to understand and munge all the data structures passed in to perform this translation, then translate back on the return path.

Another possibility I have not investigated would be to replace the nested pointers with nested structures. This would be a more invasive change, and currently we're trying to get by on PPC without any/many architecture-neutral changes.

From previous conversations, I understand that the x86-64 hypervisor runs in a separate virtual address space. However, due to the layout of the x86 page tables, it's relatively straightforward (though not ideal) to walk the page tables and perform the translation in software. The approach taken in this patch may speed up the translation currently done on x86-64, or it may not.

On PowerPC, Xen runs in real mode (untranslated mode), which is also a separate address space. However, PowerPC's page tables are not easily walkable in software, so that option is not available to us.

Instead, this patch has userspace allocate all structures from the same page, with some translation information stuffed at the base of the page. Userland records the virtual base of the page, and the kernel records the physical base. (We have not yet implemented the pfn2mfn macros, but the hypervisor can easily convert physical->machine addresses.) The Xen copy_to/from_user() functions operate on virtual addresses, masking with PAGE_SIZE to find the needed translation information. This translation is currently only done for dom0_op and memory_op hcalls, which are the offenders I've run into so far.

Multi-page data structures are not yet supported. I think we will need to allocate the needed pages in userspace, then have kernel and hypervisor cooperation to rearrange them to be machine-contiguous. I'm not really looking forward to that.

The x86 implementation should be much simpler than PowerPC. xencomm_alloc() would be malloc+mlock, and xencomm_free() would be munlock. I have not yet implemented this; if there is interest in this approach I may.

[Side note: "domctrl" is a custom domain builder we're using because libxc needs some portability love that nobody seems interested in at the moment (probably justifiably so). domctrl directly shares the bottom parts of libxc anyways, which is why you see libxc patches in this changeset.]

PowerPC Xen tree: http://xenbits.xensource.com/ext/xenppc-unstable.hg
PowerPC Xen Linux tree: http://xenbits.xensource.com/ext/linux-ppc-2.6.hg

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center

# HG changeset patch
# User [email protected]
# Node ID 7bdf08c84776c0d0aae262bdc09e48b55addac7f
# Parent b358c816a0ca672c1802e4dc4ee4577fb89aca78
Add "xencomm" shared memory area for parameter passing between userspace and hypervisor. Update various comments and whitespace along the way. tools/libxc/xc_domain.c: convert a few hcalls used for domU creation to use new xencomm_alloc()
tools/libxc/xc_private.h: remove mlock/munlock from do_dom0_op
xen/arch/ppc/hcalls.c: when dom0_op or memory_op hcalls are made, store the xencomm area into 'current' for use by copy_to/from_user() xen/arch/ppc/usercopy.c: adapt copy_to/from_user() to expect virtual addresses in the xencomm area (rather than physical addresses the kernel translated for us) xen/arch/ppc/usercopy.c: remove lots of unused code. Change access_ok() prototype to match x86 access_ok().

tools/domctrl/Makefile +1 -0
diff -r b358c816a0ca -r 7bdf08c84776 tools/domctrl/Makefile
@@ -17,6 +17,7 @@
 	-static
 
 OBJS := \
+	xencomm_ppc.o \
 	xc_domain.o \
 	xc_bvtsched.o \
 	xc_misc.o \
tools/domctrl/create.c +4 -4
diff -r b358c816a0ca -r 7bdf08c84776 tools/domctrl/create.c
@@ -97,8 +97,8 @@
 
 static int init_domain(int domid, const char *kernel_path)
 {
-    struct load_funcs load_funcs;
-    struct domain_setup_info dsi;
+	struct load_funcs load_funcs;
+	struct domain_setup_info dsi;
 	unsigned long *page_array = NULL;
 	uint8_t *kernel_img = NULL;
 	unsigned long kernel_size;
@@ -106,9 +106,9 @@
 	int rc;
 	int i;
 
-	DEBUG(printf("xc_get_tot_pages: "));
+	DEBUG(printf("xc_get_tot_pages\n"));
 	nr_pages = xc_get_tot_pages(xc_handle, domid);
-	DEBUG(printf("0x%x\n", nr_pages));
+	DEBUG(printf("  0x%x\n", nr_pages));
 
 	page_array = malloc(nr_pages * sizeof(unsigned long));
 	if (page_array == NULL) {
tools/domctrl/list.c +1 -0
diff -r b358c816a0ca -r 7bdf08c84776 tools/domctrl/list.c
@@ -17,6 +17,7 @@
 	int rc;
 	int i;
 
+	DEBUG(printf("xc_domain_getinfo\n"));
 	rc = xc_domain_getinfo(xc_handle, 0, max_nr_doms, dominfo);
 	if (rc == -1) {
 		perror("xc_domain_getinfo");
tools/libxc/Makefile +2 -0
diff -r b358c816a0ca -r 7bdf08c84776 tools/libxc/Makefile
@@ -23,9 +23,11 @@
 SRCS       += xc_physdev.c
 SRCS       += xc_private.c
 SRCS       += xc_sedf.c
+SRCS       += xencomm_ppc.c
 BUILD_SRCS += xc_linux_build.c
 BUILD_SRCS += xc_load_bin.c
 BUILD_SRCS += xc_load_elf.c
+BUILD_SRCS += xencomm_ppc.c
 
 ifeq ($(XEN_TARGET_ARCH),ia64)
 BUILD_SRCS += xc_ia64_stubs.c
tools/libxc/xc_domain.c +52 -42

146 lines not on this page · raw message

tools/libxc/xc_private.c +15 -5
diff -r b358c816a0ca -r 7bdf08c84776 tools/libxc/xc_private.c
@@ -5,6 +5,7 @@
  */
 
 #include "xc_private.h"
+#include "xencomm.h"
 #include <xen/memory.h>
 
 void *xc_map_foreign_batch(int xc_handle, uint32_t dom, int prot,
@@ -363,11 +364,20 @@
 
 long xc_get_tot_pages(int xc_handle, uint32_t domid)
 {
-    dom0_op_t op;
-    op.cmd = DOM0_GETDOMAININFO;
-    op.u.getdomaininfo.domain = (domid_t)domid;
-    return (do_dom0_op(xc_handle, &op) < 0) ? 
-        -1 : op.u.getdomaininfo.tot_pages;
+    dom0_op_t *op = xencomm_alloc(sizeof(dom0_op_t));
+    long ret;
+
+    op->cmd = DOM0_GETDOMAININFO;
+    op->u.getdomaininfo.domain = (domid_t)domid;
+
+    if (do_dom0_op(xc_handle, op) < 0)
+        ret = -1;
+    else
+        ret = op->u.getdomaininfo.tot_pages;
+
+    xencomm_free(op);
+
+    return ret;
 }
 
 int xc_copy_to_domain_page(int xc_handle,
tools/libxc/xc_private.h +0 -9
diff -r b358c816a0ca -r 7bdf08c84776 tools/libxc/xc_private.h
@@ -80,12 +80,6 @@
     hypercall.op     = __HYPERVISOR_dom0_op;
     hypercall.arg[0] = (unsigned long)op;
 
-    if ( mlock(op, sizeof(*op)) != 0 )
-    {
-        PERROR("Could not lock memory for Xen hypercall");
-        goto out1;
-    }
-
     if ( (ret = do_xen_hypercall(xc_handle, &hypercall)) < 0 )
     {
         if ( errno == EACCES )
@@ -93,9 +87,6 @@
                     " rebuild the user-space tool set?\n");
     }
 
-    safe_munlock(op, sizeof(*op));
-
- out1:
     return ret;
 }
 
xen/arch/ppc/hcalls.c +16 -4
diff -r b358c816a0ca -r 7bdf08c84776 xen/arch/ppc/hcalls.c
@@ -80,11 +80,23 @@
     ulong num = regs->gprs[3];
 
     if ((num & XEN_MARK(0)) == XEN_MARK(0)) {
-        /* its a Xen call */
+        /* it's a Xen call */
+        int rc;
+
         num &= ~XEN_MARK(0);
+
+        rc = set_xlate_info(num, regs);
+        if (rc < 0) {
+            /* bad xencomm area */
+            printf("bad xencomm area\n");
+            regs->gprs[3] = rc;
+            return;
+        }
+
         hcall_xen(num, regs);
+        unset_xlate_info();
     } else {
-        /* its a PAPR call */
+        /* it's a PAPR call */
         hcall_papr(num, regs);
     }
 }
@@ -101,13 +113,13 @@
 }
 
 /* store low 32 bits of 64-bit address in hcall table (this is safe because we
- * know we will not link above 4GB). */
+ * know we will not link above 4GB). We don't need to preserve the TOC
+ * because that only changes when calling dynamically linked objects. */
 static void register_papr_hcall(ulong num, hcall_handler_t handler)
 {
     int index = num/4;
 
     papr_hcalls[index] = (u32)(*(u64 *)handler);
-    /* XXX remind me why we don't need to worry about TOC? */
 }
 
 static void init_papr_hcalls(void)
xen/arch/ppc/usercopy.c +92 -26

156 lines not on this page · raw message

xen/include/asm-ppc/domain.h +3 -0
diff -r b358c816a0ca -r 7bdf08c84776 xen/include/asm-ppc/domain.h
@@ -69,6 +69,8 @@
 	ulong slb_esid;
 };
 
+struct xencomm;
+
 struct arch_vcpu {
     cpu_user_regs_t ctxt; /* User-level CPU registers */
 
@@ -95,6 +97,7 @@
 
     u32 dec;
     struct cpu_vcpu cpu; /* CPU-specific bits */
+    struct xencomm *xencomm;
 
     int switched;
 } __cacheline_aligned;
xen/include/asm-ppc/uaccess.h +16 -81

118 lines not on this page · raw message

tools/libxc/xencomm.h new file +28 -0
diff -r b358c816a0ca -r 7bdf08c84776 tools/libxc/xencomm.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2005 Hollis Blanchard <[email protected]>, IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *
+ */
+
+/* API to allocate memory for hypervisor communication */
+
+#ifndef XENCOMM_H
+#define XENCOMM_H
+
+extern void *xencomm_alloc(size_t bytes);
+extern void xencomm_free(void *address);
+
+#endif
tools/libxc/xencomm_ppc.c new file +91 -0

91 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 8 lines · Hollis Blanchard

Instead, this patch has userspace allocate all structures from the same page, with some translation information stuffed at the base of the page. Userland records the virtual base of the page, and the kernel records the physical base. (We have not yet implemented the pfn2mfn macros, but the hypervisor can easily convert physical->machine addresses.) The Xen copy_to/from_user() functions operate on virtual addresses, masking with PAGE_SIZE to find the needed translation information. This translation is currently only done for dom0_op and memory_op hcalls, which are the offenders I've run into so far.

I think it'd be cleaner to have a completely separate 'xencomm' communications address space. Then xencomm_alloc() would return two pointers:
1. Local virtual address that caller can use to read/write the
allocated block.
2. xencomm pointer that gets passed to Xen during hypercall

Your new_xencomm() allocates a new page of memory, then passes the MFN to Xen as part of a new 'allocate xencomm addr space' hypercall. Xen returns the allocated xencomm address for that MFN (or batch of MFNs), thus establishing the xencomm<->machine mapping in both Xen and in the guest.

copy_to_user/copy_from_user and friends all expect the user pointer to be in xencomm address space, and will do a xencomm->xen-virtual-address translation (the xen-virtual-address mapping is created during the establish hypercall described above).

The above is what I intend to implement for x86 after 3.0 (it's easy to maintain backward compatibility), so as long as any proposed generic interface allows the above implementation then I'm happy.

-- Keir

#2 lore raw
Quoted 8 lines · Keir Fraser

I think it'd be cleaner to have a completely separate 'xencomm' communications address space. Then xencomm_alloc() would return two pointers:
1. Local virtual address that caller can use to read/write the
allocated block.
2. xencomm pointer that gets passed to Xen during hypercall

Quoted 5 lines

Your new_xencomm() allocates a new page of memory, then passes the MFN to Xen as part of a new 'allocate xencomm addr space' hypercall. Xen returns the allocated xencomm address for that MFN (or batch of MFNs), thus establishing the xencomm<->machine mapping in both Xen and in the guest.

Ok, so all the nested pointers will be in the machine address space. I think that will require some rework of the layering in xc_private.c, since functions like xc_add_mmu_update() dereference the pointers they're passed.

Quoted 8 lines

copy_to_user/copy_from_user and friends all expect the user pointer to be in xencomm address space, and will do a xencomm->xen-virtual-address translation (the xen-virtual-address mapping is created during the establish hypercall described above).

The above is what I intend to implement for x86 after 3.0 (it's easy to maintain backward compatibility), so as long as any proposed generic interface allows the above implementation then I'm happy.

Perfectly reasonable. If it didn't require so much out-of-tree libxc hacking, I would volunteer to implement this design right now.

So I think I should leave the PPC implementation as-is for now, but I will be looking forward to post-3.0 (as I suppose we all will, for various reasons ;) .

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#3 lore raw
Quoted 3 lines · Hollis Blanchard

Ok, so all the nested pointers will be in the machine address space. I think that will require some rework of the layering in xc_private.c, since functions like xc_add_mmu_update() dereference the pointers they're passed.

Yes, that's true. Hopefully we can remove that, or worst case will need a xencomm->virtual reverse translation function....

Quoted 2 lines

Perfectly reasonable. If it didn't require so much out-of-tree libxc hacking, I would volunteer to implement this design right now.

Yeah, it's going to cause some short-term pain.

Quoted 3 lines

So I think I should leave the PPC implementation as-is for now, but I will be looking forward to post-3.0 (as I suppose we all will, for various reasons ;) .

:-)

-- Keir