Skip to content

Commit c004b01

Browse files
authored
Introduce support for NuttX RTOS (bytecodealliance#377)
Signed-off-by: Huang Qi <[email protected]> Co-authored-by: Huang Qi <[email protected]>
1 parent 547298d commit c004b01

File tree

8 files changed

+680
-1
lines changed

8 files changed

+680
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The iwasm supports the following architectures:
5252
Following platforms are supported. Refer to [WAMR porting guide](./doc/port_wamr.md) for how to port WAMR to a new platform.
5353

5454
- [Linux](./doc/build_wamr.md#linux), [Linux SGX (Intel Software Guard Extension)](./doc/linux_sgx.md), [MacOS](./doc/build_wamr.md#macos), [Android](./doc/build_wamr.md#android)
55-
- [Zephyr](./doc/build_wamr.md#zephyr), [AliOS-Things](./doc/build_wamr.md#alios-things), [VxWorks](./doc/build_wamr.md#vxworks)
55+
- [Zephyr](./doc/build_wamr.md#zephyr), [AliOS-Things](./doc/build_wamr.md#alios-things), [VxWorks](./doc/build_wamr.md#vxworks), [NuttX](./doc/build_wamr.md#nuttx)
5656

5757
### Build iwasm VM core (mini product)
5858

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2020 XiaoMi Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "platform_api_extension.h"
7+
#include "platform_api_vmcore.h"
8+
9+
int
10+
bh_platform_init()
11+
{
12+
return 0;
13+
}
14+
15+
void
16+
bh_platform_destroy()
17+
{}
18+
19+
void *
20+
os_malloc(unsigned size)
21+
{
22+
return malloc(size);
23+
}
24+
25+
void *
26+
os_realloc(void *ptr, unsigned size)
27+
{
28+
return realloc(ptr, size);
29+
}
30+
31+
void
32+
os_free(void *ptr)
33+
{
34+
free(ptr);
35+
}
36+
37+
void *
38+
os_mmap(void *hint, size_t size, int prot, int flags)
39+
{
40+
if ((uint64)size >= UINT32_MAX)
41+
return NULL;
42+
return malloc((uint32)size);
43+
}
44+
45+
void
46+
os_munmap(void *addr, size_t size)
47+
{
48+
return free(addr);
49+
}
50+
51+
int
52+
os_mprotect(void *addr, size_t size, int prot)
53+
{
54+
return 0;
55+
}
56+
57+
void
58+
os_dcache_flush()
59+
{}
60+
61+
uint64
62+
os_time_get_boot_microsecond()
63+
{
64+
struct timespec ts;
65+
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
66+
return 0;
67+
}
68+
69+
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
70+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2020 XiaoMi Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "platform_api_extension.h"
7+
#include "platform_api_vmcore.h"
8+
9+
korp_tid
10+
os_self_thread()
11+
{
12+
return (korp_tid)pthread_self();
13+
}
14+
15+
int
16+
os_mutex_init(korp_mutex *mutex)
17+
{
18+
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
19+
}
20+
21+
int
22+
os_mutex_destroy(korp_mutex *mutex)
23+
{
24+
int ret;
25+
26+
assert(mutex);
27+
ret = pthread_mutex_destroy(mutex);
28+
29+
return ret == 0 ? BHT_OK : BHT_ERROR;
30+
}
31+
32+
int
33+
os_mutex_lock(korp_mutex *mutex)
34+
{
35+
int ret;
36+
37+
assert(mutex);
38+
ret = pthread_mutex_lock(mutex);
39+
if (0 != ret) {
40+
os_printf("vm mutex lock failed (ret=%d)!\n", ret);
41+
exit(-1);
42+
}
43+
return ret;
44+
}
45+
46+
int
47+
os_mutex_unlock(korp_mutex *mutex)
48+
{
49+
int ret;
50+
51+
assert(mutex);
52+
ret = pthread_mutex_unlock(mutex);
53+
if (0 != ret) {
54+
os_printf("vm mutex unlock failed (ret=%d)!\n", ret);
55+
exit(-1);
56+
}
57+
return ret;
58+
}
59+
60+
uint8 *
61+
os_thread_get_stack_boundary()
62+
{
63+
return NULL;
64+
}
65+
66+
int
67+
os_cond_init(korp_cond *cond)
68+
{
69+
assert(cond);
70+
71+
if (pthread_cond_init(cond, NULL) != BHT_OK)
72+
return BHT_ERROR;
73+
74+
return BHT_OK;
75+
}
76+
77+
int
78+
os_cond_destroy(korp_cond *cond)
79+
{
80+
assert(cond);
81+
82+
if (pthread_cond_destroy(cond) != BHT_OK)
83+
return BHT_ERROR;
84+
85+
return BHT_OK;
86+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2020 XiaoMi Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#ifndef _PLATFORM_INTERNAL_H
7+
#define _PLATFORM_INTERNAL_H
8+
9+
#include <ctype.h>
10+
#include <errno.h>
11+
#include <inttypes.h>
12+
#include <limits.h>
13+
#include <stdarg.h>
14+
#include <stdbool.h>
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
#include <math.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
#ifndef BH_PLATFORM_NUTTX
25+
#define BH_PLATFORM_NUTTX
26+
#endif
27+
28+
typedef pthread_t korp_tid;
29+
typedef pthread_mutex_t korp_mutex;
30+
typedef pthread_cond_t korp_cond;
31+
typedef pthread_t korp_thread;
32+
33+
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
34+
35+
/* Default thread priority */
36+
#define BH_THREAD_DEFAULT_PRIORITY 100
37+
38+
#define os_printf printf
39+
#define os_vprintf vprintf
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
45+
#endif /* end of _BH_PLATFORM_H */
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (C) 2020 XiaoMi Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
5+
6+
add_definitions(-DBH_PLATFORM_NUTTX)
7+
8+
include_directories(${PLATFORM_SHARED_DIR})
9+
include_directories(${PLATFORM_SHARED_DIR}/../include)
10+
11+
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
12+
13+
set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_MATH_SOURCE})
14+

doc/build_wamr.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ $ # include/ includes all necesary head files
301301
$ # lib includes libiwasm.so
302302
```
303303

304+
NuttX
305+
-------------------------
306+
WAMR is intergrated with NuttX, just enable the WAMR in Kconfig option (Application Configuration/Interpreters).
304307

305308
Docker
306309
-------------------------

0 commit comments

Comments
 (0)