-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04bb073
commit e1e81f1
Showing
8 changed files
with
512 additions
and
20 deletions.
There are no files selected for viewing
Submodule rtos-source
updated
from c633c6 to 1e6323
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Microsoft and CHERIoT | ||
// Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <atomic> | ||
#include <stdio.h> | ||
#include <thread.h> | ||
|
||
// entry#begin | ||
/// Thread entry point. | ||
__cheri_compartment( | ||
"barrier") void entry() | ||
{ | ||
static std::atomic<uint32_t> | ||
barrier = 2; | ||
printf( | ||
"Thread: %d arrived at barrier\n", | ||
thread_id_get()); | ||
uint32_t value = --barrier; | ||
if (value == 0) | ||
{ | ||
barrier.notify_all(); | ||
} | ||
else | ||
{ | ||
while (value != 0) | ||
{ | ||
barrier.wait(value); | ||
value = barrier; | ||
} | ||
} | ||
printf( | ||
"Thread: %d passed barrier\n", | ||
thread_id_get()); | ||
} | ||
// entry#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- Copyright Microsoft and CHERIoT Contributors. | ||
-- SPDX-License-Identifier: MIT | ||
|
||
set_project("CHERIoT example") | ||
|
||
sdkdir = os.getenv("CHERIOT_SDK") or | ||
"../../../rtos-source/sdk/" | ||
includes(sdkdir) | ||
|
||
set_toolchains("cheriot-clang") | ||
|
||
option("board") | ||
set_default("sail") | ||
|
||
-- An single compartment for this example. | ||
compartment("barrier") | ||
add_files("barrier.cc") | ||
|
||
|
||
-- firmware#begin | ||
-- Firmware image for the example. | ||
firmware("barrier_thread") | ||
-- RTOS-provided libraries | ||
add_deps("freestanding", "stdio") | ||
-- Our compartments | ||
add_deps("barrier") | ||
on_load(function(target) | ||
-- The board to target | ||
target:values_set("board", "$(board)") | ||
-- threads#begin | ||
-- Threads to select | ||
target:values_set("threads", { | ||
{ | ||
compartment = "barrier", | ||
priority = 1, | ||
entry_point = "entry", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
}, | ||
{ | ||
compartment = "barrier", | ||
priority = 2, | ||
entry_point = "entry", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
} | ||
}, {expand = false}) | ||
-- threads#end | ||
end) | ||
-- firmware#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright Microsoft and CHERIoT | ||
// Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <locks.hh> | ||
#include <stdio.h> | ||
#include <thread.h> | ||
|
||
// declare#begin | ||
// Comment out this line and uncomment | ||
// the next one to see how ticket locks | ||
// behave. | ||
FlagLock lock; | ||
// TicketLock lock; | ||
// declare#end | ||
|
||
/** | ||
* Function that serves as a placeholder | ||
* for something that does work with the | ||
* lock held. | ||
*/ | ||
void do_useful_work() | ||
{ | ||
Timeout t{MS_TO_TICKS(1000)}; | ||
thread_sleep( | ||
&t, ThreadSleepNoEarlyWake); | ||
} | ||
|
||
// low#begin | ||
__cheri_compartment( | ||
"locking") void low() | ||
{ | ||
while (true) | ||
{ | ||
lock.lock(); | ||
printf("Low priority thread " | ||
"acquired lock\n"); | ||
do_useful_work(); | ||
lock.unlock(); | ||
} | ||
} | ||
// low#end | ||
|
||
// medium#begin | ||
__cheri_compartment( | ||
"locking") void medium() | ||
{ | ||
while (true) | ||
{ | ||
lock.lock(); | ||
printf("Medium priority thread " | ||
"acquired lock\n"); | ||
do_useful_work(); | ||
lock.unlock(); | ||
} | ||
} | ||
// medium#end | ||
|
||
// high#begin | ||
__cheri_compartment( | ||
"locking") void high() | ||
{ | ||
while (true) | ||
{ | ||
lock.lock(); | ||
printf("High priority thread " | ||
"acquired lock\n"); | ||
do_useful_work(); | ||
lock.unlock(); | ||
} | ||
} | ||
// high#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
-- Copyright Microsoft and CHERIoT Contributors. | ||
-- SPDX-License-Identifier: MIT | ||
|
||
set_project("CHERIoT example") | ||
|
||
sdkdir = os.getenv("CHERIOT_SDK") or | ||
"../../../rtos-source/sdk/" | ||
includes(sdkdir) | ||
|
||
set_toolchains("cheriot-clang") | ||
|
||
option("board") | ||
set_default("sail") | ||
|
||
-- An single compartment for this example. | ||
compartment("locking") | ||
add_files("locking.cc") | ||
|
||
|
||
-- firmware#begin | ||
-- Firmware image for the example. | ||
firmware("locking_thread") | ||
-- RTOS-provided libraries | ||
add_deps("freestanding", "stdio") | ||
-- Our compartments | ||
add_deps("locking") | ||
on_load(function(target) | ||
-- The board to target | ||
target:values_set("board", "$(board)") | ||
-- threads#begin | ||
-- Threads to select | ||
target:values_set("threads", { | ||
{ | ||
compartment = "locking", | ||
priority = 1, | ||
entry_point = "low", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
}, | ||
{ | ||
compartment = "locking", | ||
priority = 2, | ||
entry_point = "medium", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
}, | ||
{ | ||
compartment = "locking", | ||
priority = 3, | ||
entry_point = "high", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
} | ||
}, {expand = false}) | ||
-- threads#end | ||
end) | ||
-- firmware#end |
83 changes: 83 additions & 0 deletions
83
text/examples/priority_inheritance/priority_inheritance.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright Microsoft and CHERIoT | ||
// Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <locks.hh> | ||
#include <stdio.h> | ||
#include <thread.h> | ||
|
||
// Comment this line and uncomment the | ||
// line below to fix the priority | ||
// inversion in this example. | ||
FlagLock lock; | ||
//FlagLockPriorityInherited lock; | ||
|
||
// high#begin | ||
__cheri_compartment( | ||
"priority_" | ||
"inheritance") void high() | ||
{ | ||
// Let the low and | ||
// medium-priority threads start | ||
Timeout t(MS_TO_TICKS(1000)); | ||
thread_sleep(&t); | ||
while (true) | ||
{ | ||
t = Timeout(MS_TO_TICKS(1000)); | ||
if (LockGuard g{lock, &t}) | ||
{ | ||
printf( | ||
"High-priority thread " | ||
"acquired the lock!\n"); | ||
} | ||
else | ||
{ | ||
printf( | ||
"High-priority thread " | ||
"failed to acquire the " | ||
"lock!\n"); | ||
} | ||
} | ||
} | ||
// high#end | ||
|
||
std::atomic<int> x; | ||
|
||
// medium#begin | ||
__cheri_compartment( | ||
"priority_" | ||
"inheritance") void medium() | ||
{ | ||
// Let the low-priority thread run | ||
// until it yields | ||
Timeout t(MS_TO_TICKS(1000)); | ||
thread_sleep(&t); | ||
printf("Medium priority thread " | ||
"entering infinite loop and " | ||
"not yielding\n"); | ||
while (true) | ||
{ | ||
x++; | ||
} | ||
} | ||
// medium#end | ||
|
||
// low#begin | ||
__cheri_compartment( | ||
"priority_" | ||
"inheritance") void low() | ||
{ | ||
while (true) | ||
{ | ||
lock.lock(); | ||
printf("Low-priority thread " | ||
"acquired the lock\n"); | ||
Timeout t(MS_TO_TICKS(500)); | ||
thread_sleep( | ||
&t, ThreadSleepNoEarlyWake); | ||
printf("Low-priority thread " | ||
"releasing the lock\n"); | ||
lock.unlock(); | ||
} | ||
} | ||
// low#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
-- Copyright Microsoft and CHERIoT Contributors. | ||
-- SPDX-License-Identifier: MIT | ||
|
||
set_project("CHERIoT example") | ||
|
||
sdkdir = os.getenv("CHERIOT_SDK") or | ||
"../../../rtos-source/sdk/" | ||
includes(sdkdir) | ||
|
||
set_toolchains("cheriot-clang") | ||
|
||
option("board") | ||
set_default("sail") | ||
|
||
-- An single compartment for this example. | ||
compartment("priority_inheritance") | ||
add_files("priority_inheritance.cc") | ||
|
||
|
||
-- firmware#begin | ||
-- Firmware image for the example. | ||
firmware("priority_inheritance_thread") | ||
-- RTOS-provided libraries | ||
add_deps("freestanding", "stdio") | ||
-- Our compartments | ||
add_deps("priority_inheritance") | ||
on_load(function(target) | ||
-- The board to target | ||
target:values_set("board", "$(board)") | ||
-- threads#begin | ||
-- Threads to select | ||
target:values_set("threads", { | ||
{ | ||
compartment = "priority_inheritance", | ||
priority = 1, | ||
entry_point = "low", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
}, | ||
{ | ||
compartment = "priority_inheritance", | ||
priority = 2, | ||
entry_point = "medium", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
}, | ||
{ | ||
compartment = "priority_inheritance", | ||
priority = 3, | ||
entry_point = "high", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
} | ||
}, {expand = false}) | ||
-- threads#end | ||
end) | ||
-- firmware#end |
Oops, something went wrong.