1 /* $NetBSD: realprot.S,v 1.11 2013/12/24 19:00:56 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>
37 #include <x86/specialreg.h>
43 .byte 0, 0x00, 0x00, 0
45 /* kernel code segment */
49 .byte 0, 0x9f, 0xcf, 0
51 /* kernel data segment */
55 .byte 0, 0x93, 0xcf, 0
57 /* boot code segment, base will be patched */
60 .byte 0, 0x9e, 0x4f, 0
62 /* boot data segment, base will be patched */
65 .byte 0, 0x92, 0xcf, 0
67 /* 16 bit real mode, base will be patched */
70 .byte 0, 0x9e, 0x00, 0
72 /* limits (etc) for data segment in real mode */
73 bootrealdata = . - gdt
75 .byte 0, 0x92, 0x00, 0
80 .word gdtlen-1 /* limit */
81 .long 0 /* physical addr, will be inserted */
83 toreal: .word xreal /* off:seg address for indirect jump */
84 ourseg: .word 0 /* real mode code and data segment */
86 stkseg: .word 0 /* real mode stack segment */
87 stkdif: .long 0 /* diff. between real and prot sp */
98 /* sort out stuff for %ss != %ds */
106 /* fix up GDT entries for bootstrap */
111 #define FIXUP(gdt_index) \
112 movw %ax, gdt+gdt_index+2; \
113 movb %dl, gdt+gdt_index+4
119 /* fix up GDT pointer */
130 * Switch CPU to 32bit protected mode to execute C.
132 * NB: Call with the 32bit calll instruction so that a 32 bit
133 * return address is pushed.
135 * All registers are preserved, %ss:%esp will point to the same
136 * place as %ss:%sp did, although the actual value of %esp might
139 * Interrupts are disabled while we are in 32bit mode to save us
140 * having to setup a different IDT. This code is only used during
141 * the boot process and it doesn't use any interrupts.
148 lgdt %cs:gdtarg /* Global descriptor table */
152 movl %eax, %cr0 /* Enter 'protected mode' */
154 ljmp $bootcodeseg, $1f /* Jump into a 32bit segment */
158 /* Set all the segment registers to map the same area as the code */
159 mov $bootdataseg, %eax
163 addl stkdif, %esp /* Allow for real %ss != %ds */
171 * Switch CPU back to 16bit real mode in order to call system bios functions.
173 * All registers are preserved, except that %sp may be changed so that
174 * %ss:%sp points to the same memory.
175 * Note that %ebp is preserved and will not reference the correct part
178 * Interrupts are enabled while in real mode.
180 * Based on the descripton in section 14.5 of the 80386 Programmer's
186 * VIA C3 processors (Eden, Samuel 2) don't seem to correctly switch back to
187 * executing 16 bit code after the switch to real mode and subsequent jump.
189 * It is speculated that the CPU is prefetching and decoding branch
190 * targets and not invalidating this buffer on the long jump.
191 * Further investication indicates that the caching of return addresses
192 * is most likely the problem.
194 * Previous versions just used some extra call/ret and a few NOPs, these
195 * only helped a bit, but booting compressed kernels would still fail.
197 * Trashing the return address stack (by doing 'call' without matched 'ret')
198 * Seems to fix things completely. 1 iteration isn't enough, 16 is plenty.
207 1: call trash_return_cache
213 * Load the segment registers while still in protected mode.
214 * Otherwise the control bits don't get changed.
215 * The correct base addresses are loaded later.
217 movw $bootrealdata, %ax
223 * Load %cs with a segment that has the correct attributes for
226 ljmp $bootrealseg, $1f
232 movl %eax, %cr0 /* Disable potected mode */
234 /* Jump far indirect to load real mode %cs */
238 * CPU is now in real mode, load the other segment registers
239 * with their correct base addresses.
245 * If stack was above 64k, 16bit %ss needs to be different from
246 * 32bit %ss (and the other segment registers).
252 /* Check we are returning to an address below 64k */
255 movw 2/*bp*/ + 4/*eax*/ + 2(%bp), %ax /* high bits ret addr */
266 movl 2/*bp*/ + 4/*eax*/(%bp), %eax /* return address */
272 3: .asciz "prot_to_real can't return to "
274 .global dump_eax_buff
286 * convert boot time 'linear' address to a physical one