Skip to contentSkip to conversation

updates for instruction trapping and ptrace

PATCH Superseded Ben Dooks 2 participants 10 messages Nov 13, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
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
#2 lore raw

Re: [PATCH 1/3] riscv: ptrace: add regs_set_register()

Quoted 14 lines · Ben Dooks

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 +0 -0
index b5b0adcc85c1..66fc1795141d 100644

arch/riscv/include/asm/ptrace.h static inline unsigned long regs_get_register(struct pt_regs *regs, · line 147

Quoted 5 lines
arch/riscv/include/asm/ptrace.h +2 -0
@@ -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

s/from/at/

arch/riscv/include/asm/ptrace.h static inline unsigned long regs_get_register(struct pt_regs *regs, · line 148

Quoted 1 line
arch/riscv/include/asm/ptrace.h +1 -0
+ * @regs:	pt_regs from which register value is gotten

s/gotten/set/

arch/riscv/include/asm/ptrace.h static inline unsigned long regs_get_register(struct pt_regs *regs, · line 150

Quoted 2 lines
arch/riscv/include/asm/ptrace.h +2 -0
+ * @offset:	offset of the register.
+ * @to:		value to set register to

Can we name this 'value' instead of 'to'?

Quoted 21 lines
arch/riscv/include/asm/ptrace.h +15 -0
+ *
+ * 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 · 1 line
2.37.2.352.g3c44437643

Thanks,
drew

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

author
#3 lore raw

Re: [PATCH 1/3] riscv: ptrace: add regs_set_register()

Quoted 23 lines · Andrew Jones
Quoted 14 lines · Ben Dooks

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 +0 -0
index b5b0adcc85c1..66fc1795141d 100644

arch/riscv/include/asm/ptrace.h static inline unsigned long regs_get_register(struct pt_regs *regs, · line 147

Quoted 5 lines
arch/riscv/include/asm/ptrace.h +2 -0
@@ -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

s/from/at/

Quoted 8 lines

arch/riscv/include/asm/ptrace.h static inline unsigned long regs_get_register(struct pt_regs *regs, · line 148

Quoted 1 line
arch/riscv/include/asm/ptrace.h +1 -0
+ * @regs:	pt_regs from which register value is gotten

s/gotten/set/

arch/riscv/include/asm/ptrace.h static inline unsigned long regs_get_register(struct pt_regs *regs, · line 150

Quoted 2 lines
arch/riscv/include/asm/ptrace.h +2 -0
+ * @offset:	offset of the register.
+ * @to:		value to set register to

Can we name this 'value' instead of 'to'?

Thank you, I think these are all good comments.

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
#4 lore raw

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

In reply to Ben Dooks

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

---
- fixed wording of patch from rfc

---
 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
author
#5 lore raw

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

Quoted 46 lines · Ben Dooks

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

---
- fixed wording of patch from rfc

---
  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 +2 -0
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;

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

Quoted 10 lines
arch/riscv/kernel/traps.c +5 -1
+
  		irqentry_enter_from_user_mode(regs);
 
  		local_irq_enable();
 
-		handled = riscv_v_first_use_handler(regs);
+		if (!insn) {
+			if (__get_user(insn, epc)) {
+				/* todo */
+			}

grr, of course as soon as it is sent I notice the /todo/ here

Not sure if we should print something if __get_user() fails for what /should/ be an good instruction addrss.... should probably at-least bail out of do_trap_insn_illegal() if not also print a warning.

Quoted 36 lines
arch/riscv/kernel/traps.c +3 -0
+		}
+
+		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 · 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
#6 lore raw

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

Quoted 58 lines · Ben Dooks
Quoted 46 lines · Ben Dooks

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

---
- fixed wording of patch from rfc
---
  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 +0 -0
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;

Quoted 8 lines

+
          irqentry_enter_from_user_mode(regs); local_irq_enable();
-        handled = riscv_v_first_use_handler(regs); +        if (!insn) {
+            if (__get_user(insn, epc)) {
+                /* todo */
+            }

grr, of course as soon as it is sent I notice the /todo/ here

Quoted 5 lines

Not sure if we should print something if __get_user() fails for what /should/ be an good instruction addrss.... should probably at-least bail out of do_trap_insn_illegal() if not also print a warning.

i think doing:

	if (___get_user(insn, epr)) {
		handled = false;
		insn = 0;
	}

then just checking for "if (insn)" before hand handlers.

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
#7 lore raw

PATCH 3/3 riscv: insn: add RV_EXTRACT_FUNCT3()

In reply to Ben Dooks

Add extraction for the func3 field of most instructions for use with anyone who needs it.

Note, added this for decoding of CSR accesses for work we did looking at the RDCYCLE v RDTIME calls.

---
 arch/riscv/include/asm/insn.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/include/asm/insn.h +4 -0
index 09fde95a5e8f..c67f44ff2066 100644
@@ -299,6 +299,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
 
+#define RV_EXTRACT_FUNCT3(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RV_INSN_FUNCT3_OPOFF, RV_INSN_FUNCT3_MASK)); })
+
 #define RV_EXTRACT_UTYPE_IMM(x) \
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })
Signature · 7 lines
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] riscv: insn: add RV_EXTRACT_FUNCT3()

Quoted 29 lines · Ben Dooks

Add extraction for the func3 field of most instructions for use with anyone who needs it.

Note, added this for decoding of CSR accesses for work we did looking at the RDCYCLE v RDTIME calls.

---
 arch/riscv/include/asm/insn.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/include/asm/insn.h +4 -0
index 09fde95a5e8f..c67f44ff2066 100644
@@ -299,6 +299,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
 
+#define RV_EXTRACT_FUNCT3(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RV_INSN_FUNCT3_OPOFF, RV_INSN_FUNCT3_MASK)); })
+
 #define RV_EXTRACT_UTYPE_IMM(x) \
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })
Signature · 1 line
2.37.2.352.g3c44437643

It doesn't hurt to have an unused macro and there's a reasonable chance there will be a user someday, but I'd typically wait for a user before adding stuff. Anyway,

Thanks,
drew

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

author
#9 lore raw

Re: [PATCH 3/3] riscv: insn: add RV_EXTRACT_FUNCT3()

Quoted 35 lines · Andrew Jones
Quoted 29 lines · Ben Dooks

Add extraction for the func3 field of most instructions for use with anyone who needs it.

Note, added this for decoding of CSR accesses for work we did looking at the RDCYCLE v RDTIME calls.

---
  arch/riscv/include/asm/insn.h | 4 ++++
  1 file changed, 4 insertions(+)
arch/riscv/include/asm/insn.h +4 -0
index 09fde95a5e8f..c67f44ff2066 100644
@@ -299,6 +299,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
  	({typeof(x) x_ = (x); \
  	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
 
+#define RV_EXTRACT_FUNCT3(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RV_INSN_FUNCT3_OPOFF, RV_INSN_FUNCT3_MASK)); })
+
  #define RV_EXTRACT_UTYPE_IMM(x) \
  	({typeof(x) x_ = (x); \
  	(RV_X(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })
Signature · 1 line
2.37.2.352.g3c44437643

It doesn't hurt to have an unused macro and there's a reasonable chance there will be a user someday, but I'd typically wait for a user before adding stuff. Anyway,

Quoted 1 line

I am wondering if we add a check for RDCYCLE and print a warning even if we didn't try an fixup.

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