Issue created by Anthony Cole: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5826



## Summary

The `riscv/riscv` BSPs build `bsps/shared/cache/nocache.c` 
(`spec/build/bsps/riscv/riscv/obj.yml`). So 
`rtems_cache_get_instruction_line_size()` returns 0, and 
`rtems_cache_instruction_sync_after_code_change()` and the instruction cache 
invalidations do nothing. RISC-V doesn't keep instruction fetch coherent with 
stores: code written as data may only be executed after a `FENCE.I` (Zifencei) 
on the hart that executes it. Nothing in RTEMS executes one.

libdl checks the instruction line size and skips its cache synchronization when 
it is 0 (`rtems_rtl_obj_synchronize_cache()`, `cpukit/libdl/rtl-obj.c`). So 
code loaded with `dlopen()` can run stale.

**How it shows**

Not under QEMU, which models no caches. On a RISC-V core whose instruction 
fetch doesn't see stores still in the data cache (a Codasip V730 on an FPGA, a 
BSP derived from `riscv/riscv`), the libdl tests `dl01`, `dl11` and `dl12` took 
illegal instruction exceptions in freshly loaded code, intermittently (`dl01` 
passed on a retry). A test that writes a small function and calls it, 100,000 
times, ran stale code every time without `FENCE.I` and never with it.

**Fix**

A cache implementation for RISC-V, `bsps/riscv/shared/cache/cache.c`, used by 
`riscv/riscv` instead of `nocache.c`:

- `CPU_INSTRUCTION_CACHE_ALIGNMENT` (`CPU_CACHE_LINE_BYTES`; only used to align 
ranges);
- `CPU_CACHE_SUPPORT_PROVIDES_RANGE_FUNCTIONS`, with the range and entire 
instruction cache invalidations executing `FENCE.I`;
- `CPU_CACHE_NO_INSTRUCTION_CACHE_SNOOPING`, so that in SMP configurations 
`cacheimpl.h` does the invalidation on every processor (`FENCE.I` acts only on 
the hart that executes it).

`rtems_cache_instruction_sync_after_code_change()` then executes `FENCE.I` on 
every processor. Since GCC 12 the assembler accepts `fence.i` only with 
Zifencei in `-march`, which the BSP options don't name, so the file enables it 
around the instruction with `.option arch, +zifencei`. No data cache support: 
there is no standard way to maintain it, and `FENCE.I` doesn't need it.

Tested with the RSB 7 toolchain (GCC 15.2.0) on `riscv/rv64imafdc` with 
`RTEMS_SMP` under QEMU `virt -smp 4` (`dl05`, `dl12`, `smpcache01` pass; the 
FENCE.I is broadcast), and with clang on a `riscv/riscv`-derived BSP under QEMU 
and on FPGA (`spcache01`, `smpcache01`, `tmcontext01`, `tmtimer01`, 
`ts-validation-cache`, the `dl*` tests).

Other RISC-V BSP families also build `nocache.c` (`griscv`, `noel`, `esp32`) 
and could use the same file.

**Environment**

RTEMS `main` as of 53d56d9494 (2026-11-13), `bsps/riscv/riscv`. Found on a 
Codasip V730 (RV32, FPGA) with a BSP derived from `riscv/riscv`.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5826
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/namespace/49/sent_notifications/6-2ccuvrlvya3gw4ry0xfxw0jye-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to