Skip to content

Commit af499f7

Browse files
example: add setvar example
Signed-off-by: Bill Nguyen <bill.nguyen@student.unsw.edu.au>
1 parent 8a80901 commit af499f7

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

example/setvar/Makefile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#
2+
# Copyright 2025, 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+
TOOLCHAIN := aarch64-none-elf
28+
# No specific AArch64 flags
29+
CFLAGS_ARCH :=
30+
else
31+
$(error Unsupported ARCH)
32+
endif
33+
34+
CC := $(TOOLCHAIN)-gcc
35+
LD := $(TOOLCHAIN)-ld
36+
AS := $(TOOLCHAIN)-as
37+
MICROKIT_TOOL ?= $(MICROKIT_SDK)/bin/microkit
38+
39+
SETVAR_OBJS := setvar.o
40+
41+
IMAGES := setvar.elf
42+
CFLAGS := -mstrict-align -nostdlib -ffreestanding -g -O3 -Wall -Wno-unused-function -Werror -I$(BOARD_DIR)/include $(CFLAGS_ARCH)
43+
LDFLAGS := -L$(BOARD_DIR)/lib
44+
LIBS := -lmicrokit -Tmicrokit.ld
45+
46+
IMAGE_FILE = $(BUILD_DIR)/loader.img
47+
REPORT_FILE = $(BUILD_DIR)/report.txt
48+
49+
all: $(IMAGE_FILE)
50+
51+
$(BUILD_DIR)/%.o: %.c Makefile
52+
$(CC) -c $(CFLAGS) $< -o $@
53+
54+
$(BUILD_DIR)/setvar.elf: $(addprefix $(BUILD_DIR)/, $(SETVAR_OBJS))
55+
$(LD) $(LDFLAGS) $^ $(LIBS) -o $@
56+
57+
$(IMAGE_FILE) $(REPORT_FILE): $(addprefix $(BUILD_DIR)/, $(IMAGES)) setvar.system
58+
$(MICROKIT_TOOL) setvar.system --search-path $(BUILD_DIR) --board $(MICROKIT_BOARD) --config $(MICROKIT_CONFIG) -o $(IMAGE_FILE) -r $(REPORT_FILE)

example/setvar/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
Copyright 2025, UNSW
3+
SPDX-License-Identifier: CC-BY-SA-4.0
4+
-->
5+
# Example - setvar
6+
7+
This is a basic example that demonstrate how to use some `setvar` functionalities that the Microkit provides.
8+
9+
Only QEMU virt (AArch64) is supported in this example. Though verything will work the same way on other
10+
platforms. But on x86, `setvar region_paddr` won't be supported.
11+
12+
## Building
13+
14+
```sh
15+
mkdir build
16+
make BUILD_DIR=build MICROKIT_BOARD=qemu_virt_aarch64 MICROKIT_CONFIG=debug MICROKIT_SDK=/path/to/sdk
17+
```
18+
19+
## Running
20+
21+
See instructions for your board in the manual.

example/setvar/setvar.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025, UNSW.
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
#include <stdint.h>
7+
#include <microkit.h>
8+
9+
uint64_t mr_with_paddr_vaddr;
10+
uint64_t mr_with_paddr_paddr;
11+
12+
uint64_t small_mr_no_paddr_paddr;
13+
uint64_t large_mr_no_paddr_paddr;
14+
15+
uint64_t pl011_paddr;
16+
17+
void init(void)
18+
{
19+
microkit_dbg_puts("Virtual address of 'mr_with_paddr': ");
20+
microkit_dbg_put32(mr_with_paddr_vaddr);
21+
microkit_dbg_puts("\n");
22+
microkit_dbg_puts("Physical address of 'mr_with_paddr': ");
23+
microkit_dbg_put32(mr_with_paddr_paddr);
24+
microkit_dbg_puts("\n");
25+
26+
microkit_dbg_puts("Physical address of 'small_mr_no_paddr': ");
27+
microkit_dbg_put32(small_mr_no_paddr_paddr);
28+
microkit_dbg_puts("\n");
29+
microkit_dbg_puts("Physical address of 'large_mr_no_paddr': ");
30+
microkit_dbg_put32(large_mr_no_paddr_paddr);
31+
microkit_dbg_puts("\n");
32+
33+
microkit_dbg_puts("Physical address of 'pl011_paddr': ");
34+
microkit_dbg_put32(pl011_paddr);
35+
microkit_dbg_puts("\n");
36+
}
37+
38+
void notified(microkit_channel ch)
39+
{
40+
}

example/setvar/setvar.system

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2025, UNSW.
4+
5+
SPDX-License-Identifier: BSD-2-Clause
6+
-->
7+
<system>
8+
<!-- 0x900_0000 == 150994944 -->
9+
<memory_region name="pl011" size="0x1000" phys_addr="0x900_0000" />
10+
11+
<!-- 0x8000_0000 == 2147483648 -->
12+
<memory_region name="mr_with_paddr" size="0x1000" phys_addr="0x8000_0000" />
13+
14+
<memory_region name="small_mr_no_paddr" size="0x1000" />
15+
<memory_region name="large_mr_no_paddr" size="0x200000" page_size="0x200000" />
16+
17+
<protection_domain name="setvar" priority="1">
18+
<program_image path="setvar.elf" />
19+
20+
<!-- 0x2000_0000 == 536870912 -->
21+
<map mr="mr_with_paddr" vaddr="0x2000_0000" setvar_vaddr="mr_with_paddr_vaddr" />
22+
<setvar region_paddr="mr_with_paddr" symbol="mr_with_paddr_paddr" />
23+
<setvar region_paddr="small_mr_no_paddr" symbol="small_mr_no_paddr_paddr" />
24+
<setvar region_paddr="large_mr_no_paddr" symbol="large_mr_no_paddr_paddr" />
25+
<setvar region_paddr="pl011" symbol="pl011_paddr" />
26+
</protection_domain>
27+
</system>

0 commit comments

Comments
 (0)