Skip to content

Commit e876dee

Browse files
ltragercfriedt
authored andcommitted
arch: riscv: Move reg_read()/reg_write() into userspace test
Various drivers have their own static versions of reg_read()/reg_write(). When building for riscv the macros conflict with the driver function causing compiler failures. Prefix them with riscv_ to avoid conflict and move them into the unit test which is the only place they are used. Signed-off-by: Lee Trager <[email protected]>
1 parent 4b4dcbb commit e876dee

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

include/zephyr/arch/riscv/arch_inlines.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <zephyr/kernel_structs.h>
1313
#include "csr.h"
14-
#include "reg.h"
1514

1615
static ALWAYS_INLINE uint32_t arch_proc_id(void)
1716
{

include/zephyr/arch/riscv/reg.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/arch/riscv/userspace/riscv_gp/src/main.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <stdio.h>
88

9-
#include <zephyr/arch/riscv/reg.h>
109
#include <zephyr/kernel.h>
1110
#include <zephyr/ztest.h>
1211

@@ -16,13 +15,22 @@
1615

1716
#define ROGUE_USER_STACK_SZ 2048
1817

18+
#define riscv_reg_read(reg) \
19+
({ \
20+
register unsigned long __rv; \
21+
__asm__ volatile("mv %0, " STRINGIFY(reg) : "=r"(__rv)); \
22+
__rv; \
23+
})
24+
25+
#define riscv_reg_write(reg, val) ({ __asm__("mv " STRINGIFY(reg) ", %0" : : "r"(val)); })
26+
1927
static struct k_thread rogue_user_thread;
2028
static K_THREAD_STACK_DEFINE(rogue_user_stack, ROGUE_USER_STACK_SZ);
2129

2230
static void rogue_user_fn(void *p1, void *p2, void *p3)
2331
{
2432
zassert_true(k_is_user_context());
25-
uintptr_t gp_val = reg_read(gp);
33+
uintptr_t gp_val = riscv_reg_read(gp);
2634
uintptr_t gp_test_val;
2735

2836
/* Make sure that `gp` is as expected */
@@ -33,11 +41,11 @@ static void rogue_user_fn(void *p1, void *p2, void *p3)
3341
}
3442

3543
/* Corrupt `gp` reg */
36-
reg_write(gp, 0xbad);
44+
riscv_reg_write(gp, 0xbad);
3745

3846
/* Make sure that `gp` is corrupted */
3947
if (IS_ENABLED(CONFIG_RISCV_GP)) {
40-
zassert_equal(reg_read(gp), 0xbad);
48+
zassert_equal(riscv_reg_read(gp), 0xbad);
4149
} else { /* CONFIG_RISCV_CURRENT_VIA_GP */
4250
zassert_equal((uintptr_t)_current, 0xbad);
4351
}
@@ -57,7 +65,7 @@ static void rogue_user_fn(void *p1, void *p2, void *p3)
5765

5866
ZTEST_USER(riscv_gp, test_gp_value)
5967
{
60-
uintptr_t gp_val = reg_read(gp);
68+
uintptr_t gp_val = riscv_reg_read(gp);
6169
uintptr_t gp_test_val;
6270
k_tid_t th;
6371

@@ -74,7 +82,7 @@ ZTEST_USER(riscv_gp, test_gp_value)
7482
zassert_ok(k_thread_join(th, K_FOREVER));
7583

7684
/* Make sure that `gp` is the same as before a rogue thread was executed */
77-
zassert_equal(reg_read(gp), gp_val, "`gp` corrupted by user thread");
85+
zassert_equal(riscv_reg_read(gp), gp_val, "`gp` corrupted by user thread");
7886
}
7987

8088
static void *userspace_setup(void)

0 commit comments

Comments
 (0)