1 // Rom layout and bios assembler to C interface.
3 // Copyright (C) 2008 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 "config.h" // CONFIG_*
9 #include "ioport.h" // PORT_A20
10 #include "bregs.h" // CR0_*
11 #include "cmos.h" // CMOS_RESET_CODE
12 #include "../out/asm-offsets.h" // BREGS_*
15 /****************************************************************
16 * Include of 16bit C code
17 ****************************************************************/
20 .include "out/ccode.16.s"
23 /****************************************************************
25 ****************************************************************/
27 // Call a C function - this does the minimal work necessary to
28 // call into C. It sets up %ds, backs up %es, and backs up
29 // those registers that are call clobbered by the C compiler.
31 cli // In case something far-calls instead of using "int"
33 pushl %eax // Save registers clobbered by C code
38 movw %ss, %ax // Move %ss to %ds
40 pushl %esp // Backup %esp, then clear high bits
43 popl %esp // Restore %esp (including high bits)
44 popw %ds // Restore registers saved above
51 // Call a C function with current register list as an
52 // argument. This backs up the registers and sets %eax
53 // to point to the backup. On return, the registers are
54 // restored from the structure.
55 .macro ENTRY_ARG cfunc
58 pushl %eax // Save registers (matches struct bregs)
66 movw %ss, %ax // Move %ss to %ds
68 movl %esp, %ebx // Backup %esp, then zero high bits
70 movl %esp, %eax // First arg is pointer to struct bregs
72 movl %ebx, %esp // Restore %esp (including high bits)
73 popw %ds // Restore registers (from struct bregs)
83 // As above, but don't mangle %esp
84 .macro ENTRY_ARG_ESP cfunc
87 pushl %eax // Save registers (matches struct bregs)
95 movw %ss, %ax // Move %ss to %ds
97 movl %esp, %eax // First arg is pointer to struct bregs
99 popw %ds // Restore registers (from struct bregs)
109 // Reset stack, transition to 32bit mode, and call a C function.
111 .macro ENTRY_INTO32 cfunc
114 movl $ BUILD_STACK_ADDR , %esp
119 // Declare a function
121 .section .text.asm.\func
126 /****************************************************************
128 ****************************************************************/
130 // Place CPU into 32bit mode from 16bit mode.
131 // Clobbers: flags, segment registers, cr0, idt/gdt
132 // Require: interrupts must be disabled
133 DECLFUNC transition32
139 orb $A20_ENABLE_BIT, %al
142 // Set segment descriptors
143 lidtw %cs:pmode_IDT_info
144 lgdtw %cs:rombios32_gdt_48
147 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
148 outb %al, $PORT_CMOS_INDEX
149 inb $PORT_CMOS_DATA, %al
151 // Enable protected mode
156 // start 32bit protected mode code
157 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
161 // init data segments
162 movl $SEG32_MODE32_DS, %eax
172 // Call a 16bit function from 32bit mode.
173 // %eax = address of struct bregs
174 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
175 DECLFUNC __call16_from32
176 .global __call16big_from32
180 // restore data segment limits to 0xffff
181 movl $SEG32_MODE16_DS, %eax
190 andb $~A20_ENABLE_BIT, %al
193 // Jump to 16bit mode
194 ljmpw $SEG32_MODE16_CS, $1f
199 movl $SEG32_MODE16BIG_DS, %eax
206 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
210 // Disable protected mode
215 // far jump to flush CPU queue after transition to real mode
219 // restore IDT to normal real-mode defaults
220 lidtw %cs:rmode_IDT_info
222 // Clear segment registers
228 movw %ax, %ss // Assume stack is in segment 0
235 // Return via transition32
239 // Call a 16bit function from 16bit mode with a specified cpu register state
240 // %eax = address of struct bregs
241 // Clobbers: %e[bcd]x, %e[ds]i, flags
248 // Setup for iretw call
250 pushw $1f // return point
251 pushw BREGS_flags(%eax) // flags
252 pushl BREGS_ip(%eax) // CS:IP
254 // Load calling registers.
255 movl BREGS_edi(%eax), %edi
256 movl BREGS_esi(%eax), %esi
257 movl BREGS_ebx(%eax), %ebx
258 movl BREGS_edx(%eax), %edx
259 movl BREGS_ecx(%eax), %ecx
260 movw BREGS_es(%eax), %es
261 movw BREGS_ds(%eax), %ds
262 movl %ss:BREGS_eax(%eax), %eax
265 iretw // XXX - just do a lcalll
267 // Store flags, eax, ecx
270 movl 0x06(%esp), %eax
271 movl %ecx, %ss:BREGS_ecx(%eax)
272 movw %ds, %ss:BREGS_ds(%eax)
274 movw %cx, %ds // Restore %ds == %ss
276 movl %ecx, BREGS_eax(%eax)
278 movw %cx, BREGS_flags(%eax)
280 // Store remaining registers
281 movw %es, BREGS_es(%eax)
282 movl %edi, BREGS_edi(%eax)
283 movl %esi, BREGS_esi(%eax)
284 movl %ebx, BREGS_ebx(%eax)
285 movl %edx, BREGS_edx(%eax)
287 // Remove %eax, restore %ebp
297 .macro IRQ_TRAMPOLINE num
298 DECLFUNC irq_trampoline_0x\num
299 irq_trampoline_0x\num :
312 /****************************************************************
314 ****************************************************************/
320 andl $~(CR0_CD|CR0_NW), %eax
323 // Disable interrupts
327 // Check for restart indicator.
328 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
329 outb %al, $PORT_CMOS_INDEX
330 inb $PORT_CMOS_DATA, %al
334 // Normal entry point
335 ENTRY_INTO32 _code32__start
337 // Entry point when a post call looks like a resume.
339 // Save old shutdown status.
342 // Clear shutdown status register.
343 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
344 outb %al, $PORT_CMOS_INDEX
346 outb %al, $PORT_CMOS_DATA
348 // Use a stack in EBDA
351 movw BDA_ebda_seg, %ax
352 // XXX - should verify ebda_seg looks sane.
355 movl $EBDA_OFFSET_TOP_STACK, %esp
362 /****************************************************************
363 * Misc. entry points.
364 ****************************************************************/
367 DECLFUNC entry_pnp_real
368 .global entry_pnp_prot
373 pushl %esp // Backup %esp, then clear high bits
376 pushfl // Save registers clobbered by C code
382 movw %ss, %cx // Move %ss to %ds
384 lea 28(%esp), %eax // %eax points to start of u16 args
386 movw %ax, 12(%esp) // Modify %eax to return %ax
397 DECLFUNC apm16protected_entry
398 apm16protected_entry:
401 ENTRY_ARG handle_1553
402 addw $4, %sp // pop dummy
403 popfw // restore flags
407 DECLFUNC apm32protected_entry
408 apm32protected_entry:
410 pushw %cs // Setup for long jump to 16bit mode
416 ENTRY_ARG_ESP handle_1553
418 movw $2f,(%esp) // Setup for long jump back to 32bit mode
423 addl $4, %esp // pop call address
427 // 32bit elf entry point
432 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
433 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
434 movl $BUILD_STACK_ADDR, %esp
435 ljmpl $SEG32_MODE32_CS, $_code32__start
440 /****************************************************************
441 * Interrupt entry points
442 ****************************************************************/
444 // Define an entry point for an interrupt (no args passed).
452 // Define an entry point for an interrupt (can read/modify args).
453 .macro IRQ_ENTRY_ARG num
456 ENTRY_ARG handle_\num
460 // Macros that put each handler into its own section
461 .macro DECL_IRQ_ENTRY num
462 .section .text.asm.entry_\num
465 .macro DECL_IRQ_ENTRY_ARG num
466 .section .text.asm.entry_\num
470 DECL_IRQ_ENTRY_ARG 13
471 DECL_IRQ_ENTRY_ARG 12
472 DECL_IRQ_ENTRY_ARG 11
477 DECL_IRQ_ENTRY hwpic1
478 DECL_IRQ_ENTRY hwpic2
480 // int 18/19 are special - they reset stack and call into 32bit mode.
485 ENTRY_INTO32 _code32_handle_19
491 ENTRY_INTO32 _code32_handle_18
494 /****************************************************************
495 * Fixed position entry points
496 ****************************************************************/
498 // Specify a location in the fixed part of bios area.
500 .section .fixedaddr.\addr
511 .global entry_13_official
515 // 0xe401 - OldFDPT in disk.c
518 .global entry_19_official
522 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
524 // 0xe729 - BaudTable in serial.c
541 // 0xefc7 - diskette_param_table in floppy.c
548 // XXX - INT 10 Functions 0-Fh Entry Point
554 // 0xf0a4 - VideoParams in misc.c
557 .global entry_12_official
562 .global entry_11_official
569 // 0xfa6e - vgafont8 in font.c
577 // 0xfef3 - InitVectors in misc.c
579 // 0xff00 - BiosCopyright in misc.c
582 .global entry_iret_official
589 ORG 0xfff0 // Power-up Entry Point
592 ljmpw $SEG_BIOS, $entry_post_official
594 // 0xfff5 - BiosDate in misc.c
596 // 0xfffe - BiosModelId in misc.c
598 // 0xffff - BiosChecksum in misc.c