kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

entry.aa64.S (2208B)


      1 /* arm64 kernel entry: Image header + EL2->EL1 drop + stack + kmain.
      2  * Booted by qemu-system-aarch64 -kernel; exits via ARM semihosting
      3  * SYS_EXIT_EXTENDED with kmain's return value as the host exit code. */
      4 
      5     .section .text, "ax"
      6     .globl _start
      7 _start:
      8     b       stext                   /* 0x00 */
      9     .long   0                       /* 0x04 */
     10     .quad   0x80000                 /* 0x08 text_offset */
     11     .quad   _end - _start           /* 0x10 image_size */
     12     .quad   0xa                     /* 0x18 flags: 4K, anywhere, LE */
     13     .quad   0                       /* 0x20 */
     14     .quad   0                       /* 0x28 */
     15     .quad   0                       /* 0x30 */
     16     .ascii  "ARM\x64"               /* 0x38 magic */
     17     .long   0                       /* 0x3c PE COFF offset */
     18 
     19 stext:
     20     msr     daifset, #0xf
     21 
     22     mrs     x9, CurrentEL
     23     lsr     x9, x9, #2
     24     cmp     x9, #2
     25     b.ne    in_el1
     26 
     27     mov     x9, #(1 << 31)
     28     msr     hcr_el2, x9
     29     mov     x9, #0x3c5
     30     msr     spsr_el2, x9
     31     adrp    x9, in_el1
     32     add     x9, x9, :lo12:in_el1
     33     msr     elr_el2, x9
     34     adrp    x9, kstack_top
     35     add     x9, x9, :lo12:kstack_top
     36     msr     sp_el1, x9
     37     eret
     38 
     39 in_el1:
     40     adrp    x9, kstack_top
     41     add     x9, x9, :lo12:kstack_top
     42     mov     sp, x9
     43 
     44     bl      kmain
     45 
     46     /* QEMU semihosting exit. SYS_EXIT_EXTENDED (op 0x20) takes x1 =
     47      * pointer to [reason, subcode]; ADP_Stopped_ApplicationExit
     48      * (0x20026) returns subcode as the host exit code. */
     49     cbnz    w0, .Lfail
     50     mov     w0, #0x20
     51     adrp    x1, .Lexit_ok
     52     add     x1, x1, :lo12:.Lexit_ok
     53     hlt     #0xf000
     54 .Lhang_ok:
     55     wfe
     56     b       .Lhang_ok
     57 .Lfail:
     58     mov     w0, #0x20
     59     adrp    x1, .Lexit_fail
     60     add     x1, x1, :lo12:.Lexit_fail
     61     hlt     #0xf000
     62 .Lhang_fail:
     63     wfe
     64     b       .Lhang_fail
     65 
     66     .section .rodata, "a"
     67     .balign 8
     68 .Lexit_ok:
     69     .quad   0x20026                 /* ADP_Stopped_ApplicationExit */
     70     .quad   0                       /* subcode = host exit 0 */
     71 .Lexit_fail:
     72     .quad   0x20026
     73     .quad   1                       /* subcode = host exit 1 */
     74 
     75     .section .bss, "aw", %nobits
     76     .balign 16
     77 kstack_bottom:
     78     .skip   4096
     79 kstack_top: