Skip to content

Commit 4cdc4a7

Browse files
Patatermeriac
authored andcommitted
Add OsEventObserver
Add the OsEventObserver mechanism. A client interested in receiving notifications on certain OS events can register to receive notifications with osRegisterForOsEvents. This is useful for clients like the secure memory allocator, which observes thread switching events in order to swap in and out different memory allocator objects.
1 parent 55f464d commit 4cdc4a7

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "rt_MemBox.h"
6666
#include "rt_Memory.h"
6767
#include "rt_HAL_CM.h"
68+
#include "rt_OsEventObserver.h"
6869

6970
#include "cmsis_os.h"
7071

@@ -504,6 +505,10 @@ osStatus svcKernelStart (void) {
504505

505506
if (os_running) { return osOK; }
506507

508+
if (osEventObs && osEventObs->pre_start) {
509+
osEventObs->pre_start();
510+
}
511+
507512
rt_tsk_prio(0U, os_tsk.run->prio_base); // Restore priority
508513
if (os_tsk.run->task_id == 0xFFU) { // Idle Thread
509514
__set_PSP(os_tsk.run->tsk_stack + (8U*4U)); // Setup PSP
@@ -683,6 +688,12 @@ osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument) {
683688

684689
*((uint32_t *)ptcb->tsk_stack + 13) = (uint32_t)osThreadExit;
685690

691+
if (osEventObs && osEventObs->thread_create) {
692+
ptcb->context = osEventObs->thread_create(ptcb->task_id, context);
693+
} else {
694+
ptcb->context = context;
695+
}
696+
686697
return ptcb;
687698
}
688699

@@ -712,6 +723,10 @@ osStatus svcThreadTerminate (osThreadId thread_id) {
712723
stk = ptcb->priv_stack ? ptcb->stack : NULL; // Private stack
713724
#endif
714725

726+
if (osEventObs && osEventObs->thread_destroy) {
727+
osEventObs->thread_destroy(ptcb->context);
728+
}
729+
715730
res = rt_tsk_delete(ptcb->task_id); // Delete task
716731

717732
if (res == OS_R_NOK) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*----------------------------------------------------------------------------
2+
* CMSIS-RTOS - RTX
3+
*----------------------------------------------------------------------------
4+
* Name: rt_OsEventObserver.c
5+
* Purpose: OS Event Callbacks for CMSIS RTOS
6+
* Rev.: VX.XX
7+
*----------------------------------------------------------------------------
8+
*
9+
* Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
10+
* All rights reserved.
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions are met:
13+
* - Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* - Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* - Neither the name of ARM nor the names of its contributors may be used
19+
* to endorse or promote products derived from this software without
20+
* specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*---------------------------------------------------------------------------*/
34+
35+
#include "rt_OsEventObserver.h"
36+
37+
/*
38+
* _____ _____ ____ __ _____
39+
* | ___|_ _\ \/ / \/ | ____|
40+
* | |_ | | \ /| |\/| | _|
41+
* | _| | | / \| | | | |___
42+
* |_| |___/_/\_\_| |_|_____|
43+
*
44+
* FIXME:
45+
* The osEventObs variable must be in protected memory. If not every box
46+
* and box 0 can modify osEventObs to point to any handler to run code
47+
* privileged. This issue is tracked at
48+
* <https://github.com/ARMmbed/uvisor/issues/235>.
49+
*/
50+
const OsEventObserver *osEventObs;
51+
52+
void osRegisterForOsEvents(const OsEventObserver *observer)
53+
{
54+
osEventObs = observer;
55+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*----------------------------------------------------------------------------
2+
* CMSIS-RTOS - RTX
3+
*----------------------------------------------------------------------------
4+
* Name: os_events.h
5+
* Purpose: OS Event Callbacks for CMSIS RTOS
6+
* Rev.: VX.XX
7+
*----------------------------------------------------------------------------
8+
*
9+
* Copyright (c) 1999-2009 KEIL, 2009-2016 ARM Germany GmbH
10+
* All rights reserved.
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions are met:
13+
* - Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* - Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* - Neither the name of ARM nor the names of its contributors may be used
19+
* to endorse or promote products derived from this software without
20+
* specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*---------------------------------------------------------------------------*/
34+
#ifndef _RT_OS_EVENT_OBSERVER_H
35+
#define _RT_OS_EVENT_OBSERVER_H
36+
37+
#include <stdint.h>
38+
39+
#ifdef __cplusplus
40+
extern "C" {
41+
#endif
42+
43+
typedef struct {
44+
uint32_t version;
45+
void (*pre_start)(void);
46+
void *(*thread_create)(int thread_id, void *context);
47+
void (*thread_destroy)(void *context);
48+
void (*thread_switch)(void *context);
49+
} OsEventObserver;
50+
extern const OsEventObserver *osEventObs;
51+
52+
void osRegisterForOsEvents(const OsEventObserver *observer);
53+
54+
#ifdef __cplusplus
55+
};
56+
#endif
57+
58+
#endif

rtos/rtx/TARGET_CORTEX_M/rt_Task.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "rt_MemBox.h"
4141
#include "rt_Robin.h"
4242
#include "rt_HAL_CM.h"
43+
#include "rt_OsEventObserver.h"
4344

4445
/*----------------------------------------------------------------------------
4546
* Global Variables
@@ -101,6 +102,9 @@ void rt_switch_req (P_TCB p_new) {
101102
/* Switch to next task (identified by "p_new"). */
102103
os_tsk.new_tsk = p_new;
103104
p_new->state = RUNNING;
105+
if (osEventObs && osEventObs->thread_switch) {
106+
osEventObs->thread_switch(p_new->context);
107+
}
104108
DBG_TASK_SWITCH(p_new->task_id);
105109
}
106110

0 commit comments

Comments
 (0)