hw/riscv/virt: only describe PLIC contexts that exist
PATCH 1/1 hw/riscv/virt: only describe PLIC contexts that exist
From: Ralf Ramsauer <[email protected]>
The PLIC device is created from the topology string built by riscv_plic_hart_config_string(), which gives a hart a supervisor context only if that hart actually has supervisor mode. The device tree, in contrast, lists a machine and a supervisor context for every hart, unconditionally.
The two have disagreed since commit 7d10ff8a4d ("hw/riscv: virt: Use the PLIC config helper function") taught the device about harts without S-mode and left the device tree behind. Boot with
-cpu rv64,s=false,u=false -smp 4
and the PLIC implements four contexts while the device tree advertises eight. Contexts are numbered by their position in 'interrupts-extended', so a guest that takes its context number from the property gets it wrong for every hart but the first: hart 1 drives the context of hart 2, and the upper harts address contexts the PLIC never created. This is what an M-mode kernel on the virt machine runs into; sifive_u already omits the supervisor entry for its management hart.
Emit the supervisor entry only for the harts that have supervisor mode.
--- hw/riscv/virt.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
hw/riscv/virt.c +15 -6
@@ -165,14 +165,23 @@ static void create_fdt_socket_plic(RISCVVirtState *s, plic_cells[cpu *2 +1 ] =cpu_to_be32 (IRQ_S_EXT ); } }else { - cells_length = s->soc[socket].num_harts *4 ; - plic_cells =g_new0 (uint32_t, cells_length); + plic_cells =g_new0 (uint32_t, s->soc[socket].num_harts *4 ); + cells_length =0 ;for (cpu =0 ; cpu < s->soc[socket].num_harts; cpu++) { - plic_cells[cpu *4 +0 ] =cpu_to_be32 (intc_phandles[cpu]); - plic_cells[cpu *4 +1 ] =cpu_to_be32 (IRQ_M_EXT ); - plic_cells[cpu *4 +2 ] =cpu_to_be32 (intc_phandles[cpu]); - plic_cells[cpu *4 +3 ] =cpu_to_be32 (IRQ_S_EXT ); +CPURISCVState *env = &s->soc[socket].harts[cpu].env; + + plic_cells[cells_length++] =cpu_to_be32 (intc_phandles[cpu]); + plic_cells[cells_length++] =cpu_to_be32 (IRQ_M_EXT ); + +/* +* The PLIC implements a supervisor context only for harts that +* have supervisor mode, see riscv_plic_hart_config_string(). +*/ +if (riscv_has_ext (env,RVS )) { + plic_cells[cells_length++] =cpu_to_be32 (intc_phandles[cpu]); + plic_cells[cells_length++] =cpu_to_be32 (IRQ_S_EXT ); + } } }
base-commit: 81ce3a87737aa50716c42db8886082d12783e0a1
Signature · 1 line
2.54.0
Hello,
Quoted 39 lines · Ralf Ramsauer
From: Ralf Ramsauer <[email protected]>
The PLIC device is created from the topology string built by
riscv_plic_hart_config_string(), which gives a hart a supervisor context only if that hart actually has supervisor mode. The device tree, in contrast, lists a machine and a supervisor context for every hart, unconditionally.The two have disagreed since commit
7d10ff8a4d("hw/riscv: virt: Use the PLIC config helper function") taught the device about harts without S-mode and left the device tree behind. Boot with-cpu rv64,s=false,u=false -smp 4and the PLIC implements four contexts while the device tree advertises eight. Contexts are numbered by their position in 'interrupts-extended', so a guest that takes its context number from the property gets it wrong for every hart but the first: hart 1 drives the context of hart 2, and the upper harts address contexts the PLIC never created. This is what an M-mode kernel on the virt machine runs into;
sifive_ualready omits the supervisor entry for its management hart.Emit the supervisor entry only for the harts that have supervisor mode.
Fixes 7d10ff8a4d ("hw/riscv: virt: Use the PLIC config helper function")Signed-off-by Ralf Ramsauer <[email protected]>--- hw/riscv/virt.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)hw/riscv/virt.c +0
-1@@ -165,14 +165,23 @@ static void create_fdt_socket_plic(RISCVVirtState *s, plic_cells[cpu *2 +1 ] =cpu_to_be32 (IRQ_S_EXT ); } }else { - cells_length = s->soc[socket].num_harts *4 ;
hw/riscv/virt.c static void create_fdt_socket_plic(RISCVVirtState *s, · line 172
Quoted 10 lines
hw/riscv/virt.c +3
-5- plic_cells =g_new0 (uint32_t, cells_length); + plic_cells =g_new0 (uint32_t, s->soc[socket].num_harts *4 ); + cells_length =0 ;for (cpu =0 ; cpu < s->soc[socket].num_harts; cpu++) { - plic_cells[cpu *4 +0 ] =cpu_to_be32 (intc_phandles[cpu]); - plic_cells[cpu *4 +1 ] =cpu_to_be32 (IRQ_M_EXT ); - plic_cells[cpu *4 +2 ] =cpu_to_be32 (intc_phandles[cpu]); - plic_cells[cpu *4 +3 ] =cpu_to_be32 (IRQ_S_EXT ); +CPURISCVState *env = &s->soc[socket].harts[cpu].env;
There are a few things to consider here. First is that we have a very similar PLIC code in the sifive_u.c board. I believe we want this fix also applied there.
Second, we should avoid accessing CPU internals in board code. In an ideal world the board would be completely oblivious to what the CPU is about, e.g. its extensions and accelerator mode. In practice we have to account for accelerator differences since KVM and TCG requires different stuff w.r.t irq controllers and timers and so on, and at least for now this code is being open coded in the boards.
For the problem you're addressing here I would like to avoid making the board accessing the CPU env and checking for RVS. My suggestion here is:
- we need a pre-patch that adds a helper in cpu.c that tells whether a CPU has supervisor mode. Something like:
(cpu.c, could be right after "riscv_cpu_is_vendor")
bool riscv_cpu_has_supervisor_mode(RISCVCPU *cpu)
{
#ifndef CONFIG_USER_ONLY
if (kvm_enabled()) {
return true;
}
return riscv_has_ext(&cpu->env, RVS);#else
return false;
#endif
}
same thing in cpu.h. The justification for this helper in a separated patch is that "next patch needs to verify supervisor mode for CPUs in hw/riscv board code", or something around those lines.
- in this patch you can then do:
hw/riscv/virt.c static void create_fdt_socket_plic(RISCVVirtState *s, · line 184
Quoted 4 lines
hw/riscv/virt.c +4
-0+if (riscv_cpu_has_supervisor_mode (s->soc[socket].harts[cpu])) { + plic_cells[cells_length++] =cpu_to_be32 (intc_phandles[cpu]); + plic_cells[cells_length++] =cpu_to_be32 (IRQ_S_EXT ); + }
And we're at least trying to obfuscating the CPU internals from the board a bit.
And please do the same in sifive_u.c. Don't need to be in this same patch, so feel free to add another patch just for the sifive_u board.
Thanks,
Daniel
Quoted 17 lines
hw/riscv/virt.c +12
-0+ + plic_cells[cells_length++] =cpu_to_be32 (intc_phandles[cpu]); + plic_cells[cells_length++] =cpu_to_be32 (IRQ_M_EXT ); + +/* +* The PLIC implements a supervisor context only for harts that +* have supervisor mode, see riscv_plic_hart_config_string(). +*/ +if (riscv_has_ext (env,RVS )) { + plic_cells[cells_length++] =cpu_to_be32 (intc_phandles[cpu]); + plic_cells[cells_length++] =cpu_to_be32 (IRQ_S_EXT ); + } } }base-commit:
81ce3a87737aa50716c42db8886082d12783e0a1
Hi Daniel,
Quoted 44 lines · Daniel Henrique Barboza
Hello,
Quoted 41 lines · Ralf Ramsauer
From: Ralf Ramsauer <[email protected]>
The PLIC device is created from the topology string built by
riscv_plic_hart_config_string(), which gives a hart a supervisor context only if that hart actually has supervisor mode. The device tree, in contrast, lists a machine and a supervisor context for every hart, unconditionally.The two have disagreed since commit
7d10ff8a4d("hw/riscv: virt: Use the PLIC config helper function") taught the device about harts without S-mode and left the device tree behind. Boot with-cpu rv64,s=false,u=false -smp 4
and the PLIC implements four contexts while the device tree advertises eight. Contexts are numbered by their position in 'interrupts-extended', so a guest that takes its context number from the property gets it wrong for every hart but the first: hart 1 drives the context of hart 2, and the upper harts address contexts the PLIC never created. This is what an M-mode kernel on the virt machine runs into;
sifive_ualready omits the supervisor entry for its management hart.Emit the supervisor entry only for the harts that have supervisor mode.
Fixes 7d10ff8a4d ("hw/riscv: virt: Use the PLIC config helper function")Signed-off-by Ralf Ramsauer <[email protected]>---
hw/riscv/virt.c| 21 +++++++++++++++------1 file changed, 15 insertions(+), 6 deletions(-)hw/riscv/virt.c +0
-2@@ -165,14 +165,23 @@ static void create_fdt_socket_plic(RISCVVirtState *s, plic_cells[cpu * 2 + 1] = cpu_to_be32(IRQ_S_EXT); } } else { - cells_length = s->soc[socket].num_harts * 4; - plic_cells = g_new0(uint32_t, cells_length);
Quoted 8 lines
hw/riscv/virt.c static void create_fdt_socket_plic(RISCVVirtState *s, · line 172
Quoted 8 lines
hw/riscv/virt.c +3
-4+ plic_cells = g_new0(uint32_t, s->soc[socket].num_harts * 4); + cells_length = 0; for (cpu = 0; cpu < s->soc[socket].num_harts; cpu++) { - plic_cells[cpu * 4 + 0] = cpu_to_be32(intc_phandles[cpu]); - plic_cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT); - plic_cells[cpu * 4 + 2] = cpu_to_be32(intc_phandles[cpu]); - plic_cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT); + CPURISCVState *env = &s->soc[socket].harts[cpu].env;
Thanks for the review, sorry for the late reply!
Okay, with your comments, and having the helper in mind, I now see that we can also get rid of that nasty if-elsery here, if we did something like:
has_m_mode = !kvm_enable(); // constant for the platform (applies on all harts for virt as well as sifive-u, afaict)
plic_cells = num_harts * (has_m_mode ? 4 : 2);for_each_hart(i) {
if (has_m_mode) // constant
fill_m_mode_ctx();
if (hart_has_supervisor) // dynamic per-hart
fill_s_mode_ctx(i); }
And we then have the almost same pattern for sifive_u, besides the fact that we always see m-mode there.
Which now could allow us to factor that whole logic out. I'll see what we could do here. Will test that, and provide a v2 soonish!
Thanks,
Ralf
Quoted 78 lines
There are a few things to consider here. First is that we have a very similar PLIC code in the
sifive_u.cboard. I believe we want this fix also applied there.Second, we should avoid accessing CPU internals in board code. In an ideal world the board would be completely oblivious to what the CPU is about, e.g. its extensions and accelerator mode. In practice we have to account for accelerator differences since KVM and TCG requires different stuff w.r.t irq controllers and timers and so on, and at least for now this code is being open coded in the boards.
For the problem you're addressing here I would like to avoid making the board accessing the CPU env and checking for RVS. My suggestion here is:
- we need a pre-patch that adds a helper in
cpu.cthat tells whether a CPU has supervisor
mode. Something like:(
cpu.c, could be right after "riscv_cpu_is_vendor")bool
riscv_cpu_has_supervisor_mode(RISCVCPU *cpu)
{
#ifndefCONFIG_USER_ONLY
if (kvm_enabled()) {
return true;
}return
riscv_has_ext(&cpu->env, RVS);
#else
return false;
#endif
}same thing in cpu.h. The justification for this helper in a separated patch is that "next patch needs to verify supervisor mode for CPUs in hw/riscv board code", or something around those lines.
- in this patch you can then do:
hw/riscv/virt.c static void · line 175
Quoted 3 lines
hw/riscv/virt.c +2
-0+ if (riscv_cpu_has_supervisor_mode(s- soc[socket].harts[cpu])) { + plic_cells[cells_length++] =
cpu_to_be32(intc_phandles[cpu]);hw/riscv/virt.c static void create_fdt_socket_plic(RISCVVirtState *s, · line 184
Quoted 2 lines
hw/riscv/virt.c +2
-0+ plic_cells[cells_length++] = cpu_to_be32(IRQ_S_EXT); + }And we're at least trying to obfuscating the CPU internals from the board a bit.
And please do the same in sifive_u.c. Don't need to be in this same patch, so feel free to add another patch just for the
sifive_uboard.Thanks,
DanielQuoted 20 lines
hw/riscv/virt.c +6
-0+ + plic_cells[cells_length++] =cpu_to_be32 (intc_phandles[cpu]); + plic_cells[cells_length++] = cpu_to_be32(IRQ_M_EXT); + + /* +* The PLIC implements a supervisor context only for harts that
+ * have supervisor mode, seeriscv_plic_hart_config_string().
+ */
+ if (riscv_has_ext(env, RVS)) {
+ plic_cells[cells_length++] =cpu_to_be32(intc_phandles[cpu]);
+ plic_cells[cells_length++] =cpu_to_be32(IRQ_S_EXT); + }
}
}base-commit:
81ce3a87737aa50716c42db8886082d12783e0a1