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