[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH 01/23] plat/common: Introduce hardware context
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
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |