Add tool for generating assembler offset definition file.
[seabios.git] / src / romlayout.S
1 // Rom layout and bios assembler to C interface.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "config.h" // CONFIG_*
9 #include "ioport.h" // PORT_A20
10 #include "bregs.h" // CR0_*
11 #include "../out/asm-offsets.h" // BREGS_*
12
13
14 /****************************************************************
15  * Include of 16bit C code
16  ****************************************************************/
17
18         .code16gcc
19 .include "out/blob.16.s"
20
21
22 /****************************************************************
23  * Entry macros
24  ****************************************************************/
25
26         // Call a C function - this does the minimal work necessary to
27         // call into C.  It sets up %ds, backs up %es, and backs up
28         // those registers that are call clobbered by the C compiler.
29         .macro ENTRY cfunc
30         cld
31         pushl %eax              // Save registers clobbered by C code
32         pushl %ecx
33         pushl %edx
34         pushw %es
35         pushw %ds
36         movw %ss, %ax           // Move %ss to %ds
37         movw %ax, %ds
38         pushl %esp              // Backup %esp, then clear high bits
39         movzwl %sp, %esp
40         calll \cfunc
41         popl %esp               // Restore %esp (including high bits)
42         popw %ds                // Restore registers saved above
43         popw %es
44         popl %edx
45         popl %ecx
46         popl %eax
47         .endm
48
49         // Call a C function with current register list as an
50         // argument.  This backs up the registers and sets %eax
51         // to point to the backup.  On return, the registers are
52         // restored from the structure.
53         .macro ENTRY_ARG cfunc
54         cld
55         pushl %eax              // Save registers (matches struct bregs)
56         pushl %ecx
57         pushl %edx
58         pushl %ebx
59         pushl %esi
60         pushl %edi
61         pushw %es
62         pushw %ds
63         movw %ss, %ax           // Move %ss to %ds
64         movw %ax, %ds
65         movl %esp, %ebx         // Backup %esp, then zero high bits
66         movzwl %sp, %esp
67         movl %esp, %eax         // First arg is pointer to struct bregs
68         calll \cfunc
69         movl %ebx, %esp         // Restore %esp (including high bits)
70         popw %ds                // Restore registers (from struct bregs)
71         popw %es
72         popl %edi
73         popl %esi
74         popl %ebx
75         popl %edx
76         popl %ecx
77         popl %eax
78         .endm
79
80         // As above, but don't mangle %esp
81         .macro ENTRY_ARG_ESP cfunc
82         cld
83         pushl %eax              // Save registers (matches struct bregs)
84         pushl %ecx
85         pushl %edx
86         pushl %ebx
87         pushl %esi
88         pushl %edi
89         pushw %es
90         pushw %ds
91         movw %ss, %ax           // Move %ss to %ds
92         movw %ax, %ds
93         movl %esp, %eax         // First arg is pointer to struct bregs
94         calll \cfunc
95         popw %ds                // Restore registers (from struct bregs)
96         popw %es
97         popl %edi
98         popl %esi
99         popl %ebx
100         popl %edx
101         popl %ecx
102         popl %eax
103         .endm
104
105         // Macro to reset the 16bit stack
106         // Clobbers %ax
107         .macro RESET_STACK
108         xorw %ax, %ax
109         movw %ax, %ss
110         movl $ BUILD_STACK_ADDR , %esp
111         cld
112         .endm
113
114         // Specify a location in the fixed part of bios area.
115         .macro ORG addr
116         .section .text.fixed.addr
117         .org \addr - BUILD_START_FIXED
118         .endm
119
120
121 /****************************************************************
122  * POST handler
123  ****************************************************************/
124
125         ORG 0xe05b
126 post16:
127         // enable cache
128         movl %cr0, %eax
129         andl $~(CR0_CD|CR0_NW), %eax
130         movl %eax, %cr0
131
132         // init the stack pointer
133         RESET_STACK
134
135         pushl $_code32__start
136
137         // Fall through to transition32 function below
138
139
140 /****************************************************************
141  * Call trampolines
142  ****************************************************************/
143
144 // Place CPU into 32bit mode from 16bit mode.
145 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
146 transition32:
147         // Disable irqs
148         cli
149
150         // enable a20
151         inb $PORT_A20, %al
152         orb $A20_ENABLE_BIT, %al
153         outb %al, $PORT_A20
154
155         // Set segment descriptors
156         lidt %cs:pmode_IDT_info
157         lgdt %cs:rombios32_gdt_48
158
159         // Enable protected mode
160         movl %cr0, %eax
161         orl $CR0_PE, %eax
162         movl %eax, %cr0
163
164         // start protected mode code
165         ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
166
167         .code32
168 1:
169         // init data segments
170         movl $SEG32_MODE32_DS, %eax
171         movw %ax, %ds
172         movw %ax, %es
173         movw %ax, %ss
174         xorl %eax, %eax
175         movw %ax, %fs
176         movw %ax, %gs
177
178         retl
179
180 // Call a 16bit function from 32bit mode.
181 // %eax = address of struct bregs
182 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
183         .global __call16_from32
184 __call16_from32:
185         pushl %eax
186
187         // restore data segment limits to 0xffff
188         movw $SEG32_MODE16_DS, %ax
189         movw %ax, %ds
190         movw %ax, %es
191         movw %ax, %ss
192         movw %ax, %fs
193         movw %ax, %gs
194
195         // disable a20
196         inb $PORT_A20, %al
197         andb $~A20_ENABLE_BIT, %al
198         outb %al, $PORT_A20
199
200         // Jump to 16bit mode
201         ljmpw $SEG32_MODE16_CS, $1f
202
203         .code16gcc
204 1:
205         // Disable protected mode
206         movl %cr0, %eax
207         andl $~CR0_PE, %eax
208         movl %eax, %cr0
209
210         // far jump to flush CPU queue after transition to real mode
211         ljmpw $SEG_BIOS, $2f
212
213 2:
214         // restore IDT to normal real-mode defaults
215         lidt %cs:rmode_IDT_info
216
217         // Clear segment registers
218         xorw %ax, %ax
219         movw %ax, %fs
220         movw %ax, %gs
221         movw %ax, %es
222         movw %ax, %ds
223         movw %ax, %ss  // Assume stack is in segment 0
224
225         popl %eax
226
227         // Set __call16 return address to be transition32
228         pushl $transition32
229
230         // Fall through to __call16
231
232
233 // Call a 16bit function from 16bit mode with a specified cpu register state
234 // %eax = address of struct bregs
235 // Clobbers: all gp registers, es
236         .global __call16
237 __call16:
238         // Save eax
239         pushl %eax
240
241         // Setup for iretw call
242         pushw $SEG_BIOS
243         pushw $1f               // return point
244         pushw BREGS_flags(%eax) // flags
245         pushl BREGS_ip(%eax)    // CS:IP
246
247         // Load calling registers.
248         movl BREGS_edi(%eax), %edi
249         movl BREGS_esi(%eax), %esi
250         movl BREGS_ebx(%eax), %ebx
251         movl BREGS_edx(%eax), %edx
252         movl BREGS_ecx(%eax), %ecx
253         movw BREGS_es(%eax), %es
254         movw BREGS_ds(%eax), %ds
255         movl %ss:BREGS_eax(%eax), %eax
256
257         // Invoke call
258         iretw                   // XXX - just do a lcalll
259 1:
260         // Store flags, eax, ecx
261         pushfw
262         pushl %eax
263         movl 0x06(%esp), %eax
264         movl %ecx, %ss:BREGS_ecx(%eax)
265         movw %ds, %ss:BREGS_ds(%eax)
266         movw %ss, %cx
267         movw %cx, %ds           // Restore %ds == %ss
268         popl %ecx
269         movl %ecx, BREGS_eax(%eax)
270         popw %cx
271         movw %cx, BREGS_flags(%eax)
272
273         // Store remaining registers
274         movw %es, BREGS_es(%eax)
275         movl %edi, BREGS_edi(%eax)
276         movl %esi, BREGS_esi(%eax)
277         movl %ebx, BREGS_ebx(%eax)
278         movl %edx, BREGS_edx(%eax)
279
280         // Remove %eax
281         popl %eax
282
283         cld
284
285         retl
286
287
288 // APM trampolines
289         .global apm16protected_entry
290 apm16protected_entry:
291         pushfw          // save flags
292         pushl %eax      // dummy
293         ENTRY_ARG handle_1553
294         addw $4, %sp    // pop dummy
295         popfw           // restore flags
296         lretw
297
298         .code32
299         .global apm32protected_entry
300 apm32protected_entry:
301         pushfw
302         pushw %cs       // Setup for long jump to 16bit mode
303         pushw $1f
304         addw $8, 2(%esp)
305         ljmpw *(%esp)
306         .code16gcc
307 1:
308         ENTRY_ARG_ESP handle_1553
309
310         movw $2f,(%esp) // Setup for long jump back to 32bit mode
311         subw $8, 2(%esp)
312         ljmpw *(%esp)
313         .code32
314 2:
315         addl $4, %esp   // pop call address
316         popfw
317         lretl
318
319 // 32bit elf entry point
320         .global post32
321 post32:
322         cli
323         cld
324         lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
325         lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
326         movl $BUILD_STACK_ADDR, %esp
327         ljmpl $SEG32_MODE32_CS, $_code32__start
328
329         .code16gcc
330
331 // Shutdown a CPU.  We want this in the 0xf000 section to ensure that
332 // the code wont be overwritten with something else.  (Should
333 // something spurious wake up the CPU, we want to be sure that the hlt
334 // insn will still be present and will shutdown the CPU.)
335         .global permanent_halt
336 permanent_halt:
337         cli
338 1:      hlt
339         jmp 1b
340
341
342 /****************************************************************
343  * GDT and IDT tables
344  ****************************************************************/
345
346 // Protected mode IDT descriptor
347 //
348 // I just make the limit 0, so the machine will shutdown
349 // if an exception occurs during protected mode memory
350 // transfers.
351 //
352 // Set base to f0000 to correspond to beginning of BIOS,
353 // in case I actually define an IDT later
354 // Set limit to 0
355 pmode_IDT_info:
356         .word 0x0000  // limit 15:00
357         .long 0xf0000 // base 16:47
358
359 // Real mode IDT descriptor
360 //
361 // Set to typical real-mode values.
362 // base  = 000000
363 // limit =   03ff
364 rmode_IDT_info:
365         .word 0x03ff  // limit 15:00
366         .long 0       // base 16:47
367
368 rombios32_gdt_48:
369         .word 0x30
370         .word rombios32_gdt
371         .word 0x000f
372
373         .balign 8
374 rombios32_gdt:
375         .word 0, 0, 0, 0
376         .word 0, 0, 0, 0
377         // 32 bit flat code segment (SEG32_MODE32_CS)
378         .word 0xffff, 0, 0x9b00, 0x00cf
379         // 32 bit flat data segment (SEG32_MODE32_DS)
380         .word 0xffff, 0, 0x9300, 0x00cf
381         // 16 bit code segment base=0xf0000 limit=0xffff (SEG32_MODE16_CS)
382         .word 0xffff, 0, 0x9b0f, 0x0000
383         // 16 bit data segment base=0x0 limit=0xffff (SEG32_MODE16_DS)
384         .word 0xffff, 0, 0x9300, 0x0000
385
386 // We need a copy of this string in the 0xf000 segment, but we are not
387 // actually a PnP BIOS, so make sure it is *not* aligned, so OSes will
388 // not see it if they scan.
389         .global pnp_string
390         .balign 2
391         .byte 0
392 pnp_string:
393         .ascii "$PnP"
394
395
396 /****************************************************************
397  * Interrupt entry points
398  ****************************************************************/
399
400         // Define an entry point for an interrupt (no args passed).
401         .macro IRQ_ENTRY num
402         .global entry_\num
403         entry_\num :
404         cli         // In case something far-calls instead of using "int"
405         ENTRY handle_\num
406         iretw
407         .endm
408
409         // Define an entry point for an interrupt (can read/modify args).
410         .macro IRQ_ENTRY_ARG num
411         .global entry_\num
412         entry_\num :
413         cli         // In case something far-calls instead of using "int"
414         ENTRY_ARG handle_\num
415         iretw
416         .endm
417
418         ORG 0xe2c3
419         IRQ_ENTRY nmi
420
421         IRQ_ENTRY_ARG 13
422         IRQ_ENTRY_ARG 12
423         IRQ_ENTRY_ARG 11
424         IRQ_ENTRY 76
425         IRQ_ENTRY 1c
426         IRQ_ENTRY 70
427
428         ORG 0xe3fe
429         jmp entry_13
430
431         ORG 0xe401
432         // XXX - Fixed Disk Parameter Table
433
434         ORG 0xe6f2
435         jmp entry_19
436
437         ORG 0xe6f5
438 .include "out/cbt.proc.16.s"
439         .text
440
441         ORG 0xe729
442         // XXX - Baud Rate Generator Table
443
444         ORG 0xe739
445         IRQ_ENTRY_ARG 14
446
447         IRQ_ENTRY 74
448         IRQ_ENTRY 75
449
450         // int 18/19 are special - they reset the stack and do not return.
451         .global entry_19
452 entry_19:
453         RESET_STACK
454         pushl $_code32_handle_19
455         jmp transition32
456
457         .global entry_18
458 entry_18:
459         RESET_STACK
460         pushl $_code32_handle_18
461         jmp transition32
462
463         // IRQ trampolines
464         .macro IRQ_TRAMPOLINE num
465         .global irq_trampoline_0x\num
466         irq_trampoline_0x\num :
467         int $0x\num
468         lretw
469         .endm
470
471         IRQ_TRAMPOLINE 02
472         IRQ_TRAMPOLINE 10
473         IRQ_TRAMPOLINE 13
474         IRQ_TRAMPOLINE 15
475         IRQ_TRAMPOLINE 16
476         IRQ_TRAMPOLINE 18
477         IRQ_TRAMPOLINE 19
478         IRQ_TRAMPOLINE 1c
479         IRQ_TRAMPOLINE 4a
480
481         ORG 0xe82e
482         IRQ_ENTRY_ARG 16
483
484 entry_hwirq:
485         ENTRY handle_hwirq
486
487         ORG 0xe987
488         IRQ_ENTRY 09
489
490         ORG 0xec59
491         IRQ_ENTRY_ARG 40
492
493         ORG 0xef57
494         IRQ_ENTRY 0e
495
496         ORG 0xefc7
497 .include "out/floppy_dbt.proc.16.s"
498         .text
499
500         ORG 0xefd2
501         IRQ_ENTRY_ARG 17
502
503         ORG 0xf045
504         // XXX int 10
505         iretw
506
507         ORG 0xf065
508         IRQ_ENTRY_ARG 10
509
510         ORG 0xf0a4
511         // XXX int 1D
512         iretw
513
514         .global freespace2_start, freespace2_end
515 freespace2_start:
516
517         ORG 0xf841
518 freespace2_end:
519         jmp entry_12
520
521         ORG 0xf84d
522         jmp entry_11
523
524         ORG 0xf859
525         IRQ_ENTRY_ARG 15
526
527         ORG 0xfa6e
528 .include "out/font.proc.16.s"
529         .text
530
531         ORG 0xfe6e
532         IRQ_ENTRY_ARG 1a
533
534         ORG 0xfea5
535         IRQ_ENTRY 08
536
537         ORG 0xfef3
538         // XXX - Initial Interrupt Vector Offsets Loaded by POST
539
540         ORG 0xff00
541         // XXX - BIOS_COPYRIGHT_STRING
542         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
543
544         ORG 0xff53
545         .global dummy_iret_handler
546 dummy_iret_handler:
547         iretw
548
549         ORG 0xff54
550         IRQ_ENTRY_ARG 05
551
552         ORG 0xfff0 // Power-up Entry Point
553         ljmpw $SEG_BIOS, $post16
554
555         ORG 0xfff5
556         // BIOS build date
557         .ascii "06/23/99"
558
559         ORG 0xfffe
560         .byte CONFIG_MODEL_ID
561
562         .global bios_checksum
563 bios_checksum:
564         .byte 0x00
565
566         .end