1 // Macros for entering C code
3 // Copyright (C) 2008-2014 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 /****************************************************************
9 * Macros for save and restore of 'struct bregs' registers
10 ****************************************************************/
12 #define PUSHBREGS_size 32
14 // Save registers (matches struct bregs) to stack
27 // Restore registers (from struct bregs) from stack
40 // Save registers to struct bregs at %ds:%eax. The caller
41 // should "pushw %ds ; pushl %eax" prior to calling - this macro
43 .macro SAVEBREGS_POP_DSEAX
46 movl %edi, BREGS_edi(%eax)
47 movl %esi, BREGS_esi(%eax)
48 movl %ebp, BREGS_ebp(%eax)
49 movl %ebx, BREGS_ebx(%eax)
50 movl %edx, BREGS_edx(%eax)
51 movl %ecx, BREGS_ecx(%eax)
52 movw %es, BREGS_es(%eax)
55 // Restore registers from struct bregs at %ds:%eax
56 .macro RESTOREBREGS_DSEAX
57 movl BREGS_edi(%eax), %edi
58 movl BREGS_esi(%eax), %esi
59 movl BREGS_ebp(%eax), %ebp
60 movl BREGS_ebx(%eax), %ebx
61 movl BREGS_edx(%eax), %edx
62 movl BREGS_ecx(%eax), %ecx
63 movw BREGS_es(%eax), %es
65 movw BREGS_ds(%eax), %ds
70 /****************************************************************
72 ****************************************************************/
74 // Call a C function - this does the minimal work necessary to
75 // call into C. It sets up %ds, backs up %es, and backs up
76 // those registers that are call clobbered by the C compiler.
78 cli // In case something far-calls instead of using "int"
80 pushl %eax // Save registers clobbered by C code
85 movw %ss, %ax // Move %ss to %ds
87 pushl %esp // Backup %esp, then clear high bits
90 popl %esp // Restore %esp (including high bits)
91 popw %ds // Restore registers saved above
98 // Call a C function with current register list as an
99 // argument. This backs up the registers and sets %eax
100 // to point to the backup. On return, the registers are
101 // restored from the structure.
102 .macro ENTRY_ARG cfunc
106 movw %ss, %ax // Move %ss to %ds
108 movl %esp, %ebx // Backup %esp, then zero high bits
110 movl %esp, %eax // First arg is pointer to struct bregs
112 movl %ebx, %esp // Restore %esp (including high bits)
116 // As above, but get calling function from stack.
128 movw %ss, %cx // Move %ss to %ds
130 movl %esp, %ebx // Backup %esp, then zero high bits
132 movl 28(%esp), %ecx // Get calling function
133 movl %eax, 28(%esp) // Save %eax
134 movl %esp, %eax // First arg is pointer to struct bregs
136 movl %ebx, %esp // Restore %esp (including high bits)
140 // Same as ENTRY_ARG, but don't mangle %esp
141 .macro ENTRY_ARG_ESP cfunc
145 movw %ss, %ax // Move %ss to %ds
147 movl %esp, %eax // First arg is pointer to struct bregs
152 // Reset stack, transition to 32bit mode, and call a C function.
153 .macro ENTRY_INTO32 cfunc
156 movl $ BUILD_STACK_ADDR , %esp
161 // Declare a function
163 .section .text.asm.\func