2 * From coreboot x86_asm.S, cleaned up substantially
4 * Copyright (C) 2009-2010 coresystems GmbH
6 * SPDX-License-Identifier: GPL-2.0
9 #include <asm/processor.h>
10 #include <asm/processor-flags.h>
13 #define SEG(segment) $segment * X86_GDT_ENTRY_SIZE
16 * This is the interrupt handler stub code. It gets copied to the IDT and
17 * to some fixed addresses in the F segment. Before the code can used,
18 * it gets patched up by the C function copying it: byte 3 (the $0 in
19 * movb $0, %al) is overwritten with the interrupt numbers.
26 movb $0, %al /* This instruction gets modified */
27 ljmp $0, $__interrupt_handler_16bit
28 .globl __idt_handler_size
30 .long . - __idt_handler
32 .macro setup_registers
33 /* initial register values */
35 movl %eax, __registers + 0 /* eax */
37 movl %eax, __registers + 4 /* ebx */
39 movl %eax, __registers + 8 /* ecx */
41 movl %eax, __registers + 12 /* edx */
43 movl %eax, __registers + 16 /* esi */
45 movl %eax, __registers + 20 /* edi */
48 .macro enter_real_mode
49 /* Activate the right segment descriptor real mode. */
50 ljmp SEG(X86_GDT_ENTRY_16BIT_CS), $PTR_TO_REAL_MODE(1f)
54 * Load the segment registers with properly configured segment
55 * descriptors. They will retain these configurations (limits,
56 * writability, etc.) once protected mode is turned off.
58 mov SEG(X86_GDT_ENTRY_16BIT_DS), %ax
65 /* Turn off protection */
67 andl $~X86_CR0_PE, %eax
70 /* Now really going into real mode */
71 ljmp $0, $PTR_TO_REAL_MODE(1f)
74 * Set up a stack: Put the stack at the end of page zero. That way
75 * we can easily share it between real and protected, since the
76 * 16-bit ESP at segment 0 will work for any case.
88 .macro prepare_for_irom
92 /* Initialise registers for option rom lcall */
93 movl __registers + 0, %eax
94 movl __registers + 4, %ebx
95 movl __registers + 8, %ecx
96 movl __registers + 12, %edx
97 movl __registers + 16, %esi
98 movl __registers + 20, %edi
100 /* Set all segments to 0x0000, ds to 0x0040 */
106 mov SEG(X86_GDT_ENTRY_16BIT_FLAT_DS), %ax
112 .macro enter_protected_mode
113 /* Go back to protected mode */
115 orl $X86_CR0_PE, %eax
118 /* Now that we are in protected mode jump to a 32 bit code segment */
119 data32 ljmp SEG(X86_GDT_ENTRY_32BIT_CS), $PTR_TO_REAL_MODE(1f)
122 mov SEG(X86_GDT_ENTRY_32BIT_DS), %ax
127 mov SEG(X86_GDT_ENTRY_32BIT_FS), %ax
130 /* restore proper idt */
135 * In order to be independent of U-Boot's position in RAM we relocate a part
136 * of the code to the first megabyte of RAM, so the CPU can use it in
137 * real-mode. This code lives at asm_realmode_code.
139 .globl asm_realmode_code
142 /* Realmode IDT pointer structure. */
143 __realmode_idt = PTR_TO_REAL_MODE(.)
144 .word 1023 /* 16 bit limit */
145 .long 0 /* 24 bit base */
148 /* Preserve old stack */
149 __stack = PTR_TO_REAL_MODE(.)
152 /* Register store for realmode_call and realmode_interrupt */
153 __registers = PTR_TO_REAL_MODE(.)
154 .long 0 /* 0 - EAX */
155 .long 0 /* 4 - EBX */
156 .long 0 /* 8 - ECX */
157 .long 0 /* 12 - EDX */
158 .long 0 /* 16 - ESI */
159 .long 0 /* 20 - EDI */
161 /* 256 byte buffer, used by int10 */
162 .globl asm_realmode_buffer
167 .globl asm_realmode_call
169 /* save all registers to the stack */
176 * This function is called with regparm=0 and we have to skip the
177 * 36 bytes from pushf+pusha. Hence start at 40.
178 * Set up our call instruction.
181 mov %ax, __lcall_instr + 1
182 andl $0xffff0000, %eax
184 mov %ax, __lcall_instr + 3
192 __lcall_instr = PTR_TO_REAL_MODE(.)
198 /* restore stack pointer, eflags and register values and exit */
204 .globl __realmode_interrupt
205 __realmode_interrupt:
206 /* save all registers to the stack and store the stack pointer */
213 * This function is called with regparm=0 and we have to skip the
214 * 36 bytes from pushf+pusha. Hence start at 40.
215 * Prepare interrupt calling code.
218 movb %al, __intXX_instr + 1 /* intno */
224 __intXX_instr = PTR_TO_REAL_MODE(.)
225 .byte 0xcd, 0x00 /* This becomes intXX */
229 /* restore stack pointer, eflags and register values and exit */
236 * This is the 16-bit interrupt entry point called by the IDT stub code.
238 * Before this code code is called, %eax is pushed to the stack, and the
239 * interrupt number is loaded into %al. On return this function cleans up
243 __interrupt_handler_16bit = PTR_TO_REAL_MODE(.)
249 /* Clear DF to not break ABI assumptions */
253 * Clean up the interrupt number. We could do this in the stub, but
254 * it would cost two more bytes per stub entry.
257 pushl %eax /* ... and make it the first parameter */
261 /* Call the C interrupt handler */
262 movl $interrupt_handler, %eax
268 * Restore all registers, including those manipulated by the C
279 .globl asm_realmode_code_size
280 asm_realmode_code_size:
281 .long . - asm_realmode_code