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