diff --git a/doc/kernel/usermode/usermode_sharedmem.rst b/doc/kernel/usermode/usermode_sharedmem.rst index c6e7ec8cd89..e80837f7e18 100644 --- a/doc/kernel/usermode/usermode_sharedmem.rst +++ b/doc/kernel/usermode/usermode_sharedmem.rst @@ -46,19 +46,9 @@ where "part0" is the name then used to refer to that partition. This macro only creates a function and necessary data structures for the later "initialization". -To create a memory domain for the partition, the macro appmem_domain(dom0) -is called where "dom0" is the name then used for the memory domain. -To initialize the partition (effectively adding the partition -to a linked list), appmem_init_part_part0() is called. This is followed -by appmem_init_app_memory(), which walks all partitions in the linked -list and calculates the sizes for each partition. - -Once the partition is initialized, the domain can be -initialized with appmem_init_domain_dom0(part0) which initializes the -domain with partition part0. - -After the domain has been initialized, the current thread -can be added using appmem_add_thread_dom0(k_current_get()). +Once the partition is initialized, the standard memory domain APIs may +be used to add it to domains; the declared name is a k_mem_partition +symbol. Example: @@ -67,17 +57,18 @@ Example: /* create partition at top of file outside functions */ appmem_partition(part0); /* create domain */ - appmem_domain(dom0); + struct k_mem_domain dom0; /* assign variables to the domain */ - _app_dmem(dom0) int var1; - _app_bmem(dom0) static volatile int var2; + _app_dmem(part0) int var1; + _app_bmem(part0) static volatile int var2; int main() { appmem_init_part_part0(); appmem_init_app_memory(); - appmem_init_domain_dom0(part0); - appmem_add_thread_dom0(k_current_get()); + k_mem_domain_init(&dom0, 0, NULL) + k_mem_domain_add_partition(&dom0, part0); + k_mem_domain_add_thread(&dom0, k_current_get()); ... } @@ -89,12 +80,6 @@ app_macro_support.h: FOR_EACH(appmem_partition, part0, part1, part2); -or, for multiple domains, similarly: - -.. code-block:: c - - FOR_EACH(appmem_domain, dom0, dom1); - Similarly, the appmem_init_part_* can also be used in the macro: .. code-block:: c diff --git a/include/app_memory/app_memdomain.h b/include/app_memory/app_memdomain.h index aa202822c10..bb81aa3156f 100644 --- a/include/app_memory/app_memdomain.h +++ b/include/app_memory/app_memdomain.h @@ -55,11 +55,7 @@ struct app_region { char *dmem_start; char *bmem_start; -#ifdef CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT - char *smem_size; -#else u32_t smem_size; -#endif /* CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT */ u32_t dmem_size; u32_t bmem_size; struct k_mem_partition *partition; @@ -75,7 +71,7 @@ struct app_region { * calculate the region sizes. */ #define smem_size_declare(name) extern char data_smem_##name##_size[] -#define smem_size_assign(name) name.smem_size = data_smem_##name##_size +#define smem_size_assign(name) name##_region.smem_size = (u32_t)&data_smem_##name##_size #else #define smem_size_declare(name) #define smem_size_assign(name) @@ -87,48 +83,17 @@ struct app_region { smem_size_declare(name); \ _app_dmem_pad(name) char name##_dmem_pad; \ _app_bmem_pad(name) char name##_bmem_pad; \ - __kernel struct k_mem_partition mem_domain_##name; \ - __kernel struct app_region name; \ + __kernel struct k_mem_partition name; \ + __kernel struct app_region name##_region; \ static inline void appmem_init_part_##name(void) \ { \ - name.dmem_start = (char *)&data_smem_##name; \ - name.bmem_start = (char *)&data_smem_##name##b; \ + name##_region.dmem_start = (char *)&data_smem_##name; \ + name##_region.bmem_start = (char *)&data_smem_##name##b; \ smem_size_assign(name); \ - sys_dlist_append(&app_mem_list, &name.lnode); \ - mem_domain_##name.start = (u32_t) name.dmem_start; \ - mem_domain_##name.attr = K_MEM_PARTITION_P_RW_U_RW; \ - name.partition = &mem_domain_##name; \ - } - -/* - * A wrapper around the k_mem_domain_* functions. Goal here was - * to a) differentiate these operations from the k_mem_domain* - * functions, and b) to simply the usage and handling of data - * types (i.e. app_region, k_mem_domain, etc). - */ -#define appmem_domain(name) \ - __kernel struct k_mem_domain domain_##name; \ - static inline void appmem_add_thread_##name(k_tid_t thread) \ - { \ - k_mem_domain_add_thread(&domain_##name, thread); \ - } \ - static inline void appmem_rm_thread_##name(k_tid_t thread) \ - { \ - k_mem_domain_remove_thread(thread); \ - } \ - static inline void appmem_add_part_##name(struct app_region region) \ - { \ - k_mem_domain_add_partition(&domain_##name, \ - ®ion.partition[0]); \ - } \ - static inline void appmem_rm_part_##name(struct app_region region) \ - { \ - k_mem_domain_remove_partition(&domain_##name, \ - ®ion.partition[0]); \ - } \ - static inline void appmem_init_domain_##name(struct app_region region) \ - { \ - k_mem_domain_init(&domain_##name, 1, ®ion.partition); \ + sys_dlist_append(&app_mem_list, &name##_region.lnode); \ + name.start = (u32_t) name##_region.dmem_start; \ + name.attr = K_MEM_PARTITION_P_RW_U_RW; \ + name##_region.partition = &name; \ } /* diff --git a/samples/userspace/shared_mem/src/main.c b/samples/userspace/shared_mem/src/main.c index 7b069e2e9d9..a6f56aeff1f 100644 --- a/samples/userspace/shared_mem/src/main.c +++ b/samples/userspace/shared_mem/src/main.c @@ -29,7 +29,7 @@ /* prepare the memory partition structures */ FOR_EACH(appmem_partition, part0, part1, part2, part3, part4); /* prepare the memory domain structures */ -FOR_EACH(appmem_domain, dom0, dom1, dom2); +struct k_mem_domain dom0, dom1, dom2; /* each variable starts with a name defined in main.h * the names are symbolic for the memory partitions * purpose. @@ -99,7 +99,9 @@ _app_ct_d char ctMSG[] = "CT!\n"; void main(void) { - + struct k_mem_partition *dom1_parts[] = {&part2, &part1, &part3}; + struct k_mem_partition *dom2_parts[] = {&part4, &part3}; + struct k_mem_partition *dom0_parts[] = {&part0, &part1}; k_tid_t tPT, tENC, tCT; k_thread_access_grant(k_current_get(), &allforone); @@ -120,12 +122,9 @@ void main(void) k_thread_access_grant(tENC, &allforone); /* use K_FOREVER followed by k_thread_start*/ printk("ENC Thread Created %08X\n", (unsigned int) tENC); - appmem_init_domain_dom1(part2); - printk("init domain complete\n"); - appmem_add_part_dom1(part1); - appmem_add_part_dom1(part3); + k_mem_domain_init(&dom1, 3, dom1_parts); printk("Partitions added to dom1\n"); - appmem_add_thread_dom1(tENC); + k_mem_domain_add_thread(&dom1, tENC); printk("dom1 Created\n"); @@ -135,9 +134,8 @@ void main(void) K_FOREVER); k_thread_access_grant(tPT, &allforone); printk("PT Thread Created %08X\n", (unsigned int) tPT); - appmem_init_domain_dom0(part0); - appmem_add_part_dom0(part1); - appmem_add_thread_dom0(tPT); + k_mem_domain_init(&dom0, 2, dom0_parts); + k_mem_domain_add_thread(&dom0, tPT); printk("dom0 Created\n"); tCT = k_thread_create(&ct_thread, ct_stack, STACKSIZE, @@ -146,9 +144,8 @@ void main(void) K_FOREVER); k_thread_access_grant(tCT, &allforone); printk("CT Thread Created %08X\n", (unsigned int) tCT); - appmem_init_domain_dom2(part4); - appmem_add_part_dom2(part3); - appmem_add_thread_dom2(tCT); + k_mem_domain_init(&dom2, 2, dom2_parts); + k_mem_domain_add_thread(&dom2, tCT); printk("dom2 Created\n"); k_thread_start(&enc_thread); diff --git a/tests/kernel/mem_protect/userspace/src/main.c b/tests/kernel/mem_protect/userspace/src/main.c index 57b848e0fc1..d6430d215fa 100644 --- a/tests/kernel/mem_protect/userspace/src/main.c +++ b/tests/kernel/mem_protect/userspace/src/main.c @@ -45,7 +45,8 @@ FOR_EACH(appmem_partition, part0, part1, part2); * test suite, specifically. dom1 is for a specific test * in this test suite. */ -FOR_EACH(appmem_domain, dom0, dom1); +struct k_mem_domain dom0; +struct k_mem_domain dom1; _app_dmem(part0) static volatile bool give_uthread_end_sem; _app_dmem(part0) bool mem_access_check; @@ -656,19 +657,18 @@ static void shared_mem_thread(void) */ static void access_other_memdomain(void) { + struct k_mem_partition *parts[] = {&part0, &part2}; /* * Following tests the ability for a thread to access data * in a domain that it is denied. */ /* initialize domain dom1 with partition part2 */ - appmem_init_domain_dom1(part2); - /* add partition part0 for test globals */ - appmem_add_part_dom1(part0); - /* remove current thread from domain dom0 */ - appmem_rm_thread_dom0(k_current_get()); - /* initialize domain with current thread*/ - appmem_add_thread_dom1(k_current_get()); + k_mem_domain_init(&dom1, 2, parts); + + /* remove current thread from domain dom0 and add to dom1 */ + k_mem_domain_remove_thread(k_current_get()); + k_mem_domain_add_thread(&dom1, k_current_get()); /* Create user mode thread */ k_thread_create(&uthread_thread, uthread_stack, STACKSIZE, @@ -687,6 +687,8 @@ extern k_thread_stack_t ztest_thread_stack[]; void test_main(void) { + struct k_mem_partition *parts[] = {&part0, &part1}; + /* partitions must be initialized first */ FOR_EACH(appmem_init_part, part0, part1, part2); /* @@ -705,12 +707,8 @@ void test_main(void) #else appmem_init_app_memory(); #endif - /* Domain is initialized with partition part0 */ - appmem_init_domain_dom0(part0); - /* Next, the partition must be added to the domain */ - appmem_add_part_dom0(part1); - /* Finally, the current thread is added to domain */ - appmem_add_thread_dom0(k_current_get()); + k_mem_domain_init(&dom0, 2, parts); + k_mem_domain_add_thread(&dom0, k_current_get()); #if defined(CONFIG_ARM) priv_stack_ptr = (int *)_k_priv_stack_find(ztest_thread_stack) -