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
18 /****************************************************************
19 * Include of 16bit C code
20 ****************************************************************/
22 .globl bios16c_start, bios16c_end
24 .include "out/blob.proc.16.s"
29 /****************************************************************
31 ****************************************************************/
33 // Call a C function - this does the minimal work necessary to
34 // call into C. It sets up %ds, backs up %es, and backs up
35 // those registers that are call clobbered by the C compiler.
38 pushl %eax // Save registers clobbered by C code
43 movw %ss, %ax // Move %ss to %ds
45 pushl %esp // Backup %esp, then clear high bits
48 popl %esp // Restore %esp (including high bits)
49 popw %ds // Restore registers saved above
56 // Call a C function with current register list as an
57 // argument. This backs up the registers and sets %eax
58 // to point to the backup. On return, the registers are
59 // restored from the structure.
60 .macro ENTRY_ARG cfunc
62 pushl %eax // Save registers (matches struct bregs)
70 movw %ss, %ax // Move %ss to %ds
72 movl %esp, %ebx // Backup %esp, then zero high bits
74 movl %esp, %eax // First arg is pointer to struct bregs
76 movl %ebx, %esp // Restore %esp (including high bits)
77 popw %ds // Restore registers (from struct bregs)
87 // Macro to reset the 16bit stack
92 movl $ CONFIG_STACK_OFFSET , %esp
96 /****************************************************************
98 ****************************************************************/
103 // init the stack pointer
106 // Set entry point of rombios32 code - the actual address
107 // is altered later in the build process.
114 // Fall through to transition32 function below
117 /****************************************************************
119 ****************************************************************/
121 // Place CPU into 32bit mode from 16bit mode.
122 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
132 // Set segment descriptors
133 lidt %cs:pmode_IDT_info
134 lgdt %cs:rombios32_gdt_48
141 // start protected mode code
142 // ljmpl $PROTECTED_MODE_CS, $(1f | 0xf0000)
143 .word 0xea66, 1f, 0x000f, PROTECTED_MODE_CS
147 // init data segments
148 movl $PROTECTED_MODE_DS, %eax
158 // Call a 16bit function from 32bit mode.
159 // %eax = address of struct bregs
160 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
161 .globl __call16_from32
165 // Jump to 16bit mode
166 ljmpw $REAL_MODE_CS, $1f
170 // restore data segment limits to 0xffff
171 movw $REAL_MODE_DS, %ax
178 // reset PE bit in CR0
183 // far jump to flush CPU queue after transition to real mode
187 // restore IDT to normal real-mode defaults
188 lidt %cs:rmode_IDT_info
190 // Clear segment registers
196 movw %ax, %ss // Assume stack is in segment 0
200 // Set __call16 return address to be transition32
203 // Fall through to __call16
206 // Call a 16bit function from 16bit mode with a specified cpu register state
207 // %eax = address of struct bregs
208 // Clobbers: all gp registers, es
214 // Setup for iretw call
216 pushw $1f // return point
217 pushw 0x20(%eax) // flags
218 pushl 0x1c(%eax) // CS:IP
220 // Load calling registers.
221 movl 0x04(%eax), %edi
222 movl 0x08(%eax), %esi
223 movl 0x0c(%eax), %ebx
224 movl 0x10(%eax), %edx
225 movl 0x14(%eax), %ecx
226 movw 0x02(%eax), %es // XXX - should load %ds too
227 movl 0x18(%eax), %eax
230 iretw // XXX - just do a lcalll
232 // Store flags, eax, ecx
235 movl 0x06(%esp), %eax
236 movl %ecx, %ss:0x14(%eax) // Save %ecx
238 movw %cx, %ds // Restore %ds == %ss
240 movl %ecx, 0x18(%eax) // Save %eax
242 movw %cx, 0x20(%eax) // Save flags
244 // Store remaining registers
246 movl %edi, 0x04(%eax)
247 movl %esi, 0x08(%eax)
248 movl %ebx, 0x0c(%eax)
249 movl %edx, 0x10(%eax)
260 .globl apm16protected_entry
261 apm16protected_entry:
264 ENTRY_ARG handle_1553
265 addw $4, %sp // pop dummy
266 popfw // restore flags
270 .globl apm32protected_entry
271 apm32protected_entry:
273 pushw %cs // Setup for long jump to 16bit mode
279 ENTRY_ARG handle_1553
281 movw $2f,(%esp) // Setup for long jump back to 32bit mode
286 addl $4, %esp // pop call address
292 /****************************************************************
294 ****************************************************************/
296 // Protected mode IDT descriptor
298 // I just make the limit 0, so the machine will shutdown
299 // if an exception occurs during protected mode memory
302 // Set base to f0000 to correspond to beginning of BIOS,
303 // in case I actually define an IDT later
306 .word 0x0000 // limit 15:00
307 .long 0xf0000 // base 16:47
309 // Real mode IDT descriptor
311 // Set to typical real-mode values.
315 .word 0x03ff // limit 15:00
316 .long 0 // base 16:47
327 // 32 bit flat code segment (PROTECTED_MODE_CS)
328 .word 0xffff, 0, 0x9b00, 0x00cf
329 // 32 bit flat data segment (PROTECTED_MODE_DS)
330 .word 0xffff, 0, 0x9300, 0x00cf
331 // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
332 .word 0xffff, 0, 0x9b0f, 0x0000
333 // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
334 .word 0xffff, 0, 0x9300, 0x0000
337 /****************************************************************
338 * Interrupt entry points
339 ****************************************************************/
341 // Define an entry point for an interrupt (no args passed).
345 cli // In case something far-calls instead of using "int"
350 // Define an entry point for an interrupt (can read/modify args).
351 .macro IRQ_ENTRY_ARG num
354 cli // In case something far-calls instead of using "int"
355 ENTRY_ARG handle_\num
373 // XXX - Fixed Disk Parameter Table
379 .include "out/cbt.proc.16.s"
383 // XXX - Baud Rate Generator Table
391 // int 18/19 are special - they reset the stack and do not return.
403 .macro IRQ_TRAMPOLINE num
404 .globl irq_trampoline_0x\num
405 irq_trampoline_0x\num :
433 .include "out/floppy_dbt.proc.16.s"
450 .globl freespace2_start, freespace2_end
464 .include "out/font.proc.16.s"
474 // XXX - Initial Interrupt Vector Offsets Loaded by POST
477 // XXX - BIOS_COPYRIGHT_STRING
478 .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
481 .globl dummy_iret_handler
488 .org 0xfff0 // Power-up Entry Point
489 ljmpw $0xf000, $post16
496 .byte CONFIG_MODEL_ID