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.
10 #define PROTECTED_MODE_CS (2 << 3) // 0x10
11 #define PROTECTED_MODE_DS (3 << 3) // 0x18
12 #define REAL_MODE_CS (4 << 3) // 0x20
13 #define REAL_MODE_DS (5 << 3) // 0x28
16 /****************************************************************
17 * Include of 16bit C code
18 ****************************************************************/
21 .include "out/blob.16.s"
24 /****************************************************************
26 ****************************************************************/
28 // Call a C function - this does the minimal work necessary to
29 // call into C. It sets up %ds, backs up %es, and backs up
30 // those registers that are call clobbered by the C compiler.
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
57 pushl %eax // Save registers (matches struct bregs)
65 movw %ss, %ax // Move %ss to %ds
67 movl %esp, %ebx // Backup %esp, then zero high bits
69 movl %esp, %eax // First arg is pointer to struct bregs
71 movl %ebx, %esp // Restore %esp (including high bits)
72 popw %ds // Restore registers (from struct bregs)
82 // As above, but don't mangle %esp
83 .macro ENTRY_ARG_ESP cfunc
85 pushl %eax // Save registers (matches struct bregs)
93 movw %ss, %ax // Move %ss to %ds
95 movl %esp, %eax // First arg is pointer to struct bregs
97 popw %ds // Restore registers (from struct bregs)
107 // Macro to reset the 16bit stack
112 movl $ BUILD_STACK_ADDR , %esp
116 // Specify a location in the fixed part of bios area.
118 .section .text.fixed.addr
119 .org \addr - BUILD_START_FIXED
123 /****************************************************************
125 ****************************************************************/
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 $PROTECTED_MODE_CS, $(BUILD_BIOS_ADDR + 1f)
166 // init data segments
167 movl $PROTECTED_MODE_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 // Jump to 16bit mode
185 ljmpw $REAL_MODE_CS, $1f
189 // restore data segment limits to 0xffff
190 movw $REAL_MODE_DS, %ax
197 // reset PE bit in CR0
202 // far jump to flush CPU queue after transition to real mode
206 // restore IDT to normal real-mode defaults
207 lidt %cs:rmode_IDT_info
209 // Clear segment registers
215 movw %ax, %ss // Assume stack is in segment 0
219 // Set __call16 return address to be transition32
222 // Fall through to __call16
225 // Call a 16bit function from 16bit mode with a specified cpu register state
226 // %eax = address of struct bregs
227 // Clobbers: all gp registers, es
233 // Setup for iretw call
235 pushw $1f // return point
236 pushw 0x20(%eax) // flags
237 pushl 0x1c(%eax) // CS:IP
239 // Load calling registers.
240 movl 0x04(%eax), %edi
241 movl 0x08(%eax), %esi
242 movl 0x0c(%eax), %ebx
243 movl 0x10(%eax), %edx
244 movl 0x14(%eax), %ecx
245 movw 0x02(%eax), %es // XXX - should load %ds too
246 movl 0x18(%eax), %eax
249 iretw // XXX - just do a lcalll
251 // Store flags, eax, ecx
254 movl 0x06(%esp), %eax
255 movl %ecx, %ss:0x14(%eax) // Save %ecx
257 movw %cx, %ds // Restore %ds == %ss
259 movl %ecx, 0x18(%eax) // Save %eax
261 movw %cx, 0x20(%eax) // Save flags
263 // Store remaining registers
265 movl %edi, 0x04(%eax)
266 movl %esi, 0x08(%eax)
267 movl %ebx, 0x0c(%eax)
268 movl %edx, 0x10(%eax)
279 .global apm16protected_entry
280 apm16protected_entry:
283 ENTRY_ARG handle_1553
284 addw $4, %sp // pop dummy
285 popfw // restore flags
289 .global apm32protected_entry
290 apm32protected_entry:
292 pushw %cs // Setup for long jump to 16bit mode
298 ENTRY_ARG_ESP handle_1553
300 movw $2f,(%esp) // Setup for long jump back to 32bit mode
305 addl $4, %esp // pop call address
309 // 32bit elf entry point
314 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
315 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
316 movl $BUILD_STACK_ADDR, %esp
317 ljmpl $PROTECTED_MODE_CS, $_code32__start
322 /****************************************************************
324 ****************************************************************/
326 // Protected mode IDT descriptor
328 // I just make the limit 0, so the machine will shutdown
329 // if an exception occurs during protected mode memory
332 // Set base to f0000 to correspond to beginning of BIOS,
333 // in case I actually define an IDT later
336 .word 0x0000 // limit 15:00
337 .long 0xf0000 // base 16:47
339 // Real mode IDT descriptor
341 // Set to typical real-mode values.
345 .word 0x03ff // limit 15:00
346 .long 0 // base 16:47
357 // 32 bit flat code segment (PROTECTED_MODE_CS)
358 .word 0xffff, 0, 0x9b00, 0x00cf
359 // 32 bit flat data segment (PROTECTED_MODE_DS)
360 .word 0xffff, 0, 0x9300, 0x00cf
361 // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
362 .word 0xffff, 0, 0x9b0f, 0x0000
363 // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
364 .word 0xffff, 0, 0x9300, 0x0000
366 // We need a copy of this string in the 0xf000 segment, but we are not
367 // actually a PnP BIOS, so make sure it is *not* aligned, so OSes will
368 // not see it if they scan.
376 /****************************************************************
377 * Interrupt entry points
378 ****************************************************************/
380 // Define an entry point for an interrupt (no args passed).
384 cli // In case something far-calls instead of using "int"
389 // Define an entry point for an interrupt (can read/modify args).
390 .macro IRQ_ENTRY_ARG num
393 cli // In case something far-calls instead of using "int"
394 ENTRY_ARG handle_\num
412 // XXX - Fixed Disk Parameter Table
418 .include "out/cbt.proc.16.s"
422 // XXX - Baud Rate Generator Table
430 // int 18/19 are special - they reset the stack and do not return.
434 pushl $_code32_handle_19
440 pushl $_code32_handle_18
444 .macro IRQ_TRAMPOLINE num
445 .global irq_trampoline_0x\num
446 irq_trampoline_0x\num :
477 .include "out/floppy_dbt.proc.16.s"
494 .global freespace2_start, freespace2_end
508 .include "out/font.proc.16.s"
518 // XXX - Initial Interrupt Vector Offsets Loaded by POST
521 // XXX - BIOS_COPYRIGHT_STRING
522 .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
525 .global dummy_iret_handler
532 ORG 0xfff0 // Power-up Entry Point
533 ljmpw $SEG_BIOS, $post16
540 .byte CONFIG_MODEL_ID
542 .global bios_checksum