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