Improve comments 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         // 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 $0x20, $1f
108
109         .code16gcc
110 1:
111         // restore data segment limits to 0xffff
112         movw $0x28, %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, 0x1c(%eax)   // Save %ecx
177         popl %ecx
178         movl %ecx, 0x20(%eax)   // Save %eax
179         popw %cx
180         movw %cx, 0x28(%eax)    // Save flags
181
182         // Store remaining registers
183         movw %es, 0x02(%eax)
184         movl %edi, 0x04(%eax)
185         movl %esi, 0x08(%eax)
186         movl %ebp, 0x0c(%eax)
187         movl %ebx, 0x14(%eax)
188         movl %edx, 0x18(%eax)
189
190         // Remove %eax
191         popl %eax
192
193         retl
194
195
196 /****************************************************************
197  * GDT and IDT tables
198  ****************************************************************/
199
200 // Protected mode IDT descriptor
201 //
202 // I just make the limit 0, so the machine will shutdown
203 // if an exception occurs during protected mode memory
204 // transfers.
205 //
206 // Set base to f0000 to correspond to beginning of BIOS,
207 // in case I actually define an IDT later
208 // Set limit to 0
209 pmode_IDT_info:
210         .word 0x0000  // limit 15:00
211         .long 0xf0000 // base 16:47
212
213 // Real mode IDT descriptor
214 //
215 // Set to typical real-mode values.
216 // base  = 000000
217 // limit =   03ff
218 rmode_IDT_info:
219         .word 0x03ff  // limit 15:00
220         .long 0       // base 16:47
221
222 rombios32_gdt_48:
223         .word 0x30
224         .word rombios32_gdt
225         .word 0x000f
226
227 rombios32_gdt:
228         .word 0, 0, 0, 0
229         .word 0, 0, 0, 0
230         // 32 bit flat code segment (PROTECTED_MODE_CS)
231         .word 0xffff, 0, 0x9b00, 0x00cf
232         // 32 bit flat data segment (PROTECTED_MODE_DS)
233         .word 0xffff, 0, 0x9300, 0x00cf
234         // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
235         .word 0xffff, 0, 0x9b0f, 0x0000
236         // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
237         .word 0xffff, 0, 0x9300, 0x0000
238
239 // We need a copy of this string, but we are not actually a PnP BIOS,
240 // so make sure it is *not* aligned, so OSes will not see it if they
241 // scan.
242         .align 2
243         .byte 0
244         .globl pnp_string
245 pnp_string:
246         .ascii "$PnP"
247
248
249 /****************************************************************
250  * Interrupt entry points
251  ****************************************************************/
252
253         .macro ENTRY cfunc
254         cli         // In case something far-calls insted of using "int"
255         pushal
256         pushw %es
257         pushw %ds
258         movw %ss, %ax
259         movw %ax, %ds
260         movzwl %sp, %esp
261         movl %esp, %eax
262         calll \cfunc
263         popw %ds
264         popw %es
265         popal
266         .endm
267
268         .macro IRQ_ENTRY num
269         .globl entry_\num
270         entry_\num :
271         ENTRY handle_\num
272         iretw
273         .endm
274
275         .macro IRQ_TRAMPOLINE num
276         .globl irq_trampoline_0x\num
277         irq_trampoline_0x\num :
278         int $0x\num
279         lretw
280         .endm
281
282         .org 0xe2c3
283         IRQ_ENTRY nmi
284
285         IRQ_ENTRY 13
286         IRQ_ENTRY 12
287         IRQ_ENTRY 11
288         IRQ_ENTRY 76
289         IRQ_ENTRY 1c
290         IRQ_ENTRY 70
291         IRQ_ENTRY 74
292         IRQ_ENTRY 75
293
294         .globl entry_19
295 entry_19:
296         RESET_STACK
297         calll handle_19
298
299         .globl entry_18
300 entry_18:
301         RESET_STACK
302         calll handle_18
303
304         IRQ_TRAMPOLINE 02
305         IRQ_TRAMPOLINE 10
306         IRQ_TRAMPOLINE 13
307         IRQ_TRAMPOLINE 15
308         IRQ_TRAMPOLINE 18
309         IRQ_TRAMPOLINE 19
310         IRQ_TRAMPOLINE 1c
311         IRQ_TRAMPOLINE 4a
312
313         .org 0xe3fe
314         jmp entry_13
315
316         .org 0xe401
317         // XXX - Fixed Disk Parameter Table
318
319         .org 0xe6f2
320         jmp entry_19
321
322         .org 0xe6f5
323 .include "out/cbt.proc.16.s"
324         .text
325
326         .org 0xe729
327         // XXX - Baud Rate Generator Table
328
329         .org 0xe739
330         IRQ_ENTRY 14
331
332         .org 0xe82e
333         IRQ_ENTRY 16
334
335         .org 0xe987
336         IRQ_ENTRY 09
337
338         .org 0xec59
339         IRQ_ENTRY 40
340
341         .org 0xef57
342         IRQ_ENTRY 0e
343
344         .org 0xefc7
345         // XXX - Diskette Controller Parameter Table
346
347         .org 0xefd2
348         IRQ_ENTRY 17
349
350         .org 0xf045
351         // XXX int 10
352         iretw
353
354         .org 0xf065
355         IRQ_ENTRY 10
356
357         .org 0xf0a4
358         // XXX int 1D
359         iretw
360
361         .globl freespace2_start, freespace2_end
362 freespace2_start:
363
364         .org 0xf841
365 freespace2_end:
366         jmp entry_12
367
368         .org 0xf84d
369         jmp entry_11
370
371         .org 0xf859
372         IRQ_ENTRY 15
373
374         .org 0xfa6e
375 .include "out/font.proc.16.s"
376         .text
377
378         .org 0xfe6e
379         IRQ_ENTRY 1a
380
381         .org 0xfea5
382         IRQ_ENTRY 08
383
384         .org 0xfef3
385         // XXX - Initial Interrupt Vector Offsets Loaded by POST
386
387         .org 0xff00
388         // XXX - BIOS_COPYRIGHT_STRING
389         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
390
391         .org 0xff53
392         .globl dummy_iret_handler
393 dummy_iret_handler:
394         iretw
395
396         .org 0xff54
397         IRQ_ENTRY 05
398
399         .org 0xfff0 // Power-up Entry Point
400         ljmpw $0xf000, $post16
401
402         .org 0xfff5
403         // BIOS build date
404         .ascii "06/23/99"
405
406         .org 0xfffe
407         // model byte 0xFC = AT
408         .byte 0xfc
409         .byte 0x00
410
411         .end