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

+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__ */