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