linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Eric W. Biederman" <ebiederm@xmission.com>
To: Andrew Morton <akpm@osdl.org>
Cc: <fastboot@lists.osdl.org>, <linux-kernel@vger.kernel.org>
Subject: [PATCH 11/29] x86_64-entry64
Date: Wed, 19 Jan 2005 0:31:37 -0700	[thread overview]
Message-ID: <x86-64-entry64-1106119897218@ebiederm.dsl.xmission.com> (raw)
In-Reply-To: <x86-64-vmlinux-fix-physical-addrs-11061198972723@ebiederm.dsl.xmission.com>


To enable bootloaders to boot to directly load the x86_64 vmlinux
and to enable the x86_64 kernel to switch into 64bit mode earlier
this patch refactors the x86_64 entry code so there is a native
64bit entry point to the kernel.

I ran this by Andi Kleen and he agreed it looks fairly sane.

Signed-off-by: Eric Biederman <ebiederm@xmission.com>
---

 head.S        |  112 ++++++++++++++++++++++++++++++++--------------------------
 smpboot.c     |    2 -
 trampoline.S  |   24 +++---------
 vmlinux.lds.S |    3 +
 4 files changed, 70 insertions(+), 71 deletions(-)

diff -uNr linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/head.S linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/head.S
--- linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/head.S	Fri Jan 14 04:28:33 2005
+++ linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/head.S	Tue Jan 18 22:46:24 2005
@@ -26,6 +26,7 @@
 
 	.text
 	.code32
+	.globl startup_32
 /* %bx:	 1 if coming from smp trampoline on secondary cpu */ 
 startup_32:
 	
@@ -37,11 +38,13 @@
  	 * There is no stack until we set one up.
 	 */
 
-	movl %ebx,%ebp	/* Save trampoline flag */
-	
+	/* Initialize the %ds segment register */
 	movl $__KERNEL_DS,%eax
 	movl %eax,%ds
-	
+
+	/* Load new GDT with the 64bit segments using 32bit descriptor */
+	lgdt	pGDT32 - __START_KERNEL_map
+
 	/* If the CPU doesn't support CPUID this will double fault.
 	 * Unfortunately it is hard to check for CPUID without a stack. 
 	 */
@@ -57,16 +60,13 @@
 	btl	$29, %edx
 	jnc	no_long_mode
 
-	movl	%edx,%edi
-	
 	/*
 	 * Prepare for entering 64bits mode
 	 */
 
-	/* Enable PAE mode and PGE */
+	/* Enable PAE mode */
 	xorl	%eax, %eax
 	btsl	$5, %eax
-	btsl	$7, %eax
 	movl	%eax, %cr4
 
 	/* Setup early boot stage 4 level pagetables */
@@ -79,14 +79,6 @@
 
 	/* Enable Long Mode */
 	btsl	$_EFER_LME, %eax
-	/* Enable System Call */
-	btsl	$_EFER_SCE, %eax
-
-	/* No Execute supported? */	
-	btl	$20,%edi
-	jnc     1f
-	btsl	$_EFER_NX, %eax
-1:	
 				
 	/* Make changes effective */
 	wrmsr
@@ -94,38 +86,69 @@
 	xorl	%eax, %eax
 	btsl	$31, %eax			/* Enable paging and in turn activate Long Mode */
 	btsl	$0, %eax			/* Enable protected mode */
-	btsl	$1, %eax			/* Enable MP */
-	btsl	$4, %eax			/* Enable ET */
-	btsl	$5, %eax			/* Enable NE */
-	btsl	$16, %eax			/* Enable WP */
-	btsl	$18, %eax			/* Enable AM */
 	/* Make changes effective */
 	movl	%eax, %cr0
-	jmp	reach_compatibility_mode
-reach_compatibility_mode:
-	
 	/*
 	 * At this point we're in long mode but in 32bit compatibility mode
 	 * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
-	 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
+	 * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
 	 * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
 	 */
-
-	testw %bp,%bp	/* secondary CPU? */ 
-	jnz   second	
-	
-	/* Load new GDT with the 64bit segment using 32bit descriptor */
-	movl	$(pGDT32 - __START_KERNEL_map), %eax
-	lgdt	(%eax)
-
-second:	
-	movl    $(ljumpvector - __START_KERNEL_map), %eax
-	/* Finally jump in 64bit mode */
-	ljmp	*(%eax)
+	ljmp	$__KERNEL_CS, $(startup_64 - __START_KERNEL_map)
 
 	.code64
 	.org 0x100	
-reach_long64:
+	.globl startup_64
+startup_64:
+	/* We come here either from startup_32
+	 * or directly from a 64bit bootloader.
+	 * Since we may have come directly from a bootloader we
+	 * reload the page tables here.
+	 */
+	
+	/* Enable PAE mode and PGE */
+	xorq	%rax, %rax
+	btsq	$5, %rax
+	btsq	$7, %rax
+	movq	%rax, %cr4
+
+	/* Setup early boot stage 4 level pagetables. */
+	movq	$(init_level4_pgt - __START_KERNEL_map), %rax
+	movq	%rax, %cr3
+
+	/* Check if nx is implemented */
+	movl	$0x80000001, %eax
+	cpuid
+	movl	%edx,%edi
+	
+	/* Setup EFER (Extended Feature Enable Register) */
+	movl	$MSR_EFER, %ecx
+	rdmsr
+
+	/* Enable System Call */
+	btsl	$_EFER_SCE, %eax
+
+	/* No Execute supported? */
+	btl	$20,%edi
+	jnc     1f
+	btsl	$_EFER_NX, %eax
+1:	
+	/* Make changes effective */
+	wrmsr
+
+	/* Setup cr0 */
+	xorq	%rax, %rax
+	btsq	$31, %rax			/* Enable paging */
+	btsq	$0, %rax			/* Enable protected mode */
+	btsq	$1, %rax			/* Enable MP */
+	btsq	$4, %rax			/* Enable ET */
+	btsq	$5, %rax			/* Enable NE */
+	btsq	$16, %rax			/* Enable WP */
+	btsq	$18, %rax			/* Enable AM */
+	/* Make changes effective */
+	movq	%rax, %cr0
+
+	/* Setup a boot time stack */
 	movq init_rsp(%rip),%rsp
 
 	/* zero EFLAGS after setting rsp */
@@ -198,13 +221,8 @@
 .org 0xf00
 	.globl pGDT32
 pGDT32:
-	.word	gdt32_end-gdt_table32
-	.long	gdt_table32-__START_KERNEL_map
-
-.org 0xf10	
-ljumpvector:
-	.long	reach_long64-__START_KERNEL_map
-	.word	__KERNEL_CS
+	.word	gdt_end-cpu_gdt_table
+	.long	cpu_gdt_table-__START_KERNEL_map
 
 ENTRY(stext)
 ENTRY(_stext)
@@ -334,12 +352,6 @@
 	.endr
 #endif
 
-ENTRY(gdt_table32)
-	.quad	0x0000000000000000	/* This one is magic */
-	.quad	0x0000000000000000	/* unused */
-	.quad	0x00af9a000000ffff	/* __KERNEL_CS */
-gdt32_end:	
-	
 /* We need valid kernel segments for data and code in long mode too
  * IRET will check the segment types  kkeil 2000/10/28
  * Also sysret mandates a special GDT layout 
diff -uNr linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/smpboot.c linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/smpboot.c
--- linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/smpboot.c	Fri Jan 14 04:28:33 2005
+++ linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/smpboot.c	Tue Jan 18 22:46:24 2005
@@ -91,8 +91,6 @@
 static unsigned long __init setup_trampoline(void)
 {
 	void *tramp = __va(SMP_TRAMPOLINE_BASE); 
-	extern volatile __u32 tramp_gdt_ptr; 
-	tramp_gdt_ptr = __pa_symbol(&cpu_gdt_table); 
 	memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
 	return virt_to_phys(tramp);
 }
diff -uNr linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/trampoline.S linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/trampoline.S
--- linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/trampoline.S	Mon Oct 18 15:55:06 2004
+++ linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/trampoline.S	Tue Jan 18 22:46:24 2005
@@ -37,7 +37,6 @@
 	mov	%cs, %ax	# Code and data in the same place
 	mov	%ax, %ds
 
-	mov	$1, %bx		# Flag an SMP trampoline
 	cli			# We should be safe anyway
 
 	movl	$0xA5A5A5A5, trampoline_data - r_base
@@ -46,31 +45,20 @@
 	lidt	idt_48 - r_base	# load idt with 0, 0
 	lgdt	gdt_48 - r_base	# load gdt with whatever is appropriate
 
-	movw    $__KERNEL_DS,%ax
-	movw    %ax,%ds
-	movw    %ax,%es
-	
 	xor	%ax, %ax
 	inc	%ax		# protected mode (PE) bit
 	lmsw	%ax		# into protected mode
-	jmp	flush_instr
-flush_instr:
-	ljmpl	$__KERNEL32_CS, $0x00100000
-			# jump to startup_32 in arch/x86_64/kernel/head.S
-
+	# flaush prefetch and jump to startup_32 in arch/x86_64/kernel/head.S
+	ljmpl	$__KERNEL32_CS, $(startup_32-__START_KERNEL_map)
+	
+	# Careful these need to be in the same 64K segment as the above;
 idt_48:
 	.word	0			# idt limit = 0
 	.word	0, 0			# idt base = 0L
 
 gdt_48:
-	.short	0x0800			# gdt limit = 2048, 256 GDT entries
-	.globl tramp_gdt_ptr
-tramp_gdt_ptr:
-	.long	0			# gdt base = gdt (first SMP CPU)
-					# this is filled in by C because the 64bit
-					# linker doesn't support absolute 32bit
-					# relocations. 
-	
+	.short	__KERNEL32_CS + 7	# gdt limit
+	.long	cpu_gdt_table-__START_KERNEL_map
 
 .globl trampoline_end
 trampoline_end:	
diff -uNr linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/vmlinux.lds.S linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/vmlinux.lds.S
--- linux-2.6.11-rc1-mm1-nokexec-x86_64-vmlinux-fix-physical-addrs/arch/x86_64/kernel/vmlinux.lds.S	Tue Jan 18 22:46:07 2005
+++ linux-2.6.11-rc1-mm1-nokexec-x86_64-entry64/arch/x86_64/kernel/vmlinux.lds.S	Tue Jan 18 22:46:24 2005
@@ -10,11 +10,12 @@
 
 OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64")
 OUTPUT_ARCH(i386:x86-64)
-ENTRY(stext)
+ENTRY(phys_startup_64)
 jiffies_64 = jiffies;
 SECTIONS
 {
   . = __START_KERNEL;
+  phys_startup_64 = startup_64 - LOAD_OFFSET;
   _text = .;			/* Text and read-only data */
   .text :  AT(ADDR(.text) - LOAD_OFFSET) {
 	*(.text)

  reply	other threads:[~2005-01-19  8:14 UTC|newest]

Thread overview: 110+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-19  7:31 [PATCH 0/29] overview Eric W. Biederman
2005-01-19  7:31 ` [PATCH 1/29] x86-rename-apic_mode_exint Eric W. Biederman
2005-01-19  7:31   ` [PATCH 2/29] x86-local-apic-fix Eric W. Biederman
2005-01-19  7:31     ` [PATCH 3/29] x86_64-e820-64bit Eric W. Biederman
2005-01-19  7:31       ` [PATCH 4/29] x86-i8259-shutdown Eric W. Biederman
2005-01-19  7:31         ` [PATCH 5/29] x86_64-i8259-shutdown Eric W. Biederman
2005-01-19  7:31           ` [PATCH 6/29] x86-apic-virtwire-on-shutdown Eric W. Biederman
2005-01-19  7:31             ` [PATCH 7/29] x86_64-apic-virtwire-on-shutdown Eric W. Biederman
2005-01-19  7:31               ` [PATCH 8/29] vmlinux-fix-physical-addrs Eric W. Biederman
2005-01-19  7:31                 ` [PATCH 9/29] x86-vmlinux-fix-physical-addrs Eric W. Biederman
2005-01-19  7:31                   ` [PATCH 10/29] x86_64-vmlinux-fix-physical-addrs Eric W. Biederman
2005-01-19  7:31                     ` Eric W. Biederman [this message]
2005-01-19  7:31                       ` [PATCH 12/29] x86-config-kernel-start Eric W. Biederman
2005-01-19  7:31                         ` [PATCH 13/29] x86_64-config-kernel-start Eric W. Biederman
2005-01-19  7:31                           ` [PATCH 14/29] kexec-kexec-generic Eric W. Biederman
2005-01-19  7:31                             ` [PATCH 15/29] x86-machine_shutdown Eric W. Biederman
2005-01-19  7:31                               ` [PATCH 16/29] x86-kexec Eric W. Biederman
2005-01-19  7:31                                 ` [PATCH 17/29] x86-crashkernel Eric W. Biederman
2005-01-19  7:31                                   ` [PATCH 18/29] x86_64-machine_shutdown Eric W. Biederman
2005-01-19  7:31                                     ` [PATCH 19/29] x86_64-kexec Eric W. Biederman
2005-01-19  7:31                                       ` [PATCH 20/29] x86_64-crashkernel Eric W. Biederman
2005-01-19  7:31                                         ` [PATCH 21/29] kexec-ppc-support Eric W. Biederman
2005-01-19  7:31                                           ` [PATCH 22/29] x86-crash_shutdown-nmi-shootdown Eric W. Biederman
2005-01-19  7:31                                             ` [PATCH 23/29] x86-crash_shutdown-snapshot-registers Eric W. Biederman
2005-01-19  7:31                                               ` [PATCH 24/29] x86-crash_shutdown-apic-shutdown Eric W. Biederman
2005-01-19  7:31                                                 ` [PATCH 25/29] crashdump-documentation Eric W. Biederman
2005-01-19  7:31                                                   ` [PATCH 26/29] crashdump-memory-preserving-reboot-using-kexec Eric W. Biederman
2005-01-19  7:31                                                     ` [PATCH 27/29] crashdump-routines-for-copying-dump-pages Eric W. Biederman
2005-01-19  7:31                                                       ` [PATCH 28/29] crashdump-elf-format-dump-file-access Eric W. Biederman
2005-01-19  7:31                                                         ` [PATCH 29/29] crashdump-linear-raw-format-dump-file-access Eric W. Biederman
2005-01-19 12:25                                       ` [PATCH 19/29] x86_64-kexec Andi Kleen
2005-01-20 15:50                                       ` Adrian Bunk
2005-01-20 18:06                                         ` [Fastboot] " Eric W. Biederman
2005-01-19 12:10                                 ` [PATCH 16/29] x86-kexec Hariprasad Nellitheertha
2005-01-19 18:17                                   ` [Fastboot] " Eric W. Biederman
2005-01-25  3:54             ` [PATCH 6/29] x86-apic-virtwire-on-shutdown Len Brown
2005-01-25  6:39               ` Eric W. Biederman
2005-01-25  7:36                 ` Len Brown
2005-01-25  9:11                   ` Eric W. Biederman
2005-01-25  3:32         ` [PATCH 4/29] x86-i8259-shutdown Len Brown
2005-01-25  3:59           ` Dave Jones
2005-01-25  6:30             ` Eric W. Biederman
2005-01-25  8:35             ` Eric W. Biederman
2005-01-25  9:43               ` Barry K. Nathan
2005-01-25 10:14                 ` Eric W. Biederman
2005-01-25 10:49                   ` Barry K. Nathan
2005-01-25 11:40                     ` Eric W. Biederman
2005-01-25 20:57                       ` Barry K. Nathan
2005-01-25 12:12                     ` Eric W. Biederman
2005-01-25 22:02                       ` Barry K. Nathan
2005-01-25 22:12                         ` Eric W. Biederman
2005-01-26 13:27                           ` Sytse Wielinga
2005-01-26 14:06                             ` Eric W. Biederman
2005-01-26 14:43                               ` Sytse Wielinga
2005-01-26 15:12                                 ` Eric W. Biederman
2005-01-26 22:58                                   ` Barry K. Nathan
2005-01-21  7:55 ` [PATCH] Reserving backup region for kexec based crashdumps Vivek Goyal
2005-01-21  7:54   ` [Fastboot] " Eric W. Biederman
2005-01-21 10:57     ` Vivek Goyal
2005-01-21 11:13       ` Eric W. Biederman
2005-01-23 10:14         ` Vivek Goyal
2005-01-26 17:21           ` Eric W. Biederman
2005-01-26 19:15             ` Andrew Morton
2005-01-27 13:45             ` Vivek Goyal
2005-01-27 20:45               ` Eric W. Biederman
2005-01-28 13:06                 ` Vivek Goyal
2005-01-28 20:29                   ` Eric W. Biederman
2005-02-01 15:17                     ` Vivek Goyal
2005-02-01 15:26                       ` Eric W. Biederman
2005-02-02  7:10                         ` Itsuro Oda
2005-02-02  7:49                           ` Koichi Suzuki
2005-02-02 15:24                             ` Eric W. Biederman
2005-02-03  7:28                               ` Itsuro Oda
2005-02-03  9:00                                 ` Eric W. Biederman
2005-02-03 23:18                                   ` Itsuro Oda
2005-02-04  0:41                                     ` Eric W. Biederman
2005-02-04  1:07                                     ` Itsuro Oda
2005-02-16  8:49                                   ` [PATCH] /proc/cpumem Itsuro Oda
2005-02-16 13:58                                     ` Eric W. Biederman
2005-02-17  0:43                                       ` Itsuro Oda
2005-02-17  9:55                                         ` [Fastboot] " Eric W. Biederman
2005-02-18  6:17                                           ` Itsuro Oda
2005-02-18  7:22                                             ` Eric W. Biederman
2005-02-17  0:17                                     ` YAMAMOTO Takashi
2005-02-17  5:58                                     ` [Fastboot] " Vivek Goyal
2005-02-17  6:18                                       ` Itsuro Oda
2005-02-17 18:18                                     ` Dave Jones
2005-02-17 19:46                                       ` [Fastboot] " Eric W. Biederman
2005-02-02 14:26                           ` [Fastboot] [PATCH] Reserving backup region for kexec based crashdumps Eric W. Biederman
2005-02-02 10:07                         ` Vivek Goyal
2005-02-02 15:42                           ` Eric W. Biederman
2005-02-03 14:47                             ` Vivek Goyal
2005-02-01  8:04                 ` Koichi Suzuki
2005-02-01  9:06                   ` Eric W. Biederman
2005-02-02  7:42                     ` Itsuro Oda
2005-02-02 14:45                       ` Eric W. Biederman
2005-02-04  0:23                         ` Itsuro Oda
2005-02-04  1:55                           ` Eric W. Biederman
2005-02-02  9:08                     ` Koichi Suzuki
2005-02-02 14:31                       ` Eric W. Biederman
2005-02-03  7:02               ` Hirokazu Takahashi
2005-02-03  9:01                 ` Vivek Goyal
2005-02-03  9:37                   ` Hirokazu Takahashi
2005-02-03 10:07                     ` Eric W. Biederman
2005-02-03  9:13                 ` Eric W. Biederman
2005-02-03 10:10                   ` Hirokazu Takahashi
2005-02-03 10:39                     ` Eric W. Biederman
2005-02-04 10:05                       ` Hirokazu Takahashi
2005-02-04 11:17                         ` Eric W. Biederman
2005-02-04 12:02                           ` Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=x86-64-entry64-1106119897218@ebiederm.dsl.xmission.com \
    --to=ebiederm@xmission.com \
    --cc=akpm@osdl.org \
    --cc=fastboot@lists.osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).