2 * This file is part of the coreboot project.
4 * Copyright (C) 2009-2010 coresystems GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 #define REALMODE_BASE 0x600
21 #define RELOCATED(x) (x - __realmode_code + REALMODE_BASE)
26 /* This is the intXX interrupt handler stub code. It gets copied
27 * to the IDT and to some fixed addresses in the F segment. Before
28 * the code can used, it gets patched up by the C function copying
29 * it: byte 3 (the $0 in movb $0, %al) is overwritten with the int#.
36 movb $0, %al /* This instruction gets modified */
37 ljmp $0, $__interrupt_handler_16bit
38 .globl __idt_handler_size
39 __idt_handler_size = ( . - __idt_handler)
42 /* In order to be independent of coreboot's position in RAM
43 * we relocate a part of the code to the low megabyte, so the
44 * CPU can use it in real-mode. This code lives at __realmode_code.
46 .globl __realmode_code
49 /* Realmode IDT pointer structure. */
51 __realmode_idt = RELOCATED(.)
52 .word 1023 /* 16 bit limit */
53 .long 0 /* 24 bit base */
56 /* Preserve old stack */
57 __stack = RELOCATED(.)
60 /* Register store for realmode_call and realmode_interrupt */
61 __registers = RELOCATED(.)
65 .long 0 /* 12 - EDX */
66 .long 0 /* 16 - ESI */
67 .long 0 /* 20 - EDI */
69 /* 256 byte buffer, used by int10 */
71 __buffer = RELOCATED(.)
75 .globl __realmode_call
76 __realmode_call = RELOCATED(.)
77 /* save all registers to the stack */
80 /* Move the protected mode stack to a safe place */
84 /* This function is called with regparm=0 and we have
85 * to skip the 32 byte from pushal. Hence start at 36.
90 mov %ax, __lcall_instr + 1
91 andl $0xffff0000, %eax
93 mov %ax, __lcall_instr + 3
95 /* initial register values */
97 movl %eax, __registers + 0 /* eax */
99 movl %eax, __registers + 4 /* ebx */
101 movl %eax, __registers + 8 /* ecx */
103 movl %eax, __registers + 12 /* edx */
105 movl %eax, __registers + 16 /* esi */
107 movl %eax, __registers + 20 /* edi */
109 /* Activate the right segment descriptor real mode. */
110 ljmp $0x28, $RELOCATED(1f)
113 /* 16 bit code from here on... */
115 /* Load the segment registers w/ properly configured
116 * segment descriptors. They will retain these
117 * configurations (limits, writability, etc.) once
118 * protected mode is turned off.
127 /* Turn off protection */
132 /* Now really going into real mode */
133 ljmp $0, $RELOCATED(1f)
135 /* Setup a stack: Put the stack at the end of page zero.
136 * That way we can easily share it between real and
137 * protected, since the 16 bit ESP at segment 0 will
138 * work for any case. */
144 /* Load 16 bit IDT */
149 /* initialize registers for option rom lcall */
150 movl __registers + 0, %eax
151 movl __registers + 4, %ebx
152 movl __registers + 8, %ecx
153 movl __registers + 12, %edx
154 movl __registers + 16, %esi
155 movl __registers + 20, %edi
157 /* Set all segments to 0x0000, ds to 0x0040 */
167 /* ************************************ */
168 __lcall_instr = RELOCATED(.)
171 /* ************************************ */
173 /* If we got here, we are just about done.
174 * Need to get back to protected mode.
180 /* Now that we are in protected mode
181 * jump to a 32 bit code segment.
183 data32 ljmp $0x10, $RELOCATED(1f)
193 /* restore proper idt */
200 // TODO return AX from OPROM call
203 .globl __realmode_interrupt
204 __realmode_interrupt = RELOCATED(.)
205 /* save all registers to the stack */
211 /* This function is called with regparm=0 and we have
212 * to skip the 32 byte from pushal. Hence start at 36.
215 /* prepare interrupt calling code */
217 movb %al, __intXX_instr + 1 /* intno */
219 /* initial register values */
221 movl %eax, __registers + 0 /* eax */
223 movl %eax, __registers + 4 /* ebx */
225 movl %eax, __registers + 8 /* ecx */
227 movl %eax, __registers + 12 /* edx */
229 movl %eax, __registers + 16 /* esi */
231 movl %eax, __registers + 20 /* edi */
233 /* This configures CS properly for real mode. */
234 ljmp $0x28, $RELOCATED(1f)
236 .code16 /* 16 bit code from here on... */
238 /* Load the segment registers w/ properly configured segment
239 * descriptors. They will retain these configurations (limits,
240 * writability, etc.) once protected mode is turned off.
249 /* Turn off protected mode */
254 /* Now really going into real mode */
255 data32 ljmp $0, $RELOCATED(1f)
258 /* put the stack at the end of page zero. That way we can easily
259 * share it between real mode and protected mode, because %esp and
260 * %ss:%sp point to the same memory.
268 /* Load 16-bit intXX IDT */
273 /* initialize registers for intXX call */
274 movl __registers + 0, %eax
275 movl __registers + 4, %ebx
276 movl __registers + 8, %ecx
277 movl __registers + 12, %edx
278 movl __registers + 16, %esi
279 movl __registers + 20, %edi
281 /* Set all segments to 0x0000 */
290 __intXX_instr = RELOCATED(.)
291 .byte 0xcd, 0x00 /* This becomes intXX */
293 /* Ok, the job is done, now go back to protected mode coreboot */
298 /* Now that we are in protected mode jump to a 32-bit code segment. */
299 data32 ljmp $0x10, $RELOCATED(1f)
309 /* restore coreboot's 32-bit IDT */
317 /* This is the 16-bit interrupt entry point called by the IDT stub code.
319 * Before this code code is called, %eax is pushed to the stack, and the
320 * interrupt number is loaded into %al. On return this function cleans up
324 __interrupt_handler_16bit = RELOCATED(.)
330 /* Clean up the interrupt number. We could have done this in the stub,
331 * but it would have cost 2 more bytes per stub entry.
334 pushl %eax /* ... and make it the first parameter */
336 /* Switch to protected mode */
341 /* ... and jump to a 32 bit code segment. */
342 data32 ljmp $0x10, $RELOCATED(1f)
354 /* Call the C interrupt handler */
355 movl $interrupt_handler, %eax
358 /* Now return to real mode ... */
359 ljmp $0x28, $RELOCATED(1f)
362 /* Load the segment registers with properly configured segment
363 * descriptors. They will retain these configurations (limits,
364 * writability, etc.) once protected mode is turned off.
373 /* Disable Protected Mode */
378 /* Now really going into real mode */
379 ljmp $0, $RELOCATED(1f)
381 /* Restore real-mode stack segment */
385 /* Restore 16 bit IDT */
390 /* Restore all registers, including those
391 * manipulated by the C handler
401 .globl __realmode_code_size
402 __realmode_code_size = (. - __realmode_code)