|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Google LLC |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <kernel_internal.h> |
| 8 | +#include <zephyr/mem_mgmt/mem_attr.h> |
| 9 | +#include <zephyr/tc_util.h> |
| 10 | +#include <zephyr/ztest.h> |
| 11 | + |
| 12 | +/* Checks if the Machine Privilege Register Virtualization (MPRV) bit in mstatus is 1 (enabled). */ |
| 13 | +static bool riscv_mprv_is_enabled(void) |
| 14 | +{ |
| 15 | + return csr_read(mstatus) & MSTATUS_MPRV; |
| 16 | +} |
| 17 | + |
| 18 | +/* Checks if the Machine Previous Privilege (MPP) field in mstatus is set to M-Mode (0b11). */ |
| 19 | +static bool riscv_mpp_is_m_mode(void) |
| 20 | +{ |
| 21 | + return (csr_read(mstatus) & MSTATUS_MPP) == MSTATUS_MPP; |
| 22 | +} |
| 23 | + |
| 24 | +/* Helper structure to define the expected PMP regions derived from the Device Tree. */ |
| 25 | +struct expected_region { |
| 26 | + uintptr_t base; |
| 27 | + size_t size; |
| 28 | + uint8_t perm; |
| 29 | + bool found; |
| 30 | +}; |
| 31 | + |
| 32 | +/* |
| 33 | + * Extract base address, size, and permission for the memory regions |
| 34 | + * defined in the Device Tree under the 'memattr' nodes. |
| 35 | + */ |
| 36 | +static struct expected_region dt_regions[] = { |
| 37 | + {.base = DT_REG_ADDR(DT_NODELABEL(memattr_region1)), |
| 38 | + .size = DT_REG_SIZE(DT_NODELABEL(memattr_region1)), |
| 39 | + .perm = DT_MEM_RISCV_TO_PMP_PERM( |
| 40 | + DT_PROP(DT_NODELABEL(memattr_region1), zephyr_memory_attr)), |
| 41 | + .found = false}, |
| 42 | + {.base = DT_REG_ADDR(DT_NODELABEL(memattr_region2)), |
| 43 | + .size = DT_REG_SIZE(DT_NODELABEL(memattr_region2)), |
| 44 | + .perm = DT_MEM_RISCV_TO_PMP_PERM( |
| 45 | + DT_PROP(DT_NODELABEL(memattr_region2), zephyr_memory_attr)), |
| 46 | + .found = false}}; |
| 47 | + |
| 48 | +ZTEST(riscv_pmp_memattr_entries, test_pmp_devicetree_memattr_config) |
| 49 | +{ |
| 50 | + const size_t num_pmpcfg_regs = CONFIG_PMP_SLOTS / sizeof(unsigned long); |
| 51 | + const size_t num_pmpaddr_regs = CONFIG_PMP_SLOTS; |
| 52 | + |
| 53 | + unsigned long current_pmpcfg_regs[num_pmpcfg_regs]; |
| 54 | + unsigned long current_pmpaddr_regs[num_pmpaddr_regs]; |
| 55 | + |
| 56 | + /* Read the current PMP configuration from the control registers */ |
| 57 | + z_riscv_pmp_read_config(current_pmpcfg_regs, num_pmpcfg_regs); |
| 58 | + z_riscv_pmp_read_addr(current_pmpaddr_regs, num_pmpaddr_regs); |
| 59 | + |
| 60 | + const uint8_t *const current_pmp_cfg_entries = (const uint8_t *)current_pmpcfg_regs; |
| 61 | + |
| 62 | + for (unsigned int index = 0; index < CONFIG_PMP_SLOTS; ++index) { |
| 63 | + unsigned long start, end; |
| 64 | + uint8_t cfg_byte = current_pmp_cfg_entries[index]; |
| 65 | + |
| 66 | + /* Decode the configured PMP region (start and end addresses) */ |
| 67 | + pmp_decode_region(cfg_byte, current_pmpaddr_regs, index, &start, &end); |
| 68 | + |
| 69 | + /* Compare the decoded region against the list of expected DT regions */ |
| 70 | + for (size_t i = 0; i < ARRAY_SIZE(dt_regions); ++i) { |
| 71 | + if ((start == dt_regions[i].base) && |
| 72 | + (end == dt_regions[i].base + dt_regions[i].size - 1) && |
| 73 | + ((cfg_byte & 0x07) == dt_regions[i].perm)) { |
| 74 | + |
| 75 | + dt_regions[i].found = true; |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + for (size_t i = 0; i < ARRAY_SIZE(dt_regions); i++) { |
| 82 | + zassert_true(dt_regions[i].found, |
| 83 | + "PMP entry for DT region %zu (base 0x%lx, size 0x%zx, perm 0x%x) not " |
| 84 | + "found.", |
| 85 | + i + 1, dt_regions[i].base, dt_regions[i].size, dt_regions[i].perm); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +ZTEST(riscv_pmp_memattr_entries, test_riscv_mprv_mpp_config) |
| 90 | +{ |
| 91 | + zassert_true(riscv_mprv_is_enabled(), |
| 92 | + "MPRV should be enabled (1) to use the privilege specified by the MPP field."); |
| 93 | + |
| 94 | + zassert_false(riscv_mpp_is_m_mode(), |
| 95 | + "MPP should be set to 0x00 (U-Mode) before execution."); |
| 96 | +} |
| 97 | + |
| 98 | +ZTEST(riscv_pmp_memattr_entries, test_dt_pmp_perm_conversion) |
| 99 | +{ |
| 100 | + uint8_t result; |
| 101 | + |
| 102 | + result = DT_MEM_RISCV_TO_PMP_PERM(0); |
| 103 | + zassert_equal(result, 0, "Expected 0, got 0x%x", result); |
| 104 | + |
| 105 | + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_EMPTY); |
| 106 | + zassert_equal(result, 0, "Expected 0, got 0x%x", result); |
| 107 | + |
| 108 | + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_R); |
| 109 | + zassert_equal(result, PMP_R, "Expected PMP_R (0x%x), got 0x%x", PMP_R, result); |
| 110 | + |
| 111 | + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_W); |
| 112 | + zassert_equal(result, PMP_W, "Expected PMP_W (0x%x), got 0x%x", PMP_W, result); |
| 113 | + |
| 114 | + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_X); |
| 115 | + zassert_equal(result, PMP_X, "Expected PMP_X (0x%x), got 0x%x", PMP_X, result); |
| 116 | + |
| 117 | + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_R | DT_MEM_RISCV_TYPE_IO_W); |
| 118 | + zassert_equal(result, PMP_R | PMP_W, "Expected R|W (0x%x), got 0x%x", PMP_R | PMP_W, |
| 119 | + result); |
| 120 | + |
| 121 | + result = DT_MEM_RISCV_TO_PMP_PERM(DT_MEM_RISCV_TYPE_IO_R | DT_MEM_RISCV_TYPE_IO_W | |
| 122 | + DT_MEM_RISCV_TYPE_IO_X); |
| 123 | + zassert_equal(result, PMP_R | PMP_W | PMP_X, "Expected R|W|X (0x%x), got 0x%x", |
| 124 | + PMP_R | PMP_W | PMP_X, result); |
| 125 | +} |
| 126 | + |
| 127 | +ZTEST_SUITE(riscv_pmp_memattr_entries, NULL, NULL, NULL, NULL, NULL); |
0 commit comments