1 /*
  2  * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright 2007, 2008, 2010, 2015 Red Hat, Inc.
  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 "asm/assembler.inline.hpp"
 27 #include "interpreter/interpreter.hpp"
 28 #include "nativeInst_zero.hpp"
 29 #include "oops/instanceOop.hpp"
 30 #include "oops/method.hpp"
 31 #include "oops/objArrayKlass.hpp"
 32 #include "oops/oop.inline.hpp"
 33 #include "prims/methodHandles.hpp"
 34 #include "runtime/frame.inline.hpp"
 35 #include "runtime/handles.inline.hpp"
 36 #include "runtime/javaThread.hpp"
 37 #include "runtime/sharedRuntime.hpp"
 38 #include "runtime/stubCodeGenerator.hpp"
 39 #include "runtime/stubRoutines.hpp"
 40 #include "stack_zero.inline.hpp"
 41 #ifdef COMPILER2
 42 #include "opto/runtime.hpp"
 43 #endif
 44 
 45 // Declaration and definition of StubGenerator (no .hpp file).
 46 // For a more detailed description of the stub routine structure
 47 // see the comment in stubRoutines.hpp
 48 
 49 class StubGenerator: public StubCodeGenerator {
 50  private:
 51   // The call stub is used to call Java from C
 52   static void call_stub(
 53     JavaCallWrapper *call_wrapper,
 54     intptr_t*        result,
 55     BasicType        result_type,
 56     Method*          method,
 57     address          entry_point,
 58     intptr_t*        parameters,
 59     int              parameter_words,
 60     TRAPS) {
 61     JavaThread *thread = THREAD;
 62     ZeroStack *stack = thread->zero_stack();
 63 
 64     // Make sure we have no pending exceptions
 65     assert(!HAS_PENDING_EXCEPTION, "call_stub called with pending exception");
 66 
 67     // Set up the stack if necessary
 68     bool stack_needs_teardown = false;
 69     if (stack->needs_setup()) {
 70       size_t zero_stack_size = stack->suggest_size(thread);
 71       stack->setup(alloca(zero_stack_size), zero_stack_size);
 72       stack_needs_teardown = true;
 73     }
 74 
 75     // Allocate and initialize our frame
 76     EntryFrame *frame =
 77       EntryFrame::build(parameters, parameter_words, call_wrapper, THREAD);
 78 
 79     if (!HAS_PENDING_EXCEPTION) {
 80       // Push the frame
 81       thread->push_zero_frame(frame);
 82 
 83       // Make the call
 84       Interpreter::invoke_method(method, entry_point, THREAD);
 85 
 86       // Store the result
 87       if (!HAS_PENDING_EXCEPTION) {
 88         switch (result_type) {
 89         case T_INT:
 90           *(jint *) result = *(jint *) stack->sp();
 91           break;
 92         case T_LONG:
 93           *(jlong *) result = *(jlong *) stack->sp();
 94           break;
 95         case T_FLOAT:
 96           *(jfloat *) result = *(jfloat *) stack->sp();
 97           break;
 98         case T_DOUBLE:
 99           *(jdouble *) result = *(jdouble *) stack->sp();
100           break;
101         case T_OBJECT:
102           *(oop *) result = *(oop *) stack->sp();
103           break;
104         default:
105           ShouldNotReachHere();
106         }
107       }
108 
109       // Unwind the frame
110       thread->pop_zero_frame();
111     }
112 
113     // Tear down the stack if necessary
114     if (stack_needs_teardown)
115       stack->teardown();
116   }
117 
118   // These stubs get called from some dumb test routine.
119   // I'll write them properly when they're called from
120   // something that's actually doing something.
121   static void fake_arraycopy_stub(address src, address dst, int count) {
122     assert(count == 0, "huh?");
123   }
124 
125   void generate_arraycopy_stubs() {
126     // Call the conjoint generation methods immediately after
127     // the disjoint ones so that short branches from the former
128     // to the latter can be generated.
129     StubRoutines::_jbyte_disjoint_arraycopy  = (address) fake_arraycopy_stub;
130     StubRoutines::_jbyte_arraycopy           = (address) fake_arraycopy_stub;
131 
132     StubRoutines::_jshort_disjoint_arraycopy = (address) fake_arraycopy_stub;
133     StubRoutines::_jshort_arraycopy          = (address) fake_arraycopy_stub;
134 
135     StubRoutines::_jint_disjoint_arraycopy   = (address) fake_arraycopy_stub;
136     StubRoutines::_jint_arraycopy            = (address) fake_arraycopy_stub;
137 
138     StubRoutines::_jlong_disjoint_arraycopy  = (address) fake_arraycopy_stub;
139     StubRoutines::_jlong_arraycopy           = (address) fake_arraycopy_stub;
140 
141     StubRoutines::_oop_disjoint_arraycopy    = ShouldNotCallThisStub();
142     StubRoutines::_oop_arraycopy             = ShouldNotCallThisStub();
143 
144     StubRoutines::_checkcast_arraycopy       = ShouldNotCallThisStub();
145     StubRoutines::_generic_arraycopy         = ShouldNotCallThisStub();
146 
147     // Shared code tests for "null" to discover the stub is not generated.
148     StubRoutines::_unsafe_arraycopy          = nullptr;
149 
150     // Shared code tests for "null" to discover the stub is not generated.
151     StubRoutines::_unsafe_setmemory          = nullptr;
152 
153     // We don't generate specialized code for HeapWord-aligned source
154     // arrays, so just use the code we've already generated
155     StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
156       StubRoutines::_jbyte_disjoint_arraycopy;
157     StubRoutines::_arrayof_jbyte_arraycopy =
158       StubRoutines::_jbyte_arraycopy;
159 
160     StubRoutines::_arrayof_jshort_disjoint_arraycopy =
161       StubRoutines::_jshort_disjoint_arraycopy;
162     StubRoutines::_arrayof_jshort_arraycopy =
163       StubRoutines::_jshort_arraycopy;
164 
165     StubRoutines::_arrayof_jint_disjoint_arraycopy =
166       StubRoutines::_jint_disjoint_arraycopy;
167     StubRoutines::_arrayof_jint_arraycopy =
168       StubRoutines::_jint_arraycopy;
169 
170     StubRoutines::_arrayof_jlong_disjoint_arraycopy =
171       StubRoutines::_jlong_disjoint_arraycopy;
172     StubRoutines::_arrayof_jlong_arraycopy =
173       StubRoutines::_jlong_arraycopy;
174 
175     StubRoutines::_arrayof_oop_disjoint_arraycopy =
176       StubRoutines::_oop_disjoint_arraycopy;
177     StubRoutines::_arrayof_oop_arraycopy =
178       StubRoutines::_oop_arraycopy;
179   }
180 
181   void generate_initial_stubs() {
182     // entry points that exist in all platforms Note: This is code
183     // that could be shared among different platforms - however the
184     // benefit seems to be smaller than the disadvantage of having a
185     // much more complicated generator structure. See also comment in
186     // stubRoutines.hpp.
187 
188     StubRoutines::_forward_exception_entry   = ShouldNotCallThisStub();
189     StubRoutines::_call_stub_entry           = (address) call_stub;
190     StubRoutines::_catch_exception_entry     = ShouldNotCallThisStub();
191 
192     // atomic calls
193     StubRoutines::_atomic_xchg_entry         = ShouldNotCallThisStub();
194     StubRoutines::_atomic_cmpxchg_entry      = ShouldNotCallThisStub();
195     StubRoutines::_atomic_cmpxchg_long_entry = ShouldNotCallThisStub();
196     StubRoutines::_atomic_add_entry          = ShouldNotCallThisStub();
197     StubRoutines::_fence_entry               = ShouldNotCallThisStub();
198   }
199 
200   void generate_continuation_stubs() {
201     // do nothing
202   }
203 
204   void generate_compiler_stubs() {
205     // do nothing
206   }
207 
208   void generate_final_stubs() {
209     // arraycopy stubs used by compilers
210     generate_arraycopy_stubs();
211 
212   }
213 
214  public:
215   StubGenerator(CodeBuffer* code, StubGenBlobId blob_id) : StubCodeGenerator(code, blob_id) {
216     switch(blob_id) {
217     case initial_id:
218       generate_initial_stubs();
219       break;
220      case continuation_id:
221        generate_continuation_stubs();
222       break;
223     case compiler_id:
224        // do nothing
225       break;
226     case final_id:
227       generate_final_stubs();
228       break;
229     default:
230       fatal("unexpected blob id: %d", blob_id);
231       break;
232     };
233   }
234 };
235 
236 void StubGenerator_generate(CodeBuffer* code, StubGenBlobId blob_id) {
237   StubGenerator g(code, blob_id);
238 }
239 
240 EntryFrame *EntryFrame::build(const intptr_t*  parameters,
241                               int              parameter_words,
242                               JavaCallWrapper* call_wrapper,
243                               TRAPS) {
244 
245   ZeroStack *stack = THREAD->zero_stack();
246   stack->overflow_check(header_words + parameter_words, CHECK_NULL);
247 
248   stack->push(0); // next_frame, filled in later
249   intptr_t *fp = stack->sp();
250   assert(fp - stack->sp() == next_frame_off, "should be");
251 
252   stack->push(ENTRY_FRAME);
253   assert(fp - stack->sp() == frame_type_off, "should be");
254 
255   stack->push((intptr_t) call_wrapper);
256   assert(fp - stack->sp() == call_wrapper_off, "should be");
257 
258   for (int i = 0; i < parameter_words; i++)
259     stack->push(parameters[i]);
260 
261   return (EntryFrame *) fp;
262 }