Consistently disable irqs at start of each assembler entry point.
[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 LGPLv3 license.
7
8 #include "config.h" // CONFIG_*
9 #include "ioport.h" // PORT_A20
10 #include "bregs.h" // CR0_*
11 #include "cmos.h" // CMOS_RESET_CODE
12 #include "../out/asm-offsets.h" // BREGS_*
13
14
15 /****************************************************************
16  * Include of 16bit C code
17  ****************************************************************/
18
19         .code16gcc
20 .include "out/ccode.16.s"
21
22
23 /****************************************************************
24  * Entry macros
25  ****************************************************************/
26
27         // Call a C function - this does the minimal work necessary to
28         // call into C.  It sets up %ds, backs up %es, and backs up
29         // those registers that are call clobbered by the C compiler.
30         .macro ENTRY cfunc
31         cli         // In case something far-calls instead of using "int"
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         cli
57         cld
58         pushl %eax              // Save registers (matches struct bregs)
59         pushl %ecx
60         pushl %edx
61         pushl %ebx
62         pushl %esi
63         pushl %edi
64         pushw %es
65         pushw %ds
66         movw %ss, %ax           // Move %ss to %ds
67         movw %ax, %ds
68         movl %esp, %ebx         // Backup %esp, then zero high bits
69         movzwl %sp, %esp
70         movl %esp, %eax         // First arg is pointer to struct bregs
71         calll \cfunc
72         movl %ebx, %esp         // Restore %esp (including high bits)
73         popw %ds                // Restore registers (from struct bregs)
74         popw %es
75         popl %edi
76         popl %esi
77         popl %ebx
78         popl %edx
79         popl %ecx
80         popl %eax
81         .endm
82
83         // As above, but don't mangle %esp
84         .macro ENTRY_ARG_ESP cfunc
85         cli
86         cld
87         pushl %eax              // Save registers (matches struct bregs)
88         pushl %ecx
89         pushl %edx
90         pushl %ebx
91         pushl %esi
92         pushl %edi
93         pushw %es
94         pushw %ds
95         movw %ss, %ax           // Move %ss to %ds
96         movw %ax, %ds
97         movl %esp, %eax         // First arg is pointer to struct bregs
98         calll \cfunc
99         popw %ds                // Restore registers (from struct bregs)
100         popw %es
101         popl %edi
102         popl %esi
103         popl %ebx
104         popl %edx
105         popl %ecx
106         popl %eax
107         .endm
108
109         // Reset stack, transition to 32bit mode, and call a C function.
110         // Clobbers %ax
111         .macro ENTRY_INTO32 cfunc
112         xorw %ax, %ax
113         movw %ax, %ss
114         movl $ BUILD_STACK_ADDR , %esp
115         pushl $ \cfunc
116         jmp transition32
117         .endm
118
119         // Declare a function
120         .macro DECLFUNC func
121         .section .text.asm.\func
122         .global \func
123         .endm
124
125
126 /****************************************************************
127  * POST handler
128  ****************************************************************/
129
130         DECLFUNC entry_post
131 entry_post:
132         // Enable cache
133         movl %cr0, %eax
134         andl $~(CR0_CD|CR0_NW), %eax
135         movl %eax, %cr0
136
137         // Disable interrupts
138         cli
139         cld
140
141         // Check for restart indicator.
142         movl $CMOS_RESET_CODE, %eax
143         outb %al, $PORT_CMOS_INDEX
144         inb $PORT_CMOS_DATA, %al
145         cmpb $0x0, %al
146         jnz 1f
147
148         // Normal entry point
149         ENTRY_INTO32 _code32__start
150
151         // Entry point when a post call looks like a resume.
152 1:
153         // Save old shutdown status.
154         movl %eax, %ebx
155
156         // Clear shutdown status register.
157         movl $CMOS_RESET_CODE, %eax
158         outb %al, $PORT_CMOS_INDEX
159         xorl %eax, %eax
160         outb %al, $PORT_CMOS_DATA
161
162         // Use a stack in EBDA
163         movw $SEG_BDA, %ax
164         movw %ax, %ds
165         movw BDA_ebda_seg, %ax
166         // XXX - should verify ebda_seg looks sane.
167         movw %ax, %ds
168         movw %ax, %ss
169         movl $EBDA_OFFSET_TOP_STACK, %esp
170
171         // Call handler.
172         movl %ebx, %eax
173         jmp handle_resume
174
175
176 /****************************************************************
177  * Call trampolines
178  ****************************************************************/
179
180 // Place CPU into 32bit mode from 16bit mode.
181 // Clobbers: flags, segment registers, cr0, idt/gdt
182 // Require: interrupts must be disabled
183         DECLFUNC transition32
184 transition32:
185         pushl %eax
186
187         // enable a20
188         inb $PORT_A20, %al
189         orb $A20_ENABLE_BIT, %al
190         outb %al, $PORT_A20
191
192         // Set segment descriptors
193         lidtw %cs:pmode_IDT_info
194         lgdtw %cs:rombios32_gdt_48
195
196         // Enable protected mode
197         movl %cr0, %eax
198         orl $CR0_PE, %eax
199         movl %eax, %cr0
200
201         // start 32bit protected mode code
202         ljmpl $SEG32_MODE32_CS, $(BUILD_BIOS_ADDR + 1f)
203
204         .code32
205 1:
206         // init data segments
207         movl $SEG32_MODE32_DS, %eax
208         movw %ax, %ds
209         movw %ax, %es
210         movw %ax, %ss
211         movw %ax, %fs
212         movw %ax, %gs
213
214         popl %eax
215         retl
216
217 // Call a 16bit function from 32bit mode.
218 // %eax = address of struct bregs
219 // Clobbers: %e[bcd]x, %e[ds]i, flags, segment registers, idt/gdt
220         DECLFUNC __call16_from32
221         .global __call16big_from32
222 __call16_from32:
223         pushl %eax
224
225         // restore data segment limits to 0xffff
226         movl $SEG32_MODE16_DS, %eax
227         movw %ax, %ds
228         movw %ax, %es
229         movw %ax, %ss
230         movw %ax, %fs
231         movw %ax, %gs
232
233         // disable a20
234         inb $PORT_A20, %al
235         andb $~A20_ENABLE_BIT, %al
236         outb %al, $PORT_A20
237
238         // Jump to 16bit mode
239         ljmpw $SEG32_MODE16_CS, $1f
240
241 __call16big_from32:
242         pushl %eax
243
244         movl $SEG32_MODE16BIG_DS, %eax
245         movw %ax, %ds
246         movw %ax, %es
247         movw %ax, %ss
248         movw %ax, %fs
249         movw %ax, %gs
250
251         ljmpl $SEG32_MODE16BIG_CS, $(BUILD_BIOS_ADDR + 1f)
252
253         .code16gcc
254 1:
255         // Disable protected mode
256         movl %cr0, %eax
257         andl $~CR0_PE, %eax
258         movl %eax, %cr0
259
260         // far jump to flush CPU queue after transition to real mode
261         ljmpw $SEG_BIOS, $2f
262
263 2:
264         // restore IDT to normal real-mode defaults
265         lidtw %cs:rmode_IDT_info
266
267         // Clear segment registers
268         xorw %ax, %ax
269         movw %ax, %fs
270         movw %ax, %gs
271         movw %ax, %es
272         movw %ax, %ds
273         movw %ax, %ss  // Assume stack is in segment 0
274
275         popl %eax
276
277         // Make call.
278         calll __call16
279
280         // Return via transition32
281         jmp transition32
282
283
284 // Call a 16bit function from 16bit mode with a specified cpu register state
285 // %eax = address of struct bregs
286 // Clobbers: %e[bcd]x, %e[ds]i, flags
287         DECLFUNC __call16
288 __call16:
289         // Save %eax, %ebp
290         pushl %ebp
291         pushl %eax
292
293         // Setup for iretw call
294         pushw %cs
295         pushw $1f               // return point
296         pushw BREGS_flags(%eax) // flags
297         pushl BREGS_ip(%eax)    // CS:IP
298
299         // Load calling registers.
300         movl BREGS_edi(%eax), %edi
301         movl BREGS_esi(%eax), %esi
302         movl BREGS_ebx(%eax), %ebx
303         movl BREGS_edx(%eax), %edx
304         movl BREGS_ecx(%eax), %ecx
305         movw BREGS_es(%eax), %es
306         movw BREGS_ds(%eax), %ds
307         movl %ss:BREGS_eax(%eax), %eax
308
309         // Invoke call
310         iretw                   // XXX - just do a lcalll
311 1:
312         // Store flags, eax, ecx
313         pushfw
314         pushl %eax
315         movl 0x06(%esp), %eax
316         movl %ecx, %ss:BREGS_ecx(%eax)
317         movw %ds, %ss:BREGS_ds(%eax)
318         movw %ss, %cx
319         movw %cx, %ds           // Restore %ds == %ss
320         popl %ecx
321         movl %ecx, BREGS_eax(%eax)
322         popw %cx
323         movw %cx, BREGS_flags(%eax)
324
325         // Store remaining registers
326         movw %es, BREGS_es(%eax)
327         movl %edi, BREGS_edi(%eax)
328         movl %esi, BREGS_esi(%eax)
329         movl %ebx, BREGS_ebx(%eax)
330         movl %edx, BREGS_edx(%eax)
331
332         // Remove %eax, restore %ebp
333         popl %eax
334         popl %ebp
335
336         cli
337         cld
338
339         retl
340
341 // PnP trampolines
342         DECLFUNC entry_pnp_real
343         .global entry_pnp_prot
344 entry_pnp_prot:
345         pushl %esp
346         jmp 1f
347 entry_pnp_real:
348         pushl %esp              // Backup %esp, then clear high bits
349         movzwl %sp, %esp
350 1:
351         pushfl                  // Save registers clobbered by C code
352         pushl %eax
353         pushl %ecx
354         pushl %edx
355         pushw %es
356         pushw %ds
357         movw %ss, %cx           // Move %ss to %ds
358         movw %cx, %ds
359         lea 28(%esp), %eax      // %eax points to start of u16 args
360         calll handle_pnp
361         movw %ax, 12(%esp)      // Modify %eax to return %ax
362         popw %ds
363         popw %es
364         popl %edx
365         popl %ecx
366         popl %eax
367         popfl
368         popl %esp
369         lretw
370
371 // APM trampolines
372         DECLFUNC apm16protected_entry
373 apm16protected_entry:
374         pushfw          // save flags
375         pushl %eax      // dummy
376         ENTRY_ARG handle_1553
377         addw $4, %sp    // pop dummy
378         popfw           // restore flags
379         lretw
380
381         .code32
382         DECLFUNC apm32protected_entry
383 apm32protected_entry:
384         pushfw
385         pushw %cs       // Setup for long jump to 16bit mode
386         pushw $1f
387         addw $8, 2(%esp)
388         ljmpw *(%esp)
389         .code16gcc
390 1:
391         ENTRY_ARG_ESP handle_1553
392
393         movw $2f,(%esp) // Setup for long jump back to 32bit mode
394         subw $8, 2(%esp)
395         ljmpw *(%esp)
396         .code32
397 2:
398         addl $4, %esp   // pop call address
399         popfw
400         lretl
401
402 // 32bit elf entry point
403         DECLFUNC post32
404 post32:
405         cli
406         cld
407         lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
408         lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
409         movl $BUILD_STACK_ADDR, %esp
410         ljmpl $SEG32_MODE32_CS, $_code32__start
411
412         .code16gcc
413
414 // IRQ trampolines
415         .macro IRQ_TRAMPOLINE num
416         DECLFUNC irq_trampoline_0x\num
417         irq_trampoline_0x\num :
418         int $0x\num
419         lretw
420         .endm
421
422         IRQ_TRAMPOLINE 10
423         IRQ_TRAMPOLINE 13
424         IRQ_TRAMPOLINE 15
425         IRQ_TRAMPOLINE 16
426         IRQ_TRAMPOLINE 18
427         IRQ_TRAMPOLINE 19
428
429
430 /****************************************************************
431  * Interrupt entry points
432  ****************************************************************/
433
434         // Define an entry point for an interrupt (no args passed).
435         .macro IRQ_ENTRY num
436         .global entry_\num
437         entry_\num :
438         ENTRY handle_\num
439         iretw
440         .endm
441
442         // Define an entry point for an interrupt (can read/modify args).
443         .macro IRQ_ENTRY_ARG num
444         .global entry_\num
445         entry_\num :
446         ENTRY_ARG handle_\num
447         iretw
448         .endm
449
450         // Macros that put each handler into its own section
451         .macro DECL_IRQ_ENTRY num
452         .section .text.asm.entry_\num
453         IRQ_ENTRY \num
454         .endm
455         .macro DECL_IRQ_ENTRY_ARG num
456         .section .text.asm.entry_\num
457         IRQ_ENTRY_ARG \num
458         .endm
459
460         DECL_IRQ_ENTRY_ARG 13
461         DECL_IRQ_ENTRY_ARG 12
462         DECL_IRQ_ENTRY_ARG 11
463         DECL_IRQ_ENTRY 76
464         DECL_IRQ_ENTRY 70
465         DECL_IRQ_ENTRY 74
466         DECL_IRQ_ENTRY 75
467         DECL_IRQ_ENTRY hwpic1
468         DECL_IRQ_ENTRY hwpic2
469
470         // int 18/19 are special - they reset stack and call into 32bit mode.
471         DECLFUNC entry_19
472 entry_19:
473         cli
474         cld
475         ENTRY_INTO32 _code32_handle_19
476
477         DECLFUNC entry_18
478 entry_18:
479         cli
480         cld
481         ENTRY_INTO32 _code32_handle_18
482
483
484 /****************************************************************
485  * Fixed position entry points
486  ****************************************************************/
487
488         // Specify a location in the fixed part of bios area.
489         .macro ORG addr
490         .section .fixedaddr.\addr
491         .endm
492
493         ORG 0xe05b
494 entry_post_official:
495         jmp entry_post
496
497         ORG 0xe2c3
498         IRQ_ENTRY nmi
499
500         ORG 0xe3fe
501         .global entry_13_official
502 entry_13_official:
503         jmp entry_13
504
505         // 0xe401 - OldFDPT in disk.c
506
507         ORG 0xe6f2
508         .global entry_19_official
509 entry_19_official:
510         jmp entry_19
511
512         // 0xe6f5 - BIOS_CONFIG_TABLE in misc.c
513
514         // 0xe729 - BaudTable in serial.c
515
516         ORG 0xe739
517         IRQ_ENTRY_ARG 14
518
519         ORG 0xe82e
520         IRQ_ENTRY_ARG 16
521
522         ORG 0xe987
523         IRQ_ENTRY 09
524
525         ORG 0xec59
526         IRQ_ENTRY_ARG 40
527
528         ORG 0xef57
529         IRQ_ENTRY 0e
530
531         // 0xefc7 - diskette_param_table in floppy.c
532
533         ORG 0xefd2
534         IRQ_ENTRY_ARG 17
535
536         ORG 0xf045
537 entry_10_0x0f:
538         // XXX - INT 10 Functions 0-Fh Entry Point
539         iretw
540
541         ORG 0xf065
542         IRQ_ENTRY_ARG 10
543
544         // 0xf0a4 - VideoParams in misc.c
545
546         ORG 0xf841
547         .global entry_12_official
548 entry_12_official:
549         jmp entry_12
550
551         ORG 0xf84d
552         .global entry_11_official
553 entry_11_official:
554         jmp entry_11
555
556         ORG 0xf859
557         IRQ_ENTRY_ARG 15
558
559         // 0xfa6e - vgafont8 in font.c
560
561         ORG 0xfe6e
562         IRQ_ENTRY_ARG 1a
563
564         ORG 0xfea5
565         IRQ_ENTRY 08
566
567         // 0xfef3 - InitVectors in misc.c
568
569         // 0xff00 - BiosCopyright in misc.c
570
571         ORG 0xff53
572         .global entry_iret_official
573 entry_iret_official:
574         iretw
575
576         ORG 0xff54
577         IRQ_ENTRY_ARG 05
578
579         ORG 0xfff0 // Power-up Entry Point
580         .global reset_vector
581 reset_vector:
582         ljmpw $SEG_BIOS, $entry_post_official
583
584         // 0xfff5 - BiosDate in misc.c
585
586         // 0xfffe - BiosModelId in misc.c
587
588         // 0xffff - BiosChecksum in misc.c
589
590         .end