Fix bug in apm32protected_entry.
[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
39         pushl %ecx
40         pushl %edx
41         pushw %es
42         pushw %ds
43         movw %ss, %ax
44         movw %ax, %ds
45         calll \cfunc
46         popw %ds
47         popw %es
48         popl %edx
49         popl %ecx
50         popl %eax
51         .endm
52
53         // Call a C function with current register list as an
54         // argument.  This backs up the registers and sets %eax
55         // to point to the backup.  On return, the registers are
56         // restored from the structure.
57         .macro ENTRY_ARG cfunc
58         cld
59         pushl %eax
60         pushl %ecx
61         pushl %edx
62         pushl %ebx
63         pushl %esi
64         pushl %edi
65         pushw %es
66         pushw %ds
67         movw %ss, %ax
68         movw %ax, %ds
69         movzwl %sp, %esp
70         movl %esp, %eax
71         calll \cfunc
72         popw %ds
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         // Macro to reset the 16bit stack
83         // Clobbers %ax
84         .macro RESET_STACK
85         xorw %ax, %ax
86         movw %ax, %ss
87         movl $ CONFIG_STACK_OFFSET , %esp
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         pushl $transition32
195
196         // Fall through to __call16
197
198
199 // Call a 16bit function from 16bit mode with a specified cpu register state
200 // %eax = address of struct bregs
201 // Clobbers: all gp registers, es
202         .globl __call16
203 __call16:
204         // Save eax
205         pushl %eax
206
207         // Setup for iretw call
208         pushw $0xf000
209         pushw $1f               // return point
210         pushw 0x20(%eax)        // flags
211         pushl 0x1c(%eax)        // CS:IP
212
213         // Load calling registers.
214         movl 0x04(%eax), %edi
215         movl 0x08(%eax), %esi
216         movl 0x0c(%eax), %ebx
217         movl 0x10(%eax), %edx
218         movl 0x14(%eax), %ecx
219         movw 0x02(%eax), %es    // XXX - should load %ds too
220         movl 0x18(%eax), %eax
221
222         // Invoke call
223         iretw                   // XXX - just do a lcalll
224 1:
225         // Store flags, eax, ecx
226         pushfw
227         pushl %eax
228         movl 0x06(%esp), %eax
229         movl %ecx, %ss:0x14(%eax)       // Save %ecx
230         movw %ss, %cx
231         movw %cx, %ds                   // Restore %ds == %ss
232         popl %ecx
233         movl %ecx, 0x18(%eax)           // Save %eax
234         popw %cx
235         movw %cx, 0x20(%eax)            // Save flags
236
237         // Store remaining registers
238         movw %es, 0x02(%eax)
239         movl %edi, 0x04(%eax)
240         movl %esi, 0x08(%eax)
241         movl %ebx, 0x0c(%eax)
242         movl %edx, 0x10(%eax)
243
244         // Remove %eax
245         popl %eax
246
247         cld
248
249         retl
250
251         // APM trampolines
252         .globl apm16protected_entry
253 apm16protected_entry:
254         pushfw          // save flags
255         pushl %eax      // dummy
256         ENTRY_ARG handle_1553
257         addw $4, %sp    // pop dummy
258         popfw           // restore flags
259         lretw
260
261         .code32
262         .globl apm32protected_entry
263 apm32protected_entry:
264         pushfw
265         pushw %cs       // Setup for long jump to 16bit mode
266         pushw $1f
267         incw 2(%esp)
268         ljmpw *(%esp)
269         .code16gcc
270 1:
271         ENTRY_ARG handle_1553
272
273         movw $2f,(%esp) // Setup for long jump back to 32bit mode
274         decw 2(%esp)
275         ljmpw *(%esp)
276         .code32
277 2:
278         addl $4, %esp   // pop call address
279         popfw
280         lretl
281         .code16gcc
282
283
284 /****************************************************************
285  * GDT and IDT tables
286  ****************************************************************/
287
288 // Protected mode IDT descriptor
289 //
290 // I just make the limit 0, so the machine will shutdown
291 // if an exception occurs during protected mode memory
292 // transfers.
293 //
294 // Set base to f0000 to correspond to beginning of BIOS,
295 // in case I actually define an IDT later
296 // Set limit to 0
297 pmode_IDT_info:
298         .word 0x0000  // limit 15:00
299         .long 0xf0000 // base 16:47
300
301 // Real mode IDT descriptor
302 //
303 // Set to typical real-mode values.
304 // base  = 000000
305 // limit =   03ff
306 rmode_IDT_info:
307         .word 0x03ff  // limit 15:00
308         .long 0       // base 16:47
309
310 rombios32_gdt_48:
311         .word 0x30
312         .word rombios32_gdt
313         .word 0x000f
314
315         .balign 8
316 rombios32_gdt:
317         .word 0, 0, 0, 0
318         .word 0, 0, 0, 0
319         // 32 bit flat code segment (PROTECTED_MODE_CS)
320         .word 0xffff, 0, 0x9b00, 0x00cf
321         // 32 bit flat data segment (PROTECTED_MODE_DS)
322         .word 0xffff, 0, 0x9300, 0x00cf
323         // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
324         .word 0xffff, 0, 0x9b0f, 0x0000
325         // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
326         .word 0xffff, 0, 0x9300, 0x0000
327
328
329 /****************************************************************
330  * Interrupt entry points
331  ****************************************************************/
332
333         // Define an entry point for an interrupt (no args passed).
334         .macro IRQ_ENTRY num
335         .globl entry_\num
336         entry_\num :
337         cli         // In case something far-calls instead of using "int"
338         ENTRY handle_\num
339         iretw
340         .endm
341
342         // Define an entry point for an interrupt (can read/modify args).
343         .macro IRQ_ENTRY_ARG num
344         .globl entry_\num
345         entry_\num :
346         cli         // In case something far-calls instead of using "int"
347         ENTRY_ARG handle_\num
348         iretw
349         .endm
350
351         .org 0xe2c3
352         IRQ_ENTRY nmi
353
354         IRQ_ENTRY_ARG 13
355         IRQ_ENTRY_ARG 12
356         IRQ_ENTRY_ARG 11
357         IRQ_ENTRY 76
358         IRQ_ENTRY 1c
359         IRQ_ENTRY 70
360         IRQ_ENTRY 74
361
362         .org 0xe3fe
363         jmp entry_13
364
365         .org 0xe401
366         // XXX - Fixed Disk Parameter Table
367
368         .org 0xe6f2
369         jmp entry_19
370
371         .org 0xe6f5
372 .include "out/cbt.proc.16.s"
373         .text
374
375         .org 0xe729
376         // XXX - Baud Rate Generator Table
377
378         .org 0xe739
379         IRQ_ENTRY_ARG 14
380
381         IRQ_ENTRY 75
382
383         // int 18/19 are special - they reset the stack and do not return.
384         .globl entry_19
385 entry_19:
386         RESET_STACK
387         ENTRY handle_19
388
389         .globl entry_18
390 entry_18:
391         RESET_STACK
392         ENTRY handle_18
393
394         // IRQ trampolines
395         .macro IRQ_TRAMPOLINE num
396         .globl irq_trampoline_0x\num
397         irq_trampoline_0x\num :
398         int $0x\num
399         lretw
400         .endm
401
402         IRQ_TRAMPOLINE 02
403         IRQ_TRAMPOLINE 10
404         IRQ_TRAMPOLINE 13
405         IRQ_TRAMPOLINE 15
406         IRQ_TRAMPOLINE 18
407         IRQ_TRAMPOLINE 19
408         IRQ_TRAMPOLINE 1c
409         IRQ_TRAMPOLINE 4a
410
411         .org 0xe82e
412         IRQ_ENTRY_ARG 16
413
414         .org 0xe987
415         IRQ_ENTRY 09
416
417         .org 0xec59
418         IRQ_ENTRY_ARG 40
419
420         .org 0xef57
421         IRQ_ENTRY 0e
422
423         .org 0xefc7
424 .include "out/floppy_dbt.proc.16.s"
425         .text
426
427         .org 0xefd2
428         IRQ_ENTRY_ARG 17
429
430         .org 0xf045
431         // XXX int 10
432         iretw
433
434         .org 0xf065
435         IRQ_ENTRY_ARG 10
436
437         .org 0xf0a4
438         // XXX int 1D
439         iretw
440
441         .globl freespace2_start, freespace2_end
442 freespace2_start:
443
444         .org 0xf841
445 freespace2_end:
446         jmp entry_12
447
448         .org 0xf84d
449         jmp entry_11
450
451         .org 0xf859
452         IRQ_ENTRY_ARG 15
453
454         .org 0xfa6e
455 .include "out/font.proc.16.s"
456         .text
457
458         .org 0xfe6e
459         IRQ_ENTRY_ARG 1a
460
461         .org 0xfea5
462         IRQ_ENTRY 08
463
464         .org 0xfef3
465         // XXX - Initial Interrupt Vector Offsets Loaded by POST
466
467         .org 0xff00
468         // XXX - BIOS_COPYRIGHT_STRING
469         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
470
471         .org 0xff53
472         .globl dummy_iret_handler
473 dummy_iret_handler:
474         iretw
475
476         .org 0xff54
477         IRQ_ENTRY_ARG 05
478
479         .org 0xfff0 // Power-up Entry Point
480         ljmpw $0xf000, $post16
481
482         .org 0xfff5
483         // BIOS build date
484         .ascii "06/23/99"
485
486         .org 0xfffe
487         .byte CONFIG_MODEL_ID
488         .byte 0x00
489
490         .end