aarch64: xen event channels support

23 views
Skip to first unread message

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:31 AM2/20/17
to Osv Dev, Sergiy Kibrik
hi OSv developers,
Let me present patches that allow OSv to work with Xen event channels on aarch64 platform.
This series contains some clean up work -- to remove unused (and probably unneeded) code,
and to re-use some generic parts of x64 code, but there're also rather intrusive
changes to core interrupt handling code.

This is basis for further work at xenbus drivers -- blkfront/netfront, and xen console driver.

--
regards,
Sergiy

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:33 AM2/20/17
to Osv Dev, Sergiy Kibrik
It contains definitions of non-existing (in OSv tree) routines, which had been
used in original BSD code and have absolutely no traces of use in OSv.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
bsd/sys/dev/xen/blkfront/blkfront.cc | 1 -
bsd/sys/dev/xen/netfront/netfront.cc | 1 -
bsd/x64/machine/xen/xenfunc.h | 82 ------------------------------------
3 files changed, 84 deletions(-)
delete mode 100644 bsd/x64/machine/xen/xenfunc.h

diff --git a/bsd/sys/dev/xen/blkfront/blkfront.cc b/bsd/sys/dev/xen/blkfront/blkfront.cc
index 3b2027d..08fcab3 100644
--- a/bsd/sys/dev/xen/blkfront/blkfront.cc
+++ b/bsd/sys/dev/xen/blkfront/blkfront.cc
@@ -68,7 +68,6 @@ __FBSDID("$FreeBSD$");
#include <machine/_inttypes.h>
#include <machine/xen/xen-os.h>
#include <machine/xen/xenvar.h>
-#include <machine/xen/xenfunc.h>

#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
diff --git a/bsd/sys/dev/xen/netfront/netfront.cc b/bsd/sys/dev/xen/netfront/netfront.cc
index 3c0c9d1..c2b40ac 100644
--- a/bsd/sys/dev/xen/netfront/netfront.cc
+++ b/bsd/sys/dev/xen/netfront/netfront.cc
@@ -82,7 +82,6 @@ __FBSDID("$FreeBSD$");
#include <machine/intr_machdep.h>

#include <machine/xen/xen-os.h>
-#include <machine/xen/xenfunc.h>
#include <machine/xen/xenvar.h>
#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
diff --git a/bsd/x64/machine/xen/xenfunc.h b/bsd/x64/machine/xen/xenfunc.h
deleted file mode 100644
index d03d4f6..0000000
--- a/bsd/x64/machine/xen/xenfunc.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/*-
- * Copyright (c) 2004, 2005 Kip Macy
- * 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.
- *
- * $FreeBSD$
- */
-
-#ifndef _XEN_XENFUNC_H_
-#define _XEN_XENFUNC_H_
-
-#ifdef XENHVM
-#include <machine/xen/xenvar.h>
-#else
-#include <machine/xen/xenpmap.h>
-#include <machine/segments.h>
-#endif
-
-#define BKPT __asm__("int3");
-#define XPQ_CALL_DEPTH 5
-#define XPQ_CALL_COUNT 2
-#define PG_PRIV PG_AVAIL3
-typedef struct {
- unsigned long pt_ref;
- unsigned long pt_eip[XPQ_CALL_COUNT][XPQ_CALL_DEPTH];
-} pteinfo_t;
-
-extern pteinfo_t *pteinfo_list;
-#ifdef XENDEBUG_LOW
-#define __PRINTK(x) printk x
-#else
-#define __PRINTK(x)
-#endif
-
-char *xen_setbootenv(char *cmd_line);
-
-int xen_boothowto(char *envp);
-
-void _xen_machphys_update(vm_paddr_t, vm_paddr_t, char *file, int line);
-
-#ifdef INVARIANTS
-#define xen_machphys_update(a, b) _xen_machphys_update((a), (b), __FILE__, __LINE__)
-#else
-#define xen_machphys_update(a, b) _xen_machphys_update((a), (b), NULL, 0)
-#endif
-
-#ifndef XENHVM
-void xen_update_descriptor(union descriptor *, union descriptor *);
-#endif
-
-extern struct mtx balloon_lock;
-#if 0
-#define balloon_lock(__flags) mtx_lock_irqsave(&balloon_lock, __flags)
-#define balloon_unlock(__flags) mtx_unlock_irqrestore(&balloon_lock, __flags)
-#else
-#define balloon_lock(__flags) __flags = 1
-#define balloon_unlock(__flags) __flags = 0
-#endif
-
-
-
-#endif /* _XEN_XENFUNC_H_ */
--
2.7.4

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:37 AM2/20/17
to Osv Dev, Sergiy Kibrik
xenvar.h and xenpmap.h can be used for aarch64 as well without modification.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
bsd/sys/machine/xen/xenpmap.h | 227 ++++++++++++++++++++++++++++++++++++++++++
bsd/sys/machine/xen/xenvar.h | 120 ++++++++++++++++++++++
bsd/x64/machine/xen/xenpmap.h | 227 ------------------------------------------
bsd/x64/machine/xen/xenvar.h | 120 ----------------------
4 files changed, 347 insertions(+), 347 deletions(-)
create mode 100644 bsd/sys/machine/xen/xenpmap.h
create mode 100644 bsd/sys/machine/xen/xenvar.h
delete mode 100644 bsd/x64/machine/xen/xenpmap.h
delete mode 100644 bsd/x64/machine/xen/xenvar.h

diff --git a/bsd/sys/machine/xen/xenpmap.h b/bsd/sys/machine/xen/xenpmap.h
new file mode 100644
index 0000000..d768dad
--- /dev/null
+++ b/bsd/sys/machine/xen/xenpmap.h
@@ -0,0 +1,227 @@
+/*
+ *
+ * Copyright (c) 2004 Christian Limpach.
+ * Copyright (c) 2004,2005 Kip Macy
+ * 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. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Christian Limpach.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+ */
+
+
+#ifndef _XEN_XENPMAP_H_
+#define _XEN_XENPMAP_H_
+
+#include <machine/xen/features.h>
+
+void _xen_queue_pt_update(vm_paddr_t, vm_paddr_t, char *, int);
+void xen_pt_switch(vm_paddr_t);
+void xen_set_ldt(vm_paddr_t, unsigned long);
+void xen_pgdpt_pin(vm_paddr_t);
+void xen_pgd_pin(vm_paddr_t);
+void xen_pgd_unpin(vm_paddr_t);
+void xen_pt_pin(vm_paddr_t);
+void xen_pt_unpin(vm_paddr_t);
+void xen_flush_queue(void);
+void xen_check_queue(void);
+#if 0
+void pmap_ref(pt_entry_t *pte, vm_paddr_t ma);
+#endif
+
+#ifdef INVARIANTS
+#define xen_queue_pt_update(a, b) _xen_queue_pt_update((a), (b), __FILE__, __LINE__)
+#else
+#define xen_queue_pt_update(a, b) _xen_queue_pt_update((a), (b), NULL, 0)
+#endif
+
+#ifdef PMAP_DEBUG
+#define PMAP_REF pmap_ref
+#define PMAP_DEC_REF_PAGE pmap_dec_ref_page
+#define PMAP_MARK_PRIV pmap_mark_privileged
+#define PMAP_MARK_UNPRIV pmap_mark_unprivileged
+#else
+#define PMAP_MARK_PRIV(a)
+#define PMAP_MARK_UNPRIV(a)
+#define PMAP_REF(a, b)
+#define PMAP_DEC_REF_PAGE(a)
+#endif
+
+#define ALWAYS_SYNC 0
+
+#ifdef PT_DEBUG
+#define PT_LOG() printk("WP PT_SET %s:%d\n", __FILE__, __LINE__)
+#else
+#define PT_LOG()
+#endif
+
+#define INVALID_P2M_ENTRY (~0UL)
+
+#define pmap_valid_entry(E) ((E) & PG_V) /* is PDE or PTE valid? */
+
+#define SH_PD_SET_VA 1
+#define SH_PD_SET_VA_MA 2
+#define SH_PD_SET_VA_CLEAR 3
+
+struct pmap;
+void pd_set(struct pmap *pmap, int ptepindex, vm_paddr_t val, int type);
+#ifdef notyet
+static vm_paddr_t
+vptetomachpte(vm_paddr_t *pte)
+{
+ vm_offset_t offset, ppte;
+ vm_paddr_t pgoffset, retval, *pdir_shadow_ptr;
+ int pgindex;
+
+ ppte = (vm_offset_t)pte;
+ pgoffset = (ppte & PAGE_MASK);
+ offset = ppte - (vm_offset_t)PTmap;
+ pgindex = ppte >> PDRSHIFT;
+
+ pdir_shadow_ptr = (vm_paddr_t *)PCPU_GET(pdir_shadow);
+ retval = (pdir_shadow_ptr[pgindex] & ~PAGE_MASK) + pgoffset;
+ return (retval);
+}
+#endif
+#define PT_GET(_ptp) \
+ (pmap_valid_entry(*(_ptp)) ? xpmap_mtop(*(_ptp)) : (0))
+
+#ifdef WRITABLE_PAGETABLES
+
+#define PT_SET_VA(_ptp,_npte,sync) do { \
+ PMAP_REF((_ptp), xpmap_ptom(_npte)); \
+ PT_LOG(); \
+ *(_ptp) = xpmap_ptom((_npte)); \
+} while (/*CONSTCOND*/0)
+#define PT_SET_VA_MA(_ptp,_npte,sync) do { \
+ PMAP_REF((_ptp), (_npte)); \
+ PT_LOG(); \
+ *(_ptp) = (_npte); \
+} while (/*CONSTCOND*/0)
+#define PT_CLEAR_VA(_ptp, sync) do { \
+ PMAP_REF((pt_entry_t *)(_ptp), 0); \
+ PT_LOG(); \
+ *(_ptp) = 0; \
+} while (/*CONSTCOND*/0)
+
+#define PD_SET_VA(_pmap, _ptp, _npte, sync) do { \
+ PMAP_REF((_ptp), xpmap_ptom(_npte)); \
+ pd_set((_pmap),(_ptp),(_npte), SH_PD_SET_VA); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+#define PD_SET_VA_MA(_pmap, _ptp, _npte, sync) do { \
+ PMAP_REF((_ptp), (_npte)); \
+ pd_set((_pmap),(_ptp),(_npte), SH_PD_SET_VA_MA); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+#define PD_CLEAR_VA(_pmap, _ptp, sync) do { \
+ PMAP_REF((pt_entry_t *)(_ptp), 0); \
+ pd_set((_pmap),(_ptp), 0, SH_PD_SET_VA_CLEAR); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+
+#else /* !WRITABLE_PAGETABLES */
+
+#define PT_SET_VA(_ptp,_npte,sync) do { \
+ PMAP_REF((_ptp), xpmap_ptom(_npte)); \
+ xen_queue_pt_update(vtomach(_ptp), \
+ xpmap_ptom(_npte)); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+#define PT_SET_VA_MA(_ptp,_npte,sync) do { \
+ PMAP_REF((_ptp), (_npte)); \
+ xen_queue_pt_update(vtomach(_ptp), _npte); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+#define PT_CLEAR_VA(_ptp, sync) do { \
+ PMAP_REF((pt_entry_t *)(_ptp), 0); \
+ xen_queue_pt_update(vtomach(_ptp), 0); \
+ if (sync || ALWAYS_SYNC) \
+ xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+
+#define PD_SET_VA(_pmap, _ptepindex,_npte,sync) do { \
+ PMAP_REF((_ptp), xpmap_ptom(_npte)); \
+ pd_set((_pmap),(_ptepindex),(_npte), SH_PD_SET_VA); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+#define PD_SET_VA_MA(_pmap, _ptepindex,_npte,sync) do { \
+ PMAP_REF((_ptp), (_npte)); \
+ pd_set((_pmap),(_ptepindex),(_npte), SH_PD_SET_VA_MA); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+#define PD_CLEAR_VA(_pmap, _ptepindex, sync) do { \
+ PMAP_REF((pt_entry_t *)(_ptp), 0); \
+ pd_set((_pmap),(_ptepindex), 0, SH_PD_SET_VA_CLEAR); \
+ if (sync || ALWAYS_SYNC) xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+
+#endif
+
+#define PT_SET_MA(_va, _ma) \
+do { \
+ PANIC_IF(HYPERVISOR_update_va_mapping(((unsigned long)(_va)),\
+ (_ma), \
+ UVMF_INVLPG| UVMF_ALL) < 0); \
+} while (/*CONSTCOND*/0)
+
+#define PT_UPDATES_FLUSH() do { \
+ xen_flush_queue(); \
+} while (/*CONSTCOND*/0)
+
+static __inline vm_paddr_t
+xpmap_mtop(vm_paddr_t mpa)
+{
+ vm_paddr_t tmp = (mpa & PG_FRAME);
+
+ return machtophys(tmp) | (mpa & ~PG_FRAME);
+}
+
+static __inline vm_paddr_t
+xpmap_ptom(vm_paddr_t ppa)
+{
+ vm_paddr_t tmp = (ppa & PG_FRAME);
+
+ return phystomach(tmp) | (ppa & ~PG_FRAME);
+}
+
+static __inline void
+set_phys_to_machine(unsigned long pfn, unsigned long mfn)
+{
+#ifdef notyet
+ PANIC_IF(max_mapnr && pfn >= max_mapnr);
+#endif
+ if (xen_feature(XENFEAT_auto_translated_physmap)) {
+#ifdef notyet
+ PANIC_IF((pfn != mfn && mfn != INVALID_P2M_ENTRY));
+#endif
+ return;
+ }
+ xen_phys_machine[pfn] = mfn;
+}
+
+
+
+
+#endif /* _XEN_XENPMAP_H_ */
diff --git a/bsd/sys/machine/xen/xenvar.h b/bsd/sys/machine/xen/xenvar.h
new file mode 100644
index 0000000..d9dbc5d
--- /dev/null
+++ b/bsd/sys/machine/xen/xenvar.h
@@ -0,0 +1,120 @@
+/*-
+ * Copyright (c) 2008 Kip Macy
+ * 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.
+ *
+ * $FreeBSD$
+ */
+#ifndef XENVAR_H_
+#define XENVAR_H_
+#define XBOOTUP 0x1
+#define XPMAP 0x2
+extern int xendebug_flags;
+#ifndef NOXENDEBUG
+#define XENPRINTF printk
+#else
+#define XENPRINTF printf
+#endif
+#include <xen/features.h>
+
+#if 0
+#define TRACE_ENTER XENPRINTF("(file=%s, line=%d) entered %s\n", __FILE__, __LINE__, __FUNCTION__)
+#define TRACE_EXIT XENPRINTF("(file=%s, line=%d) exiting %s\n", __FILE__, __LINE__, __FUNCTION__)
+#define TRACE_DEBUG(argflags, _f, _a...) \
+if (xendebug_flags & argflags) XENPRINTF("(file=%s, line=%d) " _f "\n", __FILE__, __LINE__, ## _a);
+#else
+#define TRACE_ENTER
+#define TRACE_EXIT
+#define TRACE_DEBUG(argflags, _f, _a...)
+#endif
+
+#ifdef XENHVM
+
+static inline vm_paddr_t
+phystomach(vm_paddr_t pa)
+{
+
+ return (pa);
+}
+
+static inline vm_paddr_t
+machtophys(vm_paddr_t ma)
+{
+
+ return (ma);
+}
+
+#define vtomach(va) pmap_kextract((vm_offset_t) (va))
+#define PFNTOMFN(pa) (pa)
+#define MFNTOPFN(ma) (ma)
+
+#define set_phys_to_machine(pfn, mfn) ((void)0)
+#define phys_to_machine_mapping_valid(pfn) (TRUE)
+#define PT_UPDATES_FLUSH() ((void)0)
+
+#else
+
+extern xen_pfn_t *xen_phys_machine;
+
+
+extern xen_pfn_t *xen_machine_phys;
+/* Xen starts physical pages after the 4MB ISA hole -
+ * FreeBSD doesn't
+ */
+
+
+#undef ADD_ISA_HOLE /* XXX */
+
+#ifdef ADD_ISA_HOLE
+#define ISA_INDEX_OFFSET 1024
+#define ISA_PDR_OFFSET 1
+#else
+#define ISA_INDEX_OFFSET 0
+#define ISA_PDR_OFFSET 0
+#endif
+
+
+#define PFNTOMFN(i) (xen_phys_machine[(i)])
+#define MFNTOPFN(i) ((vm_paddr_t)xen_machine_phys[(i)])
+
+#define VTOP(x) ((((uintptr_t)(x))) - KERNBASE)
+#define PTOV(x) (((uintptr_t)(x)) + KERNBASE)
+
+#define VTOPFN(x) (VTOP(x) >> PAGE_SHIFT)
+#define PFNTOV(x) PTOV((vm_paddr_t)(x) << PAGE_SHIFT)
+
+#define VTOMFN(va) (vtomach(va) >> PAGE_SHIFT)
+#define PFN_UP(x) (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
+
+#define phystomach(pa) (((vm_paddr_t)(PFNTOMFN((pa) >> PAGE_SHIFT))) << PAGE_SHIFT)
+#define machtophys(ma) (((vm_paddr_t)(MFNTOPFN((ma) >> PAGE_SHIFT))) << PAGE_SHIFT)
+
+#endif
+
+void xpq_init(void);
+
+int xen_create_contiguous_region(vm_page_t pages, int npages);
+
+void xen_destroy_contiguous_region(void * addr, int npages);
+
+#endif
diff --git a/bsd/x64/machine/xen/xenpmap.h b/bsd/x64/machine/xen/xenpmap.h
deleted file mode 100644
index d768dad..0000000
--- a/bsd/x64/machine/xen/xenpmap.h
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- *
- * Copyright (c) 2004 Christian Limpach.
- * Copyright (c) 2004,2005 Kip Macy
- * 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. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by Christian Limpach.
- * 4. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
- */
-
-
-#ifndef _XEN_XENPMAP_H_
-#define _XEN_XENPMAP_H_
-
-#include <machine/xen/features.h>
-
-void _xen_queue_pt_update(vm_paddr_t, vm_paddr_t, char *, int);
-void xen_pt_switch(vm_paddr_t);
-void xen_set_ldt(vm_paddr_t, unsigned long);
-void xen_pgdpt_pin(vm_paddr_t);
-void xen_pgd_pin(vm_paddr_t);
-void xen_pgd_unpin(vm_paddr_t);
-void xen_pt_pin(vm_paddr_t);
-void xen_pt_unpin(vm_paddr_t);
-void xen_flush_queue(void);
-void xen_check_queue(void);
-#if 0
-void pmap_ref(pt_entry_t *pte, vm_paddr_t ma);
-#endif
-
-#ifdef INVARIANTS
-#define xen_queue_pt_update(a, b) _xen_queue_pt_update((a), (b), __FILE__, __LINE__)
-#else
-#define xen_queue_pt_update(a, b) _xen_queue_pt_update((a), (b), NULL, 0)
-#endif
-
-#ifdef PMAP_DEBUG
-#define PMAP_REF pmap_ref
-#define PMAP_DEC_REF_PAGE pmap_dec_ref_page
-#define PMAP_MARK_PRIV pmap_mark_privileged
-#define PMAP_MARK_UNPRIV pmap_mark_unprivileged
-#else
-#define PMAP_MARK_PRIV(a)
-#define PMAP_MARK_UNPRIV(a)
-#define PMAP_REF(a, b)
-#define PMAP_DEC_REF_PAGE(a)
-#endif
-
-#define ALWAYS_SYNC 0
-
-#ifdef PT_DEBUG
-#define PT_LOG() printk("WP PT_SET %s:%d\n", __FILE__, __LINE__)
-#else
-#define PT_LOG()
-#endif
-
-#define INVALID_P2M_ENTRY (~0UL)
-
-#define pmap_valid_entry(E) ((E) & PG_V) /* is PDE or PTE valid? */
-
-#define SH_PD_SET_VA 1
-#define SH_PD_SET_VA_MA 2
-#define SH_PD_SET_VA_CLEAR 3
-
-struct pmap;
-void pd_set(struct pmap *pmap, int ptepindex, vm_paddr_t val, int type);
-#ifdef notyet
-static vm_paddr_t
-vptetomachpte(vm_paddr_t *pte)
-{
- vm_offset_t offset, ppte;
- vm_paddr_t pgoffset, retval, *pdir_shadow_ptr;
- int pgindex;
-
- ppte = (vm_offset_t)pte;
- pgoffset = (ppte & PAGE_MASK);
- offset = ppte - (vm_offset_t)PTmap;
- pgindex = ppte >> PDRSHIFT;
-
- pdir_shadow_ptr = (vm_paddr_t *)PCPU_GET(pdir_shadow);
- retval = (pdir_shadow_ptr[pgindex] & ~PAGE_MASK) + pgoffset;
- return (retval);
-}
-#endif
-#define PT_GET(_ptp) \
- (pmap_valid_entry(*(_ptp)) ? xpmap_mtop(*(_ptp)) : (0))
-
-#ifdef WRITABLE_PAGETABLES
-
-#define PT_SET_VA(_ptp,_npte,sync) do { \
- PMAP_REF((_ptp), xpmap_ptom(_npte)); \
- PT_LOG(); \
- *(_ptp) = xpmap_ptom((_npte)); \
-} while (/*CONSTCOND*/0)
-#define PT_SET_VA_MA(_ptp,_npte,sync) do { \
- PMAP_REF((_ptp), (_npte)); \
- PT_LOG(); \
- *(_ptp) = (_npte); \
-} while (/*CONSTCOND*/0)
-#define PT_CLEAR_VA(_ptp, sync) do { \
- PMAP_REF((pt_entry_t *)(_ptp), 0); \
- PT_LOG(); \
- *(_ptp) = 0; \
-} while (/*CONSTCOND*/0)
-
-#define PD_SET_VA(_pmap, _ptp, _npte, sync) do { \
- PMAP_REF((_ptp), xpmap_ptom(_npte)); \
- pd_set((_pmap),(_ptp),(_npte), SH_PD_SET_VA); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-#define PD_SET_VA_MA(_pmap, _ptp, _npte, sync) do { \
- PMAP_REF((_ptp), (_npte)); \
- pd_set((_pmap),(_ptp),(_npte), SH_PD_SET_VA_MA); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-#define PD_CLEAR_VA(_pmap, _ptp, sync) do { \
- PMAP_REF((pt_entry_t *)(_ptp), 0); \
- pd_set((_pmap),(_ptp), 0, SH_PD_SET_VA_CLEAR); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-
-#else /* !WRITABLE_PAGETABLES */
-
-#define PT_SET_VA(_ptp,_npte,sync) do { \
- PMAP_REF((_ptp), xpmap_ptom(_npte)); \
- xen_queue_pt_update(vtomach(_ptp), \
- xpmap_ptom(_npte)); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-#define PT_SET_VA_MA(_ptp,_npte,sync) do { \
- PMAP_REF((_ptp), (_npte)); \
- xen_queue_pt_update(vtomach(_ptp), _npte); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-#define PT_CLEAR_VA(_ptp, sync) do { \
- PMAP_REF((pt_entry_t *)(_ptp), 0); \
- xen_queue_pt_update(vtomach(_ptp), 0); \
- if (sync || ALWAYS_SYNC) \
- xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-
-#define PD_SET_VA(_pmap, _ptepindex,_npte,sync) do { \
- PMAP_REF((_ptp), xpmap_ptom(_npte)); \
- pd_set((_pmap),(_ptepindex),(_npte), SH_PD_SET_VA); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-#define PD_SET_VA_MA(_pmap, _ptepindex,_npte,sync) do { \
- PMAP_REF((_ptp), (_npte)); \
- pd_set((_pmap),(_ptepindex),(_npte), SH_PD_SET_VA_MA); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-#define PD_CLEAR_VA(_pmap, _ptepindex, sync) do { \
- PMAP_REF((pt_entry_t *)(_ptp), 0); \
- pd_set((_pmap),(_ptepindex), 0, SH_PD_SET_VA_CLEAR); \
- if (sync || ALWAYS_SYNC) xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-
-#endif
-
-#define PT_SET_MA(_va, _ma) \
-do { \
- PANIC_IF(HYPERVISOR_update_va_mapping(((unsigned long)(_va)),\
- (_ma), \
- UVMF_INVLPG| UVMF_ALL) < 0); \
-} while (/*CONSTCOND*/0)
-
-#define PT_UPDATES_FLUSH() do { \
- xen_flush_queue(); \
-} while (/*CONSTCOND*/0)
-
-static __inline vm_paddr_t
-xpmap_mtop(vm_paddr_t mpa)
-{
- vm_paddr_t tmp = (mpa & PG_FRAME);
-
- return machtophys(tmp) | (mpa & ~PG_FRAME);
-}
-
-static __inline vm_paddr_t
-xpmap_ptom(vm_paddr_t ppa)
-{
- vm_paddr_t tmp = (ppa & PG_FRAME);
-
- return phystomach(tmp) | (ppa & ~PG_FRAME);
-}
-
-static __inline void
-set_phys_to_machine(unsigned long pfn, unsigned long mfn)
-{
-#ifdef notyet
- PANIC_IF(max_mapnr && pfn >= max_mapnr);
-#endif
- if (xen_feature(XENFEAT_auto_translated_physmap)) {
-#ifdef notyet
- PANIC_IF((pfn != mfn && mfn != INVALID_P2M_ENTRY));
-#endif
- return;
- }
- xen_phys_machine[pfn] = mfn;
-}
-
-
-
-
-#endif /* _XEN_XENPMAP_H_ */
diff --git a/bsd/x64/machine/xen/xenvar.h b/bsd/x64/machine/xen/xenvar.h
deleted file mode 100644
index d9dbc5d..0000000
--- a/bsd/x64/machine/xen/xenvar.h
+++ /dev/null
@@ -1,120 +0,0 @@
-/*-
- * Copyright (c) 2008 Kip Macy
-#ifndef XENVAR_H_
-#define XENVAR_H_
-#define XBOOTUP 0x1
-#define XPMAP 0x2
-extern int xendebug_flags;
-#ifndef NOXENDEBUG
-#define XENPRINTF printk
-#else
-#define XENPRINTF printf
-#endif
-#include <xen/features.h>
-
-#if 0
-#define TRACE_ENTER XENPRINTF("(file=%s, line=%d) entered %s\n", __FILE__, __LINE__, __FUNCTION__)
-#define TRACE_EXIT XENPRINTF("(file=%s, line=%d) exiting %s\n", __FILE__, __LINE__, __FUNCTION__)
-#define TRACE_DEBUG(argflags, _f, _a...) \
-if (xendebug_flags & argflags) XENPRINTF("(file=%s, line=%d) " _f "\n", __FILE__, __LINE__, ## _a);
-#else
-#define TRACE_ENTER
-#define TRACE_EXIT
-#define TRACE_DEBUG(argflags, _f, _a...)
-#endif
-
-#ifdef XENHVM
-
-static inline vm_paddr_t
-phystomach(vm_paddr_t pa)
-{
-
- return (pa);
-}
-
-static inline vm_paddr_t
-machtophys(vm_paddr_t ma)
-{
-
- return (ma);
-}
-
-#define vtomach(va) pmap_kextract((vm_offset_t) (va))
-#define PFNTOMFN(pa) (pa)
-#define MFNTOPFN(ma) (ma)
-
-#define set_phys_to_machine(pfn, mfn) ((void)0)
-#define phys_to_machine_mapping_valid(pfn) (TRUE)
-#define PT_UPDATES_FLUSH() ((void)0)
-
-#else
-
-extern xen_pfn_t *xen_phys_machine;
-
-
-extern xen_pfn_t *xen_machine_phys;
-/* Xen starts physical pages after the 4MB ISA hole -
- * FreeBSD doesn't
- */
-
-
-#undef ADD_ISA_HOLE /* XXX */
-
-#ifdef ADD_ISA_HOLE
-#define ISA_INDEX_OFFSET 1024
-#define ISA_PDR_OFFSET 1
-#else
-#define ISA_INDEX_OFFSET 0
-#define ISA_PDR_OFFSET 0
-#endif
-
-
-#define PFNTOMFN(i) (xen_phys_machine[(i)])
-#define MFNTOPFN(i) ((vm_paddr_t)xen_machine_phys[(i)])
-
-#define VTOP(x) ((((uintptr_t)(x))) - KERNBASE)
-#define PTOV(x) (((uintptr_t)(x)) + KERNBASE)
-
-#define VTOPFN(x) (VTOP(x) >> PAGE_SHIFT)
-#define PFNTOV(x) PTOV((vm_paddr_t)(x) << PAGE_SHIFT)
-
-#define VTOMFN(va) (vtomach(va) >> PAGE_SHIFT)
-#define PFN_UP(x) (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
-
-#define phystomach(pa) (((vm_paddr_t)(PFNTOMFN((pa) >> PAGE_SHIFT))) << PAGE_SHIFT)
-#define machtophys(ma) (((vm_paddr_t)(MFNTOPFN((ma) >> PAGE_SHIFT))) << PAGE_SHIFT)
-
-#endif
-
-void xpq_init(void);
-
-int xen_create_contiguous_region(vm_page_t pages, int npages);
-
-void xen_destroy_contiguous_region(void * addr, int npages);
-
-#endif
--
2.7.4

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:39 AM2/20/17
to Osv Dev, Sergiy Kibrik
evtchn and gnttab drivers use these operations extensively.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
bsd/aarch64/machine/xen/synch_bitops.h | 59 +++++++++++++++
bsd/aarch64/machine/xen/xen-os.h | 132 +++++++++++++++++++++++++++++++++
2 files changed, 191 insertions(+)
create mode 100644 bsd/aarch64/machine/xen/synch_bitops.h
create mode 100644 bsd/aarch64/machine/xen/xen-os.h

diff --git a/bsd/aarch64/machine/xen/synch_bitops.h b/bsd/aarch64/machine/xen/synch_bitops.h
new file mode 100644
index 0000000..a9d0d2c
--- /dev/null
+++ b/bsd/aarch64/machine/xen/synch_bitops.h
@@ -0,0 +1,59 @@
+#ifndef __XEN_SYNCH_BITOPS_H__
+#define __XEN_SYNCH_BITOPS_H__
+
+/*
+ * Based on include/arm/os.h from Xen Mini-OS priject
+ */
+
+/* If *ptr == old, then store new there (and return new).
+ * Otherwise, return the old value.
+ * Atomic. */
+#define synch_cmpxchg(ptr, old, new) \
+({ __typeof__(*ptr) stored = old; \
+ __atomic_compare_exchange_n(ptr, &stored, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? new : old; \
+})
+
+/* As test_and_clear_bit, but using __ATOMIC_SEQ_CST */
+static __inline__ int synch_test_and_clear_bit(int nr, volatile void *addr)
+{
+ uint8_t *byte = ((uint8_t *)addr) + (nr >> 3);
+ uint8_t bit = 1 << (nr & 7);
+ uint8_t orig;
+
+ orig = __atomic_fetch_and(byte, ~bit, __ATOMIC_SEQ_CST);
+
+ return (orig & bit) != 0;
+}
+
+/* As test_and_set_bit, but using __ATOMIC_SEQ_CST */
+static __inline__ int synch_test_and_set_bit(int nr, volatile void *base)
+{
+ uint8_t *byte = ((uint8_t *)base) + (nr >> 3);
+ uint8_t bit = 1 << (nr & 7);
+ uint8_t orig;
+
+ orig = __atomic_fetch_or(byte, bit, __ATOMIC_SEQ_CST);
+
+ return (orig & bit) != 0;
+}
+
+static __inline__ void synch_set_bit(int nr, volatile void * addr)
+{
+ synch_test_and_set_bit(nr, addr);
+}
+
+static __inline__ void synch_clear_bit(int nr, volatile void * addr)
+{
+ synch_test_and_clear_bit(nr, addr);
+}
+
+/* As test_bit, but with a following memory barrier. */
+static __inline__ int synch_test_bit(int nr, volatile void *addr)
+{
+ int result;
+ result = test_bit(nr, addr);
+ barrier();
+ return result;
+}
+
+#endif /* __XEN_SYNCH_BITOPS_H__ */
diff --git a/bsd/aarch64/machine/xen/xen-os.h b/bsd/aarch64/machine/xen/xen-os.h
new file mode 100644
index 0000000..6a5defb
--- /dev/null
+++ b/bsd/aarch64/machine/xen/xen-os.h
@@ -0,0 +1,132 @@
+/*
+ * Based on x64/machine/xen/xen-os.h and arm/os.h from Xen Mini OS
+ *
+ * Copyright (C) 2017 Sergiy Kibrik <sergiy...@globallogic.com>
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+#ifndef _XEN_OS_H_
+#define _XEN_OS_H_
+__BEGIN_DECLS
+
+#if !defined(__XEN_INTERFACE_VERSION__)
+/*
+ * Can update to a more recent version when we implement
+ * the hypercall page
+ */
+#define __XEN_INTERFACE_VERSION__ 0x00030204
+#endif
+
+#include <xen/interface/xen.h>
+
+extern shared_info_t *HYPERVISOR_shared_info;
+
+static inline void rep_nop(void)
+{
+ __asm__ __volatile__ ( "yield" : : : "memory" );
+}
+#define cpu_relax() rep_nop()
+
+#ifndef __ASSEMBLY__
+#define likely(x) __builtin_expect((x),1)
+#define unlikely(x) __builtin_expect((x),0)
+
+#ifndef mb
+#define mb() __asm__ __volatile__("dsb sy":::"memory")
+#endif
+#ifndef rmb
+#define rmb() __asm__ __volatile__("dsb ld":::"memory");
+#endif
+#ifndef wmb
+#define wmb() __asm__ __volatile__("dsb st":::"memory");
+#endif
+#ifdef SMP
+#define smp_mb() mb()
+#define smp_rmb() rmb()
+#define smp_wmb() wmb()
+#define smp_read_barrier_depends() read_barrier_depends()
+#define set_mb(var, value) do { xchg(&var, value); } while (0)
+#else
+#define smp_mb() barrier()
+#define smp_rmb() barrier()
+#define smp_wmb() barrier()
+#define smp_read_barrier_depends() do { } while(0)
+#define set_mb(var, value) do { var = value; barrier(); } while (0)
+#endif
+
+/* This is a barrier for the compiler only, NOT the processor! */
+#define barrier() __asm__ __volatile__("": : :"memory")
+/**
+ * test_and_clear_bit - Clear a bit and return its old value
+ * @nr: Bit to set
+ * @addr: Address to count from
+ *
+ * This operation is atomic only.
+ * use synch_test_and_clear_bit() if you need barrier.
+ */
+static __inline int test_and_clear_bit(int nr, volatile void * addr)
+{
+ uint8_t *byte = ((uint8_t *)addr) + (nr >> 3);
+ uint8_t bit = 1 << (nr & 7);
+ uint8_t orig;
+
+ orig = __atomic_fetch_and(byte, ~bit, __ATOMIC_RELAXED);
+
+ return (orig & bit) != 0;
+}
+
+/**
+ * Atomically set a bit and return the old value.
+ * Similar to test_and_clear_bit.
+ */
+static __inline__ int test_and_set_bit(int nr, volatile void *base)
+{
+ uint8_t *byte = ((uint8_t *)base) + (nr >> 3);
+ uint8_t bit = 1 << (nr & 7);
+ uint8_t orig;
+
+ orig = __atomic_fetch_or(byte, bit, __ATOMIC_RELAXED);
+
+ return (orig & bit) != 0;
+}
+
+
+/**
+ * Test whether a bit is set. */
+static __inline__ int test_bit(int nr, const volatile void *addr)
+{
+ const uint8_t *ptr = (const uint8_t *) addr;
+ return ((1 << (nr & 7)) & (ptr[nr >> 3])) != 0;
+}
+
+/**
+ * set_bit - Atomically set a bit in memory
+ * @nr: the bit to set
+ * @addr: the address to start counting from
+ *
+ * This function is atomic only.
+ * Use synch_set_bit() if you need a barrier.
+ */
+static __inline__ void set_bit(int nr, volatile void * addr)
+{
+ test_and_set_bit(nr, addr);
+}
+
+/**
+ * clear_bit - Clears a bit in memory
+ * @nr: Bit to clear
+ * @addr: Address to start counting from
+ *
+ * clear_bit() is atomic only.
+ * Use synch_clear_bit() if you need a barrier.
+ */
+static __inline__ void clear_bit(int nr, volatile void * addr)
+{
+ test_and_clear_bit(nr, addr);
+}
+
+#endif /* !__ASSEMBLY__ */
+__END_DECLS
+#endif /* _XEN_OS_H_ */
--
2.7.4

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:41 AM2/20/17
to Osv Dev, Sergiy Kibrik
Semantics of bsrq()/fls() differs on x64 and aarch64, e.g. following sample
inputs produce different results:

x64: aarch64:

bsrq(0x1) = 0 bsrq(0x1) = 63
bsrq(0xffffffffffffffff) = 63 bsrq(0xffffffffffffffff) = 0
bsrq(0x8000000000000000) = 63 bsrq(0x8000000000000000) = 0
bsrq(0x80000000) = 31 bsrq(0x80000000) = 32
bsrq(0x8008) = 15 bsrq(0x8008) = 48

bsrl(0x1) = 0 bsrl(0x1) = 63
bsrl(0xffffffff) = 31 bsrl(0xffffffff) = 32
bsrl(0x80000000) = 31 bsrl(0x80000000) = 32
bsrl(0x8008) = 15 bsrl(0x8008) = 48

fsl(0x1) = 1 fsl(0x1) = 64
fsl(0xffffffff) = 32 fsl(0xffffffff) = 33
fsl(0x80000000) = 32 fsl(0x80000000) = 33
fsl(0x8008) = 16 fsl(0x8008) = 49

Which is not quite a behaviour bitops user expects.
Use GCC built-ins to overcome this, which provide consistent behaviour across
all platforms. Arch-specific instructions generated to avoid loops, so this
should not hurt performance.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
arch/aarch64/bitops.h | 31 -------------------------------
arch/x64/bitops.h | 31 -------------------------------
bsd/porting/netport.h | 2 +-
core/xen_intr.cc | 2 +-
include/osv/bitops.h | 27 +++++++++++++++++++++++++++
5 files changed, 29 insertions(+), 64 deletions(-)
delete mode 100644 arch/aarch64/bitops.h
delete mode 100644 arch/x64/bitops.h
create mode 100644 include/osv/bitops.h

diff --git a/arch/aarch64/bitops.h b/arch/aarch64/bitops.h
deleted file mode 100644
index 0e04197..0000000
--- a/arch/aarch64/bitops.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2014 Huawei Technologies Duesseldorf GmbH
- *
- * This work is open source software, licensed under the terms of the
- * BSD license as described in the LICENSE file in the top-level directory.
- */
-
-#ifndef ARCH_BITOPS_H_
-#define ARCH_BITOPS_H_
-static inline unsigned int
-bsrl(unsigned int mask)
-{
- unsigned int result;
- asm volatile("clz %1,%0" : "=r" (result) : "r" (mask));
- return result;
-}
-
-static inline unsigned long
-bsrq(unsigned long mask)
-{
- unsigned long result;
- asm volatile("clz %1,%0" : "=r" (result) : "r" (mask));
- return result;
-}
-
-static inline int
-fls(int mask)
-{
- return (mask == 0 ? mask : (int)bsrl((unsigned int)mask) + 1);
-}
-#endif /* ARCH_BITOPS_H */
diff --git a/arch/x64/bitops.h b/arch/x64/bitops.h
deleted file mode 100644
index b5f77da..0000000
--- a/arch/x64/bitops.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2013 Cloudius Systems, Ltd.
- *
- * This work is open source software, licensed under the terms of the
- * BSD license as described in the LICENSE file in the top-level directory.
- */
-
-#ifndef ARCH_BITOPS_H_
-#define ARCH_BITOPS_H_
-static inline unsigned int
-bsrl(unsigned int mask)
-{
- unsigned int result;
- asm volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
- return result;
-}
-
-static inline unsigned long
-bsrq(unsigned long mask)
-{
- unsigned long result;
- asm volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
- return result;
-}
-
-static inline int
-fls(int mask)
-{
- return (mask == 0 ? mask : (int)bsrl((unsigned int)mask) + 1);
-}
-#endif
diff --git a/bsd/porting/netport.h b/bsd/porting/netport.h
index 784298c..2604a4a 100644
--- a/bsd/porting/netport.h
+++ b/bsd/porting/netport.h
@@ -19,7 +19,7 @@
#include <osv/debug.h>
#define __NEED_socklen_t
#include <bits/alltypes.h>
-#include "bitops.h"
+#include <osv/bitops.h>

__BEGIN_DECLS

diff --git a/core/xen_intr.cc b/core/xen_intr.cc
index 45a1869..6ea52e5 100644
--- a/core/xen_intr.cc
+++ b/core/xen_intr.cc
@@ -9,7 +9,7 @@
#include <osv/xen_intr.hh>
#include <bsd/porting/bus.h>
#include <machine/intr_machdep.h>
-#include "bitops.h"
+#include <osv/bitops.h>
#include <osv/debug.hh>

#include <osv/trace.hh>
diff --git a/include/osv/bitops.h b/include/osv/bitops.h
new file mode 100644
index 0000000..82b1b93
--- /dev/null
+++ b/include/osv/bitops.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2013 Cloudius Systems, Ltd.
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+#ifndef ARCH_BITOPS_H_
+#define ARCH_BITOPS_H_
+static inline unsigned int
+bsrl(unsigned int mask)
+{
+ return sizeof(mask) * 8 - __builtin_clz(mask) - 1;
+}
+
+static inline unsigned long
+bsrq(unsigned long mask)
+{
+ return sizeof(mask) * 8 - __builtin_clzl(mask) - 1;
+}
+
+static inline int
+fls(int mask)
+{
+ return (mask == 0 ? mask : (int)bsrl((unsigned int)mask) + 1);
+}
+#endif
--
2.7.4

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:44 AM2/20/17
to Osv Dev, Sergiy Kibrik
Replace local declaration of unmask_evtchn() and evtchn_from_irq()
with inclusion of evtchn.h where they're declared.

Additionally declare evtchn_from_irq() in header,
as it's used in Xen core interrupt handler.

Include evtchn driver into common build, so that aarch64 code can link.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
Makefile | 2 +-
bsd/sys/xen/evtchn.cc | 1 -
bsd/sys/xen/evtchn.h | 2 ++
core/xen_intr.cc | 20 +++-----------------
4 files changed, 6 insertions(+), 19 deletions(-)

diff --git a/Makefile b/Makefile
index 60404ef..6846c5e 100644
--- a/Makefile
+++ b/Makefile
@@ -595,11 +595,11 @@ bsd += bsd/sys/netinet/arpcache.o
bsd += bsd/sys/xdr/xdr.o
bsd += bsd/sys/xdr/xdr_array.o
bsd += bsd/sys/xdr/xdr_mem.o
+bsd += bsd/sys/xen/evtchn.o

ifeq ($(arch),x64)
$(out)/bsd/%.o: COMMON += -DXEN -DXENHVM
bsd += bsd/sys/xen/gnttab.o
-bsd += bsd/sys/xen/evtchn.o
bsd += bsd/sys/xen/xenstore/xenstore.o
bsd += bsd/sys/xen/xenbus/xenbus.o
bsd += bsd/sys/xen/xenbus/xenbusb.o
diff --git a/bsd/sys/xen/evtchn.cc b/bsd/sys/xen/evtchn.cc
index 825695c..a33ecf6 100644
--- a/bsd/sys/xen/evtchn.cc
+++ b/bsd/sys/xen/evtchn.cc
@@ -31,7 +31,6 @@ __FBSDID("$FreeBSD$");
#include <machine/intr_machdep.h>

#include <machine/xen/xen-os.h>
-#include <machine/xen/xenvar.h>
#include <xen/xen_intr.h>
#include <machine/xen/synch_bitops.h>
#include <xen/evtchn.h>
diff --git a/bsd/sys/xen/evtchn.h b/bsd/sys/xen/evtchn.h
index cc231a7..0891a00 100644
--- a/bsd/sys/xen/evtchn.h
+++ b/bsd/sys/xen/evtchn.h
@@ -30,6 +30,8 @@ void mask_evtchn(int port);

void unmask_evtchn(int port);

+int evtchn_from_irq(int irq);
+
#ifdef SMP
void rebind_evtchn_to_cpu(int port, unsigned int cpu);
#else
diff --git a/core/xen_intr.cc b/core/xen_intr.cc
index 6ea52e5..7a9dad2 100644
--- a/core/xen_intr.cc
+++ b/core/xen_intr.cc
@@ -8,7 +8,10 @@
#include <osv/xen.hh>
#include <osv/xen_intr.hh>
#include <bsd/porting/bus.h>
+#include <bsd/porting/netport.h>
#include <machine/intr_machdep.h>
+#include <machine/xen/xen-os.h>
+#include <xen/evtchn.h>
#include <osv/bitops.h>
#include <osv/debug.hh>

@@ -21,23 +24,6 @@ TRACEPOINT(trace_xen_irq_ret, "");
TRACEPOINT(trace_xen_irq_exec, "");
TRACEPOINT(trace_xen_irq_exec_ret, "");

-/*FIXME: use xen/evtchn.h when aarch64 supprted */
-#if defined(__x86_64__)
-void unmask_evtchn(int vector);
-int evtchn_from_irq(int irq);
-#else
-void unmask_evtchn(int port)
-{
-}
-
-int evtchn_from_irq(int irq)
-{
- abort("Not implemented yet");
- return 0; /* keep compiler happy */
-}
-
-#endif
-
namespace xen {

PERCPU(sched::thread *, xen_irq::_thread);
--
2.7.4

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:46 AM2/20/17
to Osv Dev, Sergiy Kibrik
unmask_evtchn() has side effect of setting evtchn_upcall_pending and
triggering interrupt from Xen if unmasked port is pending.
This works fine when unmasking new port, but in case of calling it from
IRQ handler can lead to race and lost interrupt.

do_irq() already handles pending interrupts, there's no need to check pending ports
and mess with interrupt handler logic, simply set mask bitfield and continue.

This fixes lost interrupt on aarch64 platform.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
core/xen_intr.cc | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/core/xen_intr.cc b/core/xen_intr.cc
index 7a9dad2..19131d3 100644
--- a/core/xen_intr.cc
+++ b/core/xen_intr.cc
@@ -96,7 +96,7 @@ void xen_irq::do_irq()
void *arg = xen_allocated_irqs[port].arg;
xen_shared_info.evtchn_pending[l1i].fetch_and(~(1ULL << l2i));
xen_allocated_irqs[port].handler(arg);
- unmask_evtchn(port);
+ xen_shared_info.evtchn_mask[l1i].fetch_and(~(1ULL << l2i));
}
}
trace_xen_irq_exec_ret();
--
2.7.4

Sergiy Kibrik

unread,
Feb 20, 2017, 8:38:49 AM2/20/17
to Osv Dev, Sergiy Kibrik
xen_shared_info & xen_vcpu_info are different on x64/aarch64, so couple of fixes
needed so that shared data is consistent with what hypervisor presumes.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
bsd/sys/xen/interface/arch-aarch64.h | 1 +
include/osv/xen.hh | 5 ++---
2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/bsd/sys/xen/interface/arch-aarch64.h b/bsd/sys/xen/interface/arch-aarch64.h
index 998cd5e..ca47783 100644
--- a/bsd/sys/xen/interface/arch-aarch64.h
+++ b/bsd/sys/xen/interface/arch-aarch64.h
@@ -37,6 +37,7 @@ typedef uint64_t xen_ulong_t;
#define PRI_xen_ulong "llx"

struct arch_vcpu_info {
+ int empty[0]; /* force zero size */
};
typedef struct arch_vcpu_info arch_vcpu_info_t;

diff --git a/include/osv/xen.hh b/include/osv/xen.hh
index 4c38447..504720b 100644
--- a/include/osv/xen.hh
+++ b/include/osv/xen.hh
@@ -30,13 +30,12 @@ struct xen_vcpu_info {
uint8_t evtchn_upcall_pending;
uint8_t evtchn_upcall_mask;
std::atomic<uint64_t> evtchn_pending_sel;
- uint64_t cr2;
- uint64_t pad;
+ arch_vcpu_info_t arch;
pvclock_vcpu_time_info time;
};

struct xen_shared_info {
- struct xen_vcpu_info vcpu_info[32];
+ struct xen_vcpu_info vcpu_info[MAX_VIRT_CPUS];

std::atomic<unsigned long> evtchn_pending[sizeof(unsigned long) * 8];
std::atomic<unsigned long> evtchn_mask[sizeof(unsigned long) * 8];
--
2.7.4

Nadav Har'El

unread,
Feb 21, 2017, 11:17:02 AM2/21/17
to Sergiy Kibrik, Osv Dev
On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com> wrote:
evtchn and gnttab drivers use these operations extensively.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
 bsd/aarch64/machine/xen/synch_bitops.h |  59 +++++++++++++++
 bsd/aarch64/machine/xen/xen-os.h       | 132 +++++++++++++++++++++++++++++++++
 2 files changed, 191 insertions(+)
 create mode 100644 bsd/aarch64/machine/xen/synch_bitops.h
 create mode 100644 bsd/aarch64/machine/xen/xen-os.h

diff --git a/bsd/aarch64/machine/xen/synch_bitops.h b/bsd/aarch64/machine/xen/synch_bitops.h
new file mode 100644
index 0000000..a9d0d2c
--- /dev/null
+++ b/bsd/aarch64/machine/xen/synch_bitops.h
@@ -0,0 +1,59 @@
+#ifndef __XEN_SYNCH_BITOPS_H__
+#define __XEN_SYNCH_BITOPS_H__
+
+/*
+ * Based on include/arm/os.h from Xen Mini-OS priject

typo: project.
This stuff is not available in BSD?
If we're using code from another project, can you please say something about the license of this project? We also probably need a file about it in licenses.

 
--
2.7.4

--
You received this message because you are subscribed to the Google Groups "OSv Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nadav Har'El

unread,
Feb 21, 2017, 11:20:13 AM2/21/17
to Sergiy Kibrik, Glauber Costa, Osv Dev
Hmm, looks reasonable to me, but I am not familar at all with the context, so I have no idea if this change has any downsides. Glauber, maybe you can take a look at this patch?

Thanks,
Nadav.


--
Nadav Har'El
n...@scylladb.com

On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com> wrote:
--
2.7.4

Nadav Har'El

unread,
Feb 21, 2017, 11:22:55 AM2/21/17
to Sergiy Kibrik, Osv Dev
On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com> wrote:
hi OSv developers,
Let me present patches that allow OSv to work with Xen event channels on aarch64 platform.
This series contains some clean up work -- to remove unused (and probably unneeded) code,
and to re-use some generic parts of x64 code, but there're also rather intrusive
changes to core interrupt handling code.

Hi,

Are these "intrusive changes" referring to the change to not use the unmask() function in one place? Or something else/more?

Thanks,
Nadav.

Sergiy Kibrik

unread,
Feb 21, 2017, 11:53:17 AM2/21/17
to Nadav Har'El, Osv Dev
On 02/21/2017 06:17 PM, Nadav Har'El wrote:
>
> On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com <mailto:osv...@googlegroups.com>> wrote:
>
> evtchn and gnttab drivers use these operations extensively.
>
> Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com <mailto:sergiy...@globallogic.com>>
> ---
> bsd/aarch64/machine/xen/synch_bitops.h | 59 +++++++++++++++
> bsd/aarch64/machine/xen/xen-os.h | 132 +++++++++++++++++++++++++++++++++
> 2 files changed, 191 insertions(+)
> create mode 100644 bsd/aarch64/machine/xen/synch_bitops.h
> create mode 100644 bsd/aarch64/machine/xen/xen-os.h
>
> diff --git a/bsd/aarch64/machine/xen/synch_bitops.h b/bsd/aarch64/machine/xen/synch_bitops.h
> new file mode 100644
> index 0000000..a9d0d2c
> --- /dev/null
> +++ b/bsd/aarch64/machine/xen/synch_bitops.h
> @@ -0,0 +1,59 @@
> +#ifndef __XEN_SYNCH_BITOPS_H__
> +#define __XEN_SYNCH_BITOPS_H__
> +
> +/*
> + * Based on include/arm/os.h from Xen Mini-OS priject
>
>
> typo: project.

oops, will fix that..

> This stuff is not available in BSD?
> If we're using code from another project, can you please say something about the license of this project? We also probably need a file about it in licenses.

The file itself [1] doesn't contain license header, but COPYING is FreeBSD License [2], so I presume os.h is
also BSD. So I should probably put COPYING contents in file's header.

[1] https://xenbits.xenproject.org/gitweb/?p=mini-os.git;a=blob;f=include/arm/os.h;hb=HEAD
[2] https://xenbits.xenproject.org/gitweb/?p=mini-os.git;a=blob;f=COPYING;hb=HEAD

--
regards,
Sergiy

Sergiy Kibrik

unread,
Feb 21, 2017, 11:56:49 AM2/21/17
to Nadav Har'El, Osv Dev

On 02/21/2017 06:22 PM, Nadav Har'El wrote:
>
> On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com <mailto:osv...@googlegroups.com>> wrote:
>
> hi OSv developers,
> Let me present patches that allow OSv to work with Xen event channels on aarch64 platform.
> This series contains some clean up work -- to remove unused (and probably unneeded) code,
> and to re-use some generic parts of x64 code, but there're also rather intrusive
> changes to core interrupt handling code.
>
>
> Hi,
>
> Are these "intrusive changes" referring to the change to not use the unmask() function in one place? Or something else/more?

hi Nadav,
It is about unmask_evtchn() replacement and bitops fix.

--
regards,
Sergiy

Nadav Har'El

unread,
Feb 22, 2017, 2:23:43 AM2/22/17
to Sergiy Kibrik, Osv Dev
Hi, since I don't normally test OSv on Xen, just on KVM, I am worried about the possibility that
these or future patches might break something for Xen on x64, and we won't even notice it.

Will you be able to also test x64 Xen, and verify that your patches do not break anything there?
 
Thanks,
Nadav.

Sergiy Kibrik

unread,
Feb 22, 2017, 6:49:20 AM2/22/17
to Nadav Har'El, Osv Dev
On 02/22/2017 09:23 AM, Nadav Har'El wrote:
[..]
> Hi, since I don't normally test OSv on Xen, just on KVM, I am worried about the possibility that
> these or future patches might break something for Xen on x64, and we won't even notice it.
>
> Will you be able to also test x64 Xen, and verify that your patches do not break anything there?
>

Yes, I was actually going to setup some x64 kvm/xen test env.
By the way -- how OSv is usually tested? Do you have some test suite? What are common sanity tests?
Do you test locally, or on separate station/board (or remotely in cloud)? Any guidance on test setup preparation would be very helpful.

--
regards,
Sergiy

Nadav Har'El

unread,
Feb 22, 2017, 7:15:29 AM2/22/17
to Sergiy Kibrik, Osv Dev
On Wed, Feb 22, 2017 at 1:49 PM, Sergiy Kibrik <sergiy...@globallogic.com> wrote:
On 02/22/2017 09:23 AM, Nadav Har'El wrote:
[..]
Hi, since I don't normally test OSv on Xen, just on KVM, I am worried about the possibility that
these or future patches might break something for Xen on x64, and we won't even notice it.

Will you be able to also test x64 Xen, and verify that your patches do not break anything there?


Yes, I was actually going to setup some x64 kvm/xen test env.

Thanks. If you'll let me know that it doesn't get broken by your patches, I will commit them.
 
By the way -- how OSv is usually tested? Do you have some test suite? What are common sanity tests?
Do you test locally, or on separate station/board (or remotely in cloud)? Any guidance on test setup preparation would be very helpful.

We have a suite of unit tests which you can run with "make check" - this runs a couple of Java related tests, but more importantly, several dozen tests in tests/tst-*.so.

We also have automated testing on http://jenkins.cloudius-systems.com:8080/, which, if I remember correctly, runs this "make check" in several configurations (1 cpu, 4 cpus) and in addition some tomcat test (?). All of these automated tests happen on qemu/kvm if I remember correctly - event though we also support virtualbox, xen and vmware - as far as I can tell, we don't have any automated testing for them at the moment, as far as I can tell :-(

Theoretically, Xen on x86 could be tested on Amazon EC2. But I don't have any setup for that either.

Waldek Kozaczuk

unread,
Feb 22, 2017, 10:49:43 AM2/22/17
to OSv Development, sergiy...@globallogic.com, Nadav Har'El
I am not sure if that all is possible in practice. 

But I think you can run make check which would leave a test image (usr.img) and use it to create EC2 AMI which then you can to create EC2 instance (using cloud formation or cli or manually). There is a scripts/ec2-make-ami.py that you can use to do it. It is pretty old so it may not work. I actually have a newer version of it that uses boto3 which I have used successfully number of times. If you want I can email you or simply send a patch that will replace the old one. Bear in mind the the AMI creation process uses raw image that is 10G and will take 30 minutes or so to create an AMI of it. I would change the 'make check' to set image size to 256M or something, Then whole process takes only 5 minutes or so. 

Now I have no idea how to execute tests remotely on EC2 instance and get the reports. Maybe the image created by make check would do it implicitly on startup. In this case you would simply collect the standard output from EC2 instance which you can do using cli.

I hope it helps a little,
Waldek 

Waldek Kozaczuk

unread,
Feb 22, 2017, 12:44:51 PM2/22/17
to OSv Development, sergiy...@globallogic.com, n...@scylladb.com
Also there is a scripts/ec2-tester.sh that seems to be outdated but maybe it can be a basis of creating a script that would actually run all unit tests on EC2 instance. There is also an old Wiki page that documents that script - https://github.com/cloudius-systems/osv/wiki/Automatic-testing-on-EC2

I might have so time to look into in and possibly come up with an EC2 unit test script.

Waldek

Sergiy Kibrik

unread,
Feb 24, 2017, 10:25:35 AM2/24/17
to Osv Dev, Nadav Har'El, Sergiy Kibrik
evtchn and gnttab drivers use these operations extensively.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
---
bsd/aarch64/machine/xen/synch_bitops.h | 81 ++++++++++++++++++++
bsd/aarch64/machine/xen/xen-os.h | 132 +++++++++++++++++++++++++++++++++
2 files changed, 213 insertions(+)
create mode 100644 bsd/aarch64/machine/xen/synch_bitops.h
create mode 100644 bsd/aarch64/machine/xen/xen-os.h

diff --git a/bsd/aarch64/machine/xen/synch_bitops.h b/bsd/aarch64/machine/xen/synch_bitops.h
new file mode 100644
index 0000000..d9e84e2
--- /dev/null
+++ b/bsd/aarch64/machine/xen/synch_bitops.h
@@ -0,0 +1,81 @@
+/* Copyright (c) 2009 Citrix Systems, Inc. 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 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 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.
+ *
+ * Based on include/arm/os.h from Xen Mini-OS project
+ */
+
+#ifndef __XEN_SYNCH_BITOPS_H__
+#define __XEN_SYNCH_BITOPS_H__
+
new file mode 100644
index 0000000..6a5defb
--- /dev/null
+++ b/bsd/aarch64/machine/xen/xen-os.h
@@ -0,0 +1,132 @@
+/*

Sergiy Kibrik

unread,
Feb 24, 2017, 10:51:34 AM2/24/17
to Nadav Har'El, Osv Dev
On 02/22/2017 02:15 PM, Nadav Har'El wrote:
>
> On Wed, Feb 22, 2017 at 1:49 PM, Sergiy Kibrik <sergiy...@globallogic.com <mailto:sergiy...@globallogic.com>> wrote:
>
> On 02/22/2017 09:23 AM, Nadav Har'El wrote:
> [..]
>
> Hi, since I don't normally test OSv on Xen, just on KVM, I am worried about the possibility that
> these or future patches might break something for Xen on x64, and we won't even notice it.
>
> Will you be able to also test x64 Xen, and verify that your patches do not break anything there?
>
>
> Yes, I was actually going to setup some x64 kvm/xen test env.
>
>
> Thanks. If you'll let me know that it doesn't get broken by your patches, I will commit them.

hi Nadav,
I've successfully verified patches on x64 Intel Atom machine (Xen guest w/ 2 vcpus), on aarch64 Renesas R-Car Gen3 platform (Xen guest w/ single vcpu) and qemu-aarch64.

cli boots fine and all commands seem to be working correctly.

--
regards,
Sergiy

Nadav Har'El

unread,
Feb 26, 2017, 1:44:24 AM2/26/17
to Sergiy Kibrik, Osv Dev
On Fri, Feb 24, 2017 at 5:51 PM, Sergiy Kibrik <sergiy...@globallogic.com> wrote:
On 02/22/2017 02:15 PM, Nadav Har'El wrote:

On Wed, Feb 22, 2017 at 1:49 PM, Sergiy Kibrik <sergiy...@globallogic.com <mailto:sergiy.kibrik@globallogic.com>> wrote:

    On 02/22/2017 09:23 AM, Nadav Har'El wrote:
    [..]

        Hi, since I don't normally test OSv on Xen, just on KVM, I am worried about the possibility that
        these or future patches might break something for Xen on x64, and we won't even notice it.

        Will you be able to also test x64 Xen, and verify that your patches do not break anything there?


    Yes, I was actually going to setup some x64 kvm/xen test env.


Thanks. If you'll let me know that it doesn't get broken by your patches, I will commit them.

hi Nadav,
I've successfully verified patches on x64 Intel Atom machine (Xen guest w/ 2 vcpus), on aarch64 Renesas R-Car Gen3 platform (Xen guest w/ single vcpu) and qemu-aarch64.

cli boots fine and all commands seem to be working correctly.


Thanks for testing this. I'll commit your patches.

Nadav Har'El

unread,
Feb 26, 2017, 4:08:16 AM2/26/17
to Sergiy Kibrik, Osv Dev
I'll commit this because obviously you understand this stuff better than I do, and have also tested it, but I do have a couple of questions:

On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com> wrote:
Doesn't this patch change the layout of this structure also on x86? Is this acceptable?
 
     pvclock_vcpu_time_info time;
 };

 struct xen_shared_info {
-    struct xen_vcpu_info vcpu_info[32];
+    struct xen_vcpu_info vcpu_info[MAX_VIRT_CPUS];

A quick grep suggests that MAX_VIRT_CPUS on aarch64 is set to 1. Does this make sense?

     std::atomic<unsigned long> evtchn_pending[sizeof(unsigned long) * 8];
     std::atomic<unsigned long> evtchn_mask[sizeof(unsigned long) * 8];
--
2.7.4

Commit Bot

unread,
Feb 26, 2017, 4:11:28 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik' via OSv Development
From: Sergiy Kibrik' via OSv Development <osv...@googlegroups.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

bsd: remove unused xenfunc.h header

It contains definitions of non-existing (in OSv tree) routines, which had
been
used in original BSD code and have absolutely no traces of use in OSv.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id:
<1487597529-28580-3-git-...@globallogic.com>

---
diff --git a/bsd/sys/dev/xen/blkfront/blkfront.cc
b/bsd/sys/dev/xen/blkfront/blkfront.cc
--- a/bsd/sys/dev/xen/blkfront/blkfront.cc
+++ b/bsd/sys/dev/xen/blkfront/blkfront.cc
@@ -68,7 +68,6 @@ __FBSDID("$FreeBSD$");
#include <machine/_inttypes.h>
#include <machine/xen/xen-os.h>
#include <machine/xen/xenvar.h>
-#include <machine/xen/xenfunc.h>

#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
diff --git a/bsd/sys/dev/xen/netfront/netfront.cc
b/bsd/sys/dev/xen/netfront/netfront.cc
--- a/bsd/sys/dev/xen/netfront/netfront.cc
+++ b/bsd/sys/dev/xen/netfront/netfront.cc
@@ -82,7 +82,6 @@ __FBSDID("$FreeBSD$");
#include <machine/intr_machdep.h>

#include <machine/xen/xen-os.h>
-#include <machine/xen/xenfunc.h>
#include <machine/xen/xenvar.h>
#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
diff --git a/bsd/x64/machine/xen/xenfunc.h b/bsd/x64/machine/xen/xenfunc.h
--- a/bsd/x64/machine/xen/xenfunc.h
+++ b/bsd/x64/machine/xen/xenfunc.h

Commit Bot

unread,
Feb 26, 2017, 4:11:30 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik' via OSv Development
From: Sergiy Kibrik' via OSv Development <osv...@googlegroups.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

bsd: xen: move common headers outside of x64/machine/xen

xenvar.h and xenpmap.h can be used for aarch64 as well without modification.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id:
<1487597529-28580-4-git-...@globallogic.com>

---
diff --git a/bsd/sys/machine/xen/xenpmap.h b/bsd/sys/machine/xen/xenpmap.h
--- a/bsd/sys/machine/xen/xenpmap.h
+++ b/bsd/sys/machine/xen/xenpmap.h
null
diff --git a/bsd/sys/machine/xen/xenvar.h b/bsd/sys/machine/xen/xenvar.h
--- a/bsd/sys/machine/xen/xenvar.h
+++ b/bsd/sys/machine/xen/xenvar.h
null

Commit Bot

unread,
Feb 26, 2017, 4:11:31 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik
From: Sergiy Kibrik <sergiy...@globallogic.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

bsd: xen: add synch_bitops.h and xen-os.h for AARCH64

evtchn and gnttab drivers use these operations extensively.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id: <1487949914-3523-1-git-s...@globallogic.com>

---
diff --git a/bsd/aarch64/machine/xen/synch_bitops.h
b/bsd/aarch64/machine/xen/synch_bitops.h
--- a/bsd/aarch64/machine/xen/synch_bitops.h
--- a/bsd/aarch64/machine/xen/xen-os.h

Commit Bot

unread,
Feb 26, 2017, 4:11:32 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik' via OSv Development
From: Sergiy Kibrik' via OSv Development <osv...@googlegroups.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

osv: generic bitops

Semantics of bsrq()/fls() differs on x64 and aarch64, e.g. following sample
inputs produce different results:

x64: aarch64:

bsrq(0x1) = 0 bsrq(0x1) = 63
bsrq(0xffffffffffffffff) = 63 bsrq(0xffffffffffffffff) = 0
bsrq(0x8000000000000000) = 63 bsrq(0x8000000000000000) = 0
bsrq(0x80000000) = 31 bsrq(0x80000000) = 32
bsrq(0x8008) = 15 bsrq(0x8008) = 48

bsrl(0x1) = 0 bsrl(0x1) = 63
bsrl(0xffffffff) = 31 bsrl(0xffffffff) = 32
bsrl(0x80000000) = 31 bsrl(0x80000000) = 32
bsrl(0x8008) = 15 bsrl(0x8008) = 48

fsl(0x1) = 1 fsl(0x1) = 64
fsl(0xffffffff) = 32 fsl(0xffffffff) = 33
fsl(0x80000000) = 32 fsl(0x80000000) = 33
fsl(0x8008) = 16 fsl(0x8008) = 49

Which is not quite a behaviour bitops user expects.
Use GCC built-ins to overcome this, which provide consistent behaviour
across
all platforms. Arch-specific instructions generated to avoid loops, so this
should not hurt performance.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id:
<1487597529-28580-6-git-...@globallogic.com>

---
diff --git a/arch/aarch64/bitops.h b/arch/aarch64/bitops.h
--- a/arch/aarch64/bitops.h
+++ b/arch/aarch64/bitops.h
diff --git a/bsd/porting/netport.h b/bsd/porting/netport.h
--- a/bsd/porting/netport.h
+++ b/bsd/porting/netport.h
@@ -19,7 +19,7 @@
#include <osv/debug.h>
#define __NEED_socklen_t
#include <bits/alltypes.h>
-#include "bitops.h"
+#include <osv/bitops.h>

__BEGIN_DECLS

diff --git a/core/xen_intr.cc b/core/xen_intr.cc
--- a/core/xen_intr.cc
+++ b/core/xen_intr.cc
@@ -9,7 +9,7 @@
#include <osv/xen_intr.hh>
#include <bsd/porting/bus.h>
#include <machine/intr_machdep.h>
-#include "bitops.h"
+#include <osv/bitops.h>
#include <osv/debug.hh>

#include <osv/trace.hh>
diff --git a/include/osv/bitops.h b/include/osv/bitops.h
--- a/include/osv/bitops.h
+++ b/include/osv/bitops.h
@@ -7,20 +7,16 @@

#ifndef ARCH_BITOPS_H_
#define ARCH_BITOPS_H_
-static inline unsigned int
+static inline unsigned int
bsrl(unsigned int mask)
{
- unsigned int result;
- asm volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
- return result;
+ return sizeof(mask) * 8 - __builtin_clz(mask) - 1;
}

static inline unsigned long
bsrq(unsigned long mask)
{
- unsigned long result;
- asm volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
- return result;
+ return sizeof(mask) * 8 - __builtin_clzl(mask) - 1;
}

static inline int

Commit Bot

unread,
Feb 26, 2017, 4:11:34 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik' via OSv Development
From: Sergiy Kibrik' via OSv Development <osv...@googlegroups.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

osv: xen_intr: use evtchn.h

Replace local declaration of unmask_evtchn() and evtchn_from_irq()
with inclusion of evtchn.h where they're declared.

Additionally declare evtchn_from_irq() in header,
as it's used in Xen core interrupt handler.

Include evtchn driver into common build, so that aarch64 code can link.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id:
<1487597529-28580-7-git-...@globallogic.com>

---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -595,11 +595,11 @@ bsd += bsd/sys/netinet/arpcache.o
bsd += bsd/sys/xdr/xdr.o
bsd += bsd/sys/xdr/xdr_array.o
bsd += bsd/sys/xdr/xdr_mem.o
+bsd += bsd/sys/xen/evtchn.o

ifeq ($(arch),x64)
$(out)/bsd/%.o: COMMON += -DXEN -DXENHVM
bsd += bsd/sys/xen/gnttab.o
-bsd += bsd/sys/xen/evtchn.o
bsd += bsd/sys/xen/xenstore/xenstore.o
bsd += bsd/sys/xen/xenbus/xenbus.o
bsd += bsd/sys/xen/xenbus/xenbusb.o
diff --git a/bsd/sys/xen/evtchn.cc b/bsd/sys/xen/evtchn.cc
--- a/bsd/sys/xen/evtchn.cc
+++ b/bsd/sys/xen/evtchn.cc
@@ -31,7 +31,6 @@ __FBSDID("$FreeBSD$");
#include <machine/intr_machdep.h>

#include <machine/xen/xen-os.h>
-#include <machine/xen/xenvar.h>
#include <xen/xen_intr.h>
#include <machine/xen/synch_bitops.h>
#include <xen/evtchn.h>
diff --git a/bsd/sys/xen/evtchn.h b/bsd/sys/xen/evtchn.h
--- a/bsd/sys/xen/evtchn.h
+++ b/bsd/sys/xen/evtchn.h
@@ -30,6 +30,8 @@ void mask_evtchn(int port);

void unmask_evtchn(int port);

+int evtchn_from_irq(int irq);
+
#ifdef SMP
void rebind_evtchn_to_cpu(int port, unsigned int cpu);
#else
diff --git a/core/xen_intr.cc b/core/xen_intr.cc

Commit Bot

unread,
Feb 26, 2017, 4:11:35 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik' via OSv Development
From: Sergiy Kibrik' via OSv Development <osv...@googlegroups.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

osv: xen_intr: do not use unmask_evtchn() in IRQ handler

unmask_evtchn() has side effect of setting evtchn_upcall_pending and
triggering interrupt from Xen if unmasked port is pending.
This works fine when unmasking new port, but in case of calling it from
IRQ handler can lead to race and lost interrupt.

do_irq() already handles pending interrupts, there's no need to check
pending ports
and mess with interrupt handler logic, simply set mask bitfield and
continue.

This fixes lost interrupt on aarch64 platform.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id:
<1487597529-28580-8-git-...@globallogic.com>

---
diff --git a/core/xen_intr.cc b/core/xen_intr.cc

Commit Bot

unread,
Feb 26, 2017, 4:11:36 AM2/26/17
to osv...@googlegroups.com, Sergiy Kibrik' via OSv Development
From: Sergiy Kibrik' via OSv Development <osv...@googlegroups.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

osv: xen: make shared structures arch-independent

xen_shared_info & xen_vcpu_info are different on x64/aarch64, so couple of
fixes
needed so that shared data is consistent with what hypervisor presumes.

Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com>
Message-Id:
<1487597529-28580-9-git-...@globallogic.com>

---
diff --git a/bsd/sys/xen/interface/arch-aarch64.h
b/bsd/sys/xen/interface/arch-aarch64.h
--- a/bsd/sys/xen/interface/arch-aarch64.h
+++ b/bsd/sys/xen/interface/arch-aarch64.h
@@ -37,6 +37,7 @@ typedef uint64_t xen_ulong_t;
#define PRI_xen_ulong "llx"

struct arch_vcpu_info {
+ int empty[0]; /* force zero size */
};
typedef struct arch_vcpu_info arch_vcpu_info_t;

diff --git a/include/osv/xen.hh b/include/osv/xen.hh

Sergiy Kibrik

unread,
Feb 27, 2017, 10:22:54 AM2/27/17
to Nadav Har'El, Osv Dev
On 02/26/2017 11:08 AM, Nadav Har'El wrote:
> I'll commit this because obviously you understand this stuff better than I do, and have also tested it, but I do have a couple of questions:
>
> On Mon, Feb 20, 2017 at 3:32 PM, 'Sergiy Kibrik' via OSv Development <osv...@googlegroups.com <mailto:osv...@googlegroups.com>> wrote:
>
> xen_shared_info & xen_vcpu_info are different on x64/aarch64, so couple of fixes
> needed so that shared data is consistent with what hypervisor presumes.
>
> Signed-off-by: Sergiy Kibrik <sergiy...@globallogic.com <mailto:sergiy...@globallogic.com>>
hi Nadav,
vcpu_info[N] with N>1 is legacy interface and doesn't actually get used by Xen on ARM for SMP purposes.
MAX_VIRT_CPUS set to 1 in ARM part of hypervisor code as well, so we're safe here.

--
regards,
Sergiy

>
> std::atomic<unsigned long> evtchn_pending[sizeof(unsigned long) * 8];
> std::atomic<unsigned long> evtchn_mask[sizeof(unsigned long) * 8];
> --
> 2.7.4
>
> --
> You received this message because you are subscribed to the Google Groups "OSv Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+u...@googlegroups.com <mailto:osv-dev%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
>
Reply all
Reply to author
Forward
0 new messages