Skip to content

Commit b65b1a4

Browse files
committed
reszta helper (user_remove_memory add_user_memory)
1 parent 0accb0e commit b65b1a4

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

zephyr/include/rtos/userspace_helper.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212
#ifndef __ZEPHYR_LIB_USERSPACE_HELPER_H__
1313
#define __ZEPHYR_LIB_USERSPACE_HELPER_H__
1414

15-
#ifdef CONFIG_USERSPACE
15+
#include <zephyr/sys/sys_heap.h>
16+
#include <zephyr/sys/sem.h>
17+
#include <zephyr/app_memory/app_memdomain.h>
18+
19+
#ifndef CONFIG_USERSPACE
20+
#define APP_TASK_DATA
21+
#else
1622
#define DRV_HEAP_SIZE ALIGN_UP(CONFIG_SOF_ZEPHYR_USERSPACE_MODULE_HEAP_SIZE, \
1723
CONFIG_MM_DRV_PAGE_SIZE)
1824

25+
#define APP_TASK_BSS K_APP_BMEM(common_partition)
26+
#define APP_TASK_DATA K_APP_DMEM(common_partition)
27+
1928
struct processing_module;
2029
struct userspace_context;
2130

@@ -33,6 +42,40 @@ struct userspace_context;
3342
*/
3443
struct sys_heap *drv_heap_init(void);
3544

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

zephyr/lib/userspace_helper.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,47 @@ int user_stack_free(void *p_stack)
148148
return k_thread_stack_free((__sparse_force void *)p_stack);
149149
}
150150

151+
int user_add_memory(struct k_mem_domain *domain, uintptr_t addr, size_t size, uint32_t attr)
152+
{
153+
uintptr_t addr_aligned;
154+
size_t size_aligned;
155+
int ret;
156+
157+
k_mem_region_align(&addr_aligned, &size_aligned, (uintptr_t)addr, size, HOST_PAGE_SIZE);
158+
/* Define parameters for partition */
159+
struct k_mem_partition part;
160+
part.start = addr_aligned;
161+
part.size = size_aligned;
162+
part.attr = attr;
163+
ret = k_mem_domain_add_partition(domain, &part);
164+
/* -EINVAL means that given page is already in the domain */
165+
/* Not an error case for us. */
166+
if (ret == -EINVAL)
167+
return 0;
168+
169+
return ret;
170+
}
171+
172+
int user_remove_memory(struct k_mem_domain *domain, uintptr_t addr, size_t size)
173+
{
174+
uintptr_t addr_aligned;
175+
size_t size_aligned;
176+
int ret;
177+
178+
/* Define parameters for user_partition */
179+
k_mem_region_align(&addr_aligned, &size_aligned, (uintptr_t)addr, size, HOST_PAGE_SIZE);
180+
struct k_mem_partition part;
181+
part.start = addr_aligned;
182+
part.size = size_aligned;
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+
151192
int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod)
152193
{
153194
struct k_mem_domain *comp_dom = mod->user_ctx->comp_dom;

0 commit comments

Comments
 (0)