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