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