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 ****************************************************************/
134 andl $~(CR0_CD|CR0_NW), %eax
137 // Disable interrupts
141 // Check for restart indicator.
142 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
143 outb %al, $PORT_CMOS_INDEX
144 inb $PORT_CMOS_DATA, %al
148 // Normal entry point
149 ENTRY_INTO32 _code32__start
151 // Entry point when a post call looks like a resume.
153 // Save old shutdown status.
156 // Clear shutdown status register.
157 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
158 outb %al, $PORT_CMOS_INDEX
160 outb %al, $PORT_CMOS_DATA
162 // Use a stack in EBDA
165 movw BDA_ebda_seg, %ax
166 // XXX - should verify ebda_seg looks sane.
169 movl $EBDA_OFFSET_TOP_STACK, %esp
176 /****************************************************************
178 ****************************************************************/
180 // Place CPU into 32bit mode from 16bit mode.
181 // Clobbers: flags, segment registers, cr0, idt/gdt
182 // Require: interrupts must be disabled
183 DECLFUNC transition32
189 orb $A20_ENABLE_BIT, %al
192 // Set segment descriptors
193 lidtw %cs:pmode_IDT_info
194 lgdtw %cs:rombios32_gdt_48
197 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
198 outb %al, $PORT_CMOS_INDEX
199 inb $PORT_CMOS_DATA, %al
201 // Enable protected mode
206 // start 32bit protected mode code
207 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
211 // init data segments
212 movl $SEG32_MODE32_DS, %eax
222 // Call a 16bit function from 32bit mode.
223 // %eax = address of struct bregs
224 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
225 DECLFUNC __call16_from32
226 .global __call16big_from32
230 // restore data segment limits to 0xffff
231 movl $SEG32_MODE16_DS, %eax
240 andb $~A20_ENABLE_BIT, %al
243 // Jump to 16bit mode
244 ljmpw $SEG32_MODE16_CS, $1f
249 movl $SEG32_MODE16BIG_DS, %eax
256 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
260 // Disable protected mode
265 // far jump to flush CPU queue after transition to real mode
269 // restore IDT to normal real-mode defaults
270 lidtw %cs:rmode_IDT_info
272 // Clear segment registers
278 movw %ax, %ss // Assume stack is in segment 0
285 // Return via transition32
289 // Call a 16bit function from 16bit mode with a specified cpu register state
290 // %eax = address of struct bregs
291 // Clobbers: %e[bcd]x, %e[ds]i, flags
298 // Setup for iretw call
300 pushw $1f // return point
301 pushw BREGS_flags(%eax) // flags
302 pushl BREGS_ip(%eax) // CS:IP
304 // Load calling registers.
305 movl BREGS_edi(%eax), %edi
306 movl BREGS_esi(%eax), %esi
307 movl BREGS_ebx(%eax), %ebx
308 movl BREGS_edx(%eax), %edx
309 movl BREGS_ecx(%eax), %ecx
310 movw BREGS_es(%eax), %es
311 movw BREGS_ds(%eax), %ds
312 movl %ss:BREGS_eax(%eax), %eax
315 iretw // XXX - just do a lcalll
317 // Store flags, eax, ecx
320 movl 0x06(%esp), %eax
321 movl %ecx, %ss:BREGS_ecx(%eax)
322 movw %ds, %ss:BREGS_ds(%eax)
324 movw %cx, %ds // Restore %ds == %ss
326 movl %ecx, BREGS_eax(%eax)
328 movw %cx, BREGS_flags(%eax)
330 // Store remaining registers
331 movw %es, BREGS_es(%eax)
332 movl %edi, BREGS_edi(%eax)
333 movl %esi, BREGS_esi(%eax)
334 movl %ebx, BREGS_ebx(%eax)
335 movl %edx, BREGS_edx(%eax)
337 // Remove %eax, restore %ebp
347 DECLFUNC entry_pnp_real
348 .global entry_pnp_prot
353 pushl %esp // Backup %esp, then clear high bits
356 pushfl // Save registers clobbered by C code
362 movw %ss, %cx // Move %ss to %ds
364 lea 28(%esp), %eax // %eax points to start of u16 args
366 movw %ax, 12(%esp) // Modify %eax to return %ax
377 DECLFUNC apm16protected_entry
378 apm16protected_entry:
381 ENTRY_ARG handle_1553
382 addw $4, %sp // pop dummy
383 popfw // restore flags
387 DECLFUNC apm32protected_entry
388 apm32protected_entry:
390 pushw %cs // Setup for long jump to 16bit mode
396 ENTRY_ARG_ESP handle_1553
398 movw $2f,(%esp) // Setup for long jump back to 32bit mode
403 addl $4, %esp // pop call address
407 // 32bit elf entry point
412 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
413 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
414 movl $BUILD_STACK_ADDR, %esp
415 ljmpl $SEG32_MODE32_CS, $_code32__start
420 .macro IRQ_TRAMPOLINE num
421 DECLFUNC irq_trampoline_0x\num
422 irq_trampoline_0x\num :
435 /****************************************************************
436 * Interrupt entry points
437 ****************************************************************/
439 // Define an entry point for an interrupt (no args passed).
447 // Define an entry point for an interrupt (can read/modify args).
448 .macro IRQ_ENTRY_ARG num
451 ENTRY_ARG handle_\num
455 // Macros that put each handler into its own section
456 .macro DECL_IRQ_ENTRY num
457 .section .text.asm.entry_\num
460 .macro DECL_IRQ_ENTRY_ARG num
461 .section .text.asm.entry_\num
465 DECL_IRQ_ENTRY_ARG 13
466 DECL_IRQ_ENTRY_ARG 12
467 DECL_IRQ_ENTRY_ARG 11
472 DECL_IRQ_ENTRY hwpic1
473 DECL_IRQ_ENTRY hwpic2
475 // int 18/19 are special - they reset stack and call into 32bit mode.
480 ENTRY_INTO32 _code32_handle_19
486 ENTRY_INTO32 _code32_handle_18
489 /****************************************************************
490 * Fixed position entry points
491 ****************************************************************/
493 // Specify a location in the fixed part of bios area.
495 .section .fixedaddr.\addr
506 .global entry_13_official
510 // 0xe401 - OldFDPT in disk.c
513 .global entry_19_official
517 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
519 // 0xe729 - BaudTable in serial.c
536 // 0xefc7 - diskette_param_table in floppy.c
543 // XXX - INT 10 Functions 0-Fh Entry Point
549 // 0xf0a4 - VideoParams in misc.c
552 .global entry_12_official
557 .global entry_11_official
564 // 0xfa6e - vgafont8 in font.c
572 // 0xfef3 - InitVectors in misc.c
574 // 0xff00 - BiosCopyright in misc.c
577 .global entry_iret_official
584 ORG 0xfff0 // Power-up Entry Point
587 ljmpw $SEG_BIOS, $entry_post_official
589 // 0xfff5 - BiosDate in misc.c
591 // 0xfffe - BiosModelId in misc.c
593 // 0xffff - BiosChecksum in misc.c