From 11a40293be57da25677af1d3a3692d0c1a156212 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 12 Dec 2020 07:05:09 +0000 Subject: [PATCH] riscv64: simplify the context swapping --- arch/riscv64/makecontext.c | 5 ++--- arch/riscv64/setcontext.S | 4 ++-- arch/riscv64/startcontext.S | 7 ++----- arch/riscv64/swapcontext.S | 4 ++-- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/arch/riscv64/makecontext.c b/arch/riscv64/makecontext.c index 23d054b..d8609f8 100644 --- a/arch/riscv64/makecontext.c +++ b/arch/riscv64/makecontext.c @@ -37,10 +37,9 @@ libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int arg /* set up the ucontext structure */ ucp->uc_mcontext.__gregs[REG_RA] = (libucontext_greg_t) libucontext_trampoline; ucp->uc_mcontext.__gregs[REG_S0] = 0; - ucp->uc_mcontext.__gregs[REG_S1] = (libucontext_greg_t) func; - ucp->uc_mcontext.__gregs[REG_S2] = (libucontext_greg_t) ucp->uc_link; + ucp->uc_mcontext.__gregs[REG_S1] = (libucontext_greg_t) ucp->uc_link; ucp->uc_mcontext.__gregs[REG_SP] = (libucontext_greg_t) sp; - ucp->uc_mcontext.__gregs[REG_PC] = 0; + ucp->uc_mcontext.__gregs[REG_PC] = (libucontext_greg_t) func; va_start(va, argc); diff --git a/arch/riscv64/setcontext.S b/arch/riscv64/setcontext.S index 18f9545..6b76f92 100644 --- a/arch/riscv64/setcontext.S +++ b/arch/riscv64/setcontext.S @@ -50,6 +50,6 @@ FUNC(libucontext_setcontext) ld s10, REG_OFFSET(REG_S10)(t0) ld s11, REG_OFFSET(REG_S11)(t0) - /* done saving, return */ - ret + /* done restoring, jump to new pc in S1 */ + jr t1 END(libucontext_setcontext) diff --git a/arch/riscv64/startcontext.S b/arch/riscv64/startcontext.S index 0174afa..0ba2d43 100644 --- a/arch/riscv64/startcontext.S +++ b/arch/riscv64/startcontext.S @@ -13,14 +13,11 @@ #include "defs.h" FUNC(libucontext_trampoline) - /* call the function passed to makecontext() */ - jalr s1 - /* check for linked context */ - beqz s2, no_linked_context + beqz s1, no_linked_context /* if one is set, invoke it */ - mv a0, s2 + mv a0, s1 jal libucontext_setcontext /* otherwise, exit. */ diff --git a/arch/riscv64/swapcontext.S b/arch/riscv64/swapcontext.S index f5757f2..900bdd2 100644 --- a/arch/riscv64/swapcontext.S +++ b/arch/riscv64/swapcontext.S @@ -75,6 +75,6 @@ FUNC(libucontext_swapcontext) ld s10, REG_OFFSET(REG_S10)(t0) ld s11, REG_OFFSET(REG_S11)(t0) - /* done swapping, return */ - ret + /* done swapping, jump to new PC in S1 */ + jr t1 END(libucontext_swapcontext)