1 /* $NetBSD: realprot.S,v 1.10 2010/12/19 17:18:23 jakllsch Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Loosely based on code from stand/lib/libcrt/bootsect/start_bootsect.S
36 #include <machine/asm.h>
44 .byte 0, 0x00, 0x00, 0
46 /* kernel code segment */
50 .byte 0, 0x9f, 0xcf, 0
52 /* kernel data segment */
56 .byte 0, 0x93, 0xcf, 0
58 /* boot code segment, base will be patched */
61 .byte 0, 0x9e, 0x4f, 0
63 /* boot data segment, base will be patched */
66 .byte 0, 0x92, 0xcf, 0
68 /* 16 bit real mode, base will be patched */
71 .byte 0, 0x9e, 0x00, 0
73 /* limits (etc) for data segment in real mode */
74 bootrealdata = . - gdt
76 .byte 0, 0x92, 0x00, 0
81 .word gdtlen-1 /* limit */
82 .long 0 /* physical addr, will be inserted */
84 toreal: .word xreal /* off:seg address for indirect jump */
85 ourseg: .word 0 /* real mode code and data segment */
87 stkseg: .word 0 /* real mode stack segment */
88 stkdif: .long 0 /* diff. between real and prot sp */
99 /* sort out stuff for %ss != %ds */
107 /* fix up GDT entries for bootstrap */
112 #define FIXUP(gdt_index) \
113 movw %ax, gdt+gdt_index+2; \
114 movb %dl, gdt+gdt_index+4
120 /* fix up GDT pointer */
131 * Switch CPU to 32bit protected mode to execute C.
133 * NB: Call with the 32bit calll instruction so that a 32 bit
134 * return address is pushed.
136 * All registers are preserved, %ss:%esp will point to the same
137 * place as %ss:%sp did, although the actual value of %esp might
140 * Interrupts are disabled while we are in 32bit mode to save us
141 * having to setup a different IDT. This code is only used during
142 * the boot process and it doesn't use any interrupts.
149 lgdt %cs:gdtarg /* Global descriptor table */
153 movl %eax, %cr0 /* Enter 'protected mode' */
155 ljmp $bootcodeseg, $1f /* Jump into a 32bit segment */
159 /* Set all the segment registers to map the same area as the code */
160 mov $bootdataseg, %eax
164 addl stkdif, %esp /* Allow for real %ss != %ds */
172 * Switch CPU back to 16bit real mode in order to call system bios functions.
174 * All registers are preserved, except that %sp may be changed so that
175 * %ss:%sp points to the same memory.
176 * Note that %ebp is preserved and will not reference the correct part
179 * Interrupts are enabled while in real mode.
181 * Based on the descripton in section 14.5 of the 80386 Programmer's
187 * VIA C3 processors (Eden, Samuel 2) don't seem to correctly switch back to
188 * executing 16 bit code after the switch to real mode and subsequent jump.
190 * It is speculated that the CPU is prefetching and decoding branch
191 * targets and not invalidating this buffer on the long jump.
192 * Further investication indicates that the caching of return addresses
193 * is most likely the problem.
195 * Previous versions just used some extra call/ret and a few NOPs, these
196 * only helped a bit, but booting compressed kernels would still fail.
198 * Trashing the return address stack (by doing 'call' without matched 'ret')
199 * Seems to fix things completely. 1 iteration isn't enough, 16 is plenty.
208 1: call trash_return_cache
214 * Load the segment registers while still in protected mode.
215 * Otherwise the control bits don't get changed.
216 * The correct base addresses are loaded later.
218 movw $bootrealdata, %ax
224 * Load %cs with a segment that has the correct attributes for
227 ljmp $bootrealseg, $1f
233 movl %eax, %cr0 /* Disable potected mode */
235 /* Jump far indirect to load real mode %cs */
239 * CPU is now in real mode, load the other segment registers
240 * with their correct base addresses.
246 * If stack was above 64k, 16bit %ss needs to be different from
247 * 32bit %ss (and the other segment registers).
253 /* Check we are returning to an address below 64k */
256 movw 2/*bp*/ + 4/*eax*/ + 2(%bp), %ax /* high bits ret addr */
267 movl 2/*bp*/ + 4/*eax*/(%bp), %eax /* return address */
273 3: .asciz "prot_to_real can't return to "
275 .global dump_eax_buff
287 * convert boot time 'linear' address to a physical one