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