Skip to main content

sel4_runtime_common/
start.rs

1//
2// Copyright 2025, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use core::arch::global_asm;
8
9#[macro_export]
10macro_rules! declare_entrypoint {
11    () => {
12        $crate::_private::global_asm! {
13            r#"
14                .extern __sel4_runtime_common__call_rust_entrypoint
15
16                .section .text
17
18                .global _start
19                _start:
20            "#,
21            #[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
22            r#"
23                    b __sel4_runtime_common__call_rust_entrypoint
24            "#,
25            #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
26            r#"
27                    j __sel4_runtime_common__call_rust_entrypoint
28            "#,
29            #[cfg(target_arch = "x86_64")]
30            r#"
31                    jmp __sel4_runtime_common__call_rust_entrypoint
32            "#,
33        }
34    };
35}
36
37#[macro_export]
38macro_rules! declare_entrypoint_with_stack_init {
39    () => {
40        $crate::_private::global_asm! {
41            r#"
42                .extern __sel4_runtime_common__stack_init
43
44                .section .text
45
46                .global _start
47                _start:
48            "#,
49            #[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
50            r#"
51                    b __sel4_runtime_common__stack_init
52            "#,
53            #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
54            r#"
55                    j __sel4_runtime_common__stack_init
56            "#,
57            #[cfg(target_arch = "x86_64")]
58            r#"
59                    jmp __sel4_runtime_common__stack_init
60            "#,
61        }
62    };
63}
64
65#[macro_export]
66macro_rules! declare_stack {
67    ($size:expr) => {
68        const _: () = {
69            #[unsafe(no_mangle)]
70            static __sel4_runtime_common__stack_bottom: $crate::_private::StackBottom = {
71                static STACK: $crate::_private::Stack<{ $size }> = $crate::_private::Stack::new();
72                STACK.bottom()
73            };
74        };
75    };
76}
77
78global_asm! {
79    r#"
80        .extern __sel4_runtime_common__rust_entrypoint
81
82        .section .text.__sel4_runtime_common__call_rust_entrypoint, "ax", %progbits
83
84        .global __sel4_runtime_common__call_rust_entrypoint
85        __sel4_runtime_common__call_rust_entrypoint:
86    "#,
87    #[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
88    r#"
89            bl __sel4_runtime_common__rust_entrypoint
90        1:  b 1b
91    "#,
92    #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
93    r#"
94        .option push
95        .option norelax
96        1:  auipc gp, %pcrel_hi(__global_pointer$)
97            addi gp, gp, %pcrel_lo(1b)
98        .option pop
99            jal __sel4_runtime_common__rust_entrypoint
100        1:  j 1b
101    "#,
102    #[cfg(target_arch = "x86_64")]
103    r#"
104            call __sel4_runtime_common__rust_entrypoint
105        1:  jmp 1b
106    "#,
107}
108
109global_asm! {
110    r#"
111        .extern __sel4_runtime_common__stack_bottom
112        .extern __sel4_runtime_common__call_rust_entrypoint
113
114        .section .text.__sel4_runtime_common__stack_init, "ax", %progbits
115
116        .global __sel4_runtime_common__stack_init
117        __sel4_runtime_common__stack_init:
118    "#,
119    #[cfg(target_arch = "aarch64")]
120    r#"
121            ldr x9, =__sel4_runtime_common__stack_bottom
122            ldr x9, [x9]
123            mov sp, x9
124            b __sel4_runtime_common__call_rust_entrypoint
125    "#,
126    #[cfg(target_arch = "arm")]
127    r#"
128            ldr r8, =__sel4_runtime_common__stack_bottom
129            ldr r8, [r8]
130            mov sp, r8
131            b __sel4_runtime_common__call_rust_entrypoint
132    "#,
133    #[cfg(target_arch = "riscv64")]
134    r#"
135            la sp, __sel4_runtime_common__stack_bottom
136            ld sp, (sp)
137            j __sel4_runtime_common__call_rust_entrypoint
138    "#,
139    #[cfg(target_arch = "riscv32")]
140    r#"
141            la sp, __sel4_runtime_common__stack_bottom
142            lw sp, (sp)
143            j __sel4_runtime_common__call_rust_entrypoint
144    "#,
145    #[cfg(target_arch = "x86_64")]
146    r#"
147            mov rsp, __sel4_runtime_common__stack_bottom
148            mov rbp, rsp
149            sub rsp, 0x8 // Stack must be 16-byte aligned before call
150            push rbp
151            call __sel4_runtime_common__call_rust_entrypoint
152        1:  jmp 1b
153    "#,
154}