Created
December 29, 2014 22:08
-
-
Save justincormack/fb0cd45de85f6b2e2817 to your computer and use it in GitHub Desktop.
asm patch for rumpfiber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -urN --exclude CVS /home/justin/netbsd/src/lib/librumpuser/arch/x86_64/rumpfiber_create_ctx.c lib/librumpuser/arch/x86_64/rumpfiber_create_ctx.c | |
--- /home/justin/netbsd/src/lib/librumpuser/arch/x86_64/rumpfiber_create_ctx.c 1970-01-01 01:00:00.000000000 +0100 | |
+++ lib/librumpuser/arch/x86_64/rumpfiber_create_ctx.c 2014-12-29 09:50:48.402573022 +0000 | |
@@ -0,0 +1,12 @@ | |
+#include <rumpfiber.h> | |
+ | |
+void | |
+arch_create_ctx(struct rf_context *ctx, void *stack, size_t stack_size, | |
+ void (*f)(void *), void *data) | |
+{ | |
+ | |
+ /* need to push stack pointer, f as ip and data as first arg */ | |
+ ctx->uc_regs[6] = (unsigned long) stack + stack_size; /* grows down */ | |
+ ctx->uc_regs[7] = (unsigned long) f; | |
+ ctx->uc_regs[8] = (unsigned long) data; | |
+} | |
diff -urN --exclude CVS /home/justin/netbsd/src/lib/librumpuser/arch/x86_64/rumpfiber_ctx.h lib/librumpuser/arch/x86_64/rumpfiber_ctx.h | |
--- /home/justin/netbsd/src/lib/librumpuser/arch/x86_64/rumpfiber_ctx.h 1970-01-01 01:00:00.000000000 +0100 | |
+++ lib/librumpuser/arch/x86_64/rumpfiber_ctx.h 2014-12-29 09:42:06.970547786 +0000 | |
@@ -0,0 +1 @@ | |
+#define RUMPFIBER_CTX_REGS 9 | |
diff -urN --exclude CVS /home/justin/netbsd/src/lib/librumpuser/arch/x86_64/rumpfiber_switch_ctx.s lib/librumpuser/arch/x86_64/rumpfiber_switch_ctx.s | |
--- /home/justin/netbsd/src/lib/librumpuser/arch/x86_64/rumpfiber_switch_ctx.s 1970-01-01 01:00:00.000000000 +0100 | |
+++ lib/librumpuser/arch/x86_64/rumpfiber_switch_ctx.s 2014-12-29 19:25:35.676807437 +0000 | |
@@ -0,0 +1,30 @@ | |
+.global arch_switch_ctx | |
+.type arch_switch_ctx,@function | |
+arch_switch_ctx: | |
+ mov %rbx,(%rdi) /* first buffer (prev) */ | |
+ mov %rbp,8(%rdi) | |
+ mov %r12,16(%rdi) | |
+ mov %r13,24(%rdi) | |
+ mov %r14,32(%rdi) | |
+ mov %r15,40(%rdi) | |
+ lea 8(%rsp),%rdx /* this is our rsp WITHOUT current ret addr */ | |
+ mov %rdx,48(%rdi) | |
+ mov (%rsp),%rdx /* save return addr ptr for new rip */ | |
+ mov %rdx,56(%rdi) | |
+ | |
+ mov (%rsi),%rbx /* second buffer (next) */ | |
+ mov 8(%rsi),%rbp | |
+ mov 16(%rsi),%r12 | |
+ mov 24(%rsi),%r13 | |
+ mov 32(%rsi),%r14 | |
+ mov 40(%rsi),%r15 | |
+ mov 48(%rsi),%rdx /* this ends up being the stack pointer */ | |
+ mov %rdx,%rsp | |
+ mov 56(%rsi),%rdx /* this is the instruction pointer */ | |
+ | |
+ mov 64(%rsi), %rdi /* load initial argument into rdi */ | |
+ | |
+ jmp *%rdx /* goto saved address without altering rsp */ | |
+ | |
+ | |
+ | |
diff -urN --exclude CVS /home/justin/netbsd/src/lib/librumpuser/Makefile lib/librumpuser/Makefile | |
--- /home/justin/netbsd/src/lib/librumpuser/Makefile 2014-11-09 17:51:19.448380820 +0000 | |
+++ lib/librumpuser/Makefile 2014-12-29 10:36:26.970940873 +0000 | |
@@ -6,9 +6,10 @@ | |
WARNS?= 5 | |
# rumpuser.h is in sys/rump for inclusion by kernel components | |
-.PATH: ${.CURDIR}/../../sys/rump/include/rump | |
+.PATH: ${.CURDIR}/../../sys/rump/include/rump ${.CURDIR}/${MACHINE_CPU} | |
RUMPUSER_THREADS?=pthread | |
+RUMPUSER_FIBER?=context | |
LIB= rumpuser | |
@@ -37,6 +38,22 @@ | |
.endif | |
SRCS= rumpfiber.c rumpfiber_bio.c | |
SRCS+= rumpfiber_sp.c | |
+.if ${RUMPUSER_FIBER} == "asm" | |
+ARCHDIR= ${.CURDIR}/arch/${MACHINE_CPU} | |
+.if exists (${ARCHDIR}) | |
+.PATH: ${ARCHDIR} | |
+SRCS+= rumpfiber_switch_ctx.s | |
+SRCS+= rumpfiber_create_ctx.c | |
+CPPFLAGS+= -I${ARCHDIR} | |
+CPPFLAGS.rumpfiber_create_ctx.c= -I${.CURDIR} | |
+INCS+= rumpfiber_ctx.h | |
+CPPFLAGS+= -UHAVE_CONTEXT | |
+.else | |
+.error No architecture specific assembly available for fibers | |
+.endif | |
+.else | |
+CPPFLAGS+= -DHAVE_CONTEXT | |
+.endif | |
.else | |
.error Unsupported rumpuser threading type: ${RUMPUSER_THREADS} | |
.endif | |
diff -urN --exclude CVS /home/justin/netbsd/src/lib/librumpuser/rumpfiber.c lib/librumpuser/rumpfiber.c | |
--- /home/justin/netbsd/src/lib/librumpuser/rumpfiber.c 2014-12-29 21:50:09.000000000 +0000 | |
+++ lib/librumpuser/rumpfiber.c 2014-12-29 22:03:52.521267057 +0000 | |
@@ -190,6 +190,7 @@ | |
} | |
} | |
+#ifdef HAVE_CONTEXT | |
static void | |
create_ctx(ucontext_t *ctx, void *stack, size_t stack_size, | |
void (*f)(void *), void *data) | |
@@ -203,6 +204,36 @@ | |
/* may have to do bounce function to call, if args to makecontext are ints */ | |
makecontext(ctx, (void (*)(void))f, 1, data); | |
} | |
+void | |
+switch_threads(struct thread *prev, struct thread *next) | |
+{ | |
+ int ret; | |
+ | |
+ current_thread = next; | |
+ ret = swapcontext(&prev->ctx, &next->ctx); | |
+ if (ret < 0) | |
+ abort(); | |
+} | |
+#else | |
+static void | |
+create_ctx(struct rf_context *ctx, void *stack, size_t stack_size, | |
+ void (*f)(void *), void *data) | |
+{ | |
+ | |
+ ctx->uc_stack.ss_sp = stack; | |
+ ctx->uc_stack.ss_size = stack_size; | |
+ ctx->uc_stack.ss_flags = 0; | |
+ ctx->uc_link = NULL; | |
+ arch_create_ctx(ctx, stack, stack_size, f, data); | |
+} | |
+void | |
+switch_threads(struct thread *prev, struct thread *next) | |
+{ | |
+ | |
+ current_thread = next; | |
+ arch_switch_ctx(&prev->ctx, &next->ctx); | |
+} | |
+#endif | |
/* TODO see notes in rumpuser_thread_create, have flags here */ | |
struct thread * | |
@@ -241,21 +272,6 @@ | |
return thread; | |
} | |
-static void | |
-switch_threads(struct thread *prev, struct thread *next) | |
-{ | |
- int ret; | |
- | |
- current_thread = next; | |
- if (scheduler_hook) | |
- scheduler_hook(prev->cookie, next->cookie); | |
- ret = swapcontext(&prev->ctx, &next->ctx); | |
- if (ret < 0) { | |
- printk("swapcontext failed\n"); | |
- abort(); | |
- } | |
-} | |
- | |
struct join_waiter { | |
struct thread *jw_thread; | |
struct thread *jw_wanted; | |
diff -urN --exclude CVS /home/justin/netbsd/src/lib/librumpuser/rumpfiber.h lib/librumpuser/rumpfiber.h | |
--- /home/justin/netbsd/src/lib/librumpuser/rumpfiber.h 2014-12-29 21:54:18.073239256 +0000 | |
+++ lib/librumpuser/rumpfiber.h 2014-12-29 22:04:46.769269683 +0000 | |
@@ -30,16 +30,41 @@ | |
#include <stdint.h> | |
#include <string.h> | |
#include <time.h> | |
-#include <ucontext.h> | |
#include <unistd.h> | |
+#ifdef HAVE_CONTEXT | |
+#include <ucontext.h> | |
+#else | |
+struct rf_stack { | |
+ void *ss_sp; | |
+ int ss_flags; | |
+ size_t ss_size; | |
+}; | |
+#include <rumpfiber_ctx.h> | |
+struct rf_context { | |
+ unsigned long uc_regs[RUMPFIBER_CTX_REGS]; | |
+ unsigned long uc_flags; | |
+ void *uc_link; | |
+ struct rf_stack uc_stack; | |
+}; | |
+void | |
+arch_create_ctx(struct rf_context *, void *, | |
+ size_t, void (*)(void *), void *); | |
+void | |
+arch_switch_ctx(struct rf_context *, struct rf_context *); | |
+#endif | |
+ | |
struct thread { | |
char *name; | |
void *lwp; | |
void *cookie; | |
int64_t wakeup_time; | |
TAILQ_ENTRY(thread) thread_list; | |
+#ifdef HAVE_CONTEXT | |
ucontext_t ctx; | |
+#else | |
+ struct rf_context ctx; | |
+#endif | |
uint32_t flags; | |
int threrrno; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment