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