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 GPLv3 license.
11 /****************************************************************
12 * Include of 16bit C code
13 ****************************************************************/
16 .include "out/blob.16.s"
19 /****************************************************************
21 ****************************************************************/
23 // Call a C function - this does the minimal work necessary to
24 // call into C. It sets up %ds, backs up %es, and backs up
25 // those registers that are call clobbered by the C compiler.
28 pushl %eax // Save registers clobbered by C code
33 movw %ss, %ax // Move %ss to %ds
35 pushl %esp // Backup %esp, then clear high bits
38 popl %esp // Restore %esp (including high bits)
39 popw %ds // Restore registers saved above
46 // Call a C function with current register list as an
47 // argument. This backs up the registers and sets %eax
48 // to point to the backup. On return, the registers are
49 // restored from the structure.
50 .macro ENTRY_ARG cfunc
52 pushl %eax // Save registers (matches struct bregs)
60 movw %ss, %ax // Move %ss to %ds
62 movl %esp, %ebx // Backup %esp, then zero high bits
64 movl %esp, %eax // First arg is pointer to struct bregs
66 movl %ebx, %esp // Restore %esp (including high bits)
67 popw %ds // Restore registers (from struct bregs)
77 // As above, but don't mangle %esp
78 .macro ENTRY_ARG_ESP cfunc
80 pushl %eax // Save registers (matches struct bregs)
88 movw %ss, %ax // Move %ss to %ds
90 movl %esp, %eax // First arg is pointer to struct bregs
92 popw %ds // Restore registers (from struct bregs)
102 // Macro to reset the 16bit stack
107 movl $ BUILD_STACK_ADDR , %esp
111 // Specify a location in the fixed part of bios area.
113 .section .text.fixed.addr
114 .org \addr - BUILD_START_FIXED
118 /****************************************************************
120 ****************************************************************/
126 andl $0x9fffffff, %eax
129 // init the stack pointer
132 pushl $_code32__start
134 // Fall through to transition32 function below
137 /****************************************************************
139 ****************************************************************/
141 // Place CPU into 32bit mode from 16bit mode.
142 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
152 // Set segment descriptors
153 lidt %cs:pmode_IDT_info
154 lgdt %cs:rombios32_gdt_48
161 // start protected mode code
162 ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
166 // init data segments
167 movl $SEG32_MODE32_DS, %eax
177 // Call a 16bit function from 32bit mode.
178 // %eax = address of struct bregs
179 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
180 .global __call16_from32
184 // restore data segment limits to 0xffff
185 movw $SEG32_MODE16_DS, %ax
197 // Jump to 16bit mode
198 ljmpw $SEG32_MODE16_CS, $1f
202 // reset PE bit in CR0
207 // far jump to flush CPU queue after transition to real mode
211 // restore IDT to normal real-mode defaults
212 lidt %cs:rmode_IDT_info
214 // Clear segment registers
220 movw %ax, %ss // Assume stack is in segment 0
224 // Set __call16 return address to be transition32
227 // Fall through to __call16
230 // Call a 16bit function from 16bit mode with a specified cpu register state
231 // %eax = address of struct bregs
232 // Clobbers: all gp registers, es
238 // Setup for iretw call
240 pushw $1f // return point
241 pushw 0x20(%eax) // flags
242 pushl 0x1c(%eax) // CS:IP
244 // Load calling registers.
245 movl 0x04(%eax), %edi
246 movl 0x08(%eax), %esi
247 movl 0x0c(%eax), %ebx
248 movl 0x10(%eax), %edx
249 movl 0x14(%eax), %ecx
250 movw 0x02(%eax), %es // XXX - should load %ds too
251 movl 0x18(%eax), %eax
254 iretw // XXX - just do a lcalll
256 // Store flags, eax, ecx
259 movl 0x06(%esp), %eax
260 movl %ecx, %ss:0x14(%eax) // Save %ecx
262 movw %cx, %ds // Restore %ds == %ss
264 movl %ecx, 0x18(%eax) // Save %eax
266 movw %cx, 0x20(%eax) // Save flags
268 // Store remaining registers
270 movl %edi, 0x04(%eax)
271 movl %esi, 0x08(%eax)
272 movl %ebx, 0x0c(%eax)
273 movl %edx, 0x10(%eax)
284 .global apm16protected_entry
285 apm16protected_entry:
288 ENTRY_ARG handle_1553
289 addw $4, %sp // pop dummy
290 popfw // restore flags
294 .global apm32protected_entry
295 apm32protected_entry:
297 pushw %cs // Setup for long jump to 16bit mode
303 ENTRY_ARG_ESP handle_1553
305 movw $2f,(%esp) // Setup for long jump back to 32bit mode
310 addl $4, %esp // pop call address
314 // 32bit elf entry point
319 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
320 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
321 movl $BUILD_STACK_ADDR, %esp
322 ljmpl $SEG32_MODE32_CS, $_code32__start
326 // Shutdown a CPU. We want this in the 0xf000 section to ensure that
327 // the code wont be overwritten with something else. (Should
328 // something spurious wake up the CPU, we want to be sure that the hlt
329 // insn will still be present and will shutdown the CPU.)
330 .global permanent_halt
337 /****************************************************************
339 ****************************************************************/
341 // Protected mode IDT descriptor
343 // I just make the limit 0, so the machine will shutdown
344 // if an exception occurs during protected mode memory
347 // Set base to f0000 to correspond to beginning of BIOS,
348 // in case I actually define an IDT later
351 .word 0x0000 // limit 15:00
352 .long 0xf0000 // base 16:47
354 // Real mode IDT descriptor
356 // Set to typical real-mode values.
360 .word 0x03ff // limit 15:00
361 .long 0 // base 16:47
372 // 32 bit flat code segment (SEG32_MODE32_CS)
373 .word 0xffff, 0, 0x9b00, 0x00cf
374 // 32 bit flat data segment (SEG32_MODE32_DS)
375 .word 0xffff, 0, 0x9300, 0x00cf
376 // 16 bit code segment base=0xf0000 limit=0xffff (SEG32_MODE16_CS)
377 .word 0xffff, 0, 0x9b0f, 0x0000
378 // 16 bit data segment base=0x0 limit=0xffff (SEG32_MODE16_DS)
379 .word 0xffff, 0, 0x9300, 0x0000
381 // We need a copy of this string in the 0xf000 segment, but we are not
382 // actually a PnP BIOS, so make sure it is *not* aligned, so OSes will
383 // not see it if they scan.
391 /****************************************************************
392 * Interrupt entry points
393 ****************************************************************/
395 // Define an entry point for an interrupt (no args passed).
399 cli // In case something far-calls instead of using "int"
404 // Define an entry point for an interrupt (can read/modify args).
405 .macro IRQ_ENTRY_ARG num
408 cli // In case something far-calls instead of using "int"
409 ENTRY_ARG handle_\num
427 // XXX - Fixed Disk Parameter Table
433 .include "out/cbt.proc.16.s"
437 // XXX - Baud Rate Generator Table
445 // int 18/19 are special - they reset the stack and do not return.
449 pushl $_code32_handle_19
455 pushl $_code32_handle_18
459 .macro IRQ_TRAMPOLINE num
460 .global irq_trampoline_0x\num
461 irq_trampoline_0x\num :
492 .include "out/floppy_dbt.proc.16.s"
509 .global freespace2_start, freespace2_end
523 .include "out/font.proc.16.s"
533 // XXX - Initial Interrupt Vector Offsets Loaded by POST
536 // XXX - BIOS_COPYRIGHT_STRING
537 .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
540 .global dummy_iret_handler
547 ORG 0xfff0 // Power-up Entry Point
548 ljmpw $SEG_BIOS, $post16
555 .byte CONFIG_MODEL_ID
557 .global bios_checksum