blob: a0559930a180ecb810df4b186d173d6fb11e3755 [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
36 /*
37 * We don't need the return address, so set up the stack so
38 * efi_main() can find its arugments.
39 */
40 add $0x4, %esp
41
42 call efi_main
43 cmpl $0, %eax
44 je preferred_addr
45 movl %eax, %esi
46 call 1f
471:
48 popl %eax
49 subl $1b, %eax
50 subl BP_pref_address(%esi), %eax
51 add BP_code32_start(%esi), %eax
52 leal preferred_addr(%eax), %eax
53 jmp *%eax
54
55preferred_addr:
56#endif
Eric W. Biedermanbd531472007-10-26 11:29:04 -060057 cld
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070058 /*
59 * Test KEEP_SEGMENTS flag to see if the bootloader is asking
60 * us to not reload segments
61 */
62 testb $(1<<6), BP_loadflags(%esi)
63 jnz 1f
Rusty Russella24e7852007-10-21 16:41:35 -070064
Eric W. Biedermanbd531472007-10-26 11:29:04 -060065 cli
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070066 movl $__BOOT_DS, %eax
67 movl %eax, %ds
68 movl %eax, %es
69 movl %eax, %fs
70 movl %eax, %gs
71 movl %eax, %ss
Eric W. Biedermanbd531472007-10-26 11:29:04 -0600721:
Rusty Russella24e7852007-10-21 16:41:35 -070073
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070074/*
75 * Calculate the delta between where we were compiled to run
Eric W. Biederman968de4f2006-12-07 02:14:04 +010076 * at and where we were actually loaded at. This can only be done
77 * with a short local call on x86. Nothing else will tell us what
78 * address we are running at. The reserved chunk of the real-mode
H. Peter Anvin85414b62007-07-11 12:18:33 -070079 * data at 0x1e4 (defined as a scratch field) are used as the stack
80 * for this calculation. Only 4 bytes are needed.
Eric W. Biederman968de4f2006-12-07 02:14:04 +010081 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070082 leal (BP_scratch+4)(%esi), %esp
83 call 1f
841: popl %ebp
85 subl $1b, %ebp
Eric W. Biederman968de4f2006-12-07 02:14:04 +010086
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070087/*
88 * %ebp contains the address we are loaded at by the boot loader and %ebx
Vivek Goyale69f2022006-12-07 02:14:04 +010089 * contains the address where we should move the kernel image temporarily
90 * for safe in-place decompression.
Eric W. Biederman968de4f2006-12-07 02:14:04 +010091 */
Vivek Goyale69f2022006-12-07 02:14:04 +010092
Eric W. Biederman968de4f2006-12-07 02:14:04 +010093#ifdef CONFIG_RELOCATABLE
H. Peter Anvin5f64ec62009-05-08 15:45:17 -070094 movl %ebp, %ebx
H. Peter Anvin37ba7ab2009-05-11 15:56:08 -070095 movl BP_kernel_alignment(%esi), %eax
96 decl %eax
97 addl %eax, %ebx
98 notl %eax
99 andl %eax, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100100#else
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700101 movl $LOAD_PHYSICAL_ADDR, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100102#endif
103
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700104 /* Target address to relocate to for decompression */
105 addl $z_extract_offset, %ebx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100106
H. Peter Anvin0a137732009-05-08 16:27:41 -0700107 /* Set up the stack */
108 leal boot_stack_end(%ebx), %esp
109
H. Peter Anvin97541912009-05-06 17:56:51 -0700110 /* Zero EFLAGS */
111 pushl $0
112 popfl
113
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700114/*
115 * Copy the compressed kernel to the end of our buffer
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100116 * where decompression in place becomes safe.
117 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700118 pushl %esi
H. Peter Anvin36d37932009-05-08 16:45:15 -0700119 leal (_bss-4)(%ebp), %esi
120 leal (_bss-4)(%ebx), %edi
H. Peter Anvin5b11f1c2009-05-08 16:20:34 -0700121 movl $(_bss - startup_32), %ecx
H. Peter Anvin36d37932009-05-08 16:45:15 -0700122 shrl $2, %ecx
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100123 std
H. Peter Anvin36d37932009-05-08 16:45:15 -0700124 rep movsl
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100125 cld
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700126 popl %esi
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100127
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700128/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100129 * Jump to the relocated address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700131 leal relocated(%ebx), %eax
132 jmp *%eax
Cyrill Gorcunovcb425af2009-02-14 00:50:23 +0300133ENDPROC(startup_32)
134
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700135 .text
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100136relocated:
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
H. Peter Anvin0a137732009-05-08 16:27:41 -0700139 * Clear BSS (stack is currently empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700141 xorl %eax, %eax
H. Peter Anvin5b11f1c2009-05-08 16:20:34 -0700142 leal _bss(%ebx), %edi
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700143 leal _ebss(%ebx), %ecx
144 subl %edi, %ecx
H. Peter Anvin36d37932009-05-08 16:45:15 -0700145 shrl $2, %ecx
146 rep stosl
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100147
148/*
H. Peter Anvin22a57f52010-08-02 15:34:44 -0700149 * Adjust our own GOT
150 */
151 leal _got(%ebx), %edx
152 leal _egot(%ebx), %ecx
1531:
154 cmpl %ecx, %edx
155 jae 2f
156 addl %ebx, (%edx)
157 addl $4, %edx
158 jmp 1b
1592:
160
161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * Do the decompression, and jump to the new kernel..
163 */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700164 leal z_extract_offset_negative(%ebx), %ebp
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700165 /* push arguments for decompress_kernel: */
166 pushl %ebp /* output address */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700167 pushl $z_input_len /* input_len */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700168 leal input_data(%ebx), %eax
169 pushl %eax /* input_data */
170 leal boot_heap(%ebx), %eax
171 pushl %eax /* heap area */
172 pushl %esi /* real mode pointer */
173 call decompress_kernel
174 addl $20, %esp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100175
176#if CONFIG_RELOCATABLE
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700177/*
178 * Find the address of the relocations.
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100179 */
H. Peter Anvin02a884c2009-05-08 17:42:16 -0700180 leal z_output_len(%ebp), %edi
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100181
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700182/*
183 * Calculate the delta between where vmlinux was compiled to run
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100184 * and where it was actually loaded.
185 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700186 movl %ebp, %ebx
187 subl $LOAD_PHYSICAL_ADDR, %ebx
188 jz 2f /* Nothing to be done if loaded at compiled addr. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100190 * Process relocations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
H. Peter Anvin5f64ec62009-05-08 15:45:17 -07001931: subl $4, %edi
194 movl (%edi), %ecx
195 testl %ecx, %ecx
196 jz 2f
197 addl %ebx, -__PAGE_OFFSET(%ebx, %ecx)
198 jmp 1b
Eric W. Biederman968de4f2006-12-07 02:14:04 +01001992:
200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100203 * Jump to the decompressed kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700205 xorl %ebx, %ebx
206 jmp *%ebp
Eric W. Biederman968de4f2006-12-07 02:14:04 +0100207
H. Peter Anvin5f64ec62009-05-08 15:45:17 -0700208/*
209 * Stack and heap for uncompression
210 */
211 .bss
212 .balign 4
Alexander van Heukelum7c539762008-04-08 12:54:30 +0200213boot_heap:
214 .fill BOOT_HEAP_SIZE, 1, 0
215boot_stack:
216 .fill BOOT_STACK_SIZE, 1, 0
217boot_stack_end: