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