Move assembler entry macros in romlayout.S into new file entryfuncs.S.
[seabios.git] / src / entryfuncs.S
1 // Macros for entering C code
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7
8 /****************************************************************
9  * Entry macros
10  ****************************************************************/
11
12         // Call a C function - this does the minimal work necessary to
13         // call into C.  It sets up %ds, backs up %es, and backs up
14         // those registers that are call clobbered by the C compiler.
15         .macro ENTRY cfunc
16         cli         // In case something far-calls instead of using "int"
17         cld
18         pushl %eax              // Save registers clobbered by C code
19         pushl %ecx
20         pushl %edx
21         pushw %es
22         pushw %ds
23         movw %ss, %ax           // Move %ss to %ds
24         movw %ax, %ds
25         pushl %esp              // Backup %esp, then clear high bits
26         movzwl %sp, %esp
27         calll \cfunc
28         popl %esp               // Restore %esp (including high bits)
29         popw %ds                // Restore registers saved above
30         popw %es
31         popl %edx
32         popl %ecx
33         popl %eax
34         .endm
35
36         // Call a C function with current register list as an
37         // argument.  This backs up the registers and sets %eax
38         // to point to the backup.  On return, the registers are
39         // restored from the structure.
40         .macro ENTRY_ARG cfunc
41         cli
42         cld
43         pushl %eax              // Save registers (matches struct bregs)
44         pushl %ecx
45         pushl %edx
46         pushl %ebx
47         pushl %esi
48         pushl %edi
49         pushw %es
50         pushw %ds
51         movw %ss, %ax           // Move %ss to %ds
52         movw %ax, %ds
53         movl %esp, %ebx         // Backup %esp, then zero high bits
54         movzwl %sp, %esp
55         movl %esp, %eax         // First arg is pointer to struct bregs
56         calll \cfunc
57         movl %ebx, %esp         // Restore %esp (including high bits)
58         popw %ds                // Restore registers (from struct bregs)
59         popw %es
60         popl %edi
61         popl %esi
62         popl %ebx
63         popl %edx
64         popl %ecx
65         popl %eax
66         .endm
67
68         // As above, but don't mangle %esp
69         .macro ENTRY_ARG_ESP cfunc
70         cli
71         cld
72         pushl %eax              // Save registers (matches struct bregs)
73         pushl %ecx
74         pushl %edx
75         pushl %ebx
76         pushl %esi
77         pushl %edi
78         pushw %es
79         pushw %ds
80         movw %ss, %ax           // Move %ss to %ds
81         movw %ax, %ds
82         movl %esp, %eax         // First arg is pointer to struct bregs
83         calll \cfunc
84         popw %ds                // Restore registers (from struct bregs)
85         popw %es
86         popl %edi
87         popl %esi
88         popl %ebx
89         popl %edx
90         popl %ecx
91         popl %eax
92         .endm
93
94         // Reset stack, transition to 32bit mode, and call a C function.
95         // Clobbers %ax
96         .macro ENTRY_INTO32 cfunc
97         xorw %ax, %ax
98         movw %ax, %ss
99         movl $ BUILD_STACK_ADDR , %esp
100         pushl $ \cfunc
101         jmp transition32
102         .endm
103
104         // Declare a function
105         .macro DECLFUNC func
106         .section .text.asm.\func
107         .global \func
108         .endm
109
110         // Define an entry point for an interrupt (no args passed).
111         .macro IRQ_ENTRY num
112         .global entry_\num
113         entry_\num :
114         ENTRY handle_\num
115         iretw
116         .endm
117
118         // Define an entry point for an interrupt (can read/modify args).
119         .macro IRQ_ENTRY_ARG num
120         .global entry_\num
121         entry_\num :
122         ENTRY_ARG handle_\num
123         iretw
124         .endm
125
126         // Macros that put each handler into its own section
127         .macro DECL_IRQ_ENTRY num
128         .section .text.asm.entry_\num
129         IRQ_ENTRY \num
130         .endm
131         .macro DECL_IRQ_ENTRY_ARG num
132         .section .text.asm.entry_\num
133         IRQ_ENTRY_ARG \num
134         .endm