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, %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, %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
196 // Enable protected mode
201 // start 32bit protected mode code
202 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
206 // init data segments
207 movl $SEG32_MODE32_DS, %eax
217 // Call a 16bit function from 32bit mode.
218 // %eax = address of struct bregs
219 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
220 DECLFUNC __call16_from32
221 .global __call16big_from32
225 // restore data segment limits to 0xffff
226 movl $SEG32_MODE16_DS, %eax
235 andb $~A20_ENABLE_BIT, %al
238 // Jump to 16bit mode
239 ljmpw $SEG32_MODE16_CS, $1f
244 movl $SEG32_MODE16BIG_DS, %eax
251 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
255 // Disable protected mode
260 // far jump to flush CPU queue after transition to real mode
264 // restore IDT to normal real-mode defaults
265 lidtw %cs:rmode_IDT_info
267 // Clear segment registers
273 movw %ax, %ss // Assume stack is in segment 0
280 // Return via transition32
284 // Call a 16bit function from 16bit mode with a specified cpu register state
285 // %eax = address of struct bregs
286 // Clobbers: %e[bcd]x, %e[ds]i, flags
293 // Setup for iretw call
295 pushw $1f // return point
296 pushw BREGS_flags(%eax) // flags
297 pushl BREGS_ip(%eax) // CS:IP
299 // Load calling registers.
300 movl BREGS_edi(%eax), %edi
301 movl BREGS_esi(%eax), %esi
302 movl BREGS_ebx(%eax), %ebx
303 movl BREGS_edx(%eax), %edx
304 movl BREGS_ecx(%eax), %ecx
305 movw BREGS_es(%eax), %es
306 movw BREGS_ds(%eax), %ds
307 movl %ss:BREGS_eax(%eax), %eax
310 iretw // XXX - just do a lcalll
312 // Store flags, eax, ecx
315 movl 0x06(%esp), %eax
316 movl %ecx, %ss:BREGS_ecx(%eax)
317 movw %ds, %ss:BREGS_ds(%eax)
319 movw %cx, %ds // Restore %ds == %ss
321 movl %ecx, BREGS_eax(%eax)
323 movw %cx, BREGS_flags(%eax)
325 // Store remaining registers
326 movw %es, BREGS_es(%eax)
327 movl %edi, BREGS_edi(%eax)
328 movl %esi, BREGS_esi(%eax)
329 movl %ebx, BREGS_ebx(%eax)
330 movl %edx, BREGS_edx(%eax)
332 // Remove %eax, restore %ebp
342 DECLFUNC entry_pnp_real
343 .global entry_pnp_prot
348 pushl %esp // Backup %esp, then clear high bits
351 pushfl // Save registers clobbered by C code
357 movw %ss, %cx // Move %ss to %ds
359 lea 28(%esp), %eax // %eax points to start of u16 args
361 movw %ax, 12(%esp) // Modify %eax to return %ax
372 DECLFUNC apm16protected_entry
373 apm16protected_entry:
376 ENTRY_ARG handle_1553
377 addw $4, %sp // pop dummy
378 popfw // restore flags
382 DECLFUNC apm32protected_entry
383 apm32protected_entry:
385 pushw %cs // Setup for long jump to 16bit mode
391 ENTRY_ARG_ESP handle_1553
393 movw $2f,(%esp) // Setup for long jump back to 32bit mode
398 addl $4, %esp // pop call address
402 // 32bit elf entry point
407 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
408 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
409 movl $BUILD_STACK_ADDR, %esp
410 ljmpl $SEG32_MODE32_CS, $_code32__start
415 .macro IRQ_TRAMPOLINE num
416 DECLFUNC irq_trampoline_0x\num
417 irq_trampoline_0x\num :
430 /****************************************************************
431 * Interrupt entry points
432 ****************************************************************/
434 // Define an entry point for an interrupt (no args passed).
442 // Define an entry point for an interrupt (can read/modify args).
443 .macro IRQ_ENTRY_ARG num
446 ENTRY_ARG handle_\num
450 // Macros that put each handler into its own section
451 .macro DECL_IRQ_ENTRY num
452 .section .text.asm.entry_\num
455 .macro DECL_IRQ_ENTRY_ARG num
456 .section .text.asm.entry_\num
460 DECL_IRQ_ENTRY_ARG 13
461 DECL_IRQ_ENTRY_ARG 12
462 DECL_IRQ_ENTRY_ARG 11
467 DECL_IRQ_ENTRY hwpic1
468 DECL_IRQ_ENTRY hwpic2
470 // int 18/19 are special - they reset stack and call into 32bit mode.
475 ENTRY_INTO32 _code32_handle_19
481 ENTRY_INTO32 _code32_handle_18
484 /****************************************************************
485 * Fixed position entry points
486 ****************************************************************/
488 // Specify a location in the fixed part of bios area.
490 .section .fixedaddr.\addr
501 .global entry_13_official
505 // 0xe401 - OldFDPT in disk.c
508 .global entry_19_official
512 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
514 // 0xe729 - BaudTable in serial.c
531 // 0xefc7 - diskette_param_table in floppy.c
538 // XXX - INT 10 Functions 0-Fh Entry Point
544 // 0xf0a4 - VideoParams in misc.c
547 .global entry_12_official
552 .global entry_11_official
559 // 0xfa6e - vgafont8 in font.c
567 // 0xfef3 - InitVectors in misc.c
569 // 0xff00 - BiosCopyright in misc.c
572 .global entry_iret_official
579 ORG 0xfff0 // Power-up Entry Point
582 ljmpw $SEG_BIOS, $entry_post_official
584 // 0xfff5 - BiosDate in misc.c
586 // 0xfffe - BiosModelId in misc.c
588 // 0xffff - BiosChecksum in misc.c