blob: c4a4635e83a5b4cba56ffc92d3f5509567094bf3 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Rom layout and bios assembler to C interface.
2//
Kevin O'Connor36feea92012-02-11 10:49:45 -05003// Copyright (C) 2008-2012 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor3dba4c22011-01-29 11:26:54 -05008#include "asm-offsets.h" // BREGS_*
Kevin O'Connor4ade5232013-09-18 21:41:48 -04009#include "config.h" // CONFIG_*
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040010#include "entryfuncs.S" // ENTRY_*
Kevin O'Connor4ade5232013-09-18 21:41:48 -040011#include "hw/rtc.h" // CMOS_RESET_CODE
12#include "x86.h" // CR0_*
Kevin O'Connor74534532008-05-12 18:28:58 -040013
Kevin O'Connor56b252e2014-12-03 12:22:14 -050014 .code16
15
Kevin O'Connor1d019512008-03-11 21:21:47 -040016
17/****************************************************************
Kevin O'Connor38aadba2013-09-29 19:25:44 -040018 * 16bit / 32bit call trampolines
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050019 ****************************************************************/
20
21// Place CPU into 32bit mode from 16bit mode.
Kevin O'Connor4057f982010-11-25 08:52:50 -050022// %edx = return location (in 32bit mode)
Kevin O'Connorad901592009-12-13 11:25:25 -050023// Clobbers: ecx, flags, segment registers, cr0, idt/gdt
Kevin O'Connord67a7032009-01-17 19:37:26 -050024 DECLFUNC transition32
Kevin O'Connor423542e2015-09-11 16:19:02 -040025 .global transition32_nmi_off
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050026transition32:
Kevin O'Connorf094ba82009-04-13 19:30:27 -040027 // Disable irqs (and clear direction flag)
28 cli
29 cld
30
31 // Disable nmi
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040032 movl %eax, %ecx
Kevin O'Connorf094ba82009-04-13 19:30:27 -040033 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
34 outb %al, $PORT_CMOS_INDEX
35 inb $PORT_CMOS_DATA, %al
36
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037 // enable a20
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -050038 inb $PORT_A20, %al
39 orb $A20_ENABLE_BIT, %al
40 outb %al, $PORT_A20
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040041 movl %ecx, %eax
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050042
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040043transition32_nmi_off:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050044 // Set segment descriptors
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040045 lidtw %cs:pmode_IDT_info
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050046 lgdtw %cs:rombios32_gdt_48
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050047
Kevin O'Connor3eac0092008-11-16 09:59:32 -050048 // Enable protected mode
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040049 movl %cr0, %ecx
Kevin O'Connor62de31b2015-09-22 12:35:00 -040050 andl $~(CR0_PG|CR0_CD|CR0_NW), %ecx
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040051 orl $CR0_PE, %ecx
52 movl %ecx, %cr0
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050053
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050054 // start 32bit protected mode code
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040055 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050056
57 .code32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050058 // init data segments
Kevin O'Connor1fb9e082015-09-03 12:23:53 -0400591: movl $SEG32_MODE32_DS, %ecx
60 movw %cx, %ds
61 movw %cx, %es
62 movw %cx, %ss
63 movw %cx, %fs
64 movw %cx, %gs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050065
Kevin O'Connor4057f982010-11-25 08:52:50 -050066 jmpl *%edx
Kevin O'Connor56b252e2014-12-03 12:22:14 -050067 .code16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050068
Kevin O'Connor752151b2009-12-10 21:16:07 -050069// Place CPU into 16bit mode from 32bit mode.
Kevin O'Connor4057f982010-11-25 08:52:50 -050070// %edx = return location (in 16bit mode)
Kevin O'Connorad901592009-12-13 11:25:25 -050071// Clobbers: ecx, flags, segment registers, cr0, idt/gdt
Kevin O'Connor752151b2009-12-10 21:16:07 -050072 DECLFUNC transition16
73 .global transition16big
Kevin O'Connor56b252e2014-12-03 12:22:14 -050074 .code32
Kevin O'Connor752151b2009-12-10 21:16:07 -050075transition16:
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040076 // Reset data segment limits
77 movl $SEG32_MODE16_DS, %ecx
78 movw %cx, %ds
79 movw %cx, %es
80 movw %cx, %ss
81 movw %cx, %fs
82 movw %cx, %gs
83
Kevin O'Connor1a72e2e2008-11-11 22:03:55 -050084 // Jump to 16bit mode
85 ljmpw $SEG32_MODE16_CS, $1f
86
Kevin O'Connor752151b2009-12-10 21:16:07 -050087transition16big:
Kevin O'Connor1fb9e082015-09-03 12:23:53 -040088 movl $SEG32_MODE16BIG_DS, %ecx
89 movw %cx, %ds
90 movw %cx, %es
91 movw %cx, %ss
92 movw %cx, %fs
93 movw %cx, %gs
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050094
Kevin O'Connor0f788892010-07-25 14:04:01 -040095 ljmpw $SEG32_MODE16BIG_CS, $1f
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050096
David Woodhouse5b22d932014-06-03 17:28:49 +010097 .code16
Kevin O'Connor3eac0092008-11-16 09:59:32 -050098 // Disable protected mode
Kevin O'Connor1fb9e082015-09-03 12:23:53 -0400991: movl %cr0, %ecx
100 andl $~CR0_PE, %ecx
101 movl %ecx, %cr0
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500102
103 // far jump to flush CPU queue after transition to real mode
Kevin O'Connore3677b12008-07-04 15:29:23 -0400104 ljmpw $SEG_BIOS, $2f
Kevin O'Connor21e930b2008-03-01 09:49:37 -0500105
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500106 // restore IDT to normal real-mode defaults
Kevin O'Connor1fb9e082015-09-03 12:23:53 -04001072: lidtw %cs:rmode_IDT_info
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500108
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500109 // Clear segment registers
Kevin O'Connor1fb9e082015-09-03 12:23:53 -0400110 xorw %cx, %cx
111 movw %cx, %fs
112 movw %cx, %gs
113 movw %cx, %es
114 movw %cx, %ds
115 movw %cx, %ss // Assume stack is in segment 0
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500116
Kevin O'Connor4057f982010-11-25 08:52:50 -0500117 jmpl *%edx
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500118
Kevin O'Connor38aadba2013-09-29 19:25:44 -0400119
120/****************************************************************
121 * External calling trampolines
122 ****************************************************************/
123
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400124// Far call a 16bit function from 16bit mode with a specified cpu register state
Kevin O'Connor2d977562013-09-29 20:21:40 -0400125// %eax = address of struct bregs, %edx = segment of struct bregs
126// Clobbers: %e[bc]x, %e[ds]i, flags
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400127 DECLFUNC __farcall16
128__farcall16:
Kevin O'Connor2d977562013-09-29 20:21:40 -0400129 // Save %edx/%eax, %ebp
Kevin O'Connor9caf7862009-02-27 20:14:05 -0500130 pushl %ebp
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500131 pushl %eax
Kevin O'Connor2d977562013-09-29 20:21:40 -0400132 pushl %edx
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500133
134 // Setup for iretw call
Kevin O'Connor2d977562013-09-29 20:21:40 -0400135 movl %edx, %ds
Kevin O'Connor273e8ae2009-01-19 19:56:07 -0500136 pushw %cs
Kevin O'Connorbeeabd62012-05-28 13:59:07 -0400137 pushw $1f // return point
Kevin O'Connor2d977562013-09-29 20:21:40 -0400138 pushw BREGS_flags(%eax) // flags
139 pushl BREGS_code(%eax) // CS:IP
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500140
Kevin O'Connor46000f52014-10-21 02:23:02 -0400141 // Load calling registers and invoke call
142 RESTOREBREGS_DSEAX
Kevin O'Connorbeeabd62012-05-28 13:59:07 -0400143 iretw // XXX - just do a lcalll
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -05001441:
Kevin O'Connorbeeabd62012-05-28 13:59:07 -0400145 // Store flags, es, eax
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500146 pushfw
Kevin O'Connorbeeabd62012-05-28 13:59:07 -0400147 cli
148 cld
Kevin O'Connor2d977562013-09-29 20:21:40 -0400149 pushw %ds
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500150 pushl %eax
Kevin O'Connor2d977562013-09-29 20:21:40 -0400151 movw 0x08(%esp), %ds
Kevin O'Connorbeeabd62012-05-28 13:59:07 -0400152 movl 0x0c(%esp), %eax
Kevin O'Connor46000f52014-10-21 02:23:02 -0400153 SAVEBREGS_POP_DSEAX
Kevin O'Connor2d977562013-09-29 20:21:40 -0400154 popw BREGS_flags(%eax)
Kevin O'Connorbeeabd62012-05-28 13:59:07 -0400155 movw %ss, %cx
156 movw %cx, %ds // Restore %ds == %ss
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500157
Kevin O'Connor2d977562013-09-29 20:21:40 -0400158 // Remove %edx/%eax, restore %ebp
159 popl %edx
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500160 popl %eax
Kevin O'Connor9caf7862009-02-27 20:14:05 -0500161 popl %ebp
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162
163 retl
164
Kevin O'Connor79dde652009-04-13 19:26:43 -0400165// IRQ trampolines
166 .macro IRQ_TRAMPOLINE num
167 DECLFUNC irq_trampoline_0x\num
168 irq_trampoline_0x\num :
169 int $0x\num
170 lretw
171 .endm
172
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400173 IRQ_TRAMPOLINE 02
Kevin O'Connor1bdc9ae2016-09-05 13:38:26 -0400174 IRQ_TRAMPOLINE 05
Kevin O'Connor79dde652009-04-13 19:26:43 -0400175 IRQ_TRAMPOLINE 10
176 IRQ_TRAMPOLINE 13
177 IRQ_TRAMPOLINE 15
178 IRQ_TRAMPOLINE 16
179 IRQ_TRAMPOLINE 18
180 IRQ_TRAMPOLINE 19
Kevin O'Connor1bdc9ae2016-09-05 13:38:26 -0400181 IRQ_TRAMPOLINE 1b
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400182 IRQ_TRAMPOLINE 1c
183 IRQ_TRAMPOLINE 4a
Kevin O'Connor79dde652009-04-13 19:26:43 -0400184
185
186/****************************************************************
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400187 * Misc. entry points.
Kevin O'Connor79dde652009-04-13 19:26:43 -0400188 ****************************************************************/
189
Kevin O'Connorf4c511c2014-04-07 19:49:12 -0400190// Entry point for QEMU smi interrupts.
191 DECLFUNC entry_smi
192entry_smi:
193 // Transition to 32bit mode.
194 movl $1f + BUILD_BIOS_ADDR, %edx
Kevin O'Connor5ae3dd62015-03-11 17:45:47 -0400195 jmp transition32_nmi_off
Kevin O'Connorf4c511c2014-04-07 19:49:12 -0400196 .code32
Kevin O'Connor31bcda22014-05-28 13:33:50 -04001971: movl $BUILD_SMM_ADDR + 0x8000, %esp
Kevin O'Connorf4c511c2014-04-07 19:49:12 -0400198 calll _cfunc32flat_handle_smi - BUILD_BIOS_ADDR
199 rsm
David Woodhouse5b22d932014-06-03 17:28:49 +0100200 .code16
Kevin O'Connorf4c511c2014-04-07 19:49:12 -0400201
Kevin O'Connor0673b782014-05-24 10:49:50 -0400202// Entry point for QEMU smp sipi interrupts.
203 DECLFUNC entry_smp
204entry_smp:
205 // Transition to 32bit mode.
Kevin O'Connor5ae3dd62015-03-11 17:45:47 -0400206 cli
207 cld
Kevin O'Connor0673b782014-05-24 10:49:50 -0400208 movl $2f + BUILD_BIOS_ADDR, %edx
Kevin O'Connor5ae3dd62015-03-11 17:45:47 -0400209 jmp transition32_nmi_off
Kevin O'Connor0673b782014-05-24 10:49:50 -0400210 .code32
211 // Acquire lock and take ownership of shared stack
Ian Campbell68354a62014-06-30 14:10:02 +01002121: rep ; nop
Kevin O'Connor0673b782014-05-24 10:49:50 -04002132: lock btsl $0, SMPLock
214 jc 1b
215 movl SMPStack, %esp
216 // Call handle_smp
217 calll _cfunc32flat_handle_smp - BUILD_BIOS_ADDR
218 // Release lock and halt processor.
219 movl $0, SMPLock
2203: hlt
221 jmp 3b
David Woodhouse5b22d932014-06-03 17:28:49 +0100222 .code16
Kevin O'Connor0673b782014-05-24 10:49:50 -0400223
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400224// Resume (and reboot) entry point - called from entry_post
225 DECLFUNC entry_resume
226entry_resume:
Kevin O'Connor79dde652009-04-13 19:26:43 -0400227 // Disable interrupts
228 cli
229 cld
Kevin O'Connor46b82622012-05-13 12:10:30 -0400230 // Use the ExtraStack in low mem.
Kevin O'Connorc9243442013-02-17 13:58:28 -0500231 movl $_zonelow_seg, %eax
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400232 movw %ax, %ds
Kevin O'Connor79dde652009-04-13 19:26:43 -0400233 movw %ax, %ss
Kevin O'Connor46b82622012-05-13 12:10:30 -0400234 movl $ExtraStack + BUILD_EXTRA_STACK_SIZE, %esp
Kevin O'Connor79dde652009-04-13 19:26:43 -0400235 // Call handler.
Kevin O'Connor79dde652009-04-13 19:26:43 -0400236 jmp handle_resume
237
Kevin O'Connore54ee382009-07-26 19:33:13 -0400238// PMM entry point
239 DECLFUNC entry_pmm
240entry_pmm:
241 pushl %esp // Backup %esp, then clear high bits
242 movzwl %sp, %esp
243 pushfl // Save registers clobbered by C code
244 cli
245 cld
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400246 PUSHBREGS
Kevin O'Connord5c61742014-09-30 09:37:26 -0400247 movl %ss, %ecx // Move %ss to %ds
Kevin O'Connore54ee382009-07-26 19:33:13 -0400248 movw %cx, %ds
Kevin O'Connord5c61742014-09-30 09:37:26 -0400249 shll $4, %ecx
Kevin O'Connor533b6282011-07-16 13:13:12 -0400250 movl $_cfunc32flat_handle_pmm, %eax // Setup: call32(handle_pmm, args, -1)
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500251 leal PUSHBREGS_size+12(%esp, %ecx), %edx // %edx points to start of args
Kevin O'Connor533b6282011-07-16 13:13:12 -0400252 movl $-1, %ecx
Kevin O'Connorb4cca862015-10-09 11:53:02 -0400253 calll __call32
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400254 movw %ax, BREGS_eax(%esp) // Modify %ax:%dx to return %eax
Kevin O'Connore54ee382009-07-26 19:33:13 -0400255 shrl $16, %eax
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400256 movw %ax, BREGS_edx(%esp)
257 POPBREGS
Kevin O'Connore54ee382009-07-26 19:33:13 -0400258 popfl
259 popl %esp
260 lretw
261
Kevin O'Connor79dde652009-04-13 19:26:43 -0400262// PnP entry points
Kevin O'Connord67a7032009-01-17 19:37:26 -0500263 DECLFUNC entry_pnp_real
264 .global entry_pnp_prot
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500265entry_pnp_prot:
266 pushl %esp
267 jmp 1f
268entry_pnp_real:
269 pushl %esp // Backup %esp, then clear high bits
270 movzwl %sp, %esp
2711:
272 pushfl // Save registers clobbered by C code
Kevin O'Connore54ee382009-07-26 19:33:13 -0400273 cli
274 cld
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400275 PUSHBREGS
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500276 movw %ss, %cx // Move %ss to %ds
277 movw %cx, %ds
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500278 leal PUSHBREGS_size+12(%esp), %eax // %eax points to start of u16 args
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500279 calll handle_pnp
Kevin O'Connor85230162014-11-03 09:45:51 -0500280 movw %ax, BREGS_eax(%esp) // Modify %eax to return %ax
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400281 POPBREGS
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500282 popfl
283 popl %esp
284 lretw
285
Kevin O'Connor79dde652009-04-13 19:26:43 -0400286// APM entry points
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400287 DECLFUNC entry_apm16
288entry_apm16:
Kevin O'Connor1d019512008-03-11 21:21:47 -0400289 pushfw // save flags
290 pushl %eax // dummy
Kevin O'Connord3e43672012-05-28 11:37:53 -0400291 ENTRY_ARG handle_apm
Kevin O'Connorb3c28be2008-06-08 13:34:43 -0400292 addw $4, %sp // pop dummy
Kevin O'Connor1d019512008-03-11 21:21:47 -0400293 popfw // restore flags
294 lretw
295
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400296 DECLFUNC entry_apm32
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500297 .code32
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400298entry_apm32:
Kevin O'Connorc0031482010-01-01 13:03:17 -0500299 pushfl
300 pushl %gs
301 pushl %cs // Move second descriptor after %cs to %gs
302 addl $16, (%esp)
303 popl %gs
Kevin O'Connord3e43672012-05-28 11:37:53 -0400304 ENTRY_ARG_ESP _cfunc32seg_handle_apm
Kevin O'Connorc0031482010-01-01 13:03:17 -0500305 popl %gs
306 popfl
Kevin O'Connor1d019512008-03-11 21:21:47 -0400307 lretl
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500308 .code16
Kevin O'Connoree4f9ff2008-07-05 21:19:10 -0400309
Kevin O'Connor922aa1b2013-03-02 04:02:11 -0500310// PCI-BIOS entry points
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400311 DECLFUNC entry_pcibios32
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500312 .code32
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400313entry_pcibios32:
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500314 pushfl
Kevin O'Connorc0031482010-01-01 13:03:17 -0500315 pushl %gs // Backup %gs and set %gs=%ds
316 pushl %ds
317 popl %gs
Kevin O'Connor922aa1b2013-03-02 04:02:11 -0500318 ENTRY_ARG_ESP _cfunc32seg_handle_pcibios
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500319 popl %gs
320 popfl
321 lretl
David Woodhouse5b22d932014-06-03 17:28:49 +0100322 .code16
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500323
Kevin O'Connor922aa1b2013-03-02 04:02:11 -0500324 DECLFUNC entry_pcibios16
325entry_pcibios16:
326 ENTRY_ARG handle_pcibios
327 iretw
328
Kevin O'Connor3fcabf02014-05-08 18:32:32 -0400329// int 1589 entry point
330 DECLFUNC entry_1589
331entry_1589:
332 ENTRY_ARG handle_1589
333 iretw
334
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500335// BIOS32 support
Kevin O'Connor3e86d6b2013-02-18 21:50:57 -0500336 DECLFUNC entry_bios32
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500337 .code32
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400338entry_bios32:
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500339 pushfl
340#if CONFIG_PCIBIOS
341 // Check for PCI-BIOS request
342 cmpl $0x49435024, %eax // $PCI
343 jne 1f
344 movl $BUILD_BIOS_ADDR, %ebx
345 movl $BUILD_BIOS_SIZE, %ecx
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400346 movl $entry_pcibios32, %edx
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500347 xorb %al, %al
348 jmp 2f
349#endif
350 // Unknown request
3511: movb $0x80, %al
352 // Return to caller
3532: popfl
354 lretl
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500355 .code16
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500356
Kevin O'Connoree4f9ff2008-07-05 21:19:10 -0400357// 32bit elf entry point
Kevin O'Connoree952532014-06-09 14:37:23 -0400358 DECLFUNC entry_elf
Kevin O'Connor56b252e2014-12-03 12:22:14 -0500359 .code32
Kevin O'Connor47c8e312011-07-10 22:57:32 -0400360entry_elf:
Kevin O'Connoree4f9ff2008-07-05 21:19:10 -0400361 cli
362 cld
Vladimir Serbinenko1b911d72015-05-18 19:07:16 +0200363 movl %eax, entry_elf_eax
364 movl %ebx, entry_elf_ebx
Kevin O'Connoree4f9ff2008-07-05 21:19:10 -0400365 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
366 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
Kevin O'Connore0504b02009-04-13 19:32:51 -0400367 movl $SEG32_MODE32_DS, %eax
368 movw %ax, %ds
369 movw %ax, %es
370 movw %ax, %fs
371 movw %ax, %gs
372 movw %ax, %ss
Kevin O'Connoree4f9ff2008-07-05 21:19:10 -0400373 movl $BUILD_STACK_ADDR, %esp
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500374 ljmpl $SEG32_MODE32_CS, $_cfunc32flat_handle_post
David Woodhouse5b22d932014-06-03 17:28:49 +0100375 .code16
Kevin O'Connor1d019512008-03-11 21:21:47 -0400376
Kevin O'Connorbbd04582013-02-18 21:57:26 -0500377// UEFI Compatibility Support Module (CSM) entry point
Kevin O'Connoree952532014-06-09 14:37:23 -0400378 DECLFUNC entry_csm
David Woodhouse118469a2013-01-25 19:46:25 -0600379entry_csm:
380 // Backup register state
381 pushfw
382 cli
383 cld
384 pushl %eax // dummy
385 PUSHBREGS
386
387 // Backup stack location and convert to a "flat pointer"
David Woodhouse4b1d2be2013-02-10 00:51:56 +0000388 movl %ss, %eax
389 movw %ax, BREGS_code+2(%esp) // Store %ss in bregs->code.seg
390 shll $4, %eax
391 addl %esp, %eax
David Woodhouse118469a2013-01-25 19:46:25 -0600392
Kevin O'Connor283ae1f2014-10-21 03:19:55 -0400393 // Change to BUILD_STACK_ADDR stack and call handle_csm(bregs)
394 ENTRY_INTO32 _cfunc32flat_handle_csm
David Woodhouse118469a2013-01-25 19:46:25 -0600395
Kevin O'Connor3e86d6b2013-02-18 21:50:57 -0500396 DECLFUNC __csm_return
Kevin O'Connorbbd04582013-02-18 21:57:26 -0500397 .code32
David Woodhouse4b1d2be2013-02-10 00:51:56 +0000398__csm_return:
Kevin O'Connorbbd04582013-02-18 21:57:26 -0500399 movl $1f, %edx
David Woodhouse118469a2013-01-25 19:46:25 -0600400 jmp transition16big
David Woodhouse5b22d932014-06-03 17:28:49 +0100401 .code16
David Woodhouse118469a2013-01-25 19:46:25 -0600402
403 // Switch back to original stack
Kevin O'Connorbbd04582013-02-18 21:57:26 -05004041: movzwl BREGS_code+2(%eax), %edx
405 movl %edx, %ecx
David Woodhouse118469a2013-01-25 19:46:25 -0600406 shll $4, %ecx
Kevin O'Connorbbd04582013-02-18 21:57:26 -0500407 subl %ecx, %eax
408 movl %edx, %ss
409 movl %eax, %esp
David Woodhouse118469a2013-01-25 19:46:25 -0600410
411 // Restore register state and return.
412 POPBREGS
413 addw $4, %sp // pop dummy
414 popfw
415 lretw
416
Gerd Hoffmannd6728f32017-09-18 10:47:23 +0200417// Serial console "hooked vga" entry point
418 DECLFUNC entry_sercon
419entry_sercon:
420 // Setup for chain loading to real vga handler
421 pushfw
422 pushl %cs:sercon_real_vga_handler
423
424 // Set %ds to varlow segment
425 cli
426 cld
427 pushw %ds
428 pushl %eax
429 movl $_zonelow_seg, %eax
430 movl %eax, %ds
431
432 // Test if the sercon handler can be called
433 movl %esp, %eax // Test for broken x86emu
434 pushl $1f
435 retl
4361: cmpl %esp, %eax
437 jne 4f
438 cmpb $0, sercon_enable // Test that sercon is enabled
439 je 3f
440
441 // call handle_sercon
442 popl %eax
443 popw %ds
4442: pushl $handle_sercon
445#if CONFIG_ENTRY_EXTRASTACK
446 jmp irqentry_arg_extrastack
447#else
448 jmp irqentry_arg
449#endif
450
451 // sercon disabled - check for legacy text modeset and otherwise exit
4523: popl %eax
453 popw %ds
454 cmpw $0x0007, %ax
455 jle 2b
456 iretw
457
458 // Running on broken x86emu - restore stack and exit
4594: movl %eax, %esp
460 popl %eax
461 popw %ds
462 iretw
463
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500464
465/****************************************************************
Kevin O'Connorc5b50362008-12-18 21:56:41 -0500466 * Interrupt entry points
467 ****************************************************************/
468
Kevin O'Connorb0d42bd2014-11-05 12:04:53 -0500469 // Main entry point for hardware interrupts handled on extra stack
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400470 DECLFUNC irqentry_extrastack
471irqentry_extrastack:
472 cli
473 cld
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400474 pushw %ds // Set %ds:%eax to space on ExtraStack
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400475 pushl %eax
Kevin O'Connorc9243442013-02-17 13:58:28 -0500476 movl $_zonelow_seg, %eax
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400477 movl %eax, %ds
478 movl StackPos, %eax
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500479 subl $PUSHBREGS_size+8, %eax
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400480 SAVEBREGS_POP_DSEAX
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400481 popl %ecx
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500482 movl %esp, PUSHBREGS_size(%eax)
483 movw %ss, PUSHBREGS_size+4(%eax)
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400484
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400485 movw %ds, %dx // Setup %ss/%esp and call function
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400486 movw %dx, %ss
487 movl %eax, %esp
488 calll *%ecx
489
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400490 movl %esp, %eax // Restore registers and return
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500491 movw PUSHBREGS_size+4(%eax), %ss
492 movl PUSHBREGS_size(%eax), %esp
Kevin O'Connor99cb8f32014-10-21 14:34:06 -0400493 RESTOREBREGS_DSEAX
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400494 iretw
495
Kevin O'Connorb0d42bd2014-11-05 12:04:53 -0500496 // Main entry point for software interrupts handled on extra stack
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500497 DECLFUNC irqentry_arg_extrastack
498irqentry_arg_extrastack:
499 cli
500 cld
501 pushw %ds // Set %ds:%eax to space on ExtraStack
502 pushl %eax
503 movl $_zonelow_seg, %eax
504 movl %eax, %ds
505 movl StackPos, %eax
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500506 subl $PUSHBREGS_size+16, %eax
Kevin O'Connor46000f52014-10-21 02:23:02 -0400507 SAVEBREGS_POP_DSEAX // Save registers on extra stack
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500508 popl %ecx
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500509 movl %esp, PUSHBREGS_size+8(%eax)
510 movw %ss, PUSHBREGS_size+12(%eax)
Kevin O'Connorfeb02842013-12-09 20:30:30 -0500511 popl BREGS_code(%eax)
512 popw BREGS_flags(%eax)
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500513
514 movw %ds, %dx // Setup %ss/%esp and call function
515 movw %dx, %ss
516 movl %eax, %esp
517 calll *%ecx
518
519 movl %esp, %eax // Restore registers and return
Kevin O'Connoraa66d652014-11-05 09:05:36 -0500520 movw PUSHBREGS_size+12(%eax), %ss
521 movl PUSHBREGS_size+8(%eax), %esp
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500522 popl %edx
523 popw %dx
524 pushw BREGS_flags(%eax)
525 pushl BREGS_code(%eax)
Kevin O'Connor46000f52014-10-21 02:23:02 -0400526 RESTOREBREGS_DSEAX
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500527 iretw
528
Kevin O'Connorb0d42bd2014-11-05 12:04:53 -0500529 // Main entry point for software interrupts (using caller's stack)
530 DECLFUNC irqentry_arg
531irqentry_arg:
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400532 ENTRY_ARG_ST
533 iretw
534
Kevin O'Connorb0d42bd2014-11-05 12:04:53 -0500535 // Helper macros for hardware interrupt declaration
Kevin O'Connor812478e2011-01-22 10:53:48 -0500536 .macro IRQ_ENTRY num
537 .global entry_\num
538 entry_\num :
539 pushl $ handle_\num
Kevin O'Connorecdc6552012-05-28 14:25:15 -0400540 jmp irqentry_extrastack
Kevin O'Connor812478e2011-01-22 10:53:48 -0500541 .endm
542
543 .macro DECL_IRQ_ENTRY num
544 DECLFUNC entry_\num
545 IRQ_ENTRY \num
546 .endm
547
Kevin O'Connorb0d42bd2014-11-05 12:04:53 -0500548 // Helper macros for software interrupt declaration
Kevin O'Connor812478e2011-01-22 10:53:48 -0500549 .macro IRQ_ENTRY_ARG num
550 .global entry_\num
551 entry_\num :
552 pushl $ handle_\num
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500553#if CONFIG_ENTRY_EXTRASTACK
554 jmp irqentry_arg_extrastack
555#else
Kevin O'Connorb0d42bd2014-11-05 12:04:53 -0500556 jmp irqentry_arg
Kevin O'Connorf0d75a02013-03-02 18:14:35 -0500557#endif
Kevin O'Connor812478e2011-01-22 10:53:48 -0500558 .endm
559
560 .macro DECL_IRQ_ENTRY_ARG num
561 DECLFUNC entry_\num
562 IRQ_ENTRY_ARG \num
563 .endm
564
565 // Various entry points (that don't require a fixed location).
Kevin O'Connord67a7032009-01-17 19:37:26 -0500566 DECL_IRQ_ENTRY_ARG 13
Kevin O'Connord67a7032009-01-17 19:37:26 -0500567 DECL_IRQ_ENTRY 76
Kevin O'Connord67a7032009-01-17 19:37:26 -0500568 DECL_IRQ_ENTRY 70
569 DECL_IRQ_ENTRY 74
570 DECL_IRQ_ENTRY 75
571 DECL_IRQ_ENTRY hwpic1
572 DECL_IRQ_ENTRY hwpic2
573
Kevin O'Connor4ebc0b72009-03-01 12:31:57 -0500574 // int 18/19 are special - they reset stack and call into 32bit mode.
Kevin O'Connord67a7032009-01-17 19:37:26 -0500575 DECLFUNC entry_19
576entry_19:
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -0500577 ENTRY_INTO32 _cfunc32flat_handle_19
Kevin O'Connord67a7032009-01-17 19:37:26 -0500578
579 DECLFUNC entry_18
580entry_18:
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -0500581 ENTRY_INTO32 _cfunc32flat_handle_18
Kevin O'Connord67a7032009-01-17 19:37:26 -0500582
583
584/****************************************************************
585 * Fixed position entry points
586 ****************************************************************/
587
588 // Specify a location in the fixed part of bios area.
589 .macro ORG addr
590 .section .fixedaddr.\addr
591 .endm
592
593 ORG 0xe05b
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400594entry_post:
595 cmpl $0, %cs:HaveRunPost // Check for resume/reboot
596 jnz entry_resume
597 ENTRY_INTO32 _cfunc32flat_handle_post // Normal entry point
Kevin O'Connord67a7032009-01-17 19:37:26 -0500598
Kevin O'Connorc5b50362008-12-18 21:56:41 -0500599 ORG 0xe2c3
Kevin O'Connor0c5a6072015-12-23 15:33:02 -0500600 .global entry_02
601entry_02:
602 ENTRY handle_02 // NMI handler does not switch onto extra stack
603 iretw
Kevin O'Connorc5b50362008-12-18 21:56:41 -0500604
Kevin O'Connor74534532008-05-12 18:28:58 -0400605 ORG 0xe3fe
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500606 .global entry_13_official
607entry_13_official:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500608 jmp entry_13
609
Kevin O'Connorca34ce52014-05-24 10:40:35 -0400610 // 0xe401 - OldFDPT in misc.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500611
Kevin O'Connor74534532008-05-12 18:28:58 -0400612 ORG 0xe6f2
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500613 .global entry_19_official
614entry_19_official:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500615 jmp entry_19
616
Kevin O'Connor30853762009-01-17 18:49:20 -0500617 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500618
Kevin O'Connorca34ce52014-05-24 10:40:35 -0400619 // 0xe729 - BaudTable in misc.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500620
Kevin O'Connor74534532008-05-12 18:28:58 -0400621 ORG 0xe739
Kevin O'Connored128492008-03-11 11:14:59 -0400622 IRQ_ENTRY_ARG 14
623
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500624 ORG 0xe82e
625 IRQ_ENTRY_ARG 16
626
627 ORG 0xe987
628 IRQ_ENTRY 09
629
630 ORG 0xec59
631 IRQ_ENTRY_ARG 40
632
633 ORG 0xef57
634 IRQ_ENTRY 0e
635
Kevin O'Connorca34ce52014-05-24 10:40:35 -0400636 // 0xefc7 - diskette_param_table in misc.c
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500637
638 ORG 0xefd2
639 IRQ_ENTRY_ARG 17
640
641 ORG 0xf045
Kevin O'Connord67a7032009-01-17 19:37:26 -0500642entry_10_0x0f:
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500643 // XXX - INT 10 Functions 0-Fh Entry Point
644 iretw
645
646 ORG 0xf065
Gerd Hoffmannd6728f32017-09-18 10:47:23 +0200647entry_10:
648 iretw
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500649
Kevin O'Connor30853762009-01-17 18:49:20 -0500650 // 0xf0a4 - VideoParams in misc.c
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500651
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500652 ORG 0xf841
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400653 IRQ_ENTRY_ARG 12
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500654
655 ORG 0xf84d
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400656 IRQ_ENTRY_ARG 11
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500657
658 ORG 0xf859
Kevin O'Connor3fcabf02014-05-08 18:32:32 -0400659 .global entry_15_official
660entry_15_official:
661 cmpb $0x89, %ah
662 je entry_1589 // 1589 calls return in protected mode
Kevin O'Connorb4f0e892008-12-13 18:33:05 -0500663 IRQ_ENTRY_ARG 15
664
Kevin O'Connor30853762009-01-17 18:49:20 -0500665 // 0xfa6e - vgafont8 in font.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500666
Kevin O'Connor74534532008-05-12 18:28:58 -0400667 ORG 0xfe6e
Kevin O'Connor922aa1b2013-03-02 04:02:11 -0500668 .global entry_1a_official
669entry_1a_official:
670 cmpb $0xb1, %ah
671 je entry_pcibios16 // PCIBIOS calls can be in protected mode
Kevin O'Connored128492008-03-11 11:14:59 -0400672 IRQ_ENTRY_ARG 1a
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500673
Kevin O'Connor74534532008-05-12 18:28:58 -0400674 ORG 0xfea5
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500675 IRQ_ENTRY 08
676
Kevin O'Connor30853762009-01-17 18:49:20 -0500677 // 0xfef3 - InitVectors in misc.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500678
Kevin O'Connor74534532008-05-12 18:28:58 -0400679 ORG 0xff53
Kevin O'Connord67a7032009-01-17 19:37:26 -0500680 .global entry_iret_official
681entry_iret_official:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500682 iretw
683
Kevin O'Connor74534532008-05-12 18:28:58 -0400684 ORG 0xff54
Kevin O'Connored128492008-03-11 11:14:59 -0400685 IRQ_ENTRY_ARG 05
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500686
Kevin O'Connor74534532008-05-12 18:28:58 -0400687 ORG 0xfff0 // Power-up Entry Point
Kevin O'Connor3f168b62008-11-29 13:22:29 -0500688 .global reset_vector
689reset_vector:
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400690 ljmpw $SEG_BIOS, $entry_post
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500691
Kevin O'Connor30853762009-01-17 18:49:20 -0500692 // 0xfff5 - BiosDate in misc.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500693
Kevin O'Connor30853762009-01-17 18:49:20 -0500694 // 0xfffe - BiosModelId in misc.c
Kevin O'Connore3677b12008-07-04 15:29:23 -0400695
Kevin O'Connor30853762009-01-17 18:49:20 -0500696 // 0xffff - BiosChecksum in misc.c
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500697
698 .end