Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2015-2016, Linaro Limited
  4 */
 
  5#include <linux/device.h>
  6#include <linux/dma-buf.h>
  7#include <linux/fdtable.h>
  8#include <linux/idr.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"
 14
 15static void tee_shm_release(struct tee_shm *shm)
 16{
 17	struct tee_device *teedev = shm->ctx->teedev;
 18
 19	if (shm->flags & TEE_SHM_DMA_BUF) {
 20		mutex_lock(&teedev->mutex);
 21		idr_remove(&teedev->idr, shm->id);
 22		mutex_unlock(&teedev->mutex);
 23	}
 24
 25	if (shm->flags & TEE_SHM_POOL) {
 26		struct tee_shm_pool_mgr *poolm;
 
 27
 28		if (shm->flags & TEE_SHM_DMA_BUF)
 29			poolm = teedev->pool->dma_buf_mgr;
 
 
 
 
 
 
 
 30		else
 31			poolm = teedev->pool->private_mgr;
 
 
 
 
 32
 33		poolm->ops->free(poolm, shm);
 34	} else if (shm->flags & TEE_SHM_REGISTER) {
 35		size_t n;
 
 
 36		int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
 37
 38		if (rc)
 39			dev_err(teedev->dev.parent,
 40				"unregister shm %p failed: %d", shm, rc);
 41
 42		for (n = 0; n < shm->num_pages; n++)
 43			put_page(shm->pages[n]);
 44
 45		kfree(shm->pages);
 46	}
 47
 48	teedev_ctx_put(shm->ctx);
 49
 50	kfree(shm);
 51
 52	tee_device_put(teedev);
 53}
 54
 55static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
 56			*attach, enum dma_data_direction dir)
 57{
 58	return NULL;
 59}
 60
 61static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
 62				     struct sg_table *table,
 63				     enum dma_data_direction dir)
 64{
 65}
 66
 67static void tee_shm_op_release(struct dma_buf *dmabuf)
 68{
 69	struct tee_shm *shm = dmabuf->priv;
 70
 71	tee_shm_release(shm);
 72}
 73
 74static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
 75{
 76	struct tee_shm *shm = dmabuf->priv;
 77	size_t size = vma->vm_end - vma->vm_start;
 78
 79	/* Refuse sharing shared memory provided by application */
 80	if (shm->flags & TEE_SHM_USER_MAPPED)
 81		return -EINVAL;
 82
 83	return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
 84			       size, vma->vm_page_prot);
 85}
 86
 87static const struct dma_buf_ops tee_shm_dma_buf_ops = {
 88	.map_dma_buf = tee_shm_op_map_dma_buf,
 89	.unmap_dma_buf = tee_shm_op_unmap_dma_buf,
 90	.release = tee_shm_op_release,
 91	.mmap = tee_shm_op_mmap,
 92};
 93
 94struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
 95{
 96	struct tee_device *teedev = ctx->teedev;
 97	struct tee_shm_pool_mgr *poolm = NULL;
 98	struct tee_shm *shm;
 99	void *ret;
100	int rc;
101
102	if (!(flags & TEE_SHM_MAPPED)) {
103		dev_err(teedev->dev.parent,
104			"only mapped allocations supported\n");
105		return ERR_PTR(-EINVAL);
106	}
107
108	if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
109		dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
110		return ERR_PTR(-EINVAL);
111	}
112
113	if (!tee_device_get(teedev))
114		return ERR_PTR(-EINVAL);
115
116	if (!teedev->pool) {
117		/* teedev has been detached from driver */
118		ret = ERR_PTR(-EINVAL);
119		goto err_dev_put;
120	}
121
122	shm = kzalloc(sizeof(*shm), GFP_KERNEL);
123	if (!shm) {
124		ret = ERR_PTR(-ENOMEM);
125		goto err_dev_put;
126	}
127
128	shm->flags = flags | TEE_SHM_POOL;
 
 
 
 
 
 
 
 
 
129	shm->ctx = ctx;
130	if (flags & TEE_SHM_DMA_BUF)
131		poolm = teedev->pool->dma_buf_mgr;
132	else
133		poolm = teedev->pool->private_mgr;
134
135	rc = poolm->ops->alloc(poolm, shm, size);
136	if (rc) {
137		ret = ERR_PTR(rc);
138		goto err_kfree;
139	}
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
142	if (flags & TEE_SHM_DMA_BUF) {
143		DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
 
 
 
144
 
 
145		mutex_lock(&teedev->mutex);
146		shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
147		mutex_unlock(&teedev->mutex);
148		if (shm->id < 0) {
149			ret = ERR_PTR(shm->id);
150			goto err_pool_free;
151		}
152
153		exp_info.ops = &tee_shm_dma_buf_ops;
154		exp_info.size = shm->size;
155		exp_info.flags = O_RDWR;
156		exp_info.priv = shm;
157
158		shm->dmabuf = dma_buf_export(&exp_info);
159		if (IS_ERR(shm->dmabuf)) {
160			ret = ERR_CAST(shm->dmabuf);
161			goto err_rem;
162		}
163	}
164
165	teedev_ctx_get(ctx);
 
 
 
 
 
 
166
167	return shm;
168err_rem:
169	if (flags & TEE_SHM_DMA_BUF) {
170		mutex_lock(&teedev->mutex);
171		idr_remove(&teedev->idr, shm->id);
172		mutex_unlock(&teedev->mutex);
173	}
174err_pool_free:
175	poolm->ops->free(poolm, shm);
176err_kfree:
177	kfree(shm);
178err_dev_put:
179	tee_device_put(teedev);
180	return ret;
181}
182EXPORT_SYMBOL_GPL(tee_shm_alloc);
183
184struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
185				 size_t length, u32 flags)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186{
187	struct tee_device *teedev = ctx->teedev;
188	const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
189	const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
190	struct tee_shm *shm;
 
 
 
191	void *ret;
192	int rc;
193	int num_pages;
194	unsigned long start;
195
196	if (flags != req_user_flags && flags != req_kernel_flags)
197		return ERR_PTR(-ENOTSUPP);
198
199	if (!tee_device_get(teedev))
200		return ERR_PTR(-EINVAL);
201
202	if (!teedev->desc->ops->shm_register ||
203	    !teedev->desc->ops->shm_unregister) {
204		tee_device_put(teedev);
205		return ERR_PTR(-ENOTSUPP);
206	}
207
208	teedev_ctx_get(ctx);
209
210	shm = kzalloc(sizeof(*shm), GFP_KERNEL);
211	if (!shm) {
212		ret = ERR_PTR(-ENOMEM);
213		goto err;
214	}
215
216	shm->flags = flags | TEE_SHM_REGISTER;
 
217	shm->ctx = ctx;
218	shm->id = -1;
219	addr = untagged_addr(addr);
220	start = rounddown(addr, PAGE_SIZE);
221	shm->offset = addr - start;
222	shm->size = length;
223	num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
 
 
 
224	shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
225	if (!shm->pages) {
226		ret = ERR_PTR(-ENOMEM);
227		goto err;
228	}
229
230	if (flags & TEE_SHM_USER_MAPPED) {
231		rc = get_user_pages_fast(start, num_pages, FOLL_WRITE,
232					 shm->pages);
233	} else {
234		struct kvec *kiov;
235		int i;
236
237		kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL);
238		if (!kiov) {
239			ret = ERR_PTR(-ENOMEM);
240			goto err;
241		}
242
243		for (i = 0; i < num_pages; i++) {
244			kiov[i].iov_base = (void *)(start + i * PAGE_SIZE);
245			kiov[i].iov_len = PAGE_SIZE;
246		}
247
248		rc = get_kernel_pages(kiov, num_pages, 0, shm->pages);
249		kfree(kiov);
250	}
251	if (rc > 0)
252		shm->num_pages = rc;
253	if (rc != num_pages) {
254		if (rc >= 0)
255			rc = -ENOMEM;
256		ret = ERR_PTR(rc);
257		goto err;
258	}
259
260	mutex_lock(&teedev->mutex);
261	shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
262	mutex_unlock(&teedev->mutex);
 
 
 
263
264	if (shm->id < 0) {
265		ret = ERR_PTR(shm->id);
266		goto err;
267	}
268
269	rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
270					     shm->num_pages, start);
271	if (rc) {
272		ret = ERR_PTR(rc);
273		goto err;
274	}
275
276	if (flags & TEE_SHM_DMA_BUF) {
277		DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
278
279		exp_info.ops = &tee_shm_dma_buf_ops;
280		exp_info.size = shm->size;
281		exp_info.flags = O_RDWR;
282		exp_info.priv = shm;
283
284		shm->dmabuf = dma_buf_export(&exp_info);
285		if (IS_ERR(shm->dmabuf)) {
286			ret = ERR_CAST(shm->dmabuf);
287			teedev->desc->ops->shm_unregister(ctx, shm);
288			goto err;
289		}
290	}
291
292	return shm;
293err:
294	if (shm) {
295		size_t n;
296
297		if (shm->id >= 0) {
298			mutex_lock(&teedev->mutex);
299			idr_remove(&teedev->idr, shm->id);
300			mutex_unlock(&teedev->mutex);
301		}
302		if (shm->pages) {
303			for (n = 0; n < shm->num_pages; n++)
304				put_page(shm->pages[n]);
305			kfree(shm->pages);
306		}
307	}
308	kfree(shm);
 
309	teedev_ctx_put(ctx);
 
310	tee_device_put(teedev);
311	return ret;
312}
313EXPORT_SYMBOL_GPL(tee_shm_register);
314
315/**
316 * tee_shm_get_fd() - Increase reference count and return file descriptor
317 * @shm:	Shared memory handle
318 * @returns user space file descriptor to shared memory
 
 
 
319 */
320int tee_shm_get_fd(struct tee_shm *shm)
 
321{
322	int fd;
 
 
 
 
 
323
324	if (!(shm->flags & TEE_SHM_DMA_BUF))
325		return -EINVAL;
326
327	get_dma_buf(shm->dmabuf);
328	fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
329	if (fd < 0)
330		dma_buf_put(shm->dmabuf);
331	return fd;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332}
333
334/**
335 * tee_shm_free() - Free shared memory
336 * @shm:	Handle to shared memory to free
 
 
 
 
 
337 */
338void tee_shm_free(struct tee_shm *shm)
 
 
339{
340	/*
341	 * dma_buf_put() decreases the dmabuf reference counter and will
342	 * call tee_shm_release() when the last reference is gone.
343	 *
344	 * In the case of driver private memory we call tee_shm_release
345	 * directly instead as it doesn't have a reference counter.
346	 */
347	if (shm->flags & TEE_SHM_DMA_BUF)
348		dma_buf_put(shm->dmabuf);
349	else
350		tee_shm_release(shm);
351}
352EXPORT_SYMBOL_GPL(tee_shm_free);
353
354/**
355 * tee_shm_va2pa() - Get physical address of a virtual address
356 * @shm:	Shared memory handle
357 * @va:		Virtual address to tranlsate
358 * @pa:		Returned physical address
359 * @returns 0 on success and < 0 on failure
360 */
361int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
362{
363	if (!(shm->flags & TEE_SHM_MAPPED))
364		return -EINVAL;
365	/* Check that we're in the range of the shm */
366	if ((char *)va < (char *)shm->kaddr)
 
 
 
 
 
 
 
367		return -EINVAL;
368	if ((char *)va >= ((char *)shm->kaddr + shm->size))
 
 
369		return -EINVAL;
370
371	return tee_shm_get_pa(
372			shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
373}
374EXPORT_SYMBOL_GPL(tee_shm_va2pa);
 
 
 
 
 
375
376/**
377 * tee_shm_pa2va() - Get virtual address of a physical address
378 * @shm:	Shared memory handle
379 * @pa:		Physical address to tranlsate
380 * @va:		Returned virtual address
381 * @returns 0 on success and < 0 on failure
382 */
383int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
384{
385	if (!(shm->flags & TEE_SHM_MAPPED))
386		return -EINVAL;
387	/* Check that we're in the range of the shm */
388	if (pa < shm->paddr)
389		return -EINVAL;
390	if (pa >= (shm->paddr + shm->size))
391		return -EINVAL;
392
393	if (va) {
394		void *v = tee_shm_get_va(shm, pa - shm->paddr);
 
 
 
 
 
395
396		if (IS_ERR(v))
397			return PTR_ERR(v);
398		*va = v;
399	}
400	return 0;
 
 
401}
402EXPORT_SYMBOL_GPL(tee_shm_pa2va);
403
404/**
405 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
406 * @shm:	Shared memory handle
407 * @offs:	Offset from start of this shared memory
408 * @returns virtual address of the shared memory + offs if offs is within
409 *	the bounds of this shared memory, else an ERR_PTR
410 */
411void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
412{
413	if (!(shm->flags & TEE_SHM_MAPPED))
414		return ERR_PTR(-EINVAL);
415	if (offs >= shm->size)
416		return ERR_PTR(-EINVAL);
417	return (char *)shm->kaddr + offs;
418}
419EXPORT_SYMBOL_GPL(tee_shm_get_va);
420
421/**
422 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
423 * @shm:	Shared memory handle
424 * @offs:	Offset from start of this shared memory
425 * @pa:		Physical address to return
426 * @returns 0 if offs is within the bounds of this shared memory, else an
427 *	error code.
428 */
429int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
430{
431	if (offs >= shm->size)
432		return -EINVAL;
433	if (pa)
434		*pa = shm->paddr + offs;
435	return 0;
436}
437EXPORT_SYMBOL_GPL(tee_shm_get_pa);
438
439/**
440 * tee_shm_get_from_id() - Find shared memory object and increase reference
441 * count
442 * @ctx:	Context owning the shared memory
443 * @id:		Id of shared memory object
444 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
445 */
446struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
447{
448	struct tee_device *teedev;
449	struct tee_shm *shm;
450
451	if (!ctx)
452		return ERR_PTR(-EINVAL);
453
454	teedev = ctx->teedev;
455	mutex_lock(&teedev->mutex);
456	shm = idr_find(&teedev->idr, id);
 
 
 
 
 
457	if (!shm || shm->ctx != ctx)
458		shm = ERR_PTR(-EINVAL);
459	else if (shm->flags & TEE_SHM_DMA_BUF)
460		get_dma_buf(shm->dmabuf);
461	mutex_unlock(&teedev->mutex);
462	return shm;
463}
464EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
465
466/**
467 * tee_shm_put() - Decrease reference count on a shared memory handle
468 * @shm:	Shared memory handle
469 */
470void tee_shm_put(struct tee_shm *shm)
471{
472	if (shm->flags & TEE_SHM_DMA_BUF)
473		dma_buf_put(shm->dmabuf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474}
475EXPORT_SYMBOL_GPL(tee_shm_put);
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2015-2017, 2019-2021 Linaro Limited
  4 */
  5#include <linux/anon_inodes.h>
  6#include <linux/device.h>
 
 
  7#include <linux/idr.h>
  8#include <linux/io.h>
  9#include <linux/mm.h>
 10#include <linux/sched.h>
 11#include <linux/slab.h>
 12#include <linux/tee_core.h>
 13#include <linux/uaccess.h>
 14#include <linux/uio.h>
 15#include <linux/highmem.h>
 16#include "tee_private.h"
 17
 18static void shm_put_kernel_pages(struct page **pages, size_t page_count)
 19{
 20	size_t n;
 21
 22	for (n = 0; n < page_count; n++)
 23		put_page(pages[n]);
 24}
 
 
 25
 26static void shm_get_kernel_pages(struct page **pages, size_t page_count)
 27{
 28	size_t n;
 29
 30	for (n = 0; n < page_count; n++)
 31		get_page(pages[n]);
 32}
 33
 34static void release_registered_pages(struct tee_shm *shm)
 35{
 36	if (shm->pages) {
 37		if (shm->flags & TEE_SHM_USER_MAPPED)
 38			unpin_user_pages(shm->pages, shm->num_pages);
 39		else
 40			shm_put_kernel_pages(shm->pages, shm->num_pages);
 41
 42		kfree(shm->pages);
 43	}
 44}
 45
 46static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
 47{
 48	if (shm->flags & TEE_SHM_POOL) {
 49		teedev->pool->ops->free(teedev->pool, shm);
 50	} else if (shm->flags & TEE_SHM_DYNAMIC) {
 51		int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
 52
 53		if (rc)
 54			dev_err(teedev->dev.parent,
 55				"unregister shm %p failed: %d", shm, rc);
 56
 57		release_registered_pages(shm);
 
 
 
 58	}
 59
 60	teedev_ctx_put(shm->ctx);
 61
 62	kfree(shm);
 63
 64	tee_device_put(teedev);
 65}
 66
 67static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t size,
 68					size_t align, u32 flags, int id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 69{
 70	struct tee_device *teedev = ctx->teedev;
 
 71	struct tee_shm *shm;
 72	void *ret;
 73	int rc;
 74
 
 
 
 
 
 
 
 
 
 
 
 75	if (!tee_device_get(teedev))
 76		return ERR_PTR(-EINVAL);
 77
 78	if (!teedev->pool) {
 79		/* teedev has been detached from driver */
 80		ret = ERR_PTR(-EINVAL);
 81		goto err_dev_put;
 82	}
 83
 84	shm = kzalloc(sizeof(*shm), GFP_KERNEL);
 85	if (!shm) {
 86		ret = ERR_PTR(-ENOMEM);
 87		goto err_dev_put;
 88	}
 89
 90	refcount_set(&shm->refcount, 1);
 91	shm->flags = flags;
 92	shm->id = id;
 93
 94	/*
 95	 * We're assigning this as it is needed if the shm is to be
 96	 * registered. If this function returns OK then the caller expected
 97	 * to call teedev_ctx_get() or clear shm->ctx in case it's not
 98	 * needed any longer.
 99	 */
100	shm->ctx = ctx;
 
 
 
 
101
102	rc = teedev->pool->ops->alloc(teedev->pool, shm, size, align);
103	if (rc) {
104		ret = ERR_PTR(rc);
105		goto err_kfree;
106	}
107
108	teedev_ctx_get(ctx);
109	return shm;
110err_kfree:
111	kfree(shm);
112err_dev_put:
113	tee_device_put(teedev);
114	return ret;
115}
116
117/**
118 * tee_shm_alloc_user_buf() - Allocate shared memory for user space
119 * @ctx:	Context that allocates the shared memory
120 * @size:	Requested size of shared memory
121 *
122 * Memory allocated as user space shared memory is automatically freed when
123 * the TEE file pointer is closed. The primary usage of this function is
124 * when the TEE driver doesn't support registering ordinary user space
125 * memory.
126 *
127 * @returns a pointer to 'struct tee_shm'
128 */
129struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size)
130{
131	u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
132	struct tee_device *teedev = ctx->teedev;
133	struct tee_shm *shm;
134	void *ret;
135	int id;
136
137	mutex_lock(&teedev->mutex);
138	id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
139	mutex_unlock(&teedev->mutex);
140	if (id < 0)
141		return ERR_PTR(id);
142
143	shm = shm_alloc_helper(ctx, size, PAGE_SIZE, flags, id);
144	if (IS_ERR(shm)) {
145		mutex_lock(&teedev->mutex);
146		idr_remove(&teedev->idr, id);
147		mutex_unlock(&teedev->mutex);
148		return shm;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149	}
150
151	mutex_lock(&teedev->mutex);
152	ret = idr_replace(&teedev->idr, shm, id);
153	mutex_unlock(&teedev->mutex);
154	if (IS_ERR(ret)) {
155		tee_shm_free(shm);
156		return ret;
157	}
158
159	return shm;
 
 
 
 
 
 
 
 
 
 
 
 
 
160}
 
161
162/**
163 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
164 * @ctx:	Context that allocates the shared memory
165 * @size:	Requested size of shared memory
166 *
167 * The returned memory registered in secure world and is suitable to be
168 * passed as a memory buffer in parameter argument to
169 * tee_client_invoke_func(). The memory allocated is later freed with a
170 * call to tee_shm_free().
171 *
172 * @returns a pointer to 'struct tee_shm'
173 */
174struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
175{
176	u32 flags = TEE_SHM_DYNAMIC | TEE_SHM_POOL;
177
178	return shm_alloc_helper(ctx, size, PAGE_SIZE, flags, -1);
179}
180EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
181
182/**
183 * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared
184 *			      kernel buffer
185 * @ctx:	Context that allocates the shared memory
186 * @size:	Requested size of shared memory
187 *
188 * This function returns similar shared memory as
189 * tee_shm_alloc_kernel_buf(), but with the difference that the memory
190 * might not be registered in secure world in case the driver supports
191 * passing memory not registered in advance.
192 *
193 * This function should normally only be used internally in the TEE
194 * drivers.
195 *
196 * @returns a pointer to 'struct tee_shm'
197 */
198struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
199{
200	u32 flags = TEE_SHM_PRIV | TEE_SHM_POOL;
201
202	return shm_alloc_helper(ctx, size, sizeof(long) * 2, flags, -1);
203}
204EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
205
206int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
207			     int (*shm_register)(struct tee_context *ctx,
208						 struct tee_shm *shm,
209						 struct page **pages,
210						 size_t num_pages,
211						 unsigned long start))
212{
213	size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
214	struct page **pages;
215	unsigned int i;
216	int rc = 0;
217
218	/*
219	 * Ignore alignment since this is already going to be page aligned
220	 * and there's no need for any larger alignment.
221	 */
222	shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
223				       GFP_KERNEL | __GFP_ZERO);
224	if (!shm->kaddr)
225		return -ENOMEM;
226
227	shm->paddr = virt_to_phys(shm->kaddr);
228	shm->size = nr_pages * PAGE_SIZE;
229
230	pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
231	if (!pages) {
232		rc = -ENOMEM;
233		goto err;
234	}
235
236	for (i = 0; i < nr_pages; i++)
237		pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
238
239	shm->pages = pages;
240	shm->num_pages = nr_pages;
241
242	if (shm_register) {
243		rc = shm_register(shm->ctx, shm, pages, nr_pages,
244				  (unsigned long)shm->kaddr);
245		if (rc)
246			goto err;
247	}
248
249	return 0;
250err:
251	free_pages_exact(shm->kaddr, shm->size);
252	shm->kaddr = NULL;
253	return rc;
254}
255EXPORT_SYMBOL_GPL(tee_dyn_shm_alloc_helper);
256
257void tee_dyn_shm_free_helper(struct tee_shm *shm,
258			     int (*shm_unregister)(struct tee_context *ctx,
259						   struct tee_shm *shm))
260{
261	if (shm_unregister)
262		shm_unregister(shm->ctx, shm);
263	free_pages_exact(shm->kaddr, shm->size);
264	shm->kaddr = NULL;
265	kfree(shm->pages);
266	shm->pages = NULL;
267}
268EXPORT_SYMBOL_GPL(tee_dyn_shm_free_helper);
269
270static struct tee_shm *
271register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags,
272		    int id)
273{
274	struct tee_device *teedev = ctx->teedev;
 
 
275	struct tee_shm *shm;
276	unsigned long start, addr;
277	size_t num_pages, off;
278	ssize_t len;
279	void *ret;
280	int rc;
 
 
 
 
 
281
282	if (!tee_device_get(teedev))
283		return ERR_PTR(-EINVAL);
284
285	if (!teedev->desc->ops->shm_register ||
286	    !teedev->desc->ops->shm_unregister) {
287		ret = ERR_PTR(-ENOTSUPP);
288		goto err_dev_put;
289	}
290
291	teedev_ctx_get(ctx);
292
293	shm = kzalloc(sizeof(*shm), GFP_KERNEL);
294	if (!shm) {
295		ret = ERR_PTR(-ENOMEM);
296		goto err_ctx_put;
297	}
298
299	refcount_set(&shm->refcount, 1);
300	shm->flags = flags;
301	shm->ctx = ctx;
302	shm->id = id;
303	addr = untagged_addr((unsigned long)iter_iov_addr(iter));
304	start = rounddown(addr, PAGE_SIZE);
305	num_pages = iov_iter_npages(iter, INT_MAX);
306	if (!num_pages) {
307		ret = ERR_PTR(-ENOMEM);
308		goto err_ctx_put;
309	}
310
311	shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
312	if (!shm->pages) {
313		ret = ERR_PTR(-ENOMEM);
314		goto err_free_shm;
315	}
316
317	len = iov_iter_extract_pages(iter, &shm->pages, LONG_MAX, num_pages, 0,
318				     &off);
319	if (unlikely(len <= 0)) {
320		ret = len ? ERR_PTR(len) : ERR_PTR(-ENOMEM);
321		goto err_free_shm_pages;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322	}
323
324	/*
325	 * iov_iter_extract_kvec_pages does not get reference on the pages,
326	 * get a reference on them.
327	 */
328	if (iov_iter_is_kvec(iter))
329		shm_get_kernel_pages(shm->pages, num_pages);
330
331	shm->offset = off;
332	shm->size = len;
333	shm->num_pages = num_pages;
 
334
335	rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
336					     shm->num_pages, start);
337	if (rc) {
338		ret = ERR_PTR(rc);
339		goto err_put_shm_pages;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340	}
341
342	return shm;
343err_put_shm_pages:
344	if (!iov_iter_is_kvec(iter))
345		unpin_user_pages(shm->pages, shm->num_pages);
346	else
347		shm_put_kernel_pages(shm->pages, shm->num_pages);
348err_free_shm_pages:
349	kfree(shm->pages);
350err_free_shm:
 
 
 
 
 
 
 
351	kfree(shm);
352err_ctx_put:
353	teedev_ctx_put(ctx);
354err_dev_put:
355	tee_device_put(teedev);
356	return ret;
357}
 
358
359/**
360 * tee_shm_register_user_buf() - Register a userspace shared memory buffer
361 * @ctx:	Context that registers the shared memory
362 * @addr:	The userspace address of the shared buffer
363 * @length:	Length of the shared buffer
364 *
365 * @returns a pointer to 'struct tee_shm'
366 */
367struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
368					  unsigned long addr, size_t length)
369{
370	u32 flags = TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC;
371	struct tee_device *teedev = ctx->teedev;
372	struct tee_shm *shm;
373	struct iov_iter iter;
374	void *ret;
375	int id;
376
377	if (!access_ok((void __user *)addr, length))
378		return ERR_PTR(-EFAULT);
379
380	mutex_lock(&teedev->mutex);
381	id = idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL);
382	mutex_unlock(&teedev->mutex);
383	if (id < 0)
384		return ERR_PTR(id);
385
386	iov_iter_ubuf(&iter, ITER_DEST,  (void __user *)addr, length);
387	shm = register_shm_helper(ctx, &iter, flags, id);
388	if (IS_ERR(shm)) {
389		mutex_lock(&teedev->mutex);
390		idr_remove(&teedev->idr, id);
391		mutex_unlock(&teedev->mutex);
392		return shm;
393	}
394
395	mutex_lock(&teedev->mutex);
396	ret = idr_replace(&teedev->idr, shm, id);
397	mutex_unlock(&teedev->mutex);
398	if (IS_ERR(ret)) {
399		tee_shm_free(shm);
400		return ret;
401	}
402
403	return shm;
404}
405
406/**
407 * tee_shm_register_kernel_buf() - Register kernel memory to be shared with
408 *				   secure world
409 * @ctx:	Context that registers the shared memory
410 * @addr:	The buffer
411 * @length:	Length of the buffer
412 *
413 * @returns a pointer to 'struct tee_shm'
414 */
415
416struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
417					    void *addr, size_t length)
418{
419	u32 flags = TEE_SHM_DYNAMIC;
420	struct kvec kvec;
421	struct iov_iter iter;
422
423	kvec.iov_base = addr;
424	kvec.iov_len = length;
425	iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, length);
426
427	return register_shm_helper(ctx, &iter, flags, -1);
 
 
428}
429EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf);
430
431static int tee_shm_fop_release(struct inode *inode, struct file *filp)
 
 
 
 
 
 
 
432{
433	tee_shm_put(filp->private_data);
434	return 0;
435}
436
437static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
438{
439	struct tee_shm *shm = filp->private_data;
440	size_t size = vma->vm_end - vma->vm_start;
441
442	/* Refuse sharing shared memory provided by application */
443	if (shm->flags & TEE_SHM_USER_MAPPED)
444		return -EINVAL;
445
446	/* check for overflowing the buffer's size */
447	if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
448		return -EINVAL;
449
450	return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
451			       size, vma->vm_page_prot);
452}
453
454static const struct file_operations tee_shm_fops = {
455	.owner = THIS_MODULE,
456	.release = tee_shm_fop_release,
457	.mmap = tee_shm_fop_mmap,
458};
459
460/**
461 * tee_shm_get_fd() - Increase reference count and return file descriptor
462 * @shm:	Shared memory handle
463 * @returns user space file descriptor to shared memory
 
 
464 */
465int tee_shm_get_fd(struct tee_shm *shm)
466{
467	int fd;
468
469	if (shm->id < 0)
 
 
 
470		return -EINVAL;
471
472	/* matched by tee_shm_put() in tee_shm_op_release() */
473	refcount_inc(&shm->refcount);
474	fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
475	if (fd < 0)
476		tee_shm_put(shm);
477	return fd;
478}
479
480/**
481 * tee_shm_free() - Free shared memory
482 * @shm:	Handle to shared memory to free
483 */
484void tee_shm_free(struct tee_shm *shm)
485{
486	tee_shm_put(shm);
487}
488EXPORT_SYMBOL_GPL(tee_shm_free);
489
490/**
491 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
492 * @shm:	Shared memory handle
493 * @offs:	Offset from start of this shared memory
494 * @returns virtual address of the shared memory + offs if offs is within
495 *	the bounds of this shared memory, else an ERR_PTR
496 */
497void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
498{
499	if (!shm->kaddr)
500		return ERR_PTR(-EINVAL);
501	if (offs >= shm->size)
502		return ERR_PTR(-EINVAL);
503	return (char *)shm->kaddr + offs;
504}
505EXPORT_SYMBOL_GPL(tee_shm_get_va);
506
507/**
508 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
509 * @shm:	Shared memory handle
510 * @offs:	Offset from start of this shared memory
511 * @pa:		Physical address to return
512 * @returns 0 if offs is within the bounds of this shared memory, else an
513 *	error code.
514 */
515int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
516{
517	if (offs >= shm->size)
518		return -EINVAL;
519	if (pa)
520		*pa = shm->paddr + offs;
521	return 0;
522}
523EXPORT_SYMBOL_GPL(tee_shm_get_pa);
524
525/**
526 * tee_shm_get_from_id() - Find shared memory object and increase reference
527 * count
528 * @ctx:	Context owning the shared memory
529 * @id:		Id of shared memory object
530 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
531 */
532struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
533{
534	struct tee_device *teedev;
535	struct tee_shm *shm;
536
537	if (!ctx)
538		return ERR_PTR(-EINVAL);
539
540	teedev = ctx->teedev;
541	mutex_lock(&teedev->mutex);
542	shm = idr_find(&teedev->idr, id);
543	/*
544	 * If the tee_shm was found in the IDR it must have a refcount
545	 * larger than 0 due to the guarantee in tee_shm_put() below. So
546	 * it's safe to use refcount_inc().
547	 */
548	if (!shm || shm->ctx != ctx)
549		shm = ERR_PTR(-EINVAL);
550	else
551		refcount_inc(&shm->refcount);
552	mutex_unlock(&teedev->mutex);
553	return shm;
554}
555EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
556
557/**
558 * tee_shm_put() - Decrease reference count on a shared memory handle
559 * @shm:	Shared memory handle
560 */
561void tee_shm_put(struct tee_shm *shm)
562{
563	struct tee_device *teedev = shm->ctx->teedev;
564	bool do_release = false;
565
566	mutex_lock(&teedev->mutex);
567	if (refcount_dec_and_test(&shm->refcount)) {
568		/*
569		 * refcount has reached 0, we must now remove it from the
570		 * IDR before releasing the mutex. This will guarantee that
571		 * the refcount_inc() in tee_shm_get_from_id() never starts
572		 * from 0.
573		 */
574		if (shm->id >= 0)
575			idr_remove(&teedev->idr, shm->id);
576		do_release = true;
577	}
578	mutex_unlock(&teedev->mutex);
579
580	if (do_release)
581		tee_shm_release(teedev, shm);
582}
583EXPORT_SYMBOL_GPL(tee_shm_put);