Use symbols instead of number for segment descriptors in romlayout.S
[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         .word 0xea66, 1f, 0x000f, PROTECTED_MODE_CS // ljmpl $PROTECTED_MODE_CS, $1f
82
83         .code32
84 1:
85         // init data segments
86         movl $PROTECTED_MODE_DS, %eax
87         movw %ax, %ds
88         movw %ax, %es
89         movw %ax, %ss
90         xorl %eax, %eax
91         movw %ax, %fs
92         movw %ax, %gs
93
94         cld
95
96         retl
97
98 // Call a 16bit function from 32bit mode.
99 // 4(%esp) = address of struct bregs
100 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
101         .globl __call16_from32
102 __call16_from32:
103         pushl %eax
104
105         // Jump to 16bit mode
106         ljmp $0x20, $1f
107
108         .code16gcc
109 1:
110         // restore data segment limits to 0xffff
111         movw $0x28, %ax
112         movw %ax, %ds
113         movw %ax, %es
114         movw %ax, %ss
115         movw %ax, %fs
116         movw %ax, %gs
117
118         // reset PE bit in CR0
119         movl %cr0, %eax
120         andb $0xfe, %al
121         movl %eax, %cr0
122
123         // far jump to flush CPU queue after transition to real mode
124         ljmpw $0xf000, $2f
125
126 2:
127         // restore IDT to normal real-mode defaults
128         lidt %cs:rmode_IDT_info
129
130         // Clear segment registers
131         xorw %ax, %ax
132         movw %ax, %fs
133         movw %ax, %gs
134         movw %ax, %es
135         movw %ax, %ds
136         movw %ax, %ss  // Assume stack is in segment 0
137
138         popl %eax
139         pushl $transition32
140
141         // Fall through to __call16
142
143
144 // Call a 16bit function from 16bit mode with a specified cpu register state
145 // %eax = address of struct bregs
146 // Clobbers: all gp registers, es
147         .globl __call16
148 __call16:
149         // Save eax
150         pushl %eax
151
152         // Setup for iretw call
153         pushw $0xf000
154         pushw $1f               // return point
155         pushw 0x28(%eax)        // flags
156         pushl 0x24(%eax)        // CS:IP
157
158         // Load calling registers.
159         movl 0x04(%eax), %edi
160         movl 0x08(%eax), %esi
161         movl 0x0c(%eax), %ebp
162         movl 0x14(%eax), %ebx
163         movl 0x18(%eax), %edx
164         movl 0x1c(%eax), %ecx
165         movw 0x02(%eax), %es    // XXX - should load %ds too
166         movl 0x20(%eax), %eax
167
168         // Invoke call
169         iretw                   // XXX - just do a lcalll
170 1:
171         // Store flags, eax, ecx
172         pushfw
173         pushl %eax
174         movl 0x06(%esp), %eax
175         movl %ecx, 0x1c(%eax)   // Save %ecx
176         popl %ecx
177         movl %ecx, 0x20(%eax)   // Save %eax
178         popw %cx
179         movw %cx, 0x28(%eax)    // Save flags
180
181         // Store remaining registers
182         movw %es, 0x02(%eax)
183         movl %edi, 0x04(%eax)
184         movl %esi, 0x08(%eax)
185         movl %ebp, 0x0c(%eax)
186         movl %ebx, 0x14(%eax)
187         movl %edx, 0x18(%eax)
188
189         // Remove %eax
190         popl %eax
191
192         retl
193
194
195 /****************************************************************
196  * GDT and IDT tables
197  ****************************************************************/
198
199 // Protected mode IDT descriptor
200 //
201 // I just make the limit 0, so the machine will shutdown
202 // if an exception occurs during protected mode memory
203 // transfers.
204 //
205 // Set base to f0000 to correspond to beginning of BIOS,
206 // in case I actually define an IDT later
207 // Set limit to 0
208 pmode_IDT_info:
209         .word 0x0000  // limit 15:00
210         .long 0xf0000 // base 16:47
211
212 // Real mode IDT descriptor
213 //
214 // Set to typical real-mode values.
215 // base  = 000000
216 // limit =   03ff
217 rmode_IDT_info:
218         .word 0x03ff  // limit 15:00
219         .long 0       // base 16:47
220
221 rombios32_gdt_48:
222         .word 0x30
223         .word rombios32_gdt
224         .word 0x000f
225
226 rombios32_gdt:
227         .word 0, 0, 0, 0
228         .word 0, 0, 0, 0
229         .word 0xffff, 0, 0x9b00, 0x00cf // 32 bit flat code segment (PROTECTED_MODE_CS)
230         .word 0xffff, 0, 0x9300, 0x00cf // 32 bit flat data segment (PROTECTED_MODE_DS)
231         .word 0xffff, 0, 0x9b0f, 0x0000 // 16 bit code segment base=0xf0000 limit=0xffff
232         .word 0xffff, 0, 0x9300, 0x0000 // 16 bit data segment base=0x0 limit=0xffff
233
234 // We need a copy of this string, but we are not actually a PnP BIOS,
235 // so make sure it is *not* aligned, so OSes will not see it if they
236 // scan.
237         .align 2
238         .byte 0
239         .globl pnp_string
240 pnp_string:
241         .ascii "$PnP"
242
243
244 /****************************************************************
245  * Interrupt entry points
246  ****************************************************************/
247
248         .macro ENTRY cfunc
249         cli         // In case something far-calls insted of using "int"
250         pushal
251         pushw %es
252         pushw %ds
253         movw %ss, %ax
254         movw %ax, %ds
255         movzwl %sp, %esp
256         movl %esp, %eax
257         calll \cfunc
258         popw %ds
259         popw %es
260         popal
261         .endm
262
263         .macro IRQ_ENTRY num
264         .globl entry_\num
265         entry_\num :
266         ENTRY handle_\num
267         iretw
268         .endm
269
270         .macro IRQ_TRAMPOLINE num
271         .globl irq_trampoline_0x\num
272         irq_trampoline_0x\num :
273         int $0x\num
274         lretw
275         .endm
276
277         .org 0xe2c3
278         IRQ_ENTRY nmi
279
280         IRQ_ENTRY 13
281         IRQ_ENTRY 12
282         IRQ_ENTRY 11
283         IRQ_ENTRY 76
284         IRQ_ENTRY 1c
285         IRQ_ENTRY 70
286         IRQ_ENTRY 74
287         IRQ_ENTRY 75
288
289         .globl entry_19
290 entry_19:
291         RESET_STACK
292         calll handle_19
293
294         .globl entry_18
295 entry_18:
296         RESET_STACK
297         calll handle_18
298
299         IRQ_TRAMPOLINE 02
300         IRQ_TRAMPOLINE 10
301         IRQ_TRAMPOLINE 13
302         IRQ_TRAMPOLINE 15
303         IRQ_TRAMPOLINE 18
304         IRQ_TRAMPOLINE 19
305         IRQ_TRAMPOLINE 1c
306         IRQ_TRAMPOLINE 4a
307
308         .org 0xe3fe
309         jmp entry_13
310
311         .org 0xe401
312         // XXX - Fixed Disk Parameter Table
313
314         .org 0xe6f2
315         jmp entry_19
316
317         .org 0xe6f5
318 .include "out/cbt.proc.16.s"
319         .text
320
321         .org 0xe729
322         // XXX - Baud Rate Generator Table
323
324         .org 0xe739
325         IRQ_ENTRY 14
326
327         .org 0xe82e
328         IRQ_ENTRY 16
329
330         .org 0xe987
331         IRQ_ENTRY 09
332
333         .org 0xec59
334         IRQ_ENTRY 40
335
336         .org 0xef57
337         IRQ_ENTRY 0e
338
339         .org 0xefc7
340         // XXX - Diskette Controller Parameter Table
341
342         .org 0xefd2
343         IRQ_ENTRY 17
344
345         .org 0xf045
346         // XXX int 10
347         iretw
348
349         .org 0xf065
350         IRQ_ENTRY 10
351
352         .org 0xf0a4
353         // XXX int 1D
354         iretw
355
356         .globl freespace2_start, freespace2_end
357 freespace2_start:
358
359         .org 0xf841
360 freespace2_end:
361         jmp entry_12
362
363         .org 0xf84d
364         jmp entry_11
365
366         .org 0xf859
367         IRQ_ENTRY 15
368
369         .org 0xfa6e
370 .include "out/font.proc.16.s"
371         .text
372
373         .org 0xfe6e
374         IRQ_ENTRY 1a
375
376         .org 0xfea5
377         IRQ_ENTRY 08
378
379         .org 0xfef3
380         // XXX - Initial Interrupt Vector Offsets Loaded by POST
381
382         .org 0xff00
383         // XXX - BIOS_COPYRIGHT_STRING
384         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
385
386         .org 0xff53
387         .globl dummy_iret_handler
388 dummy_iret_handler:
389         iretw
390
391         .org 0xff54
392         IRQ_ENTRY 05
393
394         .org 0xfff0 // Power-up Entry Point
395         ljmpw $0xf000, $post16
396
397         .org 0xfff5
398         // BIOS build date
399         .ascii "06/23/99"
400
401         .org 0xfffe
402         // model byte 0xFC = AT
403         .byte 0xfc
404         .byte 0x00
405
406         .end