98627122c146c38bc5f93cd4b41d49e39d704072
[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         .globl post16
128 post16:
129         // init the stack pointer
130         RESET_STACK
131
132         // Set entry point of rombios32 code - the actual address
133         // is altered later in the build process.
134         .globl set_entry32
135 set_entry32:
136         pushl $0xf0000000
137
138         cld
139
140         // Fall through to transition32 function below
141
142
143 /****************************************************************
144  * Call trampolines
145  ****************************************************************/
146
147 // Place CPU into 32bit mode from 16bit mode.
148 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
149 transition32:
150         // Disable irqs
151         cli
152
153         // enable a20
154         inb $0x92, %al
155         orb $0x02, %al
156         outb %al, $0x92
157
158         // Set segment descriptors
159         lidt %cs:pmode_IDT_info
160         lgdt %cs:rombios32_gdt_48
161
162         // set PE bit in CR0
163         movl  %cr0, %eax
164         orb   $0x01, %al
165         movl  %eax, %cr0
166
167         // start protected mode code
168         .set __trans32, (BUILD_BIOS_ADDR + 1f)
169         ljmpl $PROTECTED_MODE_CS, $__trans32
170
171         .code32
172 1:
173         // init data segments
174         movl $PROTECTED_MODE_DS, %eax
175         movw %ax, %ds
176         movw %ax, %es
177         movw %ax, %ss
178         xorl %eax, %eax
179         movw %ax, %fs
180         movw %ax, %gs
181
182         retl
183
184 // Call a 16bit function from 32bit mode.
185 // %eax = address of struct bregs
186 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
187         .globl __call16_from32
188 __call16_from32:
189         pushl %eax
190
191         // Jump to 16bit mode
192         ljmpw $REAL_MODE_CS, $1f
193
194         .code16gcc
195 1:
196         // restore data segment limits to 0xffff
197         movw $REAL_MODE_DS, %ax
198         movw %ax, %ds
199         movw %ax, %es
200         movw %ax, %ss
201         movw %ax, %fs
202         movw %ax, %gs
203
204         // reset PE bit in CR0
205         movl %cr0, %eax
206         andb $0xfe, %al
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         .globl __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         .globl 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         .globl 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         .code16gcc
316
317
318 /****************************************************************
319  * GDT and IDT tables
320  ****************************************************************/
321
322 // Protected mode IDT descriptor
323 //
324 // I just make the limit 0, so the machine will shutdown
325 // if an exception occurs during protected mode memory
326 // transfers.
327 //
328 // Set base to f0000 to correspond to beginning of BIOS,
329 // in case I actually define an IDT later
330 // Set limit to 0
331 pmode_IDT_info:
332         .word 0x0000  // limit 15:00
333         .long 0xf0000 // base 16:47
334
335 // Real mode IDT descriptor
336 //
337 // Set to typical real-mode values.
338 // base  = 000000
339 // limit =   03ff
340 rmode_IDT_info:
341         .word 0x03ff  // limit 15:00
342         .long 0       // base 16:47
343
344 rombios32_gdt_48:
345         .word 0x30
346         .word rombios32_gdt
347         .word 0x000f
348
349         .balign 8
350 rombios32_gdt:
351         .word 0, 0, 0, 0
352         .word 0, 0, 0, 0
353         // 32 bit flat code segment (PROTECTED_MODE_CS)
354         .word 0xffff, 0, 0x9b00, 0x00cf
355         // 32 bit flat data segment (PROTECTED_MODE_DS)
356         .word 0xffff, 0, 0x9300, 0x00cf
357         // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
358         .word 0xffff, 0, 0x9b0f, 0x0000
359         // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
360         .word 0xffff, 0, 0x9300, 0x0000
361
362
363 /****************************************************************
364  * Interrupt entry points
365  ****************************************************************/
366
367         // Define an entry point for an interrupt (no args passed).
368         .macro IRQ_ENTRY num
369         .globl entry_\num
370         entry_\num :
371         cli         // In case something far-calls instead of using "int"
372         ENTRY handle_\num
373         iretw
374         .endm
375
376         // Define an entry point for an interrupt (can read/modify args).
377         .macro IRQ_ENTRY_ARG num
378         .globl entry_\num
379         entry_\num :
380         cli         // In case something far-calls instead of using "int"
381         ENTRY_ARG handle_\num
382         iretw
383         .endm
384
385         ORG 0xe2c3
386         IRQ_ENTRY nmi
387
388         IRQ_ENTRY_ARG 13
389         IRQ_ENTRY_ARG 12
390         IRQ_ENTRY_ARG 11
391         IRQ_ENTRY 76
392         IRQ_ENTRY 1c
393         IRQ_ENTRY 70
394
395         ORG 0xe3fe
396         jmp entry_13
397
398         ORG 0xe401
399         // XXX - Fixed Disk Parameter Table
400
401         ORG 0xe6f2
402         jmp entry_19
403
404         ORG 0xe6f5
405 .include "out/cbt.proc.16.s"
406         .text
407
408         ORG 0xe729
409         // XXX - Baud Rate Generator Table
410
411         ORG 0xe739
412         IRQ_ENTRY_ARG 14
413
414         IRQ_ENTRY 74
415         IRQ_ENTRY 75
416
417         // int 18/19 are special - they reset the stack and do not return.
418         .globl entry_19
419 entry_19:
420         RESET_STACK
421         ENTRY handle_19
422
423         .globl entry_18
424 entry_18:
425         RESET_STACK
426         ENTRY handle_18
427
428         // IRQ trampolines
429         .macro IRQ_TRAMPOLINE num
430         .globl irq_trampoline_0x\num
431         irq_trampoline_0x\num :
432         int $0x\num
433         lretw
434         .endm
435
436         IRQ_TRAMPOLINE 02
437         IRQ_TRAMPOLINE 10
438         IRQ_TRAMPOLINE 13
439         IRQ_TRAMPOLINE 15
440         IRQ_TRAMPOLINE 16
441         IRQ_TRAMPOLINE 18
442         IRQ_TRAMPOLINE 19
443         IRQ_TRAMPOLINE 1c
444         IRQ_TRAMPOLINE 4a
445
446         ORG 0xe82e
447         IRQ_ENTRY_ARG 16
448
449 entry_hwirq:
450         ENTRY handle_hwirq
451
452         ORG 0xe987
453         IRQ_ENTRY 09
454
455         ORG 0xec59
456         IRQ_ENTRY_ARG 40
457
458         ORG 0xef57
459         IRQ_ENTRY 0e
460
461         ORG 0xefc7
462 .include "out/floppy_dbt.proc.16.s"
463         .text
464
465         ORG 0xefd2
466         IRQ_ENTRY_ARG 17
467
468         ORG 0xf045
469         // XXX int 10
470         iretw
471
472         ORG 0xf065
473         IRQ_ENTRY_ARG 10
474
475         ORG 0xf0a4
476         // XXX int 1D
477         iretw
478
479         .globl freespace2_start, freespace2_end
480 freespace2_start:
481
482         ORG 0xf841
483 freespace2_end:
484         jmp entry_12
485
486         ORG 0xf84d
487         jmp entry_11
488
489         ORG 0xf859
490         IRQ_ENTRY_ARG 15
491
492         ORG 0xfa6e
493 .include "out/font.proc.16.s"
494         .text
495
496         ORG 0xfe6e
497         IRQ_ENTRY_ARG 1a
498
499         ORG 0xfea5
500         IRQ_ENTRY 08
501
502         ORG 0xfef3
503         // XXX - Initial Interrupt Vector Offsets Loaded by POST
504
505         ORG 0xff00
506         // XXX - BIOS_COPYRIGHT_STRING
507         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
508
509         ORG 0xff53
510         .globl dummy_iret_handler
511 dummy_iret_handler:
512         iretw
513
514         ORG 0xff54
515         IRQ_ENTRY_ARG 05
516
517         ORG 0xfff0 // Power-up Entry Point
518         ljmpw $SEG_BIOS, $post16
519
520         ORG 0xfff5
521         // BIOS build date
522         .ascii "06/23/99"
523
524         ORG 0xfffe
525         .byte CONFIG_MODEL_ID
526
527         .globl bios_checksum
528 bios_checksum:
529         .byte 0x00
530
531         .end