Skip to contentSkip to conversation

RFC: extern illegal instruction trap and trap RDCYCLE

PATCH In review Ben Dooks 5 participants 16 messages Sep 17, 2024 – Oct 1, 2024

Subsystems (2)
lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

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

author
#1 lore raw

PATCH 1/3 riscv: ptrace: add regs_set_register()

Since we have regs_get_register() and we could use the set counterpart for things like fixing up traps, add regs_set_register() to set a pt_regs value from offset.

---
 arch/riscv/include/asm/ptrace.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/riscv/include/asm/ptrace.h +20 -0
index b5b0adcc85c1..66fc1795141d 100644
@@ -143,6 +143,26 @@ static inline unsigned long regs_get_register(struct pt_regs *regs,
 	return *(unsigned long *)((unsigned long)regs + offset);
 }
 
+/**
+ * regs_set_register() - set register value from its offset
+ * @regs:	pt_regs from which register value is gotten
+ * @offset:	offset of the register.
+ * @to:		value to set register to
+ *
+ * regs_set_register sets the value @to to a register whose offset from @regs.
+ * The @offset is the offset of the register in struct pt_regs.
+ * If @offset is bigger than MAX_REG_OFFSET, this will ignore the write.
+ */
+static inline void regs_set_register(struct pt_regs *regs,
+				     unsigned int offset,
+				     unsigned long to)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return;
+
+	*(unsigned long *)((unsigned long)regs + offset) = to;
+}
+
 /**
  * regs_get_kernel_argument() - get Nth function argument in kernel
  * @regs:       pt_regs of that context
Signature · 7 lines
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#2 lore raw

PATCH 2/3 riscv: traps: make insn fetch common in unknown instruction

In reply to Ben Dooks

Add the insn as the second argument to riscv_v_first_use_handler() form the trap handler so when we add more handlers we can do the fetch of the instruction just once.

---
 arch/riscv/include/asm/vector.h |  4 ++--
 arch/riscv/kernel/traps.c       | 11 ++++++++++-
 arch/riscv/kernel/vector.c      | 11 +----------
 3 files changed, 13 insertions(+), 13 deletions(-)
arch/riscv/include/asm/vector.h +2 -2
index be7d309cca8a..c9f0b02cd975 100644
@@ -21,7 +21,7 @@
 
 extern unsigned long riscv_v_vsize;
 int riscv_v_setup_vsize(void);
-bool riscv_v_first_use_handler(struct pt_regs *regs);
+bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn);
 void kernel_vector_begin(void);
 void kernel_vector_end(void);
 void get_cpu_vector_context(void);
@@ -268,7 +268,7 @@ struct pt_regs;
 
 static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; }
 static __always_inline bool has_vector(void) { return false; }
-static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; }
+static inline bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn) { return false; }
 static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
 static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; }
 #define riscv_v_vsize (0)
arch/riscv/kernel/traps.c +10 -1
index 51ebfd23e007..1c3fab272fd1 100644
@@ -172,11 +172,20 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
 	bool handled;
 
 	if (user_mode(regs)) {
+		u32 __user *epc = (u32 __user *)regs->epc;
+		u32 insn = (u32)regs->badaddr;
+
 		irqentry_enter_from_user_mode(regs);
 
 		local_irq_enable();
 
-		handled = riscv_v_first_use_handler(regs);
+		if (!insn) {
+			if (__get_user(insn, epc)) {
+				/* todo */
+			}
+		}
+
+		handled = riscv_v_first_use_handler(regs, insn);
 
 		local_irq_disable();
 
arch/riscv/kernel/vector.c +1 -10
index 682b3feee451..b852648cb8d5 100644
@@ -168,11 +168,8 @@ bool riscv_v_vstate_ctrl_user_allowed(void)
 }
 EXPORT_SYMBOL_GPL(riscv_v_vstate_ctrl_user_allowed);
 
-bool riscv_v_first_use_handler(struct pt_regs *regs)
+bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn)
 {
-	u32 __user *epc = (u32 __user *)regs->epc;
-	u32 insn = (u32)regs->badaddr;
-
 	if (!has_vector())
 		return false;
 
@@ -184,12 +181,6 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
 	if (riscv_v_vstate_query(regs))
 		return false;
 
-	/* Get the instruction */
-	if (!insn) {
-		if (__get_user(insn, epc))
-			return false;
-	}
-
 	/* Filter out non-V instructions */
 	if (!insn_is_vector(insn))
 		return false;
Signature · 7 lines
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#3 lore raw
In reply to Ben Dooks
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 3 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

So hopefully the other two patches get through the lists at some point, but I think in general this is a reasonable thing to do -- or I guess maybe a completely unreasonable thing to be stuck needing to do, but no way around it ;)

Quoted 7 lines

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

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

author
#4 lore raw
Quoted 5 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Quoted 7 lines

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 3 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 1 line

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Quoted 1 line

So hopefully the other two patches get through the lists at some point, but I think in general this is a reasonable thing to do -- or I guess maybe a completely unreasonable thing to be stuck needing to do, but no way around it ;)

I'll check back later, currently as OSS-EU

Quoted 13 lines
Quoted 7 lines

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

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

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw
Quoted 17 lines · Ben Dooks
Quoted 5 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Quoted 7 lines

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 3 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

Quoted 8 lines

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 1 line

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Here is the way to properly allow it via the driver.

https://www.kernel.org/doc/Documentation/admin-guide/sysctl/kernel.rst

Check the perf_user_access

As you noted, the user space application shouldn't use RDCYCLE for various reasons discussed in the past.

If RDTIME is really not an option, you can always set the legacy mode to enable access.

Quoted 19 lines
Quoted 1 line

So hopefully the other two patches get through the lists at some point, but I think in general this is a reasonable thing to do -- or I guess maybe a completely unreasonable thing to be stuck needing to do, but no way around it ;)

I'll check back later, currently as OSS-EU

Quoted 13 lines
Quoted 7 lines

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

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

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

author
#6 lore raw
Quoted 27 lines · Atish Patra
Quoted 17 lines · Ben Dooks
Quoted 5 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Quoted 7 lines

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 3 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

Quoted 8 lines

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 1 line

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Quoted 5 lines

Here is the way to properly allow it via the driver.

https://www.kernel.org/doc/Documentation/admin-guide/sysctl/kernel.rst

Check the perf_user_access

Didn't work for me, not sure why

Quoted 26 lines

As you noted, the user space application shouldn't use RDCYCLE for various reasons discussed in the past.

If RDTIME is really not an option, you can always set the legacy mode to enable access.

Quoted 19 lines
Quoted 1 line

So hopefully the other two patches get through the lists at some point, but I think in general this is a reasonable thing to do -- or I guess maybe a completely unreasonable thing to be stuck needing to do, but no way around it ;)

I'll check back later, currently as OSS-EU

Quoted 13 lines
Quoted 7 lines

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

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

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw
Quoted 35 lines · Ben Dooks
Quoted 33 lines · Atish Patra
Quoted 31 lines · Ben Dooks
Quoted 9 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 4 lines

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Quoted 8 lines
Quoted 5 lines

Here is the way to properly allow it via the driver.

https://www.kernel.org/doc/Documentation/admin-guide/sysctl/kernel.rst

Check the perf_user_access

Didn't work for me, not sure why

+Alexandre Ghiti

Hmm. That's weird. Maybe some bug crept in recently. Can you provide some more details
on the platform, kernel version and procedure followed to change it ?

Quoted 33 lines
Quoted 31 lines

As you noted, the user space application shouldn't use RDCYCLE for various reasons discussed in the past.

If RDTIME is really not an option, you can always set the legacy mode to enable access.

Quoted 22 lines
Quoted 4 lines

So hopefully the other two patches get through the lists at some point, but I think in general this is a reasonable thing to do -- or I guess maybe a completely unreasonable thing to be stuck needing to do, but no way around it ;)

I'll check back later, currently as OSS-EU

Quoted 13 lines
Quoted 7 lines

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

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

Quoted 5 lines

--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

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

#8 lore raw

Hi Atish, Ben,

Quoted 33 lines · Atish Kumar Patra
Quoted 32 lines · Ben Dooks
Quoted 31 lines · Atish Patra
Quoted 19 lines · Ben Dooks
Quoted 5 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Quoted 7 lines

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 2 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 7 lines
Quoted 4 lines

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Here is the way to properly allow it via the driver.

https://www.kernel.org/doc/Documentation/admin-guide/sysctl/kernel.rst

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Check the perf_user_access

Didn't work for me, not sure why

+Alexandre Ghiti

Hmm. That's weird. Maybe some bug crept in recently. Can you provide some more details
on the platform, kernel version and procedure followed to change it ?

So I have just tried perf_user_access on 6.11 and it works fine, meaning setting the "legacy" behaviour (ie 2) allows a userspace application to directly access RDCYCLE.

Can you provide more details Ben?

Thanks,

Alex

Quoted 31 lines
Quoted 31 lines
Quoted 26 lines

As you noted, the user space application shouldn't use RDCYCLE for various reasons discussed in the past.

If RDTIME is really not an option, you can always set the legacy mode to enable access.

Quoted 18 lines
Quoted 4 lines

So hopefully the other two patches get through the lists at some point, but I think in general this is a reasonable thing to do -- or I guess maybe a completely unreasonable thing to be stuck needing to do, but no way around it ;)

I'll check back later, currently as OSS-EU

Quoted 11 lines
Quoted 6 lines

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

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

--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius

Quoted 5 lines
Quoted 1 line

https://www.codethink.co.uk/privacy.html

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

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

author
#9 lore raw
Quoted 41 lines · Alexandre Ghiti

Hi Atish, Ben,

Quoted 38 lines · Atish Kumar Patra
Quoted 28 lines · Ben Dooks
Quoted 27 lines · Atish Patra
Quoted 26 lines · Ben Dooks
Quoted 5 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Quoted 7 lines

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 2 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 4 lines

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Quoted 7 lines
Quoted 5 lines

Here is the way to properly allow it via the driver.

https://www.kernel.org/doc/Documentation/admin-guide/sysctl/kernel.rst

Check the perf_user_access

Didn't work for me, not sure why

+Alexandre Ghiti

Quoted 8 lines
Quoted 3 lines

Hmm. That's weird. Maybe some bug crept in recently. Can you provide some more details
on the platform, kernel version and procedure followed to change it ?

So I have just tried perf_user_access on 6.11 and it works fine, meaning setting the "legacy" behaviour (ie 2) allows a userspace application to directly access RDCYCLE.

Can you provide more details Ben?

I've gone through and checked this again, I think it is very likely that I got the enable as 1 instead of 2. It works on QEMU, I've not got access to the runners that we upgraded and we've fixed the issue by upgrading libabsl to a later version.

Thanks.

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#10 lore raw
Quoted 49 lines · Ben Dooks
Quoted 48 lines · Alexandre Ghiti

Hi Atish, Ben,

Quoted 33 lines · Atish Kumar Patra
Quoted 32 lines · Ben Dooks
Quoted 31 lines · Atish Patra
Quoted 19 lines · Ben Dooks
Quoted 5 lines · Palmer Dabbelt
Quoted 3 lines · On Tue, 17 Sep 2024 06:08:50 PDT (-0700), [email protected] wrote:

This is a RFC series to change how the illegal instruction trap is handled and then how to trap RDCYCLE and emulate it with RDTIME instead.

Quoted 7 lines

Only 1/3 made it to lore for me, not sure if it's just stuck somewhere.

Quoted 2 lines

I did this when we found multiple libraries using RDCYCLE and upgrading multiple runners to newer kernels caused many problems

OK, so I think we're kind of just stuck with RDCYCLE then -- it was part of the base ISA when we merged the port, and every time it disappears we end up breaking userspace.

Yes, it was an annoyance, we managed to track down all the users and patch out.

I thought this might be a useful idea for more generic type, so might look at updating to have a table of instruction masks to call.

Quoted 7 lines
Quoted 4 lines

I'm not sure what exactly the right way to do this is: IIRC there's some perf-related hooks for this, but there's also systems that just don't implement the RDCYCLE instruction at all and thus we'll need some sort of emulation for those.

I couldn't get the PMU driver to allow it, not sure if there was an issue higher up or some other issue?

Here is the way to properly allow it via the driver.

https://www.kernel.org/doc/Documentation/admin-guide/sysctl/kernel.rst

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Check the perf_user_access

Didn't work for me, not sure why

+Alexandre Ghiti

Hmm. That's weird. Maybe some bug crept in recently. Can you provide some more details
on the platform, kernel version and procedure followed to change it ?

So I have just tried perf_user_access on 6.11 and it works fine, meaning setting the "legacy" behaviour (ie 2) allows a userspace application to directly access RDCYCLE.

Quoted 8 lines
Quoted 1 line

Can you provide more details Ben?

I've gone through and checked this again, I think it is very likely that I got the enable as 1 instead of 2. It works on QEMU, I've not got access to the runners that we upgraded and we've fixed the issue by upgrading libabsl to a later version.

Thanks.

And I put some test code up at I used for testing my patch at:
https://gitlab.com/CodethinkLabs/riscv_rdcycle_test

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#11 lore raw

PATCH 3/3 riscv: add trap and emulation for RDCYCLE

In reply to Ben Dooks

Add a trap for RDCYCLE and emulate it as RDTIME instruciton.

This is an initial PoC and should probably be made more generic way of trapping and dealing with bad instructions

---
 arch/riscv/kernel/traps.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
arch/riscv/kernel/traps.c +31 -0
index 1c3fab272fd1..51ea28ebf54d 100644
@@ -167,6 +167,35 @@ DO_ERROR_INFO(do_trap_insn_misaligned,
 DO_ERROR_INFO(do_trap_insn_fault,
 	SIGSEGV, SEGV_ACCERR, "instruction access fault");
 
+#define is_system(__i) (((__i) & 0x7f) == RVG_OPCODE_SYSTEM)
+
+static bool riscv_try_csr_fixup_user(struct pt_regs *regs, u32 insn)
+{
+	/* expecting a 4 byte CSR instruction (*/
+	if (unlikely(GET_INSN_LENGTH(insn) != 4))
+		return false;
+
+	if (is_system(insn)) {
+		u32 csr = insn >> RVG_SYSTEM_CSR_OFF;
+		u32 rd = (insn >> RVG_RD_OPOFF) & RVG_RD_MASK;
+		u32 rs = (insn >> RVG_RS1_OPOFF) & RVG_RS1_MASK;
+		u32 funct3 = (insn >> RV_INSN_FUNCT3_OPOFF) & 0x7;
+
+		if (rs == 0 && funct3 == 2 && csr == CSR_CYCLE) {
+			u64 val = csr_read(CSR_TIME);
+			/* we've got a RDCCLYE, emulated it with CSR_TIME */
+
+			printk_ratelimited("PID %d: process using RDCYCLE, emulating with RDTIME\n", current->pid);
+
+			regs_set_register(regs, rd*sizeof(unsigned long), val);
+			regs->epc += 4;
+			return true;
+		}
+	}
+
+	return false;
+}
+
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
 {
 	bool handled;
@@ -186,6 +215,8 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
 		}
 
 		handled = riscv_v_first_use_handler(regs, insn);
+		if (!handle)
+			handled = riscv_try_csr_fixup_user(regs, insn);
 
 		local_irq_disable();
 
Signature · 7 lines
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#12 lore raw

Re: [PATCH 3/3] riscv: add trap and emulation for RDCYCLE

Quoted 51 lines · Ben Dooks

Add a trap for RDCYCLE and emulate it as RDTIME instruciton.

This is an initial PoC and should probably be made more generic way of trapping and dealing with bad instructions

---
  arch/riscv/kernel/traps.c | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)
arch/riscv/kernel/traps.c +29 -0
index 1c3fab272fd1..51ea28ebf54d 100644
@@ -167,6 +167,35 @@ DO_ERROR_INFO(do_trap_insn_misaligned,
  DO_ERROR_INFO(do_trap_insn_fault,
  	SIGSEGV, SEGV_ACCERR, "instruction access fault");
 
+#define is_system(__i) (((__i) & 0x7f) == RVG_OPCODE_SYSTEM)
+
+static bool riscv_try_csr_fixup_user(struct pt_regs *regs, u32 insn)
+{
+	/* expecting a 4 byte CSR instruction (*/
+	if (unlikely(GET_INSN_LENGTH(insn) != 4))
+		return false;
+
+	if (is_system(insn)) {
+		u32 csr = insn >> RVG_SYSTEM_CSR_OFF;
+		u32 rd = (insn >> RVG_RD_OPOFF) & RVG_RD_MASK;
+		u32 rs = (insn >> RVG_RS1_OPOFF) & RVG_RS1_MASK;
+		u32 funct3 = (insn >> RV_INSN_FUNCT3_OPOFF) & 0x7;
+
+		if (rs == 0 && funct3 == 2 && csr == CSR_CYCLE) {
+			u64 val = csr_read(CSR_TIME);
+			/* we've got a RDCCLYE, emulated it with CSR_TIME */
+
+			printk_ratelimited("PID %d: process using RDCYCLE, emulating with RDTIME\n", current->pid);
+
+			regs_set_register(regs, rd*sizeof(unsigned long), val);
+			regs->epc += 4;
+			return true;
+		}
+	}
+
+	return false;
+}
+
  asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
  {
  	bool handled;

arch/riscv/kernel/traps.c asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re · line 222

Quoted 8 lines
arch/riscv/kernel/traps.c +2 -0
@@ -186,6 +215,8 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
  		}
 
  		handled = riscv_v_first_use_handler(regs, insn);
+		if (!handle)
+			handled = riscv_try_csr_fixup_user(regs, insn);
 
  		local_irq_disable();
 

oops, forgot to fold in a fix patch, this has 2 issues.

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#13 lore raw

Re: [PATCH 3/3] riscv: add trap and emulation for RDCYCLE

In reply to Ben Dooks
Quoted 15 lines · Ben Dooks

Add a trap for RDCYCLE and emulate it as RDTIME instruciton.

This is an initial PoC and should probably be made more generic way of trapping and dealing with bad instructions

---
 arch/riscv/kernel/traps.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
arch/riscv/kernel/traps.c +0 -0
index 1c3fab272fd1..51ea28ebf54d 100644

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 170

Quoted 4 lines
arch/riscv/kernel/traps.c +1 -0
@@ -167,6 +167,35 @@ DO_ERROR_INFO(do_trap_insn_misaligned,
 DO_ERROR_INFO(do_trap_insn_fault,
 	SIGSEGV, SEGV_ACCERR, "instruction access fault");
 
+#define is_system(__i) (((__i) & 0x7f) == RVG_OPCODE_SYSTEM)

We have riscv_insn_is_system()

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 182

Quoted 12 lines
arch/riscv/kernel/traps.c +12 -0
+
+static bool riscv_try_csr_fixup_user(struct pt_regs *regs, u32 insn)
+{
+	/* expecting a 4 byte CSR instruction (*/
+	if (unlikely(GET_INSN_LENGTH(insn) != 4))
+		return false;
+
+	if (is_system(insn)) {
+		u32 csr = insn >> RVG_SYSTEM_CSR_OFF;
+		u32 rd = (insn >> RVG_RD_OPOFF) & RVG_RD_MASK;
+		u32 rs = (insn >> RVG_RS1_OPOFF) & RVG_RS1_MASK;
+		u32 funct3 = (insn >> RV_INSN_FUNCT3_OPOFF) & 0x7;

There are are a few other macros in asm/insn.h that can be applied, such as RV_EXTRACT_RD_REG(), and more could be added if necessary.

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 184

Quoted 2 lines
arch/riscv/kernel/traps.c +2 -0
+
+		if (rs == 0 && funct3 == 2 && csr == CSR_CYCLE) {

We could probably create a riscv_insn_is_csr_read() and RV_EXTRACT_CSR() for this.

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 188

Quoted 4 lines
arch/riscv/kernel/traps.c +4 -0
+			u64 val = csr_read(CSR_TIME);
+			/* we've got a RDCCLYE, emulated it with CSR_TIME */
+
+			printk_ratelimited("PID %d: process using RDCYCLE, emulating with RDTIME\n", current->pid);

If we add current->comm it may be easier to find applications that should be converted to CSR_TIME.

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 190

Quoted 2 lines
arch/riscv/kernel/traps.c +2 -0
+
+			regs_set_register(regs, rd*sizeof(unsigned long), val);

nit: spaces around the '*'

Quoted 12 lines
arch/riscv/kernel/traps.c +8 -0
+			regs->epc += 4;
+			return true;
+		}
+	}
+
+	return false;
+}
+
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
 {
 	bool handled;

arch/riscv/kernel/traps.c asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re · line 218

Quoted 4 lines
arch/riscv/kernel/traps.c +1 -0
@@ -186,6 +215,8 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
 		}
 
 		handled = riscv_v_first_use_handler(regs, insn);
+		if (!handle)

^handled?

Quoted 8 lines
arch/riscv/kernel/traps.c +1 -0
+			handled = riscv_try_csr_fixup_user(regs, insn);
 
 		local_irq_disable();
 
Signature · 1 line
2.37.2.352.g3c44437643

Thanks,
drew

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

author
#14 lore raw

Re: [PATCH 3/3] riscv: add trap and emulation for RDCYCLE

Quoted 19 lines · Andrew Jones
Quoted 18 lines · Ben Dooks

Add a trap for RDCYCLE and emulate it as RDTIME instruciton.

This is an initial PoC and should probably be made more generic way of trapping and dealing with bad instructions

---
  arch/riscv/kernel/traps.c | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)
arch/riscv/kernel/traps.c +0 -0
index 1c3fab272fd1..51ea28ebf54d 100644
@@ -167,6 +167,35 @@ DO_ERROR_INFO(do_trap_insn_misaligned,
  DO_ERROR_INFO(do_trap_insn_fault,
  	SIGSEGV, SEGV_ACCERR, "instruction access fault");
 
Quoted 3 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 170

Quoted 1 line
arch/riscv/kernel/traps.c +1 -0
+#define is_system(__i) (((__i) & 0x7f) == RVG_OPCODE_SYSTEM)

We have riscv_insn_is_system()

Ah, didn't notice that.

Quoted 7 lines
Quoted 7 lines
arch/riscv/kernel/traps.c +7 -0
+
+static bool riscv_try_csr_fixup_user(struct pt_regs *regs, u32 insn)
+{
+	/* expecting a 4 byte CSR instruction (*/
+	if (unlikely(GET_INSN_LENGTH(insn) != 4))
+		return false;
+
Quoted 8 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 182

Quoted 5 lines
arch/riscv/kernel/traps.c +5 -0
+	if (is_system(insn)) {
+		u32 csr = insn >> RVG_SYSTEM_CSR_OFF;
+		u32 rd = (insn >> RVG_RD_OPOFF) & RVG_RD_MASK;
+		u32 rs = (insn >> RVG_RS1_OPOFF) & RVG_RS1_MASK;
+		u32 funct3 = (insn >> RV_INSN_FUNCT3_OPOFF) & 0x7;

There are are a few other macros in asm/insn.h that can be applied, such as RV_EXTRACT_RD_REG(), and more could be added if necessary.

will have a look at that.

Quoted 5 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 184

Quoted 2 lines
arch/riscv/kernel/traps.c +2 -0
+
+		if (rs == 0 && funct3 == 2 && csr == CSR_CYCLE) {

We could probably create a riscv_insn_is_csr_read() and RV_EXTRACT_CSR() for this.

Not sure if this is totally necessary. I will check for the relevant read/write type from the CSR acces instructions

Quoted 7 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 188

Quoted 4 lines
arch/riscv/kernel/traps.c +4 -0
+			u64 val = csr_read(CSR_TIME);
+			/* we've got a RDCCLYE, emulated it with CSR_TIME */
+
+			printk_ratelimited("PID %d: process using RDCYCLE, emulating with RDTIME\n", current->pid);

If we add current->comm it may be easier to find applications that should be converted to CSR_TIME.

ok, will add.

Quoted 4 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 190

Quoted 2 lines
arch/riscv/kernel/traps.c +2 -0
+
+			regs_set_register(regs, rd*sizeof(unsigned long), val);

nit: spaces around the '*'

oops. will fix

Quoted 22 lines
Quoted 12 lines
arch/riscv/kernel/traps.c +8 -0
+			regs->epc += 4;
+			return true;
+		}
+	}
+
+	return false;
+}
+
  asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
  {
  	bool handled;

arch/riscv/kernel/traps.c asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re · line 218

Quoted 4 lines
arch/riscv/kernel/traps.c +1 -0
@@ -186,6 +215,8 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
  		}
 
  		handled = riscv_v_first_use_handler(regs, insn);
+		if (!handle)

^handled?

Quoted 4 lines
arch/riscv/kernel/traps.c +1 -0
+			handled = riscv_try_csr_fixup_user(regs, insn);
 
  		local_irq_disable();
 
Quoted 7 lines
Quoted 4 lines
Signature · 1 line
2.37.2.352.g3c44437643

Thanks,
drew

Thanks.

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#15 lore raw

Re: [PATCH 3/3] riscv: add trap and emulation for RDCYCLE

In reply to Andrew Jones
Quoted 45 lines · Andrew Jones
Quoted 15 lines · Ben Dooks

Add a trap for RDCYCLE and emulate it as RDTIME instruciton.

This is an initial PoC and should probably be made more generic way of trapping and dealing with bad instructions

---
  arch/riscv/kernel/traps.c | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)
arch/riscv/kernel/traps.c +0 -0
index 1c3fab272fd1..51ea28ebf54d 100644

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 170

Quoted 4 lines
arch/riscv/kernel/traps.c +1 -0
@@ -167,6 +167,35 @@ DO_ERROR_INFO(do_trap_insn_misaligned,
  DO_ERROR_INFO(do_trap_insn_fault,
  	SIGSEGV, SEGV_ACCERR, "instruction access fault");
 
+#define is_system(__i) (((__i) & 0x7f) == RVG_OPCODE_SYSTEM)

We have riscv_insn_is_system()

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 182

Quoted 12 lines
arch/riscv/kernel/traps.c +12 -0
+
+static bool riscv_try_csr_fixup_user(struct pt_regs *regs, u32 insn)
+{
+	/* expecting a 4 byte CSR instruction (*/
+	if (unlikely(GET_INSN_LENGTH(insn) != 4))
+		return false;
+
+	if (is_system(insn)) {
+		u32 csr = insn >> RVG_SYSTEM_CSR_OFF;
+		u32 rd = (insn >> RVG_RD_OPOFF) & RVG_RD_MASK;
+		u32 rs = (insn >> RVG_RS1_OPOFF) & RVG_RS1_MASK;
+		u32 funct3 = (insn >> RV_INSN_FUNCT3_OPOFF) & 0x7;

There are are a few other macros in asm/insn.h that can be applied, such as RV_EXTRACT_RD_REG(), and more could be added if necessary.

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 184

Quoted 2 lines
arch/riscv/kernel/traps.c +2 -0
+
+		if (rs == 0 && funct3 == 2 && csr == CSR_CYCLE) {

We could probably create a riscv_insn_is_csr_read() and RV_EXTRACT_CSR() for this.

Quoted 7 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 188

Quoted 4 lines
arch/riscv/kernel/traps.c +4 -0
+			u64 val = csr_read(CSR_TIME);
+			/* we've got a RDCCLYE, emulated it with CSR_TIME */
+
+			printk_ratelimited("PID %d: process using RDCYCLE, emulating with RDTIME\n", current->pid);

If we add current->comm it may be easier to find applications that should be converted to CSR_TIME.

I guess I should have also checked for the 32bit case too, but i think those are slightly different encodings for RDCYCLE/RDCYCLEH calls.

Quoted 41 lines

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_misaligned, · line 190

Quoted 3 lines
arch/riscv/kernel/traps.c +2 -0
+
+			regs_set_register(regs, rd*sizeof(unsigned long), val);

nit: spaces around the '*'

Quoted 12 lines
arch/riscv/kernel/traps.c +8 -0
+			regs->epc += 4;
+			return true;
+		}
+	}
+
+	return false;
+}
+
  asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
  {
  	bool handled;

arch/riscv/kernel/traps.c asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re · line 218

Quoted 4 lines
arch/riscv/kernel/traps.c +1 -0
@@ -186,6 +215,8 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
  		}
 
  		handled = riscv_v_first_use_handler(regs, insn);
+		if (!handle)

^handled?

Quoted 8 lines
arch/riscv/kernel/traps.c +1 -0
+			handled = riscv_try_csr_fixup_user(regs, insn);
 
  		local_irq_disable();
 
Signature · 1 line
2.37.2.352.g3c44437643

Thanks,
drew

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

Signature · 9 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

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