Skip to content

Commit 6be9e47

Browse files
Patater0xc0170
authored andcommitted
uVisor: Import RTX5-capable uVisor
Imported from 20170407_v7-M tag, commit e33f2739e961 "Make function in transition macro match target".
1 parent b793a3f commit 6be9e47

File tree

23 files changed

+230
-212
lines changed

23 files changed

+230
-212
lines changed

features/FEATURE_UVISOR/AUTHORS.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
592 Milosch Meriac
2-
544 Alessandro Angelino
3-
98 Jaeden Amero
4-
64 Niklas Hauser
5-
4 Irit Arkin
2+
547 Alessandro Angelino
3+
105 Jaeden Amero
4+
65 Niklas Hauser
5+
5 Irit Arkin
66
3 Hugo Vincent
7-
3 JaredCJR
87
3 Jim Huang
8+
3 JaredCJR
9+
2 tonyyanxuan
910
2 Jan Jongboom
1011
2 Nathan Chong
1112
2 Vincenzo Frascino
12-
2 tonyyanxuan
13-
1 Aksel Skauge Mellbye
1413
1 Amanda Butler
14+
1 Aksel Skauge Mellbye
15+
1 AnotherButler
1516
1 ccli8

features/FEATURE_UVISOR/README.md

Lines changed: 77 additions & 114 deletions
Large diffs are not rendered by default.

features/FEATURE_UVISOR/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.27.0
1+
20170407_v7-M

features/FEATURE_UVISOR/includes/uvisor-lib/rtx/rtx_box_index.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#ifndef __RTX_BOX_INDEX_H__
1818
#define __RTX_BOX_INDEX_H__
1919

20-
#include "cmsis_os.h"
20+
#include "cmsis_os2.h"
21+
#include "rtx_os.h"
2122

2223
#ifdef __cplusplus
2324
extern "C" {
@@ -28,12 +29,14 @@ typedef struct
2829
/* The uvisor box index must be placed at the beginning */
2930
UvisorBoxIndex index;
3031

31-
/* Id of the mutex */
32-
osMutexId mutex_id;
33-
/* Pointer to the data of the mutex */
34-
osMutexDef_t mutex;
32+
/* ID of the mutex */
33+
osMutexId_t mutex_id;
34+
35+
/* Attribute of the mutex */
36+
osMutexAttr_t mutex_attr;
37+
3538
/* Internal data of the mutex */
36-
int32_t mutex_data[4];
39+
osRtxMutex_t mutex_data;
3740
} RtxBoxIndex;
3841

3942
#ifdef __cplusplus

features/FEATURE_UVISOR/includes/uvisor/api/inc/box_config.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
UVISOR_EXTERN const uint32_t __uvisor_mode;
2727
UVISOR_EXTERN void const * const public_box_cfg_ptr;
2828

29+
typedef struct {
30+
void (*function)(void *);
31+
size_t priority;
32+
size_t stack_size;
33+
} uvisor_box_main_t;
34+
2935
#define UVISOR_DISABLED 0
3036
#define UVISOR_PERMISSIVE 1
3137
#define UVISOR_ENABLED 2
@@ -145,8 +151,12 @@ UVISOR_EXTERN void const * const public_box_cfg_ptr;
145151
* thread of your box will use for its body. If you don't want a main thread,
146152
* too bad: you have to have one. */
147153
#define UVISOR_BOX_MAIN(function, priority, stack_size) \
148-
static osThreadDef(function, priority, stack_size); \
149-
static const void * const __uvisor_box_lib_config = osThread(function);
154+
static const uvisor_box_main_t __uvisor_box_main = { \
155+
function, \
156+
priority, \
157+
stack_size, \
158+
}; \
159+
static const void * const __uvisor_box_lib_config = &__uvisor_box_main;
150160

151161
#define UVISOR_BOX_HEAPSIZE(heap_size) \
152162
static const uint32_t __uvisor_box_heapsize = heap_size;

features/FEATURE_UVISOR/includes/uvisor/api/inc/lib_hook_exports.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct uvisor_semaphore UvisorSemaphore;
2929
* These functions will be run by unprivileged code only. */
3030
typedef struct {
3131
void (*box_init)(void * lib_config);
32-
int (*semaphore_init)(UvisorSemaphore * semaphore, int32_t count);
32+
int (*semaphore_init)(UvisorSemaphore * semaphore, uint32_t initial_count, uint32_t max_count);
3333
int (*semaphore_pend)(UvisorSemaphore * semaphore, uint32_t timeout_ms);
3434
} UvisorLibHooks;
3535

features/FEATURE_UVISOR/includes/uvisor/api/inc/uvisor_semaphore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
#include "api/inc/uvisor_semaphore_exports.h"
2121

22-
UVISOR_EXTERN int __uvisor_semaphore_init(UvisorSemaphore * semaphore, int32_t count);
22+
/* Initialize a semaphore with the specified initial count. This function is
23+
* not safe to call from interrupt context. */
24+
UVISOR_EXTERN int __uvisor_semaphore_init(UvisorSemaphore * semaphore, uint32_t initial_count, uint32_t max_count);
2325

2426
/* This function is not safe to call from interrupt context, even if the
2527
* timeout is zero. */

features/FEATURE_UVISOR/includes/uvisor/api/inc/uvisor_semaphore_exports.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "api/inc/uvisor_exports.h"
2121

2222
/* This must be big enough for all operating systems uVisor runs on. */
23-
#define UVISOR_SEMAPHORE_INTERNAL_SIZE (16)
23+
#define UVISOR_SEMAPHORE_INTERNAL_SIZE (36)
2424

2525
/* An opaque structure, that one knows the size of so that they can allocate
2626
* memory. */

features/FEATURE_UVISOR/source/rtx/box_init.c

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
#include "api/inc/pool_queue_exports.h"
1919
#include "api/inc/rpc_exports.h"
2020
#include "api/inc/uvisor_semaphore.h"
21+
#include "api/inc/box_config.h"
2122
#include "mbed_interface.h"
22-
#include "cmsis_os.h"
23+
#include "cmsis_os2.h"
2324
#include <stdint.h>
2425
#include <string.h>
2526

2627
/* Register the OS with uVisor */
2728
extern void SVC_Handler(void);
2829
extern void PendSV_Handler(void);
2930
extern void SysTick_Handler(void);
30-
extern uint32_t rt_suspend(void);
31+
extern uint32_t svcRtxKernelLock(void);
3132

32-
UVISOR_SET_PRIV_SYS_HOOKS(SVC_Handler, PendSV_Handler, SysTick_Handler, rt_suspend, __uvisor_semaphore_post);
33+
UVISOR_SET_PRIV_SYS_HOOKS(SVC_Handler, PendSV_Handler, SysTick_Handler, svcRtxKernelLock, __uvisor_semaphore_post);
3334

3435
extern RtxBoxIndex * const __uvisor_ps;
3536

@@ -56,15 +57,7 @@ void __uvisor_initialize_rpc_queues(void)
5657
/* Initialize all the result semaphores. */
5758
for (i = 0; i < UVISOR_RPC_OUTGOING_MESSAGE_SLOTS; i++) {
5859
UvisorSemaphore * semaphore = &rpc_outgoing_msg_queue->messages[i].semaphore;
59-
if (__uvisor_semaphore_init(semaphore, 1)) {
60-
uvisor_error(USER_NOT_ALLOWED);
61-
}
62-
63-
/* Semaphores are created with their value initialized to count. We
64-
* want the semaphore to start at zero. Decrement the semaphore, so it
65-
* starts with a value of zero. This will allow the first pend to
66-
* block. */
67-
if (__uvisor_semaphore_pend(semaphore, 0)) {
60+
if (__uvisor_semaphore_init(semaphore, 1, 0)) {
6861
uvisor_error(USER_NOT_ALLOWED);
6962
}
7063
}
@@ -102,15 +95,7 @@ void __uvisor_initialize_rpc_queues(void)
10295
/* Initialize all the function group semaphores. */
10396
for (i = 0; i < UVISOR_RPC_FN_GROUP_SLOTS; i++) {
10497
UvisorSemaphore * semaphore = &rpc_fn_group_queue->fn_groups[i].semaphore;
105-
if (__uvisor_semaphore_init(semaphore, 1)) {
106-
uvisor_error(USER_NOT_ALLOWED);
107-
}
108-
109-
/* Semaphores are created with their value initialized to count. We
110-
* want the semaphore to start at zero. Decrement the semaphore, so it
111-
* starts with a value of zero. This will allow the first pend to
112-
* block. */
113-
if (__uvisor_semaphore_pend(semaphore, 0)) {
98+
if (__uvisor_semaphore_init(semaphore, 1, 0)) {
11499
uvisor_error(USER_NOT_ALLOWED);
115100
}
116101
}
@@ -120,16 +105,14 @@ void __uvisor_initialize_rpc_queues(void)
120105
* create box main threads for the box. */
121106
void __uvisor_lib_box_init(void * lib_config)
122107
{
123-
osThreadId thread_id;
124-
osThreadDef_t * flash_thread_def = lib_config;
125-
osThreadDef_t thread_def;
108+
osThreadId_t thread_id;
109+
uvisor_box_main_t * box_main = lib_config;
110+
osThreadAttr_t thread_attr = { 0 };
126111

127112
__uvisor_initialize_rpc_queues();
128113

129-
/* Copy thread definition from flash to RAM. The thread definition is most
130-
* likely in flash, so we need to copy it to box-local RAM before we can
131-
* modify it. */
132-
memcpy(&thread_def, flash_thread_def, sizeof(thread_def));
114+
thread_attr.priority = box_main->priority;
115+
thread_attr.stack_size = box_main->stack_size;
133116

134117
/* Note that the box main thread stack is separate from the box stack. This
135118
* is because the thread must be created to use a different stack than the
@@ -138,17 +121,26 @@ void __uvisor_lib_box_init(void * lib_config)
138121
/* Allocate memory for the main thread from the process heap (which is
139122
* private to the process). This memory is never freed, even if the box's
140123
* main thread exits. */
141-
thread_def.stack_pointer = malloc_p(thread_def.stacksize);
124+
thread_attr.stack_mem = malloc_p(thread_attr.stack_size);
125+
if (thread_attr.stack_mem == NULL) {
126+
/* No process heap memory available for thread stack */
127+
uvisor_error(USER_NOT_ALLOWED);
128+
}
142129

143-
if (thread_def.stack_pointer == NULL) {
144-
/* No process heap memory available */
145-
mbed_die();
130+
/* Allocate memory for the main thread control block from the process heap
131+
* (which is private to the process). This memory is never freed, even if
132+
* the box's main thread exits. */
133+
thread_attr.cb_size = sizeof(osRtxThread_t);
134+
thread_attr.cb_mem = malloc_p(thread_attr.cb_size);
135+
if (thread_attr.cb_mem == NULL) {
136+
/* No process heap memory available for thread control block. */
137+
uvisor_error(USER_NOT_ALLOWED);
146138
}
147139

148-
thread_id = osThreadCreate(&thread_def, NULL);
140+
thread_id = osThreadNew(box_main->function, NULL, &thread_attr);
149141

150142
if (thread_id == NULL) {
151143
/* Failed to create thread */
152-
mbed_die();
144+
uvisor_error(USER_NOT_ALLOWED);
153145
}
154146
}

features/FEATURE_UVISOR/source/rtx/rtx_malloc_wrapper.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include "cmsis_os.h"
18+
#include "cmsis_os2.h"
1919
#include "uvisor-lib/uvisor-lib.h"
2020

2121
#include <stdint.h>
@@ -44,7 +44,7 @@ static int is_kernel_initialized()
4444
if (kernel_running) {
4545
return 1;
4646
}
47-
if (osKernelRunning()) {
47+
if (osKernelGetState() == osKernelRunning) {
4848
kernel_running = 1;
4949
return 1;
5050
}
@@ -64,10 +64,13 @@ static int init_allocator()
6464
}
6565

6666
if ((__uvisor_ps->mutex_id == NULL) && is_kernel_initialized()) {
67-
/* Point the mutex pointer to the data. */
68-
__uvisor_ps->mutex.mutex = &(__uvisor_ps->mutex_data);
67+
/* Point the mutex attr to the data. */
68+
__uvisor_ps->mutex_attr.attr_bits = 0; /* Non-recursive */
69+
__uvisor_ps->mutex_attr.cb_mem = &__uvisor_ps->mutex_data;
70+
__uvisor_ps->mutex_attr.cb_size = sizeof(__uvisor_ps->mutex_data);
71+
6972
/* Create mutex if not already done. */
70-
__uvisor_ps->mutex_id = osMutexCreate(&(__uvisor_ps->mutex));
73+
__uvisor_ps->mutex_id = osMutexNew(&__uvisor_ps->mutex_attr);
7174
/* Mutex failed to be created. */
7275
if (__uvisor_ps->mutex_id == NULL) {
7376
return -1;
@@ -80,7 +83,7 @@ static int init_allocator()
8083
/* Lock the mutex during initialization. */
8184
int kernel_initialized = is_kernel_initialized();
8285
if (kernel_initialized) {
83-
osMutexWait(__uvisor_ps->mutex_id, osWaitForever);
86+
osMutexAcquire(__uvisor_ps->mutex_id, osWaitForever);
8487
}
8588
/* Initialize the process heap. */
8689
SecureAllocator allocator = secure_allocator_create_with_pool(
@@ -123,7 +126,7 @@ static void * memory(void * ptr, size_t size, int heap, int operation)
123126
* the `rt_alloc_mem` and `rt_free_mem` functions in `uvisor_allocator.c`.
124127
* However, it is simpler to do it here for now. */
125128
if (mutexed) {
126-
osMutexWait(__uvisor_ps->mutex_id, osWaitForever);
129+
osMutexAcquire(__uvisor_ps->mutex_id, osWaitForever);
127130
}
128131
/* Perform the required operation. */
129132
switch(operation)

0 commit comments

Comments
 (0)