Skip to content

Commit d29824c

Browse files
examples: add mr_prefill
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
1 parent e134583 commit d29824c

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

.reuse/dep5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Files:
1414
VERSION
1515
docs/assets/microkit_flow.svg
1616
docs/assets/microkit_flow.pdf
17+
example/mr_prefill/fill.bin
1718
Copyright: 2024, UNSW
1819
License: BSD-2-Clause
1920

example/mr_prefill/Makefile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# Copyright 2026, UNSW
3+
#
4+
# SPDX-License-Identifier: BSD-2-Clause
5+
#
6+
ifeq ($(strip $(BUILD_DIR)),)
7+
$(error BUILD_DIR must be specified)
8+
endif
9+
10+
ifeq ($(strip $(MICROKIT_SDK)),)
11+
$(error MICROKIT_SDK must be specified)
12+
endif
13+
14+
ifeq ($(strip $(MICROKIT_BOARD)),)
15+
$(error MICROKIT_BOARD must be specified)
16+
endif
17+
18+
ifeq ($(strip $(MICROKIT_CONFIG)),)
19+
$(error MICROKIT_CONFIG must be specified)
20+
endif
21+
22+
BOARD_DIR := $(MICROKIT_SDK)/board/$(MICROKIT_BOARD)/$(MICROKIT_CONFIG)
23+
24+
ARCH := ${shell grep 'CONFIG_SEL4_ARCH ' $(BOARD_DIR)/include/kernel/gen_config.h | cut -d' ' -f4}
25+
26+
ifeq ($(ARCH),aarch64)
27+
TARGET_TRIPLE := aarch64-none-elf
28+
CFLAGS_ARCH := -mstrict-align
29+
else ifeq ($(ARCH),riscv64)
30+
TARGET_TRIPLE := riscv64-unknown-elf
31+
CFLAGS_ARCH := -march=rv64imafdc_zicsr_zifencei -mabi=lp64d
32+
else ifeq ($(ARCH),x86_64)
33+
TARGET_TRIPLE := x86_64-linux-gnu
34+
CFLAGS_ARCH := -march=x86-64 -mtune=generic
35+
else
36+
$(error Unsupported ARCH)
37+
endif
38+
39+
ifeq ($(strip $(LLVM)),True)
40+
CC := clang -target $(TARGET_TRIPLE)
41+
AS := clang -target $(TARGET_TRIPLE)
42+
LD := ld.lld
43+
else
44+
CC := $(TARGET_TRIPLE)-gcc
45+
LD := $(TARGET_TRIPLE)-ld
46+
AS := $(TARGET_TRIPLE)-as
47+
endif
48+
49+
MICROKIT_TOOL ?= $(MICROKIT_SDK)/bin/microkit
50+
51+
IMAGES := mr_prefill.elf
52+
CFLAGS := -nostdlib -ffreestanding -g3 -O3 -I$(BOARD_DIR)/include $(CFLAGS_ARCH)
53+
LDFLAGS := -L$(BOARD_DIR)/lib -z noexecstack
54+
LIBS := -lmicrokit -Tmicrokit.ld
55+
56+
IMAGE_FILE = $(BUILD_DIR)/loader.img
57+
REPORT = $(BUILD_DIR)/report.txt
58+
59+
all: $(IMAGE_FILE)
60+
61+
$(BUILD_DIR)/mr_prefill.o: mr_prefill.c Makefile
62+
$(CC) -c $(CFLAGS) -Wall -Wno-unused-function -Werror $< -o $@
63+
64+
$(BUILD_DIR)/mr_prefill.elf: $(BUILD_DIR)/mr_prefill.o
65+
$(LD) $(LDFLAGS) $^ $(LIBS) -o $@
66+
67+
$(IMAGE_FILE): $(addprefix $(BUILD_DIR)/, $(IMAGES)) mr_prefill.system
68+
$(MICROKIT_TOOL) mr_prefill.system --search-path $(BUILD_DIR) --board $(MICROKIT_BOARD) --config $(MICROKIT_CONFIG) -o $(IMAGE_FILE) -r $(REPORT)

example/mr_prefill/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--
2+
Copyright 2026, UNSW
3+
SPDX-License-Identifier: CC-BY-SA-4.0
4+
-->
5+
# Example - Memory Region Prefill
6+
7+
This is an example demostrating prefilling a memory region from a data file at compile time.
8+
9+
All Microkit platforms are supported.
10+
11+
## Building
12+
13+
```sh
14+
mkdir build
15+
make BUILD_DIR=build MICROKIT_BOARD=<board> MICROKIT_CONFIG=<debug/release/benchmark> MICROKIT_SDK=/path/to/sdk
16+
```
17+
18+
## Running
19+
20+
See instructions for your board in the manual.

example/mr_prefill/fill.bin

4.02 KB
Binary file not shown.

example/mr_prefill/mr_prefill.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2026, UNSW
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
#include <stdint.h>
7+
#include <microkit.h>
8+
9+
#define BEGIN_MAGIC 0x53145314
10+
#define VALUE_A 0x01234567
11+
#define VALUE_B 0x89ABCDEF
12+
#define END_MAGIC 0x75757575
13+
14+
typedef struct __attribute__((packed)) fill_data {
15+
uint32_t begin_magic;
16+
uint32_t value_a;
17+
uint8_t _padding1[0x400];
18+
uint32_t value_b;
19+
uint8_t _padding2[0xc00];
20+
uint32_t end_magic;
21+
} fill_data_t;
22+
23+
fill_data_t *filled_mr;
24+
uint64_t filled_mr_data_size;
25+
26+
void init(void)
27+
{
28+
microkit_dbg_puts("hello, world. my name is ");
29+
microkit_dbg_puts(microkit_name);
30+
microkit_dbg_puts("\n");
31+
32+
microkit_dbg_puts("checking prefilled memory region data length\n");
33+
if (filled_mr_data_size != sizeof(fill_data_t)) {
34+
microkit_dbg_puts("oh no prefilled data length doesn't match: ");
35+
microkit_dbg_put32(filled_mr_data_size);
36+
microkit_dbg_puts(" != ");
37+
microkit_dbg_put32(sizeof(fill_data_t));
38+
microkit_dbg_puts("\n");
39+
return;
40+
}
41+
42+
microkit_dbg_puts("checking prefilled memory region data\n");
43+
44+
if (filled_mr->begin_magic != BEGIN_MAGIC) {
45+
microkit_dbg_puts("oh no begin magic doesn't match: ");
46+
microkit_dbg_put32(filled_mr->begin_magic);
47+
microkit_dbg_puts(" != ");
48+
microkit_dbg_put32(BEGIN_MAGIC);
49+
microkit_dbg_puts("\n");
50+
return;
51+
}
52+
53+
if (filled_mr->value_a != VALUE_A) {
54+
microkit_dbg_puts("oh no value A doesn't match: ");
55+
microkit_dbg_put32(filled_mr->value_a);
56+
microkit_dbg_puts(" != ");
57+
microkit_dbg_put32(VALUE_A);
58+
microkit_dbg_puts("\n");
59+
return;
60+
}
61+
62+
if (filled_mr->value_b != VALUE_B) {
63+
microkit_dbg_puts("oh no value B doesn't match: ");
64+
microkit_dbg_put32(filled_mr->value_b);
65+
microkit_dbg_puts(" != ");
66+
microkit_dbg_put32(VALUE_B);
67+
microkit_dbg_puts("\n");
68+
return;
69+
}
70+
71+
if (filled_mr->end_magic != END_MAGIC) {
72+
microkit_dbg_puts("oh no end magic doesn't match: ");
73+
microkit_dbg_put32(filled_mr->end_magic);
74+
microkit_dbg_puts(" != ");
75+
microkit_dbg_put32(END_MAGIC);
76+
microkit_dbg_puts("\n");
77+
return;
78+
}
79+
80+
microkit_dbg_puts("prefilled data OK!\n");
81+
}
82+
83+
void notified(microkit_channel ch)
84+
{
85+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2026, UNSW
4+
5+
SPDX-License-Identifier: BSD-2-Clause
6+
-->
7+
<system>
8+
<memory_region name="prefilled" prefill_path="fill.bin" />
9+
10+
<protection_domain name="mr_prefill" priority="100" >
11+
<program_image path="mr_prefill.elf" />
12+
13+
<map mr="prefilled" vaddr="0x20000000" setvar_vaddr="filled_mr" setvar_prefill_size="filled_mr_data_size"/>
14+
</protection_domain>
15+
</system>

0 commit comments

Comments
 (0)