xref: /optee_os/core/kernel/msg_param.c (revision 18c5148d357e51235bc842b7826ff6e8da109902)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017, EPAM Systems
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <optee_msg.h>
30 #include <stdio.h>
31 #include <types_ext.h>
32 #include <kernel/msg_param.h>
33 #include <mm/mobj.h>
34 
35 /**
36  * msg_param_extract_pages() - extract list of pages from
37  * OPTEE_MSG_ATTR_NONCONTIG buffer.
38  *
39  * @buffer:	pointer to parameters array
40  * @pages:	output array of page addresses
41  * @num_pages:  number of pages in array
42  *
43  * return:
44  *	true on success, false otherwise
45  *
46  * @buffer points to the physical address of a structure that can be viewed as
47  *
48  * struct page_data {
49  *   uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
50  *   uint64_t next_page_data;
51  * };
52  *
53  * So, it is a linked list of arrays, where each element of linked list fits
54  * exactly into one 4K page.
55  *
56  * This function extracts data from arrays into one array pointed by @pages
57  *
58  * @buffer points to data shared with normal world, so some precautions
59  * should be taken.
60  */
61 static bool msg_param_extract_pages(paddr_t buffer, paddr_t *pages,
62 				       size_t num_pages)
63 {
64 	size_t cnt;
65 	struct mobj *mobj;
66 	paddr_t page;
67 	uint64_t *va;
68 	bool ret = false;
69 
70 	if (buffer & SMALL_PAGE_MASK)
71 		return false;
72 
73 	/*
74 	 * There we map first page of array.
75 	 * mobj_mapped_shm_alloc() will check if page resides in nonsec ddr
76 	 */
77 	mobj = mobj_mapped_shm_alloc(&buffer, 1, 0, 0);
78 	if (!mobj)
79 		return false;
80 
81 	va = mobj_get_va(mobj, 0);
82 	assert(va);
83 
84 	for (cnt = 0; cnt < num_pages; cnt++, va++) {
85 		/*
86 		 * If we about to roll over page boundary, then last entry holds
87 		 * address of next page of array. Unmap current page and map
88 		 * next one
89 		 */
90 		if (!((vaddr_t)(va + 1) & SMALL_PAGE_MASK)) {
91 			page = *va;
92 			if (page & SMALL_PAGE_MASK)
93 				goto out;
94 
95 			mobj_free(mobj);
96 			mobj = mobj_mapped_shm_alloc(&page, 1, 0, 0);
97 			if (!mobj)
98 				goto out;
99 
100 			va = mobj_get_va(mobj, 0);
101 			assert(va);
102 		}
103 		pages[cnt] = *va;
104 		if (pages[cnt] & SMALL_PAGE_MASK)
105 			goto out;
106 	}
107 
108 	ret = true;
109 out:
110 	mobj_free(mobj);
111 	return ret;
112 }
113 
114 struct mobj *msg_param_mobj_from_noncontig(paddr_t buf_ptr, size_t size,
115 					   uint64_t shm_ref, bool map_buffer)
116 {
117 	struct mobj *mobj = NULL;
118 	paddr_t *pages;
119 	paddr_t page_offset;
120 	size_t num_pages;
121 
122 	page_offset = buf_ptr & SMALL_PAGE_MASK;
123 	num_pages = (size + page_offset - 1) / SMALL_PAGE_SIZE + 1;
124 
125 	pages = malloc(num_pages * sizeof(paddr_t));
126 	if (!pages)
127 		return NULL;
128 
129 	if (!msg_param_extract_pages(buf_ptr & ~SMALL_PAGE_MASK,
130 				     pages, num_pages))
131 		goto out;
132 
133 	if (map_buffer)
134 		mobj = mobj_mapped_shm_alloc(pages, num_pages, page_offset,
135 					     shm_ref);
136 	else
137 		mobj = mobj_reg_shm_alloc(pages, num_pages, page_offset,
138 					  shm_ref);
139 out:
140 	free(pages);
141 	return mobj;
142 }
143