1 /* SPDX-License-Identifier: GPL-2.0 */
3 * From coreboot x86_asm.S, cleaned up substantially
5 * Copyright (C) 2009-2010 coresystems GmbH
8 #include <asm/processor.h>
9 #include <asm/processor-flags.h>
12 #define SEG(segment) $segment * X86_GDT_ENTRY_SIZE
15 * This is the interrupt handler stub code. It gets copied to the IDT and
16 * to some fixed addresses in the F segment. Before the code can used,
17 * it gets patched up by the C function copying it: byte 3 (the $0 in
18 * movb $0, %al) is overwritten with the interrupt numbers.
25 movb $0, %al /* This instruction gets modified */
26 ljmp $0, $__interrupt_handler_16bit
27 .globl __idt_handler_size
29 .long . - __idt_handler
31 .macro setup_registers
32 /* initial register values */
34 movl %eax, __registers + 0 /* eax */
36 movl %eax, __registers + 4 /* ebx */
38 movl %eax, __registers + 8 /* ecx */
40 movl %eax, __registers + 12 /* edx */
42 movl %eax, __registers + 16 /* esi */
44 movl %eax, __registers + 20 /* edi */
47 .macro enter_real_mode
48 /* Activate the right segment descriptor real mode. */
49 ljmp SEG(X86_GDT_ENTRY_16BIT_CS), $PTR_TO_REAL_MODE(1f)
53 * Load the segment registers with properly configured segment
54 * descriptors. They will retain these configurations (limits,
55 * writability, etc.) once protected mode is turned off.
57 mov SEG(X86_GDT_ENTRY_16BIT_DS), %ax
64 /* Turn off protection */
66 andl $~X86_CR0_PE, %eax
69 /* Now really going into real mode */
70 ljmp $0, $PTR_TO_REAL_MODE(1f)
73 * Set up a stack: Put the stack at the end of page zero. That way
74 * we can easily share it between real and protected, since the
75 * 16-bit ESP at segment 0 will work for any case.
87 .macro prepare_for_irom
91 /* Initialise registers for option rom lcall */
92 movl __registers + 0, %eax
93 movl __registers + 4, %ebx
94 movl __registers + 8, %ecx
95 movl __registers + 12, %edx
96 movl __registers + 16, %esi
97 movl __registers + 20, %edi
99 /* Set all segments to 0x0000, ds to 0x0040 */
105 mov SEG(X86_GDT_ENTRY_16BIT_FLAT_DS), %ax
111 .macro enter_protected_mode
112 /* Go back to protected mode */
114 orl $X86_CR0_PE, %eax
117 /* Now that we are in protected mode jump to a 32 bit code segment */
118 data32 ljmp SEG(X86_GDT_ENTRY_32BIT_CS), $PTR_TO_REAL_MODE(1f)
121 mov SEG(X86_GDT_ENTRY_32BIT_DS), %ax
126 mov SEG(X86_GDT_ENTRY_32BIT_FS), %ax
129 /* restore proper idt */
134 * In order to be independent of U-Boot's position in RAM we relocate a part
135 * of the code to the first megabyte of RAM, so the CPU can use it in
136 * real-mode. This code lives at asm_realmode_code.
138 .globl asm_realmode_code
141 /* Realmode IDT pointer structure. */
142 __realmode_idt = PTR_TO_REAL_MODE(.)
143 .word 1023 /* 16 bit limit */
144 .long 0 /* 24 bit base */
147 /* Preserve old stack */
148 __stack = PTR_TO_REAL_MODE(.)
151 /* Register store for realmode_call and realmode_interrupt */
152 __registers = PTR_TO_REAL_MODE(.)
153 .long 0 /* 0 - EAX */
154 .long 0 /* 4 - EBX */
155 .long 0 /* 8 - ECX */
156 .long 0 /* 12 - EDX */
157 .long 0 /* 16 - ESI */
158 .long 0 /* 20 - EDI */
160 /* 256 byte buffer, used by int10 */
161 .globl asm_realmode_buffer
166 .globl asm_realmode_call
168 /* save all registers to the stack */
175 * This function is called with regparm=0 and we have to skip the
176 * 36 bytes from pushf+pusha. Hence start at 40.
177 * Set up our call instruction.
180 mov %ax, __lcall_instr + 1
181 andl $0xffff0000, %eax
183 mov %ax, __lcall_instr + 3
191 __lcall_instr = PTR_TO_REAL_MODE(.)
197 /* restore stack pointer, eflags and register values and exit */
203 .globl __realmode_interrupt
204 __realmode_interrupt:
205 /* save all registers to the stack and store the stack pointer */
212 * This function is called with regparm=0 and we have to skip the
213 * 36 bytes from pushf+pusha. Hence start at 40.
214 * Prepare interrupt calling code.
217 movb %al, __intXX_instr + 1 /* intno */
223 __intXX_instr = PTR_TO_REAL_MODE(.)
224 .byte 0xcd, 0x00 /* This becomes intXX */
228 /* restore stack pointer, eflags and register values and exit */
235 * This is the 16-bit interrupt entry point called by the IDT stub code.
237 * Before this code code is called, %eax is pushed to the stack, and the
238 * interrupt number is loaded into %al. On return this function cleans up
242 __interrupt_handler_16bit = PTR_TO_REAL_MODE(.)
248 /* Save real mode SS */
249 movw %ss, %cs:__realmode_ss
251 /* Clear DF to not break ABI assumptions */
255 * Clean up the interrupt number. We could do this in the stub, but
256 * it would cost two more bytes per stub entry.
259 pushl %eax /* ... and make it the first parameter */
264 * Now we are in protected mode. We need compute the right ESP based
265 * on saved real mode SS otherwise interrupt_handler() won't get
266 * correct parameters from the stack.
268 movzwl %cs:__realmode_ss, %ecx
272 /* Call the C interrupt handler */
273 movl $interrupt_handler, %eax
276 /* Restore real mode ESP based on saved SS */
277 movzwl %cs:__realmode_ss, %ecx
283 /* Restore real mode SS */
284 movw %cs:__realmode_ss, %ss
287 * Restore all registers, including those manipulated by the C
298 __realmode_ss = PTR_TO_REAL_MODE(.)
301 .globl asm_realmode_code_size
302 asm_realmode_code_size:
303 .long . - asm_realmode_code