Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 15 | * Page 0 is deliberately kept safe, since System Management Mode code in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * laptops may need to access the BIOS data stored there. This is also |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 17 | * useful for future device drivers that either access the BIOS via VM86 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * mode. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 |
| 23 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 24 | .text |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Tim Abbott | 1dc818c | 2009-09-16 16:44:27 -0400 | [diff] [blame] | 26 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/linkage.h> |
| 28 | #include <asm/segment.h> |
Jeremy Fitzhardinge | 0341c14 | 2009-02-13 11:14:01 -0800 | [diff] [blame] | 29 | #include <asm/page_types.h> |
Vivek Goyal | e69f202 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 30 | #include <asm/boot.h> |
Rusty Russell | a24e785 | 2007-10-21 16:41:35 -0700 | [diff] [blame] | 31 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Tim Abbott | 1dc818c | 2009-09-16 16:44:27 -0400 | [diff] [blame] | 33 | __HEAD |
Cyrill Gorcunov | cb425af | 2009-02-14 00:50:23 +0300 | [diff] [blame] | 34 | ENTRY(startup_32) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 35 | #ifdef CONFIG_EFI_STUB |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 36 | jmp preferred_addr |
| 37 | |
| 38 | .balign 0x10 |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 39 | /* |
| 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 Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame^] | 45 | 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 |
| 53 | |
| 54 | .org 0x30,0x90 |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 55 | call efi_main |
| 56 | cmpl $0, %eax |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 57 | movl %eax, %esi |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 58 | jne 2f |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 59 | 1: |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 60 | /* EFI init failed, so hang. */ |
| 61 | hlt |
| 62 | jmp 1b |
| 63 | 2: |
| 64 | call 3f |
| 65 | 3: |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 66 | popl %eax |
Matt Fleming | b199430 | 2012-04-15 16:06:04 +0100 | [diff] [blame] | 67 | subl $3b, %eax |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 68 | subl BP_pref_address(%esi), %eax |
| 69 | add BP_code32_start(%esi), %eax |
| 70 | leal preferred_addr(%eax), %eax |
| 71 | jmp *%eax |
| 72 | |
| 73 | preferred_addr: |
| 74 | #endif |
Eric W. Biederman | bd53147 | 2007-10-26 11:29:04 -0600 | [diff] [blame] | 75 | cld |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 76 | /* |
| 77 | * Test KEEP_SEGMENTS flag to see if the bootloader is asking |
| 78 | * us to not reload segments |
| 79 | */ |
| 80 | testb $(1<<6), BP_loadflags(%esi) |
| 81 | jnz 1f |
Rusty Russell | a24e785 | 2007-10-21 16:41:35 -0700 | [diff] [blame] | 82 | |
Eric W. Biederman | bd53147 | 2007-10-26 11:29:04 -0600 | [diff] [blame] | 83 | cli |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 84 | movl $__BOOT_DS, %eax |
| 85 | movl %eax, %ds |
| 86 | movl %eax, %es |
| 87 | movl %eax, %fs |
| 88 | movl %eax, %gs |
| 89 | movl %eax, %ss |
Eric W. Biederman | bd53147 | 2007-10-26 11:29:04 -0600 | [diff] [blame] | 90 | 1: |
Rusty Russell | a24e785 | 2007-10-21 16:41:35 -0700 | [diff] [blame] | 91 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 92 | /* |
| 93 | * Calculate the delta between where we were compiled to run |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 94 | * at and where we were actually loaded at. This can only be done |
| 95 | * with a short local call on x86. Nothing else will tell us what |
| 96 | * address we are running at. The reserved chunk of the real-mode |
H. Peter Anvin | 85414b6 | 2007-07-11 12:18:33 -0700 | [diff] [blame] | 97 | * data at 0x1e4 (defined as a scratch field) are used as the stack |
| 98 | * for this calculation. Only 4 bytes are needed. |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 99 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 100 | leal (BP_scratch+4)(%esi), %esp |
| 101 | call 1f |
| 102 | 1: popl %ebp |
| 103 | subl $1b, %ebp |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 104 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 105 | /* |
| 106 | * %ebp contains the address we are loaded at by the boot loader and %ebx |
Vivek Goyal | e69f202 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 107 | * contains the address where we should move the kernel image temporarily |
| 108 | * for safe in-place decompression. |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 109 | */ |
Vivek Goyal | e69f202 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 110 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 111 | #ifdef CONFIG_RELOCATABLE |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 112 | movl %ebp, %ebx |
H. Peter Anvin | 37ba7ab | 2009-05-11 15:56:08 -0700 | [diff] [blame] | 113 | movl BP_kernel_alignment(%esi), %eax |
| 114 | decl %eax |
| 115 | addl %eax, %ebx |
| 116 | notl %eax |
| 117 | andl %eax, %ebx |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 118 | #else |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 119 | movl $LOAD_PHYSICAL_ADDR, %ebx |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 120 | #endif |
| 121 | |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 122 | /* Target address to relocate to for decompression */ |
| 123 | addl $z_extract_offset, %ebx |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 124 | |
H. Peter Anvin | 0a13773 | 2009-05-08 16:27:41 -0700 | [diff] [blame] | 125 | /* Set up the stack */ |
| 126 | leal boot_stack_end(%ebx), %esp |
| 127 | |
H. Peter Anvin | 9754191 | 2009-05-06 17:56:51 -0700 | [diff] [blame] | 128 | /* Zero EFLAGS */ |
| 129 | pushl $0 |
| 130 | popfl |
| 131 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 132 | /* |
| 133 | * Copy the compressed kernel to the end of our buffer |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 134 | * where decompression in place becomes safe. |
| 135 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 136 | pushl %esi |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 137 | leal (_bss-4)(%ebp), %esi |
| 138 | leal (_bss-4)(%ebx), %edi |
H. Peter Anvin | 5b11f1c | 2009-05-08 16:20:34 -0700 | [diff] [blame] | 139 | movl $(_bss - startup_32), %ecx |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 140 | shrl $2, %ecx |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 141 | std |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 142 | rep movsl |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 143 | cld |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 144 | popl %esi |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 145 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 146 | /* |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 147 | * Jump to the relocated address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 149 | leal relocated(%ebx), %eax |
| 150 | jmp *%eax |
Cyrill Gorcunov | cb425af | 2009-02-14 00:50:23 +0300 | [diff] [blame] | 151 | ENDPROC(startup_32) |
| 152 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 153 | .text |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 154 | relocated: |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
H. Peter Anvin | 0a13773 | 2009-05-08 16:27:41 -0700 | [diff] [blame] | 157 | * Clear BSS (stack is currently empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 159 | xorl %eax, %eax |
H. Peter Anvin | 5b11f1c | 2009-05-08 16:20:34 -0700 | [diff] [blame] | 160 | leal _bss(%ebx), %edi |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 161 | leal _ebss(%ebx), %ecx |
| 162 | subl %edi, %ecx |
H. Peter Anvin | 36d3793 | 2009-05-08 16:45:15 -0700 | [diff] [blame] | 163 | shrl $2, %ecx |
| 164 | rep stosl |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 165 | |
| 166 | /* |
H. Peter Anvin | 22a57f5 | 2010-08-02 15:34:44 -0700 | [diff] [blame] | 167 | * Adjust our own GOT |
| 168 | */ |
| 169 | leal _got(%ebx), %edx |
| 170 | leal _egot(%ebx), %ecx |
| 171 | 1: |
| 172 | cmpl %ecx, %edx |
| 173 | jae 2f |
| 174 | addl %ebx, (%edx) |
| 175 | addl $4, %edx |
| 176 | jmp 1b |
| 177 | 2: |
| 178 | |
| 179 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * Do the decompression, and jump to the new kernel.. |
| 181 | */ |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 182 | leal z_extract_offset_negative(%ebx), %ebp |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 183 | /* push arguments for decompress_kernel: */ |
| 184 | pushl %ebp /* output address */ |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 185 | pushl $z_input_len /* input_len */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 186 | leal input_data(%ebx), %eax |
| 187 | pushl %eax /* input_data */ |
| 188 | leal boot_heap(%ebx), %eax |
| 189 | pushl %eax /* heap area */ |
| 190 | pushl %esi /* real mode pointer */ |
| 191 | call decompress_kernel |
| 192 | addl $20, %esp |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 193 | |
| 194 | #if CONFIG_RELOCATABLE |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Find the address of the relocations. |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 197 | */ |
H. Peter Anvin | 02a884c | 2009-05-08 17:42:16 -0700 | [diff] [blame] | 198 | leal z_output_len(%ebp), %edi |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 199 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 200 | /* |
| 201 | * Calculate the delta between where vmlinux was compiled to run |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 202 | * and where it was actually loaded. |
| 203 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 204 | movl %ebp, %ebx |
| 205 | subl $LOAD_PHYSICAL_ADDR, %ebx |
| 206 | jz 2f /* Nothing to be done if loaded at compiled addr. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | /* |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 208 | * Process relocations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 211 | 1: subl $4, %edi |
| 212 | movl (%edi), %ecx |
| 213 | testl %ecx, %ecx |
| 214 | jz 2f |
| 215 | addl %ebx, -__PAGE_OFFSET(%ebx, %ecx) |
| 216 | jmp 1b |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 217 | 2: |
| 218 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /* |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 221 | * Jump to the decompressed kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | */ |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 223 | xorl %ebx, %ebx |
| 224 | jmp *%ebp |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 225 | |
H. Peter Anvin | 5f64ec6 | 2009-05-08 15:45:17 -0700 | [diff] [blame] | 226 | /* |
| 227 | * Stack and heap for uncompression |
| 228 | */ |
| 229 | .bss |
| 230 | .balign 4 |
Alexander van Heukelum | 7c53976 | 2008-04-08 12:54:30 +0200 | [diff] [blame] | 231 | boot_heap: |
| 232 | .fill BOOT_HEAP_SIZE, 1, 0 |
| 233 | boot_stack: |
| 234 | .fill BOOT_STACK_SIZE, 1, 0 |
| 235 | boot_stack_end: |