[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH 05/17] plat/common: Common x86 CPU functions


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Tue, 27 Mar 2018 15:29:38 +0300
  • Cc: simon.kuenzer@xxxxxxxxx
  • Delivery-date: Tue, 27 Mar 2018 12:30:15 +0000
  • Ironport-phdr: 9a23:IgvPGBH3dmQQGwCgpK8Tkp1GYnF86YWxBRYc798ds5kLTJ7zp8qwAkXT6L1XgUPTWs2DsrQY07GQ6/iocFdDyK7JiGoFfp1IWk1NouQttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXdrXKo8DEdBAj0OxZrKeTpAI7SiNm82/yv95HJbAhEmDSwbaluIBmqsA7cqtQYjYx+J6gr1xDHuGFIe+NYxWNpIVKcgRPx7dqu8ZBg7ipdpesv+9ZPXqvmcas4S6dYDCk9PGAu+MLrrxjDQhCR6XYaT24bjwBHAwnB7BH9Q5fxri73vfdz1SWGIcH7S60/VDK/5KlpVRDokj8KOT4n/m/KhMJ+j6VVrxCvpxFk34LYfJuYOOZkc6/BYd8XQ3dKUMZLVyxGB4Oxd5UCD+0aPeZEron9oUYFox2jBQm0GePk1zhFiWPx3a0hz+QhEAfG0BYkH9ITqHTUsc74O7sJUeyv1KnI0C7MY+lM2Tf68YXFdA0qr/KUXb9obMbcxlQjGxnGg1iQs4DpIS2Z2+YXv2WV9+ZsSO2ih3M9pwxyojWj3Nkgh4fHi44P11zJ+jt1zYAoLtOiUkF7e8SrEJ5IuiGfMIt5X90tTnlzuCY/1r0GoZm7fDUWyJg/xx7QdfiHc4+Q7xL9UeaeOzZ4hHZ/dL2jnBa+61CgyvDnWcWuylZKqTJJktjKtn8Tyxze8tWLR/Rg8ku72juC1xrf5v9aLU02j6bWJYYtwrsqmZoStUTDEDX2mELzjKKOakok/fOo6/jmYrXgvJOcM5J0ihnjMqk1hsO/Gv40MhATX2eA4+i8zrrj8VXjQLpWlv02jrXZsJfCKMQep665BQ5V0oE46xqmEjipzsoYkmcDLF9efBKHjpPpO03VIPziAvawnVKsnC1sx/DcMb3rGo/NIWTbkLf9YbZ97FZRyBEyzdBa4JJUDKsNIPHuWkDvstzZDwE2MxavzubhFtpyyoceVniUDaODLqzSrAzA2uV6JuiKZYgO/TrwNfUhz/rvlmMi308Qe++ux5RERmq/G6FNJF6FYHykpsoZDC9etQ0lUO3szlmfSSN7bG30R782oCs8XtH1RbzfT5yg1eTSlBywGYdbMzhL
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

CPU functions can be classified into 3 categories: (1) native functions that
are used on both platforms, (2) native functions used only by HVM (KVM) and (3)
paravirtualized functions currently used by Xen PV VM.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 plat/common/include/x86/cpu.h  | 109 +++++++++++++++++++++++++++++++++++++++++
 plat/common/x86/cpu_native.c   |  49 ++++++++++++++++++
 plat/kvm/shutdown.c            |   2 +-
 plat/kvm/x86/console.c         |   2 +-
 plat/xen/Makefile.uk           |   3 ++
 plat/xen/include/xen-x86/cpu.h | 103 --------------------------------------
 plat/xen/include/xen-x86/os.h  |   1 -
 plat/xen/x86/arch_events.c     |   2 +-
 plat/xen/x86/arch_time.c       |   4 +-
 plat/xen/x86/cpu_pv.c          |  47 ++++++++++++++++++
 10 files changed, 213 insertions(+), 109 deletions(-)
 create mode 100644 plat/common/include/x86/cpu.h
 create mode 100644 plat/common/x86/cpu_native.c
 delete mode 100644 plat/xen/include/xen-x86/cpu.h
 create mode 100644 plat/xen/x86/cpu_pv.c

diff --git a/plat/common/include/x86/cpu.h b/plat/common/include/x86/cpu.h
new file mode 100644
index 0000000..ce96c1c
--- /dev/null
+++ b/plat/common/include/x86/cpu.h
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * 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.
+ */
+/******************************************************************************
+ * cpu.h
+ *
+ * CPU related macros and definitions copied from mini-os/os.h
+ */
+
+#include <uk/arch/types.h>
+
+
+void halt(void);
+
+static inline void cpuid(__u32 leaf, __u32 *eax, __u32 *ebx,
+               __u32 *ecx, __u32 *edx)
+{
+       asm volatile("cpuid"
+                    : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
+                    : "0"(leaf));
+}
+
+unsigned long read_cr2(void);
+
+static inline void write_cr3(unsigned long cr3)
+{
+       asm volatile("mov %0, %%cr3" : : "r"(cr3) : "memory");
+}
+
+static inline void invlpg(unsigned long va)
+{
+       asm volatile("invlpg %0" : : "m"(*(const char *)(va)) : "memory");
+}
+
+
+static inline void wrmsr(unsigned int msr, __u32 lo, __u32 hi)
+{
+       asm volatile("wrmsr"
+                            : /* no outputs */
+                            : "c"(msr), "a"(lo), "d"(hi));
+}
+
+static inline void wrmsrl(unsigned int msr, __u64 val)
+{
+       wrmsr(msr, (__u32) (val & 0xffffffffULL), (__u32) (val >> 32));
+}
+
+
+static inline __u64 rdtsc(void)
+{
+       __u64 l, h;
+
+       __asm__ __volatile__("rdtsc" : "=a"(l), "=d"(h));
+       return (h << 32) | l;
+}
+
+
+/* accessing devices via port space */
+static inline __u8 inb(__u16 port)
+{
+       __u8 v;
+
+       __asm__ __volatile__("inb %1,%0" : "=a"(v) : "dN"(port));
+       return v;
+}
+
+static inline void outb(__u16 port, __u8 v)
+{
+       __asm__ __volatile__("outb %0,%1" : : "a"(v), "dN"(port));
+}
+
+static inline void outw(__u16 port, __u16 v)
+{
+       __asm__ __volatile__("outw %0,%1" : : "a"(v), "dN"(port));
+}
+
+static inline __u64 mul64_32(__u64 a, __u32 b)
+{
+       __u64 prod;
+
+       __asm__ (
+               "mul %%rdx ; "
+               "shrd $32, %%rdx, %%rax"
+               : "=a" (prod)
+               : "0" (a), "d" ((__u64) b)
+       );
+
+       return prod;
+}
diff --git a/plat/common/x86/cpu_native.c b/plat/common/x86/cpu_native.c
new file mode 100644
index 0000000..0330a20
--- /dev/null
+++ b/plat/common/x86/cpu_native.c
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+
+#include <x86/cpu.h>
+
+void halt(void)
+{
+       __asm__ __volatile__ ("hlt" : : : "memory");
+}
+
+unsigned long read_cr2(void)
+{
+       unsigned long cr2;
+
+       __asm__ __volatile__("mov %%cr2, %0" : "=r"(cr2));
+
+       return cr2;
+}
diff --git a/plat/kvm/shutdown.c b/plat/kvm/shutdown.c
index 6b5baf2..673d065 100644
--- a/plat/kvm/shutdown.c
+++ b/plat/kvm/shutdown.c
@@ -22,7 +22,7 @@
  */
 
 #include <errno.h>
-#include <kvm-x86/cpu_x86_64.h>
+#include <x86/cpu.h>
 #include <uk/print.h>
 #include <uk/plat/bootstrap.h>
 
diff --git a/plat/kvm/x86/console.c b/plat/kvm/x86/console.c
index 28e1246..cfcc3cd 100644
--- a/plat/kvm/x86/console.c
+++ b/plat/kvm/x86/console.c
@@ -25,8 +25,8 @@
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <x86/cpu.h>
 #include <kvm/console.h>
-#include <kvm-x86/cpu_x86_64.h>
 #include <uk/plat/console.h>
 #include <uk/essentials.h>
 #include <uk/print.h>
diff --git a/plat/xen/Makefile.uk b/plat/xen/Makefile.uk
index c1b2ecb..220c8d9 100644
--- a/plat/xen/Makefile.uk
+++ b/plat/xen/Makefile.uk
@@ -41,6 +41,9 @@ LIBXENPLAT_SRCS-$(LIBUKSCHED)  += 
$(LIBXENPLAT_BASE)/x86/arch_thread.c
 ifneq ($(XEN_HVMLITE),y)
 LIBXENPLAT_ASFLAGS-y           += -DCONFIG_PARAVIRT
 LIBXENPLAT_CFLAGS-y            += -DCONFIG_PARAVIRT
+LIBXENPLAT_SRCS-y              += $(LIBXENPLAT_BASE)/x86/cpu_pv.c
+else
+LIBXENPLAT_SRCS-y              += $(UK_PLAT_COMMON_BASE)/x86/cpu_native.c
 endif
 endif
 
diff --git a/plat/xen/include/xen-x86/cpu.h b/plat/xen/include/xen-x86/cpu.h
deleted file mode 100644
index cf9241b..0000000
--- a/plat/xen/include/xen-x86/cpu.h
+++ /dev/null
@@ -1,103 +0,0 @@
-/* SPDX-License-Identifier: BSD-2-Clause */
-/*
- * 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.
- */
-/******************************************************************************
- * cpu.h
- *
- * CPU related macros and definitions copied from os.h
- */
-
-#ifndef PLAT_XEN_INCLUDE_XEN_X86_CPU_H_
-#define PLAT_XEN_INCLUDE_XEN_X86_CPU_H_
-
-#ifdef CONFIG_PARAVIRT
-#include <common/hypervisor.h>
-#endif
-
-static inline void write_cr3(unsigned long cr3)
-{
-       asm volatile("mov %0, %%cr3" : : "r"(cr3) : "memory");
-}
-
-static inline void invlpg(unsigned long va)
-{
-       asm volatile("invlpg %0" : : "m"(*(const char *)(va)) : "memory");
-}
-
-/************************** i386 *******************************/
-#ifdef __X64_32__
-
-#define rdtscll(val) (asm volatile("rdtsc" : "=A"(val)))
-
-/************************** x86_84 *******************************/
-#elif defined __X86_64__
-
-#define rdtscll(val)                                                   \
-       do {                                                               \
-               unsigned int __a, __d;                                         \
-               asm volatile("rdtsc" : "=a"(__a), "=d"(__d));                  \
-               (val) = ((unsigned long)__a) | (((unsigned long)__d) << 32);   \
-       } while (0)
-
-#else /* ifdef __x86_64__ */
-#error "Unsupported architecture"
-#endif
-
-/********************* common i386 and x86_64  ****************************/
-
-#define wrmsr(msr, val1, val2) \
-({ \
-       asm volatile("wrmsr" \
-                            : /* no outputs */ \
-                            : "c"(msr), "a"(val1), "d"(val2)); \
-})
-
-static inline void wrmsrl(unsigned int msr, uint64_t val)
-{
-       wrmsr(msr, (uint32_t)(val & 0xffffffffULL), (uint32_t)(val >> 32));
-}
-
-static inline void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx,
-                        uint32_t *ecx, uint32_t *edx)
-{
-       asm volatile("cpuid"
-                    : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
-                    : "0"(leaf));
-}
-
-#ifdef CONFIG_PARAVIRT
-static inline unsigned long read_cr2(void)
-{
-       return HYPERVISOR_shared_info->vcpu_info[smp_processor_id()].arch.cr2;
-}
-#else
-static inline unsigned long read_cr2(void)
-{
-       unsigned long cr2;
-
-       asm volatile("mov %%cr2,%0\n\t" : "=r"(cr2));
-       return cr2;
-}
-#endif
-
-#endif /* PLAT_XEN_INCLUDE_XEN_X86_CPU_H_ */
diff --git a/plat/xen/include/xen-x86/os.h b/plat/xen/include/xen-x86/os.h
index 3df773c..6fa4fa0 100644
--- a/plat/xen/include/xen-x86/os.h
+++ b/plat/xen/include/xen-x86/os.h
@@ -111,7 +111,6 @@ typedef struct {
        volatile int counter;
 } atomic_t;
 
-#include <xen-x86/cpu.h>
 
 /********************* common i386 and x86_64  ****************************/
 #define xen_mb() mb()
diff --git a/plat/xen/x86/arch_events.c b/plat/xen/x86/arch_events.c
index dca3b9c..5b88065 100644
--- a/plat/xen/x86/arch_events.c
+++ b/plat/xen/x86/arch_events.c
@@ -28,8 +28,8 @@
  * Ported from Mini-OS
  */
 #include <stdint.h>
+#include <x86/cpu.h>
 #include <uk/plat/config.h>
-#include <xen-x86/cpu.h>
 
 #if defined(__x86_64__)
 static char irqstack[2 * STACK_SIZE];
diff --git a/plat/xen/x86/arch_time.c b/plat/xen/x86/arch_time.c
index 991c030..9e9f1bf 100644
--- a/plat/xen/x86/arch_time.c
+++ b/plat/xen/x86/arch_time.c
@@ -35,10 +35,10 @@
 
 #include <stdint.h>
 #include <sys/time.h>
+#include <x86/cpu.h>
 #include <uk/plat/time.h>
 #include <common/hypervisor.h>
 #include <common/events.h>
-#include <xen-x86/cpu.h>
 #include <xen-x86/irq.h>
 #include <uk/assert.h>
 
@@ -134,7 +134,7 @@ static unsigned long get_nsec_offset(void)
 {
        uint64_t now, delta;
 
-       rdtscll(now);
+       now = rdtsc();
        delta = now - shadow.tsc_timestamp;
 
        return scale_delta(delta, shadow.tsc_to_nsec_mul, shadow.tsc_shift);
diff --git a/plat/xen/x86/cpu_pv.c b/plat/xen/x86/cpu_pv.c
new file mode 100644
index 0000000..471cc37
--- /dev/null
+++ b/plat/xen/x86/cpu_pv.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+
+#include <x86/cpu.h>
+#include <common/hypervisor.h>
+
+
+void halt(void)
+{
+       HYPERVISOR_sched_op(SCHEDOP_block, 0);
+}
+
+unsigned long read_cr2(void)
+{
+       return HYPERVISOR_shared_info->vcpu_info[smp_processor_id()].arch.cr2;
+}
-- 
2.1.4


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.