1 /*
  2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2023 SAP SE. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "asm/macroAssembler.inline.hpp"
 28 #include "c1/c1_Defs.hpp"
 29 #include "c1/c1_MacroAssembler.hpp"
 30 #include "c1/c1_Runtime1.hpp"
 31 #include "ci/ciUtilities.hpp"
 32 #include "compiler/oopMap.hpp"
 33 #include "gc/shared/cardTable.hpp"
 34 #include "gc/shared/cardTableBarrierSet.hpp"
 35 #include "interpreter/interpreter.hpp"
 36 #include "memory/universe.hpp"
 37 #include "nativeInst_s390.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "prims/jvmtiExport.hpp"
 40 #include "register_s390.hpp"
 41 #include "registerSaver_s390.hpp"
 42 #include "runtime/sharedRuntime.hpp"
 43 #include "runtime/signature.hpp"
 44 #include "runtime/stubRoutines.hpp"
 45 #include "runtime/vframeArray.hpp"
 46 #include "utilities/macros.hpp"
 47 #include "utilities/powerOfTwo.hpp"
 48 #include "vmreg_s390.inline.hpp"
 49 
 50 // Implementation of StubAssembler
 51 
 52 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry_point, int number_of_arguments) {
 53   set_num_rt_args(0); // Nothing on stack.
 54   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) || oop_result1 != metadata_result, "registers must be different");
 55 
 56   // We cannot trust that code generated by the C++ compiler saves R14
 57   // to z_abi_160.return_pc, because sometimes it spills R14 using stmg at
 58   // z_abi_160.gpr14 (e.g. InterpreterRuntime::_new()).
 59   // Therefore we load the PC into Z_R1_scratch and let set_last_Java_frame() save
 60   // it into the frame anchor.
 61   address pc = get_PC(Z_R1_scratch);
 62   int call_offset = (int)(pc - addr_at(0));
 63   set_last_Java_frame(Z_SP, Z_R1_scratch);
 64 
 65   // ARG1 must hold thread address.
 66   z_lgr(Z_ARG1, Z_thread);
 67 
 68   address return_pc = nullptr;
 69   align_call_far_patchable(this->pc());
 70   return_pc = call_c_opt(entry_point);
 71   assert(return_pc != nullptr, "const section overflow");
 72 
 73   reset_last_Java_frame();
 74 
 75   // Check for pending exceptions.
 76   {
 77     load_and_test_long(Z_R0_scratch, Address(Z_thread, Thread::pending_exception_offset()));
 78 
 79     // This used to conditionally jump to forward_exception however it is
 80     // possible if we relocate that the branch will not reach. So we must jump
 81     // around so we can always reach.
 82 
 83     Label ok;
 84     z_bre(ok); // Bcondequal is the same as bcondZero.
 85 
 86     // exception pending => forward to exception handler
 87 
 88     // Make sure that the vm_results are cleared.
 89     if (oop_result1->is_valid()) {
 90       clear_mem(Address(Z_thread, JavaThread::vm_result_offset()), sizeof(jlong));
 91     }
 92     if (metadata_result->is_valid()) {
 93       clear_mem(Address(Z_thread, JavaThread::vm_result_2_offset()), sizeof(jlong));
 94     }
 95     if (frame_size() == no_frame_size) {
 96       // Pop the stub frame.
 97       pop_frame();
 98       restore_return_pc();
 99       load_const_optimized(Z_R1, StubRoutines::forward_exception_entry());
100       z_br(Z_R1);
101     } else if (_stub_id == Runtime1::forward_exception_id) {
102       should_not_reach_here();
103     } else {
104       load_const_optimized(Z_R1, Runtime1::entry_for (Runtime1::forward_exception_id));
105       z_br(Z_R1);
106     }
107 
108     bind(ok);
109   }
110 
111   // Get oop results if there are any and reset the values in the thread.
112   if (oop_result1->is_valid()) {
113     get_vm_result(oop_result1);
114   }
115   if (metadata_result->is_valid()) {
116     get_vm_result_2(metadata_result);
117   }
118 
119   return call_offset;
120 }
121 
122 
123 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
124   // Z_ARG1 is reserved for the thread.
125   lgr_if_needed(Z_ARG2, arg1);
126   return call_RT(oop_result1, metadata_result, entry, 1);
127 }
128 
129 
130 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
131   // Z_ARG1 is reserved for the thread.
132   lgr_if_needed(Z_ARG2, arg1);
133   assert(arg2 != Z_ARG2, "smashed argument");
134   lgr_if_needed(Z_ARG3, arg2);
135   return call_RT(oop_result1, metadata_result, entry, 2);
136 }
137 
138 
139 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
140   // Z_ARG1 is reserved for the thread.
141   lgr_if_needed(Z_ARG2, arg1);
142   assert(arg2 != Z_ARG2, "smashed argument");
143   lgr_if_needed(Z_ARG3, arg2);
144   assert(arg3 != Z_ARG3, "smashed argument");
145   lgr_if_needed(Z_ARG4, arg3);
146   return call_RT(oop_result1, metadata_result, entry, 3);
147 }
148 
149 
150 // Implementation of Runtime1
151 
152 #define __ sasm->
153 
154 #ifndef PRODUCT
155 #undef  __
156 #define __ (Verbose ? (sasm->block_comment(FILE_AND_LINE),sasm):sasm)->
157 #endif // !PRODUCT
158 
159 #define BLOCK_COMMENT(str) if (PrintAssembly) __ block_comment(str)
160 #define BIND(label)        bind(label); BLOCK_COMMENT(#label ":")
161 
162 static OopMap* generate_oop_map(StubAssembler* sasm) {
163   RegisterSaver::RegisterSet reg_set = RegisterSaver::all_registers;
164   int frame_size_in_slots =
165     RegisterSaver::live_reg_frame_size(reg_set) / VMRegImpl::stack_slot_size;
166   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
167   return RegisterSaver::generate_oop_map(sasm, reg_set);
168 }
169 
170 static OopMap* save_live_registers(StubAssembler* sasm, bool save_fpu_registers = true, Register return_pc = Z_R14) {
171   __ block_comment("save_live_registers");
172   RegisterSaver::RegisterSet reg_set =
173     save_fpu_registers ? RegisterSaver::all_registers : RegisterSaver::all_integer_registers;
174   int frame_size_in_slots =
175     RegisterSaver::live_reg_frame_size(reg_set) / VMRegImpl::stack_slot_size;
176   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
177   return RegisterSaver::save_live_registers(sasm, reg_set, return_pc);
178 }
179 
180 static OopMap* save_live_registers_except_r2(StubAssembler* sasm, bool save_fpu_registers = true) {
181   if (!save_fpu_registers) {
182     __ unimplemented(FILE_AND_LINE);
183   }
184   __ block_comment("save_live_registers");
185   RegisterSaver::RegisterSet reg_set = RegisterSaver::all_registers_except_r2;
186   int frame_size_in_slots =
187       RegisterSaver::live_reg_frame_size(reg_set) / VMRegImpl::stack_slot_size;
188   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
189   return RegisterSaver::save_live_registers(sasm, reg_set);
190 }
191 
192 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
193   __ block_comment("restore_live_registers");
194   RegisterSaver::RegisterSet reg_set =
195     restore_fpu_registers ? RegisterSaver::all_registers : RegisterSaver::all_integer_registers;
196   RegisterSaver::restore_live_registers(sasm, reg_set);
197 }
198 
199 static void restore_live_registers_except_r2(StubAssembler* sasm, bool restore_fpu_registers = true) {
200   if (!restore_fpu_registers) {
201     __ unimplemented(FILE_AND_LINE);
202   }
203   __ block_comment("restore_live_registers_except_r2");
204   RegisterSaver::restore_live_registers(sasm, RegisterSaver::all_registers_except_r2);
205 }
206 
207 void Runtime1::initialize_pd() {
208   // Nothing to do.
209 }
210 
211 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
212   // Make a frame and preserve the caller's caller-save registers.
213   OopMap* oop_map = save_live_registers(sasm);
214   int call_offset;
215   if (!has_argument) {
216     call_offset = __ call_RT(noreg, noreg, target);
217   } else {
218     call_offset = __ call_RT(noreg, noreg, target, Z_R1_scratch, Z_R0_scratch);
219   }
220   OopMapSet* oop_maps = new OopMapSet();
221   oop_maps->add_gc_map(call_offset, oop_map);
222 
223   __ should_not_reach_here();
224   return oop_maps;
225 }
226 
227 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
228   // Incoming parameters: Z_EXC_OOP and Z_EXC_PC.
229   // Keep copies in callee-saved registers during runtime call.
230   const Register exception_oop_callee_saved = Z_R11;
231   const Register exception_pc_callee_saved = Z_R12;
232   // Other registers used in this stub.
233   const Register handler_addr = Z_R4;
234 
235   if (AbortVMOnException) {
236     save_live_registers(sasm);
237     __ call_VM_leaf(CAST_FROM_FN_PTR(address, check_abort_on_vm_exception), Z_EXC_OOP);
238     restore_live_registers(sasm);
239   }
240 
241   // Verify that only exception_oop, is valid at this time.
242   __ invalidate_registers(Z_EXC_OOP, Z_EXC_PC);
243 
244   // Check that fields in JavaThread for exception oop and issuing pc are set.
245   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_oop_offset()), Z_thread, "exception oop already set : " FILE_AND_LINE, 0);
246   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_pc_offset()), Z_thread, "exception pc already set : " FILE_AND_LINE, 0);
247 
248   // Save exception_oop and pc in callee-saved register to preserve it
249   // during runtime calls.
250   __ verify_not_null_oop(Z_EXC_OOP);
251   __ lgr_if_needed(exception_oop_callee_saved, Z_EXC_OOP);
252   __ lgr_if_needed(exception_pc_callee_saved, Z_EXC_PC);
253 
254   __ push_frame_abi160(0); // Runtime code needs the z_abi_160.
255 
256   // Search the exception handler address of the caller (using the return address).
257   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), Z_thread, Z_EXC_PC);
258   // Z_RET(Z_R2): exception handler address of the caller.
259 
260   __ pop_frame();
261 
262   __ invalidate_registers(exception_oop_callee_saved, exception_pc_callee_saved, Z_RET);
263 
264   // Move result of call into correct register.
265   __ lgr_if_needed(handler_addr, Z_RET);
266 
267   // Restore exception oop and pc to Z_EXC_OOP and Z_EXC_PC (required convention of exception handler).
268   __ lgr_if_needed(Z_EXC_OOP, exception_oop_callee_saved);
269   __ lgr_if_needed(Z_EXC_PC, exception_pc_callee_saved);
270 
271   // Verify that there is really a valid exception in Z_EXC_OOP.
272   __ verify_not_null_oop(Z_EXC_OOP);
273 
274   __ z_br(handler_addr); // Jump to exception handler.
275 }
276 
277 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
278   // Make a frame and preserve the caller's caller-save registers.
279   OopMap* oop_map = save_live_registers(sasm);
280 
281   // Call the runtime patching routine, returns non-zero if nmethod got deopted.
282   int call_offset = __ call_RT(noreg, noreg, target);
283   OopMapSet* oop_maps = new OopMapSet();
284   oop_maps->add_gc_map(call_offset, oop_map);
285 
286   // Re-execute the patched instruction or, if the nmethod was
287   // deoptmized, return to the deoptimization handler entry that will
288   // cause re-execution of the current bytecode.
289   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
290   assert(deopt_blob != nullptr, "deoptimization blob must have been created");
291 
292   __ z_ltr(Z_RET, Z_RET); // return value == 0
293 
294   restore_live_registers(sasm);
295 
296   __ z_bcr(Assembler::bcondZero, Z_R14);
297 
298   // Return to the deoptimization handler entry for unpacking and
299   // rexecute if we simply returned then we'd deopt as if any call we
300   // patched had just returned.
301   AddressLiteral dest(deopt_blob->unpack_with_reexecution());
302   __ load_const_optimized(Z_R1_scratch, dest);
303   __ z_br(Z_R1_scratch);
304 
305   return oop_maps;
306 }
307 
308 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
309 
310   // for better readability
311   const bool must_gc_arguments = true;
312   const bool dont_gc_arguments = false;
313 
314   // Default value; overwritten for some optimized stubs that are
315   // called from methods that do not use the fpu.
316   bool save_fpu_registers = true;
317 
318   // Stub code and info for the different stubs.
319   OopMapSet* oop_maps = nullptr;
320   switch (id) {
321     case forward_exception_id:
322       {
323         oop_maps = generate_handle_exception(id, sasm);
324         // will not return
325       }
326       break;
327 
328     case new_instance_id:
329     case fast_new_instance_id:
330     case fast_new_instance_init_check_id:
331       {
332         Register klass    = Z_R11; // Incoming
333         Register obj      = Z_R2;  // Result
334 
335         if (id == new_instance_id) {
336           __ set_info("new_instance", dont_gc_arguments);
337         } else if (id == fast_new_instance_id) {
338           __ set_info("fast new_instance", dont_gc_arguments);
339         } else {
340           assert(id == fast_new_instance_init_check_id, "bad StubID");
341           __ set_info("fast new_instance init check", dont_gc_arguments);
342         }
343 
344         OopMap* map = save_live_registers_except_r2(sasm);
345         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
346         oop_maps = new OopMapSet();
347         oop_maps->add_gc_map(call_offset, map);
348         restore_live_registers_except_r2(sasm);
349 
350         __ verify_oop(obj, FILE_AND_LINE);
351         __ z_br(Z_R14);
352       }
353       break;
354 
355     case counter_overflow_id:
356       {
357         // Arguments :
358         //   bci    : stack param 0
359         //   method : stack param 1
360         //
361         Register bci = Z_ARG2, method = Z_ARG3;
362         // frame size in bytes
363         OopMap* map = save_live_registers(sasm);
364         const int frame_size = sasm->frame_size() * VMRegImpl::slots_per_word * VMRegImpl::stack_slot_size;
365         __ z_lg(bci,    0*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
366         __ z_lg(method, 1*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
367         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
368         oop_maps = new OopMapSet();
369         oop_maps->add_gc_map(call_offset, map);
370         restore_live_registers(sasm);
371         __ z_br(Z_R14);
372       }
373       break;
374     case new_type_array_id:
375     case new_object_array_id:
376       {
377         Register length   = Z_R13; // Incoming
378         Register klass    = Z_R11; // Incoming
379         Register obj      = Z_R2;  // Result
380 
381         if (id == new_type_array_id) {
382           __ set_info("new_type_array", dont_gc_arguments);
383         } else {
384           __ set_info("new_object_array", dont_gc_arguments);
385         }
386 
387 #ifdef ASSERT
388         // Assert object type is really an array of the proper kind.
389         {
390           NearLabel ok;
391           Register t0 = obj;
392           __ mem2reg_opt(t0, Address(klass, Klass::layout_helper_offset()), false);
393           __ z_sra(t0, Klass::_lh_array_tag_shift);
394           int tag = ((id == new_type_array_id)
395                      ? Klass::_lh_array_tag_type_value
396                      : Klass::_lh_array_tag_obj_value);
397           __ compare32_and_branch(t0, tag, Assembler::bcondEqual, ok);
398           __ stop("assert(is an array klass)");
399           __ should_not_reach_here();
400           __ bind(ok);
401         }
402 #endif // ASSERT
403 
404         OopMap* map = save_live_registers_except_r2(sasm);
405         int call_offset;
406         if (id == new_type_array_id) {
407           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
408         } else {
409           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
410         }
411 
412         oop_maps = new OopMapSet();
413         oop_maps->add_gc_map(call_offset, map);
414         restore_live_registers_except_r2(sasm);
415 
416         __ verify_oop(obj, FILE_AND_LINE);
417         __ z_br(Z_R14);
418       }
419       break;
420 
421     case new_multi_array_id:
422       { __ set_info("new_multi_array", dont_gc_arguments);
423         // Z_R3,: klass
424         // Z_R4,: rank
425         // Z_R5: address of 1st dimension
426         OopMap* map = save_live_registers(sasm);
427         int call_offset = __ call_RT(Z_R2, noreg, CAST_FROM_FN_PTR(address, new_multi_array), Z_R3, Z_R4, Z_R5);
428 
429         oop_maps = new OopMapSet();
430         oop_maps->add_gc_map(call_offset, map);
431         restore_live_registers_except_r2(sasm);
432 
433         // Z_R2,: new multi array
434         __ verify_oop(Z_R2, FILE_AND_LINE);
435         __ z_br(Z_R14);
436       }
437       break;
438 
439     case register_finalizer_id:
440       {
441         __ set_info("register_finalizer", dont_gc_arguments);
442 
443         // Load the klass and check the has finalizer flag.
444         Register klass = Z_ARG2;
445         __ load_klass(klass, Z_ARG1);
446         __ testbit(Address(klass, Klass::access_flags_offset()), exact_log2(JVM_ACC_HAS_FINALIZER));
447         __ z_bcr(Assembler::bcondAllZero, Z_R14); // Return if bit is not set.
448 
449         OopMap* oop_map = save_live_registers(sasm);
450         int call_offset = __ call_RT(noreg, noreg,
451                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), Z_ARG1);
452         oop_maps = new OopMapSet();
453         oop_maps->add_gc_map(call_offset, oop_map);
454 
455         // Now restore all the live registers.
456         restore_live_registers(sasm);
457 
458         __ z_br(Z_R14);
459       }
460       break;
461 
462     case throw_range_check_failed_id:
463       { __ set_info("range_check_failed", dont_gc_arguments);
464         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
465       }
466       break;
467 
468     case throw_index_exception_id:
469       { __ set_info("index_range_check_failed", dont_gc_arguments);
470         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
471       }
472       break;
473     case throw_div0_exception_id:
474       { __ set_info("throw_div0_exception", dont_gc_arguments);
475         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
476       }
477       break;
478     case throw_null_pointer_exception_id:
479       { __ set_info("throw_null_pointer_exception", dont_gc_arguments);
480         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
481       }
482       break;
483     case handle_exception_nofpu_id:
484     case handle_exception_id:
485       { __ set_info("handle_exception", dont_gc_arguments);
486         oop_maps = generate_handle_exception(id, sasm);
487       }
488       break;
489     case handle_exception_from_callee_id:
490       { __ set_info("handle_exception_from_callee", dont_gc_arguments);
491         oop_maps = generate_handle_exception(id, sasm);
492       }
493       break;
494     case unwind_exception_id:
495       { __ set_info("unwind_exception", dont_gc_arguments);
496         // Note: no stubframe since we are about to leave the current
497         // activation and we are calling a leaf VM function only.
498         generate_unwind_exception(sasm);
499       }
500       break;
501     case throw_array_store_exception_id:
502       { __ set_info("throw_array_store_exception", dont_gc_arguments);
503         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
504       }
505       break;
506     case throw_class_cast_exception_id:
507     { // Z_R1_scratch: object
508       __ set_info("throw_class_cast_exception", dont_gc_arguments);
509       oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
510     }
511     break;
512     case throw_incompatible_class_change_error_id:
513       { __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
514         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
515       }
516       break;
517     case slow_subtype_check_id:
518     {
519       // Arguments :
520       //   sub  : stack param 0
521       //   super: stack param 1
522       //   raddr: Z_R14, blown by call
523       //
524       // Result : condition code 0 for match (bcondEqual will be true),
525       //          condition code 2 for miss  (bcondNotEqual will be true)
526       NearLabel miss;
527       const Register Rsubklass   = Z_ARG2; // sub
528       const Register Rsuperklass = Z_ARG3; // super
529 
530       // No args, but tmp registers that are killed.
531       const Register Rlength     = Z_ARG4; // cache array length
532       const Register Rarray_ptr  = Z_ARG5; // Current value from cache array.
533 
534       if (UseCompressedOops) {
535         assert(Universe::heap() != nullptr, "java heap must be initialized to generate partial_subtype_check stub");
536       }
537 
538       const int frame_size = 4*BytesPerWord + frame::z_abi_160_size;
539       // Save return pc. This is not necessary, but could be helpful
540       // in the case of crashes.
541       __ save_return_pc();
542       __ push_frame(frame_size);
543       // Save registers before changing them.
544       int i = 0;
545       __ z_stg(Rsubklass,   (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
546       __ z_stg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
547       __ z_stg(Rlength,     (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
548       __ z_stg(Rarray_ptr,  (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
549       assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
550 
551       // Get sub and super from stack.
552       __ z_lg(Rsubklass,   0*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
553       __ z_lg(Rsuperklass, 1*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
554 
555       __ check_klass_subtype_slow_path(Rsubklass, Rsuperklass, Rarray_ptr, Rlength, nullptr, &miss);
556 
557       // Match falls through here.
558       i = 0;
559       __ z_lg(Rsubklass,   (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
560       __ z_lg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
561       __ z_lg(Rlength,     (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
562       __ z_lg(Rarray_ptr,  (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
563       assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
564       __ pop_frame();
565       // Return pc is still in R_14.
566       __ clear_reg(Z_R0_scratch);         // Zero indicates a match. Set CC 0 (bcondEqual will be true)
567       __ z_br(Z_R14);
568 
569       __ BIND(miss);
570       i = 0;
571       __ z_lg(Rsubklass,   (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
572       __ z_lg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
573       __ z_lg(Rlength,     (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
574       __ z_lg(Rarray_ptr,  (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
575       assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
576       __ pop_frame();
577       // return pc is still in R_14
578       __ load_const_optimized(Z_R0_scratch, 1); // One indicates a miss.
579       __ z_ltgr(Z_R0_scratch, Z_R0_scratch);    // Set CC 2 (bcondNotEqual will be true).
580       __ z_br(Z_R14);
581     }
582     break;
583     case monitorenter_nofpu_id:
584     case monitorenter_id:
585       { // Z_R1_scratch : object
586         // Z_R13       : lock address (see LIRGenerator::syncTempOpr())
587         __ set_info("monitorenter", dont_gc_arguments);
588 
589         int save_fpu_registers = (id == monitorenter_id);
590         // Make a frame and preserve the caller's caller-save registers.
591         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
592 
593         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), Z_R1_scratch, Z_R13);
594 
595         oop_maps = new OopMapSet();
596         oop_maps->add_gc_map(call_offset, oop_map);
597         restore_live_registers(sasm, save_fpu_registers);
598 
599         __ z_br(Z_R14);
600       }
601       break;
602 
603     case monitorexit_nofpu_id:
604     case monitorexit_id:
605       { // Z_R1_scratch : lock address
606         // Note: really a leaf routine but must setup last java sp
607         //   => Use call_RT for now (speed can be improved by
608         //      doing last java sp setup manually).
609         __ set_info("monitorexit", dont_gc_arguments);
610 
611         int save_fpu_registers = (id == monitorexit_id);
612         // Make a frame and preserve the caller's caller-save registers.
613         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
614 
615         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), Z_R1_scratch);
616 
617         oop_maps = new OopMapSet();
618         oop_maps->add_gc_map(call_offset, oop_map);
619         restore_live_registers(sasm, save_fpu_registers);
620 
621         __ z_br(Z_R14);
622       }
623       break;
624 
625     case deoptimize_id:
626       { // Args: Z_R1_scratch: trap request
627         __ set_info("deoptimize", dont_gc_arguments);
628         Register trap_request = Z_R1_scratch;
629         OopMap* oop_map = save_live_registers(sasm);
630         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize), trap_request);
631         oop_maps = new OopMapSet();
632         oop_maps->add_gc_map(call_offset, oop_map);
633         restore_live_registers(sasm);
634         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
635         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
636         AddressLiteral dest(deopt_blob->unpack_with_reexecution());
637         __ load_const_optimized(Z_R1_scratch, dest);
638         __ z_br(Z_R1_scratch);
639       }
640       break;
641 
642     case access_field_patching_id:
643       { __ set_info("access_field_patching", dont_gc_arguments);
644         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
645       }
646       break;
647 
648     case load_klass_patching_id:
649       { __ set_info("load_klass_patching", dont_gc_arguments);
650         // We should set up register map.
651         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
652       }
653       break;
654 
655     case load_mirror_patching_id:
656       { __ set_info("load_mirror_patching", dont_gc_arguments);
657         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
658       }
659       break;
660 
661     case load_appendix_patching_id:
662       { __ set_info("load_appendix_patching", dont_gc_arguments);
663         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
664       }
665       break;
666 #if 0
667     case dtrace_object_alloc_id:
668       { // rax,: object
669         StubFrame f(sasm, "dtrace_object_alloc", dont_gc_arguments);
670         // We can't gc here so skip the oopmap but make sure that all
671         // the live registers get saved.
672         save_live_registers(sasm, 1);
673 
674         __ NOT_LP64(push(rax)) LP64_ONLY(mov(c_rarg0, rax));
675         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc))));
676         NOT_LP64(__ pop(rax));
677 
678         restore_live_registers(sasm);
679       }
680       break;
681 
682     case fpu2long_stub_id:
683       {
684         // rax, and rdx are destroyed, but should be free since the result is returned there
685         // preserve rsi,ecx
686         __ push(rsi);
687         __ push(rcx);
688         LP64_ONLY(__ push(rdx);)
689 
690         // check for NaN
691         Label return0, do_return, return_min_jlong, do_convert;
692 
693         Address value_high_word(rsp, wordSize + 4);
694         Address value_low_word(rsp, wordSize);
695         Address result_high_word(rsp, 3*wordSize + 4);
696         Address result_low_word(rsp, 3*wordSize);
697 
698         __ subptr(rsp, 32);                    // more than enough on 32bit
699         __ fst_d(value_low_word);
700         __ movl(rax, value_high_word);
701         __ andl(rax, 0x7ff00000);
702         __ cmpl(rax, 0x7ff00000);
703         __ jcc(Assembler::notEqual, do_convert);
704         __ movl(rax, value_high_word);
705         __ andl(rax, 0xfffff);
706         __ orl(rax, value_low_word);
707         __ jcc(Assembler::notZero, return0);
708 
709         __ bind(do_convert);
710         __ fnstcw(Address(rsp, 0));
711         __ movzwl(rax, Address(rsp, 0));
712         __ orl(rax, 0xc00);
713         __ movw(Address(rsp, 2), rax);
714         __ fldcw(Address(rsp, 2));
715         __ fwait();
716         __ fistp_d(result_low_word);
717         __ fldcw(Address(rsp, 0));
718         __ fwait();
719         // This gets the entire long in rax on 64bit
720         __ movptr(rax, result_low_word);
721         // testing of high bits
722         __ movl(rdx, result_high_word);
723         __ mov(rcx, rax);
724         // What the heck is the point of the next instruction???
725         __ xorl(rcx, 0x0);
726         __ movl(rsi, 0x80000000);
727         __ xorl(rsi, rdx);
728         __ orl(rcx, rsi);
729         __ jcc(Assembler::notEqual, do_return);
730         __ fldz();
731         __ fcomp_d(value_low_word);
732         __ fnstsw_ax();
733         __ testl(rax, 0x4100);  // ZF & CF == 0
734         __ jcc(Assembler::equal, return_min_jlong);
735         // return max_jlong
736         __ mov64(rax, CONST64(0x7fffffffffffffff));
737         __ jmp(do_return);
738 
739         __ bind(return_min_jlong);
740         __ mov64(rax, UCONST64(0x8000000000000000));
741         __ jmp(do_return);
742 
743         __ bind(return0);
744         __ fpop();
745         __ xorptr(rax, rax);
746 
747         __ bind(do_return);
748         __ addptr(rsp, 32);
749         LP64_ONLY(__ pop(rdx);)
750         __ pop(rcx);
751         __ pop(rsi);
752         __ ret(0);
753       }
754       break;
755 #endif // TODO
756 
757     case predicate_failed_trap_id:
758       {
759         __ set_info("predicate_failed_trap", dont_gc_arguments);
760 
761         OopMap* map = save_live_registers(sasm);
762 
763         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
764         oop_maps = new OopMapSet();
765         oop_maps->add_gc_map(call_offset, map);
766         restore_live_registers(sasm);
767 
768         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
769         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
770 
771         __ load_const_optimized(Z_R1_scratch, deopt_blob->unpack_with_reexecution());
772         __ z_br(Z_R1_scratch);
773       }
774       break;
775 
776     default:
777       {
778         __ should_not_reach_here(FILE_AND_LINE, id);
779       }
780       break;
781   }
782   return oop_maps;
783 }
784 
785 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler *sasm) {
786   __ block_comment("generate_handle_exception");
787 
788   // incoming parameters: Z_EXC_OOP, Z_EXC_PC
789 
790   // Save registers if required.
791   OopMapSet* oop_maps = new OopMapSet();
792   OopMap* oop_map = nullptr;
793   Register reg_fp = Z_R1_scratch;
794 
795   switch (id) {
796     case forward_exception_id: {
797       // We're handling an exception in the context of a compiled frame.
798       // The registers have been saved in the standard places. Perform
799       // an exception lookup in the caller and dispatch to the handler
800       // if found. Otherwise unwind and dispatch to the callers
801       // exception handler.
802       oop_map = generate_oop_map(sasm);
803 
804       // Load and clear pending exception oop into.
805       __ z_lg(Z_EXC_OOP, Address(Z_thread, Thread::pending_exception_offset()));
806       __ clear_mem(Address(Z_thread, Thread::pending_exception_offset()), 8);
807 
808       // Different stubs forward their exceptions; they should all have similar frame layouts
809       // (a) to find their return address (b) for a correct oop_map generated above.
810       assert(RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers) ==
811              RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers_except_r2), "requirement");
812 
813       // Load issuing PC (the return address for this stub).
814       const int frame_size_in_bytes = sasm->frame_size() * VMRegImpl::slots_per_word * VMRegImpl::stack_slot_size;
815       __ z_lg(Z_EXC_PC, Address(Z_SP, frame_size_in_bytes + _z_common_abi(return_pc)));
816       DEBUG_ONLY(__ z_lay(reg_fp, Address(Z_SP, frame_size_in_bytes));)
817 
818       // Make sure that the vm_results are cleared (may be unnecessary).
819       __ clear_mem(Address(Z_thread, JavaThread::vm_result_offset()),   sizeof(oop));
820       __ clear_mem(Address(Z_thread, JavaThread::vm_result_2_offset()), sizeof(Metadata*));
821       break;
822     }
823     case handle_exception_nofpu_id:
824     case handle_exception_id:
825       // At this point all registers MAY be live.
826       DEBUG_ONLY(__ z_lgr(reg_fp, Z_SP);)
827       oop_map = save_live_registers(sasm, id != handle_exception_nofpu_id, Z_EXC_PC);
828       break;
829     case handle_exception_from_callee_id: {
830       // At this point all registers except Z_EXC_OOP and Z_EXC_PC are dead.
831       DEBUG_ONLY(__ z_lgr(reg_fp, Z_SP);)
832       __ save_return_pc(Z_EXC_PC);
833       const int frame_size_in_bytes = __ push_frame_abi160(0);
834       oop_map = new OopMap(frame_size_in_bytes / VMRegImpl::stack_slot_size, 0);
835       sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
836       break;
837     }
838     default:  ShouldNotReachHere();
839   }
840 
841   // Verify that only Z_EXC_OOP, and Z_EXC_PC are valid at this time.
842   __ invalidate_registers(Z_EXC_OOP, Z_EXC_PC, reg_fp);
843   // Verify that Z_EXC_OOP, contains a valid exception.
844   __ verify_not_null_oop(Z_EXC_OOP);
845 
846   // Check that fields in JavaThread for exception oop and issuing pc
847   // are empty before writing to them.
848   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_oop_offset()), Z_thread, "exception oop already set : " FILE_AND_LINE, 0);
849   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_pc_offset()), Z_thread, "exception pc already set : " FILE_AND_LINE, 0);
850 
851   // Save exception oop and issuing pc into JavaThread.
852   // (Exception handler will load it from here.)
853   __ z_stg(Z_EXC_OOP, Address(Z_thread, JavaThread::exception_oop_offset()));
854   __ z_stg(Z_EXC_PC, Address(Z_thread, JavaThread::exception_pc_offset()));
855 
856 #ifdef ASSERT
857   { NearLabel ok;
858     __ z_cg(Z_EXC_PC, Address(reg_fp, _z_common_abi(return_pc)));
859     __ branch_optimized(Assembler::bcondEqual, ok);
860     __ stop("use throwing pc as return address (has bci & oop map)");
861     __ bind(ok);
862   }
863 #endif
864 
865   // Compute the exception handler.
866   // The exception oop and the throwing pc are read from the fields in JavaThread.
867   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
868   oop_maps->add_gc_map(call_offset, oop_map);
869 
870   // Z_RET(Z_R2): handler address
871   //   will be the deopt blob if nmethod was deoptimized while we looked up
872   //   handler regardless of whether handler existed in the nmethod.
873 
874   // Only Z_R2, is valid at this time, all other registers have been destroyed by the runtime call.
875   __ invalidate_registers(Z_R2);
876 
877   switch(id) {
878     case forward_exception_id:
879     case handle_exception_nofpu_id:
880     case handle_exception_id:
881       // Restore the registers that were saved at the beginning.
882       __ z_lgr(Z_R1_scratch, Z_R2);   // Restoring live registers kills Z_R2.
883       restore_live_registers(sasm, id != handle_exception_nofpu_id);  // Pops as well the frame.
884       __ z_br(Z_R1_scratch);
885       break;
886     case handle_exception_from_callee_id: {
887       __ pop_frame();
888       __ z_br(Z_R2); // Jump to exception handler.
889     }
890     break;
891     default:  ShouldNotReachHere();
892   }
893 
894   return oop_maps;
895 }
896 
897 
898 #undef __
899 
900 const char *Runtime1::pd_name_for_address(address entry) {
901   return "<unknown function>";
902 }