From 5a26c4ebdf805950b3350eb87ecdbad788d8e8be Mon Sep 17 00:00:00 2001
From: Julien Grall <julien.grall@linaro.org>
Date: Tue, 14 Jan 2014 01:40:58 +0000
Subject: [PATCH 48/48] arm: Add xenvirt platform

This platform code allow FreeBSD to boot as Xen on ARM guest.

I'm not 100% sure of the implementation of synch_* on ARM. I choose to use
the atomic_* builtin. Is it SMP-safe?
---
 sys/arm/arm/machdep.c              |  11 +++
 sys/arm/conf/XENVIRT               | 110 ++++++++++++++++++++++
 sys/arm/include/xen/hypercall.h    |  56 +++++++++++
 sys/arm/include/xen/synch_bitops.h |  52 +++++++++++
 sys/arm/include/xen/xen-os.h       |  37 ++++++++
 sys/arm/include/xen/xenfunc.h      |   4 +
 sys/arm/include/xen/xenvar.h       |  14 +++
 sys/arm/xenvirt/files.xenvirt      |  15 +++
 sys/arm/xenvirt/hypercall.S        | 113 ++++++++++++++++++++++
 sys/arm/xenvirt/xen-dt.c           | 185 +++++++++++++++++++++++++++++++++++++
 sys/arm/xenvirt/xen-pmap.c         |  52 +++++++++++
 sys/arm/xenvirt/xenvirt_machdep.c  |  96 +++++++++++++++++++
 sys/arm/xenvirt/xenvirt_mp.c       | 132 ++++++++++++++++++++++++++
 sys/conf/files.arm                 |   3 +
 sys/conf/options.arm               |   1 +
 15 files changed, 881 insertions(+)
 create mode 100644 sys/arm/conf/XENVIRT
 create mode 100644 sys/arm/include/xen/hypercall.h
 create mode 100644 sys/arm/include/xen/synch_bitops.h
 create mode 100644 sys/arm/include/xen/xen-os.h
 create mode 100644 sys/arm/include/xen/xenfunc.h
 create mode 100644 sys/arm/include/xen/xenvar.h
 create mode 100644 sys/arm/xenvirt/files.xenvirt
 create mode 100644 sys/arm/xenvirt/hypercall.S
 create mode 100644 sys/arm/xenvirt/xen-dt.c
 create mode 100644 sys/arm/xenvirt/xen-pmap.c
 create mode 100644 sys/arm/xenvirt/xenvirt_machdep.c
 create mode 100644 sys/arm/xenvirt/xenvirt_mp.c

diff --git a/sys/arm/arm/machdep.c b/sys/arm/arm/machdep.c
index 37281c9..8e97115 100644
--- a/sys/arm/arm/machdep.c
+++ b/sys/arm/arm/machdep.c
@@ -116,6 +116,10 @@ __FBSDID("$FreeBSD$");
 #include <ddb/ddb.h>
 #endif
 
+#ifdef XENHVM
+#include <xen/xen-os.h>
+#endif
+
 #ifdef DEBUG
 #define	debugf(fmt, args...) printf(fmt, ##args)
 #else
@@ -1277,6 +1281,13 @@ initarm(struct arm_boot_params *abp)
 	OF_interpret("perform-fixup", 0);
 
 	platform_gpio_init();
+#ifdef XENHVM
+	/*
+	 * We need to know early if FreeBSD is running on top of Xen.
+	 * Therefore we can get an early console working
+	 */
+	xen_early_init();
+#endif
 
 	cninit();
 
diff --git a/sys/arm/conf/XENVIRT b/sys/arm/conf/XENVIRT
new file mode 100644
index 0000000..ff64333
--- /dev/null
+++ b/sys/arm/conf/XENVIRT
@@ -0,0 +1,110 @@
+# XENVIRT - Configuration for Xen ARM Guest
+#
+# For more information on this file, please read the handbook section on
+# Kernel Configuration Files:
+#
+#    http://www.FreeBSD.org/doc/en_US.ISO8859-1/books/handbook/kernelconfig-config.html
+#
+# The handbook is also available locally in /usr/share/doc/handbook
+# if you've installed the doc distribution, otherwise always see the
+# FreeBSD World Wide Web server (http://www.FreeBSD.org/) for the
+# latest information.
+#
+# An exhaustive list of options and more detailed explanations of the
+# device lines is also present in the ../../conf/NOTES and NOTES files. 
+# If you are in doubt as to the purpose or necessity of a line, check first 
+# in NOTES.
+#
+# $FreeBSD$
+
+ident		XENVIRT
+
+cpu		CPU_CORTEXA
+machine 	arm armv6
+makeoption	ARM_LITTLE_ENDIAN
+
+options 	XENHVM
+options 	ROOTDEVNAME=\"ufs:xbd0\"
+
+options 	KERNVIRTADDR=0xc0100000
+makeoptions	KERNVIRTADDR=0xc0100000
+options 	KERNPHYSADDR=0x80100000
+makeoptions	KERNPHYSADDR=0x80100000
+
+options		ARM_L2_PIPT
+
+files		"../xenvirt/files.xenvirt"
+
+options 	IPI_IRQ_START=0
+options 	IPI_IRQ_END=15
+
+makeoptions	MODULES_OVERRIDE=""
+
+options 	HZ=100
+options 	SCHED_4BSD		# 4BSD scheduler
+options 	INET			# InterNETworking
+options 	INET6			# IPv6 communications protocols
+options 	GEOM_PART_BSD		# BSD partition scheme
+options 	GEOM_PART_MBR		# MBR partition scheme
+options 	TMPFS			# Efficient memory filesystem
+options 	FFS			# Berkeley Fast Filesystem
+options 	SOFTUPDATES		# Enable FFS soft updates support
+options 	UFS_ACL			# Support for access control lists
+options 	UFS_DIRHASH		# Improve performance on big directories
+options 	MSDOSFS			# MSDOS Filesystem
+options 	CD9660			# ISO 9660 Filesystem
+options 	PROCFS			# Process filesystem (requires PSEUDOFS)
+options 	PSEUDOFS		# Pseudo-filesystem framework
+options 	COMPAT_43		# Compatible with BSD 4.3 [KEEP THIS!]
+options 	SCSI_DELAY=5000		# Delay (in ms) before probing SCSI
+options 	KTRACE			# ktrace(1) support
+options 	SYSVSHM			# SYSV-style shared memory
+options 	SYSVMSG			# SYSV-style message queues
+options 	SYSVSEM			# SYSV-style semaphores
+options 	_KPOSIX_PRIORITY_SCHEDULING # Posix P1003_1B real-time extensions
+options 	KBD_INSTALL_CDEV	# install a CDEV entry in /dev
+options 	PREEMPTION
+options		LINUX_BOOT_ABI
+options 	VFP			# vfp/neon
+
+# Debugging
+makeoptions	DEBUG=-g		# Build kernel with gdb(1) debug symbols
+options 	DEBUG
+options 	BREAK_TO_DEBUGGER
+#options 	VERBOSE_SYSINIT		# Enable verbose sysinit messages
+options 	KDB
+options 	DDB			# Enable the kernel debugger
+options 	INVARIANTS		# Enable calls of extra sanity checking
+options 	INVARIANT_SUPPORT	# Extra sanity checks of internal structures, required by INVARIANTS
+options 	WITNESS			# Enable checks to detect deadlocks and cycles
+options 	WITNESS_SKIPSPIN	# Don't run witness on spinlocks for speed
+#options 	DIAGNOSTIC
+
+# NFS support
+#options 	NFSCL
+#options 	NFSSERVER		# Network Filesystem Server
+#options 	NFSCLIENT		# Network Filesystem Client
+
+# Uncomment this for NFS root
+#options 	NFS_ROOT		# NFS usable as /, requires NFSCLIENT
+#options 	BOOTP_NFSROOT
+#options 	BOOTP_COMPAT
+#options 	BOOTP
+#options 	BOOTP_NFSV3
+#options 	BOOTP_WIRED_TO=cpsw0
+
+# Console and misc
+device		pty
+device		snp
+device		md
+
+# Ethernet
+device		loop
+device		ether
+device		bpf
+
+
+# Flattened Device Tree
+options         FDT
+# options         FDT_DTB_STATIC
+makeoptions     FDT_DTS_FILE=xenvm.dts
diff --git a/sys/arm/include/xen/hypercall.h b/sys/arm/include/xen/hypercall.h
new file mode 100644
index 0000000..0e1e1cc
--- /dev/null
+++ b/sys/arm/include/xen/hypercall.h
@@ -0,0 +1,56 @@
+#ifndef __MACHINE_XEN_HYPERCALL_H__
+#define __MACHINE_XEN_HYPERCALL_H__
+
+#define CONFIG_XEN_COMPAT	0x030003
+
+#include <xen/interface/xen.h>
+#include <xen/interface/sched.h>
+
+int
+HYPERVISOR_console_io(int cmd, unsigned int count, const char *str);
+
+int
+HYPERVISOR_physdev_op(int cmd, void *arg);
+
+int
+HYPERVISOR_sched_op(int cmd, void *arg);
+
+int
+HYPERVISOR_xen_version(int cmd, void *arg);
+
+int
+HYPERVISOR_grant_table_op(int cmd, void *uop, unsigned int count);
+
+int
+HYPERVISOR_memory_op(unsigned int cmd, void *arg);
+
+int
+HYPERVISOR_event_channel_op(unsigned int cmd, void *arg);
+
+static inline int
+HYPERVISOR_multicall(multicall_entry_t *call_list, unsigned int nr_calls)
+{
+	panic("Xen multicall hypercall is not implemented on ARM\n");
+	return -ENOSYS;
+}
+
+int
+HYPERVISOR_vcpu_op(int cmd, unsigned int vcpuid, void *extra_args);
+
+unsigned long
+HYPERVISOR_hvm_op(int op, void *arg);
+
+int
+privcmd_hypercall(long op, long a1, long a2, long a3, long a4, long a5);
+
+#endif /* __MACHINE_XEN_HYPERCALL_H__ */
+
+/*
+ * Local variables:
+ *  c-file-style: "linux"
+ *  indent-tabs-mode: t
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ *  tab-width: 8
+ * End:
+ */
diff --git a/sys/arm/include/xen/synch_bitops.h b/sys/arm/include/xen/synch_bitops.h
new file mode 100644
index 0000000..b939611
--- /dev/null
+++ b/sys/arm/include/xen/synch_bitops.h
@@ -0,0 +1,52 @@
+#ifndef __MACHINE_XEN_SYNCH_BITOPS_H__
+#define __MACHINE_XEN_SYNCH_BITOPS_H__
+
+#include <machine/xen/xenvar.h>
+
+#define synch_cmpxchg(ptr, old, new) \
+	__sync_val_compare_and_swap((ptr), (old), (new))
+
+#define	__bit_mask(b)		(1UL << (b) % BITS_PER_LONG)
+#define	__bit_word(b)		((b) / BITS_PER_LONG)
+#define	__bit_addr(p, b)	((volatile u_long *)(p) + __bit_word(b))
+
+#define	synch_clear_bit(b, p) \
+	atomic_clear_long(__bit_addr(p, b), __bit_mask(b))
+
+#define synch_set_bit(b, p) \
+	atomic_set_long(__bit_addr(p, b), __bit_mask(b))
+
+static inline long
+synch_test_and_clear_bit(long bit, void *p)
+{
+	long val;
+	long *var = p;
+
+	var += bit / (sizeof(long) * NBBY);
+	bit %= sizeof(long) * NBBY;
+	bit = 1 << bit;
+	do {
+		val = *(volatile long *)var;
+	} while (atomic_cmpset_long(var, val, val & ~bit) == 0);
+
+	return !!(val & bit);
+}
+
+static inline long
+synch_test_and_set_bit(long bit, void *p)
+{
+	long val;
+	long *var = p;
+
+	var += bit / (sizeof(long) * NBBY);
+	bit %= sizeof(long) * NBBY;
+	bit = 1 << bit;
+	do {
+		val = *(volatile long *)var;
+	} while (atomic_cmpset_long(var, val, val | bit) == 0);
+
+	return !!(val & bit);
+}
+
+#endif /* __MACHINE_XEN_SYNCH_BITOPS_H__ */
+
diff --git a/sys/arm/include/xen/xen-os.h b/sys/arm/include/xen/xen-os.h
new file mode 100644
index 0000000..a27650c
--- /dev/null
+++ b/sys/arm/include/xen/xen-os.h
@@ -0,0 +1,37 @@
+#ifndef __MACHINE_XEN_XEN_OS_H__
+#define __MACHINE_XEN_XEN_OS_H__
+
+#ifndef __ASSEMBLY__
+
+/* This is a barrier for the compiler only, NOT the processor! */
+#define barrier() __asm__ __volatile__("": : :"memory")
+#define cpu_relax() barrier()
+
+void *xen_pmap(vm_offset_t pa, vm_size_t size);
+void xen_unmap(vm_offset_t va, vm_size_t size);
+
+#define	__bit_mask(b)		(1UL << (b) % BITS_PER_LONG)
+#define	__bit_word(b)		((b) / BITS_PER_LONG)
+#define	__bit_addr(p, b)	((volatile u_long *)(p) + __bit_word(b))
+
+/* copied from sys/ofed/include/linux/bitops.h */
+#define	test_bit(i, a)							 \
+    !!(atomic_load_acq_long(&((volatile long *)(a))[(i)/BITS_PER_LONG]) &\
+    1 << ((i) % BITS_PER_LONG))
+
+#define clear_bit(b, p)	\
+	atomic_clear_long(__bit_addr(p, b), __bit_mask(b))
+
+#define set_bit(b, p) \
+	atomic_set_long(__bit_addr(p, b), __bit_mask(b))
+
+#define cpu_to_vcpu_id(cpu) cpu
+
+#define atomic_readandclear_xen_ulong(p)	atomic_readandclear_64(p)
+#define atomic_store_rel_xen_ulong(p, v)	atomic_store_rel_64(p, v)
+
+void xen_early_init(void);
+
+#endif
+
+#endif /* __MACHINE_XEN_XEN_OS__ */
diff --git a/sys/arm/include/xen/xenfunc.h b/sys/arm/include/xen/xenfunc.h
new file mode 100644
index 0000000..8fbb986
--- /dev/null
+++ b/sys/arm/include/xen/xenfunc.h
@@ -0,0 +1,4 @@
+#ifndef __MACHINE_XEN_XEN_FUNC_H__
+#define __MACHINE_XEN_XEN_FUNC_H__
+
+#endif /* __MACHINE_XEN_XEN_FUNC_H__ */
diff --git a/sys/arm/include/xen/xenvar.h b/sys/arm/include/xen/xenvar.h
new file mode 100644
index 0000000..8d73c74
--- /dev/null
+++ b/sys/arm/include/xen/xenvar.h
@@ -0,0 +1,14 @@
+#ifndef __MACHINE_XEN_XENVAR_H__
+#define __MACHINE_XEN_XENVAR_H__
+
+#define BITS_PER_LONG (NBBY * sizeof(long))
+
+#define phys_to_machine_mapping_valid(pnf) (TRUE)
+#define set_phys_to_machine(pfn, mfn) ((void)0)
+#define vtomach(va)	pmap_kextract((vm_offset_t)(va))
+
+#define PFNTOMFN(pa)	(pa)
+#define MFNTOPFN(ma)	(ma)
+#define PT_UPDATES_FLUSH()	((void)0)
+
+#endif /* __MACHINE_XEN_XENVAR_H__ */
diff --git a/sys/arm/xenvirt/files.xenvirt b/sys/arm/xenvirt/files.xenvirt
new file mode 100644
index 0000000..6b6da8e
--- /dev/null
+++ b/sys/arm/xenvirt/files.xenvirt
@@ -0,0 +1,15 @@
+# $FreeBSD$
+
+arm/arm/bus_space_asm_generic.S			standard
+arm/arm/bus_space_generic.c			standard
+arm/arm/cpufunc_asm_armv7.S			standard
+arm/arm/cpufunc_asm_arm11.S			standard
+
+arm/arm/bus_space-v6.c				standard
+arm/arm/gic.c					standard
+arm/arm/generic_timer.c				standard
+
+kern/kern_clocksource.c				standard
+
+arm/xenvirt/xenvirt_machdep.c			standard
+arm/xenvirt/xenvirt_mp.c			optional smp
diff --git a/sys/arm/xenvirt/hypercall.S b/sys/arm/xenvirt/hypercall.S
new file mode 100644
index 0000000..b2713aa
--- /dev/null
+++ b/sys/arm/xenvirt/hypercall.S
@@ -0,0 +1,113 @@
+/******************************************************************************
+ * hypercall.S
+ *
+ * Xen hypercall wrappers
+ *
+ * Stefano Stabellini <stefano.stabellini@eu.citrix.com>, Citrix, 2012
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation; or, when distributed
+ * separately from the Linux kernel or incorporated into other
+ * software packages, subject to the following license:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this source file (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/*
+ * The Xen hypercall calling convention is very similar to the ARM
+ * procedure calling convention: the first paramter is passed in r0, the
+ * second in r1, the third in r2 and the fourth in r3. Considering that
+ * Xen hypercalls have 5 arguments at most, the fifth paramter is passed
+ * in r4, differently from the procedure calling convention of using the
+ * stack for that case.
+ *
+ * The hypercall number is passed in r12.
+ *
+ * The return value is in r0.
+ *
+ * The hvc ISS is required to be 0xEA1, that is the Xen specific ARM
+ * hypercall tag.
+ */
+
+#undef __ASSEMBLY__
+#define __ASSEMBLY__
+
+#include <machine/asm.h>
+#include <xen/xen-os.h>
+__FBSDID("$FreeBSD$");
+
+#define __HVC(imm16)							\
+	.word (0xE1400070 | (((imm16) & 0xFFF0) << 4) | ((imm16) & 0x000F))
+#define XEN_IMM 0xEA1
+
+#define HYPERCALL_SIMPLE(hypercall)		\
+ENTRY(HYPERVISOR_##hypercall)			\
+	mov r12, #__HYPERVISOR_##hypercall;	\
+	__HVC(XEN_IMM);						\
+	mov pc, lr;							\
+END(HYPERVISOR_##hypercall)
+
+#define HYPERCALL0 HYPERCALL_SIMPLE
+#define HYPERCALL1 HYPERCALL_SIMPLE
+#define HYPERCALL2 HYPERCALL_SIMPLE
+#define HYPERCALL3 HYPERCALL_SIMPLE
+#define HYPERCALL4 HYPERCALL_SIMPLE
+
+#define HYPERCALL5(hypercall)			\
+ENTRY(HYPERVISOR_##hypercall)			\
+	stmdb sp!, {r4}						\
+	ldr r4, [sp, #4]					\
+	mov r12, #__HYPERVISOR_##hypercall;	\
+	__HVC(XEN_IMM);						\
+	ldm sp!, {r4}						\
+	mov pc, lr							\
+END(HYPERVISOR_##hypercall)
+
+                .text
+
+HYPERCALL2(xen_version);
+HYPERCALL3(console_io);
+HYPERCALL3(grant_table_op);
+HYPERCALL2(sched_op);
+HYPERCALL2(event_channel_op);
+HYPERCALL2(hvm_op);
+HYPERCALL2(memory_op);
+HYPERCALL2(physdev_op);
+HYPERCALL3(vcpu_op);
+HYPERCALL1(tmem_op);
+
+ENTRY(privcmd_hypercall)
+	stmdb sp!, {r4}
+	mov r12, r0
+	mov r0, r1
+	mov r1, r2
+	mov r2, r3
+	ldr r3, [sp, #8]
+	ldr r4, [sp, #4]
+	__HVC(XEN_IMM)
+	ldm sp!, {r4}
+	mov pc, lr
+END(privcmd_hypercall);
+
+
+ENTRY(invoke_psci_fn_hvc)
+	__HVC(0)
+	mov pc, lr
+END(invoke_psci_fn_hvc)
diff --git a/sys/arm/xenvirt/xen-dt.c b/sys/arm/xenvirt/xen-dt.c
new file mode 100644
index 0000000..998aab5
--- /dev/null
+++ b/sys/arm/xenvirt/xen-dt.c
@@ -0,0 +1,185 @@
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/pcpu.h>
+#include <sys/rman.h>
+#include <sys/module.h>
+
+#include <vm/vm.h>
+#include <vm/pmap.h>
+#include <vm/vm_page.h>
+
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+
+#include <dev/fdt/fdt_common.h>
+
+#include <xen/xen_intr.h>
+#include <xen/xen-os.h>
+#include <xen/hypervisor.h>
+#include <xen/interface/vcpu.h>
+#include <xen/features.h>
+
+static start_info_t _xen_start_info;
+start_info_t *HYPERVISOR_start_info = &_xen_start_info;
+
+struct shared_info *HYPERVISOR_shared_info = NULL;
+
+enum xen_domain_type xen_domain_type = 0;
+
+DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
+DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
+
+int xen_vector_callback_enabled = 0;
+
+struct xen_softc
+{
+	struct resource		*xen_res[2];
+	void			*intr_cookie;
+};
+
+#define XEN_RESOURCE_IRQ_EVTCHN 0
+#define XEN_RESOURCE_MEMORY_GNTTAB 1
+
+static struct resource_spec xen_spec[] = {
+	/* RF_SHAREABLE ??? */
+	{ SYS_RES_IRQ, 0, RF_ACTIVE }, /* Event channel IRQ */
+	{ SYS_RES_MEMORY, 0, 0}, /* Grant-table */
+	{ -1, 0, 0 },
+};
+
+struct xen_softc *xen_sc = NULL;
+
+void xen_early_init(void)
+{
+	phandle_t root;
+
+	if ((root = OF_finddevice("/")) == 0)
+		return;
+	/*
+	 * The device tree contains a node with the compatible string "xen,xen"
+	 * when the OS will run on top of Xen.
+	 */
+	if (fdt_depth_search_compatible(root, "xen,xen", 0) == 0)
+		return;
+
+	xen_domain_type = XEN_HVM_DOMAIN;
+	setup_xen_features();
+
+	if (xen_feature(XENFEAT_dom0))
+		HYPERVISOR_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
+	else
+		HYPERVISOR_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
+}
+
+static void xen_init(void *arg __unused)
+{
+	int rc;
+	struct xen_add_to_physmap xatp;
+	vm_page_t shared_info;
+	vm_paddr_t shared_paddr;
+
+	if (!xen_domain())
+		return;
+
+	shared_info = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
+	KASSERT(shared_info != NULL, ("Unable to allocate shared page\n"));
+	shared_paddr = VM_PAGE_TO_PHYS(shared_info);
+	HYPERVISOR_shared_info = xen_pmap(shared_paddr, PAGE_SIZE);
+
+	xatp.domid = DOMID_SELF;
+	xatp.idx = 0;
+	xatp.space = XENMAPSPACE_shared_info;
+	xatp.gpfn = shared_paddr >> PAGE_SHIFT;
+	rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
+	KASSERT(rc == 0, ("Unable to map shared info\n"));
+}
+
+SYSINIT(xen_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_init, NULL);
+
+static int
+xen_probe(device_t dev)
+{
+	if (!ofw_bus_is_compatible(dev, "xen,xen"))
+		return (ENXIO);
+
+	device_set_desc(dev, "Xen ARM");
+
+	return (BUS_PROBE_DEFAULT);
+}
+
+static int
+xen_intr_filter(void *trap_frame)
+{
+	xen_intr_handle_upcall(trap_frame);
+	return (FILTER_HANDLED);
+}
+
+extern vm_paddr_t resume_frames;
+
+static int
+xen_attach(device_t dev)
+{
+	struct xen_softc *sc;
+	struct vcpu_register_vcpu_info info;
+	struct vcpu_info *vcpu_info;
+	int rc, cpu;
+
+	if (xen_sc != NULL)
+		return (ENXIO);
+
+	sc = device_get_softc(dev);
+
+	/* TODO: Move to a proper function */
+	vcpu_info = DPCPU_PTR(vcpu_local_info);
+	cpu = PCPU_GET(cpuid);
+	info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
+	info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
+
+	rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
+	KASSERT(rc == 0, ("Unable to register cpu %u\n", cpu));
+	DPCPU_SET(vcpu_info, vcpu_info);
+
+	if (bus_alloc_resources(dev, xen_spec, sc->xen_res)) {
+			device_printf(dev, "could not allocate resources\n");
+			return (ENXIO);
+	}
+
+	resume_frames = rman_get_start(sc->xen_res[XEN_RESOURCE_MEMORY_GNTTAB]);
+
+	xen_sc = sc;
+
+	/* Setup and enable the event channel interrupt */
+	if (bus_setup_intr(dev, sc->xen_res[XEN_RESOURCE_IRQ_EVTCHN],
+			   INTR_TYPE_MISC|INTR_MPSAFE,
+			   xen_intr_filter, NULL, sc, &sc->intr_cookie))
+	{
+		bus_release_resources(dev, xen_spec, sc->xen_res);
+		device_printf(dev, "could not setup event channel interrupt\n");
+		return (ENXIO);
+	}
+
+	return (0);
+}
+
+static device_method_t xen_methods[] = {
+	DEVMETHOD(device_probe, xen_probe),
+	DEVMETHOD(device_attach, xen_attach),
+	{ 0, 0 }
+};
+
+static driver_t xen_driver = {
+	"xen",
+	xen_methods,
+	sizeof(struct xen_softc),
+};
+
+static devclass_t xen_devclass;
+
+DRIVER_MODULE(xen, simplebus, xen_driver, xen_devclass, 0, 0);
+DRIVER_MODULE(xen, ofwbus, xen_driver, xen_devclass, 0, 0);
diff --git a/sys/arm/xenvirt/xen-pmap.c b/sys/arm/xenvirt/xen-pmap.c
new file mode 100644
index 0000000..2697210
--- /dev/null
+++ b/sys/arm/xenvirt/xen-pmap.c
@@ -0,0 +1,52 @@
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_extern.h>
+#include <vm/pmap.h>
+
+#include <xen/xen-os.h>
+
+void *
+xen_pmap(vm_offset_t pa, vm_size_t size)
+{
+	vm_offset_t va, tmpva, offset;
+
+	offset = pa & PAGE_MASK;
+	size = roundup(size, PAGE_SIZE);
+
+	va = kva_alloc(size);
+	if (!va)
+		panic("xen_pmap: Couldn't allocate kernel virtual memory");
+	for (tmpva = va; size > 0;) {
+		pmap_kenter(tmpva, pa);
+		size -= PAGE_SIZE;
+		tmpva += PAGE_SIZE;
+		pa += PAGE_SIZE;
+	}
+
+	return ((void *)(va + offset));
+}
+
+void
+xen_unmap(vm_offset_t va, vm_size_t size)
+{
+	vm_offset_t tmpva, offset;
+	vm_size_t origsize;
+
+	origsize = size;
+	offset = va & PAGE_MASK;
+	va = trunc_page(va);
+	size = round_page(size + offset);
+
+	for (tmpva = va; size >0;) {
+		pmap_kremove(tmpva);
+		size -= PAGE_SIZE;
+		tmpva += PAGE_SIZE;
+	}
+
+	kva_free(va, origsize);
+}
diff --git a/sys/arm/xenvirt/xenvirt_machdep.c b/sys/arm/xenvirt/xenvirt_machdep.c
new file mode 100644
index 0000000..049c589
--- /dev/null
+++ b/sys/arm/xenvirt/xenvirt_machdep.c
@@ -0,0 +1,96 @@
+#include "opt_ddb.h"
+#include "opt_platform.h"
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#define	_ARM32_BUS_DMA_PRIVATE
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/reboot.h>
+
+#include <vm/vm.h>
+#include <vm/vm_extern.h>
+#include <vm/pmap.h>
+
+#include <machine/armreg.h>
+#include <machine/bus.h>
+#include <machine/devmap.h>
+#include <machine/machdep.h>
+#include <machine/platform.h>
+#include <machine/intr_machdep.h>
+
+#include <dev/fdt/fdt_common.h>
+
+#include <xen/xen-os.h>
+
+void
+platform_probe_and_attach(void)
+{
+
+}
+
+vm_offset_t
+platform_lastaddr(void)
+{
+	return (arm_devmap_lastaddr());
+}
+
+void
+platform_gpio_init(void)
+{
+}
+
+void
+platform_late_init(void)
+{
+	phandle_t node;
+
+	node = OF_finddevice("/");
+	KASSERT(node != 0, ("Unable to find DT root"));
+
+	KASSERT(fdt_is_compatible(node, "xen,xenvm") != 0,
+		("Not a Xen Guest"));
+
+	/* TODO: Handle Xen version */
+}
+int
+platform_devmap_init(void)
+{
+	/* We don't have static mapping for XEN */
+	return (0);
+}
+
+
+
+struct arm32_dma_range *
+bus_dma_get_range(void)
+{
+	return (NULL);
+}
+
+int
+bus_dma_get_range_nb(void)
+{
+	return (0);
+}
+
+void
+cpu_reset()
+{
+	/* TODO: handle cpu reset via PSCI/XEN */
+	printf("cpu reset\n");
+	while (1);
+}
+
+struct fdt_fixup_entry fdt_fixup_table[] = {
+	{ NULL, NULL }
+};
+
+fdt_pic_decode_t fdt_pic_table[] = {
+	&gic_decode_fdt,
+	NULL
+};
+
+
diff --git a/sys/arm/xenvirt/xenvirt_mp.c b/sys/arm/xenvirt/xenvirt_mp.c
new file mode 100644
index 0000000..48226fe
--- /dev/null
+++ b/sys/arm/xenvirt/xenvirt_mp.c
@@ -0,0 +1,132 @@
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/smp.h>
+
+#include <machine/smp.h>
+#include <machine/fdt.h>
+#include <machine/intr_machdep.h>
+
+#include <dev/ofw/openfirm.h>
+
+#include <xen/xen-os.h>
+
+static int (*invoke_psci_fn)(uint32_t function_id, uint32_t arg0,
+			     uint32_t arg1, uint32_t arg2);
+
+enum psci_function {
+	PSCI_FN_CPU_ON,
+	PSCI_FN_MAX,
+};
+
+static uint32_t psci_function_id[PSCI_FN_MAX];
+
+#define PSCI_RET_SUCESS		0
+#define PSCI_RET_EOPNOTSUPP	-1
+#define PSCI_RET_EINVAL		-2
+#define PSCI_RET_EPERM		-3
+
+int invoke_psci_fn_hvc(uint32_t function_id, uint32_t arg0,
+		       uint32_t arg1, uint32_t arg2);
+
+static int psci_cpu_on(u_int cpuid, vm_paddr_t entry_point)
+{
+	int err;
+	uint32_t fn;
+
+	xc_printf("%s: cpuid = %d entry_point = 0x%x\n",
+		  __FUNCTION__, cpuid, entry_point);
+
+	fn = psci_function_id[PSCI_FN_CPU_ON];
+	err = invoke_psci_fn(fn, cpuid, entry_point, 0);
+
+	return err;
+}
+
+void
+platform_mp_init_secondary(void)
+{
+	gic_init_secondary();
+}
+
+static int discover_cpus(void)
+{
+	phandle_t cpus, child;
+	char buf[5];
+	ssize_t len = -1;
+
+	cpus = OF_finddevice("/cpus");
+
+	if (cpus == -1) {
+		printf("WARNING: Can't find /cpus in the device tree\n");
+		return 0;
+	}
+
+	for (child = OF_child(cpus); child != 0; child = OF_peer(child)) {
+		/* Only count the number of CPU nodes for now
+		 * FreeBSD doesn't make distinction between HW CPUID and
+		 * logical CPUID.
+		 * TODO: handle non-contiguous MDIR */
+		len = OF_getprop(child, "device_type", buf, sizeof(buf) - 1);
+		buf[len] = 0;
+		if (strcmp("cpu", buf)) {
+			continue;
+		}
+		mp_ncpus++;
+	}
+
+	if (mp_ncpus == 0)
+	{
+		printf("WARNING: Missing boot CPU node in the DT?\n");
+		return 0;
+	}
+
+	if (mp_ncpus > MAXCPU) {
+		printf("Number of CPU not in the DT greater than supported, capping them\n");
+		mp_ncpus = MAXCPU;
+	}
+
+	mp_maxid = mp_ncpus - 1;
+
+	return (1);
+}
+
+void
+platform_mp_setmaxid(void)
+{
+	discover_cpus();
+}
+
+int
+platform_mp_probe(void)
+{
+	return (mp_ncpus > 0);
+}
+
+void
+platform_mp_start_ap(void)
+{
+	int err;
+	int i = 0;
+	vm_paddr_t entry_point;
+
+	invoke_psci_fn = invoke_psci_fn_hvc;
+	psci_function_id[PSCI_FN_CPU_ON] = 2;
+
+	entry_point = pmap_kextract((vm_offset_t)mpentry);
+
+	for (i = 1; i < mp_ncpus; i++) {
+		err = psci_cpu_on(i, entry_point);
+		xc_printf("%s: err = %d\n", __FUNCTION__, err);
+	}
+}
+
+void
+platform_ipi_send(cpuset_t cpus, u_int ipi)
+{
+	pic_ipi_send(cpus, ipi);
+}
diff --git a/sys/conf/files.arm b/sys/conf/files.arm
index d5a5b66..7142db6 100644
--- a/sys/conf/files.arm
+++ b/sys/conf/files.arm
@@ -56,6 +56,9 @@ arm/arm/uio_machdep.c		standard
 arm/arm/undefined.c		standard
 arm/arm/vm_machdep.c		standard
 arm/arm/vfp.c			standard
+arm/xenvirt/hypercall.S		optional	xenhvm
+arm/xenvirt/xen-dt.c		optional	xenhvm
+arm/xenvirt/xen-pmap.c		optional	xenhvm
 board_id.h			standard				   \
 	dependency	"$S/arm/conf/genboardid.awk $S/arm/conf/mach-types" \
 	compile-with	"${AWK} -f $S/arm/conf/genboardid.awk $S/arm/conf/mach-types > board_id.h" \
diff --git a/sys/conf/options.arm b/sys/conf/options.arm
index fd32e52..3580d84 100644
--- a/sys/conf/options.arm
+++ b/sys/conf/options.arm
@@ -60,3 +60,4 @@ GFB_NO_FONT_LOADING	opt_gfb.h
 GFB_NO_MODE_CHANGE	opt_gfb.h
 AT91C_MAIN_CLOCK	opt_at91.h
 VFP			opt_global.h
+XENHVM			opt_global.h
-- 
2.1.0

