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/uio.h>
13 #include "tee_private.h"
15 static void shm_put_kernel_pages(struct page **pages, size_t page_count)
19 for (n = 0; n < page_count; n++)
23 static int shm_get_kernel_pages(unsigned long start, size_t page_count,
29 if (is_vmalloc_addr((void *)start)) {
32 for (n = 0; n < page_count; n++) {
33 page = vmalloc_to_page((void *)(start + PAGE_SIZE * n));
44 kiov = kcalloc(page_count, sizeof(*kiov), GFP_KERNEL);
48 for (n = 0; n < page_count; n++) {
49 kiov[n].iov_base = (void *)(start + n * PAGE_SIZE);
50 kiov[n].iov_len = PAGE_SIZE;
53 rc = get_kernel_pages(kiov, page_count, 0, pages);
60 static void release_registered_pages(struct tee_shm *shm)
63 if (shm->flags & TEE_SHM_USER_MAPPED)
64 unpin_user_pages(shm->pages, shm->num_pages);
66 shm_put_kernel_pages(shm->pages, shm->num_pages);
72 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
74 if (shm->flags & TEE_SHM_POOL) {
75 teedev->pool->ops->free(teedev->pool, shm);
76 } else if (shm->flags & TEE_SHM_DYNAMIC) {
77 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
80 dev_err(teedev->dev.parent,
81 "unregister shm %p failed: %d", shm, rc);
83 release_registered_pages(shm);
86 teedev_ctx_put(shm->ctx);
90 tee_device_put(teedev);
93 static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t size,
94 size_t align, u32 flags, int id)
96 struct tee_device *teedev = ctx->teedev;
101 if (!tee_device_get(teedev))
102 return ERR_PTR(-EINVAL);
105 /* teedev has been detached from driver */
106 ret = ERR_PTR(-EINVAL);
110 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
112 ret = ERR_PTR(-ENOMEM);
116 refcount_set(&shm->refcount, 1);
121 * We're assigning this as it is needed if the shm is to be
122 * registered. If this function returns OK then the caller expected
123 * to call teedev_ctx_get() or clear shm->ctx in case it's not
128 rc = teedev->pool->ops->alloc(teedev->pool, shm, size, align);
139 tee_device_put(teedev);
144 * tee_shm_alloc_user_buf() - Allocate shared memory for user space
145 * @ctx: Context that allocates the shared memory
146 * @size: Requested size of shared memory
148 * Memory allocated as user space shared memory is automatically freed when
149 * the TEE file pointer is closed. The primary usage of this function is
150 * when the TEE driver doesn't support registering ordinary user space
153 * @returns a pointer to 'struct tee_shm'
155 struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size)
157 u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
158 struct tee_device *teedev = ctx->teedev;
163 mutex_lock(&teedev->mutex);
164 id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
165 mutex_unlock(&teedev->mutex);
169 shm = shm_alloc_helper(ctx, size, PAGE_SIZE, flags, id);
171 mutex_lock(&teedev->mutex);
172 idr_remove(&teedev->idr, id);
173 mutex_unlock(&teedev->mutex);
177 mutex_lock(&teedev->mutex);
178 ret = idr_replace(&teedev->idr, shm, id);
179 mutex_unlock(&teedev->mutex);
189 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
190 * @ctx: Context that allocates the shared memory
191 * @size: Requested size of shared memory
193 * The returned memory registered in secure world and is suitable to be
194 * passed as a memory buffer in parameter argument to
195 * tee_client_invoke_func(). The memory allocated is later freed with a
196 * call to tee_shm_free().
198 * @returns a pointer to 'struct tee_shm'
200 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
202 u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
204 return shm_alloc_helper(ctx, size, PAGE_SIZE, flags, -1);
206 EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
209 * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared
211 * @ctx: Context that allocates the shared memory
212 * @size: Requested size of shared memory
214 * This function returns similar shared memory as
215 * tee_shm_alloc_kernel_buf(), but with the difference that the memory
216 * might not be registered in secure world in case the driver supports
217 * passing memory not registered in advance.
219 * This function should normally only be used internally in the TEE
222 * @returns a pointer to 'struct tee_shm'
224 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
226 u32 flags = TEE_SHM_PRIV | TEE_SHM_POOL;
228 return shm_alloc_helper(ctx, size, sizeof(long) * 2, flags, -1);
230 EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
232 static struct tee_shm *
233 register_shm_helper(struct tee_context *ctx, unsigned long addr,
234 size_t length, u32 flags, int id)
236 struct tee_device *teedev = ctx->teedev;
243 if (!tee_device_get(teedev))
244 return ERR_PTR(-EINVAL);
246 if (!teedev->desc->ops->shm_register ||
247 !teedev->desc->ops->shm_unregister) {
248 ret = ERR_PTR(-ENOTSUPP);
254 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
256 ret = ERR_PTR(-ENOMEM);
260 refcount_set(&shm->refcount, 1);
264 addr = untagged_addr(addr);
265 start = rounddown(addr, PAGE_SIZE);
266 shm->offset = addr - start;
268 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
269 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
271 ret = ERR_PTR(-ENOMEM);
275 if (flags & TEE_SHM_USER_MAPPED)
276 rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
279 rc = shm_get_kernel_pages(start, num_pages, shm->pages);
282 if (rc != num_pages) {
286 goto err_put_shm_pages;
289 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
290 shm->num_pages, start);
293 goto err_put_shm_pages;
298 if (flags & TEE_SHM_USER_MAPPED)
299 unpin_user_pages(shm->pages, shm->num_pages);
301 shm_put_kernel_pages(shm->pages, shm->num_pages);
308 tee_device_put(teedev);
313 * tee_shm_register_user_buf() - Register a userspace shared memory buffer
314 * @ctx: Context that registers the shared memory
315 * @addr: The userspace address of the shared buffer
316 * @length: Length of the shared buffer
318 * @returns a pointer to 'struct tee_shm'
320 struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
321 unsigned long addr, size_t length)
323 u32 flags = TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC;
324 struct tee_device *teedev = ctx->teedev;
329 if (!access_ok((void __user *)addr, length))
330 return ERR_PTR(-EFAULT);
332 mutex_lock(&teedev->mutex);
333 id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
334 mutex_unlock(&teedev->mutex);
338 shm = register_shm_helper(ctx, addr, length, flags, id);
340 mutex_lock(&teedev->mutex);
341 idr_remove(&teedev->idr, id);
342 mutex_unlock(&teedev->mutex);
346 mutex_lock(&teedev->mutex);
347 ret = idr_replace(&teedev->idr, shm, id);
348 mutex_unlock(&teedev->mutex);
358 * tee_shm_register_kernel_buf() - Register kernel memory to be shared with
360 * @ctx: Context that registers the shared memory
362 * @length: Length of the buffer
364 * @returns a pointer to 'struct tee_shm'
367 struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
368 void *addr, size_t length)
370 u32 flags = TEE_SHM_DYNAMIC;
372 return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1);
374 EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf);
376 static int tee_shm_fop_release(struct inode *inode, struct file *filp)
378 tee_shm_put(filp->private_data);
382 static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
384 struct tee_shm *shm = filp->private_data;
385 size_t size = vma->vm_end - vma->vm_start;
387 /* Refuse sharing shared memory provided by application */
388 if (shm->flags & TEE_SHM_USER_MAPPED)
391 /* check for overflowing the buffer's size */
392 if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
395 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
396 size, vma->vm_page_prot);
399 static const struct file_operations tee_shm_fops = {
400 .owner = THIS_MODULE,
401 .release = tee_shm_fop_release,
402 .mmap = tee_shm_fop_mmap,
406 * tee_shm_get_fd() - Increase reference count and return file descriptor
407 * @shm: Shared memory handle
408 * @returns user space file descriptor to shared memory
410 int tee_shm_get_fd(struct tee_shm *shm)
417 /* matched by tee_shm_put() in tee_shm_op_release() */
418 refcount_inc(&shm->refcount);
419 fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
426 * tee_shm_free() - Free shared memory
427 * @shm: Handle to shared memory to free
429 void tee_shm_free(struct tee_shm *shm)
433 EXPORT_SYMBOL_GPL(tee_shm_free);
436 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
437 * @shm: Shared memory handle
438 * @offs: Offset from start of this shared memory
439 * @returns virtual address of the shared memory + offs if offs is within
440 * the bounds of this shared memory, else an ERR_PTR
442 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
445 return ERR_PTR(-EINVAL);
446 if (offs >= shm->size)
447 return ERR_PTR(-EINVAL);
448 return (char *)shm->kaddr + offs;
450 EXPORT_SYMBOL_GPL(tee_shm_get_va);
453 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
454 * @shm: Shared memory handle
455 * @offs: Offset from start of this shared memory
456 * @pa: Physical address to return
457 * @returns 0 if offs is within the bounds of this shared memory, else an
460 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
462 if (offs >= shm->size)
465 *pa = shm->paddr + offs;
468 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
471 * tee_shm_get_from_id() - Find shared memory object and increase reference
473 * @ctx: Context owning the shared memory
474 * @id: Id of shared memory object
475 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
477 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
479 struct tee_device *teedev;
483 return ERR_PTR(-EINVAL);
485 teedev = ctx->teedev;
486 mutex_lock(&teedev->mutex);
487 shm = idr_find(&teedev->idr, id);
489 * If the tee_shm was found in the IDR it must have a refcount
490 * larger than 0 due to the guarantee in tee_shm_put() below. So
491 * it's safe to use refcount_inc().
493 if (!shm || shm->ctx != ctx)
494 shm = ERR_PTR(-EINVAL);
496 refcount_inc(&shm->refcount);
497 mutex_unlock(&teedev->mutex);
500 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
503 * tee_shm_put() - Decrease reference count on a shared memory handle
504 * @shm: Shared memory handle
506 void tee_shm_put(struct tee_shm *shm)
508 struct tee_device *teedev = shm->ctx->teedev;
509 bool do_release = false;
511 mutex_lock(&teedev->mutex);
512 if (refcount_dec_and_test(&shm->refcount)) {
514 * refcount has reached 0, we must now remove it from the
515 * IDR before releasing the mutex. This will guarantee that
516 * the refcount_inc() in tee_shm_get_from_id() never starts
520 idr_remove(&teedev->idr, shm->id);
523 mutex_unlock(&teedev->mutex);
526 tee_shm_release(teedev, shm);
528 EXPORT_SYMBOL_GPL(tee_shm_put);