Commit Graph
1450 Commits
Author SHA1 Message Date
liutong 2afa777592 lib: sbi_mpxy: fix integer overflow in attribute range endpoint
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>
2026-10-26 11:43:21 +05:30
liutong f45b9433ed lib: sbi_pmu: fix integer overflow and zero-address in event_get_info
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>
2026-10-26 10:28:28 +05:30
liutong a14e4d24a1 lib: sbi: dbtr: fix integer overflow in read_trig bounds check
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>
2026-10-26 10:27:20 +05:30
Nickolai Zeldovich bcf587a987 lib: sbi_timer: Insert timer event before the later event
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>
2026-10-25 15:05:29 +05:30
Anup Patel 8c04100096 lib: utils/mpxy: Use domain notifier for deferred creation of MPXY RPMI
The owner domain may not be available at the time of creating MPXY RPMI
instance so use domain notifier for deferred creation.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-12-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:32:38 +05:30
Oza Pawandeep f4ed5aa8b7 lib: sbi_domain: Add domain notifier infrastructure
Introduce a notifier callback mechanism that allows subsystems to
register interest in domain events (such as registration). Whenever
such events happen for a domain, all registered notifiers are invoked.

The following public interfaces are added to sbi_domain.h:

  - sbi_domain_register_notifier(): registers a notifier callback.
    Duplicate (fn, event, priv) pairs are silently ignored.

  - sbi_domain_unregister_notifier(): removes a previously registered
    notifier callback.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Signed-off-by: Oza Pawandeep <pawandeep.oza@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-11-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel f909590830 lib: utils/mpxy: Add domain parameter to setup_group() callback
The MPXY channels can be domain specific so the setup_group() callback
must know which domain the MPXY RPMI instance is associated with.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-10-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel c4f098bfa3 lib: sbi_domain: Allow adding memrange to a particular domain from drivers
Currently, memregions of non-root domain only populated at domain creation
time which not flexible for drivers which create domain specific resources
(such as MPXY channels). Add new sbi_domain_add_memrange() function which
allows adding memrange to any domain at boot time.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-9-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel 145bd4ccc0 lib: sbi_domain: Make domain regions as fixed array
Instead of separately allocating domain regions, let us have a fixed
size array so that regions are automatically allocated along with
domain allocation and one SBI_DOMAIN_MEMREGION_MAX macro defines
maximum number of regions for all domains.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-8-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel 89b3afb16f lib: utils/mpxy: Simplify fdt_mpxy_rpmi.h by moving things to fdt_mpxy_rpmi.c
Currently, the fdt_mpxy_rpmi.h has lot things which are not used by
any of MPXY RPMI drivers so simplify fdt_mpxy_rpmi.h by moving such
things to fdt_mpxy_rpmi.c.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-7-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel dc8ceae9d0 lib: utils/mpxy: Introduce mpxy_rpmi_no_mbox_init() function
Add new mpxy_rpmi_no_mbox_init() function to instantiate MPXY RPMI
channel without any mailbox channel backing. This new function can
be used by MPXY RPMI drivers that want to emulate MPXY channels in
software.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-6-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel bdd7874991 lib: sbi_domain: Introduce sbi_domain_find_by_name() function
Add sbi_domain_find_by_name() to allow driver locate domain instance
using domain name. This also means domain names must be unique.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-5-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel db877c0908 lib: utils/mpxy: Introduce get_attribute_group() for MPXY RPMI data
Fully emulated MPXY channels need a custom way to provide RPMI attributes
so introduce optional get_attribute_group() callback in MPXY RPMI data
for this purpose.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-4-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel fa2bbaf349 lib: utils/mpxy: Setup group early in fdt_mpxy_rpmi_init()
Instead of setting group late in fdt_mpxy_rpmi_init(), it is better
to do it as early as possible right after requesting mailbox channel
so that we define new callback for channel attributes.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-3-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Anup Patel 0298a2c343 lib: utils/mpxy: Rename fdt_mpxy_rpmi_mbox to fdt_mpxy_rpmi
The common fdt_mpxy_rpmi_mbox driver is being upgraded to support emulated
MPXY channels hence drop term "mbox" from fdt_mpxy_rpmi_mbox everywhere.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260923032359.4041876-2-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:30:46 +05:30
Prajwal N 97bf679708 lib: utils/irqchip/imsic: Fix the MSI capability assignment
The struct sbi_irqchip_device is copied to the struct imsic_data
and the MSI capability is set in sbi_irqchip_device. Since the
struct assignment is not a pointer, the MSI capability setting
does not take effect.

Set the MSI capability directly in the struct imsic_data to
ensure it takes effect.

Fixes: 193b1d9e7e ("lib: utils/irqchip/imsic:migrate to irqchip framework")
Signed-off-by: Prajwal N <prajwal.n@oss.qualcomm.com>
Reviewed-by: Rahul Pathak <rahul.pathak@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261001060820.1267933-1-prajwal.n@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 14:19:19 +05:30
Sebastian Alba Vives 0ce50ae8fa lib: sbi_pmu: unmap shared memory on the early exit in event_get_info
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>
2026-10-25 14:07:17 +05:30
Sebastian Alba Vives 663786edae lib: sbi_trap_v_ldst: fix mask buffer overflow on VLEN > 128
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>
2026-10-25 14:01:44 +05:30
Jakub Janeczko 16fa280b69 lib: sbi_pmu: Fix race on pmu_ctr_write_hw
On platforms where a bit of mcountinhibit cannot be set to 1,
and when on rv32, pmu_ctr_write_hw() was given value whose lower
bits were close to 2^32, the write could miss the lower bit overflow.

Signed-off-by: Jakub Janeczko <kuba@ev1.pl>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261024195739.585621-1-kuba@ev1.pl
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 13:42:21 +05:30
Pengpeng Hou 9f37e25f80 lib: sbi: reject PMU skip-match counter class mismatches
SKIP_MATCH selects the first counter in the caller-provided set, but the
current path checks only that the index exists and is active.  A supervisor
can select an active hardware counter for a firmware event.  CLEAR_VALUE or
AUTO_START then indexes firmware-counter state with
ctr_idx - num_hw_ctrs, which underflows.  The inverse mismatch sends a
firmware-counter index to hardware-counter operations.

Reject the selected counter when its hardware/firmware class cannot monitor
the requested event.  Return SBI_ENOTSUPP, matching the PMU specification's
case where the selected counter cannot monitor that event.

Fixes: 13d40f21d5 ("lib: sbi: Add PMU support")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Tested-by: Sebastian Alba Vives <sebasjosue84@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260827115718.50124-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 11:20:03 +05:30
Pengpeng Hou 131bda9447 lib: sbi_hsm: Validate state before updating start context
sbi_hsm_hart_start() acquires the target hart's start ticket, then
writes next_arg1, next_addr and next_mode before checking whether the
target is stopped.

A start request for a non-retentively suspended hart is rejected, but
those writes have already replaced the resume context consumed by
sbi_hsm_hart_resume_finish(). The same ordering also lets other rejected
start requests modify target scratch state.

Check the state immediately after acquiring the start ticket and before
updating the scratch context. The ticket serializes start requests for
the target; retain the cmpxchg as the publication barrier and defensive
state check.

Fixes: e8e9ed3790 ("lib: sbi: Set the state of a hart to START_PENDING after the hart is ready")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260831130817.54625-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 17:27:48 +05:30
Pengpeng Hou 5930665a17 lib: sbi_timer: Restart events from the restart list
Timer callbacks can request that an expired event be restarted. Such
events are moved to a local restart_list, but the restart loop tests that
list while reading its first entry from tstate->event_list.

If the main event list is empty, the loop treats its list head as a timer
event. Otherwise it repeatedly removes and reinserts the wrong event
while restart_list remains nonempty, so timer processing cannot
complete.

No in-tree callback currently requests a restart, but the timer-event
callback interface exposes this path to OpenSBI library and platform
users.

Read the event from restart_list, matching the loop condition and the
list that owns the entry.

Fixes: b7fa8a246c ("lib: sbi_timer: Add support for timer events")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260831130641.54100-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 13:55:43 +05:30
Pengpeng Hou 9bc841226d lib: utils/mailbox: Reject invalid RPMI queue indices
The RPMI shared-memory helpers validate a queue index before using it
to select queue_ctx_tbl, but the current comparison accepts queue_id
equal to queue_count.

Only entries 0 through queue_count - 1 are initialized from the device
tree. Accepting queue_count can therefore use an uninitialized queue
context when a protocol operation selects a queue omitted by the
description.

Reject queue_id greater than or equal to queue_count in both transfer
directions.

Fixes: 91f46fb47e ("lib/utils: Add RPMI messaging protocol and shared memory transport support")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260831130444.53629-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 13:18:45 +05:30
Pengpeng Hou 95c7980851 lib: utils/mailbox: reject undersized RPMI shmem queues
The RPMI shared-memory mailbox transport derives qctx->num_slots from the
device-tree queue window size and the riscv,slot-size property. The current
initialization only checks that the queue window is nonzero before
subtracting the two RPMI header slots.

Reject queue windows that cannot provide the two header slots plus at least
two data ring slots. A zero data-slot count would later be used as a modulo
divisor, and a one-slot ring is also unusable with the current full/empty
convention because one empty slot is reserved to distinguish the two
states.

Also reject data-slot counts that cannot be represented by qctx->num_slots.

Fixes: 91f46fb47e ("lib/utils: Add RPMI messaging protocol and shared memory transport support")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260828142521.14267-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 13:06:37 +05:30
Pengpeng Hou 9dcd9f9365 lib: sbi: use unsigned masks for HPM counter 31
The HPM feature detector and CSR emulator build mhpm_mask bits with a
plain signed 1. Both macro families expand through counter 31, so the
last expansion evaluates 1 << 31 and shifts a signed int into its sign
bit.

Use BIT() at both sites. The mask is unsigned and bit 31 is part of the
represented HPM counter set.

Fixes: c744ed77b1 ("lib: sbi_pmu: Enable noncontigous hpm event and counters")
Fixes: ee1f83ca84 ("lib: sbi_pmu: remove mhpm_count field in hart feature")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260828142354.13035-1-pengpeng@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-23 12:59:53 +05:30
Shibo Zhu 2de7dfb681 lib: sbi_mpxy: Fix shared memory size rounding
The sbi_mpxy_register_channel() currently performs ceiling division by
PAGE_SIZE but stores the resulting page count in mpxy_shmem_size. All
consumers interpret mpxy_shmem_size as a byte count, so channels requiring
more than one page get an undersized shared memory value.

Address the above issue by using ROUNDUP() to keep the rounded value in
bytes.

Fixes: ec09918426 ("lib: sbi: Update MPXY framework and SBI extension as per latest spec")
Signed-off-by: Shibo Zhu <3499129952@qq.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/tencent_14AA3BA0083001905C262A5CD64E442FC205@qq.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-28 17:42:15 +05:30
Chen Pei 0ffaa1bc65 lib/utils/fdt: Reserve exactly enough FDT space for fdt_cpu_fixup
fdt_cpu_fixup() reserves a hardcoded 32 extra bytes, plus 16 more per
HART inside the "zicntr" loop. Those 32 bytes also have to cover every
"status" property rewritten to "disabled", so once more than a handful
of HARTs are disabled the fixups fail with FDT_ERR_NOSPACE (-3).

Bumping the reservation to a fixed worst case is not safe either:
fdt_open_into() cannot know how much space the caller's buffer really
has, it just trusts the requested size. As OpenSBI does not own the
memory following a device tree passed in by the previous booting stage,
over-declaring the blob risks corrupting whatever follows it.

Walk the HART nodes once without modifying them instead, and reserve
exactly what the fixups need. The read-only decision logic is factored
into fdt_cpu_fixup_needed() so the sizing and fixup passes cannot drift
apart, which also makes the per-HART fdt_open_into() redundant. Check
the return values of fdt_setprop_string() and fdt_appendprop_string()
as well, as running out of space used to fail silently.

Fixes: dd9439fbac ("lib: utils: Add a fdt_cpu_fixup() helper")
Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260817084303.1232-1-cp0613@linux.alibaba.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-28 11:41:25 +05:30
Carlos López 92b24d6acb lib: test: disable TSA for spinlock tests
The spinlock tests deliberately use unconventional locking patterns to
test edge cases. Disable thread safety analysis for them so that they
don't generate false positives when the feature is enabled.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-15-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 8f80b2651f lib: sbi_sse: add TSA annotations
Now that the lock relationship between an event and its owner hart is
explicit, add TSA annotations for the SSE handling code. This includes
indicating which fields are protected by a spinlock, as well as
annotating which functions need to run under a spinlock.

Exclude sse_local_init(), which initializes the spinlock to be acquired,
and sse_event_get() / sse_event_put(), which perform conditional locking
that TSA is not able to follow properly.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-14-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 43bb3df782 lib: sbi_sse: pass SSE hart state explicitly
In preparation to add TSA annotations, pass the SSE hart state owning an
event, instead of relying on sse_get_hart_state() to retrieve it from
the event itself. This makes the relationship more explicit, and will
allow the compiler to reason about the state of locks.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-13-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López fc1ae7a641 lib: sbi_sse: inline enable event locking
In preparation to add TSA annotations, inline sse_enabled_event_lock()
and sse_enabled_event_unlock(). This will help the compiler reason about
locking of the hart state by explicitly acquiring the spinlock in the
callers.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-12-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 8740e0a38c lib: sbi_domain: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the domain handling
code. This includes indicating which fields are protected by a spinlock,
and annotating which functions acquire a spinlock, in order to prevent
nesting. Exclude sbi_domain_register() from analysis, as it initializes
a domain's spinlock.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-11-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 1ba332872b lib: htif: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the HTIF driver
implementation.

This requires annotating the tohost/fromhost and htif_console_buf
globals to indicate that they are protected by htif_lock.
htif_system_reset() can be excluded since it is only called under
special circumstances from sbi_system_reset(), when all other harts
have been stopped.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-10-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López f5fd674f18 lib: sbi_timer: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the timer
implementation. This includes indicating which fields are protected
by a spinlock, and annotating which functions must be called under a
spinlock. Exclude sbi_timer_init(), as the spinlock cannot be acquired
before it is initialized.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-9-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 497ca4ccad lib: rpmi: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the RPMI mailbox
implementation. This includes indicating which fields are protected
by a spinlock, and annotating which functions must be called under a
spinlock. Exclude rpmi_shmem_transport_init(), as the spinlock cannot be
acquired before it is initialized.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-8-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 2d34c0e0e1 lib: sbi_scratch: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the scratch space
implementation. This only consists of annotating that the extra_offset
global must be accessed under the extra_lock spinlock.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-7-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López eaac726ea1 lib: sbi_heap: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the heap allocator.
This includes indicating which fields are protected by a spinlock and
annotating functions that must be called under a spinlock. Exclude
sbi_heap_init_new(), as the spinlock cannot be acquired before it is
initialized.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-6-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López 832ede6a8f lib: sbi_fifo: add TSA annotations
Add Thread Safety Analysis (TSA) annotations for the FIFO
implementation. This consists of indicating which fields are protected
by a spinlock and annotating functionst that must be called under a
spinlock. sbi_fifo_init() must be excluded, as acquiring a spinlock
before it is initialized does not make sense.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-5-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Carlos López b8f7e9ea80 lib: sbi_locks: annotate spinlock APIs for TSA
Add Thread Safety Analysis (TSA) annotations for spinlock APIs, allowing
the compiler to reason about lock acquisition and release.

Annotations have a different meaning if used in header declarations vs C
file implementations. In header declarations, they specify the semantics
of the given function, meaning that we must specify that the spinlock
functions acquire and release a lock ("capability" in clang terms).
In function implementations, attributes affect the function body and
callers in the same translation unit; since the spinlock functions
contain inline assembly, they must be excluded from analysis.

Signed-off-by: Carlos López <carlos.lopezr4096@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260909162322.29778-4-carlos.lopezr4096@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:48:30 +05:30
Anup Patel 55b6b73435 lib: sbi_domain: Introduce domain intialization order
Currently, the domain initialization order is implied by the order
in which domains are populated by sbi_platform_domains_init() from
sbi_domain_finalize(). This is not documented anywhere and forces
unecessary ordering between domain DT nodes.

To address the above, introduce per-domain 32-bit integer to represent
intialization order (aka "init_order") where domain with a lower
initialization order will be booted first and two domains must not
have same initialization order. For DT based domain creation, new
"init-order" DT property can be used in domain DT node to specify
the initialization order. The ROOT domain is assumed to have lowest
initialization order (aka 0xffffffff).

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Reviewed-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
Link: https://lore.kernel.org/r/20260905131748.922920-4-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:21:21 +05:30
Anup Patel 71faf5a307 lib: sbi_domain: Re-work boot-time assignment of HARTs to non-ROOT domains
Assign a non-ROOT domain to a HART on first come first serve basis if the
HART is listed as a possible HART of the non-ROOT domain. If no non-ROOT
domain list a HART as possible HART then the HART is assigned to the ROOT
domain.

This allows us to drop the OpenSBI specific DT property from each CPU DT
node (aka "opensbi-domain" Dt property).

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Reviewed-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
Link: https://lore.kernel.org/r/20260905131748.922920-3-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:21:21 +05:30
Anup Patel 6dc5a896b1 lib: sbi_domain: Check possible harts in sbi_domain_context_enter/exit()
When context switching to a domain the current hart MUST be part
of the possible harts of that domain. Add appropriate checks in
sbi_domain_context_enter/exit() along these lines.

Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Tested-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Reviewed-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
Link: https://lore.kernel.org/r/20260905131748.922920-2-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-16 09:21:21 +05:30
Rahul Pathak 3593a5facc lib: sbi_domain: Rename per-domain data to per-domain state
The per-domain sbi_domain_data hold each domain
associated state like hart context in sbi_domain_context,
mpxy state in sbi_mpxy, and others like each domain backed
by the corresponding MPT (SMMPT).
DATA reads as a generic name, while every use stores state.
Rename functions and macros appropriately. There are no
functional changes.

Signed-off-by: Rahul Pathak <rahul.pathak@oss.qualcomm.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260820121309.2551296-3-rahul.pathak@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-05 12:53:15 +05:30
Rahul Pathak 35af7c8c7e lib: sbi: Rename map_range/unmap_range functions
*_map_range and *_unmap_range functions are required
in M-Mode to get the temporary access to S-Mode and U-Mode
regions. Thse functions only operate using the TYPE_MEMORY
memory protection mechanisms. Rename them to reflect the
actual usage of these functions and let generic map/unmap_range
names to implement generic functions

Signed-off-by: Rahul Pathak <rahul.pathak@oss.qualcomm.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Pawandeep Oza <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260820121309.2551296-2-rahul.pathak@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-05 12:52:39 +05:30
whensun 7774b82e4a lib: sbi: Fix FP state enable in sbi_fp_save
sbi_fp_save() currently sets mstatus.VS before executing floating-point
store instructions. VS controls vector state and does not enable
floating-point instructions.

Set mstatus.FS instead, matching sbi_fp_restore(), so floating-point
state can be saved when FS was previously Off.

Fixes: 718e1d194f ("lib: sbi: Add floating-point context save/restore support.")
Signed-off-by: whensun <theodoruswensanfebruanto025@gmail.com>
Co-developed-by: Huamao Wu <huamao.wu@spacemit.com>
Signed-off-by: Huamao Wu <huamao.wu@spacemit.com>
Link: https://lore.kernel.org/r/20260828213104.84490-1-theodoruswensanfebruanto025@gmail.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-05 12:33:48 +05:30
Heinrich Schuchardt bf459f85bf lib: sbi_irqchip: error handling in sbi_irqchip_raw_handler_default()
sbi_irqchip_find_handler() may return NULL.
Check the return value before dereferencing.

Addresses-Coverity-ID: 1677408 Dereference null return value
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260820134431.167336-1-heinrich.schuchardt@canonical.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-04 13:55:01 +05:30
Ben Zong-You Xie 7384968e02 lib: sbi: respect scounteren when emulating timeh
beef2f6937 ("lib: sbi: Respect scounteren when emulating the time
CSR") restored it for TIME only, so on RV32 supervisor software still
cannot restrict U-mode or VS-mode access to time: rdtime traps as
intended while rdtimeh keeps returning the upper half.

Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260805093354.1022980-1-ben717@andestech.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-04 13:44:57 +05:30
David E. Garcia Porras c5077d497d lib: sbi_pmu: Fix counter and event info error codes as per SBI v3.0 spec
Align the PMU extension implementation with the error codes required
by the SBI v3.0 specification, chapter 11:

 - sbi_pmu_counter_start and sbi_pmu_counter_stop (secs 11.9-11.10,
   tables 39-42): the start_flags/stop_flags bits 2:(XLEN-1) are
   reserved and must be zero, so return SBI_ERR_INVALID_PARAM when any
   reserved flag bit is set. Introduce SBI_PMU_START_FLAGS_MASK and
   SBI_PMU_STOP_FLAGS_MASK for the valid bits of each function.

 - sbi_pmu_counter_start and sbi_pmu_counter_stop (tables 40 and 42):
   return SBI_ERR_ALREADY_STARTED / SBI_ERR_ALREADY_STOPPED when the
   set of counters includes a counter which is already started or
   stopped, instead of ignoring the error returned for each counter.

 - sbi_pmu_event_get_info (sec 11.14, table 47): the output word must
   indicate whether the event is supported, but firmware events were
   only matched against the hardware event map and were always
   reported as unsupported. Report a validated firmware event as
   supported.

Signed-off-by: David E. Garcia Porras <david.garcia@aheadcomputing.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260818210023.466462-4-david.garcia@aheadcomputing.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-04 11:43:53 +05:30
David E. Garcia Porras d34a39df77 lib: sbi_pmu: Match raw event selector only against raw event map entries
sbi_pmu_event_get_info() walks hw_event_map[] to decide whether a
requested event is supported. For SBI_PMU_EVENT_RAW_IDX and
SBI_PMU_EVENT_RAW_V2_IDX it compares the requested event_data against
temp->select / temp->select_mask without first checking that the map
entry being examined is itself a raw event entry.

Non-raw hardware event entries are added via sbi_pmu_add_hw_event_counter_map(),
leave select and select_mask at zero, hence they satisfy:

	temp->select == (event_data & temp->select_mask)

so the first non-raw entry visited will always match.
The issue's observability depends purely on the ordering of hw_event_map[]:
if the platform registers its raw events last, every raw event query, including
unsupported ones, will be reported as supported.

Fix it by checking event_idx against temp->start_idx and temp->end_idx before
comparing select/select_mask.

Fixes: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: David E. Garcia Porras <david.garcia@aheadcomputing.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260818210023.466462-3-david.garcia@aheadcomputing.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-03 13:37:34 +05:30
David E. Garcia Porras d97cfb33ae lib: sbi_pmu: Return invalid param error for reserved event_idx bits
As per section 11.14 of the SBI specification (Function: Get PMU Event
Info, FID #8), Table 47, the event_idx word of an event info entry
only uses BIT[0:19]; BIT[20:31] are reserved for the future purpose
and must be zero. Table 48 further requires the SBI implementation to
return SBI_ERR_INVALID_PARAM if any reserved bit in an event_idx word
is set.

sbi_pmu_event_get_info() does not check the reserved bits, so a
malformed event_idx is silently passed on to pmu_event_validate()
instead of failing the call. Add SBI_PMU_EVENT_IDX_MBZ_MASK covering
the must-be-zero bits and return SBI_ERR_INVALID_PARAM when any of
them are set.

Fixes: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: David E. Garcia Porras <david.garcia@aheadcomputing.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260818210023.466462-2-david.garcia@aheadcomputing.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-09-03 13:27:45 +05:30