1 /*
  2  * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "c1/c1_CodeStubs.hpp"
 26 #include "c1/c1_FrameMap.hpp"
 27 #include "c1/c1_LIRAssembler.hpp"
 28 #include "c1/c1_MacroAssembler.hpp"
 29 #include "c1/c1_Runtime1.hpp"
 30 #include "classfile/javaClasses.hpp"
 31 #include "nativeInst_x86.hpp"
 32 #include "runtime/sharedRuntime.hpp"
 33 #include "utilities/align.hpp"
 34 #include "utilities/macros.hpp"
 35 #include "vmreg_x86.inline.hpp"
 36 
 37 
 38 #define __ ce->masm()->
 39 
 40 #ifndef _LP64
 41 float ConversionStub::float_zero = 0.0;
 42 double ConversionStub::double_zero = 0.0;
 43 
 44 void ConversionStub::emit_code(LIR_Assembler* ce) {
 45   __ bind(_entry);
 46   assert(bytecode() == Bytecodes::_f2i || bytecode() == Bytecodes::_d2i, "other conversions do not require stub");
 47 
 48 
 49   if (input()->is_single_xmm()) {
 50     __ comiss(input()->as_xmm_float_reg(),
 51               ExternalAddress((address)&float_zero));
 52   } else if (input()->is_double_xmm()) {
 53     __ comisd(input()->as_xmm_double_reg(),
 54               ExternalAddress((address)&double_zero));
 55   } else {
 56     __ push(rax);
 57     __ ftst();
 58     __ fnstsw_ax();
 59     __ sahf();
 60     __ pop(rax);
 61   }
 62 
 63   Label NaN, do_return;
 64   __ jccb(Assembler::parity, NaN);
 65   __ jccb(Assembler::below, do_return);
 66 
 67   // input is > 0 -> return maxInt
 68   // result register already contains 0x80000000, so subtracting 1 gives 0x7fffffff
 69   __ decrement(result()->as_register());
 70   __ jmpb(do_return);
 71 
 72   // input is NaN -> return 0
 73   __ bind(NaN);
 74   __ xorptr(result()->as_register(), result()->as_register());
 75 
 76   __ bind(do_return);
 77   __ jmp(_continuation);
 78 }
 79 #endif // !_LP64
 80 
 81 void C1SafepointPollStub::emit_code(LIR_Assembler* ce) {
 82   __ bind(_entry);
 83   InternalAddress safepoint_pc(ce->masm()->pc() - ce->masm()->offset() + safepoint_offset());
 84 #ifdef _LP64
 85   __ lea(rscratch1, safepoint_pc);
 86   __ movptr(Address(r15_thread, JavaThread::saved_exception_pc_offset()), rscratch1);
 87 #else
 88   const Register tmp1 = rcx;
 89   const Register tmp2 = rdx;
 90   __ push(tmp1);
 91   __ push(tmp2);
 92 
 93   __ lea(tmp1, safepoint_pc);
 94   __ get_thread(tmp2);
 95   __ movptr(Address(tmp2, JavaThread::saved_exception_pc_offset()), tmp1);
 96 
 97   __ pop(tmp2);
 98   __ pop(tmp1);
 99 #endif /* _LP64 */
100   assert(SharedRuntime::polling_page_return_handler_blob() != nullptr,
101          "polling page return stub not created yet");
102 
103   address stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
104   __ jump(RuntimeAddress(stub));
105 }
106 
107 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
108   __ bind(_entry);
109   Metadata *m = _method->as_constant_ptr()->as_metadata();
110   ce->store_parameter(m, 1);
111   ce->store_parameter(_bci, 0);
112   __ call(RuntimeAddress(Runtime1::entry_for(C1StubId::counter_overflow_id)));
113   ce->add_call_info_here(_info);
114   ce->verify_oop_map(_info);
115   __ jmp(_continuation);
116 }
117 
118 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
119   __ bind(_entry);
120   if (_info->deoptimize_on_exception()) {
121     address a = Runtime1::entry_for(C1StubId::predicate_failed_trap_id);
122     __ call(RuntimeAddress(a));
123     ce->add_call_info_here(_info);
124     ce->verify_oop_map(_info);
125     debug_only(__ should_not_reach_here());
126     return;
127   }
128 
129   // pass the array index on stack because all registers must be preserved
130   if (_index->is_cpu_register()) {
131     ce->store_parameter(_index->as_register(), 0);
132   } else {
133     ce->store_parameter(_index->as_jint(), 0);
134   }
135   C1StubId stub_id;
136   if (_throw_index_out_of_bounds_exception) {
137     stub_id = C1StubId::throw_index_exception_id;
138   } else {
139     stub_id = C1StubId::throw_range_check_failed_id;
140     ce->store_parameter(_array->as_pointer_register(), 1);
141   }
142   __ call(RuntimeAddress(Runtime1::entry_for(stub_id)));
143   ce->add_call_info_here(_info);
144   ce->verify_oop_map(_info);
145   debug_only(__ should_not_reach_here());
146 }
147 
148 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
149   _info = new CodeEmitInfo(info);
150 }
151 
152 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
153   __ bind(_entry);
154   address a = Runtime1::entry_for(C1StubId::predicate_failed_trap_id);
155   __ call(RuntimeAddress(a));
156   ce->add_call_info_here(_info);
157   ce->verify_oop_map(_info);
158   debug_only(__ should_not_reach_here());
159 }
160 
161 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
162   if (_offset != -1) {
163     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
164   }
165   __ bind(_entry);
166   __ call(RuntimeAddress(Runtime1::entry_for(C1StubId::throw_div0_exception_id)));
167   ce->add_call_info_here(_info);
168   debug_only(__ should_not_reach_here());
169 }
170 
171 
172 // Implementation of NewInstanceStub
173 
174 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, C1StubId stub_id) {
175   _result = result;
176   _klass = klass;
177   _klass_reg = klass_reg;
178   _info = new CodeEmitInfo(info);
179   assert(stub_id == C1StubId::new_instance_id                 ||
180          stub_id == C1StubId::fast_new_instance_id            ||
181          stub_id == C1StubId::fast_new_instance_init_check_id,
182          "need new_instance id");
183   _stub_id   = stub_id;
184 }
185 
186 
187 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
188   assert(__ rsp_offset() == 0, "frame size should be fixed");
189   __ bind(_entry);
190   __ movptr(rdx, _klass_reg->as_register());
191   __ call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
192   ce->add_call_info_here(_info);
193   ce->verify_oop_map(_info);
194   assert(_result->as_register() == rax, "result must in rax,");
195   __ jmp(_continuation);
196 }
197 
198 
199 // Implementation of NewTypeArrayStub
200 
201 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
202   _klass_reg = klass_reg;
203   _length = length;
204   _result = result;
205   _info = new CodeEmitInfo(info);
206 }
207 
208 
209 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
210   assert(__ rsp_offset() == 0, "frame size should be fixed");
211   __ bind(_entry);
212   assert(_length->as_register() == rbx, "length must in rbx,");
213   assert(_klass_reg->as_register() == rdx, "klass_reg must in rdx");
214   __ call(RuntimeAddress(Runtime1::entry_for(C1StubId::new_type_array_id)));
215   ce->add_call_info_here(_info);
216   ce->verify_oop_map(_info);
217   assert(_result->as_register() == rax, "result must in rax,");
218   __ jmp(_continuation);
219 }
220 
221 
222 // Implementation of NewObjectArrayStub
223 
224 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
225   _klass_reg = klass_reg;
226   _result = result;
227   _length = length;
228   _info = new CodeEmitInfo(info);
229 }
230 
231 
232 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
233   assert(__ rsp_offset() == 0, "frame size should be fixed");
234   __ bind(_entry);
235   assert(_length->as_register() == rbx, "length must in rbx,");
236   assert(_klass_reg->as_register() == rdx, "klass_reg must in rdx");
237   __ call(RuntimeAddress(Runtime1::entry_for(C1StubId::new_object_array_id)));
238   ce->add_call_info_here(_info);
239   ce->verify_oop_map(_info);
240   assert(_result->as_register() == rax, "result must in rax,");
241   __ jmp(_continuation);
242 }
243 
244 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
245   assert(__ rsp_offset() == 0, "frame size should be fixed");
246   __ bind(_entry);
247   ce->store_parameter(_obj_reg->as_register(),  1);
248   ce->store_parameter(_lock_reg->as_register(), 0);
249   C1StubId enter_id;
250   if (ce->compilation()->has_fpu_code()) {
251     enter_id = C1StubId::monitorenter_id;
252   } else {
253     enter_id = C1StubId::monitorenter_nofpu_id;
254   }
255   __ call(RuntimeAddress(Runtime1::entry_for(enter_id)));
256   ce->add_call_info_here(_info);
257   ce->verify_oop_map(_info);
258   __ jmp(_continuation);
259 }
260 
261 
262 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
263   __ bind(_entry);
264   if (_compute_lock) {
265     // lock_reg was destroyed by fast unlocking attempt => recompute it
266     ce->monitor_address(_monitor_ix, _lock_reg);
267   }
268   ce->store_parameter(_lock_reg->as_register(), 0);
269   // note: non-blocking leaf routine => no call info needed
270   C1StubId exit_id;
271   if (ce->compilation()->has_fpu_code()) {
272     exit_id = C1StubId::monitorexit_id;
273   } else {
274     exit_id = C1StubId::monitorexit_nofpu_id;
275   }
276   __ call(RuntimeAddress(Runtime1::entry_for(exit_id)));
277   __ jmp(_continuation);
278 }
279 
280 
281 // Implementation of patching:
282 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
283 // - Replace original code with a call to the stub
284 // At Runtime:
285 // - call to stub, jump to runtime
286 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
287 // - in runtime: after initializing class, restore original code, reexecute instruction
288 
289 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
290 
291 void PatchingStub::align_patch_site(MacroAssembler* masm) {
292   // We're patching a 5-7 byte instruction on intel and we need to
293   // make sure that we don't see a piece of the instruction.  It
294   // appears mostly impossible on Intel to simply invalidate other
295   // processors caches and since they may do aggressive prefetch it's
296   // very hard to make a guess about what code might be in the icache.
297   // Force the instruction to be double word aligned so that it
298   // doesn't span a cache line.
299   masm->align(align_up((int)NativeGeneralJump::instruction_size, wordSize));
300 }
301 
302 void PatchingStub::emit_code(LIR_Assembler* ce) {
303   assert(NativeCall::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF, "not enough room for call");
304 
305   Label call_patch;
306 
307   // static field accesses have special semantics while the class
308   // initializer is being run so we emit a test which can be used to
309   // check that this code is being executed by the initializing
310   // thread.
311   address being_initialized_entry = __ pc();
312   if (CommentedAssembly) {
313     __ block_comment(" patch template");
314   }
315   if (_id == load_klass_id) {
316     // produce a copy of the load klass instruction for use by the being initialized case
317 #ifdef ASSERT
318     address start = __ pc();
319 #endif
320     Metadata* o = nullptr;
321     __ mov_metadata(_obj, o);
322 #ifdef ASSERT
323     for (int i = 0; i < _bytes_to_copy; i++) {
324       address ptr = (address)(_pc_start + i);
325       int a_byte = (*ptr) & 0xFF;
326       assert(a_byte == *start++, "should be the same code");
327     }
328 #endif
329   } else if (_id == load_mirror_id) {
330     // produce a copy of the load mirror instruction for use by the being
331     // initialized case
332 #ifdef ASSERT
333     address start = __ pc();
334 #endif
335     jobject o = nullptr;
336     __ movoop(_obj, o);
337 #ifdef ASSERT
338     for (int i = 0; i < _bytes_to_copy; i++) {
339       address ptr = (address)(_pc_start + i);
340       int a_byte = (*ptr) & 0xFF;
341       assert(a_byte == *start++, "should be the same code");
342     }
343 #endif
344   } else {
345     // make a copy the code which is going to be patched.
346     for (int i = 0; i < _bytes_to_copy; i++) {
347       address ptr = (address)(_pc_start + i);
348       int a_byte = (*ptr) & 0xFF;
349       __ emit_int8(a_byte);
350       *ptr = 0x90; // make the site look like a nop
351     }
352   }
353 
354   address end_of_patch = __ pc();
355   int bytes_to_skip = 0;
356   if (_id == load_mirror_id) {
357     int offset = __ offset();
358     if (CommentedAssembly) {
359       __ block_comment(" being_initialized check");
360     }
361     assert(_obj != noreg, "must be a valid register");
362     Register tmp = rax;
363     __ push(tmp);
364     __ movptr(tmp, Address(_obj, java_lang_Class::klass_offset()));
365     __ cmpptr(r15_thread, Address(tmp, InstanceKlass::init_thread_offset()));
366     __ pop(tmp); // pop it right away, no matter which path we take
367     __ jccb(Assembler::notEqual, call_patch);
368 
369     // access_field patches may execute the patched code before it's
370     // copied back into place so we need to jump back into the main
371     // code of the nmethod to continue execution.
372     __ jmp(_patch_site_continuation);
373 
374     // make sure this extra code gets skipped
375     bytes_to_skip += __ offset() - offset;
376   }
377   if (CommentedAssembly) {
378     __ block_comment("patch data encoded as movl");
379   }
380   // Now emit the patch record telling the runtime how to find the
381   // pieces of the patch.  We only need 3 bytes but for readability of
382   // the disassembly we make the data look like a movl reg, imm32,
383   // which requires 5 bytes
384   int sizeof_patch_record = 5;
385   bytes_to_skip += sizeof_patch_record;
386 
387   // emit the offsets needed to find the code to patch
388   int being_initialized_entry_offset = __ pc() - being_initialized_entry + sizeof_patch_record;
389 
390   __ emit_int8((unsigned char)0xB8);
391   __ emit_int8(0);
392   __ emit_int8(being_initialized_entry_offset);
393   __ emit_int8(bytes_to_skip);
394   __ emit_int8(_bytes_to_copy);
395   address patch_info_pc = __ pc();
396   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
397 
398   address entry = __ pc();
399   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
400   address target = nullptr;
401   relocInfo::relocType reloc_type = relocInfo::none;
402   switch (_id) {
403     case access_field_id:  target = Runtime1::entry_for(C1StubId::access_field_patching_id); break;
404     case load_klass_id:    target = Runtime1::entry_for(C1StubId::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
405     case load_mirror_id:   target = Runtime1::entry_for(C1StubId::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
406     case load_appendix_id:      target = Runtime1::entry_for(C1StubId::load_appendix_patching_id); reloc_type = relocInfo::oop_type; break;
407     default: ShouldNotReachHere();
408   }
409   __ bind(call_patch);
410 
411   if (CommentedAssembly) {
412     __ block_comment("patch entry point");
413   }
414   __ call(RuntimeAddress(target));
415   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
416   ce->add_call_info_here(_info);
417   int jmp_off = __ offset();
418   __ jmp(_patch_site_entry);
419   // Add enough nops so deoptimization can overwrite the jmp above with a call
420   // and not destroy the world. We cannot use fat nops here, since the concurrent
421   // code rewrite may transiently create the illegal instruction sequence.
422   for (int j = __ offset() ; j < jmp_off + 5 ; j++ ) {
423     __ nop();
424   }
425   if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) {
426     CodeSection* cs = __ code_section();
427     RelocIterator iter(cs, (address)_pc_start, (address)(_pc_start + 1));
428     relocInfo::change_reloc_info_for_address(&iter, (address) _pc_start, reloc_type, relocInfo::none);
429   }
430 }
431 
432 
433 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
434   __ bind(_entry);
435   ce->store_parameter(_trap_request, 0);
436   __ call(RuntimeAddress(Runtime1::entry_for(C1StubId::deoptimize_id)));
437   ce->add_call_info_here(_info);
438   DEBUG_ONLY(__ should_not_reach_here());
439 }
440 
441 
442 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
443   address a;
444   if (_info->deoptimize_on_exception()) {
445     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
446     a = Runtime1::entry_for(C1StubId::predicate_failed_trap_id);
447   } else {
448     a = Runtime1::entry_for(C1StubId::throw_null_pointer_exception_id);
449   }
450 
451   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
452   __ bind(_entry);
453   __ call(RuntimeAddress(a));
454   ce->add_call_info_here(_info);
455   ce->verify_oop_map(_info);
456   debug_only(__ should_not_reach_here());
457 }
458 
459 
460 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
461   assert(__ rsp_offset() == 0, "frame size should be fixed");
462 
463   __ bind(_entry);
464   // pass the object on stack because all registers must be preserved
465   if (_obj->is_cpu_register()) {
466     ce->store_parameter(_obj->as_register(), 0);
467   }
468   __ call(RuntimeAddress(Runtime1::entry_for(_stub)));
469   ce->add_call_info_here(_info);
470   debug_only(__ should_not_reach_here());
471 }
472 
473 
474 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
475   //---------------slow case: call to native-----------------
476   __ bind(_entry);
477   // Figure out where the args should go
478   // This should really convert the IntrinsicID to the Method* and signature
479   // but I don't know how to do that.
480   //
481   VMRegPair args[5];
482   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
483   SharedRuntime::java_calling_convention(signature, args, 5);
484 
485   // push parameters
486   // (src, src_pos, dest, destPos, length)
487   Register r[5];
488   r[0] = src()->as_register();
489   r[1] = src_pos()->as_register();
490   r[2] = dst()->as_register();
491   r[3] = dst_pos()->as_register();
492   r[4] = length()->as_register();
493 
494   // next registers will get stored on the stack
495   for (int i = 0; i < 5 ; i++ ) {
496     VMReg r_1 = args[i].first();
497     if (r_1->is_stack()) {
498       int st_off = r_1->reg2stack() * wordSize;
499       __ movptr (Address(rsp, st_off), r[i]);
500     } else {
501       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
502     }
503   }
504 
505   ce->align_call(lir_static_call);
506 
507   ce->emit_static_call_stub();
508   if (ce->compilation()->bailed_out()) {
509     return; // CodeCache is full
510   }
511   AddressLiteral resolve(SharedRuntime::get_resolve_static_call_stub(),
512                          relocInfo::static_call_type);
513   __ call(resolve);
514   ce->add_call_info_here(info());
515 
516 #ifndef PRODUCT
517   if (PrintC1Statistics) {
518     __ incrementl(ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt), rscratch1);
519   }
520 #endif
521 
522   __ jmp(_continuation);
523 }
524 
525 #undef __