Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 1 | // Macros for entering C code |
| 2 | // |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008-2014 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 4 | // |
| 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 6 | |
| 7 | |
| 8 | /**************************************************************** |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 9 | * Macros for save and restore of 'struct bregs' registers |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 10 | ****************************************************************/ |
| 11 | |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 12 | // Save registers (matches struct bregs) to stack |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 13 | .macro PUSHBREGS |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 14 | pushl %eax |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 15 | 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'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 25 | // Restore registers (from struct bregs) from stack |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 26 | .macro POPBREGS |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 27 | popw %ds |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 28 | 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'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 38 | // 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'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 72 | // 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 Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 103 | PUSHBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 104 | 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 Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 111 | POPBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 112 | .endm |
| 113 | |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 114 | // 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'Connor | 7da210c | 2009-05-16 23:57:08 -0400 | [diff] [blame] | 121 | pushl %ebp |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 122 | 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'Connor | 7da210c | 2009-05-16 23:57:08 -0400 | [diff] [blame] | 130 | movl 28(%esp), %ecx // Get calling function |
| 131 | movl %eax, 28(%esp) // Save %eax |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 132 | movl %esp, %eax // First arg is pointer to struct bregs |
| 133 | calll *%ecx |
| 134 | movl %ebx, %esp // Restore %esp (including high bits) |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 135 | POPBREGS |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 136 | .endm |
| 137 | |
| 138 | // Same as ENTRY_ARG, but don't mangle %esp |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 139 | .macro ENTRY_ARG_ESP cfunc |
| 140 | cli |
| 141 | cld |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 142 | PUSHBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 143 | 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 Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 147 | POPBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 148 | .endm |
| 149 | |
| 150 | // Reset stack, transition to 32bit mode, and call a C function. |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 151 | .macro ENTRY_INTO32 cfunc |
Kevin O'Connor | 283ae1f | 2014-10-21 03:19:55 -0400 | [diff] [blame] | 152 | xorw %dx, %dx |
| 153 | movw %dx, %ss |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 154 | movl $ BUILD_STACK_ADDR , %esp |
Kevin O'Connor | 4057f98 | 2010-11-25 08:52:50 -0500 | [diff] [blame] | 155 | movl $ \cfunc , %edx |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 156 | jmp transition32 |
| 157 | .endm |
| 158 | |
| 159 | // Declare a function |
| 160 | .macro DECLFUNC func |
| 161 | .section .text.asm.\func |
| 162 | .global \func |
| 163 | .endm |