1 /* 2 * Copyright (c) 1999, 2010, 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 "assembler_x86.inline.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_solaris.h" 34 #include "memory/allocation.inline.hpp" 35 #include "mutex_solaris.inline.hpp" 36 #include "nativeInst_x86.hpp" 37 #include "os_share_solaris.hpp" 38 #include "prims/jniFastGetField.hpp" 39 #include "prims/jvm.h" 40 #include "prims/jvm_misc.hpp" 41 #include "runtime/arguments.hpp" 42 #include "runtime/extendedPC.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/hpi.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/timer.hpp" 53 #include "thread_solaris.inline.hpp" 54 #include "utilities/events.hpp" 55 #include "utilities/vmError.hpp" 56 #ifdef COMPILER1 57 #include "c1/c1_Runtime1.hpp" 58 #endif 59 #ifdef COMPILER2 60 #include "opto/runtime.hpp" 61 #endif 62 63 // put OS-includes here 64 # include <sys/types.h> 65 # include <sys/mman.h> 66 # include <pthread.h> 67 # include <signal.h> 68 # include <setjmp.h> 69 # include <errno.h> 70 # include <dlfcn.h> 71 # include <stdio.h> 72 # include <unistd.h> 73 # include <sys/resource.h> 74 # include <thread.h> 75 # include <sys/stat.h> 76 # include <sys/time.h> 77 # include <sys/filio.h> 78 # include <sys/utsname.h> 79 # include <sys/systeminfo.h> 80 # include <sys/socket.h> 81 # include <sys/trap.h> 82 # include <sys/lwp.h> 83 # include <pwd.h> 84 # include <poll.h> 85 # include <sys/lwp.h> 86 # include <procfs.h> // see comment in <sys/procfs.h> 87 88 #ifndef AMD64 89 // QQQ seems useless at this point 90 # define _STRUCTURED_PROC 1 // this gets us the new structured proc interfaces of 5.6 & later 91 #endif // AMD64 92 # include <sys/procfs.h> // see comment in <sys/procfs.h> 93 94 95 #define MAX_PATH (2 * K) 96 97 // Minimum stack size for the VM. It's easier to document a constant value 98 // but it's different for x86 and sparc because the page sizes are different. 99 #ifdef AMD64 100 size_t os::Solaris::min_stack_allowed = 224*K; 101 #define REG_SP REG_RSP 102 #define REG_PC REG_RIP 103 #define REG_FP REG_RBP 104 #else 105 size_t os::Solaris::min_stack_allowed = 64*K; 106 #define REG_SP UESP 107 #define REG_PC EIP 108 #define REG_FP EBP 109 // 4900493 counter to prevent runaway LDTR refresh attempt 110 111 static volatile int ldtr_refresh = 0; 112 // the libthread instruction that faults because of the stale LDTR 113 114 static const unsigned char movlfs[] = { 0x8e, 0xe0 // movl %eax,%fs 115 }; 116 #endif // AMD64 117 118 char* os::non_memory_address_word() { 119 // Must never look like an address returned by reserve_memory, 120 // even in its subfields (as defined by the CPU immediate fields, 121 // if the CPU splits constants across multiple instructions). 122 return (char*) -1; 123 } 124 125 // 126 // Validate a ucontext retrieved from walking a uc_link of a ucontext. 127 // There are issues with libthread giving out uc_links for different threads 128 // on the same uc_link chain and bad or circular links. 129 // 130 bool os::Solaris::valid_ucontext(Thread* thread, ucontext_t* valid, ucontext_t* suspect) { 131 if (valid >= suspect || 132 valid->uc_stack.ss_flags != suspect->uc_stack.ss_flags || 133 valid->uc_stack.ss_sp != suspect->uc_stack.ss_sp || 134 valid->uc_stack.ss_size != suspect->uc_stack.ss_size) { 135 DEBUG_ONLY(tty->print_cr("valid_ucontext: failed test 1");) 136 return false; 137 } 138 139 if (thread->is_Java_thread()) { 140 if (!valid_stack_address(thread, (address)suspect)) { 141 DEBUG_ONLY(tty->print_cr("valid_ucontext: uc_link not in thread stack");) 142 return false; 143 } 144 if (!valid_stack_address(thread, (address) suspect->uc_mcontext.gregs[REG_SP])) { 145 DEBUG_ONLY(tty->print_cr("valid_ucontext: stackpointer not in thread stack");) 146 return false; 147 } 148 } 149 return true; 150 } 151 152 // We will only follow one level of uc_link since there are libthread 153 // issues with ucontext linking and it is better to be safe and just 154 // let caller retry later. 155 ucontext_t* os::Solaris::get_valid_uc_in_signal_handler(Thread *thread, 156 ucontext_t *uc) { 157 158 ucontext_t *retuc = NULL; 159 160 if (uc != NULL) { 161 if (uc->uc_link == NULL) { 162 // cannot validate without uc_link so accept current ucontext 163 retuc = uc; 164 } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) { 165 // first ucontext is valid so try the next one 166 uc = uc->uc_link; 167 if (uc->uc_link == NULL) { 168 // cannot validate without uc_link so accept current ucontext 169 retuc = uc; 170 } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) { 171 // the ucontext one level down is also valid so return it 172 retuc = uc; 173 } 174 } 175 } 176 return retuc; 177 } 178 179 // Assumes ucontext is valid 180 ExtendedPC os::Solaris::ucontext_get_ExtendedPC(ucontext_t *uc) { 181 return ExtendedPC((address)uc->uc_mcontext.gregs[REG_PC]); 182 } 183 184 // Assumes ucontext is valid 185 intptr_t* os::Solaris::ucontext_get_sp(ucontext_t *uc) { 186 return (intptr_t*)uc->uc_mcontext.gregs[REG_SP]; 187 } 188 189 // Assumes ucontext is valid 190 intptr_t* os::Solaris::ucontext_get_fp(ucontext_t *uc) { 191 return (intptr_t*)uc->uc_mcontext.gregs[REG_FP]; 192 } 193 194 // For Forte Analyzer AsyncGetCallTrace profiling support - thread 195 // is currently interrupted by SIGPROF. 196 // 197 // The difference between this and os::fetch_frame_from_context() is that 198 // here we try to skip nested signal frames. 199 ExtendedPC os::Solaris::fetch_frame_from_ucontext(Thread* thread, 200 ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) { 201 202 assert(thread != NULL, "just checking"); 203 assert(ret_sp != NULL, "just checking"); 204 assert(ret_fp != NULL, "just checking"); 205 206 ucontext_t *luc = os::Solaris::get_valid_uc_in_signal_handler(thread, uc); 207 return os::fetch_frame_from_context(luc, ret_sp, ret_fp); 208 } 209 210 ExtendedPC os::fetch_frame_from_context(void* ucVoid, 211 intptr_t** ret_sp, intptr_t** ret_fp) { 212 213 ExtendedPC epc; 214 ucontext_t *uc = (ucontext_t*)ucVoid; 215 216 if (uc != NULL) { 217 epc = os::Solaris::ucontext_get_ExtendedPC(uc); 218 if (ret_sp) *ret_sp = os::Solaris::ucontext_get_sp(uc); 219 if (ret_fp) *ret_fp = os::Solaris::ucontext_get_fp(uc); 220 } else { 221 // construct empty ExtendedPC for return value checking 222 epc = ExtendedPC(NULL); 223 if (ret_sp) *ret_sp = (intptr_t *)NULL; 224 if (ret_fp) *ret_fp = (intptr_t *)NULL; 225 } 226 227 return epc; 228 } 229 230 frame os::fetch_frame_from_context(void* ucVoid) { 231 intptr_t* sp; 232 intptr_t* fp; 233 ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp); 234 return frame(sp, fp, epc.pc()); 235 } 236 237 frame os::get_sender_for_C_frame(frame* fr) { 238 return frame(fr->sender_sp(), fr->link(), fr->sender_pc()); 239 } 240 241 extern "C" intptr_t *_get_current_fp(); // in .il file 242 243 frame os::current_frame() { 244 intptr_t* fp = _get_current_fp(); // it's inlined so want current fp 245 frame myframe((intptr_t*)os::current_stack_pointer(), 246 (intptr_t*)fp, 247 CAST_FROM_FN_PTR(address, os::current_frame)); 248 if (os::is_first_C_frame(&myframe)) { 249 // stack is not walkable 250 frame ret; // This will be a null useless frame 251 return ret; 252 } else { 253 return os::get_sender_for_C_frame(&myframe); 254 } 255 } 256 257 // This is a simple callback that just fetches a PC for an interrupted thread. 258 // The thread need not be suspended and the fetched PC is just a hint. 259 // This one is currently used for profiling the VMThread ONLY! 260 261 // Must be synchronous 262 void GetThreadPC_Callback::execute(OSThread::InterruptArguments *args) { 263 Thread* thread = args->thread(); 264 ucontext_t* uc = args->ucontext(); 265 intptr_t* sp; 266 267 assert(ProfileVM && thread->is_VM_thread(), "just checking"); 268 269 ExtendedPC new_addr((address)uc->uc_mcontext.gregs[REG_PC]); 270 _addr = new_addr; 271 } 272 273 static int threadgetstate(thread_t tid, int *flags, lwpid_t *lwp, stack_t *ss, gregset_t rs, lwpstatus_t *lwpstatus) { 274 char lwpstatusfile[PROCFILE_LENGTH]; 275 int lwpfd, err; 276 277 if (err = os::Solaris::thr_getstate(tid, flags, lwp, ss, rs)) 278 return (err); 279 if (*flags == TRS_LWPID) { 280 sprintf(lwpstatusfile, "/proc/%d/lwp/%d/lwpstatus", getpid(), 281 *lwp); 282 if ((lwpfd = open(lwpstatusfile, O_RDONLY)) < 0) { 283 perror("thr_mutator_status: open lwpstatus"); 284 return (EINVAL); 285 } 286 if (pread(lwpfd, lwpstatus, sizeof (lwpstatus_t), (off_t)0) != 287 sizeof (lwpstatus_t)) { 288 perror("thr_mutator_status: read lwpstatus"); 289 (void) close(lwpfd); 290 return (EINVAL); 291 } 292 (void) close(lwpfd); 293 } 294 return (0); 295 } 296 297 #ifndef AMD64 298 299 // Detecting SSE support by OS 300 // From solaris_i486.s 301 extern "C" bool sse_check(); 302 extern "C" bool sse_unavailable(); 303 304 enum { SSE_UNKNOWN, SSE_NOT_SUPPORTED, SSE_SUPPORTED}; 305 static int sse_status = SSE_UNKNOWN; 306 307 308 static void check_for_sse_support() { 309 if (!VM_Version::supports_sse()) { 310 sse_status = SSE_NOT_SUPPORTED; 311 return; 312 } 313 // looking for _sse_hw in libc.so, if it does not exist or 314 // the value (int) is 0, OS has no support for SSE 315 int *sse_hwp; 316 void *h; 317 318 if ((h=dlopen("/usr/lib/libc.so", RTLD_LAZY)) == NULL) { 319 //open failed, presume no support for SSE 320 sse_status = SSE_NOT_SUPPORTED; 321 return; 322 } 323 if ((sse_hwp = (int *)dlsym(h, "_sse_hw")) == NULL) { 324 sse_status = SSE_NOT_SUPPORTED; 325 } else if (*sse_hwp == 0) { 326 sse_status = SSE_NOT_SUPPORTED; 327 } 328 dlclose(h); 329 330 if (sse_status == SSE_UNKNOWN) { 331 bool (*try_sse)() = (bool (*)())sse_check; 332 sse_status = (*try_sse)() ? SSE_SUPPORTED : SSE_NOT_SUPPORTED; 333 } 334 335 } 336 337 #endif // AMD64 338 339 bool os::supports_sse() { 340 #ifdef AMD64 341 return true; 342 #else 343 if (sse_status == SSE_UNKNOWN) 344 check_for_sse_support(); 345 return sse_status == SSE_SUPPORTED; 346 #endif // AMD64 347 } 348 349 bool os::is_allocatable(size_t bytes) { 350 #ifdef AMD64 351 return true; 352 #else 353 354 if (bytes < 2 * G) { 355 return true; 356 } 357 358 char* addr = reserve_memory(bytes, NULL); 359 360 if (addr != NULL) { 361 release_memory(addr, bytes); 362 } 363 364 return addr != NULL; 365 #endif // AMD64 366 367 } 368 369 extern "C" int JVM_handle_solaris_signal(int signo, siginfo_t* siginfo, void* ucontext, int abort_if_unrecognized); 370 371 extern "C" void Fetch32PFI () ; 372 extern "C" void Fetch32Resume () ; 373 #ifdef AMD64 374 extern "C" void FetchNPFI () ; 375 extern "C" void FetchNResume () ; 376 #endif // AMD64 377 378 int JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid, int abort_if_unrecognized) { 379 ucontext_t* uc = (ucontext_t*) ucVoid; 380 381 #ifndef AMD64 382 if (sig == SIGILL && info->si_addr == (caddr_t)sse_check) { 383 // the SSE instruction faulted. supports_sse() need return false. 384 uc->uc_mcontext.gregs[EIP] = (greg_t)sse_unavailable; 385 return true; 386 } 387 #endif // !AMD64 388 389 Thread* t = ThreadLocalStorage::get_thread_slow(); // slow & steady 390 391 SignalHandlerMark shm(t); 392 393 if(sig == SIGPIPE || sig == SIGXFSZ) { 394 if (os::Solaris::chained_handler(sig, info, ucVoid)) { 395 return true; 396 } else { 397 if (PrintMiscellaneous && (WizardMode || Verbose)) { 398 char buf[64]; 399 warning("Ignoring %s - see 4229104 or 6499219", 400 os::exception_name(sig, buf, sizeof(buf))); 401 402 } 403 return true; 404 } 405 } 406 407 JavaThread* thread = NULL; 408 VMThread* vmthread = NULL; 409 410 if (os::Solaris::signal_handlers_are_installed) { 411 if (t != NULL ){ 412 if(t->is_Java_thread()) { 413 thread = (JavaThread*)t; 414 } 415 else if(t->is_VM_thread()){ 416 vmthread = (VMThread *)t; 417 } 418 } 419 } 420 421 guarantee(sig != os::Solaris::SIGinterrupt(), "Can not chain VM interrupt signal, try -XX:+UseAltSigs"); 422 423 if (sig == os::Solaris::SIGasync()) { 424 if(thread){ 425 OSThread::InterruptArguments args(thread, uc); 426 thread->osthread()->do_interrupt_callbacks_at_interrupt(&args); 427 return true; 428 } 429 else if(vmthread){ 430 OSThread::InterruptArguments args(vmthread, uc); 431 vmthread->osthread()->do_interrupt_callbacks_at_interrupt(&args); 432 return true; 433 } else if (os::Solaris::chained_handler(sig, info, ucVoid)) { 434 return true; 435 } else { 436 // If os::Solaris::SIGasync not chained, and this is a non-vm and 437 // non-java thread 438 return true; 439 } 440 } 441 442 if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) { 443 // can't decode this kind of signal 444 info = NULL; 445 } else { 446 assert(sig == info->si_signo, "bad siginfo"); 447 } 448 449 // decide if this trap can be handled by a stub 450 address stub = NULL; 451 452 address pc = NULL; 453 454 //%note os_trap_1 455 if (info != NULL && uc != NULL && thread != NULL) { 456 // factor me: getPCfromContext 457 pc = (address) uc->uc_mcontext.gregs[REG_PC]; 458 459 // SafeFetch32() support 460 if (pc == (address) Fetch32PFI) { 461 uc->uc_mcontext.gregs[REG_PC] = intptr_t(Fetch32Resume) ; 462 return true ; 463 } 464 #ifdef AMD64 465 if (pc == (address) FetchNPFI) { 466 uc->uc_mcontext.gregs [REG_PC] = intptr_t(FetchNResume) ; 467 return true ; 468 } 469 #endif // AMD64 470 471 // Handle ALL stack overflow variations here 472 if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) { 473 address addr = (address) info->si_addr; 474 if (thread->in_stack_yellow_zone(addr)) { 475 thread->disable_stack_yellow_zone(); 476 if (thread->thread_state() == _thread_in_Java) { 477 // Throw a stack overflow exception. Guard pages will be reenabled 478 // while unwinding the stack. 479 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW); 480 } else { 481 // Thread was in the vm or native code. Return and try to finish. 482 return true; 483 } 484 } else if (thread->in_stack_red_zone(addr)) { 485 // Fatal red zone violation. Disable the guard pages and fall through 486 // to handle_unexpected_exception way down below. 487 thread->disable_stack_red_zone(); 488 tty->print_raw_cr("An irrecoverable stack overflow has occurred."); 489 } 490 } 491 492 if (thread->thread_state() == _thread_in_vm) { 493 if (sig == SIGBUS && info->si_code == BUS_OBJERR && thread->doing_unsafe_access()) { 494 stub = StubRoutines::handler_for_unsafe_access(); 495 } 496 } 497 498 if (thread->thread_state() == _thread_in_Java) { 499 // Support Safepoint Polling 500 if ( sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) { 501 stub = SharedRuntime::get_poll_stub(pc); 502 } 503 else if (sig == SIGBUS && info->si_code == BUS_OBJERR) { 504 // BugId 4454115: A read from a MappedByteBuffer can fault 505 // here if the underlying file has been truncated. 506 // Do not crash the VM in such a case. 507 CodeBlob* cb = CodeCache::find_blob_unsafe(pc); 508 nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL; 509 if (nm != NULL && nm->has_unsafe_access()) { 510 stub = StubRoutines::handler_for_unsafe_access(); 511 } 512 } 513 else 514 if (sig == SIGFPE && info->si_code == FPE_INTDIV) { 515 // integer divide by zero 516 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO); 517 } 518 #ifndef AMD64 519 else if (sig == SIGFPE && info->si_code == FPE_FLTDIV) { 520 // floating-point divide by zero 521 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO); 522 } 523 else if (sig == SIGFPE && info->si_code == FPE_FLTINV) { 524 // The encoding of D2I in i486.ad can cause an exception prior 525 // to the fist instruction if there was an invalid operation 526 // pending. We want to dismiss that exception. From the win_32 527 // side it also seems that if it really was the fist causing 528 // the exception that we do the d2i by hand with different 529 // rounding. Seems kind of weird. QQQ TODO 530 // Note that we take the exception at the NEXT floating point instruction. 531 if (pc[0] == 0xDB) { 532 assert(pc[0] == 0xDB, "not a FIST opcode"); 533 assert(pc[1] == 0x14, "not a FIST opcode"); 534 assert(pc[2] == 0x24, "not a FIST opcode"); 535 return true; 536 } else { 537 assert(pc[-3] == 0xDB, "not an flt invalid opcode"); 538 assert(pc[-2] == 0x14, "not an flt invalid opcode"); 539 assert(pc[-1] == 0x24, "not an flt invalid opcode"); 540 } 541 } 542 else if (sig == SIGFPE ) { 543 tty->print_cr("caught SIGFPE, info 0x%x.", info->si_code); 544 } 545 #endif // !AMD64 546 547 // QQQ It doesn't seem that we need to do this on x86 because we should be able 548 // to return properly from the handler without this extra stuff on the back side. 549 550 else if (sig == SIGSEGV && info->si_code > 0 && !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) { 551 // Determination of interpreter/vtable stub/compiled code null exception 552 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL); 553 } 554 } 555 556 // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in 557 // and the heap gets shrunk before the field access. 558 if ((sig == SIGSEGV) || (sig == SIGBUS)) { 559 address addr = JNI_FastGetField::find_slowcase_pc(pc); 560 if (addr != (address)-1) { 561 stub = addr; 562 } 563 } 564 565 // Check to see if we caught the safepoint code in the 566 // process of write protecting the memory serialization page. 567 // It write enables the page immediately after protecting it 568 // so we can just return to retry the write. 569 if ((sig == SIGSEGV) && 570 os::is_memory_serialize_page(thread, (address)info->si_addr)) { 571 // Block current thread until the memory serialize page permission restored. 572 os::block_on_serialize_page_trap(); 573 return true; 574 } 575 } 576 577 // Execution protection violation 578 // 579 // Preventative code for future versions of Solaris which may 580 // enable execution protection when running the 32-bit VM on AMD64. 581 // 582 // This should be kept as the last step in the triage. We don't 583 // have a dedicated trap number for a no-execute fault, so be 584 // conservative and allow other handlers the first shot. 585 // 586 // Note: We don't test that info->si_code == SEGV_ACCERR here. 587 // this si_code is so generic that it is almost meaningless; and 588 // the si_code for this condition may change in the future. 589 // Furthermore, a false-positive should be harmless. 590 if (UnguardOnExecutionViolation > 0 && 591 (sig == SIGSEGV || sig == SIGBUS) && 592 uc->uc_mcontext.gregs[TRAPNO] == T_PGFLT) { // page fault 593 int page_size = os::vm_page_size(); 594 address addr = (address) info->si_addr; 595 address pc = (address) uc->uc_mcontext.gregs[REG_PC]; 596 // Make sure the pc and the faulting address are sane. 597 // 598 // If an instruction spans a page boundary, and the page containing 599 // the beginning of the instruction is executable but the following 600 // page is not, the pc and the faulting address might be slightly 601 // different - we still want to unguard the 2nd page in this case. 602 // 603 // 15 bytes seems to be a (very) safe value for max instruction size. 604 bool pc_is_near_addr = 605 (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15); 606 bool instr_spans_page_boundary = 607 (align_size_down((intptr_t) pc ^ (intptr_t) addr, 608 (intptr_t) page_size) > 0); 609 610 if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) { 611 static volatile address last_addr = 612 (address) os::non_memory_address_word(); 613 614 // In conservative mode, don't unguard unless the address is in the VM 615 if (addr != last_addr && 616 (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) { 617 618 // Make memory rwx and retry 619 address page_start = 620 (address) align_size_down((intptr_t) addr, (intptr_t) page_size); 621 bool res = os::protect_memory((char*) page_start, page_size, 622 os::MEM_PROT_RWX); 623 624 if (PrintMiscellaneous && Verbose) { 625 char buf[256]; 626 jio_snprintf(buf, sizeof(buf), "Execution protection violation " 627 "at " INTPTR_FORMAT 628 ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr, 629 page_start, (res ? "success" : "failed"), errno); 630 tty->print_raw_cr(buf); 631 } 632 stub = pc; 633 634 // Set last_addr so if we fault again at the same address, we don't end 635 // up in an endless loop. 636 // 637 // There are two potential complications here. Two threads trapping at 638 // the same address at the same time could cause one of the threads to 639 // think it already unguarded, and abort the VM. Likely very rare. 640 // 641 // The other race involves two threads alternately trapping at 642 // different addresses and failing to unguard the page, resulting in 643 // an endless loop. This condition is probably even more unlikely than 644 // the first. 645 // 646 // Although both cases could be avoided by using locks or thread local 647 // last_addr, these solutions are unnecessary complication: this 648 // handler is a best-effort safety net, not a complete solution. It is 649 // disabled by default and should only be used as a workaround in case 650 // we missed any no-execute-unsafe VM code. 651 652 last_addr = addr; 653 } 654 } 655 } 656 657 if (stub != NULL) { 658 // save all thread context in case we need to restore it 659 660 if (thread != NULL) thread->set_saved_exception_pc(pc); 661 // 12/02/99: On Sparc it appears that the full context is also saved 662 // but as yet, no one looks at or restores that saved context 663 // factor me: setPC 664 uc->uc_mcontext.gregs[REG_PC] = (greg_t)stub; 665 return true; 666 } 667 668 // signal-chaining 669 if (os::Solaris::chained_handler(sig, info, ucVoid)) { 670 return true; 671 } 672 673 #ifndef AMD64 674 // Workaround (bug 4900493) for Solaris kernel bug 4966651. 675 // Handle an undefined selector caused by an attempt to assign 676 // fs in libthread getipriptr(). With the current libthread design every 512 677 // thread creations the LDT for a private thread data structure is extended 678 // and thre is a hazard that and another thread attempting a thread creation 679 // will use a stale LDTR that doesn't reflect the structure's growth, 680 // causing a GP fault. 681 // Enforce the probable limit of passes through here to guard against an 682 // infinite loop if some other move to fs caused the GP fault. Note that 683 // this loop counter is ultimately a heuristic as it is possible for 684 // more than one thread to generate this fault at a time in an MP system. 685 // In the case of the loop count being exceeded or if the poll fails 686 // just fall through to a fatal error. 687 // If there is some other source of T_GPFLT traps and the text at EIP is 688 // unreadable this code will loop infinitely until the stack is exausted. 689 // The key to diagnosis in this case is to look for the bottom signal handler 690 // frame. 691 692 if(! IgnoreLibthreadGPFault) { 693 if (sig == SIGSEGV && uc->uc_mcontext.gregs[TRAPNO] == T_GPFLT) { 694 const unsigned char *p = 695 (unsigned const char *) uc->uc_mcontext.gregs[EIP]; 696 697 // Expected instruction? 698 699 if(p[0] == movlfs[0] && p[1] == movlfs[1]) { 700 701 Atomic::inc(&ldtr_refresh); 702 703 // Infinite loop? 704 705 if(ldtr_refresh < ((2 << 16) / PAGESIZE)) { 706 707 // No, force scheduling to get a fresh view of the LDTR 708 709 if(poll(NULL, 0, 10) == 0) { 710 711 // Retry the move 712 713 return false; 714 } 715 } 716 } 717 } 718 } 719 #endif // !AMD64 720 721 if (!abort_if_unrecognized) { 722 // caller wants another chance, so give it to him 723 return false; 724 } 725 726 if (!os::Solaris::libjsig_is_loaded) { 727 struct sigaction oldAct; 728 sigaction(sig, (struct sigaction *)0, &oldAct); 729 if (oldAct.sa_sigaction != signalHandler) { 730 void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 731 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 732 warning("Unexpected Signal %d occurred under user-defined signal handler %#lx", sig, (long)sighand); 733 } 734 } 735 736 if (pc == NULL && uc != NULL) { 737 pc = (address) uc->uc_mcontext.gregs[REG_PC]; 738 } 739 740 // unmask current signal 741 sigset_t newset; 742 sigemptyset(&newset); 743 sigaddset(&newset, sig); 744 sigprocmask(SIG_UNBLOCK, &newset, NULL); 745 746 VMError err(t, sig, pc, info, ucVoid); 747 err.report_and_die(); 748 749 ShouldNotReachHere(); 750 } 751 752 void os::print_context(outputStream *st, void *context) { 753 if (context == NULL) return; 754 755 ucontext_t *uc = (ucontext_t*)context; 756 st->print_cr("Registers:"); 757 758 // this is horrendously verbose but the layout of the registers in the 759 // context does not match how we defined our abstract Register set, so 760 // we can't just iterate through the gregs area 761 762 #ifdef AMD64 763 st->print( "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]); 764 st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]); 765 st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]); 766 st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]); 767 st->cr(); 768 st->print( "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]); 769 st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]); 770 st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]); 771 st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]); 772 st->cr(); 773 st->print( "R8=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]); 774 st->print(", R9=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]); 775 st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]); 776 st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]); 777 st->cr(); 778 st->print( "R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]); 779 st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]); 780 st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]); 781 st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]); 782 st->cr(); 783 st->print( "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]); 784 st->print(", RFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RFL]); 785 786 st->cr(); 787 st->cr(); 788 789 st->print_cr("Register to memory mapping:"); 790 st->cr(); 791 792 // this is only for the "general purpose" registers 793 794 st->print_cr("RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]); 795 print_location(st, uc->uc_mcontext.gregs[REG_RAX]); 796 st->cr(); 797 st->print_cr("RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]); 798 print_location(st, uc->uc_mcontext.gregs[REG_RBX]); 799 st->cr(); 800 st->print_cr("RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]); 801 print_location(st, uc->uc_mcontext.gregs[REG_RCX]); 802 st->cr(); 803 st->print_cr("RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]); 804 print_location(st, uc->uc_mcontext.gregs[REG_RDX]); 805 st->cr(); 806 st->print_cr("RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]); 807 print_location(st, uc->uc_mcontext.gregs[REG_RSP]); 808 st->cr(); 809 st->print_cr("RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]); 810 print_location(st, uc->uc_mcontext.gregs[REG_RSP]); 811 st->cr(); 812 st->print_cr("RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]); 813 print_location(st, uc->uc_mcontext.gregs[REG_RSI]); 814 st->cr(); 815 st->print_cr("RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]); 816 print_location(st, uc->uc_mcontext.gregs[REG_RDI]); 817 st->cr(); 818 st->print_cr("R8 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]); 819 print_location(st, uc->uc_mcontext.gregs[REG_R8]); 820 st->cr(); 821 st->print_cr("R9 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]); 822 print_location(st, uc->uc_mcontext.gregs[REG_R9]); 823 st->cr(); 824 st->print_cr("R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]); 825 print_location(st, uc->uc_mcontext.gregs[REG_R10]); 826 st->cr(); 827 st->print_cr("R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]); 828 print_location(st, uc->uc_mcontext.gregs[REG_R11]); 829 st->cr(); 830 st->print_cr("R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]); 831 print_location(st, uc->uc_mcontext.gregs[REG_R12]); 832 st->cr(); 833 st->print_cr("R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]); 834 print_location(st, uc->uc_mcontext.gregs[REG_R13]); 835 st->cr(); 836 st->print_cr("R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]); 837 print_location(st, uc->uc_mcontext.gregs[REG_R14]); 838 st->cr(); 839 st->print_cr("R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]); 840 print_location(st, uc->uc_mcontext.gregs[REG_R15]); 841 842 #else 843 st->print( "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EAX]); 844 st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBX]); 845 st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ECX]); 846 st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDX]); 847 st->cr(); 848 st->print( "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[UESP]); 849 st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBP]); 850 st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ESI]); 851 st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDI]); 852 st->cr(); 853 st->print( "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EIP]); 854 st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EFL]); 855 856 st->cr(); 857 st->cr(); 858 859 st->print_cr("Register to memory mapping:"); 860 st->cr(); 861 862 // this is only for the "general purpose" registers 863 864 st->print_cr("EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EAX]); 865 print_location(st, uc->uc_mcontext.gregs[EAX]); 866 st->cr(); 867 st->print_cr("EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBX]); 868 print_location(st, uc->uc_mcontext.gregs[EBX]); 869 st->cr(); 870 st->print_cr("ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ECX]); 871 print_location(st, uc->uc_mcontext.gregs[ECX]); 872 st->cr(); 873 st->print_cr("EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDX]); 874 print_location(st, uc->uc_mcontext.gregs[EDX]); 875 st->cr(); 876 st->print_cr("ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[UESP]); 877 print_location(st, uc->uc_mcontext.gregs[UESP]); 878 st->cr(); 879 st->print_cr("EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBP]); 880 print_location(st, uc->uc_mcontext.gregs[EBP]); 881 st->cr(); 882 st->print_cr("ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ESI]); 883 print_location(st, uc->uc_mcontext.gregs[ESI]); 884 st->cr(); 885 st->print_cr("EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDI]); 886 print_location(st, uc->uc_mcontext.gregs[EDI]); 887 888 #endif // AMD64 889 st->cr(); 890 st->cr(); 891 892 intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc); 893 st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp); 894 print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t)); 895 st->cr(); 896 897 // Note: it may be unsafe to inspect memory near pc. For example, pc may 898 // point to garbage if entry point in an nmethod is corrupted. Leave 899 // this at the end, and hope for the best. 900 ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc); 901 address pc = epc.pc(); 902 st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc); 903 print_hex_dump(st, pc - 16, pc + 16, sizeof(char)); 904 } 905 906 907 #ifdef AMD64 908 void os::Solaris::init_thread_fpu_state(void) { 909 // Nothing to do 910 } 911 #else 912 // From solaris_i486.s 913 extern "C" void fixcw(); 914 915 void os::Solaris::init_thread_fpu_state(void) { 916 // Set fpu to 53 bit precision. This happens too early to use a stub. 917 fixcw(); 918 } 919 920 // These routines are the initial value of atomic_xchg_entry(), 921 // atomic_cmpxchg_entry(), atomic_inc_entry() and fence_entry() 922 // until initialization is complete. 923 // TODO - replace with .il implementation when compiler supports it. 924 925 typedef jint xchg_func_t (jint, volatile jint*); 926 typedef jint cmpxchg_func_t (jint, volatile jint*, jint); 927 typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong); 928 typedef jint add_func_t (jint, volatile jint*); 929 930 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) { 931 // try to use the stub: 932 xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry()); 933 934 if (func != NULL) { 935 os::atomic_xchg_func = func; 936 return (*func)(exchange_value, dest); 937 } 938 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 939 940 jint old_value = *dest; 941 *dest = exchange_value; 942 return old_value; 943 } 944 945 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) { 946 // try to use the stub: 947 cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry()); 948 949 if (func != NULL) { 950 os::atomic_cmpxchg_func = func; 951 return (*func)(exchange_value, dest, compare_value); 952 } 953 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 954 955 jint old_value = *dest; 956 if (old_value == compare_value) 957 *dest = exchange_value; 958 return old_value; 959 } 960 961 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) { 962 // try to use the stub: 963 cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry()); 964 965 if (func != NULL) { 966 os::atomic_cmpxchg_long_func = func; 967 return (*func)(exchange_value, dest, compare_value); 968 } 969 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 970 971 jlong old_value = *dest; 972 if (old_value == compare_value) 973 *dest = exchange_value; 974 return old_value; 975 } 976 977 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) { 978 // try to use the stub: 979 add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry()); 980 981 if (func != NULL) { 982 os::atomic_add_func = func; 983 return (*func)(add_value, dest); 984 } 985 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 986 987 return (*dest) += add_value; 988 } 989 990 xchg_func_t* os::atomic_xchg_func = os::atomic_xchg_bootstrap; 991 cmpxchg_func_t* os::atomic_cmpxchg_func = os::atomic_cmpxchg_bootstrap; 992 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap; 993 add_func_t* os::atomic_add_func = os::atomic_add_bootstrap; 994 995 extern "C" void _solaris_raw_setup_fpu(address ptr); 996 void os::setup_fpu() { 997 address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std(); 998 _solaris_raw_setup_fpu(fpu_cntrl); 999 } 1000 #endif // AMD64