2005-04-16 15:20:36 -07:00
/ *
* linux/ b o o t / h e a d . S
*
* Copyright ( C ) 1 9 9 1 , 1 9 9 2 , 1 9 9 3 L i n u s T o r v a l d s
* /
/ *
* head. S c o n t a i n s t h e 3 2 - b i t s t a r t u p c o d e .
*
* NOTE! ! ! S t a r t u p h a p p e n s a t a b s o l u t e a d d r e s s 0 x00 0 0 1 0 0 0 , w h i c h i s a l s o w h e r e
* the p a g e d i r e c t o r y w i l l e x i s t . T h e s t a r t u p c o d e w i l l b e o v e r w r i t t e n b y
* the p a g e d i r e c t o r y . [ A c c o r d i n g t o c o m m e n t s e t c e l s e w h e r e o n a c o m p r e s s e d
* kernel i t w i l l e n d u p a t 0 x10 0 0 + 1 M b I h o p e s o a s I a s s u m e t h i s . - A C ]
*
* Page 0 i s d e l i b e r a t e l y k e p t s a f e , s i n c e S y s t e m M a n a g e m e n t M o d e c o d e i n
* laptops m a y n e e d t o a c c e s s t h e B I O S d a t a s t o r e d t h e r e . T h i s i s a l s o
* useful f o r f u t u r e d e v i c e d r i v e r s t h a t e i t h e r a c c e s s t h e B I O S v i a V M 8 6
* mode.
* /
/ *
2005-06-25 14:58:59 -07:00
* High l o a d e d s t u f f b y H a n s L e r m e n & W e r n e r A l m e s b e r g e r , F e b . 1 9 9 6
2005-04-16 15:20:36 -07:00
* /
2009-05-08 15:59:13 -07:00
.code32
.text
2005-04-16 15:20:36 -07:00
2009-09-16 16:44:27 -04:00
# include < l i n u x / i n i t . h >
2005-04-16 15:20:36 -07:00
# include < l i n u x / l i n k a g e . h >
# include < a s m / s e g m e n t . h >
2009-02-13 11:14:01 -08:00
# include < a s m / p g t a b l e _ t y p e s . h >
# include < a s m / p a g e _ t y p e s . h >
2008-04-08 12:54:30 +02:00
# include < a s m / b o o t . h >
2007-05-02 19:27:07 +02:00
# include < a s m / m s r . h >
2008-05-12 15:43:39 +02:00
# include < a s m / p r o c e s s o r - f l a g s . h >
2007-10-26 11:29:04 -06:00
# include < a s m / a s m - o f f s e t s . h >
2005-04-16 15:20:36 -07:00
2009-09-16 16:44:27 -04:00
_ _ HEAD
2005-04-16 15:20:36 -07:00
.code32
2009-02-14 00:50:22 +03:00
ENTRY( s t a r t u p _ 3 2 )
2005-04-16 15:20:36 -07:00
cld
2009-05-08 15:59:13 -07:00
/ *
* Test K E E P _ S E G M E N T S f l a g t o s e e i f t h e b o o t l o a d e r i s a s k i n g
* us t o n o t r e l o a d s e g m e n t s
* /
2007-10-26 11:29:04 -06:00
testb $ ( 1 < < 6 ) , B P _ l o a d f l a g s ( % e s i )
jnz 1 f
2005-04-16 15:20:36 -07:00
cli
2007-05-02 19:27:07 +02:00
movl $ ( _ _ K E R N E L _ D S ) , % e a x
movl % e a x , % d s
movl % e a x , % e s
movl % e a x , % s s
2007-10-26 11:29:04 -06:00
1 :
2007-05-02 19:27:07 +02:00
2009-05-08 15:59:13 -07:00
/ *
* Calculate t h e d e l t a b e t w e e n w h e r e w e w e r e c o m p i l e d t o r u n
2007-05-02 19:27:07 +02:00
* at a n d w h e r e w e w e r e a c t u a l l y l o a d e d a t . T h i s c a n o n l y b e d o n e
* with a s h o r t l o c a l c a l l o n x86 . N o t h i n g e l s e w i l l t e l l u s w h a t
* address w e a r e r u n n i n g a t . T h e r e s e r v e d c h u n k o f t h e r e a l - m o d e
2007-07-11 12:18:33 -07:00
* data a t 0 x1 e 4 ( d e f i n e d a s a s c r a t c h f i e l d ) a r e u s e d a s t h e s t a c k
* for t h i s c a l c u l a t i o n . O n l y 4 b y t e s a r e n e e d e d .
2007-05-02 19:27:07 +02:00
* /
2009-05-05 23:24:50 -07:00
leal ( B P _ s c r a t c h + 4 ) ( % e s i ) , % e s p
2007-05-02 19:27:07 +02:00
call 1 f
1 : popl % e b p
subl $ 1 b , % e b p
2007-05-02 19:27:08 +02:00
/* setup a stack and make sure cpu supports long mode. */
2008-04-08 12:54:30 +02:00
movl $ b o o t _ s t a c k _ e n d , % e a x
2007-05-02 19:27:08 +02:00
addl % e b p , % e a x
movl % e a x , % e s p
call v e r i f y _ c p u
testl % e a x , % e a x
jnz n o _ l o n g m o d e
2009-05-08 15:59:13 -07:00
/ *
* Compute t h e d e l t a b e t w e e n w h e r e w e w e r e c o m p i l e d t o r u n a t
2007-05-02 19:27:07 +02:00
* and w h e r e t h e c o d e w i l l a c t u a l l y r u n a t .
2009-05-08 15:59:13 -07:00
*
* % ebp c o n t a i n s t h e a d d r e s s w e a r e l o a d e d a t b y t h e b o o t l o a d e r a n d % e b x
2007-05-02 19:27:07 +02:00
* contains t h e a d d r e s s w h e r e w e s h o u l d m o v e t h e k e r n e l i m a g e t e m p o r a r i l y
* for s a f e i n - p l a c e d e c o m p r e s s i o n .
* /
# ifdef C O N F I G _ R E L O C A T A B L E
movl % e b p , % e b x
2009-05-11 15:56:08 -07:00
movl B P _ k e r n e l _ a l i g n m e n t ( % e s i ) , % e a x
decl % e a x
addl % e a x , % e b x
notl % e a x
andl % e a x , % e b x
2007-05-02 19:27:07 +02:00
# else
2009-05-11 14:41:55 -07:00
movl $ L O A D _ P H Y S I C A L _ A D D R , % e b x
2007-05-02 19:27:07 +02:00
# endif
2009-05-08 17:42:16 -07:00
/* Target address to relocate to for decompression */
addl $ z _ e x t r a c t _ o f f s e t , % e b x
2005-04-16 15:20:36 -07:00
/ *
2007-05-02 19:27:07 +02:00
* Prepare f o r e n t e r i n g 6 4 b i t m o d e
2005-04-16 15:20:36 -07:00
* /
2007-05-02 19:27:07 +02:00
/* Load new GDT with the 64bit segments using 32bit descriptor */
leal g d t ( % e b p ) , % e a x
movl % e a x , g d t + 2 ( % e b p )
lgdt g d t ( % e b p )
/* Enable PAE mode */
2009-10-24 03:37:23 +04:00
movl $ ( X 8 6 _ C R 4 _ P A E ) , % e a x
2007-05-02 19:27:07 +02:00
movl % e a x , % c r4
/ *
* Build e a r l y 4 G b o o t p a g e t a b l e
* /
2009-05-08 15:59:13 -07:00
/* Initialize Page tables to 0 */
2007-05-02 19:27:07 +02:00
leal p g t a b l e ( % e b x ) , % e d i
xorl % e a x , % e a x
movl $ ( ( 4 0 9 6 * 6 ) / 4 ) , % e c x
rep s t o s l
/* Build Level 4 */
leal p g t a b l e + 0 ( % e b x ) , % e d i
leal 0 x10 0 7 ( % e d i ) , % e a x
movl % e a x , 0 ( % e d i )
/* Build Level 3 */
leal p g t a b l e + 0 x10 0 0 ( % e b x ) , % e d i
leal 0 x10 0 7 ( % e d i ) , % e a x
movl $ 4 , % e c x
1 : movl % e a x , 0 x00 ( % e d i )
addl $ 0 x00 0 0 1 0 0 0 , % e a x
addl $ 8 , % e d i
decl % e c x
jnz 1 b
/* Build Level 2 */
leal p g t a b l e + 0 x20 0 0 ( % e b x ) , % e d i
movl $ 0 x00 0 0 0 1 8 3 , % e a x
movl $ 2 0 4 8 , % e c x
1 : movl % e a x , 0 ( % e d i )
addl $ 0 x00 2 0 0 0 0 0 , % e a x
addl $ 8 , % e d i
decl % e c x
jnz 1 b
/* Enable the boot page tables */
leal p g t a b l e ( % e b x ) , % e a x
movl % e a x , % c r3
/* Enable Long mode in EFER (Extended Feature Enable Register) */
movl $ M S R _ E F E R , % e c x
rdmsr
btsl $ _ E F E R _ L M E , % e a x
wrmsr
2009-05-08 15:59:13 -07:00
/ *
* Setup f o r t h e j u m p t o 6 4 b i t m o d e
2007-05-02 19:27:07 +02:00
*
* When t h e j u m p i s p e r f o r m e n d w e w i l l b e i n l o n g m o d e b u t
* in 3 2 b i t c o m p a t i b i l i t y m o d e w i t h E F E R . L M E = 1 , C S . L = 0 , C S . D = 1
* ( and i n t u r n E F E R . L M A = 1 ) . T o j u m p i n t o 6 4 b i t m o d e w e u s e
* the n e w g d t / i d t t h a t h a s _ _ K E R N E L _ C S w i t h C S . L = 1 .
* We p l a c e a l l o f t h e v a l u e s o n o u r m i n i s t a c k s o l r e t c a n
* used t o p e r f o r m t h a t f a r j u m p .
* /
pushl $ _ _ K E R N E L _ C S
leal s t a r t u p _ 6 4 ( % e b p ) , % e a x
pushl % e a x
/* Enter paged protected Mode, activating Long Mode */
2008-05-12 15:43:39 +02:00
movl $ ( X 8 6 _ C R 0 _ P G | X 8 6 _ C R 0 _ P E ) , % e a x / * E n a b l e P a g i n g a n d P r o t e c t e d m o d e * /
2007-05-02 19:27:07 +02:00
movl % e a x , % c r0
/* Jump from 32bit compatibility mode into 64bit mode. */
lret
2009-02-14 00:50:22 +03:00
ENDPROC( s t a r t u p _ 3 2 )
2007-05-02 19:27:07 +02:00
2007-05-02 19:27:08 +02:00
no_longmode :
/* This isn't an x86-64 CPU so hang */
1 :
hlt
jmp 1 b
2010-11-10 10:35:51 -08:00
# include " . . / . . / k e r n e l / v e r i f y _ c p u . S "
2007-05-02 19:27:08 +02:00
2009-05-08 15:59:13 -07:00
/ *
* Be c a r e f u l h e r e s t a r t u p _ 6 4 n e e d s t o b e a t a p r e d i c t a b l e
2007-05-02 19:27:07 +02:00
* address s o I c a n e x p o r t i t i n a n E L F h e a d e r . B o o t l o a d e r s
* should l o o k a t t h e E L F h e a d e r t o f i n d t h i s a d d r e s s , a s
* it m a y c h a n g e i n t h e f u t u r e .
* /
.code64
2007-05-02 19:27:08 +02:00
.org 0x200
2007-05-02 19:27:07 +02:00
ENTRY( s t a r t u p _ 6 4 )
2009-05-08 15:59:13 -07:00
/ *
* We c o m e h e r e e i t h e r f r o m s t a r t u p _ 3 2 o r d i r e c t l y f r o m a
2007-05-02 19:27:07 +02:00
* 6 4 bit b o o t l o a d e r . I f w e c o m e h e r e f r o m a b o o t l o a d e r w e d e p e n d o n
* an i d e n t i t y m a p p e d p a g e t a b l e b e i n g p r o v i e d t h a t m a p s o u r
* entire t e x t + d a t a + b s s a n d h o p e f u l l y a l l o f m e m o r y .
* /
x86, efi: EFI boot stub support
There is currently a large divide between kernel development and the
development of EFI boot loaders. The idea behind this patch is to give
the kernel developers full control over the EFI boot process. As
H. Peter Anvin put it,
"The 'kernel carries its own stub' approach been very successful in
dealing with BIOS, and would make a lot of sense to me for EFI as
well."
This patch introduces an EFI boot stub that allows an x86 bzImage to
be loaded and executed by EFI firmware. The bzImage appears to the
firmware as an EFI application. Luckily there are enough free bits
within the bzImage header so that it can masquerade as an EFI
application, thereby coercing the EFI firmware into loading it and
jumping to its entry point. The beauty of this masquerading approach
is that both BIOS and EFI boot loaders can still load and run the same
bzImage, thereby allowing a single kernel image to work in any boot
environment.
The EFI boot stub supports multiple initrds, but they must exist on
the same partition as the bzImage. Command-line arguments for the
kernel can be appended after the bzImage name when run from the EFI
shell, e.g.
Shell> bzImage console=ttyS0 root=/dev/sdb initrd=initrd.img
v7:
- Fix checkpatch warnings.
v6:
- Try to allocate initrd memory just below hdr->inird_addr_max.
v5:
- load_options_size is UTF-16, which needs dividing by 2 to convert
to the corresponding ASCII size.
v4:
- Don't read more than image->load_options_size
v3:
- Fix following warnings when compiling CONFIG_EFI_STUB=n
arch/x86/boot/tools/build.c: In function ‘main’:
arch/x86/boot/tools/build.c:138:24: warning: unused variable ‘pe_header’
arch/x86/boot/tools/build.c:138:15: warning: unused variable ‘file_sz’
- As reported by Matthew Garrett, some Apple machines have GOPs that
don't have hardware attached. We need to weed these out by
searching for ones that handle the PCIIO protocol.
- Don't allocate memory if no initrds are on cmdline
- Don't trust image->load_options_size
Maarten Lankhorst noted:
- Don't strip first argument when booted from efibootmgr
- Don't allocate too much memory for cmdline
- Don't update cmdline_size, the kernel considers it read-only
- Don't accept '\n' for initrd names
v2:
- File alignment was too large, was 8192 should be 512. Reported by
Maarten Lankhorst on LKML.
- Added UGA support for graphics
- Use VIDEO_TYPE_EFI instead of hard-coded number.
- Move linelength assignment until after we've assigned depth
- Dynamically fill out AddressOfEntryPoint in tools/build.c
- Don't use magic number for GDT/TSS stuff. Requested by Andi Kleen
- The bzImage may need to be relocated as it may have been loaded at
a high address address by the firmware. This was required to get my
macbook booting because the firmware loaded it at 0x7cxxxxxx, which
triggers this error in decompress_kernel(),
if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
error("Destination address too large");
Cc: Mike Waychison <mikew@google.com>
Cc: Matthew Garrett <mjg@redhat.com>
Tested-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Link: http://lkml.kernel.org/r/1321383097.2657.9.camel@mfleming-mobl1.ger.corp.intel.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2011-12-12 21:27:52 +00:00
# ifdef C O N F I G _ E F I _ S T U B
2012-04-15 16:06:04 +01:00
/ *
* The e n t r y p o i n t f o r t h e P E / C O F F e x e c u t a b l e i s 0 x21 0 , s o o n l y
* legacy b o o t l o a d e r s w i l l e x e c u t e t h i s j m p .
* /
jmp p r e f e r r e d _ a d d r
.org 0x210
x86, efi: EFI boot stub support
There is currently a large divide between kernel development and the
development of EFI boot loaders. The idea behind this patch is to give
the kernel developers full control over the EFI boot process. As
H. Peter Anvin put it,
"The 'kernel carries its own stub' approach been very successful in
dealing with BIOS, and would make a lot of sense to me for EFI as
well."
This patch introduces an EFI boot stub that allows an x86 bzImage to
be loaded and executed by EFI firmware. The bzImage appears to the
firmware as an EFI application. Luckily there are enough free bits
within the bzImage header so that it can masquerade as an EFI
application, thereby coercing the EFI firmware into loading it and
jumping to its entry point. The beauty of this masquerading approach
is that both BIOS and EFI boot loaders can still load and run the same
bzImage, thereby allowing a single kernel image to work in any boot
environment.
The EFI boot stub supports multiple initrds, but they must exist on
the same partition as the bzImage. Command-line arguments for the
kernel can be appended after the bzImage name when run from the EFI
shell, e.g.
Shell> bzImage console=ttyS0 root=/dev/sdb initrd=initrd.img
v7:
- Fix checkpatch warnings.
v6:
- Try to allocate initrd memory just below hdr->inird_addr_max.
v5:
- load_options_size is UTF-16, which needs dividing by 2 to convert
to the corresponding ASCII size.
v4:
- Don't read more than image->load_options_size
v3:
- Fix following warnings when compiling CONFIG_EFI_STUB=n
arch/x86/boot/tools/build.c: In function ‘main’:
arch/x86/boot/tools/build.c:138:24: warning: unused variable ‘pe_header’
arch/x86/boot/tools/build.c:138:15: warning: unused variable ‘file_sz’
- As reported by Matthew Garrett, some Apple machines have GOPs that
don't have hardware attached. We need to weed these out by
searching for ones that handle the PCIIO protocol.
- Don't allocate memory if no initrds are on cmdline
- Don't trust image->load_options_size
Maarten Lankhorst noted:
- Don't strip first argument when booted from efibootmgr
- Don't allocate too much memory for cmdline
- Don't update cmdline_size, the kernel considers it read-only
- Don't accept '\n' for initrd names
v2:
- File alignment was too large, was 8192 should be 512. Reported by
Maarten Lankhorst on LKML.
- Added UGA support for graphics
- Use VIDEO_TYPE_EFI instead of hard-coded number.
- Move linelength assignment until after we've assigned depth
- Dynamically fill out AddressOfEntryPoint in tools/build.c
- Don't use magic number for GDT/TSS stuff. Requested by Andi Kleen
- The bzImage may need to be relocated as it may have been loaded at
a high address address by the firmware. This was required to get my
macbook booting because the firmware loaded it at 0x7cxxxxxx, which
triggers this error in decompress_kernel(),
if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
error("Destination address too large");
Cc: Mike Waychison <mikew@google.com>
Cc: Matthew Garrett <mjg@redhat.com>
Tested-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Link: http://lkml.kernel.org/r/1321383097.2657.9.camel@mfleming-mobl1.ger.corp.intel.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2011-12-12 21:27:52 +00:00
mov % r c x , % r d i
mov % r d x , % r s i
call e f i _ m a i n
movq % r a x ,% r s i
2012-04-15 16:06:04 +01:00
cmpq $ 0 ,% r a x
jne 2 f
x86, efi: EFI boot stub support
There is currently a large divide between kernel development and the
development of EFI boot loaders. The idea behind this patch is to give
the kernel developers full control over the EFI boot process. As
H. Peter Anvin put it,
"The 'kernel carries its own stub' approach been very successful in
dealing with BIOS, and would make a lot of sense to me for EFI as
well."
This patch introduces an EFI boot stub that allows an x86 bzImage to
be loaded and executed by EFI firmware. The bzImage appears to the
firmware as an EFI application. Luckily there are enough free bits
within the bzImage header so that it can masquerade as an EFI
application, thereby coercing the EFI firmware into loading it and
jumping to its entry point. The beauty of this masquerading approach
is that both BIOS and EFI boot loaders can still load and run the same
bzImage, thereby allowing a single kernel image to work in any boot
environment.
The EFI boot stub supports multiple initrds, but they must exist on
the same partition as the bzImage. Command-line arguments for the
kernel can be appended after the bzImage name when run from the EFI
shell, e.g.
Shell> bzImage console=ttyS0 root=/dev/sdb initrd=initrd.img
v7:
- Fix checkpatch warnings.
v6:
- Try to allocate initrd memory just below hdr->inird_addr_max.
v5:
- load_options_size is UTF-16, which needs dividing by 2 to convert
to the corresponding ASCII size.
v4:
- Don't read more than image->load_options_size
v3:
- Fix following warnings when compiling CONFIG_EFI_STUB=n
arch/x86/boot/tools/build.c: In function ‘main’:
arch/x86/boot/tools/build.c:138:24: warning: unused variable ‘pe_header’
arch/x86/boot/tools/build.c:138:15: warning: unused variable ‘file_sz’
- As reported by Matthew Garrett, some Apple machines have GOPs that
don't have hardware attached. We need to weed these out by
searching for ones that handle the PCIIO protocol.
- Don't allocate memory if no initrds are on cmdline
- Don't trust image->load_options_size
Maarten Lankhorst noted:
- Don't strip first argument when booted from efibootmgr
- Don't allocate too much memory for cmdline
- Don't update cmdline_size, the kernel considers it read-only
- Don't accept '\n' for initrd names
v2:
- File alignment was too large, was 8192 should be 512. Reported by
Maarten Lankhorst on LKML.
- Added UGA support for graphics
- Use VIDEO_TYPE_EFI instead of hard-coded number.
- Move linelength assignment until after we've assigned depth
- Dynamically fill out AddressOfEntryPoint in tools/build.c
- Don't use magic number for GDT/TSS stuff. Requested by Andi Kleen
- The bzImage may need to be relocated as it may have been loaded at
a high address address by the firmware. This was required to get my
macbook booting because the firmware loaded it at 0x7cxxxxxx, which
triggers this error in decompress_kernel(),
if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
error("Destination address too large");
Cc: Mike Waychison <mikew@google.com>
Cc: Matthew Garrett <mjg@redhat.com>
Tested-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Link: http://lkml.kernel.org/r/1321383097.2657.9.camel@mfleming-mobl1.ger.corp.intel.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2011-12-12 21:27:52 +00:00
1 :
2012-04-15 16:06:04 +01:00
/* EFI init failed, so hang. */
hlt
jmp 1 b
2 :
call 3 f
3 :
x86, efi: EFI boot stub support
There is currently a large divide between kernel development and the
development of EFI boot loaders. The idea behind this patch is to give
the kernel developers full control over the EFI boot process. As
H. Peter Anvin put it,
"The 'kernel carries its own stub' approach been very successful in
dealing with BIOS, and would make a lot of sense to me for EFI as
well."
This patch introduces an EFI boot stub that allows an x86 bzImage to
be loaded and executed by EFI firmware. The bzImage appears to the
firmware as an EFI application. Luckily there are enough free bits
within the bzImage header so that it can masquerade as an EFI
application, thereby coercing the EFI firmware into loading it and
jumping to its entry point. The beauty of this masquerading approach
is that both BIOS and EFI boot loaders can still load and run the same
bzImage, thereby allowing a single kernel image to work in any boot
environment.
The EFI boot stub supports multiple initrds, but they must exist on
the same partition as the bzImage. Command-line arguments for the
kernel can be appended after the bzImage name when run from the EFI
shell, e.g.
Shell> bzImage console=ttyS0 root=/dev/sdb initrd=initrd.img
v7:
- Fix checkpatch warnings.
v6:
- Try to allocate initrd memory just below hdr->inird_addr_max.
v5:
- load_options_size is UTF-16, which needs dividing by 2 to convert
to the corresponding ASCII size.
v4:
- Don't read more than image->load_options_size
v3:
- Fix following warnings when compiling CONFIG_EFI_STUB=n
arch/x86/boot/tools/build.c: In function ‘main’:
arch/x86/boot/tools/build.c:138:24: warning: unused variable ‘pe_header’
arch/x86/boot/tools/build.c:138:15: warning: unused variable ‘file_sz’
- As reported by Matthew Garrett, some Apple machines have GOPs that
don't have hardware attached. We need to weed these out by
searching for ones that handle the PCIIO protocol.
- Don't allocate memory if no initrds are on cmdline
- Don't trust image->load_options_size
Maarten Lankhorst noted:
- Don't strip first argument when booted from efibootmgr
- Don't allocate too much memory for cmdline
- Don't update cmdline_size, the kernel considers it read-only
- Don't accept '\n' for initrd names
v2:
- File alignment was too large, was 8192 should be 512. Reported by
Maarten Lankhorst on LKML.
- Added UGA support for graphics
- Use VIDEO_TYPE_EFI instead of hard-coded number.
- Move linelength assignment until after we've assigned depth
- Dynamically fill out AddressOfEntryPoint in tools/build.c
- Don't use magic number for GDT/TSS stuff. Requested by Andi Kleen
- The bzImage may need to be relocated as it may have been loaded at
a high address address by the firmware. This was required to get my
macbook booting because the firmware loaded it at 0x7cxxxxxx, which
triggers this error in decompress_kernel(),
if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
error("Destination address too large");
Cc: Mike Waychison <mikew@google.com>
Cc: Matthew Garrett <mjg@redhat.com>
Tested-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Link: http://lkml.kernel.org/r/1321383097.2657.9.camel@mfleming-mobl1.ger.corp.intel.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2011-12-12 21:27:52 +00:00
popq % r a x
2012-04-15 16:06:04 +01:00
subq $ 3 b , % r a x
x86, efi: EFI boot stub support
There is currently a large divide between kernel development and the
development of EFI boot loaders. The idea behind this patch is to give
the kernel developers full control over the EFI boot process. As
H. Peter Anvin put it,
"The 'kernel carries its own stub' approach been very successful in
dealing with BIOS, and would make a lot of sense to me for EFI as
well."
This patch introduces an EFI boot stub that allows an x86 bzImage to
be loaded and executed by EFI firmware. The bzImage appears to the
firmware as an EFI application. Luckily there are enough free bits
within the bzImage header so that it can masquerade as an EFI
application, thereby coercing the EFI firmware into loading it and
jumping to its entry point. The beauty of this masquerading approach
is that both BIOS and EFI boot loaders can still load and run the same
bzImage, thereby allowing a single kernel image to work in any boot
environment.
The EFI boot stub supports multiple initrds, but they must exist on
the same partition as the bzImage. Command-line arguments for the
kernel can be appended after the bzImage name when run from the EFI
shell, e.g.
Shell> bzImage console=ttyS0 root=/dev/sdb initrd=initrd.img
v7:
- Fix checkpatch warnings.
v6:
- Try to allocate initrd memory just below hdr->inird_addr_max.
v5:
- load_options_size is UTF-16, which needs dividing by 2 to convert
to the corresponding ASCII size.
v4:
- Don't read more than image->load_options_size
v3:
- Fix following warnings when compiling CONFIG_EFI_STUB=n
arch/x86/boot/tools/build.c: In function ‘main’:
arch/x86/boot/tools/build.c:138:24: warning: unused variable ‘pe_header’
arch/x86/boot/tools/build.c:138:15: warning: unused variable ‘file_sz’
- As reported by Matthew Garrett, some Apple machines have GOPs that
don't have hardware attached. We need to weed these out by
searching for ones that handle the PCIIO protocol.
- Don't allocate memory if no initrds are on cmdline
- Don't trust image->load_options_size
Maarten Lankhorst noted:
- Don't strip first argument when booted from efibootmgr
- Don't allocate too much memory for cmdline
- Don't update cmdline_size, the kernel considers it read-only
- Don't accept '\n' for initrd names
v2:
- File alignment was too large, was 8192 should be 512. Reported by
Maarten Lankhorst on LKML.
- Added UGA support for graphics
- Use VIDEO_TYPE_EFI instead of hard-coded number.
- Move linelength assignment until after we've assigned depth
- Dynamically fill out AddressOfEntryPoint in tools/build.c
- Don't use magic number for GDT/TSS stuff. Requested by Andi Kleen
- The bzImage may need to be relocated as it may have been loaded at
a high address address by the firmware. This was required to get my
macbook booting because the firmware loaded it at 0x7cxxxxxx, which
triggers this error in decompress_kernel(),
if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
error("Destination address too large");
Cc: Mike Waychison <mikew@google.com>
Cc: Matthew Garrett <mjg@redhat.com>
Tested-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Link: http://lkml.kernel.org/r/1321383097.2657.9.camel@mfleming-mobl1.ger.corp.intel.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2011-12-12 21:27:52 +00:00
subq B P _ p r e f _ a d d r e s s ( % r s i ) , % r a x
add B P _ c o d e 3 2 _ s t a r t ( % e s i ) , % e a x
leaq p r e f e r r e d _ a d d r ( % r a x ) , % r a x
jmp * % r a x
preferred_addr :
# endif
2007-05-02 19:27:07 +02:00
/* Setup data segments. */
xorl % e a x , % e a x
movl % e a x , % d s
movl % e a x , % e s
movl % e a x , % s s
2007-08-10 22:31:05 +02:00
movl % e a x , % f s
movl % e a x , % g s
lldt % a x
movl $ 0 x20 , % e a x
ltr % a x
2007-05-02 19:27:07 +02:00
2009-05-08 15:59:13 -07:00
/ *
* Compute t h e d e c o m p r e s s e d k e r n e l s t a r t a d d r e s s . I t i s w h e r e
2007-05-02 19:27:07 +02:00
* we w e r e l o a d e d a t a l i g n e d t o a 2 M b o u n d a r y . % r b p c o n t a i n s t h e
* decompressed k e r n e l s t a r t a d d r e s s .
*
* If i t i s a r e l o c a t a b l e k e r n e l t h e n d e c o m p r e s s a n d r u n t h e k e r n e l
* from l o a d a d d r e s s a l i g n e d t o 2 M B a d d r , o t h e r w i s e d e c o m p r e s s a n d
2009-05-11 14:41:55 -07:00
* run t h e k e r n e l f r o m L O A D _ P H Y S I C A L _ A D D R
2009-05-08 17:42:16 -07:00
*
* We c a n n o t r e l y o n t h e c a l c u l a t i o n d o n e i n 3 2 - b i t m o d e , s i n c e w e
* may h a v e b e e n i n v o k e d v i a t h e 6 4 - b i t e n t r y p o i n t .
2007-05-02 19:27:07 +02:00
* /
/* Start with the delta to where the kernel will run at. */
# ifdef C O N F I G _ R E L O C A T A B L E
leaq s t a r t u p _ 3 2 ( % r i p ) / * - $ s t a r t u p _ 3 2 * / , % r b p
2009-05-11 15:56:08 -07:00
movl B P _ k e r n e l _ a l i g n m e n t ( % r s i ) , % e a x
decl % e a x
addq % r a x , % r b p
notq % r a x
andq % r a x , % r b p
2007-05-02 19:27:07 +02:00
# else
2009-05-11 14:41:55 -07:00
movq $ L O A D _ P H Y S I C A L _ A D D R , % r b p
2007-05-02 19:27:07 +02:00
# endif
2009-05-08 17:42:16 -07:00
/* Target address to relocate to for decompression */
leaq z _ e x t r a c t _ o f f s e t ( % r b p ) , % r b x
2007-05-02 19:27:07 +02:00
2009-05-08 16:27:41 -07:00
/* Set up the stack */
leaq b o o t _ s t a c k _ e n d ( % r b x ) , % r s p
/* Zero EFLAGS */
pushq $ 0
popfq
2009-05-08 15:59:13 -07:00
/ *
* Copy t h e c o m p r e s s e d k e r n e l t o t h e e n d o f o u r b u f f e r
2007-05-02 19:27:07 +02:00
* where d e c o m p r e s s i o n i n p l a c e b e c o m e s s a f e .
* /
2009-05-08 16:45:15 -07:00
pushq % r s i
leaq ( _ b s s - 8 ) ( % r i p ) , % r s i
leaq ( _ b s s - 8 ) ( % r b x ) , % r d i
2009-05-08 16:20:34 -07:00
movq $ _ b s s / * - $ s t a r t u p _ 3 2 * / , % r c x
2009-05-08 16:45:15 -07:00
shrq $ 3 , % r c x
std
rep m o v s q
cld
popq % r s i
2007-05-02 19:27:07 +02:00
/ *
* Jump t o t h e r e l o c a t e d a d d r e s s .
* /
leaq r e l o c a t e d ( % r b x ) , % r a x
jmp * % r a x
2009-05-08 15:59:13 -07:00
.text
2007-05-02 19:27:07 +02:00
relocated :
2005-04-16 15:20:36 -07:00
/ *
2009-05-08 16:27:41 -07:00
* Clear B S S ( s t a c k i s c u r r e n t l y e m p t y )
2005-04-16 15:20:36 -07:00
* /
2009-05-08 16:45:15 -07:00
xorl % e a x , % e a x
leaq _ b s s ( % r i p ) , % r d i
leaq _ e b s s ( % r i p ) , % r c x
2007-05-02 19:27:07 +02:00
subq % r d i , % r c x
2009-05-08 16:45:15 -07:00
shrq $ 3 , % r c x
rep s t o s q
2007-05-02 19:27:07 +02:00
2010-08-02 15:34:44 -07:00
/ *
* Adjust o u r o w n G O T
* /
leaq _ g o t ( % r i p ) , % r d x
leaq _ e g o t ( % r i p ) , % r c x
1 :
cmpq % r c x , % r d x
jae 2 f
addq % r b x , ( % r d x )
addq $ 8 , % r d x
jmp 1 b
2 :
2005-04-16 15:20:36 -07:00
/ *
* Do t h e d e c o m p r e s s i o n , a n d j u m p t o t h e n e w k e r n e l . .
* /
2009-05-08 17:42:16 -07:00
pushq % r s i / * S a v e t h e r e a l m o d e a r g u m e n t * /
movq % r s i , % r d i / * r e a l m o d e a d d r e s s * /
leaq b o o t _ h e a p ( % r i p ) , % r s i / * m a l l o c a r e a f o r u n c o m p r e s s i o n * /
leaq i n p u t _ d a t a ( % r i p ) , % r d x / * i n p u t _ d a t a * /
movl $ z _ i n p u t _ l e n , % e c x / * i n p u t _ l e n * /
movq % r b p , % r8 / * o u t p u t t a r g e t a d d r e s s * /
2007-05-02 19:27:07 +02:00
call d e c o m p r e s s _ k e r n e l
popq % r s i
2005-04-16 15:20:36 -07:00
/ *
2007-05-02 19:27:07 +02:00
* Jump t o t h e d e c o m p r e s s e d k e r n e l .
2005-04-16 15:20:36 -07:00
* /
2007-05-02 19:27:07 +02:00
jmp * % r b p
2005-04-16 15:20:36 -07:00
2007-05-02 19:27:07 +02:00
.data
gdt :
.word gdt_end - gdt
.long gdt
.word 0
.quad 0x0000000000000000 /* NULL descriptor */
.quad 0x00af9a000000ffff /* __KERNEL_CS */
.quad 0x00cf92000000ffff /* __KERNEL_DS */
2007-08-10 22:31:05 +02:00
.quad 0x0080890000000000 /* TS descriptor */
.quad 0x0000000000000000 /* TS continued */
2007-05-02 19:27:07 +02:00
gdt_end :
2008-04-08 12:54:30 +02:00
2009-05-08 15:59:13 -07:00
/ *
* Stack a n d h e a p f o r u n c o m p r e s s i o n
* /
.bss
.balign 4
2008-04-08 12:54:30 +02:00
boot_heap :
.fill BOOT_ H E A P _ S I Z E , 1 , 0
boot_stack :
.fill BOOT_ S T A C K _ S I Z E , 1 , 0
boot_stack_end :
2009-05-08 16:20:34 -07:00
/ *
* Space f o r p a g e t a b l e s ( n o t i n . b s s s o n o t z e r o e d )
* /
.section " .pgtable " , " a" ,@nobits
.balign 4096
pgtable :
.fill 6 * 4 0 9 6 , 1 , 0