Skip to content

Commit 24c6700

Browse files
jxsteltersoftwarecki
authored andcommitted
userspace: helper: Add helper functions for memory domain manipulation
Add helper functions user_add_memory and user_remove_memory that allows to add/remove memory regions from the memory domain. The purpose of these functions is to round addresses appropriately for the memory domain. Signed-off-by: Jaroslaw Stelter <Jaroslaw.Stelter@intel.com> Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 1c4dbc1 commit 24c6700

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

zephyr/include/rtos/userspace_helper.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@ struct userspace_context;
4040
*/
4141
struct sys_heap *module_driver_heap_init(void);
4242

43+
/**
44+
* Add memory region to non-privileged module memory domain.
45+
* @param domain - pointer to the modules memory domain.
46+
* @param addr - pointer to memory region start
47+
* @param size - size of the memory region
48+
* @param attr - memory region access attributes
49+
*
50+
* @return 0 for success, error otherwise.
51+
*
52+
* @note
53+
* Function used only when CONFIG_USERSPACE is set.
54+
* Function adds page aligned region to the memory domain.
55+
* Caller should take care to not expose other data than these
56+
* intended to be shared with the module.
57+
*/
58+
int user_add_memory(struct k_mem_domain *domain, uintptr_t addr, size_t size, uint32_t attr);
59+
60+
/**
61+
* Remove memory region from non-privileged module memory domain.
62+
* @param domain - pointer to the modules memory domain.
63+
* @param addr - pointer to memory region start
64+
* @param size - size of the memory region
65+
*
66+
* @return 0 for success, error otherwise.
67+
*
68+
* @note
69+
* Function used only when CONFIG_USERSPACE is set.
70+
* Function removes previously added page aligned region
71+
* from the memory domain.
72+
*/
73+
int user_remove_memory(struct k_mem_domain *domain, uintptr_t addr, size_t size);
74+
4375
/**
4476
* Add DP scheduler created thread to module memory domain.
4577
* @param thread_id - id of thread to be added to memory domain.

zephyr/lib/userspace_helper.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,39 @@ int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod)
156156
return k_mem_domain_add_thread(comp_dom, thread_id);
157157
}
158158

159+
int user_add_memory(struct k_mem_domain *domain, uintptr_t addr, size_t size, uint32_t attr)
160+
{
161+
struct k_mem_partition part;
162+
int ret;
163+
164+
k_mem_region_align(&part.start, &part.size, addr, size, HOST_PAGE_SIZE);
165+
/* Define parameters for partition */
166+
part.attr = attr;
167+
ret = k_mem_domain_add_partition(domain, &part);
168+
/* -EINVAL means that given page is already in the domain */
169+
/* Not an error case for us. */
170+
if (ret == -EINVAL)
171+
return 0;
172+
173+
return ret;
174+
}
175+
176+
int user_remove_memory(struct k_mem_domain *domain, uintptr_t addr, size_t size)
177+
{
178+
struct k_mem_partition part;
179+
int ret;
180+
181+
/* Define parameters for user_partition */
182+
k_mem_region_align(&part.start, &part.size, addr, size, HOST_PAGE_SIZE);
183+
ret = k_mem_domain_remove_partition(domain, &part);
184+
/* -ENOENT means that given partition is already removed */
185+
/* Not an error case for us. */
186+
if (ret == -ENOENT)
187+
return 0;
188+
189+
return ret;
190+
}
191+
159192
#else /* CONFIG_USERSPACE */
160193

161194
void *user_stack_allocate(size_t stack_size, uint32_t options)

0 commit comments

Comments
 (0)