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