virtual interrupt controller (vAPLIC/vIMSIC) support
Re: [PATCH v3 29/39] xen/riscv: prepare new IMSIC VS-file
In reply to Baptiste Le DucQuoted 8 lines · Baptiste Le Duc
Quoted 8 lines
[ ... 64 lines skipped ... ]
xen/arch/riscv/aia.c +0
-0@@ -1,8 +1,10 @@/* SPDX-License-Identifier: GPL-2.0-only */
Quoted 8 lines
xen/arch/riscv/aia.c line 7
Quoted 5 lines
xen/arch/riscv/aia.c +2
-0+#include <xen/bug.h> #include <xen/errno.h> #include <xen/init.h> #include <xen/sections.h> +#include <xen/sched.h> Nit: Why
xen/sched.h? The stub doesn't dereference v, andasm/aia.halready forward-declares struct vcpu. If it stays: sched < sections, so it goes one line up.
Indeed, nothing needs it here. I'll include <asm/aia.h> instead, whichaia.c doesn't include yet: without that the stub's struct vcpu would only be declared inside the parameter list. <xen/sched.h> then moves to the patch where v gets dereferenced first.
Quoted 53 lines
Quoted 53 lines
[ ... 30 lines skipped ... ]
xen/arch/riscv/aia.c +52
-0+#define imsic_vs_csr_write (c, v) \ +do { \ + csr_write(CSR_VSISELECT, c); \ + csr_write(CSR_VSIREG, v); \ +} while (0) + +/* +* Leaf macros for the switchcase expansion pyramid below: +* imsic_switchcase_break(ireg, op, v) - emit "case ireg: op(ireg, v); break" +*/ +#define imsic_switchcase_break (ireg, op, v) \ + case ireg: \ + op(ireg, v); \ + break + +/* +* Generic switchcase expansion pyramid. +* F is the per-operation leaf macro, ireg is the base register index. +* Extra args (e.g. an operation and/or a value) are forwarded to F via +* __VA_ARGS__. +*/ +#define imsic_switchcase_2 (F , ireg, ...) \ + F((ireg) + 0, __VA_ARGS__); \ + F((ireg) + 1, __VA_ARGS__) +#define imsic_switchcase_4 (F , ireg, ...) \ + imsic_switchcase_2(F, (ireg) + 0, __VA_ARGS__); \ + imsic_switchcase_2(F, (ireg) + 2, __VA_ARGS__) +#define imsic_switchcase_8 (F , ireg, ...) \ + imsic_switchcase_4(F, (ireg) + 0, __VA_ARGS__); \ + imsic_switchcase_4(F, (ireg) + 4, __VA_ARGS__) +#define imsic_switchcase_16 (F , ireg, ...) \ + imsic_switchcase_8(F, (ireg) + 0, __VA_ARGS__); \ + imsic_switchcase_8(F, (ireg) + 8, __VA_ARGS__) +#define imsic_switchcase_32 (F , ireg, ...) \ + imsic_switchcase_16(F, (ireg) + 0, __VA_ARGS__); \ + imsic_switchcase_16(F, (ireg) + 16, __VA_ARGS__) +#define imsic_switchcase_64 (F , ireg, ...) \ + imsic_switchcase_32(F, (ireg) + 0, __VA_ARGS__); \ + imsic_switchcase_32(F, (ireg) + 32, __VA_ARGS__) + +static void imsic_eix_write (unsigned int ireg,unsigned long val) +{ +switch ( ireg ) + { +imsic_switchcase_64 (imsic_switchcase_break,IMSIC_EIP0 , + imsic_vs_csr_write, val); +imsic_switchcase_64 (imsic_switchcase_break,IMSIC_EIE0 , + imsic_vs_csr_write, val); +default : +ASSERT_UNREACHABLE (); +break ; + }
Quoted 8 lines
xen/arch/riscv/aia.c
Quoted 2 lines
xen/arch/riscv/aia.c +2
-0+} +What does the switch buy? ireg only ends up as the value written to vsiselect, not as a CSR number, so it doesn't need to be a compile-time constant.
imsic_vs_csr_write(ireg, val)does the same, without the 128 case labels and the macro pyramid. If there's a reason, it wants stating in a comment; otherwise please drop the pyramid (anASSERT()on the range can stay if you want one).
Good point.
I will drop it and use the following instead:
/*
* On RV64 a 64-bit EIx group is the even-numbered register alone,
whereas on
* RV32 it is split into the even-numbered (low half) and the following
* odd-numbered (high half) register. ireg names the first, even-numbered
* register of such a group, among eip0..eip63 or eie0..eie63. */
static void imsic_eix_check(unsigned int ireg)
{
ASSERT(!(ireg & 1));
ASSERT((ireg >= IMSIC_EIP0 && ireg < IMSIC_EIP0 + 64) ||
(ireg >= IMSIC_EIE0 && ireg < IMSIC_EIE0 + 64));
}/* Write a whole 64-bit EIx group, laid out as described above. */ static void imsic_eix_write64(unsigned int ireg, uint64_t val)
{
imsic_eix_check(ireg);
imsic_vs_csr_write(ireg, val);
if ( IS_ENABLED(CONFIG_RISCV_32) )
imsic_vs_csr_write(ireg + 1, val >> 32);
}I will applt the similar patter to other patches where the pyramid was used.
Quoted 4 lines
Typo (only if the macros stay): "Leaf macros" - there's only one here. And
case ireg:/op(ireg, v)as well as c and v inimsic_vs_csr_write()want parenthesizing (MISRA 20.7).
As to c and v in imsic_vs_csr_write(): they're passed as whole arguments
to csr_write() and the other csr_*() macros, which is covered by the
R20.7 deviation for macro parameters used as macro arguments. That's also why the parentheses were dropped in v3.
Quoted 23 lines
xen/arch/riscv/aia.c
Quoted 8 lines
[ ... 67 lines skipped ... ]
xen/arch/riscv/aia.c +7
-0+static void cf_check imsic_vsfile_local_clear(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* We can only zero-out if we have a IMSIC VS-file */ Typo: s/a IMSIC/an IMSIC/
Quoted 14 lines
[ ... 31 lines skipped ... ]
voidimsic_migrate_vcpu(struct vcpu *v)
{xen/arch/riscv/aia.c +4
-0+struct imsic_vsfile_data vsfile_data = { + .nr_eix = imsic_cfg.nr_eix, + }; +/* * The scheduler can mark a freshly created vCPU's unit as migrated and * invoke this before the vCPU has ever run (see the migrated branch in @@ -703,5 +849,18 @@ void imsic_migrate_vcpu(struct vcpu *v)if ( v->arch.last_cpu ==CPU_NONE )return ;
Quoted 14 lines
xen/arch/riscv/aia.c void imsic_migrate_vcpu(struct vcpu *v) · line 863
Quoted 12 lines
xen/arch/riscv/aia.c +12
-0+/* +* At this point, all interrupt producers are still using the old IMSIC +* VS-file. +*/ + + vsfile_data.hgei =vgein_assign (v); + +/* We don't support SW interrupt files at the moment. */ +BUG_ON (!vsfile_data.hgei); + +/* Zero-out new IMSIC VS-file */ +imsic_call_on_cpu (v->processor, imsic_vsfile_local_clear, &vsfile_data);Nit: The comment restates the call; drop it.
Agreed, I will drop.
Thanks!
~ Oleksii
Re: [PATCH v3 29/39] xen/riscv: prepare new IMSIC VS-file
Quoted 21 lines · Oleksii Kurochko
[ ... 79 lines skipped ... ]
Quoted 7 lines
Quoted 1 line
+
What does the switch buy? ireg only ends up as the value written to vsiselect, not as a CSR number, so it doesn't need to be a compile-time constant.
imsic_vs_csr_write(ireg, val)does the same, without the 128 case labels and the macro pyramid. If there's a reason, it wants stating in a comment; otherwise please drop the pyramid (anASSERT()on the range can stay if you want one).Good point.
I will drop it and use the following instead:
/*
* On RV64 a 64-bit EIx group is the even-numbered register alone,
whereas on
* RV32 it is split into the even-numbered (low half) and the following
* odd-numbered (high half) register. ireg names the first, even-numbered
* register of such a group, among eip0..eip63 or eie0..eie63. */
static voidimsic_eix_check(unsigned int ireg)
Quoted 7 lines
{ ASSERT(!(ireg & 1)); ASSERT((ireg >= IMSIC_EIP0 && ireg < IMSIC_EIP0 + 64) || (ireg >= IMSIC_EIE0 && ireg < IMSIC_EIE0 + 64)); }/* Write a whole 64-bit EIx group, laid out as described above. */
Nit: "above" may go stale if the code moves. Better to name the function you are referring to.
Also, the parameter isn't an *ireg value. It is the register number written to *iselect; *ireg is only the data window. Per AIA (IMSIC chapter, "Interrupt files"), values 0x80-0xFF of *iselect select eip0-63/eie0-63, and imsic_csr_write() uses its first argument that way:
#define imsic_csr_write(c, v) \
do { \
csr_write(CSR_SISELECT, c); \
csr_write(CSR_SIREG, v); \
} while (0)So I'd rename it to "iselect" (here and in the other helpers taking the same argument).
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 29/39] xen/riscv: prepare new IMSIC VS-file
Quoted 28 lines · Baptiste Le Duc
Quoted 27 lines · Oleksii Kurochko
[ ... 79 lines skipped ... ]
Quoted 7 lines
Quoted 1 line
+
What does the switch buy? ireg only ends up as the value written to vsiselect, not as a CSR number, so it doesn't need to be a compile-time constant.
imsic_vs_csr_write(ireg, val)does the same, without the 128 case labels and the macro pyramid. If there's a reason, it wants stating in a comment; otherwise please drop the pyramid (anASSERT()on the range can stay if you want one).Good point.
I will drop it and use the following instead:
/*
* On RV64 a 64-bit EIx group is the even-numbered register alone,
whereas on
* RV32 it is split into the even-numbered (low half) and the following
* odd-numbered (high half) register. ireg names the first, even-numbered
* register of such a group, among eip0..eip63 or eie0..eie63. */
static voidimsic_eix_check(unsigned int ireg){ ASSERT(!(ireg & 1)); ASSERT((ireg >= IMSIC_EIP0 && ireg < IMSIC_EIP0 + 64) || (ireg >= IMSIC_EIE0 && ireg < IMSIC_EIE0 + 64)); }
Quoted 4 lines
Quoted 1 line
/* Write a whole 64-bit EIx group, laid out as described above. */
Nit: "above" may go stale if the code moves. Better to name the function you are referring to.
Agree with that.
Quoted 5 lines
Also, the parameter isn't an *ireg value. It is the register number written to *iselect; *ireg is only the data window. Per AIA (IMSIC chapter, "Interrupt files"), values 0x80-0xFF of *iselect select eip0-63/eie0-63, and
imsic_csr_write()uses its first argument that way:
Quoted 9 lines
#define
imsic_csr_write(c, v)\do { \ csr_write(CSR_SISELECT, c); \ csr_write(CSR_SIREG, v); \ } while (0)So I'd rename it to "iselect" (here and in the other helpers taking the same argument).
As to the name: I'd rather keep ireg. The value does go into *iselect, but what it denotes is a register of the interrupt file: per the AIA spec, values of *iselect in the range 0x70-0xFF select the registers of the interrupt file (eidelivery, eithreshold, eip0-63, eie0-63), and ireg is the number of such a register, eip0-63/eie0-63 here.
~ Oleksii
PATCH v3 30/39 xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
In reply to Oleksii KurochkoWhen a vCPU is moved to a different guest interrupt file, MSIs that the APLIC has already sent towards the old file may still be in flight. They must land before the old file's state is saved and the switch is done, otherwise they would be lost.
To wait for them, use the APLIC's genmsi register. Writing it makes the APLIC itself send an MSI (an "extempore" MSI) with a given interrupt identity to a given hart. genmsi can only target the hart's hypervisor- level interrupt file, not a guest one, but the AIA spec guarantees that all MSIs previously sent by the same APLIC to the same hart become visible at the hart's IMSIC before the extempore MSI does. So once the extempore MSI has been delivered, no older MSI from this APLIC to the hart can still be in flight, whichever interrupt file it targets.
genmsi's Busy bit clearing only tells that the APLIC has sent the extempore MSI, not that it has arrived, so the sequence described in the AIA spec under "Synchronizing interactions between a hart and the APLIC" is split in two. aplic_genmsi_barrier() does the APLIC side, while imsic_aplic_sync() does the hart side: it clears the pending bit of the reserved identity before the genmsi write and spins on it afterwards, which is what tells that the extempore MSI, and hence every older MSI from this APLIC to this hart, has really arrived. Keeping the hart side in imsic.c leaves the APLIC code free of the IMSIC local CSR accessors.
The last interrupt identity (nr_ids) is reserved for this purpose.
Migration doesn't use the barrier at this stage, so imsic_migrate_vcpu() only references imsic_aplic_sync() to keep the compiler from complaining about it being unused.
---
Changes in v3:
- Rename from "xen/riscv: implement APLIC-hart sync barrier for vCPU migration", as the barrier waits for the MSIs to reach the hart's IMSIC.
- Re-word the commit message, describing in it how the sequence is split between aplic_genmsi_barrier() and imsic_aplic_sync().
- Move imsic_aplic_sync(), imsic_local_is_pending() and imsic_csr_read() here from "xen/riscv: remap interrupts to new IMSIC VS-file", so that both halves of the synchronization sequence are introduced together.
- s/aplic_hart_field/aplic_hart_index/ following the rename of the helper in "xen/riscv: implement virtual APLIC MMIO emulation".
- Drop masking of sync_id with APLIC_TARGET_EIID, which could silently chop off bits; instead add BUILD_BUG_ON() and ASSERT() to make it explicit that sync_id always fits into the EIID field.
- Drop the redundant parentheses around the macro argument in imsic_csr_read().
- Use unsigned int for isel in imsic_local_is_pending() and drop the redundant !! as the function returns bool.
- Rename the unused argument of imsic_aplic_sync() to unused.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/aplic.c | 33 +++++++++++++++++++ xen/arch/riscv/imsic.c | 52 ++++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/aplic.h | 3 ++ xen/arch/riscv/include/asm/imsic.h | 8 +++++ 4 files changed, 96 insertions(+)
xen/arch/riscv/aplic.c +33 -0
@@ -27,7 +27,9 @@#include <asm/imsic.h> #include <asm/intc.h> #include <asm/io.h> +#include <asm/processor.h> #include <asm/riscv_encoding.h> +#include <asm/smp.h> static struct aplic_priv aplic = { .lock =SPIN_LOCK_UNLOCKED ,
@@ -183,6 +185,37 @@ void aplic_hw_write_reg(unsigned int offset, uint32_t value)spin_unlock_irqrestore (&aplic.lock, flags); } +/* +* As needed, synchronize with all IOMMUs and APLICs to ensure that no +* straggler MSIs will arrive at the old interrupt file after this step. +*/ +void aplic_genmsi_barrier (void ) +{ +const struct imsic_config *imsic =imsic_get_config (); +unsigned int cpu =smp_processor_id (); +unsigned long flags; +uint32_t val; + +/* +* sync_id is nr_ids, which imsic_parse_node() limits to IMSIC_MAX_ID, +* so it always fits into the EIID field. +*/ +BUILD_BUG_ON (IMSIC_MAX_ID >MASK_EXTR (~0U ,APLIC_TARGET_EIID )); +ASSERT (imsic->sync_id <=IMSIC_MAX_ID ); + + val =MASK_INSR (aplic_hart_index (cpu),APLIC_TARGET_HART_IDX ) | + imsic->sync_id; + +spin_lock_irqsave (&aplic.lock, flags); + +writel (val, &aplic.regs->genmsi); + +while (readl (&aplic.regs->genmsi) &APLIC_GENMSI_BUSY ) +cpu_relax (); + +spin_unlock_irqrestore (&aplic.lock, flags); +} +static void __init aplic_init_hw_interrupts (void ) {unsigned int i;
xen/arch/riscv/imsic.c +52 -0
93 lines not on this page · raw message
xen/arch/riscv/include/asm/aplic.h +3 -0
@@ -94,6 +94,7 @@#define APLIC_SETIPNUM_LE 0x2000#define APLIC_GENMSI 0x3000 +#define APLIC_GENMSI_BUSY BIT(12, U)#define APLIC_TARGET_BASE 0x3004#define APLIC_TARGET_LAST 0x3ffc
@@ -177,4 +178,6 @@ struct aplic_regs {uint32_t aplic_hw_read_reg (unsigned int offset);void aplic_hw_write_reg (unsigned int offset,uint32_t value); +void aplic_genmsi_barrier (void ); +#endif /* ASM_RISCV_APLIC_H */
xen/arch/riscv/include/asm/imsic.h +8 -0
@@ -62,6 +62,14 @@ struct imsic_config {*/ unsigned int nr_eix; +/* +* Interrupt identity reserved exclusively for APLIC-to-hart +* synchronization. +* +* Must not be allocated to any interrupt source. +*/ +unsigned int sync_id; +/* MSI */ const struct imsic_msi *msi;
Signature · 1 line
2.55.0
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 245 lines · Oleksii Kurochko
When a vCPU is moved to a different guest interrupt file, MSIs that the APLIC has already sent towards the old file may still be in flight. They must land before the old file's state is saved and the switch is done, otherwise they would be lost.
To wait for them, use the APLIC's genmsi register. Writing it makes the APLIC itself send an MSI (an "extempore" MSI) with a given interrupt identity to a given hart. genmsi can only target the hart's hypervisor- level interrupt file, not a guest one, but the AIA spec guarantees that all MSIs previously sent by the same APLIC to the same hart become visible at the hart's IMSIC before the extempore MSI does. So once the extempore MSI has been delivered, no older MSI from this APLIC to the hart can still be in flight, whichever interrupt file it targets.
genmsi's Busy bit clearing only tells that the APLIC has sent the extempore MSI, not that it has arrived, so the sequence described in the AIA spec under "Synchronizing interactions between a hart and the APLIC" is split in two.
aplic_genmsi_barrier()does the APLIC side, whileimsic_aplic_sync()does the hart side: it clears the pending bit of the reserved identity before the genmsi write and spins on it afterwards, which is what tells that the extempore MSI, and hence every older MSI from this APLIC to this hart, has really arrived. Keeping the hart side inimsic.cleaves the APLIC code free of the IMSIC local CSR accessors.The last interrupt identity (
nr_ids) is reserved for this purpose.Migration doesn't use the barrier at this stage, so
imsic_migrate_vcpu()only referencesimsic_aplic_sync()to keep the compiler from complaining about it being unused.Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v3:
- Rename from "xen/riscv: implement APLIC-hart sync barrier for vCPU migration", as the barrier waits for the MSIs to reach the hart's IMSIC.
- Re-word the commit message, describing in it how the sequence is split betweenaplic_genmsi_barrier()andimsic_aplic_sync().
- Moveimsic_aplic_sync(),imsic_local_is_pending()andimsic_csr_read()here from "xen/riscv: remap interrupts to new IMSIC VS-file", so that both halves of the synchronization sequence are introduced together.
- s/aplic_hart_field/aplic_hart_index/ following the rename of the helper in "xen/riscv: implement virtual APLIC MMIO emulation".
- Drop masking ofsync_idwithAPLIC_TARGET_EIID, which could silently chop off bits; instead addBUILD_BUG_ON()andASSERT()to make it explicit thatsync_idalways fits into the EIID field.
- Drop the redundant parentheses around the macro argument inimsic_csr_read().- Use unsigned int for isel in
imsic_local_is_pending()and drop the redundant !! as the function returns bool.
- Rename the unused argument ofimsic_aplic_sync()to unused.
---
Changes in v2:
- New patch.
------ xen/arch/riscv/aplic.c | 33 +++++++++++++++++++ xen/arch/riscv/imsic.c | 52 ++++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/aplic.h | 3 ++ xen/arch/riscv/include/asm/imsic.h | 8 +++++ 4 files changed, 96 insertions(+)xen/arch/riscv/aplic.c +33
-0@@ -27,7 +27,9 @@#include <asm/imsic.h> #include <asm/intc.h> #include <asm/io.h> +#include <asm/processor.h> #include <asm/riscv_encoding.h> +#include <asm/smp.h> static struct aplic_priv aplic = { .lock =SPIN_LOCK_UNLOCKED ,@@ -183,6 +185,37 @@ void aplic_hw_write_reg(unsigned int offset, uint32_t value)spin_unlock_irqrestore (&aplic.lock, flags); } +/* +* As needed, synchronize with all IOMMUs and APLICs to ensure that no +* straggler MSIs will arrive at the old interrupt file after this step. +*/ +void aplic_genmsi_barrier (void ) +{ +const struct imsic_config *imsic =imsic_get_config (); +unsigned int cpu =smp_processor_id (); +unsigned long flags; +uint32_t val; + +/* +* sync_id is nr_ids, which imsic_parse_node() limits to IMSIC_MAX_ID, +* so it always fits into the EIID field. +*/ +BUILD_BUG_ON (IMSIC_MAX_ID >MASK_EXTR (~0U ,APLIC_TARGET_EIID )); +ASSERT (imsic->sync_id <=IMSIC_MAX_ID ); + + val =MASK_INSR (aplic_hart_index (cpu),APLIC_TARGET_HART_IDX ) | + imsic->sync_id; + +spin_lock_irqsave (&aplic.lock, flags); + +writel (val, &aplic.regs->genmsi); + +while (readl (&aplic.regs->genmsi) &APLIC_GENMSI_BUSY ) +cpu_relax (); + +spin_unlock_irqrestore (&aplic.lock, flags); +} +static void __init aplic_init_hw_interrupts (void ) {unsigned int i;xen/arch/riscv/imsic.c +52
-0@@ -27,6 +27,7 @@#include <xen/xvmalloc.h> #include <asm/aia.h> +#include <asm/aplic.h> #include <asm/imsic.h> #define IMSIC_HART_SIZE (guest_bits) (BIT(guest_bits, U) * IMSIC_MMIO_PAGE_SZ)@@ -60,6 +61,12 @@ static unsigned int __ro_after_init guest_num_msis;#define IMSIC_DISABLE_EITHRESHOLD 1#define IMSIC_ENABLE_EITHRESHOLD 0 +#define imsic_csr_read (c) \ +({ \ + csr_write(CSR_SISELECT, c); \ + csr_read(CSR_SIREG); \ +}) +#define imsic_csr_write (c, v) \ do { \ csr_write(CSR_SISELECT, c); \@@ -203,6 +210,15 @@ void imsic_irq_enable(unsigned int irq)*/ ASSERT (!local_irq_is_enabled ()); +if ( irq == imsic_cfg.sync_id ) + { +printk (XENLOG_WARNING +"irq%u is reserved for APLIC sync so shouldn't be set by %s\n" , + irq, __func__); + +return ; + } +spin_lock (&imsic_cfg.lock);/* * There is no irq - 1 here (look at aplic_set_irq_type()) because: @@ -241,6 +257,15 @@ void imsic_irq_disable(unsigned int irq)spin_unlock (&imsic_cfg.lock); } +static bool imsic_local_is_pending (unsigned int id) +{ +unsigned int isel = + (id /BITS_PER_LONG ) * (BITS_PER_LONG / IMSIC_EIPx_BITS) +IMSIC_EIP0 ; +unsigned long bit =BIT (id %BITS_PER_LONG ,UL ); + +return imsic_csr_read (isel) & bit; +} +/* Callers aren't intended to changed imsic_cfg so return const. */ const struct imsic_config *imsic_get_config (void ) {@@ -387,6 +412,9 @@ static int __init imsic_parse_node(const struct dt_device_node *node, imsic_cfg.nr_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,64 ); +/* Reserve last identity for APLIC-to-hart synchronization */ + imsic_cfg.sync_id = imsic_cfg.nr_ids; +/* Compute base address */ *nr_mmios =0 ; rc =dt_device_get_address (node, *nr_mmios, &base_addr,NULL );@@ -497,6 +525,27 @@ static void imsic_call_on_cpu(unsigned int cpu, void (*func)(void *),on_selected_cpus (cpumask_of (cpu), func, data,1 ); } +/* +* Ensure that all the MSIs the APLIC has already generated for the hart this +* runs on have really reached the hart's IMSIC. +* +* The barrier is the one described by the AIA specification in +* "Synchronizing interactions between a hart and the APLIC": ask the APLIC to +* send an MSI to the hart itself and wait until it shows up as pending in the +* hart's own interrupt file. As it says nothing about MSIs on their way to +* any other hart, it has to be executed by the pCPU owning the interrupt file +* the MSIs were being sent to. +*/ +static void cf_check imsic_aplic_sync(void *unused) +{ +imsic_local_eix_update (imsic_cfg.sync_id,1 , true, false); + +aplic_genmsi_barrier (); + +while ( !imsic_local_is_pending (imsic_cfg.sync_id) ) +cpu_relax (); +} + static void cf_check imsic_vsfile_local_clear(void *data) {unsigned int i;@@ -862,5 +911,8 @@ void imsic_migrate_vcpu(struct vcpu *v)/* Zero-out new IMSIC VS-file */ imsic_call_on_cpu (v->processor, imsic_vsfile_local_clear, &vsfile_data); +/* Avoid an unused-function error until the barrier has a user. */ + (void )imsic_aplic_sync; +BUG_ON ("unimplemented" ); }xen/arch/riscv/include/asm/aplic.h +3
-0@@ -94,6 +94,7 @@#define APLIC_SETIPNUM_LE 0x2000#define APLIC_GENMSI 0x3000 +#define APLIC_GENMSI_BUSY BIT(12, U)#define APLIC_TARGET_BASE 0x3004#define APLIC_TARGET_LAST 0x3ffc@@ -177,4 +178,6 @@ struct aplic_regs {uint32_t aplic_hw_read_reg (unsigned int offset);void aplic_hw_write_reg (unsigned int offset,uint32_t value); +void aplic_genmsi_barrier (void ); +#endif /* ASM_RISCV_APLIC_H */ xen/arch/riscv/include/asm/imsic.h +1
-0@@ -62,6 +62,14 @@ struct imsic_config {*/ unsigned int nr_eix; +/*
xen/arch/riscv/include/asm/imsic.h struct imsic_config { · line 75
Quoted 10 lines
xen/arch/riscv/include/asm/imsic.h +7
-0+* Interrupt identity reserved exclusively for APLIC-to-hart +* synchronization. +* +* Must not be allocated to any interrupt source. +*/ +unsigned int sync_id; +/* MSI */ const struct imsic_msi *msi;
I think it also makes sense here to apply the following fixup patch:
commit db57e94ed8405c98d7a21f4f88764fa5e0ea2d04 (riscv-next-upstreaming-fixups)
Author: Oleksii Kurochko <[email protected]> Date: Tue Oct 6 15:59:52 2026 +0200
fixup! xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU
migration
In MSI delivery mode the MSI of APLIC source N carries identity N, by which Xen recognises it in its own IMSIC interrupt file. A source numbered sync_id or higher can't be told apart: its identity either isn't implemented, so its MSIs are dropped, or is the one reserved for the sync barrier, which it would satisfy early and whose pending bit would swallow its interrupt. Limit the usable sources to those below sync_id, and document how the identities of Xen's interrupt file are allocated.
xen/arch/riscv/aplic.c +17 -0
@@ -246,6 +246,7 @@ static int __init cf_check aplic_init(void)uint64_t size, paddr;const struct dt_device_node *imsic_node;const struct dt_device_node *node = aplic_info.node; +const struct imsic_config *imsic;int rc;/* Check for associated imsic node */
@@ -270,6 +271,22 @@ static int __init cf_check aplic_init(void)panic ("%s: failed to get number of interrupt sources\n" , node->full_name); +/* +* In MSI delivery mode the MSI of source N carries identity N, by which +* Xen recognises it in its own IMSIC interrupt file. Hence the sources +* numbered from sync_id on can't be used: their identities either aren't +* implemented or are the one reserved for the APLIC-IMSIC sync barrier. +*/ + imsic =imsic_get_config (); +if ( aplic_info.num_irqs >= imsic->sync_id ) + { +printk (XENLOG_WARNING +"%s: only %u of %u interrupt sources usable with %u IMSIC identities\n" , + node->full_name, imsic->sync_id -1 , aplic_info.num_irqs, + imsic->nr_ids); + aplic_info.num_irqs = imsic->sync_id -1 ; + } + guest_aplic_num_sources = min(GUEST_APLIC_MAX_SOURCES,
aplic_info.num_irqs);
if ( aplic_info.num_irqs > ARRAY_SIZE(aplic.regs->sourcecfg) )
xen/arch/riscv/imsic.c +9 -1
@@ -533,7 +533,15 @@ static int __init imsic_parse_node(const struct dt_device_node *node, imsic_cfg.nr_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,64 ); -/* Reserve last identity for APLIC-to-hart synchronization */ +/* +* The identities of Xen's own interrupt file are allocated statically: +* - the APLIC sources take 1 .. riscv,num-sources, as the MSI of source +* N carries identity N (aplic_init() drops the sources which don't fit +* below sync_id); +* - the last one, nr_ids, is reserved for APLIC-to-hart synchronization. +* Anything else taking identities from this file has to stay clear of +* both. +*/ imsic_cfg.sync_id = imsic_cfg.nr_ids;/* Compute base address */
Any thoughts about it?
Thanks in advance.
~ Oleksii
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 343 lines · Oleksii Kurochko
Quoted 245 lines · Oleksii Kurochko
When a vCPU is moved to a different guest interrupt file, MSIs that the APLIC has already sent towards the old file may still be in flight. They must land before the old file's state is saved and the switch is done, otherwise they would be lost.
To wait for them, use the APLIC's genmsi register. Writing it makes the APLIC itself send an MSI (an "extempore" MSI) with a given interrupt identity to a given hart. genmsi can only target the hart's hypervisor- level interrupt file, not a guest one, but the AIA spec guarantees that all MSIs previously sent by the same APLIC to the same hart become visible at the hart's IMSIC before the extempore MSI does. So once the extempore MSI has been delivered, no older MSI from this APLIC to the hart can still be in flight, whichever interrupt file it targets.
genmsi's Busy bit clearing only tells that the APLIC has sent the extempore MSI, not that it has arrived, so the sequence described in the AIA spec under "Synchronizing interactions between a hart and the APLIC" is split in two.
aplic_genmsi_barrier()does the APLIC side, whileimsic_aplic_sync()does the hart side: it clears the pending bit of the reserved identity before the genmsi write and spins on it afterwards, which is what tells that the extempore MSI, and hence every older MSI from this APLIC to this hart, has really arrived. Keeping the hart side inimsic.cleaves the APLIC code free of the IMSIC local CSR accessors.The last interrupt identity (
nr_ids) is reserved for this purpose.Migration doesn't use the barrier at this stage, so
imsic_migrate_vcpu()only referencesimsic_aplic_sync()to keep the compiler from complaining about it being unused.Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v3:
- Rename from "xen/riscv: implement APLIC-hart sync barrier for vCPU migration", as the barrier waits for the MSIs to reach the hart's IMSIC.
- Re-word the commit message, describing in it how the sequence is split betweenaplic_genmsi_barrier()andimsic_aplic_sync().
- Moveimsic_aplic_sync(),imsic_local_is_pending()andimsic_csr_read()here from "xen/riscv: remap interrupts to new IMSIC VS-file", so that both halves of the synchronization sequence are introduced together.
- s/aplic_hart_field/aplic_hart_index/ following the rename of the helper in "xen/riscv: implement virtual APLIC MMIO emulation".
- Drop masking ofsync_idwithAPLIC_TARGET_EIID, which could silently chop off bits; instead addBUILD_BUG_ON()andASSERT()to make it explicit thatsync_idalways fits into the EIID field.
- Drop the redundant parentheses around the macro argument inimsic_csr_read().- Use unsigned int for isel in
imsic_local_is_pending()and drop the redundant !! as the function returns bool.
- Rename the unused argument ofimsic_aplic_sync()to unused.
---
Changes in v2:
- New patch.
------ xen/arch/riscv/aplic.c | 33 +++++++++++++++++++ xen/arch/riscv/imsic.c | 52 ++++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/aplic.h | 3 ++ xen/arch/riscv/include/asm/imsic.h | 8 +++++ 4 files changed, 96 insertions(+)xen/arch/riscv/aplic.c +33
-0@@ -27,7 +27,9 @@#include <asm/imsic.h> #include <asm/intc.h> #include <asm/io.h> +#include <asm/processor.h> #include <asm/riscv_encoding.h> +#include <asm/smp.h> static struct aplic_priv aplic = { .lock =SPIN_LOCK_UNLOCKED ,@@ -183,6 +185,37 @@ void aplic_hw_write_reg(unsigned int offset, uint32_t value)spin_unlock_irqrestore (&aplic.lock, flags); } +/* +* As needed, synchronize with all IOMMUs and APLICs to ensure that no +* straggler MSIs will arrive at the old interrupt file after this step. +*/ +void aplic_genmsi_barrier (void ) +{ +const struct imsic_config *imsic =imsic_get_config (); +unsigned int cpu =smp_processor_id (); +unsigned long flags; +uint32_t val; + +/* +* sync_id is nr_ids, which imsic_parse_node() limits to IMSIC_MAX_ID, +* so it always fits into the EIID field. +*/ +BUILD_BUG_ON (IMSIC_MAX_ID >MASK_EXTR (~0U ,APLIC_TARGET_EIID )); +ASSERT (imsic->sync_id <=IMSIC_MAX_ID ); + + val =MASK_INSR (aplic_hart_index (cpu),APLIC_TARGET_HART_IDX ) | + imsic->sync_id; + +spin_lock_irqsave (&aplic.lock, flags); + +writel (val, &aplic.regs->genmsi); + +while (readl (&aplic.regs->genmsi) &APLIC_GENMSI_BUSY ) +cpu_relax (); + +spin_unlock_irqrestore (&aplic.lock, flags); +} +static void __init aplic_init_hw_interrupts (void ) {unsigned int i;xen/arch/riscv/imsic.c +52
-0@@ -27,6 +27,7 @@#include <xen/xvmalloc.h> #include <asm/aia.h> +#include <asm/aplic.h> #include <asm/imsic.h> #define IMSIC_HART_SIZE (guest_bits) (BIT(guest_bits, U) * IMSIC_MMIO_PAGE_SZ)@@ -60,6 +61,12 @@ static unsigned int __ro_after_init guest_num_msis;#define IMSIC_DISABLE_EITHRESHOLD 1#define IMSIC_ENABLE_EITHRESHOLD 0 +#define imsic_csr_read (c) \ +({ \ + csr_write(CSR_SISELECT, c); \ + csr_read(CSR_SIREG); \ +}) +#define imsic_csr_write (c, v) \ do { \ csr_write(CSR_SISELECT, c); \@@ -203,6 +210,15 @@ void imsic_irq_enable(unsigned int irq)*/ ASSERT (!local_irq_is_enabled ()); +if ( irq == imsic_cfg.sync_id ) + { +printk (XENLOG_WARNING +"irq%u is reserved for APLIC sync so shouldn't be set by %s\n" , + irq, __func__); + +return ; + } +spin_lock (&imsic_cfg.lock);/* * There is no irq - 1 here (look at aplic_set_irq_type()) because: @@ -241,6 +257,15 @@ void imsic_irq_disable(unsigned int irq)spin_unlock (&imsic_cfg.lock); } +static bool imsic_local_is_pending (unsigned int id) +{ +unsigned int isel = + (id /BITS_PER_LONG ) * (BITS_PER_LONG / IMSIC_EIPx_BITS) +IMSIC_EIP0 ; +unsigned long bit =BIT (id %BITS_PER_LONG ,UL ); + +return imsic_csr_read (isel) & bit; +} +/* Callers aren't intended to changed imsic_cfg so return const. */ const struct imsic_config *imsic_get_config (void ) {@@ -387,6 +412,9 @@ static int __init imsic_parse_node(const struct dt_device_node *node, imsic_cfg.nr_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,64 ); +/* Reserve last identity for APLIC-to-hart synchronization */ + imsic_cfg.sync_id = imsic_cfg.nr_ids; +/* Compute base address */ *nr_mmios =0 ; rc =dt_device_get_address (node, *nr_mmios, &base_addr,NULL );@@ -497,6 +525,27 @@ static void imsic_call_on_cpu(unsigned int cpu, void (*func)(void *),on_selected_cpus (cpumask_of (cpu), func, data,1 ); } +/* +* Ensure that all the MSIs the APLIC has already generated for the hart this +* runs on have really reached the hart's IMSIC. +* +* The barrier is the one described by the AIA specification in +* "Synchronizing interactions between a hart and the APLIC": ask the APLIC to +* send an MSI to the hart itself and wait until it shows up as pending in the +* hart's own interrupt file. As it says nothing about MSIs on their way to +* any other hart, it has to be executed by the pCPU owning the interrupt file +* the MSIs were being sent to. +*/ +static void cf_check imsic_aplic_sync(void *unused) +{ +imsic_local_eix_update (imsic_cfg.sync_id,1 , true, false); + +aplic_genmsi_barrier (); + +while ( !imsic_local_is_pending (imsic_cfg.sync_id) ) +cpu_relax (); +} + static void cf_check imsic_vsfile_local_clear(void *data) {unsigned int i;@@ -862,5 +911,8 @@ void imsic_migrate_vcpu(struct vcpu *v)/* Zero-out new IMSIC VS-file */ imsic_call_on_cpu (v->processor, imsic_vsfile_local_clear, &vsfile_data); +/* Avoid an unused-function error until the barrier has a user. */ + (void )imsic_aplic_sync; +BUG_ON ("unimplemented" ); }xen/arch/riscv/include/asm/aplic.h +3
-0@@ -94,6 +94,7 @@#define APLIC_SETIPNUM_LE 0x2000#define APLIC_GENMSI 0x3000 +#define APLIC_GENMSI_BUSY BIT(12, U)#define APLIC_TARGET_BASE 0x3004#define APLIC_TARGET_LAST 0x3ffc@@ -177,4 +178,6 @@ struct aplic_regs {uint32_t aplic_hw_read_reg (unsigned int offset);void aplic_hw_write_reg (unsigned int offset,uint32_t value); +void aplic_genmsi_barrier (void ); +#endif /* ASM_RISCV_APLIC_H */ xen/arch/riscv/include/asm/imsic.h +1
-0@@ -62,6 +62,14 @@ struct imsic_config {*/ unsigned int nr_eix; +/* xen/arch/riscv/include/asm/imsic.h struct imsic_config { · line 75
Quoted 10 lines
xen/arch/riscv/include/asm/imsic.h +7
-0+* Interrupt identity reserved exclusively for APLIC-to-hart +* synchronization. +* +* Must not be allocated to any interrupt source. +*/ +unsigned int sync_id; +/* MSI */ const struct imsic_msi *msi;I think it also makes sense here to apply the following fixup patch:
commit
db57e94ed8405c98d7a21f4f88764fa5e0ea2d04
(riscv-next-upstreaming-fixups)Author: Oleksii Kurochko <[email protected]> Date: Tue Oct 6 15:59:52 2026 +0200fixup! xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU
migrationIn MSI delivery mode the MSI of APLIC source N carries identity N, by which Xen recognises it in its own IMSIC interrupt file. A source numbered
sync_idor higher can't be told apart: its identity either isn't implemented, so its MSIs are dropped, or is the one reserved for the sync barrier, which it would satisfy early and whose pending bit would swallow its interrupt. Limit the usable sources to those belowsync_id, and document how the identities of Xen's interrupt file are allocated.xen/arch/riscv/aplic.c +3
-0@@ -246,6 +246,7 @@ static int __init cf_check aplic_init(void)uint64_t size, paddr;const struct dt_device_node *imsic_node;const struct dt_device_node *node = aplic_info.node; +const struct imsic_config *imsic;int rc;/* Check for associated imsic node */ @@ -270,6 +271,22 @@ static int __init cf_check aplic_init(void)panic ("%s: failed to get number of interrupt sources\n" , node->full_name); +/* +* In MSI delivery mode the MSI of source N carries identity N, by which
xen/arch/riscv/aplic.c +2
-0+* Xen recognises it in its own IMSIC interrupt file. Hence the sources +* numbered from sync_id on can't be used: their identities either aren't
+ * implemented or are the one reserved for the APLIC-IMSIC sync
barrier.xen/arch/riscv/aplic.c +6
-0+*/ + imsic =imsic_get_config (); + if ( aplic_info.num_irqs >= imsic->sync_id ) + { + printk(XENLOG_WARNING + "%s: only %u of %u interrupt sources usable with %uIMSIC identities\n",
xen/arch/riscv/aplic.c +5
-0+ node->full_name, imsic->sync_id -1 , aplic_info.num_irqs, + imsic->nr_ids); + aplic_info.num_irqs = imsic->sync_id -1 ; + } + guest_aplic_num_sources = min(GUEST_APLIC_MAX_SOURCES,aplic_info.num_irqs);
if ( aplic_info.num_irqs > ARRAY_SIZE(aplic.regs->sourcecfg) )xen/arch/riscv/imsic.c +3
-1@@ -533,7 +533,15 @@ static int __init imsic_parse_node(const struct dt_device_node *node, imsic_cfg.nr_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,64 ); -/* Reserve last identity for APLIC-to-hart synchronization */ +/* +* The identities of Xen's own interrupt file are allocated statically: +* - the APLIC sources take 1 .. riscv,num-sources, as the MSI of source
+ * N carries identity N (aplic_init()drops the sources which
don't fitxen/arch/riscv/imsic.c +2
-0+* below sync_id); +* - the last one, nr_ids, is reserved for APLIC-to-hart synchronization.
xen/arch/riscv/imsic.c +3
-0+* Anything else taking identities from this file has to stay clear of +* both. +*/ imsic_cfg.sync_id = imsic_cfg.nr_ids;/* Compute base address */
Quoted 1 line
Any thoughts about it?
LGTM, but I wonder why, as I said in the original patch, sync_id is really needed.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 342 lines · Baptiste Le Duc
Quoted 341 lines · Oleksii Kurochko
Quoted 245 lines · Oleksii Kurochko
When a vCPU is moved to a different guest interrupt file, MSIs that the APLIC has already sent towards the old file may still be in flight. They must land before the old file's state is saved and the switch is done, otherwise they would be lost.
To wait for them, use the APLIC's genmsi register. Writing it makes the APLIC itself send an MSI (an "extempore" MSI) with a given interrupt identity to a given hart. genmsi can only target the hart's hypervisor- level interrupt file, not a guest one, but the AIA spec guarantees that all MSIs previously sent by the same APLIC to the same hart become visible at the hart's IMSIC before the extempore MSI does. So once the extempore MSI has been delivered, no older MSI from this APLIC to the hart can still be in flight, whichever interrupt file it targets.
genmsi's Busy bit clearing only tells that the APLIC has sent the extempore MSI, not that it has arrived, so the sequence described in the AIA spec under "Synchronizing interactions between a hart and the APLIC" is split in two.
aplic_genmsi_barrier()does the APLIC side, whileimsic_aplic_sync()does the hart side: it clears the pending bit of the reserved identity before the genmsi write and spins on it afterwards, which is what tells that the extempore MSI, and hence every older MSI from this APLIC to this hart, has really arrived. Keeping the hart side inimsic.cleaves the APLIC code free of the IMSIC local CSR accessors.The last interrupt identity (
nr_ids) is reserved for this purpose.Migration doesn't use the barrier at this stage, so
imsic_migrate_vcpu()only referencesimsic_aplic_sync()to keep the compiler from complaining about it being unused.Signed-off-by Oleksii Kurochko <[email protected]>---
Changes in v3:
- Rename from "xen/riscv: implement APLIC-hart sync barrier for vCPU migration", as the barrier waits for the MSIs to reach the hart's IMSIC.
- Re-word the commit message, describing in it how the sequence is split betweenaplic_genmsi_barrier()andimsic_aplic_sync().
- Moveimsic_aplic_sync(),imsic_local_is_pending()andimsic_csr_read()here from "xen/riscv: remap interrupts to new IMSIC VS-file", so that both halves of the synchronization sequence are introduced together.
- s/aplic_hart_field/aplic_hart_index/ following the rename of the helper in "xen/riscv: implement virtual APLIC MMIO emulation".
- Drop masking ofsync_idwithAPLIC_TARGET_EIID, which could silently chop off bits; instead addBUILD_BUG_ON()andASSERT()to make it explicit thatsync_idalways fits into the EIID field.
- Drop the redundant parentheses around the macro argument inimsic_csr_read().- Use unsigned int for isel in
imsic_local_is_pending()and drop the redundant !! as the function returns bool.
- Rename the unused argument ofimsic_aplic_sync()to unused.
---
Changes in v2:
- New patch.
------ xen/arch/riscv/aplic.c | 33 +++++++++++++++++++ xen/arch/riscv/imsic.c | 52 ++++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/aplic.h | 3 ++ xen/arch/riscv/include/asm/imsic.h | 8 +++++ 4 files changed, 96 insertions(+)xen/arch/riscv/aplic.c +33
-0@@ -27,7 +27,9 @@#include <asm/imsic.h> #include <asm/intc.h> #include <asm/io.h> +#include <asm/processor.h> #include <asm/riscv_encoding.h> +#include <asm/smp.h> static struct aplic_priv aplic = { .lock =SPIN_LOCK_UNLOCKED ,@@ -183,6 +185,37 @@ void aplic_hw_write_reg(unsigned int offset, uint32_t value)spin_unlock_irqrestore (&aplic.lock, flags); } +/* +* As needed, synchronize with all IOMMUs and APLICs to ensure that no +* straggler MSIs will arrive at the old interrupt file after this step. +*/ +void aplic_genmsi_barrier (void ) +{ +const struct imsic_config *imsic =imsic_get_config (); +unsigned int cpu =smp_processor_id (); +unsigned long flags; +uint32_t val; + +/* +* sync_id is nr_ids, which imsic_parse_node() limits to IMSIC_MAX_ID, +* so it always fits into the EIID field. +*/ +BUILD_BUG_ON (IMSIC_MAX_ID >MASK_EXTR (~0U ,APLIC_TARGET_EIID )); +ASSERT (imsic->sync_id <=IMSIC_MAX_ID ); + + val =MASK_INSR (aplic_hart_index (cpu),APLIC_TARGET_HART_IDX ) | + imsic->sync_id; + +spin_lock_irqsave (&aplic.lock, flags); + +writel (val, &aplic.regs->genmsi); + +while (readl (&aplic.regs->genmsi) &APLIC_GENMSI_BUSY ) +cpu_relax (); + +spin_unlock_irqrestore (&aplic.lock, flags); +} +static void __init aplic_init_hw_interrupts (void ) {unsigned int i;xen/arch/riscv/imsic.c +52
-0@@ -27,6 +27,7 @@#include <xen/xvmalloc.h> #include <asm/aia.h> +#include <asm/aplic.h> #include <asm/imsic.h> #define IMSIC_HART_SIZE (guest_bits) (BIT(guest_bits, U) * IMSIC_MMIO_PAGE_SZ)@@ -60,6 +61,12 @@ static unsigned int __ro_after_init guest_num_msis;#define IMSIC_DISABLE_EITHRESHOLD 1#define IMSIC_ENABLE_EITHRESHOLD 0 +#define imsic_csr_read (c) \ +({ \ + csr_write(CSR_SISELECT, c); \ + csr_read(CSR_SIREG); \ +}) +#define imsic_csr_write (c, v) \ do { \ csr_write(CSR_SISELECT, c); \@@ -203,6 +210,15 @@ void imsic_irq_enable(unsigned int irq)*/ ASSERT (!local_irq_is_enabled ()); +if ( irq == imsic_cfg.sync_id ) + { +printk (XENLOG_WARNING +"irq%u is reserved for APLIC sync so shouldn't be set by %s\n" , + irq, __func__); + +return ; + } +spin_lock (&imsic_cfg.lock);/* * There is no irq - 1 here (look at aplic_set_irq_type()) because: @@ -241,6 +257,15 @@ void imsic_irq_disable(unsigned int irq)spin_unlock (&imsic_cfg.lock); } +static bool imsic_local_is_pending (unsigned int id) +{ +unsigned int isel = + (id /BITS_PER_LONG ) * (BITS_PER_LONG / IMSIC_EIPx_BITS) +IMSIC_EIP0 ; +unsigned long bit =BIT (id %BITS_PER_LONG ,UL ); + +return imsic_csr_read (isel) & bit; +} +/* Callers aren't intended to changed imsic_cfg so return const. */ const struct imsic_config *imsic_get_config (void ) {@@ -387,6 +412,9 @@ static int __init imsic_parse_node(const struct dt_device_node *node, imsic_cfg.nr_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,64 ); +/* Reserve last identity for APLIC-to-hart synchronization */ + imsic_cfg.sync_id = imsic_cfg.nr_ids; +/* Compute base address */ *nr_mmios =0 ; rc =dt_device_get_address (node, *nr_mmios, &base_addr,NULL );@@ -497,6 +525,27 @@ static void imsic_call_on_cpu(unsigned int cpu, void (*func)(void *),on_selected_cpus (cpumask_of (cpu), func, data,1 ); } +/* +* Ensure that all the MSIs the APLIC has already generated for the hart this +* runs on have really reached the hart's IMSIC. +* +* The barrier is the one described by the AIA specification in +* "Synchronizing interactions between a hart and the APLIC": ask the APLIC to +* send an MSI to the hart itself and wait until it shows up as pending in the +* hart's own interrupt file. As it says nothing about MSIs on their way to +* any other hart, it has to be executed by the pCPU owning the interrupt file +* the MSIs were being sent to. +*/ +static void cf_check imsic_aplic_sync(void *unused) +{ +imsic_local_eix_update (imsic_cfg.sync_id,1 , true, false); + +aplic_genmsi_barrier (); + +while ( !imsic_local_is_pending (imsic_cfg.sync_id) ) +cpu_relax (); +} + static void cf_check imsic_vsfile_local_clear(void *data) {unsigned int i;@@ -862,5 +911,8 @@ void imsic_migrate_vcpu(struct vcpu *v)/* Zero-out new IMSIC VS-file */ imsic_call_on_cpu (v->processor, imsic_vsfile_local_clear, &vsfile_data); +/* Avoid an unused-function error until the barrier has a user. */ + (void )imsic_aplic_sync; +BUG_ON ("unimplemented" ); }xen/arch/riscv/include/asm/aplic.h +3
-0@@ -94,6 +94,7 @@#define APLIC_SETIPNUM_LE 0x2000#define APLIC_GENMSI 0x3000 +#define APLIC_GENMSI_BUSY BIT(12, U)#define APLIC_TARGET_BASE 0x3004#define APLIC_TARGET_LAST 0x3ffc@@ -177,4 +178,6 @@ struct aplic_regs {uint32_t aplic_hw_read_reg (unsigned int offset);void aplic_hw_write_reg (unsigned int offset,uint32_t value); +void aplic_genmsi_barrier (void ); +#endif /* ASM_RISCV_APLIC_H */ xen/arch/riscv/include/asm/imsic.h +1
-0@@ -62,6 +62,14 @@ struct imsic_config {*/ unsigned int nr_eix; +/* xen/arch/riscv/include/asm/imsic.h struct imsic_config { · line 75
Quoted 10 lines
xen/arch/riscv/include/asm/imsic.h +7
-0+* Interrupt identity reserved exclusively for APLIC-to-hart +* synchronization. +* +* Must not be allocated to any interrupt source. +*/ +unsigned int sync_id; +/* MSI */ const struct imsic_msi *msi;I think it also makes sense here to apply the following fixup patch:
commit
db57e94ed8405c98d7a21f4f88764fa5e0ea2d04
(riscv-next-upstreaming-fixups)Author: Oleksii Kurochko <[email protected]> Date: Tue Oct 6 15:59:52 2026 +0200fixup! xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU
migrationIn MSI delivery mode the MSI of APLIC source N carries identity N, by which Xen recognises it in its own IMSIC interrupt file. A source numbered
sync_idor higher can't be told apart: its identity either isn't implemented, so its MSIs are dropped, or is the one reserved for the sync barrier, which it would satisfy early and whose pending bit would swallow its interrupt. Limit the usable sources to those belowsync_id, and document how the identities of Xen's interrupt file are allocated.xen/arch/riscv/aplic.c +3
-0@@ -246,6 +246,7 @@ static int __init cf_check aplic_init(void)uint64_t size, paddr;const struct dt_device_node *imsic_node;const struct dt_device_node *node = aplic_info.node; +const struct imsic_config *imsic;int rc;/* Check for associated imsic node */ @@ -270,6 +271,22 @@ static int __init cf_check aplic_init(void)panic ("%s: failed to get number of interrupt sources\n" , node->full_name); +/* +* In MSI delivery mode the MSI of source N carries identity N, by which
xen/arch/riscv/aplic.c +2
-0+* Xen recognises it in its own IMSIC interrupt file. Hence the sources +* numbered from sync_id on can't be used: their identities either aren't
+ * implemented or are the one reserved for the APLIC-IMSIC sync
barrier.xen/arch/riscv/aplic.c +6
-0+*/ + imsic =imsic_get_config (); + if ( aplic_info.num_irqs >= imsic->sync_id ) + { + printk(XENLOG_WARNING + "%s: only %u of %u interrupt sources usable with %uIMSIC identities\n",
xen/arch/riscv/aplic.c +5
-0+ node->full_name, imsic->sync_id -1 , aplic_info.num_irqs, + imsic->nr_ids); + aplic_info.num_irqs = imsic->sync_id -1 ; + } + guest_aplic_num_sources = min(GUEST_APLIC_MAX_SOURCES,aplic_info.num_irqs);
if ( aplic_info.num_irqs > ARRAY_SIZE(aplic.regs->sourcecfg) )xen/arch/riscv/imsic.c +3
-1@@ -533,7 +533,15 @@ static int __init imsic_parse_node(const struct dt_device_node *node, imsic_cfg.nr_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,64 ); -/* Reserve last identity for APLIC-to-hart synchronization */ +/* +* The identities of Xen's own interrupt file are allocated statically: +* - the APLIC sources take 1 .. riscv,num-sources, as the MSI of source
+ * N carries identity N (aplic_init()drops the sources which
don't fitxen/arch/riscv/imsic.c +2
-0+* below sync_id); +* - the last one, nr_ids, is reserved for APLIC-to-hart synchronization.
xen/arch/riscv/imsic.c +3
-0+* Anything else taking identities from this file has to stay clear of +* both. +*/ imsic_cfg.sync_id = imsic_cfg.nr_ids;
Quoted 6 lines
Quoted 3 lines
/* Compute base address */Any thoughts about it?
LGTM, but I wonder why, as I said in the original patch,
sync_idis really needed.
I think that I am not understand your response. Could you please re-phrase it?
~ Oleksii
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 7 lines · Oleksii Kurochko
[ ... 348 lines skipped ... ]
Quoted 3 lines
LGTM, but I wonder why, as I said in the original patch,
sync_idis really needed.I think that I am not understand your response. Could you please re-phrase it?
I'm questionning the need of the sync_id field as it will always be equal to imsic_cfg.nr_ids, couldn't we drop it?
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 11 lines · Baptiste Le Duc
Quoted 7 lines · Oleksii Kurochko
[ ... 348 lines skipped ... ]
Quoted 3 lines
LGTM, but I wonder why, as I said in the original patch,
sync_idis really needed.I think that I am not understand your response. Could you please re-phrase it?
I'm questionning the need of the
sync_idfield as it will always be equal to imsic_cfg.nr_ids, couldn't we drop it?
It's equal to nr_ids by choice rather than by necessity: which identity gets reserved is decided in a single place, imsic_parse_node(), and all the users (the genmsi write, the pending bit the barrier spins on, the limit on the APLIC sources and the ASSERT() in imsic_irq_enable()) refer to it by its role rather than as "the last identity".
Basically if one day someone will decide to come up with EIID allocator instead of using static allocation then it will be harder to find a places where to change.
So yes, it could be drooped but at the moment I think it is useful to have separate field or ,at least, as an option in asm/imsic.h:
static inline unsigned int imsic_sync_id(const struct imsic_config *cfg)
{
return cfg->nr_ids;
}~ Oleksii
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 21 lines · Oleksii Kurochko
Quoted 11 lines · Baptiste Le Duc
Quoted 7 lines · Oleksii Kurochko
[ ... 348 lines skipped ... ]
Quoted 3 lines
LGTM, but I wonder why, as I said in the original patch,
sync_idis really needed.I think that I am not understand your response. Could you please re-phrase it?
I'm questionning the need of the
sync_idfield as it will always be equal to imsic_cfg.nr_ids, couldn't we drop it?It's equal to
nr_idsby choice rather than by necessity: which identity gets reserved is decided in a single place,imsic_parse_node(), and all the users (the genmsi write, the pending bit the barrier spins on, the limit on the APLIC sources and theASSERT()inimsic_irq_enable()) refer to it by its role rather than as "the last identity".
Quoted 8 lines
Basically if one day someone will decide to come up with EIID allocator instead of using static allocation then it will be harder to find a places where to change.
So yes, it could be drooped but at the moment I think it is useful to have separate field or ,at least, as an option in
asm/imsic.h:static inline unsigned int
imsic_sync_id(const struct imsic_config *cfg){
returncfg->nr_ids;
}
I prefer this as it would save space in imsic_config struct.
Quoted 1 line
~ Oleksii
Re: [PATCH v3 30/39] xen/riscv: implement APLIC-IMSIC-hart sync barrier for vCPU migration
Quoted 26 lines · Baptiste Le Duc
Quoted 23 lines · Oleksii Kurochko
Quoted 11 lines · Baptiste Le Duc
Quoted 7 lines · Oleksii Kurochko
[ ... 348 lines skipped ... ]
Quoted 3 lines
LGTM, but I wonder why, as I said in the original patch,
sync_idis really needed.I think that I am not understand your response. Could you please re-phrase it?
I'm questionning the need of the
sync_idfield as it will always be equal to imsic_cfg.nr_ids, couldn't we drop it?It's equal to
nr_idsby choice rather than by necessity: which identity gets reserved is decided in a single place,imsic_parse_node(), and all the users (the genmsi write, the pending bit the barrier spins on, the limit on the APLIC sources and theASSERT()inimsic_irq_enable()) refer to it by its role rather than as "the last identity".Basically if one day someone will decide to come up with EIID allocator instead of using static allocation then it will be harder to find a places where to change.
Quoted 9 lines
Quoted 6 lines
So yes, it could be drooped but at the moment I think it is useful to have separate field or ,at least, as an option in
asm/imsic.h:static inline unsigned int
imsic_sync_id(const struct imsic_config *cfg){
returncfg->nr_ids;
}I prefer this as it would save space in
imsic_configstruct.
Thanks for the clarification. I'll go with that approach, then.
~ Oleksii
PATCH v3 31/39 xen/riscv: remap interrupts to new IMSIC VS-file
In reply to Oleksii KurochkoOnce the new IMSIC VS-file has been zeroed out, it has to be mapped into the guest's G-stage in place of the old one. Also, if any interrupts at an APLIC are forwarded by MSIs to the old interrupt file, reconfigure the APLIC to send them to the new interrupt file.
Taking a h/w guest interrupt file (assigning it, zeroing it out, mapping it into G-stage and recording it in the vCPU's IMSIC state) is factored out into imsic_vsfile_acquire().
The relevant translation tables at all IOMMUs would also have to be updated so that MSIs for this virtual interrupt file are sent to the new physical interrupt file, but that is skipped for now as there is no IOMMU support for RISC-V.
Synchronize with the APLIC to ensure that no straggler MSIs will arrive at the old interrupt file by running imsic_aplic_sync() on the pCPU which owns that file.
If the new file can't be mapped into the guest, release the guest external interrupt number imsic_vsfile_acquire() took for it instead of leaking it. vgein_release() takes the pCPU explicitly instead of relying on v->processor, as on migration v->processor already refers to the new pCPU while the number to be released may belong to the old one.
Keep BUG_ON("unimplemented") placeholder in imsic_migrate_vcpu() to guard against silent incorrect behaviour or unexpected panics in guest VMs until the function is fully implemented.
Implement imsic_update_state(), which records the vCPU's guest interrupt file id together with the pCPU owning the file. The two are read as a pair, e.g. by imsic_migrate_vcpu() and the IMSIC context switch hooks, so they are updated under vsfile_lock as a single unit.
imsic_map_guest_file() and vgein_release() are stubs for now.
---imsic_vsfile_acquire() isn't introduced already by "xen/riscv: prepare new IMSIC VS-file": there, taking the new file is just vgein_assign() and zeroing the file out. What makes it worth a helper of its own, mapping the file into the guest, recording it in the vCPU's IMSIC state and releasing it again with vgein_release() if the mapping fails, only comes with this patch. The helper is also shared with the first attach of a h/w file in "xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu".
---
Changes in v3:
- Update the commit message.
- Move imsic_aplic_sync() and imsic_local_is_pending() to "xen/riscv:
implement APLIC-IMSIC-hart sync barrier for vCPU migration" and only call the helper here.
- compare old_vsfile_cpu against CPU_NONE instead of NR_CPUS.
- Call vaplic_reconfigure_target() instead of aplic_reconfigure_target().
- Update the comment at the start of the migration: at that point the new IMSIC VS-file is only being prepared, the interrupt producers are moved to it later.
- Factor taking a h/w guest interrupt file (assign, zero out, map into G-stage and record in the per-vCPU IMSIC state) out of imsic_migrate_vcpu() into imsic_vsfile_acquire(), introduced here instead of in "xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu".
- Crash the domain instead of BUG_ON()ing when no h/w guest interrupt file is free, and report which file could not be mapped.
- Introduce the vgein_release() stub here, together with its first
user: release the h/w guest interrupt file taken by imsic_vsfile_acquire() if mapping it into the guest fails. Drop the stray "\n" from its BUG_ON().
- Clarify the comment ahead of imsic_vsfile_acquire() on why setting hstatus.VGEIN is left to the caller.
- Implement imsic_update_state() here instead of leaving it a stub until "xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu": all it needs, vsfile_lock and CPU_NONE, is available already. The s/w IMSIC VS-file is recorded with vsfile_cpu == CPU_NONE rather than NR_CPUS.
- Make imsic_update_state() static and drop its declaration from asm/imsic.h: imsic_vsfile_acquire() is its only user.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/aia.c | 5 ++ xen/arch/riscv/imsic.c | 130 +++++++++++++++++++++++++++-- xen/arch/riscv/include/asm/aia.h | 1 + xen/arch/riscv/include/asm/imsic.h | 2 + 4 files changed, 130 insertions(+), 8 deletions(-)
xen/arch/riscv/aia.c +5 -0
@@ -30,3 +30,8 @@ unsigned int vgein_assign(struct vcpu *v)return 0 ; } + +void vgein_release (struct vcpu *v,unsigned int vgein_id,unsigned int cpu) +{ +BUG_ON ("unimplemented" ); +}
xen/arch/riscv/imsic.c +122 -8
172 lines not on this page · raw message
xen/arch/riscv/include/asm/aia.h +1 -0
@@ -10,5 +10,6 @@ bool aia_usable(void);void aia_init (void );unsigned int vgein_assign (struct vcpu *v); +void vgein_release (struct vcpu *v,unsigned int vgein_id,unsigned int cpu);#endif /* RISCV_AIA_H */
xen/arch/riscv/include/asm/imsic.h +2 -0
@@ -114,6 +114,8 @@ int vimsic_make_domu_dt_node(struct kernel_info *kinfo, unsigned int *phandle);void imsic_ctxt_switch_from (struct vcpu *p);void imsic_ctxt_switch_to (struct vcpu *n); +int imsic_map_guest_file (struct vcpu *v,unsigned int vsfile_id); +void imsic_migrate_vcpu (struct vcpu *v);#endif /* ASM_RISCV_IMSIC_H */
Signature · 1 line
2.55.0
Re: [PATCH v3 31/39] xen/riscv: remap interrupts to new IMSIC VS-file
Quoted 28 lines
Once the new IMSIC VS-file has been zeroed out, it has to be mapped into the guest's G-stage in place of the old one. Also, if any interrupts at an APLIC are forwarded by MSIs to the old interrupt file, reconfigure the APLIC to send them to the new interrupt file.
Taking a h/w guest interrupt file (assigning it, zeroing it out, mapping it into G-stage and recording it in the vCPU's IMSIC state) is factored out into
imsic_vsfile_acquire().The relevant translation tables at all IOMMUs would also have to be updated so that MSIs for this virtual interrupt file are sent to the new physical interrupt file, but that is skipped for now as there is no IOMMU support for RISC-V.
Synchronize with the APLIC to ensure that no straggler MSIs will arrive at the old interrupt file by running
imsic_aplic_sync()on the pCPU which owns that file.If the new file can't be mapped into the guest, release the guest external interrupt number
imsic_vsfile_acquire()took for it instead of leaking it.vgein_release()takes the pCPU explicitly instead of relying onv->processor, as on migrationv->processoralready refers to the new pCPU while the number to be released may belong to the old one.Keep
BUG_ON("unimplemented")placeholder inimsic_migrate_vcpu()to guard against silent incorrect behaviour or unexpected panics in guest VMs until the function is fully implemented.
Quoted 6 lines
Implement
imsic_update_state(), which records the vCPU's guest interrupt file id together with the pCPU owning the file. The two are read as a pair, e.g. byimsic_migrate_vcpu()and the IMSIC context switch hooks, so they are updated undervsfile_lockas a single unit.
imsic_map_guest_file()andvgein_release()are stubs for now.
I have the feeling this patch does more than its subject says: besides retargeting the APLIC and adding the sync, it introduces imsic_vsfile_acquire() (assign + clear + G-stage map + state update), imsic_update_state() and the error path, which relies on two stubs, imsic_map_guest_file() and vgein_release(). At this point vgein_assign() is still BUG_ON("unimplemented") too, so none of the new code can actually run, and the error path can't be reviewed against the real vgein_release().
Also, until "introduce IMSIC h/w interrupt file attaching to vcpu", no vCPU ever owns a h/w file, so imsic_migrate_vcpu() can only hit the CPU_NONE check.
Wouldn't it be simpler to move the two already-acked patches and the attach first, so that no stubs are needed and each migration step is written against its real callees when it lands:
- Add basic VGEIN management for AIA guests (real vgein_assign() and vgein_release(); the reason for the explicit cpu argument of vgein_release() would then have to go into this commit message)
- Map IMSIC interrupt file for vCPUs (real imsic_map_guest_file())
- Implement continue_new_vcpu() and set hstatus.VSXL
- Attach a h/w interrupt file to a vCPU on its first run, introducing imsic_vsfile_acquire() and imsic_update_state() with their first user (acquire, vaplic_reconfigure_target(), set hstatus.VGEIN)
- Prepare new IMSIC VS-file (on top of imsic_vsfile_acquire())
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file (APLIC retarget + sync)
- Save/clear old VS-file, restore, SGEI wake-up as today
A side benefit: attach no longer has to be squeezed into the finished migration code, so imsic_vsfile_move() with its first_attach argument and the goto set_vgein go away. Attach stays a plain sequence, and imsic_migrate_vcpu() simply reuses imsic_vsfile_acquire().
Migrating a vCPU which has already run would still hit the trailing BUG_ON("unimplemented") until the restore patch, but that is no worse than the panic() it hits today.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 31/39] xen/riscv: remap interrupts to new IMSIC VS-file
Quoted 33 lines · Baptiste Le Duc
Quoted 33 lines
Once the new IMSIC VS-file has been zeroed out, it has to be mapped into the guest's G-stage in place of the old one. Also, if any interrupts at an APLIC are forwarded by MSIs to the old interrupt file, reconfigure the APLIC to send them to the new interrupt file.
Taking a h/w guest interrupt file (assigning it, zeroing it out, mapping it into G-stage and recording it in the vCPU's IMSIC state) is factored out into
imsic_vsfile_acquire().The relevant translation tables at all IOMMUs would also have to be updated so that MSIs for this virtual interrupt file are sent to the new physical interrupt file, but that is skipped for now as there is no IOMMU support for RISC-V.
Synchronize with the APLIC to ensure that no straggler MSIs will arrive at the old interrupt file by running
imsic_aplic_sync()on the pCPU which owns that file.If the new file can't be mapped into the guest, release the guest external interrupt number
imsic_vsfile_acquire()took for it instead of leaking it.vgein_release()takes the pCPU explicitly instead of relying onv->processor, as on migrationv->processoralready refers to the new pCPU while the number to be released may belong to the old one.Keep
BUG_ON("unimplemented")placeholder inimsic_migrate_vcpu()to guard against silent incorrect behaviour or unexpected panics in guest VMs until the function is fully implemented.Implement
imsic_update_state(), which records the vCPU's guest interrupt file id together with the pCPU owning the file. The two are read as a pair, e.g. byimsic_migrate_vcpu()and the IMSIC context switch hooks, so they are updated undervsfile_lockas a single unit.
Quoted 7 lines
Quoted 1 line
imsic_map_guest_file()andvgein_release()are stubs for now.I have the feeling this patch does more than its subject says: besides retargeting the APLIC and adding the sync, it introduces
imsic_vsfile_acquire()(assign + clear + G-stage map + state update),imsic_update_state()and the error path, which relies on two stubs,imsic_map_guest_file()andvgein_release().
These are the steps the AIA spec lists for moving the interrupt producers to the new file: map the new file into G-stage in place of the old one, retarget the APLIC and synchronize with it. imsic_vsfile_acquire() and imsic_update_state() are just how the "take and map a new file" part is split out, so I'd still say they belong to this patch. Your point below, that none of this can run yet, is the real issue though.
Quoted 9 lines
At this point
vgein_assign()
is stillBUG_ON("unimplemented")too, so none of the new code can actually run, and the error path can't be reviewed against the realvgein_release().Also, until "introduce IMSIC h/w interrupt file attaching to vcpu", no vCPU ever owns a h/w file, so
imsic_migrate_vcpu()can only hit theCPU_NONEcheck.
Quoted 7 lines
Wouldn't it be simpler to move the two already-acked patches and the attach first, so that no stubs are needed and each migration step is written against its real callees when it lands:
- Add basic VGEIN management for AIA guests (realvgein_assign()andvgein_release(); the reason for the explicit cpu argument ofvgein_release()would then have to go into this commit message)
- Map IMSIC interrupt file for vCPUs (realimsic_map_guest_file())
I think I have such order before and it was changed becuase for locks in the first patch it wasn't enough context to understand if they are needed or not.
Quoted 9 lines
- Implement
continue_new_vcpu()and set hstatus.VSXL
- Attach a h/w interrupt file to a vCPU on its first run, introducingimsic_vsfile_acquire()andimsic_update_state()with their first user (acquire,vaplic_reconfigure_target(), set hstatus.VGEIN)
- Prepare new IMSIC VS-file (on top ofimsic_vsfile_acquire())
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file (APLIC retarget + sync)
- Save/clear old VS-file, restore, SGEI wake-up as today
Quoted 4 lines
A side benefit: attach no longer has to be squeezed into the finished migration code, so
imsic_vsfile_move()with itsfirst_attachargument and the gotoset_vgeingo away. Attach stays a plain sequence, andimsic_migrate_vcpu()simply reusesimsic_vsfile_acquire().
I'm fine with that order. It does mean that "add basic VGEIN management" and "map IMSIC interrupt file for vCPUs" introduce functions whose first user only comes with the attach patch a few patches later. If that's acceptable, I'll add a post-commit note to each of them saying where the functions get used (as IIUC, they will first introduced and then used in later patches).
Quoted 5 lines
Migrating a vCPU which has already run would still hit the trailing
BUG_ON("unimplemented")until the restore patch, but that is no worse than thepanic()it hits today.
~ Oleksii
Re: [PATCH v3 31/39] xen/riscv: remap interrupts to new IMSIC VS-file
Quoted 37 lines · Oleksii Kurochko
[ ... 52 lines skipped ... ]
Quoted 9 lines
At this point
vgein_assign()
is stillBUG_ON("unimplemented")too, so none of the new code can actually run, and the error path can't be reviewed against the realvgein_release().Also, until "introduce IMSIC h/w interrupt file attaching to vcpu", no vCPU ever owns a h/w file, so
imsic_migrate_vcpu()can only hit theCPU_NONEcheck.Quoted 7 lines
Wouldn't it be simpler to move the two already-acked patches and the attach first, so that no stubs are needed and each migration step is written against its real callees when it lands:
- Add basic VGEIN management for AIA guests (realvgein_assign()andvgein_release(); the reason for the explicit cpu argument ofvgein_release()would then have to go into this commit message)
- Map IMSIC interrupt file for vCPUs (realimsic_map_guest_file())I think I have such order before and it was changed becuase for locks in the first patch it wasn't enough context to understand if they are needed or not.
Quoted 9 lines
- Implement
continue_new_vcpu()and set hstatus.VSXL
- Attach a h/w interrupt file to a vCPU on its first run, introducingimsic_vsfile_acquire()andimsic_update_state()with their first user (acquire,vaplic_reconfigure_target(), set hstatus.VGEIN)
- Prepare new IMSIC VS-file (on top ofimsic_vsfile_acquire())
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file (APLIC retarget + sync)
- Save/clear old VS-file, restore, SGEI wake-up as todayQuoted 4 lines
A side benefit: attach no longer has to be squeezed into the finished migration code, so
imsic_vsfile_move()with itsfirst_attachargument and the gotoset_vgeingo away. Attach stays a plain sequence, andimsic_migrate_vcpu()simply reusesimsic_vsfile_acquire().
Quoted 7 lines
I'm fine with that order. It does mean that "add basic VGEIN management" and "map IMSIC interrupt file for vCPUs" introduce functions whose first user only comes with the attach patch a few patches later. If that's acceptable, I'll add a post-commit note to each of them saying where the functions get used (as IIUC, they will first introduced and then used in later patches).
Yes, the post-commit notes are fine with me. IMO it's better to introduce the real functions before their first caller, even if that caller only lands a few patches later, than to write callers against stubs. That way each migration step is written against its real callees, and the order follows how IMSIC actually works: a vCPU gets a file on its first run, and only then can that file be migrated.
The locks in the VGEIN patch would then get a real user a few patches later (the attach patch), and that patch's commit message can explain why they are needed.
What do you think of something like this, after "introduce vaplic_reconfigure_target()"?
- Set the guest's XLEN explicitly in hstatus.VSXL (moved before continue_new_vcpu(), with the vcpu_set_vsxl() call added to its stub, so that no commit enters a guest with an undefined VSXL)
- Implement continue_new_vcpu()
- Add basic VGEIN management for AIA guests (acked; the commit message would have to explain the explicit cpu argument of vgein_release())
- Map IMSIC interrupt file for vCPUs (acked)
- Attach a h/w interrupt file to a vCPU on its first run, introducing imsic_vsfile_acquire() and imsic_update_state() with their first user
=== migration
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file ("Prepare new IMSIC VS-file" folded in, as with imsic_vsfile_acquire() already there it is reduced to a single call)
- Save and clear the old IMSIC VS-file on vCPU migration
- Restore register state in the new IMSIC VS-file
- Wake up a descheduled vCPU on a guest external interrupt (last, as it arms HGEIE in imsic_migrate_vcpu() and clears it in imsic_vsfile_local_read_clear())
But I'm not an expert in patch series management, so I may be wrong.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 31/39] xen/riscv: remap interrupts to new IMSIC VS-file
Quoted 79 lines · Baptiste Le Duc
Quoted 37 lines · Oleksii Kurochko
[ ... 52 lines skipped ... ]
Quoted 9 lines
At this point
vgein_assign()
is stillBUG_ON("unimplemented")too, so none of the new code can actually run, and the error path can't be reviewed against the realvgein_release().Also, until "introduce IMSIC h/w interrupt file attaching to vcpu", no vCPU ever owns a h/w file, so
imsic_migrate_vcpu()can only hit theCPU_NONEcheck.Quoted 7 lines
Wouldn't it be simpler to move the two already-acked patches and the attach first, so that no stubs are needed and each migration step is written against its real callees when it lands:
- Add basic VGEIN management for AIA guests (realvgein_assign()andvgein_release(); the reason for the explicit cpu argument ofvgein_release()would then have to go into this commit message)
- Map IMSIC interrupt file for vCPUs (realimsic_map_guest_file())I think I have such order before and it was changed becuase for locks in the first patch it wasn't enough context to understand if they are needed or not.
Quoted 9 lines
- Implement
continue_new_vcpu()and set hstatus.VSXL
- Attach a h/w interrupt file to a vCPU on its first run, introducingimsic_vsfile_acquire()andimsic_update_state()with their first user (acquire,vaplic_reconfigure_target(), set hstatus.VGEIN)
- Prepare new IMSIC VS-file (on top ofimsic_vsfile_acquire())
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file (APLIC retarget + sync)
- Save/clear old VS-file, restore, SGEI wake-up as todayQuoted 4 lines
A side benefit: attach no longer has to be squeezed into the finished migration code, so
imsic_vsfile_move()with itsfirst_attachargument and the gotoset_vgeingo away. Attach stays a plain sequence, andimsic_migrate_vcpu()simply reusesimsic_vsfile_acquire().Quoted 7 lines
I'm fine with that order. It does mean that "add basic VGEIN management" and "map IMSIC interrupt file for vCPUs" introduce functions whose first user only comes with the attach patch a few patches later. If that's acceptable, I'll add a post-commit note to each of them saying where the functions get used (as IIUC, they will first introduced and then used in later patches).
Yes, the post-commit notes are fine with me. IMO it's better to introduce the real functions before their first caller, even if that caller only lands a few patches later, than to write callers against stubs. That way each migration step is written against its real callees, and the order follows how IMSIC actually works: a vCPU gets a file on its first run, and only then can that file be migrated.
The locks in the VGEIN patch would then get a real user a few patches later (the attach patch), and that patch's commit message can explain why they are needed.
What do you think of something like this, after "introduce
vaplic_reconfigure_target()"?- Set the guest's XLEN explicitly in hstatus.VSXL (moved before
continue_new_vcpu(), with thevcpu_set_vsxl()call added to its stub, so that no commit enters a guest with an undefined VSXL)
- Implementcontinue_new_vcpu()
- Add basic VGEIN management for AIA guests (acked; the commit message would have to explain the explicit cpu argument ofvgein_release())
- Map IMSIC interrupt file for vCPUs (acked)
- Attach a h/w interrupt file to a vCPU on its first run, introducingimsic_vsfile_acquire()andimsic_update_state()with their first user
=== migration
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file ("Prepare new IMSIC VS-file" folded in, as withimsic_vsfile_acquire()already there it is reduced to a single call)
- Save and clear the old IMSIC VS-file on vCPU migration
- Restore register state in the new IMSIC VS-file
- Wake up a descheduled vCPU on a guest external interrupt (last, as it arms HGEIE inimsic_migrate_vcpu()and clears it inimsic_vsfile_local_read_clear())
Quoted 1 line
But I'm not an expert in patch series management, so I may be wrong.
Let's see how it works. I'll try to reshuffle the patches as suggested.
Thanks for the suggestion!
~ Oleksii
Re: [PATCH v3 31/39] xen/riscv: remap interrupts to new IMSIC VS-file
Quoted 62 lines · Oleksii Kurochko
Quoted 59 lines · Baptiste Le Duc
Quoted 37 lines · Oleksii Kurochko
[ ... 52 lines skipped ... ]
Quoted 9 lines
At this point
vgein_assign()
is stillBUG_ON("unimplemented")too, so none of the new code can actually run, and the error path can't be reviewed against the realvgein_release().Also, until "introduce IMSIC h/w interrupt file attaching to vcpu", no vCPU ever owns a h/w file, so
imsic_migrate_vcpu()can only hit theCPU_NONEcheck.Quoted 7 lines
Wouldn't it be simpler to move the two already-acked patches and the attach first, so that no stubs are needed and each migration step is written against its real callees when it lands:
- Add basic VGEIN management for AIA guests (realvgein_assign()andvgein_release(); the reason for the explicit cpu argument ofvgein_release()would then have to go into this commit message)
- Map IMSIC interrupt file for vCPUs (realimsic_map_guest_file())I think I have such order before and it was changed becuase for locks in the first patch it wasn't enough context to understand if they are needed or not.
Quoted 9 lines
- Implement
continue_new_vcpu()and set hstatus.VSXL
- Attach a h/w interrupt file to a vCPU on its first run, introducingimsic_vsfile_acquire()andimsic_update_state()with their first user (acquire,vaplic_reconfigure_target(), set hstatus.VGEIN)
- Prepare new IMSIC VS-file (on top ofimsic_vsfile_acquire())
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file (APLIC retarget + sync)
- Save/clear old VS-file, restore, SGEI wake-up as todayQuoted 4 lines
A side benefit: attach no longer has to be squeezed into the finished migration code, so
imsic_vsfile_move()with itsfirst_attachargument and the gotoset_vgeingo away. Attach stays a plain sequence, andimsic_migrate_vcpu()simply reusesimsic_vsfile_acquire().Quoted 7 lines
I'm fine with that order. It does mean that "add basic VGEIN management" and "map IMSIC interrupt file for vCPUs" introduce functions whose first user only comes with the attach patch a few patches later. If that's acceptable, I'll add a post-commit note to each of them saying where the functions get used (as IIUC, they will first introduced and then used in later patches).
Yes, the post-commit notes are fine with me. IMO it's better to introduce the real functions before their first caller, even if that caller only lands a few patches later, than to write callers against stubs. That way each migration step is written against its real callees, and the order follows how IMSIC actually works: a vCPU gets a file on its first run, and only then can that file be migrated.
The locks in the VGEIN patch would then get a real user a few patches later (the attach patch), and that patch's commit message can explain why they are needed.
What do you think of something like this, after "introduce
vaplic_reconfigure_target()"?
Quoted 6 lines
Quoted 6 lines
- Set the guest's XLEN explicitly in hstatus.VSXL (moved before
continue_new_vcpu(), with thevcpu_set_vsxl()call added to its stub, so that no commit enters a guest with an undefined VSXL)
- Implementcontinue_new_vcpu()
- Add basic VGEIN management for AIA guests (acked; the commit message would have to explain the explicit cpu argument ofvgein_release())
Would you be okay with such explanation:
vgein_release() takes the pCPU the VGEIN belongs to explicitly rather than relying on v->processor, as the two may differ: a vCPU moved to another pCPU has to release the VGEIN of the pCPU it leaves, while v->processor already names the new one. As the bitmap of a pCPU may thus be changed from another pCPU, it is protected by a lock.
I tried to write in the way as it is requirement to API rather than a description of the code which will be introduced later somewhere in "=== migration"
~ Oleksii
Quoted 19 lines
Quoted 14 lines
- Map IMSIC interrupt file for vCPUs (acked)
- Attach a h/w interrupt file to a vCPU on its first run, introducingimsic_vsfile_acquire()andimsic_update_state()with their first user === migration
- Implement APLIC-IMSIC-hart sync barrier for vCPU migration
- Remap interrupts to the new IMSIC VS-file ("Prepare new IMSIC VS-file" folded in, as withimsic_vsfile_acquire()already there it is reduced to a single call)
- Save and clear the old IMSIC VS-file on vCPU migration
- Restore register state in the new IMSIC VS-file
- Wake up a descheduled vCPU on a guest external interrupt (last, as it arms HGEIE inimsic_migrate_vcpu()and clears it inimsic_vsfile_local_read_clear())Quoted 1 line
But I'm not an expert in patch series management, so I may be wrong.
Let's see how it works. I'll try to reshuffle the patches as suggested.
Thanks for the suggestion!
Re: [PATCH v3 31/39] xen/riscv: remap interrupts to new IMSIC VS-file
Quoted 9 lines · Oleksii Kurochko
Would you be okay with such explanation:
vgein_release()takes the pCPU the VGEIN belongs to explicitly rather than relying onv->processor, as the two may differ: a vCPU moved to another pCPU has to release the VGEIN of the pCPU it leaves, whilev->processoralready names the new one. As the bitmap of a pCPU may thus be changed from another pCPU, it is protected by a lock.I tried to write in the way as it is requirement to API rather than a description of the code which will be introduced later somewhere in "=== migration"
LGTM!
Quoted 1 line
~ Oleksii
PATCH v3 32/39 xen/riscv: save and clear the old IMSIC VS-file on vCPU migration
In reply to Oleksii KurochkoWhen a vCPU migrates to another pCPU, its guest interrupt file has to move as well: the eidelivery, eithreshold and eip/eie state of the old IMSIC VS-file has to be carried over to the newly acquired one.
Once all interrupt producers have been retargeted to the new VS-file, so that nothing can reach the old one anymore, read the state of the old one and clear it at the same time. This has to be done on the pCPU owning the old VS-file, as a guest interrupt file is only accessible through vsiselect/vsireg with hstatus.VGEIN selecting it. After that the old VS-file is no longer in use and its guest external interrupt number can be released.
The state is saved into struct imsic_mrif. Its eix[] array follows the layout of a memory-resident interrupt file (MRIF) as defined by the AIA
specification: pairs of 64-bit interrupt-pending and interrupt-enable doublewords, each pair covering 64 interrupt identities. eithreshold and eidelivery aren't part of an MRIF and are stored next to it as they belong to the interrupt file state too. Using the MRIF layout allows the same structure to hold the interrupt file of a vCPU which doesn't own a guest interrupt file, which is how the AIA specification describes MRIFs being used by a hypervisor.
Here the structure is only a temporary copy of a guest interrupt file:
it is filled and consumed by Xen alone and its address is never given to an IOMMU, so no MSI is ever recorded into it. Hence plain (non-atomic) accesses are sufficient and it can live on the stack.
The saved state still has to be restored into the new VS-file, so keep the BUG_ON("unimplemented") placeholder at the end of imsic_migrate_vcpu() to avoid silently losing the guest's interrupt state.
---
Changes in v3:
- Reword the commit message: explain why the MRIF layout is used and why the copy isn't an MSI target, drop references to later patches.
- Compare vsfile_cpu against CPU_NONE instead of NR_CPUS.
- Introduce imsic_switchcase_ret() here, together with its first user, drop its unused variadic form and leave the trailing semicolon to its users.
- Introduce the mrif member of struct imsic_vsfile_data here, together with its first user.
- Initialise vsfile_data.mrif in imsic_migrate_vcpu() with a compound literal instead of adding a separate tmrif local variable, so the MRIF the old interrupt file is dumped into is visibly the one later restored from.
- Drop the redundant parentheses around the arguments of imsic_vs_csr_swap() and its unnecessary local variable.
- Use plain 64 instead of BITS_PER_TYPE(uint64_t) for the number of interrupt identities covered by an EIx group, and explain in the comment ahead of IMSIC_MAX_EIX where it comes from (the MRIF layout from the AIA spec).
- Keep each EIx group of the MRIF as a uint64_t and introduce imsic_eix_swap64() to swap a whole group, which lets the #ifdef CONFIG_RISCV_32 in imsic_vsfile_local_read_clear() go.
- Add missing break after ASSERT_UNREACHABLE() in the default case of imsic_eix_swap().
- Use MASK_INSR() to set HSTATUS.VGEIN in imsic_vsfile_local_read_clear().
- Explain in imsic_vsfile_local_read_clear() why hstatus and vsiselect are saved and restored.
- Reword the comment on why no atomic accessors are needed to store the values into the MRIF.
- Take the number of EIx groups from vsfile_data.nr_eix, as the nr_hw_eix local variable is gone from imsic_migrate_vcpu().
- Move the vgein_release() stub to "xen/riscv: remap interrupts to new IMSIC VS-file", where it gains its first user.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/imsic.c | 124 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+)
xen/arch/riscv/imsic.c +124 -0
@@ -57,6 +57,28 @@ static unsigned int __ro_after_init guest_num_msis;*/ #define GUEST_IMSIC_MAX_MSIS 255U +/* +* The interrupt identities an IMSIC interrupt file provides are 0 (which is +* never valid, but still occupies a bit) up to IMSIC_MAX_ID inclusive, so +* IMSIC_MAX_ID + 1 bits have to be covered. +* +* Each EIx group covers 64 identities, matching the memory-resident interrupt +* file (MRIF) layout from the AIA spec, where pending and enable bits are +* stored as pairs of 64-bit doublewords. +*/ +#define IMSIC_MAX_EIX DIV_ROUND_UP(IMSIC_MAX_ID + 1, 64) + +struct imsic_mrif_eix { +uint64_t eip; +uint64_t eie; +}; + +struct imsic_mrif { +struct imsic_mrif_eix eix[IMSIC_MAX_EIX ]; +unsigned long eithreshold; +unsigned long eidelivery; +}; +#define IMSIC_DISABLE_EIDELIVERY 0#define IMSIC_ENABLE_EIDELIVERY 1#define IMSIC_DISABLE_EITHRESHOLD 1
@@ -86,6 +108,12 @@ do { \csr_clear (CSR_SIREG , v); \ } while (0) +#define imsic_vs_csr_swap (c, v) \ +({ \ + csr_write(CSR_VSISELECT, c); \ + csr_swap(CSR_VSIREG, v); \ +}) +#define imsic_vs_csr_write (c, v) \ do { \ csr_write(CSR_VSISELECT, c); \
@@ -95,12 +123,17 @@ do { \/* * Leaf macros for the switchcase expansion pyramid below: * imsic_switchcase_break(ireg, op, v) - emit "case ireg: op(ireg, v); break" +* imsic_switchcase_ret(ireg, op, v) - emit "case ireg: return op(ireg, v)" */ #define imsic_switchcase_break (ireg, op, v) \ case ireg: \ op(ireg, v); \ break +#define imsic_switchcase_ret (ireg, op, v) \ + case ireg: \ + return op(ireg, v) +/* * Generic switchcase expansion pyramid. * F is the per-operation leaf macro, ireg is the base register index.
@@ -126,6 +159,22 @@ do { \imsic_switchcase_32 (F , (ireg) +0 , __VA_ARGS__); \ imsic_switchcase_32(F, (ireg) + 32, __VA_ARGS__) +static unsigned long imsic_eix_swap (unsigned int ireg,unsigned long val) +{ +switch ( ireg ) + { +imsic_switchcase_64 (imsic_switchcase_ret,IMSIC_EIP0 , + imsic_vs_csr_swap, val); +imsic_switchcase_64 (imsic_switchcase_ret,IMSIC_EIE0 , + imsic_vs_csr_swap, val); +default : +ASSERT_UNREACHABLE (); +break ; + } + +return 0 ; +} +static void imsic_eix_write (unsigned int ireg,unsigned long val) { switch ( ireg )
@@ -152,6 +201,17 @@ static void imsic_eix_write64(unsigned int ireg, uint64_t val)imsic_eix_write (ireg +1 , val >>32 ); } +/* Swap a whole 64-bit EIx group, laid out as described above. */ +static uint64_t imsic_eix_swap64 (unsigned int ireg,uint64_t val) +{ +uint64_t old =imsic_eix_swap (ireg, val); + +if (IS_ENABLED (CONFIG_RISCV_32 ) ) + old |= (uint64_t )imsic_eix_swap (ireg +1 , val >>32 ) <<32 ; + +return old; +} +unsigned int vcpu_guest_file_id (const struct vcpu *v) {return ACCESS_ONCE (v->arch.vimsic_state->guest_file_id);
@@ -514,6 +574,8 @@ struct imsic_vsfile_data {unsigned int hgei;/* Number of 64-bit EIx groups to cover, see imsic_cfg.nr_eix. */ unsigned int nr_eix; +/* Where the interrupt file state is saved to or restored from. */ +struct imsic_mrif *mrif; };/*
@@ -598,6 +660,60 @@ static void cf_check imsic_vsfile_local_clear(void *data)csr_write (CSR_VSISELECT , old_vsiselect); } +static void cf_check imsic_vsfile_local_read_clear(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +struct imsic_mrif *mrif = idata->mrif; +unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* +* The file is reached by retargeting hstatus.VGEIN, and +* imsic_vs_csr_swap() alters vsiselect. Both belong to the vCPU running +* on this pCPU, so restore them before returning. +*/ + old_vsiselect =csr_read (CSR_VSISELECT ); + old_hstatus =csr_read (CSR_HSTATUS ); + new_hstatus = old_hstatus & ~HSTATUS_VGEIN ; + new_hstatus |=MASK_INSR (idata->hgei,HSTATUS_VGEIN ); +csr_write (CSR_HSTATUS , new_hstatus); + +/* +* No atomic accessors are needed to store the values into the MRIF here, +* as imsic_vsfile_read_clear() is always called with a pointer to a +* temporary MRIF on the stack. +*/ + + mrif->eidelivery =imsic_vs_csr_swap (IMSIC_EIDELIVERY ,0 ); + mrif->eithreshold =imsic_vs_csr_swap (IMSIC_EITHRESHOLD ,0 ); +for ( i =0 ; i < idata->nr_eix; i++ ) + { + mrif->eix[i].eip =imsic_eix_swap64 (IMSIC_EIP0 + i *2 ,0 ); + mrif->eix[i].eie =imsic_eix_swap64 (IMSIC_EIE0 + i *2 ,0 ); + } + +csr_write (CSR_HSTATUS , old_hstatus); +csr_write (CSR_VSISELECT , old_vsiselect); +} + +static void imsic_vsfile_read_clear (unsigned int vsfile_id, +unsigned int vsfile_cpu, +unsigned int nr_eix, +struct imsic_mrif *mrif) +{ +struct imsic_vsfile_data idata = { + .hgei = vsfile_id, + .nr_eix = nr_eix, + .mrif = mrif, + }; + +/* We can only read clear if we have an IMSIC VS-file */ +if ( vsfile_cpu ==CPU_NONE || !vsfile_id ) +return ; + +imsic_call_on_cpu (vsfile_cpu, imsic_vsfile_local_read_clear, &idata); +} + void cf_check vcpu_imsic_deinit(struct vcpu *v) {XVFREE (v->arch.vimsic_state);
@@ -960,6 +1076,7 @@ void imsic_migrate_vcpu(struct vcpu *v) {struct imsic_vsfile_data vsfile_data = { .nr_eix = imsic_cfg.nr_eix, + .mrif = &(struct imsic_mrif ){ }, };struct vimsic_state *imsic_state = v->arch.vimsic_state;unsigned long flags;
@@ -1028,5 +1145,12 @@ void imsic_migrate_vcpu(struct vcpu *v)* to the new IMSIC VS-file. */ +/* Read and clear register state from old IMSIC VS-file */ +imsic_vsfile_read_clear (old_vsfile_id, old_vsfile_cpu, vsfile_data.nr_eix, + vsfile_data.mrif); + +/* Free-up old IMSIC VS-file */ +vgein_release (v, old_vsfile_id, old_vsfile_cpu); +BUG_ON ("unimplemented" ); }
Signature · 1 line
2.55.0
Re: [PATCH v3 32/39] xen/riscv: save and clear the old IMSIC VS-file on vCPU migration
Quoted 12 lines
When a vCPU migrates to another pCPU, its guest interrupt file has to move as well: the eidelivery, eithreshold and eip/eie state of the old IMSIC VS-file has to be carried over to the newly acquired one.
Once all interrupt producers have been retargeted to the new VS-file, so that nothing can reach the old one anymore, read the state of the old one and clear it at the same time. This has to be done on the pCPU owning the old VS-file, as a guest interrupt file is only accessible through vsiselect/vsireg with hstatus.VGEIN selecting it. After that the old VS-file is no longer in use and its guest external interrupt number can be released.
Quoted 9 lines
The state is saved into struct
imsic_mrif. Its eix[] array follows the layout of a memory-resident interrupt file (MRIF) as defined by the AIA
specification: pairs of 64-bit interrupt-pending and interrupt-enable doublewords, each pair covering 64 interrupt identities. eithreshold and eidelivery aren't part of an MRIF and are stored next to it as they belong to the interrupt file state too. Using the MRIF layout allows the same structure to hold the interrupt file of a vCPU which doesn't own a guest interrupt file, which is how the AIA specification describes MRIFs being used by a hypervisor.
I think it's worth mentioning that this MRIF structure could be reused for software interrupt files later.
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_clear(void *data) · line 674
Quoted 13 lines
[ ... 172 lines skipped ... ]
xen/arch/riscv/imsic.c +12
-0+static void cf_check imsic_vsfile_local_read_clear(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +struct imsic_mrif *mrif = idata->mrif; +unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* +* The file is reached by retargeting hstatus.VGEIN, and +* imsic_vs_csr_swap() alters vsiselect. Both belong to the vCPU running +* on this pCPU, so restore them before returning. +*/
You're saving the registers here, not restoring them. I'd use something like:
/*
* vsiselect/vsireg only reach the guest interrupt file selected by
* hstatus.VGEIN, so point VGEIN at this file for the access.
* hstatus and vsiselect hold the state of the vCPU running on this
* pCPU; save them here and restore them before returning. */
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_clear(void *data) · line 685
Quoted 11 lines
xen/arch/riscv/imsic.c +11
-0+ old_vsiselect =csr_read (CSR_VSISELECT ); + old_hstatus =csr_read (CSR_HSTATUS ); + new_hstatus = old_hstatus & ~HSTATUS_VGEIN ; + new_hstatus |=MASK_INSR (idata->hgei,HSTATUS_VGEIN ); +csr_write (CSR_HSTATUS , new_hstatus); + +/* +* No atomic accessors are needed to store the values into the MRIF here, +* as imsic_vsfile_read_clear() is always called with a pointer to a +* temporary MRIF on the stack. +*/
Nit: is this blank line really needed?
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 32/39] xen/riscv: save and clear the old IMSIC VS-file on vCPU migration
Quoted 22 lines · Baptiste Le Duc
Quoted 12 lines
When a vCPU migrates to another pCPU, its guest interrupt file has to move as well: the eidelivery, eithreshold and eip/eie state of the old IMSIC VS-file has to be carried over to the newly acquired one.
Once all interrupt producers have been retargeted to the new VS-file, so that nothing can reach the old one anymore, read the state of the old one and clear it at the same time. This has to be done on the pCPU owning the old VS-file, as a guest interrupt file is only accessible through vsiselect/vsireg with hstatus.VGEIN selecting it. After that the old VS-file is no longer in use and its guest external interrupt number can be released.
Quoted 9 lines
The state is saved into struct
imsic_mrif. Its eix[] array follows the layout of a memory-resident interrupt file (MRIF) as defined by the AIA
specification: pairs of 64-bit interrupt-pending and interrupt-enable doublewords, each pair covering 64 interrupt identities. eithreshold and eidelivery aren't part of an MRIF and are stored next to it as they belong to the interrupt file state too. Using the MRIF layout allows the same structure to hold the interrupt file of a vCPU which doesn't own a guest interrupt file, which is how the AIA specification describes MRIFs being used by a hypervisor.
Quoted 2 lines
I think it's worth mentioning that this MRIF structure could be reused for software interrupt files later.
I'm fine with making that explicit. I tried to cover it with "... a vCPU which doesn't own a guest interrupt file ...", and adding "hardware" should make it clearer: "... a vCPU which doesn't own a hardware guest interrupt file ...". I think that covers your suggestion as well, so I'll add it.
To be honest, I'm a bit unsure how much to say about software interrupt files at this point: some earlier commit messages mentioned them, and I was asked to drop that, as they are neither introduced nor used yet. That's why I'd rather keep the wording implicit as above, but if you
still prefer them to be named explicitly, I can add a sentence.
At this point, I'm honestly getting a bit confused about how commit messages are supposed to be written.
Quoted 6 lines
Quoted 6 lines
[ ... 172 lines skipped ... ]
xen/arch/riscv/imsic.c +5
-0+static void cf_check imsic_vsfile_local_read_clear(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +struct imsic_mrif *mrif = idata->mrif;
Quoted 8 lines
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_clear(void *data) · line 674
Quoted 7 lines
xen/arch/riscv/imsic.c +7
-0+unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* +* The file is reached by retargeting hstatus.VGEIN, and +* imsic_vs_csr_swap() alters vsiselect. Both belong to the vCPU running +* on this pCPU, so restore them before returning. +*/ You're saving the registers here, not restoring them.
It was about CSR_HSTATUS, and CSR_VSISELECT which are stored and restored:
+ csr_write(CSR_HSTATUS, old_hstatus);
+ csr_write(CSR_VSISELECT, old_vsiselect);
Quoted 8 lines
I'd use something
like:
/*
* vsiselect/vsireg only reach the guest interrupt file selected by
* hstatus.VGEIN, so point VGEIN at this file for the access.
* hstatus and vsiselect hold the state of the vCPU running on this
* pCPU; save them here and restore them before returning. */
I agree that this option is clearer.
Also, as the same switch to a guest interrupt file is done in
three more places (in the prev patches), I'll factor it out into imsic_vsfile_select() and imsic_vsfile_deselect(), with your wording as the comment on them.
Quoted 12 lines
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_clear(void *data) · line 685
Quoted 12 lines
xen/arch/riscv/imsic.c +11
-0+ old_vsiselect =csr_read (CSR_VSISELECT ); + old_hstatus =csr_read (CSR_HSTATUS ); + new_hstatus = old_hstatus & ~HSTATUS_VGEIN ; + new_hstatus |=MASK_INSR (idata->hgei,HSTATUS_VGEIN ); +csr_write (CSR_HSTATUS , new_hstatus); + +/* +* No atomic accessors are needed to store the values into the MRIF here, +* as imsic_vsfile_read_clear() is always called with a pointer to a +* temporary MRIF on the stack. +*/
Quoted 2 lines
Nit: is this blank line really needed?
No real need. Lets drop it.
Thanks for review!
~ Oleksii
Re: [PATCH v3 32/39] xen/riscv: save and clear the old IMSIC VS-file on vCPU migration
Quoted 10 lines · Oleksii Kurochko
[ ... 25 lines skipped ... ]
Quoted 2 lines
I think it's worth mentioning that this MRIF structure could be reused for software interrupt files later.
I'm fine with making that explicit. I tried to cover it with "... a vCPU which doesn't own a guest interrupt file ...", and adding "hardware" should make it clearer: "... a vCPU which doesn't own a hardware guest interrupt file ...". I think that covers your suggestion as well, so I'll add it.
Quoted 5 lines
To be honest, I'm a bit unsure how much to say about software interrupt files at this point: some earlier commit messages mentioned them, and I was asked to drop that, as they are neither introduced nor used yet. That's why I'd rather keep the wording implicit as above, but if you still prefer them to be named explicitly, I can add a sentence.
Agreed, keep it implicit.
Quoted 3 lines
At this point, I'm honestly getting a bit confused about how commit messages are supposed to be written.
Sorry for the confusion. I'm generally not a fan of mentioning things which don't exist yet, but here the commit message explains why the MRIF layout is used, and the spec ties MRIFs closely to s/w interrupt files, so I wondered whether a short hint would help. The implicit wording is fine too.
I should have marked it as a nit. As I said before, your commit messages are great, and this was just my subjective opinion, possibly partly because I'm not a native English speaker. Feel free to discard such remarks whenever you don't find them accurate ;)
Quoted 7 lines
[ ... 49 lines skipped ... ]
Quoted 2 lines
Nit: is this blank line really needed?
No real need. Lets drop it.
Thanks for review!
You're wecome!
Signature · 1 line
Baptiste Le Duc <[email protected]>
PATCH v3 33/39 xen/riscv: restore register state in the new IMSIC VS-file
In reply to Oleksii KurochkoComplete the migration of a vCPU's guest interrupt file: move the state saved from the old IMSIC VS-file into the new one and make the vCPU use the new VS-file.
The interrupt producers have already been retargeted to the new VS-file, so MSIs may land in it before the saved state is restored. Hence the saved eip/eie bits are ORed into the new VS-file rather than written to it, so that such MSIs aren't lost. eithreshold and eidelivery are restored last: the new VS-file was cleared when it was acquired, so it doesn't deliver interrupts until its pending and enable bits are complete.
As with reading the old VS-file, this has to be done on the pCPU owning the new VS-file, with hstatus.VGEIN temporarily selecting it. That is v->processor, the pCPU the vCPU is migrating to.
Finally, point hstatus.VGEIN in the vCPU's saved context at the new VS-file, so that it becomes the source of the vCPU's VS-level external interrupts and the file the guest accesses through its siselect/sireg. With that the migration is complete, so drop the BUG_ON("unimplemented") placeholder from imsic_migrate_vcpu().
---
Changes in v3:
- Reword the commit message: explain why the saved state is ORed into the new VS-file and why eithreshold and eidelivery are restored last, drop the note about explanatory comments.
- Leave the trailing semicolon of the switchcase macros to their users.
- Drop the redundant parentheses around the arguments of imsic_vs_csr_set() as they are passed to csr_*() as whole expressions.
- Add missing break after ASSERT_UNREACHABLE() in the default case of imsic_eix_set().
- Restore the state on v->processor and take the id of the new IMSIC VS-file from vsfile_data.hgei, as imsic_migrate_vcpu() no longer has separate local variables for the new VS-file.
- Explain why hstatus and vsiselect are saved and restored with a short comment in imsic_vsfile_local_update().
- Introduce imsic_eix_set64() to set bits of a whole 64-bit EIx group, which lets the #ifdef CONFIG_RISCV_32 in imsic_vsfile_local_update() go.
- Move the MASK_INSR() conversion and the comment rewording in imsic_vsfile_local_read_clear() to the patch introducing that function.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/imsic.c | 80 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-)
xen/arch/riscv/imsic.c +78 -2
@@ -114,6 +114,12 @@ do { \csr_swap (CSR_VSIREG , v); \ }) +#define imsic_vs_csr_set (c, v) \ +do { \ + csr_write(CSR_VSISELECT, c); \ + csr_set(CSR_VSIREG, v); \ +} while (0) +#define imsic_vs_csr_write (c, v) \ do { \ csr_write(CSR_VSISELECT, c); \
@@ -212,6 +218,28 @@ static uint64_t imsic_eix_swap64(unsigned int ireg, uint64_t val)return old; } +static void imsic_eix_set (unsigned int ireg,unsigned long val) +{ +switch ( ireg ) + { +imsic_switchcase_64 (imsic_switchcase_break,IMSIC_EIP0 , + imsic_vs_csr_set, val); +imsic_switchcase_64 (imsic_switchcase_break,IMSIC_EIE0 , + imsic_vs_csr_set, val); +default : +ASSERT_UNREACHABLE (); +break ; + } +} + +/* Set bits of a whole 64-bit EIx group, laid out as described above. */ +static void imsic_eix_set64 (unsigned int ireg,uint64_t val) +{ +imsic_eix_set (ireg, val); +if (IS_ENABLED (CONFIG_RISCV_32 ) ) +imsic_eix_set (ireg +1 , val >>32 ); +} +unsigned int vcpu_guest_file_id (const struct vcpu *v) {return ACCESS_ONCE (v->arch.vimsic_state->guest_file_id);
@@ -1016,6 +1044,47 @@ int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo,return fdt_end_node (fdt); } +static void cf_check imsic_vsfile_local_update(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +struct imsic_mrif *mrif = idata->mrif; +unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* We can only update if we have a HW IMSIC context */ +if ( !idata->hgei ) +return ; + +/* +* No atomic accessors are needed to read the values out of the MRIF here, +* as this is always called with a pointer to a temporary MRIF on the +* stack. +*/ + +/* +* The file is reached by retargeting hstatus.VGEIN, and the +* imsic_vs_csr_*() accessors alter vsiselect. Both belong to the vCPU +* running on this pCPU, so restore them before returning. +*/ + old_vsiselect =csr_read (CSR_VSISELECT ); + old_hstatus =csr_read (CSR_HSTATUS ); + new_hstatus = old_hstatus & ~HSTATUS_VGEIN ; + new_hstatus |=MASK_INSR (idata->hgei,HSTATUS_VGEIN ); +csr_write (CSR_HSTATUS , new_hstatus); + +for ( i =0 ; i < idata->nr_eix; i++ ) + { +imsic_eix_set64 (IMSIC_EIP0 + i *2 , mrif->eix[i].eip); +imsic_eix_set64 (IMSIC_EIE0 + i *2 , mrif->eix[i].eie); + } + +imsic_vs_csr_write (IMSIC_EITHRESHOLD , mrif->eithreshold); +imsic_vs_csr_write (IMSIC_EIDELIVERY , mrif->eidelivery); + +csr_write (CSR_HSTATUS , old_hstatus); +csr_write (CSR_VSISELECT , old_vsiselect); +} +/* * Take a h/w guest interrupt file on the pCPU @v runs on and make it the * vCPU's one.
@@ -1142,7 +1211,8 @@ void imsic_migrate_vcpu(struct vcpu *v)/* * At this point, all interrupt producers have been moved -* to the new IMSIC VS-file. +* to the new IMSIC VS-file so we move register state from +* the old IMSIC VS/SW-file to the new IMSIC VS-file. */ /* Read and clear register state from old IMSIC VS-file */
@@ -1152,5 +1222,11 @@ void imsic_migrate_vcpu(struct vcpu *v)/* Free-up old IMSIC VS-file */ vgein_release (v, old_vsfile_id, old_vsfile_cpu); -BUG_ON ("unimplemented" ); +/* Restore register state in the new IMSIC VS-file */ +imsic_call_on_cpu (v->processor, imsic_vsfile_local_update, &vsfile_data); + +/* Set VCPU HSTATUS.VGEIN to new IMSIC VS-file */ +vcpu_guest_cpu_user_regs (v)->hstatus &= ~HSTATUS_VGEIN ; +vcpu_guest_cpu_user_regs (v)->hstatus |= +MASK_INSR (vsfile_data.hgei,HSTATUS_VGEIN ); }
Signature · 1 line
2.55.0
Re: [PATCH v3 33/39] xen/riscv: restore register state in the new IMSIC VS-file
Quoted 6 lines
Complete the migration of a vCPU's guest interrupt file: move the state saved from the old IMSIC VS-file into the new one and make the vCPU use the new VS-file.
The interrupt producers have already been retargeted to the new VS-file, so MSIs may land in it before the saved state is restored.
Could we send an extempore MSI to the hart of the new file once the interrupt producers have been retargeted, and spin until it is received? That would guarantee that no more MSIs land once we have received it.
Quoted 6 lines
Hence the saved eip/eie bits are ORed into the new VS-file rather than written to it, so that such MSIs aren't lost. eithreshold and eidelivery are restored last: the new VS-file was cleared when it was acquired, so it doesn't deliver interrupts until its pending and enable bits are complete.
Quoted 8 lines
As with reading the old VS-file, this has to be done on the pCPU owning the new VS-file, with hstatus.VGEIN temporarily selecting it. That is
v->processor, the pCPU the vCPU is migrating to.Finally, point hstatus.VGEIN in the vCPU's saved context at the new VS-file, so that it becomes the source of the vCPU's VS-level external interrupts and the file the guest accesses through its siselect/sireg. With that the migration is complete, so drop the
BUG_ON("unimplemented")
Great!
xen/arch/riscv/imsic.c int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo, · line 1051
Quoted 6 lines
[ ... 82 lines skipped ... ]
xen/arch/riscv/imsic.c +5
-0+static void cf_check imsic_vsfile_local_update(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +struct imsic_mrif *mrif = idata->mrif;
mrif is only read here, so it can be const:
const struct imsic_mrif *mrif = idata->mrif;
Quoted 7 lines
xen/arch/riscv/imsic.c +7
-0+unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* We can only update if we have a HW IMSIC context */ +if ( !idata->hgei ) +return ; + +/*
xen/arch/riscv/imsic.c int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo, · line 1068
Quoted 10 lines
xen/arch/riscv/imsic.c +10
-0+* No atomic accessors are needed to read the values out of the MRIF here, +* as this is always called with a pointer to a temporary MRIF on the +* stack. +*/ + +/* +* The file is reached by retargeting hstatus.VGEIN, and the +* imsic_vs_csr_*() accessors alter vsiselect. Both belong to the vCPU +* running on this pCPU, so restore them before returning. +*/
Same comment as on the previous patch: you are not restoring the CSRs here but saving them, as imsic_vs_csr_*() will alter vsiselect. Please update the comment accordingly.
xen/arch/riscv/imsic.c void imsic_migrate_vcpu(struct vcpu *v) · line 1228
Quoted 10 lines
[ ... 32 lines skipped ... ]
xen/arch/riscv/imsic.c +4
-1@@ -1152,5 +1222,11 @@ void imsic_migrate_vcpu(struct vcpu *v)/* Free-up old IMSIC VS-file */ vgein_release (v, old_vsfile_id, old_vsfile_cpu); -BUG_ON ("unimplemented" ); +/* Restore register state in the new IMSIC VS-file */ +imsic_call_on_cpu (v->processor, imsic_vsfile_local_update, &vsfile_data); + +/* Set VCPU HSTATUS.VGEIN to new IMSIC VS-file */
Nit: s/VCPU/vCPU/
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 33/39] xen/riscv: restore register state in the new IMSIC VS-file
Quoted 10 lines · Baptiste Le Duc
Quoted 6 lines
Complete the migration of a vCPU's guest interrupt file: move the state saved from the old IMSIC VS-file into the new one and make the vCPU use the new VS-file.
The interrupt producers have already been retargeted to the new VS-file, so MSIs may land in it before the saved state is restored.
Could we send an extempore MSI to the hart of the new file once the interrupt producers have been retargeted, and spin until it is received? That would guarantee that no more MSIs land once we have received it.
I don't think it's needed. The barrier is there so that nothing gets lost from the old file: once the extempore MSI sent to the hart of the old file has arrived, every MSI the APLIC sent towards the old file before the retargeting has landed there, so what's read out of the old file is complete.
MSIs sent after the retargeting go to the new file, and they can keep arriving at any time, including while the saved state is being restored, so an extempore MSI to the hart of the new file wouldn't stop them anyway: it would only tell that the MSIs sent before it have arrived. That's why the saved eip/eie bits are ORed into the new file rather than written to it, so that whatever lands there in the meantime is kept, and why eidelivery is restored last, so that the new file doesn't deliver anything until its state is complete.
Quoted 15 lines
Quoted 6 lines
Hence the saved eip/eie bits are ORed into the new VS-file rather than written to it, so that such MSIs aren't lost. eithreshold and eidelivery are restored last: the new VS-file was cleared when it was acquired, so it doesn't deliver interrupts until its pending and enable bits are complete.
Quoted 8 lines
As with reading the old VS-file, this has to be done on the pCPU owning the new VS-file, with hstatus.VGEIN temporarily selecting it. That is
v->processor, the pCPU the vCPU is migrating to.Finally, point hstatus.VGEIN in the vCPU's saved context at the new VS-file, so that it becomes the source of the vCPU's VS-level external interrupts and the file the guest accesses through its siselect/sireg. With that the migration is complete, so drop the
BUG_ON("unimplemented")Great!
Quoted 8 lines
xen/arch/riscv/imsic.c int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo, · line 1051
Quoted 6 lines
[ ... 82 lines skipped ... ]
xen/arch/riscv/imsic.c +5
-0+static void cf_check imsic_vsfile_local_update(void *data) +{ +unsigned int i; +const struct imsic_vsfile_data *idata = data; +struct imsic_mrif *mrif = idata->mrif;mrif is only read here, so it can be const:
const struct imsic_mrif *mrif = idata->mrif;
Good point. I will apply that.
Quoted 12 lines
Quoted 12 lines
xen/arch/riscv/imsic.c +12
-0+unsigned long new_hstatus, old_hstatus, old_vsiselect; + +/* We can only update if we have a HW IMSIC context */ +if ( !idata->hgei ) +return ; + +/* +* No atomic accessors are needed to read the values out of the MRIF here, +* as this is always called with a pointer to a temporary MRIF on the +* stack. +*/ +
Quoted 8 lines
xen/arch/riscv/imsic.c int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo, · line 1068
Quoted 5 lines
xen/arch/riscv/imsic.c +5
-0+/* +* The file is reached by retargeting hstatus.VGEIN, and the +* imsic_vs_csr_*() accessors alter vsiselect. Both belong to the vCPU +* running on this pCPU, so restore them before returning. +*/ Same comment as on the previous patch: you are not restoring the CSRs here but saving them, as imsic_vs_csr_*() will alter vsiselect. Please update the comment accordingly.
Sure! As I mentioned in reply to prev. patch I think that it would be better to factor out the sequence of work with hstatus&vsiselect to separate helpers, put your comment suggestion above the helpers once.
Quoted 5 lines
Quoted 5 lines
[ ... 32 lines skipped ... ]
xen/arch/riscv/imsic.c +0
-0@@ -1152,5 +1222,11 @@ void imsic_migrate_vcpu(struct vcpu *v)/* Free-up old IMSIC VS-file */ vgein_release (v, old_vsfile_id, old_vsfile_cpu);
Quoted 7 lines
xen/arch/riscv/imsic.c void imsic_migrate_vcpu(struct vcpu *v) · line 1228
Quoted 5 lines
xen/arch/riscv/imsic.c +4
-1-BUG_ON ("unimplemented" ); +/* Restore register state in the new IMSIC VS-file */ +imsic_call_on_cpu (v->processor, imsic_vsfile_local_update, &vsfile_data); + +/* Set VCPU HSTATUS.VGEIN to new IMSIC VS-file */ Nit: s/VCPU/vCPU/
Acked.
Thanks for review.
~ Oleksii
Re: [PATCH v3 33/39] xen/riscv: restore register state in the new IMSIC VS-file
Quoted 7 lines · Oleksii Kurochko
[ ... 13 lines skipped ... ]
I don't think it's needed. The barrier is there so that nothing gets lost from the old file: once the extempore MSI sent to the hart of the old file has arrived, every MSI the APLIC sent towards the old file before the retargeting has landed there, so what's read out of the old file is complete.
Quoted 9 lines
MSIs sent after the retargeting go to the new file, and they can keep arriving at any time, including while the saved state is being restored, so an extempore MSI to the hart of the new file wouldn't stop them anyway: it would only tell that the MSIs sent before it have arrived. That's why the saved eip/eie bits are ORed into the new file rather than written to it, so that whatever lands there in the meantime is kept, and why eidelivery is restored last, so that the new file doesn't deliver anything until its state is complete.
Ok it's more clear thanks!
Signature · 1 line
Baptiste Le Duc <[email protected]>
PATCH v3 34/39 xen/riscv: add basic VGEIN management for AIA guests
In reply to Oleksii KurochkoIt was decided to add support for IMSIC from the start instead of having APLIC operate in direct delivery mode, as it requires a trap-and-emulation approach, which is not optimal from a performance standpoint.
AIA provides a hardware-accelerated mechanism for delivering external interrupts to domains via "guest interrupt files" located in IMSIC. A single physical hart can implement multiple such files (up to GEILEN), allowing several virtual harts to receive interrupts directly from hardware.
Introduce per-CPU tracking of guest interrupt file identifiers (VGEIN) for systems implementing AIA specification. Each CPU maintains a bitmap describing which guest interrupt files are currently in use.
Implement helpers to initialize the bitmap based on the number of available guest interrupt files (GEILEN), assign a VGEIN to a vCPU, and release it when no longer needed.
---
Also in the next patch there is other context to understand the usage of spinlock introduced here.
---
Changes in v3:
- Add Acked-by: Jan Beulich <[email protected]>.
- Add Reviewed-by: Baptiste Le Duc <[email protected]>.
---
Changes in v2:
- make vgein_init() pCPU agnostic as it is working with CSR which could be read only on local pCPU itself.
- Move introduction of vgein_ctrl->owners[] to separate patch.
- Add ASSERT() and re-init vgein->bmp with 0.
- Update the commit message (drop the last sentence as ->hstatus isn't filled anymore in in vgein_*() functions).
- Introduce vgein_deinit().
---
--- xen/arch/riscv/aia.c | 141 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 137 insertions(+), 4 deletions(-)
xen/arch/riscv/aia.c +137 -4
@@ -1,13 +1,31 @@/* SPDX-License-Identifier: GPL-2.0-only */ -#include <xen/bug.h> +#include <xen/bitops.h> +#include <xen/cpu.h> #include <xen/errno.h> #include <xen/init.h> #include <xen/sections.h> #include <xen/sched.h> +#include <xen/spinlock.h> #include <xen/types.h> +#include <asm/aia.h> #include <asm/cpufeature.h> +#include <asm/csr.h> +#include <asm/current.h> + +struct vgein_ctrl { +/* The least-significant bits are implemented first, apart from bit 0 */ +unsigned long bmp; +spinlock_t lock; +unsigned int geilen; +}; + +/* +* VGEIN control structure for each physical CPU to track which VS (guest) +* interrupt file IDs are in use. +*/ +static DEFINE_PER_CPU(struct vgein_ctrl, vgein);static bool __ro_after_init _aia_usable;
@@ -16,22 +34,137 @@ bool aia_usable(void)return _aia_usable; } +/* HGEIE is a per-hart CSR, so this has to run on the CPU being initialized. */ +static int vgein_init (void ) +{ +struct vgein_ctrl *vgein = &this_cpu (vgein); + +spin_lock_init (&vgein->lock); + +csr_write (CSR_HGEIE , ~0UL ); + vgein->geilen =flsl (csr_read (CSR_HGEIE ) >>1 ); +csr_write (CSR_HGEIE ,0 ); + + vgein->bmp =0 ; + +if ( !vgein->geilen ) +return -EOPNOTSUPP ; + +return 0 ; +} + +static void vgein_deinit (void ) +{ +csr_write (CSR_HGEIE ,0 ); +} + +static int cf_check cpu_callback(struct notifier_block *nfb, +unsigned long action,void *hcpu) +{ +unsigned int cpu = (unsigned long )hcpu; +int rc =0 ; + +switch ( action ) + { +case CPU_STARTING : + rc =vgein_init (); +if ( rc ) +printk (XENLOG_ERR "AIA: failed to init vgein for CPU%u: %d\n" , + cpu, rc); +break ; + +case CPU_DYING : +vgein_deinit (); +break ; + } + +return notifier_from_errno (rc); +} + +static struct notifier_block cpu_nfb = { + .notifier_call = cpu_callback, +}; +void __init aia_init (void ) { +int rc; +if ( !riscv_isa_extension_available (NULL , RISCV_ISA_EXT_ssaia) ) + { +dprintk (XENLOG_WARNING ,"SSAIA isn't present in riscv,isa\n" );return ; + } + +if ( (rc =vgein_init ()) ) + { +dprintk (XENLOG_ERR ,"vgein_init() failed: %d\n" , rc); +return ; + } _aia_usable = true; + +register_cpu_notifier (&cpu_nfb); }unsigned int vgein_assign (struct vcpu *v) { -BUG_ON ("unimplemented" ); +unsigned int vgein_id; +struct vgein_ctrl *vgein = &per_cpu (vgein, v->processor); +unsigned long *bmp = &vgein->bmp; +unsigned long flags; -return 0 ; +if ( !vgein->geilen ) +return 0 ; + +spin_lock_irqsave (&vgein->lock, flags); +/* +* The vgein_id shouldn't be zero, as it will indicate that no guest +* external interrupt source is selected for VS-level external interrupts +* according to RISC-V privileged spec: +* Hypervisor Status Register (hstatus) in RISC-V privileged spec: +* +* The VGEIN (Virtual Guest External Interrupt Number) field selects +* a guest external interrupt source for VS-level external interrupts. +* VGEIN is a WLRL field that must be able to hold values between zero +* and the maximum guest external interrupt number (known as GEILEN), +* inclusive. +* When VGEIN=0, no guest external interrupt source is selected for +* VS-level external interrupts. +* +* So start to search from bit number 1. +*/ + vgein_id =find_next_zero_bit (bmp, vgein->geilen +1 ,1 ); + +if ( vgein_id > vgein->geilen ) + vgein_id =0 ; +else +__set_bit (vgein_id, bmp); + +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: %pv: vgein_id(%u), xen_cpu%u_bmp=%#lx\n" , + __func__, v, vgein_id, v->processor, *bmp); +#endif + +return vgein_id; }void vgein_release (struct vcpu *v,unsigned int vgein_id,unsigned int cpu) { -BUG_ON ("unimplemented" ); +unsigned long flags; +struct vgein_ctrl *vgein = &per_cpu (vgein, cpu); + +if ( !vgein_id ) +return ; + +spin_lock_irqsave (&vgein->lock, flags); +if ( !__test_and_clear_bit (vgein_id, &vgein->bmp) ) +ASSERT_UNREACHABLE (); +spin_unlock_irqrestore (&vgein->lock, flags); + +#ifdef VGEIN_DEBUG +gprintk (XENLOG_DEBUG ,"%s: %pv: vgein_id(%u), xen_cpu%u_bmp=%#lx\n" , + __func__, v, vgein_id, cpu, vgein->bmp); +#endif }
Signature · 1 line
2.55.0
PATCH v3 35/39 xen/riscv: wake up a descheduled vCPU on a guest external interrupt
In reply to Oleksii KurochkoWhile a vCPU is running, MSIs written to its h/w IMSIC guest interrupt file are delivered straight to VS-mode. Once the vCPU is descheduled nobody observes that file anymore, so a guest blocked on such an interrupt would stay blocked until some unrelated event happens to schedule it again.
Let Xen observe the file in that window: on deschedule set the vCPU's bit in HGEIE, which turns an interrupt pending in its VS-file into an HS-level SGEI, and clear the bit again on schedule-in. HGEIP only reports an interrupt file number, so to find the vCPU to kick, keep a per-pCPU owners[] array indexed by file number and updated in vgein_assign()/vgein_release() together with the VGEIN bitmap. The SGEI handler clears the HGEIE bits it has seen before kicking their owners, so that the interrupt doesn't keep firing until the vCPU is switched in.
The same applies to a vCPU which is migrated to another pCPU while it isn't running. Its old interrupt file is released during the migration, and the vCPU isn't switched in on the new pCPU until something wakes it up, so nothing would observe the new interrupt file. Hence arm HGEIE for the new interrupt file on the new pCPU in imsic_migrate_vcpu(), and clear the bit of the old one in imsic_vsfile_local_read_clear(), which already runs on the pCPU owning it.
SGEIs are enabled per vCPU through v->arch.hie: vcpu_csr_init() sets IRQ_S_GEXT in it and csr_regs_ctxt_switch_to() loads it into the CSR.
vgein_release() is only called on vCPU migration: a vCPU going away also has to free its VGEIN slot and drop the owners[] entry, but there is no vCPU teardown path to hook it into yet.
---
Changes in v3:
- Drop the unrelated cpu_callback() change.
- Read HGEIP and HGEIE in separate statements (MISRA C Rule 13.2).
- Check that guest interrupt file ID 0 isn't reported once, ahead of the loop in hgei_interrupt(), instead of on every iteration.
- Drop the VGEIN_DEBUG print from hgei_interrupt().
- Explain in imsic_ctxt_switch_from() why setting the HGEIE bit last doesn't lose an interrupt.
- Initialise hie with BIT(IRQ_S_GEXT, UL) and explain why SGEIs are enabled.
- Arm HGEIE of the new interrupt file on v->processor, as the new_vsfile_cpu local variable is gone from imsic_migrate_vcpu().
- Describe in the commit message why HGEIE is armed for a vCPU which is migrated while it isn't running.
- Correct the commit message: vgein_release() is called on vCPU migration.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/aia.c | 38 ++++++++++++++++++ xen/arch/riscv/domain.c | 7 ++++ xen/arch/riscv/imsic.c | 62 ++++++++++++++++++++++++++++- xen/arch/riscv/include/asm/aia.h | 2 + xen/arch/riscv/include/asm/domain.h | 1 + xen/arch/riscv/traps.c | 7 +++- 6 files changed, 115 insertions(+), 2 deletions(-)
xen/arch/riscv/aia.c +38 -0
@@ -18,6 +18,13 @@ struct vgein_ctrl {/* The least-significant bits are implemented first, apart from bit 0 */ unsigned long bmp;spinlock_t lock; +/* +* Guest interrupt file IDs run from 1 to geilen inclusive (0 means that +* no guest external interrupt source is selected), and geilen can never +* exceed BITS_PER_LONG - 1, so indexing this array by the ID directly +* always fits. +*/ +struct vcpu *owners[BITS_PER_LONG ];unsigned int geilen; };
@@ -138,7 +145,10 @@ unsigned int vgein_assign(struct vcpu *v)if ( vgein_id > vgein->geilen ) vgein_id =0 ;else + {__set_bit (vgein_id, bmp); + vgein->owners[vgein_id] = v; + }spin_unlock_irqrestore (&vgein->lock, flags);
@@ -161,6 +171,7 @@ void vgein_release(struct vcpu *v, unsigned int vgein_id, unsigned int cpu)spin_lock_irqsave (&vgein->lock, flags);if ( !__test_and_clear_bit (vgein_id, &vgein->bmp) )ASSERT_UNREACHABLE (); + vgein->owners[vgein_id] =NULL ;spin_unlock_irqrestore (&vgein->lock, flags);#ifdef VGEIN_DEBUG
@@ -168,3 +179,30 @@ void vgein_release(struct vcpu *v, unsigned int vgein_id, unsigned int cpu) __func__, v, vgein_id, cpu, vgein->bmp);#endif } + +void hgei_interrupt (void ) +{ +unsigned long hgei_mask, flags; +struct vgein_ctrl *vgein = &this_cpu (vgein); + + hgei_mask =csr_read (CSR_HGEIP ); + hgei_mask &=csr_read (CSR_HGEIE ); + +csr_clear (CSR_HGEIE , hgei_mask); + +spin_lock_irqsave (&vgein->lock, flags); + +/* +* Bit 0 of HGEIP/HGEIE is read-only zero: guest interrupt file ID 0 +* means that no guest external interrupt source is selected. +*/ +ASSERT (!(hgei_mask &1 )); + + for_each_set_bit ( guest_file_id, hgei_mask ) + { +if ( vgein->owners[guest_file_id] ) +vcpu_kick (vgein->owners[guest_file_id]); + } + +spin_unlock_irqrestore (&vgein->lock, flags); +}
xen/arch/riscv/domain.c +7 -0
@@ -130,6 +130,12 @@ static void vcpu_csr_init(struct vcpu *v) v->arch.hstateen0 = (hstateen0 & csr_masks.hstateen0) | csr_masks.ro_one.hstateen0; } + +/* +* Enable SGEIs, so that a guest interrupt file marked in HGEIE while +* the vCPU is descheduled can raise an interrupt to Xen. +*/ + v->arch.hie =BIT (IRQ_S_GEXT ,UL ); }static void continue_new_vcpu (struct vcpu *prev)
@@ -386,6 +392,7 @@ static void csr_regs_ctxt_switch_to(struct vcpu *n)csr_write (CSR_HEDELEG , n->arch.hedeleg);csr_write (CSR_HIDELEG , n->arch.hideleg);csr_write (CSR_HVIP , n->arch.hvip); +csr_write (CSR_HIE , n->arch.hie);csr_write64 (CSR_HENVCFG , n->arch.henvcfg);csr_write (CSR_HCOUNTEREN , n->arch.hcounteren);csr_write64 (CSR_HTIMEDELTA , n->arch.htimedelta);
xen/arch/riscv/imsic.c +61 -1
91 lines not on this page · raw message
xen/arch/riscv/include/asm/aia.h +2 -0
@@ -12,4 +12,6 @@ void aia_init(void);unsigned int vgein_assign (struct vcpu *v);void vgein_release (struct vcpu *v,unsigned int vgein_id,unsigned int cpu); +void hgei_interrupt (void ); +#endif /* RISCV_AIA_H */
xen/arch/riscv/include/asm/domain.h +1 -0
@@ -69,6 +69,7 @@ struct arch_vcpu {register_t hstateen0;uint64_t htimedelta;register_t hvip; +register_t hie;register_t vsatp;register_t vscause;
xen/arch/riscv/traps.c +6 -1
@@ -12,9 +12,10 @@#include <xen/sched.h> #include <xen/softirq.h> -#include <asm/extable.h> +#include <asm/aia.h> #include <asm/cpufeature.h> #include <asm/emulate.h> +#include <asm/extable.h> #include <asm/intc.h> #include <asm/processor.h> #include <asm/riscv_encoding.h>
@@ -273,6 +274,10 @@ void do_trap(struct cpu_user_regs *cpu_regs)timer_interrupt ();break ; +case IRQ_S_GEXT : +hgei_interrupt (); +break ; +default : intr_handled = false;break ;
Signature · 1 line
2.55.0
Re: [PATCH v3 35/39] xen/riscv: wake up a descheduled vCPU on a guest external interrupt
Quoted 6 lines
While a vCPU is running, MSIs written to its h/w IMSIC guest interrupt file are delivered straight to VS-mode. Once the vCPU is descheduled nobody observes that file anymore, so a guest blocked on such an interrupt would stay blocked until some unrelated event happens to schedule it again.
Quoted 8 lines
Let Xen observe the file in that window: on deschedule set the vCPU's bit in HGEIE, which turns an interrupt pending in its VS-file into an HS-level SGEI, and clear the bit again on schedule-in. HGEIP only reports an interrupt file number, so to find the vCPU to kick, keep a per-pCPU owners[] array indexed by file number and updated in vgein_assign()/vgein_release() together with the VGEIN bitmap. The SGEI handler clears the HGEIE bits it has seen before kicking their owners, so that the interrupt doesn't keep firing until the vCPU is switched in.
Please explain why this is enough for a descheduled
vCPU to never miss a wake-up: an interrupt pending in a VS-file always shows up in HGEIP, but it reaches VS-mode only if hstatus.VGEIN selects that file, and traps to Xen as an SGEI only if the file's HGEIE bit is set. While the vCPU is descheduled, HGEIE is therefore Xen's only way to notice it. And as HGEIP reflects the file's current state rather than being latched, an interrupt which arrived before the bit got set still raises the SGEI once it is.
Quoted 5 lines
[ ... 49 lines skipped ... ]
xen/arch/riscv/aia.c struct vgein_ctrl { · line 27
Quoted 10 lines
xen/arch/riscv/aia.c +7
-0@@ -18,6 +18,13 @@ struct vgein_ctrl {/* The least-significant bits are implemented first, apart from bit 0 */ unsigned long bmp;spinlock_t lock; +/* +* Guest interrupt file IDs run from 1 to geilen inclusive (0 means that +* no guest external interrupt source is selected), and geilen can never +* exceed BITS_PER_LONG - 1, so indexing this array by the ID directly +* always fits. +*/ +struct vcpu *owners[BITS_PER_LONG ];
It looks like owners[] already carries the information bmp provides. Couldn't we drop bmp and use owners[] alone, with owners[i] == NULL meaning that file i is free? That would avoid having to keep the two in sync, and vgein_release() could then check ASSERT(owners[id] == v) rather than just that the bit was set. A linear scan over 1..geilen in vgein_assign() should be fine given geilen < BITS_PER_LONG.
If you agree, it would probably be cleaner to introduce owners[] in place of bmp in the earlier "add basic VGEIN management" patch, rather than adding bmp there and replacing it here.
Quoted 9 lines
[ ... 26 lines skipped ... ]
xen/arch/riscv/aia.c +8
-0+ +void hgei_interrupt (void ) +{ +unsigned long hgei_mask, flags; +struct vgein_ctrl *vgein = &this_cpu (vgein); + + hgei_mask =csr_read (CSR_HGEIP ); + hgei_mask &=csr_read (CSR_HGEIE );
xen/arch/riscv/aia.c void vgein_release(struct vcpu *v, unsigned int vgein_id, unsigned int cpu) · line 199
Quoted 10 lines
xen/arch/riscv/aia.c +10
-0+ +csr_clear (CSR_HGEIE , hgei_mask); + +spin_lock_irqsave (&vgein->lock, flags); + +/* +* Bit 0 of HGEIP/HGEIE is read-only zero: guest interrupt file ID 0 +* means that no guest external interrupt source is selected. +*/ +ASSERT (!(hgei_mask &1 ));
Is this ASSERT() really needed? Bit 0 of HGEIP/HGEIE is read-only zero per the spec, and vgein_init() already relies on that when computing geilen. Even if it were ever reported, owners[0] is never assigned, so the loop below would simply skip it.
xen/arch/riscv/imsic.c void cf_check imsic_ctxt_switch_from(struct vcpu *p) · line 561
Quoted 11 lines
[ ... 35 lines skipped ... ]
xen/arch/riscv/imsic.c +4
-0@@ -555,12 +555,37 @@ void cf_check imsic_ctxt_switch_from(struct vcpu *p)write_lock_irqsave (&imsic_state->vsfile_lock, flags); imsic_state->vsfile_cpu = p->processor; +/* +* Start to observe the VS-file from HS-mode: while the vCPU isn't +* running an interrupt pending in its VS-file is reported through HGEIP +* instead of being delivered to VS-mode, which lets Xen wake the vCPU up.
"instead of" reads as if HGEIP only reports it while the vCPU isn't running. It always does; what changes is that VGEIN no longer selects the file and the HGEIE bit set below turns it into an SGEI.
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_read_clear(void *data) · line 726
Quoted 10 lines
[ ... 29 lines skipped ... ]
intimsic_map_guest_file(struct vcpu *v, unsigned int vsfile_id)xen/arch/riscv/imsic.c +4
-0@@ -695,6 +720,12 @@ static void cf_check imsic_vsfile_local_read_clear(void *data)struct imsic_mrif *mrif = idata->mrif;unsigned long new_hstatus, old_hstatus, old_vsiselect; +/* +* The HGEIE bit imsic_ctxt_switch_from() armed belongs to the old owner +* only. +*/
"Old owner" is ambiguous: it reads like another vCPU, but it's the same vCPU on the file it is leaving. To mirror imsic_local_hgeie_set(), I'd rather have an imsic_local_hgeie_clear() called from here, documenting the pairing the same way, e.g.:
/*
* Stop observing an interrupt file which a vCPU leaves when it moves to
* another pCPU by clearing the bit imsic_ctxt_switch_from() set. *
* Counterpart of imsic_local_hgeie_set() for the new file. */
In that case ...
Quoted 6 lines
xen/arch/riscv/imsic.c +2
-0+csr_clear (CSR_HGEIE ,BIT (idata->hgei,UL )); +/* * The file is reached by retargeting hstatus.VGEIN, and * imsic_vs_csr_swap() alters vsiselect. Both belong to the vCPU running
xen/arch/riscv/imsic.c int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo, · line 1084
Quoted 10 lines
xen/arch/riscv/imsic.c +7
-0@@ -1044,6 +1075,21 @@ int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo,return fdt_end_node (fdt); } +/* +* Start to observe the interrupt file from HS-mode, the same way +* imsic_ctxt_switch_from() does it for a vCPU which is switched out. +* +* The counterpart, clearing the bit of the interrupt file which is left +* behind, is done by imsic_vsfile_local_read_clear(), which already runs on +* the pCPU owning that file.
... this wouldn't be needed at all.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 35/39] xen/riscv: wake up a descheduled vCPU on a guest external interrupt
Quoted 15 lines · Baptiste Le Duc
Quoted 6 lines
While a vCPU is running, MSIs written to its h/w IMSIC guest interrupt file are delivered straight to VS-mode. Once the vCPU is descheduled nobody observes that file anymore, so a guest blocked on such an interrupt would stay blocked until some unrelated event happens to schedule it again.
Quoted 8 lines
Let Xen observe the file in that window: on deschedule set the vCPU's bit in HGEIE, which turns an interrupt pending in its VS-file into an HS-level SGEI, and clear the bit again on schedule-in. HGEIP only reports an interrupt file number, so to find the vCPU to kick, keep a per-pCPU owners[] array indexed by file number and updated in vgein_assign()/vgein_release() together with the VGEIN bitmap. The SGEI handler clears the HGEIE bits it has seen before kicking their owners, so that the interrupt doesn't keep firing until the vCPU is switched in.
Quoted 8 lines
Please explain why this is enough for a descheduled
vCPU to never miss a wake-up: an interrupt pending in a VS-file always shows up in HGEIP, but it reaches VS-mode only if hstatus.VGEIN selects that file, and traps to Xen as an SGEI only if the file's HGEIE bit is set. While the vCPU is descheduled, HGEIE is therefore Xen's only way to notice it. And as HGEIP reflects the file's current state rather than being latched, an interrupt which arrived before the bit got set still raises the SGEI once it is.
Will add, thanks.
Quoted 23 lines
Quoted 5 lines
[ ... 49 lines skipped ... ]
xen/arch/riscv/aia.c struct vgein_ctrl { · line 27
Quoted 10 lines
xen/arch/riscv/aia.c +7
-0@@ -18,6 +18,13 @@ struct vgein_ctrl {/* The least-significant bits are implemented first, apart from bit 0 */ unsigned long bmp;spinlock_t lock; +/* +* Guest interrupt file IDs run from 1 to geilen inclusive (0 means that +* no guest external interrupt source is selected), and geilen can never +* exceed BITS_PER_LONG - 1, so indexing this array by the ID directly +* always fits. +*/ +struct vcpu *owners[BITS_PER_LONG ];It looks like owners[] already carries the information bmp provides. Couldn't we drop bmp and use owners[] alone, with owners[i] == NULL meaning that file i is free? That would avoid having to keep the two in sync, and
vgein_release()could then checkASSERT(owners[id] == v)rather than just that the bit was set. A linear scan over 1..geilen invgein_assign()should be fine given geilen <BITS_PER_LONG.
Quoted 3 lines
If you agree, it would probably be cleaner to introduce owners[] in place of bmp in the earlier "add basic VGEIN management" patch, rather than adding bmp there and replacing it here.
It would work, but I'd rather keep bmp: it's updated together with
owners[], under the same lock, in vgein_assign() and vgein_release()
only, so there isn't much to keep in sync. It also keeps the state of
all the files in a single word, so a free VGEIN ID is found with a
single find_next_zero_bit() rather than by iterating over owners[] (but I could agree that likely not a big win), and the debug output can print it as is.
The stronger check in vgein_release() is a good idea regardless, so I'll add ASSERT(vgein->owners[vgein_id] == v) here.
Quoted 16 lines
Quoted 16 lines
[ ... 26 lines skipped ... ]
xen/arch/riscv/aia.c +15
-0+ +void hgei_interrupt (void ) +{ +unsigned long hgei_mask, flags; +struct vgein_ctrl *vgein = &this_cpu (vgein); + + hgei_mask =csr_read (CSR_HGEIP ); + hgei_mask &=csr_read (CSR_HGEIE ); + +csr_clear (CSR_HGEIE , hgei_mask); + +spin_lock_irqsave (&vgein->lock, flags); + +/* +* Bit 0 of HGEIP/HGEIE is read-only zero: guest interrupt file ID 0
Quoted 8 lines
xen/arch/riscv/aia.c void vgein_release(struct vcpu *v, unsigned int vgein_id, unsigned int cpu) · line 199
Quoted 3 lines
xen/arch/riscv/aia.c +3
-0+* means that no guest external interrupt source is selected. +*/ +ASSERT (!(hgei_mask &1 ));Is this
ASSERT()really needed? Bit 0 of HGEIP/HGEIE is read-only zero per the spec, andvgein_init()already relies on that when computing geilen. Even if it were ever reported, owners[0] is never assigned, so the loop below would simply skip it.
Agreed, lets drop it.
Quoted 5 lines
Quoted 5 lines
[ ... 35 lines skipped ... ]
xen/arch/riscv/imsic.c +0
-0@@ -555,12 +555,37 @@ void cf_check imsic_ctxt_switch_from(struct vcpu *p)
Quoted 11 lines
xen/arch/riscv/imsic.c void cf_check imsic_ctxt_switch_from(struct vcpu *p) · line 561
Quoted 6 lines
xen/arch/riscv/imsic.c +4
-0write_lock_irqsave (&imsic_state->vsfile_lock, flags); imsic_state->vsfile_cpu = p->processor; +/* +* Start to observe the VS-file from HS-mode: while the vCPU isn't +* running an interrupt pending in its VS-file is reported through HGEIP +* instead of being delivered to VS-mode, which lets Xen wake the vCPU up. "instead of" reads as if HGEIP only reports it while the vCPU isn't running. It always does; what changes is that VGEIN no longer selects the file and the HGEIE bit set below turns it into an SGEI.
Right, I'll reword it accordingly.
Quoted 32 lines
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_read_clear(void *data) · line 726
Quoted 10 lines
[ ... 29 lines skipped ... ]
intimsic_map_guest_file(struct vcpu *v, unsigned int vsfile_id)xen/arch/riscv/imsic.c +4
-0@@ -695,6 +720,12 @@ static void cf_check imsic_vsfile_local_read_clear(void *data)struct imsic_mrif *mrif = idata->mrif;unsigned long new_hstatus, old_hstatus, old_vsiselect; +/* +* The HGEIE bit imsic_ctxt_switch_from() armed belongs to the old owner +* only. +*/ "Old owner" is ambiguous: it reads like another vCPU, but it's the same vCPU on the file it is leaving. To mirror
imsic_local_hgeie_set(), I'd rather have animsic_local_hgeie_clear()called from here, documenting the pairing the same way, e.g.:/*
* Stop observing an interrupt file which a vCPU leaves when it moves to
* another pCPU by clearing the bitimsic_ctxt_switch_from()set. *
* Counterpart ofimsic_local_hgeie_set()for the new file. */In that case ...
Quoted 9 lines
xen/arch/riscv/imsic.c +2
-0+csr_clear (CSR_HGEIE ,BIT (idata->hgei,UL )); +/* * The file is reached by retargeting hstatus.VGEIN, and * imsic_vs_csr_swap() alters vsiselect. Both belong to the vCPU running @@ -1044,6 +1075,21 @@ int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo,return fdt_end_node (fdt); }
Quoted 9 lines
xen/arch/riscv/imsic.c int __init vimsic_make_domu_dt_node(struct kernel_info *kinfo, · line 1084
Quoted 7 lines
xen/arch/riscv/imsic.c +7
-0+/* +* Start to observe the interrupt file from HS-mode, the same way +* imsic_ctxt_switch_from() does it for a vCPU which is switched out. +* +* The counterpart, clearing the bit of the interrupt file which is left +* behind, is done by imsic_vsfile_local_read_clear(), which already runs on +* the pCPU owning that file. ... this wouldn't be needed at all.
Agreed, I'll introduce imsic_local_hgeie_clear() next to imsic_local_hgeie_set(), with your comment. It'll take the file number rather than void *, though, as it's called directly from imsic_vsfile_local_read_clear() and not through imsic_call_on_cpu().
~ Oleksii
PATCH v3 36/39 xen/riscv: map IMSIC interrupt file for vCPUs
In reply to Oleksii KurochkoA guest running in VS-mode expects its own IMSIC S-file at offset 0 of its guest-physical IMSIC block. Physically, the guest-file (G-file) assigned to this vCPU lives at a hart-relative offset given by guest_file_id (assigned via the vGEIN allocator). Therefore, imsic_map_guest_file() uses stage-2 translation to redirect the guest's fixed per-vCPU GPA page (offset 0) to the specific physical guest-file page.
---
Changes in v3:
- Add Acked-by: Jan Beulich <[email protected]>
- Use dprintk() instead of printk() for the IMSIC_DEBUG message in imsic_map_guest_file().
- Add Reviewed-by: Baptiste Le Duc <[email protected]>
---
Changes in v2:
- Use GUEST_IMSIC_S_BASE instead of imsic_cfg.base_addr as the base of the guest address to map to, and change the type of gaddr to paddr_t as it holds a guest physical address.
- Rename guest_stride to guest_offset: it is an offset of the VS-file inside the pCPU's IMSIC block, not a stride.
- Use PRIpaddr for physical addresses and %u for unsigned values in the debug/error messages.
- Switch the mapping failure message from printk() to dprintk(XENLOG_ERR, ...).
- Update the comment above imsic_map_guest_file(): vCPUs aren't pinned, they run on the pCPU chosen by the scheduler, and mention that on migration a VS-file is acquired on the new pCPU and mapped at the same GFN, so the stale mapping is replaced rather than explicitly torn down.
---
--- xen/arch/riscv/imsic.c | 66 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-)
xen/arch/riscv/imsic.c +65 -1
@@ -29,6 +29,7 @@#include <asm/aia.h> #include <asm/aplic.h> #include <asm/imsic.h> +#include <asm/p2m.h> #include <asm/vaplic.h> #define IMSIC_HART_SIZE (guest_bits) (BIT(guest_bits, U) * IMSIC_MMIO_PAGE_SZ)
@@ -588,9 +589,72 @@ void cf_check imsic_ctxt_switch_to(struct vcpu *n)read_unlock_irqrestore (&imsic_state->vsfile_lock, flags); } +/* +* Map the physical IMSIC guest interrupt file (G-file) assigned to vCPU +* into the domain's stage-2 guest-physical address space. +* +* In the machine's physical address space (SPA), each hart's IMSIC +* supervisor-level file (S-file) is located at offset 0 of its address block, +* followed contiguously by GEILEN guest files at offsets of 1, 2, ..., N pages. +* +* Because a guest OS running in VS-mode expects its own supervisor-level +* interrupt file to be at offset 0 of its guest-physical IMSIC block, the +* hypervisor must use stage-2 address translation to map the vCPU's +* guest-physical "supervisor" page (GPA offset 0) to the specific +* physical guest file page (SPA offset guest_file_id) on the physical hart. +* +* A vCPU runs on the pCPU the scheduler picked for it (v->processor), and +* the guest file it is given (guest_file_id, from the vGEIN allocator) +* belongs to that very pCPU's IMSIC. A guest_file_id of 0 indicates that no +* hardware guest file is selected (matching the architectural behavior where +* vGEIN = 0 in the hstatus CSR selects no guest external interrupt source), +* requiring the VS-file to be emulated in software. +* +* Consequently the mapping installed here is only valid as long as the vCPU +* stays on that pCPU. When it migrates, a VS-file is acquired on the new +* pCPU and mapped at the very same GFN, so the stale mapping needs no +* explicit tear-down: it is simply replaced. +* +* The base guest-physical address advertised to the guest in the device +* tree matches offset 0 of the vCPU's virtual IMSIC block. Stage-2 +* translation ensures that guest supervisor accesses to this page are +* transparently routed to the real hardware VS-file granted to it on +* the pCPU it currently runs on. +*/ int imsic_map_guest_file (struct vcpu *v,unsigned int vsfile_id) { -return -EOPNOTSUPP ; +struct domain *d = v->domain; +unsigned int cpu = v->processor; +paddr_t gaddr =GUEST_IMSIC_S_BASE + (IMSIC_MMIO_PAGE_SZ * v->vcpu_id); +paddr_t paddr, guest_offset; +int res; + +/* Nothing to map in the case of sw interrupt file. */ +if ( !vsfile_id ) +return 0 ; + + guest_offset = vsfile_id *IMSIC_MMIO_PAGE_SZ ; + + paddr = imsic_cfg.msi[cpu].base_addr + imsic_cfg.msi[cpu].offset + + guest_offset; + +#ifdef IMSIC_DEBUG +dprintk (XENLOG_DEBUG , +"%s: %pv: ga(%#" PRIpaddr") -> pa(%#" PRIpaddr"), cpu(%u), " +"guest_file_id(%u) base_addr(%#" PRIpaddr") offset(%#lx)\n" , + __func__, v, gaddr, paddr, cpu, vsfile_id, + imsic_cfg.msi[cpu].base_addr, imsic_cfg.msi[cpu].offset); +#endif + + res =map_regions_p2mt (d,gaddr_to_gfn (gaddr), +PFN_DOWN (IMSIC_MMIO_PAGE_SZ ),maddr_to_mfn (paddr), +arch_dt_passthrough_p2m_type ()); +if ( res ) +dprintk (XENLOG_ERR , +"%s: Failed to map %#" PRIpaddr" to the guest at %#" PRIpaddr"\n" , + __func__, paddr, gaddr); + +return res; } int cf_check vcpu_imsic_init(struct vcpu *v)
Signature · 1 line
2.55.0
PATCH v3 37/39 xen/riscv: implement continue_new_vcpu()
In reply to Oleksii Kurochkocontinue_new_vcpu() is the arch hook invoked the first time a freshly created vCPU is scheduled. Implement both cases it has to cover:
- for the idle vCPU, switch to its own stack and jump to idle_loop();
- for a guest vCPU, enter the guest through the new return_to_new_vcpu() path in entry.S, which restores hstatus and sepc, passes the hart id in a0 and, in a1, either the DTB address (boot vCPU, as expected by the RISC-V boot protocol) or the opaque value passed to SBI HSM hart_start() (secondary vCPUs), clears the remaining GPRs, sets sstatus.SPP and sstatus.SPIE and executes sret.
The remaining GPRs have to be cleared as sret neither switches stacks nor touches the GPRs: the guest would otherwise start with Xen's values in them, sp pointing to this vCPU's Xen stack among them.
Interrupts have to stay disabled across the restore. A trap taken in HS-mode overwrites hstatus.SPV, sstatus.SPP and sepc (trap entry clears hstatus.SPV in particular), so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring them afterwards is non-trivial. Instead interrupts are simply kept off and sstatus.SPIE is set, so that sret re-enables them by restoring sstatus.SIE from SPIE.
Introduce get_cpu_info() and reset_stack_and_jump() in asm/current.h, needed by the above. get_cpu_info() is a macro rather than a static inline because asm/current.h is pulled in by <xen/percpu.h> before this_cpu() is defined and before <xen/sched.h> completes struct vcpu.
idle_loop() is only introduced as a stub.
---
Changes in v3:
- Drop unneeded inclusion of asm/aia.h, asm/aplic.h and asm/imsic.h.
- Mark return_to_new_vcpu() declaration with asmlinkage.
- Drop the "else" after reset_stack_and_jump(idle_loop), which cannot return.
- Move the restore of hstatus and the setting of sstatus.SPIE from continue_new_vcpu() to return_to_new_vcpu(), so that everything sret depends on is set up in one place, and shorten the comment in continue_new_vcpu() accordingly.
- Move the tp/SSCRATCH swap out to a separate patch, as nothing reads SSCRATCH yet.
- Reword the comments in return_to_new_vcpu(), which suggested that the values of .a0 and .a1 are established there.
- Clear the GPRs which are not meaningful to the vCPU being started before sret, so that Xen register values are not exposed to a guest.
- Update the commit message: a1 carries the opaque value passed to hart_start() for secondary vCPUs, describe the clearing of the GPRs, drop the comparison with a real CPU and the reference to a later implementation of idle_loop().
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/domain.c | 25 +++++++++++++++++- xen/arch/riscv/entry.S | 39 ++++++++++++++++++++++++++++ xen/arch/riscv/include/asm/current.h | 4 +++ 3 files changed, 67 insertions(+), 1 deletion(-)
xen/arch/riscv/domain.c +24 -1
@@ -138,9 +138,27 @@ static void vcpu_csr_init(struct vcpu *v) v->arch.hie =BIT (IRQ_S_GEXT ,UL ); } +static void schedule_tail (struct vcpu *prev); +static void noreturnidle_loop (void ); +void asmlinkage noreturn return_to_new_vcpu(void); +static void continue_new_vcpu (struct vcpu *prev) { -BUG_ON ("unimplemented\n" ); +schedule_tail (prev); + +if (is_idle_vcpu (current) ) +reset_stack_and_jump (idle_loop); + +/* +* return_to_new_vcpu() sets up hstatus.SPV, sstatus.SPP and sepc so +* that sret enters the guest in VS-mode. A trap taken in HS-mode +* overwrites all of them (trap entry clears hstatus.SPV in particular), +* so interrupts have to stay disabled until sret. They are re-enabled by +* sret itself, as return_to_new_vcpu() also sets sstatus.SPIE. +*/ +local_irq_disable (); + +reset_stack_and_jump (return_to_new_vcpu); }int arch_vcpu_create (struct vcpu *v)
@@ -519,3 +537,8 @@ static void __init __maybe_unused build_assertions(void)#undef CHECK_GPR_INDEX } + +static void noreturnidle_loop (void ) +{ +BUG_ON ("unimplemented" ); +}
xen/arch/riscv/entry.S +39 -0
@@ -147,3 +147,42 @@ FUNC(__context_switch) ret END(__context_switch) + +/* + * Enter a vCPU for the first time. Must be called with interrupts disabled, + * see continue_new_vcpu(). + */ +FUNC(return_to_new_vcpu) + REG_L t0, CPU_USER_REGS_HSTATUS(sp) + csrw CSR_HSTATUS, t0 + + REG_L t0, CPU_USER_REGS_SEPC(sp) + csrw CSR_SEPC, t0 + + /* .a0 holds the hart id */ + REG_L a0, CPU_USER_REGS_A0(sp) + + /* + * .a1 holds the DTB address for the boot vCPU, or the opaque value + * passed to SBI HSM hart_start() for secondary vCPUs + */ + REG_L a1, CPU_USER_REGS_A1(sp) + + /* Return to (V)S-mode, with interrupts re-enabled by sret */ + li t0, SSTATUS_SPP | SSTATUS_SPIE + csrs CSR_SSTATUS, t0 + + /* + * sret doesn't switch stacks and leaves the GPRs alone, so every + * register which isn't meaningful to the vCPU being started has to be + * cleared here: otherwise the guest would see Xen's values, sp (this + * vCPU's Xen stack) and ra among them. + */ + .irp reg, ra, sp, gp, tp, t0, t1, t2, s0, s1, a2, a3, a4, a5, a6, a7, \ + s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, t3, t4, t5, t6 + mv \reg, zero + .endr + + /* Enter guest */ + sret +END(return_to_new_vcpu)
xen/arch/riscv/include/asm/current.h +4 -0
@@ -47,6 +47,8 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);#define set_current (vcpu) do { current = (vcpu); } while (0)#define get_cpu_current (cpu) per_cpu(curr_vcpu, cpu) +#define get_cpu_info () (current->arch.cpu_info) +#define guest_cpu_user_regs () ({ BUG_ON("unimplemented"); NULL; })#define vcpu_guest_cpu_user_regs (vcpu) \ (&(vcpu)->arch.cpu_info->guest_cpu_user_regs)
@@ -58,6 +60,8 @@ DECLARE_PER_CPU(struct vcpu *, curr_vcpu);unreachable (); \ } while ( false ) +#define reset_stack_and_jump (fn) switch_stack_and_jump(get_cpu_info(), fn) +#define get_per_cpu_offset () __per_cpu_offset[smp_processor_id()]#endif /* __ASSEMBLER__ */
Signature · 1 line
2.55.0
Re: [PATCH v3 37/39] xen/riscv: implement continue_new_vcpu()
Quoted 10 lines
continue_new_vcpu()is the arch hook invoked the first time a freshly created vCPU is scheduled. Implement both cases it has to cover:
- for the idle vCPU, switch to its own stack and jump toidle_loop();
- for a guest vCPU, enter the guest through the newreturn_to_new_vcpu()path inentry.S, which restores hstatus and sepc, passes the hart id in a0 and, in a1, either the DTB address (boot vCPU, as expected by the RISC-V boot protocol) or the opaque value passed to SBI HSMhart_start()(secondary vCPUs), clears the remaining GPRs, sets sstatus.SPP and sstatus.SPIE and executes sret.
Nit: the guest vCPU sentence is hard to follow in one go. Maybe split it:
- for a guest vCPU, enter the guest through the new return_to_new_vcpu() path in entry.S. It restores hstatus and sepc and passes the hart id in a0. a1 holds the DTB address for the boot vCPU (as expected by the RISC-V boot protocol), or the opaque value passed to SBI HSM hart_start() for secondary vCPUs. It then clears the remaining GPRs, sets sstatus.SPP and sstatus.SPIE, and executes sret.
Quoted 6 lines
[ ... 94 lines skipped ... ]
xen/arch/riscv/entry.S +0
-0@@ -147,3 +147,42 @@ FUNC(__context_switch) ret
xen/arch/riscv/entry.S FUNC(__context_switch) · line 158
Quoted 10 lines
xen/arch/riscv/entry.S +9
-0END(__context_switch) + +/* + * Enter a vCPU for the first time. Must be called with interrupts disabled, + * see continue_new_vcpu(). + */ +FUNC(return_to_new_vcpu) + REG_L t0, CPU_USER_REGS_HSTATUS(sp) + csrw CSR_HSTATUS, t0 +
At this point regs->hstatus only has SPV | SPVP (arch_vcpu_create()), so VSXL is written as 0, which is a reserved encoding. That only gets fixed by "xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL", which comes later in the series. Wouldn't it be better to move that one ahead of this patch?
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 37/39] xen/riscv: implement continue_new_vcpu()
Quoted 12 lines · Baptiste Le Duc
Quoted 10 lines
continue_new_vcpu()is the arch hook invoked the first time a freshly created vCPU is scheduled. Implement both cases it has to cover:
- for the idle vCPU, switch to its own stack and jump toidle_loop();
- for a guest vCPU, enter the guest through the newreturn_to_new_vcpu()path inentry.S, which restores hstatus and sepc, passes the hart id in a0 and, in a1, either the DTB address (boot vCPU, as expected by the RISC-V boot protocol) or the opaque value passed to SBI HSMhart_start()(secondary vCPUs), clears the remaining GPRs, sets sstatus.SPP and sstatus.SPIE and executes sret.Nit: the guest vCPU sentence is hard to follow in one go. Maybe split it:
Quoted 7 lines
- for a guest vCPU, enter the guest through the new
return_to_new_vcpu()path inentry.S. It restores hstatus and sepc and passes the hart id in a0. a1 holds the DTB address for the boot vCPU (as expected by the RISC-V boot protocol), or the opaque value passed to SBI HSMhart_start()for secondary vCPUs. It then clears the remaining GPRs, sets sstatus.SPP and sstatus.SPIE, and executes sret.
Thanks for the suggestion. I'll use this wording.
Quoted 5 lines
Quoted 5 lines
[ ... 94 lines skipped ... ]
xen/arch/riscv/entry.S +0
-0@@ -147,3 +147,42 @@ FUNC(__context_switch)
Quoted 16 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 158
Quoted 11 lines
xen/arch/riscv/entry.S +9
-0ret END(__context_switch) + +/* + * Enter a vCPU for the first time. Must be called with interrupts disabled, + * see continue_new_vcpu(). + */ +FUNC(return_to_new_vcpu) + REG_L t0, CPU_USER_REGS_HSTATUS(sp) + csrw CSR_HSTATUS, t0 +At this point
regs->hstatusonly has SPV | SPVP (arch_vcpu_create()), so VSXL is written as 0, which is a reserved encoding. That only gets fixed by "xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL", which comes later in the series. Wouldn't it be better to move that one ahead of this patch?
Agreed. I'll move "xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL" before this patch.
Quoted 3 lines
Reviewed-by Baptiste Le Duc <[email protected]>
Thanks!
~ Oleksii
PATCH v3 38/39 xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL
In reply to Oleksii KurochkoThe reset value of hstatus.VSXL isn't architecturally defined, so it has to be programmed explicitly from the domain's type instead of relying on whatever the hardware happens to leave there.
This matters beyond the guest's own view of itself: decoding of a trapped instruction depends on the effective XLEN of the guest, as the encodings which exist for XLEN=64 only must not be recognized for a 32-bit one.
The field is WARL: an implementation is permitted to make it read-only with a value that always keeps VSXLEN == HSXLEN. Probe at boot which encodings can really be set, and refuse to construct a domain whose width the hardware cannot provide, rather than silently running it at the wrong XLEN.
The field occupies bits 33:32 and hence doesn't exist when HSXLEN=32, where VSXLEN is always 32 and HSTATUS_VSXL isn't even defined. Hence the split on CONFIG_RISCV_32, even though RV32 isn't buildable yet.
The domain's type is only established by set_domain_type(), that is once its kernel image has been probed, whereas vCPU0 is created before that. vcpu_csr_init() therefore leaves the field zero, and vcpu_set_vsxl() fills it in from continue_new_vcpu() instead: that runs once per vCPU with the type already settled, right before return_to_new_vcpu() loads the saved hstatus into the CSR.
By then the domain is built and about to run, so it is too late to reject a width the hardware cannot provide. construct_domain() therefore checks it up front, before any of the domain's images are loaded.
domain_vsxl() only maps a domain type onto the encoding, and hstatus_vsxl_settable() tells whether the hardware accepts it. The switch() in domain_vsxl() deliberately has no default case, so that adding a 128-bit domain type fails to build until the mapping is updated.
While at it drop HSTATUS_VSXL_SHIFT, which has no users and isn't expected to gain any.
---
Changes in v3:
- Derive the width from the domain's type instead of hardcoding HSTATUS_VSXL_64, now that the preceding patches make d->type correct.
- Probe at boot which VSXL encodings the hardware really accepts, and refuse in construct_domain() a domain whose width it cannot provide instead of silently running the guest at the wrong XLEN.
- Move the write out of vcpu_csr_init(), which runs before the domain's type is known, into vcpu_set_vsxl() called from continue_new_vcpu(). As a consequence the patch is moved after the one implementing continue_new_vcpu().
- Handle RV32, where the field doesn't exist at all and VSXLEN is always 32.
- Use XLEN_FIELD_{32,64} introduced earlier in the series instead of adding HSTATUS_VSXL_{32,64}: it is the same encoding, and unlike the latter it isn't hidden behind #if __riscv_xlen == 64, which the RV32 paths need.
- Drop HSTATUS_VSXL_SHIFT, which has no users.
- Rework the commit message accordingly.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/domain-build.c | 8 ++ xen/arch/riscv/domain.c | 119 ++++++++++++++++++++ xen/arch/riscv/include/asm/domain.h | 3 + xen/arch/riscv/include/asm/riscv_encoding.h | 1 - xen/arch/riscv/include/asm/setup.h | 2 + xen/arch/riscv/setup.c | 2 + 6 files changed, 134 insertions(+), 1 deletion(-)
xen/arch/riscv/domain-build.c +8 -0
@@ -15,9 +15,17 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo) {struct vcpu *v = d->vcpu[0 ];struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (v); +unsigned int vsxl =domain_vsxl (d);BUG_ON (v->is_initialised); +/* Refuse the domain before any of its images are loaded. */ +if ( !hstatus_vsxl_settable (vsxl) ) + { +printk (XENLOG_ERR "%pd: hstatus.VSXL cannot be set to %u\n" , d, vsxl); +return -EOPNOTSUPP ; + } +/* * At the moment *_load() don't return value and will just panic() * inside.
xen/arch/riscv/domain.c +119 -0
137 lines not on this page · raw message
xen/arch/riscv/include/asm/domain.h +3 -0
@@ -146,6 +146,9 @@ int vcpu_unset_interrupt(struct vcpu *v, unsigned int irq);void vcpu_sync_interrupts (struct vcpu *curr);void vcpu_flush_interrupts (struct vcpu *curr); +bool hstatus_vsxl_settable (unsigned int vsxl); +unsigned int domain_vsxl (const struct domain *d); +#endif /* ASM__RISCV__DOMAIN_H */ /*
xen/arch/riscv/include/asm/riscv_encoding.h +0 -1
@@ -75,7 +75,6 @@#if __riscv_xlen ==64 #define HSTATUS_VSXL _UL(0x300000000) -#define HSTATUS_VSXL_SHIFT 32#endif #define HSTATUS_VTSR _UL(0x00400000)#define HSTATUS_VTW _UL(0x00200000)
xen/arch/riscv/include/asm/setup.h +2 -0
@@ -11,6 +11,8 @@ void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len);void init_csr_masks (void ); +void init_hstatus_vsxl_settable_mask (void ); +#endif /* ASM__RISCV__SETUP_H */ /*
xen/arch/riscv/setup.c +2 -0
@@ -149,6 +149,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,init_csr_masks (); +init_hstatus_vsxl_settable_mask (); +preinit_xen_time ();intc_preinit ();
Signature · 1 line
2.55.0
Re: [PATCH v3 38/39] xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL
Quoted 15 lines
[ ... 97 lines skipped ... ]
xen/arch/riscv/domain.c +8
-0@@ -51,6 +51,119 @@ static struct csr_masks __ro_after_init csr_masks;#define HENVCFG_VALID_MASK 0xe0000003000000ffUL#define HSTATEEN0_VALID_MASK 0xde00000000000007UL +/* +* Return the hstatus.VSXL value encoding the guest's XLEN. Whether the +* hardware can actually provide that width is a separate question, answered +* by hstatus_vsxl_settable(). +* +* The switch() deliberately has no default case, so that adding a new +* domain_type (a 128-bit one, in particular) fails to build until this +* mapping is updated.
xen/arch/riscv/domain.c static struct csr_masks __ro_after_init csr_masks; · line 71
Quoted 10 lines
xen/arch/riscv/domain.c +10
-0+*/ +unsigned int domain_vsxl (const struct domain *d) +{ +switch ( d->type ) + { +case DOMAIN_32BIT : +return XLEN_FIELD_32 ; + +case DOMAIN_64BIT : +return XLEN_FIELD_64 ;
Couldn't we add to avoid having to explain why there is no default in the comment above, and to respect MISRA rules?
default:
break
xen/arch/riscv/domain.c static struct csr_masks __ro_after_init csr_masks; · line 77
Quoted 6 lines
xen/arch/riscv/domain.c +6
-0+ } + +ASSERT_UNREACHABLE (); + +/* A reserved encoding, which hstatus_vsxl_settable() never accepts. */ +return 0 ;
Could this get a name next to XLEN_FIELD_{32,64,128} in riscv_encoding.h, e.g.:
#define XLEN_FIELD_RESERVED _UL(0)
so the comment isn't the only thing explaining the 0?
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 210
Quoted 13 lines
[ ... 88 lines skipped ... ]
xen/arch/riscv/domain.c +5
-0+void __init init_csr_masks (void ) {/* @@ -91,6 +204,10 @@ static void vcpu_csr_init(struct vcpu *v) { v->arch.hedeleg =HEDELEG_DEFAULT & csr_masks.hedeleg; +/* +* hstatus.VSXL is left zero here: the domain's type, and hence the +* guest's XLEN, is only known once its kernel image has been probed. +*/
"Probe" refers to two different things in this series: the hardware probe of the settable hstatus.VSXL values (init_hstatus_vsxl_settable_mask()) and the guest kernel header parsing (kernel_probe()). This comment is about the latter, but it is easy to read it as the former, which already ran at boot. Could you name it explicitly and state the ordering that actually matters? I'd suggest:
/*
* hstatus.VSXL is left zero here: vCPU0 is created before
* kernel_probe() reads the guest kernel header, so the domain's type,
* and hence the guest's XLEN, is not known yet.
*/
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 38/39] xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL
Quoted 21 lines · Baptiste Le Duc
Quoted 21 lines
[ ... 97 lines skipped ... ]
xen/arch/riscv/domain.c +14
-0@@ -51,6 +51,119 @@ static struct csr_masks __ro_after_init csr_masks;#define HENVCFG_VALID_MASK 0xe0000003000000ffUL#define HSTATEEN0_VALID_MASK 0xde00000000000007UL +/* +* Return the hstatus.VSXL value encoding the guest's XLEN. Whether the +* hardware can actually provide that width is a separate question, answered +* by hstatus_vsxl_settable(). +* +* The switch() deliberately has no default case, so that adding a new +* domain_type (a 128-bit one, in particular) fails to build until this +* mapping is updated. +*/ +unsigned int domain_vsxl (const struct domain *d) +{ +switch ( d->type ) + { +case DOMAIN_32BIT :
Quoted 8 lines
xen/arch/riscv/domain.c static struct csr_masks __ro_after_init csr_masks; · line 71
Quoted 4 lines
xen/arch/riscv/domain.c +4
-0+return XLEN_FIELD_32 ; + +case DOMAIN_64BIT : +return XLEN_FIELD_64 ;Couldn't we add to avoid having to explain why there is no default in the comment above, and to respect MISRA rules?
default:
break
I'd rather not: d->type is an enum, and for those Xen deliberately omits the default label, as -Wall enables -Wswitch, which breaks the build (-Werror) if one of the enum's values isn't handled. A default label would silence exactly that check. MISRA-wise this is covered by the R16.4 deviation for switches on enums.
I'll move the explanation into the switch() itself, though, and mention -Wswitch there, which should make the intent clearer:
...
case DOMAIN_64BIT:
return XLEN_FIELD_64;
/*
* No default case: -Wswitch then fails the build should a new
* domain_type (a 128-bit one, in particular) be added without
* updating this mapping.
*/
}...
Quoted 11 lines
xen/arch/riscv/domain.c static struct csr_masks __ro_after_init csr_masks; · line 77
Quoted 6 lines
xen/arch/riscv/domain.c +6
-0+ } + +ASSERT_UNREACHABLE (); + +/* A reserved encoding, which hstatus_vsxl_settable() never accepts. */ +return 0 ;Could this get a name next to XLEN_FIELD_{32,64,128} in
riscv_encoding.h, e.g.:#define
XLEN_FIELD_RESERVED_UL(0)
so the comment isn't the only thing explaining the 0?
Sure, I'll add XLEN_FIELD_RESERVED and return it there.
Quoted 21 lines
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 210
Quoted 13 lines
[ ... 88 lines skipped ... ]
xen/arch/riscv/domain.c +5
-0+void __init init_csr_masks (void ) {/* @@ -91,6 +204,10 @@ static void vcpu_csr_init(struct vcpu *v) { v->arch.hedeleg =HEDELEG_DEFAULT & csr_masks.hedeleg; +/* +* hstatus.VSXL is left zero here: the domain's type, and hence the +* guest's XLEN, is only known once its kernel image has been probed. +*/ "Probe" refers to two different things in this series: the hardware probe of the settable hstatus.VSXL values (
init_hstatus_vsxl_settable_mask()) and the guest kernel header parsing (kernel_probe()). This comment is about the latter, but it is easy to read it as the former, which already ran at boot. Could you name it explicitly and state the ordering that actually matters? I'd suggest:
Quoted 6 lines
/*
* hstatus.VSXL is left zero here: vCPU0 is created before
*kernel_probe()reads the guest kernel header, so the domain's type,
* and hence the guest's XLEN, is not known yet.
*/
Your suggested rewording looks good to me. I'll take your wording, and name kernel_probe() in the commit message as well, which has the same ambiguity.
Thanks.
~ Oleksii
Re: [PATCH v3 38/39] xen/riscv: set the guest's XLEN explicitly in hstatus.VSXL
Quoted 4 lines · Oleksii Kurochko
[ ... 33 lines skipped ... ]
I'd rather not:d->typeis an enum, and for those Xen deliberately omits the default label, as -Wall enables -Wswitch, which breaks the build (-Werror) if one of the enum's values isn't handled.
I didn't know that, so yes, you're right it's better to keep without default.
Quoted 11 lines
A default label
would silence exactly that check. MISRA-wise this is covered by the R16.4 deviation for switches on enums.I'll move the explanation into the
switch()itself, though, and mention -Wswitch there, which should make the intent clearer:...
caseDOMAIN_64BIT:
returnXLEN_FIELD_64;
Quoted 5 lines
/*
* No default case: -Wswitch then fails the build should a new
*domain_type(a 128-bit one, in particular) be added without
* updating this mapping.
*/
More clear, thanks.
Quoted 4 lines
[ ... 44 lines skipped ... ]
Your suggested rewording looks good to me. I'll take your wording, and namekernel_probe()in the commit message as well, which has the same ambiguity.
Agree, thanks.
Signature · 1 line
Baptiste Le Duc <[email protected]>
PATCH v3 39/39 xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
In reply to Oleksii KurochkoIntroduce imsic_vsfile_attach() to give a vCPU a working guest interrupt file.
A guest (VS) interrupt file has to be backed by one of the hardware guest interrupt files of the pCPU the vCPU runs on, so that pCPU has to be known first. arch_vcpu_create() is therefore not a suitable place:
the pCPU assigned to a vCPU can still change before the vCPU is first scheduled, which would mean taking another file and remapping the guest's IMSIC page to it. Call imsic_vsfile_attach() from continue_new_vcpu() instead, i.e. when the vCPU is scheduled for the first time; as that isn't an __init context, neither is imsic_vsfile_attach().
Giving a vCPU its first file shares most of the steps with moving it to a new one on migration, so move the body of imsic_migrate_vcpu() to imsic_vsfile_move() and use it for both. Its first_attach argument skips the part which moves the interrupt state out of the old file: the vCPU has never run at that point, so the freshly acquired and zeroed file is all the interrupt state it has, and hstatus.VGEIN can be pointed at it right away.
vAPLIC target registers are reconfigured on the first attach as well: a target the guest writes for a vCPU which has no h/w guest interrupt file yet only updates the stored copy, which reaches the h/w at this point.
A vCPU which has already run is left without a h/w file only when its first attach failed, which crashed its domain. Such a vCPU can still be migrated, e.g. by an affinity change or a pCPU going offline, until the domain is destroyed, so don't panic() for it, which would take the whole host down: return early. Should a vCPU of a domain which isn't shutting down ever get there, crash that domain rather than the host.
---imsic_vsfile_move() isn't split out of imsic_migrate_vcpu() earlier in the
series: until this patch imsic_migrate_vcpu() would be its only user, so the first_attach path would have no caller. The first attach itself can't come earlier either, as it is done from continue_new_vcpu(), introduced by "xen/riscv: implement continue_new_vcpu()".
---
Changes in v3:
- imsic_update_state() is now implemented by "xen/riscv: remap interrupts to new IMSIC VS-file".
- Share the code of imsic_vsfile_attach() and imsic_migrate_vcpu() in imsic_vsfile_move(), whose first_attach argument skips the part moving state out of the old file.
- Call vaplic_reconfigure_target() on the first attach as well, so that vAPLIC targets the guest wrote for the vCPU before it got its file reach the h/w.
- Replace panic() for a vCPU left without a h/w file by domain_crash():
it can only happen to a vCPU whose first imsic_vsfile_acquire() failed and crashed its domain, and such a vCPU can still be migrated.
- imsic_vsfile_acquire(), together with the vgein_release() on a mapping failure, is now introduced by "xen/riscv: remap interrupts to new IMSIC VS-file", without the cpu argument (the file is always taken on v->processor).
- Include asm/imsic.h for imsic_vsfile_attach().
- Drop imsic_nr_eix() in favour of imsic_cfg.nr_eix.
- Drop vcpu_set_vgein(): with the code shared in imsic_vsfile_move(), hstatus.VGEIN is updated in one place only.
- Rework the commit message: drop the stale vcpu_aia_init() name and the claim that the IMSIC state has no concurrent readers yet.
---
Changes in v2:
- Update vcpu_aia_init() to catch sw interrupt file and update some debug messages in it.
- Add vgein_release() if IMSIC h/w mapping failed.
- imsic_update_state(): store v->processor rather than cpuid_to_hartid(), as ->vsfile_cpu is consumed as a Xen CPU id (aplic_hart_field(), cpumask_of()) and its NR_CPUS sentinel lives in that numbering space.
- Drop parentheses around guest_file_id ? ... in imsic_update_state().
- Rename vcpu_aia_init to imsic_vsfile_attach() and move the code to imsic.c.
---
--- xen/arch/riscv/domain.c | 3 ++ xen/arch/riscv/imsic.c | 79 +++++++++++++++++++++++------- xen/arch/riscv/include/asm/imsic.h | 2 + 3 files changed, 66 insertions(+), 18 deletions(-)
xen/arch/riscv/domain.c +3 -0
@@ -13,6 +13,7 @@#include <asm/csr.h> #include <asm/current.h> #include <asm/gpr-num.h> +#include <asm/imsic.h> #include <asm/intc.h> #include <asm/mmio.h> #include <asm/riscv_encoding.h>
@@ -268,6 +269,8 @@ static void continue_new_vcpu(struct vcpu *prev)vcpu_set_vsxl (current); +imsic_vsfile_attach (current); +/* * return_to_new_vcpu() sets up hstatus.SPV, sstatus.SPP and sepc so * that sret enters the guest in VS-mode. A trap taken in HS-mode
xen/arch/riscv/imsic.c +61 -18
115 lines not on this page · raw message
xen/arch/riscv/include/asm/imsic.h +2 -0
@@ -118,4 +118,6 @@ int imsic_map_guest_file(struct vcpu *v, unsigned int vsfile_id);void imsic_migrate_vcpu (struct vcpu *v); +void imsic_vsfile_attach (struct vcpu *v); +#endif /* ASM_RISCV_IMSIC_H */
Signature · 1 line
2.55.0
Re: [PATCH v3 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
Quoted 21 lines
Introduce
imsic_vsfile_attach()to give a vCPU a working guest interrupt file.A guest (VS) interrupt file has to be backed by one of the hardware guest interrupt files of the pCPU the vCPU runs on, so that pCPU has to be known first.
arch_vcpu_create()is therefore not a suitable place:
the pCPU assigned to a vCPU can still change before the vCPU is first scheduled, which would mean taking another file and remapping the guest's IMSIC page to it. Callimsic_vsfile_attach()fromcontinue_new_vcpu()instead, i.e. when the vCPU is scheduled for the first time; as that isn't an__initcontext, neither isimsic_vsfile_attach().Giving a vCPU its first file shares most of the steps with moving it to a new one on migration, so move the body of
imsic_migrate_vcpu()toimsic_vsfile_move()and use it for both. Itsfirst_attachargument skips the part which moves the interrupt state out of the old file: the vCPU has never run at that point, so the freshly acquired and zeroed file is all the interrupt state it has, and hstatus.VGEIN can be pointed at it right away.
Quoted 4 lines
vAPLIC target registers are reconfigured on the first attach as well: a target the guest writes for a vCPU which has no h/w guest interrupt file yet only updates the stored copy, which reaches the h/w at this point.
Nit: I didn't quite understand this paragraph, "only updates" leaves out that the h/w APLIC write is skipped, because there is no file to point the target at yet. Maybe something like:
The guest may program a vAPLIC TARGET register for a vCPU which has not run yet. That vCPU has no h/w guest interrupt file yet, so vaplic_mmio_write() only stores the value. imsic_vsfile_move() then writes it to the h/w APLIC on the first attach, pointing to the newly acquired file.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v3 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
Quoted 29 lines · Baptiste Le Duc
Quoted 21 lines
Introduce
imsic_vsfile_attach()to give a vCPU a working guest interrupt file.A guest (VS) interrupt file has to be backed by one of the hardware guest interrupt files of the pCPU the vCPU runs on, so that pCPU has to be known first.
arch_vcpu_create()is therefore not a suitable place:
the pCPU assigned to a vCPU can still change before the vCPU is first scheduled, which would mean taking another file and remapping the guest's IMSIC page to it. Callimsic_vsfile_attach()fromcontinue_new_vcpu()instead, i.e. when the vCPU is scheduled for the first time; as that isn't an__initcontext, neither isimsic_vsfile_attach().Giving a vCPU its first file shares most of the steps with moving it to a new one on migration, so move the body of
imsic_migrate_vcpu()toimsic_vsfile_move()and use it for both. Itsfirst_attachargument skips the part which moves the interrupt state out of the old file: the vCPU has never run at that point, so the freshly acquired and zeroed file is all the interrupt state it has, and hstatus.VGEIN can be pointed at it right away.Quoted 4 lines
vAPLIC target registers are reconfigured on the first attach as well: a target the guest writes for a vCPU which has no h/w guest interrupt file yet only updates the stored copy, which reaches the h/w at this point.
Nit: I didn't quite understand this paragraph, "only updates" leaves out that the h/w APLIC write is skipped, because there is no file to point the target at yet. Maybe something like:
Quoted 6 lines
The guest may program a vAPLIC TARGET register for a vCPU which has not run yet. That vCPU has no h/w guest interrupt file yet, so
vaplic_mmio_write()only stores the value.imsic_vsfile_move()then writes it to the h/w APLIC on the first attach, pointing to the newly acquired file.
Agreed, I'll reword it along those lines, naming vaplic_target_write(), which is where the value gets stored, and vaplic_reconfigure_target(), which is what writes it to the h/w APLIC on the first attach:
``` The guest may program the vAPLIC target register of a h/w source for a vCPU which hasn't run yet. That vCPU has no h/w guest interrupt file yet, so there is nothing to point the h/w target register at, and vaplic_target_write() only stores the value. Hence imsic_vsfile_move() calls vaplic_reconfigure_target() on the first attach as well, which writes the stored value to the h/w APLIC, pointing it at the newly acquired file. ```
Quoted 2 lines
Reviewed-by Baptiste Le Duc <[email protected]>
Thanks for the review!
~ Oleksii