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 DECLFUNC transition32
136 // Disable irqs (and clear direction flag)
141 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
142 outb %al, $PORT_CMOS_INDEX
143 inb $PORT_CMOS_DATA, %al
147 orb $A20_ENABLE_BIT, %al
150 // Set segment descriptors
151 lidtw %cs:pmode_IDT_info
152 lgdtw %cs:rombios32_gdt_48
154 // Enable protected mode
159 // start 32bit protected mode code
160 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
164 // init data segments
165 movl $SEG32_MODE32_DS, %eax
175 // Call a 16bit function from 32bit mode.
176 // %eax = address of struct bregs
177 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
178 DECLFUNC __call16_from32
179 .global __call16big_from32
183 // restore data segment limits to 0xffff
184 movl $SEG32_MODE16_DS, %eax
191 #if CONFIG_DISABLE_A20
194 andb $~A20_ENABLE_BIT, %al
198 // Jump to 16bit mode
199 ljmpw $SEG32_MODE16_CS, $1f
204 movl $SEG32_MODE16BIG_DS, %eax
211 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
215 // Disable protected mode
220 // far jump to flush CPU queue after transition to real mode
224 // restore IDT to normal real-mode defaults
225 lidtw %cs:rmode_IDT_info
227 // Clear segment registers
233 movw %ax, %ss // Assume stack is in segment 0
240 // Return via transition32
244 // Call a 16bit function from 16bit mode with a specified cpu register state
245 // %eax = address of struct bregs
246 // Clobbers: %e[bcd]x, %e[ds]i, flags
253 // Setup for iretw call
255 pushw $1f // return point
256 pushw BREGS_flags(%eax) // flags
257 pushl BREGS_ip(%eax) // CS:IP
259 // Load calling registers.
260 movl BREGS_edi(%eax), %edi
261 movl BREGS_esi(%eax), %esi
262 movl BREGS_ebx(%eax), %ebx
263 movl BREGS_edx(%eax), %edx
264 movl BREGS_ecx(%eax), %ecx
265 movw BREGS_es(%eax), %es
266 movw BREGS_ds(%eax), %ds
267 movl %ss:BREGS_eax(%eax), %eax
270 iretw // XXX - just do a lcalll
272 // Store flags, eax, ecx
275 movl 0x06(%esp), %eax
276 movl %ecx, %ss:BREGS_ecx(%eax)
277 movw %ds, %ss:BREGS_ds(%eax)
279 movw %cx, %ds // Restore %ds == %ss
281 movl %ecx, BREGS_eax(%eax)
283 movw %cx, BREGS_flags(%eax)
285 // Store remaining registers
286 movw %es, BREGS_es(%eax)
287 movl %edi, BREGS_edi(%eax)
288 movl %esi, BREGS_esi(%eax)
289 movl %ebx, BREGS_ebx(%eax)
290 movl %edx, BREGS_edx(%eax)
292 // Remove %eax, restore %ebp
299 .macro IRQ_TRAMPOLINE num
300 DECLFUNC irq_trampoline_0x\num
301 irq_trampoline_0x\num :
314 /****************************************************************
316 ****************************************************************/
322 andl $~(CR0_CD|CR0_NW), %eax
325 // Disable interrupts
329 // Check for restart indicator.
330 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
331 outb %al, $PORT_CMOS_INDEX
332 inb $PORT_CMOS_DATA, %al
336 // Normal entry point
337 ENTRY_INTO32 _code32__start
339 // Entry point when a post call looks like a resume.
341 // Save old shutdown status.
344 // Clear shutdown status register.
345 movl $CMOS_RESET_CODE|NMI_DISABLE_BIT, %eax
346 outb %al, $PORT_CMOS_INDEX
348 outb %al, $PORT_CMOS_DATA
350 // Use a stack in EBDA
353 movw BDA_ebda_seg, %ax
354 // XXX - should verify ebda_seg looks sane.
357 movl $EBDA_OFFSET_TOP_STACK, %esp
364 /****************************************************************
365 * Misc. entry points.
366 ****************************************************************/
369 DECLFUNC entry_pnp_real
370 .global entry_pnp_prot
375 pushl %esp // Backup %esp, then clear high bits
378 pushfl // Save registers clobbered by C code
384 movw %ss, %cx // Move %ss to %ds
386 lea 28(%esp), %eax // %eax points to start of u16 args
388 movw %ax, 12(%esp) // Modify %eax to return %ax
399 DECLFUNC apm16protected_entry
400 apm16protected_entry:
403 ENTRY_ARG handle_1553
404 addw $4, %sp // pop dummy
405 popfw // restore flags
409 DECLFUNC apm32protected_entry
410 apm32protected_entry:
412 pushw %cs // Setup for long jump to 16bit mode
418 ENTRY_ARG_ESP handle_1553
420 movw $2f,(%esp) // Setup for long jump back to 32bit mode
425 addl $4, %esp // pop call address
429 // 32bit elf entry point
434 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
435 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
436 movl $SEG32_MODE32_DS, %eax
442 movl $BUILD_STACK_ADDR, %esp
443 ljmpl $SEG32_MODE32_CS, $_code32__start
448 /****************************************************************
449 * Interrupt entry points
450 ****************************************************************/
452 // Define an entry point for an interrupt (no args passed).
460 // Define an entry point for an interrupt (can read/modify args).
461 .macro IRQ_ENTRY_ARG num
464 ENTRY_ARG handle_\num
468 // Macros that put each handler into its own section
469 .macro DECL_IRQ_ENTRY num
470 .section .text.asm.entry_\num
473 .macro DECL_IRQ_ENTRY_ARG num
474 .section .text.asm.entry_\num
478 DECL_IRQ_ENTRY_ARG 13
479 DECL_IRQ_ENTRY_ARG 12
480 DECL_IRQ_ENTRY_ARG 11
485 DECL_IRQ_ENTRY hwpic1
486 DECL_IRQ_ENTRY hwpic2
488 // int 18/19 are special - they reset stack and call into 32bit mode.
491 ENTRY_INTO32 _code32_handle_19
495 ENTRY_INTO32 _code32_handle_18
498 /****************************************************************
499 * Fixed position entry points
500 ****************************************************************/
502 // Specify a location in the fixed part of bios area.
504 .section .fixedaddr.\addr
515 .global entry_13_official
519 // 0xe401 - OldFDPT in disk.c
522 .global entry_19_official
526 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
528 // 0xe729 - BaudTable in serial.c
545 // 0xefc7 - diskette_param_table in floppy.c
552 // XXX - INT 10 Functions 0-Fh Entry Point
558 // 0xf0a4 - VideoParams in misc.c
561 .global entry_12_official
566 .global entry_11_official
573 // 0xfa6e - vgafont8 in font.c
581 // 0xfef3 - InitVectors in misc.c
583 // 0xff00 - BiosCopyright in misc.c
586 .global entry_iret_official
593 ORG 0xfff0 // Power-up Entry Point
596 ljmpw $SEG_BIOS, $entry_post_official
598 // 0xfff5 - BiosDate in misc.c
600 // 0xfffe - BiosModelId in misc.c
602 // 0xffff - BiosChecksum in misc.c