1 /* 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 // no precompiled headers 26 #include "asm/macroAssembler.hpp" 27 #include "classfile/classLoader.hpp" 28 #include "classfile/systemDictionary.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "code/codeCache.hpp" 31 #include "code/icBuffer.hpp" 32 #include "code/vtableStubs.hpp" 33 #include "interpreter/interpreter.hpp" 34 #include "jvm_bsd.h" 35 #include "memory/allocation.inline.hpp" 36 #include "os_share_bsd.hpp" 37 #include "prims/jniFastGetField.hpp" 38 #include "prims/jvm.h" 39 #include "prims/jvm_misc.hpp" 40 #include "runtime/arguments.hpp" 41 #include "runtime/extendedPC.hpp" 42 #include "runtime/frame.inline.hpp" 43 #include "runtime/interfaceSupport.hpp" 44 #include "runtime/java.hpp" 45 #include "runtime/javaCalls.hpp" 46 #include "runtime/mutexLocker.hpp" 47 #include "runtime/osThread.hpp" 48 #include "runtime/sharedRuntime.hpp" 49 #include "runtime/stubRoutines.hpp" 50 #include "runtime/thread.inline.hpp" 51 #include "runtime/timer.hpp" 52 #include "utilities/events.hpp" 53 #include "utilities/vmError.hpp" 54 55 // put OS-includes here 56 # include <sys/types.h> 57 # include <sys/mman.h> 58 # include <pthread.h> 59 # include <signal.h> 60 # include <errno.h> 61 # include <dlfcn.h> 62 # include <stdlib.h> 63 # include <stdio.h> 64 # include <unistd.h> 65 # include <sys/resource.h> 66 # include <pthread.h> 67 # include <sys/stat.h> 68 # include <sys/time.h> 69 # include <sys/utsname.h> 70 # include <sys/socket.h> 71 # include <sys/wait.h> 72 # include <pwd.h> 73 # include <poll.h> 74 #ifndef __OpenBSD__ 75 # include <ucontext.h> 76 #endif 77 78 #if !defined(__APPLE__) && !defined(__NetBSD__) 79 # include <pthread_np.h> 80 #endif 81 82 // needed by current_stack_region() workaround for Mavericks 83 #if defined(__APPLE__) 84 # include <errno.h> 85 # include <sys/types.h> 86 # include <sys/sysctl.h> 87 # define DEFAULT_MAIN_THREAD_STACK_PAGES 2048 88 # define OS_X_10_9_0_KERNEL_MAJOR_VERSION 13 89 #endif 90 91 #ifdef AMD64 92 #define SPELL_REG_SP "rsp" 93 #define SPELL_REG_FP "rbp" 94 #else 95 #define SPELL_REG_SP "esp" 96 #define SPELL_REG_FP "ebp" 97 #endif // AMD64 98 99 #ifdef __FreeBSD__ 100 # define context_trapno uc_mcontext.mc_trapno 101 # ifdef AMD64 102 # define context_pc uc_mcontext.mc_rip 103 # define context_sp uc_mcontext.mc_rsp 104 # define context_fp uc_mcontext.mc_rbp 105 # define context_rip uc_mcontext.mc_rip 106 # define context_rsp uc_mcontext.mc_rsp 107 # define context_rbp uc_mcontext.mc_rbp 108 # define context_rax uc_mcontext.mc_rax 109 # define context_rbx uc_mcontext.mc_rbx 110 # define context_rcx uc_mcontext.mc_rcx 111 # define context_rdx uc_mcontext.mc_rdx 112 # define context_rsi uc_mcontext.mc_rsi 113 # define context_rdi uc_mcontext.mc_rdi 114 # define context_r8 uc_mcontext.mc_r8 115 # define context_r9 uc_mcontext.mc_r9 116 # define context_r10 uc_mcontext.mc_r10 117 # define context_r11 uc_mcontext.mc_r11 118 # define context_r12 uc_mcontext.mc_r12 119 # define context_r13 uc_mcontext.mc_r13 120 # define context_r14 uc_mcontext.mc_r14 121 # define context_r15 uc_mcontext.mc_r15 122 # define context_flags uc_mcontext.mc_flags 123 # define context_err uc_mcontext.mc_err 124 # else 125 # define context_pc uc_mcontext.mc_eip 126 # define context_sp uc_mcontext.mc_esp 127 # define context_fp uc_mcontext.mc_ebp 128 # define context_eip uc_mcontext.mc_eip 129 # define context_esp uc_mcontext.mc_esp 130 # define context_eax uc_mcontext.mc_eax 131 # define context_ebx uc_mcontext.mc_ebx 132 # define context_ecx uc_mcontext.mc_ecx 133 # define context_edx uc_mcontext.mc_edx 134 # define context_ebp uc_mcontext.mc_ebp 135 # define context_esi uc_mcontext.mc_esi 136 # define context_edi uc_mcontext.mc_edi 137 # define context_eflags uc_mcontext.mc_eflags 138 # define context_trapno uc_mcontext.mc_trapno 139 # endif 140 #endif 141 142 #ifdef __APPLE__ 143 # if __DARWIN_UNIX03 && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 144 // 10.5 UNIX03 member name prefixes 145 #define DU3_PREFIX(s, m) __ ## s.__ ## m 146 # else 147 #define DU3_PREFIX(s, m) s ## . ## m 148 # endif 149 150 # ifdef AMD64 151 # define context_pc context_rip 152 # define context_sp context_rsp 153 # define context_fp context_rbp 154 # define context_rip uc_mcontext->DU3_PREFIX(ss,rip) 155 # define context_rsp uc_mcontext->DU3_PREFIX(ss,rsp) 156 # define context_rax uc_mcontext->DU3_PREFIX(ss,rax) 157 # define context_rbx uc_mcontext->DU3_PREFIX(ss,rbx) 158 # define context_rcx uc_mcontext->DU3_PREFIX(ss,rcx) 159 # define context_rdx uc_mcontext->DU3_PREFIX(ss,rdx) 160 # define context_rbp uc_mcontext->DU3_PREFIX(ss,rbp) 161 # define context_rsi uc_mcontext->DU3_PREFIX(ss,rsi) 162 # define context_rdi uc_mcontext->DU3_PREFIX(ss,rdi) 163 # define context_r8 uc_mcontext->DU3_PREFIX(ss,r8) 164 # define context_r9 uc_mcontext->DU3_PREFIX(ss,r9) 165 # define context_r10 uc_mcontext->DU3_PREFIX(ss,r10) 166 # define context_r11 uc_mcontext->DU3_PREFIX(ss,r11) 167 # define context_r12 uc_mcontext->DU3_PREFIX(ss,r12) 168 # define context_r13 uc_mcontext->DU3_PREFIX(ss,r13) 169 # define context_r14 uc_mcontext->DU3_PREFIX(ss,r14) 170 # define context_r15 uc_mcontext->DU3_PREFIX(ss,r15) 171 # define context_flags uc_mcontext->DU3_PREFIX(ss,rflags) 172 # define context_trapno uc_mcontext->DU3_PREFIX(es,trapno) 173 # define context_err uc_mcontext->DU3_PREFIX(es,err) 174 # else 175 # define context_pc context_eip 176 # define context_sp context_esp 177 # define context_fp context_ebp 178 # define context_eip uc_mcontext->DU3_PREFIX(ss,eip) 179 # define context_esp uc_mcontext->DU3_PREFIX(ss,esp) 180 # define context_eax uc_mcontext->DU3_PREFIX(ss,eax) 181 # define context_ebx uc_mcontext->DU3_PREFIX(ss,ebx) 182 # define context_ecx uc_mcontext->DU3_PREFIX(ss,ecx) 183 # define context_edx uc_mcontext->DU3_PREFIX(ss,edx) 184 # define context_ebp uc_mcontext->DU3_PREFIX(ss,ebp) 185 # define context_esi uc_mcontext->DU3_PREFIX(ss,esi) 186 # define context_edi uc_mcontext->DU3_PREFIX(ss,edi) 187 # define context_eflags uc_mcontext->DU3_PREFIX(ss,eflags) 188 # define context_trapno uc_mcontext->DU3_PREFIX(es,trapno) 189 # endif 190 #endif 191 192 #ifdef __OpenBSD__ 193 # define context_trapno sc_trapno 194 # ifdef AMD64 195 # define context_pc sc_rip 196 # define context_sp sc_rsp 197 # define context_fp sc_rbp 198 # define context_rip sc_rip 199 # define context_rsp sc_rsp 200 # define context_rbp sc_rbp 201 # define context_rax sc_rax 202 # define context_rbx sc_rbx 203 # define context_rcx sc_rcx 204 # define context_rdx sc_rdx 205 # define context_rsi sc_rsi 206 # define context_rdi sc_rdi 207 # define context_r8 sc_r8 208 # define context_r9 sc_r9 209 # define context_r10 sc_r10 210 # define context_r11 sc_r11 211 # define context_r12 sc_r12 212 # define context_r13 sc_r13 213 # define context_r14 sc_r14 214 # define context_r15 sc_r15 215 # define context_flags sc_rflags 216 # define context_err sc_err 217 # else 218 # define context_pc sc_eip 219 # define context_sp sc_esp 220 # define context_fp sc_ebp 221 # define context_eip sc_eip 222 # define context_esp sc_esp 223 # define context_eax sc_eax 224 # define context_ebx sc_ebx 225 # define context_ecx sc_ecx 226 # define context_edx sc_edx 227 # define context_ebp sc_ebp 228 # define context_esi sc_esi 229 # define context_edi sc_edi 230 # define context_eflags sc_eflags 231 # define context_trapno sc_trapno 232 # endif 233 #endif 234 235 #ifdef __NetBSD__ 236 # define context_trapno uc_mcontext.__gregs[_REG_TRAPNO] 237 # ifdef AMD64 238 # define __register_t __greg_t 239 # define context_pc uc_mcontext.__gregs[_REG_RIP] 240 # define context_sp uc_mcontext.__gregs[_REG_URSP] 241 # define context_fp uc_mcontext.__gregs[_REG_RBP] 242 # define context_rip uc_mcontext.__gregs[_REG_RIP] 243 # define context_rsp uc_mcontext.__gregs[_REG_URSP] 244 # define context_rax uc_mcontext.__gregs[_REG_RAX] 245 # define context_rbx uc_mcontext.__gregs[_REG_RBX] 246 # define context_rcx uc_mcontext.__gregs[_REG_RCX] 247 # define context_rdx uc_mcontext.__gregs[_REG_RDX] 248 # define context_rbp uc_mcontext.__gregs[_REG_RBP] 249 # define context_rsi uc_mcontext.__gregs[_REG_RSI] 250 # define context_rdi uc_mcontext.__gregs[_REG_RDI] 251 # define context_r8 uc_mcontext.__gregs[_REG_R8] 252 # define context_r9 uc_mcontext.__gregs[_REG_R9] 253 # define context_r10 uc_mcontext.__gregs[_REG_R10] 254 # define context_r11 uc_mcontext.__gregs[_REG_R11] 255 # define context_r12 uc_mcontext.__gregs[_REG_R12] 256 # define context_r13 uc_mcontext.__gregs[_REG_R13] 257 # define context_r14 uc_mcontext.__gregs[_REG_R14] 258 # define context_r15 uc_mcontext.__gregs[_REG_R15] 259 # define context_flags uc_mcontext.__gregs[_REG_RFL] 260 # define context_err uc_mcontext.__gregs[_REG_ERR] 261 # else 262 # define context_pc uc_mcontext.__gregs[_REG_EIP] 263 # define context_sp uc_mcontext.__gregs[_REG_UESP] 264 # define context_fp uc_mcontext.__gregs[_REG_EBP] 265 # define context_eip uc_mcontext.__gregs[_REG_EIP] 266 # define context_esp uc_mcontext.__gregs[_REG_UESP] 267 # define context_eax uc_mcontext.__gregs[_REG_EAX] 268 # define context_ebx uc_mcontext.__gregs[_REG_EBX] 269 # define context_ecx uc_mcontext.__gregs[_REG_ECX] 270 # define context_edx uc_mcontext.__gregs[_REG_EDX] 271 # define context_ebp uc_mcontext.__gregs[_REG_EBP] 272 # define context_esi uc_mcontext.__gregs[_REG_ESI] 273 # define context_edi uc_mcontext.__gregs[_REG_EDI] 274 # define context_eflags uc_mcontext.__gregs[_REG_EFL] 275 # define context_trapno uc_mcontext.__gregs[_REG_TRAPNO] 276 # endif 277 #endif 278 279 address os::current_stack_pointer() { 280 #if defined(__clang__) || defined(__llvm__) 281 register void *esp; 282 __asm__("mov %%"SPELL_REG_SP", %0":"=r"(esp)); 283 return (address) esp; 284 #elif defined(SPARC_WORKS) 285 register void *esp; 286 __asm__("mov %%"SPELL_REG_SP", %0":"=r"(esp)); 287 return (address) ((char*)esp + sizeof(long)*2); 288 #else 289 register void *esp __asm__ (SPELL_REG_SP); 290 return (address) esp; 291 #endif 292 } 293 294 char* os::non_memory_address_word() { 295 // Must never look like an address returned by reserve_memory, 296 // even in its subfields (as defined by the CPU immediate fields, 297 // if the CPU splits constants across multiple instructions). 298 299 return (char*) -1; 300 } 301 302 void os::initialize_thread(Thread* thr) { 303 // Nothing to do. 304 } 305 306 address os::Bsd::ucontext_get_pc(const ucontext_t * uc) { 307 return (address)uc->context_pc; 308 } 309 310 void os::Bsd::ucontext_set_pc(ucontext_t * uc, address pc) { 311 uc->context_pc = (intptr_t)pc ; 312 } 313 314 intptr_t* os::Bsd::ucontext_get_sp(const ucontext_t * uc) { 315 return (intptr_t*)uc->context_sp; 316 } 317 318 intptr_t* os::Bsd::ucontext_get_fp(const ucontext_t * uc) { 319 return (intptr_t*)uc->context_fp; 320 } 321 322 // For Forte Analyzer AsyncGetCallTrace profiling support - thread 323 // is currently interrupted by SIGPROF. 324 // os::Solaris::fetch_frame_from_ucontext() tries to skip nested signal 325 // frames. Currently we don't do that on Bsd, so it's the same as 326 // os::fetch_frame_from_context(). 327 // This method is also used for stack overflow signal handling. 328 ExtendedPC os::Bsd::fetch_frame_from_ucontext(Thread* thread, 329 const ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) { 330 331 assert(thread != NULL, "just checking"); 332 assert(ret_sp != NULL, "just checking"); 333 assert(ret_fp != NULL, "just checking"); 334 335 return os::fetch_frame_from_context(uc, ret_sp, ret_fp); 336 } 337 338 ExtendedPC os::fetch_frame_from_context(const void* ucVoid, 339 intptr_t** ret_sp, intptr_t** ret_fp) { 340 341 ExtendedPC epc; 342 const ucontext_t* uc = (const ucontext_t*)ucVoid; 343 344 if (uc != NULL) { 345 epc = ExtendedPC(os::Bsd::ucontext_get_pc(uc)); 346 if (ret_sp) *ret_sp = os::Bsd::ucontext_get_sp(uc); 347 if (ret_fp) *ret_fp = os::Bsd::ucontext_get_fp(uc); 348 } else { 349 // construct empty ExtendedPC for return value checking 350 epc = ExtendedPC(NULL); 351 if (ret_sp) *ret_sp = (intptr_t *)NULL; 352 if (ret_fp) *ret_fp = (intptr_t *)NULL; 353 } 354 355 return epc; 356 } 357 358 frame os::fetch_frame_from_context(const void* ucVoid) { 359 intptr_t* sp; 360 intptr_t* fp; 361 ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp); 362 return frame(sp, fp, epc.pc()); 363 } 364 365 frame os::fetch_frame_from_ucontext(Thread* thread, void* ucVoid) { 366 intptr_t* sp; 367 intptr_t* fp; 368 ExtendedPC epc = os::Bsd::fetch_frame_from_ucontext(thread, (ucontext_t*)ucVoid, &sp, &fp); 369 return frame(sp, fp, epc.pc()); 370 } 371 372 bool os::Bsd::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) { 373 address pc = (address) os::Bsd::ucontext_get_pc(uc); 374 if (Interpreter::contains(pc)) { 375 // interpreter performs stack banging after the fixed frame header has 376 // been generated while the compilers perform it before. To maintain 377 // semantic consistency between interpreted and compiled frames, the 378 // method returns the Java sender of the current frame. 379 *fr = os::fetch_frame_from_ucontext(thread, uc); 380 if (!fr->is_first_java_frame()) { 381 // get_frame_at_stack_banging_point() is only called when we 382 // have well defined stacks so java_sender() calls do not need 383 // to assert safe_for_sender() first. 384 *fr = fr->java_sender(); 385 } 386 } else { 387 // more complex code with compiled code 388 assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above"); 389 CodeBlob* cb = CodeCache::find_blob(pc); 390 if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) { 391 // Not sure where the pc points to, fallback to default 392 // stack overflow handling 393 return false; 394 } else { 395 *fr = os::fetch_frame_from_ucontext(thread, uc); 396 // in compiled code, the stack banging is performed just after the return pc 397 // has been pushed on the stack 398 *fr = frame(fr->sp() + 1, fr->fp(), (address)*(fr->sp())); 399 if (!fr->is_java_frame()) { 400 // get_frame_at_stack_banging_point() is only called when we 401 // have well defined stacks so java_sender() calls do not need 402 // to assert safe_for_sender() first. 403 *fr = fr->java_sender(); 404 } 405 } 406 } 407 assert(fr->is_java_frame(), "Safety check"); 408 return true; 409 } 410 411 // By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get 412 // turned off by -fomit-frame-pointer, 413 frame os::get_sender_for_C_frame(frame* fr) { 414 return frame(fr->sender_sp(), fr->link(), fr->sender_pc()); 415 } 416 417 intptr_t* _get_previous_fp() { 418 #if defined(SPARC_WORKS) || defined(__clang__) || defined(__llvm__) 419 register intptr_t **ebp; 420 __asm__("mov %%"SPELL_REG_FP", %0":"=r"(ebp)); 421 #else 422 register intptr_t **ebp __asm__ (SPELL_REG_FP); 423 #endif 424 // ebp is for this frame (_get_previous_fp). We want the ebp for the 425 // caller of os::current_frame*(), so go up two frames. However, for 426 // optimized builds, _get_previous_fp() will be inlined, so only go 427 // up 1 frame in that case. 428 #ifdef _NMT_NOINLINE_ 429 return **(intptr_t***)ebp; 430 #else 431 return *ebp; 432 #endif 433 } 434 435 436 frame os::current_frame() { 437 intptr_t* fp = _get_previous_fp(); 438 frame myframe((intptr_t*)os::current_stack_pointer(), 439 (intptr_t*)fp, 440 CAST_FROM_FN_PTR(address, os::current_frame)); 441 if (os::is_first_C_frame(&myframe)) { 442 // stack is not walkable 443 return frame(); 444 } else { 445 return os::get_sender_for_C_frame(&myframe); 446 } 447 } 448 449 // Utility functions 450 451 // From IA32 System Programming Guide 452 enum { 453 trap_page_fault = 0xE 454 }; 455 456 extern "C" JNIEXPORT int 457 JVM_handle_bsd_signal(int sig, 458 siginfo_t* info, 459 void* ucVoid, 460 int abort_if_unrecognized) { 461 ucontext_t* uc = (ucontext_t*) ucVoid; 462 463 Thread* t = Thread::current_or_null_safe(); 464 465 // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away 466 // (no destructors can be run) 467 os::WatcherThreadCrashProtection::check_crash_protection(sig, t); 468 469 SignalHandlerMark shm(t); 470 471 // Note: it's not uncommon that JNI code uses signal/sigset to install 472 // then restore certain signal handler (e.g. to temporarily block SIGPIPE, 473 // or have a SIGILL handler when detecting CPU type). When that happens, 474 // JVM_handle_bsd_signal() might be invoked with junk info/ucVoid. To 475 // avoid unnecessary crash when libjsig is not preloaded, try handle signals 476 // that do not require siginfo/ucontext first. 477 478 if (sig == SIGPIPE || sig == SIGXFSZ) { 479 // allow chained handler to go first 480 if (os::Bsd::chained_handler(sig, info, ucVoid)) { 481 return true; 482 } else { 483 // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219 484 return true; 485 } 486 } 487 488 JavaThread* thread = NULL; 489 VMThread* vmthread = NULL; 490 if (os::Bsd::signal_handlers_are_installed) { 491 if (t != NULL ){ 492 if(t->is_Java_thread()) { 493 thread = (JavaThread*)t; 494 } 495 else if(t->is_VM_thread()){ 496 vmthread = (VMThread *)t; 497 } 498 } 499 } 500 /* 501 NOTE: does not seem to work on bsd. 502 if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) { 503 // can't decode this kind of signal 504 info = NULL; 505 } else { 506 assert(sig == info->si_signo, "bad siginfo"); 507 } 508 */ 509 // decide if this trap can be handled by a stub 510 address stub = NULL; 511 512 address pc = NULL; 513 514 //%note os_trap_1 515 if (info != NULL && uc != NULL && thread != NULL) { 516 pc = (address) os::Bsd::ucontext_get_pc(uc); 517 518 if (StubRoutines::is_safefetch_fault(pc)) { 519 os::Bsd::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc)); 520 return 1; 521 } 522 523 // Handle ALL stack overflow variations here 524 if (sig == SIGSEGV || sig == SIGBUS) { 525 address addr = (address) info->si_addr; 526 527 // check if fault address is within thread stack 528 if (thread->on_local_stack(addr)) { 529 // stack overflow 530 if (thread->in_stack_yellow_reserved_zone(addr)) { 531 if (thread->thread_state() == _thread_in_Java) { 532 if (thread->in_stack_reserved_zone(addr)) { 533 frame fr; 534 if (os::Bsd::get_frame_at_stack_banging_point(thread, uc, &fr)) { 535 assert(fr.is_java_frame(), "Must be a Java frame"); 536 frame activation = SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr); 537 if (activation.sp() != NULL) { 538 thread->disable_stack_reserved_zone(); 539 if (activation.is_interpreted_frame()) { 540 thread->set_reserved_stack_activation((address)( 541 activation.fp() + frame::interpreter_frame_initial_sp_offset)); 542 } else { 543 thread->set_reserved_stack_activation((address)activation.unextended_sp()); 544 } 545 return 1; 546 } 547 } 548 } 549 // Throw a stack overflow exception. Guard pages will be reenabled 550 // while unwinding the stack. 551 thread->disable_stack_yellow_reserved_zone(); 552 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW); 553 } else { 554 // Thread was in the vm or native code. Return and try to finish. 555 thread->disable_stack_yellow_reserved_zone(); 556 return 1; 557 } 558 } else if (thread->in_stack_red_zone(addr)) { 559 // Fatal red zone violation. Disable the guard pages and fall through 560 // to handle_unexpected_exception way down below. 561 thread->disable_stack_red_zone(); 562 tty->print_raw_cr("An irrecoverable stack overflow has occurred."); 563 } 564 } 565 } 566 567 if ((sig == SIGSEGV || sig == SIGBUS) && VM_Version::is_cpuinfo_segv_addr(pc)) { 568 // Verify that OS save/restore AVX registers. 569 stub = VM_Version::cpuinfo_cont_addr(); 570 } 571 572 // We test if stub is already set (by the stack overflow code 573 // above) so it is not overwritten by the code that follows. This 574 // check is not required on other platforms, because on other 575 // platforms we check for SIGSEGV only or SIGBUS only, where here 576 // we have to check for both SIGSEGV and SIGBUS. 577 if (thread->thread_state() == _thread_in_Java && stub == NULL) { 578 // Java thread running in Java code => find exception handler if any 579 // a fault inside compiled code, the interpreter, or a stub 580 581 if ((sig == SIGSEGV || sig == SIGBUS) && os::is_poll_address((address)info->si_addr)) { 582 stub = SharedRuntime::get_poll_stub(pc); 583 #if defined(__APPLE__) 584 // 32-bit Darwin reports a SIGBUS for nearly all memory access exceptions. 585 // 64-bit Darwin may also use a SIGBUS (seen with compressed oops). 586 // Catching SIGBUS here prevents the implicit SIGBUS NULL check below from 587 // being called, so only do so if the implicit NULL check is not necessary. 588 } else if (sig == SIGBUS && MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) { 589 #else 590 } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) { 591 #endif 592 // BugId 4454115: A read from a MappedByteBuffer can fault 593 // here if the underlying file has been truncated. 594 // Do not crash the VM in such a case. 595 CodeBlob* cb = CodeCache::find_blob_unsafe(pc); 596 CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL; 597 if (nm != NULL && nm->has_unsafe_access()) { 598 address next_pc = Assembler::locate_next_instruction(pc); 599 stub = SharedRuntime::handle_unsafe_access(thread, next_pc); 600 } 601 } 602 else 603 604 #ifdef AMD64 605 if (sig == SIGFPE && 606 (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) { 607 stub = 608 SharedRuntime:: 609 continuation_for_implicit_exception(thread, 610 pc, 611 SharedRuntime:: 612 IMPLICIT_DIVIDE_BY_ZERO); 613 #ifdef __APPLE__ 614 } else if (sig == SIGFPE && info->si_code == FPE_NOOP) { 615 int op = pc[0]; 616 617 // Skip REX 618 if ((pc[0] & 0xf0) == 0x40) { 619 op = pc[1]; 620 } else { 621 op = pc[0]; 622 } 623 624 // Check for IDIV 625 if (op == 0xF7) { 626 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime:: IMPLICIT_DIVIDE_BY_ZERO); 627 } else { 628 // TODO: handle more cases if we are using other x86 instructions 629 // that can generate SIGFPE signal. 630 tty->print_cr("unknown opcode 0x%X with SIGFPE.", op); 631 fatal("please update this code."); 632 } 633 #endif /* __APPLE__ */ 634 635 #else 636 if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) { 637 // HACK: si_code does not work on bsd 2.2.12-20!!! 638 int op = pc[0]; 639 if (op == 0xDB) { 640 // FIST 641 // TODO: The encoding of D2I in i486.ad can cause an exception 642 // prior to the fist instruction if there was an invalid operation 643 // pending. We want to dismiss that exception. From the win_32 644 // side it also seems that if it really was the fist causing 645 // the exception that we do the d2i by hand with different 646 // rounding. Seems kind of weird. 647 // NOTE: that we take the exception at the NEXT floating point instruction. 648 assert(pc[0] == 0xDB, "not a FIST opcode"); 649 assert(pc[1] == 0x14, "not a FIST opcode"); 650 assert(pc[2] == 0x24, "not a FIST opcode"); 651 return true; 652 } else if (op == 0xF7) { 653 // IDIV 654 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO); 655 } else { 656 // TODO: handle more cases if we are using other x86 instructions 657 // that can generate SIGFPE signal on bsd. 658 tty->print_cr("unknown opcode 0x%X with SIGFPE.", op); 659 fatal("please update this code."); 660 } 661 #endif // AMD64 662 } else if ((sig == SIGSEGV || sig == SIGBUS) && 663 !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) { 664 // Determination of interpreter/vtable stub/compiled code null exception 665 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL); 666 } 667 } else if (thread->thread_state() == _thread_in_vm && 668 sig == SIGBUS && /* info->si_code == BUS_OBJERR && */ 669 thread->doing_unsafe_access()) { 670 address next_pc = Assembler::locate_next_instruction(pc); 671 stub = SharedRuntime::handle_unsafe_access(thread, next_pc); 672 } 673 674 // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in 675 // and the heap gets shrunk before the field access. 676 if ((sig == SIGSEGV) || (sig == SIGBUS)) { 677 address addr = JNI_FastGetField::find_slowcase_pc(pc); 678 if (addr != (address)-1) { 679 stub = addr; 680 } 681 } 682 683 // Check to see if we caught the safepoint code in the 684 // process of write protecting the memory serialization page. 685 // It write enables the page immediately after protecting it 686 // so we can just return to retry the write. 687 if ((sig == SIGSEGV || sig == SIGBUS) && 688 os::is_memory_serialize_page(thread, (address) info->si_addr)) { 689 // Block current thread until the memory serialize page permission restored. 690 os::block_on_serialize_page_trap(); 691 return true; 692 } 693 } 694 695 #ifndef AMD64 696 // Execution protection violation 697 // 698 // This should be kept as the last step in the triage. We don't 699 // have a dedicated trap number for a no-execute fault, so be 700 // conservative and allow other handlers the first shot. 701 // 702 // Note: We don't test that info->si_code == SEGV_ACCERR here. 703 // this si_code is so generic that it is almost meaningless; and 704 // the si_code for this condition may change in the future. 705 // Furthermore, a false-positive should be harmless. 706 if (UnguardOnExecutionViolation > 0 && 707 (sig == SIGSEGV || sig == SIGBUS) && 708 uc->context_trapno == trap_page_fault) { 709 int page_size = os::vm_page_size(); 710 address addr = (address) info->si_addr; 711 address pc = os::Bsd::ucontext_get_pc(uc); 712 // Make sure the pc and the faulting address are sane. 713 // 714 // If an instruction spans a page boundary, and the page containing 715 // the beginning of the instruction is executable but the following 716 // page is not, the pc and the faulting address might be slightly 717 // different - we still want to unguard the 2nd page in this case. 718 // 719 // 15 bytes seems to be a (very) safe value for max instruction size. 720 bool pc_is_near_addr = 721 (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15); 722 bool instr_spans_page_boundary = 723 (align_size_down((intptr_t) pc ^ (intptr_t) addr, 724 (intptr_t) page_size) > 0); 725 726 if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) { 727 static volatile address last_addr = 728 (address) os::non_memory_address_word(); 729 730 // In conservative mode, don't unguard unless the address is in the VM 731 if (addr != last_addr && 732 (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) { 733 734 // Set memory to RWX and retry 735 address page_start = 736 (address) align_size_down((intptr_t) addr, (intptr_t) page_size); 737 bool res = os::protect_memory((char*) page_start, page_size, 738 os::MEM_PROT_RWX); 739 740 log_debug(os)("Execution protection violation " 741 "at " INTPTR_FORMAT 742 ", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr), 743 p2i(page_start), (res ? "success" : "failed"), errno); 744 stub = pc; 745 746 // Set last_addr so if we fault again at the same address, we don't end 747 // up in an endless loop. 748 // 749 // There are two potential complications here. Two threads trapping at 750 // the same address at the same time could cause one of the threads to 751 // think it already unguarded, and abort the VM. Likely very rare. 752 // 753 // The other race involves two threads alternately trapping at 754 // different addresses and failing to unguard the page, resulting in 755 // an endless loop. This condition is probably even more unlikely than 756 // the first. 757 // 758 // Although both cases could be avoided by using locks or thread local 759 // last_addr, these solutions are unnecessary complication: this 760 // handler is a best-effort safety net, not a complete solution. It is 761 // disabled by default and should only be used as a workaround in case 762 // we missed any no-execute-unsafe VM code. 763 764 last_addr = addr; 765 } 766 } 767 } 768 #endif // !AMD64 769 770 if (stub != NULL) { 771 // save all thread context in case we need to restore it 772 if (thread != NULL) thread->set_saved_exception_pc(pc); 773 774 os::Bsd::ucontext_set_pc(uc, stub); 775 return true; 776 } 777 778 // signal-chaining 779 if (os::Bsd::chained_handler(sig, info, ucVoid)) { 780 return true; 781 } 782 783 if (!abort_if_unrecognized) { 784 // caller wants another chance, so give it to him 785 return false; 786 } 787 788 if (pc == NULL && uc != NULL) { 789 pc = os::Bsd::ucontext_get_pc(uc); 790 } 791 792 // unmask current signal 793 sigset_t newset; 794 sigemptyset(&newset); 795 sigaddset(&newset, sig); 796 sigprocmask(SIG_UNBLOCK, &newset, NULL); 797 798 VMError::report_and_die(t, sig, pc, info, ucVoid); 799 800 ShouldNotReachHere(); 801 return false; 802 } 803 804 // From solaris_i486.s ported to bsd_i486.s 805 extern "C" void fixcw(); 806 807 void os::Bsd::init_thread_fpu_state(void) { 808 #ifndef AMD64 809 // Set fpu to 53 bit precision. This happens too early to use a stub. 810 fixcw(); 811 #endif // !AMD64 812 } 813 814 815 // Check that the bsd kernel version is 2.4 or higher since earlier 816 // versions do not support SSE without patches. 817 bool os::supports_sse() { 818 return true; 819 } 820 821 bool os::is_allocatable(size_t bytes) { 822 #ifdef AMD64 823 // unused on amd64? 824 return true; 825 #else 826 827 if (bytes < 2 * G) { 828 return true; 829 } 830 831 char* addr = reserve_memory(bytes, NULL); 832 833 if (addr != NULL) { 834 release_memory(addr, bytes); 835 } 836 837 return addr != NULL; 838 #endif // AMD64 839 } 840 841 //////////////////////////////////////////////////////////////////////////////// 842 // thread stack 843 844 #ifdef AMD64 845 size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K; 846 size_t os::Posix::_java_thread_min_stack_allowed = 64 * K; 847 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K; 848 #else 849 size_t os::Posix::_compiler_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K; 850 size_t os::Posix::_java_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K; 851 size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K; 852 853 #ifdef __GNUC__ 854 #define GET_GS() ({int gs; __asm__ volatile("movw %%gs, %w0":"=q"(gs)); gs&0xffff;}) 855 #endif 856 857 #endif // AMD64 858 859 // return default stack size for thr_type 860 size_t os::Posix::default_stack_size(os::ThreadType thr_type) { 861 // default stack size (compiler thread needs larger stack) 862 #ifdef AMD64 863 size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M); 864 #else 865 size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K); 866 #endif // AMD64 867 return s; 868 } 869 870 871 // Java thread: 872 // 873 // Low memory addresses 874 // +------------------------+ 875 // | |\ JavaThread created by VM does not have glibc 876 // | glibc guard page | - guard, attached Java thread usually has 877 // | |/ 1 page glibc guard. 878 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size() 879 // | |\ 880 // | HotSpot Guard Pages | - red and yellow pages 881 // | |/ 882 // +------------------------+ JavaThread::stack_yellow_zone_base() 883 // | |\ 884 // | Normal Stack | - 885 // | |/ 886 // P2 +------------------------+ Thread::stack_base() 887 // 888 // Non-Java thread: 889 // 890 // Low memory addresses 891 // +------------------------+ 892 // | |\ 893 // | glibc guard page | - usually 1 page 894 // | |/ 895 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size() 896 // | |\ 897 // | Normal Stack | - 898 // | |/ 899 // P2 +------------------------+ Thread::stack_base() 900 // 901 // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from 902 // pthread_attr_getstack() 903 904 static void current_stack_region(address * bottom, size_t * size) { 905 #ifdef __APPLE__ 906 pthread_t self = pthread_self(); 907 void *stacktop = pthread_get_stackaddr_np(self); 908 *size = pthread_get_stacksize_np(self); 909 // workaround for OS X 10.9.0 (Mavericks) 910 // pthread_get_stacksize_np returns 128 pages even though the actual size is 2048 pages 911 if (pthread_main_np() == 1) { 912 if ((*size) < (DEFAULT_MAIN_THREAD_STACK_PAGES * (size_t)getpagesize())) { 913 char kern_osrelease[256]; 914 size_t kern_osrelease_size = sizeof(kern_osrelease); 915 int ret = sysctlbyname("kern.osrelease", kern_osrelease, &kern_osrelease_size, NULL, 0); 916 if (ret == 0) { 917 // get the major number, atoi will ignore the minor amd micro portions of the version string 918 if (atoi(kern_osrelease) >= OS_X_10_9_0_KERNEL_MAJOR_VERSION) { 919 *size = (DEFAULT_MAIN_THREAD_STACK_PAGES*getpagesize()); 920 } 921 } 922 } 923 } 924 *bottom = (address) stacktop - *size; 925 #elif defined(__OpenBSD__) 926 stack_t ss; 927 int rslt = pthread_stackseg_np(pthread_self(), &ss); 928 929 if (rslt != 0) 930 fatal("pthread_stackseg_np failed with err = %d", rslt); 931 932 *bottom = (address)((char *)ss.ss_sp - ss.ss_size); 933 *size = ss.ss_size; 934 #else 935 pthread_attr_t attr; 936 937 int rslt = pthread_attr_init(&attr); 938 939 // JVM needs to know exact stack location, abort if it fails 940 if (rslt != 0) 941 fatal("pthread_attr_init failed with err = %d", rslt); 942 943 rslt = pthread_attr_get_np(pthread_self(), &attr); 944 945 if (rslt != 0) 946 fatal("pthread_attr_get_np failed with err = %d", rslt); 947 948 if (pthread_attr_getstackaddr(&attr, (void **)bottom) != 0 || 949 pthread_attr_getstacksize(&attr, size) != 0) { 950 fatal("Can not locate current stack attributes!"); 951 } 952 953 pthread_attr_destroy(&attr); 954 #endif 955 assert(os::current_stack_pointer() >= *bottom && 956 os::current_stack_pointer() < *bottom + *size, "just checking"); 957 } 958 959 address os::current_stack_base() { 960 address bottom; 961 size_t size; 962 current_stack_region(&bottom, &size); 963 return (bottom + size); 964 } 965 966 size_t os::current_stack_size() { 967 // stack size includes normal stack and HotSpot guard pages 968 address bottom; 969 size_t size; 970 current_stack_region(&bottom, &size); 971 return size; 972 } 973 974 ///////////////////////////////////////////////////////////////////////////// 975 // helper functions for fatal error handler 976 977 void os::print_context(outputStream *st, const void *context) { 978 if (context == NULL) return; 979 980 const ucontext_t *uc = (const ucontext_t*)context; 981 st->print_cr("Registers:"); 982 #ifdef AMD64 983 st->print( "RAX=" INTPTR_FORMAT, uc->context_rax); 984 st->print(", RBX=" INTPTR_FORMAT, uc->context_rbx); 985 st->print(", RCX=" INTPTR_FORMAT, uc->context_rcx); 986 st->print(", RDX=" INTPTR_FORMAT, uc->context_rdx); 987 st->cr(); 988 st->print( "RSP=" INTPTR_FORMAT, uc->context_rsp); 989 st->print(", RBP=" INTPTR_FORMAT, uc->context_rbp); 990 st->print(", RSI=" INTPTR_FORMAT, uc->context_rsi); 991 st->print(", RDI=" INTPTR_FORMAT, uc->context_rdi); 992 st->cr(); 993 st->print( "R8 =" INTPTR_FORMAT, uc->context_r8); 994 st->print(", R9 =" INTPTR_FORMAT, uc->context_r9); 995 st->print(", R10=" INTPTR_FORMAT, uc->context_r10); 996 st->print(", R11=" INTPTR_FORMAT, uc->context_r11); 997 st->cr(); 998 st->print( "R12=" INTPTR_FORMAT, uc->context_r12); 999 st->print(", R13=" INTPTR_FORMAT, uc->context_r13); 1000 st->print(", R14=" INTPTR_FORMAT, uc->context_r14); 1001 st->print(", R15=" INTPTR_FORMAT, uc->context_r15); 1002 st->cr(); 1003 st->print( "RIP=" INTPTR_FORMAT, uc->context_rip); 1004 st->print(", EFLAGS=" INTPTR_FORMAT, uc->context_flags); 1005 st->print(", ERR=" INTPTR_FORMAT, uc->context_err); 1006 st->cr(); 1007 st->print(" TRAPNO=" INTPTR_FORMAT, uc->context_trapno); 1008 #else 1009 st->print( "EAX=" INTPTR_FORMAT, uc->context_eax); 1010 st->print(", EBX=" INTPTR_FORMAT, uc->context_ebx); 1011 st->print(", ECX=" INTPTR_FORMAT, uc->context_ecx); 1012 st->print(", EDX=" INTPTR_FORMAT, uc->context_edx); 1013 st->cr(); 1014 st->print( "ESP=" INTPTR_FORMAT, uc->context_esp); 1015 st->print(", EBP=" INTPTR_FORMAT, uc->context_ebp); 1016 st->print(", ESI=" INTPTR_FORMAT, uc->context_esi); 1017 st->print(", EDI=" INTPTR_FORMAT, uc->context_edi); 1018 st->cr(); 1019 st->print( "EIP=" INTPTR_FORMAT, uc->context_eip); 1020 st->print(", EFLAGS=" INTPTR_FORMAT, uc->context_eflags); 1021 #endif // AMD64 1022 st->cr(); 1023 st->cr(); 1024 1025 intptr_t *sp = (intptr_t *)os::Bsd::ucontext_get_sp(uc); 1026 st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp); 1027 print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t)); 1028 st->cr(); 1029 1030 // Note: it may be unsafe to inspect memory near pc. For example, pc may 1031 // point to garbage if entry point in an nmethod is corrupted. Leave 1032 // this at the end, and hope for the best. 1033 address pc = os::Bsd::ucontext_get_pc(uc); 1034 st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc); 1035 print_hex_dump(st, pc - 32, pc + 32, sizeof(char)); 1036 } 1037 1038 void os::print_register_info(outputStream *st, const void *context) { 1039 if (context == NULL) return; 1040 1041 const ucontext_t *uc = (const ucontext_t*)context; 1042 1043 st->print_cr("Register to memory mapping:"); 1044 st->cr(); 1045 1046 // this is horrendously verbose but the layout of the registers in the 1047 // context does not match how we defined our abstract Register set, so 1048 // we can't just iterate through the gregs area 1049 1050 // this is only for the "general purpose" registers 1051 1052 #ifdef AMD64 1053 st->print("RAX="); print_location(st, uc->context_rax); 1054 st->print("RBX="); print_location(st, uc->context_rbx); 1055 st->print("RCX="); print_location(st, uc->context_rcx); 1056 st->print("RDX="); print_location(st, uc->context_rdx); 1057 st->print("RSP="); print_location(st, uc->context_rsp); 1058 st->print("RBP="); print_location(st, uc->context_rbp); 1059 st->print("RSI="); print_location(st, uc->context_rsi); 1060 st->print("RDI="); print_location(st, uc->context_rdi); 1061 st->print("R8 ="); print_location(st, uc->context_r8); 1062 st->print("R9 ="); print_location(st, uc->context_r9); 1063 st->print("R10="); print_location(st, uc->context_r10); 1064 st->print("R11="); print_location(st, uc->context_r11); 1065 st->print("R12="); print_location(st, uc->context_r12); 1066 st->print("R13="); print_location(st, uc->context_r13); 1067 st->print("R14="); print_location(st, uc->context_r14); 1068 st->print("R15="); print_location(st, uc->context_r15); 1069 #else 1070 st->print("EAX="); print_location(st, uc->context_eax); 1071 st->print("EBX="); print_location(st, uc->context_ebx); 1072 st->print("ECX="); print_location(st, uc->context_ecx); 1073 st->print("EDX="); print_location(st, uc->context_edx); 1074 st->print("ESP="); print_location(st, uc->context_esp); 1075 st->print("EBP="); print_location(st, uc->context_ebp); 1076 st->print("ESI="); print_location(st, uc->context_esi); 1077 st->print("EDI="); print_location(st, uc->context_edi); 1078 #endif // AMD64 1079 1080 st->cr(); 1081 } 1082 1083 void os::setup_fpu() { 1084 #ifndef AMD64 1085 address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std(); 1086 __asm__ volatile ( "fldcw (%0)" : 1087 : "r" (fpu_cntrl) : "memory"); 1088 #endif // !AMD64 1089 } 1090 1091 #ifndef PRODUCT 1092 void os::verify_stack_alignment() { 1093 } 1094 #endif 1095 1096 int os::extra_bang_size_in_bytes() { 1097 // JDK-8050147 requires the full cache line bang for x86. 1098 return VM_Version::L1_line_size(); 1099 }