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.
32 pushl %eax // Save registers clobbered by C code
37 movw %ss, %ax // Move %ss to %ds
39 pushl %esp // Backup %esp, then clear high bits
42 popl %esp // Restore %esp (including high bits)
43 popw %ds // Restore registers saved above
50 // Call a C function with current register list as an
51 // argument. This backs up the registers and sets %eax
52 // to point to the backup. On return, the registers are
53 // restored from the structure.
54 .macro ENTRY_ARG cfunc
56 pushl %eax // Save registers (matches struct bregs)
64 movw %ss, %ax // Move %ss to %ds
66 movl %esp, %ebx // Backup %esp, then zero high bits
68 movl %esp, %eax // First arg is pointer to struct bregs
70 movl %ebx, %esp // Restore %esp (including high bits)
71 popw %ds // Restore registers (from struct bregs)
81 // As above, but don't mangle %esp
82 .macro ENTRY_ARG_ESP cfunc
84 pushl %eax // Save registers (matches struct bregs)
92 movw %ss, %ax // Move %ss to %ds
94 movl %esp, %eax // First arg is pointer to struct bregs
96 popw %ds // Restore registers (from struct bregs)
106 // Macro to reset the 16bit stack
111 movl $ BUILD_STACK_ADDR , %esp
115 // Declare a function
117 .section .text.asm.\func
122 /****************************************************************
124 ****************************************************************/
130 andl $~(CR0_CD|CR0_NW), %eax
133 // Check for restart indicator.
134 movl $CMOS_RESET_CODE, %eax
135 outb %al, $PORT_CMOS_INDEX
136 inb $PORT_CMOS_DATA, %al
140 // Normal entry point
142 pushl $_code32__start
145 // Entry point when a post call looks like a resume.
147 // Save old shutdown status.
150 // Clear shutdown status register.
151 movl $CMOS_RESET_CODE, %eax
152 outb %al, $PORT_CMOS_INDEX
154 outb %al, $PORT_CMOS_DATA
156 // Use a stack in EBDA
159 movw BDA_ebda_seg, %ax
160 // XXX - should verify ebda_seg looks sane.
163 movl $EBDA_OFFSET_TOP_STACK, %esp
172 /****************************************************************
174 ****************************************************************/
176 // Place CPU into 32bit mode from 16bit mode.
177 // Clobbers: flags, segment registers, cr0, idt/gdt
178 DECLFUNC transition32
187 orb $A20_ENABLE_BIT, %al
190 // Set segment descriptors
191 lidtw %cs:pmode_IDT_info
192 lgdtw %cs:rombios32_gdt_48
194 // Enable protected mode
199 // start 32bit protected mode code
200 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
204 // init data segments
205 movl $SEG32_MODE32_DS, %eax
215 // Call a 16bit function from 32bit mode.
216 // %eax = address of struct bregs
217 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
218 DECLFUNC __call16_from32
219 .global __call16big_from32
223 // restore data segment limits to 0xffff
224 movl $SEG32_MODE16_DS, %eax
233 andb $~A20_ENABLE_BIT, %al
236 // Jump to 16bit mode
237 ljmpw $SEG32_MODE16_CS, $1f
242 movl $SEG32_MODE16BIG_DS, %eax
249 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
253 // Disable protected mode
258 // far jump to flush CPU queue after transition to real mode
262 // restore IDT to normal real-mode defaults
263 lidtw %cs:rmode_IDT_info
265 // Clear segment registers
271 movw %ax, %ss // Assume stack is in segment 0
278 // Return via transition32
282 // Call a 16bit function from 16bit mode with a specified cpu register state
283 // %eax = address of struct bregs
284 // Clobbers: %e[bcd]x, %e[ds]i, flags
291 // Setup for iretw call
293 pushw $1f // return point
294 pushw BREGS_flags(%eax) // flags
295 pushl BREGS_ip(%eax) // CS:IP
297 // Load calling registers.
298 movl BREGS_edi(%eax), %edi
299 movl BREGS_esi(%eax), %esi
300 movl BREGS_ebx(%eax), %ebx
301 movl BREGS_edx(%eax), %edx
302 movl BREGS_ecx(%eax), %ecx
303 movw BREGS_es(%eax), %es
304 movw BREGS_ds(%eax), %ds
305 movl %ss:BREGS_eax(%eax), %eax
308 iretw // XXX - just do a lcalll
310 // Store flags, eax, ecx
313 movl 0x06(%esp), %eax
314 movl %ecx, %ss:BREGS_ecx(%eax)
315 movw %ds, %ss:BREGS_ds(%eax)
317 movw %cx, %ds // Restore %ds == %ss
319 movl %ecx, BREGS_eax(%eax)
321 movw %cx, BREGS_flags(%eax)
323 // Store remaining registers
324 movw %es, BREGS_es(%eax)
325 movl %edi, BREGS_edi(%eax)
326 movl %esi, BREGS_esi(%eax)
327 movl %ebx, BREGS_ebx(%eax)
328 movl %edx, BREGS_edx(%eax)
330 // Remove %eax, restore %ebp
339 DECLFUNC entry_pnp_real
340 .global entry_pnp_prot
345 pushl %esp // Backup %esp, then clear high bits
348 pushfl // Save registers clobbered by C code
354 movw %ss, %cx // Move %ss to %ds
356 lea 28(%esp), %eax // %eax points to start of u16 args
358 movw %ax, 12(%esp) // Modify %eax to return %ax
369 DECLFUNC apm16protected_entry
370 apm16protected_entry:
373 ENTRY_ARG handle_1553
374 addw $4, %sp // pop dummy
375 popfw // restore flags
379 DECLFUNC apm32protected_entry
380 apm32protected_entry:
382 pushw %cs // Setup for long jump to 16bit mode
388 ENTRY_ARG_ESP handle_1553
390 movw $2f,(%esp) // Setup for long jump back to 32bit mode
395 addl $4, %esp // pop call address
399 // 32bit elf entry point
404 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
405 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
406 movl $BUILD_STACK_ADDR, %esp
407 ljmpl $SEG32_MODE32_CS, $_code32__start
412 .macro IRQ_TRAMPOLINE num
413 DECLFUNC irq_trampoline_0x\num
414 irq_trampoline_0x\num :
427 /****************************************************************
428 * Interrupt entry points
429 ****************************************************************/
431 // Define an entry point for an interrupt (no args passed).
435 cli // In case something far-calls instead of using "int"
440 // Define an entry point for an interrupt (can read/modify args).
441 .macro IRQ_ENTRY_ARG num
444 cli // In case something far-calls instead of using "int"
445 ENTRY_ARG handle_\num
449 // Macros that put each handler into its own section
450 .macro DECL_IRQ_ENTRY num
451 .section .text.asm.entry_\num
454 .macro DECL_IRQ_ENTRY_ARG num
455 .section .text.asm.entry_\num
459 DECL_IRQ_ENTRY_ARG 13
460 DECL_IRQ_ENTRY_ARG 12
461 DECL_IRQ_ENTRY_ARG 11
466 DECL_IRQ_ENTRY hwpic1
467 DECL_IRQ_ENTRY hwpic2
469 // int 18/19 are special - they reset the stack and do not return.
473 pushl $_code32_handle_19
479 pushl $_code32_handle_18
483 /****************************************************************
484 * Fixed position entry points
485 ****************************************************************/
487 // Specify a location in the fixed part of bios area.
489 .section .fixedaddr.\addr
500 .global entry_13_official
504 // 0xe401 - OldFDPT in disk.c
507 .global entry_19_official
511 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
513 // 0xe729 - BaudTable in serial.c
530 // 0xefc7 - diskette_param_table in floppy.c
537 // XXX - INT 10 Functions 0-Fh Entry Point
543 // 0xf0a4 - VideoParams in misc.c
546 .global entry_12_official
551 .global entry_11_official
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