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 // Specify a location in the fixed part of bios area.
117 .section .fixedaddr.\addr
121 /****************************************************************
123 ****************************************************************/
129 andl $~(CR0_CD|CR0_NW), %eax
132 // Check for restart indicator.
133 movl $CMOS_RESET_CODE, %eax
134 outb %al, $PORT_CMOS_INDEX
135 inb $PORT_CMOS_DATA, %al
139 // Normal entry point
142 pushl $_code32__start
144 // Fall through to transition32 function below
147 /****************************************************************
149 ****************************************************************/
151 // Place CPU into 32bit mode from 16bit mode.
152 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
159 orb $A20_ENABLE_BIT, %al
162 // Set segment descriptors
163 lidtw %cs:pmode_IDT_info
164 lgdtw %cs:rombios32_gdt_48
166 // Enable protected mode
171 // start 32bit protected mode code
172 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
176 // init data segments
177 movl $SEG32_MODE32_DS, %eax
186 // Call a 16bit function from 32bit mode.
187 // %eax = address of struct bregs
188 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
189 .global __call16_from32, __call16big_from32
193 // restore data segment limits to 0xffff
194 movl $SEG32_MODE16_DS, %eax
203 andb $~A20_ENABLE_BIT, %al
206 // Jump to 16bit mode
207 ljmpw $SEG32_MODE16_CS, $1f
212 movl $SEG32_MODE16BIG_DS, %eax
219 ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
223 // Disable protected mode
228 // far jump to flush CPU queue after transition to real mode
232 // restore IDT to normal real-mode defaults
233 lidtw %cs:rmode_IDT_info
235 // Clear segment registers
241 movw %ax, %ss // Assume stack is in segment 0
245 // Set __call16 return address to be transition32
248 // Fall through to __call16
251 // Call a 16bit function from 16bit mode with a specified cpu register state
252 // %eax = address of struct bregs
253 // Clobbers: all gp registers, es
259 // Setup for iretw call
261 pushw $1f // return point
262 pushw BREGS_flags(%eax) // flags
263 pushl BREGS_ip(%eax) // CS:IP
265 // Load calling registers.
266 movl BREGS_edi(%eax), %edi
267 movl BREGS_esi(%eax), %esi
268 movl BREGS_ebx(%eax), %ebx
269 movl BREGS_edx(%eax), %edx
270 movl BREGS_ecx(%eax), %ecx
271 movw BREGS_es(%eax), %es
272 movw BREGS_ds(%eax), %ds
273 movl %ss:BREGS_eax(%eax), %eax
276 iretw // XXX - just do a lcalll
278 // Store flags, eax, ecx
281 movl 0x06(%esp), %eax
282 movl %ecx, %ss:BREGS_ecx(%eax)
283 movw %ds, %ss:BREGS_ds(%eax)
285 movw %cx, %ds // Restore %ds == %ss
287 movl %ecx, BREGS_eax(%eax)
289 movw %cx, BREGS_flags(%eax)
291 // Store remaining registers
292 movw %es, BREGS_es(%eax)
293 movl %edi, BREGS_edi(%eax)
294 movl %esi, BREGS_esi(%eax)
295 movl %ebx, BREGS_ebx(%eax)
296 movl %edx, BREGS_edx(%eax)
305 // Entry point when a post call looks like a resume.
306 // %eax = shutdown status from cmos
308 // Save old shutdown status.
311 // Clear shutdown status register.
312 movl $CMOS_RESET_CODE, %eax
313 outb %al, $PORT_CMOS_INDEX
315 outb %al, $PORT_CMOS_DATA
317 // Use a stack in EBDA
320 movw BDA_ebda_seg, %ax
323 movl $EBDA_OFFSET_TOP_STACK, %esp
332 .global entry_pnp_real, entry_pnp_prot
337 pushl %esp // Backup %esp, then clear high bits
340 pushfl // Save registers clobbered by C code
346 movw %ss, %cx // Move %ss to %ds
348 lea 28(%esp), %eax // %eax points to start of u16 args
350 movw %ax, 12(%esp) // Modify %eax to return %ax
361 .global apm16protected_entry
362 apm16protected_entry:
365 ENTRY_ARG handle_1553
366 addw $4, %sp // pop dummy
367 popfw // restore flags
371 .global apm32protected_entry
372 apm32protected_entry:
374 pushw %cs // Setup for long jump to 16bit mode
380 ENTRY_ARG_ESP handle_1553
382 movw $2f,(%esp) // Setup for long jump back to 32bit mode
387 addl $4, %esp // pop call address
391 // 32bit elf entry point
396 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
397 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
398 movl $BUILD_STACK_ADDR, %esp
399 ljmpl $SEG32_MODE32_CS, $_code32__start
403 // Shutdown a CPU. We want this in the 0xf000 section to ensure that
404 // the code wont be overwritten with something else. (Should
405 // something spurious wake up the CPU, we want to be sure that the hlt
406 // insn will still be present and will shutdown the CPU.)
407 .global permanent_halt
414 .macro IRQ_TRAMPOLINE num
415 .global irq_trampoline_0x\num
416 irq_trampoline_0x\num :
429 /****************************************************************
430 * Interrupt entry points
431 ****************************************************************/
433 // Define an entry point for an interrupt (no args passed).
437 cli // In case something far-calls instead of using "int"
442 // Define an entry point for an interrupt (can read/modify args).
443 .macro IRQ_ENTRY_ARG num
446 cli // In case something far-calls instead of using "int"
447 ENTRY_ARG handle_\num
454 /****************************************************************
455 * GDT and IDT tables (between 0xe2c3 - 0xe3fe)
456 ****************************************************************/
458 // Protected mode IDT descriptor
460 // I just make the limit 0, so the machine will shutdown
461 // if an exception occurs during protected mode memory
464 // Set base to f0000 to correspond to beginning of BIOS,
465 // in case I actually define an IDT later
467 .type pmode_IDT_info, @object
469 .word 0x0000 // limit 15:00
470 .long 0xf0000 // base 16:47
472 // Real mode IDT descriptor
474 // Set to typical real-mode values.
477 .type rmode_IDT_info, @object
479 .word 0x03ff // limit 15:00
480 .long 0 // base 16:47
482 .type rombios32_gdt_48, @object
484 .word (rombios32_gdt_end - rombios32_gdt)
485 .long (BUILD_BIOS_ADDR + rombios32_gdt)
488 .type rombios32_gdt, @object
492 // 32 bit flat code segment (SEG32_MODE32_CS)
493 .word 0xffff, 0, 0x9b00, 0x00cf
494 // 32 bit flat data segment (SEG32_MODE32_DS)
495 .word 0xffff, 0, 0x9300, 0x00cf
496 // 16 bit code segment base=0xf0000 limit=0xffff (SEG32_MODE16_CS)
497 .word 0xffff, 0, 0x9b0f, 0x0000
498 // 16 bit data segment base=0x0 limit=0xffff (SEG32_MODE16_DS)
499 .word 0xffff, 0, 0x9300, 0x0000
500 // 16 bit code segment base=0 limit=0xffffffff (SEG32_MODE16BIG_CS)
501 .word 0xffff, 0, 0x9b00, 0x008f
502 // 16 bit data segment base=0 limit=0xffffffff (SEG32_MODE16BIG_DS)
503 .word 0xffff, 0, 0x9300, 0x008f
507 /****************************************************************
508 * Interrupt entry points (continued)
509 ****************************************************************/
512 .global entry_13_official
517 .type __fdpt, @object
519 // XXX - Fixed Disk Parameter Table
523 .global entry_19_official
528 .include "out/cbt.proc.16.s"
532 .type __brgt, @object
534 // XXX - Baud Rate Generator Table
553 .include "out/floppy_dbt.proc.16.s"
561 // XXX - INT 10 Functions 0-Fh Entry Point
568 .type __int1d, @object
570 // XXX - INT 1D - SYSTEM DATA - VIDEO PARAMETER TABLES
574 .global entry_12_official
579 .global entry_11_official
586 // Fit other misc defs if the freespace between 0xf859-0xfa6e
599 // int 18/19 are special - they reset the stack and do not return.
602 pushl $_code32_handle_19
608 pushl $_code32_handle_18
612 .include "out/font.proc.16.s"
623 // XXX - Initial Interrupt Vector Offsets Loaded by POST
627 .type __copyright, @object
629 // XXX - BIOS_COPYRIGHT_STRING
630 .asciz "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
633 .global dummy_iret_handler
640 ORG 0xfff0 // Power-up Entry Point
643 ljmpw $SEG_BIOS, $post16
646 .type __biosdate, @object
652 .type __model_id, @object
654 .byte CONFIG_MODEL_ID
656 .global bios_checksum
657 .type bios_checksum, @object