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

[Minios-devel] [UNIKRAFT PATCH 09/23] plat/xen: Introduce paravirtualization context


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Mon, 8 Jul 2019 11:33:38 +0300
  • Cc: felipe.huici@xxxxxxxxx, simon.kuenzer@xxxxxxxxx
  • Delivery-date: Mon, 08 Jul 2019 08:50:21 +0000
  • Ironport-phdr: 9a23:IQm6URz1T/PlAC/XCy+O+j09IxM/srCxBDY+r6Qd2+0TIJqq85mqBkHD//Il1AaPAdyBra4cwLKK+4nbGkU4qa6bt34DdJEeHzQksu4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2aFLduGC94iAPERvjKwV1Ov71GonPhMiryuy+4ZLebxhWiDanbr5+MBq6oRneu8ILnYZsN6E9xwfTrHBVYepW32RoJVySnxb4+Mi9+YNo/jpTtfw86cNOSL32cKskQ7NWCjQmKH0169bwtRbfVwuP52ATXXsQnxFVHgXK9hD6XpP2sivnqupw3TSRMMPqQbwoXzmp8qFmQwLqhigaLT406GHZhNJtgqxVoxyvoB5ww4DPbY2JKPZzZL/RcMkGSWZdWMtaSixPApm7b4sKF+cMI+FYr5Lhp1sPqxu1GA+iBP73yj9Vm3T72rE10+M6EQHa3QwgGcgCsHDJrNXtKacfSvy6zLLSwTXbcvNZwi3x6JLPch04p/yHQLF+cdLJxEUyGA7Jk0+cpI/lMj+PyOgBrWqW4/BuWO63lmIqpBx9riKsy8oskIXFmIwYx17e+Slkz4s5O9u1Q1Nhb9G+CptfrSSaOpNzQsMlXm5npj43yqYDuZ6nZCgKz4knxwLHZ/yHbYeI5hXjWf6PITd9nn1leba/iwyu/ki70OH8TtS0301QoipfldnArnEN1xrN5cibUvZx40is1SuV2w3Q6uxIO144mbTZJpI7zLM8jp8Tvl7CHi/ylkX2lqiWdkA89+e07OTneanmpp6aN4NujgH+L7wumtGkDOskKQgCRXCb+fmn27H55035R61GjucqnanBrJDaOcMbq7a8Aw9U1IYj6hG/Dyy/3NsFg3YHMkxKeBacgojtOlHOO+z4Deylj1WjjjhrwerKPrr7ApXCNnLDiqvufa5h605Azwo+1d5f6IxQCrEAOPL8RFX9tNzFDh84LwO0wv3qCNNj2YwCXWKAGLSWPLnMvl+V/ugvOfWDZJcJuDbhLPgo/+XujX48mV8ae6mlx5gXaG2mEfRgIkSWf2Dsj8wHEWgUogU+SPblh0aYXTFNenbhF547szQ6DoOhFsLPS56ghJSF3TynBdtGa2YADUqDQlnycIDRcPAXdCOUaut8iiFMAbOmUJMg01eqqRfn47F8aPLJ8GsCssSwh5BO++TPmERqpnRPBMOH3jTVQg==
  • Ironport-sdr: ZdwsbXzDFYTWNp+jz3S2HeiEaHlU5/ID2LaZL1U9OgCJmvzNR2NBbisZUZDrqZjc5S8oQb0GVy g58zx9XPQuIg==
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

With Xen paravirtualization we cannot use the hardware context because instead
of saving the exception registers on a preconfigured stack, the hypervisor
chooses to use the current thread stack for saving them. Therefore we introduce
a new type of context switch: the paravirtualization context. It is a hybrid
between the software context, given that it does not use any hardware support,
and the hardware context from which it keeps the 'interrupted' field that is,
of course, needed for saving info when preempting threads.

All four cases of context switches that happened for hardware contexts also
apply for paravirtualization contexts, but we choose to handle only the
simplest ones in this patch: switching an interrupted thread to another
previously interrupted thread and yielding to a previously yielding thread.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 plat/common/thread.c              |  16 +++--
 plat/xen/Makefile.uk              |   3 +
 plat/xen/include/xen-x86/pv_ctx.h |  50 ++++++++++++++
 plat/xen/x86/pv_ctx.c             | 111 ++++++++++++++++++++++++++++++
 4 files changed, 176 insertions(+), 4 deletions(-)
 create mode 100644 plat/xen/include/xen-x86/pv_ctx.h
 create mode 100644 plat/xen/x86/pv_ctx.c

diff --git a/plat/common/thread.c b/plat/common/thread.c
index bbdde13e..4535193d 100644
--- a/plat/common/thread.c
+++ b/plat/common/thread.c
@@ -40,9 +40,13 @@
 #include <x86/cpu.h> /* TODO revisit for ARM */
 #include <tls.h>
 #include <sw_ctx.h>
-#if defined(CONFIG_HAVE_SCHED_PREEMPT) && !defined(CONFIG_PARAVIRT)
+#if CONFIG_HAVE_SCHED_PREEMPT
+#if CONFIG_PARAVIRT
+#include <xen-x86/pv_ctx.h>
+#else
 #include <hw_ctx.h>
-#endif
+#endif /* CONFIG_PARAVIRT */
+#endif /* CONFIG_HAVE_SCHED_PREEMPT */
 
 
 struct thread_context *ukplat_thread_ctx_create(
@@ -131,11 +135,15 @@ int ukplat_ctx_callbacks_init(struct ukplat_ctx_callbacks 
*ctx_cbs,
        UK_ASSERT(ctx_cbs != NULL);
 
        switch (ctx_type) {
+#if CONFIG_HAVE_SCHED_PREEMPT
        case ukplat_ctx_hw:
-#if defined(CONFIG_HAVE_SCHED_PREEMPT) && !defined(CONFIG_PARAVIRT)
+#if CONFIG_PARAVIRT
+               pv_ctx_callbacks_init(ctx_cbs);
+#else
                hw_ctx_callbacks_init(ctx_cbs);
+#endif /* CONFIG_PARAVIRT */
                break;
-#endif
+#endif /* CONFIG_HAVE_SCHED_PREEMPT */
        case ukplat_ctx_sw:
                sw_ctx_callbacks_init(ctx_cbs);
                break;
diff --git a/plat/xen/Makefile.uk b/plat/xen/Makefile.uk
index 48b8814b..57589e6f 100644
--- a/plat/xen/Makefile.uk
+++ b/plat/xen/Makefile.uk
@@ -54,6 +54,9 @@ ifneq ($(CONFIG_XEN_HVMLITE),y)
 LIBXENPLAT_ASFLAGS-y           += -DCONFIG_PARAVIRT
 LIBXENPLAT_CFLAGS-y            += -DCONFIG_PARAVIRT
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += $(LIBXENPLAT_BASE)/x86/cpu_pv.c
+ifeq ($(CONFIG_HAVE_SCHED_PREEMPT),y)
+LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += $(LIBXENPLAT_BASE)/x86/pv_ctx.c
+endif # CONFIG_HAVE_SCHED_PREEMPT
 else
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/cpu_native.c
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_ARM_32) += 
$(UK_PLAT_COMMON_BASE)/arm/cpu_native.c
diff --git a/plat/xen/include/xen-x86/pv_ctx.h 
b/plat/xen/include/xen-x86/pv_ctx.h
new file mode 100644
index 00000000..c7a6fa63
--- /dev/null
+++ b/plat/xen/include/xen-x86/pv_ctx.h
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2019, University Politehnica of Bucharest. 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.
+ */
+#ifndef __PLAT_XEN_PV_CTX_H__
+#define __PLAT_XEN_PV_CTX_H__
+
+#ifndef __ASSEMBLY__
+#include <stdbool.h>
+#include <uk/plat/thread.h>
+#include <sw_ctx.h>
+
+struct pv_ctx {
+       struct sw_ctx sw_ctx;
+       bool interrupted;
+};
+
+void pv_ctx_callbacks_init(struct ukplat_ctx_callbacks *ctx_cbs);
+#endif
+
+#endif /* __PLAT_XEN_PV_CTX_H__ */
diff --git a/plat/xen/x86/pv_ctx.c b/plat/xen/x86/pv_ctx.c
new file mode 100644
index 00000000..a354621e
--- /dev/null
+++ b/plat/xen/x86/pv_ctx.c
@@ -0,0 +1,111 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2019, University Politehnica of Bucharest. 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 <stdlib.h>
+#include <uk/alloc.h>
+#include <uk/assert.h>
+#include <uk/arch/limits.h>
+#include <uk/plat/memory.h>
+#include <x86/cpu_defs.h>
+#include <xen-x86/traps.h>
+#include <xen-x86/pv_ctx.h>
+
+
+static void  pv_ctx_init(void *ctx, unsigned long sp);
+static void  pv_ctx_start(void *ctx) __noreturn;
+static void  pv_ctx_switch(void *prevctx, void *nextctx);
+
+extern void asm_thread_starter(void);
+
+static void pv_ctx_init(void *ctx, unsigned long sp)
+{
+       struct pv_ctx *pv_ctx;
+
+       UK_ASSERT(ctx != NULL);
+       pv_ctx = ctx;
+
+       pv_ctx->sw_ctx.sp = sp;
+       pv_ctx->sw_ctx.ip = (unsigned long) asm_thread_starter;
+       pv_ctx->interrupted = false;
+}
+
+extern void asm_ctx_start(unsigned long sp, unsigned long ip) __noreturn;
+
+static void pv_ctx_start(void *ctx)
+{
+       struct pv_ctx *pv_ctx = ctx;
+
+       UK_ASSERT(pv_ctx != NULL);
+
+       /* Switch stacks and run the thread */
+       asm_ctx_start(pv_ctx->sw_ctx.sp, pv_ctx->sw_ctx.ip);
+
+       UK_CRASH("Thread did not start.");
+}
+
+extern void asm_sw_ctx_switch(void *prevctx, void *nextctx);
+
+static void pv_ctx_switch(void *prevctx, void *nextctx)
+{
+       struct pv_ctx *prev_hw_ctx = prevctx;
+       struct pv_ctx *next_hw_ctx = nextctx;
+
+       if (ukplat_irq_context()) {
+               /* IRQ context */
+               prev_hw_ctx->interrupted = true;
+
+               if (next_hw_ctx->interrupted)
+                       /* switching interrupted to interrupted */
+                       next_hw_ctx->interrupted = false;
+
+       } else {
+               /* yielding */
+               if (next_hw_ctx->interrupted) {
+                       /* switching yielding to interrupted */
+                       next_hw_ctx->interrupted = false;
+
+               } else
+                       /* switching yielding to yielding */
+                       asm_sw_ctx_switch(prevctx, nextctx);
+       }
+}
+
+void pv_ctx_callbacks_init(struct ukplat_ctx_callbacks *ctx_cbs)
+{
+       UK_ASSERT(ctx_cbs != NULL);
+       ctx_cbs->ctx_size = sizeof(struct pv_ctx);
+       ctx_cbs->init_cb = pv_ctx_init;
+       ctx_cbs->start_cb = pv_ctx_start;
+       ctx_cbs->switch_cb = pv_ctx_switch;
+}
-- 
2.20.1


_______________________________________________
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®.