blob: ea6f9900bf0b1c1f6940fcd4743e3af8778a0bd8 [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
David Woodhouse02e94c12013-01-19 17:34:28 +000012 .macro PUSHBREGS
13 pushl %eax // Save registers (matches struct bregs)
14 pushl %ecx
15 pushl %edx
16 pushl %ebx
17 pushl %ebp
18 pushl %esi
19 pushl %edi
20 pushw %es
21 pushw %ds
22 .endm
23
24 .macro POPBREGS
25 popw %ds // Restore registers (from struct bregs)
26 popw %es
27 popl %edi
28 popl %esi
29 popl %ebp
30 popl %ebx
31 popl %edx
32 popl %ecx
33 popl %eax
34 .endm
35
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040036 // Call a C function - this does the minimal work necessary to
37 // call into C. It sets up %ds, backs up %es, and backs up
38 // those registers that are call clobbered by the C compiler.
39 .macro ENTRY cfunc
40 cli // In case something far-calls instead of using "int"
41 cld
42 pushl %eax // Save registers clobbered by C code
43 pushl %ecx
44 pushl %edx
45 pushw %es
46 pushw %ds
47 movw %ss, %ax // Move %ss to %ds
48 movw %ax, %ds
49 pushl %esp // Backup %esp, then clear high bits
50 movzwl %sp, %esp
51 calll \cfunc
52 popl %esp // Restore %esp (including high bits)
53 popw %ds // Restore registers saved above
54 popw %es
55 popl %edx
56 popl %ecx
57 popl %eax
58 .endm
59
Kevin O'Connor9f193b92009-05-16 23:31:27 -040060 // As above, but get calling function from stack.
61 .macro ENTRY_ST
62 cli
63 cld
64 pushl %ecx
65 pushl %edx
66 pushw %es
67 pushw %ds
68 movw %ss, %cx // Move %ss to %ds
69 movw %cx, %ds
70 pushl %esp // Backup %esp, then clear high bits
71 movzwl %sp, %esp
72 movl 16(%esp), %ecx // Get calling function
73 movl %eax, 16(%esp) // Save %eax
74 calll *%ecx
75 popl %esp // Restore %esp (including high bits)
76 popw %ds // Restore registers saved above
77 popw %es
78 popl %edx
79 popl %ecx
80 popl %eax
81 .endm
82
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040083 // Call a C function with current register list as an
84 // argument. This backs up the registers and sets %eax
85 // to point to the backup. On return, the registers are
86 // restored from the structure.
87 .macro ENTRY_ARG cfunc
88 cli
89 cld
David Woodhouse02e94c12013-01-19 17:34:28 +000090 PUSHBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040091 movw %ss, %ax // Move %ss to %ds
92 movw %ax, %ds
93 movl %esp, %ebx // Backup %esp, then zero high bits
94 movzwl %sp, %esp
95 movl %esp, %eax // First arg is pointer to struct bregs
96 calll \cfunc
97 movl %ebx, %esp // Restore %esp (including high bits)
David Woodhouse02e94c12013-01-19 17:34:28 +000098 POPBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040099 .endm
100
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400101 // As above, but get calling function from stack.
102 .macro ENTRY_ARG_ST
103 cli
104 cld
105 pushl %ecx
106 pushl %edx
107 pushl %ebx
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400108 pushl %ebp
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400109 pushl %esi
110 pushl %edi
111 pushw %es
112 pushw %ds
113 movw %ss, %cx // Move %ss to %ds
114 movw %cx, %ds
115 movl %esp, %ebx // Backup %esp, then zero high bits
116 movzwl %sp, %esp
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400117 movl 28(%esp), %ecx // Get calling function
118 movl %eax, 28(%esp) // Save %eax
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400119 movl %esp, %eax // First arg is pointer to struct bregs
120 calll *%ecx
121 movl %ebx, %esp // Restore %esp (including high bits)
David Woodhouse02e94c12013-01-19 17:34:28 +0000122 POPBREGS
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400123 .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
David Woodhouse02e94c12013-01-19 17:34:28 +0000129 PUSHBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400130 movw %ss, %ax // Move %ss to %ds
131 movw %ax, %ds
132 movl %esp, %eax // First arg is pointer to struct bregs
133 calll \cfunc
David Woodhouse02e94c12013-01-19 17:34:28 +0000134 POPBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400135 .endm
136
137 // Reset stack, transition to 32bit mode, and call a C function.
138 // Clobbers %ax
139 .macro ENTRY_INTO32 cfunc
140 xorw %ax, %ax
141 movw %ax, %ss
142 movl $ BUILD_STACK_ADDR , %esp
Kevin O'Connor4057f982010-11-25 08:52:50 -0500143 movl $ \cfunc , %edx
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400144 jmp transition32
145 .endm
146
147 // Declare a function
148 .macro DECLFUNC func
149 .section .text.asm.\func
150 .global \func
151 .endm
Kevin O'Connorc0693942009-06-10 21:56:01 -0400152
153 // Declare an exported function
154 .macro EXPORTFUNC func
155 .section .text.asm.export.\func
156 .global \func
157 .endm