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