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
config TRUSTKERNEL_TEE_SUPPORT
bool "TrustKernel Trusted Execution Environment Support"
default n
help
Enable the support of TrustKernel TEE drivers.
All Trustkernel related functions will depend on this option.
Say Y if you would like yo enable this feature.
If unsure, say N.
config TRUSTKERNEL_TEE_FP_SUPPORT
bool "TrustKernel Trusted Execution Environment Finger Print Support"
depends on TRUSTKERNEL_TEE_SUPPORT
default n
help
Enable TrustKernel TEE clock management mechanism.
This feature allows TEE OS to manage clock already
used by Linux.
Say Y if you would like yo enable this feature.
If unsure, say Y.
config TRUSTKERNEL_TEE_RPMB_SUPPORT
bool "TrustKernel Trusted Execution Environment RPMB Support"
depends on TRUSTKERNEL_TEE_SUPPORT
default n
help
Enable the access of Replay Protected Memory Block
region for Trustkernel TEE.
Say Y if you would like yo enable this feature.
If unsure, say Y.
+5
View File
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2015-2019 TrustKernel Incorporated
obj-$(CONFIG_TRUSTKERNEL_TEE_SUPPORT) += core/
obj-$(CONFIG_TRUSTKERNEL_TEE_SUPPORT) += armtz/
+21
View File
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2015-2019 TrustKernel Incorporated
ifneq ($(strip $(M)),)
obj-m += tkcore_drv.o
else
obj-y += tkcore_drv.o
endif
M ?= $(srctree)/drivers/tee/tkcore
ccflags-y += -I$(M)/include/arm_common
ccflags-y += -I$(M)/include/linux
ccflags-y += -I$(M)/include
ccflags-y += -I$(M)/core
tkcore_drv-objs:= \
tee_tz_drv.o \
tee_smc_xfer.o \
tee_mem.o \
handle.o
+80
View File
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/slab.h>
#include <linux/string.h>
#include "handle.h"
/*
* Define the initial capacity of the database. It should be a low number
* multiple of 2 since some databases a likely to only use a few handles.
* Since the algorithm is to doubles up when growing it shouldn't cause a
* noticeable overhead on large databases.
*/
#define HANDLE_DB_INITIAL_MAX_PTRS 4
void handle_db_destroy(struct handle_db *db)
{
if (db) {
kfree(db->ptrs);
db->ptrs = NULL;
db->max_ptrs = 0;
}
}
int handle_get(struct handle_db *db, void *ptr)
{
unsigned int n;
void *p;
unsigned int new_max_ptrs;
if (!db || !ptr)
return -1;
/* Try to find an empty location */
for (n = 0; n < db->max_ptrs; n++) {
if (!db->ptrs[n]) {
db->ptrs[n] = ptr;
return n;
}
}
/* No location available, grow the ptrs array */
if (db->max_ptrs)
new_max_ptrs = db->max_ptrs * 2;
else
new_max_ptrs = HANDLE_DB_INITIAL_MAX_PTRS;
p = krealloc(db->ptrs, new_max_ptrs * sizeof(void *), GFP_KERNEL);
if (!p)
return -1;
db->ptrs = p;
memset(db->ptrs + db->max_ptrs, 0,
(new_max_ptrs - db->max_ptrs) * sizeof(void *));
db->max_ptrs = new_max_ptrs;
/* Since n stopped at db->max_ptrs there is an empty location there */
db->ptrs[n] = ptr;
return n;
}
void *handle_put(struct handle_db *db, int handle)
{
void *p;
if (!db || handle < 0 || (unsigned int) handle >= db->max_ptrs)
return NULL;
p = db->ptrs[handle];
db->ptrs[handle] = NULL;
return p;
}
void *handle_lookup(struct handle_db *db, int handle)
{
if (!db || handle < 0 || (unsigned int) handle >= db->max_ptrs)
return NULL;
return db->ptrs[handle];
}
+45
View File
@@ -0,0 +1,45 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_HANDLE_H
#define TEE_HANDLE_H
struct handle_db {
void **ptrs;
unsigned int max_ptrs;
};
#define HANDLE_DB_INITIALIZER { NULL, 0 }
/*
* Frees all internal data structures of the database, but does not free
* the db pointer. The database is safe to reuse after it's destroyed, it
* just be empty again.
*/
void handle_db_destroy(struct handle_db *db);
/*
* Allocates a new handle and assigns the supplied pointer to it,
* ptr must not be NULL.
* The function returns
* >= 0 on success and
* -1 on failure
*/
int handle_get(struct handle_db *db, void *ptr);
/*
* Deallocates a handle. Returns the associated pointer of the handle
* the the handle was valid or NULL if it's invalid.
*/
void *handle_put(struct handle_db *db, int handle);
/*
* Returns the associated pointer of the handle if the handle is a valid
* handle.
* Returns NULL on failure.
*/
void *handle_lookup(struct handle_db *db, int handle);
#endif /*HANDLE_H*/
+615
View File
@@ -0,0 +1,615 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/slab.h>
#include "tee_mem.h"
#define _DUMP_INFO_ALLOCATOR 0
#define USE_DEVM_ALLOC 1
#ifndef USE_DEVM_ALLOC
#define _KMALLOC(s, f) kmalloc(s, f)
#define _KFREE(a) kfree(a)
#else
#define _KMALLOC(s, f) devm_kzalloc(dev, s, f)
#define _KFREE(a) devm_kfree(dev, a)
#endif
/**
* \struct mem_chunk
* \brief Elementary descriptor of an allocated memory block
*
* \param node Node for linked list
* \param counter Reference counter
* (0 indicates that the block is not used/freed)
* \param size Total size in bytes
* \param paddr Physical base address
*
* Elementary memory block definition
*/
struct mem_chunk {
struct list_head node;
uint32_t counter;
size_t size;
unsigned long paddr;
};
/**
* \struct shm_pool
* \brief Main structure to describe a shared memory pool
*
* \param size Total size in bytes of the associated memory region
* \param vaddr Logical base address
* \param paddr Physical base address
* \param used Total size in bytes of the used memory
* \param mchunks List head for handle the elementary memory blocks
*
* Shared memory pool structure definition
*/
struct shm_pool {
struct mutex lock;
size_t size; /* Size of pool/heap memory segment */
size_t used; /* Number of bytes allocated */
void *vaddr; /* Associated Virtual address */
unsigned long paddr; /* Associated Physical address */
bool cached; /* true if pool is cacheable */
struct list_head mchunks; /* Head of memory chunk/block list */
};
#define __CALCULATE_RATIO_MEM_USED(a) (((a->used)*100)/(a->size))
/**
* \brief Dumps the information of the shared memory pool
*
* \param pool Pointer on the pool
* \param detailforced Flag to force the log for the detailed information
*
* Dump/log the meta data of the shared memory pool on the standard output.
*
*/
void tee_shm_pool_dump(struct device *dev, struct shm_pool *pool, bool forced)
{
struct mem_chunk *chunk;
if (WARN_ON(!dev || !pool))
return;
pr_info(
"%s() poolH(0x%p) pAddr=0x%p vAddr=0x%p size=%zu used=%zu(%zu%%)\n",
__func__,
(void *) pool,
(void *) pool->paddr,
(void *) pool->vaddr,
pool->size, pool->used, __CALCULATE_RATIO_MEM_USED(pool));
if ((pool->used != 0) || (forced == true)) {
pr_info(" \\ HEAD next:[0x%p] prev:[0x%p]\n",
(void *)pool->mchunks.next,
(void *)pool->mchunks.prev);
pr_info(
" |-[@] next prev pAddr size refCount\n");
list_for_each_entry(chunk, &pool->mchunks, node) {
pr_info(" | [0x%p] 0x%p 0x%p 0x%p %08zu %d\n",
(void *)chunk,
(void *)chunk->node.next,
(void *)chunk->node.prev,
(void *)chunk->paddr,
chunk->size, chunk->counter);
}
}
}
bool tee_shm_pool_is_cached(struct shm_pool *pool)
{
return pool->cached;
}
void tee_shm_pool_set_cached(struct shm_pool *pool)
{
pool->cached = true;
}
/**
* \brief Creates and returns a new shared memory pool manager structure
*
* \param shm_size Size of the associated memory chunk
* \param shm_vaddr Virtual/logical base address
* \param shm_paddr Physical base address
*
* \return Reference of the created shared memory pool manager
*
* Create and initialize a shared memory pool manager.
* The description of the memory region (shm_size, shm_vaddr, shm_paddr)
* which is passed should be a physically AND virtually contiguous
* (no check is performed by the function).
* If a error is detected returned pool is NULL.
*/
struct shm_pool *tee_shm_pool_create(struct device *dev, size_t shm_size,
void *shm_vaddr, unsigned long shm_paddr)
{
struct mem_chunk *chunk = NULL;
struct shm_pool *pool = NULL;
if (WARN_ON(!dev))
goto alloc_failed;
/* Alloc and initialize the shm_pool structure */
pool = _KMALLOC(sizeof(struct shm_pool), GFP_KERNEL);
if (!pool) {
pr_err("kmalloc <struct shm_pool> failed\n");
goto alloc_failed;
}
memset(pool, 0, sizeof(*pool));
mutex_init(&pool->lock);
mutex_lock(&pool->lock);
INIT_LIST_HEAD(&(pool->mchunks));
pool->size = shm_size;
pool->vaddr = shm_vaddr;
pool->paddr = shm_paddr;
/* Create the initial elementary memory chunk */
/* which handles the whole memory region */
chunk = _KMALLOC(sizeof(struct mem_chunk), GFP_KERNEL);
if (!chunk) {
pr_err("kmalloc <struct MemChunk> failed\n");
goto alloc_failed;
}
memset(chunk, 0, sizeof(*chunk));
chunk->paddr = shm_paddr;
chunk->size = shm_size;
/* Adds the new entry immediately after the list head */
list_add(&(chunk->node), &(pool->mchunks));
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 0)
tee_shm_pool_dump(dev, pool, true);
#endif
mutex_unlock(&pool->lock);
return pool;
alloc_failed:
if (chunk)
_KFREE(chunk);
if (pool)
_KFREE(pool);
return NULL;
}
/**
* Local helper function to check that the physical address is valid
*/
static inline int is_valid_paddr(struct shm_pool *pool, unsigned long paddr)
{
return (paddr >= pool->paddr && paddr < (pool->paddr + pool->size));
}
/**
* Local helper function to check that the virtual address is valid
*/
static inline int is_valid_vaddr(struct shm_pool *pool, void *vaddr)
{
return (vaddr >= pool->vaddr && vaddr < (pool->vaddr + pool->size));
}
/**
* \brief Destroy the shared memory pool manager
*
* \param pool Pointer on the pool
*
* Destroy a memory pool manager
*
*/
void tee_shm_pool_destroy(struct device *dev, struct shm_pool *pool)
{
struct mem_chunk *chunk;
if (WARN_ON(!dev || !pool))
return;
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 0)
tee_shm_pool_dump(dev, pool, true);
#endif
tee_shm_pool_reset(dev, pool);
chunk = list_first_entry(&pool->mchunks, struct mem_chunk, node);
_KFREE(chunk);
_KFREE(pool);
}
/**
* \brief Free all reserved chunk if any, and set pool at it initial state
*
* \param pool Pointer on the pool
*
*/
void tee_shm_pool_reset(struct device *dev, struct shm_pool *pool)
{
struct mem_chunk *chunk;
struct mem_chunk *tmp;
struct mem_chunk *first = NULL;
if (WARN_ON(!dev || !pool))
return;
mutex_lock(&pool->lock);
list_for_each_entry_safe(chunk, tmp, &pool->mchunks, node) {
if (first != NULL) {
pr_err("Free lost chunkH=0x%p\n", (void *)chunk);
list_del(&chunk->node);
_KFREE(chunk);
} else
first = chunk;
}
first->counter = 0;
first->paddr = pool->paddr;
first->size = pool->size;
pool->used = 0;
mutex_unlock(&pool->lock);
}
/**
* \brief Return the logical address
*
* \param pool Pointer on the pool
* \param paddr Physical address
*
* \return Virtual/logical address
*
* Return the associated virtual/logical address. The address should be inside
* the range of addresses managed by the shm pool.
*
*/
void *tee_shm_pool_p2v(struct device *dev, struct shm_pool *pool,
unsigned long paddr)
{
unsigned long offset;
void *p;
if (WARN_ON(!dev || !pool))
return NULL;
mutex_lock(&pool->lock);
if (!is_valid_paddr(pool, paddr)) {
mutex_unlock(&pool->lock);
pr_err("%s() paddr=0x%p not in the shm pool\n",
__func__,
(void *) paddr);
return NULL;
}
offset = paddr - pool->paddr;
p = (void *)((unsigned long)pool->vaddr + offset);
mutex_unlock(&pool->lock);
return p;
}
/**
* \brief Return the physical address
*
* \param pool Pointer on the pool
* \param vaddr Logical/Virtual address
*
* \return Physical address
*
* Return the associated physical address. The address should be inside
* the range of addresses managed by the pool.
*
*/
unsigned long tee_shm_pool_v2p(struct device *dev, struct shm_pool *pool,
void *vaddr)
{
unsigned long offset, p;
if (WARN_ON(!dev || !pool))
return 0UL;
mutex_lock(&pool->lock);
if (!is_valid_vaddr(pool, vaddr)) {
pr_err("%s() vaddr=0x%p not in shm pool\n",
__func__, (void *) vaddr);
mutex_unlock(&pool->lock);
return 0UL;
}
offset = vaddr - pool->vaddr;
p = pool->paddr + offset;
mutex_unlock(&pool->lock);
return p;
}
/**
* \brief Allocate a new block of memory
*
* \param pool Pointer on the pool
* \param size Expected size (in byte)
* \param alignment Alignment constraint (in byte)
*
* \return Physical base address of the allocated block
*
* Allocate a memory chunk inside the memory region managed by the pool.
*
*/
unsigned long tkcore_shm_pool_alloc(struct device *dev,
struct shm_pool *pool,
size_t size, size_t alignment)
{
struct mem_chunk *chunk;
struct mem_chunk *betterchunk = NULL;
struct mem_chunk *prev_chunk = NULL;
struct mem_chunk *next_chunk = NULL;
unsigned long begAddr;
unsigned long endAddr;
if (WARN_ON(!dev || !pool))
return 0UL;
/* Align on cache line of the target */
/* \todo(jmd) Should be defined by a global target specific parameter */
/* size = (size + (32-1)) & ~(32-1) */
if (ALIGN(size, 0x20) < size)
goto failed_out;
if (alignment == 0)
alignment = 1;
size = ALIGN(size, 0x20);
alignment = ALIGN(alignment, 0x20);
if (size > (pool->size - pool->used))
goto failed_out;
mutex_lock(&pool->lock);
/**
* Algorithm: Smallest waste (best fit):
* We choose the block that has the
* smallest waste. In other words we choose
* the block so that
* size(b) - size is as small as possible.
*/
list_for_each_entry(chunk, &pool->mchunks, node) {
if (chunk->counter == 0) { /* Free chunk */
begAddr = ALIGN(chunk->paddr, alignment);
endAddr = begAddr + size;
if (begAddr >= chunk->paddr
&& endAddr <= (chunk->paddr + chunk->size)
&& (betterchunk == NULL
/* Always split smaller block */
|| chunk->size < betterchunk->size))
betterchunk = chunk;
}
}
/**
* Update the linked list
*/
if (betterchunk != NULL) {
prev_chunk = _KMALLOC(sizeof(struct mem_chunk), GFP_KERNEL);
next_chunk = _KMALLOC(sizeof(struct mem_chunk), GFP_KERNEL);
if ((!prev_chunk) || (!next_chunk))
goto failed_out_unlock;
begAddr = ALIGN(betterchunk->paddr, alignment);
endAddr = begAddr + size;
if (betterchunk->paddr < begAddr) {
/* memory between begin of chunk and begin
* of created memory => create a free chunk
*/
prev_chunk->counter = 0;
prev_chunk->paddr = betterchunk->paddr;
prev_chunk->size = begAddr - betterchunk->paddr;
betterchunk->paddr = begAddr;
betterchunk->size -= prev_chunk->size;
list_add_tail(&(prev_chunk->node),
&(betterchunk->node));
prev_chunk = NULL;
} else
_KFREE(prev_chunk);
if (betterchunk->paddr + betterchunk->size > endAddr) {
/* memory between end of chunk and end of
* created memory => create a free chunk
*/
next_chunk->counter = 0;
next_chunk->paddr = endAddr;
next_chunk->size = betterchunk->size - size;
betterchunk->size = size;
list_add(&(next_chunk->node), &(betterchunk->node));
next_chunk = NULL;
} else
_KFREE(next_chunk);
betterchunk->counter = 1;
pool->used += size;
mutex_unlock(&pool->lock);
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 1)
tee_shm_pool_dump(dev, pool, false);
#endif
return betterchunk->paddr;
}
failed_out_unlock:
mutex_unlock(&pool->lock);
failed_out:
if (prev_chunk)
_KFREE(prev_chunk);
if (next_chunk)
_KFREE(next_chunk);
pr_err(
"%s() FAILED, size=0x%zx, align=0x%zx free=%zu\n",
__func__, size, alignment, pool->size - pool->used);
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 1)
tee_shm_pool_dump(dev, pool, true);
#endif
return 0UL;
}
/**
* \brief Release a allocated block of memory
*
* \param pool Pointer on the pool
* \param paddr Physical @ of the block which must be released
* \param size Reference to return the size of the block
*
* Free a allocated memory block inside
* the memory region managed by the pool.
*
*/
int tkcore_shm_pool_free(struct device *dev, struct shm_pool *pool,
unsigned long paddr, size_t *size)
{
struct mem_chunk *chunk;
if (WARN_ON(!dev || !pool))
return -EINVAL;
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 1)
tee_shm_pool_dump(dev, pool, false);
#endif
mutex_lock(&pool->lock);
if (!is_valid_paddr(pool, paddr))
goto out_failed;
list_for_each_entry(chunk, &pool->mchunks, node) {
if (chunk->paddr == paddr) {
if (size != NULL)
*size = chunk->size;
if (chunk->counter == 0) {
pr_warn(
"< %s() WARNING, paddr=0x%p already released\n",
__func__, (void *) paddr);
return -EINVAL;
} else if (--chunk->counter == 0) {
pool->used -= chunk->size;
/* Merge with previous */
if (chunk->node.prev != &pool->mchunks) {
struct mem_chunk *prev =
list_entry(chunk->node.prev,
struct mem_chunk, node);
if (prev->counter == 0) {
prev->size += chunk->size;
list_del(&chunk->node);
_KFREE(chunk);
chunk = prev;
}
}
/* Merge with next */
if (chunk->node.next != &pool->mchunks) {
struct mem_chunk *next =
list_entry(chunk->node.next,
struct mem_chunk, node);
if (next->counter == 0) {
chunk->size += next->size;
list_del(&next->node);
_KFREE(next);
}
}
mutex_unlock(&pool->lock);
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 1)
tee_shm_pool_dump(dev, pool, false);
#endif
return 0;
} else {
mutex_unlock(&pool->lock);
return 1;
}
}
}
out_failed:
mutex_unlock(&pool->lock);
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 1)
tee_shm_pool_dump(dev, pool, false);
#endif
pr_err(
"< %s() FAILED, pAddr=0x%p not found\n",
__func__, (void *) paddr);
return -EINVAL;
}
/**
* \brief Increase the reference count of the memory chunk
*
* \param pool Pointer on the pool
* \param paddr Physical address
*
* \return true if successful (false otherwise)
*
* Increment the reference count of the allocated block of memory.
* paddr should a valid address returned by the tkcore_shm_pool_alloc().
*
*/
bool tee_shm_pool_incref(struct device *dev, struct shm_pool *pool,
unsigned long paddr)
{
struct mem_chunk *chunk;
if (WARN_ON(!dev || !pool))
return false;
mutex_lock(&pool->lock);
if (!is_valid_paddr(pool, paddr))
goto out_failed;
list_for_each_entry(chunk, &pool->mchunks, node) {
if (chunk->paddr == paddr) {
chunk->counter++;
#if defined(_DUMP_INFO_ALLOCATOR) && (_DUMP_INFO_ALLOCATOR > 0)
tee_shm_pool_dump(dev, pool, false);
#endif
mutex_unlock(&pool->lock);
return true;
}
}
out_failed:
mutex_unlock(&pool->lock);
pr_err(
"%s() FAILED, pAddr=%p is not a valid @\n",
__func__, (void *) paddr);
return false;
}
+42
View File
@@ -0,0 +1,42 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_MEM_H
#define TEE_MEM_H
#include <linux/types.h>
#include <linux/device.h>
struct shm_pool;
struct shm_pool *tee_shm_pool_create(struct device *dev, size_t shm_size,
void *shm_vaddr, unsigned long shm_paddr);
void tee_shm_pool_destroy(struct device *dev, struct shm_pool *pool);
void *tee_shm_pool_p2v(struct device *dev, struct shm_pool *pool,
unsigned long paddr);
unsigned long tee_shm_pool_v2p(struct device *dev, struct shm_pool *pool,
void *vaddr);
unsigned long tkcore_shm_pool_alloc(struct device *dev,
struct shm_pool *pool, size_t size, size_t alignment);
int tkcore_shm_pool_free(struct device *dev, struct shm_pool *pool,
unsigned long paddr, size_t *size);
bool tee_shm_pool_incref(struct device *dev, struct shm_pool *pool,
unsigned long paddr);
void tee_shm_pool_dump(struct device *dev, struct shm_pool *pool, bool forced);
void tee_shm_pool_reset(struct device *dev, struct shm_pool *pool);
bool tee_shm_pool_is_cached(struct shm_pool *pool);
void tee_shm_pool_set_cached(struct shm_pool *pool);
#endif
+235
View File
@@ -0,0 +1,235 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#include <linux/spinlock.h>
#include <linux/wait.h>
#include <linux/semaphore.h>
#include <linux/slab.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/list.h>
#include <linux/atomic.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/version.h>
#include <linux/tee_clkmgr.h>
#include <tee_kernel_lowlevel_api.h>
#include <arm_common/teesmc.h>
struct tee_smc_xfer_ctl {
/* number of cmds that are not
* processed by `tee_smc_daemon` kthreads
*/
atomic_t nr_unbound_cmds;
/* guarantee the mutual-exlusiveness of smc */
struct mutex xfer_lock;
/* cmds that wait for
* available TEE thread slots
*/
atomic_t nr_waiting_cmds;
struct completion smc_comp;
/* statistics information */
s64 max_smc_time;
s64 max_cmd_time;
};
static struct tee_smc_xfer_ctl tee_smc_xfer_ctl;
static inline void trace_tee_smc(struct tee_smc_xfer_ctl *ctl, int rv,
s64 time_start, s64 time_end)
{
s64 duration = time_end - time_start;
if (duration > 1000000LL) {
pr_warn("WARNING SMC[0x%x] %sDURATION %lld us\n", rv,
rv == TEESMC_RPC_FUNC_IRQ ? "IRQ " : "", duration);
}
/* we needn't handle concurrency here. */
if (duration > ctl->max_smc_time)
ctl->max_smc_time = duration;
}
static inline void trace_tee_smc_done(struct tee_smc_xfer_ctl *ctl,
s64 time_start,
s64 time_end)
{
s64 duration = time_end - time_start;
if (duration > ctl->max_cmd_time)
ctl->max_cmd_time = duration;
}
/* return 0 for nonpreempt rpc, 1 for others */
static int handle_nonpreempt_rpc(struct smc_param *p)
{
uint32_t func_id = TEESMC_RETURN_GET_RPC_FUNC(p->a0);
/* for compatibility with legacy tee-os which
* does not support clkmgr
*/
if (func_id == T6SMC_RPC_CLKMGR_LEGACY_CMD) {
p->a1 = tee_clkmgr_handle(p->a1, p->a2);
return 0;
}
if (func_id != T6SMC_RPC_NONPREEMPT_CMD)
return 1;
switch (T6SMC_RPC_NONPREEMPT_GET_FUNC(p->a0)) {
case T6SMC_RPC_CLKMGR_CMD:
/* compatible with old interface */
p->a1 = tee_clkmgr_handle(p->a1,
(p->a1 & TEE_CLKMGR_TOKEN_NOT_LEGACY) ?
p->a2 : (p->a2 | TEE_CLKMGR_OP_ENABLE));
break;
default:
pr_err("Unknown non-preempt rpc cmd: 0x%llx\n",
(unsigned long long) p->a0);
}
return 0;
}
static void tee_smc_work(struct tee_smc_xfer_ctl *ctl, struct smc_param *p)
{
s64 start, end;
u64 rv = p->a0 == TEESMC32_FASTCALL_WITH_ARG ?
TEESMC32_FASTCALL_RETURN_FROM_RPC :
TEESMC32_CALL_RETURN_FROM_RPC;
/* we need to place atomic_inc ahead of xfer_lock
* in order that an smc-execution thread can
* see other pending commands without releasing
* xfer_lock
*/
atomic_inc(&ctl->nr_unbound_cmds);
mutex_lock(&ctl->xfer_lock);
start = ktime_to_us(ktime_get());
while (1) {
s64 a = ktime_to_us(ktime_get()), b;
tee_smc_call(p);
b = ktime_to_us(ktime_get());
trace_tee_smc(ctl, TEESMC_RETURN_GET_RPC_FUNC(p->a0), a, b);
if (!TEESMC_RETURN_IS_RPC(p->a0))
goto smc_return;
if (handle_nonpreempt_rpc(p)) {
if (TEESMC_RETURN_GET_RPC_FUNC(p->a0)
!= TEESMC_RPC_FUNC_IRQ)
goto smc_return;
}
p->a0 = rv;
}
smc_return:
atomic_dec(&ctl->nr_unbound_cmds);
mutex_unlock(&ctl->xfer_lock);
end = ktime_to_us(ktime_get());
trace_tee_smc_done(ctl, start, end);
}
static inline void __smc_xfer(struct tee_smc_xfer_ctl *ctl, struct smc_param *p)
{
tee_smc_work(ctl, p);
}
static int platform_bl_init(struct tee_smc_xfer_ctl *ctl)
{
return 0;
}
static void platform_bl_deinit(struct tee_smc_xfer_ctl *ctl) { }
static void xfer_enqueue_waiters(struct tee_smc_xfer_ctl *ctl)
{
/*TODO handle too long time of waiting */
atomic_inc(&ctl->nr_waiting_cmds);
wait_for_completion(&ctl->smc_comp);
}
static void xfer_dequeue_waiters(struct tee_smc_xfer_ctl *ctl)
{
if (atomic_dec_if_positive(&ctl->nr_waiting_cmds) >= 0)
complete(&ctl->smc_comp);
}
void __call_tee(struct smc_param *p)
{
/* NOTE!!! we remove the e_lock_teez(ptee) here !!!! */
#ifdef ARM64
uint64_t orig_a0 = p->a0;
#else
uint32_t orig_a0 = p->a0;
#endif
for (;;) {
__smc_xfer(&tee_smc_xfer_ctl, p);
if (p->a0 == TEESMC_RETURN_ETHREAD_LIMIT) {
xfer_enqueue_waiters(&tee_smc_xfer_ctl);
p->a0 = orig_a0;
} else {
if (!TEESMC_RETURN_IS_RPC(p->a0))
xfer_dequeue_waiters(&tee_smc_xfer_ctl);
break;
}
}
}
inline void smc_xfer(struct smc_param *p)
{
__smc_xfer(&tee_smc_xfer_ctl, p);
}
int tee_init_smc_xfer(void)
{
int r;
struct tee_smc_xfer_ctl *ctl = &tee_smc_xfer_ctl;
atomic_set(&ctl->nr_unbound_cmds, 0);
mutex_init(&ctl->xfer_lock);
atomic_set(&ctl->nr_waiting_cmds, 0);
init_completion(&ctl->smc_comp);
ctl->max_smc_time = 0LL;
ctl->max_cmd_time = 0LL;
r = platform_bl_init(ctl);
if (r < 0)
goto err;
return 0;
err:
return r;
}
void tee_exit_smc_xfer(void)
{
struct tee_smc_xfer_ctl *ctl = &tee_smc_xfer_ctl;
platform_bl_deinit(ctl);
}
+17
View File
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_SMC_XFER_H
#define TEE_SMC_XFER_H
struct smc_param;
void smc_xfer(struct smc_param *p);
void __call_tee(struct smc_param *p);
int tee_init_smc_xfer(void);
void tee_exit_smc_xfer(void);
#endif
File diff suppressed because it is too large. Load diff
+263
View File
@@ -0,0 +1,263 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_ARMV7_OP_H__
#define __TEE_ARMV7_OP_H__
enum t_issw_service_id {
/*
* ("SSAPI_PRE_INIT_SERV")
*/
SSAPI_PRE_INIT_SERV = 1,
/*
* ("SSAPI_POST_SPEEDUP_INIT_SERV")
* Reserved, Not used
*/
SSAPI_POST_SPEEDUP_INIT_SERV = 2,
/*
* ("SSAPI_ISSW_IMPORT_SERV")
*/
SSAPI_ISSW_IMPORT_SERV = 3,
/*
* ("SSAPI_RET_FROM_INT_SERV")
*/
SSAPI_RET_FROM_INT_SERV = 4,
/*
* ("SSAPI_RET_FROM_RPC_SERV")
*/
SSAPI_RET_FROM_RPC_SERV = 5,
/*
* "ISSWAPI_ISSW_EXECUTE_SERV" is linked to ROM code
* ("SSAPI_ISSW_EXECUTE_SERV")
*/
ISSWAPI_ISSW_EXECUTE_SERV = 6,
ISSWAPI_PROT_APPL_MSG_SEND = 0x10000000,
ISSWAPI_EXTERNAL_CODE_CHECK = 0x10000001,
ISSWAPI_SECURE_LOAD = 0x10000002,
ISSWAPI_ISSW_REIMPORT_PUB_KEYS = 0x10000003,
/* Accessible only on request */
ISSWAPI_WRITE_L2CC = 0x10000004,
ISSWAPI_WRITE_CP15_SCTLR = 0x10000005,
ISSWAPI_READ_CP15_SCTLR = 0x10000006,
ISSWAPI_WRITE_CP15_ACTLR = 0x10000007,
ISSWAPI_READ_CP15_ACTLR = 0x10000008,
ISSWAPI_WRITE_CP15_DIAGR = 0x10000009,
ISSWAPI_READ_CP15_DIAGR = 0x1000000A,
ISSWAPI_EXECUTE_TA = 0x11000001,
ISSWAPI_CLOSE_TA = 0x11000002,
ISSWAPI_FLUSH_BOOT_CODE = 0x11000003,
/* Generic, restricted to be used by u-boot */
ISSWAPI_VERIFY_SIGNED_HEADER = 0x11000005,
ISSWAPI_VERIFY_HASH = 0x11000006,
/* 8500 only, restricted to be used by u-boot */
ISSWAPI_GET_RT_FLAGS = 0x11000007,
/* For TEE Client API 1.0 */
ISSWAPI_TEEC_OPEN_SESSION = 0x11000008,
ISSWAPI_TEEC_CLOSE_SESSION = 0x11000009,
ISSWAPI_TEEC_INVOKE_COMMAND = 0x1100000a,
ISSWAPI_REGISTER_RPC = 0x1100000b, /* this is NOT a GP TEE API ! */
ISSWAPI_SET_SEC_DDR = 0x1100000c, /* this is NOT a GP TEE API ! */
ISSWAPI_TEEC_CANCEL_COMMAND = 0x1100000d,
ISSWAPI_TEEC_REGISTER_MEMORY = 0x1100000e,
ISSWAPI_TEEC_UNREGISTER_MEMORY = 0x1100000f,
/* Internal command */
ISSWAPI_TEE_DEINIT_CPU = 0x11000010,
ISSWAPI_TEE_CRASH_CPU = 0x11000011,
ISSWAPI_TEE_SET_CORE_TRACE_LEVEL = 0x11000012,
ISSWAPI_TEE_GET_CORE_TRACE_LEVEL = 0x11000013,
ISSWAPI_TEE_SET_TA_TRACE_LEVEL = 0x11000014,
ISSWAPI_TEE_GET_TA_TRACE_LEVEL = 0x11000015,
ISSWAPI_TEE_GET_CORE_STATUS = 0x11000016,
ISSWAPI_TEE_FLUSH_CACHE = 0x11000017,
ISSWAPI_REGISTER_DEF_SHM = 0x11000020,
ISSWAPI_UNREGISTER_DEF_SHM = 0x11000021,
ISSWAPI_REGISTER_IRQFWD = 0x11000022,
ISSWAPI_UNREGISTER_IRQFWD = 0x11000023,
ISSWAPI_GET_SHM_START = 0x11000024,
ISSWAPI_GET_SHM_SIZE = 0x11000025,
ISSWAPI_GET_SHM_CACHED = 0x11000026,
ISSWAPI_ENABLE_L2CC_MUTEX = 0x20000000,
ISSWAPI_DISABLE_L2CC_MUTEX = 0x20000001,
ISSWAPI_GET_L2CC_MUTEX = 0x20000002,
ISSWAPI_SET_L2CC_MUTEX = 0x20000003,
ISSWAPI_LOAD_TEE = 0x20000004,
};
/*
* tee_msg_send - generic part of the msg sent to the TEE
*/
struct tee_msg_send {
unsigned int service;
};
/*
* tee_msg_recv - default strcutre of TEE service output message
*/
struct tee_msg_recv {
int duration;
uint32_t res;
uint32_t origin;
};
/*
* tee_register_irqfwd_xxx - (un)register callback for interrupt forwarding
*/
struct tee_register_irqfwd_send {
struct tee_msg_send header;
struct {
unsigned long cb;
} data;
};
struct tee_register_irqfwd_recv {
struct tee_msg_recv header;
};
/*
* tee_get_l2cc_mutex - input/output argument structures
*/
struct tee_get_l2cc_mutex_send {
struct tee_msg_send header;
};
struct tee_get_l2cc_mutex_recv {
struct tee_msg_recv header;
struct {
unsigned long paddr;
} data;
};
/**
* struct tee_identity - Represents the identity of the client
* @login: Login id
* @uuid: UUID as defined above
*/
struct tee_identity {
uint32_t login;
struct TEEC_UUID uuid;
};
/*
* tee_open_session_data - input arg structure for TEE open session service
*/
struct tee_open_session_data {
struct ta_signed_header_t *ta;
struct TEEC_UUID uuid;
uint32_t param_types;
struct TEEC_Value params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
struct tee_identity client_id;
uint32_t params_flags[TEEC_CONFIG_PAYLOAD_REF_COUNT];
};
/*
* tee_open_session_send - input arg msg for TEE open session service
*/
struct tee_open_session_send {
struct tee_msg_send header;
struct tee_open_session_data data;
};
/*
* tee_open_session_recv - output arg structure for TEE open session service
*/
struct tee_open_session_recv {
struct tee_msg_recv header;
uint32_t sess;
struct TEEC_Value params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
};
/*
* tee_invoke_command_data - input arg structure for TEE invoke cmd service
*/
struct tee_invoke_command_data {
uint32_t sess;
uint32_t cmd;
uint32_t param_types;
struct TEEC_Value params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
uint32_t params_flags[TEEC_CONFIG_PAYLOAD_REF_COUNT];
};
struct tee_invoke_command_send {
struct tee_msg_send header;
struct tee_invoke_command_data data;
};
/*
* tee_invoke_command_recv - output arg structure for TEE invoke cmd service
*/
struct tee_invoke_command_recv {
struct tee_msg_recv header;
struct TEEC_Value params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
};
/*
* tee_cancel_command_data - input arg structure for TEE cancel service
*/
struct tee_cancel_command_data {
uint32_t sess;
};
/*
* tee_cancel_command_send - input msg structure for TEE cancel service
*/
struct tee_cancel_command_send {
struct tee_msg_send header;
struct tee_cancel_command_data data;
};
/*
* tee_close_session_data - input arg structure for TEE close session service
*/
struct tee_close_session_data {
uint32_t sess;
};
/*
* tee_close_session_send - input arg msg for TEE close session service
*/
struct tee_close_session_send {
struct tee_msg_send header;
struct tee_close_session_data data;
};
/*
* tee_register_rpc_send_data - input arg structure for TEE register rpc service
*/
struct tee_register_rpc_send_data {
uint32_t fnk;
uint32_t bf;
uint32_t nbr_bf;
};
/*
* tee_register_rpc_send - input msg structure for TEE register rpc service
*/
struct tee_register_rpc_send {
struct tee_msg_send header;
struct tee_register_rpc_send_data data;
};
/*
* tee_core_status_out - output arg structure for TEE status service
*/
#define TEEC_STATUS_MSG_SIZE 80
struct tee_core_status_out {
struct tee_msg_recv header;
char raw[TEEC_STATUS_MSG_SIZE];
};
#endif /* __TEE_ARMV7_OP_H__ */
+27
View File
@@ -0,0 +1,27 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_TZ_PRIV__
#define __TEE_TZ_PRIV__
#include <linux/tee_kernel_lowlevel_api.h>
struct tee;
struct shm_pool;
struct tee_rpc_bf;
struct tee_tz {
bool started;
struct tee *tee;
unsigned long shm_paddr;
void *shm_vaddr;
struct shm_pool *shm_pool;
void *tz_outer_cache_mutex;
struct tee_rpc_bf *rpc_buffers;
bool shm_cached;
struct tee_wait_queue_private wait_queue;
};
#endif /* __TEE_TZ_PRIV__ */
+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*/
@@ -0,0 +1,710 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEESMC_H
#define TEESMC_H
#ifndef ASM
/*
* This section depends on uint64_t, uint32_t uint8_t already being
* defined. Since this file is used in several different environments
* (secure world OS and normal world Linux kernel to start with) where
* stdint.h may not be available it's the responsibility of the one
* including this file to provide those types.
*/
/*
* Trusted OS SMC interface.
*
* The SMC interface follows SMC Calling Convention
* (ARM_DEN0028A_SMC_Calling_Convention).
*
* The primary objective of this API is to provide a transport layer on
* which a Global Platform compliant TEE interfaces can be deployed. But the
* interface can also be used for other implementations.
*
* This file is divided in two parts.
* Part 1 deals with passing parameters to Trusted Applications running in
* a trusted OS in secure world.
* Part 2 deals with the lower level handling of the SMC.
*/
/*
*******************************************************************************
* Part 1 - passing parameters to Trusted Applications
*******************************************************************************
*/
/*
* Same values as TEE_PARAM_* from TEE Internal API
*/
#define TEESMC_ATTR_TYPE_NONE 0
#define TEESMC_ATTR_TYPE_VALUE_INPUT 1
#define TEESMC_ATTR_TYPE_VALUE_OUTPUT 2
#define TEESMC_ATTR_TYPE_VALUE_INOUT 3
#define TEESMC_ATTR_TYPE_MEMREF_INPUT 5
#define TEESMC_ATTR_TYPE_MEMREF_OUTPUT 6
#define TEESMC_ATTR_TYPE_MEMREF_INOUT 7
#define TEESMC_ATTR_TYPE_MASK 0x7
/*
* Meta parameter to be absorbed by the Secure OS and not passed
* to the Trusted Application.
*
* One example of this is a struct teesmc_meta_open_session which
* is added to TEESMC{32,64}_CMD_OPEN_SESSION.
*/
#define TEESMC_ATTR_META 0x8
/*
* Used as an indication from normal world of compatible cache usage.
* 'I' stands for inner cache and 'O' for outer cache.
*/
#define TEESMC_ATTR_CACHE_I_NONCACHE 0x0
#define TEESMC_ATTR_CACHE_I_WRITE_THR 0x1
#define TEESMC_ATTR_CACHE_I_WRITE_BACK 0x2
#define TEESMC_ATTR_CACHE_O_NONCACHE 0x0
#define TEESMC_ATTR_CACHE_O_WRITE_THR 0x4
#define TEESMC_ATTR_CACHE_O_WRITE_BACK 0x8
#define TEESMC_ATTR_CACHE_NONCACHE (TEESMC_ATTR_CACHE_I_NONCACHE | \
TEESMC_ATTR_CACHE_O_NONCACHE)
#define TEESMC_ATTR_CACHE_DEFAULT (TEESMC_ATTR_CACHE_I_WRITE_BACK | \
TEESMC_ATTR_CACHE_O_WRITE_BACK)
#define TEESMC_ATTR_CACHE_SHIFT 4
#define TEESMC_ATTR_CACHE_MASK 0xf
#define TEESMC_CMD_OPEN_SESSION 0
#define TEESMC_CMD_INVOKE_COMMAND 1
#define TEESMC_CMD_CLOSE_SESSION 2
#define TEESMC_CMD_CANCEL 3
/**
* struct teesmc32_param_memref - memory reference
* @buf_ptr: Address of the buffer
* @size: Size of the buffer
*
* Secure and normal world communicates pointer via physical address instead of
* the virtual address with is usually used for pointers. This is because
* Secure and normal world has completely independent memory mapping. Normal
* world can even have a hypervisor which need to translate the guest
* physical address (AKA IPA in ARM lingo) to a real physical address
* before passing the structure to secure world.
*/
struct teesmc32_param_memref {
uint32_t buf_ptr;
uint32_t size;
};
/**
* struct teesmc32_param_memref - memory reference
* @buf_ptr: Address of the buffer
* @size: Size of the buffer
*
* See description of struct teesmc32_param_memref.
*/
struct teesmc64_param_memref {
uint64_t buf_ptr;
uint64_t size;
};
/**
* struct teesmc32_param_value - values
* @a: first value
* @b: second value
*/
struct teesmc32_param_value {
uint32_t a;
uint32_t b;
};
/**
* struct teesmc64_param_value - values
* @a: first value
* @b: second value
*/
struct teesmc64_param_value {
uint64_t a;
uint64_t b;
};
/**
* struct teesmc32_param - parameter
* @attr: attributes
* @memref: a memory reference
* @value: a value
*
* attr & TEESMC_ATTR_TYPE_MASK indicates if memref or value is used in the
* union. TEESMC_ATTR_TYPE_VALUE_* indicates value and
* TEESMC_ATTR_TYPE_MEMREF_* indicates memref. TEESMC_ATTR_TYPE_NONE
* indicates that none of the members are used.
*/
struct teesmc32_param {
uint32_t attr;
union {
struct teesmc32_param_memref memref;
struct teesmc32_param_value value;
} u;
};
/**
* struct teesmc64_param - parameter
* @attr: attributes
* @memref: a memory reference
* @value: a value
*
* See description of union teesmc32_param.
*/
struct teesmc64_param {
uint64_t attr;
union {
struct teesmc64_param_memref memref;
struct teesmc64_param_value value;
} u;
};
/**
* struct teesmc32_arg - SMC argument for Trusted OS
* @cmd: Command, one of TEESMC_CMD_*
* @ta_func: Trusted Application function, specific to the Trusted Application,
* used if cmd == TEESMC_CMD_INVOKE_COMMAND
* @session: In parameter for all TEESMC_CMD_* except
* TEESMC_CMD_OPEN_SESSION where it's an output parameter instead
* @ret: return value
* @ret_origin: origin of the return value
* @num_params: number of parameters supplied to the OS Command
* @params: the parameters supplied to the OS Command
*
* All normal SMC calls to Trusted OS uses this struct. If cmd requires
* further information than what these field holds it can be passed as a
* parameter tagged as meta (setting the TEESMC_ATTR_META bit in
* corresponding param_attrs). This is used for TEESMC_CMD_OPEN_SESSION
* to pass a struct teesmc32_meta_open_session which is needed find the
* Trusted Application and to indicate the credentials of the client.
*/
struct teesmc32_arg {
uint32_t cmd;
uint32_t ta_func;
uint32_t session;
uint32_t ret;
uint32_t ret_origin;
uint32_t num_params;
/*
* Commented out elements used to visualize the layout dynamic part
* of the struct. Note that these fields are not available at all
* if num_params == 0.
*
* params is accessed through the macro TEESMC32_GET_PARAMS
*/
/* struct teesmc32_param params[num_params]; */
};
/**
* TEESMC32_GET_PARAMS - return pointer to union teesmc32_param *
*
* @x: Pointer to a struct teesmc32_arg
*
* Returns a pointer to the params[] inside a struct teesmc32_arg.
*/
#define TEESMC32_GET_PARAMS(x) \
(struct teesmc32_param *)(((struct teesmc32_arg *)(x)) + 1)
/**
* TEESMC32_GET_ARG_SIZE - return size of struct teesmc32_arg
*
* @num_params: Number of parameters embedded in the struct teesmc32_arg
*
* Returns the size of the struct teesmc32_arg together with the number
* of embedded parameters.
*/
#define TEESMC32_GET_ARG_SIZE(num_params) \
(sizeof(struct teesmc32_arg) + \
sizeof(struct teesmc32_param) * (num_params))
/**
* struct teesmc64_arg - SMC argument for Trusted OS
* @cmd: OS Command, one of TEESMC_CMD_*
* @ta_func: Trusted Application function, specific to the Trusted Application
* @session: In parameter for all TEESMC_CMD_* but
* TEESMC_CMD_OPEN_SESSION
* @ret: return value
* @ret_origin: origin of the return value
* @num_params: number of parameters supplied to the OS Command
* @params: the parameters supplied to the OS Command
*
* See description of struct teesmc32_arg.
*/
struct teesmc64_arg {
uint64_t cmd;
uint64_t ta_func;
uint64_t session;
uint64_t ret;
uint64_t ret_origin;
uint64_t num_params;
/*
* Commented out elements used to visualize the layout dynamic part
* of the struct. Note that these fields are not available at all
* if num_params == 0.
*
* params is accessed through the macro TEESMC64_GET_PARAMS
*/
/* struct teesmc64_param params[num_params]; */
};
/**
* TEESMC64_GET_PARAMS - return pointer to union teesmc64_param *
*
* @x: Pointer to a struct teesmc64_arg
*
* Returns a pointer to the params[] inside a struct teesmc64_arg.
*/
#define TEESMC64_GET_PARAMS(x) \
(struct teesmc64_param *)(((struct teesmc64_arg *)(x)) + 1)
/**
* TEESMC64_GET_ARG_SIZE - return size of struct teesmc64_arg
*
* @num_params: Number of parameters embedded in the struct teesmc64_arg
*
* Returns the size of the struct teesmc64_arg together with the number
* of embedded parameters.
*/
#define TEESMC64_GET_ARG_SIZE(num_params) \
(sizeof(struct teesmc64_arg) + \
sizeof(union teesmc64_param) * (num_params))
#define TEESMC_UUID_LEN 16
/**
* struct teesmc_meta_open_session - additional parameters for
* TEESMC32_CMD_OPEN_SESSION and
* TEESMC64_CMD_OPEN_SESSION
* @uuid: UUID of the Trusted Application
* @clnt_uuid: UUID of client
* @clnt_login: Login class of client, TEE_LOGIN_* if being Global Platform
* compliant
*
* This struct is passed in the first parameter as an input memref tagged
* as meta on an TEESMC{32,64}_CMD_OPEN_SESSION cmd. It's important
* that it really is the first parameter to make it easy for an eventual
* hypervisor to inspect and possibly update clnt_* values.
*/
struct teesmc_meta_open_session {
uint8_t uuid[TEESMC_UUID_LEN];
uint8_t clnt_uuid[TEESMC_UUID_LEN];
uint32_t clnt_login;
};
#endif /*!ASM*/
/*
*******************************************************************************
* Part 2 - low level SMC interaction
*******************************************************************************
*/
#define TEESMC_32 0
#define TEESMC_64 0x40000000
#define TEESMC_FAST_CALL 0x80000000
#define TEESMC_STD_CALL 0
#define TEESMC_OWNER_MASK 0x3F
#define TEESMC_OWNER_SHIFT 24
#define TEESMC_FUNC_MASK 0xFFFF
#define TEESMC_IS_FAST_CALL(smc_val) ((smc_val) & TEESMC_FAST_CALL)
#define TEESMC_IS_64(smc_val) ((smc_val) & TEESMC_64)
#define TEESMC_FUNC_NUM(smc_val) ((smc_val) & TEESMC_FUNC_MASK)
#define TEESMC_OWNER_NUM(smc_val) (((smc_val) >> TEESMC_OWNER_SHIFT) & \
TEESMC_OWNER_MASK)
#define TEESMC_CALL_VAL(type, calling_convention, owner, func_num) \
((type) | (calling_convention) | \
(((owner) & TEESMC_OWNER_MASK) << TEESMC_OWNER_SHIFT) |\
((func_num) & TEESMC_FUNC_MASK))
#define TEESMC_OWNER_ARCH 0
#define TEESMC_OWNER_CPU 1
#define TEESMC_OWNER_SIP 2
#define TEESMC_OWNER_OEM 3
#define TEESMC_OWNER_STANDARD 4
#define TEESMC_OWNER_TRUSTED_APP 48
#define TEESMC_OWNER_TRUSTED_OS 50
#define TEESMC_OWNER_TRUSTED_OS_API 63
/*
* Function specified by SMC Calling convention.
*/
#define TEESMC32_FUNCID_CALLS_COUNT 0xFF00
#define TEESMC32_CALLS_COUNT \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, \
TEESMC_OWNER_TRUSTED_OS_API, \
TEESMC32_FUNCID_CALLS_COUNT)
/*
* Function specified by SMC Calling convention
*
* Return one of the following UIDs if using API specified in this file
* without further extentions:
* 65cb6b93-af0c-4617-8ed6-644a8d1140f8 : Only 32 bit calls are supported
* 65cb6b93-af0c-4617-8ed6-644a8d1140f9 : Both 32 and 64 bit calls are supported
*/
#define TEESMC_UID_R0 0x65cb6b93
#define TEESMC_UID_R1 0xaf0c4617
#define TEESMC_UID_R2 0x8ed6644a
#define TEESMC_UID32_R3 0x8d1140f8
#define TEESMC_UID64_R3 0x8d1140f9
#define TEESMC32_FUNCID_CALLS_UID 0xFF01
#define TEESMC32_CALLS_UID \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, \
TEESMC_OWNER_TRUSTED_OS_API, \
TEESMC32_FUNCID_CALLS_UID)
/*
* Function specified by SMC Calling convention
*
* Returns 1.0 if using API specified in this file without further extentions.
*/
#define TEESMC_REVISION_MAJOR 1
#define TEESMC_REVISION_MINOR 0
#define TEESMC32_FUNCID_CALLS_REVISION 0xFF03
#define TEESMC32_CALLS_REVISION \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, \
TEESMC_OWNER_TRUSTED_OS_API, \
TEESMC32_FUNCID_CALLS_REVISION)
/*
* Get UUID of Trusted OS.
*
* Used by non-secure world to figure out which Trusted OS is installed.
* Note that returned UUID is the UUID of the Trusted OS, not of the API.
*
* Returns UUID in r0-4/w0-4 in the same way as TEESMC32_CALLS_UID
* described above.
*/
#define TEESMC_FUNCID_GET_OS_UUID 0
#define TEESMC32_CALL_GET_OS_UUID \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_GET_OS_UUID)
/*
* Get revision of Trusted OS.
*
* Used by non-secure world to figure out which version of the Trusted OS
* is installed. Note that the returned revision is the revision of the
* Trusted OS, not of the API.
*
* Returns revision in r0-1/w0-1 in the same way as TEESMC32_CALLS_REVISION
* described above.
*/
#define TEESMC_FUNCID_GET_OS_REVISION 1
#define TEESMC32_CALL_GET_OS_REVISION \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_GET_OS_REVISION)
/*
* Call with struct teesmc32_arg as argument
*
* Call register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_WITH_ARG
* r1/x1 Physical pointer to a struct teesmc32_arg
* r2-6/x2-6 Not used
* r7/x7 Hypervisor Client ID register
*
* Normal return register usage:
* r0/x0 Return value, TEESMC_RETURN_*
* r1-3/x1-3 Not used
* r4-7/x4-7 Preserved
*
* Ebusy return register usage:
* r0/x0 Return value, TEESMC_RETURN_ETHREAD_LIMIT
* r1-3/x1-3 Preserved
* r4-7/x4-7 Preserved
*
* RPC return register usage:
* r0/x0 Return value, TEESMC_RETURN_IS_RPC(val)
* r1-2/x1-2 RPC parameters
* r3-7/x3-7 Resume information, must be preserved
*
* Possible return values:
* TEESMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
* function.
* TEESMC_RETURN_OK Call completed, result updated in
* the previously supplied struct
* teesmc32_arg.
* TEESMC_RETURN_ETHREAD_LIMIT Trusted OS out of threads,
* try again later.
* TEESMC_RETURN_EBADADDR Bad physcial pointer to struct
* teesmc32_arg.
* TEESMC_RETURN_EBADCMD Bad/unknown cmd in struct teesmc32_arg
* TEESMC_RETURN_IS_RPC() Call suspended by RPC call to normal
* world.
*/
#define TEESMC_FUNCID_CALL_WITH_ARG 2
#define TEESMC32_CALL_WITH_ARG \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_CALL_WITH_ARG)
/* Same as TEESMC32_CALL_WITH_ARG but a "fast call". */
#define TEESMC32_FASTCALL_WITH_ARG \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_CALL_WITH_ARG)
/*
* Call with struct teesmc64_arg as argument
*
* See description of TEESMC32_CALL_WITH_ARG above, uses struct
* teesmc64_arg in x1 instead.
*/
#define TEESMC64_CALL_WITH_ARG \
TEESMC_CALL_VAL(TEESMC_64, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_CALL_WITH_ARG)
/* Same as TEESMC64_CALL_WITH_ARG but a "fast call". */
#define TEESMC64_FASTCALL_WITH_ARG \
TEESMC_CALL_VAL(TEESMC_64, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_CALL_WITH_ARG)
/*
* Resume from RPC (for example after processing an IRQ)
*
* Call register usage:
* r0/x0 SMC Function ID,
* TEESMC32_CALL_RETURN_FROM_RPC or
* TEESMC32_FASTCALL_RETURN_FROM_RPC
* r1-3/x1-3 Value of r1-3/x1-3 when TEESMC32_CALL_WITH_ARG returned
* TEESMC_RETURN_RPC in r0/x0
*
* Return register usage is the same as for TEESMC32_CALL_WITH_ARG above.
*
* Possible return values
* TEESMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
* function.
* TEESMC_RETURN_OK Original call completed, result
* updated in the previously supplied.
* struct teesmc32_arg
* TEESMC_RETURN_RPC Call suspended by RPC call to normal
* world.
* TEESMC_RETURN_ETHREAD_LIMIT Trusted OS out of threads,
* try again later.
* TEESMC_RETURN_ERESUME Resume failed, the opaque resume
* information was corrupt.
*/
#define TEESMC_FUNCID_RETURN_FROM_RPC 3
#define TEESMC32_CALL_RETURN_FROM_RPC \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_RETURN_FROM_RPC)
/* Same as TEESMC32_CALL_RETURN_FROM_RPC but a "fast call". */
#define TEESMC32_FASTCALL_RETURN_FROM_RPC \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_RETURN_FROM_RPC)
/*
* Resume from RPC (for example after processing an IRQ)
*
* See description of TEESMC32_CALL_RETURN_FROM_RPC above, used when
* it's a 64bit call that has returned.
*/
#define TEESMC64_CALL_RETURN_FROM_RPC \
TEESMC_CALL_VAL(TEESMC_64, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_RETURN_FROM_RPC)
/* Same as TEESMC64_CALL_RETURN_FROM_RPC but a "fast call". */
#define TEESMC64_FASTCALL_RETURN_FROM_RPC \
TEESMC_CALL_VAL(TEESMC_64, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_RETURN_FROM_RPC)
/*
* From secure monitor to Trusted OS, handle FIQ
*
* A virtual call which is injected by the Secure Monitor when an FIQ is
* raised while in normal world (SCR_NS is set). The monitor restores
* secure architecture registers and secure EL_SP1 and jumps to previous
* secure EL3_ELR. Trusted OS should preserve all general purpose
* registers.
*
* Call register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_HANDLE_FIQ
* r1-7/x1-7 Not used, but must be preserved
*
* Return register usage:
* Note used
*/
#define TEESMC_FUNCID_CALL_HANDLE_FIQ 0xf000
#define TEESMC32_CALL_HANDLE_FIQ \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_FUNCID_CALL_HANDLE_FIQ)
#define TEESMC_RETURN_RPC_PREFIX_MASK 0xFFFF0000
#define TEESMC_RETURN_RPC_PREFIX 0xFFFF0000
#define TEESMC_RETURN_RPC_FUNC_MASK 0x0000FFFF
#define TEESMC_RETURN_GET_RPC_FUNC(ret) ((ret) & TEESMC_RETURN_RPC_FUNC_MASK)
#define TEESMC_RPC_VAL(func) ((func) | TEESMC_RETURN_RPC_PREFIX)
/*
* Allocate argument memory for RPC parameter passing.
* Argument memory is used to hold a struct teesmc32_arg.
*
* "Call" register usage:
* r0/x0 This value, TEESMC_RETURN_RPC_ALLOC
* r1/x1 Size in bytes of required argument memory
* r2-7/x2-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1/x1 Physical pointer to allocated argument memory, 0 if size
* was 0 or if memory can't be allocated
* r2-7/x2-7 Preserved
*/
#define TEESMC_RPC_FUNC_ALLOC_ARG 0
#define TEESMC_RETURN_RPC_ALLOC_ARG \
TEESMC_RPC_VAL(TEESMC_RPC_FUNC_ALLOC_ARG)
/*
* Allocate payload memory for RPC parameter passing.
* Payload memory is used to hold the memory referred to by struct
* teesmc32_param_memref.
*
* "Call" register usage:
* r0/x0 This value, TEESMC_RETURN_RPC_ALLOC
* r1/x1 Size in bytes of required payload memory
* r2-7/x2-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1/x1 Physical pointer to allocated payload memory, 0 if size
* was 0 or if memory can't be allocated
* r2-7/x2-7 Preserved
*/
#define TEESMC_RPC_FUNC_ALLOC_PAYLOAD 1
#define TEESMC_RETURN_RPC_ALLOC_PAYLOAD \
TEESMC_RPC_VAL(TEESMC_RPC_FUNC_ALLOC_PAYLOAD)
/*
* Free memory previously allocated by TEESMC_RETURN_RPC_ALLOC_ARG.
*
* "Call" register usage:
* r0/x0 This value, TEESMC_RETURN_RPC_FREE
* r1/x1 Physical pointer to previously allocated argument memory
* r2-7/x2-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1/x1 Not used
* r2-7/x2-7 Preserved
*/
#define TEESMC_RPC_FUNC_FREE_ARG 2
#define TEESMC_RETURN_RPC_FREE_ARG TEESMC_RPC_VAL(TEESMC_RPC_FUNC_FREE_ARG)
/*
* Free memory previously allocated by TEESMC_RETURN_RPC_ALLOC_PAYLOAD.
*
* "Call" register usage:
* r0/x0 This value, TEESMC_RETURN_RPC_FREE
* r1/x1 Physical pointer to previously allocated payload memory
* r3-7/x3-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1-2/x1-2 Not used
* r3-7/x3-7 Preserved
*/
#define TEESMC_RPC_FUNC_FREE_PAYLOAD 3
#define TEESMC_RETURN_RPC_FREE_PAYLOAD \
TEESMC_RPC_VAL(TEESMC_RPC_FUNC_FREE_PAYLOAD)
/*
* Deliver an IRQ in normal world.
*
* "Call" register usage:
* r0/x0 TEESMC_RETURN_RPC_IRQ
* r1-7/x1-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1-7/x1-7 Preserved
*/
#define TEESMC_RPC_FUNC_IRQ 4
#define TEESMC_RETURN_RPC_IRQ TEESMC_RPC_VAL(TEESMC_RPC_FUNC_IRQ)
/*
* Do an RPC request. The supplied struct teesmc{32,64}_arg tells which
* request to do and the parameters for the request. The following fields
* are used (the rest are unused):
* - cmd the Request ID
* - ret return value of the request, filled in by normal world
* - num_params number of parameters for the request
* - params the parameters
* - param_attrs attributes of the parameters
*
* "Call" register usage:
* r0/x0 TEESMC_RETURN_RPC_CMD
* r1/x1 Physical pointer to a struct teesmc32_arg if returning from
* a AArch32 SMC or a struct teesmc64_arg if returning from a
* AArch64 SMC, must be preserved, only the data should
* be updated
* r2-7/x2-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1-7/x1-7 Preserved
*/
#define TEESMC_RPC_FUNC_CMD 5
#define TEESMC_RETURN_RPC_CMD TEESMC_RPC_VAL(TEESMC_RPC_FUNC_CMD)
#define T6SMC_RPC_NONPREEMPT_CMD 6
#define T6SMC_RPC_NONPREEMPT_GET_FUNC(ret) \
((TEESMC_RETURN_GET_RPC_FUNC(ret) >> 8) & 0xff)
#define T6SMC_RETURN_RPC_NONPREEMPT_CMD(cmd) \
TEESMC_RPC_VAL(T6SMC_RPC_NONPREEMPT_CMD | (((cmd) & 0xff) << 8))
#define T6SMC_RPC_CLKMGR_CMD 0
#define T6SMC_RETURN_RPC_CLKMGR_CMD \
T6SMC_RETURN_RPC_NONPREEMPT_CMD(T6SMC_RPC_CLKMGR_CMD)
/* not used. for compatible with old tos version */
#define T6SMC_RPC_CLKMGR_LEGACY_CMD 7
#define T6SMC_RETURN_RPC_CLKMGR_LEGACY_CMD \
TEESMC_RPC_VAL(T6SMC_RPC_CLKMGR_LEGACY_CMD)
/* Returned in r0 */
#define TEESMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
/* Returned in r0 only from Trusted OS functions */
#define TEESMC_RETURN_OK 0x0
#define TEESMC_RETURN_ETHREAD_LIMIT 0x1
#define TEESMC_RETURN_ERESUME 0x2
#define TEESMC_RETURN_EBADADDR 0x3
#define TEESMC_RETURN_EBADCMD 0x4
#define TEESMC_RETURN_IS_RPC(ret) \
(((ret) & TEESMC_RETURN_RPC_PREFIX_MASK) == TEESMC_RETURN_RPC_PREFIX)
/*
* Returned in r1 by Trusted OS functions if r0 = TEESMC_RETURN_RPC
*/
#define TEESMC_RPC_REQUEST_IRQ 0x0
#endif /* TEESMC_H */
@@ -0,0 +1,149 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEESMC_ST_H
#define TEESMC_ST_H
#define TEESMC_ST_RETURN_NOTAVAIL 0x5700
/*
* Get Shared Memory Config
*
* Returns the Secure/Non-secure shared memory config.
*
* Call register usage:
* r0 SMC Function ID, TEESMC32_ST_FASTCALL_GET_SHM_CONFIG
* r1-6 Not used
* r7 Hypervisor Client ID register
*
* Have config return register usage:
* r0 TEESMC_RETURN_OK
* r1 Physical address of start of SHM
* r2 Size of of SHM
* r3 1 if SHM is cached, 0 if uncached.
* r4-7 Preserved
*
* Not available register usage:
* r0 TEESMC_ST_RETURN_NOTAVAIL
* r1-3 Not used
* r4-7 Preserved
*/
#define TEESMC_ST_FUNCID_GET_SHM_CONFIG 0x5700
#define TEESMC32_ST_FASTCALL_GET_SHM_CONFIG \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_ST_FUNCID_GET_SHM_CONFIG)
/*
* Get Log Memory Config
*
* Returns the Secure/Non-secure shared memory config.
*
* Call register usage:
* r0 SMC Function ID, TEESMC32_ST_FASTCALL_GET_LOGM_CONFIG
* r1-6 Not used
* r7 Hypervisor Client ID register
*
* Have config return register usage:
* r0 TEESMC_RETURN_OK
* r1 Physical address of start of LOGM
* r2 Size of of SHM
* r3 1 if SHM is cached, 0 if uncached.
* r4-7 Preserved
*
* Not available register usage:
* r0 TEESMC_ST_RETURN_NOTAVAIL
* r1-3 Not used
* r4-7 Preserved
*/
#define TEESMC_ST_FUNCID_GET_LOGM_CONFIG 0x5702
#define TEESMC32_ST_FASTCALL_GET_LOGM_CONFIG \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_ST_FUNCID_GET_LOGM_CONFIG)
#define TKCORE_FUNCID_TRACE_CONFIG 0x5703
#define TKCORE_FASTCALL_TRACE_CONFIG \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TKCORE_FUNCID_TRACE_CONFIG)
/*
* Configures TZ/NS shared mutex for outer cache maintenance
*
* Disables, enables usage of outercache mutex.
* Returns or sets physical address of outercache mutex.
*
* Call register usage:
* r0 SMC Function ID, TEESMC32_ST_FASTCALL_L2CC_MUTEX
* r1 TEESMC_ST_L2CC_MUTEX_GET_ADDR Get physical address of mutex
* TEESMC_ST_L2CC_MUTEX_SET_ADDR Set physical address of mutex
* TEESMC_ST_L2CC_MUTEX_ENABLE Enable usage of mutex
* TEESMC_ST_L2CC_MUTEX_DISABLE Disable usage of mutex
* r2 if r1 == TEESMC_ST_L2CC_MUTEX_SET_ADDR, physical address of mutex
* r3-6 Not used
* r7 Hypervisor Client ID register
*
* Have config return register usage:
* r0 TEESMC_RETURN_OK
* r1 Preserved
* r2 if r1 == 0, physical address of L2CC mutex
* r3-7 Preserved
*
* Error return register usage:
* r0 TEESMC_ST_RETURN_NOTAVAIL Physical address not available
* TEESMC_RETURN_EBADADDR Bad supplied physical address
* TEESMC_RETURN_EBADCMD Unsupported value in r1
* r1-7 Preserved
*/
#define TEESMC_ST_L2CC_MUTEX_GET_ADDR 0
#define TEESMC_ST_L2CC_MUTEX_SET_ADDR 1
#define TEESMC_ST_L2CC_MUTEX_ENABLE 2
#define TEESMC_ST_L2CC_MUTEX_DISABLE 3
#define TEESMC_ST_FUNCID_L2CC_MUTEX 0x5701
#define TEESMC32_ST_FASTCALL_L2CC_MUTEX \
TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
TEESMC_ST_FUNCID_L2CC_MUTEX)
/*
* Allocate payload memory for RPC parameter passing.
*
* "Call" register usage:
* r0/x0 This value, TEESMC_RETURN_ST_RPC_ALLOC_PAYLOAD
* r1/x1 Size in bytes of required payload memory
* r2/x2 Not used
* r3-7/x3-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r1/x1 Physical pointer to allocated payload memory, 0 if size
* was 0 or if memory can't be allocated
* r2/x2 Shared memory cookie used when freeing the memory
* r3-7/x3-7 Preserved
*/
#define TEESMC_ST_RPC_FUNC_ALLOC_PAYLOAD 0x5700
#define TEESMC_RETURN_ST_RPC_ALLOC_PAYLOAD \
TEESMC_RPC_VAL(TEESMC_ST_RPC_FUNC_ALLOC_PAYLOAD)
/*
* Free memory previously allocated by TEESMC_RETURN_ST_RPC_ALLOC_PAYLOAD
*
* "Call" register usage:
* r0/x0 This value, TEESMC_RETURN_ST_RPC_FREE_PAYLOAD
* r1/x1 Shared memory cookie belonging to this payload memory
* r2-7/x2-7 Resume information, must be preserved
*
* "Return" register usage:
* r0/x0 SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
* AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
* AArch64 SMC return
* r2-7/x2-7 Preserved
*/
#define TEESMC_ST_RPC_FUNC_FREE_PAYLOAD 0x5701
#define TEESMC_RETURN_ST_RPC_FREE_PAYLOAD \
TEESMC_RPC_VAL(TEESMC_ST_RPC_FUNC_FREE_PAYLOAD)
#endif /*TEESMC_ST_H*/
@@ -0,0 +1,161 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_CLIENT_API_H
#define TEE_CLIENT_API_H
#define TEEC_CONFIG_PAYLOAD_REF_COUNT 4
#define TEEC_CONFIG_SHAREDMEM_MAX_SIZE 0x8000
#define TEEC_NONE 0x00000000
#define TEEC_VALUE_INPUT 0x00000001
#define TEEC_VALUE_OUTPUT 0x00000002
#define TEEC_VALUE_INOUT 0x00000003
#define TEEC_MEMREF_TEMP_INPUT 0x00000005
#define TEEC_MEMREF_TEMP_OUTPUT 0x00000006
#define TEEC_MEMREF_TEMP_INOUT 0x00000007
#define TEEC_MEMREF_WHOLE 0x0000000C
#define TEEC_MEMREF_PARTIAL_INPUT 0x0000000D
#define TEEC_MEMREF_PARTIAL_OUTPUT 0x0000000E
#define TEEC_MEMREF_PARTIAL_INOUT 0x0000000F
#define TEEC_MEMREF_PERMANENT 0x00000008
#define TEEC_MEM_INPUT 0x00000001
#define TEEC_MEM_OUTPUT 0x00000002
#define TEEC_MEM_DMABUF 0x00010000
#define TEEC_MEM_KAPI 0x00020000
#define TEEC_MEM_NONSECURE 0x00040000
#define TEEC_SUCCESS 0x00000000
#define TEEC_ERROR_GENERIC 0xFFFF0000
#define TEEC_ERROR_ACCESS_DENIED 0xFFFF0001
#define TEEC_ERROR_CANCEL 0xFFFF0002
#define TEEC_ERROR_ACCESS_CONFLICT 0xFFFF0003
#define TEEC_ERROR_EXCESS_DATA 0xFFFF0004
#define TEEC_ERROR_BAD_FORMAT 0xFFFF0005
#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
#define TEEC_ERROR_BAD_STATE 0xFFFF0007
#define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008
#define TEEC_ERROR_NOT_IMPLEMENTED 0xFFFF0009
#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
#define TEEC_ERROR_NO_DATA 0xFFFF000B
#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
#define TEEC_ERROR_BUSY 0xFFFF000D
#define TEEC_ERROR_COMMUNICATION 0xFFFF000E
#define TEEC_ERROR_SECURITY 0xFFFF000F
#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024
#define TEEC_ORIGIN_API 0x00000001
#define TEEC_ORIGIN_COMMS 0x00000002
#define TEEC_ORIGIN_TEE 0x00000003
#define TEEC_ORIGIN_TRUSTED_APP 0x00000004
#define TEEC_LOGIN_PUBLIC 0x00000000
#define TEEC_LOGIN_USER 0x00000001
#define TEEC_LOGIN_GROUP 0x00000002
#define TEEC_LOGIN_APPLICATION 0x00000004
#define TEEC_PARAM_TYPES(p0, p1, p2, p3) \
((p0) | ((p1) << 4) | ((p2) << 8) | ((p3) << 12))
#define TEEC_PARAM_TYPE_GET(p, i)\
(((p) >> (i * 4)) & 0xF)
typedef uint32_t TEEC_Result;
struct TEEC_Context {
char devname[256];
union {
struct tee_context *ctx;
int fd;
};
};
struct TEEC_UUID {
uint32_t timeLow;
uint16_t timeMid;
uint16_t timeHiAndVersion;
uint8_t clockSeqAndNode[8];
};
struct TEEC_SharedMemory {
union {
void *buffer;
uint64_t padding_ptr;
};
union {
size_t size;
uint64_t padding_sz;
};
uint32_t flags;
uint32_t reserved;
union {
int fd;
void *ptr;
uint64_t padding_d;
} d;
uint64_t registered;
};
struct TEEC_TempMemoryReference {
union {
void *buffer;
uint64_t padding_ptr;
};
union {
size_t size;
uint64_t padding_sz;
};
};
struct TEEC_RegisteredMemoryReference {
union {
struct TEEC_SharedMemory *parent;
uint64_t padding_ptr;
};
union {
size_t size;
uint64_t padding_sz;
};
union {
size_t offset;
uint64_t padding_off;
};
};
struct TEEC_Value {
uint32_t a;
uint32_t b;
};
union TEEC_Parameter {
struct TEEC_TempMemoryReference tmpref;
struct TEEC_RegisteredMemoryReference memref;
struct TEEC_Value value;
};
struct TEEC_Session {
int fd;
};
struct TEEC_Operation {
uint32_t started;
uint32_t paramTypes;
union TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
/* Implementation-Defined */
union {
struct TEEC_Session *session;
uint64_t padding_ptr;
};
struct TEEC_SharedMemory memRefs[TEEC_CONFIG_PAYLOAD_REF_COUNT];
uint64_t flags;
};
#endif
@@ -0,0 +1,57 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_CLKMGR_H
#define TEE_CLKMGR_H
/* TKCORE clk manager framework assumes that
* the clock enable/disable operations are
* symmetric, that is, a clock enabling operation
* requires the same amount of arguments as
* the clock disabling operation.
*
* TKCORE Clk mgr supports at the most 3 arguments
* for a clk operation
*/
int tee_clkmgr_register(const char *clkname, int master_id,
void *enable_fn, void *disable_fn,
void *p1, void *p2, void *p3, size_t argnum);
#define tee_clkmgr_register0(clkname, id, e, d) \
tee_clkmgr_register((clkname), (id), \
(void *) (e), (void *) (d), \
NULL, NULL, NULL, 0)
#define tee_clkmgr_register1(clkname, id, e, d, p0) \
tee_clkmgr_register((clkname), (id), \
(void *) (e), (void *) (d), \
(void *) (p0), NULL, NULL, 1)
#define tee_clkmgr_register2(clkname, id, e, d, p0, p1)\
tee_clkmgr_register((clkname), (id), \
(void *) (e), (void *) (d), \
(void *) (p0), (void *) (p1), NULL, 2)
#define tee_clkmgr_register3(clkname, id, e, d, p0, p1, p2)\
tee_clkmgr_register((clkname), (id), \
(void *) (e), (void *) (d), \
(void *) (p0), (void *) (p1), (void *) (p2), 3)
#define TEE_CLKMGR_TOKEN_NOT_LEGACY (0x1)
#define TEE_CLKMGR_TOKEN_ID_SHIFT (1)
#define TEE_CLKMGR_TOKEN_TYPE_MASK (0xffffu)
#define TEE_CLKMGR_TOKEN_TYPE_SHIFT (16)
#define TEE_CLKMGR_TOKEN(type, id) \
(((type) << TEE_CLKMGR_TOKEN_TYPE_SHIFT) | \
((id) << TEE_CLKMGR_TOKEN_ID_SHIFT) | \
TEE_CLKMGR_TOKEN_NOT_LEGACY)
#define TEE_CLKMGR_OP_ENABLE (0x1)
int tee_clkmgr_handle(uint32_t token, uint32_t op);
#endif
+231
View File
@@ -0,0 +1,231 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef __TEE_CORE_DRV_H__
#define __TEE_CORE_DRV_H__
#include <linux/klist.h>
#include <linux/device.h>
#include <linux/file.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/scatterlist.h>
#include <linux/types.h>
#include <linux/tee_client_api.h>
#include <linux/err.h>
#include <linux/sched.h>
struct tee_cmd_io;
struct tee_shm_io;
struct tee_rpc;
enum tee_state {
TEE_OFFLINE = 0,
TEE_ONLINE = 1,
TEE_SUSPENDED = 2,
TEE_RUNNING = 3,
TEE_CRASHED = 4,
TEE_LAST = 5,
};
#define TEE_CONF_TEST_MODE 0x01000000
#define TEE_CONF_FW_NOT_CAPABLE 0x00000001
struct tee_stats_entry {
int count;
int max;
};
#define TEE_STATS_CONTEXT_IDX 0
#define TEE_STATS_SESSION_IDX 1
#define TEE_STATS_SHM_IDX 2
#define TEE_MAX_TEE_DEV_NAME (64)
struct tee {
struct klist_node node;
char name[TEE_MAX_TEE_DEV_NAME];
int id;
void *priv;
const struct tee_ops *ops;
struct device *dev;
struct miscdevice miscdev;
struct tee_rpc *rpc;
atomic_t refcount;
int max_refcount;
struct tee_stats_entry stats[3];
struct list_head list_ctx;
struct list_head list_rpc_shm;
struct mutex lock;
unsigned int state;
uint32_t shm_flags; /* supported flags for shm allocation */
uint32_t conf;
uint32_t test;
};
#define _DEV(tee) (tee->miscdev.this_device)
#define TEE_MAX_CLIENT_NAME (128)
/**
* struct tee_context - internal structure to store a TEE context.
*
* @tee: tee attached to the tee_context
* @usr_client: flag to known if the client is user side client
* @entry: list of tee_context
* @list_sess: list of tee_session that denotes all tee_session attached
* @list_shm: list of tee_shm that denotes all tee_shm attached
* @refcount: number of objects which reference it (including itself)
*/
struct tee_context {
struct tee *tee;
char name[TEE_MAX_CLIENT_NAME];
int tgid;
int usr_client;
struct list_head entry;
struct list_head list_sess;
struct list_head list_shm;
struct kref refcount;
};
/**
* struct tee_session - internal structure to store a TEE session.
*
* @entry: list of tee_context
* @ctx: tee_context attached to the tee_session
* @sessid: session ID returned by the secure world
* @priv: exporter specific private data for this buffer object
*/
struct tee_session {
struct list_head entry;
struct tee_context *ctx;
uint32_t sessid;
void *priv;
};
struct tee_shm_dma_buf {
struct dma_buf_attachment *attach;
struct sg_table *sgt;
bool tee_allocated;
};
struct tee_shm_resv {
void *kaddr;
dma_addr_t paddr;
struct sg_table sgt;
struct tee_shm_dma_buf *sdb;
};
struct tee_shm_ns {
uint32_t token;
struct page **pages;
size_t nr_pages;
atomic_t ref;
};
/**
* struct tee_shm - internal structure to store a shm object.
*
* @ctx: tee_context attached to the buffer.
* @tee: tee attached to the buffer.
* @dev: device attached to the buffer.
* @size_req: requested size for the buffer
* @size_alloc: effective size of the buffer
* @kaddr: kernel address if mapped kernel side
* @paddr: physical address
* @flags: flags which denote the type of the buffer
* @entry: list of tee_shm
*/
struct tee_shm {
struct list_head entry;
struct tee_context *ctx;
struct tee *tee;
struct device *dev;
size_t size_req;
size_t size_alloc;
uint32_t flags;
union {
struct tee_shm_resv resv;
struct tee_shm_ns ns;
};
};
#define TEE_SHM_MAPPED 0x01000000
#define TEE_SHM_TEMP 0x02000000
#define TEE_SHM_FROM_RPC 0x04000000
#define TEE_SHM_REGISTERED 0x08000000
#define TEE_SHM_MEMREF 0x10000000
#define TEE_SHM_CACHED 0x20000000
#define TEE_SHM_DRV_PRIV_MASK 0xFF000000
struct tee_data {
uint32_t type;
uint32_t type_original;
struct TEEC_SharedMemory c_shm[TEEC_CONFIG_PAYLOAD_REF_COUNT];
union {
struct tee_shm *shm;
struct TEEC_Value value;
} params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
};
struct tee_cmd {
TEEC_Result err;
uint32_t origin;
uint32_t cmd;
struct tee_shm *uuid;
struct tee_shm *ta;
struct tee_data param;
};
void *tee_map_cached_shm(unsigned long pa, size_t len);
void tee_unmap_cached_shm(void *va);
struct tee_shm *tee_shm_alloc_from_rpc(struct tee *tee,
size_t size, uint32_t extra_flags);
void tee_shm_realloc_from_rpc(struct tee *tee,
struct tee_shm *shm);
void tee_shm_free_from_rpc(struct tee_shm *shm);
int tee_core_add(struct tee *tee);
int tee_core_del(struct tee *tee);
int __tee_get(struct tee *tee);
struct tee *tee_core_alloc(struct device *dev, char *name, int id,
const struct tee_ops *ops, size_t len);
int tee_core_free(struct tee *tee);
#include <linux/tee_kernel_lowlevel_api.h>
struct tee_ops {
struct module *owner;
const char *type;
int (*start)(struct tee *tee);
int (*stop)(struct tee *tee);
int (*open)(struct tee_session *sess, struct tee_cmd *cmd);
int (*close)(struct tee_session *sess);
int (*invoke)(struct tee_session *sess, struct tee_cmd *cmd);
int (*cancel)(struct tee_session *sess, struct tee_cmd *cmd);
struct tee_shm *(*alloc)(struct tee *tee, size_t size,
uint32_t flags);
void (*free)(struct tee_shm *shm);
int (*shm_inc_ref)(struct tee_shm *shm);
void (*call_tee)(struct smc_param *p);
void (*raw_call_tee)(struct smc_param *p);
};
#endif /* __TEE_CORE_DRV_H__ */
+16
View File
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_FP_H
#define TEE_FP_H
int tee_spi_cfg_padsel(uint32_t padsel);
int tee_spi_transfer(void *conf, uint32_t conf_size, void *inbuf, void *outbuf,
uint32_t size);
int tee_spi_transfer_disable(void);
#endif
+141
View File
@@ -0,0 +1,141 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef _TEE_IOC_H
#define _TEE_IOC_H
#include <linux/tee_client_api.h>
#ifndef __KERNEL__
#define __user
#endif
/**
* struct tee_cmd_io - The command sent to an open tee device.
* @err: Error code (as in Global Platform TEE Client API spec)
* @origin: Origin for the error code (also from spec).
* @cmd: The command to be executed in the trusted application.
* @uuid: The uuid for the trusted application.
* @data: The trusted application or memory block.
* @data_size: The size of the trusted application or memory block.
* @op: The cmd payload operation for the trusted application.
*
* This structure is mainly used in the Linux kernel for communication
* with the user space.
*/
struct tee_cmd_io {
TEEC_Result err;
uint32_t origin;
uint32_t cmd;
int fd_sess;
/*
* Here fd_sess is 32-bit variable. Since TEEC_Result also is defined as
* "uint32_t", this structure is aligned.
*/
union {
struct TEEC_UUID __user *uuid;
uint64_t padding_uuid;
};
union {
void __user *data;
uint64_t padding_data;
};
union {
struct TEEC_Operation __user *op;
uint64_t padding_op;
};
uint32_t data_size;
/* use this field to represent
* whether there is a difference
* in abi
*/
int32_t reserved;
};
struct tee_shm_io {
union {
void __user *buffer;
void *paddr;
uint64_t padding_buf;
};
uint32_t size;
uint32_t flags;
/*
* Here fd_shm is 32-bit. To be compliant with the convention of file
* descriptor definition, fd_shm is defined as "int" type other
* than "int32_t". Even though using "int32_t" is more obvious to
* indicate that we intend to keep this structure aligned.
*/
int fd_shm;
uint32_t registered;
};
struct tee_ta_inst_desc {
union {
struct TEEC_UUID __user *uuid;
uint64_t pad1;
};
union {
void __user *ta_buf;
uint64_t pad2;
};
uint32_t ta_buf_size;
union {
uint32_t *resp_size;
uint64_t pad3;
};
};
struct tee_spta_inst_desc {
union {
void *ta_binary;
uint64_t padding_buf;
};
uint32_t ta_size;
union {
uint32_t *response_len;
uint64_t paddind_response_len;
};
};
#define TEE_OPEN_SESSION_IOC \
_IOWR('t', 161, struct tee_cmd_io)
#define TEE_INVOKE_COMMAND_IOC \
_IOWR('t', 163, struct tee_cmd_io)
#define TEE_REQUEST_CANCELLATION_IOC \
_IOWR('t', 164, struct tee_cmd_io)
#define TEE_ALLOC_SHM_IOC \
_IOWR('t', 165, struct tee_shm_io)
#define TEE_ALLOC_SHM_PERM_IOC \
_IOWR('t', 166, struct tee_shm_io)
#define TEE_GET_FD_FOR_RPC_SHM_IOC \
_IOWR('t', 167, struct tee_shm_io)
#define TEE_TUI_OPEN_SESSION_IOC \
_IOWR('t', 168, struct tee_cmd_io)
#define TEE_TUI_CLOSE_SESSION_IOC \
_IOWR('t', 169, struct tee_cmd_io)
#define TEE_TUI_NOTIFY_IOC \
_IOWR('t', 170, uint32_t)
#define TEE_TUI_WAITCMD_IOC \
_IOWR('t', 171, uint32_t)
#define TEE_INSTALL_TA_IOC \
_IOWR('t', 174, struct tee_spta_inst_desc)
#define TEE_INSTALL_TA_RESP_IOC \
_IO('t', 175)
#define TEE_DELETE_TA_IOC \
_IOWR('t', 176, struct TEEC_UUID)
#define TEE_QUERY_DRV_FEATURE_IOC \
_IOR('t', 183, uint32_t)
/* Command IDs */
#define TEEC_TUI_CMD_NONE 0
#define TEEC_TUI_CMD_START_ACTIVITY 1
#define TEEC_TUI_CMD_STOP_ACTIVITY 2
#endif /* _TEE_IOC_H */
@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef _TEE_KERNEL_API_H
#define _TEE_KERNEL_API_H
#include <linux/tee_client_api.h>
TEEC_Result TEEC_InitializeContext(const char *name,
struct TEEC_Context *context);
void TEEC_FinalizeContext(struct TEEC_Context *context);
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 *returnOrigin);
void TEEC_CloseSession(struct TEEC_Session *session);
TEEC_Result TEEC_InvokeCommand(struct TEEC_Session *session,
uint32_t commandID,
struct TEEC_Operation *operation,
uint32_t *returnOrigin);
TEEC_Result TEEC_RegisterSharedMemory(struct TEEC_Context *context,
struct TEEC_SharedMemory *sharedMem);
TEEC_Result TEEC_AllocateSharedMemory(struct TEEC_Context *context,
struct TEEC_SharedMemory *sharedMem);
void TEEC_ReleaseSharedMemory(struct TEEC_SharedMemory *sharedMemory);
#endif
@@ -0,0 +1,47 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef _TEE_KERNEL_LOWLEVEL_API_H
#define _TEE_KERNEL_LOWLEVEL_API_H
#include <linux/arm-smccc.h>
#ifdef CONFIG_ARM
struct smc_param {
uint32_t a0;
uint32_t a1;
uint32_t a2;
uint32_t a3;
uint32_t a4;
uint32_t a5;
uint32_t a6;
uint32_t a7;
};
#endif
#ifdef CONFIG_ARM64
struct smc_param {
uint64_t a0;
uint64_t a1;
uint64_t a2;
uint64_t a3;
uint64_t a4;
uint64_t a5;
uint64_t a6;
uint64_t a7;
};
#endif
#define tee_smc_call(p) do {\
struct arm_smccc_res res; \
arm_smccc_smc(p->a0, p->a1, p->a2, p->a3, \
p->a4, p->a5, p->a6, p->a7, &res); \
p->a0 = res.a0; \
p->a1 = res.a1; \
p->a2 = res.a2; \
p->a3 = res.a3; \
} while (0)
#endif
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_PM_SEC_CALL_H_
#define TEE_PM_SEC_CALL_H_
#ifdef CONFIG_ARM
#define TKCORE_SET_NS_BOOT_ADDR (0xBF000103U)
#define TKCORE_PREPARE_CPU_OFF (0xBF000104U)
#define TKCORE_ERRATA_802022 (0xBF000105U)
#define TKCORE_SET_WDT_KADDR (0xBF000300U)
static void tee_pm_sec_call(u32 cmd, u32 p0, u32 p1, u32 p2)
{
__asm__ __volatile__(
".arch_extension sec\n"
"mov r0, %0\n"
"mov r1, %1\n"
"mov r2, %2\n"
"mov r3, %3\n"
"smc #0\n" :
: "r"(cmd), "r"(p0), "r"(p1), "r"(p2)
: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7");
}
#endif
#endif
@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TEE_RPMB_H
#define TEE_RPMB_H
#define TEE_RPMB_EMMC_CID_SIZE 16
#if defined(IN_KERNEL_RPMB_SUPPORT)
struct tee_rpmb_dev_info {
uint8_t cid[TEE_RPMB_EMMC_CID_SIZE];
uint8_t rpmb_size_mult;
uint8_t rel_wr_sec_c;
uint8_t ret_code;
};
struct tkcore_rpmb_request {
uint16_t type;
uint16_t blk_cnt;
uint16_t addr;
uint8_t *data_frame;
};
#define TEE_RPMB_GET_DEV_INFO 0x10
#define TEE_RPMB_PROGRAM_KEY 0x11
#define TEE_RPMB_GET_WRITE_COUNTER 0x12
#define TEE_RPMB_WRITE_DATA 0x13
#define TEE_RPMB_READ_DATA 0x14
#define TEE_RPMB_SWITCH_NORMAL 0x15
/*
* the following function must be
* implemented in kernel driver
*/
extern int tkcore_emmc_rpmb_execute(
struct tkcore_rpmb_request *req);
#endif
#endif
+11
View File
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2015-2019 TrustKernel Incorporated
*/
#ifndef TKCORE_VERSION_H
#define TKCORE_VERSION_H
static const char tkcore_nsdrv_version[] = "3.4p3\n";
#endif