Skip to contentSkip to conversation

arm/arm64: Zero BSS and stack at startup

PATCH In review Andrew Jones 2 participants 4 messages Mar 22, 2021 – Mar 23, 2021 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm/arm64: Zero BSS and stack at startup

So far we've counted on QEMU or kvmtool implicitly zeroing all memory. With our goal of eventually supporting bare-metal targets with target-efi we should explicitly zero any memory we expect to be zeroed ourselves. This obviously includes the BSS, but also the bootcpu's stack, as the bootcpu's thread-info lives in the stack and may get used in early setup to get the cpu index. Note, this means we still assume the bootcpu's cpu index to be zero. That assumption can be removed later.

---
 arm/cstart.S   | 20 ++++++++++++++++++++
 arm/cstart64.S | 22 +++++++++++++++++++++-
 arm/flat.lds   |  4 ++++
 3 files changed, 45 insertions(+), 1 deletion(-)
arm/cstart.S +20 -0
index ef936ae2f874..9eb11ba4dcaf 100644
@@ -15,12 +15,32 @@
 
 #define THREAD_START_SP ((THREAD_SIZE - S_FRAME_SIZE * 8) & ~7)
 
+.macro zero_range, tmp1, tmp2, tmp3, tmp4
+	mov	\tmp3, #0
+	mov	\tmp4, #0
+9998:	cmp	\tmp1, \tmp2
+	beq	9997f
+	strd	\tmp3, \tmp4, [\tmp1], #8
+	b	9998b
+9997:
+.endm
+
 .arm
 
 .section .init
 
 .globl start
 start:
+	/* zero BSS */
+	ldr	r4, =bss
+	ldr	r5, =ebss
+	zero_range r4, r5, r6, r7
+
+	/* zero stack */
+	ldr	r5, =stacktop
+	sub	r4, r5, #THREAD_SIZE
+	zero_range r4, r5, r6, r7
+
 	/*
 	 * set stack, making room at top of stack for cpu0's
 	 * exception stacks. Must start wtih stackptr, not
arm/cstart64.S +21 -1
index 0428014aa58a..2a691f8f5065 100644
@@ -12,6 +12,15 @@
 #include <asm/processor.h>
 #include <asm/page.h>
 #include <asm/pgtable-hwdef.h>
+#include <asm/thread_info.h>
+
+.macro zero_range, tmp1, tmp2
+9998:	cmp	\tmp1, \tmp2
+	b.eq	9997f
+	stp	xzr, xzr, [\tmp1], #16
+	b	9998b
+9997:
+.endm
 
 .section .init
 
@@ -51,7 +60,18 @@ start:
 	b	1b
 
 1:
-	/* set up stack */
+	/* zero BSS */
+	adrp	x4, bss
+	add	x4, x4, :lo12:bss
+	adrp    x5, ebss
+	add     x5, x5, :lo12:ebss
+	zero_range x4, x5
+
+	/* zero and set up stack */
+	adrp    x5, stacktop
+	add     x5, x5, :lo12:stacktop
+	sub	x4, x5, #THREAD_SIZE
+	zero_range x4, x5
 	mov	x4, #1
 	msr	spsel, x4
 	isb
arm/flat.lds +4 -0
index 25f8d03cba87..4d43cdfeab41 100644
@@ -17,7 +17,11 @@ SECTIONS
 
     .rodata   : { *(.rodata*) }
     .data     : { *(.data) }
+    . = ALIGN(16);
+    PROVIDE(bss = .);
     .bss      : { *(.bss) }
+    . = ALIGN(16);
+    PROVIDE(ebss = .);
     . = ALIGN(64K);
     PROVIDE(edata = .);
 
Signature · 1 line
2.26.3
#1 lore raw
Quoted 9 lines · Andrew Jones

So far we've counted on QEMU or kvmtool implicitly zeroing all memory. With our goal of eventually supporting bare-metal targets with target-efi we should explicitly zero any memory we expect to be zeroed ourselves. This obviously includes the BSS, but also the bootcpu's stack, as the bootcpu's thread-info lives in the stack and may get used in early setup to get the cpu index. Note, this means we still assume the bootcpu's cpu index to be zero. That assumption can be removed later.

Quoted 5 lines

---

Sorry, I forgot the v2 changelog.

- Optimize zero_range [Alexandru]
- Calculate the bottom of the thread-info rather than use a linker script symbol [Alexandru]

Quoted 101 lines
 arm/cstart.S   | 20 ++++++++++++++++++++
 arm/cstart64.S | 22 +++++++++++++++++++++-
 arm/flat.lds   |  4 ++++
 3 files changed, 45 insertions(+), 1 deletion(-)
arm/cstart.S +20 -0
index ef936ae2f874..9eb11ba4dcaf 100644
@@ -15,12 +15,32 @@
 
 #define THREAD_START_SP ((THREAD_SIZE - S_FRAME_SIZE * 8) & ~7)
 
+.macro zero_range, tmp1, tmp2, tmp3, tmp4
+	mov	\tmp3, #0
+	mov	\tmp4, #0
+9998:	cmp	\tmp1, \tmp2
+	beq	9997f
+	strd	\tmp3, \tmp4, [\tmp1], #8
+	b	9998b
+9997:
+.endm
+
 .arm
 
 .section .init
 
 .globl start
 start:
+	/* zero BSS */
+	ldr	r4, =bss
+	ldr	r5, =ebss
+	zero_range r4, r5, r6, r7
+
+	/* zero stack */
+	ldr	r5, =stacktop
+	sub	r4, r5, #THREAD_SIZE
+	zero_range r4, r5, r6, r7
+
 	/*
 	 * set stack, making room at top of stack for cpu0's
 	 * exception stacks. Must start wtih stackptr, not
arm/cstart64.S +21 -1
index 0428014aa58a..2a691f8f5065 100644
@@ -12,6 +12,15 @@
 #include <asm/processor.h>
 #include <asm/page.h>
 #include <asm/pgtable-hwdef.h>
+#include <asm/thread_info.h>
+
+.macro zero_range, tmp1, tmp2
+9998:	cmp	\tmp1, \tmp2
+	b.eq	9997f
+	stp	xzr, xzr, [\tmp1], #16
+	b	9998b
+9997:
+.endm
 
 .section .init
 
@@ -51,7 +60,18 @@ start:
 	b	1b
 
 1:
-	/* set up stack */
+	/* zero BSS */
+	adrp	x4, bss
+	add	x4, x4, :lo12:bss
+	adrp    x5, ebss
+	add     x5, x5, :lo12:ebss
+	zero_range x4, x5
+
+	/* zero and set up stack */
+	adrp    x5, stacktop
+	add     x5, x5, :lo12:stacktop
+	sub	x4, x5, #THREAD_SIZE
+	zero_range x4, x5
 	mov	x4, #1
 	msr	spsel, x4
 	isb
arm/flat.lds +4 -0
index 25f8d03cba87..4d43cdfeab41 100644
@@ -17,7 +17,11 @@ SECTIONS
 
     .rodata   : { *(.rodata*) }
     .data     : { *(.data) }
+    . = ALIGN(16);
+    PROVIDE(bss = .);
     .bss      : { *(.bss) }
+    . = ALIGN(16);
+    PROVIDE(ebss = .);
     . = ALIGN(64K);
     PROVIDE(edata = .);
 
Signature · 1 line
2.26.3
Reviewed-by on v2 1/1
#2 lore raw
In reply to Andrew Jones

Hi Drew,

Quoted 24 lines · Andrew Jones

So far we've counted on QEMU or kvmtool implicitly zeroing all memory. With our goal of eventually supporting bare-metal targets with target-efi we should explicitly zero any memory we expect to be zeroed ourselves. This obviously includes the BSS, but also the bootcpu's stack, as the bootcpu's thread-info lives in the stack and may get used in early setup to get the cpu index. Note, this means we still assume the bootcpu's cpu index to be zero. That assumption can be removed later.

---
 arm/cstart.S   | 20 ++++++++++++++++++++
 arm/cstart64.S | 22 +++++++++++++++++++++-
 arm/flat.lds   |  4 ++++
 3 files changed, 45 insertions(+), 1 deletion(-)
arm/cstart.S +0 -0
index ef936ae2f874..9eb11ba4dcaf 100644

arm/cstart.S line 18

Quoted 4 lines
arm/cstart.S +1 -0
@@ -15,12 +15,32 @@
 
 #define THREAD_START_SP ((THREAD_SIZE - S_FRAME_SIZE * 8) & ~7)
 
+.macro zero_range, tmp1, tmp2, tmp3, tmp4

tmp1 and tmp2 could be renamed to start and end (same for arm64), but the macro is nice and simple, and it's also pretty clear what the arguments represent from the call sites. Looks good either way:

Thanks,

Alex

Quoted 84 lines
arm/cstart.S +19 -0
+	mov	\tmp3, #0
+	mov	\tmp4, #0
+9998:	cmp	\tmp1, \tmp2
+	beq	9997f
+	strd	\tmp3, \tmp4, [\tmp1], #8
+	b	9998b
+9997:
+.endm
+
 .arm
 
 .section .init
 
 .globl start
 start:
+	/* zero BSS */
+	ldr	r4, =bss
+	ldr	r5, =ebss
+	zero_range r4, r5, r6, r7
+
+	/* zero stack */
+	ldr	r5, =stacktop
+	sub	r4, r5, #THREAD_SIZE
+	zero_range r4, r5, r6, r7
+
 	/*
 	 * set stack, making room at top of stack for cpu0's
 	 * exception stacks. Must start wtih stackptr, not
arm/cstart64.S +21 -1
index 0428014aa58a..2a691f8f5065 100644
@@ -12,6 +12,15 @@
 #include <asm/processor.h>
 #include <asm/page.h>
 #include <asm/pgtable-hwdef.h>
+#include <asm/thread_info.h>
+
+.macro zero_range, tmp1, tmp2
+9998:	cmp	\tmp1, \tmp2
+	b.eq	9997f
+	stp	xzr, xzr, [\tmp1], #16
+	b	9998b
+9997:
+.endm
 
 .section .init
 
@@ -51,7 +60,18 @@ start:
 	b	1b
 
 1:
-	/* set up stack */
+	/* zero BSS */
+	adrp	x4, bss
+	add	x4, x4, :lo12:bss
+	adrp    x5, ebss
+	add     x5, x5, :lo12:ebss
+	zero_range x4, x5
+
+	/* zero and set up stack */
+	adrp    x5, stacktop
+	add     x5, x5, :lo12:stacktop
+	sub	x4, x5, #THREAD_SIZE
+	zero_range x4, x5
 	mov	x4, #1
 	msr	spsel, x4
 	isb
arm/flat.lds +4 -0
index 25f8d03cba87..4d43cdfeab41 100644
@@ -17,7 +17,11 @@ SECTIONS
 
     .rodata   : { *(.rodata*) }
     .data     : { *(.data) }
+    . = ALIGN(16);
+    PROVIDE(bss = .);
     .bss      : { *(.bss) }
+    . = ALIGN(16);
+    PROVIDE(ebss = .);
     . = ALIGN(64K);
     PROVIDE(edata = .);
 
author Applied
#3 lore raw
Quoted 28 lines · Alexandru Elisei

Hi Drew,

Quoted 25 lines · Andrew Jones

So far we've counted on QEMU or kvmtool implicitly zeroing all memory. With our goal of eventually supporting bare-metal targets with target-efi we should explicitly zero any memory we expect to be zeroed ourselves. This obviously includes the BSS, but also the bootcpu's stack, as the bootcpu's thread-info lives in the stack and may get used in early setup to get the cpu index. Note, this means we still assume the bootcpu's cpu index to be zero. That assumption can be removed later.

---
 arm/cstart.S   | 20 ++++++++++++++++++++
 arm/cstart64.S | 22 +++++++++++++++++++++-
 arm/flat.lds   |  4 ++++
 3 files changed, 45 insertions(+), 1 deletion(-)
arm/cstart.S +0 -0
index ef936ae2f874..9eb11ba4dcaf 100644
@@ -15,12 +15,32 @@
 
Quoted 7 lines

arm/cstart.S line 18

Quoted 3 lines
arm/cstart.S +1 -0
 #define THREAD_START_SP ((THREAD_SIZE - S_FRAME_SIZE * 8) & ~7)
 
+.macro zero_range, tmp1, tmp2, tmp3, tmp4

tmp1 and tmp2 could be renamed to start and end (same for arm64), but the macro is nice and simple, and it's also pretty clear what the arguments represent from the call sites. Looks good either way:

I considered tmp1 and tmp2 being start and end, but start gets used like a temporary anyway.

Quoted 2 lines

Thanks!

Applied to arm/queue

https://gitlab.com/rhdrjones/kvm-unit-tests/-/commits/arm/queue

Quoted 90 lines

Thanks,

Alex

Quoted 84 lines
arm/cstart.S +19 -0
+	mov	\tmp3, #0
+	mov	\tmp4, #0
+9998:	cmp	\tmp1, \tmp2
+	beq	9997f
+	strd	\tmp3, \tmp4, [\tmp1], #8
+	b	9998b
+9997:
+.endm
+
 .arm
 
 .section .init
 
 .globl start
 start:
+	/* zero BSS */
+	ldr	r4, =bss
+	ldr	r5, =ebss
+	zero_range r4, r5, r6, r7
+
+	/* zero stack */
+	ldr	r5, =stacktop
+	sub	r4, r5, #THREAD_SIZE
+	zero_range r4, r5, r6, r7
+
 	/*
 	 * set stack, making room at top of stack for cpu0's
 	 * exception stacks. Must start wtih stackptr, not
arm/cstart64.S +21 -1
index 0428014aa58a..2a691f8f5065 100644
@@ -12,6 +12,15 @@
 #include <asm/processor.h>
 #include <asm/page.h>
 #include <asm/pgtable-hwdef.h>
+#include <asm/thread_info.h>
+
+.macro zero_range, tmp1, tmp2
+9998:	cmp	\tmp1, \tmp2
+	b.eq	9997f
+	stp	xzr, xzr, [\tmp1], #16
+	b	9998b
+9997:
+.endm
 
 .section .init
 
@@ -51,7 +60,18 @@ start:
 	b	1b
 
 1:
-	/* set up stack */
+	/* zero BSS */
+	adrp	x4, bss
+	add	x4, x4, :lo12:bss
+	adrp    x5, ebss
+	add     x5, x5, :lo12:ebss
+	zero_range x4, x5
+
+	/* zero and set up stack */
+	adrp    x5, stacktop
+	add     x5, x5, :lo12:stacktop
+	sub	x4, x5, #THREAD_SIZE
+	zero_range x4, x5
 	mov	x4, #1
 	msr	spsel, x4
 	isb
arm/flat.lds +4 -0
index 25f8d03cba87..4d43cdfeab41 100644
@@ -17,7 +17,11 @@ SECTIONS
 
     .rodata   : { *(.rodata*) }
     .data     : { *(.data) }
+    . = ALIGN(16);
+    PROVIDE(bss = .);
     .bss      : { *(.bss) }
+    . = ALIGN(16);
+    PROVIDE(ebss = .);
     . = ALIGN(64K);
     PROVIDE(edata = .);