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 /****************************************************************
234 ****************************************************************/
240 andl $~(CR0_CD|CR0_NW), %eax
243 // Disable interrupts
247 // Check for restart indicator.
248 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
249 outb %al, $PORT_CMOS_INDEX
250 inb $PORT_CMOS_DATA, %al
254 // Normal entry point
255 ENTRY_INTO32 _cfunc32flat__start
257 // Entry point when a post call looks like a resume.
259 // Save old shutdown status.
262 // Clear shutdown status register.
263 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
264 outb %al, $PORT_CMOS_INDEX
266 outb %al, $PORT_CMOS_DATA
268 // Use a stack in EBDA
271 movw BDA_ebda_seg, %ax
273 cmpw $EBDA_SEGMENT_START, %ax
275 // EBDA segment doesn't look valid - use startup value.
276 movw $EBDA_SEGMENT_START, %ax
280 movl $EBDA_OFFSET_TOP_STACK, %esp
287 /****************************************************************
288 * Misc. entry points.
289 ****************************************************************/
294 pushl %esp // Backup %esp, then clear high bits
296 pushfl // Save registers clobbered by C code
304 movw %ss, %cx // Move %ss to %ds
306 leal 28(%esp), %eax // %eax points to start of args
308 movw %ax, 12(%esp) // Modify %ax:%dx to return %eax
311 popw %ds // Restore saved registers
321 DECLFUNC entry_pnp_real
322 .global entry_pnp_prot
327 pushl %esp // Backup %esp, then clear high bits
330 pushfl // Save registers clobbered by C code
338 movw %ss, %cx // Move %ss to %ds
340 leal 28(%esp), %eax // %eax points to start of u16 args
342 movw %ax, 12(%esp) // Modify %eax to return %ax
353 DECLFUNC apm16protected_entry
354 apm16protected_entry:
357 ENTRY_ARG handle_apm16
358 addw $4, %sp // pop dummy
359 popfw // restore flags
363 DECLFUNC apm32protected_entry
364 apm32protected_entry:
367 pushl %cs // Move second descriptor after %cs to %gs
370 ENTRY_ARG_ESP _cfunc32seg_handle_apm32
375 // PCI-BIOS 32bit entry point
376 DECLFUNC pcibios32_entry
379 pushl %gs // Backup %gs and set %gs=%ds
382 ENTRY_ARG_ESP _cfunc32seg_handle_pcibios32
388 EXPORTFUNC bios32_entry
392 // Check for PCI-BIOS request
393 cmpl $0x49435024, %eax // $PCI
395 movl $BUILD_BIOS_ADDR, %ebx
396 movl $BUILD_BIOS_SIZE, %ecx
397 movl $pcibios32_entry, %edx
407 // 32bit elf entry point
412 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
413 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
414 movl $SEG32_MODE32_DS, %eax
420 movl $BUILD_STACK_ADDR, %esp
421 ljmpl $SEG32_MODE32_CS, $_cfunc32flat__start
426 /****************************************************************
427 * Interrupt entry points
428 ****************************************************************/
430 // Main entry point for interrupts without args
436 // Main entry point for interrupts with args
442 // Define an entry point for an interrupt (no args passed).
450 .macro DECL_IRQ_ENTRY num
455 // Define an entry point for an interrupt (can read/modify args).
456 .macro IRQ_ENTRY_ARG num
463 .macro DECL_IRQ_ENTRY_ARG num
468 // Various entry points (that don't require a fixed location).
469 DECL_IRQ_ENTRY_ARG 13
474 DECL_IRQ_ENTRY hwpic1
475 DECL_IRQ_ENTRY hwpic2
477 // int 18/19 are special - they reset stack and call into 32bit mode.
480 ENTRY_INTO32 _cfunc32flat_handle_19
484 ENTRY_INTO32 _cfunc32flat_handle_18
487 /****************************************************************
488 * Fixed position entry points
489 ****************************************************************/
491 // Specify a location in the fixed part of bios area.
493 .section .fixedaddr.\addr
504 .global entry_13_official
508 // 0xe401 - OldFDPT in disk.c
511 .global entry_19_official
515 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
517 // 0xe729 - BaudTable in serial.c
534 // 0xefc7 - diskette_param_table in floppy.c
541 // XXX - INT 10 Functions 0-Fh Entry Point
547 // 0xf0a4 - VideoParams in misc.c
558 // 0xfa6e - vgafont8 in font.c
566 // 0xfef3 - InitVectors in misc.c
568 // 0xff00 - BiosCopyright in misc.c
571 .global entry_iret_official
578 ORG 0xfff0 // Power-up Entry Point
581 ljmpw $SEG_BIOS, $entry_post_official
583 // 0xfff5 - BiosDate in misc.c
585 // 0xfffe - BiosModelId in misc.c
587 // 0xffff - BiosChecksum in misc.c