blob: afc5e611944afb4ad9d3eb01800acbe59635b424 [file] [log] [blame]
Kevin O'Connorf8e176c2009-05-06 23:33:32 -04001// 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
Kevin O'Connor9f193b92009-05-16 23:31:27 -040036 // As above, but get calling function from stack.
37 .macro ENTRY_ST
38 cli
39 cld
40 pushl %ecx
41 pushl %edx
42 pushw %es
43 pushw %ds
44 movw %ss, %cx // Move %ss to %ds
45 movw %cx, %ds
46 pushl %esp // Backup %esp, then clear high bits
47 movzwl %sp, %esp
48 movl 16(%esp), %ecx // Get calling function
49 movl %eax, 16(%esp) // Save %eax
50 calll *%ecx
51 popl %esp // Restore %esp (including high bits)
52 popw %ds // Restore registers saved above
53 popw %es
54 popl %edx
55 popl %ecx
56 popl %eax
57 .endm
58
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040059 // Call a C function with current register list as an
60 // argument. This backs up the registers and sets %eax
61 // to point to the backup. On return, the registers are
62 // restored from the structure.
63 .macro ENTRY_ARG cfunc
64 cli
65 cld
66 pushl %eax // Save registers (matches struct bregs)
67 pushl %ecx
68 pushl %edx
69 pushl %ebx
Kevin O'Connor7da210c2009-05-16 23:57:08 -040070 pushl %ebp
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040071 pushl %esi
72 pushl %edi
73 pushw %es
74 pushw %ds
75 movw %ss, %ax // Move %ss to %ds
76 movw %ax, %ds
77 movl %esp, %ebx // Backup %esp, then zero high bits
78 movzwl %sp, %esp
79 movl %esp, %eax // First arg is pointer to struct bregs
80 calll \cfunc
81 movl %ebx, %esp // Restore %esp (including high bits)
82 popw %ds // Restore registers (from struct bregs)
83 popw %es
84 popl %edi
85 popl %esi
Kevin O'Connor7da210c2009-05-16 23:57:08 -040086 popl %ebp
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040087 popl %ebx
88 popl %edx
89 popl %ecx
90 popl %eax
91 .endm
92
Kevin O'Connor9f193b92009-05-16 23:31:27 -040093 // As above, but get calling function from stack.
94 .macro ENTRY_ARG_ST
95 cli
96 cld
97 pushl %ecx
98 pushl %edx
99 pushl %ebx
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400100 pushl %ebp
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400101 pushl %esi
102 pushl %edi
103 pushw %es
104 pushw %ds
105 movw %ss, %cx // Move %ss to %ds
106 movw %cx, %ds
107 movl %esp, %ebx // Backup %esp, then zero high bits
108 movzwl %sp, %esp
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400109 movl 28(%esp), %ecx // Get calling function
110 movl %eax, 28(%esp) // Save %eax
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400111 movl %esp, %eax // First arg is pointer to struct bregs
112 calll *%ecx
113 movl %ebx, %esp // Restore %esp (including high bits)
114 popw %ds // Restore registers (from struct bregs)
115 popw %es
116 popl %edi
117 popl %esi
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400118 popl %ebp
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400119 popl %ebx
120 popl %edx
121 popl %ecx
122 popl %eax
123 .endm
124
125 // Same as ENTRY_ARG, but don't mangle %esp
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400126 .macro ENTRY_ARG_ESP cfunc
127 cli
128 cld
129 pushl %eax // Save registers (matches struct bregs)
130 pushl %ecx
131 pushl %edx
132 pushl %ebx
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400133 pushl %ebp
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400134 pushl %esi
135 pushl %edi
136 pushw %es
137 pushw %ds
138 movw %ss, %ax // Move %ss to %ds
139 movw %ax, %ds
140 movl %esp, %eax // First arg is pointer to struct bregs
141 calll \cfunc
142 popw %ds // Restore registers (from struct bregs)
143 popw %es
144 popl %edi
145 popl %esi
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400146 popl %ebp
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400147 popl %ebx
148 popl %edx
149 popl %ecx
150 popl %eax
151 .endm
152
153 // Reset stack, transition to 32bit mode, and call a C function.
154 // Clobbers %ax
155 .macro ENTRY_INTO32 cfunc
156 xorw %ax, %ax
157 movw %ax, %ss
158 movl $ BUILD_STACK_ADDR , %esp
Kevin O'Connor4057f982010-11-25 08:52:50 -0500159 movl $ \cfunc , %edx
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400160 jmp transition32
161 .endm
162
163 // Declare a function
164 .macro DECLFUNC func
165 .section .text.asm.\func
166 .global \func
167 .endm
Kevin O'Connorc0693942009-06-10 21:56:01 -0400168
169 // Declare an exported function
170 .macro EXPORTFUNC func
171 .section .text.asm.export.\func
172 .global \func
173 .endm