blob: c85e3ac99bbabc597d772cb85b4efd4d4b3bc291 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/boot/head.S
3 *
4 * Copyright (C) 1991, 1992, 1993 Linus Torvalds
5 */
6
7/*
8 * head.S contains the 32-bit startup code.
9 *
10 * NOTE!!! Startup happens at absolute address 0x00001000, which is also where
11 * the page directory will exist. The startup code will be overwritten by
12 * the page directory. [According to comments etc elsewhere on a compressed
13 * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC]
14 *
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070015 * Page 0 is deliberately kept safe, since System Management Mode code in
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * laptops may need to access the BIOS data stored there. This is also
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070017 * useful for future device drivers that either access the BIOS via VM86
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * mode.
19 */
20
21/*
22 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
23 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070024 .text
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Tim Abbott1dc818c2009-09-16 16:44:27 -040026#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/linkage.h>
28#include <asm/segment.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080029#include <asm/page_types.h>
Vivek Goyale69f2022006-12-07 02:14:04 +010030#include <asm/boot.h>
Rusty Russella24e7852007-10-21 16:41:35 -070031#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Tim Abbott1dc818c2009-09-16 16:44:27 -040033 __HEAD
Cyrill Gorcunovcb425af2009-02-14 00:50:23 +030034ENTRY(startup_32)
Matt Fleming291f3632011-12-12 21:27:52 +000035#ifdef CONFIG_EFI_STUB
Matt Flemingb1994302012-04-15 16:06:04 +010036 jmp preferred_addr
37
38 .balign 0x10
Matt Fleming291f3632011-12-12 21:27:52 +000039 /*
40 * We don't need the return address, so set up the stack so
41 * efi_main() can find its arugments.
42 */
43 add $0x4, %esp
44
45 call efi_main
46 cmpl $0, %eax
Matt Fleming291f3632011-12-12 21:27:52 +000047 movl %eax, %esi
Matt Flemingb1994302012-04-15 16:06:04 +010048 jne 2f
Matt Fleming291f3632011-12-12 21:27:52 +0000491:
Matt Flemingb1994302012-04-15 16:06:04 +010050 /* EFI init failed, so hang. */
51 hlt
52 jmp 1b
532:
54 call 3f
553:
Matt Fleming291f3632011-12-12 21:27:52 +000056 popl %eax
Matt Flemingb1994302012-04-15 16:06:04 +010057 subl $3b, %eax
Matt Fleming291f3632011-12-12 21:27:52 +000058 subl BP_pref_address(%esi), %eax
59 add BP_code32_start(%esi), %eax
60 leal preferred_addr(%eax), %eax
61 jmp *%eax
62
63preferred_addr:
64#endif
Eric W. Biedermanbd531472007-10-26 11:29:04 -060065 cld
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070066 /*
67 * Test KEEP_SEGMENTS flag to see if the bootloader is asking
68 * us to not reload segments
69 */
70 testb $(1<<6), BP_loadflags(%esi)
71 jnz 1f
Rusty Russella24e7852007-10-21 16:41:35 -070072
Eric W. Biedermanbd531472007-10-26 11:29:04 -060073 cli
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070074 movl $__BOOT_DS, %eax
75 movl %eax, %ds
76 movl %eax, %es
77 movl %eax, %fs
78 movl %eax, %gs
79 movl %eax, %ss
Eric W. Biedermanbd531472007-10-26 11:29:04 -0600801:
Rusty Russella24e7852007-10-21 16:41:35 -070081
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070082/*
83 * Calculate the delta between where we were compiled to run
Eric W. Biederman968de4f2006-12-07 02:14:04 +010084 * at and where we were actually loaded at. This can only be done
85 * with a short local call on x86. Nothing else will tell us what
86 * address we are running at. The reserved chunk of the real-mode
H. Peter Anvin85414b62007-07-11 12:18:33 -070087 * data at 0x1e4 (defined as a scratch field) are used as the stack
88 * for this calculation. Only 4 bytes are needed.
Eric W. Biederman968de4f2006-12-07 02:14:04 +010089 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070090 leal (BP_scratch+4)(%esi), %esp
91 call 1f
921: popl %ebp
93 subl $1b, %ebp
Eric W. Biederman968de4f2006-12-07 02:14:04 +010094
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070095/*
96 * %ebp contains the address we are loaded at by the boot loader and %ebx
Vivek Goyale69f2022006-12-07 02:14:04 +010097 * contains the address where we should move the kernel image temporarily
98 * for safe in-place decompression.
Eric W. Biederman968de4f2006-12-07 02:14:04 +010099 */
Vivek Goyale69f2022006-12-07 02:14:04 +0100100
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100101#ifdef CONFIG_RELOCATABLE
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700102 movl %ebp, %ebx
H. Peter Anvin37ba7ab2009-05-11 15:56:08 -0700103 movl BP_kernel_alignment(%esi), %eax
104 decl %eax
105 addl %eax, %ebx
106 notl %eax
107 andl %eax, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100108#else
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700109 movl $LOAD_PHYSICAL_ADDR, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100110#endif
111
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700112 /* Target address to relocate to for decompression */
113 addl $z_extract_offset, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100114
H. Peter Anvin0a137732009-05-08 16:27:41 -0700115 /* Set up the stack */
116 leal boot_stack_end(%ebx), %esp
117
H. Peter Anvin97541912009-05-06 17:56:51 -0700118 /* Zero EFLAGS */
119 pushl $0
120 popfl
121
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700122/*
123 * Copy the compressed kernel to the end of our buffer
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100124 * where decompression in place becomes safe.
125 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700126 pushl %esi
H. Peter Anvin36d37932009-05-08 16:45:15 -0700127 leal (_bss-4)(%ebp), %esi
128 leal (_bss-4)(%ebx), %edi
H. Peter Anvin5b11f1c2009-05-08 16:20:34 -0700129 movl $(_bss - startup_32), %ecx
H. Peter Anvin36d37932009-05-08 16:45:15 -0700130 shrl $2, %ecx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100131 std
H. Peter Anvin36d37932009-05-08 16:45:15 -0700132 rep movsl
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100133 cld
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700134 popl %esi
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100135
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700136/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100137 * Jump to the relocated address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700139 leal relocated(%ebx), %eax
140 jmp *%eax
Cyrill Gorcunovcb425af2009-02-14 00:50:23 +0300141ENDPROC(startup_32)
142
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700143 .text
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100144relocated:
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
H. Peter Anvin0a137732009-05-08 16:27:41 -0700147 * Clear BSS (stack is currently empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700149 xorl %eax, %eax
H. Peter Anvin5b11f1c2009-05-08 16:20:34 -0700150 leal _bss(%ebx), %edi
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700151 leal _ebss(%ebx), %ecx
152 subl %edi, %ecx
H. Peter Anvin36d37932009-05-08 16:45:15 -0700153 shrl $2, %ecx
154 rep stosl
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100155
156/*
H. Peter Anvin22a57f52010-08-02 15:34:44 -0700157 * Adjust our own GOT
158 */
159 leal _got(%ebx), %edx
160 leal _egot(%ebx), %ecx
1611:
162 cmpl %ecx, %edx
163 jae 2f
164 addl %ebx, (%edx)
165 addl $4, %edx
166 jmp 1b
1672:
168
169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Do the decompression, and jump to the new kernel..
171 */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700172 leal z_extract_offset_negative(%ebx), %ebp
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700173 /* push arguments for decompress_kernel: */
174 pushl %ebp /* output address */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700175 pushl $z_input_len /* input_len */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700176 leal input_data(%ebx), %eax
177 pushl %eax /* input_data */
178 leal boot_heap(%ebx), %eax
179 pushl %eax /* heap area */
180 pushl %esi /* real mode pointer */
181 call decompress_kernel
182 addl $20, %esp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100183
184#if CONFIG_RELOCATABLE
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700185/*
186 * Find the address of the relocations.
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100187 */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700188 leal z_output_len(%ebp), %edi
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100189
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700190/*
191 * Calculate the delta between where vmlinux was compiled to run
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100192 * and where it was actually loaded.
193 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700194 movl %ebp, %ebx
195 subl $LOAD_PHYSICAL_ADDR, %ebx
196 jz 2f /* Nothing to be done if loaded at compiled addr. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100198 * Process relocations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
H. Peter Anvin5f64ec62009-05-08 15:45:17 -07002011: subl $4, %edi
202 movl (%edi), %ecx
203 testl %ecx, %ecx
204 jz 2f
205 addl %ebx, -__PAGE_OFFSET(%ebx, %ecx)
206 jmp 1b
Eric W. Biederman968de4f2006-12-07 02:14:04 +01002072:
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100211 * Jump to the decompressed kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700213 xorl %ebx, %ebx
214 jmp *%ebp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100215
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700216/*
217 * Stack and heap for uncompression
218 */
219 .bss
220 .balign 4
Alexander van Heukelum7c539762008-04-08 12:54:30 +0200221boot_heap:
222 .fill BOOT_HEAP_SIZE, 1, 0
223boot_stack:
224 .fill BOOT_STACK_SIZE, 1, 0
225boot_stack_end: