1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2017, 2019-2021 Linaro Limited
5 #include <linux/anon_inodes.h>
6 #include <linux/device.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/tee_drv.h>
12 #include <linux/uaccess.h>
13 #include <linux/uio.h>
14 #include <linux/highmem.h>
15 #include "tee_private.h"
17 static void shm_put_kernel_pages(struct page **pages, size_t page_count)
21 for (n = 0; n < page_count; n++)
25 static int shm_get_kernel_pages(unsigned long start, size_t page_count,
31 if (WARN_ON_ONCE(is_vmalloc_addr((void *)start) ||
32 is_kmap_addr((void *)start)))
35 page = virt_to_page((void *)start);
36 for (n = 0; n < page_count; n++) {
44 static void release_registered_pages(struct tee_shm *shm)
47 if (shm->flags & TEE_SHM_USER_MAPPED)
48 unpin_user_pages(shm->pages, shm->num_pages);
50 shm_put_kernel_pages(shm->pages, shm->num_pages);
56 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
58 if (shm->flags & TEE_SHM_POOL) {
59 teedev->pool->ops->free(teedev->pool, shm);
60 } else if (shm->flags & TEE_SHM_DYNAMIC) {
61 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
64 dev_err(teedev->dev.parent,
65 "unregister shm %p failed: %d", shm, rc);
67 release_registered_pages(shm);
70 teedev_ctx_put(shm->ctx);
74 tee_device_put(teedev);
77 static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t size,
78 size_t align, u32 flags, int id)
80 struct tee_device *teedev = ctx->teedev;
85 if (!tee_device_get(teedev))
86 return ERR_PTR(-EINVAL);
89 /* teedev has been detached from driver */
90 ret = ERR_PTR(-EINVAL);
94 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
96 ret = ERR_PTR(-ENOMEM);
100 refcount_set(&shm->refcount, 1);
105 * We're assigning this as it is needed if the shm is to be
106 * registered. If this function returns OK then the caller expected
107 * to call teedev_ctx_get() or clear shm->ctx in case it's not
112 rc = teedev->pool->ops->alloc(teedev->pool, shm, size, align);
123 tee_device_put(teedev);
128 * tee_shm_alloc_user_buf() - Allocate shared memory for user space
129 * @ctx: Context that allocates the shared memory
130 * @size: Requested size of shared memory
132 * Memory allocated as user space shared memory is automatically freed when
133 * the TEE file pointer is closed. The primary usage of this function is
134 * when the TEE driver doesn't support registering ordinary user space
137 * @returns a pointer to 'struct tee_shm'
139 struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size)
141 u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
142 struct tee_device *teedev = ctx->teedev;
147 mutex_lock(&teedev->mutex);
148 id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
149 mutex_unlock(&teedev->mutex);
153 shm = shm_alloc_helper(ctx, size, PAGE_SIZE, flags, id);
155 mutex_lock(&teedev->mutex);
156 idr_remove(&teedev->idr, id);
157 mutex_unlock(&teedev->mutex);
161 mutex_lock(&teedev->mutex);
162 ret = idr_replace(&teedev->idr, shm, id);
163 mutex_unlock(&teedev->mutex);
173 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
174 * @ctx: Context that allocates the shared memory
175 * @size: Requested size of shared memory
177 * The returned memory registered in secure world and is suitable to be
178 * passed as a memory buffer in parameter argument to
179 * tee_client_invoke_func(). The memory allocated is later freed with a
180 * call to tee_shm_free().
182 * @returns a pointer to 'struct tee_shm'
184 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
186 u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
188 return shm_alloc_helper(ctx, size, PAGE_SIZE, flags, -1);
190 EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
193 * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared
195 * @ctx: Context that allocates the shared memory
196 * @size: Requested size of shared memory
198 * This function returns similar shared memory as
199 * tee_shm_alloc_kernel_buf(), but with the difference that the memory
200 * might not be registered in secure world in case the driver supports
201 * passing memory not registered in advance.
203 * This function should normally only be used internally in the TEE
206 * @returns a pointer to 'struct tee_shm'
208 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
210 u32 flags = TEE_SHM_PRIV | TEE_SHM_POOL;
212 return shm_alloc_helper(ctx, size, sizeof(long) * 2, flags, -1);
214 EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
216 static struct tee_shm *
217 register_shm_helper(struct tee_context *ctx, unsigned long addr,
218 size_t length, u32 flags, int id)
220 struct tee_device *teedev = ctx->teedev;
227 if (!tee_device_get(teedev))
228 return ERR_PTR(-EINVAL);
230 if (!teedev->desc->ops->shm_register ||
231 !teedev->desc->ops->shm_unregister) {
232 ret = ERR_PTR(-ENOTSUPP);
238 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
240 ret = ERR_PTR(-ENOMEM);
244 refcount_set(&shm->refcount, 1);
248 addr = untagged_addr(addr);
249 start = rounddown(addr, PAGE_SIZE);
250 shm->offset = addr - start;
252 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
253 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
255 ret = ERR_PTR(-ENOMEM);
259 if (flags & TEE_SHM_USER_MAPPED)
260 rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
263 rc = shm_get_kernel_pages(start, num_pages, shm->pages);
266 if (rc != num_pages) {
270 goto err_put_shm_pages;
273 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
274 shm->num_pages, start);
277 goto err_put_shm_pages;
282 if (flags & TEE_SHM_USER_MAPPED)
283 unpin_user_pages(shm->pages, shm->num_pages);
285 shm_put_kernel_pages(shm->pages, shm->num_pages);
292 tee_device_put(teedev);
297 * tee_shm_register_user_buf() - Register a userspace shared memory buffer
298 * @ctx: Context that registers the shared memory
299 * @addr: The userspace address of the shared buffer
300 * @length: Length of the shared buffer
302 * @returns a pointer to 'struct tee_shm'
304 struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
305 unsigned long addr, size_t length)
307 u32 flags = TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC;
308 struct tee_device *teedev = ctx->teedev;
313 if (!access_ok((void __user *)addr, length))
314 return ERR_PTR(-EFAULT);
316 mutex_lock(&teedev->mutex);
317 id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
318 mutex_unlock(&teedev->mutex);
322 shm = register_shm_helper(ctx, addr, length, flags, id);
324 mutex_lock(&teedev->mutex);
325 idr_remove(&teedev->idr, id);
326 mutex_unlock(&teedev->mutex);
330 mutex_lock(&teedev->mutex);
331 ret = idr_replace(&teedev->idr, shm, id);
332 mutex_unlock(&teedev->mutex);
342 * tee_shm_register_kernel_buf() - Register kernel memory to be shared with
344 * @ctx: Context that registers the shared memory
346 * @length: Length of the buffer
348 * @returns a pointer to 'struct tee_shm'
351 struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
352 void *addr, size_t length)
354 u32 flags = TEE_SHM_DYNAMIC;
356 return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1);
358 EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf);
360 static int tee_shm_fop_release(struct inode *inode, struct file *filp)
362 tee_shm_put(filp->private_data);
366 static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
368 struct tee_shm *shm = filp->private_data;
369 size_t size = vma->vm_end - vma->vm_start;
371 /* Refuse sharing shared memory provided by application */
372 if (shm->flags & TEE_SHM_USER_MAPPED)
375 /* check for overflowing the buffer's size */
376 if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
379 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
380 size, vma->vm_page_prot);
383 static const struct file_operations tee_shm_fops = {
384 .owner = THIS_MODULE,
385 .release = tee_shm_fop_release,
386 .mmap = tee_shm_fop_mmap,
390 * tee_shm_get_fd() - Increase reference count and return file descriptor
391 * @shm: Shared memory handle
392 * @returns user space file descriptor to shared memory
394 int tee_shm_get_fd(struct tee_shm *shm)
401 /* matched by tee_shm_put() in tee_shm_op_release() */
402 refcount_inc(&shm->refcount);
403 fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
410 * tee_shm_free() - Free shared memory
411 * @shm: Handle to shared memory to free
413 void tee_shm_free(struct tee_shm *shm)
417 EXPORT_SYMBOL_GPL(tee_shm_free);
420 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
421 * @shm: Shared memory handle
422 * @offs: Offset from start of this shared memory
423 * @returns virtual address of the shared memory + offs if offs is within
424 * the bounds of this shared memory, else an ERR_PTR
426 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
429 return ERR_PTR(-EINVAL);
430 if (offs >= shm->size)
431 return ERR_PTR(-EINVAL);
432 return (char *)shm->kaddr + offs;
434 EXPORT_SYMBOL_GPL(tee_shm_get_va);
437 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
438 * @shm: Shared memory handle
439 * @offs: Offset from start of this shared memory
440 * @pa: Physical address to return
441 * @returns 0 if offs is within the bounds of this shared memory, else an
444 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
446 if (offs >= shm->size)
449 *pa = shm->paddr + offs;
452 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
455 * tee_shm_get_from_id() - Find shared memory object and increase reference
457 * @ctx: Context owning the shared memory
458 * @id: Id of shared memory object
459 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
461 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
463 struct tee_device *teedev;
467 return ERR_PTR(-EINVAL);
469 teedev = ctx->teedev;
470 mutex_lock(&teedev->mutex);
471 shm = idr_find(&teedev->idr, id);
473 * If the tee_shm was found in the IDR it must have a refcount
474 * larger than 0 due to the guarantee in tee_shm_put() below. So
475 * it's safe to use refcount_inc().
477 if (!shm || shm->ctx != ctx)
478 shm = ERR_PTR(-EINVAL);
480 refcount_inc(&shm->refcount);
481 mutex_unlock(&teedev->mutex);
484 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
487 * tee_shm_put() - Decrease reference count on a shared memory handle
488 * @shm: Shared memory handle
490 void tee_shm_put(struct tee_shm *shm)
492 struct tee_device *teedev = shm->ctx->teedev;
493 bool do_release = false;
495 mutex_lock(&teedev->mutex);
496 if (refcount_dec_and_test(&shm->refcount)) {
498 * refcount has reached 0, we must now remove it from the
499 * IDR before releasing the mutex. This will guarantee that
500 * the refcount_inc() in tee_shm_get_from_id() never starts
504 idr_remove(&teedev->idr, shm->id);
507 mutex_unlock(&teedev->mutex);
510 tee_shm_release(teedev, shm);
512 EXPORT_SYMBOL_GPL(tee_shm_put);