From: Nam Cao <namcaov@gmail.com>
To: Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: Nam Cao <namcaov@gmail.com>
Subject: test code for kprobe
Date: Sun, 30 Jul 2023 10:27:10 +0200 [thread overview]
Message-ID: <fc71730b9350026427fe1c2bdbce9b993d8bc6f5.1690704360.git.namcaov@gmail.com> (raw)
In-Reply-To: <cover.1690704360.git.namcaov@gmail.com>
---
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
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);
--
2.34.1
next prev parent reply other threads:[~2023-07-30 8:27 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-30 8:27 [PATCH 0/3] riscv: kprobes: simulate some instructions Nam Cao
2023-07-30 8:27 ` [PATCH 1/3] riscv: kprobes: simulate c.j instruction Nam Cao
2023-08-09 0:11 ` Charlie Jenkins
2023-08-09 1:05 ` Charlie Jenkins
2023-07-30 8:27 ` [PATCH 2/3] riscv: kprobes: simulate c.jr and c.jalr instructions Nam Cao
2023-08-09 1:06 ` Charlie Jenkins
2023-07-30 8:27 ` [PATCH 3/3] riscv: kprobes: simulate c.beqz and c.bnez Nam Cao
2023-08-09 1:14 ` Charlie Jenkins
2023-07-30 8:27 ` Nam Cao [this message]
2023-08-14 12:28 ` test code for kprobe Björn Töpel
2023-08-14 13:50 ` Nam Cao
2023-08-14 14:10 ` Björn Töpel
2023-08-30 13:20 ` [PATCH 0/3] riscv: kprobes: simulate some instructions patchwork-bot+linux-riscv
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fc71730b9350026427fe1c2bdbce9b993d8bc6f5.1690704360.git.namcaov@gmail.com \
--to=namcaov@gmail.com \
--cc=aou@eecs.berkeley.edu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®