aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaleem Abdulrasool <abdulras@google.com>2021-05-07 16:09:31 -0700
committerGitHub <noreply@github.com>2021-05-07 16:09:31 -0700
commit5450c2f731f16abe3a4f244c383c55f559c97359 (patch)
tree5ebfcf70b9fff2775002dc9f8615dfe311d690e1
parent23f1834fc628f4093bdd5e322ac828956d185f8d (diff)
downloadpk-5450c2f731f16abe3a4f244c383c55f559c97359.zip
pk-5450c2f731f16abe3a4f244c383c55f559c97359.tar.gz
pk-5450c2f731f16abe3a4f244c383c55f559c97359.tar.bz2
machine: fix a case of undefined behaviour with SP handling (#245)
The use of `asm` for register aliasing is supported in two different contexts: - local variables (including GNU expression statements) where it may only be used for specifying registers for input and output operands to extended `asm` syntax. c.f. https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html#Local-Register-Variables - global variables where it may be used to observe the contents of a register. c.f. https://gcc.gnu.org/onlinedocs/gcc/Global-Register-Variables.html#Global-Register-Variables The two options here is to either to hoist the variable out into a global variable, but then it should not be in a header due to fears of ODR in case the optimizer does not inline it away, and thus becomes a bit more tricky. The alternative that this change actually adopts is to explicitly use a move to copy the value out via the GNU extended assembly syntax. With this change, it is now possible to build the Proxy Kernel completely with clang/LLVM and link with LLD. The generated kernel also runs under SPIKE and behaves as expected in a simple smoke test (without any executable prints the expected message, and runs a trivial RVV example).
-rw-r--r--machine/mtrap.h11
1 files changed, 8 insertions, 3 deletions
diff --git a/machine/mtrap.h b/machine/mtrap.h
index a8dbeff..e9ef139 100644
--- a/machine/mtrap.h
+++ b/machine/mtrap.h
@@ -48,9 +48,14 @@ typedef struct {
volatile uint32_t* plic_s_ie;
} hls_t;
-#define MACHINE_STACK_TOP() ({ \
- register uintptr_t sp asm ("sp"); \
- (void*)((sp + RISCV_PGSIZE) & -RISCV_PGSIZE); })
+#define STACK_POINTER() ({ \
+ uintptr_t __sp; \
+ __asm__("mv %0, sp" : "=r"(__sp)); \
+ __sp; \
+})
+
+#define MACHINE_STACK_TOP() \
+ ({ (void*)((STACK_POINTER() + RISCV_PGSIZE) & -RISCV_PGSIZE); })
// hart-local storage, at top of stack
#define HLS() ((hls_t*)(MACHINE_STACK_TOP() - HLS_SIZE))