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

[UNIKRAFT PATCH 2/4] include/: No dependency to `lib/ukalloc` for thread contexts



Changes the platform API in a way so that there is no dependency to
`ukalloc` for context creation. An API function is added to query the
allocation size for a thread context. Another API function is added to
call the platform-specific thread context initialization.

Signed-off-by: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
---
 include/uk/plat/thread.h        | 33 ++++++++++++++++++------------
 lib/uksched/include/uk/thread.h |  1 +
 lib/uksched/thread.c            | 26 ++++++++++++++++++------
 plat/common/sw_ctx.c            | 36 ++++++++++++++++-----------------
 4 files changed, 58 insertions(+), 38 deletions(-)

diff --git a/include/uk/plat/thread.h b/include/uk/plat/thread.h
index 8f3b0cea..16cfdb5a 100644
--- a/include/uk/plat/thread.h
+++ b/include/uk/plat/thread.h
@@ -48,19 +48,20 @@ enum ukplat_ctx_type {
        ukplat_ctx_sw,
 };
 
-struct uk_alloc;
-
-typedef void *(*ukplat_ctx_create_func_t)
-               (struct uk_alloc *allocator, unsigned long sp,
-                unsigned long tlsp);
+typedef __sz  (*ukplat_ctx_size_func_t)
+               (void);
+typedef void  (*ukplat_ctx_init_func_t)
+               (void *ctx, unsigned long sp, unsigned long tlsp);
 typedef void  (*ukplat_ctx_start_func_t)
                (void *ctx);
 typedef void  (*ukplat_ctx_switch_func_t)
                (void *prevctx, void *nextctx);
 
 struct ukplat_ctx_callbacks {
-       /* callback for creating thread context */
-       ukplat_ctx_create_func_t create_cb;
+       /* callback for returning the needed size for a thread context */
+       ukplat_ctx_size_func_t size_cb;
+       /* callback for initializing a new thread context */
+       ukplat_ctx_init_func_t init_cb;
        /* callback for starting thread context */
        ukplat_ctx_start_func_t start_cb __noreturn;
        /* callback for switching contexts */
@@ -72,17 +73,23 @@ int ukplat_ctx_callbacks_init(struct ukplat_ctx_callbacks 
*ctx_cbs,
 
 
 static inline
-void *ukplat_thread_ctx_create(struct ukplat_ctx_callbacks *cbs,
-               struct uk_alloc *allocator, unsigned long sp,
-               unsigned long tlsp)
+__sz ukplat_thread_ctx_size(struct ukplat_ctx_callbacks *cbs)
 {
        UK_ASSERT(cbs != __NULL);
-       UK_ASSERT(allocator != __NULL);
 
-       return cbs->create_cb(allocator, sp, tlsp);
+       return cbs->size_cb();
 }
 
-void ukplat_thread_ctx_destroy(struct uk_alloc *allocator, void *ctx);
+static inline
+void ukplat_thread_ctx_init(struct ukplat_ctx_callbacks *cbs,
+                           void *ctx,
+                           unsigned long sp, unsigned long tlsp)
+{
+       UK_ASSERT(cbs != __NULL);
+       UK_ASSERT(ctx != __NULL);
+
+       return cbs->init_cb(ctx, sp, tlsp);
+}
 
 static inline
 void ukplat_thread_ctx_start(struct ukplat_ctx_callbacks *cbs,
diff --git a/lib/uksched/include/uk/thread.h b/lib/uksched/include/uk/thread.h
index d8a4ac88..15490517 100644
--- a/lib/uksched/include/uk/thread.h
+++ b/lib/uksched/include/uk/thread.h
@@ -33,6 +33,7 @@
 #ifdef CONFIG_LIBNEWLIBC
 #include <sys/reent.h>
 #endif
+#include <uk/alloc.h>
 #include <uk/arch/lcpu.h>
 #include <uk/arch/time.h>
 #include <uk/plat/thread.h>
diff --git a/lib/uksched/thread.c b/lib/uksched/thread.c
index f8ae786c..909e242f 100644
--- a/lib/uksched/thread.c
+++ b/lib/uksched/thread.c
@@ -94,6 +94,8 @@ int uk_thread_init(struct uk_thread *thread,
                void (*function)(void *), void *arg)
 {
        unsigned long sp;
+       void *ctx;
+       int ret = 0;
 
        UK_ASSERT(thread != NULL);
        UK_ASSERT(stack != NULL);
@@ -104,12 +106,15 @@ int uk_thread_init(struct uk_thread *thread,
 
        init_sp(&sp, stack, function, arg);
 
-       /* Call platform specific setup. */
-       thread->ctx = ukplat_thread_ctx_create(cbs, allocator, sp,
-                       (uintptr_t)ukarch_tls_pointer(tls));
-       if (thread->ctx == NULL)
-               return -1;
+       /* Allocate thread context */
+       ctx = uk_zalloc(allocator, ukplat_thread_ctx_size(cbs));
+       if (!ctx) {
+               ret = -1;
+               goto err_out;
+       }
 
+       memset(thread, 0, sizeof(*thread));
+       thread->ctx = ctx;
        thread->name = name;
        thread->stack = stack;
        thread->tls = tls;
@@ -126,16 +131,25 @@ int uk_thread_init(struct uk_thread *thread,
        reent_init(&thread->reent);
 #endif
 
+       /* Platform specific context initialization */
+       ukplat_thread_ctx_init(cbs, thread->ctx, sp,
+                              (uintptr_t) ukarch_tls_pointer(tls));
+
        uk_pr_info("Thread \"%s\": pointer: %p, stack: %p, tls: %p\n",
                   name, thread, thread->stack, thread->tls);
 
        return 0;
+
+err_out:
+       return ret;
 }
 
 void uk_thread_fini(struct uk_thread *thread, struct uk_alloc *allocator)
 {
        UK_ASSERT(thread != NULL);
-       ukplat_thread_ctx_destroy(allocator, thread->ctx);
+
+       uk_free(allocator, thread->ctx);
+       thread->ctx = NULL;
 }
 
 static void uk_thread_block_until(struct uk_thread *thread, __snsec until)
diff --git a/plat/common/sw_ctx.c b/plat/common/sw_ctx.c
index 482e99e6..b941caeb 100644
--- a/plat/common/sw_ctx.c
+++ b/plat/common/sw_ctx.c
@@ -40,8 +40,8 @@
 #include <uk/plat/common/tls.h>
 #include <uk/plat/common/cpu.h>
 
-static void *sw_ctx_create(struct uk_alloc *allocator, unsigned long sp,
-                               unsigned long tlsp);
+static size_t sw_ctx_size(void);
+static void  sw_ctx_init(void *ctx, unsigned long sp, unsigned long tlsp);
 static void  sw_ctx_start(void *ctx) __noreturn;
 static void  sw_ctx_switch(void *prevctx, void *nextctx);
 
@@ -51,27 +51,24 @@ static void  sw_ctx_switch(void *prevctx, void *nextctx);
  */
 extern void asm_thread_starter(void);
 
-static void *sw_ctx_create(struct uk_alloc *allocator, unsigned long sp,
-                               unsigned long tlsp)
+static size_t sw_ctx_size(void)
 {
-       struct sw_ctx *ctx;
-
-       UK_ASSERT(allocator != NULL);
+       return sizeof(struct sw_ctx);
+}
 
-       ctx = arch_alloc_sw_ctx(allocator);
-       if (ctx == NULL) {
-               uk_pr_warn("Error allocating software context.");
-               return NULL;
-       }
+static void sw_ctx_init(void *ctx,
+                       unsigned long sp, unsigned long tlsp)
+{
+       struct sw_ctx *sw_ctx = ctx;
 
-       ctx->sp = sp;
-       ctx->tlsp = tlsp;
-       ctx->ip = (unsigned long) asm_thread_starter;
-       arch_init_extregs(ctx);
+       UK_ASSERT(sw_ctx != NULL);
 
-       save_extregs(ctx);
+       sw_ctx->sp   = sp;
+       sw_ctx->tlsp = tlsp;
+       sw_ctx->ip   = (unsigned long) asm_thread_starter;
+       arch_init_extregs(sw_ctx);
 
-       return ctx;
+       save_extregs(sw_ctx);
 }
 
 extern void asm_ctx_start(unsigned long sp, unsigned long ip) __noreturn;
@@ -105,7 +102,8 @@ static void sw_ctx_switch(void *prevctx, void *nextctx)
 void sw_ctx_callbacks_init(struct ukplat_ctx_callbacks *ctx_cbs)
 {
        UK_ASSERT(ctx_cbs != NULL);
-       ctx_cbs->create_cb = sw_ctx_create;
+       ctx_cbs->size_cb = sw_ctx_size;
+       ctx_cbs->init_cb = sw_ctx_init;
        ctx_cbs->start_cb = sw_ctx_start;
        ctx_cbs->switch_cb = sw_ctx_switch;
 }
-- 
2.20.1



 


Rackspace

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