Skip to content

Commit a4d8e2f

Browse files
Add an EL2 hello, world example for MPS3-AN536
1 parent 2056011 commit a4d8e2f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Hello, this is semihosting! x = 1.000, y = 2.000
2+
Region 0: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
3+
Region 1: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
4+
Region 2: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
5+
Region 3: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
6+
Region 4: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
7+
Region 5: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
8+
Region 6: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
9+
Region 7: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
10+
Region 8: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
11+
Region 9: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
12+
Region 10: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
13+
Region 11: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
14+
Region 12: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
15+
Region 13: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
16+
Region 14: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
17+
Region 15: El2Region { range: 0x0..=0x3f, shareability: NonShareable, access: ReadWriteNoEL10, no_exec: false, mair: 0, enable: false }
18+
PANIC: PanicInfo {
19+
message: I am an example panic,
20+
location: Location {
21+
file: "src/bin/el2_hello.rs",
22+
line: 31,
23+
column: 5,
24+
},
25+
can_unwind: true,
26+
force_no_backtrace: false,
27+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//! Semihosting hello-world for Arm Cortex-R52 running in EL2 (Hyp Mode)
2+
3+
#![no_std]
4+
#![no_main]
5+
6+
// pull in our start-up code
7+
use cortex_r_rt::entry;
8+
9+
// pull in our library
10+
use mps3_an536 as _;
11+
12+
use cortex_ar::register::Hactlr;
13+
use semihosting::println;
14+
15+
/// The entry-point to the Rust application.
16+
///
17+
/// It is called by the start-up code at the bottom of this file.
18+
#[entry]
19+
fn main() -> ! {
20+
let x = 1.0f64;
21+
let y = x * 2.0;
22+
println!("Hello, this is semihosting! x = {:0.3}, y = {:0.3}", x, y);
23+
24+
let mut mpu = unsafe { cortex_ar::pmsav8::El2Mpu::new() };
25+
for idx in 0..mpu.num_regions() {
26+
if let Some(region) = mpu.get_region(idx) {
27+
println!("Region {}: {:?}", idx, region);
28+
}
29+
}
30+
31+
panic!("I am an example panic");
32+
}
33+
34+
// Provide a custom `_start` function that sets us up in EL2 mode, with a
35+
// stack.
36+
//
37+
// Unlike the default routine, it does not initialise any other stacks, or
38+
// switch to EL1 mode.
39+
core::arch::global_asm!(
40+
r#"
41+
// Work around https://github.com/rust-lang/rust/issues/127269
42+
.fpu vfp3-d16
43+
44+
.section .text.start
45+
46+
.global _start
47+
.type _start, %function
48+
_start:
49+
// Set stack pointer
50+
ldr r0, =_stack_top
51+
mov sp, r0
52+
ldr r1, =_hyp_stack_size
53+
sub r0, r0, r1
54+
// Set the HVBAR (for EL2) to _vector_table
55+
ldr r1, =_vector_table
56+
mcr p15, 4, r1, c12, c0, 0
57+
// Configure HACTLR to let us enter EL1
58+
mrc p15, 4, r1, c1, c0, 1
59+
mov r2, {hactlr_bits}
60+
orr r1, r1, r2
61+
mcr p15, 4, r1, c1, c0, 1
62+
// Init .data and .bss
63+
bl _init_segments
64+
// Allow VFP coprocessor access
65+
mrc p15, 0, r0, c1, c0, 2
66+
orr r0, r0, #0xF00000
67+
mcr p15, 0, r0, c1, c0, 2
68+
// Enable VFP
69+
mov r0, #0x40000000
70+
vmsr fpexc, r0
71+
// Zero all registers before calling kmain
72+
mov r0, 0
73+
mov r1, 0
74+
mov r2, 0
75+
mov r3, 0
76+
mov r4, 0
77+
mov r5, 0
78+
mov r6, 0
79+
mov r7, 0
80+
mov r8, 0
81+
mov r9, 0
82+
mov r10, 0
83+
mov r11, 0
84+
mov r12, 0
85+
// Jump to application
86+
bl kmain
87+
// In case the application returns, loop forever
88+
b .
89+
.size _start, . - _start
90+
"#,
91+
hactlr_bits = const {
92+
Hactlr::new_with_raw_value(0)
93+
.with_cpuactlr(true)
94+
.with_cdbgdci(true)
95+
.with_flashifregionr(true)
96+
.with_periphpregionr(true)
97+
.with_qosr(true)
98+
.with_bustimeoutr(true)
99+
.with_intmonr(true)
100+
.with_err(true)
101+
.with_testr1(true)
102+
.raw_value()
103+
},
104+
);

0 commit comments

Comments
 (0)