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