1 // Rom layout and bios assembler to C interface.
3 // Copyright (C) 2008,2009 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.
9 /****************************************************************
10 * Include of 16bit C code
11 ****************************************************************/
16 #include "config.h" // CONFIG_*
17 #include "ioport.h" // PORT_A20
18 #include "bregs.h" // CR0_*
19 #include "cmos.h" // CMOS_RESET_CODE
20 #include "asm-offsets.h" // BREGS_*
21 #include "entryfuncs.S" // ENTRY_*
24 /****************************************************************
26 ****************************************************************/
28 // Place CPU into 32bit mode from 16bit mode.
29 // %edx = return location (in 32bit mode)
30 // Clobbers: ecx, flags, segment registers, cr0, idt/gdt
35 // Disable irqs (and clear direction flag)
40 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
41 outb %al, $PORT_CMOS_INDEX
42 inb $PORT_CMOS_DATA, %al
46 orb $A20_ENABLE_BIT, %al
49 // Set segment descriptors
50 lidtw %cs:pmode_IDT_info
51 lgdtw %cs:rombios32_gdt_48
53 // Enable protected mode
58 // start 32bit protected mode code
59 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
64 movl $SEG32_MODE32_DS, %eax
74 // Place CPU into 16bit mode from 32bit mode.
75 // %edx = return location (in 16bit mode)
76 // Clobbers: ecx, flags, segment registers, cr0, idt/gdt
78 .global transition16big
82 // restore data segment limits to 0xffff
83 movl $SEG32_MODE16_DS, %eax
90 #if CONFIG_DISABLE_A20
93 andb $~A20_ENABLE_BIT, %al
98 ljmpw $SEG32_MODE16_CS, $1f
103 movl $SEG32_MODE16BIG_DS, %eax
110 ljmpw $SEG32_MODE16BIG_CS, $1f
114 // Disable protected mode
119 // far jump to flush CPU queue after transition to real mode
123 // restore IDT to normal real-mode defaults
124 lidtw %cs:rmode_IDT_info
126 // Clear segment registers
132 movw %ax, %ss // Assume stack is in segment 0
137 // Call a 16bit function from 16bit mode with a specified cpu register state
138 // %eax = address of struct bregs
139 // Clobbers: %e[bcd]x, %e[ds]i, flags
146 // Setup for iretw call
148 pushw $1f // return point
149 pushw BREGS_flags(%eax) // flags
150 pushl BREGS_code(%eax) // CS:IP
152 // Load calling registers.
153 movl BREGS_edi(%eax), %edi
154 movl BREGS_esi(%eax), %esi
155 movl BREGS_ebp(%eax), %ebp
156 movl BREGS_ebx(%eax), %ebx
157 movl BREGS_edx(%eax), %edx
158 movl BREGS_ecx(%eax), %ecx
159 movw BREGS_es(%eax), %es
160 movw BREGS_ds(%eax), %ds
161 movl %ss:BREGS_eax(%eax), %eax
164 iretw // XXX - just do a lcalll
166 // Store flags, eax, ecx
169 movl 0x06(%esp), %eax
170 movl %ecx, %ss:BREGS_ecx(%eax)
171 movw %ds, %ss:BREGS_ds(%eax)
173 movw %cx, %ds // Restore %ds == %ss
175 movl %ecx, BREGS_eax(%eax)
177 movw %cx, BREGS_flags(%eax)
179 // Store remaining registers
180 movw %es, BREGS_es(%eax)
181 movl %edi, BREGS_edi(%eax)
182 movl %esi, BREGS_esi(%eax)
183 movl %ebp, BREGS_ebp(%eax)
184 movl %ebx, BREGS_ebx(%eax)
185 movl %edx, BREGS_edx(%eax)
187 // Remove %eax, restore %ebp
193 // Call a 16bit function from 32bit mode.
194 // %eax = address of struct bregs
195 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
196 DECLFUNC __call16_from32
197 .global __call16big_from32
209 // Return via transition32
210 movl $(2f + BUILD_BIOS_ADDR), %edx
217 .macro IRQ_TRAMPOLINE num
218 DECLFUNC irq_trampoline_0x\num
219 irq_trampoline_0x\num :
232 /****************************************************************
233 * Misc. entry points.
234 ****************************************************************/
236 // Resume (and reboot) entry point - called from entry_post
237 DECLFUNC entry_resume
239 // Disable interrupts
242 // Use a stack in EBDA
245 movw BDA_ebda_seg, %ax
248 movl $EBDA_OFFSET_TOP_STACK, %esp
255 pushl %esp // Backup %esp, then clear high bits
257 pushfl // Save registers clobbered by C code
265 movw %ss, %cx // Move %ss to %ds
267 movl $_cfunc32flat_handle_pmm, %eax // Setup: call32(handle_pmm, args, -1)
268 leal 28(%esp), %edx // %edx points to start of args
271 movw %ax, 12(%esp) // Modify %ax:%dx to return %eax
274 popw %ds // Restore saved registers
284 DECLFUNC entry_pnp_real
285 .global entry_pnp_prot
290 pushl %esp // Backup %esp, then clear high bits
293 pushfl // Save registers clobbered by C code
301 movw %ss, %cx // Move %ss to %ds
303 leal 28(%esp), %eax // %eax points to start of u16 args
305 movw %ax, 12(%esp) // Modify %eax to return %ax
320 ENTRY_ARG handle_apm16
321 addw $4, %sp // pop dummy
322 popfw // restore flags
330 pushl %cs // Move second descriptor after %cs to %gs
333 ENTRY_ARG_ESP _cfunc32seg_handle_apm32
338 // PCI-BIOS 32bit entry point
339 DECLFUNC entry_pcibios32
342 pushl %gs // Backup %gs and set %gs=%ds
345 ENTRY_ARG_ESP _cfunc32seg_handle_pcibios32
351 EXPORTFUNC entry_bios32
355 // Check for PCI-BIOS request
356 cmpl $0x49435024, %eax // $PCI
358 movl $BUILD_BIOS_ADDR, %ebx
359 movl $BUILD_BIOS_SIZE, %ecx
360 movl $entry_pcibios32, %edx
370 // 32bit elf entry point
375 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
376 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
377 movl $SEG32_MODE32_DS, %eax
383 movl $BUILD_STACK_ADDR, %esp
384 ljmpl $SEG32_MODE32_CS, $_cfunc32flat_handle_post
389 /****************************************************************
390 * Interrupt entry points
391 ****************************************************************/
393 // Main entry point for interrupts without args
399 // Main entry point for interrupts with args
405 // Define an entry point for an interrupt (no args passed).
413 .macro DECL_IRQ_ENTRY num
418 // Define an entry point for an interrupt (can read/modify args).
419 .macro IRQ_ENTRY_ARG num
426 .macro DECL_IRQ_ENTRY_ARG num
431 // Various entry points (that don't require a fixed location).
432 DECL_IRQ_ENTRY_ARG 13
437 DECL_IRQ_ENTRY hwpic1
438 DECL_IRQ_ENTRY hwpic2
440 // int 18/19 are special - they reset stack and call into 32bit mode.
443 ENTRY_INTO32 _cfunc32flat_handle_19
447 ENTRY_INTO32 _cfunc32flat_handle_18
450 /****************************************************************
451 * Fixed position entry points
452 ****************************************************************/
454 // Specify a location in the fixed part of bios area.
456 .section .fixedaddr.\addr
461 cmpl $0, %cs:HaveRunPost // Check for resume/reboot
463 ENTRY_INTO32 _cfunc32flat_handle_post // Normal entry point
469 .global entry_13_official
473 // 0xe401 - OldFDPT in disk.c
476 .global entry_19_official
480 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
482 // 0xe729 - BaudTable in serial.c
499 // 0xefc7 - diskette_param_table in floppy.c
506 // XXX - INT 10 Functions 0-Fh Entry Point
512 // 0xf0a4 - VideoParams in misc.c
523 // 0xfa6e - vgafont8 in font.c
531 // 0xfef3 - InitVectors in misc.c
533 // 0xff00 - BiosCopyright in misc.c
536 .global entry_iret_official
543 ORG 0xfff0 // Power-up Entry Point
546 ljmpw $SEG_BIOS, $entry_post
548 // 0xfff5 - BiosDate in misc.c
550 // 0xfffe - BiosModelId in misc.c
552 // 0xffff - BiosChecksum in misc.c