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 ****************************************************************/
129 // init the stack pointer
132 // Set entry point of rombios32 code - the actual address
133 // is altered later in the build process.
140 // Fall through to transition32 function below
143 /****************************************************************
145 ****************************************************************/
147 // Place CPU into 32bit mode from 16bit mode.
148 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
158 // Set segment descriptors
159 lidt %cs:pmode_IDT_info
160 lgdt %cs:rombios32_gdt_48
167 // start protected mode code
168 // ljmpl $PROTECTED_MODE_CS, $(1f | 0xf0000)
169 .word 0xea66, 1f, 0x000f, PROTECTED_MODE_CS
173 // init data segments
174 movl $PROTECTED_MODE_DS, %eax
184 // Call a 16bit function from 32bit mode.
185 // %eax = address of struct bregs
186 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
187 .globl __call16_from32
191 // Jump to 16bit mode
192 ljmpw $REAL_MODE_CS, $1f
196 // restore data segment limits to 0xffff
197 movw $REAL_MODE_DS, %ax
204 // reset PE bit in CR0
209 // far jump to flush CPU queue after transition to real mode
213 // restore IDT to normal real-mode defaults
214 lidt %cs:rmode_IDT_info
216 // Clear segment registers
222 movw %ax, %ss // Assume stack is in segment 0
226 // Set __call16 return address to be transition32
229 // Fall through to __call16
232 // Call a 16bit function from 16bit mode with a specified cpu register state
233 // %eax = address of struct bregs
234 // Clobbers: all gp registers, es
240 // Setup for iretw call
242 pushw $1f // return point
243 pushw 0x20(%eax) // flags
244 pushl 0x1c(%eax) // CS:IP
246 // Load calling registers.
247 movl 0x04(%eax), %edi
248 movl 0x08(%eax), %esi
249 movl 0x0c(%eax), %ebx
250 movl 0x10(%eax), %edx
251 movl 0x14(%eax), %ecx
252 movw 0x02(%eax), %es // XXX - should load %ds too
253 movl 0x18(%eax), %eax
256 iretw // XXX - just do a lcalll
258 // Store flags, eax, ecx
261 movl 0x06(%esp), %eax
262 movl %ecx, %ss:0x14(%eax) // Save %ecx
264 movw %cx, %ds // Restore %ds == %ss
266 movl %ecx, 0x18(%eax) // Save %eax
268 movw %cx, 0x20(%eax) // Save flags
270 // Store remaining registers
272 movl %edi, 0x04(%eax)
273 movl %esi, 0x08(%eax)
274 movl %ebx, 0x0c(%eax)
275 movl %edx, 0x10(%eax)
286 .globl apm16protected_entry
287 apm16protected_entry:
290 ENTRY_ARG handle_1553
291 addw $4, %sp // pop dummy
292 popfw // restore flags
296 .globl apm32protected_entry
297 apm32protected_entry:
299 pushw %cs // Setup for long jump to 16bit mode
305 ENTRY_ARG_ESP handle_1553
307 movw $2f,(%esp) // Setup for long jump back to 32bit mode
312 addl $4, %esp // pop call address
318 /****************************************************************
320 ****************************************************************/
322 // Protected mode IDT descriptor
324 // I just make the limit 0, so the machine will shutdown
325 // if an exception occurs during protected mode memory
328 // Set base to f0000 to correspond to beginning of BIOS,
329 // in case I actually define an IDT later
332 .word 0x0000 // limit 15:00
333 .long 0xf0000 // base 16:47
335 // Real mode IDT descriptor
337 // Set to typical real-mode values.
341 .word 0x03ff // limit 15:00
342 .long 0 // base 16:47
353 // 32 bit flat code segment (PROTECTED_MODE_CS)
354 .word 0xffff, 0, 0x9b00, 0x00cf
355 // 32 bit flat data segment (PROTECTED_MODE_DS)
356 .word 0xffff, 0, 0x9300, 0x00cf
357 // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
358 .word 0xffff, 0, 0x9b0f, 0x0000
359 // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
360 .word 0xffff, 0, 0x9300, 0x0000
363 /****************************************************************
364 * Interrupt entry points
365 ****************************************************************/
367 // Define an entry point for an interrupt (no args passed).
371 cli // In case something far-calls instead of using "int"
376 // Define an entry point for an interrupt (can read/modify args).
377 .macro IRQ_ENTRY_ARG num
380 cli // In case something far-calls instead of using "int"
381 ENTRY_ARG handle_\num
399 // XXX - Fixed Disk Parameter Table
405 .include "out/cbt.proc.16.s"
409 // XXX - Baud Rate Generator Table
417 // int 18/19 are special - they reset the stack and do not return.
429 .macro IRQ_TRAMPOLINE num
430 .globl irq_trampoline_0x\num
431 irq_trampoline_0x\num :
462 .include "out/floppy_dbt.proc.16.s"
479 .globl freespace2_start, freespace2_end
493 .include "out/font.proc.16.s"
503 // XXX - Initial Interrupt Vector Offsets Loaded by POST
506 // XXX - BIOS_COPYRIGHT_STRING
507 .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
510 .globl dummy_iret_handler
517 ORG 0xfff0 // Power-up Entry Point
518 ljmpw $0xf000, $post16
525 .byte CONFIG_MODEL_ID