summaryrefslogtreecommitdiff
path: root/src/boot.s
blob: 7baa4638fd745e72493dc24175f96ccfda831992 (plain)
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
/*
References:
 https://wiki.osdev.org/Raspberry_Pi_Bare_Bones
 https://jsandler18.github.io/tutorial/boot.html
 https://jsandler18.github.io/explanations/boot_S.html
 https://www.rpi4os.com/part1-bootstrapping/
 https://developer.arm.com/documentation/102422/0100/Example-solutions/System-control-solution
 https://developer.arm.com/documentation/102422/0100/GAS-syntax-reference?lang=en
*/

.section ".text.boot"

.global _start

_start:
  // Let core 0.0.0.0 be the only one running for now. The other cores halt.
  // Each core has a unique affinity number: <aff3>.<aff2>.<aff1>.<aff0>.
  // On AArch64, Aff3 takes bits 39-32. The code below copies bits 39-32 over
  // to 31-24 so that we get a 32-bit value <aff3>.<aff2>.<aff1>.<aff0>.
  mrs   x0, MPIDR_EL1
  ubfx  x1, x0, #32, #8 // x1[0..7] = Aff3; x[8..63] = 0
  bfi   w0, w1, #24, #8 // w0[31..24] = Aff3
  cbnz  w0, halt        // All cores except 0.0.0.0 halt.

core0:
  // Initialize the stack. The stack will grow below this boot code.
  ldr x1, =_start
  mov sp, x1
  
  // Zero-initialize the BSS section.
  ldr x1, =__bss_start   // Start address of BSS section.
  ldr x2, =__bss_size    // Size of the BSS section.
bss_init_loop:
  str  xzr, [x1], #8     // Store 64-bit 0 to addr; increment addr by 8 bytes.
  sub  x2, x2, #8        // Decrement remaining size.
  cbnz x2, bss_init_loop // Loop back if remaining size is non-zero (>0).

  // Jump to C main()
  bl main

halt:
  wfi    // Wait for interrupt. Core enters low-power state.
  b halt // Loop back.