From: Jim Shu <jim.shu@sifive.com>
To: Chao Liu <chao.liu@yeah.net>,
Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Cc: alistair.francis@wdc.com, arikalo@gmail.com, atar4qemu@gmail.com,
aurelien@aurel32.net, david@redhat.com, deller@gmx.de,
edgar.iglesias@gmail.com, eduardo@habkost.net,
gaosong@loongson.cn, iii@linux.ibm.com, jcmvbkbc@gmail.com,
jiaxun.yang@flygoat.com, kbastian@mail.uni-paderborn.de,
laurent@vivier.eu, liwei1518@gmail.com,
marcel.apfelbaum@gmail.com, mark.cave-ayland@ilande.co.uk,
mrolnik@gmail.com, npiggin@gmail.com, palmer@dabbelt.com,
pbonzini@redhat.com, peterx@redhat.com, philmd@linaro.org,
qemu-devel@nongnu.org, qemu-ppc@nongnu.org,
qemu-riscv@nongnu.org, qemu-s390x@nongnu.org,
richard.henderson@linaro.org, shorne@gmail.com,
thuth@redhat.com, wangyanan55@huawei.com, zhao1.liu@intel.com,
zhiwei_liu@linux.alibaba.com
Subject: Re: [PATCH v2 13/18] hw/misc: riscv_worldguard: Add API to enable WG extension of CPU
Date: Wed, 8 Oct 2025 14:19:42 +0800 [thread overview]
Message-ID: <CALw707r3tBUGbZqYXbPc2B+so6AVhYqUr7ccYYjVidDqqAWemw@mail.gmail.com> (raw)
In-Reply-To: <4d658cff-d8ab-4d95-b340-c6b7b5375395@yeah.net>
Hi Daniel and Chao,
I will replace qemu_get_cpu() with cpu_by_arch_id().
However, cpu_by_arch_id() can still return NULL if the hartid is
invalid, so I will handle that case with a clean exit.
I will fix these in the v3 patch.
Thanks,
Jim
On Wed, Aug 13, 2025 at 11:10 AM Chao Liu <chao.liu@yeah.net> wrote:
>
> > On 4/17/25 7:52 AM, Jim Shu wrote:
> > > riscv_worldguard_apply_cpu() could enable WG CPU extension and set WG
> > > callback to CPUs. It is used by machine code after realizing global WG
> > > device.
> > >
> > > Signed-off-by: Jim Shu <jim.shu@sifive.com>
> > > ---
> > > hw/misc/riscv_worldguard.c | 87 ++++++++++++++++++++++++++++++
> > > include/hw/misc/riscv_worldguard.h | 1 +
> > > 2 files changed, 88 insertions(+)
> > >
> > > diff --git a/hw/misc/riscv_worldguard.c b/hw/misc/riscv_worldguard.c
> > > index b02bd28d02..1a910f4cf3 100644
> > > --- a/hw/misc/riscv_worldguard.c
> > > +++ b/hw/misc/riscv_worldguard.c
> > > @@ -92,6 +92,93 @@ uint32_t mem_attrs_to_wid(MemTxAttrs attrs)
> > > }
> > > }
> > >
> > > +static void riscv_cpu_wg_reset(CPURISCVState *env)
> > > +{
> > > + CPUState *cs = env_cpu(env);
> > > + RISCVCPU *cpu = RISCV_CPU(cs);
> > > + uint32_t mlwid, slwid, mwiddeleg;
> > > + uint32_t trustedwid;
> > > +
> > > + if (!riscv_cpu_cfg(env)->ext_smwg) {
> > > + return;
> > > + }
> > > +
> > > + if (worldguard_config == NULL) {
> > > + /*
> > > + * Note: This reset is dummy now and WG CSRs will be reset again
> > > + * after worldguard_config is realized.
> > > + */
> > > + return;
> > > + }
> > > +
> > > + trustedwid = worldguard_config->trustedwid;
> > > + if (trustedwid == NO_TRUSTEDWID) {
> > > + trustedwid = worldguard_config->nworlds - 1;
> > > + }
> > > +
> > > + /* Reset mlwid, slwid, mwiddeleg CSRs */
> > > + if (worldguard_config->hw_bypass) {
> > > + /* HW bypass mode */
> > > + mlwid = trustedwid;
> > > + } else {
> > > + mlwid = 0;
> > > + }
> > > + slwid = 0;
> > > + mwiddeleg = 0;
> > > +
> > > + env->mlwid = mlwid;
> > > + if (riscv_cpu_cfg(env)->ext_sswg) {
> > > + env->slwid = slwid;
> > > + env->mwiddeleg = mwiddeleg;
> > > + }
> > > +
> > > + /* Check mwid, mwidlist config */
> > > + if (worldguard_config != NULL) {
> > > + uint32_t valid_widlist = MAKE_64BIT_MASK(0, worldguard_config->nworlds);
> > > +
> > > + /* CPU use default mwid / mwidlist config if not set */
> > > + if (cpu->cfg.mwidlist == UINT32_MAX) {
> > > + /* mwidlist contains all WIDs */
> > > + cpu->cfg.mwidlist = valid_widlist;
> > > + }
> > > + if (cpu->cfg.mwid == UINT32_MAX) {
> > > + cpu->cfg.mwid = trustedwid;
> > > + }
> > > +
> > > + /* Check if mwid/mwidlist HW config is valid in NWorld. */
> > > + g_assert((cpu->cfg.mwidlist & ~valid_widlist) == 0);
> > > + g_assert(cpu->cfg.mwid < worldguard_config->nworlds);
> > > + }
> > > +}
> > > +
> > > +/*
> > > + * riscv_worldguard_apply_cpu - Enable WG extension of CPU
> > > + *
> > > + * Note: This API should be used after global WG device is created
> > > + * (riscv_worldguard_realize()).
> > > + */
> > > +void riscv_worldguard_apply_cpu(uint32_t hartid)
> > > +{
> > > + /* WG global config should exist */
> > > + g_assert(worldguard_config);
> >
> > We usually add g_asserts() after the variable declarations.
> >
> > > +
> > > + CPUState *cpu = qemu_get_cpu(hartid);
>
> arm_get_cpu() uses CPUState::cpu_index to obtain the corresponding CPUState pointer.
>
> However, CPUState::cpu_index and the RISC-V HART index are not necessarily strictly
> one-to-one (for instance, when the hartid base is non-zero or when hartids are
> discontinuous).
>
> Typically, we use arm_get_cpu() at the accelerators, rather than in hw/code.
>
> A better approach is to use cpu_by_arch_id() instead of qemu_get_cpu(),
> in RISC-V cpu_by_arch_id() uses the hartid.
>
> e.g.
>
> CPUState *cpu = cpu_by_arch_id(hartid);
>
>
> Thanks,
>
> Chao
>
> > > + RISCVCPU *rcpu = RISCV_CPU(cpu);
> > > + CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
> > > +
> > > + rcpu->cfg.ext_smwg = true;
> > > + if (riscv_has_ext(env, RVS) && riscv_has_ext(env, RVU)) {
> > > + rcpu->cfg.ext_sswg = true;
> > > + }
> >
> > riscv_has_ext() will segfault if env == NULL, and you're creating a code
> > path where this might happen:
> >
> > > + CPURISCVState *env = cpu ? cpu_env(cpu) : NULL;
> >
> > In fact, cpu == NULL will explode on you earlier via this macro:
> >
> > > + RISCVCPU *rcpu = RISCV_CPU(cpu);
> >
> > You can either handle cpu == NULL with a clean exit before using 'cpu' to assign
> > stuff or g_assert(cpu != NULL) for a more rude exit. But with this code as is
> > you're gambling with segfaults.
> >
> >
> > Thanks,
> >
> > Daniel
> >
> >
> > > +
> > > + /* Set machine specific WorldGuard callback */
> > > + env->wg_reset = riscv_cpu_wg_reset;
> > > + env->wid_to_mem_attrs = wid_to_mem_attrs;
> > > +
> > > + /* Reset WG CSRs in CPU */
> > > + env->wg_reset(env);
> > > +}
> > > +
> > > bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock)
> > > {
> > > uint32_t wid = mem_attrs_to_wid(attrs);
> > > diff --git a/include/hw/misc/riscv_worldguard.h b/include/hw/misc/riscv_worldguard.h
> > > index 8a533a0517..211a72e438 100644
> > > --- a/include/hw/misc/riscv_worldguard.h
> > > +++ b/include/hw/misc/riscv_worldguard.h
> > > @@ -48,6 +48,7 @@ extern struct RISCVWorldGuardState *worldguard_config;
> > >
> > > DeviceState *riscv_worldguard_create(uint32_t nworlds, uint32_t trustedwid,
> > > bool hw_bypass, bool tz_compat);
> > > +void riscv_worldguard_apply_cpu(uint32_t hartid);
> > >
> > > uint32_t mem_attrs_to_wid(MemTxAttrs attrs);
> > > bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock);
> >
>
next prev parent reply other threads:[~2025-10-08 6:21 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-17 10:52 [PATCH v2 00/18] Implements RISC-V WorldGuard extension v0.4 Jim Shu
2025-04-17 10:52 ` [PATCH v2 01/18] accel/tcg: Store section pointer in CPUTLBEntryFull Jim Shu
2025-04-17 10:52 ` [PATCH v2 02/18] system/physmem: Remove the assertion of page-aligned section number Jim Shu
2025-04-17 10:52 ` [PATCH v2 03/18] accel/tcg: memory access from CPU will pass access_type to IOMMU Jim Shu
2025-04-17 10:52 ` [PATCH v2 04/18] exec: Add RISC-V WorldGuard WID to MemTxAttrs Jim Shu
2025-08-09 12:34 ` Daniel Henrique Barboza
2025-10-08 5:54 ` Jim Shu
2025-10-12 17:27 ` Daniel Henrique Barboza
2025-10-16 3:33 ` Jim Shu
2025-04-17 10:52 ` [PATCH v2 05/18] hw/misc: riscv_worldguard: Add RISC-V WorldGuard global config Jim Shu
2025-08-09 12:37 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 06/18] target/riscv: Add CPU options of WorldGuard CPU extension Jim Shu
2025-08-09 12:47 ` Daniel Henrique Barboza
2025-10-08 7:22 ` Jim Shu
2025-04-17 10:52 ` [PATCH v2 07/18] target/riscv: Add hard-coded CPU state of WG extension Jim Shu
2025-04-17 10:52 ` [PATCH v2 08/18] target/riscv: Add defines for WorldGuard CSRs Jim Shu
2025-08-09 12:49 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 09/18] target/riscv: Allow global WG config to set WG CPU callbacks Jim Shu
2025-08-09 12:50 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 10/18] target/riscv: Implement WorldGuard CSRs Jim Shu
2025-08-09 12:54 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 11/18] target/riscv: Add WID to MemTxAttrs of CPU memory transactions Jim Shu
2025-04-17 10:52 ` [PATCH v2 12/18] target/riscv: Expose CPU options of WorldGuard Jim Shu
2025-08-09 12:55 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 13/18] hw/misc: riscv_worldguard: Add API to enable WG extension of CPU Jim Shu
2025-08-09 13:07 ` Daniel Henrique Barboza
2025-08-13 3:07 ` Chao Liu
2025-10-08 6:19 ` Jim Shu [this message]
2025-04-17 10:52 ` [PATCH v2 14/18] hw/misc: riscv_wgchecker: Implement RISC-V WorldGuard Checker Jim Shu
2025-08-09 16:39 ` Daniel Henrique Barboza
2025-10-08 7:23 ` Jim Shu
2025-04-17 10:52 ` [PATCH v2 15/18] hw/misc: riscv_wgchecker: Implement wgchecker slot registers Jim Shu
2025-08-09 16:42 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 16/18] hw/misc: riscv_wgchecker: Implement correct block-access behavior Jim Shu
2025-04-17 10:52 ` [PATCH v2 17/18] hw/misc: riscv_wgchecker: Check the slot settings in translate Jim Shu
2025-08-09 16:43 ` Daniel Henrique Barboza
2025-04-17 10:52 ` [PATCH v2 18/18] hw/riscv: virt: Add WorldGuard support Jim Shu
2025-08-09 16:53 ` Daniel Henrique Barboza
2025-10-08 7:23 ` Jim Shu
2025-08-09 17:00 ` [PATCH v2 00/18] Implements RISC-V WorldGuard extension v0.4 Daniel Henrique Barboza
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CALw707r3tBUGbZqYXbPc2B+so6AVhYqUr7ccYYjVidDqqAWemw@mail.gmail.com \
--to=jim.shu@sifive.com \
--cc=alistair.francis@wdc.com \
--cc=arikalo@gmail.com \
--cc=atar4qemu@gmail.com \
--cc=aurelien@aurel32.net \
--cc=chao.liu@yeah.net \
--cc=david@redhat.com \
--cc=dbarboza@ventanamicro.com \
--cc=deller@gmx.de \
--cc=edgar.iglesias@gmail.com \
--cc=eduardo@habkost.net \
--cc=gaosong@loongson.cn \
--cc=iii@linux.ibm.com \
--cc=jcmvbkbc@gmail.com \
--cc=jiaxun.yang@flygoat.com \
--cc=kbastian@mail.uni-paderborn.de \
--cc=laurent@vivier.eu \
--cc=liwei1518@gmail.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mark.cave-ayland@ilande.co.uk \
--cc=mrolnik@gmail.com \
--cc=npiggin@gmail.com \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=qemu-riscv@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=shorne@gmail.com \
--cc=thuth@redhat.com \
--cc=wangyanan55@huawei.com \
--cc=zhao1.liu@intel.com \
--cc=zhiwei_liu@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).