x86, head_32: Some cleanups, -v2
Borislav Petkov
maintainercover x86, head_32: Some cleanups, -v2
From: Borislav Petkov <[email protected]>
Ok,
here's the next version with new_cpu_data left put and two minor fixlets added at the end. The patchset was boot-tested on a bunch of baremetal boxes and all QEMU cpu models - no issues.
Boot tests:
* baremetal:
- P4
- Atom n270
- 32-bit kernel on an AMD64 (F10h Phenom and Intel SNB)
* qemu, with cpu models:
- qemu64
- phenom
- core2duo
- kvm64
- qemu32
- kvm32
- coreduo
- 486{,SX}
- pentium{,2,3}
- athlon
- n270,+movbe
- Conroe
- Penryn
- Nehalem
- Westmere
- SandyBridge
- Haswell
- Opteron_G{1,2,3,4,5}
Why am I testing all those, you ask? Because I'm a sadistic mofo :-)
Changelog:
v1:
here are some initial low-hanging fruits wrt head_32.S cleanup. I've made them as easily digestible as possible; after all, this is boot asm and meddling with it tends to upset kernels.
Also, I've made the assumption that having boot_cpu_data.cpuid_level contain the CPUID level for the boot cpu means that the APs have the same CPUID level. This should be the case on X86.
They boot fine 486 and 486SX in qemu but I'd like to hear whether the direction I'm going is ok before I continue testing them on real hardware.
Borislav Petkov (5):
x86, head_32: Remove i386 pieces
x86: Detect CPUID support early at boot
x86, head_32: Remove second CPUID detection from default_entry x86, head_32: Give the 6 label a real name
x86, head_32: Remove an old gcc2 fix
arch/x86/kernel/head_32.S | 92 ++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 57 deletions(-)
Signature · 1 line
1.8.1.3.535.ga923c31
Borislav Petkov
maintainerPATCH 1/5 x86, head_32: Remove i386 pieces
From: Borislav Petkov <[email protected]>
Remove code fragments detecting a 386 CPU since we don't support those anymore. Also, do not do alignment checks because they're done only at CPL3. Also, no need to preserve EFLAGS.
--- arch/x86/kernel/head_32.S | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-)
arch/x86/kernel/head_32.S +5 -17
@@ -405,30 +405,21 @@ default_entry: jz 1f # Did we do this already? call *%eax 1: - -/* check if it is 486 or 386. */ + /* - * XXX - this does a lot of unnecessary setup. Alignment checks don't - * apply at our cpl of 0 and the stack ought to be aligned already, and - * we don't need to preserve eflags. + * Check if it is 486 */ movl $-1,X86_CPUID # -1 for no CPUID initially - movb $3,X86 # at least 386 + movb $4,X86 # at least 486 pushfl # push EFLAGS popl %eax # get EFLAGS movl %eax,%ecx # save original EFLAGS - xorl $0x240000,%eax # flip AC and ID bits in EFLAGS + xorl $0x200000,%eax # flip ID bit in EFLAGS pushl %eax # copy to EFLAGS popfl # set EFLAGS pushfl # get new EFLAGS popl %eax # put it in eax xorl %ecx,%eax # change in flags - pushl %ecx # restore original EFLAGS - popfl - testl $0x40000,%eax # check if AC bit changed - je is386 - - movb $4,X86 # at least 486 testl $0x200000,%eax # check if ID bit changed je is486
@@ -456,10 +447,7 @@ default_entry: movl %edx,X86_CAPABILITY is486: movl $0x50022,%ecx # set AM, WP, NE and MP - jmp 2f - -is386: movl $2,%ecx # set MP -2: movl %cr0,%eax + movl %cr0,%eax andl $0x80000011,%eax # Save PG,PE,ET orl %ecx,%eax movl %eax,%cr0
Signature · 1 line
1.8.1.3.535.ga923c31
Borislav Petkov
maintainerPATCH 2/5 x86: Detect CPUID support early at boot
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
We detect CPUID function support on each CPU and save it for later use, obviating the need to play the toggle EFLAGS.ID game every time. C code is looking at ->cpuid_level anyway.
--- arch/x86/kernel/head_32.S | 48 +++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-)
arch/x86/kernel/head_32.S +24 -24
@@ -318,30 +318,38 @@ default_entry: movl %eax,%cr0 /* - * New page tables may be in 4Mbyte page mode and may - * be using the global pages. + * Initialize EFLAGS. Some BIOSes leave bits like NT set. This would confuse the + * debugger if this code is traced. Best to initialize before switching to + * protected mode. + */ + + pushl $0 + popfl + +/* + * New page tables may be in 4Mbyte page mode and may be using the global pages. * - * NOTE! If we are on a 486 we may have no cr4 at all! - * Specifically, cr4 exists if and only if CPUID exists - * and has flags other than the FPU flag set. + * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists + * if and only if CPUID exists and has flags other than the FPU flag set. */ + movl $-1,pa(X86_CPUID) # preset CPUID level movl $X86_EFLAGS_ID,%ecx pushl %ecx - popfl - pushfl - popl %eax - pushl $0 - popfl + popfl # set EFLAGS=ID pushfl - popl %edx - xorl %edx,%eax - testl %ecx,%eax - jz 6f # No ID flag = no CPUID = no CR4 + popl %eax # get EFLAGS + testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set? + jz 6f # hw disallowed setting of ID bit + # which means no CPUID and no CR4 + + xorl %eax,%eax + cpuid + movl %eax,pa(X86_CPUID) # save largest std CPUID function movl $1,%eax cpuid - andl $~1,%edx # Ignore CPUID.FPU - jz 6f # No flags or only CPUID.FPU = no CR4 + andl $~1,%edx # Ignore CPUID.FPU + jz 6f # No flags or only CPUID.FPU = no CR4 movl pa(mmu_cr4_features),%eax movl %eax,%cr4
@@ -389,14 +397,6 @@ default_entry: addl $__PAGE_OFFSET, %esp /* - * Initialize eflags. Some BIOS's leave bits like NT set. This would - * confuse the debugger if this code is traced. - * XXX - best to initialize before switching to protected mode. - */ - pushl $0 - popfl - -/* * start system 32-bit setup. We need to re-do some of the things done * in 16-bit mode for the "real" operations. */
Signature · 1 line
1.8.1.3.535.ga923c31
Borislav Petkov
maintainerPATCH 3/5 x86, head_32: Remove second CPUID detection from default_entry
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
We do that once earlier now and cache it into new_cpu_data.cpuid_level so no need for the EFLAGS.ID toggling dance anymore.
--- arch/x86/kernel/head_32.S | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
arch/x86/kernel/head_32.S +4 -13
@@ -409,18 +409,7 @@ default_entry: /* * Check if it is 486 */ - movl $-1,X86_CPUID # -1 for no CPUID initially - movb $4,X86 # at least 486 - pushfl # push EFLAGS - popl %eax # get EFLAGS - movl %eax,%ecx # save original EFLAGS - xorl $0x200000,%eax # flip ID bit in EFLAGS - pushl %eax # copy to EFLAGS - popfl # set EFLAGS - pushfl # get new EFLAGS - popl %eax # put it in eax - xorl %ecx,%eax # change in flags - testl $0x200000,%eax # check if ID bit changed + cmpl $-1,X86_CPUID je is486 /* get vendor info */
@@ -446,7 +435,9 @@ default_entry: movb %cl,X86_MASK movl %edx,X86_CAPABILITY -is486: movl $0x50022,%ecx # set AM, WP, NE and MP +is486: + movb $4,X86 + movl $0x50022,%ecx # set AM, WP, NE and MP movl %cr0,%eax andl $0x80000011,%eax # Save PG,PE,ET orl %ecx,%eax
Signature · 1 line
1.8.1.3.535.ga923c31
Borislav Petkov
maintainerPATCH 4/5 x86, head_32: Give the 6 label a real name
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Jumping here we are about to enable paging so rename the label accordingly.
--- arch/x86/kernel/head_32.S | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/kernel/head_32.S +6 -6
@@ -339,7 +339,7 @@ default_entry: pushfl popl %eax # get EFLAGS testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set? - jz 6f # hw disallowed setting of ID bit + jz enable_paging # hw disallowed setting of ID bit # which means no CPUID and no CR4 xorl %eax,%eax
@@ -349,13 +349,13 @@ default_entry: movl $1,%eax cpuid andl $~1,%edx # Ignore CPUID.FPU - jz 6f # No flags or only CPUID.FPU = no CR4 + jz enable_paging # No flags or only CPUID.FPU = no CR4 movl pa(mmu_cr4_features),%eax movl %eax,%cr4 testb $X86_CR4_PAE, %al # check if PAE is enabled - jz 6f + jz enable_paging /* Check if extended functions are implemented */ movl $0x80000000, %eax
@@ -363,7 +363,7 @@ default_entry: /* Value must be in the range 0x80000001 to 0x8000ffff */ subl $0x80000001, %eax cmpl $(0x8000ffff-0x80000001), %eax - ja 6f + ja enable_paging /* Clear bogus XD_DISABLE bits */ call verify_cpu
@@ -372,7 +372,7 @@ default_entry: cpuid /* Execute Disable bit supported? */ btl $(X86_FEATURE_NX & 31), %edx - jnc 6f + jnc enable_paging /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx
@@ -382,7 +382,7 @@ default_entry: /* Make changes effective */ wrmsr -6: +enable_paging: /* * Enable paging
Signature · 1 line
1.8.1.3.535.ga923c31
Borislav Petkov
maintainerPATCH 5/5 x86, head_32: Remove an old gcc2 fix
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
gcc2 wants direction flag cleared but we don't support gcc2 anymore. So drop it. Original patch adding this was:
commit 57d40092c375d2b6d34f814f5fb306967e22c4f5
Author: linus1 <[email protected]> Date: Mon Nov 9 12:00:00 1992 -0600
[PATCH] Linux-0.98.4 (November 9, 1992)
...
--- arch/x86/kernel/head_32.S | 1 - 1 file changed, 1 deletion(-)
arch/x86/kernel/head_32.S +0 -1
@@ -463,7 +463,6 @@ is486: xorl %eax,%eax # Clear LDT lldt %ax - cld # gcc2 wants the direction flag cleared at all times pushl $0 # fake return address for unwinder jmp *(initial_code)
Signature · 1 line
1.8.1.3.535.ga923c31
H. Peter Anvin
reviewerRe: [PATCH 5/5] x86, head_32: Remove an old gcc2 fix
ALL versions of gcc want DF=0 at all times...
Quoted 29 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
gcc2 wants direction flag cleared but we don't support gcc2 anymore. So drop it. Original patch adding this was:
commit
57d40092c375d2b6d34f814f5fb306967e22c4f5Author: linus1 <[email protected]> Date: Mon Nov 9 12:00:00 1992 -0600[PATCH] Linux-0.98.4 (November 9, 1992)...
Signed-off-by Borislav Petkov <[email protected]>---
arch/x86/kernel/head_32.S| 1 -1 file changed, 1 deletion(-)arch/x86/kernel/head_32.S +0
-1@@ -463,7 +463,6 @@ is486: xorl %eax,%eax # Clear LDT lldt %ax - cld # gcc2 wants the direction flag cleared at all times pushl $0 # fake return address for unwinder jmp *(initial_code)
Signature · 1 line
Sent from my mobile phone. Please excuse brevity and lack of formatting.
H. Peter Anvin
reviewerRe: [PATCH 5/5] x86, head_32: Remove an old gcc2 fix
In reply to Borislav PetkovHowever... DF should have been cleared long before this...
Quoted 29 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
gcc2 wants direction flag cleared but we don't support gcc2 anymore. So drop it. Original patch adding this was:
commit
57d40092c375d2b6d34f814f5fb306967e22c4f5Author: linus1 <[email protected]> Date: Mon Nov 9 12:00:00 1992 -0600[PATCH] Linux-0.98.4 (November 9, 1992)...
Signed-off-by Borislav Petkov <[email protected]>---
arch/x86/kernel/head_32.S| 1 -1 file changed, 1 deletion(-)arch/x86/kernel/head_32.S +0
-1@@ -463,7 +463,6 @@ is486: xorl %eax,%eax # Clear LDT lldt %ax - cld # gcc2 wants the direction flag cleared at all times pushl $0 # fake return address for unwinder jmp *(initial_code)
Signature · 1 line
Sent from my mobile phone. Please excuse brevity and lack of formatting.
Borislav Petkov
maintainerRe: [PATCH 5/5] x86, head_32: Remove an old gcc2 fix
Quoted 1 line · H. Peter Anvin
However... DF should have been cleared long before this...
How about we do this at the beginning of default_entry where we clear EFLAGS too:
arch/x86/kernel/head_32.S +2 -1
@@ -322,10 +322,11 @@ default_entry: * debugger if this code is traced. Best to initialize before switching to * protected mode. */ - pushl $0 popfl + cld # GCC wants DF=0 at all times + /* * New page tables may be in 4Mbyte page mode and may be using the global pages. *
--
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Borislav Petkov
maintainerPATCH 5/5 x86, head_32: Clear DF much earlier
From: Borislav Petkov <[email protected]>
All GCC versions expect the direction flag to be cleared (DF=0) so move this to the default entry point for each core.
--- arch/x86/kernel/head_32.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/head_32.S +2 -2
@@ -322,10 +322,11 @@ default_entry: * debugger if this code is traced. Best to initialize before switching to * protected mode. */ - pushl $0 popfl + cld # GCC wants DF=0 at all times + /* * New page tables may be in 4Mbyte page mode and may be using the global pages. *
@@ -463,7 +464,6 @@ is486: xorl %eax,%eax # Clear LDT lldt %ax - cld # gcc2 wants the direction flag cleared at all times pushl $0 # fake return address for unwinder jmp *(initial_code)
Signature · 1 line
1.8.1.3.535.ga923c31
H. Peter Anvin
reviewerRe: [PATCH 5/5] x86, head_32: Remove an old gcc2 fix
In reply to Borislav PetkovQuoted 11 lines · Borislav Petkov
Quoted 1 line · H. Peter Anvin
However... DF should have been cleared long before this...
How about we do this at the beginning of
default_entrywhere we clear EFLAGS too:arch/x86/kernel/head_32.S +0
-0
arch/x86/kernel/head_32.S default_entry: · line 329
Quoted 9 lines
arch/x86/kernel/head_32.S +2
-1@@ -322,10 +322,11 @@ default_entry: * debugger if this code is traced. Best to initialize before switching to * protected mode. */ - pushl $0 popfl + cld # GCC wants DF=0 at all times +
The pushfl/popfl sequence clears DF too...
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Borislav Petkov
maintainerRe: [PATCH 5/5] x86, head_32: Remove an old gcc2 fix
Quoted 1 line · H. Peter Anvin
The pushfl/popfl sequence clears DF too...
Yes, indeed, good realization!
Ok, I'll fold that fact as a comment into the 2/5 patch resend it only as a reply to this mail so as not to spam unnecessarily.
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Borislav Petkov
maintainerPATCH 2/5 x86: Detect CPUID support early at boot
From: Borislav Petkov <[email protected]>
We detect CPUID function support on each CPU and save it for later use, obviating the need to play the toggle EFLAGS.ID game every time. C code is looking at ->cpuid_level anyway.
--- arch/x86/kernel/head_32.S | 48 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 25 deletions(-)
arch/x86/kernel/head_32.S +23 -25
@@ -318,30 +318,37 @@ default_entry: movl %eax,%cr0 /* - * New page tables may be in 4Mbyte page mode and may - * be using the global pages. + * Initialize EFLAGS. Some BIOSes leave bits like NT set. This would confuse the + * debugger if this code is traced. Best to initialize before switching to + * protected mode. As a side effect, we clear DF too because GCC expects it so. + */ + pushl $0 + popfl + +/* + * New page tables may be in 4Mbyte page mode and may be using the global pages. * - * NOTE! If we are on a 486 we may have no cr4 at all! - * Specifically, cr4 exists if and only if CPUID exists - * and has flags other than the FPU flag set. + * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists + * if and only if CPUID exists and has flags other than the FPU flag set. */ + movl $-1,pa(X86_CPUID) # preset CPUID level movl $X86_EFLAGS_ID,%ecx pushl %ecx - popfl - pushfl - popl %eax - pushl $0 - popfl + popfl # set EFLAGS=ID pushfl - popl %edx - xorl %edx,%eax - testl %ecx,%eax - jz 6f # No ID flag = no CPUID = no CR4 + popl %eax # get EFLAGS + testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set? + jz 6f # hw disallowed setting of ID bit + # which means no CPUID and no CR4 + + xorl %eax,%eax + cpuid + movl %eax,pa(X86_CPUID) # save largest std CPUID function movl $1,%eax cpuid - andl $~1,%edx # Ignore CPUID.FPU - jz 6f # No flags or only CPUID.FPU = no CR4 + andl $~1,%edx # Ignore CPUID.FPU + jz 6f # No flags or only CPUID.FPU = no CR4 movl pa(mmu_cr4_features),%eax movl %eax,%cr4
@@ -389,14 +396,6 @@ default_entry: addl $__PAGE_OFFSET, %esp /* - * Initialize eflags. Some BIOS's leave bits like NT set. This would - * confuse the debugger if this code is traced. - * XXX - best to initialize before switching to protected mode. - */ - pushl $0 - popfl - -/* * start system 32-bit setup. We need to re-do some of the things done * in 16-bit mode for the "real" operations. */
@@ -472,7 +471,6 @@ is486: movl $0x50022,%ecx # set AM, WP, NE and MP xorl %eax,%eax # Clear LDT lldt %ax - cld # gcc2 wants the direction flag cleared at all times pushl $0 # fake return address for unwinder jmp *(initial_code)
Signature · 1 line
1.8.1.3.535.ga923c31
H. Peter Anvin
reviewerRe: [PATCH 2/5 -v2.1] x86: Detect CPUID support early at boot
Quoted 18 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
We detect CPUID function support on each CPU and save it for later use, obviating the need to play the toggle EFLAGS.ID game every time. C code is looking at ->cpuid_level anyway.
Signed-off-by Borislav Petkov <[email protected]>--- arch/x86/kernel/head_32.S | 48 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 25 deletions(-)arch/x86/kernel/head_32.S +0
-0@@ -318,30 +318,37 @@ default_entry: movl %eax,%cr0
arch/x86/kernel/head_32.S default_entry: · line 327
Quoted 10 lines
arch/x86/kernel/head_32.S +7
-2/* - * New page tables may be in 4Mbyte page mode and may - * be using the global pages. + * Initialize EFLAGS. Some BIOSes leave bits like NT set. This would confuse the + * debugger if this code is traced. Best to initialize before switching to + * protected mode. As a side effect, we clear DF too because GCC expects it so. + */ + pushl $0 + popfl +
I wouldn't really call it a "side effect". Perhaps the right thing here is to say something like "we want to start out with %eflags unambiguously clear".
(Note also we have had to CLD earlier because we have already copied the command line.)
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Borislav Petkov
maintainerRe: [PATCH 2/5 -v2.1] x86: Detect CPUID support early at boot
Quoted 6 lines · H. Peter Anvin
I wouldn't really call it a "side effect". Perhaps the right thing here is to say something like "we want to start out with %eflags unambiguously clear".
(Note also we have had to CLD earlier because we have already copied the command line.)
Ok, let's make it even more verbose so that people know in the future:
"... we want to start out with EFLAGS unambiguously clear. That means DF in particular (even though we have cleared it earlier after copying the command line) because GCC expects it."
How does that sound?
Also, I was wondering about the whole reasoning behind that: do you know why DF=0 is a GCC requirement? I mean, nothing hurts GCC from issuing a CLD each time?
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--H. Peter Anvin
reviewerRe: [PATCH 2/5 -v2.1] x86: Detect CPUID support early at boot
Sure it does... it takes time.
Quoted 23 lines · Borislav Petkov
Quoted 6 lines · H. Peter Anvin
I wouldn't really call it a "side effect". Perhaps the right thing here is to say something like "we want to start out with %eflags unambiguously clear".
(Note also we have had to CLD earlier because we have already copied the command line.)
Ok, let's make it even more verbose so that people know in the future:
"... we want to start out with EFLAGS unambiguously clear. That means DF
in particular (even though we have cleared it earlier after copying the command line) because GCC expects it."How does that sound?
Also, I was wondering about the whole reasoning behind that: do you know
why DF=0 is a GCC requirement? I mean, nothing hurts GCC from issuing a CLD each time?Thanks.
Signature · 1 line
Sent from my mobile phone. Please excuse brevity and lack of formatting.