Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | /* * Copyright (c) 2019 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ .code64 .globl _compat_mode_trampoline _compat_mode_trampoline: /* * %rdi => address of far_call_t (64-bit offset, then 16-bit selector) * %rsi => lowmem stack * %rdx => argument to 32-bit function * %rcx => address of long mode callback * %r8 => 64-bit address of _thunk64 */ movq %rsp, %rax movq %rsi, %rsp pushq %rax /* Save 64-bit stack pointer */ leaq 1f(%rip), %rax movq %rdx, %r9 xorq %rdx, %rdx movw %cs, %dx shlq $32, %rdx orq %rdx, %rax movq %r9, %rdx /* * Save all callee-saved regs before calling down to compat mode, * as there's no guarantee that the top 32 bits are preserved * across compat mode/long mode switches. */ pushq %rbp pushq %rbx pushq %r12 pushq %r13 pushq %r14 pushq %r15 pushq %r8 /* Push the absolute address of _thunk64 below */ pushq %rcx /* Push the 64-bit fn ptr that compat mode will call */ pushq %rdx /* Push arg to 32-bit code */ pushq %rax /* Push the return offset + segment onto the stack */ ljmpq *(%rdi) 1: /* * lretl from compat mode pops off the first 8 bytes, * so manually reclaim the remaining 24 bytes */ addq $0x18, %rsp /* Restore callee-saved registers */ popq %r15 popq %r14 popq %r13 popq %r12 popq %rbx popq %rbp popq %rsp retq .code32 .globl _code_32 .align 12 _code_32: /* * After the standard stack frame is established, the stack layout is as follows: * * (%esp) -> old %ebp * 4(%ebp) -> return %eip * 8(%ebp) -> return %cs * 0xc(%ebp) -> function arg (value to increment and return) * 0x14(%ebp) -> 8-byte long mode function pointer to call via trampoline (with 0 args) * 0x1c(%ebp) -> absolute (32-bit) base address of the 64-bit thunk * (Note that the caller pushed a 64-bit value here, so the 4 bytes * at 0x20(%ebp) are zeroes.) */ pushl %ebp movl %esp, %ebp pushl %ebx call 1f 1: popl %ebx /* save EIP for use in PIC calculation below */ subl $8, %esp movl 0x1c(%ebp), %eax /* Populate the far call descriptor: */ movl %eax, -8(%ebp) movl 8(%ebp), %eax /* The long-mode %cs from whence we came */ movl %eax, -4(%ebp) pushl $0 /* number of arguments */ pushl 0x18(%ebp) /* high 32-bits of long mode funcptr */ pushl 0x14(%ebp) /* low 32-bits of long mode funcptr */ /* * The next 2 instructions are necessary because clang cannot deal with * a "leal offset(index_reg), dest_reg" construct despite the fact that * this code is marked .code32 (because the target is 64-bit and cannot * process this uniquely-32-bit construct.) */ leal 2f - 1b, %eax addl %ebx, %eax pushl $0 pushl %cs pushl $0 pushl %eax /* * Note that the long-mode-based function that is called will need * to restore GSbase before calling into any frameworks that might * access %gs-relative data. */ ljmpl *-8(%ebp) /* far call to the long mode trampoline */ 2: /* * lretq from long mode pops 16 bytes, so reclaim the remaining 12 */ addl $12, %esp /* * Do a division-by-zero so the exception handler can catch it and * restore execution right after. If a signal handler is used, * it must restore GSbase first if it intends to call into any * frameworks / APIs that access %gs-relative data. */ xorl %eax, %eax div %eax .globl _first_invalid_opcode _first_invalid_opcode: /* * Next, try to perform a sysenter syscall -- which should result in * a #UD. */ leal 3f - 1b, %edx addl %ebx, %edx /* return address is expected in %edx */ pushl %ecx movl %esp, %ecx /* stack ptr is expected in %ecx */ sysenter 3: popl %ecx /* * Do the same with each of the old-style INT syscalls. */ int $0x80 int $0x81 .globl _last_invalid_opcode _last_invalid_opcode: int $0x82 /* * discard the return value from the trampolined function and * increment the value passed in as this function's first argument * then return that value + 1 so caller can verify a successful * thunk. */ movl 0xc(%ebp), %eax incl %eax addl $8, %esp popl %ebx popl %ebp lret .code64 .globl _thunk64 _thunk64: /* * The thunk is a very simple code fragment that uses an * absolute address modified at setup time to call into * the long mode trampoline.far call data passed on the stack to jump to long mode * code (where %rip-relative addressing will work properly.) * */ .globl _thunk64_movabs _thunk64_movabs: movabs $0xdeadbeeffeedface, %rax jmpq *%rax .globl _compat_mode_trampoline_len _compat_mode_trampoline_len: .long (. - _compat_mode_trampoline) .globl _long_mode_trampoline _long_mode_trampoline: /* * After creating a standard stack frame, the stack layout is: * * 8(%rbp) => %eip of far return to compat mode * 0x10(%rbp) => %cs of far return to compat mode * 0x18(%rbp) => low 32-bits of function pointer * 0x1C(%rbp) => high 32-bits of function pointer * 0x20(%rbp) => number of parameters (0..4) * 0x24(%rbp) => first argument [low 32-bits] (if needed) * 0x28(%rbp) => first argument [high 32-bits] (if needed) * 0x2c(%rbp) => second argument [low 32-bits] (if needed) * 0x30(%rbp) => second argument [high 32-bits] (if needed) * 0x34(%rbp) => third argument [low 32-bits] (if needed) * 0x38(%rbp) => third argument [high 32-bits] (if needed) * 0x3c(%rbp) => fourth argument [low 32-bits] (if needed) * 0x40(%rbp) => fourth argument [high 32-bits] (if needed) * * Note that we continue to use the existing (<4G) stack * after the call into long mode. */ pushq %rbp movq %rsp, %rbp pushq %rdi pushq %rsi pushq %rcx movl 0x20(%rbp), %eax testl %eax, %eax jz 5f movq 0x24(%rbp), %rdi decl %eax 2: testl %eax, %eax jz 5f movq 0x2c(%rbp), %rsi decl %eax 3: testl %eax, %eax jz 5f movq 0x34(%rbp), %rdx decl %eax 4: testl %eax, %eax jnz 1f /* too many arguments specified -- bail out and return */ movq 0x3c(%rbp), %rcx 5: /* Call passed-in function */ /* Note that the stack MUST be 16-byte aligned before we call into frameworks in long mode */ pushq %rbx movq %rsp, %rbx subq $0x10, %rsp andq $0xffffffffffffffe0, %rsp callq *0x18(%rbp) movq %rbx, %rsp popq %rbx 1: popq %rcx popq %rsi popq %rdi popq %rbp lretq |