1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2009, 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 // no precompiled headers
  27 #include "assembler_zero.inline.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/icBuffer.hpp"
  32 #include "code/vtableStubs.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_linux.h"
  35 #include "memory/allocation.inline.hpp"
  36 #include "mutex_linux.inline.hpp"
  37 #include "nativeInst_zero.hpp"
  38 #include "os_share_linux.hpp"
  39 #include "prims/jniFastGetField.hpp"
  40 #include "prims/jvm.h"
  41 #include "prims/jvm_misc.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/extendedPC.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/interfaceSupport.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/mutexLocker.hpp"
  49 #include "runtime/osThread.hpp"
  50 #include "runtime/sharedRuntime.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/thread.inline.hpp"
  53 #include "runtime/timer.hpp"
  54 #include "utilities/events.hpp"
  55 #include "utilities/vmError.hpp"
  56 
  57 address os::current_stack_pointer() {
  58   address dummy = (address) &dummy;
  59   return dummy;
  60 }
  61 
  62 frame os::get_sender_for_C_frame(frame* fr) {
  63   ShouldNotCallThis();
  64 }
  65 
  66 frame os::current_frame() {
  67   // The only thing that calls this is the stack printing code in
  68   // VMError::report:
  69   //   - Step 110 (printing stack bounds) uses the sp in the frame
  70   //     to determine the amount of free space on the stack.  We
  71   //     set the sp to a close approximation of the real value in
  72   //     order to allow this step to complete.
  73   //   - Step 120 (printing native stack) tries to walk the stack.
  74   //     The frame we create has a NULL pc, which is ignored as an
  75   //     invalid frame.
  76   frame dummy = frame();
  77   dummy.set_sp((intptr_t *) current_stack_pointer());
  78   return dummy;
  79 }
  80 
  81 char* os::non_memory_address_word() {
  82   // Must never look like an address returned by reserve_memory,
  83   // even in its subfields (as defined by the CPU immediate fields,
  84   // if the CPU splits constants across multiple instructions).
  85 #ifdef SPARC
  86   // On SPARC, 0 != %hi(any real address), because there is no
  87   // allocation in the first 1Kb of the virtual address space.
  88   return (char *) 0;
  89 #else
  90   // This is the value for x86; works pretty well for PPC too.
  91   return (char *) -1;
  92 #endif // SPARC
  93 }
  94 
  95 void os::initialize_thread(Thread * thr){
  96   // Nothing to do.
  97 }
  98 
  99 address os::Linux::ucontext_get_pc(ucontext_t* uc) {
 100   ShouldNotCallThis();
 101 }
 102 
 103 ExtendedPC os::fetch_frame_from_context(void* ucVoid,
 104                                         intptr_t** ret_sp,
 105                                         intptr_t** ret_fp) {
 106   ShouldNotCallThis();
 107 }
 108 
 109 frame os::fetch_frame_from_context(void* ucVoid) {
 110   ShouldNotCallThis();
 111 }
 112 
 113 extern "C" JNIEXPORT int
 114 JVM_handle_linux_signal(int sig,
 115                         siginfo_t* info,
 116                         void* ucVoid,
 117                         int abort_if_unrecognized) {
 118   ucontext_t* uc = (ucontext_t*) ucVoid;
 119 
 120   Thread* t = ThreadLocalStorage::get_thread_slow();
 121 
 122   SignalHandlerMark shm(t);
 123 
 124   // Note: it's not uncommon that JNI code uses signal/sigset to
 125   // install then restore certain signal handler (e.g. to temporarily
 126   // block SIGPIPE, or have a SIGILL handler when detecting CPU
 127   // type). When that happens, JVM_handle_linux_signal() might be
 128   // invoked with junk info/ucVoid. To avoid unnecessary crash when
 129   // libjsig is not preloaded, try handle signals that do not require
 130   // siginfo/ucontext first.
 131 
 132   if (sig == SIGPIPE || sig == SIGXFSZ) {
 133     // allow chained handler to go first
 134     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 135       return true;
 136     } else {
 137       if (PrintMiscellaneous && (WizardMode || Verbose)) {
 138         char buf[64];
 139         warning("Ignoring %s - see bugs 4229104 or 646499219",
 140                 os::exception_name(sig, buf, sizeof(buf)));
 141       }
 142       return true;
 143     }
 144   }
 145 
 146   JavaThread* thread = NULL;
 147   VMThread* vmthread = NULL;
 148   if (os::Linux::signal_handlers_are_installed) {
 149     if (t != NULL ){
 150       if(t->is_Java_thread()) {
 151         thread = (JavaThread*)t;
 152       }
 153       else if(t->is_VM_thread()){
 154         vmthread = (VMThread *)t;
 155       }
 156     }
 157   }
 158 
 159   if (info != NULL && thread != NULL) {
 160     // Handle ALL stack overflow variations here
 161     if (sig == SIGSEGV) {
 162       address addr = (address) info->si_addr;
 163 
 164       // check if fault address is within thread stack
 165       if (addr < thread->stack_base() &&
 166           addr >= thread->stack_base() - thread->stack_size()) {
 167         // stack overflow
 168         if (thread->in_stack_yellow_zone(addr)) {
 169           thread->disable_stack_yellow_zone();
 170           ShouldNotCallThis();
 171         }
 172         else if (thread->in_stack_red_zone(addr)) {
 173           thread->disable_stack_red_zone();
 174           ShouldNotCallThis();
 175         }
 176         else {
 177           // Accessing stack address below sp may cause SEGV if
 178           // current thread has MAP_GROWSDOWN stack. This should
 179           // only happen when current thread was created by user
 180           // code with MAP_GROWSDOWN flag and then attached to VM.
 181           // See notes in os_linux.cpp.
 182           if (thread->osthread()->expanding_stack() == 0) {
 183             thread->osthread()->set_expanding_stack();
 184             if (os::Linux::manually_expand_stack(thread, addr)) {
 185               thread->osthread()->clear_expanding_stack();
 186               return true;
 187             }
 188             thread->osthread()->clear_expanding_stack();
 189           }
 190           else {
 191             fatal("recursive segv. expanding stack.");
 192           }
 193         }
 194       }
 195     }
 196 
 197     /*if (thread->thread_state() == _thread_in_Java) {
 198       ShouldNotCallThis();
 199     }
 200     else*/ if (thread->thread_state() == _thread_in_vm &&
 201                sig == SIGBUS && thread->doing_unsafe_access()) {
 202       ShouldNotCallThis();
 203     }
 204 
 205     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC
 206     // kicks in and the heap gets shrunk before the field access.
 207     /*if (sig == SIGSEGV || sig == SIGBUS) {
 208       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 209       if (addr != (address)-1) {
 210         stub = addr;
 211       }
 212     }*/
 213 
 214     // Check to see if we caught the safepoint code in the process
 215     // of write protecting the memory serialization page.  It write
 216     // enables the page immediately after protecting it so we can
 217     // just return to retry the write.
 218     if (sig == SIGSEGV &&
 219         os::is_memory_serialize_page(thread, (address) info->si_addr)) {
 220       // Block current thread until permission is restored.
 221       os::block_on_serialize_page_trap();
 222       return true;
 223     }
 224   }
 225 
 226   // signal-chaining
 227   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 228      return true;
 229   }
 230 
 231   if (!abort_if_unrecognized) {
 232     // caller wants another chance, so give it to him
 233     return false;
 234   }
 235 
 236 #ifndef PRODUCT
 237   if (sig == SIGSEGV) {
 238     fatal("\n#"
 239           "\n#    /--------------------\\"
 240           "\n#    | segmentation fault |"
 241           "\n#    \\---\\ /--------------/"
 242           "\n#        /"
 243           "\n#    [-]        |\\_/|    "
 244           "\n#    (+)=C      |o o|__  "
 245           "\n#    | |        =-*-=__\\ "
 246           "\n#    OOO        c_c_(___)");
 247   }
 248 #endif // !PRODUCT
 249 
 250   const char *fmt = "caught unhandled signal %d";
 251   char buf[64];
 252 
 253   sprintf(buf, fmt, sig);
 254   fatal(buf);
 255 }
 256 
 257 void os::Linux::init_thread_fpu_state(void) {
 258   // Nothing to do
 259 }
 260 
 261 int os::Linux::get_fpu_control_word() {
 262   ShouldNotCallThis();
 263 }
 264 
 265 void os::Linux::set_fpu_control_word(int fpu) {
 266   ShouldNotCallThis();
 267 }
 268 
 269 bool os::is_allocatable(size_t bytes) {
 270 #ifdef _LP64
 271   return true;
 272 #else
 273   if (bytes < 2 * G) {
 274     return true;
 275   }
 276 
 277   char* addr = reserve_memory(bytes, NULL);
 278 
 279   if (addr != NULL) {
 280     release_memory(addr, bytes);
 281   }
 282 
 283   return addr != NULL;
 284 #endif // _LP64
 285 }
 286 
 287 ///////////////////////////////////////////////////////////////////////////////
 288 // thread stack
 289 
 290 size_t os::Linux::min_stack_allowed = 64 * K;
 291 
 292 bool os::Linux::supports_variable_stack_size() {
 293   return true;
 294 }
 295 
 296 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
 297 #ifdef _LP64
 298   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 299 #else
 300   size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
 301 #endif // _LP64
 302   return s;
 303 }
 304 
 305 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
 306   // Only enable glibc guard pages for non-Java threads
 307   // (Java threads have HotSpot guard pages)
 308   return (thr_type == java_thread ? 0 : page_size());
 309 }
 310 
 311 static void current_stack_region(address *bottom, size_t *size) {
 312   pthread_attr_t attr;
 313   int res = pthread_getattr_np(pthread_self(), &attr);
 314   if (res != 0) {
 315     if (res == ENOMEM) {
 316       vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");
 317     }
 318     else {
 319       fatal(err_msg("pthread_getattr_np failed with errno = %d", res));
 320     }
 321   }
 322 
 323   address stack_bottom;
 324   size_t stack_bytes;
 325   res = pthread_attr_getstack(&attr, (void **) &stack_bottom, &stack_bytes);
 326   if (res != 0) {
 327     fatal(err_msg("pthread_attr_getstack failed with errno = %d", res));
 328   }
 329   address stack_top = stack_bottom + stack_bytes;
 330 
 331   // The block of memory returned by pthread_attr_getstack() includes
 332   // guard pages where present.  We need to trim these off.
 333   size_t page_bytes = os::Linux::page_size();
 334   assert(((intptr_t) stack_bottom & (page_bytes - 1)) == 0, "unaligned stack");
 335 
 336   size_t guard_bytes;
 337   res = pthread_attr_getguardsize(&attr, &guard_bytes);
 338   if (res != 0) {
 339     fatal(err_msg("pthread_attr_getguardsize failed with errno = %d", res));
 340   }
 341   int guard_pages = align_size_up(guard_bytes, page_bytes) / page_bytes;
 342   assert(guard_bytes == guard_pages * page_bytes, "unaligned guard");
 343 
 344 #ifdef IA64
 345   // IA64 has two stacks sharing the same area of memory, a normal
 346   // stack growing downwards and a register stack growing upwards.
 347   // Guard pages, if present, are in the centre.  This code splits
 348   // the stack in two even without guard pages, though in theory
 349   // there's nothing to stop us allocating more to the normal stack
 350   // or more to the register stack if one or the other were found
 351   // to grow faster.
 352   int total_pages = align_size_down(stack_bytes, page_bytes) / page_bytes;
 353   stack_bottom += (total_pages - guard_pages) / 2 * page_bytes;
 354 #endif // IA64
 355 
 356   stack_bottom += guard_bytes;
 357 
 358   pthread_attr_destroy(&attr);
 359 
 360   // The initial thread has a growable stack, and the size reported
 361   // by pthread_attr_getstack is the maximum size it could possibly
 362   // be given what currently mapped.  This can be huge, so we cap it.
 363   if (os::Linux::is_initial_thread()) {
 364     stack_bytes = stack_top - stack_bottom;
 365 
 366     if (stack_bytes > JavaThread::stack_size_at_create())
 367       stack_bytes = JavaThread::stack_size_at_create();
 368 
 369     stack_bottom = stack_top - stack_bytes;
 370   }
 371 
 372   assert(os::current_stack_pointer() >= stack_bottom, "should do");
 373   assert(os::current_stack_pointer() < stack_top, "should do");
 374 
 375   *bottom = stack_bottom;
 376   *size = stack_top - stack_bottom;
 377 }
 378 
 379 address os::current_stack_base() {
 380   address bottom;
 381   size_t size;
 382   current_stack_region(&bottom, &size);
 383   return bottom + size;
 384 }
 385 
 386 size_t os::current_stack_size() {
 387   // stack size includes normal stack and HotSpot guard pages
 388   address bottom;
 389   size_t size;
 390   current_stack_region(&bottom, &size);
 391   return size;
 392 }
 393 
 394 /////////////////////////////////////////////////////////////////////////////
 395 // helper functions for fatal error handler
 396 
 397 void os::print_context(outputStream* st, void* context) {
 398   ShouldNotCallThis();
 399 }
 400 
 401 void os::print_register_info(outputStream *st, void *context) {
 402   ShouldNotCallThis();
 403 }
 404 
 405 /////////////////////////////////////////////////////////////////////////////
 406 // Stubs for things that would be in linux_zero.s if it existed.
 407 // You probably want to disassemble these monkeys to check they're ok.
 408 
 409 extern "C" {
 410   int SpinPause() {
 411   }
 412 
 413 
 414   void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
 415     if (from > to) {
 416       jshort *end = from + count;
 417       while (from < end)
 418         *(to++) = *(from++);
 419     }
 420     else if (from < to) {
 421       jshort *end = from;
 422       from += count - 1;
 423       to   += count - 1;
 424       while (from >= end)
 425         *(to--) = *(from--);
 426     }
 427   }
 428   void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
 429     if (from > to) {
 430       jint *end = from + count;
 431       while (from < end)
 432         *(to++) = *(from++);
 433     }
 434     else if (from < to) {
 435       jint *end = from;
 436       from += count - 1;
 437       to   += count - 1;
 438       while (from >= end)
 439         *(to--) = *(from--);
 440     }
 441   }
 442   void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
 443     if (from > to) {
 444       jlong *end = from + count;
 445       while (from < end)
 446         os::atomic_copy64(from++, to++);
 447     }
 448     else if (from < to) {
 449       jlong *end = from;
 450       from += count - 1;
 451       to   += count - 1;
 452       while (from >= end)
 453         os::atomic_copy64(from--, to--);
 454     }
 455   }
 456 
 457   void _Copy_arrayof_conjoint_bytes(HeapWord* from,
 458                                     HeapWord* to,
 459                                     size_t    count) {
 460     memmove(to, from, count);
 461   }
 462   void _Copy_arrayof_conjoint_jshorts(HeapWord* from,
 463                                       HeapWord* to,
 464                                       size_t    count) {
 465     memmove(to, from, count * 2);
 466   }
 467   void _Copy_arrayof_conjoint_jints(HeapWord* from,
 468                                     HeapWord* to,
 469                                     size_t    count) {
 470     memmove(to, from, count * 4);
 471   }
 472   void _Copy_arrayof_conjoint_jlongs(HeapWord* from,
 473                                      HeapWord* to,
 474                                      size_t    count) {
 475     memmove(to, from, count * 8);
 476   }
 477 };
 478 
 479 /////////////////////////////////////////////////////////////////////////////
 480 // Implementations of atomic operations not supported by processors.
 481 //  -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html
 482 
 483 #ifndef _LP64
 484 extern "C" {
 485   long long unsigned int __sync_val_compare_and_swap_8(
 486     volatile void *ptr,
 487     long long unsigned int oldval,
 488     long long unsigned int newval) {
 489     ShouldNotCallThis();
 490   }
 491 };
 492 #endif // !_LP64
 493 
 494 #ifndef PRODUCT
 495 void os::verify_stack_alignment() {
 496 }
 497 #endif
 498 
 499 int os::extra_bang_size_in_bytes() {
 500   // JDK-8050147 requires the full cache line bang for x86.
 501   return VM_Version::L1_line_size();
 502 }