Skip to content

Commit 2cdff1c

Browse files
committed
refactor: move BSS initializing from Assembly to Swift
1 parent 40ff515 commit 2cdff1c

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

Sources/AsmSupport/aarch64/asmfunc.S

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ get_kernel_end:
2929
add x0, x0, :lo12:__kernel_end
3030
ret
3131

32+
.global get_bss_start
33+
get_bss_start:
34+
adrp x0, __bss_start
35+
add x0, x0, :lo12:__bss_start
36+
ret
37+
38+
.global get_bss_end
39+
get_bss_end:
40+
adrp x0, __bss_end
41+
add x0, x0, :lo12:__bss_end
42+
ret
43+
3244
#endif

Sources/AsmSupport/aarch64/boot.S

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ el1_entry:
6565
ldr x1, =_start
6666
mov sp, x1
6767

68-
// clear bss
69-
ldr x1, =__bss_start
70-
ldr w2, =__bss_size
71-
clear_bss_loop:
72-
cbz w2, jump_to_main
73-
str xzr, [x1], #8 // stores a 64-bit doubleword so __bss_end must be 8-byte aligned
74-
sub w2, w2, #1
75-
cbnz w2, clear_bss_loop
76-
7768
jump_to_main:
7869
// jump to Swift code, should not return
7970
bl Kernel_main

Sources/AsmSupport/include/Support.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ void delay(uint64_t);
66
void halt();
77
uintptr_t get_kernel_start();
88
uintptr_t get_kernel_end();
9+
uintptr_t get_bss_start();
10+
uintptr_t get_bss_end();
911
#ifdef __aarch64__
1012
uint32_t get_el();
1113
#endif

Sources/Kernel/Kernel.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,25 @@ private import AsmSupport
44
private import RaspberryPi
55
#endif
66

7+
func zeroBSS() {
8+
let start = get_bss_start()
9+
let end = get_bss_end()
10+
let size = end - start
11+
12+
precondition(start % 8 == 0)
13+
precondition(size % 8 == 0)
14+
15+
// swift-format-ignore: NeverForceUnwrap
16+
let ptr = unsafe UnsafeMutableRawPointer(bitPattern: start)!
17+
18+
unsafe ptr.initializeMemory(as: UInt64.self, repeating: 0, count: Int(size >> 3))
19+
}
20+
721
@main
822
struct Kernel {
923
static func main() {
24+
zeroBSS()
25+
1026
#if RASPI
1127
initUART()
1228

linker.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ SECTIONS {
2424
*(.swift_modhash*)
2525
}
2626
}
27-
__bss_size = (__bss_end - __bss_start) >> 3;

0 commit comments

Comments
 (0)