Skip to contentSkip to conversation

RISC-V: Add FP register ptrace support for gdb.

PATCH New Jim Wilson 2 participants 3 messages Oct 13, 2018 – Oct 15, 2018

Subsystems (2)
lore
Share & tools
lore

Jim Wilson

author
#0 lore raw

PATCH 1/1 RISC-V: Add FP register ptrace support for gdb.

This has been tested by hand, and with the gdb testsuite, and is working well. Corresponding patches for gdb have already been upstreamed, but they don't depend on the exact form of this patch, they only depend on the use of NT_PRFPREG.

I think there is room for improvement here, but not sure if that is required before this can be accepted. I'm abusing the ELF_NGREG macro because we don't have one for FP registers, and the elf_greg_t type again because there isn't one for FP registers. In the riscv_fpr_get function, I'm using a single call to user_regset_copyout which means effectively the entire fstate struct is being copied from kernel space to user space. That may include some padding at the end of the structure. I don't know if that is safe or not. This could be fixed by using two user_regset_copyout calls with a little bit of extra complexity. Similarly with riscv_fpr_set and user_regset_copyin. This probably only works for targets that have the D extension. Not sure if this matters; I don't have a system with only F support, or no FP support, to test against.

Jim

---
 arch/riscv/kernel/ptrace.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
arch/riscv/kernel/ptrace.c +30 -1
index 9f82a7e34c64..8352d1bfadaf 100644
@@ -27,7 +27,7 @@
 #include <trace/events/syscalls.h>
 
 enum riscv_regset {
-	REGSET_X,
+	REGSET_X, REGSET_F
 };
 
 static int riscv_gpr_get(struct task_struct *target,
@@ -54,6 +54,27 @@ static int riscv_gpr_set(struct task_struct *target,
 	return ret;
 }
 
+static int riscv_fpr_get(struct task_struct *target,
+			 const struct user_regset *regset,
+			 unsigned int pos, unsigned int count,
+			 void *kbuf, void __user *ubuf)
+{
+	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
+
+	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0, -1);
+}
+
+static int riscv_fpr_set(struct task_struct *target,
+			 const struct user_regset *regset,
+			 unsigned int pos, unsigned int count,
+			 const void *kbuf, const void __user *ubuf)
+{
+	int ret;
+	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
+
+	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, -1);
+	return ret;
+}
 
 static const struct user_regset riscv_user_regset[] = {
 	[REGSET_X] = {
@@ -64,6 +85,14 @@ static const struct user_regset riscv_user_regset[] = {
 		.get = &riscv_gpr_get,
 		.set = &riscv_gpr_set,
 	},
+	[REGSET_F] = {
+		.core_note_type = NT_PRFPREG,
+		.n = ELF_NGREG+1,
+		.size = sizeof(elf_greg_t),
+		.align = sizeof(elf_greg_t),
+		.get = &riscv_fpr_get,
+		.set = &riscv_fpr_set,
+	},
 };
 
 static const struct user_regset_view riscv_user_native_view = {
Signature · 1 line
2.19.0.rc0
#1 lore raw

PATCH 1/1 RISC-V: Add FP register ptrace support for gdb.

Quoted 4 lines

I think there is room for improvement here, but not sure if that is required before this can be accepted. I'm abusing the ELF_NGREG macro because we don't have one for FP registers, and the elf_greg_t type again because there isn't one for FP registers.

So why don't we have a proper macro / type for the FP registers?

Quoted 4 lines

In the riscv_fpr_get function, I'm using a single call
to user_regset_copyout which means effectively the entire fstate struct is being copied from kernel space to user space. That may include some padding at the end of the structure. I don't know if that is safe or not.

In general we should avoid copying padding to userspace as it might be uninitialized. If you can't avoid that the structures always need to be zeroed explicitly first, which might to lead to security issues later on.

Quoted 6 lines

This could
be fixed by using two user_regset_copyout calls with a little bit of extra complexity. Similarly with riscv_fpr_set and user_regset_copyin. This probably only works for targets that have the D extension. Not sure if this matters; I don't have a system with only F support, or no FP support, to test against.

The kernel currently assumes the D extension is presend if the F extension is present.

I don't really know enough about the ptrace code to have useful comments, but the code should depend on CONFIG_FPU, otherwise builds without support for the F and D extensions will fail.

author
#2 lore raw

PATCH 1/1 RISC-V: Add FP register ptrace support for gdb.

Quoted 1 line · Christoph Hellwig

So why don't we have a proper macro / type for the FP registers?

Shrug. No one has defined them. I can try adding them, but the farther I get from ptrace the less comfortable I am, and the more revisions I'll probably need.

The other comments are as I expected. I will work on a followup version of the patch that tries to fix these problems.

Jim