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