Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // Implement shared vtbl methods. |
michael@0 | 8 | |
michael@0 | 9 | // Keep this in sync with the linux version. |
michael@0 | 10 | |
michael@0 | 11 | #include "xptcprivate.h" |
michael@0 | 12 | #include "xptiprivate.h" |
michael@0 | 13 | |
michael@0 | 14 | // The Darwin/x86-64 ABI passes the first 6 integer parameters and the |
michael@0 | 15 | // first 8 floating point parameters in registers (rdi, rsi, rdx, rcx, |
michael@0 | 16 | // r8, r9 and xmm0-xmm7), no stack space is allocated for these by the |
michael@0 | 17 | // caller. The rest of the parameters are passed in the callers stack |
michael@0 | 18 | // area. |
michael@0 | 19 | |
michael@0 | 20 | const uint32_t PARAM_BUFFER_COUNT = 16; |
michael@0 | 21 | const uint32_t GPR_COUNT = 6; |
michael@0 | 22 | const uint32_t FPR_COUNT = 8; |
michael@0 | 23 | |
michael@0 | 24 | // PrepareAndDispatch() is called by SharedStub() and calls the actual method. |
michael@0 | 25 | // |
michael@0 | 26 | // - 'args[]' contains the arguments passed on stack |
michael@0 | 27 | // - 'gpregs[]' contains the arguments passed in integer registers |
michael@0 | 28 | // - 'fpregs[]' contains the arguments passed in floating point registers |
michael@0 | 29 | // |
michael@0 | 30 | // The parameters are mapped into an array of type 'nsXPTCMiniVariant' |
michael@0 | 31 | // and then the method gets called. |
michael@0 | 32 | |
michael@0 | 33 | extern "C" nsresult ATTRIBUTE_USED |
michael@0 | 34 | PrepareAndDispatch(nsXPTCStubBase * self, uint32_t methodIndex, |
michael@0 | 35 | uint64_t * args, uint64_t * gpregs, double *fpregs) |
michael@0 | 36 | { |
michael@0 | 37 | nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT]; |
michael@0 | 38 | nsXPTCMiniVariant* dispatchParams = nullptr; |
michael@0 | 39 | const nsXPTMethodInfo* info; |
michael@0 | 40 | uint32_t paramCount; |
michael@0 | 41 | uint32_t i; |
michael@0 | 42 | nsresult result = NS_ERROR_FAILURE; |
michael@0 | 43 | |
michael@0 | 44 | NS_ASSERTION(self,"no self"); |
michael@0 | 45 | |
michael@0 | 46 | self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info); |
michael@0 | 47 | NS_ASSERTION(info,"no method info"); |
michael@0 | 48 | if (!info) |
michael@0 | 49 | return NS_ERROR_UNEXPECTED; |
michael@0 | 50 | |
michael@0 | 51 | paramCount = info->GetParamCount(); |
michael@0 | 52 | |
michael@0 | 53 | // setup variant array pointer |
michael@0 | 54 | if (paramCount > PARAM_BUFFER_COUNT) |
michael@0 | 55 | dispatchParams = new nsXPTCMiniVariant[paramCount]; |
michael@0 | 56 | else |
michael@0 | 57 | dispatchParams = paramBuffer; |
michael@0 | 58 | |
michael@0 | 59 | NS_ASSERTION(dispatchParams,"no place for params"); |
michael@0 | 60 | if (!dispatchParams) |
michael@0 | 61 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 62 | |
michael@0 | 63 | uint64_t* ap = args; |
michael@0 | 64 | uint32_t nr_gpr = 1; // skip one GPR register for 'that' |
michael@0 | 65 | uint32_t nr_fpr = 0; |
michael@0 | 66 | uint64_t value; |
michael@0 | 67 | |
michael@0 | 68 | for (i = 0; i < paramCount; i++) { |
michael@0 | 69 | const nsXPTParamInfo& param = info->GetParam(i); |
michael@0 | 70 | const nsXPTType& type = param.GetType(); |
michael@0 | 71 | nsXPTCMiniVariant* dp = &dispatchParams[i]; |
michael@0 | 72 | |
michael@0 | 73 | if (!param.IsOut() && type == nsXPTType::T_DOUBLE) { |
michael@0 | 74 | if (nr_fpr < FPR_COUNT) |
michael@0 | 75 | dp->val.d = fpregs[nr_fpr++]; |
michael@0 | 76 | else |
michael@0 | 77 | dp->val.d = *(double*) ap++; |
michael@0 | 78 | continue; |
michael@0 | 79 | } |
michael@0 | 80 | else if (!param.IsOut() && type == nsXPTType::T_FLOAT) { |
michael@0 | 81 | if (nr_fpr < FPR_COUNT) |
michael@0 | 82 | // The value in %xmm register is already prepared to |
michael@0 | 83 | // be retrieved as a float. Therefore, we pass the |
michael@0 | 84 | // value verbatim, as a double without conversion. |
michael@0 | 85 | dp->val.d = fpregs[nr_fpr++]; |
michael@0 | 86 | else |
michael@0 | 87 | dp->val.f = *(float*) ap++; |
michael@0 | 88 | continue; |
michael@0 | 89 | } |
michael@0 | 90 | else { |
michael@0 | 91 | if (nr_gpr < GPR_COUNT) |
michael@0 | 92 | value = gpregs[nr_gpr++]; |
michael@0 | 93 | else |
michael@0 | 94 | value = *ap++; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (param.IsOut() || !type.IsArithmetic()) { |
michael@0 | 98 | dp->val.p = (void*) value; |
michael@0 | 99 | continue; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | switch (type) { |
michael@0 | 103 | case nsXPTType::T_I8: dp->val.i8 = (int8_t) value; break; |
michael@0 | 104 | case nsXPTType::T_I16: dp->val.i16 = (int16_t) value; break; |
michael@0 | 105 | case nsXPTType::T_I32: dp->val.i32 = (int32_t) value; break; |
michael@0 | 106 | case nsXPTType::T_I64: dp->val.i64 = (int64_t) value; break; |
michael@0 | 107 | case nsXPTType::T_U8: dp->val.u8 = (uint8_t) value; break; |
michael@0 | 108 | case nsXPTType::T_U16: dp->val.u16 = (uint16_t) value; break; |
michael@0 | 109 | case nsXPTType::T_U32: dp->val.u32 = (uint32_t) value; break; |
michael@0 | 110 | case nsXPTType::T_U64: dp->val.u64 = (uint64_t) value; break; |
michael@0 | 111 | // Cast to uint8_t first, to remove garbage on upper 56 bits. |
michael@0 | 112 | case nsXPTType::T_BOOL: dp->val.b = (bool)(uint8_t) value; break; |
michael@0 | 113 | case nsXPTType::T_CHAR: dp->val.c = (char) value; break; |
michael@0 | 114 | case nsXPTType::T_WCHAR: dp->val.wc = (wchar_t) value; break; |
michael@0 | 115 | |
michael@0 | 116 | default: |
michael@0 | 117 | NS_ERROR("bad type"); |
michael@0 | 118 | break; |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | result = self->mOuter->CallMethod((uint16_t) methodIndex, info, dispatchParams); |
michael@0 | 123 | |
michael@0 | 124 | if (dispatchParams != paramBuffer) |
michael@0 | 125 | delete [] dispatchParams; |
michael@0 | 126 | |
michael@0 | 127 | return result; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | // Darwin/x86-64 uses gcc >= 4.2 |
michael@0 | 131 | |
michael@0 | 132 | #define STUB_ENTRY(n) \ |
michael@0 | 133 | asm(".section __TEXT,__text\n\t" \ |
michael@0 | 134 | ".align 3\n\t" \ |
michael@0 | 135 | ".if " #n " < 10\n\t" \ |
michael@0 | 136 | ".globl __ZN14nsXPTCStubBase5Stub" #n "Ev\n\t" \ |
michael@0 | 137 | "__ZN14nsXPTCStubBase5Stub" #n "Ev:\n\t" \ |
michael@0 | 138 | ".elseif " #n " < 100\n\t" \ |
michael@0 | 139 | ".globl __ZN14nsXPTCStubBase6Stub" #n "Ev\n\t" \ |
michael@0 | 140 | "__ZN14nsXPTCStubBase6Stub" #n "Ev:\n\t" \ |
michael@0 | 141 | ".elseif " #n " < 1000\n\t" \ |
michael@0 | 142 | ".globl __ZN14nsXPTCStubBase7Stub" #n "Ev\n\t" \ |
michael@0 | 143 | "__ZN14nsXPTCStubBase7Stub" #n "Ev:\n\t" \ |
michael@0 | 144 | ".else\n\t" \ |
michael@0 | 145 | ".err \"stub number " #n " >= 1000 not yet supported\"\n\t" \ |
michael@0 | 146 | ".endif\n\t" \ |
michael@0 | 147 | "movl $" #n ", %eax\n\t" \ |
michael@0 | 148 | "jmp SharedStub\n\t"); |
michael@0 | 149 | |
michael@0 | 150 | // static nsresult SharedStub(uint32_t methodIndex) |
michael@0 | 151 | asm(".section __TEXT,__text\n\t" |
michael@0 | 152 | ".align 3\n\t" |
michael@0 | 153 | "SharedStub:\n\t" |
michael@0 | 154 | // make room for gpregs (48), fpregs (64) |
michael@0 | 155 | "pushq %rbp\n\t" |
michael@0 | 156 | "movq %rsp,%rbp\n\t" |
michael@0 | 157 | "subq $112,%rsp\n\t" |
michael@0 | 158 | // save GP registers |
michael@0 | 159 | "movq %rdi,-112(%rbp)\n\t" |
michael@0 | 160 | "movq %rsi,-104(%rbp)\n\t" |
michael@0 | 161 | "movq %rdx, -96(%rbp)\n\t" |
michael@0 | 162 | "movq %rcx, -88(%rbp)\n\t" |
michael@0 | 163 | "movq %r8 , -80(%rbp)\n\t" |
michael@0 | 164 | "movq %r9 , -72(%rbp)\n\t" |
michael@0 | 165 | "leaq -112(%rbp),%rcx\n\t" |
michael@0 | 166 | // save FP registers |
michael@0 | 167 | "movsd %xmm0,-64(%rbp)\n\t" |
michael@0 | 168 | "movsd %xmm1,-56(%rbp)\n\t" |
michael@0 | 169 | "movsd %xmm2,-48(%rbp)\n\t" |
michael@0 | 170 | "movsd %xmm3,-40(%rbp)\n\t" |
michael@0 | 171 | "movsd %xmm4,-32(%rbp)\n\t" |
michael@0 | 172 | "movsd %xmm5,-24(%rbp)\n\t" |
michael@0 | 173 | "movsd %xmm6,-16(%rbp)\n\t" |
michael@0 | 174 | "movsd %xmm7, -8(%rbp)\n\t" |
michael@0 | 175 | "leaq -64(%rbp),%r8\n\t" |
michael@0 | 176 | // rdi has the 'self' pointer already |
michael@0 | 177 | "movl %eax,%esi\n\t" |
michael@0 | 178 | "leaq 16(%rbp),%rdx\n\t" |
michael@0 | 179 | "call _PrepareAndDispatch\n\t" |
michael@0 | 180 | "leave\n\t" |
michael@0 | 181 | "ret\n\t"); |
michael@0 | 182 | |
michael@0 | 183 | #define SENTINEL_ENTRY(n) \ |
michael@0 | 184 | nsresult nsXPTCStubBase::Sentinel##n() \ |
michael@0 | 185 | { \ |
michael@0 | 186 | NS_ERROR("nsXPTCStubBase::Sentinel called"); \ |
michael@0 | 187 | return NS_ERROR_NOT_IMPLEMENTED; \ |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | #include "xptcstubsdef.inc" |