Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 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 | |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 12 | .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'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 36 | // 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'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 60 | // 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'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 83 | // 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 Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 90 | PUSHBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 91 | 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 Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 98 | POPBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 99 | .endm |
| 100 | |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 101 | // 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'Connor | 7da210c | 2009-05-16 23:57:08 -0400 | [diff] [blame] | 108 | pushl %ebp |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 109 | 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'Connor | 7da210c | 2009-05-16 23:57:08 -0400 | [diff] [blame] | 117 | movl 28(%esp), %ecx // Get calling function |
| 118 | movl %eax, 28(%esp) // Save %eax |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 119 | movl %esp, %eax // First arg is pointer to struct bregs |
| 120 | calll *%ecx |
| 121 | movl %ebx, %esp // Restore %esp (including high bits) |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 122 | POPBREGS |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 123 | .endm |
| 124 | |
| 125 | // Same as ENTRY_ARG, but don't mangle %esp |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 126 | .macro ENTRY_ARG_ESP cfunc |
| 127 | cli |
| 128 | cld |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 129 | PUSHBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 130 | 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 Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 134 | POPBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 135 | .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'Connor | 4057f98 | 2010-11-25 08:52:50 -0500 | [diff] [blame] | 143 | movl $ \cfunc , %edx |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 144 | 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'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 152 | |
| 153 | // Declare an exported function |
| 154 | .macro EXPORTFUNC func |
| 155 | .section .text.asm.export.\func |
| 156 | .global \func |
| 157 | .endm |