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: %eax, flags, stack registers, cr0, idt/gdt
178 DECLFUNC transition32
185 orb $A20_ENABLE_BIT, %al
188 // Set segment descriptors
189 lidtw %cs:pmode_IDT_info
190 lgdtw %cs:rombios32_gdt_48
192 // Enable protected mode
197 // start 32bit protected mode code
198 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
202 // init data segments
203 movl $SEG32_MODE32_DS, %eax
212 // Call a 16bit function from 32bit mode.
213 // %eax = address of struct bregs
214 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
215 DECLFUNC __call16_from32
216 .global __call16big_from32
220 // restore data segment limits to 0xffff
221 movl $SEG32_MODE16_DS, %eax
230 andb $~A20_ENABLE_BIT, %al
233 // Jump to 16bit mode
234 ljmpw $SEG32_MODE16_CS, $1f
239 movl $SEG32_MODE16BIG_DS, %eax
246 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
250 // Disable protected mode
255 // far jump to flush CPU queue after transition to real mode
259 // restore IDT to normal real-mode defaults
260 lidtw %cs:rmode_IDT_info
262 // Clear segment registers
268 movw %ax, %ss // Assume stack is in segment 0
275 // Return via transition32
279 // Call a 16bit function from 16bit mode with a specified cpu register state
280 // %eax = address of struct bregs
281 // Clobbers: all gp registers, es
287 // Setup for iretw call
289 pushw $1f // return point
290 pushw BREGS_flags(%eax) // flags
291 pushl BREGS_ip(%eax) // CS:IP
293 // Load calling registers.
294 movl BREGS_edi(%eax), %edi
295 movl BREGS_esi(%eax), %esi
296 movl BREGS_ebx(%eax), %ebx
297 movl BREGS_edx(%eax), %edx
298 movl BREGS_ecx(%eax), %ecx
299 movw BREGS_es(%eax), %es
300 movw BREGS_ds(%eax), %ds
301 movl %ss:BREGS_eax(%eax), %eax
304 iretw // XXX - just do a lcalll
306 // Store flags, eax, ecx
309 movl 0x06(%esp), %eax
310 movl %ecx, %ss:BREGS_ecx(%eax)
311 movw %ds, %ss:BREGS_ds(%eax)
313 movw %cx, %ds // Restore %ds == %ss
315 movl %ecx, BREGS_eax(%eax)
317 movw %cx, BREGS_flags(%eax)
319 // Store remaining registers
320 movw %es, BREGS_es(%eax)
321 movl %edi, BREGS_edi(%eax)
322 movl %esi, BREGS_esi(%eax)
323 movl %ebx, BREGS_ebx(%eax)
324 movl %edx, BREGS_edx(%eax)
334 DECLFUNC entry_pnp_real
335 .global entry_pnp_prot
340 pushl %esp // Backup %esp, then clear high bits
343 pushfl // Save registers clobbered by C code
349 movw %ss, %cx // Move %ss to %ds
351 lea 28(%esp), %eax // %eax points to start of u16 args
353 movw %ax, 12(%esp) // Modify %eax to return %ax
364 DECLFUNC apm16protected_entry
365 apm16protected_entry:
368 ENTRY_ARG handle_1553
369 addw $4, %sp // pop dummy
370 popfw // restore flags
374 DECLFUNC apm32protected_entry
375 apm32protected_entry:
377 pushw %cs // Setup for long jump to 16bit mode
383 ENTRY_ARG_ESP handle_1553
385 movw $2f,(%esp) // Setup for long jump back to 32bit mode
390 addl $4, %esp // pop call address
394 // 32bit elf entry point
399 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
400 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
401 movl $BUILD_STACK_ADDR, %esp
402 ljmpl $SEG32_MODE32_CS, $_code32__start
406 // Shutdown a CPU. We want this in the 0xf000 section to ensure that
407 // the code wont be overwritten with something else. (Should
408 // something spurious wake up the CPU, we want to be sure that the hlt
409 // insn will still be present and will shutdown the CPU.)
410 DECLFUNC permanent_halt
417 .macro IRQ_TRAMPOLINE num
418 DECLFUNC irq_trampoline_0x\num
419 irq_trampoline_0x\num :
432 /****************************************************************
433 * Interrupt entry points
434 ****************************************************************/
436 // Define an entry point for an interrupt (no args passed).
440 cli // In case something far-calls instead of using "int"
445 // Define an entry point for an interrupt (can read/modify args).
446 .macro IRQ_ENTRY_ARG num
449 cli // In case something far-calls instead of using "int"
450 ENTRY_ARG handle_\num
454 // Macros that put each handler into its own section
455 .macro DECL_IRQ_ENTRY num
456 .section .text.asm.entry_\num
459 .macro DECL_IRQ_ENTRY_ARG num
460 .section .text.asm.entry_\num
464 DECL_IRQ_ENTRY_ARG 13
465 DECL_IRQ_ENTRY_ARG 12
466 DECL_IRQ_ENTRY_ARG 11
471 DECL_IRQ_ENTRY hwpic1
472 DECL_IRQ_ENTRY hwpic2
474 // int 18/19 are special - they reset the stack and do not return.
478 pushl $_code32_handle_19
484 pushl $_code32_handle_18
488 /****************************************************************
489 * Fixed position entry points
490 ****************************************************************/
492 // Specify a location in the fixed part of bios area.
494 .section .fixedaddr.\addr
505 .global entry_13_official
509 // 0xe401 - OldFDPT in disk.c
512 .global entry_19_official
516 // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
518 // 0xe729 - BaudTable in serial.c
535 // 0xefc7 - diskette_param_table in floppy.c
542 // XXX - INT 10 Functions 0-Fh Entry Point
548 // 0xf0a4 - VideoParams in misc.c
551 .global entry_12_official
556 .global entry_11_official
563 // 0xfa6e - vgafont8 in font.c
571 // 0xfef3 - InitVectors in misc.c
573 // 0xff00 - BiosCopyright in misc.c
576 .global entry_iret_official
583 ORG 0xfff0 // Power-up Entry Point
586 ljmpw $SEG_BIOS, $entry_post_official
588 // 0xfff5 - BiosDate in misc.c
590 // 0xfffe - BiosModelId in misc.c
592 // 0xffff - BiosChecksum in misc.c