Add the MPXY MM source driver for forwarding RPMI Management Mode
requests through the MPXY request-forwarding bridge.
The driver:
- Registers the `opensbi,rpmi-mpxy-reqfwd-mm` FDT-compatible source.
- Defines MM service metadata for get attributes and communicate services.
- Builds request-forwarding responses for MM GET_ATTRIBUTES and
COMMUNICATE requests.
- Handles MM response completion and copies response payloads back to the
source buffer.
- Extracts and stores the MM shared-memory address and size from the
GET_ATTRIBUTES response.
- Registers the MM source with the Management Mode RPMI service group.
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Oza Pawandeep <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261118215549.2499040-5-pawandeep.oza@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
Introduce an FDT-configured MPXY RPMI bridge driver that enables
cooperative message passing between two SBI domains sharing the
same physical hart via domain context switching.
MPXY channels are registered at runtime when the named domains
appear via the domain registration notifier:
- src_ch: used by the source domain to send messages
- dst_ch: used by the destination domain to retrieve and
complete forwarded messages via RPMI REQFWD service group
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Oza Pawandeep <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261118215549.2499040-4-pawandeep.oza@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
Add message protocol definitions for the RPMI REQUEST_FORWARD service
group (SERVICEGROUP_ID: 0x000D).
The following are introduced:
- enum rpmi_request_forward_service_id: service IDs for the
REQUEST_FORWARD service group:
- REQFWD_ENABLE_NOTIFICATION (0x01)
- REQFWD_RETRIEVE_CURRENT_MESSAGE (0x02)
- REQFWD_COMPLETE_CURRENT_MESSAGE (0x03)
- struct rpmi_request_forward_resp: response structure for
REQFWD_RETRIEVE_CURRENT_MESSAGE (SERVICE_ID 0x02) carrying
status, remaining bytes, returned bytes and a flexible array
member for the inline message data.
- struct rpmi_request_complete_resp: response structure for
REQFWD_COMPLETE_CURRENT_MESSAGE (SERVICE_ID 0x03) carrying
status and the number of pending forwarded messages.
These definitions are used by the MPXY bridge driver to implement
the RPMI REQUEST_FORWARD protocol for inter-domain message passing
over the SBI MPXY extension.
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Oza Pawandeep <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261118215549.2499040-2-pawandeep.oza@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_dbtr_read_trig() rejects ranges ending exactly at total_trigs
because it treats trig_idx_base + trig_count == total_trigs as
out of range. This breaks the corner case where supervisor
software reads the last trigger by asking for base total_trigs - 1
and count 1.
Validate the base first, then compare trig_count against
total_trigs - trig_idx_base. This permits exact-end ranges and avoids
relying on unsigned addition that could overflow for a bad count.
On similar lines, the trig_count parameter of sbi_dbtr_install_trig()
and sbi_dbtr_update_trig() can be equal to total_trigs.
Fixes: a14e4d24a1 ("lib: sbi: dbtr: fix integer overflow in read_trig bounds check")
Fixes: 97f234f15c ("lib: sbi: Introduce the SBI debug triggers extension support")
Signed-off-by: Himanshu Chauhan <himanshu.chauhan@oss.qualcomm.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261107060325.1117887-1-himanshu.chauhan@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
Per the RPMI specification, CPPC_PROBE_REG, CPPC_READ_REG, and
CPPC_WRITE_REG require REG_ID in word 0 and HART_ID in word 1.
The current request structures have these fields swapped, causing
incompatibility with spec-compliant RPMI servers.
Swap the fields to match the specification and reorder the
corresponding field assignments in lib for consistency.
Fixes: 591a98bdd5 ("lib: utils/cppc: Add RPMI CPPC driver")
Signed-off-by: wuliwen <liwen.wu@spacemit.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261022020525.1503945-1-liwen.wu@spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_ecall_pmu_handler() passes the uninitialized local temp to
sbi_pmu_ctr_fw_read() and copies it into out->value unconditionally.
sbi_pmu_ctr_fw_read() only writes *cval on success, so every error
return of SBI_EXT_PMU_COUNTER_FW_READ (e.g. a hardware or inactive
counter index) hands an M-mode stack word to the supervisor in a1.
SBI_EXT_PMU_COUNTER_FW_READ_HI does the same with the upper half of
temp on RV32.
Initialize temp to zero so that the error paths return zero in
sbiret.value; the success paths are unchanged.
Fixes: 1664d0efce ("lib: sbi_pmu: Replace sbi_pmu_ctr_read() with sbi_pmu_ctr_fw_read()")
Fixes: 51951d9e9a ("lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi")
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Assisted-By: Claude Fable 5.1
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261011222115.769450-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_hart_switch_mode() writes next_addr unmasked into stvec (vstvec,
utvec). Bits [1:0] of xtvec are the MODE field, so a 2-byte aligned
entry, legal with the C extension, selects a reserved mode: QEMU drops
the write, the Sail model keeps the old mode, and the next stage starts
with a trap vector that is not its entry.
Mask the mode bits and install the entry as a direct-mode vector; mepc
still receives the exact address.
Signed-off-by: Nickolai Zeldovich <nickolai.zeldovich@gmail.com>
Assisted-by: Claude Fable 5.1
Tested-by: Chen Pei <cp0613@linux.alibaba.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261006193619.735580-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
Non-boot harts exit _wait_for_boot_hart on a plain load of _boot_status
and go on to read platform.hart_index2id and other data the boot hart
relocated before publishing. Nothing orders those loads after the
status load, so under RVWMO a hart can read the unrelocated pointer and
fault through it before mtvec is set.
Pair the boot hart's release fence with a "fence r, rw" after the loop.
Signed-off-by: Nickolai Zeldovich <nickolai.zeldovich@gmail.com>
Assisted-by: Claude Fable 5.1
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261006193609.735547-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
Platforms with fixed or mtvec-based NMI vectors need no vendor
register programming, so calling sbi_panic() when smrnmi_handlers_init
is missing is too strict; an unhandled RNMI already falls back to
sbi_trap_error().
Allow a NULL smrnmi_handlers_init only when the platform has set
SBI_PLATFORM_HAS_FIXED_SMRNMI_VECTOR to indicate it uses a fixed or
mtvec-based vector. This distinguishes platforms that intentionally
have nothing to program from ones that simply forgot to set the
callback, so the latter now fails instead of enabling NMIs and
jumping to an undefined vector on the first RNMI.
Report how Smrnmi was initialized in the boot HART banner: "Platform"
when smrnmi_handlers_init programmed it, "Default" when the flag was
set instead.
Suggested-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
Co-developed-by: Nia Su <nia.su@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260915-smrnmi-context-restore-v3-3-e9dd31152515@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
The JH7110 DDR controller folds physical addresses beyond the 8GB DDR
window back into DRAM: PA (X + 0x200000000) resolves to the same cells
as PA X. PMP entries match the presented address, so an aliased S-mode
access to the firmware region bypasses the entry protecting the
firmware and matches the allow-everything root region, letting root
read and write the firmware through the alias.
Add a root domain memory region covering the alias of the firmware
image (fw_start + 0x200000000 .. fw_start + fw_size) with M-mode-only
permissions, in platform early_init() so that it is in place before
sbi_domain_finalize(). The region sorts before the allow-everything
region, so it gets a lower PMP entry index and denies S/U-mode access
to the firmware alias. The rest of the alias window is left untouched.
This follows the same approach as the RZ/Five ILM/DLM protection.
Verified on QEMU riscv64 (root region list and PMP encodings) and on a
VisionFive 2 board: S/U-mode accesses to both the alias (0x240000000)
and the firmware region (0x40000000) fault as expected after this
patch, while normal boot is unaffected.
Reported-by: Zhongyi Cheng <zhongyicheng241@mails.ucas.ac.cn>
Signed-off-by: Zhongyi Cheng <zhongyicheng241@mails.ucas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260928032414.4125686-1-zhongyicheng241@mails.ucas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
The sifive-uart and cadence-uart drivers store the input clock
frequency and baudrate into file-static globals during init, but
never read them back; the divisor is computed directly from the
function arguments. Clang 23 adds -Wunused-but-set-global, which is
enabled by -Wall, so the build fails with -Werror:
sifive-uart.c:34:12: error: variable 'uart_in_freq' set but not used [-Werror,-Wunused-but-set-global]
sifive-uart.c:35:12: error: variable 'uart_baudrate' set but not used [-Werror,-Wunused-but-set-global]
Drop the dead variables.
Signed-off-by: Khem Raj <raj.khem@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261015203610.835320-1-khem.raj@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
pmu_ctr_write_hw() writes CSR_MCYCLE + cidx with no check on cidx,
while its sibling pmu_ctr_start_hw() guards the same index space:
if (cidx >= num_hw_ctrs || cidx == 1)
return SBI_EINVAL;
A cidx at or above num_hw_ctrs addresses an mhpmcounter the platform
need not implement, and cidx == 1 addresses 0xb01, which is not
assigned. Either one traps in M-mode instead of returning an error.
Give the function the same guard and a return value. The guard is
copied from pmu_ctr_start_hw() rather than limited to the upper
bound, so that the two functions accept exactly the same index space.
Only the call site in sbi_pmu_ctr_cfg_match() propagates the error.
The two call sites inside pmu_ctr_start_hw() run after the identical
guard in that function, so they cannot fail and are left unchanged.
Fixes: 13d40f21d5 ("lib: sbi: Add PMU support")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261025145338.2569358-3-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_pmu_ctr_cfg_match() calls pmu_ctr_start_hw() for the AUTO_START
flag and drops the result. pmu_ctr_start_hw() carries its own index
guard
if (cidx >= num_hw_ctrs || cidx == 1)
return SBI_EINVAL;
and can also return SBI_EALREADY_STARTED when the counter is already
running. Both are reported to the caller as a successful configure.
Neither is benign. A rejected index means nothing was configured, and
an already running counter may have been configured for a different
event previously, so the supervisor must not be told its configure
succeeded.
This is also the only one of the three start paths in the file that
discards the error. sbi_pmu_ctr_start() propagates it:
ret = pmu_ctr_start_hw(cidx, ival, bUpdate);
if (ret)
return ret;
and the firmware branch directly below this call site propagates the
error from pmu_dev->fw_counter_start() as well.
Fixes: 13d40f21d5 ("lib: sbi: Add PMU support")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261025145338.2569358-2-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
SpacemiT K3 has 16 harts split across four clusters, with eight X100
harts in C0/C1 and eight A100 harts in C2/C3. Only hart 0 is powered at
reset, so secondary harts cannot reach the OpenSBI warm-boot path until
the platform removes their PMU power-down votes and wakes them.
Add a start-only HSM backend which performs those operations for the
requested hart. This lets the common HSM state machine start secondary
harts on demand instead of waking every hart during cold boot.
Select the HSM backend from the root compatible, preserving K1's
existing start and stop behavior.
The Boot ROM maps the audio buffer region as I/O although later firmware
uses it as normal memory. Restore its cacheable PMA attribute and mark
the XIP region as I/O before enabling caches. The boot hart performs the
per-hart setup during early platform discovery, while secondary harts do
the same in their assembly entry before reaching common warm-boot code.
This also enables MISA.H on X100 before common code selects its trap
entry; A100 does not implement H. Fence the PMA and guest translation
changes before continuing.
Use assembler-safe constants shared by the C and warm-entry assembly
paths. Since K1 and K3 are RV64-only, omit the SpacemiT platform objects
from RV32 generic builds.
Program the four cluster warm-boot vectors once and enable CCI-550 snoop
and DVM requests before any secondary hart is started.
Signed-off-by: Xianbin Zhu <xianbin.zhu@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Co-developed-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-5-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
K3 shares cache-control CSRs, PMU idle fields, cluster sizing, and the
CCI-550 programming sequence with K1. Keeping these definitions and the
CCI helper in K1-specific files would either duplicate them or make K3
depend on K1 support.
Add a hidden PLATFORM_SPACEMIT option and move the shared definitions to
spacemit/common.h. Move cci_enable_snoop_dvm_reqs() to spacemit.c while
keeping each SoC's CCI topology and boot flow in its own platform driver.
Rename the PMU idle fields to match the K1 and K3 User Manuals. Keep the
K1 power-down mask at 0x1b, so K1 power-management behavior remains
unchanged. Keep the K1 HSM implementation K1-specific and switch it to
the shared definitions.
This is a non-functional refactoring that prepares the common support
used by K3.
Signed-off-by: Xianbin Zhu <xianbin.zhu@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Co-developed-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-4-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
OpenSBI assigns SBI_PMU_HW_CPU_CYCLES and SBI_PMU_HW_INSTRUCTIONS to
fixed counters during PMU initialization. sbi_pmu_ctr_cfg_match()
recognizes these events without searching hw_event_map[].
However, sbi_pmu_event_get_info() only checks hw_event_map[] when
determining whether a hardware event is supported. As a result, these
fixed-counter events are reported as unsupported when no riscv,pmu
device tree node is provided.
Check fixed-counter events separately using pmu_ctr_find_fixed_hw() and
report them as supported.
Fixes: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: Fengwei Tan <tfx2001@outlook.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/SY8P300MB0041D04649621E79FE950B43B58E2@SY8P300MB0041.AUSP300.PROD.OUTLOOK.COM
Signed-off-by: Anup Patel <anup@brainfault.org>
The Allwinner D1s SoC is a stripped-down version of the D1 and D1s
boards use "allwinner,sun20i-d1s" as their root compatible string
instead of "allwinner,sun20i-d1". So, currently the sun20i override is
not selected on them.
Add the D1s compatible to the match table.
Tested on the MangoPi MQ board with the Allwinner F133-A (D1s) SoC.
Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
Link: https://lore.kernel.org/r/20260921182017.999610-1-indrek.kruusa@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
Parallel build is extremely slow when using vars (O, I,
PLATFORM_DIR+PLATFORM). And all this is because the variables are
calculated continuously, with the simultaneous invocation of
the $(shell ...). So use "Simply expanded variables" to
calculate value and call $(shell ...) just once.
Signed-off-by: Maxim Kochetkov <fido_max@inbox.ru>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260924060133.294142-1-fido_max@inbox.ru
Signed-off-by: Anup Patel <anup@brainfault.org>
An earlier SBI trap may have already used the same M-mode exception
stack slot to save its trap context before an RNMI is taken.
sbi_trap_rnmi_handler() never initializes prev_context before
sbi_trap_error() walks it to print trap diagnostics, so it can read
whatever stale value happens to be left in that slot.
Link prev_context on entry and restore it on exit, the same way
sbi_trap_handler() does. This also lets an exception taken while
already inside RNMI handling chain back to the RNMI's own trap
context instead of dropping it, so sbi_trap_error() prints every
nested context correctly, including the RNMI's.
Fixes: 00fec20b49 ("firmware: Add RNMI handler infrastructure")
Suggested-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260914-trap-context-fix-v2-2-3a6bedc6fcf0@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_trap_handler() links tcntx->prev_context on entry and restores
scratch->trap_context on exit, forming a stack that unwinds through
the same path it was pushed from.
Non-retentive suspend/resume breaks that unwind: the SBI call
requesting suspend jumps straight to warm boot (jump_warmboot())
instead of returning through the normal exit path, leaving
scratch->trap_context stale. Since each hart's scratch space sits at
a fixed address, the next trap taken after warm boot resume reads
that same stale scratch->trap_context as its prev_context, which
sbi_trap_error() then walks to print trap diagnostics -- producing a
broken trap dump.
Clear trap_context in the scratch space on warm boot entry.
Fixes: 60ffc154c8 ("include: sbi: Add trap_context pointer in struct sbi_scratch")
Signed-off-by: Nia Su <nia.su@sifive.com>
Reviewed-by: Evgeny Voevodin <evvoevod@tenstorrent.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260914-trap-context-fix-v2-1-3a6bedc6fcf0@sifive.com
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_mpxy_read_attrs() and sbi_mpxy_write_attrs() compute the end of the
requested range as a u32:
end_id = base_attr_id + attr_count - 1;
Message protocol attribute ids extend to
SBI_MPXY_ATTR_MSGPROTO_ATTR_END (0xffffffff), so the guard
"end_id > SBI_MPXY_ATTR_MSGPROTO_ATTR_END" cannot hold for a u32 and
never rejects anything.
Such a request then reaches the message protocol driver, which computes
the same endpoint in a u32 and so passes its own range check as well,
and indexes its attribute array with attr_id2index(base_attr_id). For
base_attr_id 0xffffffff that is index 0x7fffffff, an out-of-bounds read
whose result is copied into the shared memory.
Widen end_id to u64 so the endpoint is exact and such a request is
rejected before a driver runs. The standard attribute path is not
affected, since attr_count is already limited to the shared memory size
there.
Fixes: 7939bf1329 ("lib: sbi: Add SBI Message Proxy (MPXY) framework")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-5-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_pmu_event_get_info() computes the shared memory size as
num_events * sizeof(struct sbi_pmu_event_info) before num_events is
validated. The product can wrap, in which case the domain check runs
against a truncated size while the loop below still iterates over the
original count, that is, past the region that was checked.
Check num_events against the largest value that cannot wrap, and reject
a zero address, both before the multiplication.
Fixes: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-3-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
In sbi_dbtr_read_trig(), the range check is:
if (trig_idx_base + trig_count >= hs->total_trigs)
When trig_idx_base and trig_count are both unsigned long values supplied
by S-mode, their sum can wrap past ULONG_MAX to a small value, making
the check pass. For example trig_idx_base=1, trig_count=ULONG_MAX wraps
to 0, which is less than total_trigs.
This allows the subsequent for_each_trig_entry loop to access trigger
entries far beyond the triggers[] array, corrupting M-mode heap memory
via CSR read-back writes and leaking M-mode internal state to S-mode
shared memory.
Rewrite the condition as trig_count >= total_trigs - trig_idx_base. The
subtraction is safe because the preceding check already guarantees
trig_idx_base < total_trigs.
Fixes: 97f234f15c ("lib: sbi: Introduce the SBI debug triggers extension support")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Himanshu Chauhan <himanshu.chauhan@oss.qualcomm.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-2-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
The per-HART event list is kept sorted by expiry time: the head is the
event programmed into the timer device by __sbi_timer_update_device(),
and sbi_timer_process() stops at the first unexpired entry.
__sbi_timer_event_start() finds the first event that expires after the
new one, but then inserts the new event with sbi_list_add(), i.e. after
that event instead of before it. Starting an event at time 50 while an
event at time 1000 is pending yields the list [1000, 50]: the device is
programmed for 1000 and the earlier event fires 950 ticks late.
Today each HART only ever has its S-mode timer event on the list, so
the misordering needs a second timer event user to show up. Once there
is one, on HARTs without Sstc an S-mode timer programmed while a later
M-mode event is pending is delivered only when that later event
expires.
Use sbi_list_add_tail() so the new event goes in front of next_ev.
Fixes: b7fa8a246c ("lib: sbi_timer: Add support for timer events")
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Assisted-By: Claude Fable 5.1
Reviewe-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261011222412.769530-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
sbi_pmu_event_get_info() maps the caller's shared memory into M-mode with
sbi_hart_protection_temp_map_range(). The must-be-zero check added inside the
event loop returns straight to the supervisor without reaching the matching
temp_unmap_range() at the end of the function, so the mapping is left behind.
This has a security impact beyond leaking a mapping. Under Smepmp the
temporary mapping is a single reserved PMP entry, SBI_SMEPMP_RESV_ENTRY.
sbi_hart_smepmp_temp_map_range() refuses to reprogram that entry while it is
already enabled:
if (sbi_hart_is_pmp_enabled(SBI_SMEPMP_RESV_ENTRY))
return SBI_ENOSPC;
and no caller in the tree checks that return value. sbi_pmu.c, sbi_sse.c,
sbi_mpxy.c, sbi_dbtr.c and sbi_ecall_dbcn.c all discard it. So once the entry
is left behind, every later shared memory ecall on that hart runs with PMP 0
still covering the previous region, and M-mode faults the first time it
dereferences the new buffer.
A supervisor triggers this with one ecall carrying a reserved bit in the first
event_idx. The next SBI_EXT_DBCN_CONSOLE_WRITE then takes an M-mode load
access fault on the console buffer and the firmware stops making forward
progress, taking DBCN, SSE, MPXY and DBTR down with it on that hart. It also
breaks the Smepmp invariant that M-mode does not retain standing access to
supervisor memory.
Confirmed on qemu virt with a bare metal S-mode payload, QEMU 8.2.2:
build -cpu protection result
master 6ad246a1 rv64 pmp completes
master 6ad246a1 rv64,smepmp=true epmp firmware dies
master + this patch rv64,smepmp=true epmp completes
The middle row leaves this as the last entry in the QEMU exception trace,
where the epc is inside nputs() in M-mode and the tval is the supervisor
console buffer:
cause:0000000000000005, epc:0x000000008000204c,
tval:0x0000000080500000, desc=fault_load
Without Smepmp the map and unmap pair compiles down to nothing, which is why
the first row survives.
This only affects master. v1.9 and earlier have the same map and unmap pair
but no return between them, so no released version needs a backport.
Route the early exit through the existing unmap.
Fixes: d97cfb33ae ("lib: sbi_pmu: Return invalid param error for reserved event_idx bits")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261012022033.1165060-1-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
mask_len counts mask bits: it is derived from vlenb * 8, and the mask
indexing below divides by 8 to turn a bit index into a byte index.
get_vreg() counts bytes: its size argument feeds "vsetvli x0, %0, e8,
m8" and a vse8.v, both of which operate on 8-bit elements.
Both emulators pass mask_len directly as get_vreg()'s size, so the
firmware writes eight times more than intended into mask[], a
MASK_BUFFLEN / 8 == 128 byte stack buffer. The write stays in bounds
only while vlenb * 8 <= 128, that is VLEN <= 128. Above that it runs
past the end of the buffer and corrupts the M-mode stack, including the
saved return address.
The pre-rework code passed vlenb, a byte count, which was correct; the
chunked version replaced it with a bit count.
Reachable from S-mode or U-mode with a single masked, misaligned vector
load or store on a hart whose VLEN exceeds 128 bits. Observed on
qemu-system-riscv64 with -cpu rv64,v=true,vlen=256,zicclsm=false: the
firmware takes an instruction access fault in M-mode with mepc and ra
both zero. The same payload at vlen=128 enters the emulator and
completes normally.
Convert pos and size to bytes at both call sites. mask_len is a power of
two and at least 8, and pos is already a multiple of mask_len, so both
divisions are exact.
Fixes: a8be5e94 ("lib: sbi: Rework misaligned vector load/store")
Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260922030633.10886-1-sebasjosue84@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>