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 | aa66d65 | 2014-11-05 09:05:36 -0500 | [diff] [blame] | 12 | #define PUSHBREGS_size 32 |
| 13 | |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 14 | // Save registers (matches struct bregs) to stack |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 15 | .macro PUSHBREGS |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 16 | pushl %eax |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 17 | pushl %ecx |
| 18 | pushl %edx |
| 19 | pushl %ebx |
| 20 | pushl %ebp |
| 21 | pushl %esi |
| 22 | pushl %edi |
| 23 | pushw %es |
| 24 | pushw %ds |
| 25 | .endm |
| 26 | |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 27 | // Restore registers (from struct bregs) from stack |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 28 | .macro POPBREGS |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 29 | popw %ds |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 30 | popw %es |
| 31 | popl %edi |
| 32 | popl %esi |
| 33 | popl %ebp |
| 34 | popl %ebx |
| 35 | popl %edx |
| 36 | popl %ecx |
| 37 | popl %eax |
| 38 | .endm |
| 39 | |
Kevin O'Connor | 46000f5 | 2014-10-21 02:23:02 -0400 | [diff] [blame] | 40 | // Save registers to struct bregs at %ds:%eax. The caller |
| 41 | // should "pushw %ds ; pushl %eax" prior to calling - this macro |
| 42 | // will pop them off. |
| 43 | .macro SAVEBREGS_POP_DSEAX |
| 44 | popl BREGS_eax(%eax) |
| 45 | popw BREGS_ds(%eax) |
| 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) |
| 53 | .endm |
| 54 | |
| 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 |
| 64 | pushl BREGS_eax(%eax) |
| 65 | movw BREGS_ds(%eax), %ds |
| 66 | popl %eax |
| 67 | .endm |
| 68 | |
| 69 | |
| 70 | /**************************************************************** |
| 71 | * Entry macros |
| 72 | ****************************************************************/ |
| 73 | |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 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. |
| 77 | .macro ENTRY cfunc |
| 78 | cli // In case something far-calls instead of using "int" |
| 79 | cld |
| 80 | pushl %eax // Save registers clobbered by C code |
| 81 | pushl %ecx |
| 82 | pushl %edx |
| 83 | pushw %es |
| 84 | pushw %ds |
| 85 | movw %ss, %ax // Move %ss to %ds |
| 86 | movw %ax, %ds |
| 87 | pushl %esp // Backup %esp, then clear high bits |
| 88 | movzwl %sp, %esp |
| 89 | calll \cfunc |
| 90 | popl %esp // Restore %esp (including high bits) |
| 91 | popw %ds // Restore registers saved above |
| 92 | popw %es |
| 93 | popl %edx |
| 94 | popl %ecx |
| 95 | popl %eax |
| 96 | .endm |
| 97 | |
| 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 |
| 103 | cli |
| 104 | cld |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 105 | PUSHBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 106 | movw %ss, %ax // Move %ss to %ds |
| 107 | movw %ax, %ds |
| 108 | movl %esp, %ebx // Backup %esp, then zero high bits |
| 109 | movzwl %sp, %esp |
| 110 | movl %esp, %eax // First arg is pointer to struct bregs |
| 111 | calll \cfunc |
| 112 | movl %ebx, %esp // Restore %esp (including high bits) |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 113 | POPBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 114 | .endm |
| 115 | |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 116 | // As above, but get calling function from stack. |
| 117 | .macro ENTRY_ARG_ST |
| 118 | cli |
| 119 | cld |
| 120 | pushl %ecx |
| 121 | pushl %edx |
| 122 | pushl %ebx |
Kevin O'Connor | 7da210c | 2009-05-16 23:57:08 -0400 | [diff] [blame] | 123 | pushl %ebp |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 124 | pushl %esi |
| 125 | pushl %edi |
| 126 | pushw %es |
| 127 | pushw %ds |
| 128 | movw %ss, %cx // Move %ss to %ds |
| 129 | movw %cx, %ds |
| 130 | movl %esp, %ebx // Backup %esp, then zero high bits |
| 131 | movzwl %sp, %esp |
Kevin O'Connor | 7da210c | 2009-05-16 23:57:08 -0400 | [diff] [blame] | 132 | movl 28(%esp), %ecx // Get calling function |
| 133 | movl %eax, 28(%esp) // Save %eax |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 134 | movl %esp, %eax // First arg is pointer to struct bregs |
| 135 | calll *%ecx |
| 136 | movl %ebx, %esp // Restore %esp (including high bits) |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 137 | POPBREGS |
Kevin O'Connor | 9f193b9 | 2009-05-16 23:31:27 -0400 | [diff] [blame] | 138 | .endm |
| 139 | |
| 140 | // Same as ENTRY_ARG, but don't mangle %esp |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 141 | .macro ENTRY_ARG_ESP cfunc |
| 142 | cli |
| 143 | cld |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 144 | PUSHBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 145 | movw %ss, %ax // Move %ss to %ds |
| 146 | movw %ax, %ds |
| 147 | movl %esp, %eax // First arg is pointer to struct bregs |
| 148 | calll \cfunc |
David Woodhouse | 02e94c1 | 2013-01-19 17:34:28 +0000 | [diff] [blame] | 149 | POPBREGS |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 150 | .endm |
| 151 | |
| 152 | // Reset stack, transition to 32bit mode, and call a C function. |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 153 | .macro ENTRY_INTO32 cfunc |
Kevin O'Connor | 283ae1f | 2014-10-21 03:19:55 -0400 | [diff] [blame] | 154 | xorw %dx, %dx |
| 155 | movw %dx, %ss |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 156 | movl $ BUILD_STACK_ADDR , %esp |
Kevin O'Connor | 4057f98 | 2010-11-25 08:52:50 -0500 | [diff] [blame] | 157 | movl $ \cfunc , %edx |
Kevin O'Connor | f8e176c | 2009-05-06 23:33:32 -0400 | [diff] [blame] | 158 | jmp transition32 |
| 159 | .endm |
| 160 | |
| 161 | // Declare a function |
| 162 | .macro DECLFUNC func |
| 163 | .section .text.asm.\func |
| 164 | .global \func |
| 165 | .endm |