virtual interrupt controller (vAPLIC/vIMSIC) support
Re: [PATCH v2 37/39] xen/riscv: map IMSIC interrupt file for vCPUs
In reply to Oleksii KurochkoQuoted 8 lines · Oleksii Kurochko
A 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.[...]
Signature · 1 line
Baptiste Le Duc <[email protected]>
PATCH v2 38/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, restore hstatus and enter the guest through the new return_to_new_vcpu() path in entry.S, which loads sepc, passes the hart id in a0 and the DTB address in a1 as expected by the RISC-V boot protocol, sets sstatus.SPP and executes sret.
Interrupts have to stay disabled across the restore. The trap entry logic implicitly clears hstatus.SPV, so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring SPV afterwards is non-trivial. Instead interrupts are simply kept off and sstatus.SPIE is set, so that SIE is restored from SPIE once sret has been executed. Also, it follows what hardware will do with real CPU which is also started with interrupts disabled.
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 added as a stub on purpose; its real implementation will come separately later.
---
Changes in v2:
- New patch.
---
--- xen/arch/riscv/domain.c | 44 +++++++++++++++++++++++++++- xen/arch/riscv/entry.S | 23 +++++++++++++++ xen/arch/riscv/include/asm/current.h | 4 +++ 3 files changed, 70 insertions(+), 1 deletion(-)
xen/arch/riscv/domain.c +43 -1
@@ -8,10 +8,13 @@#include <xen/smp.h> #include <xen/vmap.h> +#include <asm/aia.h> +#include <asm/aplic.h> #include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> #include <asm/current.h> +#include <asm/imsic.h> #include <asm/intc.h> #include <asm/mmio.h> #include <asm/riscv_encoding.h>
@@ -140,9 +143,43 @@ static void vcpu_csr_init(struct vcpu *v) v->arch.hie =MIP_SGEIP ; } +static void schedule_tail (struct vcpu *prev); +static void noreturnidle_loop (void ); +void noreturnreturn_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); +else + { +/* +* During a context switch to a new vCPU, interrupts must be disabled +* to guarantee that the vCPU's CSR state can be safely restored into +* the hart without being clobbered by an interrupt trap. +* +* For example, when return_to_new_vcpu() finishes, it executes sret. +* At that point, the hart checks hstatus.SPV=1 and sstatus.SPP=1 in +* order to return from HS-mode into VS-mode. If an interrupt were to +* arrive before sret, the trap entry logic would implicitly clear +* hstatus.SPV to 0. Correctly restoring it afterwards is non-trivial, +* and if left as 0, sret would incorrectly return to HS-mode instead +* of VS-mode. +* +* To avoid this, interrupts are kept disabled during the restore. +* Additionally, setting sstatus.SPIE=1 ensures that after sret is +* executed (as sstatus.SIE will be loaded from SPIE), HS-mode will +* continue to receive interrupts normally. +*/ +local_irq_disable (); +csr_set (CSR_SSTATUS ,SSTATUS_SPIE ); + +csr_write (CSR_HSTATUS ,vcpu_guest_cpu_user_regs (current)->hstatus); + +reset_stack_and_jump (return_to_new_vcpu); + } }int arch_vcpu_create (struct vcpu *v)
@@ -551,3 +588,8 @@ static void __init __maybe_unused build_assertions(void)*/ BUILD_BUG_ON (offsetof(struct cpu_info , guest_cpu_user_regs)); } + +static void noreturnidle_loop (void ) +{ +BUG_ON ("unimplemented" ); +}
xen/arch/riscv/entry.S +23 -0
@@ -143,3 +143,26 @@ FUNC(__context_switch) ret END(__context_switch) + +/* t0 is used as a temporary reg and is clobbered to oblivion */ +FUNC(return_to_new_vcpu) + /* Swap tp with sscratch */ + csrrw tp, CSR_SSCRATCH, tp + + /* Set vCPU registers */ + REG_L t0, CPU_USER_REGS_SEPC(sp) + csrw sepc, t0 + + /* Hartid goes to a0 */ + REG_L a0, CPU_USER_REGS_A0(sp) + + /* DTB goes to a1 */ + REG_L a1, CPU_USER_REGS_A1(sp) + + /* Set guest mode to supervisor */ + li t0, SSTATUS_SPP + csrs CSR_SSTATUS, t0 + + /* 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 v2 38/39] xen/riscv: implement continue_new_vcpu()
Quoted 7 lines · Oleksii Kurochko
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, restore hstatus and enter the guest through the newreturn_to_new_vcpu()path inentry.S, which loads sepc, passes the hart id in a0 and the DTB address in a1 as expected by the RISC-V boot protocol, sets sstatus.SPP and executes sret.
Is this a requirement for all CPUs, or just for the boot one? (I can't quite see why secondary processors would need passing a DTB address.)
Quoted 6 lines
Interrupts have to stay disabled across the restore. The trap entry logic implicitly clears hstatus.SPV, so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring SPV afterwards is non-trivial. Instead interrupts are simply kept off and sstatus.SPIE is set, so that SIE is restored from SPIE once sret has been executed.
As written this reads as if the guest would be responsible for doing this. Isn't it rather SRET itself which does this?
Quoted 2 lines
Also, it follows what hardware will do
with real CPU which is also started with interrupts disabled.
Further up, aiui, you talk about the host's interrupt state. How vCPU-s are started, however, is virtual interrupt state. Mixing both isn't very helpful.
Quoted 4 lines
Introduce
get_cpu_info()andreset_stack_and_jump()inasm/current.h, needed by the above.get_cpu_info()is a macro rather than a static inline becauseasm/current.his pulled in by <xen/percpu.h> beforethis_cpu()is defined and before <xen/sched.h> completes struct vcpu.
This is odd, given the similarity to Arm. They get away without using "current", and hence without using this_cpu().
Quoted 6 lines
xen/arch/riscv/domain.c +0
-0@@ -8,10 +8,13 @@#include <xen/smp.h> #include <xen/vmap.h>
xen/arch/riscv/domain.c line 20
Quoted 10 lines
xen/arch/riscv/domain.c +3
-0+#include <asm/aia.h> +#include <asm/aplic.h> #include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> #include <asm/current.h> +#include <asm/imsic.h> #include <asm/intc.h> #include <asm/mmio.h> #include <asm/riscv_encoding.h>
What makes these additions necessary here?
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 148
Quoted 7 lines
xen/arch/riscv/domain.c +3
-0@@ -140,9 +143,43 @@ static void vcpu_csr_init(struct vcpu *v) v->arch.hie =MIP_SGEIP ; } +static void schedule_tail (struct vcpu *prev); +static void noreturnidle_loop (void ); +void noreturnreturn_to_new_vcpu (void );
For this last one: asmlinkage?
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 156
Quoted 8 lines
xen/arch/riscv/domain.c +5
-1static 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); +else
This is the kind of "else" which I consider particularly confusing: It suggests that the if() body can actually be exited at the bottom, when (by the name "reset_stack_and_jump") it hopefully cannot.
Quoted 15 lines
xen/arch/riscv/domain.c +15
-0+ { +/* +* During a context switch to a new vCPU, interrupts must be disabled +* to guarantee that the vCPU's CSR state can be safely restored into +* the hart without being clobbered by an interrupt trap. +* +* For example, when return_to_new_vcpu() finishes, it executes sret. +* At that point, the hart checks hstatus.SPV=1 and sstatus.SPP=1 in +* order to return from HS-mode into VS-mode. If an interrupt were to +* arrive before sret, the trap entry logic would implicitly clear +* hstatus.SPV to 0. Correctly restoring it afterwards is non-trivial, +* and if left as 0, sret would incorrectly return to HS-mode instead +* of VS-mode. +* +* To avoid this, interrupts are kept disabled during the restore.
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 181
Quoted 10 lines
xen/arch/riscv/domain.c +10
-0+* Additionally, setting sstatus.SPIE=1 ensures that after sret is +* executed (as sstatus.SIE will be loaded from SPIE), HS-mode will +* continue to receive interrupts normally. +*/ +local_irq_disable (); +csr_set (CSR_SSTATUS ,SSTATUS_SPIE ); + +csr_write (CSR_HSTATUS ,vcpu_guest_cpu_user_regs (current)->hstatus); + +reset_stack_and_jump (return_to_new_vcpu);
What are the criteria by which you split CSR accesses between doing some here and some in return_to_new_vcpu()? In particular you set sstatus.SPIE here but sstatus.SPP there, when both could - I think - be done with a single CSR access.
xen/arch/riscv/entry.S FUNC(__context_switch) · line 150
Quoted 11 lines
xen/arch/riscv/entry.S +5
-0@@ -143,3 +143,26 @@ FUNC(__context_switch) ret END(__context_switch) + +/* t0 is used as a temporary reg and is clobbered to oblivion */ +FUNC(return_to_new_vcpu) + /* Swap tp with sscratch */ + csrrw tp, CSR_SSCRATCH, tp
What is this about? I'm not aware of any counterpart code, yet all on its own this I can't see it being overly useful.
xen/arch/riscv/entry.S FUNC(__context_switch) · line 160
Quoted 9 lines
xen/arch/riscv/entry.S +9
-0+ /* Set vCPU registers */ + REG_L t0, CPU_USER_REGS_SEPC(sp) + csrw sepc, t0 + + /* Hartid goes to a0 */ + REG_L a0, CPU_USER_REGS_A0(sp) + + /* DTB goes to a1 */ + REG_L a1, CPU_USER_REGS_A1(sp)
The fields loaded are merely .a0 and .a1 of the register struct. There's nothing here making sure (all on its own) that what is loaded is what is said by the comments. If e.g. the first comment was /* .a0 holds the hart id */ or some such to remind readers what is being loaded without giving the impression that the correct value is _established_ here, that may be better.
xen/arch/riscv/entry.S FUNC(__context_switch) · line 168
Quoted 7 lines
xen/arch/riscv/entry.S +7
-0+ /* Set guest mode to supervisor */ + li t0, SSTATUS_SPP + csrs CSR_SSTATUS, t0 + + /* Enter guest */ + sret +END(return_to_new_vcpu)
Aiui SRET does not switch stacks. Shouldn't you therefore clear sp here? And perhaps also other GPRs, not the least ra? Exposing hypervisor register values to guests is, well, a bit of a problem.
Jan
Re: [PATCH v2 38/39] xen/riscv: implement continue_new_vcpu()
Quoted 11 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko
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, restore hstatus and enter the guest through the newreturn_to_new_vcpu()path inentry.S, which loads sepc, passes the hart id in a0 and the DTB address in a1 as expected by the RISC-V boot protocol, sets sstatus.SPP and executes sret.Is this a requirement for all CPUs, or just for the boot one? (I can't quite see why secondary processors would need passing a DTB address.)
It is requirement for boot one. For secondary processors it is HSM boot data which is passed to sbi_hsm_hart_start() and then intercpeted by Xen.
At the moment of writing of this commit message we have only boot CPU and so only DTB could be passed.
I can update the commit message and the comment in return_to_new_vcpu() to tell that it could be DTB address for boot cpu and/or for secondary CPUs HSM boot data or it will be better to add info about HSM boot data during and an introduction of secondary CPUs support?
Quoted 10 lines
Quoted 7 lines
Interrupts have to stay disabled across the restore. The trap entry logic implicitly clears hstatus.SPV, so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring SPV afterwards is non-trivial. Instead interrupts are simply kept off and sstatus.SPIE is set, so that SIE is restored from SPIE once sret has been executed.
As written this reads as if the guest would be responsible for doing this. Isn't it rather SRET itself which does this?
IIUC to which part you refer then yes, it is SRET itself which does this. So some re-wording should be done ...
Quoted 7 lines
Quoted 3 lines
Also, it follows what hardware will do
with real CPU which is also started with interrupts disabled.Further up, aiui, you talk about the host's interrupt state. How vCPU-s are started, however, is virtual interrupt state. Mixing both isn't very helpful.
...:
Interrupts have to stay disabled across the restore. The trap entry
logic implicitly clears hstatus.SPV, so an interrupt taken between the
write of hstatus and sret would make sret return to HS-mode instead of
VS-mode, and restoring SPV afterwards is non-trivial. Hence interrupts
are kept disabled, and sstatus.SPIE is set so that sret itself re-enables them (SIE := SPIE) as part of entering the guest.
Then it will be also need to update the comment inside continue_new_vcpu() to:
- * To avoid this, interrupts are kept disabled during the restore.
- * Additionally, setting sstatus.SPIE=1 ensures that after sret is
- * executed (as sstatus.SIE will be loaded from SPIE), HS-mode will
- * continue to receive interrupts normally.
+ * To avoid this, interrupts are kept disabled during the restore,
+ * and sstatus.SPIE is set so that sret itself re-enables them
+ * (SIE := SPIE) as part of entering the guest.
*/Would it be the wording okay for you now?
Quoted 8 lines
Quoted 5 lines
Introduce
get_cpu_info()andreset_stack_and_jump()inasm/current.h, needed by the above.get_cpu_info()is a macro rather than a static inline becauseasm/current.his pulled in by <xen/percpu.h> beforethis_cpu()is defined and before <xen/sched.h> completes struct vcpu.This is odd, given the similarity to Arm. They get away without using "current", and hence without using
this_cpu().
Arm calculates struct cpu_info * based on sp register + STACK_SIZE:
static inline struct cpu_info *get_cpu_info(void)
{
#ifdef __clang__
unsigned long sp;
asm ("mov %0, sp" : "=r" (sp));
#else
register unsigned long sp asm ("sp");#endif
return (struct cpu_info *)((sp & ~(STACK_SIZE - 1)) +
STACK_SIZE - sizeof(struct cpu_info));
}what is equal to current->arch.cpu_info as I can see based on how both arch-es are initializing v->arch.cpu_info what is used in RISC-V.
Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/domain.c +0
-0@@ -8,10 +8,13 @@#include <xen/smp.h> #include <xen/vmap.h>
Quoted 12 lines
xen/arch/riscv/domain.c line 20
Quoted 10 lines
xen/arch/riscv/domain.c +3
-0+#include <asm/aia.h> +#include <asm/aplic.h> #include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> #include <asm/current.h> +#include <asm/imsic.h> #include <asm/intc.h> #include <asm/mmio.h> #include <asm/riscv_encoding.h> What makes these additions necessary here?
None, they are leftovers from an earlier version of this patch. I'll drop them; <asm/imsic.h> will be added by the patch which starts using imsic_vsfile_attach().
Quoted 10 lines
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 148
Quoted 8 lines
xen/arch/riscv/domain.c +3
-0@@ -140,9 +143,43 @@ static void vcpu_csr_init(struct vcpu *v) v->arch.hie =MIP_SGEIP ; } +static void schedule_tail (struct vcpu *prev); +static void noreturnidle_loop (void ); +void noreturnreturn_to_new_vcpu (void );For this last one: asmlinkage?
Will add.
Quoted 10 lines
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 156
Quoted 9 lines
xen/arch/riscv/domain.c +5
-1static 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); +else
Quoted 3 lines
This is the kind of "else" which I consider particularly confusing: It suggests that the
if()body can actually be exited at the bottom, when (by the name "reset_stack_and_jump") it hopefully cannot.
Agreed, the "else" isn't needed here. I kept it only because it looked more symmetric to me, but I'll drop it and move its body out to the same level as the if().
Quoted 23 lines
Quoted 23 lines
xen/arch/riscv/domain.c +22
-0+ { +/* +* During a context switch to a new vCPU, interrupts must be disabled +* to guarantee that the vCPU's CSR state can be safely restored into +* the hart without being clobbered by an interrupt trap. +* +* For example, when return_to_new_vcpu() finishes, it executes sret. +* At that point, the hart checks hstatus.SPV=1 and sstatus.SPP=1 in +* order to return from HS-mode into VS-mode. If an interrupt were to +* arrive before sret, the trap entry logic would implicitly clear +* hstatus.SPV to 0. Correctly restoring it afterwards is non-trivial, +* and if left as 0, sret would incorrectly return to HS-mode instead +* of VS-mode. +* +* To avoid this, interrupts are kept disabled during the restore. +* Additionally, setting sstatus.SPIE=1 ensures that after sret is +* executed (as sstatus.SIE will be loaded from SPIE), HS-mode will +* continue to receive interrupts normally. +*/ +local_irq_disable (); +csr_set (CSR_SSTATUS ,SSTATUS_SPIE ); +
Quoted 8 lines
xen/arch/riscv/domain.c static void vcpu_csr_init(struct vcpu *v) · line 181
Quoted 3 lines
xen/arch/riscv/domain.c +3
-0+csr_write (CSR_HSTATUS ,vcpu_guest_cpu_user_regs (current)->hstatus); + +reset_stack_and_jump (return_to_new_vcpu);What are the criteria by which you split CSR accesses between doing some here and some in
return_to_new_vcpu()? In particular you set sstatus.SPIE here but sstatus.SPP there, when both could - I think - be done with a single CSR access.
There was no real criterion (and SPIE is set here only for better explanation of the comment). I'll move all the CSR setup sret depends on (hstatus, sepc, sstatus.SPP and sstatus.SPIE) into return_to_new_vcpu(),
like the regular return-to-guest path in entry.S already does, and set SPP and SPIE with a single CSR access there. Only local_irq_disable() will stay in continue_new_vcpu().
I will do the following:
+ * 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();- csr_set(CSR_SSTATUS, SSTATUS_SPIE);
-
- csr_write(CSR_HSTATUS, vcpu_guest_cpu_user_regs(current)->hstatus);
reset_stack_and_jump(return_to_new_vcpu);
and then:
-/* t0 is used as a temporary reg and is clobbered to oblivion */ +/*
+ * Enter a vCPU for the first time. Must be called with interrupts disabled,
+ * see continue_new_vcpu().
+ *
+ * t0 is used as a temporary reg and is clobbered to oblivion.
+ */
FUNC(return_to_new_vcpu)
/* Swap tp with sscratch */
csrrw tp, CSR_SSCRATCH, tp
/* Set vCPU registers */
+ REG_L t0, CPU_USER_REGS_HSTATUS(sp)
+ csrw CSR_HSTATUS, t0
+
REG_L t0, CPU_USER_REGS_SEPC(sp)
- csrw sepc, t0
+ csrw CSR_SEPC, t0 /* Hartid goes to a0 */
REG_L a0, CPU_USER_REGS_A0(sp)
/* DTB goes to a1 */
REG_L a1, CPU_USER_REGS_A1(sp)- /* Set guest mode to supervisor */ - li t0, SSTATUS_SPP + /* Return to (V)S-mode, with interrupts re-enabled by sret */ + li t0, SSTATUS_SPP | SSTATUS_SPIE
csrs CSR_SSTATUS, t0
/* Enter guest */
sret
END(return_to_new_vcpu)
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/entry.S +0
-0@@ -143,3 +143,26 @@ FUNC(__context_switch)
Quoted 10 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 150
Quoted 7 lines
xen/arch/riscv/entry.S +5
-0ret END(__context_switch) + +/* t0 is used as a temporary reg and is clobbered to oblivion */ +FUNC(return_to_new_vcpu) + /* Swap tp with sscratch */ + csrrw tp, CSR_SSCRATCH, tpWhat is this about? I'm not aware of any counterpart code, yet all on its own this I can't see it being overly useful.
It will be needed later when a guest will be able to launch to distinguish in handle_trap() [1] if a trap is from guest or not. I can drop it for now and re-introduce it with the code of handle_trap() or as an option I could update the comment above to:
/*
* SSCRATCH holds this hart's struct pcpu_info while a guest
runs and
* is zero while Xen runs, so that a trap handler can tell the two
* apart: tp is Xen's pointer to pcpu_info in Xen context, but
belongs
* to the guest once sret has been executed. Establish that by
swapping
* the two here; the trap path swaps them back.
*/
csrrw tp, CSR_SSCRATCH, tp
(but then the last part will point to the part which isn't yet introduced so probably it will be better to drop this line for now)
Quoted 17 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 160
Quoted 10 lines
xen/arch/riscv/entry.S +9
-0+ /* Set vCPU registers */ + REG_L t0, CPU_USER_REGS_SEPC(sp) + csrw sepc, t0 + + /* Hartid goes to a0 */ + REG_L a0, CPU_USER_REGS_A0(sp) + + /* DTB goes to a1 */ + REG_L a1, CPU_USER_REGS_A1(sp)The fields loaded are merely .a0 and .a1 of the register struct. There's nothing here making sure (all on its own) that what is loaded is what is said by the comments. If e.g. the first comment was /* .a0 holds the hart id */ or some such to remind readers what is being loaded without giving the impression that the correct value is _established_ here, that may be better.
I will re-word the comments in suggested way:
- /* Hartid goes to a0 */
+ /* .a0 holds the hart id */
REG_L a0, CPU_USER_REGS_A0(sp)
- /* DTB goes to a1 */
+ /* .a1 holds the address of the DTB */
REG_L a1, CPU_USER_REGS_A1(sp)
Quoted 12 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 168
Quoted 8 lines
xen/arch/riscv/entry.S +7
-0+ /* Set guest mode to supervisor */ + li t0, SSTATUS_SPP + csrs CSR_SSTATUS, t0 + + /* Enter guest */ + sret +END(return_to_new_vcpu)Aiui SRET does not switch stacks. Shouldn't you therefore clear sp here? And perhaps also other GPRs, not the least ra? Exposing hypervisor register values to guests is, well, a bit of a problem.
Good point, sret leaves all GPRs as they are, so the guest would indeed see Xen's sp, ra and friends. Only a0 and a1 are architecturally meaningful for a booting hart, so I'll clear every other GPR right before sret.
I will add the following before sret:
/*
* 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
Thanks.
~ Oleksii
Re: [PATCH v2 38/39] xen/riscv: implement continue_new_vcpu()
Quoted 10 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko
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, restore hstatus and enter the guest through the newreturn_to_new_vcpu()path inentry.S, which loads sepc, passes the hart id in a0 and the DTB address in a1 as expected by the RISC-V boot protocol, sets sstatus.SPP and executes sret.
Quoted 8 lines
Quoted 2 lines
Is this a requirement for all CPUs, or just for the boot one? (I can't quite see why secondary processors would need passing a DTB address.)
It is requirement for boot one. For secondary processors it is HSM boot data which is passed to
sbi_hsm_hart_start()and then intercpeted by Xen.At the moment of writing of this commit message we have only boot CPU and so only DTB could be passed.
That you're talking about Xen. Despite Xen being UP only right now, guests still can have more than one vCPU, can't they?
Quoted 4 lines
I can update the commit message and the comment in
return_to_new_vcpu()to tell that it could be DTB address for boot cpu and/or for secondary CPUs HSM boot data or it will be better to add info about HSM boot data during and an introduction of secondary CPUs support?
As per above you want to deal with multi-vCPU guests right now.
Quoted 42 lines
Quoted 9 lines
Quoted 6 lines
Interrupts have to stay disabled across the restore. The trap entry logic implicitly clears hstatus.SPV, so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring SPV afterwards is non-trivial. Instead interrupts are simply kept off and sstatus.SPIE is set, so that SIE is restored from SPIE once sret has been executed.
As written this reads as if the guest would be responsible for doing this. Isn't it rather SRET itself which does this?
IIUC to which part you refer then yes, it is SRET itself which does this. So some re-wording should be done ...
Quoted 7 lines
Quoted 3 lines
Also, it follows what hardware will do
with real CPU which is also started with interrupts disabled.Further up, aiui, you talk about the host's interrupt state. How vCPU-s are started, however, is virtual interrupt state. Mixing both isn't very helpful.
...:
Interrupts have to stay disabled across the restore. The trap entry logic implicitly clears hstatus.SPV, so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring SPV afterwards is non-trivial. Hence interrupts are kept disabled, and sstatus.SPIE is set so that sret itself re-enables them (SIE := SPIE) as part of entering the guest.
Then it will be also need to update the comment inside
continue_new_vcpu()to:(quoted diff) +3
-4- * To avoid this, interrupts are kept disabled during the restore. - * Additionally, setting sstatus.SPIE=1 ensures that after sret is - * executed (as sstatus.SIE will be loaded from SPIE), HS-mode will - * continue to receive interrupts normally. + * To avoid this, interrupts are kept disabled during the restore, + * and sstatus.SPIE is set so that sret itself re-enables them + * (SIE := SPIE) as part of entering the guest. */
Quoted 1 line
Would it be the wording okay for you now?
I think so, yes.
Quoted 32 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/riscv/entry.S +0
-0@@ -143,3 +143,26 @@ FUNC(__context_switch)Quoted 10 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 150
Quoted 7 lines
xen/arch/riscv/entry.S +5
-0ret END(__context_switch) + +/* t0 is used as a temporary reg and is clobbered to oblivion */ +FUNC(return_to_new_vcpu) + /* Swap tp with sscratch */ + csrrw tp, CSR_SSCRATCH, tpWhat is this about? I'm not aware of any counterpart code, yet all on its own this I can't see it being overly useful.
It will be needed later when a guest will be able to launch to distinguish in
handle_trap()[1] if a trap is from guest or not. I can drop it for now and re-introduce it with the code ofhandle_trap()or as an option I could update the comment above to:/*
* SSCRATCH holds this hart's structpcpu_infowhile a guest
runs and
* is zero while Xen runs, so that a trap handler can tell the two
* apart: tp is Xen's pointer topcpu_infoin Xen context, but
belongs
* to the guest once sret has been executed. Establish that by
swapping
* the two here; the trap path swaps them back.
*/
csrrw tp,CSR_SSCRATCH, tp
Quoted 2 lines
(but then the last part will point to the part which isn't yet introduced so probably it will be better to drop this line for now)
Yes, introducing it together with the other related pieces is going to be more consistent and easier to follow.
Quoted 25 lines
Quoted 11 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 168
Quoted 7 lines
xen/arch/riscv/entry.S +7
-0+ /* Set guest mode to supervisor */ + li t0, SSTATUS_SPP + csrs CSR_SSTATUS, t0 + + /* Enter guest */ + sret +END(return_to_new_vcpu)Aiui SRET does not switch stacks. Shouldn't you therefore clear sp here? And perhaps also other GPRs, not the least ra? Exposing hypervisor register values to guests is, well, a bit of a problem.
Good point, sret leaves all GPRs as they are, so the guest would indeed see Xen's sp, ra and friends. Only a0 and a1 are architecturally meaningful for a booting hart, so I'll clear every other GPR right before sret.
I will add the following before sret:
/*
* 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
Quoted 7 lines
* 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
At which point discussing the clobbering of t0 in the comment ahead of the function also isn't needed anymore.
Jan
Re: [PATCH v2 38/39] xen/riscv: implement continue_new_vcpu()
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich
Quoted 7 lines · Oleksii Kurochko
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, restore hstatus and enter the guest through the newreturn_to_new_vcpu()path inentry.S, which loads sepc, passes the hart id in a0 and the DTB address in a1 as expected by the RISC-V boot protocol, sets sstatus.SPP and executes sret.Is this a requirement for all CPUs, or just for the boot one? (I can't quite see why secondary processors would need passing a DTB address.)
Quoted 8 lines
Quoted 5 lines
It is requirement for boot one. For secondary processors it is HSM boot data which is passed to
sbi_hsm_hart_start()and then intercpeted by Xen.At the moment of writing of this commit message we have only boot CPU and so only DTB could be passed.
That you're talking about Xen. Despite Xen being UP only right now, guests still can have more than one vCPU, can't they?
They can.
Quoted 7 lines
Quoted 5 lines
I can update the commit message and the comment in
return_to_new_vcpu()to tell that it could be DTB address for boot cpu and/or for secondary CPUs HSM boot data or it will be better to add info about HSM boot data during and an introduction of secondary CPUs support?As per above you want to deal with multi-vCPU guests right now.
Then I will update the commit message and the comment in return_to_new_vcpu() to say that a1 holds the DTB address for the boot vCPU, and for secondary vCPUs the opaque value the guest passed to SBI HSM hart_start().
For commit message:
- for a guest vCPU, restore hstatus and enter the guest through the new return_to_new_vcpu() path in entry.S, which loads sepc, passes the
hart id in a0 and, in a1, either the DTB address (boot vCPU) or the
opaque argument of SBI HSM hart_start() (secondary vCPUs), as
required by the RISC-V boot protocol and the SBI specification, sets sstatus.SPP and executes sret.
In the code:
/*
* .a1 holds the DTB address for the boot vCPU, or the opaque value
* passed to SBI HSM hart_start() for secondary vCPUs */
Quoted 33 lines
Quoted 25 lines
Quoted 11 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 168
Quoted 7 lines
xen/arch/riscv/entry.S +7
-0+ /* Set guest mode to supervisor */ + li t0, SSTATUS_SPP + csrs CSR_SSTATUS, t0 + + /* Enter guest */ + sret +END(return_to_new_vcpu)Aiui SRET does not switch stacks. Shouldn't you therefore clear sp here? And perhaps also other GPRs, not the least ra? Exposing hypervisor register values to guests is, well, a bit of a problem.
Good point, sret leaves all GPRs as they are, so the guest would indeed see Xen's sp, ra and friends. Only a0 and a1 are architecturally meaningful for a booting hart, so I'll clear every other GPR right before sret.
I will add the following before sret:
/*
* 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
(thisQuoted 7 lines
* 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
Quoted 2 lines
At which point discussing the clobbering of t0 in the comment ahead of the function also isn't needed anymore.
Indeed, I'll drop it.
~ Oleksii
Re: [PATCH v2 38/39] xen/riscv: implement continue_new_vcpu()
In reply to Oleksii KurochkoQuoted 119 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, restore hstatus and enter the guest through the newreturn_to_new_vcpu()path inentry.S, which loads sepc, passes the hart id in a0 and the DTB address in a1 as expected by the RISC-V boot protocol, sets sstatus.SPP and executes sret.Interrupts have to stay disabled across the restore. The trap entry logic implicitly clears hstatus.SPV, so an interrupt taken between the write of hstatus and sret would make sret return to HS-mode instead of VS-mode, and restoring SPV afterwards is non-trivial. Instead interrupts are simply kept off and sstatus.SPIE is set, so that SIE is restored from
SPIE once sret has been executed. Also, it follows what hardware will do with real CPU which is also started with interrupts disabled.
Introduce
get_cpu_info()andreset_stack_and_jump()inasm/current.h, needed by the above.get_cpu_info()is a macro rather than a static inline becauseasm/current.his pulled in by <xen/percpu.h> before
this_cpu()is defined and before <xen/sched.h> completes struct vcpu.
idle_loop()is added as a stub on purpose; its real implementation will come separately later.Signed-off-by Oleksii Kurochko <[email protected]>xen/arch/riscv/domain.c +43
-1@@ -8,10 +8,13 @@#include <xen/smp.h> #include <xen/vmap.h> +#include <asm/aia.h> +#include <asm/aplic.h> #include <asm/bitops.h> #include <asm/cpufeature.h> #include <asm/csr.h> #include <asm/current.h> +#include <asm/imsic.h> #include <asm/intc.h> #include <asm/mmio.h> #include <asm/riscv_encoding.h> @@ -140,9 +143,43 @@ static void vcpu_csr_init(struct vcpu *v) v->arch.hie =MIP_SGEIP ; } +static void schedule_tail (struct vcpu *prev); +static void noreturnidle_loop (void ); +void noreturnreturn_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); +else + { +/* +* During a context switch to a new vCPU, interrupts must be disabled +* to guarantee that the vCPU's CSR state can be safely restored into +* the hart without being clobbered by an interrupt trap. +* +* For example, when return_to_new_vcpu() finishes, it executes sret. +* At that point, the hart checks hstatus.SPV=1 and sstatus.SPP=1 in +* order to return from HS-mode into VS-mode. If an interrupt were to +* arrive before sret, the trap entry logic would implicitly clear +* hstatus.SPV to 0. Correctly restoring it afterwards is non-trivial, +* and if left as 0, sret would incorrectly return to HS-mode instead +* of VS-mode. +* +* To avoid this, interrupts are kept disabled during the restore. +* Additionally, setting sstatus.SPIE=1 ensures that after sret is +* executed (as sstatus.SIE will be loaded from SPIE), HS-mode will +* continue to receive interrupts normally. +*/ +local_irq_disable (); +csr_set (CSR_SSTATUS ,SSTATUS_SPIE ); + +csr_write (CSR_HSTATUS ,vcpu_guest_cpu_user_regs (current)->hstatus); + +reset_stack_and_jump (return_to_new_vcpu); + } }int arch_vcpu_create (struct vcpu *v)@@ -551,3 +588,8 @@ static void __init __maybe_unused build_assertions(void)*/ BUILD_BUG_ON (offsetof(struct cpu_info , guest_cpu_user_regs)); } + +static void noreturnidle_loop (void ) +{ +BUG_ON ("unimplemented" ); +}xen/arch/riscv/entry.S +0
-0
xen/arch/riscv/entry.S FUNC(__context_switch) · line 150
Quoted 8 lines
xen/arch/riscv/entry.S +5
-0@@ -143,3 +143,26 @@ FUNC(__context_switch) ret END(__context_switch) + +/* t0 is used as a temporary reg and is clobbered to oblivion */ +FUNC(return_to_new_vcpu) + /* Swap tp with sscratch */ + csrrw tp, CSR_SSCRATCH, tp
Why do we need a swap? After it, tp will be equal to zero, but we won't use it. Couldn't we do a basic csrw instead?
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v2 38/39] xen/riscv: implement continue_new_vcpu()
Quoted 11 lines
Quoted 11 lines
xen/arch/riscv/domain.c +4
-0+static void noreturnidle_loop (void ) +{ +BUG_ON ("unimplemented" ); +}xen/arch/riscv/entry.S +0
-0@@ -143,3 +143,26 @@ FUNC(__context_switch)
Quoted 10 lines
xen/arch/riscv/entry.S FUNC(__context_switch) · line 150
Quoted 7 lines
xen/arch/riscv/entry.S +5
-0ret END(__context_switch) + +/* t0 is used as a temporary reg and is clobbered to oblivion */ +FUNC(return_to_new_vcpu) + /* Swap tp with sscratch */ + csrrw tp, CSR_SSCRATCH, tpWhy do we need a swap? After it, tp will be equal to zero, but we won't use it. Couldn't we do a basic csrw instead?
SSCRATCH needs to hold pcpu_info while the guest runs and be 0 while Xen runs. The trap entry will then do csrrw tp, sscratch, tp, and the resulting tp says where the trap came from:
- Non-zero: the trap came from the guest. tp now points to pcpu_info and the guest's tp is parked in SSCRATCH.
- Zero: the trap came from Xen. Recover tp with csrr tp, sscratch and write SSCRATCH back to 0.
On trap entry the swap really is needed. No register is free there, so one instruction has to save the guest's tp and load pcpu_info at once.
But considering just the line you commented I think csrw could be enough. I will double-check during an introduction of this line in more appropriate patch.
Considering that it can't be clear for now why it is needed I will move this part to the patch (which updates handle_trap() with guest trap support handling).
Thanks.
~ Oleksii
PATCH v2 39/39 xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
In reply to Oleksii KurochkoIntroduce imsic_vsfile_attach() to initialize the AIA-related state needed for a vCPU to have a working guest interrupt file.
A guest (VS) interrupt file must be mapped to one of a pCPU's hardware interrupt files (if they exist), so the pCPU a vCPU will actually run on needs to be known first. arch_vcpu_create() is therefore not a suitable place to call vcpu_aia_init(), since the pCPU assigned to a vCPU can still change before it is first scheduled. To avoid reassigning the VS interrupt file id and remapping it to a different pCPU's hardware interrupt file, imsic_vsfile_attach() is called from a later point in the scheduling path (e.g. continue_new_vcpu()). Since it will end up being called from a non-__init context, it is not itself marked __init.
Introduce imsic_update_state() to update a vCPU's guest IMSIC state (the guest interrupt file id and the pCPU whose hardware interrupt file it is mapped to) as a single consistent unit. This state can be read concurrently, e.g. by a future helper that checks whether a vCPU has a pending IMSIC interrupt, though no such consumer exists yet at this stage, so it is protected by a lock.
---
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 parantethis aroud 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 | 2 + xen/arch/riscv/imsic.c | 146 +++++++++++++++++++++++------ xen/arch/riscv/include/asm/imsic.h | 2 + 3 files changed, 121 insertions(+), 29 deletions(-)
xen/arch/riscv/domain.c +2 -0
@@ -155,6 +155,8 @@ static void continue_new_vcpu(struct vcpu *prev)reset_stack_and_jump (idle_loop); else { +imsic_vsfile_attach (current); +/* * During a context switch to a new vCPU, interrupts must be disabled * to guarantee that the vCPU's CSR state can be safely restored into
xen/arch/riscv/imsic.c +117 -29
189 lines not on this page · raw message
xen/arch/riscv/include/asm/imsic.h +2 -0
@@ -125,4 +125,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 v2 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
Quoted 11 lines · Oleksii Kurochko
Introduce
imsic_vsfile_attach()to initialize the AIA-related state needed for a vCPU to have a working guest interrupt file.A guest (VS) interrupt file must be mapped to one of a pCPU's hardware interrupt files (if they exist), so the pCPU a vCPU will actually run on needs to be known first.
arch_vcpu_create()is therefore not a suitable place to callvcpu_aia_init(), since the pCPU assigned to a vCPU can still change before it is first scheduled. To avoid reassigning the VS interrupt file id and remapping it to a different pCPU's hardware interrupt file,imsic_vsfile_attach()is called from a later point in the scheduling path (e.g.continue_new_vcpu()).
Hmm, why does first-time handling need to be this different from the handling of a vCPU moving across pCPU-s? The sole difference should be "no state to load" vs "load state that was saved on the old pCPU".
Jan
Re: [PATCH v2 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Introduce
imsic_vsfile_attach()to initialize the AIA-related state needed for a vCPU to have a working guest interrupt file.A guest (VS) interrupt file must be mapped to one of a pCPU's hardware interrupt files (if they exist), so the pCPU a vCPU will actually run on needs to be known first.
arch_vcpu_create()is therefore not a suitable place to callvcpu_aia_init(), since the pCPU assigned to a vCPU can still change before it is first scheduled. To avoid reassigning the VS interrupt file id and remapping it to a different pCPU's hardware interrupt file,imsic_vsfile_attach()is called from a later point in the scheduling path (e.g.continue_new_vcpu()).
Quoted 4 lines
Hmm, why does first-time handling need to be this different from the handling of a vCPU moving across pCPU-s? The sole difference should be "no state to load" vs "load state that was saved on the old pCPU".
Generally I think it could be the same but not all the steps done in migration functions aren't needed for new vCPU (despite of the fact they aren't harmful). For new vCPU it seems to me it is enough only to attach h/w interrupt file to vCPU.
~ Oleksii
Re: [PATCH v2 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
Quoted 14 lines · Oleksii Kurochko
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Introduce
imsic_vsfile_attach()to initialize the AIA-related state needed for a vCPU to have a working guest interrupt file.A guest (VS) interrupt file must be mapped to one of a pCPU's hardware interrupt files (if they exist), so the pCPU a vCPU will actually run on needs to be known first.
arch_vcpu_create()is therefore not a suitable place to callvcpu_aia_init(), since the pCPU assigned to a vCPU can still change before it is first scheduled. To avoid reassigning the VS interrupt file id and remapping it to a different pCPU's hardware interrupt file,imsic_vsfile_attach()is called from a later point in the scheduling path (e.g.continue_new_vcpu()).
Quoted 8 lines
Quoted 3 lines
Hmm, why does first-time handling need to be this different from the handling of a vCPU moving across pCPU-s? The sole difference should be "no state to load" vs "load state that was saved on the old pCPU".
Generally I think it could be the same but not all the steps done in migration functions aren't needed for new vCPU (despite of the fact they aren't harmful). For new vCPU it seems to me it is enough only to attach h/w interrupt file to vCPU.
Sure, yet still imo you want to aim at re-using as much code as possible, especially when otherwise you introduce multiple very similar but not exactly identical variants of logic.
Jan
Re: [PATCH v2 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
In reply to Oleksii KurochkoQuoted 14 lines
Introduce
imsic_vsfile_attach()to initialize the AIA-related state needed for a vCPU to have a working guest interrupt file.A guest (VS) interrupt file must be mapped to one of a pCPU's hardware interrupt files (if they exist), so the pCPU a vCPU will actually run on needs to be known first.
arch_vcpu_create()is therefore not a suitable place to callvcpu_aia_init(), since the pCPU assigned to a vCPU can still change before it is first scheduled. To avoid reassigning the VS interrupt file id and remapping it to a different pCPU's hardware interrupt file,imsic_vsfile_attach()is called from a later point in the scheduling path (e.g.continue_new_vcpu()). Since it will end up being called from a non-__init context, it is not itself marked__init.
Quoted 6 lines
Introduce
imsic_update_state()to update a vCPU's guest IMSIC state (the guest interrupt file id and the pCPU whose hardware interrupt file it is mapped to) as a single consistent unit. This state can be read concurrently, e.g. by a future helper that checks whether a vCPU has a pending IMSIC interrupt, though no such consumer exists yet at this stage, so it is protected by a lock.
I have difficulties to clearly understand the aim of this function. In fact, if we want to know if a vCPU has a pending interrupts, wouldn't be enough to check hgeip[guest_file_id]?
In any case, why does updating vCPU's guest IMSIC state would be required?
Quoted 71 lines
Signed-off-by Oleksii Kurochko <[email protected]>xen/arch/riscv/domain.c +2
-0@@ -155,6 +155,8 @@ static void continue_new_vcpu(struct vcpu *prev)reset_stack_and_jump (idle_loop); else { +imsic_vsfile_attach (current); +/* * During a context switch to a new vCPU, interrupts must be disabled * to guarantee that the vCPU's CSR state can be safely restored into xen/arch/riscv/imsic.c +32
-1@@ -212,7 +212,14 @@ unsigned int vcpu_guest_file_id(const struct vcpu *v)void imsic_update_state (struct vcpu *v,unsigned int guest_file_id) { -BUG_ON ("unimplemented\n" ); +unsigned long flags; +struct vimsic_state *vimsic_state = v->arch.vimsic_state; +unsigned int cpu = guest_file_id ? v->processor :NR_CPUS ; + +write_lock_irqsave (&vimsic_state->vsfile_lock, flags); + vimsic_state->guest_file_id = guest_file_id; + vimsic_state->vsfile_cpu = cpu; +write_unlock_irqrestore (&vimsic_state->vsfile_lock, flags); }void __init imsic_ids_local_delivery (bool enable)@@ -640,6 +647,16 @@ struct imsic_vsfile_data {struct imsic_mrif *mrif; }; +/* +* Number of 64-bit EIx groups needed to cover all the interrupt identities an +* IMSIC interrupt file provides, which are 0 (never valid, but it still +* occupies a bit) up to and including imsic_cfg.nr_ids. +*/ +static unsigned int imsic_nr_eix (void ) +{ +return DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,BITS_PER_TYPE (uint64_t)); +} +/* * Execute func() on the pCPU which owns the IMSIC interrupt file func() is * going to work with. @@ -1138,20 +1155,90 @@ static void cf_check imsic_vsfile_local_update(void *data)csr_write (CSR_VSISELECT , old_vsiselect); } +/* +* Point the vCPU's HSTATUS.VGEIN at the guest interrupt file it has been +* given. It is applied to the hart when the vCPU's context is restored. +*/ +static void vcpu_set_vgein (struct vcpu *v,unsigned int vsfile_id) +{ +unsigned long hstatus =vcpu_guest_cpu_user_regs (v)->hstatus; + + hstatus &= ~HSTATUS_VGEIN ; + hstatus |=MASK_INSR (vsfile_id,HSTATUS_VGEIN ); + +vcpu_guest_cpu_user_regs (v)->hstatus = hstatus; +} +
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_update(void *data) · line 1181
Quoted 10 lines
xen/arch/riscv/imsic.c +10
-0+/* +* Take a h/w guest interrupt file of 'cpu' for the vCPU: zero the file out, +* map it into the domain's G-stage at the vCPU's virtual IMSIC page and +* record the new location in the per-vCPU IMSIC state. +* +* HSTATUS.VGEIN is deliberately left alone: the vCPU may be pointed at the +* file only when the file already holds the vCPU's interrupt state, which in +* the case of imsic_migrate_vcpu() happens only after the old file has been +* moved to the new one. Thereby it is up to the caller to call +* vcpu_set_vgein() at the right moment.
Nit: would clarify the paragraph here:
HSTATUS.VGEIN is deliberately left alone: it must only point at a file which already holds the vCPU's interrupt state. The file taken here is zeroed, so for imsic_migrate_vcpu() VGEIN can only be updated once the state of the old file has been copied into it. Thereby it is up to the caller to call vcpu_set_vgein() at the right moment.
Quoted 99 lines
xen/arch/riscv/imsic.c +55
-24+* +* Returns the id of the taken interrupt file, or 0 if none could be taken, in +* which case the domain is crashed. +*/ +static unsigned int imsic_vsfile_acquire (struct vcpu *v,unsigned int cpu) +{ +struct imsic_vsfile_data vsfile_data = { .nr_eix =imsic_nr_eix () }; +unsigned int vsfile_id; +int rc; + + vsfile_id =vgein_assign (v); +if ( !vsfile_id ) + { +/* +* vgein_assign() returns 0 when no free h/w guest interrupt file is +* available. s/w guest interrupt files aren't supported yet, so such +* a vCPU can't be run. +*/ +domain_crash (v->domain, +"%pv: no free h/w guest interrupt file on CPU%u\n" , + v, cpu); +return 0 ; + } + + vsfile_data.hgei = vsfile_id; + +/* The file could still hold the state of its previous owner */ +imsic_call_on_cpu (cpu, imsic_vsfile_local_clear, &vsfile_data); + + rc =imsic_map_guest_file (v, vsfile_id); +if ( rc ) + { +vgein_release (v, vsfile_id, cpu); + +/* Can't continue w/o correctly mapped IMSIC interrupt file */ +domain_crash (v->domain, +"%pv: failed to map h/w guest interrupt file %u: %d\n" , + v, vsfile_id, rc); +return 0 ; + } + +imsic_update_state (v, vsfile_id); + +return vsfile_id; +} +void imsic_migrate_vcpu (struct vcpu *v) { -unsigned int new_vsfile_hgei; +unsigned int new_vsfile_id;unsigned int new_vsfile_cpu; -unsigned int nr_hw_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 , -BITS_PER_TYPE (uint64_t)); -struct imsic_vsfile_data vsfile_data = { - .nr_eix = nr_hw_eix, - }; +unsigned int nr_hw_eix =imsic_nr_eix ();struct vimsic_state *imsic_state = v->arch.vimsic_state;unsigned long flags;unsigned int old_vsfile_id;unsigned int old_vsfile_cpu;struct imsic_mrif tmrif = { }; +struct imsic_vsfile_data vsfile_data = { + .nr_eix = nr_hw_eix, + .mrif = &tmrif, + };/* * The scheduler can mark a freshly created vCPU's unit as migrated and @@ -1189,25 +1276,10 @@ void imsic_migrate_vcpu(struct vcpu *v)*/ new_vsfile_cpu = v->processor; - new_vsfile_hgei =vgein_assign (v); - -/* We don't support SW interrupt files at the moment. */ -BUG_ON (!new_vsfile_hgei); - - vsfile_data.hgei = new_vsfile_hgei; - -/* Zero-out new IMSIC VS-file */ -imsic_call_on_cpu (new_vsfile_cpu, imsic_vsfile_local_clear, &vsfile_data); - -/* Update G-stage mapping for the new IMSIC VS-file */ -if (imsic_map_guest_file (v, new_vsfile_hgei) ) - { -domain_crash (v->domain,"Migration to hw interrupt file failed\n" ); - +/* Zero-out, map and start to use the new IMSIC VS-file */ + new_vsfile_id =imsic_vsfile_acquire (v, new_vsfile_cpu); +if ( !new_vsfile_id )return ; - } - -imsic_update_state (v, new_vsfile_hgei);/* * TODO: Modify the relevant translation tables at all IOMMUs so that MSIs
xen/arch/riscv/imsic.c void imsic_migrate_vcpu(struct vcpu *v) · line 1320
Quoted 5 lines
xen/arch/riscv/imsic.c +1
-1@@ -1245,7 +1317,7 @@ void imsic_migrate_vcpu(struct vcpu *v)vgein_release (v, old_vsfile_id, old_vsfile_cpu);/* Restore register state in the new IMSIC VS-file */ - vsfile_data.mrif = &tmrif; + vsfile_data.hgei = new_vsfile_id;
Isn't this already done by imsic_vsfile_acquire()? Couldn't we pass a &vsfile_data in their arg so it could fill it instead of allocating one in the stack?
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v2 39/39] xen/riscv: introduce IMSIC h/w interrupt file attaching to vcpu
Quoted 20 lines · Baptiste Le Duc
Quoted 14 lines
Introduce
imsic_vsfile_attach()to initialize the AIA-related state needed for a vCPU to have a working guest interrupt file.A guest (VS) interrupt file must be mapped to one of a pCPU's hardware interrupt files (if they exist), so the pCPU a vCPU will actually run on needs to be known first.
arch_vcpu_create()is therefore not a suitable place to callvcpu_aia_init(), since the pCPU assigned to a vCPU can still change before it is first scheduled. To avoid reassigning the VS interrupt file id and remapping it to a different pCPU's hardware interrupt file,imsic_vsfile_attach()is called from a later point in the scheduling path (e.g.continue_new_vcpu()). Since it will end up being called from a non-__init context, it is not itself marked__init.Quoted 6 lines
Introduce
imsic_update_state()to update a vCPU's guest IMSIC state (the guest interrupt file id and the pCPU whose hardware interrupt file it is mapped to) as a single consistent unit. This state can be read concurrently, e.g. by a future helper that checks whether a vCPU has a pending IMSIC interrupt, though no such consumer exists yet at this stage, so it is protected by a lock.
Quoted 3 lines
I have difficulties to clearly understand the aim of this function. In fact, if we want to know if a vCPU has a pending interrupts, wouldn't be enough to check hgeip[guest_file_id]?
Lets consider the function which checks pending interrupts:
bool imsic_has_interrupt(const struct vcpu *vcpu)
{
struct vimsic_state *imsic_state = vcpu->arch.vimsic_state;
unsigned long flags;
bool ret = false;
/*
* The IMSIC SW-file directly injects interrupt via hvip so
* only check for interrupt when IMSIC VS-file is being used.
*/
read_lock_irqsave(&imsic_state->vsfile_lock, flags);
if ( imsic_state->vsfile_cpu != NR_CPUS )
ret = !!(csr_read(CSR_HGEIP) & BIT(imsic_state->guest_file_id,
UL));
read_unlock_irqrestore(&imsic_state->vsfile_lock, flags);
return ret;
}You are right that it is enough hgeip[guest_file_id] but at the same time we won't to be sure that guest_file_id wasn't changed during this check, right? Otherwise we could accidentally check guest_file_id of ohter vCPU.
And so we need imsic_update_state() to have abitility atomically update guest_file_id (and also vsfile_pcpu).
I will re-write this part with dropping part of pending interrupts it will be just clear from the code of imsic_has_interrupt() when it will be needed to be introduced:
``` 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. ```
Quoted 3 lines
In any case, why does updating vCPU's guest IMSIC state would be required?
If it is migrating from one pCPU to another we will need to update guest_file_id as on new pCPU guest_file_id could be different from what we had for this vCPU on old pCPU + we have to update vsfile_pcpu as a new pCPU will be an owner of guest file id of vCPU.
Quoted 79 lines
Quoted 79 lines
Signed-off-by Oleksii Kurochko <[email protected]>xen/arch/riscv/domain.c +2
-0@@ -155,6 +155,8 @@ static void continue_new_vcpu(struct vcpu *prev)reset_stack_and_jump (idle_loop); else { +imsic_vsfile_attach (current); +/* * During a context switch to a new vCPU, interrupts must be disabled * to guarantee that the vCPU's CSR state can be safely restored into xen/arch/riscv/imsic.c +40
-1@@ -212,7 +212,14 @@ unsigned int vcpu_guest_file_id(const struct vcpu *v)void imsic_update_state (struct vcpu *v,unsigned int guest_file_id) { -BUG_ON ("unimplemented\n" ); +unsigned long flags; +struct vimsic_state *vimsic_state = v->arch.vimsic_state; +unsigned int cpu = guest_file_id ? v->processor :NR_CPUS ; + +write_lock_irqsave (&vimsic_state->vsfile_lock, flags); + vimsic_state->guest_file_id = guest_file_id; + vimsic_state->vsfile_cpu = cpu; +write_unlock_irqrestore (&vimsic_state->vsfile_lock, flags); }void __init imsic_ids_local_delivery (bool enable)@@ -640,6 +647,16 @@ struct imsic_vsfile_data {struct imsic_mrif *mrif; }; +/* +* Number of 64-bit EIx groups needed to cover all the interrupt identities an +* IMSIC interrupt file provides, which are 0 (never valid, but it still +* occupies a bit) up to and including imsic_cfg.nr_ids. +*/ +static unsigned int imsic_nr_eix (void ) +{ +return DIV_ROUND_UP (imsic_cfg.nr_ids +1 ,BITS_PER_TYPE (uint64_t)); +} +/* * Execute func() on the pCPU which owns the IMSIC interrupt file func() is * going to work with. @@ -1138,20 +1155,90 @@ static void cf_check imsic_vsfile_local_update(void *data)csr_write (CSR_VSISELECT , old_vsiselect); } +/* +* Point the vCPU's HSTATUS.VGEIN at the guest interrupt file it has been +* given. It is applied to the hart when the vCPU's context is restored. +*/ +static void vcpu_set_vgein (struct vcpu *v,unsigned int vsfile_id) +{ +unsigned long hstatus =vcpu_guest_cpu_user_regs (v)->hstatus; + + hstatus &= ~HSTATUS_VGEIN ; + hstatus |=MASK_INSR (vsfile_id,HSTATUS_VGEIN ); + +vcpu_guest_cpu_user_regs (v)->hstatus = hstatus; +} + +/* +* Take a h/w guest interrupt file of 'cpu' for the vCPU: zero the file out, +* map it into the domain's G-stage at the vCPU's virtual IMSIC page and +* record the new location in the per-vCPU IMSIC state. +* +* HSTATUS.VGEIN is deliberately left alone: the vCPU may be pointed at the +* file only when the file already holds the vCPU's interrupt state, which in +* the case of imsic_migrate_vcpu() happens only after the old file has been
Quoted 8 lines
xen/arch/riscv/imsic.c static void cf_check imsic_vsfile_local_update(void *data) · line 1181
Quoted 2 lines
xen/arch/riscv/imsic.c +2
-0+* moved to the new one. Thereby it is up to the caller to call +* vcpu_set_vgein() at the right moment. Nit: would clarify the paragraph here:
HSTATUS.VGEIN is deliberately left alone: it must only point at a file which already holds the vCPU's interrupt state. The file taken here is zeroed, so forimsic_migrate_vcpu()VGEIN can only be updated once the state of the old file has been copied into it. Thereby it is up to the caller to callvcpu_set_vgein()at the right moment.
I will use your suggesttion.
Quoted 101 lines
Quoted 101 lines
xen/arch/riscv/imsic.c +55
-24+* +* Returns the id of the taken interrupt file, or 0 if none could be taken, in +* which case the domain is crashed. +*/ +static unsigned int imsic_vsfile_acquire (struct vcpu *v,unsigned int cpu) +{ +struct imsic_vsfile_data vsfile_data = { .nr_eix =imsic_nr_eix () }; +unsigned int vsfile_id; +int rc; + + vsfile_id =vgein_assign (v); +if ( !vsfile_id ) + { +/* +* vgein_assign() returns 0 when no free h/w guest interrupt file is +* available. s/w guest interrupt files aren't supported yet, so such +* a vCPU can't be run. +*/ +domain_crash (v->domain, +"%pv: no free h/w guest interrupt file on CPU%u\n" , + v, cpu); +return 0 ; + } + + vsfile_data.hgei = vsfile_id; + +/* The file could still hold the state of its previous owner */ +imsic_call_on_cpu (cpu, imsic_vsfile_local_clear, &vsfile_data); + + rc =imsic_map_guest_file (v, vsfile_id); +if ( rc ) + { +vgein_release (v, vsfile_id, cpu); + +/* Can't continue w/o correctly mapped IMSIC interrupt file */ +domain_crash (v->domain, +"%pv: failed to map h/w guest interrupt file %u: %d\n" , + v, vsfile_id, rc); +return 0 ; + } + +imsic_update_state (v, vsfile_id); + +return vsfile_id; +} +void imsic_migrate_vcpu (struct vcpu *v) { -unsigned int new_vsfile_hgei; +unsigned int new_vsfile_id;unsigned int new_vsfile_cpu; -unsigned int nr_hw_eix =DIV_ROUND_UP (imsic_cfg.nr_ids +1 , -BITS_PER_TYPE (uint64_t)); -struct imsic_vsfile_data vsfile_data = { - .nr_eix = nr_hw_eix, - }; +unsigned int nr_hw_eix =imsic_nr_eix ();struct vimsic_state *imsic_state = v->arch.vimsic_state;unsigned long flags;unsigned int old_vsfile_id;unsigned int old_vsfile_cpu;struct imsic_mrif tmrif = { }; +struct imsic_vsfile_data vsfile_data = { + .nr_eix = nr_hw_eix, + .mrif = &tmrif, + };/* * The scheduler can mark a freshly created vCPU's unit as migrated and @@ -1189,25 +1276,10 @@ void imsic_migrate_vcpu(struct vcpu *v)*/ new_vsfile_cpu = v->processor; - new_vsfile_hgei =vgein_assign (v); - -/* We don't support SW interrupt files at the moment. */ -BUG_ON (!new_vsfile_hgei); - - vsfile_data.hgei = new_vsfile_hgei; - -/* Zero-out new IMSIC VS-file */ -imsic_call_on_cpu (new_vsfile_cpu, imsic_vsfile_local_clear, &vsfile_data); - -/* Update G-stage mapping for the new IMSIC VS-file */ -if (imsic_map_guest_file (v, new_vsfile_hgei) ) - { -domain_crash (v->domain,"Migration to hw interrupt file failed\n" ); - +/* Zero-out, map and start to use the new IMSIC VS-file */ + new_vsfile_id =imsic_vsfile_acquire (v, new_vsfile_cpu); +if ( !new_vsfile_id )return ; - } - -imsic_update_state (v, new_vsfile_hgei);/* * TODO: Modify the relevant translation tables at all IOMMUs so that MSIs @@ -1245,7 +1317,7 @@ void imsic_migrate_vcpu(struct vcpu *v)vgein_release (v, old_vsfile_id, old_vsfile_cpu);
Quoted 4 lines
xen/arch/riscv/imsic.c void imsic_migrate_vcpu(struct vcpu *v) · line 1320
Quoted 3 lines
xen/arch/riscv/imsic.c +1
-1/* Restore register state in the new IMSIC VS-file */ - vsfile_data.mrif = &tmrif; + vsfile_data.hgei = new_vsfile_id;Isn't this already done by
imsic_vsfile_acquire()?
It isn't done by imsic_vsfile_acquire() as this function returns new vsfile id.
Quoted 3 lines
Couldn't we pass a
&vsfile_datain their arg so it could fill it instead of allocating one in the stack?
We could done in that way but then we will need a stack variable for vsfile_data structure in imsic_vsfile_attach() which will consume more stack memory then just one local variable for new_vsfile_id currently declared in imsic_vsfile_attach(). But I will double-check if it won't be really better just refactor something and pass &vsfile_data as you suggested. Thanks.
~ Oleksii