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 "tee_private.h"
16 static void shm_put_kernel_pages(struct page **pages, size_t page_count)
20 for (n = 0; n < page_count; n++)
24 static int shm_get_kernel_pages(unsigned long start, size_t page_count,
30 if (is_vmalloc_addr((void *)start)) {
33 for (n = 0; n < page_count; n++) {
34 page = vmalloc_to_page((void *)(start + PAGE_SIZE * n));
45 kiov = kcalloc(page_count, sizeof(*kiov), GFP_KERNEL);
49 for (n = 0; n < page_count; n++) {
50 kiov[n].iov_base = (void *)(start + n * PAGE_SIZE);
51 kiov[n].iov_len = PAGE_SIZE;
54 rc = get_kernel_pages(kiov, page_count, 0, pages);
61 static void release_registered_pages(struct tee_shm *shm)
64 if (shm->flags & TEE_SHM_USER_MAPPED)
65 unpin_user_pages(shm->pages, shm->num_pages);
67 shm_put_kernel_pages(shm->pages, shm->num_pages);
73 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
75 if (shm->flags & TEE_SHM_POOL) {
76 teedev->pool->ops->free(teedev->pool, shm);
77 } else if (shm->flags & TEE_SHM_DYNAMIC) {
78 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
81 dev_err(teedev->dev.parent,
82 "unregister shm %p failed: %d", shm, rc);
84 release_registered_pages(shm);
87 teedev_ctx_put(shm->ctx);
91 tee_device_put(teedev);
94 static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t size,
95 size_t align, u32 flags, int id)
97 struct tee_device *teedev = ctx->teedev;
102 if (!tee_device_get(teedev))
103 return ERR_PTR(-EINVAL);
106 /* teedev has been detached from driver */
107 ret = ERR_PTR(-EINVAL);
111 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
113 ret = ERR_PTR(-ENOMEM);
117 refcount_set(&shm->refcount, 1);
122 * We're assigning this as it is needed if the shm is to be
123 * registered. If this function returns OK then the caller expected
124 * to call teedev_ctx_get() or clear shm->ctx in case it's not
129 rc = teedev->pool->ops->alloc(teedev->pool, shm, size, align);
140 tee_device_put(teedev);
145 * tee_shm_alloc_user_buf() - Allocate shared memory for user space
146 * @ctx: Context that allocates the shared memory
147 * @size: Requested size of shared memory
149 * Memory allocated as user space shared memory is automatically freed when
150 * the TEE file pointer is closed. The primary usage of this function is
151 * when the TEE driver doesn't support registering ordinary user space
154 * @returns a pointer to 'struct tee_shm'
156 struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size)
158 u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
159 struct tee_device *teedev = ctx->teedev;
164 mutex_lock(&teedev->mutex);
165 id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
166 mutex_unlock(&teedev->mutex);
170 shm = shm_alloc_helper(ctx, size, PAGE_SIZE, flags, id);
172 mutex_lock(&teedev->mutex);
173 idr_remove(&teedev->idr, id);
174 mutex_unlock(&teedev->mutex);
178 mutex_lock(&teedev->mutex);
179 ret = idr_replace(&teedev->idr, shm, id);
180 mutex_unlock(&teedev->mutex);
190 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
191 * @ctx: Context that allocates the shared memory
192 * @size: Requested size of shared memory
194 * The returned memory registered in secure world and is suitable to be
195 * passed as a memory buffer in parameter argument to
196 * tee_client_invoke_func(). The memory allocated is later freed with a
197 * call to tee_shm_free().
199 * @returns a pointer to 'struct tee_shm'
201 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
203 u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
205 return shm_alloc_helper(ctx, size, PAGE_SIZE, flags, -1);
207 EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
210 * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared
212 * @ctx: Context that allocates the shared memory
213 * @size: Requested size of shared memory
215 * This function returns similar shared memory as
216 * tee_shm_alloc_kernel_buf(), but with the difference that the memory
217 * might not be registered in secure world in case the driver supports
218 * passing memory not registered in advance.
220 * This function should normally only be used internally in the TEE
223 * @returns a pointer to 'struct tee_shm'
225 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
227 u32 flags = TEE_SHM_PRIV | TEE_SHM_POOL;
229 return shm_alloc_helper(ctx, size, sizeof(long) * 2, flags, -1);
231 EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
233 static struct tee_shm *
234 register_shm_helper(struct tee_context *ctx, unsigned long addr,
235 size_t length, u32 flags, int id)
237 struct tee_device *teedev = ctx->teedev;
244 if (!tee_device_get(teedev))
245 return ERR_PTR(-EINVAL);
247 if (!teedev->desc->ops->shm_register ||
248 !teedev->desc->ops->shm_unregister) {
249 ret = ERR_PTR(-ENOTSUPP);
255 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
257 ret = ERR_PTR(-ENOMEM);
261 refcount_set(&shm->refcount, 1);
265 addr = untagged_addr(addr);
266 start = rounddown(addr, PAGE_SIZE);
267 shm->offset = addr - start;
269 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
270 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
272 ret = ERR_PTR(-ENOMEM);
276 if (flags & TEE_SHM_USER_MAPPED)
277 rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
280 rc = shm_get_kernel_pages(start, num_pages, shm->pages);
283 if (rc != num_pages) {
287 goto err_put_shm_pages;
290 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
291 shm->num_pages, start);
294 goto err_put_shm_pages;
299 if (flags & TEE_SHM_USER_MAPPED)
300 unpin_user_pages(shm->pages, shm->num_pages);
302 shm_put_kernel_pages(shm->pages, shm->num_pages);
309 tee_device_put(teedev);
314 * tee_shm_register_user_buf() - Register a userspace shared memory buffer
315 * @ctx: Context that registers the shared memory
316 * @addr: The userspace address of the shared buffer
317 * @length: Length of the shared buffer
319 * @returns a pointer to 'struct tee_shm'
321 struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
322 unsigned long addr, size_t length)
324 u32 flags = TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC;
325 struct tee_device *teedev = ctx->teedev;
330 if (!access_ok((void __user *)addr, length))
331 return ERR_PTR(-EFAULT);
333 mutex_lock(&teedev->mutex);
334 id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
335 mutex_unlock(&teedev->mutex);
339 shm = register_shm_helper(ctx, addr, length, flags, id);
341 mutex_lock(&teedev->mutex);
342 idr_remove(&teedev->idr, id);
343 mutex_unlock(&teedev->mutex);
347 mutex_lock(&teedev->mutex);
348 ret = idr_replace(&teedev->idr, shm, id);
349 mutex_unlock(&teedev->mutex);
359 * tee_shm_register_kernel_buf() - Register kernel memory to be shared with
361 * @ctx: Context that registers the shared memory
363 * @length: Length of the buffer
365 * @returns a pointer to 'struct tee_shm'
368 struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
369 void *addr, size_t length)
371 u32 flags = TEE_SHM_DYNAMIC;
373 return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1);
375 EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf);
377 static int tee_shm_fop_release(struct inode *inode, struct file *filp)
379 tee_shm_put(filp->private_data);
383 static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
385 struct tee_shm *shm = filp->private_data;
386 size_t size = vma->vm_end - vma->vm_start;
388 /* Refuse sharing shared memory provided by application */
389 if (shm->flags & TEE_SHM_USER_MAPPED)
392 /* check for overflowing the buffer's size */
393 if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
396 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
397 size, vma->vm_page_prot);
400 static const struct file_operations tee_shm_fops = {
401 .owner = THIS_MODULE,
402 .release = tee_shm_fop_release,
403 .mmap = tee_shm_fop_mmap,
407 * tee_shm_get_fd() - Increase reference count and return file descriptor
408 * @shm: Shared memory handle
409 * @returns user space file descriptor to shared memory
411 int tee_shm_get_fd(struct tee_shm *shm)
418 /* matched by tee_shm_put() in tee_shm_op_release() */
419 refcount_inc(&shm->refcount);
420 fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
427 * tee_shm_free() - Free shared memory
428 * @shm: Handle to shared memory to free
430 void tee_shm_free(struct tee_shm *shm)
434 EXPORT_SYMBOL_GPL(tee_shm_free);
437 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
438 * @shm: Shared memory handle
439 * @offs: Offset from start of this shared memory
440 * @returns virtual address of the shared memory + offs if offs is within
441 * the bounds of this shared memory, else an ERR_PTR
443 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
446 return ERR_PTR(-EINVAL);
447 if (offs >= shm->size)
448 return ERR_PTR(-EINVAL);
449 return (char *)shm->kaddr + offs;
451 EXPORT_SYMBOL_GPL(tee_shm_get_va);
454 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
455 * @shm: Shared memory handle
456 * @offs: Offset from start of this shared memory
457 * @pa: Physical address to return
458 * @returns 0 if offs is within the bounds of this shared memory, else an
461 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
463 if (offs >= shm->size)
466 *pa = shm->paddr + offs;
469 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
472 * tee_shm_get_from_id() - Find shared memory object and increase reference
474 * @ctx: Context owning the shared memory
475 * @id: Id of shared memory object
476 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
478 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
480 struct tee_device *teedev;
484 return ERR_PTR(-EINVAL);
486 teedev = ctx->teedev;
487 mutex_lock(&teedev->mutex);
488 shm = idr_find(&teedev->idr, id);
490 * If the tee_shm was found in the IDR it must have a refcount
491 * larger than 0 due to the guarantee in tee_shm_put() below. So
492 * it's safe to use refcount_inc().
494 if (!shm || shm->ctx != ctx)
495 shm = ERR_PTR(-EINVAL);
497 refcount_inc(&shm->refcount);
498 mutex_unlock(&teedev->mutex);
501 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
504 * tee_shm_put() - Decrease reference count on a shared memory handle
505 * @shm: Shared memory handle
507 void tee_shm_put(struct tee_shm *shm)
509 struct tee_device *teedev = shm->ctx->teedev;
510 bool do_release = false;
512 mutex_lock(&teedev->mutex);
513 if (refcount_dec_and_test(&shm->refcount)) {
515 * refcount has reached 0, we must now remove it from the
516 * IDR before releasing the mutex. This will guarantee that
517 * the refcount_inc() in tee_shm_get_from_id() never starts
521 idr_remove(&teedev->idr, shm->id);
524 mutex_unlock(&teedev->mutex);
527 tee_shm_release(teedev, shm);
529 EXPORT_SYMBOL_GPL(tee_shm_put);