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