1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
    parse, parse_macro_input, spanned::Spanned, Ident, ItemFn, ReturnType, Type, Visibility,
};

#[proc_macro_attribute]
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
    let f = parse_macro_input!(input as ItemFn);

    let valid_signature = f.sig.constness.is_none()
        && f.vis == Visibility::Inherited
        && f.sig.abi.is_none()
        && f.sig.inputs.is_empty()
        && f.sig.generics.params.is_empty()
        && f.sig.generics.where_clause.is_none()
        && f.sig.variadic.is_none()
        && match f.sig.output {
            ReturnType::Default => false,
            ReturnType::Type(_, ref ty) => matches! {**ty, Type::Never(_)},
        };

    if !valid_signature {
        return parse::Error::new(
            f.span(),
            "`#[entry]` function must have signature `[unsafe] fn() -> !`",
        )
        .to_compile_error()
        .into();
    }

    if !args.is_empty() {
        return parse::Error::new(Span::call_site(), "This attribute accepts no arguments")
            .to_compile_error()
            .into();
    }

    let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site());
    let ident = &f.sig.ident;

    quote! {
        global_asm!("
        .macro mov_q, reg, val
        .if (((\\val) >> 31) == 0 || ((\\val) >> 31) == 0x1ffffffff)
        movz \\reg, :abs_g1_s:\\val
        .else
        .if (((\\val) >> 47) == 0 || ((\\val) >> 47) == 0x1ffff)
        movz \\reg, :abs_g2_s:\\val
        .else
        movz \\reg, :abs_g3:\\val
        movk \\reg, :abs_g2_nc:\\val
        .endif
        movk \\reg, :abs_g1_nc:\\val
        .endif
        movk \\reg, :abs_g0_nc:\\val
        .endm
    .section \".head.text\",\"ax\"
    _head:
      b stext
      .long 0

    .global stext; .align 2 ; stext:
    mov x21, x0
    bl el2_setup
    mov x1, #3 << 20
    msr cpacr_el1, x1
    adrp x4, __top_stack
    mov x1, x4
    mov sp, x1
    bl  clear_bss
    b   main
    .type stext, @function ; .size stext, .-stext
    .section \".idmap.text\",\"awx\"
    .globl el2_setup ; .align 2 ; el2_setup:
        msr SPsel, #1
        mrs x0, CurrentEL
        cmp x0, #(2 << 2)
        b.eq 1f
        mov_q x0, (((((1) << (11))) | (((1) << (20))) | (((1) << (22))) | (((1) << (28))) | (((1) << (29)))) | 0)
        msr sctlr_el1, x0
        mov w0, #(0xe11)
        isb
        ret

        1: mov_q x0, (((((1) << (4))) | (((1) << (5))) | (((1) << (11))) | (((1) << (16))) | (((1) << (18))) | (((1) << (22))) | (((1) << (23))) | (((1) << (28))) | (((1) << (29)))) | 0)
        msr sctlr_el2, x0

        mov_q x0, (((((1) << (11))) | (((1) << (20))) | (((1) << (22))) | (((1) << (28))) | (((1) << (29)))) | 0)
        msr sctlr_el1, x0

        mov x0, #(0x00000040 | 0x00000080 | 0x00000100 | 0x00000200 | 0x00000005)

        msr spsr_el2, x0
        msr elr_el2, lr
        mov w0, #(0xe12)
        eret
    .type el2_setup, @function ; .size el2_setup, .-el2_setup"
        );

        #[doc(hidden)]
        #[export_name = "main"]
        pub unsafe extern "C" fn #tramp_ident() {
            #ident()
        }

        #f
    }
    .into()
}