Skip to contentSkip to conversation

more i386 head.S cleanups

PATCH In review Brian Gerst 6 participants 7 messages Apr 3, 2004 – Apr 15, 2004

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 more i386 head.S cleanups

- Move empty_zero_page and swapper_pg_dir to BSS. This requires that BSS is cleared earlier, but reclaims over 3k that was lost due to page alignment.
- Move stack_start, ready, and int_msg, boot_gdt_descr, idt_descr, and cpu_gdt_descr to .data. They were interfering with disassembly while in .text.

--
Brian Gerst

arch/i386/kernel/head.S +42 -52
diff -urN linux-bk/arch/i386/kernel/head.S linux/arch/i386/kernel/head.S
@@ -69,9 +69,22 @@
 	movl %eax,%gs
 
 /*
+ * Clear BSS first so that there are no surprises...
+ * No need to cld as DF is already clear from cld above...
+ */
+	xorl %eax,%eax
+	movl $__bss_start - __PAGE_OFFSET,%edi
+	movl $__bss_stop - __PAGE_OFFSET,%ecx
+	subl %edi,%ecx
+	shrl $2,%ecx
+	rep ; stosl
+
+/*
  * Initialize page tables.  This creates a PDE and a set of page
  * tables, which are located immediately beyond _end.  The variable
  * init_pg_tables_end is set up to point to the first "safe" location.
+ * Mappings are created both at virtual address 0 (identity mapping)
+ * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
  *
  * Warning: don't use %esi or the stack in this code.  However, %esp
  * can be used as a GPR if you really need it...
@@ -173,17 +186,6 @@
 #endif /* CONFIG_SMP */
 
 /*
- * Clear BSS first so that there are no surprises...
- * No need to cld as DF is already clear from cld above...
- */
-	xorl %eax,%eax
-	movl $__bss_start,%edi
-	movl $__bss_stop,%ecx
-	subl %edi,%ecx
-	shrl $2,%ecx
-	rep ; stosl
-
-/*
  * start system 32-bit setup. We need to re-do some of the things done
  * in 16-bit mode for the "real" operations.
  */
@@ -304,8 +306,6 @@
 	jmp L6			# main should never return here, but
 				# just in case, we know what happens.
 
-ready:	.byte 0
-
 /*
  * We depend on ET to be correct. This checks for 287/387.
  */
@@ -353,13 +353,7 @@
 	jne rp_sidt
 	ret
 
-ENTRY(stack_start)
-	.long init_thread_union+THREAD_SIZE
-	.long __BOOT_DS
-
 /* This is the default interrupt "handler" :-) */
-int_msg:
-	.asciz "Unknown interrupt or fault at EIP %p %p %p\n"
 	ALIGN
 ignore_int:
 	cld
@@ -386,6 +380,35 @@
 	iret
 
 /*
+ * Real beginning of normal "text" segment
+ */
+ENTRY(stext)
+ENTRY(_stext)
+
+/*
+ * BSS section
+ */
+.section ".bss.page_aligned","w"
+ENTRY(swapper_pg_dir)
+	.fill 1024,4,0
+ENTRY(empty_zero_page)
+	.fill 4096,1,0
+
+/*
+ * This starts the data section.
+ */
+.data
+
+ENTRY(stack_start)
+	.long init_thread_union+THREAD_SIZE
+	.long __BOOT_DS
+
+ready:	.byte 0
+
+int_msg:
+	.asciz "Unknown interrupt or fault at EIP %p %p %p\n"
+
+/*
  * The IDT and GDT 'descriptors' are a strange 48-bit object
  * only used by the lidt and lgdt instructions. They are not
  * like usual segment descriptors - they consist of a 16-bit
@@ -417,39 +440,6 @@
 	.fill NR_CPUS-1,8,0		# space for the other GDT descriptors
 
 /*
- * swapper_pg_dir is the main page directory, address 0x00101000
- *
- * This is initialized to create an identity-mapping at 0 (for bootup
- * purposes) and another mapping at virtual address PAGE_OFFSET.  The
- * values put here should be all invalid (zero); the valid
- * entries are created dynamically at boot time.
- *
- * The code creates enough page tables to map 0-_end, the page tables
- * themselves, plus INIT_MAP_BEYOND_END bytes; see comment at beginning.
- */
-.org 0x1000
-ENTRY(swapper_pg_dir)
-	.fill 1024,4,0
-
-.org 0x2000
-ENTRY(empty_zero_page)
-	.fill 4096,1,0
-
-.org 0x3000
-/*
- * Real beginning of normal "text" segment
- */
-ENTRY(stext)
-ENTRY(_stext)
-
-/*
- * This starts the data section. Note that the above is all
- * in the text section because it has alignment requirements
- * that we cannot fulfill any other way.
- */
-.data
-
-/*
  * The boot_gdt_table must mirror the equivalent in setup.S and is
  * used only for booting.
  */
arch/i386/kernel/vmlinux.lds.S +4 -1
diff -urN linux-bk/arch/i386/kernel/vmlinux.lds.S linux/arch/i386/kernel/vmlinux.lds.S
@@ -105,7 +105,10 @@
   /* freed after init ends here */
 	
   __bss_start = .;		/* BSS */
-  .bss : { *(.bss) }
+  .bss : {
+	*(.bss.page_aligned)
+	*(.bss)
+  }
   . = ALIGN(4);
   __bss_stop = .; 
 
#1 lore raw
Quoted 6 lines · Brian Gerst

- Move empty_zero_page and swapper_pg_dir to BSS. This requires that BSS is cleared earlier, but reclaims over 3k that was lost due to page alignment.
- Move stack_start, ready, and int_msg, boot_gdt_descr, idt_descr, and cpu_gdt_descr to .data. They were interfering with disassembly while in .text.

Nice. Do you mean 3k here or 0x3000?

On a related note, I've been sitting on this patch which reorders the bootstrap code so we can free most of it once we're up:

drop i386 bootstrap text

From: Zwane Mwaikambo <[email protected]>
Subject: [PATCH][2.6-tiny] free i386 bootstrap text

Shuffle the idt/gdt descriptors around so that we get a full page and then free it.

Index: linux-2.6.1-rc1-tiny2/arch/i386/kernel/head.S
===================================================================
RCS file: /home/cvsroot/linux-2.6.1-rc1-tiny2/arch/i386/kernel/head.S,v retrieving revision 1.1.1.1
diff -u -p -B -r1.1.1.1 head.S

 tiny-mpm/arch/i386/kernel/head.S |   40 +++++++++++++++++++--------------------
 tiny-mpm/arch/i386/mm/init.c     |    8 +++++++
 2 files changed, 28 insertions(+), 20 deletions(-)
arch/i386/kernel/head.S renamed from arch/i386/kernel/head.S~bootstrap-free +20 -20
diff -puN arch/i386/kernel/head.S~bootstrap-free arch/i386/kernel/head.S
@@ -386,6 +386,26 @@ ignore_int:
 	iret
 
 /*
+ * swapper_pg_dir is the main page directory, address 0x00101000
+ *
+ * This is initialized to create an identity-mapping at 0 (for bootup
+ * purposes) and another mapping at virtual address PAGE_OFFSET.  The
+ * values put here should be all invalid (zero); the valid
+ * entries are created dynamically at boot time.
+ *
+ * The code creates enough page tables to map 0-_end, the page tables
+ * themselves, plus INIT_MAP_BEYOND_END bytes; see comment at beginning.
+ */
+.org 0x1000
+ENTRY(swapper_pg_dir)
+	.fill 1024,4,0
+
+.org 0x2000
+ENTRY(empty_zero_page)
+	.fill 4096,1,0
+
+.org 0x3000
+/*
  * The IDT and GDT 'descriptors' are a strange 48-bit object
  * only used by the lidt and lgdt instructions. They are not
  * like usual segment descriptors - they consist of a 16-bit
@@ -417,26 +437,6 @@ cpu_gdt_descr:
 	.fill NR_CPUS-1,8,0		# space for the other GDT descriptors
 
 /*
- * swapper_pg_dir is the main page directory, address 0x00101000
- *
- * This is initialized to create an identity-mapping at 0 (for bootup
- * purposes) and another mapping at virtual address PAGE_OFFSET.  The
- * values put here should be all invalid (zero); the valid
- * entries are created dynamically at boot time.
- *
- * The code creates enough page tables to map 0-_end, the page tables
- * themselves, plus INIT_MAP_BEYOND_END bytes; see comment at beginning.
- */
-.org 0x1000
-ENTRY(swapper_pg_dir)
-	.fill 1024,4,0
-
-.org 0x2000
-ENTRY(empty_zero_page)
-	.fill 4096,1,0
-
-.org 0x3000
-/*
  * Real beginning of normal "text" segment
  */
 ENTRY(stext)
arch/i386/mm/init.c renamed from arch/i386/mm/init.c~bootstrap-free +8 -0
diff -puN arch/i386/mm/init.c~bootstrap-free arch/i386/mm/init.c
@@ -586,6 +586,14 @@ void free_initmem(void)
 		free_page(addr);
 		totalram_pages++;
 	}
+
+	/* free early bootstrap code in head.S */
+	addr = (unsigned long)&_text;
+	ClearPageReserved(virt_to_page(addr));
+	set_page_count(virt_to_page(addr), 1);
+	free_page(addr);
+	totalram_pages++;
+
 	printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
 }
 

_

Signature · 1 line
Matt Mackall : http://www.selenic.com : Linux development and consulting
#2 lore raw
Quoted 9 lines · Matt Mackall
Quoted 4 lines · Brian Gerst

- Move empty_zero_page and swapper_pg_dir to BSS. This requires that BSS is cleared earlier, but reclaims over 3k that was lost due to page alignment.
- Move stack_start, ready, and int_msg, boot_gdt_descr, idt_descr, and cpu_gdt_descr to .data. They were interfering with disassembly while in .text.

Nice. Do you mean 3k here or 0x3000?

3 kilobytes apporiximately. Most of the first page was wasted because swapper_pg_dir aligns to the start of the next page.

Quoted 4 lines

On a related note, I've been sitting on this patch which reorders the bootstrap code so we can free most of it once we're up:

The bootstrap page tables can easily be reclaimed if large pages are used, but the bootstrap code needs more care, especially with hotplug cpus.

--
Brian Gerst

#3 lore raw
Quoted 6 lines · Brian Gerst
Quoted 3 lines

On a related note, I've been sitting on this patch which reorders the bootstrap code so we can free most of it once we're up:

The bootstrap page tables can easily be reclaimed if large pages are used, but the bootstrap code needs more care, especially with hotplug cpus.

This would be fine for hotplug cpus as well, we never hit that path again.

#4 lore raw
In reply to Brian Gerst
Quoted 5 lines · Brian Gerst

- Move empty_zero_page and swapper_pg_dir to BSS. This requires that BSS is cleared earlier, but reclaims over 3k that was lost due to page alignment.
- Move stack_start, ready, and int_msg, boot_gdt_descr, idt_descr, and cpu_gdt_descr to .data. They were interfering with disassembly while in .text.

Interesting, two years ago in 2002, you sent a patch basically of the same idea.

http://www.uwsg.iu.edu/hypermail/linux/kernel/0208.0/0850.html

Signature · 2 lines
Coywolf Qi Hunt
Admin of http://GreatCN.org and http://LoveCN.org
#5 lore raw

On Thu, 15 Apr 2004 22:14:19 +0800 Coywolf Qi Hunt wrote:

| Brian Gerst wrote:
| 
| > - Move empty_zero_page and swapper_pg_dir to BSS.  This requires that 
| > BSS is cleared earlier, but reclaims over 3k that was lost due to page 
| > alignment.
| > - Move stack_start, ready, and int_msg, boot_gdt_descr, idt_descr, and 
| > cpu_gdt_descr to .data.  They were interfering with disassembly while in 
| > .text.
| > 
| 
| Interesting, two years ago in 2002, you sent a patch basically of the
| same idea.
| 
| http://www.uwsg.iu.edu/hypermail/linux/kernel/0208.0/0850.html

and your point is??

--
~Randy

#6 lore raw
In reply to Brian Gerst
Followup to:  <[email protected]>
By author:    Brian Gerst <[email protected]>
In newsgroup: linux.dev.kernel
Quoted 8 lines

- Move empty_zero_page and swapper_pg_dir to BSS. This requires that BSS is cleared earlier, but reclaims over 3k that was lost due to page alignment.
- Move stack_start, ready, and int_msg, boot_gdt_descr, idt_descr, and cpu_gdt_descr to .data. They were interfering with disassembly while in .text.

Some of them should be in .rodata instead of .data.

.data should only be used for modifiable numbers.

All in all these patches are a good idea; I had the same thoughts when I was doing the changes to the boot page tables, but didn't want to change too much at once.

-hpa