diff --git a/arch/riscv/include/pmp.h b/arch/riscv/include/pmp.h index ca4f37f3a2aba..52402393afce6 100644 --- a/arch/riscv/include/pmp.h +++ b/arch/riscv/include/pmp.h @@ -7,6 +7,13 @@ #ifndef PMP_H_ #define PMP_H_ +#include + +#define DT_MEM_RISCV_TO_PMP_PERM(dt_attr) ( \ + (((dt_attr) & DT_MEM_RISCV_TYPE_IO_R) ? PMP_R : 0) | \ + (((dt_attr) & DT_MEM_RISCV_TYPE_IO_W) ? PMP_W : 0) | \ + (((dt_attr) & DT_MEM_RISCV_TYPE_IO_X) ? PMP_X : 0)) + void z_riscv_pmp_init(void); void z_riscv_pmp_stackguard_prepare(struct k_thread *thread); void z_riscv_pmp_stackguard_enable(struct k_thread *thread); diff --git a/tests/arch/riscv/pmp/mem-attr-entries/CMakeLists.txt b/tests/arch/riscv/pmp/mem-attr-entries/CMakeLists.txt new file mode 100644 index 0000000000000..e0f392177d6f3 --- /dev/null +++ b/tests/arch/riscv/pmp/mem-attr-entries/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(riscv_pmp) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) + +target_include_directories(app PRIVATE + ${ZEPHYR_BASE}/kernel/include + ${ZEPHYR_BASE}/arch/${ARCH}/include + ) diff --git a/tests/arch/riscv/pmp/mem-attr-entries/prj.conf b/tests/arch/riscv/pmp/mem-attr-entries/prj.conf new file mode 100644 index 0000000000000..d490cfd2e07b1 --- /dev/null +++ b/tests/arch/riscv/pmp/mem-attr-entries/prj.conf @@ -0,0 +1,3 @@ +CONFIG_ZTEST=y +CONFIG_MULTITHREADING=y +CONFIG_RISCV_PMP=y diff --git a/tests/arch/riscv/pmp/mem-attr-entries/src/main.c b/tests/arch/riscv/pmp/mem-attr-entries/src/main.c new file mode 100644 index 0000000000000..f4fd8bde0c53a --- /dev/null +++ b/tests/arch/riscv/pmp/mem-attr-entries/src/main.c @@ -0,0 +1,35 @@ +/* Copyright (c) 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +ZTEST(riscv_pmp_memattr_entries, test_dt_pmp_perm_conversion) +{ + uint8_t result; + + result = DT_MEM_RISCV_TO_PMP_PERM(0); + zassert_equal(result, 0, "Expected 0, got 0x%x", result); + + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_R); + zassert_equal(result, PMP_R, "Expected PMP_R (0x%x), got 0x%x", PMP_R, result); + + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_W); + zassert_equal(result, PMP_W, "Expected PMP_W (0x%x), got 0x%x", PMP_W, result); + + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_X); + zassert_equal(result, PMP_X, "Expected PMP_X (0x%x), got 0x%x", PMP_X, result); + + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_R | DT_MEM_RISCV_TYPE_IO_W); + zassert_equal(result, PMP_R | PMP_W, "Expected R|W (0x%x), got 0x%x", PMP_R | PMP_W, + result); + + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_R | DT_MEM_RISCV_TYPE_IO_W | + DT_MEM_RISCV_TYPE_IO_X); + zassert_equal(result, PMP_R | PMP_W | PMP_X, "Expected R|W|X (0x%x), got 0x%x", + PMP_R | PMP_W | PMP_X, result); +} + +ZTEST_SUITE(riscv_pmp_memattr_entries, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/arch/riscv/pmp/mem-attr-entries/testcase.yaml b/tests/arch/riscv/pmp/mem-attr-entries/testcase.yaml new file mode 100644 index 0000000000000..dff2f2fc1cee5 --- /dev/null +++ b/tests/arch/riscv/pmp/mem-attr-entries/testcase.yaml @@ -0,0 +1,9 @@ +common: + platform_allow: + - qemu_riscv32 + - qemu_riscv32e + - qemu_riscv64 + filter: CONFIG_RISCV_PMP + +tests: + arch.riscv.pmp.memattr.entries: {}