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
162 movl $EBDA_OFFSET_TOP_STACK, %esp
171 /****************************************************************
173 ****************************************************************/
175 // Place CPU into 32bit mode from 16bit mode.
176 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
177 DECLFUNC transition32
184 orb $A20_ENABLE_BIT, %al
187 // Set segment descriptors
188 lidtw %cs:pmode_IDT_info
189 lgdtw %cs:rombios32_gdt_48
191 // Enable protected mode
196 // start 32bit protected mode code
197 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
201 // init data segments
202 movl $SEG32_MODE32_DS, %eax
211 // Call a 16bit function from 32bit mode.
212 // %eax = address of struct bregs
213 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
214 DECLFUNC __call16_from32
215 .global __call16big_from32
219 // restore data segment limits to 0xffff
220 movl $SEG32_MODE16_DS, %eax
229 andb $~A20_ENABLE_BIT, %al
232 // Jump to 16bit mode
233 ljmpw $SEG32_MODE16_CS, $1f
238 movl $SEG32_MODE16BIG_DS, %eax
245 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
249 // Disable protected mode
254 // far jump to flush CPU queue after transition to real mode
258 // restore IDT to normal real-mode defaults
259 lidtw %cs:rmode_IDT_info
261 // Clear segment registers
267 movw %ax, %ss // Assume stack is in segment 0
271 // Set __call16 return address to be transition32
274 // Fall through to __call16
277 // Call a 16bit function from 16bit mode with a specified cpu register state
278 // %eax = address of struct bregs
279 // Clobbers: all gp registers, es
285 // Setup for iretw call
287 pushw $1f // return point
288 pushw BREGS_flags(%eax) // flags
289 pushl BREGS_ip(%eax) // CS:IP
291 // Load calling registers.
292 movl BREGS_edi(%eax), %edi
293 movl BREGS_esi(%eax), %esi
294 movl BREGS_ebx(%eax), %ebx
295 movl BREGS_edx(%eax), %edx
296 movl BREGS_ecx(%eax), %ecx
297 movw BREGS_es(%eax), %es
298 movw BREGS_ds(%eax), %ds
299 movl %ss:BREGS_eax(%eax), %eax
302 iretw // XXX - just do a lcalll
304 // Store flags, eax, ecx
307 movl 0x06(%esp), %eax
308 movl %ecx, %ss:BREGS_ecx(%eax)
309 movw %ds, %ss:BREGS_ds(%eax)
311 movw %cx, %ds // Restore %ds == %ss
313 movl %ecx, BREGS_eax(%eax)
315 movw %cx, BREGS_flags(%eax)
317 // Store remaining registers
318 movw %es, BREGS_es(%eax)
319 movl %edi, BREGS_edi(%eax)
320 movl %esi, BREGS_esi(%eax)
321 movl %ebx, BREGS_ebx(%eax)
322 movl %edx, BREGS_edx(%eax)
332 DECLFUNC entry_pnp_real
333 .global entry_pnp_prot
338 pushl %esp // Backup %esp, then clear high bits
341 pushfl // Save registers clobbered by C code
347 movw %ss, %cx // Move %ss to %ds
349 lea 28(%esp), %eax // %eax points to start of u16 args
351 movw %ax, 12(%esp) // Modify %eax to return %ax
362 DECLFUNC apm16protected_entry
363 apm16protected_entry:
366 ENTRY_ARG handle_1553
367 addw $4, %sp // pop dummy
368 popfw // restore flags
372 DECLFUNC apm32protected_entry
373 apm32protected_entry:
375 pushw %cs // Setup for long jump to 16bit mode
381 ENTRY_ARG_ESP handle_1553
383 movw $2f,(%esp) // Setup for long jump back to 32bit mode
388 addl $4, %esp // pop call address
392 // 32bit elf entry point
397 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
398 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
399 movl $BUILD_STACK_ADDR, %esp
400 ljmpl $SEG32_MODE32_CS, $_code32__start
404 // Shutdown a CPU. We want this in the 0xf000 section to ensure that
405 // the code wont be overwritten with something else. (Should
406 // something spurious wake up the CPU, we want to be sure that the hlt
407 // insn will still be present and will shutdown the CPU.)
408 DECLFUNC permanent_halt
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).
438 cli // In case something far-calls instead of using "int"
443 // Define an entry point for an interrupt (can read/modify args).
444 .macro IRQ_ENTRY_ARG num
447 cli // In case something far-calls instead of using "int"
448 ENTRY_ARG handle_\num
452 // Macros that put each handler into its own section
453 .macro DECL_IRQ_ENTRY num
454 .section .text.asm.entry_\num
457 .macro DECL_IRQ_ENTRY_ARG num
458 .section .text.asm.entry_\num
462 DECL_IRQ_ENTRY_ARG 13
463 DECL_IRQ_ENTRY_ARG 12
464 DECL_IRQ_ENTRY_ARG 11
470 DECL_IRQ_ENTRY hwpic1
471 DECL_IRQ_ENTRY hwpic2
473 // int 18/19 are special - they reset the stack and do not return.
477 pushl $_code32_handle_19
483 pushl $_code32_handle_18
487 /****************************************************************
488 * Fixed position entry points
489 ****************************************************************/
491 // Specify a location in the fixed part of bios area.
493 .section .fixedaddr.\addr
504 .global entry_13_official
508 // 0xe401 - OldFDPT in disk.c
511 .global entry_19_official
515 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
517 // 0xe729 - BaudTable in serial.c
534 // 0xefc7 - diskette_param_table in floppy.c
541 // XXX - INT 10 Functions 0-Fh Entry Point
547 // 0xf0a4 - VideoParams in misc.c
550 .global entry_12_official
555 .global entry_11_official
562 // 0xfa6e - vgafont8 in font.c
570 // 0xfef3 - InitVectors in misc.c
572 // 0xff00 - BiosCopyright in misc.c
575 .global entry_iret_official
582 ORG 0xfff0 // Power-up Entry Point
585 ljmpw $SEG_BIOS, $entry_post_official
587 // 0xfff5 - BiosDate in misc.c
589 // 0xfffe - BiosModelId in misc.c
591 // 0xffff - BiosChecksum in misc.c