Use ld to build final rom; remove custom build utilities.
[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         .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         // init the stack pointer
129         RESET_STACK
130
131         pushl $_code32__start
132
133         cld
134
135         // Fall through to transition32 function below
136
137
138 /****************************************************************
139  * Call trampolines
140  ****************************************************************/
141
142 // Place CPU into 32bit mode from 16bit mode.
143 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
144 transition32:
145         // Disable irqs
146         cli
147
148         // enable a20
149         inb $0x92, %al
150         orb $0x02, %al
151         outb %al, $0x92
152
153         // Set segment descriptors
154         lidt %cs:pmode_IDT_info
155         lgdt %cs:rombios32_gdt_48
156
157         // set PE bit in CR0
158         movl  %cr0, %eax
159         orb   $0x01, %al
160         movl  %eax, %cr0
161
162         // start protected mode code
163         .set __trans32, (BUILD_BIOS_ADDR + 1f)
164         ljmpl $PROTECTED_MODE_CS, $__trans32
165
166         .code32
167 1:
168         // init data segments
169         movl $PROTECTED_MODE_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         // Jump to 16bit mode
187         ljmpw $REAL_MODE_CS, $1f
188
189         .code16gcc
190 1:
191         // restore data segment limits to 0xffff
192         movw $REAL_MODE_DS, %ax
193         movw %ax, %ds
194         movw %ax, %es
195         movw %ax, %ss
196         movw %ax, %fs
197         movw %ax, %gs
198
199         // reset PE bit in CR0
200         movl %cr0, %eax
201         andb $0xfe, %al
202         movl %eax, %cr0
203
204         // far jump to flush CPU queue after transition to real mode
205         ljmpw $SEG_BIOS, $2f
206
207 2:
208         // restore IDT to normal real-mode defaults
209         lidt %cs:rmode_IDT_info
210
211         // Clear segment registers
212         xorw %ax, %ax
213         movw %ax, %fs
214         movw %ax, %gs
215         movw %ax, %es
216         movw %ax, %ds
217         movw %ax, %ss  // Assume stack is in segment 0
218
219         popl %eax
220
221         // Set __call16 return address to be transition32
222         pushl $transition32
223
224         // Fall through to __call16
225
226
227 // Call a 16bit function from 16bit mode with a specified cpu register state
228 // %eax = address of struct bregs
229 // Clobbers: all gp registers, es
230         .global __call16
231 __call16:
232         // Save eax
233         pushl %eax
234
235         // Setup for iretw call
236         pushw $SEG_BIOS
237         pushw $1f               // return point
238         pushw 0x20(%eax)        // flags
239         pushl 0x1c(%eax)        // CS:IP
240
241         // Load calling registers.
242         movl 0x04(%eax), %edi
243         movl 0x08(%eax), %esi
244         movl 0x0c(%eax), %ebx
245         movl 0x10(%eax), %edx
246         movl 0x14(%eax), %ecx
247         movw 0x02(%eax), %es    // XXX - should load %ds too
248         movl 0x18(%eax), %eax
249
250         // Invoke call
251         iretw                   // XXX - just do a lcalll
252 1:
253         // Store flags, eax, ecx
254         pushfw
255         pushl %eax
256         movl 0x06(%esp), %eax
257         movl %ecx, %ss:0x14(%eax)       // Save %ecx
258         movw %ss, %cx
259         movw %cx, %ds                   // Restore %ds == %ss
260         popl %ecx
261         movl %ecx, 0x18(%eax)           // Save %eax
262         popw %cx
263         movw %cx, 0x20(%eax)            // Save flags
264
265         // Store remaining registers
266         movw %es, 0x02(%eax)
267         movl %edi, 0x04(%eax)
268         movl %esi, 0x08(%eax)
269         movl %ebx, 0x0c(%eax)
270         movl %edx, 0x10(%eax)
271
272         // Remove %eax
273         popl %eax
274
275         cld
276
277         retl
278
279
280 // APM trampolines
281         .global apm16protected_entry
282 apm16protected_entry:
283         pushfw          // save flags
284         pushl %eax      // dummy
285         ENTRY_ARG handle_1553
286         addw $4, %sp    // pop dummy
287         popfw           // restore flags
288         lretw
289
290         .code32
291         .global apm32protected_entry
292 apm32protected_entry:
293         pushfw
294         pushw %cs       // Setup for long jump to 16bit mode
295         pushw $1f
296         addw $8, 2(%esp)
297         ljmpw *(%esp)
298         .code16gcc
299 1:
300         ENTRY_ARG_ESP handle_1553
301
302         movw $2f,(%esp) // Setup for long jump back to 32bit mode
303         subw $8, 2(%esp)
304         ljmpw *(%esp)
305         .code32
306 2:
307         addl $4, %esp   // pop call address
308         popfw
309         lretl
310         .code16gcc
311
312
313 /****************************************************************
314  * GDT and IDT tables
315  ****************************************************************/
316
317 // Protected mode IDT descriptor
318 //
319 // I just make the limit 0, so the machine will shutdown
320 // if an exception occurs during protected mode memory
321 // transfers.
322 //
323 // Set base to f0000 to correspond to beginning of BIOS,
324 // in case I actually define an IDT later
325 // Set limit to 0
326         .global pmode_IDT_info
327 pmode_IDT_info:
328         .word 0x0000  // limit 15:00
329         .long 0xf0000 // base 16:47
330
331 // Real mode IDT descriptor
332 //
333 // Set to typical real-mode values.
334 // base  = 000000
335 // limit =   03ff
336 rmode_IDT_info:
337         .word 0x03ff  // limit 15:00
338         .long 0       // base 16:47
339
340         .global rombios32_gdt_48
341 rombios32_gdt_48:
342         .word 0x30
343         .word rombios32_gdt
344         .word 0x000f
345
346         .balign 8
347 rombios32_gdt:
348         .word 0, 0, 0, 0
349         .word 0, 0, 0, 0
350         // 32 bit flat code segment (PROTECTED_MODE_CS)
351         .word 0xffff, 0, 0x9b00, 0x00cf
352         // 32 bit flat data segment (PROTECTED_MODE_DS)
353         .word 0xffff, 0, 0x9300, 0x00cf
354         // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
355         .word 0xffff, 0, 0x9b0f, 0x0000
356         // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
357         .word 0xffff, 0, 0x9300, 0x0000
358
359
360 /****************************************************************
361  * Interrupt entry points
362  ****************************************************************/
363
364         // Define an entry point for an interrupt (no args passed).
365         .macro IRQ_ENTRY num
366         .global entry_\num
367         entry_\num :
368         cli         // In case something far-calls instead of using "int"
369         ENTRY handle_\num
370         iretw
371         .endm
372
373         // Define an entry point for an interrupt (can read/modify args).
374         .macro IRQ_ENTRY_ARG num
375         .global entry_\num
376         entry_\num :
377         cli         // In case something far-calls instead of using "int"
378         ENTRY_ARG handle_\num
379         iretw
380         .endm
381
382         ORG 0xe2c3
383         IRQ_ENTRY nmi
384
385         IRQ_ENTRY_ARG 13
386         IRQ_ENTRY_ARG 12
387         IRQ_ENTRY_ARG 11
388         IRQ_ENTRY 76
389         IRQ_ENTRY 1c
390         IRQ_ENTRY 70
391
392         ORG 0xe3fe
393         jmp entry_13
394
395         ORG 0xe401
396         // XXX - Fixed Disk Parameter Table
397
398         ORG 0xe6f2
399         jmp entry_19
400
401         ORG 0xe6f5
402 .include "out/cbt.proc.16.s"
403         .text
404
405         ORG 0xe729
406         // XXX - Baud Rate Generator Table
407
408         ORG 0xe739
409         IRQ_ENTRY_ARG 14
410
411         IRQ_ENTRY 74
412         IRQ_ENTRY 75
413
414         // int 18/19 are special - they reset the stack and do not return.
415         .global entry_19
416 entry_19:
417         RESET_STACK
418         ENTRY handle_19
419
420         .global entry_18
421 entry_18:
422         RESET_STACK
423         ENTRY handle_18
424
425         // IRQ trampolines
426         .macro IRQ_TRAMPOLINE num
427         .global irq_trampoline_0x\num
428         irq_trampoline_0x\num :
429         int $0x\num
430         lretw
431         .endm
432
433         IRQ_TRAMPOLINE 02
434         IRQ_TRAMPOLINE 10
435         IRQ_TRAMPOLINE 13
436         IRQ_TRAMPOLINE 15
437         IRQ_TRAMPOLINE 16
438         IRQ_TRAMPOLINE 18
439         IRQ_TRAMPOLINE 19
440         IRQ_TRAMPOLINE 1c
441         IRQ_TRAMPOLINE 4a
442
443         ORG 0xe82e
444         IRQ_ENTRY_ARG 16
445
446 entry_hwirq:
447         ENTRY handle_hwirq
448
449         ORG 0xe987
450         IRQ_ENTRY 09
451
452         ORG 0xec59
453         IRQ_ENTRY_ARG 40
454
455         ORG 0xef57
456         IRQ_ENTRY 0e
457
458         ORG 0xefc7
459 .include "out/floppy_dbt.proc.16.s"
460         .text
461
462         ORG 0xefd2
463         IRQ_ENTRY_ARG 17
464
465         ORG 0xf045
466         // XXX int 10
467         iretw
468
469         ORG 0xf065
470         IRQ_ENTRY_ARG 10
471
472         ORG 0xf0a4
473         // XXX int 1D
474         iretw
475
476         .global freespace2_start, freespace2_end
477 freespace2_start:
478
479         ORG 0xf841
480 freespace2_end:
481         jmp entry_12
482
483         ORG 0xf84d
484         jmp entry_11
485
486         ORG 0xf859
487         IRQ_ENTRY_ARG 15
488
489         ORG 0xfa6e
490 .include "out/font.proc.16.s"
491         .text
492
493         ORG 0xfe6e
494         IRQ_ENTRY_ARG 1a
495
496         ORG 0xfea5
497         IRQ_ENTRY 08
498
499         ORG 0xfef3
500         // XXX - Initial Interrupt Vector Offsets Loaded by POST
501
502         ORG 0xff00
503         // XXX - BIOS_COPYRIGHT_STRING
504         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
505
506         ORG 0xff53
507         .global dummy_iret_handler
508 dummy_iret_handler:
509         iretw
510
511         ORG 0xff54
512         IRQ_ENTRY_ARG 05
513
514         ORG 0xfff0 // Power-up Entry Point
515         ljmpw $SEG_BIOS, $post16
516
517         ORG 0xfff5
518         // BIOS build date
519         .ascii "06/23/99"
520
521         ORG 0xfffe
522         .byte CONFIG_MODEL_ID
523
524         .global bios_checksum
525 bios_checksum:
526         .byte 0x00
527
528         .end