[PATCH 2/7] lib: sbi: dbtr: share trigger checks of install and update
Himanshu Chauhan
himanshu.chauhan at oss.qualcomm.com
Sun Nov 22 23:42:02 PST 2026
On Sat, Nov 21, 2026 at 12:11:14AM +0100, Heinrich Schuchardt wrote:
> sbi_dbtr_install_trig() validates every trigger configuration before it
> programs any trigger. sbi_dbtr_update_trig() only checked the optional
> tdata2/tdata3 CSRs and the platform hook. It did not check that the
> trigger type is supported and it did not reject a tdata1 with the M or
> DMODE bit set, which install refuses. Supervisor software could
> therefore update an installed trigger into one that fires in M-mode or
> that is owned by the debug module.
>
> Move the checks of one configuration into dbtr_check_trigger_config()
> and call it from both functions. The checks and their order are the same
> as before for install. For update the configuration is additionally
> checked against the type mask of the trigger that is updated, instead of
> against any trigger of the hart.
>
> The helper reads tdata1, tdata2 and tdata3 from the shared memory once
> and uses the local copies for all checks.
>
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
> ---
> lib/sbi/sbi_dbtr.c | 114 +++++++++++++++++++++++----------------------
> 1 file changed, 59 insertions(+), 55 deletions(-)
>
> diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
> index 2108ca9e..53fcb0a2 100644
> --- a/lib/sbi/sbi_dbtr.c
> +++ b/lib/sbi/sbi_dbtr.c
> @@ -682,6 +682,55 @@ int sbi_dbtr_read_trig(unsigned long smode,
> return SBI_SUCCESS;
> }
>
> +/*
> + * Check the configuration of one trigger read from the shared memory.
> + *
> + * The same checks apply when a trigger is installed and when it is updated.
> + *
> + * @hs: trigger state of the calling hart
> + * @trig: trigger the configuration is meant for (update), or NULL if any
> + * trigger of the hart may be used (install)
> + * @recv: configuration to check
> + *
> + * SBI v3.0 sec 19.4 and 19.5 require SBI_ERR_NOT_SUPPORTED when a trigger
> + * programs a non-zero value into an unimplemented optional CSR. Only the
> + * "whole CSR unimplemented" case is caught here; WARL bits tied off inside an
> + * otherwise-implemented CSR are delegated to the device-specific
> + * trigger_supported() callback.
> + */
You can drop the reference to SBI sections. Mention them in cover letter. The comment
has been copied from the preivous implementation of the in function check. It is
neither clear nor approriate to what exactly function is trying to achieve. Please
take a look and add information pertaining to the function's job.
> +static int dbtr_check_trigger_config(struct sbi_dbtr_hart_triggers_state *hs,
> + const struct sbi_dbtr_trigger *trig,
> + const struct sbi_dbtr_data_msg *recv,
> + bool tdata2_impl, bool tdata3_impl)
> +{
> + /* Read the shared memory only once */
> + unsigned long tdata1 = lle_to_cpu(recv->tdata1);
> + unsigned long tdata2 = lle_to_cpu(recv->tdata2);
> + unsigned long tdata3 = lle_to_cpu(recv->tdata3);
> + unsigned long type = TDATA1_GET_TYPE(tdata1);
> +
> + if (!dbtr_trigger_supported(type))
> + return SBI_ERR_FAILED;
> +
> + if (!dbtr_trigger_valid(type, tdata1))
> + return SBI_ERR_INVALID_PARAM;
> +
> + if ((tdata2 && !tdata2_impl) || (tdata3 && !tdata3_impl))
> + return SBI_ERR_NOT_SUPPORTED;
> +
> + if (trig) {
> + if (!__test_bit(type, &trig->type_mask) ||
> + !dbtr_trigger_hw_supported(trig->index, tdata1, tdata2,
> + tdata3))
> + return SBI_ERR_NOT_SUPPORTED;
> + } else if (!dbtr_trigger_any_hw_supported(hs, tdata1, tdata2,
> + tdata3)) {
> + return SBI_ERR_NOT_SUPPORTED;
> + }
> +
> + return SBI_SUCCESS;
> +}
> +
> int sbi_dbtr_install_trig(unsigned long smode,
> unsigned long trig_count, unsigned long *out)
> {
> @@ -689,9 +738,8 @@ int sbi_dbtr_install_trig(unsigned long smode,
> union sbi_dbtr_shmem_entry *entry;
> struct sbi_dbtr_data_msg *recv;
> struct sbi_dbtr_id_msg *xmit;
> - unsigned long ctrl;
> u32 claimed = 0;
> - int slot;
> + int slot, rc;
> struct sbi_dbtr_trigger *trig;
> struct sbi_dbtr_hart_triggers_state *hs = NULL;
> bool tdata2_impl, tdata3_impl;
> @@ -710,52 +758,20 @@ int sbi_dbtr_install_trig(unsigned long smode,
> sbi_hart_protection_temp_map_range((unsigned long)shmem_base,
> trig_count * sizeof(*entry));
>
> - /*
> - * SBI v3.0 sec 19.4 requires SBI_ERR_NOT_SUPPORTED when a trigger
> - * programs a non-zero value into an unimplemented optional CSR. Only
> - * the "whole CSR unimplemented" case is caught here; WARL bits tied
> - * off inside an otherwise-implemented CSR are delegated to the
> - * device-specific trigger_supported() callback via
> - * dbtr_trigger_any_hw_supported().
> - */
> tdata2_impl = tdata_implemented(CSR_TDATA2);
> tdata3_impl = tdata_implemented(CSR_TDATA3);
>
> /* Check requested triggers configuration */
> for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
> recv = (struct sbi_dbtr_data_msg *)(&entry->data);
> - ctrl = recv->tdata1;
>
> - if (!dbtr_trigger_supported(TDATA1_GET_TYPE(ctrl))) {
> + rc = dbtr_check_trigger_config(hs, NULL, recv,
> + tdata2_impl, tdata3_impl);
> + if (rc) {
> *out = _idx;
> sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
> trig_count * sizeof(*entry));
> - return SBI_ERR_FAILED;
> - }
> -
> - if (!dbtr_trigger_valid(TDATA1_GET_TYPE(ctrl), ctrl)) {
> - *out = _idx;
> - sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
> - trig_count * sizeof(*entry));
> - return SBI_ERR_INVALID_PARAM;
> - }
> -
> - if ((recv->tdata2 && !tdata2_impl) ||
> - (recv->tdata3 && !tdata3_impl)) {
> - *out = _idx;
> - sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
> - trig_count * sizeof(*entry));
> - return SBI_ERR_NOT_SUPPORTED;
> - }
> -
> - if (!dbtr_trigger_any_hw_supported(hs,
> - lle_to_cpu(recv->tdata1),
> - lle_to_cpu(recv->tdata2),
> - lle_to_cpu(recv->tdata3))) {
> - *out = _idx;
> - sbi_hart_protection_temp_unmap_range((unsigned long)shmem_base,
> - trig_count * sizeof(*entry));
> - return SBI_ERR_NOT_SUPPORTED;
> + return rc;
> }
> }
>
> @@ -867,6 +883,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
> void *shmem_base = NULL;
> struct sbi_dbtr_hart_triggers_state *hs = NULL;
> bool tdata2_impl, tdata3_impl;
> + int rc;
>
> hs = dbtr_thishart_state_ptr();
> if (!hs)
> @@ -880,12 +897,6 @@ int sbi_dbtr_update_trig(unsigned long smode,
> if (trig_count > hs->total_trigs)
> return SBI_ERR_BAD_RANGE;
>
> - /*
> - * SBI v3.0 sec 19.5 requires SBI_ERR_NOT_SUPPORTED when a trigger
> - * programs a non-zero value into an unimplemented optional CSR. Only
> - * the "whole CSR unimplemented" case is caught; WARL bits tied off
> - * inside an otherwise-implemented CSR are not.
> - */
> tdata2_impl = tdata_implemented(CSR_TDATA2);
> tdata3_impl = tdata_implemented(CSR_TDATA3);
>
> @@ -905,18 +916,11 @@ int sbi_dbtr_update_trig(unsigned long smode,
> return SBI_ERR_FAILED;
> }
>
> - if ((entry->data.tdata2 && !tdata2_impl) ||
> - (entry->data.tdata3 && !tdata3_impl)) {
> + rc = dbtr_check_trigger_config(hs, trig, &entry->data,
> + tdata2_impl, tdata3_impl);
> + if (rc) {
> sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
> - return SBI_ERR_NOT_SUPPORTED;
> - }
> -
> - if (!dbtr_trigger_hw_supported(trig->index,
> - lle_to_cpu(entry->data.tdata1),
> - lle_to_cpu(entry->data.tdata2),
> - lle_to_cpu(entry->data.tdata3))) {
> - sbi_hart_protection_temp_unmap_range((unsigned long)entry, sizeof(*entry));
> - return SBI_ERR_NOT_SUPPORTED;
> + return rc;
> }
>
> dbtr_trigger_setup(trig, &entry->data);
Liutong was trying to solve the same TOCTOU problem. It is under review and I had similar comments on that patch series.
Could you please take a look at that?
Regards
Himanshu
> --
> 2.53.0
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
More information about the opensbi
mailing list