[PATCH v7 00/19] FreeBSD PVH DomU support

2013-12-19 Thread Roger Pau Monne
This series is a split of the previous patch Xen x86 DomU PVH 
support, with the aim to make the review of the code easier.

The series can also be found on my git repo:

git://xenbits.xen.org/people/royger/freebsd.git pvh_v7

or

http://xenbits.xen.org/gitweb/?p=people/royger/freebsd.git;a=shortlog;h=refs/heads/pvh_v7

PVH mode is basically a PV guest inside an HVM container, and shares
a great amount of code with PVHVM. The main difference is the way the
guest is started, PVH uses the PV start sequence, jumping directly
into the kernel entry point in long mode and with page tables set.
The main work of this patch consists in setting the environment as
similar as possible to what native FreeBSD expects, and then adding
hooks to the PV ops when necessary.

sys/amd64/amd64/locore.S:
 * Add PV entry point, hypervisor_page and the necessary elfnotes.

sys/amd64/amd64/machdep.c:
 * Add hooks to replace bare metal operations that should use a PV
  helper, this includes:
   - Preload metadata
   - i8254_init and i8254_delay
   - Fetching the e820 memory map
   - Reserve of the MP bootstrap region

 * Create a DELAY function that uses the PV hooks.
 * Introduce a new hammer_time_xen that sets the necessary stuff when
   running in PVH mode.

sys/amd64/amd64/mp_machdep.c:
 * Introduce a hook to replace start_all_aps.
 * Use lapic_disabled variable to prevent polluting the code
   with xen specific gates.

sys/amd64/include/asmacros.h:
 * Copy the ELFNOTE macro from the i386 Xen PV port.

sys/amd64/include/clock.h:
sys/i386/include/clock.h:
 * Prototypes for the xen early delay initialization and usage.

sys/amd64/include/cpu.h:
 * Introduce a new cpu hook to init APs.

sys/amd64/include/sysarch.h:
 * Declare the init_ops structure.

sys/amd64/include/xen/hypercall.h:
sys/i386/include/xen/hypercall.h
 * Switch to the PV style hypercall mechanism for HVM also.

sys/conf/files:
 * Make the PV console available on XENHVM also.

sys/conf/files.amd64:
 * Include the new files for the PVH port.

sys/dev/xen/console/console.c:
sys/dev/xen/console/xencons_ring.c:
 * Remove the identify method and instead add the device from
   nexus_xen.
 * Use HYPERVISOR_start_info instead of xen_start_info.
 * Use HYPERVISOR_event_channel_op to kick the event channel before
   xen interrupts are setup.
 * Copy the xc_printf debug function from xen_machdep.c

sys/dev/xen/control/control.c:
 * Use the PV shutdown on PVH.

sys/dev/xen/timer/timer.c:
 * Pass a vcpu_info to xen_fetch_vcpu_time, this allows using this
   function at very early init, before per-cpu vcpu_info is set.
 * Remove critical_{enter/exit} from xen_fetch_vcpu_time so it can be
   used at early boot, instead place them on the callers.
 * Introduce two new functions, xen_delay_init and xen_delay that can
   be used at early boot to implement the generic DELAY function.
 * Remove the identify method that used to add the device, now it is
   manually added from either xenpci (HVM) or nexus_xen (PV).

sys/i386/i386/locore.s:
 * Reserve space for the hypercall page.

sys/i386/i386/machdep.c:
 * Create a generic DELAY function.

sys/i386/xen/xen_machdep.c:
 * Set HYPERVISOR_start_info.
 * Move and rename xc_printf debug function to xen console.c

sys/x86/isa/clock.c:
 * Rename the generic DELAY function to i8254_delay.

sys/x86/x86/delay.c:
 * Put generic delay helpers here, get_tsc and delay_tc.

sys/x86/x86/local_apic.c:
 * Prevent the local apic from attaching when running on PVH mode.

sys/x86/xen/hvm.c:
 * Set the start_all_aps hook.
 * Fix the setting of the hypercall page now that we are using the
   same mechanism as the PV port.
 * Initialize Xen CPU hooks for the PVH port.
 * Initialize APs before SI_SUB_SMP (SI_SUB_SMP-1).

sys/x86/xen/mptable.c:
 * Create a dummy PV CPU enumerator for the PVH port.

sys/x86/xen/pv.c:
 * Implement the PV functions for the early boot hooks,
   parse_preload_data and fetch_e820_map.
 * Implement the PV function for the start_all_aps hook.

sys/x86/xen/pvcpu.c:
 * Dummy Xen PV CPU device, that we use to set the per-cpu pc_device.

sys/xen/gnttab.c:
 * Allocate resume_frames for the PVH port.

sys/xen/pv.h:
 * Header that exports the specific PV functions.

sys/xen/xen-os.h:
 * Declare prototypes for the newly added functions.
 * Include new xen_initial_domain function.

sys/xen/xenstore/xenstore.c:
 * Make the xenstore driver hang from both xenpci and the nexus when
   running XENHVM, this is because we don't have a xenpci device on
   the PVH port.
 * Remove the identify routine that added the device, instead add it
   from either xenpci (HVM) or nexus_xen (PV).

sys/dev/xen/xenpci/xenpci.c:
 * Add the xenstore and xen_et devices on succesful attach.

sys/x86/xen/xen_nexus.c:
 * Create a specific nexus for Xen PV guests that takes care of adding
   the top level Xen PV devices.

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, 

[PATCH v7 07/19] xen: implement hook to fetch e820 memory map

2013-12-19 Thread Roger Pau Monne
---
 sys/amd64/amd64/machdep.c   |   50 ++
 sys/amd64/include/pc/bios.h |2 +
 sys/amd64/include/sysarch.h |1 +
 sys/x86/xen/pv.c|   26 ++
 4 files changed, 60 insertions(+), 19 deletions(-)

diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c
index a2dcb90..6bbfe5a 100644
--- a/sys/amd64/amd64/machdep.c
+++ b/sys/amd64/amd64/machdep.c
@@ -177,11 +177,15 @@ SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, 
NULL);
 /* Preload data parse function */
 static caddr_t native_parse_preload_data(u_int64_t);
 
+/* Native function to fetch and parse the e820 map */
+static void native_parse_memmap(caddr_t, vm_paddr_t *, int *);
+
 /* Default init_ops implementation. */
 struct init_ops init_ops = {
.parse_preload_data =   native_parse_preload_data,
.early_delay_init = i8254_init,
.early_delay =  i8254_delay,
+   .parse_memmap = native_parse_memmap,
 };
 
 /*
@@ -1418,21 +1422,12 @@ add_physmap_entry(uint64_t base, uint64_t length, 
vm_paddr_t *physmap,
return (1);
 }
 
-static void
-add_smap_entries(struct bios_smap *smapbase, vm_paddr_t *physmap,
-int *physmap_idx)
+void
+bios_add_smap_entries(struct bios_smap *smapbase, u_int32_t smapsize,
+  vm_paddr_t *physmap, int *physmap_idx)
 {
struct bios_smap *smap, *smapend;
-   u_int32_t smapsize;
 
-   /*
-* Memory map from INT 15:E820.
-*
-* subr_module.c says:
-* Consumer may safely assume that size value precedes data.
-* ie: an int32_t immediately precedes smap.
-*/
-   smapsize = *((u_int32_t *)smapbase - 1);
smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
 
for (smap = smapbase; smap  smapend; smap++) {
@@ -1449,6 +1444,29 @@ add_smap_entries(struct bios_smap *smapbase, vm_paddr_t 
*physmap,
}
 }
 
+static void
+native_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
+{
+   struct bios_smap *smap;
+   u_int32_t size;
+
+   /*
+* Memory map from INT 15:E820.
+*
+* subr_module.c says:
+* Consumer may safely assume that size value precedes data.
+* ie: an int32_t immediately precedes smap.
+*/
+
+   smap = (struct bios_smap *)preload_search_info(kmdp,
+   MODINFO_METADATA | MODINFOMD_SMAP);
+   if (smap == NULL)
+   panic(No BIOS smap info from loader!);
+   size = *((u_int32_t *)smap - 1);
+
+   bios_add_smap_entries(smap, size, physmap, physmap_idx);
+}
+
 /*
  * Populate the (physmap) array with base/bound pairs describing the
  * available physical memory in the system, then test this memory and
@@ -1466,19 +1484,13 @@ getmemsize(caddr_t kmdp, u_int64_t first)
vm_paddr_t pa, physmap[PHYSMAP_SIZE];
u_long physmem_start, physmem_tunable, memtest;
pt_entry_t *pte;
-   struct bios_smap *smapbase;
quad_t dcons_addr, dcons_size;
 
bzero(physmap, sizeof(physmap));
basemem = 0;
physmap_idx = 0;
 
-   smapbase = (struct bios_smap *)preload_search_info(kmdp,
-   MODINFO_METADATA | MODINFOMD_SMAP);
-   if (smapbase == NULL)
-   panic(No BIOS smap info from loader!);
-
-   add_smap_entries(smapbase, physmap, physmap_idx);
+   init_ops.parse_memmap(kmdp, physmap, physmap_idx);
 
/*
 * Find the 'base memory' segment for SMP
diff --git a/sys/amd64/include/pc/bios.h b/sys/amd64/include/pc/bios.h
index e7d568e..92d4265 100644
--- a/sys/amd64/include/pc/bios.h
+++ b/sys/amd64/include/pc/bios.h
@@ -106,6 +106,8 @@ struct bios_oem {
 intbios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen);
 uint32_t bios_sigsearch(uint32_t start, u_char *sig, int siglen, int paralen,
int sigofs);
+void bios_add_smap_entries(struct bios_smap *smapbase, u_int32_t smapsize,
+   vm_paddr_t *physmap, int *physmap_idx);
 #endif
 
 #endif /* _MACHINE_PC_BIOS_H_ */
diff --git a/sys/amd64/include/sysarch.h b/sys/amd64/include/sysarch.h
index 60fa635..084223e 100644
--- a/sys/amd64/include/sysarch.h
+++ b/sys/amd64/include/sysarch.h
@@ -15,6 +15,7 @@ struct init_ops {
caddr_t (*parse_preload_data)(u_int64_t);
void(*early_delay_init)(void);
void(*early_delay)(int);
+   void(*parse_memmap)(caddr_t, vm_paddr_t *, int *);
 };
 
 extern struct init_ops init_ops;
diff --git a/sys/x86/xen/pv.c b/sys/x86/xen/pv.c
index f8d8cfa..db576e0 100644
--- a/sys/x86/xen/pv.c
+++ b/sys/x86/xen/pv.c
@@ -37,12 +37,17 @@ __FBSDID($FreeBSD$);
 
 #include machine/sysarch.h
 #include machine/clock.h
+#include machine/pc/bios.h
 
 #include xen/xen-os.h
 #include xen/pv.h
+#include xen/hypervisor.h
+
+#define MAX_E820_ENTRIES   128
 
 /*--- Forward Declarations 
---*/
 static caddr_t 

[PATCH v7 08/19] xen: use the same hypercall mechanism for XEN and XENHVM

2013-12-19 Thread Roger Pau Monne
---
 sys/amd64/include/xen/hypercall.h |7 ---
 sys/i386/i386/locore.s|9 +
 sys/i386/include/xen/hypercall.h  |8 
 sys/x86/xen/hvm.c |   24 ++--
 4 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/sys/amd64/include/xen/hypercall.h 
b/sys/amd64/include/xen/hypercall.h
index a1b2a5c..499fb4d 100644
--- a/sys/amd64/include/xen/hypercall.h
+++ b/sys/amd64/include/xen/hypercall.h
@@ -51,15 +51,8 @@
 #define CONFIG_XEN_COMPAT  0x030002
 #define __must_check
 
-#ifdef XEN
 #define HYPERCALL_STR(name)\
call hypercall_page + (STR(__HYPERVISOR_##name) * 32)
-#else
-#define HYPERCALL_STR(name)\
-   mov $(STR(__HYPERVISOR_##name) * 32),%%eax; \
-   add hypercall_stubs(%%rip),%%rax; \
-   call *%%rax
-#endif
 
 #define _hypercall0(type, name)\
 ({ \
diff --git a/sys/i386/i386/locore.s b/sys/i386/i386/locore.s
index 68cb430..bd136b1 100644
--- a/sys/i386/i386/locore.s
+++ b/sys/i386/i386/locore.s
@@ -898,3 +898,12 @@ done_pde:
 #endif
 
ret
+
+#ifdef XENHVM
+/* Xen Hypercall page */
+   .text
+.p2align PAGE_SHIFT, 0x90  /* Hypercall_page needs to be PAGE aligned */
+
+NON_GPROF_ENTRY(hypercall_page)
+   .skip   0x1000, 0x90/* Fill with nops */
+#endif
diff --git a/sys/i386/include/xen/hypercall.h b/sys/i386/include/xen/hypercall.h
index edc13f4..16b5ee2 100644
--- a/sys/i386/include/xen/hypercall.h
+++ b/sys/i386/include/xen/hypercall.h
@@ -39,16 +39,8 @@
 #defineENOXENSYS   38
 #define CONFIG_XEN_COMPAT  0x030002
 
-
-#if defined(XEN)
 #define HYPERCALL_STR(name) \
 call hypercall_page + (STR(__HYPERVISOR_##name) * 32)
-#else
-#define HYPERCALL_STR(name) \
-mov hypercall_stubs,%%eax;\
-add $(STR(__HYPERVISOR_##name) * 32),%%eax; \
-call *%%eax
-#endif
 
 #define _hypercall0(type, name) \
 ({  \
diff --git a/sys/x86/xen/hvm.c b/sys/x86/xen/hvm.c
index b397721..9a0411e 100644
--- a/sys/x86/xen/hvm.c
+++ b/sys/x86/xen/hvm.c
@@ -157,7 +157,7 @@ DPCPU_DEFINE(xen_intr_handle_t, 
ipi_handle[nitems(xen_ipis)]);
 
 /*-- Hypervisor Access Shared Memory Regions 
-*/
 /** Hypercall table accessed via HYPERVISOR_*_op() methods. */
-char *hypercall_stubs;
+extern char *hypercall_page;
 shared_info_t *HYPERVISOR_shared_info;
 start_info_t *HYPERVISOR_start_info;
 
@@ -559,7 +559,7 @@ xen_hvm_cpuid_base(void)
  * Allocate and fill in the hypcall page.
  */
 static int
-xen_hvm_init_hypercall_stubs(void)
+xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
 {
uint32_t base, regs[4];
int i;
@@ -568,7 +568,7 @@ xen_hvm_init_hypercall_stubs(void)
if (base == 0)
return (ENXIO);
 
-   if (hypercall_stubs == NULL) {
+   if (init_type == XEN_HVM_INIT_COLD) {
do_cpuid(base + 1, regs);
printf(XEN: Hypervisor version %d.%d detected.\n,
regs[0]  16, regs[0]  0x);
@@ -578,18 +578,9 @@ xen_hvm_init_hypercall_stubs(void)
 * Find the hypercall pages.
 */
do_cpuid(base + 2, regs);
-   
-   if (hypercall_stubs == NULL) {
-   size_t call_region_size;
-
-   call_region_size = regs[0] * PAGE_SIZE;
-   hypercall_stubs = malloc(call_region_size, M_XENHVM, M_NOWAIT);
-   if (hypercall_stubs == NULL)
-   panic(Unable to allocate Xen hypercall region);
-   }
 
for (i = 0; i  regs[0]; i++)
-   wrmsr(regs[1], vtophys(hypercall_stubs + i * PAGE_SIZE) + i);
+   wrmsr(regs[1], vtophys(hypercall_page + i * PAGE_SIZE) + i);
 
return (0);
 }
@@ -692,7 +683,12 @@ xen_hvm_init(enum xen_hvm_init_type init_type)
if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
return;
 
-   error = xen_hvm_init_hypercall_stubs();
+   if (xen_pv_domain()) {
+   /* hypercall page is already set in the PV case */
+   error = 0;
+   } else {
+   error = xen_hvm_init_hypercall_stubs(init_type);
+   }
 
switch (init_type) {
case XEN_HVM_INIT_COLD:
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org


[PATCH v7 09/19] xen: add a apic_enumerator for PVH

2013-12-19 Thread Roger Pau Monne
---
 sys/conf/files.amd64  |1 +
 sys/x86/xen/mptable.c |  136 +
 2 files changed, 137 insertions(+), 0 deletions(-)
 create mode 100644 sys/x86/xen/mptable.c

diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64
index bdc1517..3bdc05e 100644
--- a/sys/conf/files.amd64
+++ b/sys/conf/files.amd64
@@ -568,3 +568,4 @@ x86/x86/delay.c standard
 x86/xen/hvm.c  optionalxenhvm
 x86/xen/xen_intr.c optionalxen | xenhvm
 x86/xen/pv.c   optionalxenhvm
+x86/xen/mptable.c  optionalxenhvm
diff --git a/sys/x86/xen/mptable.c b/sys/x86/xen/mptable.c
new file mode 100644
index 000..0384886
--- /dev/null
+++ b/sys/x86/xen/mptable.c
@@ -0,0 +1,136 @@
+/*-
+ * Copyright (c) 2003 John Baldwin j...@freebsd.org
+ * Copyright (c) 2013 Roger Pau Monné roger@citrix.com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the names of any co-contributors
+ *may be used to endorse or promote products derived from this software
+ *without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include sys/cdefs.h
+__FBSDID($FreeBSD$);
+
+#include sys/param.h
+#include sys/systm.h
+#include sys/bus.h
+#include sys/kernel.h
+#include sys/smp.h
+#include sys/pcpu.h
+#include vm/vm.h
+#include vm/pmap.h
+
+#include machine/intr_machdep.h
+#include machine/apicvar.h
+
+#include machine/cpu.h
+#include machine/smp.h
+
+#include xen/xen-os.h
+#include xen/hypervisor.h
+
+#include xen/interface/vcpu.h
+
+static int xenpv_probe(void);
+static int xenpv_probe_cpus(void);
+static int xenpv_setup_local(void);
+static int xenpv_setup_io(void);
+
+static struct apic_enumerator xenpv_enumerator = {
+   Xen PV,
+   xenpv_probe,
+   xenpv_probe_cpus,
+   xenpv_setup_local,
+   xenpv_setup_io
+};
+
+/*
+ * This enumerator will only be registered on PVH
+ */
+static int
+xenpv_probe(void)
+{
+   return (-100);
+}
+
+/*
+ * Test each possible vCPU in order to find the number of vCPUs
+ */
+static int
+xenpv_probe_cpus(void)
+{
+#ifdef SMP
+   int i, ret;
+
+   for (i = 0; i  MAXCPU; i++) {
+   ret = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
+   if (ret = 0)
+   cpu_add((i * 2), (i == 0));
+   }
+#endif
+   return (0);
+}
+
+/*
+ * Initialize the vCPU id of the BSP
+ */
+static int
+xenpv_setup_local(void)
+{
+   PCPU_SET(vcpu_id, 0);
+   return (0);
+}
+
+/*
+ * On PVH guests there's no IO APIC
+ */
+static int
+xenpv_setup_io(void)
+{
+   return (0);
+}
+
+static void
+xenpv_register(void *dummy __unused)
+{
+   if (xen_pv_domain()) {
+   apic_register_enumerator(xenpv_enumerator);
+   }
+}
+SYSINIT(xenpv_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, xenpv_register, 
NULL);
+
+/*
+ * Setup per-CPU vCPU IDs
+ */
+static void
+xenpv_set_ids(void *dummy)
+{
+   struct pcpu *pc;
+   int i;
+
+   CPU_FOREACH(i) {
+   pc = pcpu_find(i);
+   pc-pc_vcpu_id = i;
+   }
+}
+SYSINIT(xenpv_set_ids, SI_SUB_CPU, SI_ORDER_MIDDLE, xenpv_set_ids, NULL);
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org

[PATCH v7 17/19] xen: xenstore changes to support PVH

2013-12-19 Thread Roger Pau Monne
---
 sys/xen/xenstore/xenstore.c |   21 ++---
 1 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/sys/xen/xenstore/xenstore.c b/sys/xen/xenstore/xenstore.c
index bcf6357..2893c84 100644
--- a/sys/xen/xenstore/xenstore.c
+++ b/sys/xen/xenstore/xenstore.c
@@ -229,13 +229,11 @@ struct xs_softc {
 */
struct sx xenwatch_mutex;
 
-#ifdef XENHVM
/**
 * The HVM guest pseudo-physical frame number.  This is Xen's mapping
 * of the true machine frame number into our physical address space.
 */
unsigned long gpfn;
-#endif
 
/**
 * The event channel for communicating with the
@@ -1141,13 +1139,15 @@ xs_attach(device_t dev)
/* Initialize the interface to xenstore. */
struct proc *p;
 
-#ifdef XENHVM
-   xs.evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
-   xs.gpfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
-   xen_store = pmap_mapdev(xs.gpfn * PAGE_SIZE, PAGE_SIZE);
-#else
-   xs.evtchn = xen_start_info-store_evtchn;
-#endif
+   if (xen_hvm_domain()) {
+   xs.evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
+   xs.gpfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
+   xen_store = pmap_mapdev(xs.gpfn * PAGE_SIZE, PAGE_SIZE);
+   } else if (xen_pv_domain()) {
+   xs.evtchn = HYPERVISOR_start_info-store_evtchn;
+   } else {
+   panic(Unknown domain type, cannot initialize xenstore\n);
+   }
 
TAILQ_INIT(xs.reply_list);
TAILQ_INIT(xs.watch_events);
@@ -1256,9 +1256,8 @@ static devclass_t xenstore_devclass;
  
 #ifdef XENHVM
 DRIVER_MODULE(xenstore, xenpci, xenstore_driver, xenstore_devclass, 0, 0);
-#else
-DRIVER_MODULE(xenstore, nexus, xenstore_driver, xenstore_devclass, 0, 0);
 #endif
+DRIVER_MODULE(xenstore, nexus, xenstore_driver, xenstore_devclass, 0, 0);
 
 /*--- Sysctl Data 
*/
 /* XXX Shouldn't the node be somewhere else? */
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org


[PATCH v7 18/19] xen: changes to gnttab for PVH

2013-12-19 Thread Roger Pau Monne
---
 sys/xen/gnttab.c |   26 +-
 1 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/sys/xen/gnttab.c b/sys/xen/gnttab.c
index 03c32b7..6949be5 100644
--- a/sys/xen/gnttab.c
+++ b/sys/xen/gnttab.c
@@ -25,6 +25,7 @@ __FBSDID($FreeBSD$);
 #include sys/lock.h
 #include sys/malloc.h
 #include sys/mman.h
+#include sys/limits.h
 
 #include xen/xen-os.h
 #include xen/hypervisor.h
@@ -607,6 +608,7 @@ gnttab_resume(void)
 {
int error;
unsigned int max_nr_gframes, nr_gframes;
+   void *alloc_mem;
 
nr_gframes = nr_grant_frames;
max_nr_gframes = max_nr_grant_frames();
@@ -614,11 +616,25 @@ gnttab_resume(void)
return (ENOSYS);
 
if (!resume_frames) {
-   error = xenpci_alloc_space(PAGE_SIZE * max_nr_gframes,
-   resume_frames);
-   if (error) {
-   printf(error mapping gnttab share frames\n);
-   return (error);
+   if (xen_pv_domain()) {
+   /*
+* This is a waste of physical memory,
+* we should use ballooned pages instead,
+* but it will do for now.
+*/
+   alloc_mem = contigmalloc(max_nr_gframes * PAGE_SIZE,
+M_DEVBUF, M_NOWAIT, 0,
+ULONG_MAX, PAGE_SIZE, 0);
+   KASSERT((alloc_mem != NULL),
+   (unable to alloc memory for gnttab));
+   resume_frames = vtophys(alloc_mem);
+   } else {
+   error = xenpci_alloc_space(PAGE_SIZE * max_nr_gframes,
+   resume_frames);
+   if (error) {
+   printf(error mapping gnttab share frames\n);
+   return (error);
+   }
}
}
 
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org


[PATCH v7 10/19] xen: add hook for AP bootstrap memory reservation

2013-12-19 Thread Roger Pau Monne
This hook will only be implemented for bare metal, Xen doesn't require
any bootstrap code since APs are started in long mode with paging
enabled.
---
 sys/amd64/amd64/machdep.c   |6 +-
 sys/amd64/include/sysarch.h |3 +++
 2 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c
index 6bbfe5a..a811a9b 100644
--- a/sys/amd64/amd64/machdep.c
+++ b/sys/amd64/amd64/machdep.c
@@ -186,6 +186,9 @@ struct init_ops init_ops = {
.early_delay_init = i8254_init,
.early_delay =  i8254_delay,
.parse_memmap = native_parse_memmap,
+#ifdef SMP
+   .mp_bootaddress =   mp_bootaddress,
+#endif
 };
 
 /*
@@ -1507,7 +1510,8 @@ getmemsize(caddr_t kmdp, u_int64_t first)
 
 #ifdef SMP
/* make hole for AP bootstrap code */
-   physmap[1] = mp_bootaddress(physmap[1] / 1024);
+   if (init_ops.mp_bootaddress)
+   physmap[1] = init_ops.mp_bootaddress(physmap[1] / 1024);
 #endif
 
/*
diff --git a/sys/amd64/include/sysarch.h b/sys/amd64/include/sysarch.h
index 084223e..77f4b29 100644
--- a/sys/amd64/include/sysarch.h
+++ b/sys/amd64/include/sysarch.h
@@ -16,6 +16,9 @@ struct init_ops {
void(*early_delay_init)(void);
void(*early_delay)(int);
void(*parse_memmap)(caddr_t, vm_paddr_t *, int *);
+#ifdef SMP
+   u_int   (*mp_bootaddress)(u_int);
+#endif
 };
 
 extern struct init_ops init_ops;
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org


[PATCH v7 14/19] xen: introduce a dummy pvcpu device

2013-12-19 Thread Roger Pau Monne
Since Xen PVH guests doesn't have ACPI, we need to create a dummy
pvcpu device that will be used to fill the pcpu-pc_device field.
---
 sys/conf/files.amd64 |1 +
 sys/x86/xen/pvcpu.c  |   84 ++
 2 files changed, 85 insertions(+), 0 deletions(-)
 create mode 100644 sys/x86/xen/pvcpu.c

diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64
index 3bdc05e..4a3f612 100644
--- a/sys/conf/files.amd64
+++ b/sys/conf/files.amd64
@@ -569,3 +569,4 @@ x86/xen/hvm.c   optionalxenhvm
 x86/xen/xen_intr.c optionalxen | xenhvm
 x86/xen/pv.c   optionalxenhvm
 x86/xen/mptable.c  optionalxenhvm
+x86/xen/pvcpu.coptionalxenhvm
diff --git a/sys/x86/xen/pvcpu.c b/sys/x86/xen/pvcpu.c
new file mode 100644
index 000..8725e76
--- /dev/null
+++ b/sys/x86/xen/pvcpu.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2013 Roger Pau Monné roger@citrix.com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include sys/cdefs.h
+__FBSDID($FreeBSD$);
+
+#include sys/param.h
+#include sys/systm.h
+#include sys/bus.h
+#include sys/kernel.h
+#include sys/module.h
+#include sys/pcpu.h
+#include sys/smp.h
+
+#include xen/xen-os.h
+
+/*
+ * Dummy Xen cpu device
+ *
+ * Since there's no ACPI on PVH guests, we need to create a dummy
+ * CPU device in order to fill the pcpu-pc_device field.
+ */
+
+static int
+xenpvcpu_probe(device_t dev)
+{
+   if (!xen_pv_domain())
+   return (ENXIO);
+
+   device_set_desc(dev, Xen PV CPU);
+   return (0);
+}
+
+static int
+xenpvcpu_attach(device_t dev)
+{
+   struct pcpu *pc;
+   int cpu;
+
+   cpu = device_get_unit(dev);
+   pc = pcpu_find(cpu);
+   pc-pc_device = dev;
+   return (0);
+}
+
+static device_method_t xenpvcpu_methods[] = {
+   DEVMETHOD(device_probe, xenpvcpu_probe),
+   DEVMETHOD(device_attach, xenpvcpu_attach),
+   DEVMETHOD_END
+};
+
+static driver_t xenpvcpu_driver = {
+   pvcpu,
+   xenpvcpu_methods,
+   0,
+};
+
+devclass_t xenpvcpu_devclass;
+
+DRIVER_MODULE(xenpvcpu, nexus, xenpvcpu_driver, xenpvcpu_devclass, 0, 0);
+MODULE_DEPEND(xenpvcpu, nexus, 1, 1, 1);
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org

[PATCH v7 12/19] xen: add a hook to perform AP startup

2013-12-19 Thread Roger Pau Monne
AP startup on PVH follows the PV method, so we need to add a hook in
order to diverge from bare metal.
---
 sys/amd64/amd64/mp_machdep.c |   16 ---
 sys/amd64/include/cpu.h  |1 +
 sys/x86/xen/hvm.c|   17 +++-
 sys/x86/xen/pv.c |   90 ++
 sys/xen/pv.h |1 +
 5 files changed, 117 insertions(+), 8 deletions(-)

diff --git a/sys/amd64/amd64/mp_machdep.c b/sys/amd64/amd64/mp_machdep.c
index 4ef4b3d..e302886 100644
--- a/sys/amd64/amd64/mp_machdep.c
+++ b/sys/amd64/amd64/mp_machdep.c
@@ -90,7 +90,7 @@ extern  struct pcpu __pcpu[];
 
 /* AP uses this during bootstrap.  Do not staticize.  */
 char *bootSTK;
-static int bootAP;
+int bootAP;
 
 /* Free these after use */
 void *bootstacks[MAXCPU];
@@ -122,9 +122,12 @@ u_long *ipi_rendezvous_counts[MAXCPU];
 static u_long *ipi_hardclock_counts[MAXCPU];
 #endif
 
+int native_start_all_aps(void);
+
 /* Default cpu_ops implementation. */
 struct cpu_ops cpu_ops = {
-   .ipi_vectored = lapic_ipi_vectored
+   .ipi_vectored = lapic_ipi_vectored,
+   .start_all_aps = native_start_all_aps,
 };
 
 extern inthand_t IDTVEC(fast_syscall), IDTVEC(fast_syscall32);
@@ -138,7 +141,7 @@ extern int pmap_pcid_enabled;
 static volatile cpuset_t ipi_nmi_pending;
 
 /* used to hold the AP's until we are ready to release them */
-static struct mtx ap_boot_mtx;
+struct mtx ap_boot_mtx;
 
 /* Set to 1 once we're ready to let the APs out of the pen. */
 static volatile int aps_ready = 0;
@@ -165,7 +168,6 @@ static int cpu_cores;   /* cores per 
package */
 
 static voidassign_cpu_ids(void);
 static voidset_interrupt_apic_ids(void);
-static int start_all_aps(void);
 static int start_ap(int apic_id);
 static voidrelease_aps(void *dummy);
 
@@ -569,7 +571,7 @@ cpu_mp_start(void)
assign_cpu_ids();
 
/* Start each Application Processor */
-   start_all_aps();
+   cpu_ops.start_all_aps();
 
set_interrupt_apic_ids();
 }
@@ -908,8 +910,8 @@ assign_cpu_ids(void)
 /*
  * start each AP in our list
  */
-static int
-start_all_aps(void)
+int
+native_start_all_aps(void)
 {
vm_offset_t va = boot_address + KERNBASE;
u_int64_t *pt4, *pt3, *pt2;
diff --git a/sys/amd64/include/cpu.h b/sys/amd64/include/cpu.h
index 3d9ff531..ed9f1db 100644
--- a/sys/amd64/include/cpu.h
+++ b/sys/amd64/include/cpu.h
@@ -64,6 +64,7 @@ struct cpu_ops {
void (*cpu_init)(void);
void (*cpu_resume)(void);
void (*ipi_vectored)(u_int, int);
+   int  (*start_all_aps)(void);
 };
 
 extern struct  cpu_ops cpu_ops;
diff --git a/sys/x86/xen/hvm.c b/sys/x86/xen/hvm.c
index fb1ed79..5ec9f3a 100644
--- a/sys/x86/xen/hvm.c
+++ b/sys/x86/xen/hvm.c
@@ -53,6 +53,9 @@ __FBSDID($FreeBSD$);
 #include xen/hypervisor.h
 #include xen/hvm.h
 #include xen/xen_intr.h
+#ifdef __amd64__
+#include xen/pv.h
+#endif
 
 #include xen/interface/hvm/params.h
 #include xen/interface/vcpu.h
@@ -97,6 +100,11 @@ extern void pmap_lazyfix_action(void);
 /* Variables used by mp_machdep to perform the bitmap IPI */
 extern volatile u_int cpu_ipi_pending[MAXCPU];
 
+#ifdef __amd64__
+/* Native AP start used on PVHVM */
+extern int native_start_all_aps(void);
+#endif
+
 /*-- Macros 
--*/
 #defineIPI_TO_IDX(ipi) ((ipi) - APIC_IPI_INTS)
 
@@ -119,7 +127,10 @@ enum xen_domain_type xen_domain_type = XEN_NATIVE;
 struct cpu_ops xen_hvm_cpu_ops = {
.ipi_vectored   = lapic_ipi_vectored,
.cpu_init   = xen_hvm_cpu_init,
-   .cpu_resume = xen_hvm_cpu_resume
+   .cpu_resume = xen_hvm_cpu_resume,
+#ifdef __amd64__
+   .start_all_aps = native_start_all_aps,
+#endif
 };
 
 static MALLOC_DEFINE(M_XENHVM, xen_hvm, Xen HVM PV Support);
@@ -698,6 +709,10 @@ xen_hvm_init(enum xen_hvm_init_type init_type)
setup_xen_features();
cpu_ops = xen_hvm_cpu_ops;
vm_guest = VM_GUEST_XEN;
+#ifdef __amd64__
+   if (xen_pv_domain())
+   cpu_ops.start_all_aps = xen_pv_start_all_aps;
+#endif
break;
case XEN_HVM_INIT_RESUME:
if (error != 0)
diff --git a/sys/x86/xen/pv.c b/sys/x86/xen/pv.c
index db576e0..7e45a83 100644
--- a/sys/x86/xen/pv.c
+++ b/sys/x86/xen/pv.c
@@ -34,21 +34,43 @@ __FBSDID($FreeBSD$);
 #include sys/kernel.h
 #include sys/reboot.h
 #include sys/systm.h
+#include sys/malloc.h
+#include sys/lock.h
+#include sys/mutex.h
+#include sys/smp.h
+
+#include vm/vm.h
+#include vm/pmap.h
+#include vm/vm_extern.h
+#include vm/vm_kern.h
 
 #include machine/sysarch.h
 #include machine/clock.h
 #include machine/pc/bios.h
+#include machine/smp.h
 
 #include xen/xen-os.h
 #include xen/pv.h
 #include xen/hypervisor.h
 
+#include xen/interface/vcpu.h
+
 #define MAX_E820_ENTRIES   128
 
 /*--- Forward Declarations 

[PATCH v7 16/19] xen: add shutdown hook for PVH

2013-12-19 Thread Roger Pau Monne
Add the PV shutdown hook to PVH.
---
 sys/dev/xen/control/control.c |   37 ++---
 1 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/sys/dev/xen/control/control.c b/sys/dev/xen/control/control.c
index bc0609d..78894ba 100644
--- a/sys/dev/xen/control/control.c
+++ b/sys/dev/xen/control/control.c
@@ -316,21 +316,6 @@ xctrl_suspend()
EVENTHANDLER_INVOKE(power_resume);
 }
 
-static void
-xen_pv_shutdown_final(void *arg, int howto)
-{
-   /*
-* Inform the hypervisor that shutdown is complete.
-* This is not necessary in HVM domains since Xen
-* emulates ACPI in that mode and FreeBSD's ACPI
-* support will request this transition.
-*/
-   if (howto  (RB_HALT | RB_POWEROFF))
-   HYPERVISOR_shutdown(SHUTDOWN_poweroff);
-   else
-   HYPERVISOR_shutdown(SHUTDOWN_reboot);
-}
-
 #else
 
 /* HVM mode suspension. */
@@ -440,6 +425,21 @@ xctrl_crash()
panic(Xen directed crash);
 }
 
+static void
+xen_pv_shutdown_final(void *arg, int howto)
+{
+   /*
+* Inform the hypervisor that shutdown is complete.
+* This is not necessary in HVM domains since Xen
+* emulates ACPI in that mode and FreeBSD's ACPI
+* support will request this transition.
+*/
+   if (howto  (RB_HALT | RB_POWEROFF))
+   HYPERVISOR_shutdown(SHUTDOWN_poweroff);
+   else
+   HYPERVISOR_shutdown(SHUTDOWN_reboot);
+}
+
 /*-- Event Reception 
-*/
 static void
 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int 
len)
@@ -522,10 +522,9 @@ xctrl_attach(device_t dev)
xctrl-xctrl_watch.callback_data = (uintptr_t)xctrl;
xs_register_watch(xctrl-xctrl_watch);
 
-#ifndef XENHVM
-   EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL,
- SHUTDOWN_PRI_LAST);
-#endif
+   if (xen_pv_domain())
+   EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, 
NULL,
+ SHUTDOWN_PRI_LAST);
 
return (0);
 }
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org


[PATCH v7 19/19] isa: allow ISA bus to attach to the nexus

2013-12-19 Thread Roger Pau Monne
---
 sys/x86/isa/isa.c |3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/sys/x86/isa/isa.c b/sys/x86/isa/isa.c
index 1a57137..09d1ab7 100644
--- a/sys/x86/isa/isa.c
+++ b/sys/x86/isa/isa.c
@@ -241,3 +241,6 @@ isa_release_resource(device_t bus, device_t child, int 
type, int rid,
  * On this platform, isa can also attach to the legacy bus.
  */
 DRIVER_MODULE(isa, legacy, isa_driver, isa_devclass, 0, 0);
+#ifdef XENHVM
+DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
+#endif
-- 
1.7.7.5 (Apple Git-26)

___
freebsd-xen@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-xen
To unsubscribe, send any mail to freebsd-xen-unsubscr...@freebsd.org


Re: [PATCH v7 01/19] xen: add PV/PVH kernel entry point

2013-12-19 Thread Konstantin Belousov
Thank you for doing the split.

On Thu, Dec 19, 2013 at 07:54:38PM +0100, Roger Pau Monne wrote:
 Add the PV/PVH entry point and the low level functions for PVH
 initialization.
 ---
  sys/amd64/amd64/locore.S |   53 +++
  sys/amd64/amd64/machdep.c|   72 
 ++
  sys/amd64/include/asmacros.h |   26 +++
The changes to the three files above all protected by #ifdef XENHVM.
IMO it is more logical to move the code into xen-specific files.
The support would be localized and somewhat postpone morphing the
sys/amd64 into the maze of #ifdefs, which is the sys/i386 already.



pgppMhUpikD1y.pgp
Description: PGP signature