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