1
// Rom layout and bios assembler to C interface.
3
// Copyright (C) 2008-2012 Kevin O'Connor <kevin@koconnor.net>
4
// Copyright (C) 2002 MandrakeSoft S.A.
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
8
#include "asm-offsets.h" // BREGS_*
9
#include "config.h" // CONFIG_*
10
#include "entryfuncs.S" // ENTRY_*
11
#include "hw/rtc.h" // CMOS_RESET_CODE
12
#include "x86.h" // CR0_*
17
/****************************************************************
18
* 16bit / 32bit call trampolines
19
****************************************************************/
21
// Place CPU into 32bit mode from 16bit mode.
22
// %edx = return location (in 32bit mode)
23
// Clobbers: ecx, flags, segment registers, cr0, idt/gdt
25
.global transition32_nmi_off
27
// Disable irqs (and clear direction flag)
33
movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
34
outb %al, $PORT_CMOS_INDEX
35
inb $PORT_CMOS_DATA, %al
39
orb $A20_ENABLE_BIT, %al
44
// Set segment descriptors
45
lidtw %cs:pmode_IDT_info
46
lgdtw %cs:rombios32_gdt_48
48
// Enable protected mode
50
andl $~(CR0_PG|CR0_CD|CR0_NW), %ecx
54
// start 32bit protected mode code
55
ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
59
1: movl $SEG32_MODE32_DS, %ecx
69
// Place CPU into 16bit mode from 32bit mode.
70
// %edx = return location (in 16bit mode)
71
// Clobbers: ecx, flags, segment registers, cr0, idt/gdt
73
.global transition16big
76
// Reset data segment limits
77
movl $SEG32_MODE16_DS, %ecx
85
ljmpw $SEG32_MODE16_CS, $1f
88
movl $SEG32_MODE16BIG_DS, %ecx
95
ljmpw $SEG32_MODE16BIG_CS, $1f
98
// Disable protected mode
103
// far jump to flush CPU queue after transition to real mode
106
// restore IDT to normal real-mode defaults
107
2: lidtw %cs:rmode_IDT_info
109
// Clear segment registers
115
movw %cx, %ss // Assume stack is in segment 0
120
/****************************************************************
121
* External calling trampolines
122
****************************************************************/
124
// Far call a 16bit function from 16bit mode with a specified cpu register state
125
// %eax = address of struct bregs, %edx = segment of struct bregs
126
// Clobbers: %e[bc]x, %e[ds]i, flags
129
// Save %edx/%eax, %ebp
134
// Setup for iretw call
137
pushw $1f // return point
138
pushw BREGS_flags(%eax) // flags
139
pushl BREGS_code(%eax) // CS:IP
141
// Load calling registers and invoke call
143
iretw // XXX - just do a lcalll
145
// Store flags, es, eax
152
movl 0x0c(%esp), %eax
154
popw BREGS_flags(%eax)
156
movw %cx, %ds // Restore %ds == %ss
158
// Remove %edx/%eax, restore %ebp
166
.macro IRQ_TRAMPOLINE num
167
DECLFUNC irq_trampoline_0x\num
168
irq_trampoline_0x\num :
186
/****************************************************************
187
* Misc. entry points.
188
****************************************************************/
190
// Entry point for QEMU smi interrupts.
193
// Transition to 32bit mode.
194
movl $1f + BUILD_BIOS_ADDR, %edx
195
jmp transition32_nmi_off
197
1: movl $BUILD_SMM_ADDR + 0x8000, %esp
198
calll _cfunc32flat_handle_smi - BUILD_BIOS_ADDR
202
// Entry point for QEMU smp sipi interrupts.
205
// Transition to 32bit mode.
208
movl $2f + BUILD_BIOS_ADDR, %edx
209
jmp transition32_nmi_off
211
// Acquire lock and take ownership of shared stack
213
2: lock btsl $0, SMPLock
217
calll _cfunc32flat_handle_smp - BUILD_BIOS_ADDR
218
// Release lock and halt processor.
224
// Resume (and reboot) entry point - called from entry_post
225
DECLFUNC entry_resume
227
// Disable interrupts
230
// Use the ExtraStack in low mem.
231
movl $_zonelow_seg, %eax
234
movl $ExtraStack + BUILD_EXTRA_STACK_SIZE, %esp
241
pushl %esp // Backup %esp, then clear high bits
243
pushfl // Save registers clobbered by C code
247
movl %ss, %ecx // Move %ss to %ds
250
movl $_cfunc32flat_handle_pmm, %eax // Setup: call32(handle_pmm, args, -1)
251
leal PUSHBREGS_size+12(%esp, %ecx), %edx // %edx points to start of args
254
movw %ax, BREGS_eax(%esp) // Modify %ax:%dx to return %eax
256
movw %ax, BREGS_edx(%esp)
263
DECLFUNC entry_pnp_real
264
.global entry_pnp_prot
269
pushl %esp // Backup %esp, then clear high bits
272
pushfl // Save registers clobbered by C code
276
movw %ss, %cx // Move %ss to %ds
278
leal PUSHBREGS_size+12(%esp), %eax // %eax points to start of u16 args
280
movw %ax, BREGS_eax(%esp) // Modify %eax to return %ax
292
addw $4, %sp // pop dummy
293
popfw // restore flags
301
pushl %cs // Move second descriptor after %cs to %gs
304
ENTRY_ARG_ESP _cfunc32seg_handle_apm
310
// PCI-BIOS entry points
311
DECLFUNC entry_pcibios32
315
pushl %gs // Backup %gs and set %gs=%ds
318
ENTRY_ARG_ESP _cfunc32seg_handle_pcibios
324
DECLFUNC entry_pcibios16
326
ENTRY_ARG handle_pcibios
329
// int 1589 entry point
332
ENTRY_ARG handle_1589
336
DECLFUNC entry_bios32
341
// Check for PCI-BIOS request
342
cmpl $0x49435024, %eax // $PCI
344
movl $BUILD_BIOS_ADDR, %ebx
345
movl $BUILD_BIOS_SIZE, %ecx
346
movl $entry_pcibios32, %edx
357
// 32bit elf entry point
363
movl %eax, entry_elf_eax
364
movl %ebx, entry_elf_ebx
365
lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
366
lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
367
movl $SEG32_MODE32_DS, %eax
373
movl $BUILD_STACK_ADDR, %esp
374
ljmpl $SEG32_MODE32_CS, $_cfunc32flat_handle_post
377
// UEFI Compatibility Support Module (CSM) entry point
380
// Backup register state
387
// Backup stack location and convert to a "flat pointer"
389
movw %ax, BREGS_code+2(%esp) // Store %ss in bregs->code.seg
393
// Change to BUILD_STACK_ADDR stack and call handle_csm(bregs)
394
ENTRY_INTO32 _cfunc32flat_handle_csm
396
DECLFUNC __csm_return
403
// Switch back to original stack
404
1: movzwl BREGS_code+2(%eax), %edx
411
// Restore register state and return.
413
addw $4, %sp // pop dummy
418
/****************************************************************
419
* Interrupt entry points
420
****************************************************************/
422
// Main entry point for hardware interrupts handled on extra stack
423
DECLFUNC irqentry_extrastack
427
pushw %ds // Set %ds:%eax to space on ExtraStack
429
movl $_zonelow_seg, %eax
432
subl $PUSHBREGS_size+8, %eax
435
movl %esp, PUSHBREGS_size(%eax)
436
movw %ss, PUSHBREGS_size+4(%eax)
438
movw %ds, %dx // Setup %ss/%esp and call function
443
movl %esp, %eax // Restore registers and return
444
movw PUSHBREGS_size+4(%eax), %ss
445
movl PUSHBREGS_size(%eax), %esp
449
// Main entry point for software interrupts handled on extra stack
450
DECLFUNC irqentry_arg_extrastack
451
irqentry_arg_extrastack:
454
pushw %ds // Set %ds:%eax to space on ExtraStack
456
movl $_zonelow_seg, %eax
459
subl $PUSHBREGS_size+16, %eax
460
SAVEBREGS_POP_DSEAX // Save registers on extra stack
462
movl %esp, PUSHBREGS_size+8(%eax)
463
movw %ss, PUSHBREGS_size+12(%eax)
464
popl BREGS_code(%eax)
465
popw BREGS_flags(%eax)
467
movw %ds, %dx // Setup %ss/%esp and call function
472
movl %esp, %eax // Restore registers and return
473
movw PUSHBREGS_size+12(%eax), %ss
474
movl PUSHBREGS_size+8(%eax), %esp
477
pushw BREGS_flags(%eax)
478
pushl BREGS_code(%eax)
482
// Main entry point for software interrupts (using caller's stack)
483
DECLFUNC irqentry_arg
488
// Helper macros for hardware interrupt declaration
493
jmp irqentry_extrastack
496
.macro DECL_IRQ_ENTRY num
501
// Helper macros for software interrupt declaration
502
.macro IRQ_ENTRY_ARG num
506
#if CONFIG_ENTRY_EXTRASTACK
507
jmp irqentry_arg_extrastack
513
.macro DECL_IRQ_ENTRY_ARG num
518
// Various entry points (that don't require a fixed location).
519
DECL_IRQ_ENTRY_ARG 13
524
DECL_IRQ_ENTRY hwpic1
525
DECL_IRQ_ENTRY hwpic2
527
// int 18/19 are special - they reset stack and call into 32bit mode.
530
ENTRY_INTO32 _cfunc32flat_handle_19
534
ENTRY_INTO32 _cfunc32flat_handle_18
537
/****************************************************************
538
* Fixed position entry points
539
****************************************************************/
541
// Specify a location in the fixed part of bios area.
543
.section .fixedaddr.\addr
548
cmpl $0, %cs:HaveRunPost // Check for resume/reboot
550
ENTRY_INTO32 _cfunc32flat_handle_post // Normal entry point
555
ENTRY handle_02 // NMI handler does not switch onto extra stack
559
.global entry_13_official
563
// 0xe401 - OldFDPT in misc.c
566
.global entry_19_official
570
// 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
572
// 0xe729 - BaudTable in misc.c
589
// 0xefc7 - diskette_param_table in misc.c
596
// XXX - INT 10 Functions 0-Fh Entry Point
602
// 0xf0a4 - VideoParams in misc.c
611
.global entry_15_official
614
je entry_1589 // 1589 calls return in protected mode
617
// 0xfa6e - vgafont8 in font.c
620
.global entry_1a_official
623
je entry_pcibios16 // PCIBIOS calls can be in protected mode
629
// 0xfef3 - InitVectors in misc.c
632
.global entry_iret_official
639
ORG 0xfff0 // Power-up Entry Point
642
ljmpw $SEG_BIOS, $entry_post
644
// 0xfff5 - BiosDate in misc.c
646
// 0xfffe - BiosModelId in misc.c
648
// 0xffff - BiosChecksum in misc.c