From: Yuvraj Sakshith <yuvraj.kernel@gmail.com>
To: op-tee@lists.trustedfirmware.org
Subject: [RFC PATCH 2/7] tee: Add TEE Mediator module which aims to expose TEE to a KVM guest.
Date: Tue, 01 Apr 2025 22:35:22 +0530 [thread overview]
Message-ID: <20250401170527.344092-3-yuvraj.kernel@gmail.com> (raw)
In-Reply-To: <20250401170527.344092-1-yuvraj.kernel@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 6106 bytes --]
The TEE Mediator module is an upper abstraction layer which lets KVM guests
interact with a trusted execution environment.
TEE specific subsystems (such as OP-TEE, for example) can register a set of
handlers through tee_mediator_register_ops() with the TEE Mediator, which will
be called by the kernel when required.
Given this module, architecture specific TEE drivers can implement handler functions to work with these
events if necessary. In most implementations, a special instruction (such as SMC, in arm64) switches control
leading to the TEE. These instructions are usually trapped by the hypervisor when executed by the guest.
This module allows making use of these trapped instructions and mediating the request between guest and TEE.
Signed-off-by: Yuvraj Sakshith <yuvraj.kernel@gmail.com>
---
drivers/tee/Kconfig | 5 ++
drivers/tee/Makefile | 1 +
drivers/tee/tee_mediator.c | 145 +++++++++++++++++++++++++++++++++++
include/linux/tee_mediator.h | 39 ++++++++++
4 files changed, 190 insertions(+)
create mode 100644 drivers/tee/tee_mediator.c
create mode 100644 include/linux/tee_mediator.h
diff --git a/drivers/tee/Kconfig b/drivers/tee/Kconfig
index 61b507c18780..dc446c9746ee 100644
--- a/drivers/tee/Kconfig
+++ b/drivers/tee/Kconfig
@@ -11,6 +11,11 @@ menuconfig TEE
This implements a generic interface towards a Trusted Execution
Environment (TEE).
+config TEE_MEDIATOR
+ bool "Trusted Execution Environment Mediator support"
+ depends on KVM
+ help
+ Provides an abstraction layer for TEE drivers to mediate KVM guest requests to the TEE.
if TEE
source "drivers/tee/optee/Kconfig"
diff --git a/drivers/tee/Makefile b/drivers/tee/Makefile
index 5488cba30bd2..46c44e59dd0b 100644
--- a/drivers/tee/Makefile
+++ b/drivers/tee/Makefile
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_TEE) += tee.o
+obj-$(CONFIG_TEE_MEDIATOR) += tee_mediator.o
tee-objs += tee_core.o
tee-objs += tee_shm.o
tee-objs += tee_shm_pool.o
diff --git a/drivers/tee/tee_mediator.c b/drivers/tee/tee_mediator.c
new file mode 100644
index 000000000000..d1ae7f4cb994
--- /dev/null
+++ b/drivers/tee/tee_mediator.c
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * TEE Mediator for the Linux Kernel
+ *
+ * This module enables a KVM guest to interact with a
+ * Trusted Execution Environment in the secure processing
+ * state provided by the architecture.
+ *
+ * Author:
+ * Yuvraj Sakshith <yuvraj.kernel@gmail.com>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/tee_mediator.h>
+
+static struct tee_mediator *mediator;
+
+int tee_mediator_register_ops(struct tee_mediator_ops *ops)
+{
+
+ int ret = 0;
+
+ if (!ops) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (!mediator) {
+ ret = -EOPNOTSUPP;
+ goto out;
+ }
+
+ mediator->ops = ops;
+
+out:
+ return ret;
+}
+
+int tee_mediator_is_active(void)
+{
+ return (mediator != NULL &&
+ mediator->ops != NULL && mediator->ops->is_active());
+}
+
+int tee_mediator_create_host(void)
+{
+ int ret = 0;
+
+ if (!tee_mediator_is_active() || !mediator->ops->create_host) {
+ ret = -ENODEV;
+ goto out;
+ }
+
+ ret = mediator->ops->create_host();
+
+out:
+ return ret;
+}
+
+int tee_mediator_destroy_host(void)
+{
+ int ret = 0;
+
+ if (!tee_mediator_is_active() || !mediator->ops->destroy_host) {
+ ret = -ENODEV;
+ goto out;
+ }
+
+ ret = mediator->ops->destroy_host();
+out:
+ return ret;
+}
+
+int tee_mediator_create_vm(struct kvm *kvm)
+{
+ int ret = 0;
+
+ if (!kvm) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (!tee_mediator_is_active() || !mediator->ops->create_vm) {
+ ret = -ENODEV;
+ goto out;
+ }
+
+ ret = mediator->ops->create_vm(kvm);
+
+out:
+ return ret;
+}
+
+int tee_mediator_destroy_vm(struct kvm *kvm)
+{
+ int ret = 0;
+
+ if (!kvm) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (!tee_mediator_is_active() || !mediator->ops->destroy_vm) {
+ ret = -ENODEV;
+ goto out;
+ }
+
+ ret = mediator->ops->destroy_vm(kvm);
+
+out:
+ return ret;
+}
+
+void tee_mediator_forward_request(struct kvm_vcpu *vcpu)
+{
+ if (!vcpu || !tee_mediator_is_active() || !mediator->ops->forward_request)
+ return;
+
+ mediator->ops->forward_request(vcpu);
+}
+
+static int __init tee_mediator_init(void)
+{
+ int ret = 0;
+
+ mediator = kzalloc(sizeof(*mediator), GFP_KERNEL);
+ if (!mediator) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ pr_info("mediator initialised\n");
+out:
+ return ret;
+}
+module_init(tee_mediator_init);
+
+static void __exit tee_mediator_exit(void)
+{
+ kfree(mediator);
+
+ pr_info("mediator exiting\n");
+}
+module_exit(tee_mediator_exit);
diff --git a/include/linux/tee_mediator.h b/include/linux/tee_mediator.h
new file mode 100644
index 000000000000..4a971de158ec
--- /dev/null
+++ b/include/linux/tee_mediator.h
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * TEE Mediator for the Linux Kernel
+ *
+ * This module enables a KVM guest to interact with a
+ * Trusted Execution Environment in the secure processing
+ * state provided by the architecture.
+ *
+ * Author:
+ * Yuvraj Sakshith <yuvraj.kernel@gmail.com>
+ */
+
+#ifndef __TEE_MEDIATOR_H
+#define __TEE_MEDIATOR_H
+
+#include <linux/kvm_host.h>
+
+struct tee_mediator_ops {
+ int (*create_host)(void);
+ int (*destroy_host)(void);
+ int (*create_vm)(struct kvm *kvm);
+ int (*destroy_vm)(struct kvm *kvm);
+ void (*forward_request)(struct kvm_vcpu *vcpu);
+ int (*is_active)(void);
+};
+
+struct tee_mediator {
+ struct tee_mediator_ops *ops;
+};
+
+int tee_mediator_create_host(void);
+int tee_mediator_destroy_host(void);
+int tee_mediator_create_vm(struct kvm *kvm);
+int tee_mediator_destroy_vm(struct kvm *kvm);
+void tee_mediator_forward_request(struct kvm_vcpu *vcpu);
+int tee_mediator_is_active(void);
+int tee_mediator_register_ops(struct tee_mediator_ops *ops);
+
+#endif
--
2.43.0
next prev parent reply other threads:[~2025-04-01 17:05 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-01 17:05 [RFC PATCH 0/7] KVM: optee: Introduce OP-TEE Mediator for exposing secure world to KVM guests Yuvraj Sakshith
2025-04-01 17:05 ` [RFC PATCH 1/7] firmware: smccc: Add macros for Trusted OS/App owner check on SMC value Yuvraj Sakshith
2025-04-01 17:05 ` Yuvraj Sakshith [this message]
2025-04-01 17:05 ` [RFC PATCH 3/7] KVM: Notify TEE Mediator when KVM creates and destroys guests Yuvraj Sakshith
2025-04-01 17:05 ` [RFC PATCH 4/7] KVM: arm64: Forward guest CPU state to TEE mediator on SMC trap Yuvraj Sakshith
2025-04-01 17:05 ` [RFC PATCH 5/7] tee: optee: Add OPTEE_SMC_VM_CREATED and OPTEE_SMC_VM_DESTROYED Yuvraj Sakshith
2025-04-01 17:05 ` [RFC PATCH 6/7] tee: optee: Add OP-TEE Mediator Yuvraj Sakshith
2025-04-01 17:05 ` [RFC PATCH 7/7] tee: optee: Notify TEE Mediator on OP-TEE driver initialization and release Yuvraj Sakshith
2025-04-01 18:13 ` [RFC PATCH 0/7] KVM: optee: Introduce OP-TEE Mediator for exposing secure world to KVM guests Marc Zyngier
2025-04-02 2:58 ` Yuvraj Sakshith
2025-04-02 8:42 ` Marc Zyngier
2025-04-02 11:19 ` Yuvraj Sakshith
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=20250401170527.344092-3-yuvraj.kernel@gmail.com \
--to=yuvraj.kernel@gmail.com \
--cc=op-tee@lists.trustedfirmware.org \
/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