1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the
14 // distribution.
15 //
16 // - Neither the name of Sun Microsystems or the names of contributors may
17 // be used to endorse or promote products derived from this software without
18 // specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 // The original source code covered by the above license above has been modified
34 // significantly by Google Inc.
35 // Copyright 2014 the V8 project authors. All rights reserved.
36 
37 #ifndef V8_CODEGEN_S390_ASSEMBLER_S390_INL_H_
38 #define V8_CODEGEN_S390_ASSEMBLER_S390_INL_H_
39 
40 #include "src/codegen/s390/assembler-s390.h"
41 
42 #include "src/codegen/assembler.h"
43 #include "src/debug/debug.h"
44 #include "src/objects/objects-inl.h"
45 
46 namespace v8 {
47 namespace internal {
48 
SupportsOptimizer()49 bool CpuFeatures::SupportsOptimizer() { return true; }
50 
apply(intptr_t delta)51 void RelocInfo::apply(intptr_t delta) {
52   // Absolute code pointer inside code object moves with the code object.
53   if (IsInternalReference(rmode_)) {
54     // Jump table entry
55     Address target = Memory<Address>(pc_);
56     Memory<Address>(pc_) = target + delta;
57   } else if (IsCodeTarget(rmode_)) {
58     SixByteInstr instr =
59         Instruction::InstructionBits(reinterpret_cast<const byte*>(pc_));
60     int32_t dis = static_cast<int32_t>(instr & 0xFFFFFFFF) * 2  // halfwords
61                   - static_cast<int32_t>(delta);
62     instr >>= 32;  // Clear the 4-byte displacement field.
63     instr <<= 32;
64     instr |= static_cast<uint32_t>(dis / 2);
65     Instruction::SetInstructionBits<SixByteInstr>(reinterpret_cast<byte*>(pc_),
66                                                   instr);
67   } else {
68     // mov sequence
69     DCHECK(IsInternalReferenceEncoded(rmode_));
70     Address target = Assembler::target_address_at(pc_, constant_pool_);
71     Assembler::set_target_address_at(pc_, constant_pool_, target + delta,
72                                      SKIP_ICACHE_FLUSH);
73   }
74 }
75 
target_internal_reference()76 Address RelocInfo::target_internal_reference() {
77   if (IsInternalReference(rmode_)) {
78     // Jump table entry
79     return Memory<Address>(pc_);
80   } else {
81     // mov sequence
82     DCHECK(IsInternalReferenceEncoded(rmode_));
83     return Assembler::target_address_at(pc_, constant_pool_);
84   }
85 }
86 
target_internal_reference_address()87 Address RelocInfo::target_internal_reference_address() {
88   DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
89   return pc_;
90 }
91 
target_address()92 Address RelocInfo::target_address() {
93   DCHECK(IsRelativeCodeTarget(rmode_) || IsCodeTarget(rmode_) ||
94          IsRuntimeEntry(rmode_) || IsWasmCall(rmode_));
95   return Assembler::target_address_at(pc_, constant_pool_);
96 }
97 
target_address_address()98 Address RelocInfo::target_address_address() {
99   DCHECK(HasTargetAddressAddress());
100 
101   // Read the address of the word containing the target_address in an
102   // instruction stream.
103   // The only architecture-independent user of this function is the serializer.
104   // The serializer uses it to find out how many raw bytes of instruction to
105   // output before the next target.
106   // For an instruction like LIS/ORI where the target bits are mixed into the
107   // instruction bits, the size of the target will be zero, indicating that the
108   // serializer should not step forward in memory after a target is resolved
109   // and written.
110   return pc_;
111 }
112 
constant_pool_entry_address()113 Address RelocInfo::constant_pool_entry_address() { UNREACHABLE(); }
114 
set_target_compressed_address_at(Address pc,Address constant_pool,Tagged_t target,ICacheFlushMode icache_flush_mode)115 void Assembler::set_target_compressed_address_at(
116     Address pc, Address constant_pool, Tagged_t target,
117     ICacheFlushMode icache_flush_mode) {
118   Assembler::set_target_address_at(
119       pc, constant_pool, static_cast<Address>(target), icache_flush_mode);
120 }
121 
target_address_size()122 int RelocInfo::target_address_size() {
123   if (IsCodedSpecially()) {
124     return Assembler::kSpecialTargetSize;
125   } else {
126     return kSystemPointerSize;
127   }
128 }
129 
target_compressed_address_at(Address pc,Address constant_pool)130 Tagged_t Assembler::target_compressed_address_at(Address pc,
131                                                  Address constant_pool) {
132   return static_cast<Tagged_t>(target_address_at(pc, constant_pool));
133 }
134 
code_target_object_handle_at(Address pc)135 Handle<Object> Assembler::code_target_object_handle_at(Address pc) {
136   SixByteInstr instr =
137       Instruction::InstructionBits(reinterpret_cast<const byte*>(pc));
138   int index = instr & 0xFFFFFFFF;
139   return GetCodeTarget(index);
140 }
141 
target_object()142 HeapObject RelocInfo::target_object() {
143   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
144   if (IsDataEmbeddedObject(rmode_)) {
145     return HeapObject::cast(Object(ReadUnalignedValue<Address>(pc_)));
146   } else if (IsCompressedEmbeddedObject(rmode_)) {
147     return HeapObject::cast(Object(DecompressTaggedAny(
148         host_.address(),
149         Assembler::target_compressed_address_at(pc_, constant_pool_))));
150   } else {
151     return HeapObject::cast(
152         Object(Assembler::target_address_at(pc_, constant_pool_)));
153   }
154 }
155 
target_object_no_host(PtrComprCageBase cage_base)156 HeapObject RelocInfo::target_object_no_host(PtrComprCageBase cage_base) {
157   if (IsCompressedEmbeddedObject(rmode_)) {
158     return HeapObject::cast(Object(DecompressTaggedAny(
159         cage_base,
160         Assembler::target_compressed_address_at(pc_, constant_pool_))));
161   } else {
162     return target_object();
163   }
164 }
165 
compressed_embedded_object_handle_at(Address pc,Address const_pool)166 Handle<HeapObject> Assembler::compressed_embedded_object_handle_at(
167     Address pc, Address const_pool) {
168   return GetEmbeddedObject(target_compressed_address_at(pc, const_pool));
169 }
170 
target_object_handle(Assembler * origin)171 Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
172   DCHECK(IsRelativeCodeTarget(rmode_) || IsCodeTarget(rmode_) ||
173          IsEmbeddedObjectMode(rmode_));
174   if (IsDataEmbeddedObject(rmode_)) {
175     return Handle<HeapObject>::cast(ReadUnalignedValue<Handle<Object>>(pc_));
176   } else if (IsCodeTarget(rmode_) || IsRelativeCodeTarget(rmode_)) {
177     return Handle<HeapObject>::cast(origin->code_target_object_handle_at(pc_));
178   } else {
179     if (IsCompressedEmbeddedObject(rmode_)) {
180       return origin->compressed_embedded_object_handle_at(pc_, constant_pool_);
181     }
182     return Handle<HeapObject>(reinterpret_cast<Address*>(
183         Assembler::target_address_at(pc_, constant_pool_)));
184   }
185 }
186 
set_target_object(Heap * heap,HeapObject target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)187 void RelocInfo::set_target_object(Heap* heap, HeapObject target,
188                                   WriteBarrierMode write_barrier_mode,
189                                   ICacheFlushMode icache_flush_mode) {
190   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
191   if (IsDataEmbeddedObject(rmode_)) {
192     WriteUnalignedValue(pc_, target.ptr());
193     // No need to flush icache since no instructions were changed.
194   } else if (IsCompressedEmbeddedObject(rmode_)) {
195     Assembler::set_target_compressed_address_at(
196         pc_, constant_pool_, CompressTagged(target.ptr()), icache_flush_mode);
197   } else {
198     DCHECK(IsFullEmbeddedObject(rmode_));
199     Assembler::set_target_address_at(pc_, constant_pool_, target.ptr(),
200                                      icache_flush_mode);
201   }
202   if (write_barrier_mode == UPDATE_WRITE_BARRIER && !host().is_null() &&
203       !FLAG_disable_write_barriers) {
204     WriteBarrierForCode(host(), this, target);
205   }
206 }
207 
target_external_reference()208 Address RelocInfo::target_external_reference() {
209   DCHECK(rmode_ == EXTERNAL_REFERENCE);
210   return Assembler::target_address_at(pc_, constant_pool_);
211 }
212 
set_target_external_reference(Address target,ICacheFlushMode icache_flush_mode)213 void RelocInfo::set_target_external_reference(
214     Address target, ICacheFlushMode icache_flush_mode) {
215   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
216   Assembler::set_target_address_at(pc_, constant_pool_, target,
217                                    icache_flush_mode);
218 }
219 
target_runtime_entry(Assembler * origin)220 Address RelocInfo::target_runtime_entry(Assembler* origin) {
221   DCHECK(IsRuntimeEntry(rmode_));
222   return target_address();
223 }
224 
target_off_heap_target()225 Address RelocInfo::target_off_heap_target() {
226   DCHECK(IsOffHeapTarget(rmode_));
227   return Assembler::target_address_at(pc_, constant_pool_);
228 }
229 
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)230 void RelocInfo::set_target_runtime_entry(Address target,
231                                          WriteBarrierMode write_barrier_mode,
232                                          ICacheFlushMode icache_flush_mode) {
233   DCHECK(IsRuntimeEntry(rmode_));
234   if (target_address() != target)
235     set_target_address(target, write_barrier_mode, icache_flush_mode);
236 }
237 
WipeOut()238 void RelocInfo::WipeOut() {
239   DCHECK(IsEmbeddedObjectMode(rmode_) || IsCodeTarget(rmode_) ||
240          IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
241          IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_) ||
242          IsOffHeapTarget(rmode_));
243   if (IsInternalReference(rmode_)) {
244     // Jump table entry
245     Memory<Address>(pc_) = kNullAddress;
246   } else if (IsCompressedEmbeddedObject(rmode_)) {
247     Assembler::set_target_compressed_address_at(pc_, constant_pool_,
248                                                 kNullAddress);
249   } else if (IsInternalReferenceEncoded(rmode_) || IsOffHeapTarget(rmode_)) {
250     // mov sequence
251     // Currently used only by deserializer, no need to flush.
252     Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress,
253                                      SKIP_ICACHE_FLUSH);
254   } else {
255     Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress);
256   }
257 }
258 
259 // Operand constructors
Operand(Register rm)260 Operand::Operand(Register rm) : rm_(rm), rmode_(RelocInfo::NONE) {}
261 
262 // Fetch the 32bit value from the FIXED_SEQUENCE IIHF / IILF
target_address_at(Address pc,Address constant_pool)263 Address Assembler::target_address_at(Address pc, Address constant_pool) {
264   // S390 Instruction!
265   // We want to check for instructions generated by Asm::mov()
266   Opcode op1 = Instruction::S390OpcodeValue(reinterpret_cast<const byte*>(pc));
267   SixByteInstr instr_1 =
268       Instruction::InstructionBits(reinterpret_cast<const byte*>(pc));
269 
270   if (BRASL == op1 || BRCL == op1) {
271     int32_t dis = static_cast<int32_t>(instr_1 & 0xFFFFFFFF) * 2;
272     return pc + dis;
273   }
274 
275 #if V8_TARGET_ARCH_S390X
276   int instr1_length =
277       Instruction::InstructionLength(reinterpret_cast<const byte*>(pc));
278   Opcode op2 = Instruction::S390OpcodeValue(
279       reinterpret_cast<const byte*>(pc + instr1_length));
280   SixByteInstr instr_2 = Instruction::InstructionBits(
281       reinterpret_cast<const byte*>(pc + instr1_length));
282   // IIHF for hi_32, IILF for lo_32
283   if (IIHF == op1 && IILF == op2) {
284     return static_cast<Address>(((instr_1 & 0xFFFFFFFF) << 32) |
285                                 ((instr_2 & 0xFFFFFFFF)));
286   }
287 #else
288   // IILF loads 32-bits
289   if (IILF == op1 || CFI == op1) {
290     return static_cast<Address>((instr_1 & 0xFFFFFFFF));
291   }
292 #endif
293 
294   UNIMPLEMENTED();
295   return 0;
296 }
297 
298 // This sets the branch destination (which gets loaded at the call address).
299 // This is for calls and branches within generated code.  The serializer
300 // has already deserialized the mov instructions etc.
301 // There is a FIXED_SEQUENCE assumption here
deserialization_set_special_target_at(Address instruction_payload,Code code,Address target)302 void Assembler::deserialization_set_special_target_at(
303     Address instruction_payload, Code code, Address target) {
304   set_target_address_at(instruction_payload,
305                         !code.is_null() ? code.constant_pool() : kNullAddress,
306                         target);
307 }
308 
deserialization_special_target_size(Address instruction_payload)309 int Assembler::deserialization_special_target_size(
310     Address instruction_payload) {
311   return kSpecialTargetSize;
312 }
313 
deserialization_set_target_internal_reference_at(Address pc,Address target,RelocInfo::Mode mode)314 void Assembler::deserialization_set_target_internal_reference_at(
315     Address pc, Address target, RelocInfo::Mode mode) {
316   if (RelocInfo::IsInternalReferenceEncoded(mode)) {
317     set_target_address_at(pc, kNullAddress, target, SKIP_ICACHE_FLUSH);
318   } else {
319     Memory<Address>(pc) = target;
320   }
321 }
322 
323 // This code assumes the FIXED_SEQUENCE of IIHF/IILF
set_target_address_at(Address pc,Address constant_pool,Address target,ICacheFlushMode icache_flush_mode)324 void Assembler::set_target_address_at(Address pc, Address constant_pool,
325                                       Address target,
326                                       ICacheFlushMode icache_flush_mode) {
327   // Check for instructions generated by Asm::mov()
328   Opcode op1 = Instruction::S390OpcodeValue(reinterpret_cast<const byte*>(pc));
329   SixByteInstr instr_1 =
330       Instruction::InstructionBits(reinterpret_cast<const byte*>(pc));
331   bool patched = false;
332 
333   if (BRASL == op1 || BRCL == op1) {
334     instr_1 >>= 32;  // Zero out the lower 32-bits
335     instr_1 <<= 32;
336     int32_t halfwords = (target - pc) / 2;  // number of halfwords
337     instr_1 |= static_cast<uint32_t>(halfwords);
338     Instruction::SetInstructionBits<SixByteInstr>(reinterpret_cast<byte*>(pc),
339                                                   instr_1);
340     if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
341       FlushInstructionCache(pc, 6);
342     }
343     patched = true;
344   } else {
345 #if V8_TARGET_ARCH_S390X
346     int instr1_length =
347         Instruction::InstructionLength(reinterpret_cast<const byte*>(pc));
348     Opcode op2 = Instruction::S390OpcodeValue(
349         reinterpret_cast<const byte*>(pc + instr1_length));
350     SixByteInstr instr_2 = Instruction::InstructionBits(
351         reinterpret_cast<const byte*>(pc + instr1_length));
352     // IIHF for hi_32, IILF for lo_32
353     if (IIHF == op1 && IILF == op2) {
354       // IIHF
355       instr_1 >>= 32;  // Zero out the lower 32-bits
356       instr_1 <<= 32;
357       instr_1 |= reinterpret_cast<uint64_t>(target) >> 32;
358 
359       Instruction::SetInstructionBits<SixByteInstr>(reinterpret_cast<byte*>(pc),
360                                                     instr_1);
361 
362       // IILF
363       instr_2 >>= 32;
364       instr_2 <<= 32;
365       instr_2 |= reinterpret_cast<uint64_t>(target) & 0xFFFFFFFF;
366 
367       Instruction::SetInstructionBits<SixByteInstr>(
368           reinterpret_cast<byte*>(pc + instr1_length), instr_2);
369       if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
370         FlushInstructionCache(pc, 12);
371       }
372       patched = true;
373     }
374 #else
375     // IILF loads 32-bits
376     if (IILF == op1 || CFI == op1) {
377       instr_1 >>= 32;  // Zero out the lower 32-bits
378       instr_1 <<= 32;
379       instr_1 |= reinterpret_cast<uint32_t>(target);
380 
381       Instruction::SetInstructionBits<SixByteInstr>(reinterpret_cast<byte*>(pc),
382                                                     instr_1);
383       if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
384         FlushInstructionCache(pc, 6);
385       }
386       patched = true;
387     }
388 #endif
389   }
390   if (!patched) UNREACHABLE();
391 }
392 
393 }  // namespace internal
394 }  // namespace v8
395 
396 #endif  // V8_CODEGEN_S390_ASSEMBLER_S390_INL_H_
397