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