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

[Minios-devel] [UNIKRAFT PATCH 01/23] plat/common: Introduce hardware context


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Mon, 8 Jul 2019 11:33:30 +0300
  • Cc: felipe.huici@xxxxxxxxx, simon.kuenzer@xxxxxxxxx
  • Delivery-date: Mon, 08 Jul 2019 08:50:17 +0000
  • Ironport-phdr: 9a23:ApJCshxDQWqz9znXCy+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: 6Y/R+LP0pwd+Za+3aXoNC2cePFOr2IeInEp8fS+NWZPNpuOU4Pu2UYdMMkjxuYuv3jQqiSiS9r gDo8PKXZoAIw==
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

Until now, the only type of scheduling Unikraft supported was cooperative
scheduling which introduced the software context as abstraction used when
switching threads. In prepare for supporting preemptive scheduling, we
introduce the hardware context which leverages the hardware support when
switching threads preemptively. The hardware context includes the generic
registers which are pushed on the exception stack (SS, SP, EFLAGS, CS, IP) and
the rest of generic registers for which we need to save their values between
thread runs.

Unlike general purpose OSes, unikernels bring a new situation when switching
from one thread to another. In general purpose OSes, an interrupted thread
would always be replaced with another interrupted thread. Here we mean by
"interrupted" that the threads are interrupted by an IRQ or by a syscall -
yielding also involves a software interrupt (the syscall). On the other hand,
for unikernels, yielding doesn't change the execution context, therefore
switching from an yielding thread to an interrupted thread, or viceversa,
becomes a special case for preemptive scheduling on unikernels. This patch only
takes care of the case when switching interrupted threads to interrupted
threads.

We also enforce the thread context allocation to have 16-byte alignment, the
same as the alignment for the exception stack.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 plat/common/include/hw_ctx.h     |  53 ++++++++++++++++
 plat/common/include/x86/regs.h   |   2 +-
 plat/common/thread.c             |  13 +++-
 plat/common/x86/hw_ctx.c         | 101 +++++++++++++++++++++++++++++++
 plat/kvm/Makefile.uk             |   1 +
 plat/kvm/include/kvm-x86/traps.h |   1 +
 plat/xen/Makefile.uk             |   3 +
 7 files changed, 171 insertions(+), 3 deletions(-)
 create mode 100644 plat/common/include/hw_ctx.h
 create mode 100644 plat/common/x86/hw_ctx.c

diff --git a/plat/common/include/hw_ctx.h b/plat/common/include/hw_ctx.h
new file mode 100644
index 00000000..c770b1fe
--- /dev/null
+++ b/plat/common/include/hw_ctx.h
@@ -0,0 +1,53 @@
+/* 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_CMN_HW_CTX_H__
+#define __PLAT_CMN_HW_CTX_H__
+
+#include <stdbool.h>
+#include <uk/plat/thread.h>
+/* TODO include a generic header for __regs structure */
+#ifdef __X86_64__
+#include <x86/regs.h>
+#else
+#error "Create regs.h for current architecture"
+#endif
+
+struct hw_ctx {
+       struct __regs regs;  /* Generic CPU registers */
+       bool interrupted;    /* True if thread was interrupted */
+};
+
+void hw_ctx_callbacks_init(struct ukplat_ctx_callbacks *ctx_cbs);
+
+#endif /* __PLAT_CMN_HW_CTX_H__ */
diff --git a/plat/common/include/x86/regs.h b/plat/common/include/x86/regs.h
index a6a847e4..1eb4936f 100644
--- a/plat/common/include/x86/regs.h
+++ b/plat/common/include/x86/regs.h
@@ -89,6 +89,6 @@ struct __regs {
 #endif
 
 /* This should be better defined in the thread header */
-#define OFFSETOF_UKTHREAD_REGS  16
+#define OFFSETOF_UKTHREAD_REGS  24
 
 #endif /* __UKARCH_REGS_H__ */
diff --git a/plat/common/thread.c b/plat/common/thread.c
index 242da09d..bbdde13e 100644
--- a/plat/common/thread.c
+++ b/plat/common/thread.c
@@ -40,6 +40,9 @@
 #include <x86/cpu.h> /* TODO revisit for ARM */
 #include <tls.h>
 #include <sw_ctx.h>
+#if defined(CONFIG_HAVE_SCHED_PREEMPT) && !defined(CONFIG_PARAVIRT)
+#include <hw_ctx.h>
+#endif
 
 
 struct thread_context *ukplat_thread_ctx_create(
@@ -50,6 +53,7 @@ struct thread_context *ukplat_thread_ctx_create(
        struct thread_context *thread_ctx;
        void *ctx, *extregs;
        __sz sz;
+       int rc;
 
        UK_ASSERT(cbs != NULL);
        UK_ASSERT(allocator != NULL);
@@ -62,8 +66,8 @@ struct thread_context *ukplat_thread_ctx_create(
                + x86_cpu_features.extregs_size
                + sizeof(struct thread_context);
 
-       ctx = uk_malloc(allocator, sz);
-       if (ctx == NULL) {
+       rc = uk_posix_memalign(allocator, (void **) &ctx, 16, sz);
+       if (rc != 0 || ctx == NULL) {
                uk_pr_warn("Error allocating thread context.");
                return NULL;
        }
@@ -127,6 +131,11 @@ int ukplat_ctx_callbacks_init(struct ukplat_ctx_callbacks 
*ctx_cbs,
        UK_ASSERT(ctx_cbs != NULL);
 
        switch (ctx_type) {
+       case ukplat_ctx_hw:
+#if defined(CONFIG_HAVE_SCHED_PREEMPT) && !defined(CONFIG_PARAVIRT)
+               hw_ctx_callbacks_init(ctx_cbs);
+               break;
+#endif
        case ukplat_ctx_sw:
                sw_ctx_callbacks_init(ctx_cbs);
                break;
diff --git a/plat/common/x86/hw_ctx.c b/plat/common/x86/hw_ctx.c
new file mode 100644
index 00000000..c7b8dbac
--- /dev/null
+++ b/plat/common/x86/hw_ctx.c
@@ -0,0 +1,101 @@
+/* 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/plat/thread.h>
+#include <uk/plat/memory.h>
+#include <uk/alloc.h>
+#include <uk/assert.h>
+#include <x86/cpu_defs.h>
+#include <x86/regs.h>
+#include <x86/traps.h>
+#include <kvm-x86/traps.h>
+#include <hw_ctx.h>
+
+static void  hw_ctx_init(void *ctx, unsigned long sp);
+static void  hw_ctx_start(void *ctx) __noreturn;
+static void  hw_ctx_switch(void *prevctx, void *nextctx);
+
+extern void asm_thread_starter(void);
+
+/* Architecture specific setup of thread creation */
+void hw_ctx_init(void *ctx, unsigned long sp)
+{
+       struct hw_ctx *hw_ctx;
+
+       UK_ASSERT(ctx != NULL);
+       hw_ctx = ctx;
+
+       /* Setup exception stack */
+       hw_ctx->regs.rip = (unsigned long) asm_thread_starter;
+       hw_ctx->regs.cs = GDT_DESC_SELECTOR(CODE);
+       hw_ctx->regs.eflags = X86_EFLAGS_IF;
+       hw_ctx->regs.rsp = sp;
+       hw_ctx->regs.ss = GDT_DESC_SELECTOR(DATA);
+}
+
+extern void asm_ctx_start(unsigned long sp, unsigned long ip) __noreturn;
+
+void hw_ctx_start(void *ctx)
+{
+       struct hw_ctx *hw_ctx = ctx;
+
+       UK_ASSERT(hw_ctx != NULL);
+
+       /* Switch stacks and run the thread */
+       asm_ctx_start(hw_ctx->regs.rsp, hw_ctx->regs.rip);
+
+       UK_CRASH("Thread did not start.");
+}
+
+static void hw_ctx_switch(void *prevctx, void *nextctx)
+{
+       struct hw_ctx *prev_hw_ctx = prevctx;
+       struct hw_ctx *next_hw_ctx = nextctx;
+
+       if (ukplat_irq_context()) {
+               /* IRQ context */
+               prev_hw_ctx->interrupted = true;
+               next_hw_ctx->interrupted = false;
+       }
+}
+
+void hw_ctx_callbacks_init(struct ukplat_ctx_callbacks *ctx_cbs)
+{
+       UK_ASSERT(ctx_cbs != NULL);
+       ctx_cbs->ctx_size = sizeof(struct hw_ctx);
+       ctx_cbs->init_cb = hw_ctx_init;
+       ctx_cbs->start_cb = hw_ctx_start;
+       ctx_cbs->switch_cb = hw_ctx_switch;
+}
diff --git a/plat/kvm/Makefile.uk b/plat/kvm/Makefile.uk
index 8eb162d4..50319a6e 100644
--- a/plat/kvm/Makefile.uk
+++ b/plat/kvm/Makefile.uk
@@ -34,6 +34,7 @@ LIBKVMPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/cpu_features
 LIBKVMPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/cpu_native.c|common
 ifeq ($(CONFIG_HAVE_SCHED),y)
 LIBKVMPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/thread_start.S|common
+LIBKVMPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/hw_ctx.c|common
 LIBKVMPLAT_SRCS-$(CONFIG_ARCH_X86_64) += $(UK_PLAT_COMMON_BASE)/thread.c|common
 LIBKVMPLAT_SRCS-$(CONFIG_ARCH_X86_64) += $(UK_PLAT_COMMON_BASE)/sw_ctx.c|common
 endif
diff --git a/plat/kvm/include/kvm-x86/traps.h b/plat/kvm/include/kvm-x86/traps.h
index 39bf0386..506afffb 100644
--- a/plat/kvm/include/kvm-x86/traps.h
+++ b/plat/kvm/include/kvm-x86/traps.h
@@ -33,6 +33,7 @@
 #define GDT_DESC_TSS_LO         3
 #define GDT_DESC_TSS_HI         4
 #define GDT_DESC_TSS            GDT_DESC_TSS_LO
+#define GDT_DESC_SELECTOR(a)    (GDT_DESC_ ##a << 3)
 
 #define GDT_DESC_OFFSET(n)      ((n) * 0x8)
 #define GDT_NUM_ENTRIES         5
diff --git a/plat/xen/Makefile.uk b/plat/xen/Makefile.uk
index 69b10efc..48b8814b 100644
--- a/plat/xen/Makefile.uk
+++ b/plat/xen/Makefile.uk
@@ -37,6 +37,9 @@ LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/traps.c|comm
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/cpu_features.c|common
 ifeq ($(CONFIG_HAVE_SCHED),y)
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/thread_start.S|common
+ifeq ($(XEN_HVMLITE),y)
+LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += 
$(UK_PLAT_COMMON_BASE)/x86/hw_ctx.c|common
+endif
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += $(UK_PLAT_COMMON_BASE)/thread.c|common
 LIBXENPLAT_SRCS-$(CONFIG_ARCH_X86_64) += $(UK_PLAT_COMMON_BASE)/sw_ctx.c|common
 endif
-- 
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®.