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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "jit/arm64/Assembler-arm64.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Maybe.h"
#include "gc/Marking.h"
#include "jit/arm64/Architecture-arm64.h"
#include "jit/arm64/MacroAssembler-arm64.h"
#include "jit/arm64/vixl/Disasm-vixl.h"
#include "jit/AutoWritableJitCode.h"
#include "jit/ExecutableAllocator.h"
#include "vm/Realm.h"
#include "gc/StoreBuffer-inl.h"
using namespace js;
using namespace js::jit;
using mozilla::CountLeadingZeroes32;
using mozilla::DebugOnly;
// Note this is used for inter-wasm calls and may pass arguments and results
// in floating point registers even if the system ABI does not.
ABIArg ABIArgGenerator::next(MIRType type) {
switch (type) {
case MIRType::Int32:
case MIRType::Int64:
case MIRType::Pointer:
case MIRType::WasmAnyRef:
case MIRType::StackResults:
if (intRegIndex_ == NumIntArgRegs) {
current_ = ABIArg(stackOffset_);
stackOffset_ += sizeof(uintptr_t);
break;
}
current_ = ABIArg(Register::FromCode(intRegIndex_));
intRegIndex_++;
break;
case MIRType::Float32:
case MIRType::Double:
if (floatRegIndex_ == NumFloatArgRegs) {
current_ = ABIArg(stackOffset_);
stackOffset_ += sizeof(double);
break;
}
current_ = ABIArg(FloatRegister(FloatRegisters::Encoding(floatRegIndex_),
type == MIRType::Double
? FloatRegisters::Double
: FloatRegisters::Single));
floatRegIndex_++;
break;
#ifdef ENABLE_WASM_SIMD
case MIRType::Simd128:
if (floatRegIndex_ == NumFloatArgRegs) {
stackOffset_ = AlignBytes(stackOffset_, SimdMemoryAlignment);
current_ = ABIArg(stackOffset_);
stackOffset_ += FloatRegister::SizeOfSimd128;
break;
}
current_ = ABIArg(FloatRegister(FloatRegisters::Encoding(floatRegIndex_),
FloatRegisters::Simd128));
floatRegIndex_++;
break;
#endif
default:
// Note that in Assembler-x64.cpp there's a special case for Win64 which
// does not allow passing SIMD by value. Since there's Win64 on ARM64 we
// may need to duplicate that logic here.
MOZ_CRASH("Unexpected argument type");
}
return current_;
}
namespace js {
namespace jit {
void Assembler::finish() {
armbuffer_.flushPool();
// The extended jump table is part of the code buffer.
ExtendedJumpTable_ = emitExtendedJumpTable();
Assembler::FinalizeCode();
}
bool Assembler::appendRawCode(const uint8_t* code, size_t numBytes) {
flush();
return armbuffer_.appendRawCode(code, numBytes);
}
bool Assembler::reserve(size_t size) {
// This buffer uses fixed-size chunks so there's no point in reserving
// now vs. on-demand.
return !oom();
}
bool Assembler::swapBuffer(wasm::Bytes& bytes) {
// For now, specialize to the one use case. As long as wasm::Bytes is a
// Vector, not a linked-list of chunks, there's not much we can do other
// than copy.
MOZ_ASSERT(bytes.empty());
if (!bytes.resize(bytesNeeded())) {
return false;
}
armbuffer_.executableCopy(bytes.begin());
return true;
}
BufferOffset Assembler::emitExtendedJumpTable() {
if (!pendingJumps_.length() || oom()) {
return BufferOffset();
}
armbuffer_.flushPool();
armbuffer_.align(SizeOfJumpTableEntry);
BufferOffset tableOffset = armbuffer_.nextOffset();
for (size_t i = 0; i < pendingJumps_.length(); i++) {
// Each JumpTableEntry is of the form:
// LDR ip0 [PC, 8]
// BR ip0
// [Patchable 8-byte constant low bits]
// [Patchable 8-byte constant high bits]
DebugOnly<size_t> preOffset = size_t(armbuffer_.nextOffset().getOffset());
// The unguarded use of ScratchReg64 here is OK:
//
// - The present function is called from code that does not claim any
// scratch registers, we're done compiling user code and are emitting jump
// tables. Hence the scratch registers are available when we enter.
//
// - The pendingJumps_ represent jumps to other code sections that are not
// known to this MacroAssembler instance, and we're generating code to
// jump there. It is safe to assume that any code using such a generated
// branch to an unknown location did not store any valuable value in any
// scratch register. Hence the scratch registers can definitely be
// clobbered here.
//
// - Scratch register usage is restricted to sequential control flow within
// MacroAssembler functions. Hence the scratch registers will not be
// clobbered by ldr and br as they are Assembler primitives, not
// MacroAssembler functions.
ldr(ScratchReg64, ptrdiff_t(8 / vixl::kInstructionSize));
br(ScratchReg64);
DebugOnly<size_t> prePointer = size_t(armbuffer_.nextOffset().getOffset());
MOZ_ASSERT_IF(!oom(),
prePointer - preOffset == OffsetOfJumpTableEntryPointer);
brk(0x0);
brk(0x0);
DebugOnly<size_t> postOffset = size_t(armbuffer_.nextOffset().getOffset());
MOZ_ASSERT_IF(!oom(), postOffset - preOffset == SizeOfJumpTableEntry);
}
if (oom()) {
return BufferOffset();
}
return tableOffset;
}
void Assembler::executableCopy(uint8_t* buffer) {
// Copy the code and all constant pools into the output buffer.
armbuffer_.executableCopy(buffer);
// Patch any relative jumps that target code outside the buffer.
// The extended jump table may be used for distant jumps.
for (size_t i = 0; i < pendingJumps_.length(); i++) {
RelativePatch& rp = pendingJumps_[i];
MOZ_ASSERT(rp.target);
Instruction* target = (Instruction*)rp.target;
Instruction* branch = (Instruction*)(buffer + rp.offset.getOffset());
JumpTableEntry* extendedJumpTable = reinterpret_cast<JumpTableEntry*>(
buffer + ExtendedJumpTable_.getOffset());
if (branch->BranchType() != vixl::UnknownBranchType) {
if (branch->IsTargetReachable(target)) {
branch->SetImmPCOffsetTarget(target);
} else {
JumpTableEntry* entry = &extendedJumpTable[i];
branch->SetImmPCOffsetTarget(entry->getLdr());
entry->data = target;
}
} else {
// Currently a two-instruction call, it should be possible to optimize
// this into a single instruction call + nop in some instances, but this
// will work.
}
}
}
BufferOffset Assembler::immPool(ARMRegister dest, uint8_t* value,
vixl::LoadLiteralOp op, const LiteralDoc& doc,
ARMBuffer::PoolEntry* pe) {
uint32_t inst = op | Rt(dest);
const size_t numInst = 1;
const unsigned sizeOfPoolEntryInBytes = 4;
const unsigned numPoolEntries = sizeof(value) / sizeOfPoolEntryInBytes;
return allocLiteralLoadEntry(numInst, numPoolEntries, (uint8_t*)&inst, value,
doc, pe);
}
BufferOffset Assembler::immPool64(ARMRegister dest, uint64_t value,
ARMBuffer::PoolEntry* pe) {
return immPool(dest, (uint8_t*)&value, vixl::LDR_x_lit, LiteralDoc(value),
pe);
}
BufferOffset Assembler::fImmPool(ARMFPRegister dest, uint8_t* value,
vixl::LoadLiteralOp op,
const LiteralDoc& doc) {
uint32_t inst = op | Rt(dest);
const size_t numInst = 1;
const unsigned sizeOfPoolEntryInBits = 32;
const unsigned numPoolEntries = dest.size() / sizeOfPoolEntryInBits;
return allocLiteralLoadEntry(numInst, numPoolEntries, (uint8_t*)&inst, value,
doc);
}
BufferOffset Assembler::fImmPool64(ARMFPRegister dest, double value) {
return fImmPool(dest, (uint8_t*)&value, vixl::LDR_d_lit, LiteralDoc(value));
}
BufferOffset Assembler::fImmPool32(ARMFPRegister dest, float value) {
return fImmPool(dest, (uint8_t*)&value, vixl::LDR_s_lit, LiteralDoc(value));
}
void Assembler::bind(Label* label, BufferOffset targetOffset) {
#ifdef JS_DISASM_ARM64
spew_.spewBind(label);
#endif
// Nothing has seen the label yet: just mark the location.
// If we've run out of memory, don't attempt to modify the buffer which may
// not be there. Just mark the label as bound to the (possibly bogus)
// targetOffset.
if (!label->used() || oom()) {
label->bind(targetOffset.getOffset());
return;
}
// Get the most recent instruction that used the label, as stored in the
// label. This instruction is the head of an implicit linked list of label
// uses.
BufferOffset branchOffset(label);
while (branchOffset.assigned()) {
// Before overwriting the offset in this instruction, get the offset of
// the next link in the implicit branch list.
BufferOffset nextOffset = NextLink(branchOffset);
// Linking against the actual (Instruction*) would be invalid,
// since that Instruction could be anywhere in memory.
// Instead, just link against the correct relative offset, assuming
// no constant pools, which will be taken into consideration
// during finalization.
ptrdiff_t relativeByteOffset =
targetOffset.getOffset() - branchOffset.getOffset();
Instruction* link = getInstructionAt(branchOffset);
// This branch may still be registered for callbacks. Stop tracking it.
vixl::ImmBranchType branchType = link->BranchType();
vixl::ImmBranchRangeType branchRange =
Instruction::ImmBranchTypeToRange(branchType);
if (branchRange < vixl::NumShortBranchRangeTypes) {
BufferOffset deadline(
branchOffset.getOffset() +
Instruction::ImmBranchMaxForwardOffset(branchRange));
armbuffer_.unregisterBranchDeadline(branchRange, deadline);
}
// Is link able to reach the label?
if (link->IsPCRelAddressing() ||
link->IsTargetReachable(link + relativeByteOffset)) {
// Write a new relative offset into the instruction.
link->SetImmPCOffsetTarget(link + relativeByteOffset);
} else {
// This is a short-range branch, and it can't reach the label directly.
// Verify that it branches to a veneer: an unconditional branch.
MOZ_ASSERT(getInstructionAt(nextOffset)->BranchType() ==
vixl::UncondBranchType);
}
branchOffset = nextOffset;
}
// Bind the label, so that future uses may encode the offset immediately.
label->bind(targetOffset.getOffset());
}
void Assembler::addPendingJump(BufferOffset src, ImmPtr target,
RelocationKind reloc) {
MOZ_ASSERT(target.value != nullptr);
if (reloc == RelocationKind::JITCODE) {
jumpRelocations_.writeUnsigned(src.getOffset());
}
// This jump is not patchable at runtime. Extended jump table entry
// requirements cannot be known until finalization, so to be safe, give each
// jump and entry. This also causes GC tracing of the target.
enoughMemory_ &=
pendingJumps_.append(RelativePatch(src, target.value, reloc));
}
void Assembler::PatchWrite_NearCall(CodeLocationLabel start,
CodeLocationLabel toCall) {
Instruction* dest = (Instruction*)start.raw();
ptrdiff_t relTarget = (Instruction*)toCall.raw() - dest;
ptrdiff_t relTarget00 = relTarget >> 2;
MOZ_RELEASE_ASSERT((relTarget & 0x3) == 0);
MOZ_RELEASE_ASSERT(vixl::IsInt26(relTarget00));
bl(dest, relTarget00);
}
void Assembler::PatchDataWithValueCheck(CodeLocationLabel label,
PatchedImmPtr newValue,
PatchedImmPtr expected) {
Instruction* i = (Instruction*)label.raw();
void** pValue = i->LiteralAddress<void**>();
MOZ_ASSERT(*pValue == expected.value);
*pValue = newValue.value;
}
void Assembler::PatchDataWithValueCheck(CodeLocationLabel label,
ImmPtr newValue, ImmPtr expected) {
PatchDataWithValueCheck(label, PatchedImmPtr(newValue.value),
PatchedImmPtr(expected.value));
}
void Assembler::ToggleToJmp(CodeLocationLabel inst_) {
Instruction* i = (Instruction*)inst_.raw();
MOZ_ASSERT(i->IsAddSubImmediate());
// Refer to instruction layout in ToggleToCmp().
int imm19 = (int)i->Bits(23, 5);
MOZ_ASSERT(vixl::IsInt19(imm19));
b(i, imm19, Always);
}
void Assembler::ToggleToCmp(CodeLocationLabel inst_) {
Instruction* i = (Instruction*)inst_.raw();
MOZ_ASSERT(i->IsCondB());
int imm19 = i->ImmCondBranch();
// bit 23 is reserved, and the simulator throws an assertion when this happens
// It'll be messy to decode, but we can steal bit 30 or bit 31.
MOZ_ASSERT(vixl::IsInt18(imm19));
// 31 - 64-bit if set, 32-bit if unset. (OK!)
// 30 - sub if set, add if unset. (OK!)
// 29 - SetFlagsBit. Must be set.
// 22:23 - ShiftAddSub. (OK!)
// 10:21 - ImmAddSub. (OK!)
// 5:9 - First source register (Rn). (OK!)
// 0:4 - Destination Register. Must be xzr.
// From the above, there is a safe 19-bit contiguous region from 5:23.
Emit(i, vixl::ThirtyTwoBits | vixl::AddSubImmediateFixed | vixl::SUB |
Flags(vixl::SetFlags) | Rd(vixl::xzr) |
(imm19 << vixl::Rn_offset));
}
void Assembler::ToggleCall(CodeLocationLabel inst_, bool enabled) {
const Instruction* first = reinterpret_cast<Instruction*>(inst_.raw());
Instruction* load;
Instruction* call;
// There might be a constant pool at the very first instruction.
first = first->skipPool();
// Skip the stack pointer restore instruction.
if (first->IsStackPtrSync()) {
first = first->InstructionAtOffset(vixl::kInstructionSize)->skipPool();
}
load = const_cast<Instruction*>(first);
// The call instruction follows the load, but there may be an injected
// constant pool.
call = const_cast<Instruction*>(
load->InstructionAtOffset(vixl::kInstructionSize)->skipPool());
if (call->IsBLR() == enabled) {
return;
}
if (call->IsBLR()) {
// If the second instruction is blr(), then we have:
// ldr x17, [pc, offset]
// blr x17
MOZ_ASSERT(load->IsLDR());
// We want to transform this to:
// adr xzr, [pc, offset]
// nop
int32_t offset = load->ImmLLiteral();
adr(load, xzr, int32_t(offset));
nop(call);
} else {
// We have:
// adr xzr, [pc, offset] (or ldr x17, [pc, offset])
// nop
MOZ_ASSERT(load->IsADR() || load->IsLDR());
MOZ_ASSERT(call->IsNOP());
// Transform this to:
// ldr x17, [pc, offset]
// blr x17
int32_t offset = (int)load->ImmPCRawOffset();
MOZ_ASSERT(vixl::IsInt19(offset));
ldr(load, ScratchReg2_64, int32_t(offset));
blr(call, ScratchReg2_64);
}
}
// Patches loads generated by MacroAssemblerCompat::mov(CodeLabel*, Register).
// The loading code is implemented in movePatchablePtr().
void Assembler::UpdateLoad64Value(Instruction* inst0, uint64_t value) {
MOZ_ASSERT(inst0->IsLDR());
uint64_t* literal = inst0->LiteralAddress<uint64_t*>();
*literal = value;
}
class RelocationIterator {
CompactBufferReader reader_;
uint32_t offset_ = 0;
public:
explicit RelocationIterator(CompactBufferReader& reader) : reader_(reader) {}
bool read() {
if (!reader_.more()) {
return false;
}
offset_ = reader_.readUnsigned();
return true;
}
uint32_t offset() const { return offset_; }
};
static JitCode* CodeFromJump(JitCode* code, uint8_t* jump) {
const Instruction* inst = (const Instruction*)jump;
uint8_t* target;
// We're expecting a call created by MacroAssembler::call(JitCode*).
// It looks like:
//
// ldr scratch, [pc, offset]
// blr scratch
//
// If the call has been toggled by ToggleCall(), it looks like:
//
// adr xzr, [pc, offset]
// nop
//
// There might be a constant pool at the very first instruction.
// See also ToggleCall().
inst = inst->skipPool();
// Skip the stack pointer restore instruction.
if (inst->IsStackPtrSync()) {
inst = inst->InstructionAtOffset(vixl::kInstructionSize)->skipPool();
}
if (inst->BranchType() != vixl::UnknownBranchType) {
// This is an immediate branch.
target = (uint8_t*)inst->ImmPCOffsetTarget();
} else if (inst->IsLDR()) {
// This is an ldr+blr call that is enabled. See ToggleCall().
mozilla::DebugOnly<const Instruction*> nextInst =
inst->InstructionAtOffset(vixl::kInstructionSize)->skipPool();
MOZ_ASSERT(nextInst->IsNOP() || nextInst->IsBLR());
target = (uint8_t*)inst->Literal64();
} else if (inst->IsADR()) {
// This is a disabled call: adr+nop. See ToggleCall().
mozilla::DebugOnly<const Instruction*> nextInst =
inst->InstructionAtOffset(vixl::kInstructionSize)->skipPool();
MOZ_ASSERT(nextInst->IsNOP());
ptrdiff_t offset = inst->ImmPCRawOffset() << vixl::kLiteralEntrySizeLog2;
// This is what Literal64 would do with the corresponding ldr.
memcpy(&target, inst + offset, sizeof(target));
} else {
MOZ_CRASH("Unrecognized jump instruction.");
}
// If the jump is within the code buffer, it uses the extended jump table.
if (target >= code->raw() &&
target < code->raw() + code->instructionsSize()) {
MOZ_ASSERT(target + Assembler::SizeOfJumpTableEntry <=
code->raw() + code->instructionsSize());
uint8_t** patchablePtr =
(uint8_t**)(target + Assembler::OffsetOfJumpTableEntryPointer);
target = *patchablePtr;
}
return JitCode::FromExecutable(target);
}
void Assembler::TraceJumpRelocations(JSTracer* trc, JitCode* code,
CompactBufferReader& reader) {
RelocationIterator iter(reader);
while (iter.read()) {
JitCode* child = CodeFromJump(code, code->raw() + iter.offset());
TraceManuallyBarrieredEdge(trc, &child, "rel32");
MOZ_ASSERT(child == CodeFromJump(code, code->raw() + iter.offset()));
}
}
/* static */
void Assembler::TraceDataRelocations(JSTracer* trc, JitCode* code,
CompactBufferReader& reader) {
mozilla::Maybe<AutoWritableJitCode> awjc;
uint8_t* buffer = code->raw();
while (reader.more()) {
size_t offset = reader.readUnsigned();
Instruction* load = (Instruction*)&buffer[offset];
// The only valid traceable operation is a 64-bit load to an ARMRegister.
// Refer to movePatchablePtr() for generation.
MOZ_ASSERT(load->Mask(vixl::LoadLiteralMask) == vixl::LDR_x_lit);
uintptr_t* literalAddr = load->LiteralAddress<uintptr_t*>();
uintptr_t literal = *literalAddr;
// Data relocations can be for Values or for raw pointers. If a Value is
// zero-tagged, we can trace it as if it were a raw pointer. If a Value
// is not zero-tagged, we have to interpret it as a Value to ensure that the
// tag bits are masked off to recover the actual pointer.
if (literal >> JSVAL_TAG_SHIFT) {
// This relocation is a Value with a non-zero tag.
Value v = Value::fromRawBits(literal);
TraceManuallyBarrieredEdge(trc, &v, "jit-masm-value");
if (*literalAddr != v.asRawBits()) {
if (awjc.isNothing()) {
awjc.emplace(code);
}
*literalAddr = v.asRawBits();
}
continue;
}
// This relocation is a raw pointer or a Value with a zero tag.
// No barriers needed since the pointers are constants.
gc::Cell* cell = reinterpret_cast<gc::Cell*>(literal);
MOZ_ASSERT(gc::IsCellPointerValid(cell));
TraceManuallyBarrieredGenericPointerEdge(trc, &cell, "jit-masm-ptr");
if (uintptr_t(cell) != literal) {
if (awjc.isNothing()) {
awjc.emplace(code);
}
*literalAddr = uintptr_t(cell);
}
}
}
void Assembler::retarget(Label* label, Label* target) {
#ifdef JS_DISASM_ARM64
spew_.spewRetarget(label, target);
#endif
if (label->used()) {
if (target->bound()) {
bind(label, BufferOffset(target));
} else if (target->used()) {
// The target is not bound but used. Prepend label's branch list
// onto target's.
BufferOffset labelBranchOffset(label);
// Find the head of the use chain for label.
BufferOffset next = NextLink(labelBranchOffset);
while (next.assigned()) {
labelBranchOffset = next;
next = NextLink(next);
}
// Then patch the head of label's use chain to the tail of target's
// use chain, prepending the entire use chain of target.
SetNextLink(labelBranchOffset, BufferOffset(target));
target->use(label->offset());
} else {
// The target is unbound and unused. We can just take the head of
// the list hanging off of label, and dump that into target.
target->use(label->offset());
}
}
label->reset();
}
} // namespace jit
} // namespace js
|