[DONOTMERGE] pkvm-mod1 and pkvm-mod2 for symbol sharing testing
Change-Id: I6a09819afe90e7c275348b1fb1e20075df983708
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index f060107..c665952 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -72,3 +72,4 @@
obj-$(CONFIG_MARVELL_CN10K_DPI) += mrvl_cn10k_dpi.o
obj-y += keba/
obj-$(CONFIG_UID_SYS_STATS) += uid_sys_stats.o
+obj-y += pkvm_test/
diff --git a/drivers/misc/pkvm_test/Makefile b/drivers/misc/pkvm_test/Makefile
new file mode 100644
index 0000000..a0690d0
--- /dev/null
+++ b/drivers/misc/pkvm_test/Makefile
@@ -0,0 +1 @@
+obj-y := pkvm-mod1/ pkvm-mod2/
diff --git a/drivers/misc/pkvm_test/pkvm-mod1/Makefile b/drivers/misc/pkvm_test/pkvm-mod1/Makefile
new file mode 100644
index 0000000..505bdcb
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod1/Makefile
@@ -0,0 +1,5 @@
+obj-m += pkvm_mod1.o
+pkvm_mod1-y := main.o hyp/kvm_nvhe.o
+
+$(obj)/hyp/kvm_nvhe.o: FORCE
+ $(Q)$(MAKE) $(build)=$(obj)/hyp $(obj)/hyp/kvm_nvhe.o
diff --git a/drivers/misc/pkvm_test/pkvm-mod1/hyp/.gitignore b/drivers/misc/pkvm_test/pkvm-mod1/hyp/.gitignore
new file mode 100644
index 0000000..185e3ad6
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod1/hyp/.gitignore
@@ -0,0 +1,2 @@
+hyp-reloc.S
+hyp.lds
diff --git a/drivers/misc/pkvm_test/pkvm-mod1/hyp/Makefile b/drivers/misc/pkvm_test/pkvm-mod1/hyp/Makefile
new file mode 100644
index 0000000..a30bba7
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod1/hyp/Makefile
@@ -0,0 +1,2 @@
+hyp-obj-y := hyp_mod1.o
+include $(srctree)/arch/arm64/kvm/hyp/nvhe/Makefile.module
diff --git a/drivers/misc/pkvm_test/pkvm-mod1/hyp/events.h b/drivers/misc/pkvm_test/pkvm-mod1/hyp/events.h
new file mode 100644
index 0000000..f62d690
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod1/hyp/events.h
@@ -0,0 +1,18 @@
+#if !defined(__PKVM_SMC_FILTER_HYPEVENTS_H_) || defined(HYP_EVENT_MULTI_READ)
+#define __PKVM_SMC_FILTER_HYPEVENTS_H_
+
+#ifdef __KVM_NVHE_HYPERVISOR__
+#include <trace.h>
+#endif
+
+HYP_EVENT(pkvm_mod1,
+ HE_PROTO(u64 nr_calls),
+ HE_STRUCT(
+ he_field(u64, nr_calls)
+ ),
+ HE_ASSIGN(
+ __entry->nr_calls = nr_calls;
+ ),
+ HE_PRINTK("nr_calls=%lld", __entry->nr_calls)
+);
+#endif
diff --git a/drivers/misc/pkvm_test/pkvm-mod1/hyp/hyp_mod1.c b/drivers/misc/pkvm_test/pkvm-mod1/hyp/hyp_mod1.c
new file mode 100644
index 0000000..7bdf3d5
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod1/hyp/hyp_mod1.c
@@ -0,0 +1,50 @@
+#include <asm/kvm_pkvm_module.h>
+
+#include "events.h"
+#define HYP_EVENT_FILE ../../../../drivers/misc/pkvm_test/pkvm-mod1/hyp/events.h
+#include <define_events.h>
+
+const struct pkvm_module_ops *pkvm_ops;
+
+#ifdef CONFIG_TRACING
+extern char __hyp_event_ids_start[];
+extern char __hyp_event_ids_end[];
+
+void *tracing_reserve_entry(unsigned long length)
+{
+ return pkvm_ops->tracing_reserve_entry(length);
+}
+
+void tracing_commit_entry(void)
+{
+ pkvm_ops->tracing_commit_entry();
+}
+
+void trace___hyp_printk(u8 fmt_id, u64 a, u64 b, u64 c, u64 d)
+{
+ pkvm_ops->tracing_mod_hyp_printk(fmt_id, a, b, c, d);
+}
+#endif
+
+int pkvm_mod1_export(void)
+{
+ static u64 cnt = 0;
+
+ trace_pkvm_mod1(cnt++);
+
+ trace_hyp_printk("Hi there!");
+
+ return 0;
+}
+
+
+int pkvm_mod1_hyp_init(const struct pkvm_module_ops *ops)
+{
+ pkvm_ops = ops;
+
+#ifdef CONFIG_TRACING
+ ops->register_hyp_event_ids((unsigned long)__hyp_event_ids_start,
+ (unsigned long)__hyp_event_ids_end);
+#endif
+ return 0;
+}
diff --git a/drivers/misc/pkvm_test/pkvm-mod1/main.c b/drivers/misc/pkvm_test/pkvm-mod1/main.c
new file mode 100644
index 0000000..ecbf42d
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod1/main.c
@@ -0,0 +1,25 @@
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <asm/kvm_pkvm_module.h>
+
+#define HYP_EVENT_FILE ../../../../drivers/misc/pkvm_test/pkvm-mod1/hyp/events.h
+#include <asm/kvm_define_hypevents.h>
+
+int __kvm_nvhe_pkvm_mod1_hyp_init(const struct pkvm_module_ops *ops);
+
+int __kvm_nvhe_pkvm_mod1_export(void);
+EXPORT_SYMBOL(__kvm_nvhe_pkvm_mod1_export);
+
+static int __init pkvm_mod1_init(void)
+{
+ unsigned long token;
+ int ret;
+
+ ret = pkvm_load_el2_module(__kvm_nvhe_pkvm_mod1_hyp_init, &token);
+ if (ret)
+ return ret;
+ return 0;
+}
+module_init(pkvm_mod1_init);
+MODULE_LICENSE("GPL");
diff --git a/drivers/misc/pkvm_test/pkvm-mod2/Makefile b/drivers/misc/pkvm_test/pkvm-mod2/Makefile
new file mode 100644
index 0000000..e9f6e73
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod2/Makefile
@@ -0,0 +1,5 @@
+obj-m += pkvm_mod2.o
+pkvm_mod2-y := main.o hyp/kvm_nvhe.o
+
+$(obj)/hyp/kvm_nvhe.o: FORCE
+ $(Q)$(MAKE) $(build)=$(obj)/hyp $(obj)/hyp/kvm_nvhe.o
diff --git a/drivers/misc/pkvm_test/pkvm-mod2/hyp/.gitignore b/drivers/misc/pkvm_test/pkvm-mod2/hyp/.gitignore
new file mode 100644
index 0000000..185e3ad6
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod2/hyp/.gitignore
@@ -0,0 +1,2 @@
+hyp-reloc.S
+hyp.lds
diff --git a/drivers/misc/pkvm_test/pkvm-mod2/hyp/Makefile b/drivers/misc/pkvm_test/pkvm-mod2/hyp/Makefile
new file mode 100644
index 0000000..18aedd9
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod2/hyp/Makefile
@@ -0,0 +1,2 @@
+hyp-obj-y := hyp_mod2.o
+include $(srctree)/arch/arm64/kvm/hyp/nvhe/Makefile.module
diff --git a/drivers/misc/pkvm_test/pkvm-mod2/hyp/hyp_mod2.c b/drivers/misc/pkvm_test/pkvm-mod2/hyp/hyp_mod2.c
new file mode 100644
index 0000000..dbbc928
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod2/hyp/hyp_mod2.c
@@ -0,0 +1,16 @@
+#include <asm/kvm_pkvm_module.h>
+
+extern int pkvm_mod1_export(void);
+
+void pkvm_mod2_hyp_hvc(struct user_pt_regs *regs)
+{
+ pkvm_mod1_export();
+
+ regs->regs[1] = 0;
+ regs->regs[0] = 0;
+}
+
+int pkvm_mod2_hyp_init(const struct pkvm_module_ops *ops)
+{
+ return 0;
+}
diff --git a/drivers/misc/pkvm_test/pkvm-mod2/main.c b/drivers/misc/pkvm_test/pkvm-mod2/main.c
new file mode 100644
index 0000000..357e5a4
--- /dev/null
+++ b/drivers/misc/pkvm_test/pkvm-mod2/main.c
@@ -0,0 +1,49 @@
+#include <linux/debugfs.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <asm/kvm_pkvm_module.h>
+
+static int hvc_number;
+
+int __kvm_nvhe_pkvm_mod2_hyp_init(const struct pkvm_module_ops *ops);
+void __kvm_nvhe_pkvm_mod2_hyp_hvc(struct user_pt_regs *regs);
+
+static int hvc_open(struct inode *inode, struct file *f)
+{
+ return pkvm_el2_mod_call(hvc_number);
+}
+
+ssize_t hvc_write(struct file *f, const char __user *buf, size_t len, loff_t *off)
+{
+ return len;
+}
+
+static const struct file_operations hvc_fops = {
+ .open = hvc_open,
+ .read = NULL,
+ .write = hvc_write,
+ .llseek = default_llseek,
+};
+
+static int __init pkvm_mod2_init(void)
+{
+ unsigned long token;
+ int ret;
+
+ ret = pkvm_load_el2_module(__kvm_nvhe_pkvm_mod2_hyp_init, &token);
+ if (ret)
+ return ret;
+
+ ret = pkvm_register_el2_mod_call(__kvm_nvhe_pkvm_mod2_hyp_hvc, token);
+ if (ret < 0)
+ return ret;
+
+ hvc_number = ret;
+
+ debugfs_create_file("pkvm_mod2", 0200, NULL, NULL, &hvc_fops);
+
+ return 0;
+}
+module_init(pkvm_mod2_init);
+MODULE_LICENSE("GPL");
diff --git a/modules.bzl b/modules.bzl
index f97fbc4..682f93b 100644
--- a/modules.bzl
+++ b/modules.bzl
@@ -13,6 +13,8 @@
"drivers/bluetooth/btqca.ko",
"drivers/bluetooth/btsdio.ko",
"drivers/bluetooth/hci_uart.ko",
+ "drivers/misc/pkvm_test/pkvm-mod1/pkvm_mod1.ko",
+ "drivers/misc/pkvm_test/pkvm-mod2/pkvm_mod2.ko",
"drivers/net/can/dev/can-dev.ko",
"drivers/net/can/slcan/slcan.ko",
"drivers/net/can/vcan.ko",