* [PATCH] platform: generic: spacemit: k3: deny S-mode access to the reset vectors
@ 2026-11-03 9:44 liutong
2026-11-12 7:59 ` Xiang W
0 siblings, 1 reply; 4+ messages in thread
From: liutong @ 2026-11-03 9:44 UTC (permalink / raw)
To: opensbi; +Cc: troy.mitchell, xianbin.zhu, anup, liutong
spacemit_k3_pre_init() programs the per-cluster reset vector registers
with the warm boot entry point, so a hart that is powered back on
resumes in M-mode at that address. The registers sit in ordinary MMIO
and the K3 support registers no memory region covering them, so they
fall to the root domain's allow-everything region and S-mode can write
them. S-mode can then point a cluster's reset vector at its own code
and have that code run in M-mode the next time the cluster is powered
up.
Add a root domain region over the naturally aligned 1KB block at
0xd4282c00, which contains all four reset vector register pairs, with
M-mode-only permissions. It sorts before the allow-everything region
and so takes a lower PMP entry index. The APMU registers live below
that block and are left alone, so S-mode keeps the access it uses for
core idle and wakeup.
SpaceMit's own k3-br-v1.0.y branch already protects this address range
in spacemit_k3.c, as part of a larger page; the protection was not
carried over when K3 support was upstreamed.
Signed-off-by: liutong <liutong@iscas.ac.cn>
---
Found while auditing the DBTR shared memory paths; the reset vector
registers turned up because they sit in the same MMIO page the SpaceMit
power management code touches.
Why this range and not the whole page. SpaceMit's k3-br-v1.0.y branch
protects 0xd4282000 .. 0xd4282fff in spacemit_k3.c:
rc = sbi_domain_root_add_memrange(REGISTER_PRESERVATION_BASE,
REGISTER_PRESERVATION_SIZE, PAGE_SIZE,
SBI_DOMAIN_MEMREGION_M_RWX);
That covers the APMU block at 0xd4282800 as well. Taking only the 1KB
block at 0xd4282c00 keeps every reset vector register inside it:
C0 0xd4282db0/db4 C1 0xd4282eb0/eb4
C2 0xd4282fe8/fec C3 0xd4282e60/e64
while the highest APMU register in use here is PMU_AP_BASE + 0x36c =
0xd4282b6c, below the block. S-mode therefore keeps the APMU access it
needs. That seemed worth preserving after
4c5d9d9f ("lib: utils/andes: add the SMU and LLC registers to the root
domain"), where denying S-mode its power management registers broke
suspend and resume.
The block is naturally aligned, so it needs one PMP entry.
Verified by building for generic (RV64 and RV32) and booting qemu virt,
where the K3 early_init does not run and the root region list is
unchanged. I do not have K3 hardware running an upstream build to
confirm the region appears and that idle and wakeup still work; if
someone on the SpaceMit side can check that, I would appreciate it.
Not addressed here: platform/generic/spacemit/k1.c programs
C0/C1_RVBADDR in spacemit_k1_pre_init() the same way, at addresses in
the same window, and registers no region either. It looks like the same
gap, but the K1 vendor branch is not the one I compared against, so I
have left it for someone who knows that part.
platform/generic/include/spacemit/k3.h | 9 +++++++++
platform/generic/spacemit/k3.c | 16 ++++++++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/platform/generic/include/spacemit/k3.h b/platform/generic/include/spacemit/k3.h
index f444d064..e9580ecb 100644
--- a/platform/generic/include/spacemit/k3.h
+++ b/platform/generic/include/spacemit/k3.h
@@ -108,6 +108,15 @@
PMU_CX_IDLE_MP_SCU_SRAM_PWRDWN | \
PMU_CX_IDLE_ACNACTM_HW_CTRL)
+/*
+ * All four per-cluster reset vector register pairs fall inside this
+ * naturally aligned 1KB block. The APMU registers sit below it and are
+ * deliberately left out, so S-mode keeps the access it uses for core
+ * idle and wakeup.
+ */
+#define K3_RESET_VECTOR_BASE 0xd4282c00
+#define K3_RESET_VECTOR_SIZE 0x400
+
/* boot entry for A100 clusters */
#define C2_RVBADDR_LO_ADDR (0xd4282c00 + 0x3e8)
#define C2_RVBADDR_HI_ADDR (0xd4282c00 + 0x3ec)
diff --git a/platform/generic/spacemit/k3.c b/platform/generic/spacemit/k3.c
index 7922ab11..9048678d 100644
--- a/platform/generic/spacemit/k3.c
+++ b/platform/generic/spacemit/k3.c
@@ -10,6 +10,7 @@
#include <platform_override.h>
#include <sbi/riscv_asm.h>
#include <sbi/riscv_io.h>
+#include <sbi/sbi_domain.h>
#include <sbi/sbi_hfence.h>
#include <sbi/sbi_tlb.h>
#include <spacemit/k3.h>
@@ -96,8 +97,19 @@ static void spacemit_k3_pre_init(void)
static int spacemit_k3_early_init(bool cold_boot)
{
- if (cold_boot)
- spacemit_k3_pre_init();
+ int rc;
+
+ if (!cold_boot)
+ return generic_early_init(cold_boot);
+
+ spacemit_k3_pre_init();
+
+ rc = sbi_domain_root_add_memrange(K3_RESET_VECTOR_BASE,
+ K3_RESET_VECTOR_SIZE,
+ K3_RESET_VECTOR_SIZE,
+ SBI_DOMAIN_MEMREGION_M_RWX);
+ if (rc)
+ return rc;
return generic_early_init(cold_boot);
}
base-commit: 2c7788e4c3efa63d670ee90402dac2a9799d0bd7
--
2.34.1
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] platform: generic: spacemit: k3: deny S-mode access to the reset vectors
2026-11-03 9:44 [PATCH] platform: generic: spacemit: k3: deny S-mode access to the reset vectors liutong
@ 2026-11-12 7:59 ` Xiang W
2026-11-12 9:13 ` liutong
0 siblings, 1 reply; 4+ messages in thread
From: Xiang W @ 2026-11-12 7:59 UTC (permalink / raw)
To: liutong, opensbi; +Cc: troy.mitchell, xianbin.zhu, anup, gaohan
在 2026-11-03二的 09:44 +0000,liutong写道:
> spacemit_k3_pre_init() programs the per-cluster reset vector registers
> with the warm boot entry point, so a hart that is powered back on
> resumes in M-mode at that address. The registers sit in ordinary MMIO
> and the K3 support registers no memory region covering them, so they
> fall to the root domain's allow-everything region and S-mode can write
> them. S-mode can then point a cluster's reset vector at its own code
> and have that code run in M-mode the next time the cluster is powered
> up.
>
> Add a root domain region over the naturally aligned 1KB block at
> 0xd4282c00, which contains all four reset vector register pairs, with
> M-mode-only permissions. It sorts before the allow-everything region
> and so takes a lower PMP entry index. The APMU registers live below
> that block and are left alone, so S-mode keeps the access it uses for
> core idle and wakeup.
>
> SpaceMit's own k3-br-v1.0.y branch already protects this address range
> in spacemit_k3.c, as part of a larger page; the protection was not
> carried over when K3 support was upstreamed.
>
> Signed-off-by: liutong <liutong@iscas.ac.cn>
> ---
>
> Found while auditing the DBTR shared memory paths; the reset vector
> registers turned up because they sit in the same MMIO page the SpaceMit
> power management code touches.
>
> Why this range and not the whole page. SpaceMit's k3-br-v1.0.y branch
> protects 0xd4282000 .. 0xd4282fff in spacemit_k3.c:
>
> rc = sbi_domain_root_add_memrange(REGISTER_PRESERVATION_BASE,
> REGISTER_PRESERVATION_SIZE, PAGE_SIZE,
> SBI_DOMAIN_MEMREGION_M_RWX);
>
> That covers the APMU block at 0xd4282800 as well. Taking only the 1KB
> block at 0xd4282c00 keeps every reset vector register inside it:
>
> C0 0xd4282db0/db4 C1 0xd4282eb0/eb4
> C2 0xd4282fe8/fec C3 0xd4282e60/e64
>
> while the highest APMU register in use here is PMU_AP_BASE + 0x36c =
> 0xd4282b6c, below the block. S-mode therefore keeps the APMU access it
> needs. That seemed worth preserving after
> 4c5d9d9f ("lib: utils/andes: add the SMU and LLC registers to the root
> domain"), where denying S-mode its power management registers broke
> suspend and resume.
>
> The block is naturally aligned, so it needs one PMP entry.
>
> Verified by building for generic (RV64 and RV32) and booting qemu virt,
> where the K3 early_init does not run and the root region list is
> unchanged. I do not have K3 hardware running an upstream build to
> confirm the region appears and that idle and wakeup still work; if
> someone on the SpaceMit side can check that, I would appreciate it.
>
> Not addressed here: platform/generic/spacemit/k1.c programs
> C0/C1_RVBADDR in spacemit_k1_pre_init() the same way, at addresses in
> the same window, and registers no region either. It looks like the same
> gap, but the K1 vendor branch is not the one I compared against, so I
> have left it for someone who knows that part.
>
> platform/generic/include/spacemit/k3.h | 9 +++++++++
> platform/generic/spacemit/k3.c | 16 ++++++++++++++--
> 2 files changed, 23 insertions(+), 2 deletions(-)
>
> diff --git a/platform/generic/include/spacemit/k3.h b/platform/generic/include/spacemit/k3.h
> index f444d064..e9580ecb 100644
> --- a/platform/generic/include/spacemit/k3.h
> +++ b/platform/generic/include/spacemit/k3.h
> @@ -108,6 +108,15 @@
> PMU_CX_IDLE_MP_SCU_SRAM_PWRDWN | \
> PMU_CX_IDLE_ACNACTM_HW_CTRL)
>
> +/*
> + * All four per-cluster reset vector register pairs fall inside this
> + * naturally aligned 1KB block. The APMU registers sit below it and are
> + * deliberately left out, so S-mode keeps the access it uses for core
> + * idle and wakeup.
> + */
> +#define K3_RESET_VECTOR_BASE 0xd4282c00
> +#define K3_RESET_VECTOR_SIZE 0x400
> +
> /* boot entry for A100 clusters */
> #define C2_RVBADDR_LO_ADDR (0xd4282c00 + 0x3e8)
> #define C2_RVBADDR_HI_ADDR (0xd4282c00 + 0x3ec)
> diff --git a/platform/generic/spacemit/k3.c b/platform/generic/spacemit/k3.c
> index 7922ab11..9048678d 100644
> --- a/platform/generic/spacemit/k3.c
> +++ b/platform/generic/spacemit/k3.c
> @@ -10,6 +10,7 @@
> #include <platform_override.h>
> #include <sbi/riscv_asm.h>
> #include <sbi/riscv_io.h>
> +#include <sbi/sbi_domain.h>
> #include <sbi/sbi_hfence.h>
> #include <sbi/sbi_tlb.h>
> #include <spacemit/k3.h>
> @@ -96,8 +97,19 @@ static void spacemit_k3_pre_init(void)
>
> static int spacemit_k3_early_init(bool cold_boot)
> {
> - if (cold_boot)
> - spacemit_k3_pre_init();
> + int rc;
> +
> + if (!cold_boot)
> + return generic_early_init(cold_boot);
> +
> + spacemit_k3_pre_init();
> +
> + rc = sbi_domain_root_add_memrange(K3_RESET_VECTOR_BASE,
> + K3_RESET_VECTOR_SIZE,
> + K3_RESET_VECTOR_SIZE,
> + SBI_DOMAIN_MEMREGION_M_RWX);
Hi,
I have a question regarding this patch.
My colleague Han Gao <gaohan@iscas.ac.cn> has access to a K3
platform and tested it. The PMP granularity on K3 is 4 KiB,
so a mapping size smaller than that will be ignored.
Could you clarify how this case should be handled?
Best regards,
Xiang W
> + if (rc)
> + return rc;
>
> return generic_early_init(cold_boot);
> }
>
> base-commit: 2c7788e4c3efa63d670ee90402dac2a9799d0bd7
> --
> 2.34.1
>
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] platform: generic: spacemit: k3: deny S-mode access to the reset vectors
2026-11-12 7:59 ` Xiang W
@ 2026-11-12 9:13 ` liutong
2026-11-12 14:42 ` Troy Mitchell
0 siblings, 1 reply; 4+ messages in thread
From: liutong @ 2026-11-12 9:13 UTC (permalink / raw)
To: opensbi, Xiang W; +Cc: gaohan, troy.mitchell, xianbin.zhu, anup
On Thu, 12 Nov 2026 07:59:32 +0000, Xiang W wrote:
> The PMP granularity on K3 is 4 KiB, so a mapping size smaller than
> that will be ignored.
>
> Could you clarify how this case should be handled?
Thanks, and thanks to Han Gao for putting it on hardware. You are
right.
The region order comes out of sbi_domain_memregion_init() without
reference to the PMP granularity, and both protection backends then
drop a region whose order is below it:
if (pmp_log2gran <= reg->order && pmp_addr < pmp_addr_max)
...program the entry...
else
sbi_printf("Can not configure pmp for domain %s ...
(lib/sbi/sbi_hart_pmp.c:209 and :390). So with a 4 KiB granularity the
1 KiB region in my patch is never programmed and the firmware boots on
with one line printed. The patch was a no-op on your board. I had only
tested on qemu virt, where the granularity is 4 bytes and any size
passes.
Rounding up to the page is not a fix either, and this is the part I had
wrong. The page at 0xd4282000 also holds the APMU block, which upstream
Linux drives directly from S-mode:
syscon_apmu: system-controller@d4282800 {
compatible = "spacemit,k3-syscon-apmu";
reg = <0x0 0xd4282800 0x0 0x400>;
#clock-cells = <1>;
#power-domain-cells = <1>;
};
(arch/riscv/boot/dts/spacemit/k3.dtsi, with drivers/clk/spacemit/ccu-k3.c
behind it). Denying S-mode the page on its own would take out the clock,
reset and power domain provider for the whole board.
SpaceMit's k3-br-v1.0.y does not only register the region. It also
installs emulate_load and emulate_store, which translate the faulting
S-mode address and perform the access in M-mode, except for an
m_only_ranges list that it refuses to emulate - and that list is exactly
the four RVBADDR pairs plus the PMU CAP and CX idle and wakeup
registers. The comment on the barriers in that emulator names the kernel
CCU driver's recalc_rate loop, so the S-mode traffic is expected there by
design. Upstream already has the hooks for that
(sbi_platform_operations.emulate_load and emulate_store), although no
in-tree platform uses them yet.
That two part shape is what makes the protection safe on that branch,
and my patch carried over only the first part. I am dropping it rather
than sending a v2 that would break clocks.
Troy, Xianbin: would you be interested in upstreaming that protection
from k3-br-v1.0.y?
One smaller thing this exposed, separate from K3: a region below the PMP
granularity is dropped with a single printf and boot continues, so a
platform author gets no signal that the protection they registered is
not in effect. That is how I missed it. Whether it should be louder, or
rounded up, looks like its own discussion and I did not want to fold it
into this thread.
Thanks again for catching this before it went in.
liutong
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] platform: generic: spacemit: k3: deny S-mode access to the reset vectors
2026-11-12 9:13 ` liutong
@ 2026-11-12 14:42 ` Troy Mitchell
0 siblings, 0 replies; 4+ messages in thread
From: Troy Mitchell @ 2026-11-12 14:42 UTC (permalink / raw)
To: liutong, opensbi, Xiang W; +Cc: gaohan, troy.mitchell, xianbin.zhu, anup
[-- Attachment #1.1: Type: text/plain, Size: 3259 bytes --]
On Thu Nov 12, 2026 at 9:13 AM GMT, liutong wrote:
> On Thu, 12 Nov 2026 07:59:32 +0000, Xiang W wrote:
>> The PMP granularity on K3 is 4 KiB, so a mapping size smaller than
>> that will be ignored.
>>
>> Could you clarify how this case should be handled?
>
> Thanks, and thanks to Han Gao for putting it on hardware. You are
> right.
>
> The region order comes out of sbi_domain_memregion_init() without
> reference to the PMP granularity, and both protection backends then
> drop a region whose order is below it:
>
> if (pmp_log2gran <= reg->order && pmp_addr < pmp_addr_max)
> ...program the entry...
> else
> sbi_printf("Can not configure pmp for domain %s ...
>
> (lib/sbi/sbi_hart_pmp.c:209 and :390). So with a 4 KiB granularity the
> 1 KiB region in my patch is never programmed and the firmware boots on
> with one line printed. The patch was a no-op on your board. I had only
> tested on qemu virt, where the granularity is 4 bytes and any size
> passes.
>
> Rounding up to the page is not a fix either, and this is the part I had
> wrong. The page at 0xd4282000 also holds the APMU block, which upstream
> Linux drives directly from S-mode:
>
> syscon_apmu: system-controller@d4282800 {
> compatible = "spacemit,k3-syscon-apmu";
> reg = <0x0 0xd4282800 0x0 0x400>;
> #clock-cells = <1>;
> #power-domain-cells = <1>;
> };
>
> (arch/riscv/boot/dts/spacemit/k3.dtsi, with drivers/clk/spacemit/ccu-k3.c
> behind it). Denying S-mode the page on its own would take out the clock,
> reset and power domain provider for the whole board.
>
> SpaceMit's k3-br-v1.0.y does not only register the region. It also
> installs emulate_load and emulate_store, which translate the faulting
> S-mode address and perform the access in M-mode, except for an
> m_only_ranges list that it refuses to emulate - and that list is exactly
> the four RVBADDR pairs plus the PMU CAP and CX idle and wakeup
> registers. The comment on the barriers in that emulator names the kernel
> CCU driver's recalc_rate loop, so the S-mode traffic is expected there by
> design. Upstream already has the hooks for that
> (sbi_platform_operations.emulate_load and emulate_store), although no
> in-tree platform uses them yet.
>
> That two part shape is what makes the protection safe on that branch,
> and my patch carried over only the first part. I am dropping it rather
> than sending a v2 that would break clocks.
>
> Troy, Xianbin: would you be interested in upstreaming that protection
> from k3-br-v1.0.y?
Yes, I'd be happy to take this over.
Anup and Xiang, would you be open to this approach (using emulate_load or
emulate_store for the shared page)? If so, I can send out an RFC first
to get your feedback.
>
> One smaller thing this exposed, separate from K3: a region below the PMP
> granularity is dropped with a single printf and boot continues, so a
> platform author gets no signal that the protection they registered is
> not in effect. That is how I missed it. Whether it should be louder, or
> rounded up, looks like its own discussion and I did not want to fold it
> into this thread.
>
> Thanks again for catching this before it went in.
>
> liutong
--
Troy Mitchell
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]
[-- Attachment #2: Type: text/plain, Size: 105 bytes --]
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-12 14:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-03 9:44 [PATCH] platform: generic: spacemit: k3: deny S-mode access to the reset vectors liutong
2026-11-12 7:59 ` Xiang W
2026-11-12 9:13 ` liutong
2026-11-12 14:42 ` Troy Mitchell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox