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 ****************************************************************/
14 .include "out/ccode.16.s"
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 "../out/asm-offsets.h" // BREGS_*
21 #include "entryfuncs.S" // ENTRY_*
24 /****************************************************************
26 ****************************************************************/
28 // Place CPU into 32bit mode from 16bit mode.
29 // Clobbers: ecx, flags, segment registers, cr0, idt/gdt
34 // Disable irqs (and clear direction flag)
39 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
40 outb %al, $PORT_CMOS_INDEX
41 inb $PORT_CMOS_DATA, %al
45 orb $A20_ENABLE_BIT, %al
48 // Set segment descriptors
49 lidtw %cs:pmode_IDT_info
50 lgdtw %cs:rombios32_gdt_48
52 // Enable protected mode
57 // start 32bit protected mode code
58 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
63 movl $SEG32_MODE32_DS, %eax
73 // Place CPU into 16bit mode from 32bit mode.
74 // Clobbers: ecx, flags, segment registers, cr0, idt/gdt
76 .global transition16big
80 // restore data segment limits to 0xffff
81 movl $SEG32_MODE16_DS, %eax
88 #if CONFIG_DISABLE_A20
91 andb $~A20_ENABLE_BIT, %al
96 ljmpw $SEG32_MODE16_CS, $1f
101 movl $SEG32_MODE16BIG_DS, %eax
108 ljmpw $SEG32_MODE16BIG_CS, $1f
112 // Disable protected mode
117 // far jump to flush CPU queue after transition to real mode
121 // restore IDT to normal real-mode defaults
122 lidtw %cs:rmode_IDT_info
124 // Clear segment registers
130 movw %ax, %ss // Assume stack is in segment 0
135 // Call a 16bit function from 16bit mode with a specified cpu register state
136 // %eax = address of struct bregs
137 // Clobbers: %e[bcd]x, %e[ds]i, flags
144 // Setup for iretw call
146 pushw $1f // return point
147 pushw BREGS_flags(%eax) // flags
148 pushl BREGS_code(%eax) // CS:IP
150 // Load calling registers.
151 movl BREGS_edi(%eax), %edi
152 movl BREGS_esi(%eax), %esi
153 movl BREGS_ebp(%eax), %ebp
154 movl BREGS_ebx(%eax), %ebx
155 movl BREGS_edx(%eax), %edx
156 movl BREGS_ecx(%eax), %ecx
157 movw BREGS_es(%eax), %es
158 movw BREGS_ds(%eax), %ds
159 movl %ss:BREGS_eax(%eax), %eax
162 iretw // XXX - just do a lcalll
164 // Store flags, eax, ecx
167 movl 0x06(%esp), %eax
168 movl %ecx, %ss:BREGS_ecx(%eax)
169 movw %ds, %ss:BREGS_ds(%eax)
171 movw %cx, %ds // Restore %ds == %ss
173 movl %ecx, BREGS_eax(%eax)
175 movw %cx, BREGS_flags(%eax)
177 // Store remaining registers
178 movw %es, BREGS_es(%eax)
179 movl %edi, BREGS_edi(%eax)
180 movl %esi, BREGS_esi(%eax)
181 movl %ebp, BREGS_ebp(%eax)
182 movl %ebx, BREGS_ebx(%eax)
183 movl %edx, BREGS_edx(%eax)
185 // Remove %eax, restore %ebp
191 // Call a 16bit function from 32bit mode.
192 // %eax = address of struct bregs
193 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
194 DECLFUNC __call16_from32
195 .global __call16big_from32
207 // Return via transition32
211 .macro IRQ_TRAMPOLINE num
212 DECLFUNC irq_trampoline_0x\num
213 irq_trampoline_0x\num :
226 /****************************************************************
228 ****************************************************************/
234 andl $~(CR0_CD|CR0_NW), %eax
237 // Disable interrupts
241 // Check for restart indicator.
242 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
243 outb %al, $PORT_CMOS_INDEX
244 inb $PORT_CMOS_DATA, %al
248 // Normal entry point
251 // Entry point when a post call looks like a resume.
253 // Save old shutdown status.
256 // Clear shutdown status register.
257 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
258 outb %al, $PORT_CMOS_INDEX
260 outb %al, $PORT_CMOS_DATA
262 // Use a stack in EBDA
265 movw BDA_ebda_seg, %ax
267 cmpw $EBDA_SEGMENT_START, %ax
269 // EBDA segment doesn't look valid - use startup value.
270 movw $EBDA_SEGMENT_START, %ax
274 movl $EBDA_OFFSET_TOP_STACK, %esp
281 /****************************************************************
282 * Misc. entry points.
283 ****************************************************************/
288 pushl %esp // Backup %esp, then clear high bits
290 pushfl // Save registers clobbered by C code
298 movw %ss, %cx // Move %ss to %ds
300 leal 28(%esp), %eax // %eax points to start of args
302 movw %ax, 12(%esp) // Modify %ax:%dx to return %eax
305 popw %ds // Restore saved registers
315 DECLFUNC entry_pnp_real
316 .global entry_pnp_prot
321 pushl %esp // Backup %esp, then clear high bits
324 pushfl // Save registers clobbered by C code
332 movw %ss, %cx // Move %ss to %ds
334 leal 28(%esp), %eax // %eax points to start of u16 args
336 movw %ax, 12(%esp) // Modify %eax to return %ax
347 DECLFUNC apm16protected_entry
348 apm16protected_entry:
351 ENTRY_ARG handle_apm16
352 addw $4, %sp // pop dummy
353 popfw // restore flags
357 DECLFUNC apm32protected_entry
358 apm32protected_entry:
361 pushl %cs // Move second descriptor after %cs to %gs
364 ENTRY_ARG_ESP handle_apm32
369 // PCI-BIOS 32bit entry point
370 DECLFUNC pcibios32_entry
373 pushl %gs // Backup %gs and set %gs=%ds
376 ENTRY_ARG_ESP handle_pcibios32
382 EXPORTFUNC bios32_entry
386 // Check for PCI-BIOS request
387 cmpl $0x49435024, %eax // $PCI
389 movl $BUILD_BIOS_ADDR, %ebx
390 movl $BUILD_BIOS_SIZE, %ecx
391 movl $pcibios32_entry, %edx
401 // 32bit elf entry point
406 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
407 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
408 movl $SEG32_MODE32_DS, %eax
414 movl $BUILD_STACK_ADDR, %esp
415 ljmpl $SEG32_MODE32_CS, $_start
420 /****************************************************************
421 * Interrupt entry points
422 ****************************************************************/
424 // Define an entry point for an interrupt (no args passed).
432 // Define an entry point for an interrupt (can read/modify args).
433 .macro IRQ_ENTRY_ARG num
440 // Macros that put each handler into its own section
441 .macro DECL_IRQ_ENTRY num
445 .macro DECL_IRQ_ENTRY_ARG num
450 // Main entry point for interrupts without args
456 // Main entry point for interrupts with args
462 DECL_IRQ_ENTRY_ARG 13
467 DECL_IRQ_ENTRY hwpic1
468 DECL_IRQ_ENTRY hwpic2
470 // int 18/19 are special - they reset stack and call into 32bit mode.
473 ENTRY_INTO32 handle_19
477 ENTRY_INTO32 handle_18
480 /****************************************************************
481 * Fixed position entry points
482 ****************************************************************/
484 // Specify a location in the fixed part of bios area.
486 .section .fixedaddr.\addr
497 .global entry_13_official
501 // 0xe401 - OldFDPT in disk.c
504 .global entry_19_official
508 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
510 // 0xe729 - BaudTable in serial.c
527 // 0xefc7 - diskette_param_table in floppy.c
534 // XXX - INT 10 Functions 0-Fh Entry Point
540 // 0xf0a4 - VideoParams in misc.c
551 // 0xfa6e - vgafont8 in font.c
559 // 0xfef3 - InitVectors in misc.c
561 // 0xff00 - BiosCopyright in misc.c
564 .global entry_iret_official
571 ORG 0xfff0 // Power-up Entry Point
574 ljmpw $SEG_BIOS, $entry_post_official
576 // 0xfff5 - BiosDate in misc.c
578 // 0xfffe - BiosModelId in misc.c
580 // 0xffff - BiosChecksum in misc.c