1 /*
  2  * Copyright (c) 2003, 2021, 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 "precompiled.hpp"
 26 #ifdef COMPILER2
 27 #include "asm/macroAssembler.hpp"
 28 #include "asm/macroAssembler.inline.hpp"
 29 #include "code/vmreg.hpp"
 30 #include "interpreter/interpreter.hpp"
 31 #include "opto/runtime.hpp"
 32 #include "runtime/sharedRuntime.hpp"
 33 #include "runtime/stubRoutines.hpp"
 34 #include "runtime/vframeArray.hpp"
 35 #include "utilities/globalDefinitions.hpp"
 36 #include "vmreg_x86.inline.hpp"
 37 
 38 class SimpleRuntimeFrame {
 39 
 40   public:
 41 
 42   // Most of the runtime stubs have this simple frame layout.
 43   // This class exists to make the layout shared in one place.
 44   // Offsets are for compiler stack slots, which are jints.
 45   enum layout {
 46     // The frame sender code expects that rbp will be in the "natural" place and
 47     // will override any oopMap setting for it. We must therefore force the layout
 48     // so that it agrees with the frame sender code.
 49     rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
 50     rbp_off2,
 51     return_off, return_off2,
 52     framesize
 53   };
 54 };
 55 
 56 #define __ masm->
 57 
 58 //------------------------------generate_uncommon_trap_blob--------------------
 59 void OptoRuntime::generate_uncommon_trap_blob() {
 60   // Allocate space for the code
 61   ResourceMark rm;
 62   // Setup code generation tools
 63   CodeBuffer buffer("uncommon_trap_blob", 2048, 1024);
 64   MacroAssembler* masm = new MacroAssembler(&buffer);
 65 
 66   assert(SimpleRuntimeFrame::framesize % 4 == 0, "sp not 16-byte aligned");
 67 
 68   address start = __ pc();
 69 
 70   // Push self-frame.  We get here with a return address on the
 71   // stack, so rsp is 8-byte aligned until we allocate our frame.
 72   __ subptr(rsp, SimpleRuntimeFrame::return_off << LogBytesPerInt); // Epilog!
 73 
 74   // No callee saved registers. rbp is assumed implicitly saved
 75   __ movptr(Address(rsp, SimpleRuntimeFrame::rbp_off << LogBytesPerInt), rbp);
 76 
 77   // compiler left unloaded_class_index in j_rarg0 move to where the
 78   // runtime expects it.
 79   __ movl(c_rarg1, j_rarg0);
 80 
 81   __ set_last_Java_frame(noreg, noreg, nullptr, rscratch1);
 82 
 83   // Call C code.  Need thread but NOT official VM entry
 84   // crud.  We cannot block on this call, no GC can happen.  Call should
 85   // capture callee-saved registers as well as return values.
 86   // Thread is in rdi already.
 87   //
 88   // UnrollBlock* uncommon_trap(JavaThread* thread, jint unloaded_class_index);
 89 
 90   __ mov(c_rarg0, r15_thread);
 91   __ movl(c_rarg2, Deoptimization::Unpack_uncommon_trap);
 92   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap)));
 93 
 94   // Set an oopmap for the call site
 95   OopMapSet* oop_maps = new OopMapSet();
 96   OopMap* map = new OopMap(SimpleRuntimeFrame::framesize, 0);
 97 
 98   // location of rbp is known implicitly by the frame sender code
 99 
100   oop_maps->add_gc_map(__ pc() - start, map);
101 
102   __ reset_last_Java_frame(false);
103 
104   // Load UnrollBlock* into rdi
105   __ mov(rdi, rax);
106 
107 #ifdef ASSERT
108   { Label L;
109     __ cmpptr(Address(rdi, Deoptimization::UnrollBlock::unpack_kind_offset()),
110               Deoptimization::Unpack_uncommon_trap);
111     __ jcc(Assembler::equal, L);
112     __ stop("OptoRuntime::generate_uncommon_trap_blob: expected Unpack_uncommon_trap");
113     __ bind(L);
114   }
115 #endif
116 
117   // Pop all the frames we must move/replace.
118   //
119   // Frame picture (youngest to oldest)
120   // 1: self-frame (no frame link)
121   // 2: deopting frame  (no frame link)
122   // 3: caller of deopting frame (could be compiled/interpreted).
123 
124   // Pop self-frame.  We have no frame, and must rely only on rax and rsp.
125   __ addptr(rsp, (SimpleRuntimeFrame::framesize - 2) << LogBytesPerInt); // Epilog!
126 
127   // Pop deoptimized frame (int)
128   __ movl(rcx, Address(rdi,
129                        Deoptimization::UnrollBlock::
130                        size_of_deoptimized_frame_offset()));
131   __ addptr(rsp, rcx);
132 
133   // rsp should be pointing at the return address to the caller (3)
134 
135   // Pick up the initial fp we should save
136   // restore rbp before stack bang because if stack overflow is thrown it needs to be pushed (and preserved)
137   __ movptr(rbp, Address(rdi, Deoptimization::UnrollBlock::initial_info_offset()));
138 
139 #ifdef ASSERT
140   // Compilers generate code that bang the stack by as much as the
141   // interpreter would need. So this stack banging should never
142   // trigger a fault. Verify that it does not on non product builds.
143   __ movl(rbx, Address(rdi ,Deoptimization::UnrollBlock::total_frame_sizes_offset()));
144   __ bang_stack_size(rbx, rcx);
145 #endif
146 
147   // Load address of array of frame pcs into rcx (address*)
148   __ movptr(rcx, Address(rdi, Deoptimization::UnrollBlock::frame_pcs_offset()));
149 
150   // Trash the return pc
151   __ addptr(rsp, wordSize);
152 
153   // Load address of array of frame sizes into rsi (intptr_t*)
154   __ movptr(rsi, Address(rdi, Deoptimization::UnrollBlock:: frame_sizes_offset()));
155 
156   // Counter
157   __ movl(rdx, Address(rdi, Deoptimization::UnrollBlock:: number_of_frames_offset())); // (int)
158 
159   // Now adjust the caller's stack to make up for the extra locals but
160   // record the original sp so that we can save it in the skeletal
161   // interpreter frame and the stack walking of interpreter_sender
162   // will get the unextended sp value and not the "real" sp value.
163 
164   const Register sender_sp = r8;
165 
166   __ mov(sender_sp, rsp);
167   __ movl(rbx, Address(rdi, Deoptimization::UnrollBlock:: caller_adjustment_offset())); // (int)
168   __ subptr(rsp, rbx);
169 
170   // Push interpreter frames in a loop
171   Label loop;
172   __ bind(loop);
173   __ movptr(rbx, Address(rsi, 0)); // Load frame size
174   __ subptr(rbx, 2 * wordSize);    // We'll push pc and rbp by hand
175   __ pushptr(Address(rcx, 0));     // Save return address
176   __ enter();                      // Save old & set new rbp
177   __ subptr(rsp, rbx);             // Prolog
178   __ movptr(Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize),
179             sender_sp);            // Make it walkable
180   // This value is corrected by layout_activation_impl
181   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
182   __ mov(sender_sp, rsp);          // Pass sender_sp to next frame
183   __ addptr(rsi, wordSize);        // Bump array pointer (sizes)
184   __ addptr(rcx, wordSize);        // Bump array pointer (pcs)
185   __ decrementl(rdx);              // Decrement counter
186   __ jcc(Assembler::notZero, loop);
187   __ pushptr(Address(rcx, 0));     // Save final return address
188 
189   // Re-push self-frame
190   __ enter();                 // Save old & set new rbp
191   __ subptr(rsp, (SimpleRuntimeFrame::framesize - 4) << LogBytesPerInt);
192                               // Prolog
193 
194   // Use rbp because the frames look interpreted now
195   // Save "the_pc" since it cannot easily be retrieved using the last_java_SP after we aligned SP.
196   // Don't need the precise return PC here, just precise enough to point into this code blob.
197   address the_pc = __ pc();
198   __ set_last_Java_frame(noreg, rbp, the_pc, rscratch1);
199 
200   // Call C code.  Need thread but NOT official VM entry
201   // crud.  We cannot block on this call, no GC can happen.  Call should
202   // restore return values to their stack-slots with the new SP.
203   // Thread is in rdi already.
204   //
205   // BasicType unpack_frames(JavaThread* thread, int exec_mode);
206 
207   __ andptr(rsp, -(StackAlignmentInBytes)); // Align SP as required by ABI
208   __ mov(c_rarg0, r15_thread);
209   __ movl(c_rarg1, Deoptimization::Unpack_uncommon_trap);
210   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames)));
211 
212   // Set an oopmap for the call site
213   // Use the same PC we used for the last java frame
214   oop_maps->add_gc_map(the_pc - start, new OopMap(SimpleRuntimeFrame::framesize, 0));
215 
216   // Clear fp AND pc
217   __ reset_last_Java_frame(true);
218 
219   // Pop self-frame.
220   __ leave();                 // Epilog
221 
222   // Jump to interpreter
223   __ ret(0);
224 
225   // Make sure all code is generated
226   masm->flush();
227 
228   _uncommon_trap_blob =  UncommonTrapBlob::create(&buffer, oop_maps,
229                                                  SimpleRuntimeFrame::framesize >> 1);
230 }
231 
232 //------------------------------generate_exception_blob---------------------------
233 // creates exception blob at the end
234 // Using exception blob, this code is jumped from a compiled method.
235 // (see emit_exception_handler in x86_64.ad file)
236 //
237 // Given an exception pc at a call we call into the runtime for the
238 // handler in this method. This handler might merely restore state
239 // (i.e. callee save registers) unwind the frame and jump to the
240 // exception handler for the nmethod if there is no Java level handler
241 // for the nmethod.
242 //
243 // This code is entered with a jmp.
244 //
245 // Arguments:
246 //   rax: exception oop
247 //   rdx: exception pc
248 //
249 // Results:
250 //   rax: exception oop
251 //   rdx: exception pc in caller or ???
252 //   destination: exception handler of caller
253 //
254 // Note: the exception pc MUST be at a call (precise debug information)
255 //       Registers rax, rdx, rcx, rsi, rdi, r8-r11 are not callee saved.
256 //
257 
258 void OptoRuntime::generate_exception_blob() {
259   assert(!OptoRuntime::is_callee_saved_register(RDX_num), "");
260   assert(!OptoRuntime::is_callee_saved_register(RAX_num), "");
261   assert(!OptoRuntime::is_callee_saved_register(RCX_num), "");
262 
263   assert(SimpleRuntimeFrame::framesize % 4 == 0, "sp not 16-byte aligned");
264 
265   // Allocate space for the code
266   ResourceMark rm;
267   // Setup code generation tools
268   CodeBuffer buffer("exception_blob", 2048, 1024);
269   MacroAssembler* masm = new MacroAssembler(&buffer);
270 
271 
272   address start = __ pc();
273 
274   // Exception pc is 'return address' for stack walker
275   __ push(rdx);
276   __ subptr(rsp, SimpleRuntimeFrame::return_off << LogBytesPerInt); // Prolog
277 
278   // Save callee-saved registers.  See x86_64.ad.
279 
280   // rbp is an implicitly saved callee saved register (i.e., the calling
281   // convention will save/restore it in the prolog/epilog). Other than that
282   // there are no callee save registers now that adapter frames are gone.
283 
284   __ movptr(Address(rsp, SimpleRuntimeFrame::rbp_off << LogBytesPerInt), rbp);
285 
286   // Store exception in Thread object. We cannot pass any arguments to the
287   // handle_exception call, since we do not want to make any assumption
288   // about the size of the frame where the exception happened in.
289   // c_rarg0 is either rdi (Linux) or rcx (Windows).
290   __ movptr(Address(r15_thread, JavaThread::exception_oop_offset()),rax);
291   __ movptr(Address(r15_thread, JavaThread::exception_pc_offset()), rdx);
292 
293   // This call does all the hard work.  It checks if an exception handler
294   // exists in the method.
295   // If so, it returns the handler address.
296   // If not, it prepares for stack-unwinding, restoring the callee-save
297   // registers of the frame being removed.
298   //
299   // address OptoRuntime::handle_exception_C(JavaThread* thread)
300 
301   // At a method handle call, the stack may not be properly aligned
302   // when returning with an exception.
303   address the_pc = __ pc();
304   __ set_last_Java_frame(noreg, noreg, the_pc, rscratch1);
305   __ mov(c_rarg0, r15_thread);
306   __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
307   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, OptoRuntime::handle_exception_C)));
308 
309   // Set an oopmap for the call site.  This oopmap will only be used if we
310   // are unwinding the stack.  Hence, all locations will be dead.
311   // Callee-saved registers will be the same as the frame above (i.e.,
312   // handle_exception_stub), since they were restored when we got the
313   // exception.
314 
315   OopMapSet* oop_maps = new OopMapSet();
316 
317   oop_maps->add_gc_map(the_pc - start, new OopMap(SimpleRuntimeFrame::framesize, 0));
318 
319   __ reset_last_Java_frame(false);
320 
321   // Restore callee-saved registers
322 
323   // rbp is an implicitly saved callee-saved register (i.e., the calling
324   // convention will save restore it in prolog/epilog) Other than that
325   // there are no callee save registers now that adapter frames are gone.
326 
327   __ movptr(rbp, Address(rsp, SimpleRuntimeFrame::rbp_off << LogBytesPerInt));
328 
329   __ addptr(rsp, SimpleRuntimeFrame::return_off << LogBytesPerInt); // Epilog
330   __ pop(rdx);                  // No need for exception pc anymore
331 
332   // rax: exception handler
333 
334   // We have a handler in rax (could be deopt blob).
335   __ mov(r8, rax);
336 
337   // Get the exception oop
338   __ movptr(rax, Address(r15_thread, JavaThread::exception_oop_offset()));
339   // Get the exception pc in case we are deoptimized
340   __ movptr(rdx, Address(r15_thread, JavaThread::exception_pc_offset()));
341 #ifdef ASSERT
342   __ movptr(Address(r15_thread, JavaThread::exception_handler_pc_offset()), NULL_WORD);
343   __ movptr(Address(r15_thread, JavaThread::exception_pc_offset()), NULL_WORD);
344 #endif
345   // Clear the exception oop so GC no longer processes it as a root.
346   __ movptr(Address(r15_thread, JavaThread::exception_oop_offset()), NULL_WORD);
347 
348   // rax: exception oop
349   // r8:  exception handler
350   // rdx: exception pc
351   // Jump to handler
352 
353   __ jmp(r8);
354 
355   // Make sure all code is generated
356   masm->flush();
357 
358   // Set exception blob
359   _exception_blob =  ExceptionBlob::create(&buffer, oop_maps, SimpleRuntimeFrame::framesize >> 1);
360 }
361 #endif // COMPILER2