diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.s | 44 | ||||
-rw-r--r-- | src/kernel.c | 4 | ||||
-rw-r--r-- | src/link.ld | 20 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/boot.s b/src/boot.s new file mode 100644 index 0000000..7baa463 --- /dev/null +++ b/src/boot.s | |||
@@ -0,0 +1,44 @@ | |||
1 | /* | ||
2 | References: | ||
3 | https://wiki.osdev.org/Raspberry_Pi_Bare_Bones | ||
4 | https://jsandler18.github.io/tutorial/boot.html | ||
5 | https://jsandler18.github.io/explanations/boot_S.html | ||
6 | https://www.rpi4os.com/part1-bootstrapping/ | ||
7 | https://developer.arm.com/documentation/102422/0100/Example-solutions/System-control-solution | ||
8 | https://developer.arm.com/documentation/102422/0100/GAS-syntax-reference?lang=en | ||
9 | */ | ||
10 | |||
11 | .section ".text.boot" | ||
12 | |||
13 | .global _start | ||
14 | |||
15 | _start: | ||
16 | // Let core 0.0.0.0 be the only one running for now. The other cores halt. | ||
17 | // Each core has a unique affinity number: <aff3>.<aff2>.<aff1>.<aff0>. | ||
18 | // On AArch64, Aff3 takes bits 39-32. The code below copies bits 39-32 over | ||
19 | // to 31-24 so that we get a 32-bit value <aff3>.<aff2>.<aff1>.<aff0>. | ||
20 | mrs x0, MPIDR_EL1 | ||
21 | ubfx x1, x0, #32, #8 // x1[0..7] = Aff3; x[8..63] = 0 | ||
22 | bfi w0, w1, #24, #8 // w0[31..24] = Aff3 | ||
23 | cbnz w0, halt // All cores except 0.0.0.0 halt. | ||
24 | |||
25 | core0: | ||
26 | // Initialize the stack. The stack will grow below this boot code. | ||
27 | ldr x1, =_start | ||
28 | mov sp, x1 | ||
29 | |||
30 | // Zero-initialize the BSS section. | ||
31 | ldr x1, =__bss_start // Start address of BSS section. | ||
32 | ldr x2, =__bss_size // Size of the BSS section. | ||
33 | bss_init_loop: | ||
34 | str xzr, [x1], #8 // Store 64-bit 0 to addr; increment addr by 8 bytes. | ||
35 | sub x2, x2, #8 // Decrement remaining size. | ||
36 | cbnz x2, bss_init_loop // Loop back if remaining size is non-zero (>0). | ||
37 | |||
38 | // Jump to C main() | ||
39 | bl main | ||
40 | |||
41 | halt: | ||
42 | wfi // Wait for interrupt. Core enters low-power state. | ||
43 | b halt // Loop back. | ||
44 | |||
diff --git a/src/kernel.c b/src/kernel.c new file mode 100644 index 0000000..eb7d832 --- /dev/null +++ b/src/kernel.c | |||
@@ -0,0 +1,4 @@ | |||
1 | void main() { | ||
2 | while (1); | ||
3 | } | ||
4 | |||
diff --git a/src/link.ld b/src/link.ld new file mode 100644 index 0000000..f1d1730 --- /dev/null +++ b/src/link.ld | |||
@@ -0,0 +1,20 @@ | |||
1 | SECTIONS | ||
2 | { | ||
3 | . = 0x80000; /* Kernel load address for AArch64 */ | ||
4 | .text (READONLY) : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } | ||
5 | .rodata (READONLY) : { *(.rodata .rodata.* .gnu.linkonce.r*) } | ||
6 | PROVIDE(_data = .); | ||
7 | .data : { *(.data .data.* .gnu.linkonce.d*) } | ||
8 | .bss (NOLOAD) : { | ||
9 | . = ALIGN(16); | ||
10 | __bss_start = .; | ||
11 | *(.bss .bss.*) | ||
12 | *(COMMON) | ||
13 | __bss_end = .; | ||
14 | } | ||
15 | _end = .; | ||
16 | |||
17 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } | ||
18 | } | ||
19 | __bss_size = (__bss_end - __bss_start); | ||
20 | |||