Skip to contentSkip to conversation

Trap and Emulate changes

PATCH In review [email protected] 3 participants 5 messages Feb 11 – Feb 12 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Trap and Emulate changes

From: Dave Patel <[email protected]>

---
 lib/sbi/sbi_illegal_insn.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +24 -1
index fa82264a..88079e39 100644
@@ -110,10 +110,33 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
 	return 0;
 }
 
+static int sbi_get_emulated_irq_insn(ulong insn, struct sbi_trap_regs *regs)
+{
+	ulong rs1_val	= GET_RS1(insn, regs);
+	ulong rs2_val	= GET_RS2(insn, regs);
+	//ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
+	ulong irq_val;
+
+	/*
+	// This is were the emualted irq vlaue is fetched
+	if (prev_mode == PRV_S && sbi_emulate_irq_read(&irq_val))
+		return truly_illegal_insn(insn, regs);
+	*/
+
+	// For testing
+	irq_val = rs1_val + rs2_val;
+
+	SET_RD(insn, regs, irq_val);
+
+	regs->mepc += 4;
+
+	return 0;
+}
+
 static const illegal_insn_func illegal_insn_table[32] = {
 	truly_illegal_insn, /* 0 */
 	truly_illegal_insn, /* 1 */
-	truly_illegal_insn, /* 2 */
+	sbi_get_emulated_irq_insn,   /* 2 */
 	misc_mem_opcode_insn, /* 3 */
 	truly_illegal_insn, /* 4 */
 	truly_illegal_insn, /* 5 */

--
2.43.0

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw
Quoted 34 lines · On Wed, Feb 11, 2026 at 3:30 PM <[email protected]> wrote:

From: Dave Patel <[email protected]>

---
 lib/sbi/sbi_illegal_insn.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +18 -0
index fa82264a..88079e39 100644
@@ -110,10 +110,33 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
        return 0;
 }
 
+static int sbi_get_emulated_irq_insn(ulong insn, struct sbi_trap_regs *regs)
+{
+       ulong rs1_val   = GET_RS1(insn, regs);
+       ulong rs2_val   = GET_RS2(insn, regs);
+       //ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
+       ulong irq_val;
+
+       /*
+       // This is were the emualted irq vlaue is fetched
+       if (prev_mode == PRV_S && sbi_emulate_irq_read(&irq_val))
+               return truly_illegal_insn(insn, regs);
+       */
+
+       // For testing
+       irq_val = rs1_val + rs2_val;
+
+       SET_RD(insn, regs, irq_val);
+

lib/sbi/sbi_illegal_insn.c static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) · line 139

Quoted 10 lines
lib/sbi/sbi_illegal_insn.c +6 -1
+       regs->mepc += 4;
+
+       return 0;
+}
+
 static const illegal_insn_func illegal_insn_table[32] = {
        truly_illegal_insn, /* 0 */
        truly_illegal_insn, /* 1 */
-       truly_illegal_insn, /* 2 */
+       sbi_get_emulated_irq_insn,   /* 2 */

We can't pick arbitrary instruction encoding for
trap-n-emulation of an interrupt controller.

If the goal is to trap-n-emulate an MMIO device
then use PMP based access faults.

Regards
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

A message that is not in the archive (sent off-list or not yet received): CAL2H45cQm_O-K-6weC-xeGNB0+yCHjQe1msS+JwHdKLKecM5CA@mail.gmail.com

#3 lore raw
Quoted 2 lines · Scott Bambrough

Is PMP universally available?

Yes, PMP is not universally available plus we might not have enough PMP regions.

Cleaner approach is to use an "ecall" based paravirt interrupt controller.

Regards,
Anup

Quoted 58 lines

Scott

Quoted 55 lines · Anup Patel
Quoted 34 lines · On Wed, Feb 11, 2026 at 3:30 PM <[email protected]> wrote:

From: Dave Patel <[email protected]>

---
 lib/sbi/sbi_illegal_insn.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +18 -0
index fa82264a..88079e39 100644
@@ -110,10 +110,33 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
        return 0;
 }
 
+static int sbi_get_emulated_irq_insn(ulong insn, struct sbi_trap_regs *regs)
+{
+       ulong rs1_val   = GET_RS1(insn, regs);
+       ulong rs2_val   = GET_RS2(insn, regs);
+       //ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
+       ulong irq_val;
+
+       /*
+       // This is were the emualted irq vlaue is fetched
+       if (prev_mode == PRV_S && sbi_emulate_irq_read(&irq_val))
+               return truly_illegal_insn(insn, regs);
+       */
+
+       // For testing
+       irq_val = rs1_val + rs2_val;
+
+       SET_RD(insn, regs, irq_val);
+

lib/sbi/sbi_illegal_insn.c static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) · line 139

Quoted 10 lines
lib/sbi/sbi_illegal_insn.c +6 -1
+       regs->mepc += 4;
+
+       return 0;
+}
+
 static const illegal_insn_func illegal_insn_table[32] = {
        truly_illegal_insn, /* 0 */
        truly_illegal_insn, /* 1 */
-       truly_illegal_insn, /* 2 */
+       sbi_get_emulated_irq_insn,   /* 2 */

We can't pick arbitrary instruction encoding for
trap-n-emulation of an interrupt controller.

If the goal is to trap-n-emulate an MMIO device
then use PMP based access faults.

Regards
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

Hi Anup,

Quoted 11 lines · Anup Patel
Quoted 2 lines · Scott Bambrough

Is PMP universally available?

Yes, PMP is not universally available plus we might not have enough PMP regions.

Cleaner approach is to use an "ecall" based paravirt interrupt controller.

ecall is already in the plan, bare-metal app will use the ecall to pop an VIRQ from the queue it belongs to.
So do you mean to use a trap handler in S-domain to fire the ecall when a MMIO address (simulates CLAIMI) is accessed?
But that means the emulation is implemented in the bare-metal app side, and other apps need to integrate the same work.
The original idea is to do the emulation in M-mode, so that all S-mode SW can stay unchanged.

Regards,
Raymond

Quoted 66 lines

Regards,
Anup

Quoted 58 lines

Scott

Quoted 55 lines · Anup Patel
Quoted 34 lines · On Wed, Feb 11, 2026 at 3:30 PM <[email protected]> wrote:

From: Dave Patel <[email protected]>

---
 lib/sbi/sbi_illegal_insn.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +18 -0
index fa82264a..88079e39 100644
@@ -110,10 +110,33 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
        return 0;
 }
 
+static int sbi_get_emulated_irq_insn(ulong insn, struct sbi_trap_regs *regs)
+{
+       ulong rs1_val   = GET_RS1(insn, regs);
+       ulong rs2_val   = GET_RS2(insn, regs);
+       //ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
+       ulong irq_val;
+
+       /*
+       // This is were the emualted irq vlaue is fetched
+       if (prev_mode == PRV_S && sbi_emulate_irq_read(&irq_val))
+               return truly_illegal_insn(insn, regs);
+       */
+
+       // For testing
+       irq_val = rs1_val + rs2_val;
+
+       SET_RD(insn, regs, irq_val);
+

lib/sbi/sbi_illegal_insn.c static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) · line 139

Quoted 10 lines
lib/sbi/sbi_illegal_insn.c +6 -1
+       regs->mepc += 4;
+
+       return 0;
+}
+
 static const illegal_insn_func illegal_insn_table[32] = {
        truly_illegal_insn, /* 0 */
        truly_illegal_insn, /* 1 */
-       truly_illegal_insn, /* 2 */
+       sbi_get_emulated_irq_insn,   /* 2 */

We can't pick arbitrary instruction encoding for
trap-n-emulation of an interrupt controller.

If the goal is to trap-n-emulate an MMIO device
then use PMP based access faults.

Regards
Anup

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

A message that is not in the archive (sent off-list or not yet received): CAEigVW8cs7=f0xk-QZN2vKTTg-=dU9eoof=RrMHqzj9t8Yi=4Q@mail.gmail.com

#6 lore raw
Quoted 6 lines · Dave Patel

Thanks Scott, Thanks Anup. Very much appreciated.

We have already been using ecall, I can extend it to use it for paravirt for 'IRQ'.

The only downside is ecall is an extension rather than trap and emulate (though it resides within trap and emulate functionality).

You don't need to define a new SBI extension for paravirt interrupt controller.

OpenSBI can emulate RPMI System MSI over SBI MPXY channel as the paravirt interrupt controller. There are some improvements required in the RPMI System MSI service group to achieve this which I will propose soon.

Regards,
Anup

Quoted 79 lines

Thanks for the feedback.

Thanks and regards
Dave

Quoted 72 lines · Anup Patel
Quoted 2 lines · Scott Bambrough

Is PMP universally available?

Yes, PMP is not universally available plus we might not have enough PMP regions.

Cleaner approach is to use an "ecall" based paravirt interrupt controller.

Regards,
Anup

Quoted 58 lines

Scott

Quoted 55 lines · Anup Patel
Quoted 34 lines · On Wed, Feb 11, 2026 at 3:30 PM <[email protected]> wrote:

From: Dave Patel <[email protected]>

---
 lib/sbi/sbi_illegal_insn.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +18 -0
index fa82264a..88079e39 100644
@@ -110,10 +110,33 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
        return 0;
 }
 
+static int sbi_get_emulated_irq_insn(ulong insn, struct sbi_trap_regs *regs)
+{
+       ulong rs1_val   = GET_RS1(insn, regs);
+       ulong rs2_val   = GET_RS2(insn, regs);
+       //ulong prev_mode = sbi_mstatus_prev_mode(regs->mstatus);
+       ulong irq_val;
+
+       /*
+       // This is were the emualted irq vlaue is fetched
+       if (prev_mode == PRV_S && sbi_emulate_irq_read(&irq_val))
+               return truly_illegal_insn(insn, regs);
+       */
+
+       // For testing
+       irq_val = rs1_val + rs2_val;
+
+       SET_RD(insn, regs, irq_val);
+

lib/sbi/sbi_illegal_insn.c static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) · line 139

Quoted 10 lines
lib/sbi/sbi_illegal_insn.c +6 -1
+       regs->mepc += 4;
+
+       return 0;
+}
+
 static const illegal_insn_func illegal_insn_table[32] = {
        truly_illegal_insn, /* 0 */
        truly_illegal_insn, /* 1 */
-       truly_illegal_insn, /* 2 */
+       sbi_get_emulated_irq_insn,   /* 2 */

We can't pick arbitrary instruction encoding for
trap-n-emulation of an interrupt controller.

If the goal is to trap-n-emulate an MMIO device
then use PMP based access faults.

Regards
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi