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
115 // Specify a location in the fixed part of bios area.
117 .section .text.fixed.addr
118 .org \addr - BUILD_START_FIXED
122 /****************************************************************
124 ****************************************************************/
128 // init the stack pointer
131 pushl $_code32__start
135 // Fall through to transition32 function below
138 /****************************************************************
140 ****************************************************************/
142 // Place CPU into 32bit mode from 16bit mode.
143 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
153 // Set segment descriptors
154 lidt %cs:pmode_IDT_info
155 lgdt %cs:rombios32_gdt_48
162 // start protected mode code
163 ljmpl $PROTECTED_MODE_CS, $(BUILD_BIOS_ADDR + 1f)
167 // init data segments
168 movl $PROTECTED_MODE_DS, %eax
178 // Call a 16bit function from 32bit mode.
179 // %eax = address of struct bregs
180 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
181 .global __call16_from32
185 // Jump to 16bit mode
186 ljmpw $REAL_MODE_CS, $1f
190 // restore data segment limits to 0xffff
191 movw $REAL_MODE_DS, %ax
198 // reset PE bit in CR0
203 // far jump to flush CPU queue after transition to real mode
207 // restore IDT to normal real-mode defaults
208 lidt %cs:rmode_IDT_info
210 // Clear segment registers
216 movw %ax, %ss // Assume stack is in segment 0
220 // Set __call16 return address to be transition32
223 // Fall through to __call16
226 // Call a 16bit function from 16bit mode with a specified cpu register state
227 // %eax = address of struct bregs
228 // Clobbers: all gp registers, es
234 // Setup for iretw call
236 pushw $1f // return point
237 pushw 0x20(%eax) // flags
238 pushl 0x1c(%eax) // CS:IP
240 // Load calling registers.
241 movl 0x04(%eax), %edi
242 movl 0x08(%eax), %esi
243 movl 0x0c(%eax), %ebx
244 movl 0x10(%eax), %edx
245 movl 0x14(%eax), %ecx
246 movw 0x02(%eax), %es // XXX - should load %ds too
247 movl 0x18(%eax), %eax
250 iretw // XXX - just do a lcalll
252 // Store flags, eax, ecx
255 movl 0x06(%esp), %eax
256 movl %ecx, %ss:0x14(%eax) // Save %ecx
258 movw %cx, %ds // Restore %ds == %ss
260 movl %ecx, 0x18(%eax) // Save %eax
262 movw %cx, 0x20(%eax) // Save flags
264 // Store remaining registers
266 movl %edi, 0x04(%eax)
267 movl %esi, 0x08(%eax)
268 movl %ebx, 0x0c(%eax)
269 movl %edx, 0x10(%eax)
280 .global apm16protected_entry
281 apm16protected_entry:
284 ENTRY_ARG handle_1553
285 addw $4, %sp // pop dummy
286 popfw // restore flags
290 .global apm32protected_entry
291 apm32protected_entry:
293 pushw %cs // Setup for long jump to 16bit mode
299 ENTRY_ARG_ESP handle_1553
301 movw $2f,(%esp) // Setup for long jump back to 32bit mode
306 addl $4, %esp // pop call address
310 // 32bit elf entry point
315 lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
316 lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
317 movl $BUILD_STACK_ADDR, %esp
318 ljmpl $PROTECTED_MODE_CS, $_code32__start
323 /****************************************************************
325 ****************************************************************/
327 // Protected mode IDT descriptor
329 // I just make the limit 0, so the machine will shutdown
330 // if an exception occurs during protected mode memory
333 // Set base to f0000 to correspond to beginning of BIOS,
334 // in case I actually define an IDT later
337 .word 0x0000 // limit 15:00
338 .long 0xf0000 // base 16:47
340 // Real mode IDT descriptor
342 // Set to typical real-mode values.
346 .word 0x03ff // limit 15:00
347 .long 0 // base 16:47
358 // 32 bit flat code segment (PROTECTED_MODE_CS)
359 .word 0xffff, 0, 0x9b00, 0x00cf
360 // 32 bit flat data segment (PROTECTED_MODE_DS)
361 .word 0xffff, 0, 0x9300, 0x00cf
362 // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
363 .word 0xffff, 0, 0x9b0f, 0x0000
364 // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
365 .word 0xffff, 0, 0x9300, 0x0000
368 /****************************************************************
369 * Interrupt entry points
370 ****************************************************************/
372 // Define an entry point for an interrupt (no args passed).
376 cli // In case something far-calls instead of using "int"
381 // Define an entry point for an interrupt (can read/modify args).
382 .macro IRQ_ENTRY_ARG num
385 cli // In case something far-calls instead of using "int"
386 ENTRY_ARG handle_\num
404 // XXX - Fixed Disk Parameter Table
410 .include "out/cbt.proc.16.s"
414 // XXX - Baud Rate Generator Table
422 // int 18/19 are special - they reset the stack and do not return.
434 .macro IRQ_TRAMPOLINE num
435 .global irq_trampoline_0x\num
436 irq_trampoline_0x\num :
467 .include "out/floppy_dbt.proc.16.s"
484 .global freespace2_start, freespace2_end
498 .include "out/font.proc.16.s"
508 // XXX - Initial Interrupt Vector Offsets Loaded by POST
511 // XXX - BIOS_COPYRIGHT_STRING
512 .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
515 .global dummy_iret_handler
522 ORG 0xfff0 // Power-up Entry Point
523 ljmpw $SEG_BIOS, $post16
530 .byte CONFIG_MODEL_ID
532 .global bios_checksum