-
Notifications
You must be signed in to change notification settings - Fork 32
/
rust-smem.c
80 lines (60 loc) · 2.23 KB
/
rust-smem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <version.h>
#if KERNEL_VERSION_MAJOR < 3
#include <zephyr.h>
#include <init.h>
#include <app_memory/app_memdomain.h>
#else
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/app_memory/app_memdomain.h>
#endif
#ifdef CONFIG_USERSPACE
struct k_mem_domain rust_std_domain;
K_APPMEM_PARTITION_DEFINE(rust_std_partition);
#define RUST_STD_SECTION K_APP_DMEM_SECTION(rust_std_partition)
#else
#define RUST_STD_SECTION .data
#endif
#if defined(CONFIG_RUST_ALLOC_POOL)
#define RUST_STD_MEM_POOL_SIZE (WB_UP(CONFIG_RUST_HEAP_MEM_POOL_MAX_SIZE) * \
CONFIG_RUST_HEAP_MEM_POOL_NMAX)
char __aligned(sizeof(void *)) Z_GENERIC_SECTION(RUST_STD_SECTION)
kheap_rust_std_mem_pool[RUST_STD_MEM_POOL_SIZE];
struct k_heap Z_GENERIC_SECTION(RUST_STD_SECTION) rust_std_mem_pool;
#elif CONFIG_HEAP_MEM_POOL_SIZE == 0
#error CONFIG_HEAP_MEM_POOL_SIZE (k_malloc) \
must be non-zero if not using a Rust sys mem pool.
#endif /* defined(CONFIG_RUST_ALLOC_POOL) */
#if defined(CONFIG_USERSPACE) || defined(CONFIG_RUST_ALLOC_POOL)
/* Harmless API difference that generates a warning */
#if ZEPHYR_VERSION_CODE >= ZEPHYR_VERSION(3, 4, 0)
static int rust_std_init(void)
{
#elif ZEPHYR_VERSION_CODE >= ZEPHYR_VERSION(2, 4, 0)
static int rust_std_init(const struct device *arg)
{
ARG_UNUSED(arg);
#else
static int rust_std_init(struct device *arg)
{
ARG_UNUSED(arg);
#endif /* ZEPHYR_VERSION_CODE >= ZEPHYR_VERSION(2, 4, 0) */
#ifdef CONFIG_USERSPACE
struct k_mem_partition *rust_std_parts[] = { &rust_std_partition };
k_mem_domain_init(&rust_std_domain,
ARRAY_SIZE(rust_std_parts), rust_std_parts);
#endif /* CONFIG_USERSPACE */
#ifdef CONFIG_RUST_ALLOC_POOL
k_heap_init(&rust_std_mem_pool, kheap_rust_std_mem_pool,
RUST_STD_MEM_POOL_SIZE);
#endif /* CONFIG_RUST_ALLOC_POOL */
#if defined(CONFIG_USERSPACE) && defined(CONFIG_RUST_MUTEX_POOL)
extern struct k_mutex rust_mutex_pool[CONFIG_RUST_MUTEX_POOL_SIZE];
for (size_t i = 0; i < ARRAY_SIZE(rust_mutex_pool); i++) {
k_object_access_all_grant(&rust_mutex_pool[i]);
}
#endif /* defined(CONFIG_USERSPACE) && defined(CONFIG_RUST_MUTEX_POOL) */
return 0;
}
SYS_INIT(rust_std_init, PRE_KERNEL_2, CONFIG_APPLICATION_INIT_PRIORITY);
#endif /* defined(CONFIG_USERSPACE) || defined(CONFIG_RUST_ALLOC_POOL) */