1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2010 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 "precompiled.hpp"
  27 #include "asm/assembler.hpp"
  28 #include "assembler_zero.inline.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "nativeInst_zero.hpp"
  31 #include "oops/instanceOop.hpp"
  32 #include "oops/methodOop.hpp"
  33 #include "oops/objArrayKlass.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/methodHandles.hpp"
  36 #include "runtime/frame.inline.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/stubCodeGenerator.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "stack_zero.inline.hpp"
  42 #include "utilities/top.hpp"
  43 #ifdef TARGET_OS_FAMILY_linux
  44 # include "thread_linux.inline.hpp"
  45 #endif
  46 #ifdef COMPILER2
  47 #include "opto/runtime.hpp"
  48 #endif
  49 
  50 // Declaration and definition of StubGenerator (no .hpp file).
  51 // For a more detailed description of the stub routine structure
  52 // see the comment in stubRoutines.hpp
  53 
  54 class StubGenerator: public StubCodeGenerator {
  55  private:
  56   // The call stub is used to call Java from C
  57   static void call_stub(
  58     JavaCallWrapper *call_wrapper,
  59     intptr_t*        result,
  60     BasicType        result_type,
  61     methodOop        method,
  62     address          entry_point,
  63     intptr_t*        parameters,
  64     int              parameter_words,
  65     TRAPS) {
  66     JavaThread *thread = (JavaThread *) THREAD;
  67     ZeroStack *stack = thread->zero_stack();
  68 
  69     // Make sure we have no pending exceptions
  70     assert(!HAS_PENDING_EXCEPTION, "call_stub called with pending exception");
  71 
  72     // Set up the stack if necessary
  73     bool stack_needs_teardown = false;
  74     if (stack->needs_setup()) {
  75       size_t zero_stack_size = stack->suggest_size(thread);
  76       stack->setup(alloca(zero_stack_size), zero_stack_size);
  77       stack_needs_teardown = true;
  78     }
  79 
  80     // Allocate and initialize our frame
  81     EntryFrame *frame =
  82       EntryFrame::build(parameters, parameter_words, call_wrapper, THREAD);
  83 
  84     if (!HAS_PENDING_EXCEPTION) {
  85       // Push the frame
  86       thread->push_zero_frame(frame);
  87 
  88       // Make the call
  89       Interpreter::invoke_method(method, entry_point, THREAD);
  90 
  91       // Store the result
  92       if (!HAS_PENDING_EXCEPTION) {
  93         switch (result_type) {
  94         case T_INT:
  95           *(jint *) result = *(jint *) stack->sp();
  96           break;
  97         case T_LONG:
  98           *(jlong *) result = *(jlong *) stack->sp();
  99           break;
 100         case T_FLOAT:
 101           *(jfloat *) result = *(jfloat *) stack->sp();
 102           break;
 103         case T_DOUBLE:
 104           *(jdouble *) result = *(jdouble *) stack->sp();
 105           break;
 106         case T_OBJECT:
 107           *(oop *) result = *(oop *) stack->sp();
 108           break;
 109         default:
 110           ShouldNotReachHere();
 111         }
 112       }
 113 
 114       // Unwind the frame
 115       thread->pop_zero_frame();
 116     }
 117 
 118     // Tear down the stack if necessary
 119     if (stack_needs_teardown)
 120       stack->teardown();
 121   }
 122 
 123   // These stubs get called from some dumb test routine.
 124   // I'll write them properly when they're called from
 125   // something that's actually doing something.
 126   static void fake_arraycopy_stub(address src, address dst, int count) {
 127     assert(count == 0, "huh?");
 128   }
 129 
 130   void generate_arraycopy_stubs() {
 131     // Call the conjoint generation methods immediately after
 132     // the disjoint ones so that short branches from the former
 133     // to the latter can be generated.
 134     StubRoutines::_jbyte_disjoint_arraycopy  = (address) fake_arraycopy_stub;
 135     StubRoutines::_jbyte_arraycopy           = (address) fake_arraycopy_stub;
 136 
 137     StubRoutines::_jshort_disjoint_arraycopy = (address) fake_arraycopy_stub;
 138     StubRoutines::_jshort_arraycopy          = (address) fake_arraycopy_stub;
 139 
 140     StubRoutines::_jint_disjoint_arraycopy   = (address) fake_arraycopy_stub;
 141     StubRoutines::_jint_arraycopy            = (address) fake_arraycopy_stub;
 142 
 143     StubRoutines::_jlong_disjoint_arraycopy  = (address) fake_arraycopy_stub;
 144     StubRoutines::_jlong_arraycopy           = (address) fake_arraycopy_stub;
 145 
 146     StubRoutines::_oop_disjoint_arraycopy    = ShouldNotCallThisStub();
 147     StubRoutines::_oop_arraycopy             = ShouldNotCallThisStub();
 148 
 149     StubRoutines::_checkcast_arraycopy       = ShouldNotCallThisStub();
 150     StubRoutines::_unsafe_arraycopy          = ShouldNotCallThisStub();
 151     StubRoutines::_generic_arraycopy         = ShouldNotCallThisStub();
 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() {
 182     // Generates all stubs and initializes the entry points
 183 
 184     // entry points that exist in all platforms Note: This is code
 185     // that could be shared among different platforms - however the
 186     // benefit seems to be smaller than the disadvantage of having a
 187     // much more complicated generator structure. See also comment in
 188     // stubRoutines.hpp.
 189 
 190     StubRoutines::_forward_exception_entry   = ShouldNotCallThisStub();
 191     StubRoutines::_call_stub_entry           = (address) call_stub;
 192     StubRoutines::_catch_exception_entry     = ShouldNotCallThisStub();
 193 
 194     // atomic calls
 195     StubRoutines::_atomic_xchg_entry         = ShouldNotCallThisStub();
 196     StubRoutines::_atomic_xchg_ptr_entry     = ShouldNotCallThisStub();
 197     StubRoutines::_atomic_cmpxchg_entry      = ShouldNotCallThisStub();
 198     StubRoutines::_atomic_cmpxchg_ptr_entry  = ShouldNotCallThisStub();
 199     StubRoutines::_atomic_cmpxchg_long_entry = ShouldNotCallThisStub();
 200     StubRoutines::_atomic_add_entry          = ShouldNotCallThisStub();
 201     StubRoutines::_atomic_add_ptr_entry      = ShouldNotCallThisStub();
 202     StubRoutines::_fence_entry               = ShouldNotCallThisStub();
 203 
 204     // amd64 does this here, sparc does it in generate_all()
 205     StubRoutines::_handler_for_unsafe_access_entry =
 206       ShouldNotCallThisStub();
 207   }
 208 
 209   void generate_all() {
 210     // Generates all stubs and initializes the entry points
 211 
 212     // These entry points require SharedInfo::stack0 to be set up in
 213     // non-core builds and need to be relocatable, so they each
 214     // fabricate a RuntimeStub internally.
 215     StubRoutines::_throw_AbstractMethodError_entry =
 216       ShouldNotCallThisStub();
 217 
 218     StubRoutines::_throw_ArithmeticException_entry =
 219       ShouldNotCallThisStub();
 220 
 221     StubRoutines::_throw_NullPointerException_entry =
 222       ShouldNotCallThisStub();
 223 
 224     StubRoutines::_throw_NullPointerException_at_call_entry =
 225       ShouldNotCallThisStub();
 226 
 227     StubRoutines::_throw_StackOverflowError_entry =
 228       ShouldNotCallThisStub();
 229 
 230     // support for verify_oop (must happen after universe_init)
 231     StubRoutines::_verify_oop_subroutine_entry =
 232       ShouldNotCallThisStub();
 233 
 234     // arraycopy stubs used by compilers
 235     generate_arraycopy_stubs();
 236   }
 237 
 238  public:
 239   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
 240     if (all) {
 241       generate_all();
 242     } else {
 243       generate_initial();
 244     }
 245   }
 246 };
 247 
 248 void StubGenerator_generate(CodeBuffer* code, bool all) {
 249   StubGenerator g(code, all);
 250 }
 251 
 252 EntryFrame *EntryFrame::build(const intptr_t*  parameters,
 253                               int              parameter_words,
 254                               JavaCallWrapper* call_wrapper,
 255                               TRAPS) {
 256 
 257   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
 258   stack->overflow_check(header_words + parameter_words, CHECK_NULL);
 259 
 260   stack->push(0); // next_frame, filled in later
 261   intptr_t *fp = stack->sp();
 262   assert(fp - stack->sp() == next_frame_off, "should be");
 263 
 264   stack->push(ENTRY_FRAME);
 265   assert(fp - stack->sp() == frame_type_off, "should be");
 266 
 267   stack->push((intptr_t) call_wrapper);
 268   assert(fp - stack->sp() == call_wrapper_off, "should be");
 269 
 270   for (int i = 0; i < parameter_words; i++)
 271     stack->push(parameters[i]);
 272 
 273   return (EntryFrame *) fp;
 274 }