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 *
15 * Page 0 is deliberately kept safe, since System Management Mode code in
16 * laptops may need to access the BIOS data stored there.  This is also
17 * useful for future device drivers that either access the BIOS via VM86
18 * mode.
19 */
20
21/*
22 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
23 */
24	.text
25
26#include <linux/init.h>
27#include <linux/linkage.h>
28#include <asm/segment.h>
29#include <asm/page_types.h>
30#include <asm/boot.h>
31#include <asm/asm-offsets.h>
32#include <asm/bootparam.h>
33
34	__HEAD
35ENTRY(startup_32)
36#ifdef CONFIG_EFI_STUB
37	jmp	preferred_addr
38
39	/*
40	 * We don't need the return address, so set up the stack so
41	 * efi_main() can find its arguments.
42	 */
43ENTRY(efi_pe_entry)
44	add	$0x4, %esp
45
46	call	1f
471:	popl	%esi
48	subl	$1b, %esi
49
50	popl	%ecx
51	movl	%ecx, efi32_config(%esi)	/* Handle */
52	popl	%ecx
53	movl	%ecx, efi32_config+8(%esi)	/* EFI System table pointer */
54
55	/* Relocate efi_config->call() */
56	leal	efi32_config(%esi), %eax
57	add	%esi, 88(%eax)
58	pushl	%eax
59
60	call	make_boot_params
61	cmpl	$0, %eax
62	je	fail
63	movl	%esi, BP_code32_start(%eax)
64	popl	%ecx
65	pushl	%eax
66	pushl	%ecx
67	jmp	2f		/* Skip efi_config initialization */
68
69ENTRY(efi32_stub_entry)
70	add	$0x4, %esp
71	popl	%ecx
72	popl	%edx
73
74	call	1f
751:	popl	%esi
76	subl	$1b, %esi
77
78	movl	%ecx, efi32_config(%esi)	/* Handle */
79	movl	%edx, efi32_config+8(%esi)	/* EFI System table pointer */
80
81	/* Relocate efi_config->call() */
82	leal	efi32_config(%esi), %eax
83	add	%esi, 88(%eax)
84	pushl	%eax
852:
86	call	efi_main
87	cmpl	$0, %eax
88	movl	%eax, %esi
89	jne	2f
90fail:
91	/* EFI init failed, so hang. */
92	hlt
93	jmp	fail
942:
95	movl	BP_code32_start(%esi), %eax
96	leal	preferred_addr(%eax), %eax
97	jmp	*%eax
98
99preferred_addr:
100#endif
101	cld
102	/*
103	 * Test KEEP_SEGMENTS flag to see if the bootloader is asking
104	 * us to not reload segments
105	 */
106	testb	$KEEP_SEGMENTS, BP_loadflags(%esi)
107	jnz	1f
108
109	cli
110	movl	$__BOOT_DS, %eax
111	movl	%eax, %ds
112	movl	%eax, %es
113	movl	%eax, %fs
114	movl	%eax, %gs
115	movl	%eax, %ss
1161:
117
118/*
119 * Calculate the delta between where we were compiled to run
120 * at and where we were actually loaded at.  This can only be done
121 * with a short local call on x86.  Nothing  else will tell us what
122 * address we are running at.  The reserved chunk of the real-mode
123 * data at 0x1e4 (defined as a scratch field) are used as the stack
124 * for this calculation. Only 4 bytes are needed.
125 */
126	leal	(BP_scratch+4)(%esi), %esp
127	call	1f
1281:	popl	%ebp
129	subl	$1b, %ebp
130
131/*
132 * %ebp contains the address we are loaded at by the boot loader and %ebx
133 * contains the address where we should move the kernel image temporarily
134 * for safe in-place decompression.
135 */
136
137#ifdef CONFIG_RELOCATABLE
138	movl	%ebp, %ebx
139	movl	BP_kernel_alignment(%esi), %eax
140	decl	%eax
141	addl    %eax, %ebx
142	notl	%eax
143	andl    %eax, %ebx
144	cmpl	$LOAD_PHYSICAL_ADDR, %ebx
145	jge	1f
146#endif
147	movl	$LOAD_PHYSICAL_ADDR, %ebx
1481:
149
150	/* Target address to relocate to for decompression */
151	addl	$z_extract_offset, %ebx
152
153	/* Set up the stack */
154	leal	boot_stack_end(%ebx), %esp
155
156	/* Zero EFLAGS */
157	pushl	$0
158	popfl
159
160/*
161 * Copy the compressed kernel to the end of our buffer
162 * where decompression in place becomes safe.
163 */
164	pushl	%esi
165	leal	(_bss-4)(%ebp), %esi
166	leal	(_bss-4)(%ebx), %edi
167	movl	$(_bss - startup_32), %ecx
168	shrl	$2, %ecx
169	std
170	rep	movsl
171	cld
172	popl	%esi
173
174/*
175 * Jump to the relocated address.
176 */
177	leal	relocated(%ebx), %eax
178	jmp	*%eax
179ENDPROC(startup_32)
180
181	.text
182relocated:
183
184/*
185 * Clear BSS (stack is currently empty)
186 */
187	xorl	%eax, %eax
188	leal	_bss(%ebx), %edi
189	leal	_ebss(%ebx), %ecx
190	subl	%edi, %ecx
191	shrl	$2, %ecx
192	rep	stosl
193
194/*
195 * Adjust our own GOT
196 */
197	leal	_got(%ebx), %edx
198	leal	_egot(%ebx), %ecx
1991:
200	cmpl	%ecx, %edx
201	jae	2f
202	addl	%ebx, (%edx)
203	addl	$4, %edx
204	jmp	1b
2052:
206
207/*
208 * Do the decompression, and jump to the new kernel..
209 */
210				/* push arguments for decompress_kernel: */
211	pushl	$z_run_size	/* size of kernel with .bss and .brk */
212	pushl	$z_output_len	/* decompressed length, end of relocs */
213	leal	z_extract_offset_negative(%ebx), %ebp
214	pushl	%ebp		/* output address */
215	pushl	$z_input_len	/* input_len */
216	leal	input_data(%ebx), %eax
217	pushl	%eax		/* input_data */
218	leal	boot_heap(%ebx), %eax
219	pushl	%eax		/* heap area */
220	pushl	%esi		/* real mode pointer */
221	call	decompress_kernel /* returns kernel location in %eax */
222	addl	$28, %esp
223
224/*
225 * Jump to the decompressed kernel.
226 */
227	xorl	%ebx, %ebx
228	jmp	*%eax
229
230#ifdef CONFIG_EFI_STUB
231	.data
232efi32_config:
233	.fill 11,8,0
234	.long efi_call_phys
235	.long 0
236	.byte 0
237#endif
238
239/*
240 * Stack and heap for uncompression
241 */
242	.bss
243	.balign 4
244boot_heap:
245	.fill BOOT_HEAP_SIZE, 1, 0
246boot_stack:
247	.fill BOOT_STACK_SIZE, 1, 0
248boot_stack_end:
249