Skip to contentSkip to conversation

RISC-V: p2m context switch

PATCH Superseded Oleksii Kurochko 2 participants 15 messages Feb 2 – Feb 9 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 xen/riscv: introduce struct arch_vcpu

In reply to Oleksii Kurochko

Introdce struct arch_vcpu to hold RISC-V vCPU-specific state.

The structure contains:
- Guest-visible CSR state, used to save and restore vCPU execution state across context switches. hstatus isn't added here as it is already part of cpu_user_regs struct.
- Callee-saved registers used to preserve Xen’s own execution context when switching between vCPU stacks.

It is going to be used in the following way (pseudocode):
context_switch(prev_vcpu, next_vcpu):

    ...

    /* Switch from previous stack to the next stack. */
    __context_switch(prev_vcpu, next_vcpu);

    ...
    schedule_tail(prev_vcpu):
       Save and restore vCPU's CSRs.

The Xen-saved context allows __context_switch() to switch execution from the previous vCPU’s stack to the next vCPU’s stack and later resume execution on the original stack when switching back.

During vCPU creation, the Xen-saved context is going to be initialized with:
- SP pointing to the newly allocated vCPU stack
- RA pointing to a helper that performs final vCPU setup before transferring control to the guest
After the first execution of __context_switch(), RA naturally points to the instruction following the call site, and the remaining callee-saved registers contain the Xen register state at the time of the switch.

---
Changes in V3:
- Drop gp from struct {...} xen_saved_context as it ought to be stable accross Xen so there is no need to be saved/restored and, also, it shouldn't be preserved across calls according to RISC-V ABI.
- Update the comment above struct {...} xen_saved_context to make it more clear.
- Drop CSRs and VCSRS comments in arch_vcpu as it is clear what kind of CSR it is based on the name.
- Drop __cacheline_aligned for struct arch_vcpu as proper measurements can't be made now so it is hard to prove that the attribute really boost performance.
---
Changes in v2:
- Drop hstatus from struct arch_vcpu as it is stored in struct cpu_user_regs which will be stored on top of vCPU's stack.
- Drop the comment above ra in xen_saved_context struct as it is potentially misleading.

---
 xen/arch/riscv/include/asm/domain.h | 49 +++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
xen/arch/riscv/include/asm/domain.h +49 -0
index 316e7c6c8448..cca39effc1ba 100644
@@ -24,6 +24,55 @@ struct arch_vcpu_io {
 
 struct arch_vcpu {
     struct vcpu_vmid vmid;
+
+    /*
+     * Callee saved registers for Xen's state used to switch from
+     * prev's stack to the next's stack during context switch.
+     */
+    struct
+    {
+        register_t s0;
+        register_t s1;
+        register_t s2;
+        register_t s3;
+        register_t s4;
+        register_t s5;
+        register_t s6;
+        register_t s7;
+        register_t s8;
+        register_t s9;
+        register_t s10;
+        register_t s11;
+        register_t sp;
+        register_t ra;
+    } xen_saved_context;
+
+    register_t hedeleg;
+    register_t hideleg;
+    register_t hvip;
+    register_t hip;
+    register_t hie;
+    register_t hgeie;
+    register_t henvcfg;
+    register_t hcounteren;
+    register_t htimedelta;
+    register_t htval;
+    register_t htinst;
+    register_t hstateen0;
+#ifdef CONFIG_RISCV_32
+    register_t henvcfgh;
+    register_t htimedeltah;
+#endif
+
+    register_t vsstatus;
+    register_t vsip;
+    register_t vsie;
+    register_t vstvec;
+    register_t vsscratch;
+    register_t vscause;
+    register_t vstval;
+    register_t vsatp;
+    register_t vsepc;
 };
 
 struct paging_domain {
Signature · 1 line
2.52.0
#0 lore raw

cover RISC-V: p2m context switch

Introduce functions required to perform a p2m context switch during a vCPU context switch.

This patch series originally depended on a single patch (Patch 1) from [1]. To avoid introducing a dependency on the larger patch series [1], that patch was cherry-picked into the current series, since it is the only true dependency.

Patch 2 is fully independent.

Patch 3 depends on Patch 1.

[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t

Oleksii Kurochko (3):
xen/riscv: introduce struct arch_vcpu
xen/riscv: add support for local guest TLB flush using HFENCE.VVMA xen/riscv: implement p2m_ctx_switch_{to,from}_state()

 xen/arch/riscv/include/asm/domain.h    | 49 ++++++++++++++++
 xen/arch/riscv/include/asm/flushtlb.h  |  7 +++
 xen/arch/riscv/include/asm/insn-defs.h | 10 ++++
 xen/arch/riscv/include/asm/p2m.h       |  4 ++
 xen/arch/riscv/p2m.c                   | 81 ++++++++++++++++++++++++++
 5 files changed, 151 insertions(+)
 create mode 100644 xen/arch/riscv/include/asm/insn-defs.h
Signature · 1 line
2.52.0
#4 lore raw

PATCH 2/3 xen/riscv: add support for local guest TLB flush using HFENCE.VVMA

Introduce flush_tlb_guest_local() to perform a local TLB flush of the guest's address space for the current hart. This leverages the RISC-V HFENCE.VVMA instruction, which is used to invalidate translations in the VS-stage of address translation.

As for RISC-V binutils >= 2.39 is choosen, we can use hfence.vvma mnemonics instead of defining hfence.vvma using .insn.

Although it would be possible to use sbi_remote_hfence_vvma() for this purpose, it is unnecessary in this context since the flush is required only on the local hart. Using the SBI call would introduce additional overhead without benefit, resulting in unnecessary performance loss.

---
 xen/arch/riscv/include/asm/flushtlb.h  |  7 +++++++
 xen/arch/riscv/include/asm/insn-defs.h | 10 ++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 xen/arch/riscv/include/asm/insn-defs.h
xen/arch/riscv/include/asm/flushtlb.h +7 -0
index 4f64f9757058..b0112d416dbe 100644
@@ -5,6 +5,7 @@
 #include <xen/bug.h>
 #include <xen/cpumask.h>
 
+#include <asm/insn-defs.h>
 #include <asm/sbi.h>
 
 struct page_info;
@@ -14,6 +15,12 @@ static inline void local_hfence_gvma_all(void)
     asm volatile ( "hfence.gvma zero, zero" ::: "memory" );
 }
 
+/* Flush VS-stage TLB for current hart. */
+static inline void flush_tlb_guest_local(void)
+{
+    HFENCE_VVMA(0, 0);
+}
+
 /* Flush TLB of local processor for address va. */
 static inline void flush_tlb_one_local(vaddr_t va)
 {
xen/arch/riscv/include/asm/insn-defs.h new file +10 -0
new file mode 100644
index 000000000000..4d50b5e23c11
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ASM_RISCV_INSN_DEFS_H
+#define ASM_RISCV_INSN_DEFS_H
+
+#define HFENCE_VVMA(vaddr, asid) \
+    asm volatile ("hfence.vvma %0, %1" \
+                  :: "r"(vaddr), "r"(asid) : "memory")
+
+#endif /* ASM_RISCV_INSN_DEFS_H */
Signature · 1 line
2.52.0
#9 lore raw

PATCH 3/3 xen/riscv: implement p2m_ctx_switch_{to,from}_state()

In reply to Oleksii Kurochko

Introduce functions required to perform a p2m context switch during a vCPU context switch.

As no mechanism is provided to atomically change vsatp and hgatp together. Hence, to prevent speculative execution causing one guest’s VS-stage translations to be cached under another guest’s VMID, world-switch code should zero vsatp in p2m_ctx_swith_from(), then construct new hgatp and write the new vsatp value in p2m_ctx_switch_to().

---
 xen/arch/riscv/include/asm/p2m.h |  4 ++
 xen/arch/riscv/p2m.c             | 81 ++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
xen/arch/riscv/include/asm/p2m.h +4 -0
index f63b5dec99b1..1d8c97326565 100644
@@ -255,6 +255,10 @@ static inline bool p2m_is_locked(const struct p2m_domain *p2m)
 struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn,
                                         p2m_type_t *t);
 
+
+void p2m_ctx_switch_from(struct vcpu *p);
+void p2m_ctx_switch_to(struct vcpu *n);
+
 #endif /* ASM__RISCV__P2M_H */
 
 /*
xen/arch/riscv/p2m.c +81 -0

84 lines not on this page · raw message

Signature · 1 line
2.52.0
#14 lore raw
In reply to Oleksii Kurochko
Quoted 7 lines · Oleksii Kurochko

Introduce functions required to perform a p2m context switch during a vCPU context switch.

This patch series originally depended on a single patch (Patch 1) from [1]. To avoid introducing a dependency on the larger patch series [1], that patch was cherry-picked into the current series, since it is the only true dependency.

But its versioning is now broken. A v2 of that was already posted, and now we have a (newer) v1, with a revision log claiming it's v3.

Jan

#2 lore raw

Re: [PATCH v1 1/3] xen/riscv: introduce struct arch_vcpu

In reply to Oleksii Kurochko

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 26

Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/domain.h +0 -0
@@ -24,6 +24,55 @@ struct arch_vcpu_io {
 
 struct arch_vcpu {
     struct vcpu_vmid vmid;

Nit: This being already here contradicts the title saying "introduce".

Quoted 29 lines
xen/arch/riscv/include/asm/domain.h +29 -0
+
+    /*
+     * Callee saved registers for Xen's state used to switch from
+     * prev's stack to the next's stack during context switch.
+     */
+    struct
+    {
+        register_t s0;
+        register_t s1;
+        register_t s2;
+        register_t s3;
+        register_t s4;
+        register_t s5;
+        register_t s6;
+        register_t s7;
+        register_t s8;
+        register_t s9;
+        register_t s10;
+        register_t s11;
+        register_t sp;
+        register_t ra;
+    } xen_saved_context;
+
+    register_t hedeleg;
+    register_t hideleg;
+    register_t hvip;
+    register_t hip;
+    register_t hie;
+    register_t hgeie;

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 65

Quoted 10 lines
xen/arch/riscv/include/asm/domain.h +10 -0
+    register_t henvcfg;
+    register_t hcounteren;
+    register_t htimedelta;
+    register_t htval;
+    register_t htinst;
+    register_t hstateen0;
+#ifdef CONFIG_RISCV_32
+    register_t henvcfgh;
+    register_t htimedeltah;
+#endif

When you add these right away, any reason other high-half registers aren't added as well? HVIP (above) and VSIE / VSIP at the very least have such counterparts, iirc.

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 76

Quoted 10 lines
xen/arch/riscv/include/asm/domain.h +9 -0
+    register_t vsstatus;
+    register_t vsip;
+    register_t vsie;
+    register_t vstvec;
+    register_t vsscratch;
+    register_t vscause;
+    register_t vstval;
+    register_t vsatp;
+    register_t vsepc;
 };

What I'm generally concerned of: With the registers all being put here in one go, without any of them actually being used in this patch, it'll likely be harder to track that they're saved / restored / initialized correctly (once uses appear).

Jan

#3 lore raw

Re: [PATCH v1 1/3] xen/riscv: introduce struct arch_vcpu

Quoted 8 lines · Jan Beulich

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 26

Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/domain.h +0 -0
@@ -24,6 +24,55 @@ struct arch_vcpu_io {
 
  struct arch_vcpu {
      struct vcpu_vmid vmid;

Nit: This being already here contradicts the title saying "introduce".

I'll update then the commit message.

Quoted 35 lines
Quoted 35 lines
xen/arch/riscv/include/asm/domain.h +34 -0
+
+    /*
+     * Callee saved registers for Xen's state used to switch from
+     * prev's stack to the next's stack during context switch.
+     */
+    struct
+    {
+        register_t s0;
+        register_t s1;
+        register_t s2;
+        register_t s3;
+        register_t s4;
+        register_t s5;
+        register_t s6;
+        register_t s7;
+        register_t s8;
+        register_t s9;
+        register_t s10;
+        register_t s11;
+        register_t sp;
+        register_t ra;
+    } xen_saved_context;
+
+    register_t hedeleg;
+    register_t hideleg;
+    register_t hvip;
+    register_t hip;
+    register_t hie;
+    register_t hgeie;
+    register_t henvcfg;
+    register_t hcounteren;
+    register_t htimedelta;
+    register_t htval;
+    register_t htinst;
Quoted 8 lines

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 65

Quoted 5 lines
xen/arch/riscv/include/asm/domain.h +5 -0
+    register_t hstateen0;
+#ifdef CONFIG_RISCV_32
+    register_t henvcfgh;
+    register_t htimedeltah;
+#endif

When you add these right away, any reason other high-half registers aren't added as well? HVIP (above) and VSIE / VSIP at the very least have such counterparts, iirc.

No specific reason. It was added early in the RISC-V port when I was considering supporting RV32, but as you know I later decided to focus only on RV64, at least for now. Therefore, it seems better to drop this #ifdef from the arch_vcpu structure and re-introduce when RV32 support will be added.

Quoted 11 lines

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 76

Quoted 11 lines
xen/arch/riscv/include/asm/domain.h +9 -0
+    register_t vsstatus;
+    register_t vsip;
+    register_t vsie;
+    register_t vstvec;
+    register_t vsscratch;
+    register_t vscause;
+    register_t vstval;
+    register_t vsatp;
+    register_t vsepc;
  };
Quoted 3 lines

What I'm generally concerned of: With the registers all being put here in one go, without any of them actually being used in this patch, it'll likely be harder to track that they're saved / restored / initialized correctly (once uses appear).

Then if it will really better I'll introduce in this patch series only vsatp register as it is used in the further patches. (or even will just introduce vsatp in Patch 3)

I will keep the patch [1] separately in the patch series with introduction of vtimer things and drop only an introduction of vsatp there.

[1] https://lore.kernel.org/xen-devel/[email protected]/T/#ma33af3ff4b2b033f86aa964902ebcab8705fd114

~ Oleksii

#5 lore raw

Re: [PATCH v1 2/3] xen/riscv: add support for local guest TLB flush using HFENCE.VVMA

In reply to Oleksii Kurochko

xen/arch/riscv/include/asm/flushtlb.h static inline void local_hfence_gvma_all(void) · line 21

Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/flushtlb.h +4 -0
@@ -14,6 +15,12 @@ static inline void local_hfence_gvma_all(void)
     asm volatile ( "hfence.gvma zero, zero" ::: "memory" );
 }
 
+/* Flush VS-stage TLB for current hart. */
+static inline void flush_tlb_guest_local(void)
+{
+    HFENCE_VVMA(0, 0);

For this use, ...

xen/arch/riscv/include/asm/insn-defs.h line 8

Quoted 11 lines
xen/arch/riscv/include/asm/insn-defs.h new file +8 -0
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ASM_RISCV_INSN_DEFS_H
+#define ASM_RISCV_INSN_DEFS_H
+
+#define HFENCE_VVMA(vaddr, asid) \
+    asm volatile ("hfence.vvma %0, %1" \
+                  :: "r"(vaddr), "r"(asid) : "memory")

... don't you want to use "rJ" as the constraints here?

It's further not quite clear to me whether the memory clobber is really needed. Pretty certainly not for the use in context switch, but perhaps for other future uses it's better to be there, even if only to be on the safe side.

Also, style nit: There are a number of blanks missing in this asm().

Jan

#10 lore raw

Re: [PATCH v1 3/3] xen/riscv: implement p2m_ctx_switch_{to,from}_state()

In reply to Oleksii Kurochko
Quoted 5 lines · Oleksii Kurochko

Introduce functions required to perform a p2m context switch during a vCPU context switch.

As no mechanism is provided to atomically change vsatp and hgatp together.

This sentence doesn't parse; imo you simply want to drop "As".

Quoted 15 lines

Hence, to prevent speculative execution causing one guest’s VS-stage translations to be cached under another guest’s VMID, world-switch code should zero vsatp in p2m_ctx_swith_from(), then construct new hgatp and write the new vsatp value in p2m_ctx_switch_to().

---
 xen/arch/riscv/include/asm/p2m.h |  4 ++
 xen/arch/riscv/p2m.c             | 81 ++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

xen/arch/riscv/include/asm/p2m.h static inline bool p2m_is_locked(const struct p2m_domain *p2m) · line 258

Quoted 4 lines
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -255,6 +255,10 @@ static inline bool p2m_is_locked(const struct p2m_domain *p2m)
 struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn,
                                         p2m_type_t *t);
 
+

As before: No double blank lines please.

xen/arch/riscv/include/asm/p2m.h static inline bool p2m_is_locked(const struct p2m_domain *p2m) · line 260

Quoted 2 lines
xen/arch/riscv/include/asm/p2m.h +2 -0
+void p2m_ctx_switch_from(struct vcpu *p);
+void p2m_ctx_switch_to(struct vcpu *n);

Like for the other functions, please consider s/ctx/ctxt/.

Quoted 8 lines
xen/arch/riscv/p2m.c +2 -0
@@ -1434,3 +1434,84 @@ struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn,
 
     return get_page(page, p2m->domain) ? page : NULL;
 }
+
+void p2m_ctx_switch_from(struct vcpu *p)

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1448

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+{
+    /*
+     * No mechanism is provided to atomically change vsatp and hgatp
+     * together. Hence, to prevent speculative execution causing one
+     * guest’s VS-stage translations to be cached under another guest’s
+     * VMID, world-switch code should zero vsatp, then swap hgatp, then
+     * finally write the new vsatp value.
+     */
+    p->arch.vsatp = csr_read(CSR_VSATP);
+    csr_write(CSR_VSATP, 0);

Why two CSR accesses when one will do? RISC-V specifically has the CSRRW insn.

Quoted 13 lines
xen/arch/riscv/p2m.c +13 -0
+    /*
+     * No need for VS-stage TLB flush here:
+     *  Changing satp.MODE from Bare to other modes and vice versa also
+     *  takes effect immediately, without the need to execute an
+     *  SFENCE.VMA instruction.
+     * Note that VSATP is just VS-mode’s version of SATP, so the mentioned
+     * above should be true for VSATP.
+     */
+
+    /*
+     * Nothing to do with HGATP as it is constructed each time when
+     * p2m_ctx_switch_to() is called.
+     */

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1472

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+}
+
+void p2m_ctx_switch_to(struct vcpu *n)
+{
+    struct vcpu_vmid *p_vmid = &n->arch.vmid;
+    uint16_t old_vmid, new_vmid;
+    bool need_flush;
+
+    if ( is_idle_vcpu(n) )
+        return;

Shouldn't the other function have such a check, too?

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1475

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+    old_vmid = p_vmid->vmid;
+    need_flush = vmid_handle_vmenter(p_vmid);

As you're re-using x86'es model, I'm not quite sure why this would be needed. I don't think we have such in x86; aiui this should be handled while actually doing the VM entry.

Quoted 18 lines
xen/arch/riscv/p2m.c +18 -0
+    new_vmid = p_vmid->vmid;
+
+#ifdef P2M_DEBUG
+    printk(XENLOG_INFO, "%pv: oldvmid(%d) new_vmid(%d), need_flush(%d)\n",
+           n, old_vmid, new_vmid, need_flush);
+#endif
+
+    csr_write(CSR_HGATP, construct_hgatp(p2m_get_hostp2m(current->domain),
+              new_vmid));
+
+    if ( unlikely(need_flush) )
+        local_hfence_gvma_all();
+
+    /*
+     * According to the RISC-V specification, speculation can happen
+     * during an update of hgatp and vsatp:
+     *   No mechanism is provided to atomically change vsatp and hgatp
+     *   together. Hence, to prevent speculative execution causing one

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1503

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+     *   guest’s VS-stage translations to be cached under another guest’s
+     *   VMID, world-switch code should zero vsatp, then swap hgatp, then
+     *   finally write the new vsatp value. Similarly, if henvcfg.PBMTE
+     *   need be world-switched, it should be switched after zeroing vsatp
+     *   but before writing the new vsatp value, obviating the need to
+     *   execute an HFENCE.VVMA instruction.
+     * So just flush TLBs for VS-Stage and G-stage after both of regs are
+     * touched.
+     */
+    flush_tlb_guest_local();

Which "both regs" does the comment talk about? Doesn't the flush want to come
...

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1516

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+    /*
+     * The vsatp register is a VSXLEN-bit read/write register that is
+     * VS-mode’s version of supervisor register satp, so the following is
+     * true for VSATP registers:
+     *  Changing satp.MODE from Bare to other modes and vice versa also takes
+     *  effect immediately, without the need to execute an SFENCE.VMA
+     *  instruction. Likewise, changes to satp.ASID take effect immediately.
+     * Considering the mentioned above and that VS-stage TLB flush has been
+     * already done there is no need to flush VS-stage TLB after an update
+     * of VSATP from Bare mode to what is written in `n->arch.vsatp`.
+     */
+    csr_write(CSR_VSATP, n->arch.vsatp);

... after this? Then some of the commentary also doesn't look to be necessary.

Jan

#6 lore raw

Re: [PATCH v1 2/3] xen/riscv: add support for local guest TLB flush using HFENCE.VVMA

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

xen/arch/riscv/include/asm/flushtlb.h static inline void local_hfence_gvma_all(void) · line 21

Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/flushtlb.h +4 -0
@@ -14,6 +15,12 @@ static inline void local_hfence_gvma_all(void)
      asm volatile ( "hfence.gvma zero, zero" ::: "memory" );
  }
 
+/* Flush VS-stage TLB for current hart. */
+static inline void flush_tlb_guest_local(void)
+{
+    HFENCE_VVMA(0, 0);

For this use, ...

Quoted 12 lines

xen/arch/riscv/include/asm/insn-defs.h line 8

Quoted 11 lines
xen/arch/riscv/include/asm/insn-defs.h new file +8 -0
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ASM_RISCV_INSN_DEFS_H
+#define ASM_RISCV_INSN_DEFS_H
+
+#define HFENCE_VVMA(vaddr, asid) \
+    asm volatile ("hfence.vvma %0, %1" \
+                  :: "r"(vaddr), "r"(asid) : "memory")

... don't you want to use "rJ" as the constraints here?

Even without "rJ" it is using x0 when argument 0 is passed. Just to be on a safe side I don't mind to add "J".

~ Oleksii

#11 lore raw

Re: [PATCH v1 3/3] xen/riscv: implement p2m_ctx_switch_{to,from}_state()

In reply to Jan Beulich
Quoted 24 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko

Introduce functions required to perform a p2m context switch during a vCPU context switch.

As no mechanism is provided to atomically change vsatp and hgatp together.

This sentence doesn't parse; imo you simply want to drop "As".

Quoted 16 lines

Hence, to prevent speculative execution causing one guest’s VS-stage translations to be cached under another guest’s VMID, world-switch code should zero vsatp in p2m_ctx_swith_from(), then construct new hgatp and write the new vsatp value in p2m_ctx_switch_to().

---
  xen/arch/riscv/include/asm/p2m.h |  4 ++
  xen/arch/riscv/p2m.c             | 81 ++++++++++++++++++++++++++++++++
  2 files changed, 85 insertions(+)
xen/arch/riscv/p2m.c +0 -0
@@ -1434,3 +1434,84 @@ struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn,
 
Quoted 15 lines
Quoted 4 lines
xen/arch/riscv/p2m.c +2 -0
      return get_page(page, p2m->domain) ? page : NULL;
  }
+
+void p2m_ctx_switch_from(struct vcpu *p)

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1448

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+{
+    /*
+     * No mechanism is provided to atomically change vsatp and hgatp
+     * together. Hence, to prevent speculative execution causing one
+     * guest’s VS-stage translations to be cached under another guest’s
+     * VMID, world-switch code should zero vsatp, then swap hgatp, then
+     * finally write the new vsatp value.
+     */
+    p->arch.vsatp = csr_read(CSR_VSATP);
+    csr_write(CSR_VSATP, 0);

Why two CSR accesses when one will do? RISC-V specifically has the CSRRW insn.

csr_swap() could really be used here instead, thanks.

Quoted 17 lines
Quoted 17 lines
xen/arch/riscv/p2m.c +16 -0
+    /*
+     * No need for VS-stage TLB flush here:
+     *  Changing satp.MODE from Bare to other modes and vice versa also
+     *  takes effect immediately, without the need to execute an
+     *  SFENCE.VMA instruction.
+     * Note that VSATP is just VS-mode’s version of SATP, so the mentioned
+     * above should be true for VSATP.
+     */
+
+    /*
+     * Nothing to do with HGATP as it is constructed each time when
+     * p2m_ctx_switch_to() is called.
+     */
+}
+
+void p2m_ctx_switch_to(struct vcpu *n)
Quoted 8 lines

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1472

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+{
+    struct vcpu_vmid *p_vmid = &n->arch.vmid;
+    uint16_t old_vmid, new_vmid;
+    bool need_flush;
+
+    if ( is_idle_vcpu(n) )
+        return;

Shouldn't the other function have such a check, too?

Yes, it should. I will add the check.

Quoted 6 lines

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1475

Quoted 3 lines
xen/arch/riscv/p2m.c +2 -0
+    old_vmid = p_vmid->vmid;
+    need_flush = vmid_handle_vmenter(p_vmid);

As you're re-using x86'es model, I'm not quite sure why this would be needed. I don't think we have such in x86; aiui this should be handled while actually doing the VM entry.

I can move this to the end of do_trap().

However, isn’t this effectively the same?
If a context switch to vCPU n happens, it means that once the trap is fully handled, we will enter vCPU n.
The same will be if to put the changes at the end of do_trap() before control will be given to VM.

Quoted 44 lines
Quoted 19 lines
xen/arch/riscv/p2m.c +18 -0
+    new_vmid = p_vmid->vmid;
+
+#ifdef P2M_DEBUG
+    printk(XENLOG_INFO, "%pv: oldvmid(%d) new_vmid(%d), need_flush(%d)\n",
+           n, old_vmid, new_vmid, need_flush);
+#endif
+
+    csr_write(CSR_HGATP, construct_hgatp(p2m_get_hostp2m(current->domain),
+              new_vmid));
+
+    if ( unlikely(need_flush) )
+        local_hfence_gvma_all();
+
+    /*
+     * According to the RISC-V specification, speculation can happen
+     * during an update of hgatp and vsatp:
+     *   No mechanism is provided to atomically change vsatp and hgatp
+     *   together. Hence, to prevent speculative execution causing one

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1503

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+     *   guest’s VS-stage translations to be cached under another guest’s
+     *   VMID, world-switch code should zero vsatp, then swap hgatp, then
+     *   finally write the new vsatp value. Similarly, if henvcfg.PBMTE
+     *   need be world-switched, it should be switched after zeroing vsatp
+     *   but before writing the new vsatp value, obviating the need to
+     *   execute an HFENCE.VVMA instruction.
+     * So just flush TLBs for VS-Stage and G-stage after both of regs are
+     * touched.
+     */
+    flush_tlb_guest_local();

Which "both regs" does the comment talk about? Doesn't the flush want to come
...

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1516

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+    /*
+     * The vsatp register is a VSXLEN-bit read/write register that is
+     * VS-mode’s version of supervisor register satp, so the following is
+     * true for VSATP registers:
+     *  Changing satp.MODE from Bare to other modes and vice versa also takes
+     *  effect immediately, without the need to execute an SFENCE.VMA
+     *  instruction. Likewise, changes to satp.ASID take effect immediately.
+     * Considering the mentioned above and that VS-stage TLB flush has been
+     * already done there is no need to flush VS-stage TLB after an update
+     * of VSATP from Bare mode to what is written in `n->arch.vsatp`.
+     */
+    csr_write(CSR_VSATP, n->arch.vsatp);
Quoted 1 line

... after this? Then some of the commentary also doesn't look to be necessary.

I think there is no need for a guest TLB flush here or above
(i.e. flush_tlb_guest_local()), because:
- If CSR_VSATP is set to 0, or changed from 0 to a non-zero value, no speculation can occur. Therefore, the guest TLB cannot be polluted with unwanted entries, and no flush is required.
- If need_flush is false, it means that no VMID wraparound has occurred. In that case, the VMID is still unique, so we are safe and no guest TLB flush is needed.

Additionally, it seems that I do not need to introduce flush_tlb_guest_local() at all, since local_hfence_gvma_all() already provides the same functionality. Instead, it probably makes sense to introduce an HFENCE_VVMA() helper as a generic wrapper around the hfence.vvma instruction, and then reuse it inside local_hfence_gvma_all().

~ Oleksii

#12 lore raw

Re: [PATCH v1 3/3] xen/riscv: implement p2m_ctx_switch_{to,from}_state()

Quoted 20 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1472

Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +8 -0
+void p2m_ctx_switch_to(struct vcpu *n)
+{
+    struct vcpu_vmid *p_vmid = &n->arch.vmid;
+    uint16_t old_vmid, new_vmid;
+    bool need_flush;
+
+    if ( is_idle_vcpu(n) )
+        return;

Shouldn't the other function have such a check, too?

Yes, it should. I will add the check.

Quoted 5 lines

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1475

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+    old_vmid = p_vmid->vmid;
+    need_flush = vmid_handle_vmenter(p_vmid);

As you're re-using x86'es model, I'm not quite sure why this would be needed. I don't think we have such in x86; aiui this should be handled while actually doing the VM entry.

Quoted 7 lines

I can move this to the end of do_trap().

However, isn’t this effectively the same?
If a context switch to vCPU n happens, it means that once the trap is fully handled, we will enter vCPU n.
The same will be if to put the changes at the end of do_trap() before control will be given to VM.

Thing is - you will need it at the end of do_trap() (or wherever VM entry is going to be dealt with) anyway, to cover situation other than context switch as well.

Quoted 54 lines
Quoted 43 lines
Quoted 18 lines
xen/arch/riscv/p2m.c +18 -0
+    new_vmid = p_vmid->vmid;
+
+#ifdef P2M_DEBUG
+    printk(XENLOG_INFO, "%pv: oldvmid(%d) new_vmid(%d), need_flush(%d)\n",
+           n, old_vmid, new_vmid, need_flush);
+#endif
+
+    csr_write(CSR_HGATP, construct_hgatp(p2m_get_hostp2m(current->domain),
+              new_vmid));
+
+    if ( unlikely(need_flush) )
+        local_hfence_gvma_all();
+
+    /*
+     * According to the RISC-V specification, speculation can happen
+     * during an update of hgatp and vsatp:
+     *   No mechanism is provided to atomically change vsatp and hgatp
+     *   together. Hence, to prevent speculative execution causing one

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1503

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+     *   guest’s VS-stage translations to be cached under another guest’s
+     *   VMID, world-switch code should zero vsatp, then swap hgatp, then
+     *   finally write the new vsatp value. Similarly, if henvcfg.PBMTE
+     *   need be world-switched, it should be switched after zeroing vsatp
+     *   but before writing the new vsatp value, obviating the need to
+     *   execute an HFENCE.VVMA instruction.
+     * So just flush TLBs for VS-Stage and G-stage after both of regs are
+     * touched.
+     */
+    flush_tlb_guest_local();

Which "both regs" does the comment talk about? Doesn't the flush want to come
...

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1516

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+    /*
+     * The vsatp register is a VSXLEN-bit read/write register that is
+     * VS-mode’s version of supervisor register satp, so the following is
+     * true for VSATP registers:
+     *  Changing satp.MODE from Bare to other modes and vice versa also takes
+     *  effect immediately, without the need to execute an SFENCE.VMA
+     *  instruction. Likewise, changes to satp.ASID take effect immediately.
+     * Considering the mentioned above and that VS-stage TLB flush has been
+     * already done there is no need to flush VS-stage TLB after an update
+     * of VSATP from Bare mode to what is written in `n->arch.vsatp`.
+     */
+    csr_write(CSR_VSATP, n->arch.vsatp);
Quoted 1 line

... after this? Then some of the commentary also doesn't look to be necessary.

I think there is no need for a guest TLB flush here or above
(i.e. flush_tlb_guest_local()), because:
- If CSR_VSATP is set to 0, or changed from 0 to a non-zero value, no speculation can occur. Therefore, the guest TLB cannot be polluted with unwanted entries, and no flush is required.
- If need_flush is false, it means that no VMID wraparound has occurred. In that case, the VMID is still unique, so we are safe and no guest TLB flush is needed.

Quoted 5 lines

Additionally, it seems that I do not need to introduce flush_tlb_guest_local() at all, since local_hfence_gvma_all() already provides the same functionality. Instead, it probably makes sense to introduce an HFENCE_VVMA() helper as a generic wrapper around the hfence.vvma instruction, and then reuse it inside local_hfence_gvma_all().

Hmm, I fear I'm confused: hfence.gvma != hfence.vvma ?

Jan

#13 lore raw

Re: [PATCH v1 3/3] xen/riscv: implement p2m_ctx_switch_{to,from}_state()

Quoted 21 lines · Jan Beulich
Quoted 20 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1472

Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +8 -0
+void p2m_ctx_switch_to(struct vcpu *n)
+{
+    struct vcpu_vmid *p_vmid = &n->arch.vmid;
+    uint16_t old_vmid, new_vmid;
+    bool need_flush;
+
+    if ( is_idle_vcpu(n) )
+        return;

Shouldn't the other function have such a check, too?

Yes, it should. I will add the check.

Quoted 5 lines

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1475

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+    old_vmid = p_vmid->vmid;
+    need_flush = vmid_handle_vmenter(p_vmid);

As you're re-using x86'es model, I'm not quite sure why this would be needed. I don't think we have such in x86; aiui this should be handled while actually doing the VM entry.

I can move this to the end of do_trap().

Quoted 8 lines
Quoted 5 lines

However, isn’t this effectively the same?
If a context switch to vCPU n happens, it means that once the trap is fully handled, we will enter vCPU n.
The same will be if to put the changes at the end of do_trap() before control will be given to VM.

Thing is - you will need it at the end of do_trap() (or wherever VM entry is going to be dealt with) anyway, to cover situation other than context switch as well.

If there are such situation then it makes sense.

Quoted 54 lines
Quoted 54 lines
Quoted 44 lines
Quoted 19 lines
xen/arch/riscv/p2m.c +18 -0
+    new_vmid = p_vmid->vmid;
+
+#ifdef P2M_DEBUG
+    printk(XENLOG_INFO, "%pv: oldvmid(%d) new_vmid(%d), need_flush(%d)\n",
+           n, old_vmid, new_vmid, need_flush);
+#endif
+
+    csr_write(CSR_HGATP, construct_hgatp(p2m_get_hostp2m(current->domain),
+              new_vmid));
+
+    if ( unlikely(need_flush) )
+        local_hfence_gvma_all();
+
+    /*
+     * According to the RISC-V specification, speculation can happen
+     * during an update of hgatp and vsatp:
+     *   No mechanism is provided to atomically change vsatp and hgatp
+     *   together. Hence, to prevent speculative execution causing one

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1503

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+     *   guest’s VS-stage translations to be cached under another guest’s
+     *   VMID, world-switch code should zero vsatp, then swap hgatp, then
+     *   finally write the new vsatp value. Similarly, if henvcfg.PBMTE
+     *   need be world-switched, it should be switched after zeroing vsatp
+     *   but before writing the new vsatp value, obviating the need to
+     *   execute an HFENCE.VVMA instruction.
+     * So just flush TLBs for VS-Stage and G-stage after both of regs are
+     * touched.
+     */
+    flush_tlb_guest_local();

Which "both regs" does the comment talk about? Doesn't the flush want to come
...

xen/arch/riscv/p2m.c struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, · line 1516

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+    /*
+     * The vsatp register is a VSXLEN-bit read/write register that is
+     * VS-mode’s version of supervisor register satp, so the following is
+     * true for VSATP registers:
+     *  Changing satp.MODE from Bare to other modes and vice versa also takes
+     *  effect immediately, without the need to execute an SFENCE.VMA
+     *  instruction. Likewise, changes to satp.ASID take effect immediately.
+     * Considering the mentioned above and that VS-stage TLB flush has been
+     * already done there is no need to flush VS-stage TLB after an update
+     * of VSATP from Bare mode to what is written in `n->arch.vsatp`.
+     */
+    csr_write(CSR_VSATP, n->arch.vsatp);
Quoted 1 line

... after this? Then some of the commentary also doesn't look to be necessary.

I think there is no need for a guest TLB flush here or above
(i.e. flush_tlb_guest_local()), because:
- If CSR_VSATP is set to 0, or changed from 0 to a non-zero value, no speculation can occur. Therefore, the guest TLB cannot be polluted with unwanted entries, and no flush is required.
- If need_flush is false, it means that no VMID wraparound has occurred. In that case, the VMID is still unique, so we are safe and no guest TLB flush is needed.

Quoted 6 lines
Quoted 5 lines

Additionally, it seems that I do not need to introduce flush_tlb_guest_local() at all, since local_hfence_gvma_all() already provides the same functionality. Instead, it probably makes sense to introduce an HFENCE_VVMA() helper as a generic wrapper around the hfence.vvma instruction, and then reuse it inside local_hfence_gvma_all().

Hmm, I fear I'm confused: hfence.gvma != hfence.vvma ?

I overlooked that it Gvma in local_hfence_gvma_all(). Then it make sense for having both local_hfence_gvma_all() and flush_tlb_guest_local().
But it is still look like flush_tlb_guest_local() shouldn't be called in p2m context switch, because of what I mentioned in the first paragraph above what allow not to introduce flush_tlb_guest_local() now.

~ Oleksii

#7 lore raw

Re: [PATCH v1 2/3] xen/riscv: add support for local guest TLB flush using HFENCE.VVMA

In reply to Oleksii Kurochko
Quoted 25 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/flushtlb.h static inline void local_hfence_gvma_all(void) · line 21

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/flushtlb.h +0 -0
@@ -14,6 +15,12 @@ static inline void local_hfence_gvma_all(void)
       asm volatile ( "hfence.gvma zero, zero" ::: "memory" );
   }

  +/* Flush VS-stage TLB for current hart. */

xen/arch/riscv/include/asm/flushtlb.h +3 -0
+static inline void flush_tlb_guest_local(void)
+{
+    HFENCE_VVMA(0, 0);

For this use, ...

Quoted 12 lines

xen/arch/riscv/include/asm/insn-defs.h line 8

Quoted 11 lines
xen/arch/riscv/include/asm/insn-defs.h new file +8 -0
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ASM_RISCV_INSN_DEFS_H
+#define ASM_RISCV_INSN_DEFS_H
+
+#define HFENCE_VVMA(vaddr, asid) \
+    asm volatile ("hfence.vvma %0, %1" \
+                  :: "r"(vaddr), "r"(asid) : "memory")

... don't you want to use "rJ" as the constraints here?

Quoted 2 lines

Even without "rJ" it is using x0 when argument 0 is passed. Just to be on a safe side I don't mind to add "J".

When "J" is used compiler is trying to use integer 0 (what I misread when read about J) but hfence.vvma expects two registers (zero register in this case), so "J" can't be really used here.

~ Oleksii

#8 lore raw

Re: [PATCH v1 2/3] xen/riscv: add support for local guest TLB flush using HFENCE.VVMA

Quoted 27 lines · Oleksii Kurochko
Quoted 25 lines · Oleksii Kurochko
Quoted 22 lines · Jan Beulich

xen/arch/riscv/include/asm/flushtlb.h static inline void local_hfence_gvma_all(void) · line 21

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/flushtlb.h +0 -0
@@ -14,6 +15,12 @@ static inline void local_hfence_gvma_all(void)
       asm volatile ( "hfence.gvma zero, zero" ::: "memory" );
   }

  +/* Flush VS-stage TLB for current hart. */

xen/arch/riscv/include/asm/flushtlb.h +3 -0
+static inline void flush_tlb_guest_local(void)
+{
+    HFENCE_VVMA(0, 0);

For this use, ...

xen/arch/riscv/include/asm/insn-defs.h line 8

Quoted 11 lines
xen/arch/riscv/include/asm/insn-defs.h new file +8 -0
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef ASM_RISCV_INSN_DEFS_H
+#define ASM_RISCV_INSN_DEFS_H
+
+#define HFENCE_VVMA(vaddr, asid) \
+    asm volatile ("hfence.vvma %0, %1" \
+                  :: "r"(vaddr), "r"(asid) : "memory")

... don't you want to use "rJ" as the constraints here?

Quoted 7 lines
Quoted 3 lines

Even without "rJ" it is using x0 when argument 0 is passed. Just to be on a
safe side I don't mind to add "J".

When "J" is used compiler is trying to use integer 0 (what I misread when read about J) but hfence.vvma expects two registers (zero register in this case), so "J" can't be really used here.

Hmm, I see. Besides the option of using just "r" as you did originally, another looks to be to use the 'z' operand modifier along with "rJ". That's what gcc's riscv.md looks to be doing, and I assume there is a reason for this.

Jan