Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2015-2019 TrustKernel Incorporated
ifneq ($(strip $(M)),)
obj-m += tkcore.o
else
obj-y += tkcore.o
endif
M ?=$(srctree)/drivers/tee/tkcore
#ccflags-y+=-Werror
ccflags-y += -I$(M)/include/linux
ccflags-y += -I$(M)/include
ccflags-y += -I$(M)
tkcore-objs:= \
tee_core.o \
tee_context.o \
tee_session.o \
tee_shm.o \
tee_supp_com.o \
tee_sysfs.o \
tee_kernel_api.o \
tee_wait_queue.o \
tee_tui.o \
tee_tui_hal.o \
tee_procfs.o \
tee_ta_mgmt.o \
tee_clkmgr.o \
tee_fp.o \
pm.o \
+132
View File
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/wait.h>
#include <linux/freezer.h>
#include <linux/suspend.h>
#include <linux/atomic.h>
#include "pm.h"
/* tee_wakeup_cnt == 0 means currently
* the system is in idle state, without
* wakeup source or suspend request.
* wakeup source > 0 means there are outgoing
* commands which stop the system from suspending.
* wakeup source == -1 means the system is now
* preparing for suspend or suspending
*/
static atomic_t tee_wakeup_cnt = ATOMIC_INIT(0);
static DECLARE_WAIT_QUEUE_HEAD(suspend_done);
static DECLARE_WAIT_QUEUE_HEAD(awake_done);
static void tee_keep_awake(void)
{
while (atomic_inc_unless_negative(&tee_wakeup_cnt) == 0)
wait_event_freezable(suspend_done,
atomic_read(&tee_wakeup_cnt) >= 0);
}
static void tee_cancel_awake(void)
{
if (atomic_dec_and_test(&tee_wakeup_cnt)) {
/* wake_up() implies a memory barrier */
wake_up(&awake_done);
}
}
static void tee_prepare_suspend(void)
{
int r;
while ((r = atomic_read(&tee_wakeup_cnt)) >= 0) {
if (atomic_cmpxchg(&tee_wakeup_cnt, 0, -1) == 0)
return;
/* wait_event() implies a memory barrier */
wait_event(awake_done, atomic_read(&tee_wakeup_cnt) == 0);
}
pr_warn("tee_wakeup_cnt unexpected value: %d\n", r);
}
#if defined(CONFIG_TRUSTKERNEL_TEE_RPMB_SUPPORT) && \
defined(IN_KERNEL_RPMB_SUPPORT)
#include "linux/tee_rpmb.h"
#endif
static void tee_post_suspend(void)
{
/* we do not need to use atomic instruction here,
* because there is one single suspend source.
*/
atomic_set(&tee_wakeup_cnt, atomic_read(&tee_wakeup_cnt) + 1);
wake_up(&suspend_done);
}
static int tee_pm_suspend_notifier(struct notifier_block *nb,
unsigned long event, void *dummy)
{
switch (event) {
case PM_SUSPEND_PREPARE:
tee_prepare_suspend();
#if defined(CONFIG_TRUSTKERNEL_TEE_RPMB_SUPPORT) && \
defined(IN_KERNEL_RPMB_SUPPORT)
{
struct tkcore_rpmb_request rq = {
.type = TEE_RPMB_SWITCH_NORMAL
};
tkcore_emmc_rpmb_execute(&rq);
}
#endif
break;
case PM_POST_SUSPEND:
tee_post_suspend();
break;
default:
break;
}
return NOTIFY_OK;
}
static struct notifier_block tz_pm_notifier = {
.notifier_call = tee_pm_suspend_notifier,
};
int tkcore_stay_awake(void *fn, void *data)
{
int r;
tee_keep_awake();
r = ((int (*) (void *)) fn) (data);
tee_cancel_awake();
return r;
}
int tkcore_tee_pm_init(void)
{
int r;
r = register_pm_notifier(&tz_pm_notifier);
if (r) {
pr_err("failed to register pm notifier: %d\n", r);
return r;
}
return 0;
}
void tkcore_tee_pm_exit(void)
{
int r;
r = unregister_pm_notifier(&tz_pm_notifier);
if (r)
pr_err("failed to unregister_pm_notifier: %d\n", r);
}
+15
View File
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TKCORE_PM_H
#define TKCORE_PM_H
int tkcore_stay_awake(void *fn, void *data);
int tkcore_tee_pm_init(void);
void tkcore_tee_pm_exit(void);
#endif
+175
View File
@@ -0,0 +1,175 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/kernel.h>
#include <linux/compiler.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/tee_clkmgr.h>
#include <linux/tee_client_api.h>
#include "tee_clkmgr_priv.h"
struct clkmgr_handle {
uint32_t token;
void *e, *d;
const void *p0, *p1, *p2;
size_t argnum;
struct list_head le;
};
/* sync with tee-os */
enum tee_clkmgr_type {
TEE_CLKMGR_TYPE_SPI = 0,
TEE_CLKMGR_TYPE_I2C,
TEE_CLKMGR_TYPE_I2C_DMA
};
static const char * const clkid[] = {
[TEE_CLKMGR_TYPE_SPI] = "spi",
[TEE_CLKMGR_TYPE_I2C] = "i2c",
[TEE_CLKMGR_TYPE_I2C_DMA] = "i2c-dma",
};
static LIST_HEAD(clk_list);
static DEFINE_SPINLOCK(clk_list_lock);
/* called inside list_lock */
static struct clkmgr_handle *get_clkmgr_handle(uint32_t token)
{
struct clkmgr_handle *h;
list_for_each_entry(h, &clk_list, le) {
if (h->token == token)
return h;
}
return NULL;
}
int tee_clkmgr_handle(uint32_t token, uint32_t op)
{
struct clkmgr_handle *ph, h;
void *fn;
spin_lock(&clk_list_lock);
ph = get_clkmgr_handle(token | TEE_CLKMGR_TOKEN_NOT_LEGACY);
if (ph == NULL) {
pr_err("invalid token %u\n", token);
spin_unlock(&clk_list_lock);
return TEEC_ERROR_ITEM_NOT_FOUND;
}
memcpy(&h, ph, sizeof(h));
spin_unlock(&clk_list_lock);
fn = (op & TEE_CLKMGR_OP_ENABLE) ? h.e : h.d;
if (h.argnum == 0) {
((void (*)(void)) fn) ();
} else if (h.argnum == 1) {
((void (*)(const void *)) fn) (h.p0);
} else if (h.argnum == 2) {
((void (*)(const void *, const void *)) fn) (h.p0, h.p1);
} else if (h.argnum == 3) {
((void (*) (const void *, const void *, const void *)) fn)
(h.p0, h.p1, h.p2);
} else {
pr_err("unsupported token %u argnum %zu\n",
h.token, h.argnum);
return TEEC_ERROR_NOT_SUPPORTED;
}
return 0;
}
EXPORT_SYMBOL(tee_clkmgr_handle);
int tee_clkmgr_register(const char *clkname, int id, void *e, void *d,
void *p0, void *p1, void *p2, size_t argnum)
{
size_t n;
struct clkmgr_handle *h, *w;
pr_info("tkcoredrv: clkname=%s id=%d\n",
clkname, id);
if (argnum > 3) {
pr_err("does not support argnum %zu\n", argnum);
return -EINVAL;
}
for (n = 0; n < ARRAY_SIZE(clkid); n++) {
if (clkid[n] && strcmp(clkname, clkid[n]) == 0)
break;
}
if (n == ARRAY_SIZE(clkid)) {
pr_err("invalid clkname %s\n", clkname);
return -EINVAL;
}
if ((id << TEE_CLKMGR_TOKEN_ID_SHIFT) &
(TEE_CLKMGR_TOKEN_TYPE_MASK << TEE_CLKMGR_TOKEN_TYPE_SHIFT)) {
pr_err("%s-%d: invalid id\n", clkname, id);
return -EINVAL;
}
h = kmalloc(sizeof(struct clkmgr_handle), GFP_KERNEL);
if (h == NULL)
return -ENOMEM;
h->token = TEE_CLKMGR_TOKEN((uint32_t) n, (uint32_t) id);
h->e = e;
h->d = d;
h->p0 = p0;
h->p1 = p1;
h->p2 = p2;
h->argnum = argnum;
spin_lock(&clk_list_lock);
/* check for duplication */
list_for_each_entry(w, &clk_list, le) {
if (w->token == h->token) {
pr_err("clk 0x%x already registered\n",
h->token);
spin_unlock(&clk_list_lock);
return -EINVAL;
}
}
list_add(&(h->le), &clk_list);
spin_unlock(&clk_list_lock);
return 0;
}
EXPORT_SYMBOL(tee_clkmgr_register);
int tee_clkmgr_init(void)
{
return 0;
}
void tee_clkmgr_exit(void)
{
struct clkmgr_handle *h, *n;
spin_lock(&clk_list_lock);
list_for_each_entry_safe(h, n, &clk_list, le) {
list_del(&(h->le));
kfree(h);
}
spin_unlock(&clk_list_lock);
}
+12
View File
@@ -0,0 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_CLKMGR_PRIV_H
#define TEE_CLKMGR_PRIV_H
int tee_clkmgr_init(void);
void tee_clkmgr_exit(void);
#endif
+295
View File
@@ -0,0 +1,295 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/file.h>
#include <linux/atomic.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include "tee_shm.h"
#include "tee_core_priv.h"
/**
* tee_context_dump - Dump in a buffer the information (ctx, sess & shm)
* associated to a tee.
*/
int tee_context_dump(struct tee *tee, char *buff, size_t len)
{
struct list_head *ptr_ctx, *ptr_sess, *ptr_shm;
struct tee_context *ctx;
struct tee_session *sess;
struct tee_shm *shm;
int i = -1;
int j = 0;
int pos = 0;
WARN_ON(!tee);
if (len < 80 || list_empty(&tee->list_ctx))
return 0;
mutex_lock(&tee->lock);
list_for_each(ptr_ctx, &tee->list_ctx) {
ctx = list_entry(ptr_ctx, struct tee_context, entry);
++i;
pos += sprintf(buff + pos,
"[%02d] ctx=%p (refcount=%d) (usr=%d)",
i, ctx,
(int)kref_read(&ctx->refcount),
ctx->usr_client);
pos += sprintf(buff + pos, "name=\"%s\" (tgid=%d)\n",
ctx->name,
ctx->tgid);
if ((len - pos) < 80) {
pos = 0;
goto out;
}
if (list_empty(&ctx->list_sess))
continue;
j = 0;
list_for_each(ptr_sess, &ctx->list_sess) {
sess = list_entry(ptr_sess,
struct tee_session,
entry);
pos += sprintf(buff + pos,
"[%02d.%d] sess=%p sessid=%08x\n",
i, j, sess,
sess->sessid);
if ((len - pos) < 80) {
pos = 0;
goto out;
}
}
if (list_empty(&ctx->list_shm))
continue;
j = 0;
list_for_each(ptr_shm, &ctx->list_shm) {
shm = list_entry(ptr_shm, struct tee_shm, entry);
pos += sprintf(buff + pos,
"[%02d.%d] shm=%p paddr=%p kaddr=%p",
i, j, shm,
&shm->resv.paddr,
shm->resv.kaddr);
pos += sprintf(buff + pos,
" s=%zu(%zu)\n",
shm->size_req,
shm->size_alloc);
if ((len - pos) < 80) {
pos = 0;
goto out;
}
j++;
}
}
out:
mutex_unlock(&tee->lock);
return pos;
}
/**
* tee_context_create - Allocate and create a new context.
* Reference on the back-end is requested.
*/
struct tee_context *tee_context_create(struct tee *tee)
{
int ret;
struct tee_context *ctx;
ctx = devm_kzalloc(_DEV(tee), sizeof(struct tee_context), GFP_KERNEL);
if (!ctx) {
ctx = ERR_PTR(-ENOMEM);
pr_err("tee_context allocation failed\n");
return ctx;
}
kref_init(&ctx->refcount);
INIT_LIST_HEAD(&ctx->list_sess);
INIT_LIST_HEAD(&ctx->list_shm);
ctx->tee = tee;
snprintf(ctx->name, sizeof(ctx->name), "%s", current->comm);
ctx->tgid = current->tgid;
ret = tee_get(tee);
if (ret) {
devm_kfree(_DEV(tee), ctx);
return ERR_PTR(ret);
}
mutex_lock(&tee->lock);
tee_inc_stats(&tee->stats[TEE_STATS_CONTEXT_IDX]);
list_add_tail(&ctx->entry, &tee->list_ctx);
mutex_unlock(&tee->lock);
return ctx;
}
/**
* _tee_context_do_release - Final function to release
* and free a context.
*/
static void _tee_context_do_release(struct kref *kref)
{
struct tee_context *ctx;
struct tee *tee;
ctx = container_of(kref, struct tee_context, refcount);
WARN_ON(!ctx || !ctx->tee);
tee = ctx->tee;
tee_dec_stats(&tee->stats[TEE_STATS_CONTEXT_IDX]);
list_del(&ctx->entry);
devm_kfree(_DEV(tee), ctx);
tee_put(tee);
}
/**
* tee_context_get - Increase the reference count of
* the context.
*/
void tee_context_get(struct tee_context *ctx)
{
WARN_ON(!ctx || !ctx->tee);
kref_get(&ctx->refcount);
}
static int is_in_list(struct tee *tee, struct list_head *entry)
{
int present = 1;
if ((entry->next == LIST_POISON1) && (entry->prev == LIST_POISON2))
present = 0;
return present;
}
/**
* tee_context_put - Decreases the reference count of
* the context. If 0, the final
* release function is called.
*/
void tee_context_put(struct tee_context *ctx)
{
struct tee_context *_ctx = ctx;
struct tee *tee;
(void) _ctx;
WARN_ON(!ctx || !ctx->tee);
tee = ctx->tee;
if (!is_in_list(tee, &ctx->entry))
return;
kref_put(&ctx->refcount, _tee_context_do_release);
}
/**
* tee_context_destroy - Request to destroy a context.
*/
void tee_context_destroy(struct tee_context *ctx)
{
struct tee *tee;
if (!ctx || !ctx->tee)
return;
tee = ctx->tee;
mutex_lock(&tee->lock);
tee_context_put(ctx);
mutex_unlock(&tee->lock);
}
int tee_context_copy_from_client(const struct tee_context *ctx,
void *dest, const void *src, size_t size)
{
int res = 0;
if (dest && src && (size > 0)) {
if (ctx->usr_client)
res = copy_from_user(dest, src, size);
else
memcpy(dest, src, size);
}
return res;
}
struct tee_shm *tee_context_alloc_shm_tmp(struct tee_context *ctx,
size_t size, const void *src,
int type)
{
struct tee_shm *shm;
type &= (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT);
shm = tkcore_alloc_shm(ctx->tee, size,
TEE_SHM_MAPPED | TEE_SHM_TEMP | type);
if (IS_ERR_OR_NULL(shm)) {
pr_err("buffer allocation failed (%ld)\n",
PTR_ERR(shm));
return shm;
}
shm->ctx = ctx;
if (type & TEEC_MEM_INPUT) {
if (tee_context_copy_from_client(ctx,
shm->resv.kaddr, src, size)) {
pr_err(
"tee_context_copy_from_client failed\n");
tkcore_shm_free(shm);
shm = NULL;
}
}
return shm;
}
struct tee_shm *tee_context_create_tmpref_buffer(struct tee_context *ctx,
size_t size,
const void *buffer, int type)
{
struct tee_shm *shm = NULL;
int flags;
switch (type) {
case TEEC_MEMREF_TEMP_OUTPUT:
flags = TEEC_MEM_OUTPUT;
break;
case TEEC_MEMREF_TEMP_INPUT:
flags = TEEC_MEM_INPUT;
break;
case TEEC_MEMREF_TEMP_INOUT:
flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
break;
default:
flags = 0;
WARN_ON(1);
}
shm = tee_context_alloc_shm_tmp(ctx, size, buffer, flags);
return shm;
}
+960
View File
@@ -0,0 +1,960 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/idr.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/uaccess.h>
#include <asm-generic/ioctl.h>
#include <linux/sched.h>
#include <linux/version.h>
#include "linux/tee_core.h"
#include "linux/tee_ioc.h"
#include <linux/tee_client_api.h>
#include "tee_core_priv.h"
#include "tee_sysfs.h"
#include "tee_shm.h"
#include "tee_supp_com.h"
#include "tee_tui.h"
#include "tee_ta_mgmt.h"
#include "tee_procfs.h"
#include "tee_fp_priv.h"
#include "tee_clkmgr_priv.h"
#include "pm.h"
static uint32_t nsdrv_feature_flags;
#if defined(CONFIG_ARM)
#include <asm/mach/map.h>
#include <linux/io.h>
void *__arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
{
phys_addr_t last_addr;
unsigned long offset = phys_addr & ~PAGE_MASK;
unsigned long pfn = __phys_to_pfn(phys_addr);
last_addr = phys_addr + size - 1;
if (!size || last_addr < phys_addr)
return NULL;
return __arm_ioremap_pfn(pfn, offset, size, mtype);
}
#endif
void *tee_map_cached_shm(unsigned long pa, size_t len)
{
#if defined(CONFIG_ARM64)
return ioremap_cache(pa, len);
#elif defined(CONFIG_ARM)
return __arm_ioremap(pa, len, MT_MEMORY_RW);
#else
#error "tee_map_cached_shm() not implemented for this platform"
#endif
}
EXPORT_SYMBOL(tee_map_cached_shm);
void tee_unmap_cached_shm(void *va)
{
iounmap(va);
}
EXPORT_SYMBOL(tee_unmap_cached_shm);
#define _TEE_CORE_FW_VER "1:0.1"
static char *_tee_supp_app_name = "teed";
/* Store the class misc reference */
static struct class *misc_class;
static int device_match(struct device *device, const void *devname)
{
int ret;
struct tee *tee = dev_get_drvdata(device);
WARN_ON(!tee);
/*
* It shall always return
* 0 if tee is a null
* ptr
*/
if (tee == NULL)
return 0;
ret = strncmp(devname,
tee->name, sizeof(tee->name));
return ret == 0;
}
/*
* For the kernel api.
* Get a reference on a device tee from the device needed
*/
struct tee *tee_get_tee(const char *devname)
{
struct device *device;
if (!devname)
return NULL;
device = class_find_device(misc_class, NULL,
(void *) devname, device_match);
if (!device) {
pr_err("can't find device [%s]\n",
devname);
return NULL;
}
return dev_get_drvdata(device);
}
void tee_inc_stats(struct tee_stats_entry *entry)
{
entry->count++;
if (entry->count > entry->max)
entry->max = entry->count;
}
void tee_dec_stats(struct tee_stats_entry *entry)
{
entry->count--;
}
int __tee_get(struct tee *tee)
{
int ret = 0;
int v;
WARN_ON(!tee);
v = atomic_inc_return(&tee->refcount);
if (v == 1) {
WARN_ON(!try_module_get(tee->ops->owner));
get_device(tee->dev);
if (tee->ops->start)
ret = tee->ops->start(tee);
if (ret) {
pr_err("%s::start() failed, err=0x%x\n",
tee->name, ret);
put_device(tee->dev);
module_put(tee->ops->owner);
atomic_dec(&tee->refcount);
}
} else {
dev_warn(_DEV(tee), "Unexpected tee->refcount: 0x%x\n", v);
return -1;
}
return ret;
}
EXPORT_SYMBOL(__tee_get);
/**
* tee_get - increases refcount of the tee
* @tee: [in] tee to increase refcount of
*
* @note: If tee.ops.start() callback function is available,
* it is called when refcount is equal at 1.
*/
int tee_get(struct tee *tee)
{
int ret = 0;
WARN_ON(!tee);
if (atomic_inc_return(&tee->refcount) == 1) {
pr_warn("unexpected refcount 1\n");
} else {
int count = (int) atomic_read(&tee->refcount);
if (count > tee->max_refcount)
tee->max_refcount = count;
}
return ret;
}
/**
* tee_put - decreases refcount of the tee
* @tee: [in] tee to reduce refcount of
*
* @note: If tee.ops.stop() callback function is available,
* it is called when refcount is equal at 0.
*/
int tee_put(struct tee *tee)
{
int ret = 0;
int count;
WARN_ON(!tee);
if (atomic_dec_and_test(&tee->refcount)) {
pr_warn("unexpected refcount: 0\n");
/*
* tee should never be stopped
*/
}
count = (int)atomic_read(&tee->refcount);
return ret;
}
static int tee_supp_open(struct tee *tee)
{
int ret = 0;
WARN_ON(!tee->rpc);
if (strncmp(_tee_supp_app_name, current->comm,
strlen(_tee_supp_app_name)) == 0) {
if (atomic_add_return(1, &tee->rpc->used) > 1) {
ret = -EBUSY;
pr_err("Only one teed is allowed\n");
atomic_sub(1, &tee->rpc->used);
}
}
return ret;
}
static void tee_supp_release(struct tee *tee)
{
WARN_ON(!tee->rpc);
if ((atomic_read(&tee->rpc->used) == 1) &&
(strncmp(_tee_supp_app_name, current->comm,
strlen(_tee_supp_app_name)) == 0))
atomic_sub(1, &tee->rpc->used);
}
static int tee_ctx_open(struct inode *inode, struct file *filp)
{
struct tee_context *ctx;
struct tee *tee;
int ret;
tee = container_of(filp->private_data, struct tee, miscdev);
WARN_ON(!tee);
WARN_ON(tee->miscdev.minor != iminor(inode));
ret = tee_supp_open(tee);
if (ret)
return ret;
ctx = tee_context_create(tee);
if (IS_ERR_OR_NULL(ctx))
return PTR_ERR(ctx);
ctx->usr_client = 1;
filp->private_data = ctx;
return 0;
}
static int tee_ctx_release(struct inode *inode, struct file *filp)
{
struct tee_context *ctx = filp->private_data;
struct tee *tee;
if (!ctx)
return -EINVAL;
WARN_ON(!ctx->tee);
tee = ctx->tee;
WARN_ON(tee->miscdev.minor != iminor(inode));
tee_context_destroy(ctx);
tee_supp_release(tee);
return 0;
}
static int tee_do_create_session(struct tee_context *ctx,
struct tee_cmd_io __user *u_cmd)
{
int ret = -EINVAL;
struct tee_cmd_io k_cmd;
struct tee *tee;
tee = ctx->tee;
WARN_ON(!ctx->usr_client);
if (copy_from_user(&k_cmd, (void *)u_cmd, sizeof(struct tee_cmd_io))) {
pr_err("create_session: copy_from_user failed\n");
goto exit;
}
if (k_cmd.fd_sess > 0) {
pr_err("invalid fd_sess %d\n", k_cmd.fd_sess);
goto exit;
}
if ((k_cmd.op == NULL) || (k_cmd.uuid == NULL) ||
((k_cmd.data != NULL) && (k_cmd.data_size == 0)) ||
((k_cmd.data == NULL) && (k_cmd.data_size != 0))) {
pr_err("op or/and data parameters are not valid\n");
goto exit;
}
ret = tee_session_create_fd(ctx, &k_cmd);
put_user(k_cmd.err, &u_cmd->err);
put_user(k_cmd.origin, &u_cmd->origin);
if (ret)
goto exit;
put_user(k_cmd.fd_sess, &u_cmd->fd_sess);
exit:
return ret;
}
static int tee_do_shm_alloc_perm(struct tee_context *ctx,
struct tee_shm_io __user *u_shm)
{
int ret = -EINVAL;
struct tee_shm_io k_shm;
struct tee *tee = ctx->tee;
(void) tee;
if (copy_from_user(&k_shm, (void *)u_shm, sizeof(struct tee_shm_io))) {
pr_err("shm_alloc_perm: copy_from_user failed\n");
goto exit;
}
ret = tee_shm_alloc_io_perm(ctx, &k_shm);
if (ret)
goto exit;
put_user(k_shm.paddr, &u_shm->paddr);
put_user(k_shm.fd_shm, &u_shm->fd_shm);
put_user(k_shm.flags, &u_shm->flags);
exit:
return ret;
}
static int tee_do_shm_alloc(struct tee_context *ctx,
struct tee_shm_io __user *u_shm)
{
int ret = -EINVAL;
struct tee_shm_io k_shm;
struct tee *tee = ctx->tee;
WARN_ON(!ctx->usr_client);
if (copy_from_user(&k_shm, (void *)u_shm, sizeof(struct tee_shm_io))) {
pr_err("copy_from_user failed\n");
goto exit;
}
if ((k_shm.buffer != NULL) || (k_shm.fd_shm != 0) ||
((k_shm.flags & tee->shm_flags) == 0) ||
(k_shm.registered != 0)) {
pr_err(
"shm parameters are not valid %p %d %08x %08x %d\n",
(void *) k_shm.buffer,
k_shm.fd_shm,
(unsigned int) k_shm.flags,
(unsigned int) tee->shm_flags,
k_shm.registered);
goto exit;
}
ret = tee_shm_alloc_io(ctx, &k_shm);
if (ret)
goto exit;
put_user(k_shm.fd_shm, &u_shm->fd_shm);
put_user(k_shm.flags, &u_shm->flags);
exit:
return ret;
}
static int tee_do_get_fd_for_rpc_shm(struct tee_context *ctx,
struct tee_shm_io __user *u_shm)
{
int ret = -EINVAL;
struct tee_shm_io k_shm;
struct tee *tee = ctx->tee;
WARN_ON(!ctx->usr_client);
if (copy_from_user(&k_shm, (void *)u_shm, sizeof(struct tee_shm_io))) {
pr_err("copy_from_user failed\n");
goto exit;
}
if (k_shm.registered != 0) {
pr_err("expecting shm to be unregistered\n");
goto exit;
}
if ((k_shm.buffer == NULL) || (k_shm.size == 0) ||
(k_shm.fd_shm != 0)) {
pr_err("Invalid shm param. buffer: %p size: %u fd: %d\n",
k_shm.buffer, k_shm.size, k_shm.fd_shm);
goto exit;
}
if ((k_shm.flags & ~(tee->shm_flags)) ||
((k_shm.flags & tee->shm_flags) == 0)) {
pr_err(
"Invalid shm flags: 0x%x expecting to be within 0x%x\n",
k_shm.flags, tee->shm_flags);
goto exit;
}
ret = tee_shm_fd_for_rpc(ctx, &k_shm);
if (ret)
goto exit;
put_user(k_shm.fd_shm, &u_shm->fd_shm);
exit:
return ret;
}
static int tee_tui_notify(uint32_t arg)
{
if (teec_notify_event(arg))
return 0;
return -EINVAL;
}
static int tee_tui_wait(uint32_t __user *u_arg)
{
int r;
uint32_t cmd_id;
r = teec_wait_cmd(&cmd_id);
if (r)
return r;
if (copy_to_user(u_arg, &cmd_id, sizeof(cmd_id)))
return -EFAULT;
return 0;
}
static long tee_internal_ioctl(struct tee_context *ctx,
unsigned int cmd,
void __user *u_arg)
{
int ret = -EINVAL;
switch (cmd) {
case TEE_OPEN_SESSION_IOC:
ret = tee_do_create_session(ctx,
(struct tee_cmd_io __user *) u_arg);
break;
case TEE_ALLOC_SHM_PERM_IOC:
ret = tee_do_shm_alloc_perm(ctx,
(struct tee_shm_io __user *) u_arg);
break;
case TEE_ALLOC_SHM_IOC:
ret = tee_do_shm_alloc(ctx,
(struct tee_shm_io __user *) u_arg);
break;
case TEE_GET_FD_FOR_RPC_SHM_IOC:
ret = tee_do_get_fd_for_rpc_shm(ctx,
(struct tee_shm_io __user *) u_arg);
break;
case TEE_TUI_NOTIFY_IOC:
ret = tee_tui_notify(
(uint32_t) (unsigned long) u_arg);
break;
case TEE_TUI_WAITCMD_IOC:
ret = tee_tui_wait(
(uint32_t __user *) u_arg);
break;
case TEE_INSTALL_TA_IOC:
ret = tee_install_sp_ta(ctx, u_arg);
break;
case TEE_INSTALL_TA_RESP_IOC:
ret = tee_install_sp_ta_response(
ctx, u_arg);
break;
case TEE_DELETE_TA_IOC:
ret = tee_delete_sp_ta(ctx, u_arg);
break;
case TEE_QUERY_DRV_FEATURE_IOC:
if (u_arg) {
pr_info("tkcoredrv: nsdrv feature = 0x%x\n",
nsdrv_feature_flags);
if (copy_to_user(u_arg, &nsdrv_feature_flags,
sizeof(nsdrv_feature_flags))) {
ret = -EFAULT;
} else {
ret = 0;
}
} else
ret = -EINVAL;
break;
default:
pr_err("internal_ioctl: Unknown command: %u\n", cmd);
ret = -EINVAL;
}
return ret;
}
#ifdef CONFIG_COMPAT
static int convert_compat_tee_shm(struct TEEC_SharedMemory __user *shm)
{
if (unlikely(put_user(0, ((uint32_t __user *) &(shm->buffer)) + 1)))
return -EFAULT;
if (unlikely(put_user(0, ((uint32_t __user *) &(shm->size)) + 1)))
return -EFAULT;
if (unlikely(put_user(0, ((uint32_t __user *) &(shm->d.fd)) + 1)))
return -EFAULT;
return 0;
}
static int convert_compat_tee_param(union TEEC_Parameter __user *p,
uint32_t type)
{
struct TEEC_SharedMemory __user *p_shm;
switch (type) {
case TEEC_MEMREF_TEMP_INPUT:
case TEEC_MEMREF_TEMP_OUTPUT:
case TEEC_MEMREF_TEMP_INOUT:
if (unlikely(put_user(0,
((uint32_t __user *) &(p->tmpref.buffer)) + 1)))
return -EFAULT;
if (unlikely(put_user(0,
((uint32_t __user *) &(p->tmpref.size)) + 1)))
return -EFAULT;
break;
case TEEC_MEMREF_PARTIAL_INPUT:
case TEEC_MEMREF_PARTIAL_OUTPUT:
case TEEC_MEMREF_PARTIAL_INOUT:
case TEEC_MEMREF_WHOLE:
if (unlikely(put_user(0,
((uint32_t __user *) &(p->memref.parent)) + 1)))
return -EFAULT;
if (unlikely(put_user(0,
((uint32_t __user *) &(p->memref.size)) + 1)))
return -EFAULT;
if (unlikely(put_user(0,
((uint32_t __user *) &(p->memref.offset)) + 1)))
return -EFAULT;
if ((copy_from_user(&p_shm, &p->memref.parent,
sizeof(p_shm))))
return -EFAULT;
if (p_shm == NULL)
break;
if (convert_compat_tee_shm(p_shm))
return -EFAULT;
break;
default:
break;
}
return 0;
}
int convert_compat_tee_cmd(struct tee_cmd_io __user *u_cmd)
{
uint32_t i;
struct TEEC_Operation __user *p_op;
uint32_t paramTypes;
if (u_cmd == NULL)
return -EINVAL;
if (unlikely(put_user(0, ((uint32_t __user *) &u_cmd->uuid) + 1)))
return -EFAULT;
if (unlikely(put_user(0, ((uint32_t __user *) &u_cmd->data) + 1)))
return -EFAULT;
if (unlikely(put_user(0, ((uint32_t __user *) &u_cmd->op) + 1)))
return -EFAULT;
if (copy_from_user(&p_op, &u_cmd->op, sizeof(p_op)))
return -EFAULT;
if (p_op == NULL)
return -EINVAL;
if (copy_from_user(&paramTypes, (uint32_t __user *) &p_op->paramTypes,
sizeof(p_op->paramTypes)))
return -EFAULT;
if (unlikely(put_user(0, ((uint32_t __user *) &p_op->session) + 1)))
return -EFAULT;
for (i = 0; i < TEEC_CONFIG_PAYLOAD_REF_COUNT; i++) {
if (convert_compat_tee_param(&p_op->params[i],
TEEC_PARAM_TYPE_GET(paramTypes, i))) {
pr_err("bad param %u\n", i);
return -EFAULT;
}
}
return 0;
}
int convert_compat_tee_shm_io(struct tee_shm_io __user *shm_io)
{
if (shm_io == NULL)
return -EINVAL;
if (unlikely(put_user(0, ((uint32_t __user *) &shm_io->buffer) + 1)))
return -EFAULT;
return 0;
}
int convert_compat_tee_spta_inst(struct tee_spta_inst_desc __user *spta)
{
if (spta == NULL)
return -EINVAL;
if (unlikely(put_user(0,
((uint32_t __user *) &spta->ta_binary) + 1)))
return -EFAULT;
if (unlikely(put_user(0,
((uint32_t __user *) &spta->response_len) + 1)))
return -EFAULT;
return 0;
}
static long tee_compat_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
struct tee_context *ctx = filp->private_data;
void __user *u_arg;
WARN_ON(!ctx);
WARN_ON(!ctx->tee);
if (is_compat_task())
u_arg = compat_ptr(arg);
else
u_arg = (void __user *)arg;
switch (cmd) {
case TEE_OPEN_SESSION_IOC:
if (convert_compat_tee_cmd((struct tee_cmd_io __user *) u_arg))
return -EFAULT;
break;
case TEE_ALLOC_SHM_PERM_IOC:
case TEE_ALLOC_SHM_IOC:
case TEE_GET_FD_FOR_RPC_SHM_IOC:
if (convert_compat_tee_shm_io(
(struct tee_shm_io __user *) u_arg))
return -EFAULT;
break;
case TEE_INSTALL_TA_IOC:
if (convert_compat_tee_spta_inst(
(struct tee_spta_inst_desc __user *) u_arg))
return -EFAULT;
break;
default:
break;
}
return tee_internal_ioctl(ctx, cmd, u_arg);
}
#endif
static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct tee_context *ctx = filp->private_data;
WARN_ON(!ctx);
WARN_ON(!ctx->tee);
return tee_internal_ioctl(ctx, cmd, (void __user *) arg);
}
const struct file_operations tee_fops = {
.owner = THIS_MODULE,
.read = tee_supp_read,
.write = tee_supp_write,
.open = tee_ctx_open,
.release = tee_ctx_release,
#ifdef CONFIG_COMPAT
.compat_ioctl = tee_compat_ioctl,
#endif
.unlocked_ioctl = tee_ioctl
};
static void tee_plt_device_release(struct device *dev)
{
(void) dev;
}
static spinlock_t tee_idr_lock;
static struct idr tee_idr;
/* let caller to guarantee tee
* and id are not NULL. using lock to protect
*/
int tee_core_alloc_uuid(void *ptr)
{
int r;
idr_preload(GFP_KERNEL);
spin_lock(&tee_idr_lock);
r = idr_alloc(&tee_idr, ptr, 1, 0, GFP_NOWAIT);
if (r < 0)
pr_err("Bad alloc tee_uuid. rv: %d\n",
r);
spin_unlock(&tee_idr_lock);
idr_preload_end();
return r;
}
void *tee_core_uuid2ptr(int id)
{
return idr_find(&tee_idr, id);
}
/* let caller to guarantee tee
*and id are not NULL
*/
void tee_core_free_uuid(int id)
{
idr_remove(&tee_idr, id);
}
struct tee *tee_core_alloc(struct device *dev, char *name, int id,
const struct tee_ops *ops, size_t len)
{
struct tee *tee;
if (!dev || !name || !ops ||
!ops->open || !ops->close || !ops->alloc || !ops->free)
return NULL;
tee = devm_kzalloc(dev, sizeof(struct tee) + len, GFP_KERNEL);
if (!tee) {
tee = NULL;
pr_err("core_alloc: kzalloc failed\n");
return tee;
}
if (!dev->release)
dev->release = tee_plt_device_release;
tee->dev = dev;
tee->id = id;
tee->ops = ops;
tee->priv = &tee[1];
snprintf(tee->name, sizeof(tee->name), "%s", name);
pr_info("TEE core: Alloc the misc device \"%s\" (id=%d)\n",
tee->name, tee->id);
tee->miscdev.parent = dev;
tee->miscdev.minor = MISC_DYNAMIC_MINOR;
tee->miscdev.name = tee->name;
tee->miscdev.fops = &tee_fops;
mutex_init(&tee->lock);
atomic_set(&tee->refcount, 0);
INIT_LIST_HEAD(&tee->list_ctx);
INIT_LIST_HEAD(&tee->list_rpc_shm);
tee->state = TEE_OFFLINE;
tee->shm_flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT | TEEC_MEM_NONSECURE;
tee->test = 0;
if ((tee_supp_init(tee))) {
devm_kfree(dev, tee);
return NULL;
}
return tee;
}
EXPORT_SYMBOL(tee_core_alloc);
int tee_core_free(struct tee *tee)
{
if (tee) {
tee_supp_deinit(tee);
devm_kfree(tee->dev, tee);
}
return 0;
}
EXPORT_SYMBOL(tee_core_free);
int tee_core_add(struct tee *tee)
{
int rc = 0;
if (!tee)
return -EINVAL;
rc = misc_register(&tee->miscdev);
if (rc != 0) {
pr_err("misc_register() failed with ret = %d\n",
rc);
return rc;
}
dev_set_drvdata(tee->miscdev.this_device, tee);
rc = tee_init_procfs(tee);
if (rc) {
misc_deregister(&tee->miscdev);
return rc;
}
rc = tee_init_sysfs(tee);
if (rc) {
misc_deregister(&tee->miscdev);
return rc;
}
/* Register a static reference on the class misc
* to allow finding device by class
*/
WARN_ON(!tee->miscdev.this_device->class);
if (misc_class)
WARN_ON(misc_class != tee->miscdev.this_device->class);
else
misc_class = tee->miscdev.this_device->class;
pr_info(
"TKCore misc: Register the misc device \"%s\" (id=%d,minor=%d)\n",
dev_name(tee->miscdev.this_device), tee->id,
tee->miscdev.minor);
return rc;
}
EXPORT_SYMBOL(tee_core_add);
int tee_core_del(struct tee *tee)
{
if (tee) {
pr_info(
"TEE Core: Destroy the misc device \"%s\" (id=%d)\n",
dev_name(tee->miscdev.this_device), tee->id);
tee_cleanup_sysfs(tee);
if (tee->miscdev.minor != MISC_DYNAMIC_MINOR) {
pr_info(
"TEE Core: Deregister the misc device \"%s\" (id=%d)\n",
dev_name(tee->miscdev.this_device), tee->id);
misc_deregister(&tee->miscdev);
}
}
tee_core_free(tee);
return 0;
}
EXPORT_SYMBOL(tee_core_del);
static int __init tee_core_init(void)
{
int r;
pr_info("\nTEE Core Framework initialization (ver %s)\n",
_TEE_CORE_FW_VER);
r = tkcore_tee_pm_init();
if (r) {
pr_err("tkcore_tee_pm_init() failed with %d\n", r);
return r;
}
spin_lock_init(&tee_idr_lock);
idr_init(&tee_idr);
tee_fp_init();
tee_clkmgr_init();
tee_ta_mgmt_init();
return 0;
}
static void __exit tee_core_exit(void)
{
pr_info("TEE Core Framework unregistered\n");
tkcore_tee_pm_exit();
tee_clkmgr_exit();
tee_fp_exit();
}
#ifndef MODULE
rootfs_initcall(tee_core_init);
#else
module_init(tee_core_init);
#endif
module_exit(tee_core_exit);
MODULE_AUTHOR("TrustKernel");
MODULE_DESCRIPTION("TrustKernel TKCore TEEC v1.0");
MODULE_SUPPORTED_DEVICE("");
MODULE_VERSION(_TEE_CORE_FW_VER);
MODULE_LICENSE("GPL");
+59
View File
@@ -0,0 +1,59 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_CORE_PRIV_H__
#define __TEE_CORE_PRIV_H__
#include "linux/tee_core.h"
#include "linux/tee_ioc.h"
/* from tee_core_module.c */
int tee_get(struct tee *tee);
int tee_put(struct tee *tee);
void tee_inc_stats(struct tee_stats_entry *entry);
void tee_dec_stats(struct tee_stats_entry *entry);
/* from tee_context.c */
int tee_context_dump(struct tee *tee, char *buff, size_t len);
struct tee_context *tee_context_create(struct tee *tee);
void tee_context_destroy(struct tee_context *ctx);
void tee_context_get(struct tee_context *ctx);
void tee_context_put(struct tee_context *ctx);
struct tee_shm *tee_context_create_tmpref_buffer(struct tee_context *ctx,
size_t size,
const void *buffer, int type);
struct tee_shm *tee_context_alloc_shm_tmp(struct tee_context *ctx, size_t size,
const void *data, int type);
int tee_context_copy_from_client(const struct tee_context *ctx, void *dest,
const void *src, size_t size);
/* from tee_session.c */
int tee_session_create_fd(struct tee_context *ctx, struct tee_cmd_io *cmd_io);
struct tee_session *tee_session_create_and_open(struct tee_context *ctx,
struct tee_cmd_io *cmd_io);
int tee_session_close_and_destroy(struct tee_session *sess);
struct tee *tee_get_tee(const char *devname);
int tee_session_invoke_be(struct tee_session *sess, struct tee_cmd_io *cmd_io);
int tee_core_alloc_uuid(void *ptr);
void tee_core_free_uuid(int id);
void *tee_core_uuid2ptr(int id);
#ifdef CONFIG_COMPAT
int convert_compat_tee_cmd(struct tee_cmd_io __user *u_cmd);
int convert_compat_tee_shm_io(struct tee_shm_io __user *shm_io);
int convert_compat_tee_spta_inst(struct tee_spta_inst_desc __user *spta);
#endif
#endif
+219
View File
@@ -0,0 +1,219 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/tee_fp.h>
#include <tee_kernel_api.h>
#include "tee_fp_priv.h"
static struct TEEC_UUID SENSOR_DETECTOR_TA_UUID = { 0x966d3f7c, 0x04ef, 0x1beb,
{ 0x08, 0xb7, 0x57, 0xf3, 0x7a, 0x6d, 0x87, 0xf9 } };
#define CMD_READ_CHIPID 0x0
#define CMD_DISABLE 0x1
#define CMD_CONFIG_PADSEL 0x2
int tee_spi_cfg_padsel(uint32_t padsel)
{
struct TEEC_Context context;
struct TEEC_Session session;
struct TEEC_Operation op;
TEEC_Result r;
uint32_t returnOrigin;
pr_info("padsel=0x%x\n", padsel);
memset(&context, 0, sizeof(context));
memset(&session, 0, sizeof(session));
memset(&op, 0, sizeof(op));
r = TEEC_InitializeContext(NULL, &context);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_InitializeContext() failed with 0x%08x\n", r);
return r;
}
r = TEEC_OpenSession(
&context, &session, &SENSOR_DETECTOR_TA_UUID,
TEEC_LOGIN_PUBLIC,
NULL, NULL, &returnOrigin);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_OpenSession failed with 0x%x returnOrigun: %u\n",
r, returnOrigin);
TEEC_FinalizeContext(&context);
return r;
}
op.paramTypes = TEEC_PARAM_TYPES(
TEEC_VALUE_INPUT,
TEEC_NONE,
TEEC_NONE,
TEEC_NONE);
op.params[0].value.a = padsel;
r = TEEC_InvokeCommand(&session, CMD_CONFIG_PADSEL, &op, &returnOrigin);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_InvokeCommand() failed with 0x%08x returnOrigin: %u\n",
r, returnOrigin);
}
TEEC_CloseSession(&session);
TEEC_FinalizeContext(&context);
return r;
}
EXPORT_SYMBOL(tee_spi_cfg_padsel);
int tee_spi_transfer(void *conf, uint32_t conf_size,
void *inbuf, void *outbuf, uint32_t size)
{
struct TEEC_Context context;
struct TEEC_Session session;
struct TEEC_Operation op;
TEEC_Result r;
char *buf;
uint32_t returnOrigin;
pr_info("conf=%p conf_size=%u inbuf=%p outbuf=%p size=%u\n",
conf, conf_size, inbuf, outbuf, size);
if (!conf || !inbuf || !outbuf) {
pr_err("Bad parameters NULL buf\n");
return -EINVAL;
}
if (size == 0) {
pr_err("zero buf size\n");
return -EINVAL;
}
memset(&context, 0, sizeof(context));
memset(&session, 0, sizeof(session));
memset(&op, 0, sizeof(op));
memcpy(outbuf, inbuf, size);
r = TEEC_InitializeContext(NULL, &context);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_InitializeContext() failed with 0x%08x\n", r);
return r;
}
r = TEEC_OpenSession(
&context, &session, &SENSOR_DETECTOR_TA_UUID,
TEEC_LOGIN_PUBLIC,
NULL, NULL, &returnOrigin);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_OpenSession failed with 0x%x returnOrigun: %u\n",
r, returnOrigin);
TEEC_FinalizeContext(&context);
return r;
}
op.paramTypes = TEEC_PARAM_TYPES(
TEEC_MEMREF_TEMP_INPUT,
TEEC_MEMREF_TEMP_INOUT,
TEEC_NONE,
TEEC_NONE);
op.params[0].tmpref.buffer = conf;
op.params[0].tmpref.size = conf_size;
op.params[1].tmpref.buffer = outbuf;
op.params[1].tmpref.size = size;
buf = outbuf;
r = TEEC_InvokeCommand(&session, CMD_READ_CHIPID, &op, &returnOrigin);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_InvokeCommand() failed with 0x%08x returnOrigin: %u\n",
r, returnOrigin);
}
TEEC_CloseSession(&session);
TEEC_FinalizeContext(&context);
return r;
}
EXPORT_SYMBOL(tee_spi_transfer);
int tee_spi_transfer_disable(void)
{
struct TEEC_Context context;
struct TEEC_Session session;
struct TEEC_Operation op;
TEEC_Result r;
uint32_t returnOrigin;
memset(&context, 0, sizeof(context));
memset(&session, 0, sizeof(session));
memset(&op, 0, sizeof(op));
r = TEEC_InitializeContext(NULL, &context);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_InitializeContext() failed with 0x%08x\n", r);
return r;
}
r = TEEC_OpenSession(
&context, &session, &SENSOR_DETECTOR_TA_UUID,
TEEC_LOGIN_PUBLIC,
NULL, NULL, &returnOrigin);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_OpenSession failed with 0x%x returnOrigun: %u\n",
r, returnOrigin);
TEEC_FinalizeContext(&context);
return r;
}
op.paramTypes = TEEC_PARAM_TYPES(
TEEC_NONE,
TEEC_NONE,
TEEC_NONE,
TEEC_NONE);
r = TEEC_InvokeCommand(&session, CMD_DISABLE, &op, &returnOrigin);
if (r != TEEC_SUCCESS) {
pr_err(
"TEEC_InvokeCommand() failed with 0x%08x returnOrigin: %u\n",
r, returnOrigin);
}
TEEC_CloseSession(&session);
TEEC_FinalizeContext(&context);
return r;
}
EXPORT_SYMBOL(tee_spi_transfer_disable);
int tee_fp_init(void)
{
return 0;
}
void tee_fp_exit(void)
{
}
+12
View File
@@ -0,0 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_FP_PRIV_H
#define TEE_FP_PRIV_H
int tee_fp_init(void);
void tee_fp_exit(void);
#endif
+332
View File
@@ -0,0 +1,332 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/io.h>
#include <linux/vmalloc.h>
#include "linux/tee_kernel_api.h"
#include "linux/tee_core.h"
#include "linux/tee_ioc.h"
#include "tee_core_priv.h"
#include "tee_shm.h"
#include "tee_supp_com.h"
#define TEE_TZ_DEVICE_NAME "tkcoredrv"
static void reset_tee_cmd(struct tee_cmd_io *cmd)
{
memset(cmd, 0, sizeof(struct tee_cmd_io));
cmd->fd_sess = -1;
cmd->cmd = 0;
cmd->uuid = NULL;
cmd->origin = TEEC_ORIGIN_API;
cmd->err = TEEC_SUCCESS;
cmd->data = NULL;
cmd->data_size = 0;
cmd->op = NULL;
}
TEEC_Result TEEC_InitializeContext(const char *name,
struct TEEC_Context *context)
{
struct tee *tee;
struct tee_context *ctx;
if (!context)
return TEEC_ERROR_BAD_PARAMETERS;
context->fd = 0;
if (name == NULL)
strncpy(context->devname, TEE_TZ_DEVICE_NAME,
sizeof(context->devname));
else
strncpy(context->devname, name, sizeof(context->devname));
tee = tee_get_tee(context->devname);
if (!tee) {
pr_err("can't get device [%s]\n", name);
return TEEC_ERROR_BAD_PARAMETERS;
}
ctx = tee_context_create(tee);
if (IS_ERR_OR_NULL(ctx)) {
pr_err("ctx is NULL\n");
return TEEC_ERROR_BAD_PARAMETERS;
}
ctx->usr_client = 0;
context->fd = tee_core_alloc_uuid(ctx);
if (context->fd < 0) {
if (context->fd == -ENOSPC || context->fd == -ENOMEM)
return TEEC_ERROR_OUT_OF_MEMORY;
if (context->fd == -EINVAL) {
pr_err("context->fd is invalid\n");
return TEEC_ERROR_BAD_PARAMETERS;
}
return TEEC_ERROR_GENERIC;
}
return TEEC_SUCCESS;
}
EXPORT_SYMBOL(TEEC_InitializeContext);
void TEEC_FinalizeContext(struct TEEC_Context *context)
{
struct tee_context *ctx;
if (!context || !context->fd) {
pr_err("can't release context %p:[%s]\n",
context, strnlen(context->devname, 256) < 256 ?
context->devname : "[invalid]");
return;
}
ctx = tee_core_uuid2ptr(context->fd);
if (ctx == NULL) {
pr_err("bad context->fd %d provided\n",
context->fd);
return;
}
tee_core_free_uuid(context->fd);
tee_context_destroy(ctx);
}
EXPORT_SYMBOL(TEEC_FinalizeContext);
TEEC_Result TEEC_OpenSession(struct TEEC_Context *context,
struct TEEC_Session *session,
const struct TEEC_UUID *destination,
uint32_t connectionMethod,
const void *connectionData,
struct TEEC_Operation *operation,
uint32_t *return_origin)
{
struct TEEC_Operation dummy_op;
struct tee_cmd_io cmd;
struct tee_session *sess;
struct tee_context *ctx;
uint32_t error_origin;
if (!operation) {
/*
* The code here exist because Global Platform API states that
* it is allowed to give operation as a NULL pointer.
* In kernel and secure world we in most cases don't want
* this to be NULL, hence we use this dummy operation when
* a client doesn't provide any operation.
*/
memset(&dummy_op, 0, sizeof(struct TEEC_Operation));
operation = &dummy_op;
}
if (!context || !session || !destination || !operation)
return TEEC_ERROR_BAD_PARAMETERS;
session->fd = 0;
ctx = tee_core_uuid2ptr(context->fd);
if (ctx == NULL) {
pr_err("bad context_fd %d provided\n",
context->fd);
if (return_origin)
*return_origin = TEEC_ORIGIN_COMMS;
return TEEC_ERROR_BAD_PARAMETERS;
}
reset_tee_cmd(&cmd);
cmd.op = operation;
cmd.uuid = (struct TEEC_UUID *) destination;
sess = tee_session_create_and_open(ctx, &cmd);
if (IS_ERR_OR_NULL(sess)) {
if (cmd.origin)
error_origin = cmd.origin;
else
error_origin = TEEC_ORIGIN_COMMS;
if (return_origin)
*return_origin = error_origin;
if (cmd.err)
return cmd.err;
else
return TEEC_ERROR_COMMUNICATION;
} else {
error_origin = cmd.origin;
session->fd = tee_core_alloc_uuid(sess);
if (session->fd < 0) {
/* since we failed to alloc uuid, we need
* to close this session
*/
tee_session_close_and_destroy(sess);
error_origin = TEEC_ORIGIN_COMMS;
if (return_origin)
*return_origin = error_origin;
if (session->fd == -ENOSPC || session->fd == -ENOMEM)
return TEEC_ERROR_OUT_OF_MEMORY;
if (session->fd == -EINVAL)
return TEEC_ERROR_BAD_PARAMETERS;
return TEEC_ERROR_GENERIC;
}
if (return_origin)
*return_origin = error_origin;
return cmd.err;
}
}
EXPORT_SYMBOL(TEEC_OpenSession);
void TEEC_CloseSession(struct TEEC_Session *session)
{
if (session && session->fd) {
struct tee_session *sess = tee_core_uuid2ptr(session->fd);
if (sess == NULL) {
pr_err("bad session_id %d provided\n",
session->fd);
return;
}
tee_core_free_uuid(session->fd);
tee_session_close_and_destroy(sess);
}
}
EXPORT_SYMBOL(TEEC_CloseSession);
TEEC_Result TEEC_InvokeCommand(struct TEEC_Session *session,
uint32_t commandID,
struct TEEC_Operation *operation,
uint32_t *return_origin)
{
int ret = 0;
struct tee_cmd_io cmd;
struct tee_session *sess;
uint32_t error_origin;
struct TEEC_Operation dummy_op;
if (!session || !session->fd)
return TEEC_ERROR_BAD_PARAMETERS;
if (!operation) {
memset(&dummy_op, 0, sizeof(struct TEEC_Operation));
operation = &dummy_op;
}
sess = tee_core_uuid2ptr(session->fd);
if (sess == NULL) {
pr_err("bad session_fd %d provided\n",
session->fd);
if (return_origin)
*return_origin = TEEC_ORIGIN_COMMS;
return TEEC_ERROR_BAD_PARAMETERS;
}
reset_tee_cmd(&cmd);
cmd.cmd = commandID;
cmd.op = operation;
ret = tee_session_invoke_be(sess, &cmd);
if (ret) {
if (cmd.origin)
error_origin = cmd.origin;
else
error_origin = TEEC_ORIGIN_COMMS;
if (return_origin)
*return_origin = error_origin;
if (cmd.err)
return cmd.err;
else
return TEEC_ERROR_COMMUNICATION;
} else {
if (return_origin)
*return_origin = cmd.origin;
return cmd.err;
}
}
EXPORT_SYMBOL(TEEC_InvokeCommand);
TEEC_Result TEEC_RegisterSharedMemory(struct TEEC_Context *context,
struct TEEC_SharedMemory *sharedMem)
{
if (!sharedMem)
return TEEC_ERROR_BAD_PARAMETERS;
sharedMem->registered = 1;
sharedMem->d.fd = 0;
return TEEC_SUCCESS;
}
EXPORT_SYMBOL(TEEC_RegisterSharedMemory);
TEEC_Result TEEC_AllocateSharedMemory(struct TEEC_Context *context,
struct TEEC_SharedMemory *shared_memory)
{
struct tee_shm_io shm_io;
int ret;
struct tee_shm *shm;
struct tee_context *ctx;
if (!context || !context->fd || !shared_memory)
return TEEC_ERROR_BAD_PARAMETERS;
shm_io.size = shared_memory->size;
shm_io.flags = shared_memory->flags | TEEC_MEM_KAPI;
ctx = tee_core_uuid2ptr(context->fd);
if (ctx == NULL) {
pr_err("Invalid fd %d for tee context\n",
context->fd);
return TEEC_ERROR_BAD_PARAMETERS;
}
ret = tee_shm_alloc_io(ctx, &shm_io);
if (ret) {
pr_err("tee_shm_alloc_io(%zd) failed\n",
shared_memory->size);
return TEEC_ERROR_OUT_OF_MEMORY;
}
shared_memory->registered = 0;
shared_memory->flags = shm_io.flags;
shared_memory->d.fd = shm_io.fd_shm;
shm = (struct tee_shm *)(long)shm_io.fd_shm;
shared_memory->buffer = shm->resv.kaddr;
return TEEC_SUCCESS;
}
EXPORT_SYMBOL(TEEC_AllocateSharedMemory);
void TEEC_ReleaseSharedMemory(struct TEEC_SharedMemory *shared_memory)
{
struct tee_shm *shm;
if (!shared_memory || shared_memory->registered)
return;
shm = (struct tee_shm *)(long)shared_memory->d.fd;
tee_shm_free_io(shm);
shared_memory->buffer = NULL;
shared_memory->d.fd = 0;
}
EXPORT_SYMBOL(TEEC_ReleaseSharedMemory);
File diff suppressed because it is too large. Load diff
+22
View File
@@ -0,0 +1,22 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_PROCFS_H__
#define __TEE_PROCFS_H__
#define TEE_LOG_IRQ 280
#define TEE_LOG_CTL_BUF_SIZE 256
#define TEE_LOG_SIGNAL_THRESHOLD_SIZE 1024
#define TEE_CRASH_MAGIC_NO 0xdeadbeef
struct tee;
int tee_init_procfs(struct tee *tee);
void tee_exit_procfs(void);
#endif
+949
View File
@@ -0,0 +1,949 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/file.h>
#include <linux/atomic.h>
#include <linux/uaccess.h>
#include <linux/anon_inodes.h>
#include <linux/fs.h>
#include "tee_shm.h"
#include "tee_core_priv.h"
#include "tee_tui_hal.h"
static int _init_tee_cmd(struct tee_session *sess, struct tee_cmd_io *cmd_io,
struct tee_cmd *cmd);
static void _update_client_tee_cmd(struct tee_session *sess,
struct tee_cmd_io *cmd_io,
struct tee_cmd *cmd);
static void _release_tee_cmd(struct tee_session *sess, struct tee_cmd *cmd);
/******************************************************************************/
static inline bool flag_set(int val, int flags)
{
return (val & flags) == flags;
}
static inline bool is_mapped_temp(int flags)
{
return flag_set(flags, TEE_SHM_MAPPED | TEE_SHM_TEMP);
}
/******************************************************************************/
static int tee_copy_from_user(struct tee_context *ctx, void *to, void *from,
size_t size)
{
if ((!to) || (!from) || (!size))
return 0;
if (ctx->usr_client)
return copy_from_user(to, from, size);
memcpy(to, from, size);
return 0;
}
static int tee_copy_to_user(struct tee_context *ctx, void *to, void *from,
size_t size)
{
if ((!to) || (!from) || (!size))
return 0;
if (ctx->usr_client)
return copy_to_user(to, from, size);
memcpy(to, from, size);
return 0;
}
/* Defined as macro to let the put_user macro see the types */
#define tee_put_user(ctx, from, to) \
do { \
if ((ctx)->usr_client) \
put_user(from, to); \
else \
*to = from; \
} while (0)
static inline int tee_session_is_opened(struct tee_session *sess)
{
if (sess && sess->sessid)
return (sess->sessid != 0);
return 0;
}
static int tee_session_open_be(struct tee_session *sess,
struct tee_cmd_io *cmd_io)
{
int ret = -EINVAL;
struct tee *tee;
struct tee_cmd cmd;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
tee = sess->ctx->tee;
sess->sessid = 0;
ret = _init_tee_cmd(sess, cmd_io, &cmd);
if (ret) {
pr_err(
"init tee command failed with 0x%08x\n",
ret);
goto out;
}
ret = tee->ops->open(sess, &cmd);
if (ret == 0)
_update_client_tee_cmd(sess, cmd_io, &cmd);
else {
/* propagate the reason of the error */
cmd_io->origin = cmd.origin;
cmd_io->err = cmd.err;
}
out:
_release_tee_cmd(sess, &cmd);
return ret;
}
int tee_session_invoke_be(struct tee_session *sess, struct tee_cmd_io *cmd_io)
{
int ret = -EINVAL;
struct tee *tee;
struct tee_cmd cmd;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
tee = sess->ctx->tee;
ret = _init_tee_cmd(sess, cmd_io, &cmd);
if (ret)
goto out;
ret = tee->ops->invoke(sess, &cmd);
if (!ret)
_update_client_tee_cmd(sess, cmd_io, &cmd);
else {
/* propagate the reason of the error */
cmd_io->origin = cmd.origin;
cmd_io->err = cmd.err;
}
out:
_release_tee_cmd(sess, &cmd);
return ret;
}
static int tee_session_close_be(struct tee_session *sess)
{
int ret = -EINVAL;
struct tee *tee;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
tee = sess->ctx->tee;
ret = tee->ops->close(sess);
sess->sessid = 0;
return ret;
}
static int tee_session_cancel_be(struct tee_session *sess,
struct tee_cmd_io *cmd_io)
{
int ret = -EINVAL;
struct tee *tee;
struct tee_cmd cmd;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
tee = sess->ctx->tee;
ret = _init_tee_cmd(sess, cmd_io, &cmd);
if (ret)
goto out;
ret = tee->ops->cancel(sess, &cmd);
out:
_release_tee_cmd(sess, &cmd);
return ret;
}
static int tee_do_invoke_command(struct tee_session *sess,
struct tee_cmd_io __user *u_cmd)
{
int ret = -EINVAL;
struct tee *tee;
struct tee_cmd_io k_cmd;
struct tee_context *ctx;
WARN_ON(!sess->ctx);
WARN_ON(!sess->ctx->tee);
ctx = sess->ctx;
tee = sess->ctx->tee;
WARN_ON(!sess->sessid);
if (tee_copy_from_user
(ctx, &k_cmd, (void *)u_cmd, sizeof(struct tee_cmd_io))) {
pr_err("tee_copy_from_user failed\n");
goto exit;
}
if ((k_cmd.op == NULL) || (k_cmd.uuid != NULL) ||
(k_cmd.data != NULL) || (k_cmd.data_size != 0)) {
pr_err(
"op or/and data parameters are not valid\n");
goto exit;
}
ret = tee_session_invoke_be(sess, &k_cmd);
if (ret)
pr_err("tee_invoke_command failed with %d\n", ret);
tee_put_user(ctx, k_cmd.err, &u_cmd->err);
tee_put_user(ctx, k_cmd.origin, &u_cmd->origin);
exit:
return ret;
}
static int tee_do_cancel_cmd(struct tee_session *sess,
struct tee_cmd_io __user *u_cmd)
{
int ret = -EINVAL;
struct tee *tee;
struct tee_cmd_io k_cmd;
struct tee_context *ctx;
WARN_ON(!sess->ctx);
WARN_ON(!sess->ctx->tee);
ctx = sess->ctx;
tee = sess->ctx->tee;
WARN_ON(!sess->sessid);
if (tee_copy_from_user
(ctx, &k_cmd, (void *)u_cmd, sizeof(struct tee_cmd_io))) {
pr_err("cancel_cmd: tee_copy_from_user failed\n");
goto exit;
}
if ((k_cmd.op == NULL) || (k_cmd.uuid != NULL) ||
(k_cmd.data != NULL) || (k_cmd.data_size != 0)) {
pr_err("op or/and data parameters are not valid\n");
goto exit;
}
ret = tee_session_cancel_be(sess, &k_cmd);
if (ret)
pr_err("tee_invoke_command failed\n");
tee_put_user(ctx, k_cmd.err, &u_cmd->err);
tee_put_user(ctx, k_cmd.origin, &u_cmd->origin);
exit:
return ret;
}
static int tee_do_kernel_cancel_cmd(struct tee_session *sess,
struct tee_cmd_io *k_cmd)
{
int ret = -EINVAL;
struct tee *tee;
struct tee_context *ctx;
struct tee_cmd cmd;
WARN_ON(!sess->ctx);
WARN_ON(!sess->ctx->tee);
ctx = sess->ctx;
tee = sess->ctx->tee;
WARN_ON(!sess->sessid);
if ((k_cmd == NULL || k_cmd->op == NULL) || (k_cmd->uuid != NULL) ||
(k_cmd->data != NULL) || (k_cmd->data_size != 0)) {
pr_err(
"op or/and data parameters are not valid\n");
goto exit;
}
cmd.cmd = k_cmd->cmd;
cmd.origin = TEEC_ORIGIN_TEE;
cmd.err = TEEC_ERROR_BAD_PARAMETERS;
cmd.param.type_original = 0;
ret = tee->ops->cancel(sess, &cmd);
if (ret)
pr_err("tee_cancel failed\n");
exit:
return ret;
}
DECLARE_COMPLETION(io_comp);
static int flag_tui_obj;
struct tui_obj {
struct tee_cmd_io op;
struct tee_session *sess;
uint32_t cmd_id;
uint32_t status;
struct mutex lock;
} g_tui_obj;
static uint32_t send_cmd_to_user(uint32_t command_id)
{
uint32_t ret = 0;
g_tui_obj.cmd_id = command_id;
complete(&io_comp);
return ret;
}
bool teec_notify_event(uint32_t event_type)
{
bool ret = false;
/* Cancel the TUI session if exists */
if (g_tui_obj.status)
ret = tee_do_kernel_cancel_cmd(g_tui_obj.sess, &g_tui_obj.op);
return ret;
}
EXPORT_SYMBOL(teec_notify_event);
int teec_wait_cmd(uint32_t *cmd_id)
{
/* Wait for signal from DCI handler */
wait_for_completion(&io_comp);
#ifdef INIT_COMPLETION
INIT_COMPLETION(io_comp);
#else
io_comp.done = 0;
#endif
*cmd_id = g_tui_obj.cmd_id;
return 0;
}
EXPORT_SYMBOL(teec_wait_cmd);
static long tee_session_internal_ioctl(struct tee_session *sess,
unsigned int cmd, unsigned long arg)
{
int ret;
struct tee *tee;
tee = sess->ctx->tee;
switch (cmd) {
case TEE_INVOKE_COMMAND_IOC:
ret = tee_do_invoke_command(sess,
(struct tee_cmd_io __user *) arg);
break;
case TEE_REQUEST_CANCELLATION_IOC:
ret = tee_do_cancel_cmd(sess,
(struct tee_cmd_io __user *) arg);
break;
case TEE_TUI_OPEN_SESSION_IOC:
pr_debug("TEE_TUI_OPEN_SESSION_IOC.\n");
if (flag_tui_obj == 0) {
mutex_init(&g_tui_obj.lock);
flag_tui_obj = 1;
}
mutex_lock(&g_tui_obj.lock);
if (g_tui_obj.status != 0 ||
(struct tee_cmd_io __user *)arg == NULL) {
ret = -EBUSY;
mutex_unlock(&g_tui_obj.lock);
pr_warn(
"TEE_TUI_OPEN_SESSION_IOC: tui busy or invalid argument\n");
break;
}
if (tee_copy_from_user(sess->ctx, &g_tui_obj.op, (void *)arg,
sizeof(struct tee_cmd_io))) {
ret = -EINVAL;
mutex_unlock(&g_tui_obj.lock);
break;
}
/* reset part of op */
g_tui_obj.op.uuid = NULL;
g_tui_obj.op.data = NULL;
g_tui_obj.op.data_size = 0;
g_tui_obj.sess = sess;
g_tui_obj.status = 1;
mutex_unlock(&g_tui_obj.lock);
/* Start android TUI activity */
ret = send_cmd_to_user(TEEC_TUI_CMD_START_ACTIVITY);
if (ret != 0) {
mutex_lock(&g_tui_obj.lock);
g_tui_obj.status = 0;
mutex_unlock(&g_tui_obj.lock);
break;
}
/* Deactivate linux UI drivers */
ret = hal_tui_deactivate();
if (ret != 0) {
mutex_lock(&g_tui_obj.lock);
g_tui_obj.status = 0;
mutex_unlock(&g_tui_obj.lock);
send_cmd_to_user(TEEC_TUI_CMD_STOP_ACTIVITY);
break;
}
break;
case TEE_TUI_CLOSE_SESSION_IOC:
pr_debug(
"TEE_TUI_CLOSE_SESSION_IOC.\n");
if (flag_tui_obj == 0) {
mutex_init(&g_tui_obj.lock);
flag_tui_obj = 1;
}
mutex_lock(&g_tui_obj.lock);
g_tui_obj.status = 0;
mutex_unlock(&g_tui_obj.lock);
/* Activate linux UI drivers */
ret = hal_tui_activate();
/* Stop android TUI activity */
ret = send_cmd_to_user(TEEC_TUI_CMD_STOP_ACTIVITY);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static long tee_session_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
struct tee_session *sess = filp->private_data;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
return tee_session_internal_ioctl(sess, cmd, arg);
}
#ifdef CONFIG_COMPAT
static long tee_session_compat_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
struct tee_session *sess = filp->private_data;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
switch (cmd) {
case TEE_INVOKE_COMMAND_IOC:
case TEE_REQUEST_CANCELLATION_IOC:
case TEE_TUI_OPEN_SESSION_IOC:
if (convert_compat_tee_cmd((struct tee_cmd_io __user *) arg))
return -EFAULT;
break;
default:
break;
}
return tee_session_internal_ioctl(sess, cmd, arg);
}
#endif
static int tee_session_release(struct inode *inode, struct file *filp)
{
struct tee_session *sess = filp->private_data;
int ret = 0;
struct tee *tee;
WARN_ON(!sess || !sess->ctx || !sess->ctx->tee);
tee = sess->ctx->tee;
ret = tee_session_close_and_destroy(sess);
return ret;
}
const struct file_operations tee_session_fops = {
.owner = THIS_MODULE,
#ifdef CONFIG_COMPAT
.compat_ioctl = tee_session_compat_ioctl,
#endif
.unlocked_ioctl = tee_session_ioctl,
.release = tee_session_release,
};
int tee_session_close_and_destroy(struct tee_session *sess)
{
int ret;
struct tee *tee;
struct tee_context *ctx;
if (!sess || !sess->ctx || !sess->ctx->tee)
return -EINVAL;
ctx = sess->ctx;
tee = ctx->tee;
if (!tee_session_is_opened(sess))
return -EINVAL;
ret = tee_session_close_be(sess);
mutex_lock(&tee->lock);
tee_dec_stats(&tee->stats[TEE_STATS_SESSION_IDX]);
list_del(&sess->entry);
devm_kfree(_DEV(tee), sess);
tee_context_put(ctx);
tee_put(tee);
mutex_unlock(&tee->lock);
return ret;
}
struct tee_session *tee_session_create_and_open(struct tee_context *ctx,
struct tee_cmd_io *cmd_io)
{
int ret = 0;
struct tee_session *sess;
struct tee *tee;
WARN_ON(!ctx->tee);
tee = ctx->tee;
ret = tee_get(tee);
if (ret)
return ERR_PTR(-EBUSY);
sess = devm_kzalloc(_DEV(tee), sizeof(struct tee_session), GFP_KERNEL);
if (!sess) {
tee_put(tee);
return ERR_PTR(-ENOMEM);
}
tee_context_get(ctx);
sess->ctx = ctx;
ret = tee_session_open_be(sess, cmd_io);
mutex_lock(&tee->lock);
if (ret || !sess->sessid || cmd_io->err) {
pr_err(
"ERROR ret=%d (err=0x%08x, org=%d, sessid=0x%08x)\n",
ret, cmd_io->err,
cmd_io->origin, sess->sessid);
tee_put(tee);
tee_context_put(ctx);
devm_kfree(_DEV(tee), sess);
mutex_unlock(&tee->lock);
if (ret)
return ERR_PTR(ret);
else
return NULL;
}
tee_inc_stats(&tee->stats[TEE_STATS_SESSION_IDX]);
list_add_tail(&sess->entry, &ctx->list_sess);
mutex_unlock(&tee->lock);
return sess;
}
int tee_session_create_fd(struct tee_context *ctx, struct tee_cmd_io *cmd_io)
{
int ret;
struct tee_session *sess;
struct tee *tee = ctx->tee;
(void) tee;
WARN_ON(cmd_io->fd_sess > 0);
sess = tee_session_create_and_open(ctx, cmd_io);
if (IS_ERR_OR_NULL(sess)) {
ret = PTR_ERR(sess);
pr_warn(
"ERROR can't create the session (ret=%d, err=0x%08x, org=%d)\n",
ret, cmd_io->err, cmd_io->origin);
cmd_io->fd_sess = -1;
goto out;
}
/* Retrieve a fd */
cmd_io->fd_sess = -1;
ret = anon_inode_getfd("tee_session",
&tee_session_fops, sess, O_CLOEXEC);
if (ret < 0) {
pr_err("ERROR can't get a fd (ret=%d)\n",
ret);
tee_session_close_and_destroy(sess);
goto out;
}
cmd_io->fd_sess = ret;
ret = 0;
out:
return ret;
}
static bool tee_session_is_supported_type(struct tee_session *sess, int type)
{
switch (type) {
case TEEC_NONE:
case TEEC_VALUE_INPUT:
case TEEC_VALUE_OUTPUT:
case TEEC_VALUE_INOUT:
case TEEC_MEMREF_TEMP_INPUT:
case TEEC_MEMREF_TEMP_OUTPUT:
case TEEC_MEMREF_TEMP_INOUT:
case TEEC_MEMREF_WHOLE:
case TEEC_MEMREF_PARTIAL_INPUT:
case TEEC_MEMREF_PARTIAL_OUTPUT:
case TEEC_MEMREF_PARTIAL_INOUT:
return true;
default:
pr_err("type is invalid (type %02x)\n", type);
return false;
}
}
static int to_memref_type(int flags)
{
if (flag_set(flags, TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))
return TEEC_MEMREF_TEMP_INOUT;
if (flag_set(flags, TEEC_MEM_INPUT))
return TEEC_MEMREF_TEMP_INPUT;
if (flag_set(flags, TEEC_MEM_OUTPUT))
return TEEC_MEMREF_TEMP_OUTPUT;
pr_warn("tkcoredrv: %s: bad flags=%x\n", __func__, flags);
return 0;
}
static int _init_tee_cmd(struct tee_session *sess, struct tee_cmd_io *cmd_io,
struct tee_cmd *cmd)
{
int ret = -EINVAL;
int idx;
struct TEEC_Operation op;
struct tee_data *param = &cmd->param;
struct tee *tee;
struct tee_context *ctx;
WARN_ON(!sess->ctx);
WARN_ON(!sess->ctx->tee);
ctx = sess->ctx;
tee = sess->ctx->tee;
memset(cmd, 0, sizeof(struct tee_cmd));
cmd->cmd = cmd_io->cmd;
cmd->origin = TEEC_ORIGIN_TEE;
cmd->err = TEEC_ERROR_BAD_PARAMETERS;
cmd_io->origin = cmd->origin;
cmd_io->err = cmd->err;
if (tee_context_copy_from_client(ctx, &op, cmd_io->op, sizeof(op)))
goto out;
cmd->param.type_original = op.paramTypes;
for (idx = 0; idx < TEEC_CONFIG_PAYLOAD_REF_COUNT; ++idx) {
uint32_t offset = 0;
uint32_t size = 0;
int type = TEEC_PARAM_TYPE_GET(op.paramTypes, idx);
switch (type) {
case TEEC_NONE:
break;
case TEEC_VALUE_INPUT:
case TEEC_VALUE_OUTPUT:
case TEEC_VALUE_INOUT:
param->params[idx].value = op.params[idx].value;
break;
case TEEC_MEMREF_TEMP_INPUT:
case TEEC_MEMREF_TEMP_OUTPUT:
case TEEC_MEMREF_TEMP_INOUT:
param->params[idx].shm =
tee_context_create_tmpref_buffer(
ctx, op.params[idx].tmpref.size,
op.params[idx].tmpref.buffer,
type);
if (IS_ERR_OR_NULL(param->params[idx].shm))
goto out;
break;
case TEEC_MEMREF_PARTIAL_INPUT:
case TEEC_MEMREF_PARTIAL_OUTPUT:
case TEEC_MEMREF_PARTIAL_INOUT:
case TEEC_MEMREF_WHOLE:
if (tee_copy_from_user(ctx, &param->c_shm[idx],
op.params[idx].memref.parent,
sizeof(param->c_shm[idx])))
goto out;
if (type == TEEC_MEMREF_WHOLE) {
offset = 0;
size = param->c_shm[idx].size;
} else { /* for PARTIAL, check the size */
offset = op.params[idx].memref.offset;
size = op.params[idx].memref.size;
if (param->c_shm[idx].size < size + offset) {
pr_err(
"A PARTIAL parameter is bigger than the parent %zd < %d + %d\n",
param->c_shm[idx].size, size,
offset);
goto out;
}
}
type = to_memref_type(param->c_shm[idx].flags);
if (type == 0)
goto out;
param->params[idx].shm = tkcore_shm_get(ctx,
&param->c_shm[idx], size, offset);
if (IS_ERR_OR_NULL(param->params[idx].shm)) {
param->params[idx].shm =
tee_context_create_tmpref_buffer(
ctx, size,
param->c_shm[idx].buffer
+ offset, type);
if (IS_ERR_OR_NULL(param->params[idx].shm))
goto out;
}
break;
default:
WARN_ON(1);
}
param->type |= (type << (idx * 4));
}
if (cmd_io->uuid != NULL) {
cmd->uuid = tee_context_alloc_shm_tmp(sess->ctx,
sizeof(*cmd_io->uuid), cmd_io->uuid, TEEC_MEM_INPUT);
if (IS_ERR_OR_NULL(cmd->uuid)) {
ret = -EINVAL;
goto out;
}
}
ret = 0;
out:
if (ret)
_release_tee_cmd(sess, cmd);
return ret;
}
static void _update_client_tee_cmd(struct tee_session *sess,
struct tee_cmd_io *cmd_io,
struct tee_cmd *cmd)
{
int idx;
struct tee_context *ctx;
struct TEEC_Operation tmp_op;
WARN_ON(!cmd_io);
WARN_ON(!cmd_io->op);
WARN_ON(!cmd);
WARN_ON(!sess->ctx);
ctx = sess->ctx;
cmd_io->origin = cmd->origin;
cmd_io->err = cmd->err;
if (tee_context_copy_from_client(ctx,
&tmp_op, cmd_io->op, sizeof(tmp_op))) {
pr_err("Failed to copy op from client\n");
return;
}
if (cmd->param.type_original == TEEC_PARAM_TYPES(TEEC_NONE,
TEEC_NONE, TEEC_NONE, TEEC_NONE))
return;
for (idx = 0; idx < TEEC_CONFIG_PAYLOAD_REF_COUNT; ++idx) {
int type = TEEC_PARAM_TYPE_GET(cmd->param.type_original, idx);
int offset = 0;
size_t size;
size_t size_new;
struct TEEC_SharedMemory *parent;
WARN_ON(!tee_session_is_supported_type(sess, type));
switch (type) {
case TEEC_NONE:
case TEEC_VALUE_INPUT:
case TEEC_MEMREF_TEMP_INPUT:
case TEEC_MEMREF_PARTIAL_INPUT:
break;
case TEEC_VALUE_OUTPUT:
case TEEC_VALUE_INOUT:
if (tee_copy_to_user
(ctx, &cmd_io->op->params[idx].value,
&cmd->param.params[idx].value,
sizeof(tmp_op.params[idx].value))) {
pr_err(
"can't update %d result to user\n",
idx);
}
break;
case TEEC_MEMREF_TEMP_OUTPUT:
case TEEC_MEMREF_TEMP_INOUT:
/* Returned updated size */
size_new = cmd->param.params[idx].shm->size_req;
if (size_new > tmp_op.params[idx].tmpref.size) {
pr_warn(
" *** Wrong returned size from %d:%zd > %zd\n",
idx, size_new,
tmp_op.params[idx].tmpref.size);
size_new = tmp_op.params[idx].tmpref.size;
}
if (size_new != tmp_op.params[idx].tmpref.size) {
tee_put_user(ctx, size_new,
&cmd_io->op->params[idx].tmpref.size);
}
/* ensure we do not exceed the shared buffer length */
if (tee_copy_to_user(
ctx,
tmp_op.params[idx].tmpref.buffer,
cmd->param.params[idx].shm->resv.kaddr,
size_new)) {
pr_err(
"can't update %d result to user\n",
idx);
}
break;
case TEEC_MEMREF_PARTIAL_OUTPUT:
case TEEC_MEMREF_PARTIAL_INOUT:
case TEEC_MEMREF_WHOLE:
parent = &cmd->param.c_shm[idx];
if (type == TEEC_MEMREF_WHOLE) {
offset = 0;
size = parent->size;
} else {
offset = tmp_op.params[idx].memref.offset;
size = tmp_op.params[idx].memref.size;
}
/* Returned updated size */
size_new = cmd->param.params[idx].shm->size_req;
tee_put_user(ctx, size_new,
&cmd_io->op->params[idx].memref.size);
/*
* If we allocated a tmpref buffer,
* copy back data to the user buffer
*/
if (is_mapped_temp(cmd->param.params[idx].shm->flags)) {
if (parent->buffer &&
offset + size_new <= parent->size) {
if (tee_copy_to_user(ctx,
parent->buffer + offset,
cmd->param.params[idx].shm
->resv.kaddr,
size_new)) {
pr_err(
"can't update %d data to user\n",
idx);
}
}
}
break;
default:
WARN_ON(1);
}
}
}
static void _release_tee_cmd(struct tee_session *sess, struct tee_cmd *cmd)
{
int idx;
struct tee_context *ctx;
WARN_ON(!cmd);
WARN_ON(!sess);
WARN_ON(!sess->ctx);
WARN_ON(!sess->ctx->tee);
ctx = sess->ctx;
tkcore_shm_free(cmd->uuid);
if (cmd->param.type_original == TEEC_PARAM_TYPES(TEEC_NONE,
TEEC_NONE, TEEC_NONE, TEEC_NONE))
goto out;
for (idx = 0; idx < TEEC_CONFIG_PAYLOAD_REF_COUNT; ++idx) {
int type = TEEC_PARAM_TYPE_GET(cmd->param.type_original, idx);
struct tee_shm *shm;
switch (type) {
case TEEC_NONE:
case TEEC_VALUE_INPUT:
case TEEC_VALUE_OUTPUT:
case TEEC_VALUE_INOUT:
break;
case TEEC_MEMREF_TEMP_INPUT:
case TEEC_MEMREF_TEMP_OUTPUT:
case TEEC_MEMREF_TEMP_INOUT:
case TEEC_MEMREF_WHOLE:
case TEEC_MEMREF_PARTIAL_INPUT:
case TEEC_MEMREF_PARTIAL_OUTPUT:
case TEEC_MEMREF_PARTIAL_INOUT:
if (IS_ERR_OR_NULL(cmd->param.params[idx].shm))
break;
shm = cmd->param.params[idx].shm;
if (is_mapped_temp(shm->flags))
tkcore_shm_free(shm);
else
tkcore_shm_put(ctx, shm);
break;
default:
WARN_ON(1);
}
}
out:
memset(cmd, 0, sizeof(struct tee_cmd));
}
+921
View File
@@ -0,0 +1,921 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/dma-buf.h>
#include <linux/hugetlb.h>
#include <linux/version.h>
#include <linux/anon_inodes.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include "tee_core_priv.h"
#include "tee_shm.h"
struct tee_shm_attach {
struct sg_table sgt;
enum dma_data_direction dir;
bool is_mapped;
};
static struct tee_shm *tee_shm_alloc_static(struct tee *tee, size_t size,
uint32_t flags)
{
struct tee_shm *shm;
unsigned long pfn;
unsigned int nr_pages;
struct page *page;
int ret;
shm = tee->ops->alloc(tee, size, flags);
if (IS_ERR_OR_NULL(shm)) {
pr_err("allocation failed (s=%d,flags=0x%08x) err=%ld\n",
(int) size, flags, PTR_ERR(shm));
goto exit;
}
pfn = shm->resv.paddr >> PAGE_SHIFT;
page = pfn_to_page(pfn);
if (IS_ERR_OR_NULL(page)) {
pr_err("pfn_to_page(%lx) failed\n", pfn);
tee->ops->free(shm);
return (struct tee_shm *) page;
}
/* Only one page of contiguous physical memory */
nr_pages = 1;
ret = sg_alloc_table_from_pages(&shm->resv.sgt, &page,
nr_pages, 0,
nr_pages * PAGE_SIZE, GFP_KERNEL);
if (ret) {
pr_err("sg_alloc_table_from_pages() failed\n");
tee->ops->free(shm);
shm = ERR_PTR(ret);
}
exit:
return shm;
}
static struct tee_shm *tee_shm_alloc_ns(struct tee *tee, size_t size,
uint32_t flags)
{
size_t i, nr_pages;
struct page **pages;
struct tee_shm *shm;
if (size == 0) {
pr_warn("invalid size %zu flags 0x%x\n",
size, flags);
return NULL;
}
shm = kzalloc(sizeof(struct tee_shm), GFP_KERNEL);
if (shm == NULL) {
shm = NULL;
pr_err("bad kmalloc tee_shm: %zu\n",
sizeof(struct tee_shm));
return shm;
}
shm->ns.token = tee_core_alloc_uuid(shm);
if (shm->ns.token <= 0) {
pr_err("failed to alloc idr for shm\n");
kfree(shm);
return NULL;
}
/* FIXME whether it's correct? */
nr_pages = ((size - 1) >> PAGE_SHIFT) + 1;
pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
if (pages == NULL)
goto err_free_pagelist;
for (i = 0; i < nr_pages; i++) {
pages[i] = alloc_page(GFP_KERNEL);
if (pages[i] == NULL) {
pr_err("bad alloc page %zu\n", i);
goto err_free_pages;
}
}
shm->ns.pages = pages;
shm->ns.nr_pages = (size_t) nr_pages;
atomic_set(&shm->ns.ref, 1);
shm->size_req = size;
shm->size_alloc = nr_pages << PAGE_SHIFT;
shm->flags = flags;
return shm;
err_free_pages:
for (i = 0; i < nr_pages; i++) {
if (pages[i] == NULL)
break;
__free_page(pages[i]);
}
err_free_pagelist:
kfree(pages);
tee_core_free_uuid(shm->ns.token);
kfree(shm);
return NULL;
}
void tee_shm_free_ns(struct tee_shm *shm)
{
size_t i;
if (atomic_dec_return(&shm->ns.ref) != 0)
return;
for (i = 0; i < shm->ns.nr_pages; i++)
__free_page(shm->ns.pages[i]);
kfree(shm->ns.pages);
tee_core_free_uuid(shm->ns.token);
kfree(shm);
}
struct tee_shm *tkcore_alloc_shm(struct tee *tee, size_t size, uint32_t flags)
{
struct tee_shm *shm;
if ((shm_test_nonsecure(flags)))
shm = tee_shm_alloc_ns(tee, size, flags);
else
shm = tee_shm_alloc_static(tee, size, flags);
if (IS_ERR_OR_NULL(shm))
goto exit;
shm->tee = tee;
exit:
return shm;
}
void tkcore_shm_free(struct tee_shm *shm)
{
struct tee *tee;
if (IS_ERR_OR_NULL(shm))
return;
tee = shm->tee;
if (tee == NULL) {
pr_warn("tkcoredrv: %s(): NULL tee\n",
__func__);
return;
}
if (shm->tee == NULL) {
pr_warn("tkcoredrv: %s(): invalid shm\n", __func__);
return;
}
if (shm_test_nonsecure(shm->flags))
tee_shm_free_ns(shm);
else {
sg_free_table(&shm->resv.sgt);
shm->tee->ops->free(shm);
}
}
static int __tee_shm_attach_dma_buf(struct dma_buf *dmabuf,
struct dma_buf_attachment *attach)
{
struct tee_shm_attach *tee_shm_attach;
struct tee_shm *shm;
struct tee *tee;
shm = dmabuf->priv;
tee = shm->tee;
tee_shm_attach = devm_kzalloc(_DEV(tee),
sizeof(*tee_shm_attach), GFP_KERNEL);
if (!tee_shm_attach)
return -ENOMEM;
tee_shm_attach->dir = DMA_NONE;
attach->priv = tee_shm_attach;
return 0;
}
static void __tee_shm_detach_dma_buf(struct dma_buf *dmabuf,
struct dma_buf_attachment *attach)
{
struct tee_shm_attach *tee_shm_attach = attach->priv;
struct sg_table *sgt;
struct tee_shm *shm;
struct tee *tee;
shm = dmabuf->priv;
tee = shm->tee;
if (!tee_shm_attach) {
pr_err("No shm attached with this dmabuf context");
return;
}
sgt = &tee_shm_attach->sgt;
if (tee_shm_attach->dir != DMA_NONE)
dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
tee_shm_attach->dir);
sg_free_table(sgt);
devm_kfree(_DEV(tee), tee_shm_attach);
attach->priv = NULL;
}
static struct sg_table *__tee_shm_dma_buf_map_dma_buf(
struct dma_buf_attachment *attach, enum dma_data_direction dir)
{
struct tee_shm_attach *tee_shm_attach = attach->priv;
struct tee_shm *tee_shm = attach->dmabuf->priv;
struct sg_table *sgt = NULL;
struct scatterlist *rd, *wr;
unsigned int i;
int nents, ret;
struct tee *tee;
tee = tee_shm->tee;
/* just return current sgt if already requested. */
if (tee_shm_attach->dir == dir && tee_shm_attach->is_mapped)
return &tee_shm_attach->sgt;
sgt = &tee_shm_attach->sgt;
ret = sg_alloc_table(sgt, tee_shm->resv.sgt.orig_nents, GFP_KERNEL);
if (ret) {
pr_err("failed to alloc sgt.\n");
return ERR_PTR(-ENOMEM);
}
rd = tee_shm->resv.sgt.sgl;
wr = sgt->sgl;
for (i = 0; i < sgt->orig_nents; ++i) {
sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
rd = sg_next(rd);
wr = sg_next(wr);
}
if (dir != DMA_NONE) {
nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
if (!nents) {
pr_err("failed to map sgl with iommu.\n");
sg_free_table(sgt);
sgt = ERR_PTR(-EIO);
goto err_unlock;
}
}
tee_shm_attach->is_mapped = true;
tee_shm_attach->dir = dir;
attach->priv = tee_shm_attach;
err_unlock:
return sgt;
}
static void __tee_shm_dma_buf_unmap_dma_buf(struct dma_buf_attachment *attach,
struct sg_table *table,
enum dma_data_direction dir)
{
}
static void __tee_shm_dma_buf_release(struct dma_buf *dmabuf)
{
struct tee_shm *shm = dmabuf->priv;
struct tee_context *ctx;
struct tee *tee;
tee = shm->ctx->tee;
ctx = shm->ctx;
tee_shm_free_io(shm);
}
static int __tee_shm_dma_buf_mmap(struct dma_buf *dmabuf,
struct vm_area_struct *vma)
{
struct tee_shm *shm = dmabuf->priv;
size_t size = vma->vm_end - vma->vm_start;
struct tee *tee;
int ret;
pgprot_t prot;
unsigned long pfn;
tee = shm->ctx->tee;
pfn = shm->resv.paddr >> PAGE_SHIFT;
if (shm->flags & TEE_SHM_CACHED)
prot = vma->vm_page_prot;
else
prot = pgprot_noncached(vma->vm_page_prot);
ret =
remap_pfn_range(vma, vma->vm_start, pfn, size, prot);
if (!ret)
vma->vm_private_data = (void *)shm;
return ret;
}
static void *map_stub(struct dma_buf *dmabuf, unsigned long length)
{
(void) dmabuf;
(void) length;
return NULL;
}
static const struct dma_buf_ops tee_static_shm_dma_buf_ops = {
.attach = __tee_shm_attach_dma_buf,
.detach = __tee_shm_detach_dma_buf,
.map_dma_buf = __tee_shm_dma_buf_map_dma_buf,
.unmap_dma_buf = __tee_shm_dma_buf_unmap_dma_buf,
.release = __tee_shm_dma_buf_release,
.mmap = __tee_shm_dma_buf_mmap,
.map = map_stub,
};
static int tee_static_shm_export(struct tee *tee, struct tee_shm *shm,
int *export)
{
struct dma_buf *dmabuf;
int ret = 0;
#if defined(DEFINE_DMA_BUF_EXPORT_INFO)
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
#endif
if (shm_test_nonsecure(shm->flags)) {
pr_err(
"cannot export dmabuf for nonsecure buf flags: 0x%x\n",
shm->flags);
return -EINVAL;
}
/* Temporary fix to support both older and newer kernel versions. */
#if defined(DEFINE_DMA_BUF_EXPORT_INFO)
exp_info.priv = shm;
exp_info.ops = &tee_static_shm_dma_buf_ops;
exp_info.size = shm->size_alloc;
exp_info.flags = O_RDWR;
dmabuf = dma_buf_export(&exp_info);
#else
dmabuf = dma_buf_export(shm, &tee_static_shm_dma_buf_ops,
shm->size_alloc, O_RDWR, NULL);
#endif
if (IS_ERR_OR_NULL(dmabuf)) {
pr_err("dmabuf: couldn't export buffer (%ld)\n",
PTR_ERR(dmabuf));
ret = -EINVAL;
goto out;
}
*export = dma_buf_fd(dmabuf, O_CLOEXEC);
out:
return ret;
}
static int __tee_ns_shm_vma_fault(struct vm_fault *vmf)
{
struct vm_area_struct *vma = vmf->vma;
struct tee_shm *shm = (struct tee_shm *) vma->vm_private_data;
struct page *page;
if (vmf->pgoff >= shm->ns.nr_pages)
return VM_FAULT_ERROR;
page = shm->ns.pages[vmf->pgoff];
get_page(page);
vmf->page = page;
return 0;
}
static const struct vm_operations_struct tee_ns_shm_vm_ops = {
/* .close = __tee_ns_shm_vma_close, */
.fault = __tee_ns_shm_vma_fault,
};
static int __tee_ns_shm_release(struct inode *inode, struct file *filp)
{
struct tee_shm *shm = filp->private_data;
tee_shm_free_io(shm);
return 0;
}
static int __tee_ns_shm_mmap(struct file *filp, struct vm_area_struct *vma)
{
vma->vm_ops = &tee_ns_shm_vm_ops;
vma->vm_private_data = filp->private_data;
return 0;
}
static const struct file_operations tee_ns_shm_fops = {
.release = __tee_ns_shm_release,
.mmap = __tee_ns_shm_mmap,
};
static int tee_ns_shm_export(struct tee *tee, struct tee_shm *shm, int *export)
{
int fd;
if (!shm_test_nonsecure(shm->flags)) {
pr_err("cannot export for static buf flags: 0x%x\n",
shm->flags);
return -EINVAL;
}
fd = anon_inode_getfd("tz_ns_shm", &tee_ns_shm_fops,
(void *) shm, O_RDWR | O_CLOEXEC);
if (fd < 0) {
pr_err("anon_inode_getfd() failed with %d\n", fd);
return fd;
}
*export = fd;
return 0;
}
/* called inside tee->lock */
static int tee_ns_shm_inc_ref(struct tee_shm *shm)
{
/* check old value, if old value < 1, then do not inc ref.
* actually this part of logic is already protected by tee->lock
*/
atomic_inc(&shm->ns.ref);
return 0;
}
struct tee_shm *tee_shm_alloc_from_rpc(struct tee *tee, size_t size,
uint32_t extra_flags)
{
struct tee_shm *shm;
mutex_lock(&tee->lock);
shm = tkcore_alloc_shm(tee, size,
TEE_SHM_TEMP | TEE_SHM_FROM_RPC | extra_flags);
if (IS_ERR_OR_NULL(shm)) {
pr_err("buffer allocation failed (%ld)\n",
PTR_ERR(shm));
goto out;
}
tee_inc_stats(&tee->stats[TEE_STATS_SHM_IDX]);
list_add_tail(&shm->entry, &tee->list_rpc_shm);
shm->ctx = NULL;
out:
mutex_unlock(&tee->lock);
return shm;
}
void tee_shm_realloc_from_rpc(struct tee *tee, struct tee_shm *shm)
{
if (tee == NULL || shm == NULL)
return;
mutex_lock(&tee->lock);
if (shm->ctx == NULL)
goto out;
tee_inc_stats(&tee->stats[TEE_STATS_SHM_IDX]);
list_add_tail(&shm->entry, &tee->list_rpc_shm);
shm->ctx = NULL;
out:
mutex_unlock(&tee->lock);
}
void tee_shm_free_from_rpc(struct tee_shm *shm)
{
struct tee *tee;
if (shm == NULL)
return;
tee = shm->tee;
mutex_lock(&tee->lock);
if (shm->ctx == NULL) {
tee_dec_stats(&shm->tee->stats[TEE_STATS_SHM_IDX]);
list_del(&shm->entry);
}
tkcore_shm_free(shm);
mutex_unlock(&tee->lock);
}
static struct tee_shm *shm_from_paddr(struct tee *tee, void *paddr, bool ns)
{
struct list_head *pshm;
if (list_empty(&tee->list_rpc_shm))
return NULL;
list_for_each(pshm, &tee->list_rpc_shm) {
void *this_addr;
struct tee_shm *shm;
shm = list_entry(pshm, struct tee_shm, entry);
this_addr = ns ? (void *) (unsigned long) shm->ns.token :
(void *) (unsigned long) shm->resv.paddr;
if (this_addr == paddr)
return shm;
}
return NULL;
}
struct tee_shm *tee_shm_from_paddr(struct tee *tee, void *paddr, bool ns)
{
struct tee_shm *shm;
mutex_lock(&tee->lock);
shm = shm_from_paddr(tee, paddr, ns);
mutex_unlock(&tee->lock);
return shm;
}
/* Buffer allocated by rpc from fw and to be accessed by the user
* Not need to be registered as it is not allocated by the user
*/
int tee_shm_fd_for_rpc(struct tee_context *ctx, struct tee_shm_io *shm_io)
{
int ret;
bool ns;
struct tee_shm *shm;
struct tee *tee = ctx->tee;
shm_io->fd_shm = 0;
ns = !!shm_test_nonsecure(shm_io->flags);
mutex_lock(&tee->lock);
shm = shm_from_paddr(tee, shm_io->buffer, ns);
if (shm == NULL) {
pr_err("Can't find shm for %p\n", shm_io->buffer);
ret = -ENOMEM;
goto out;
}
if (ns)
ret = tee_ns_shm_export(tee, shm, &shm_io->fd_shm);
else
ret = tee_static_shm_export(tee, shm, &shm_io->fd_shm);
if (ret) {
ret = -ENOMEM;
goto out;
}
shm->ctx = ctx;
list_move(&shm->entry, &ctx->list_shm);
shm->dev = get_device(_DEV(tee));
ret = tee_get(tee);
WARN_ON(ret);
tee_context_get(ctx);
if (shm_test_nonsecure(shm_io->flags)) {
/*FIXME check for return value */
tee_ns_shm_inc_ref(shm);
} else
WARN_ON(!tee->ops->shm_inc_ref(shm));
out:
mutex_unlock(&tee->lock);
return ret;
}
int tee_shm_alloc_io_perm(struct tee_context *ctx, struct tee_shm_io *shm_io)
{
struct tee_shm *shm;
struct tee *tee = ctx->tee;
int ret;
if (shm_test_nonsecure(shm_io->flags)) {
pr_err("permanent shm cannot be nonsecure\n");
return -EINVAL;
}
if (ctx->usr_client)
shm_io->fd_shm = 0;
mutex_lock(&tee->lock);
shm = tkcore_alloc_shm(tee, shm_io->size, shm_io->flags);
if (IS_ERR_OR_NULL(shm)) {
pr_err("buffer allocation failed (%ld)\n",
PTR_ERR(shm));
ret = PTR_ERR(shm);
goto out;
}
if (ctx->usr_client) {
ret = tee_static_shm_export(tee, shm, &shm_io->fd_shm);
if (ret) {
tkcore_shm_free(shm);
ret = -ENOMEM;
goto out;
}
shm->flags |= TEEC_MEM_DMABUF;
}
shm_io->paddr = (void *)(unsigned long) shm->resv.paddr;
shm->ctx = ctx;
shm->dev = get_device(_DEV(tee));
ret = tee_get(tee);
WARN_ON(ret);
tee_context_get(ctx);
tee_inc_stats(&tee->stats[TEE_STATS_SHM_IDX]);
list_add_tail(&shm->entry, &ctx->list_shm);
out:
mutex_unlock(&tee->lock);
return ret;
}
int tee_shm_alloc_io(struct tee_context *ctx, struct tee_shm_io *shm_io)
{
struct tee_shm *shm;
struct tee *tee = ctx->tee;
int ret;
if (ctx->usr_client)
shm_io->fd_shm = 0;
mutex_lock(&tee->lock);
shm = tkcore_alloc_shm(tee, shm_io->size, shm_io->flags);
if (IS_ERR_OR_NULL(shm)) {
pr_err("buffer allocation failed (%ld)\n",
PTR_ERR(shm));
ret = PTR_ERR(shm);
goto out;
}
if (ctx->usr_client) {
if (shm_test_nonsecure(shm_io->flags))
ret = tee_ns_shm_export(tee, shm, &shm_io->fd_shm);
else
ret = tee_static_shm_export(tee, shm, &shm_io->fd_shm);
if (ret) {
tkcore_shm_free(shm);
ret = -ENOMEM;
goto out;
}
shm->flags |= TEEC_MEM_DMABUF;
}
shm->ctx = ctx;
shm->dev = get_device(_DEV(tee));
ret = tee_get(tee);
WARN_ON(ret); /* tee_core_get must not issue */
tee_context_get(ctx);
tee_inc_stats(&tee->stats[TEE_STATS_SHM_IDX]);
list_add_tail(&shm->entry, &ctx->list_shm);
out:
mutex_unlock(&tee->lock);
return ret;
}
void tee_shm_free_io(struct tee_shm *shm)
{
struct tee_context *ctx = shm->ctx;
struct tee *tee = ctx->tee;
struct device *dev = shm->dev;
mutex_lock(&tee->lock);
tee_dec_stats(&tee->stats[TEE_STATS_SHM_IDX]);
list_del(&shm->entry);
tkcore_shm_free(shm);
tee_put(ctx->tee);
tee_context_put(ctx);
if (dev)
put_device(dev);
mutex_unlock(&tee->lock);
}
static int tee_shm_db_get(struct tee *tee, struct tee_shm *shm, int fd,
unsigned int flags, size_t size, int offset)
{
struct tee_shm_dma_buf *sdb;
struct dma_buf *dma_buf;
int ret = 0;
dma_buf = dma_buf_get(fd);
if (IS_ERR(dma_buf)) {
ret = PTR_ERR(dma_buf);
goto exit;
}
sdb = kzalloc(sizeof(*sdb), GFP_KERNEL);
if (IS_ERR_OR_NULL(sdb)) {
pr_err("can't alloc tee_shm_dma_buf\n");
ret = PTR_ERR(sdb);
goto buf_put;
}
shm->resv.sdb = sdb;
if (dma_buf->size < size + offset) {
pr_err("dma_buf too small %zd < %zd + %d\n",
dma_buf->size, size, offset);
ret = -EINVAL;
goto free_sdb;
}
sdb->attach = dma_buf_attach(dma_buf, _DEV(tee));
if (IS_ERR_OR_NULL(sdb->attach)) {
ret = PTR_ERR(sdb->attach);
goto free_sdb;
}
sdb->sgt = dma_buf_map_attachment(sdb->attach, DMA_NONE);
if (IS_ERR_OR_NULL(sdb->sgt)) {
ret = PTR_ERR(sdb->sgt);
goto buf_detach;
}
if (sg_nents(sdb->sgt->sgl) != 1) {
ret = -EINVAL;
goto buf_unmap;
}
shm->resv.paddr = sg_phys(sdb->sgt->sgl) + offset;
if (dma_buf->ops->attach == __tee_shm_attach_dma_buf)
sdb->tee_allocated = true;
else
sdb->tee_allocated = false;
shm->flags |= TEEC_MEM_DMABUF;
goto exit;
buf_unmap:
dma_buf_unmap_attachment(sdb->attach, sdb->sgt, DMA_NONE);
buf_detach:
dma_buf_detach(dma_buf, sdb->attach);
free_sdb:
kfree(sdb);
buf_put:
dma_buf_put(dma_buf);
exit:
return ret;
}
struct tee_shm *tkcore_shm_get(struct tee_context *ctx,
struct TEEC_SharedMemory *c_shm,
size_t size, int offset)
{
struct tee_shm *shm;
struct tee *tee = ctx->tee;
int ret;
if (shm_test_nonsecure(c_shm->flags)) {
pr_err("invalid shared memory flags: 0x%x\n",
c_shm->flags);
return NULL;
}
mutex_lock(&tee->lock);
shm = kzalloc(sizeof(*shm), GFP_KERNEL);
if (IS_ERR_OR_NULL(shm)) {
pr_err("can't alloc tee_shm\n");
ret = -ENOMEM;
goto err;
}
shm->ctx = ctx;
shm->tee = tee;
shm->dev = _DEV(tee);
shm->flags = c_shm->flags | TEE_SHM_MEMREF;
shm->size_req = size;
shm->size_alloc = 0;
if (c_shm->flags & TEEC_MEM_KAPI) {
struct tee_shm *kc_shm = (struct tee_shm *)c_shm->d.ptr;
if (!kc_shm) {
pr_err("kapi fd null\n");
ret = -EINVAL;
goto err;
}
shm->resv.paddr = kc_shm->resv.paddr;
if (kc_shm->size_alloc < size + offset) {
pr_err("kapi buff too small %zd < %zd + %d\n",
kc_shm->size_alloc, size, offset);
ret = -EINVAL;
goto err;
}
} else if (c_shm->d.fd) {
ret = tee_shm_db_get(tee, shm,
c_shm->d.fd, c_shm->flags, size, offset);
if (ret)
goto err;
} else if (!c_shm->buffer) {
pr_debug("null buffer, pass 'as is'\n");
} else {
ret = -EINVAL;
goto err;
}
mutex_unlock(&tee->lock);
return shm;
err:
kfree(shm);
mutex_unlock(&tee->lock);
return ERR_PTR(ret);
}
void tkcore_shm_put(struct tee_context *ctx, struct tee_shm *shm)
{
struct tee *tee;
WARN_ON(!ctx);
if (!ctx)
return;
tee = ctx->tee;
WARN_ON(!tee);
if (!tee)
return;
WARN_ON(!shm);
if (!shm)
return;
WARN_ON(!(shm->flags & TEE_SHM_MEMREF));
if (shm_test_nonsecure(shm->flags)) {
pr_warn("invalid shared memory flags: 0x%x\n",
shm->flags);
return;
}
mutex_lock(&tee->lock);
if (shm->flags & TEEC_MEM_DMABUF) {
struct tee_shm_dma_buf *sdb;
struct dma_buf *dma_buf;
sdb = shm->resv.sdb;
dma_buf = sdb->attach->dmabuf;
dma_buf_unmap_attachment(sdb->attach, sdb->sgt, DMA_NONE);
dma_buf_detach(dma_buf, sdb->attach);
dma_buf_put(dma_buf);
kfree(sdb);
sdb = 0;
}
kfree(shm);
mutex_unlock(&tee->lock);
}
+37
View File
@@ -0,0 +1,37 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_SHM_H__
#define __TEE_SHM_H__
#include <linux/tee_client_api.h>
struct tee_context;
struct tee_shm;
struct tee_shm_io;
struct tee;
static inline int shm_test_nonsecure(uint32_t flags)
{
return flags & TEEC_MEM_NONSECURE;
}
int tee_shm_alloc_io(struct tee_context *ctx, struct tee_shm_io *shm_io);
void tee_shm_free_io(struct tee_shm *shm);
struct tee_shm *tee_shm_from_paddr(struct tee *tee, void *paddr, bool ns);
int tee_shm_fd_for_rpc(struct tee_context *ctx, struct tee_shm_io *shm_io);
int tee_shm_alloc_io_perm(struct tee_context *ctx, struct tee_shm_io *shm_io);
struct tee_shm *tkcore_alloc_shm(struct tee *tee, size_t size, uint32_t flags);
void tkcore_shm_free(struct tee_shm *shm);
struct tee_shm *tkcore_shm_get(struct tee_context *ctx,
struct TEEC_SharedMemory *c_shm,
size_t size, int offset);
void tkcore_shm_put(struct tee_context *ctx, struct tee_shm *shm);
#endif /* __TEE_SHM_H__ */
+291
View File
@@ -0,0 +1,291 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/anon_inodes.h>
#include <linux/semaphore.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/device.h>
#include "tee_shm.h"
#include "tee_core.h"
#include "tee_supp_com.h"
enum teec_rpc_result tee_supp_cmd(struct tee *tee,
uint32_t id, void *data, size_t datalen)
{
struct tee_rpc *rpc = tee->rpc;
enum teec_rpc_result res = TEEC_RPC_FAIL;
size_t size;
struct task_struct *task = current;
(void) task;
if (id == TEE_RPC_ICMD_INVOKE) {
if (atomic_read(&rpc->used) == 0) {
pr_err("teed not ready. id=0x%x\n", id);
goto out;
}
}
switch (id) {
case TEE_RPC_ICMD_ALLOCATE:
{
struct tee_rpc_alloc *alloc;
struct tee_shm *shmint;
alloc = (struct tee_rpc_alloc *)data;
size = alloc->size;
memset(alloc, 0, sizeof(struct tee_rpc_alloc));
shmint = tee_shm_alloc_from_rpc(tee, size, 0U);
if (IS_ERR_OR_NULL(shmint))
break;
alloc->size = size;
alloc->data = (void *) (unsigned long)
shmint->resv.paddr;
alloc->shm = shmint;
res = TEEC_RPC_OK;
break;
}
case TEE_RPC_ICMD_FREE:
{
struct tee_rpc_free *free;
free = (struct tee_rpc_free *)data;
tee_shm_free_from_rpc(free->shm);
res = TEEC_RPC_OK;
break;
}
case TEE_RPC_ICMD_INVOKE:
{
if (sizeof(rpc->commToUser) < datalen)
break;
/*
* Other threads blocks here until we've copied our
* answer from the teed
*/
mutex_lock(&rpc->thrd_mutex);
mutex_lock(&rpc->outsync);
rpc->res = 0;
memcpy(&rpc->commToUser, data, datalen);
mutex_unlock(&rpc->outsync);
up(&rpc->datatouser);
down(&rpc->datafromuser);
mutex_lock(&rpc->insync);
memcpy(data, &rpc->commFromUser, datalen);
mutex_unlock(&rpc->insync);
res = rpc->res;
mutex_unlock(&rpc->thrd_mutex);
break;
}
default:
/* not supported */
break;
}
out:
return res;
}
EXPORT_SYMBOL(tee_supp_cmd);
ssize_t tee_supp_read(struct file *filp, char __user *buffer,
size_t length, loff_t *offset)
{
struct tee_context *ctx = (struct tee_context *)(filp->private_data);
struct tee *tee;
struct tee_rpc *rpc;
struct task_struct *task = current;
int ret;
(void) task;
if (ctx == NULL ||
ctx->tee == NULL ||
ctx->tee->dev == NULL ||
ctx->tee->rpc == NULL) {
pr_err("invalid context\n");
return -EINVAL;
}
tee = ctx->tee;
rpc = tee->rpc;
if (atomic_read(&rpc->used) == 0) {
pr_err("teed application not ready\n");
ret = -EPERM;
goto out;
}
if (down_interruptible(&rpc->datatouser))
return -ERESTARTSYS;
mutex_lock(&rpc->outsync);
ret =
sizeof(rpc->commToUser) - sizeof(rpc->commToUser.cmds) +
sizeof(rpc->commToUser.cmds[0]) * rpc->commToUser.nbr_bf;
if (length < ret) {
ret = -EINVAL;
} else {
if (copy_to_user(buffer, &rpc->commToUser, ret)) {
pr_err("copy_to_user(comm) failed!\n");
ret = -EINVAL;
}
}
mutex_unlock(&rpc->outsync);
out:
return ret;
}
ssize_t tee_supp_write(struct file *filp, const char __user *buffer,
size_t length, loff_t *offset)
{
int ret = 0;
uint32_t i;
unsigned long r;
struct tee_context *ctx = (struct tee_context *) (filp->private_data);
struct tee *tee;
struct tee_rpc *rpc;
struct task_struct *task = current;
(void) task;
if (ctx == NULL || ctx->tee == NULL || ctx->tee->rpc == NULL) {
pr_err("Invalid ctx\n");
return -EINVAL;
}
if (length == 0)
return 0;
tee = ctx->tee;
rpc = tee->rpc;
if (atomic_read(&rpc->used) == 0) {
pr_err("teed not ready\n");
goto out;
}
if (length > sizeof(rpc->commFromUser)) {
ret = -EINVAL;
goto out;
}
mutex_lock(&rpc->insync);
r = copy_from_user(&rpc->commFromUser, buffer, length);
if (r) {
pr_err("copy_from_user(comm) failed: %lu\n", r);
rpc->res = -EINVAL;
mutex_unlock(&rpc->insync);
up(&rpc->datafromuser);
ret = -EINVAL;
goto out;
}
if (rpc->commFromUser.nbr_bf > TEE_RPC_BUFFER_NUMBER) {
rpc->res = -EINVAL;
mutex_unlock(&rpc->insync);
up(&rpc->datafromuser);
ret = -EINVAL;
goto out;
}
/* Translate virtual address of caller into physical address */
for (i = 0; i < rpc->commFromUser.nbr_bf; i++) {
uint32_t type = rpc->commFromUser.cmds[i].type;
void *buffer = rpc->commFromUser.cmds[i].buffer;
if (type != TEE_RPC_BUFFER || buffer == NULL)
continue;
if (type & TEE_RPC_BUFFER_NONSECURE) {
} else {
struct tee_shm *shm;
struct vm_area_struct *vma = find_vma(
current->mm, (unsigned long) buffer);
if (vma == NULL)
continue;
shm = vma->vm_private_data;
if (shm == NULL) {
pr_err(
"Invalid vma->vm_private_data [%s:%d:%d]\n",
current->comm,
current->tgid,
current->pid);
rpc->res = -EINVAL;
mutex_unlock(&rpc->insync);
up(&rpc->datafromuser);
ret = -EINVAL;
goto out;
}
rpc->commFromUser.cmds[i].buffer =
(void *) (unsigned long)
shm->resv.paddr;
}
}
rpc->res = 0;
mutex_unlock(&rpc->insync);
up(&rpc->datafromuser);
ret = length;
out:
return ret;
}
int tee_supp_init(struct tee *tee)
{
struct tee_rpc *rpc;
rpc = devm_kzalloc(tee->dev, sizeof(struct tee_rpc), GFP_KERNEL);
if (rpc == NULL)
return -ENOMEM;
rpc->datafromuser = (struct semaphore)
__SEMAPHORE_INITIALIZER(rpc->datafromuser, 0);
rpc->datatouser = (struct semaphore)
__SEMAPHORE_INITIALIZER(rpc->datatouser, 0);
mutex_init(&rpc->thrd_mutex);
mutex_init(&rpc->outsync);
mutex_init(&rpc->insync);
atomic_set(&rpc->used, 0);
tee->rpc = rpc;
return 0;
}
void tee_supp_deinit(struct tee *tee)
{
devm_kfree(tee->dev, tee->rpc);
tee->rpc = NULL;
}
+116
View File
@@ -0,0 +1,116 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_SUPP_COMM_H
#define TEE_SUPP_COMM_H
#define TEE_RPMB_BUFFER_NUMBER 2
#define TEE_RPC_ICMD_ALLOCATE 0x1001
#define TEE_RPC_ICMD_FREE 0x1002
#define TEE_RPC_ICMD_INVOKE 0x1003
#define TEE_RPC_NBR_BUFF 1
#define TEE_RPC_DATA_SIZE 64
#define TEE_RPC_BUFFER_NUMBER 5
#define TEE_RPC_STATE_IDLE 0x00
#define TEE_RPC_STATE_ACTIVE 0x01
/* Be Consistent with teed (user space) */
#define TEE_RPC_BUFFER 0x00000001
#define TEE_RPC_VALUE 0x00000002
#define TEE_RPC_BUFFER_NONSECURE 0x00040000
#define TEE_RPC_LOAD_TA 0x10000001
#define TEE_RPC_RPMB_CMD 0x1000000A
#define TEE_RPC_INSTALL_TA 0x10000013
#define TEE_RPC_DELETE_TA 0x10000014
#define TEE_RPC_INSTALL_TA_RESP 0x10000015
#define TEE_RPC_INSTALL_SYS_TA 0x10000020
/*
* Handled within the driver only
* Be CONSISTENT with tkcore-os (secure space)
*/
#define TEE_RPC_WAIT_QUEUE_SLEEP 0x20000001
#define TEE_RPC_WAIT_QUEUE_WAKEUP 0x20000002
#define TEE_RPC_WAIT 0x30000000
#include <linux/semaphore.h>
/**
* struct tee_rpc_bf - Contains definition of the tee com buffer
* @state: Buffer state
* @data: Command data
*/
struct tee_rpc_bf {
uint32_t state;
uint8_t data[TEE_RPC_DATA_SIZE];
};
struct tee_rpc_alloc {
uint32_t size; /* size of block */
void *data; /* pointer to data */
void *shm; /* pointer to an opaque data, being shm structure */
};
struct tee_rpc_free {
void *shm; /* pointer to an opaque data, being shm structure */
};
struct tee_rpc_cmd {
union {
void *buffer;
uint64_t padding_buf;
};
uint32_t size;
uint32_t type;
int fd;
int reserved;
};
struct tee_rpc_invoke {
uint32_t cmd;
uint32_t res;
uint32_t nbr_bf;
uint32_t reserved;
struct tee_rpc_cmd cmds[TEE_RPC_BUFFER_NUMBER];
};
struct tee_rpc {
struct tee_rpc_invoke commToUser;
struct tee_rpc_invoke commFromUser;
struct semaphore datatouser;
struct semaphore datafromuser;
struct mutex thrd_mutex; /* Block the thread to wait for supp answer */
struct mutex outsync; /* Out sync mutex */
struct mutex insync; /* In sync mutex */
atomic_t used;
int res;
};
enum teec_rpc_result {
TEEC_RPC_OK,
TEEC_RPC_FAIL
};
struct tee;
int tee_supp_init(struct tee *tee);
void tee_supp_deinit(struct tee *tee);
enum teec_rpc_result tee_supp_cmd(struct tee *tee,
uint32_t id, void *data, size_t datalen);
ssize_t tee_supp_read(struct file *filp, char __user *buffer,
size_t length, loff_t *offset);
ssize_t tee_supp_write(struct file *filp, const char __user *buffer,
size_t length, loff_t *offset);
#endif
+196
View File
@@ -0,0 +1,196 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/atomic.h>
#include <asm/page.h>
#include "tee_core_priv.h"
static ssize_t dump_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
int len;
char *tmp_buf;
tmp_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
if (!tmp_buf)
return -ENOMEM;
len = tee_context_dump(tee, tmp_buf, PAGE_SIZE - 128);
if (len > 0)
len = snprintf(buf, PAGE_SIZE, "%s", tmp_buf);
kfree(tmp_buf);
return len;
}
static ssize_t stat_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "%d/%d %d/%d %d/%d %d/%d\n",
atomic_read(&tee->refcount),
tee->max_refcount,
tee->stats[TEE_STATS_CONTEXT_IDX].count,
tee->stats[TEE_STATS_CONTEXT_IDX].max,
tee->stats[TEE_STATS_SESSION_IDX].count,
tee->stats[TEE_STATS_SESSION_IDX].max,
tee->stats[TEE_STATS_SHM_IDX].count,
tee->stats[TEE_STATS_SHM_IDX].max);
}
static ssize_t info_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "%s iminor=%d dev=\"%s\" state=%d\n",
dev_name(tee->dev), tee->miscdev.minor,
dev_name(tee->miscdev.this_device), tee->state);
}
static ssize_t name_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "%s\n", tee->name);
}
static ssize_t type_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "%s\n", tee->ops->type);
}
static ssize_t refcount_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&tee->refcount));
}
static ssize_t conf_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "0x%08x\n", tee->conf);
}
static ssize_t test_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
return snprintf(buf, PAGE_SIZE, "%08X\n", tee->test);
}
static ssize_t test_store(struct device *device,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct tee *tee = dev_get_drvdata(device);
unsigned long val;
int status;
status = kstrtoul(buf, 0, &val);
if (status)
return status;
if ((tee->conf & TEE_CONF_TEST_MODE) == TEE_CONF_TEST_MODE)
tee->test = val;
return count;
}
/*
* A state-to-string lookup table, for exposing a human readable state
* via sysfs. Always keep in sync with enum tee_state
*/
static const char *const tee_state_string[] = {
"offline",
"online",
"suspended",
"running",
"crashed",
"invalid",
};
static ssize_t tee_show_state(struct device *device,
struct device_attribute *attr, char *buf)
{
struct tee *tee = dev_get_drvdata(device);
int state = tee->state > TEE_LAST ? TEE_LAST : tee->state;
return snprintf(buf, PAGE_SIZE, "%s (%d)\n", tee_state_string[state],
tee->state);
}
static struct device_attribute device_attrs[] = {
__ATTR_RO(dump),
__ATTR_RO(stat),
__ATTR_RO(info),
__ATTR(test, (0660), test_show, test_store),
__ATTR(state, 0444, tee_show_state, NULL),
__ATTR(name, 0444, name_show, NULL),
__ATTR(refcount, 0444, refcount_show, NULL),
__ATTR(type, 0444, type_show, NULL),
__ATTR(conf, 0444, conf_show, NULL),
};
int tee_init_sysfs(struct tee *tee)
{
int i, error = 0;
if (!tee)
return -EINVAL;
if (dev_get_drvdata(tee->miscdev.this_device) != tee) {
pr_err("drvdata is not valid\n");
return -ENODEV;
}
for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
error = device_create_file(tee->miscdev.this_device,
&device_attrs[i]);
if (error) {
pr_err("bad device_create_file(): %d\n",
error);
break;
}
}
if (error) {
while (--i >= 0) {
device_remove_file(
tee->miscdev.this_device,
&device_attrs[i]);
}
}
return error;
}
void tee_cleanup_sysfs(struct tee *tee)
{
int i;
if (!tee)
return;
for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
device_remove_file(tee->miscdev.this_device, &device_attrs[i]);
}
+14
View File
@@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_SYSFS_H__
#define __TEE_SYSFS_H__
struct tee;
int tee_init_sysfs(struct tee *tee);
void tee_cleanup_sysfs(struct tee *tee);
#endif
+286
View File
@@ -0,0 +1,286 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/tee_ioc.h>
#include "tee_core.h"
#include "tee_ta_mgmt.h"
#include "tee_shm.h"
#include "tee_supp_com.h"
struct completion event_inst_ta;
static struct mutex mutex_inst_ta;
int tee_install_sp_ta(struct tee_context *ctx,
void __user *__tee_spta_inst_desc)
{
struct tee *tee;
struct tee_shm *shm_in = NULL;
struct tee_shm *shm_out = NULL;
struct tee_rpc_invoke request;
struct tee_spta_inst_desc ta;
uint32_t response_len = 0;
uint32_t response_msg_len = 0;
int ret = 0;
if (ctx == NULL || __tee_spta_inst_desc == NULL) {
pr_err("invalid param\n");
return -EINVAL;
}
tee = ctx->tee;
if (copy_from_user(&ta, (void *) __tee_spta_inst_desc,
sizeof(struct tee_spta_inst_desc))) {
pr_err("copy_from_user tee_spta_inst_desc failed\n");
return -EINVAL;
}
if (ta.ta_size < 4)
return -EINVAL;
shm_in = tee_shm_alloc_from_rpc(tee, ta.ta_size, 0);
if (IS_ERR_OR_NULL(shm_in)) {
pr_err("failed to alloc shm_in\n");
return -ENOMEM;
}
shm_out = tee_shm_alloc_from_rpc(tee, ta.ta_size, 0);
if (IS_ERR_OR_NULL(shm_out)) {
pr_err("failed to alloc shm_out\n");
tee_shm_free_from_rpc(shm_in);
return -ENOMEM;
}
if (copy_from_user(shm_in->resv.kaddr, (void *) ta.ta_binary,
ta.ta_size)) {
pr_err("copy ta_binary failed\n");
ret = -EINVAL;
goto exit;
}
memset(&request, 0, sizeof(struct tee_rpc_invoke));
request.cmd = TEE_RPC_INSTALL_TA;
request.nbr_bf = 2;
request.cmds[0].buffer = (void *) (unsigned long) shm_in->resv.paddr;
request.cmds[0].size = shm_in->size_req;
request.cmds[1].buffer = (void *) (unsigned long) shm_out->resv.paddr;
request.cmds[1].size = shm_out->size_req;
mutex_lock(&mutex_inst_ta);
ret = tee_supp_cmd(ctx->tee, TEE_RPC_ICMD_INVOKE,
&request, sizeof(struct tee_rpc_invoke));
if (ret == 0)
ret = request.res;
if (ret) {
pr_err(
"TA install command failed to start with %d\n", ret);
goto exit;
}
wait_for_completion(&event_inst_ta);
mutex_unlock(&mutex_inst_ta);
/*
* response_msg format:
* +----------------------------------+
* | TEE_Result | resp_msg_len | resp_msg_json | ta_bin_len | ta_bin |
*/
memcpy(&ret, (void *) shm_out->resv.kaddr, 4);
if (ret) {
pr_err("install TA failed with 0x%x\n", ret);
goto exit;
}
memcpy(&response_msg_len, (uint8_t *) shm_out->resv.kaddr + 4, 4);
if (ta.ta_size - 4 < response_msg_len) {
pr_err(
"unexpected response msg len: %u total_size: %u\n",
ta.ta_size, response_msg_len);
ret = -E2BIG;
goto exit;
}
/* response_msg_len + sizeof(response_msg_len) + sizeof(TEE_Result */
response_len = response_msg_len + 4 + 4;
ret = copy_to_user((void __user *) ta.ta_binary,
(void *) shm_out->resv.kaddr, response_len);
if (ret) {
pr_err("copy response buffer failed with %d\n", ret);
ret = ret < 0 ? ret : -EAGAIN;
goto exit;
}
put_user(response_len, (uint32_t __user *) ta.response_len);
exit:
tee_shm_free_from_rpc(shm_in);
tee_shm_free_from_rpc(shm_out);
return ret;
}
int tee_install_sp_ta_response(struct tee_context *ctx, void __user *u_arg)
{
complete(&event_inst_ta);
return 0;
}
int tee_delete_sp_ta(struct tee_context *ctx, void __user *uuid)
{
int ret = 0;
struct tee *tee;
struct tee_rpc_invoke request;
struct tee_shm *shm_uuid;
if (ctx == NULL || uuid == NULL) {
pr_err("invalid param\n");
return -EINVAL;
}
tee = ctx->tee;
shm_uuid = tee_shm_alloc_from_rpc(tee, sizeof(struct TEEC_UUID), 0);
if (IS_ERR_OR_NULL(shm_uuid)) {
pr_err("failed to alloc shm uuid\n");
return -ENOMEM;
}
if (copy_from_user(shm_uuid->resv.kaddr,
uuid, sizeof(struct TEEC_UUID))) {
pr_err("TEEC_UUID copy_from_user failed\n");
ret = -EINVAL;
goto exit;
}
memset(&request, 0, sizeof(struct tee_rpc_invoke));
request.cmd = TEE_RPC_DELETE_TA;
request.nbr_bf = 1;
request.cmds[0].buffer = (void *) (unsigned long) shm_uuid->resv.paddr;
request.cmds[0].size = shm_uuid->size_req;
ret = tee_supp_cmd(tee, TEE_RPC_ICMD_INVOKE, &request, sizeof(request));
if (ret) {
pr_err("start delete_ta failed with %d\n", ret);
goto exit;
}
ret = request.res;
if (ret)
pr_err("delete_ta failed with 0x%x\n", ret);
exit:
tee_shm_free_from_rpc(shm_uuid);
return ret;
}
int tee_install_sys_ta(struct tee *tee, void __user *u_arg)
{
int r;
void *shm_kva;
unsigned long left;
struct TEEC_UUID uuid;
struct tee_rpc_invoke inv;
struct tee_shm *shm;
struct tee_ta_inst_desc ta_inst_desc;
if ((copy_from_user(&ta_inst_desc, u_arg,
sizeof(struct tee_ta_inst_desc)))) {
return -EFAULT;
}
if (copy_from_user(&uuid, ta_inst_desc.uuid, sizeof(struct TEEC_UUID)))
return -EFAULT;
/* check for integer overflow */
if (sizeof(struct TEEC_UUID) + sizeof(uint32_t) >=
sizeof(struct TEEC_UUID) + sizeof(uint32_t) +
ta_inst_desc.ta_buf_size) {
return -ENOMEM;
}
shm = tee_shm_alloc_from_rpc(tee, sizeof(struct TEEC_UUID) +
sizeof(uint32_t) + ta_inst_desc.ta_buf_size,
TEEC_MEM_NONSECURE);
if (shm == NULL)
return -ENOMEM;
shm_kva = vmap(shm->ns.pages, shm->ns.nr_pages, VM_MAP, PAGE_KERNEL);
if (shm_kva == NULL) {
pr_err("failed to vmap %zu pages\n",
shm->ns.nr_pages);
r = -ENOMEM;
goto exit;
}
memcpy(shm_kva, &uuid, sizeof(struct TEEC_UUID));
memcpy((char *) shm_kva + sizeof(struct TEEC_UUID),
&ta_inst_desc.ta_buf_size, sizeof(uint32_t));
left = copy_from_user(
(char *) shm_kva + sizeof(struct TEEC_UUID) + sizeof(uint32_t),
ta_inst_desc.ta_buf,
ta_inst_desc.ta_buf_size);
if (left) {
pr_err("copy_from_user failed size %x return: %lu\n",
ta_inst_desc.ta_buf_size, left);
vunmap(shm_kva);
r = -EFAULT;
goto exit;
}
vunmap(shm_kva);
memset(&inv, 0, sizeof(inv));
inv.cmd = TEE_RPC_INSTALL_SYS_TA;
inv.res = TEEC_ERROR_NOT_IMPLEMENTED;
inv.nbr_bf = 1;
inv.cmds[0].buffer = (void *) (unsigned long) shm->ns.token;
inv.cmds[0].type = TEE_RPC_BUFFER | TEE_RPC_BUFFER_NONSECURE;
inv.cmds[0].size = shm->size_req;
r = tee_supp_cmd(tee, TEE_RPC_ICMD_INVOKE, &inv, sizeof(inv));
if (r)
pr_err("install_sys_ta failed with %d\n", r);
else
r = inv.res;
exit:
tee_shm_free_from_rpc(shm);
return r;
}
int tee_ta_mgmt_init(void)
{
mutex_init(&mutex_inst_ta);
init_completion(&event_inst_ta);
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_TA_MGMT_H
#define TEE_TA_MGMT_H
struct tee;
struct tee_context;
int tee_ta_mgmt_init(void);
int tee_install_sp_ta(struct tee_context *ctx, void __user *ta_spta_inst_desc);
int tee_install_sp_ta_response(struct tee_context *ctx, void __user *u_arg);
int tee_delete_sp_ta(struct tee_context *ctx, void __user *uuid);
int tee_install_sys_ta(struct tee *tee, void __user *u_arg);
#endif
+115
View File
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/module.h>
#include <linux/spinlock.h>
#include "tee_tui.h"
static int trustedui_mode = TRUSTEDUI_MODE_OFF;
static int trustedui_blank_counter;
static DEFINE_SPINLOCK(trustedui_lock);
int trustedui_blank_inc(void)
{
unsigned long flags;
int newvalue;
spin_lock_irqsave(&trustedui_lock, flags);
newvalue = ++trustedui_blank_counter;
spin_unlock_irqrestore(&trustedui_lock, flags);
return newvalue;
}
EXPORT_SYMBOL(trustedui_blank_inc);
int trustedui_blank_dec(void)
{
unsigned long flags;
int newvalue;
spin_lock_irqsave(&trustedui_lock, flags);
newvalue = --trustedui_blank_counter;
spin_unlock_irqrestore(&trustedui_lock, flags);
return newvalue;
}
EXPORT_SYMBOL(trustedui_blank_dec);
int trustedui_blank_get_counter(void)
{
unsigned long flags;
int newvalue;
spin_lock_irqsave(&trustedui_lock, flags);
newvalue = trustedui_blank_counter;
spin_unlock_irqrestore(&trustedui_lock, flags);
return newvalue;
}
EXPORT_SYMBOL(trustedui_blank_get_counter);
void trustedui_blank_set_counter(int counter)
{
unsigned long flags;
spin_lock_irqsave(&trustedui_lock, flags);
trustedui_blank_counter = counter;
spin_unlock_irqrestore(&trustedui_lock, flags);
}
EXPORT_SYMBOL(trustedui_blank_set_counter);
int trustedui_get_current_mode(void)
{
unsigned long flags;
int mode;
spin_lock_irqsave(&trustedui_lock, flags);
mode = trustedui_mode;
spin_unlock_irqrestore(&trustedui_lock, flags);
return mode;
}
EXPORT_SYMBOL(trustedui_get_current_mode);
void trustedui_set_mode(int mode)
{
unsigned long flags;
spin_lock_irqsave(&trustedui_lock, flags);
trustedui_mode = mode;
spin_unlock_irqrestore(&trustedui_lock, flags);
}
EXPORT_SYMBOL(trustedui_set_mode);
int trustedui_set_mask(int mask)
{
unsigned long flags;
int mode;
spin_lock_irqsave(&trustedui_lock, flags);
mode = trustedui_mode |= mask;
spin_unlock_irqrestore(&trustedui_lock, flags);
return mode;
}
EXPORT_SYMBOL(trustedui_set_mask);
int trustedui_clear_mask(int mask)
{
unsigned long flags;
int mode;
spin_lock_irqsave(&trustedui_lock, flags);
mode = trustedui_mode &= ~mask;
spin_unlock_irqrestore(&trustedui_lock, flags);
return mode;
}
EXPORT_SYMBOL(trustedui_clear_mask);
+36
View File
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef _TEE_TUI_H_
#define _TEE_TUI_H_
#define TRUSTEDUI_MODE_OFF 0x00
#define TRUSTEDUI_MODE_ALL 0xff
#define TRUSTEDUI_MODE_TUI_SESSION 0x01
#define TRUSTEDUI_MODE_VIDEO_SECURED 0x02
#define TRUSTEDUI_MODE_INPUT_SECURED 0x04
int teec_wait_cmd(uint32_t *cmd_id);
bool teec_notify_event(uint32_t event_type);
int trustedui_blank_inc(void);
int trustedui_blank_dec(void);
int trustedui_blank_get_counter(void);
void trustedui_blank_set_counter(int counter);
int trustedui_get_current_mode(void);
void trustedui_set_mode(int mode);
int trustedui_set_mask(int mask);
int trustedui_clear_mask(int mask);
/**
* Notification ID's for communication Trustlet Connector -> Driver.
*/
#define NOT_TUI_NONE 0
/* NWd system event that closes the current TUI session*/
#define NOT_TUI_CANCEL_EVENT 1
#endif /* _TEE_TUI_H_ */
+86
View File
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/types.h>
#include <linux/device.h>
#include <linux/fb.h>
#include "tee_tui.h"
#include "tee_tui_hal.h"
#include <linux/delay.h>
/**
* hal_tui_init() - integrator specific initialization for kernel module
*
* This function is called when the kernel module is initialized, either at
* boot time, if the module is built statically in the kernel, or when the
* kernel is dynamically loaded if the module is built as a dynamic kernel
* module. This function may be used by the integrator, for instance, to get a
* memory pool that will be used to allocate the secure framebuffer and work
* buffer for TUI sessions.
*
* Return: must return 0 on success, or non-zero on error. If the function
* returns an error, the module initialization will fail.
*/
uint32_t __weak hal_tui_init(void)
{
return 0;
}
/**
* hal_tui_exit() - integrator specific exit code for kernel module
*
* This function is called when the kernel module exit. It is called when the
* kernel module is unloaded, for a dynamic kernel module, and never called for
* a module built into the kernel. It can be used to free any resources
* allocated by hal_tui_init().
*/
void __weak hal_tui_exit(void)
{
}
/**
* hal_tui_deactivate() - deactivate Normal World display and input
*
* This function should stop the Normal World display and, if necessary, Normal
* World input. It is called when a TUI session is opening, before the Secure
* World takes control of display and input.
*
* Return: must return 0 on success, non-zero otherwise.
*/
uint32_t __weak hal_tui_deactivate(void)
{
/* Set linux TUI flag */
trustedui_set_mask(TRUSTEDUI_MODE_TUI_SESSION);
/*TODO: save touch/display state */
trustedui_set_mask(TRUSTEDUI_MODE_VIDEO_SECURED|
TRUSTEDUI_MODE_INPUT_SECURED);
return 0;
}
/**
* hal_tui_activate() - restore Normal World display and input after a TUI
* session
*
* This function should enable Normal World display and, if necessary, Normal
* World input. It is called after a TUI session, after the Secure World has
* released the display and input.
*
* Return: must return 0 on success, non-zero otherwise.
*/
uint32_t __weak hal_tui_activate(void)
{
/* Protect NWd */
trustedui_clear_mask(TRUSTEDUI_MODE_VIDEO_SECURED|
TRUSTEDUI_MODE_INPUT_SECURED);
/*TODO: restore touch/display state */
trustedui_set_mode(TRUSTEDUI_MODE_OFF);
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef _TUI_HAL_H_
#define _TUI_HAL_H_
#include <linux/types.h>
uint32_t hal_tui_init(void);
void hal_tui_exit(void);
uint32_t hal_tui_deactivate(void);
uint32_t hal_tui_activate(void);
#endif
+79
View File
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/types.h>
#include <linux/completion.h>
#include <linux/slab.h>
#include "tee_wait_queue.h"
struct tee_wait_queue {
struct list_head link;
struct completion comp;
u32 key;
};
void tee_wait_queue_init(struct tee_wait_queue_private *priv)
{
mutex_init(&priv->mu);
INIT_LIST_HEAD(&priv->db);
}
EXPORT_SYMBOL(tee_wait_queue_init);
void tee_wait_queue_exit(struct tee_wait_queue_private *priv)
{
mutex_destroy(&priv->mu);
}
EXPORT_SYMBOL(tee_wait_queue_exit);
static struct tee_wait_queue *tee_wait_queue_get(struct device *dev,
struct tee_wait_queue_private *priv, u32 key)
{
struct tee_wait_queue *w;
mutex_lock(&priv->mu);
list_for_each_entry(w, &priv->db, link)
if (w->key == key)
goto out;
w = kmalloc(sizeof(struct tee_wait_queue), GFP_KERNEL);
if (!w)
goto out;
init_completion(&w->comp);
w->key = key;
list_add_tail(&w->link, &priv->db);
out:
mutex_unlock(&priv->mu);
return w;
}
void tee_wait_queue_sleep(struct device *dev,
struct tee_wait_queue_private *priv, u32 key)
{
struct tee_wait_queue *w = tee_wait_queue_get(dev, priv, key);
if (!w)
return;
wait_for_completion(&w->comp);
mutex_lock(&priv->mu);
list_del(&w->link);
mutex_unlock(&priv->mu);
kfree(w);
}
EXPORT_SYMBOL(tee_wait_queue_sleep);
void tee_wait_queue_wakeup(struct device *dev,
struct tee_wait_queue_private *priv, u32 key)
{
struct tee_wait_queue *w = tee_wait_queue_get(dev, priv, key);
if (!w)
return;
complete(&w->comp);
}
EXPORT_SYMBOL(tee_wait_queue_wakeup);
+25
View File
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_WAIT_QUEUE_H
#define TEE_WAIT_QUEUE_H
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/device.h>
struct tee_wait_queue_private {
struct mutex mu;
struct list_head db;
};
void tee_wait_queue_init(struct tee_wait_queue_private *priv);
void tee_wait_queue_exit(struct tee_wait_queue_private *priv);
void tee_wait_queue_sleep(struct device *dev,
struct tee_wait_queue_private *priv, u32 key);
void tee_wait_queue_wakeup(struct device *dev,
struct tee_wait_queue_private *priv, u32 key);
#endif /*TEE_WAIT_QUEUE_H*/