blob: cc1f433245954b348a451ffc657461228a233d67 [file] [log] [blame]
Kevin O'Connorf8e176c2009-05-06 23:33:32 -04001// Macros for entering C code
2//
Kevin O'Connor46000f52014-10-21 02:23:02 -04003// Copyright (C) 2008-2014 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf8e176c2009-05-06 23:33:32 -04004//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7
8/****************************************************************
Kevin O'Connor46000f52014-10-21 02:23:02 -04009 * Macros for save and restore of 'struct bregs' registers
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040010 ****************************************************************/
11
Kevin O'Connor46000f52014-10-21 02:23:02 -040012 // Save registers (matches struct bregs) to stack
David Woodhouse02e94c12013-01-19 17:34:28 +000013 .macro PUSHBREGS
Kevin O'Connor46000f52014-10-21 02:23:02 -040014 pushl %eax
David Woodhouse02e94c12013-01-19 17:34:28 +000015 pushl %ecx
16 pushl %edx
17 pushl %ebx
18 pushl %ebp
19 pushl %esi
20 pushl %edi
21 pushw %es
22 pushw %ds
23 .endm
24
Kevin O'Connor46000f52014-10-21 02:23:02 -040025 // Restore registers (from struct bregs) from stack
David Woodhouse02e94c12013-01-19 17:34:28 +000026 .macro POPBREGS
Kevin O'Connor46000f52014-10-21 02:23:02 -040027 popw %ds
David Woodhouse02e94c12013-01-19 17:34:28 +000028 popw %es
29 popl %edi
30 popl %esi
31 popl %ebp
32 popl %ebx
33 popl %edx
34 popl %ecx
35 popl %eax
36 .endm
37
Kevin O'Connor46000f52014-10-21 02:23:02 -040038 // Save registers to struct bregs at %ds:%eax. The caller
39 // should "pushw %ds ; pushl %eax" prior to calling - this macro
40 // will pop them off.
41 .macro SAVEBREGS_POP_DSEAX
42 popl BREGS_eax(%eax)
43 popw BREGS_ds(%eax)
44 movl %edi, BREGS_edi(%eax)
45 movl %esi, BREGS_esi(%eax)
46 movl %ebp, BREGS_ebp(%eax)
47 movl %ebx, BREGS_ebx(%eax)
48 movl %edx, BREGS_edx(%eax)
49 movl %ecx, BREGS_ecx(%eax)
50 movw %es, BREGS_es(%eax)
51 .endm
52
53 // Restore registers from struct bregs at %ds:%eax
54 .macro RESTOREBREGS_DSEAX
55 movl BREGS_edi(%eax), %edi
56 movl BREGS_esi(%eax), %esi
57 movl BREGS_ebp(%eax), %ebp
58 movl BREGS_ebx(%eax), %ebx
59 movl BREGS_edx(%eax), %edx
60 movl BREGS_ecx(%eax), %ecx
61 movw BREGS_es(%eax), %es
62 pushl BREGS_eax(%eax)
63 movw BREGS_ds(%eax), %ds
64 popl %eax
65 .endm
66
67
68/****************************************************************
69 * Entry macros
70 ****************************************************************/
71
Kevin O'Connorf8e176c2009-05-06 23:33:32 -040072 // Call a C function - this does the minimal work necessary to
73 // call into C. It sets up %ds, backs up %es, and backs up
74 // those registers that are call clobbered by the C compiler.
75 .macro ENTRY cfunc
76 cli // In case something far-calls instead of using "int"
77 cld
78 pushl %eax // Save registers clobbered by C code
79 pushl %ecx
80 pushl %edx
81 pushw %es
82 pushw %ds
83 movw %ss, %ax // Move %ss to %ds
84 movw %ax, %ds
85 pushl %esp // Backup %esp, then clear high bits
86 movzwl %sp, %esp
87 calll \cfunc
88 popl %esp // Restore %esp (including high bits)
89 popw %ds // Restore registers saved above
90 popw %es
91 popl %edx
92 popl %ecx
93 popl %eax
94 .endm
95
96 // Call a C function with current register list as an
97 // argument. This backs up the registers and sets %eax
98 // to point to the backup. On return, the registers are
99 // restored from the structure.
100 .macro ENTRY_ARG cfunc
101 cli
102 cld
David Woodhouse02e94c12013-01-19 17:34:28 +0000103 PUSHBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400104 movw %ss, %ax // Move %ss to %ds
105 movw %ax, %ds
106 movl %esp, %ebx // Backup %esp, then zero high bits
107 movzwl %sp, %esp
108 movl %esp, %eax // First arg is pointer to struct bregs
109 calll \cfunc
110 movl %ebx, %esp // Restore %esp (including high bits)
David Woodhouse02e94c12013-01-19 17:34:28 +0000111 POPBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400112 .endm
113
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400114 // As above, but get calling function from stack.
115 .macro ENTRY_ARG_ST
116 cli
117 cld
118 pushl %ecx
119 pushl %edx
120 pushl %ebx
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400121 pushl %ebp
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400122 pushl %esi
123 pushl %edi
124 pushw %es
125 pushw %ds
126 movw %ss, %cx // Move %ss to %ds
127 movw %cx, %ds
128 movl %esp, %ebx // Backup %esp, then zero high bits
129 movzwl %sp, %esp
Kevin O'Connor7da210c2009-05-16 23:57:08 -0400130 movl 28(%esp), %ecx // Get calling function
131 movl %eax, 28(%esp) // Save %eax
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400132 movl %esp, %eax // First arg is pointer to struct bregs
133 calll *%ecx
134 movl %ebx, %esp // Restore %esp (including high bits)
David Woodhouse02e94c12013-01-19 17:34:28 +0000135 POPBREGS
Kevin O'Connor9f193b92009-05-16 23:31:27 -0400136 .endm
137
138 // Same as ENTRY_ARG, but don't mangle %esp
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400139 .macro ENTRY_ARG_ESP cfunc
140 cli
141 cld
David Woodhouse02e94c12013-01-19 17:34:28 +0000142 PUSHBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400143 movw %ss, %ax // Move %ss to %ds
144 movw %ax, %ds
145 movl %esp, %eax // First arg is pointer to struct bregs
146 calll \cfunc
David Woodhouse02e94c12013-01-19 17:34:28 +0000147 POPBREGS
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400148 .endm
149
150 // Reset stack, transition to 32bit mode, and call a C function.
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400151 .macro ENTRY_INTO32 cfunc
Kevin O'Connor283ae1f2014-10-21 03:19:55 -0400152 xorw %dx, %dx
153 movw %dx, %ss
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400154 movl $ BUILD_STACK_ADDR , %esp
Kevin O'Connor4057f982010-11-25 08:52:50 -0500155 movl $ \cfunc , %edx
Kevin O'Connorf8e176c2009-05-06 23:33:32 -0400156 jmp transition32
157 .endm
158
159 // Declare a function
160 .macro DECLFUNC func
161 .section .text.asm.\func
162 .global \func
163 .endm