blob: ccb2f4a5f7bda805a18f65b1ceb07fb16635156f [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
Matt Fleming9ca8f722012-07-19 10:23:48 +010045 call make_boot_params
46 cmpl $0, %eax
47 je 1f
48 movl 0x4(%esp), %esi
49 movl (%esp), %ecx
50 pushl %eax
51 pushl %esi
52 pushl %ecx
David Woodhousef7916202013-01-07 22:01:50 +000053 sub $0x4, %esp
Matt Fleming9ca8f722012-07-19 10:23:48 +010054
55 .org 0x30,0x90
David Woodhousef7916202013-01-07 22:01:50 +000056 add $0x4, %esp
Matt Fleming291f3632011-12-12 21:27:52 +000057 call efi_main
58 cmpl $0, %eax
Matt Fleming291f3632011-12-12 21:27:52 +000059 movl %eax, %esi
Matt Flemingb1994302012-04-15 16:06:04 +010060 jne 2f
Matt Fleming291f3632011-12-12 21:27:52 +0000611:
Matt Flemingb1994302012-04-15 16:06:04 +010062 /* EFI init failed, so hang. */
63 hlt
64 jmp 1b
652:
66 call 3f
673:
Matt Fleming291f3632011-12-12 21:27:52 +000068 popl %eax
Matt Flemingb1994302012-04-15 16:06:04 +010069 subl $3b, %eax
Matt Fleming291f3632011-12-12 21:27:52 +000070 subl BP_pref_address(%esi), %eax
71 add BP_code32_start(%esi), %eax
72 leal preferred_addr(%eax), %eax
73 jmp *%eax
74
75preferred_addr:
76#endif
Eric W. Biedermanbd531472007-10-26 11:29:04 -060077 cld
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070078 /*
79 * Test KEEP_SEGMENTS flag to see if the bootloader is asking
80 * us to not reload segments
81 */
82 testb $(1<<6), BP_loadflags(%esi)
83 jnz 1f
Rusty Russella24e7852007-10-21 16:41:35 -070084
Eric W. Biedermanbd531472007-10-26 11:29:04 -060085 cli
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070086 movl $__BOOT_DS, %eax
87 movl %eax, %ds
88 movl %eax, %es
89 movl %eax, %fs
90 movl %eax, %gs
91 movl %eax, %ss
Eric W. Biedermanbd531472007-10-26 11:29:04 -0600921:
Rusty Russella24e7852007-10-21 16:41:35 -070093
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070094/*
95 * Calculate the delta between where we were compiled to run
Eric W. Biederman968de4f2006-12-07 02:14:04 +010096 * at and where we were actually loaded at. This can only be done
97 * with a short local call on x86. Nothing else will tell us what
98 * address we are running at. The reserved chunk of the real-mode
H. Peter Anvin85414b62007-07-11 12:18:33 -070099 * data at 0x1e4 (defined as a scratch field) are used as the stack
100 * for this calculation. Only 4 bytes are needed.
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100101 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700102 leal (BP_scratch+4)(%esi), %esp
103 call 1f
1041: popl %ebp
105 subl $1b, %ebp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100106
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700107/*
108 * %ebp contains the address we are loaded at by the boot loader and %ebx
Vivek Goyale69f2022006-12-07 02:14:04 +0100109 * contains the address where we should move the kernel image temporarily
110 * for safe in-place decompression.
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100111 */
Vivek Goyale69f2022006-12-07 02:14:04 +0100112
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100113#ifdef CONFIG_RELOCATABLE
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700114 movl %ebp, %ebx
H. Peter Anvin37ba7ab2009-05-11 15:56:08 -0700115 movl BP_kernel_alignment(%esi), %eax
116 decl %eax
117 addl %eax, %ebx
118 notl %eax
119 andl %eax, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100120#else
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700121 movl $LOAD_PHYSICAL_ADDR, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100122#endif
123
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700124 /* Target address to relocate to for decompression */
125 addl $z_extract_offset, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100126
H. Peter Anvin0a137732009-05-08 16:27:41 -0700127 /* Set up the stack */
128 leal boot_stack_end(%ebx), %esp
129
H. Peter Anvin97541912009-05-06 17:56:51 -0700130 /* Zero EFLAGS */
131 pushl $0
132 popfl
133
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700134/*
135 * Copy the compressed kernel to the end of our buffer
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100136 * where decompression in place becomes safe.
137 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700138 pushl %esi
H. Peter Anvin36d37932009-05-08 16:45:15 -0700139 leal (_bss-4)(%ebp), %esi
140 leal (_bss-4)(%ebx), %edi
H. Peter Anvin5b11f1c2009-05-08 16:20:34 -0700141 movl $(_bss - startup_32), %ecx
H. Peter Anvin36d37932009-05-08 16:45:15 -0700142 shrl $2, %ecx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100143 std
H. Peter Anvin36d37932009-05-08 16:45:15 -0700144 rep movsl
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100145 cld
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700146 popl %esi
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100147
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700148/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100149 * Jump to the relocated address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700151 leal relocated(%ebx), %eax
152 jmp *%eax
Cyrill Gorcunovcb425af2009-02-14 00:50:23 +0300153ENDPROC(startup_32)
154
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700155 .text
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100156relocated:
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
H. Peter Anvin0a137732009-05-08 16:27:41 -0700159 * Clear BSS (stack is currently empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700161 xorl %eax, %eax
H. Peter Anvin5b11f1c2009-05-08 16:20:34 -0700162 leal _bss(%ebx), %edi
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700163 leal _ebss(%ebx), %ecx
164 subl %edi, %ecx
H. Peter Anvin36d37932009-05-08 16:45:15 -0700165 shrl $2, %ecx
166 rep stosl
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100167
168/*
H. Peter Anvin22a57f52010-08-02 15:34:44 -0700169 * Adjust our own GOT
170 */
171 leal _got(%ebx), %edx
172 leal _egot(%ebx), %ecx
1731:
174 cmpl %ecx, %edx
175 jae 2f
176 addl %ebx, (%edx)
177 addl $4, %edx
178 jmp 1b
1792:
180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Do the decompression, and jump to the new kernel..
183 */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700184 leal z_extract_offset_negative(%ebx), %ebp
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700185 /* push arguments for decompress_kernel: */
186 pushl %ebp /* output address */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700187 pushl $z_input_len /* input_len */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700188 leal input_data(%ebx), %eax
189 pushl %eax /* input_data */
190 leal boot_heap(%ebx), %eax
191 pushl %eax /* heap area */
192 pushl %esi /* real mode pointer */
193 call decompress_kernel
194 addl $20, %esp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100195
196#if CONFIG_RELOCATABLE
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700197/*
198 * Find the address of the relocations.
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100199 */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700200 leal z_output_len(%ebp), %edi
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100201
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700202/*
203 * Calculate the delta between where vmlinux was compiled to run
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100204 * and where it was actually loaded.
205 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700206 movl %ebp, %ebx
207 subl $LOAD_PHYSICAL_ADDR, %ebx
208 jz 2f /* Nothing to be done if loaded at compiled addr. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100210 * Process relocations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
H. Peter Anvin5f64ec62009-05-08 15:45:17 -07002131: subl $4, %edi
214 movl (%edi), %ecx
215 testl %ecx, %ecx
216 jz 2f
217 addl %ebx, -__PAGE_OFFSET(%ebx, %ecx)
218 jmp 1b
Eric W. Biederman968de4f2006-12-07 02:14:04 +01002192:
220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100223 * Jump to the decompressed kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700225 xorl %ebx, %ebx
226 jmp *%ebp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100227
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700228/*
229 * Stack and heap for uncompression
230 */
231 .bss
232 .balign 4
Alexander van Heukelum7c539762008-04-08 12:54:30 +0200233boot_heap:
234 .fill BOOT_HEAP_SIZE, 1, 0
235boot_stack:
236 .fill BOOT_STACK_SIZE, 1, 0
237boot_stack_end: