diff mbox series

test code for kprobe

Message ID fc71730b9350026427fe1c2bdbce9b993d8bc6f5.1690704360.git.namcaov@gmail.com (mailing list archive)
State Handled Elsewhere
Headers show
Series test code for kprobe | expand

Checks

Context Check Description
conchuod/cover_letter success Single patches do not need cover letters
conchuod/tree_selection success Guessed tree name to be for-next at HEAD 471aba2e4760
conchuod/fixes_present success Fixes tag not required for -next series
conchuod/maintainers_pattern success MAINTAINERS pattern errors before the patch: 4 and now 4
conchuod/verify_signedoff fail author Signed-off-by missing
conchuod/kdoc success Errors and warnings before: 0 this patch: 0
conchuod/build_rv64_clang_allmodconfig success Errors and warnings before: 9 this patch: 9
conchuod/module_param success Was 0 now: 0
conchuod/build_rv64_gcc_allmodconfig success Errors and warnings before: 9 this patch: 9
conchuod/build_rv32_defconfig success Build OK
conchuod/dtb_warn_rv64 success Errors and warnings before: 3 this patch: 3
conchuod/header_inline success No static functions without inline keyword in header files
conchuod/checkpatch fail + printk("ERROR: expect value 4, got %d\n", ret); CHECK: Lines should not end with a '(' CHECK: Unbalanced braces around else statement ERROR: else should follow close brace '}' ERROR: open brace '{' following function definitions go on the next line WARNING: Missing commit description - Add an appropriate one WARNING: Missing or malformed SPDX-License-Identifier tag in line 1 WARNING: Prefer using '"%s...", __func__' to using 'pre_handler', this function's name, in a string WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: else is not generally useful after a break or return WARNING: printk() should include KERN_<LEVEL> facility level
conchuod/build_rv64_nommu_k210_defconfig success Build OK
conchuod/verify_fixes success No Fixes tag
conchuod/build_rv64_nommu_virt_defconfig success Build OK

Commit Message

Nam Cao July 30, 2023, 8:27 a.m. UTC
---
 drivers/test_kprobe/Makefile      |   3 +
 drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+)
 create mode 100644 drivers/test_kprobe/Makefile
 create mode 100644 drivers/test_kprobe/test_kprobe.c

Comments

Björn Töpel Aug. 14, 2023, 12:28 p.m. UTC | #1
Nam Cao <namcaov@gmail.com> writes:

A RISC-V specific kprobes test -- much welcome!

Please add a proper commit message here.

> ---
>  drivers/test_kprobe/Makefile      |   3 +
>  drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
>  2 files changed, 268 insertions(+)
>  create mode 100644 drivers/test_kprobe/Makefile
>  create mode 100644 drivers/test_kprobe/test_kprobe.c
>
> diff --git a/drivers/test_kprobe/Makefile b/drivers/test_kprobe/Makefile

Architecture specific test code usually reside in "arch/$ARCH"
(arch/riscv), and is part of Kconfig.debug.

Have a look at:
* grep for ARM_KPROBES_TEST in arch/arm
* grep for KPROBES_SANITY_TEST, and in arch/powerpc grep
  test_emulate_step
* grep S390_KPROBES_SANITY_TEST


Björn
Nam Cao Aug. 14, 2023, 1:50 p.m. UTC | #2
On Mon, Aug 14, 2023 at 02:28:11PM +0200, Björn Töpel wrote:
> Nam Cao <namcaov@gmail.com> writes:
> 
> A RISC-V specific kprobes test -- much welcome!
> 
> Please add a proper commit message here.
> 
> > ---
> >  drivers/test_kprobe/Makefile      |   3 +
> >  drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
> >  2 files changed, 268 insertions(+)
> >  create mode 100644 drivers/test_kprobe/Makefile
> >  create mode 100644 drivers/test_kprobe/test_kprobe.c
> >
> > diff --git a/drivers/test_kprobe/Makefile b/drivers/test_kprobe/Makefile
> 
> Architecture specific test code usually reside in "arch/$ARCH"
> (arch/riscv), and is part of Kconfig.debug.
> 
> Have a look at:
> * grep for ARM_KPROBES_TEST in arch/arm
> * grep for KPROBES_SANITY_TEST, and in arch/powerpc grep
>   test_emulate_step
> * grep S390_KPROBES_SANITY_TEST

Sorry that I wasn't clear with this: I just wanted to show how testing was done.
This is not meant to be merged.

I do have plans to clean this up and send upstream in the future, but not with
this patch series.

Best regards,
Nam
Björn Töpel Aug. 14, 2023, 2:10 p.m. UTC | #3
Nam Cao <namcaov@gmail.com> writes:

> On Mon, Aug 14, 2023 at 02:28:11PM +0200, Björn Töpel wrote:
>> Nam Cao <namcaov@gmail.com> writes:
>> 
>> A RISC-V specific kprobes test -- much welcome!
>> 
>> Please add a proper commit message here.
>> 
>> > ---
>> >  drivers/test_kprobe/Makefile      |   3 +
>> >  drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
>> >  2 files changed, 268 insertions(+)
>> >  create mode 100644 drivers/test_kprobe/Makefile
>> >  create mode 100644 drivers/test_kprobe/test_kprobe.c
>> >
>> > diff --git a/drivers/test_kprobe/Makefile b/drivers/test_kprobe/Makefile
>> 
>> Architecture specific test code usually reside in "arch/$ARCH"
>> (arch/riscv), and is part of Kconfig.debug.
>> 
>> Have a look at:
>> * grep for ARM_KPROBES_TEST in arch/arm
>> * grep for KPROBES_SANITY_TEST, and in arch/powerpc grep
>>   test_emulate_step
>> * grep S390_KPROBES_SANITY_TEST
>
> Sorry that I wasn't clear with this: I just wanted to show how testing was done.
> This is not meant to be merged.
>
> I do have plans to clean this up and send upstream in the future, but not with
> this patch series.

Please do! A RISC-V specific kprobes test would be nice.


Björn
diff mbox series

Patch

diff --git a/drivers/test_kprobe/Makefile b/drivers/test_kprobe/Makefile
new file mode 100644
index 000000000000..c3c39bd0f8b5
--- /dev/null
+++ b/drivers/test_kprobe/Makefile
@@ -0,0 +1,3 @@ 
+# SPDX-License-Identifier: GPL-2.0
+#
+obj-m += test_kprobe.o
diff --git a/drivers/test_kprobe/test_kprobe.c b/drivers/test_kprobe/test_kprobe.c
new file mode 100644
index 000000000000..543108c5fc8a
--- /dev/null
+++ b/drivers/test_kprobe/test_kprobe.c
@@ -0,0 +1,265 @@ 
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kprobes.h>
+#include <linux/string.h>
+MODULE_LICENSE("Dual BSD/GPL");
+
+__attribute__ ((naked))
+int sample_c_j(void) {
+	__asm__(
+		"c.j label_cj\n"
+		"li a0, 0\n ret\n"
+		"li a0, 1\n ret\n"
+		"li a0, 2\n ret\n"
+		"li a0, 3\n ret\n"
+		"label_cj:\n"
+		"li a0, 4\n ret\n"
+		"li a0, 5\n ret\n"
+		"li a0, 6\n ret\n"
+	);
+}
+
+__attribute__ ((naked))
+int sample_c_jr(void) {
+	__asm__(
+		"la a0, label_c_jr\n"
+		"c_jr_location:\n"
+		"c.jr a0\n"
+		"li a0, 0\n ret\n"
+		"li a0, 1\n ret\n"
+		"li a0, 2\n ret\n"
+		"li a0, 3\n ret\n"
+		"label_c_jr:\n"
+		"li a0, 4\n ret\n"
+		"li a0, 5\n ret\n"
+		"li a0, 6\n ret\n"
+	);
+}
+
+__attribute__ ((naked))
+int sample_c_jalr(void) {
+	__asm__(
+		"mv a1, x1\n"
+		"la a0, label_c_jalr\n"
+		"c_jalr_location:\n"
+		"c.jalr a0\n"
+
+		"addi a0, a0, -5\n"
+		"jr a1\n"
+
+		"label_c_jalr:\n"
+		"li a0, 9\n ret\n"
+	);
+}
+
+__attribute__ ((naked))
+int sample_c_beqz(int a0) {
+	__asm__(
+		"c.beqz a0, beqz_label\n"
+		"li a0, 10\n ret\n"
+		"beqz_label:\n"
+		"li a0, 4\n ret\n"
+	);
+}
+
+__attribute__ ((naked))
+int sample_c_bnez(int a0) {
+	__asm__(
+		"c.bnez a0, bnez_label\n"
+		"li a0, 10\n ret\n"
+		"bnez_label:\n"
+		"li a0, 4\n ret\n"
+	);
+}
+
+static int pre_handler(struct kprobe *p, struct pt_regs *regs) {
+	printk("pre_handler() called\n");
+
+	return 0;
+}
+
+static int test_c_j(void) {
+	static struct kprobe kp;
+
+	int ret;
+
+	/* Test C.J */
+	kp.symbol_name = "sample_c_j";
+	kp.pre_handler = pre_handler;
+
+	ret = register_kprobe(&kp);
+	if (ret) {
+		printk("Couldn't register kprobe, err=%d\n", ret);
+		return -1;
+	}
+
+	ret = sample_c_j();
+	if (ret != 4) {
+		printk("ERROR: expect value 4, got %d\n", ret);
+		return -1;
+	}
+	else {
+		printk("Got value 4, all good!\n");
+		return 0;
+	}
+}
+
+static int test_c_jr(void) {
+	static struct kprobe kp;
+	int ret;
+
+	/* Test C.JR */
+	kp.symbol_name = "c_jr_location";
+	kp.pre_handler = pre_handler;
+
+	ret = register_kprobe(&kp);
+	if (ret) {
+		printk("Couldn't register kprobe, err=%d\n", ret);
+		return -1;
+	}
+
+	ret = sample_c_jr();
+	if (ret != 4) {
+		printk("Expect value 4, got %d\n", ret);
+		return -1;
+	}
+	else {
+		printk("Got value 4, all good!\n");
+		return 0;
+	}
+}
+
+static int test_c_jalr(void) {
+	struct kprobe kp;
+	int ret;
+
+	memset(&kp, 0, sizeof(kp));
+
+	/* Test C.JR */
+	kp.symbol_name = "c_jalr_location";
+	kp.pre_handler = pre_handler;
+
+	ret = register_kprobe(&kp);
+	if (ret) {
+		printk("Couldn't register kprobe, err=%d\n", ret);
+		return -1;
+	}
+
+	ret = sample_c_jalr();
+	if (ret != 4) {
+		printk("Expect value 4, got %d\n", ret);
+		return -1;
+	}
+	else {
+		printk("Got value 4, all good!\n");
+	}
+
+	unregister_kprobe(&kp);
+
+	return 0;
+}
+
+static int test_c_bnez(void) {
+	static struct kprobe kp;
+
+	int ret;
+
+	/* Test C.JR */
+	kp.symbol_name = "sample_c_bnez";
+	kp.pre_handler = pre_handler;
+
+	ret = register_kprobe(&kp);
+	if (ret) {
+		printk("Couldn't register kprobe, err=%d\n", ret);
+		return -1;
+	}
+
+	ret = sample_c_bnez(1);
+	if (ret != 4) {
+		printk("Expect value 4, got %d\n", ret);
+		return -1;
+	} else {
+		printk("Got value 4, all good!\n");
+	}
+
+	ret = sample_c_bnez(0);
+	if (ret != 10) {
+		printk("Expect value 10, got %d\n", ret);
+		return -1;
+	} else {
+		printk("Got value 4, all good!\n");
+	}
+
+	return 0;
+}
+
+static int test_c_beqz(void) {
+	static struct kprobe kp;
+
+	int ret;
+
+	/* Test C.JR */
+	kp.symbol_name = "sample_c_beqz";
+	kp.pre_handler = pre_handler;
+
+	ret = register_kprobe(&kp);
+	if (ret) {
+		printk("Couldn't register kprobe, err=%d\n", ret);
+		return -1;
+	}
+
+	ret = sample_c_beqz(0);
+	if (ret != 4) {
+		printk("Expect value 4, got %d\n", ret);
+		return -1;
+	}
+	else {
+		printk("Got value 4, all good!\n");
+		return 0;
+	}
+
+	ret = sample_c_beqz(1);
+	if (ret != 10) {
+		printk("Expect value 10, got %d\n", ret);
+		return -1;
+	}
+	else {
+		printk("Got value 4, all good!\n");
+		return 0;
+	}
+}
+
+static int hello_init(void)
+{
+	printk("Hello\n");
+
+	printk("Testing C.J...\n");
+	if (test_c_j())
+		return -1;
+
+	printk("Testing C.JR...\n");
+	if (test_c_jr())
+		return -1;
+
+	printk("Testing C.JALR...\n");
+	if (test_c_jalr())
+		return -1;
+
+	printk("Testing C.BNEZ...\n");
+	if (test_c_bnez())
+		return -1;
+
+	printk("Testing C.BEQZ...\n");
+	if (test_c_beqz())
+		return -1;
+
+	return 0;
+}
+
+static void hello_exit(void)
+{
+	printk("Goodbye\n");
+}
+
+module_init(hello_init);
+module_exit(hello_exit);