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