Path: blob/master/src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp
40930 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// no precompiled headers25#include "jvm.h"26#include "asm/macroAssembler.hpp"27#include "classfile/vmSymbols.hpp"28#include "code/codeCache.hpp"29#include "code/icBuffer.hpp"30#include "code/vtableStubs.hpp"31#include "interpreter/interpreter.hpp"32#include "logging/log.hpp"33#include "memory/allocation.inline.hpp"34#include "os_share_bsd.hpp"35#include "prims/jniFastGetField.hpp"36#include "prims/jvm_misc.hpp"37#include "runtime/arguments.hpp"38#include "runtime/frame.inline.hpp"39#include "runtime/interfaceSupport.inline.hpp"40#include "runtime/java.hpp"41#include "runtime/javaCalls.hpp"42#include "runtime/mutexLocker.hpp"43#include "runtime/osThread.hpp"44#include "runtime/safepointMechanism.hpp"45#include "runtime/sharedRuntime.hpp"46#include "runtime/stubRoutines.hpp"47#include "runtime/thread.inline.hpp"48#include "runtime/timer.hpp"49#include "signals_posix.hpp"50#include "utilities/align.hpp"51#include "utilities/events.hpp"52#include "utilities/vmError.hpp"5354// put OS-includes here55# include <sys/types.h>56# include <sys/mman.h>57# include <pthread.h>58# include <signal.h>59# include <errno.h>60# include <dlfcn.h>61# include <stdlib.h>62# include <stdio.h>63# include <unistd.h>64# include <sys/resource.h>65# include <sys/stat.h>66# include <sys/time.h>67# include <sys/utsname.h>68# include <sys/socket.h>69# include <sys/wait.h>70# include <pwd.h>71# include <poll.h>72#ifndef __OpenBSD__73# include <ucontext.h>74#endif7576#if !defined(__APPLE__) && !defined(__NetBSD__)77# include <pthread_np.h>78#endif7980// needed by current_stack_region() workaround for Mavericks81#if defined(__APPLE__)82# include <errno.h>83# include <sys/types.h>84# include <sys/sysctl.h>85# define DEFAULT_MAIN_THREAD_STACK_PAGES 204886# define OS_X_10_9_0_KERNEL_MAJOR_VERSION 1387#endif8889#ifdef AMD6490#define SPELL_REG_SP "rsp"91#define SPELL_REG_FP "rbp"92#else93#define SPELL_REG_SP "esp"94#define SPELL_REG_FP "ebp"95#endif // AMD649697#ifdef __FreeBSD__98# define context_trapno uc_mcontext.mc_trapno99# ifdef AMD64100# define context_pc uc_mcontext.mc_rip101# define context_sp uc_mcontext.mc_rsp102# define context_fp uc_mcontext.mc_rbp103# define context_rip uc_mcontext.mc_rip104# define context_rsp uc_mcontext.mc_rsp105# define context_rbp uc_mcontext.mc_rbp106# define context_rax uc_mcontext.mc_rax107# define context_rbx uc_mcontext.mc_rbx108# define context_rcx uc_mcontext.mc_rcx109# define context_rdx uc_mcontext.mc_rdx110# define context_rsi uc_mcontext.mc_rsi111# define context_rdi uc_mcontext.mc_rdi112# define context_r8 uc_mcontext.mc_r8113# define context_r9 uc_mcontext.mc_r9114# define context_r10 uc_mcontext.mc_r10115# define context_r11 uc_mcontext.mc_r11116# define context_r12 uc_mcontext.mc_r12117# define context_r13 uc_mcontext.mc_r13118# define context_r14 uc_mcontext.mc_r14119# define context_r15 uc_mcontext.mc_r15120# define context_flags uc_mcontext.mc_flags121# define context_err uc_mcontext.mc_err122# else123# define context_pc uc_mcontext.mc_eip124# define context_sp uc_mcontext.mc_esp125# define context_fp uc_mcontext.mc_ebp126# define context_eip uc_mcontext.mc_eip127# define context_esp uc_mcontext.mc_esp128# define context_eax uc_mcontext.mc_eax129# define context_ebx uc_mcontext.mc_ebx130# define context_ecx uc_mcontext.mc_ecx131# define context_edx uc_mcontext.mc_edx132# define context_ebp uc_mcontext.mc_ebp133# define context_esi uc_mcontext.mc_esi134# define context_edi uc_mcontext.mc_edi135# define context_eflags uc_mcontext.mc_eflags136# define context_trapno uc_mcontext.mc_trapno137# endif138#endif139140#ifdef __APPLE__141# if __DARWIN_UNIX03 && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)142// 10.5 UNIX03 member name prefixes143#define DU3_PREFIX(s, m) __ ## s.__ ## m144# else145#define DU3_PREFIX(s, m) s ## . ## m146# endif147148# ifdef AMD64149# define context_pc context_rip150# define context_sp context_rsp151# define context_fp context_rbp152# define context_rip uc_mcontext->DU3_PREFIX(ss,rip)153# define context_rsp uc_mcontext->DU3_PREFIX(ss,rsp)154# define context_rax uc_mcontext->DU3_PREFIX(ss,rax)155# define context_rbx uc_mcontext->DU3_PREFIX(ss,rbx)156# define context_rcx uc_mcontext->DU3_PREFIX(ss,rcx)157# define context_rdx uc_mcontext->DU3_PREFIX(ss,rdx)158# define context_rbp uc_mcontext->DU3_PREFIX(ss,rbp)159# define context_rsi uc_mcontext->DU3_PREFIX(ss,rsi)160# define context_rdi uc_mcontext->DU3_PREFIX(ss,rdi)161# define context_r8 uc_mcontext->DU3_PREFIX(ss,r8)162# define context_r9 uc_mcontext->DU3_PREFIX(ss,r9)163# define context_r10 uc_mcontext->DU3_PREFIX(ss,r10)164# define context_r11 uc_mcontext->DU3_PREFIX(ss,r11)165# define context_r12 uc_mcontext->DU3_PREFIX(ss,r12)166# define context_r13 uc_mcontext->DU3_PREFIX(ss,r13)167# define context_r14 uc_mcontext->DU3_PREFIX(ss,r14)168# define context_r15 uc_mcontext->DU3_PREFIX(ss,r15)169# define context_flags uc_mcontext->DU3_PREFIX(ss,rflags)170# define context_trapno uc_mcontext->DU3_PREFIX(es,trapno)171# define context_err uc_mcontext->DU3_PREFIX(es,err)172# else173# define context_pc context_eip174# define context_sp context_esp175# define context_fp context_ebp176# define context_eip uc_mcontext->DU3_PREFIX(ss,eip)177# define context_esp uc_mcontext->DU3_PREFIX(ss,esp)178# define context_eax uc_mcontext->DU3_PREFIX(ss,eax)179# define context_ebx uc_mcontext->DU3_PREFIX(ss,ebx)180# define context_ecx uc_mcontext->DU3_PREFIX(ss,ecx)181# define context_edx uc_mcontext->DU3_PREFIX(ss,edx)182# define context_ebp uc_mcontext->DU3_PREFIX(ss,ebp)183# define context_esi uc_mcontext->DU3_PREFIX(ss,esi)184# define context_edi uc_mcontext->DU3_PREFIX(ss,edi)185# define context_eflags uc_mcontext->DU3_PREFIX(ss,eflags)186# define context_trapno uc_mcontext->DU3_PREFIX(es,trapno)187# endif188#endif189190#ifdef __OpenBSD__191# define context_trapno sc_trapno192# ifdef AMD64193# define context_pc sc_rip194# define context_sp sc_rsp195# define context_fp sc_rbp196# define context_rip sc_rip197# define context_rsp sc_rsp198# define context_rbp sc_rbp199# define context_rax sc_rax200# define context_rbx sc_rbx201# define context_rcx sc_rcx202# define context_rdx sc_rdx203# define context_rsi sc_rsi204# define context_rdi sc_rdi205# define context_r8 sc_r8206# define context_r9 sc_r9207# define context_r10 sc_r10208# define context_r11 sc_r11209# define context_r12 sc_r12210# define context_r13 sc_r13211# define context_r14 sc_r14212# define context_r15 sc_r15213# define context_flags sc_rflags214# define context_err sc_err215# else216# define context_pc sc_eip217# define context_sp sc_esp218# define context_fp sc_ebp219# define context_eip sc_eip220# define context_esp sc_esp221# define context_eax sc_eax222# define context_ebx sc_ebx223# define context_ecx sc_ecx224# define context_edx sc_edx225# define context_ebp sc_ebp226# define context_esi sc_esi227# define context_edi sc_edi228# define context_eflags sc_eflags229# define context_trapno sc_trapno230# endif231#endif232233#ifdef __NetBSD__234# define context_trapno uc_mcontext.__gregs[_REG_TRAPNO]235# ifdef AMD64236# define __register_t __greg_t237# define context_pc uc_mcontext.__gregs[_REG_RIP]238# define context_sp uc_mcontext.__gregs[_REG_URSP]239# define context_fp uc_mcontext.__gregs[_REG_RBP]240# define context_rip uc_mcontext.__gregs[_REG_RIP]241# define context_rsp uc_mcontext.__gregs[_REG_URSP]242# define context_rax uc_mcontext.__gregs[_REG_RAX]243# define context_rbx uc_mcontext.__gregs[_REG_RBX]244# define context_rcx uc_mcontext.__gregs[_REG_RCX]245# define context_rdx uc_mcontext.__gregs[_REG_RDX]246# define context_rbp uc_mcontext.__gregs[_REG_RBP]247# define context_rsi uc_mcontext.__gregs[_REG_RSI]248# define context_rdi uc_mcontext.__gregs[_REG_RDI]249# define context_r8 uc_mcontext.__gregs[_REG_R8]250# define context_r9 uc_mcontext.__gregs[_REG_R9]251# define context_r10 uc_mcontext.__gregs[_REG_R10]252# define context_r11 uc_mcontext.__gregs[_REG_R11]253# define context_r12 uc_mcontext.__gregs[_REG_R12]254# define context_r13 uc_mcontext.__gregs[_REG_R13]255# define context_r14 uc_mcontext.__gregs[_REG_R14]256# define context_r15 uc_mcontext.__gregs[_REG_R15]257# define context_flags uc_mcontext.__gregs[_REG_RFL]258# define context_err uc_mcontext.__gregs[_REG_ERR]259# else260# define context_pc uc_mcontext.__gregs[_REG_EIP]261# define context_sp uc_mcontext.__gregs[_REG_UESP]262# define context_fp uc_mcontext.__gregs[_REG_EBP]263# define context_eip uc_mcontext.__gregs[_REG_EIP]264# define context_esp uc_mcontext.__gregs[_REG_UESP]265# define context_eax uc_mcontext.__gregs[_REG_EAX]266# define context_ebx uc_mcontext.__gregs[_REG_EBX]267# define context_ecx uc_mcontext.__gregs[_REG_ECX]268# define context_edx uc_mcontext.__gregs[_REG_EDX]269# define context_ebp uc_mcontext.__gregs[_REG_EBP]270# define context_esi uc_mcontext.__gregs[_REG_ESI]271# define context_edi uc_mcontext.__gregs[_REG_EDI]272# define context_eflags uc_mcontext.__gregs[_REG_EFL]273# define context_trapno uc_mcontext.__gregs[_REG_TRAPNO]274# endif275#endif276277address os::current_stack_pointer() {278#if defined(__clang__) || defined(__llvm__)279void *esp;280__asm__("mov %%" SPELL_REG_SP ", %0":"=r"(esp));281return (address) esp;282#else283register void *esp __asm__ (SPELL_REG_SP);284return (address) esp;285#endif286}287288char* os::non_memory_address_word() {289// Must never look like an address returned by reserve_memory,290// even in its subfields (as defined by the CPU immediate fields,291// if the CPU splits constants across multiple instructions).292293return (char*) -1;294}295296address os::Posix::ucontext_get_pc(const ucontext_t * uc) {297return (address)uc->context_pc;298}299300void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {301uc->context_pc = (intptr_t)pc ;302}303304intptr_t* os::Bsd::ucontext_get_sp(const ucontext_t * uc) {305return (intptr_t*)uc->context_sp;306}307308intptr_t* os::Bsd::ucontext_get_fp(const ucontext_t * uc) {309return (intptr_t*)uc->context_fp;310}311312address os::fetch_frame_from_context(const void* ucVoid,313intptr_t** ret_sp, intptr_t** ret_fp) {314315address epc;316const ucontext_t* uc = (const ucontext_t*)ucVoid;317318if (uc != NULL) {319epc = os::Posix::ucontext_get_pc(uc);320if (ret_sp) *ret_sp = os::Bsd::ucontext_get_sp(uc);321if (ret_fp) *ret_fp = os::Bsd::ucontext_get_fp(uc);322} else {323epc = NULL;324if (ret_sp) *ret_sp = (intptr_t *)NULL;325if (ret_fp) *ret_fp = (intptr_t *)NULL;326}327328return epc;329}330331frame os::fetch_frame_from_context(const void* ucVoid) {332intptr_t* sp;333intptr_t* fp;334address epc = fetch_frame_from_context(ucVoid, &sp, &fp);335return frame(sp, fp, epc);336}337338frame os::fetch_compiled_frame_from_context(const void* ucVoid) {339const ucontext_t* uc = (const ucontext_t*)ucVoid;340frame fr = os::fetch_frame_from_context(uc);341// in compiled code, the stack banging is performed just after the return pc342// has been pushed on the stack343return frame(fr.sp() + 1, fr.fp(), (address)*(fr.sp()));344}345346// By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get347// turned off by -fomit-frame-pointer,348frame os::get_sender_for_C_frame(frame* fr) {349return frame(fr->sender_sp(), fr->link(), fr->sender_pc());350}351352intptr_t* _get_previous_fp() {353#if defined(__clang__) || defined(__llvm__)354intptr_t **ebp;355__asm__("mov %%" SPELL_REG_FP ", %0":"=r"(ebp));356#else357register intptr_t **ebp __asm__ (SPELL_REG_FP);358#endif359// ebp is for this frame (_get_previous_fp). We want the ebp for the360// caller of os::current_frame*(), so go up two frames. However, for361// optimized builds, _get_previous_fp() will be inlined, so only go362// up 1 frame in that case.363#ifdef _NMT_NOINLINE_364return **(intptr_t***)ebp;365#else366return *ebp;367#endif368}369370371frame os::current_frame() {372intptr_t* fp = _get_previous_fp();373frame myframe((intptr_t*)os::current_stack_pointer(),374(intptr_t*)fp,375CAST_FROM_FN_PTR(address, os::current_frame));376if (os::is_first_C_frame(&myframe)) {377// stack is not walkable378return frame();379} else {380return os::get_sender_for_C_frame(&myframe);381}382}383384// From IA32 System Programming Guide385enum {386trap_page_fault = 0xE387};388389bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,390ucontext_t* uc, JavaThread* thread) {391// decide if this trap can be handled by a stub392address stub = NULL;393394address pc = NULL;395396//%note os_trap_1397if (info != NULL && uc != NULL && thread != NULL) {398pc = (address) os::Posix::ucontext_get_pc(uc);399400// Handle ALL stack overflow variations here401if (sig == SIGSEGV || sig == SIGBUS) {402address addr = (address) info->si_addr;403404// check if fault address is within thread stack405if (thread->is_in_full_stack(addr)) {406// stack overflow407if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {408return true; // continue409}410}411}412413if ((sig == SIGSEGV || sig == SIGBUS) && VM_Version::is_cpuinfo_segv_addr(pc)) {414// Verify that OS save/restore AVX registers.415stub = VM_Version::cpuinfo_cont_addr();416}417418// We test if stub is already set (by the stack overflow code419// above) so it is not overwritten by the code that follows. This420// check is not required on other platforms, because on other421// platforms we check for SIGSEGV only or SIGBUS only, where here422// we have to check for both SIGSEGV and SIGBUS.423if (thread->thread_state() == _thread_in_Java && stub == NULL) {424// Java thread running in Java code => find exception handler if any425// a fault inside compiled code, the interpreter, or a stub426427if ((sig == SIGSEGV || sig == SIGBUS) && SafepointMechanism::is_poll_address((address)info->si_addr)) {428stub = SharedRuntime::get_poll_stub(pc);429#if defined(__APPLE__)430// 32-bit Darwin reports a SIGBUS for nearly all memory access exceptions.431// 64-bit Darwin may also use a SIGBUS (seen with compressed oops).432// Catching SIGBUS here prevents the implicit SIGBUS NULL check below from433// being called, so only do so if the implicit NULL check is not necessary.434} else if (sig == SIGBUS && !MacroAssembler::uses_implicit_null_check(info->si_addr)) {435#else436} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {437#endif438// BugId 4454115: A read from a MappedByteBuffer can fault439// here if the underlying file has been truncated.440// Do not crash the VM in such a case.441CodeBlob* cb = CodeCache::find_blob_unsafe(pc);442CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;443bool is_unsafe_arraycopy = thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc);444if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {445address next_pc = Assembler::locate_next_instruction(pc);446if (is_unsafe_arraycopy) {447next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);448}449stub = SharedRuntime::handle_unsafe_access(thread, next_pc);450}451}452else453454#ifdef AMD64455if (sig == SIGFPE &&456(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV457// Workaround for macOS ARM incorrectly reporting FPE_FLTINV for "div by 0"458// instead of the expected FPE_FLTDIV when running x86_64 binary under Rosetta emulation459MACOS_ONLY(|| (VM_Version::is_cpu_emulated() && info->si_code == FPE_FLTINV)))) {460stub =461SharedRuntime::462continuation_for_implicit_exception(thread,463pc,464SharedRuntime::465IMPLICIT_DIVIDE_BY_ZERO);466#ifdef __APPLE__467} else if (sig == SIGFPE && info->si_code == FPE_NOOP) {468int op = pc[0];469470// Skip REX471if ((pc[0] & 0xf0) == 0x40) {472op = pc[1];473} else {474op = pc[0];475}476477// Check for IDIV478if (op == 0xF7) {479stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime:: IMPLICIT_DIVIDE_BY_ZERO);480} else {481// TODO: handle more cases if we are using other x86 instructions482// that can generate SIGFPE signal.483tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);484fatal("please update this code.");485}486#endif /* __APPLE__ */487488#else489if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {490// HACK: si_code does not work on bsd 2.2.12-20!!!491int op = pc[0];492if (op == 0xDB) {493// FIST494// TODO: The encoding of D2I in x86_32.ad can cause an exception495// prior to the fist instruction if there was an invalid operation496// pending. We want to dismiss that exception. From the win_32497// side it also seems that if it really was the fist causing498// the exception that we do the d2i by hand with different499// rounding. Seems kind of weird.500// NOTE: that we take the exception at the NEXT floating point instruction.501assert(pc[0] == 0xDB, "not a FIST opcode");502assert(pc[1] == 0x14, "not a FIST opcode");503assert(pc[2] == 0x24, "not a FIST opcode");504return true;505} else if (op == 0xF7) {506// IDIV507stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);508} else {509// TODO: handle more cases if we are using other x86 instructions510// that can generate SIGFPE signal on bsd.511tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);512fatal("please update this code.");513}514#endif // AMD64515} else if ((sig == SIGSEGV || sig == SIGBUS) &&516MacroAssembler::uses_implicit_null_check(info->si_addr)) {517// Determination of interpreter/vtable stub/compiled code null exception518stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);519}520} else if ((thread->thread_state() == _thread_in_vm ||521thread->thread_state() == _thread_in_native) &&522sig == SIGBUS && /* info->si_code == BUS_OBJERR && */523thread->doing_unsafe_access()) {524address next_pc = Assembler::locate_next_instruction(pc);525if (UnsafeCopyMemory::contains_pc(pc)) {526next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);527}528stub = SharedRuntime::handle_unsafe_access(thread, next_pc);529}530531// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in532// and the heap gets shrunk before the field access.533if ((sig == SIGSEGV) || (sig == SIGBUS)) {534address addr = JNI_FastGetField::find_slowcase_pc(pc);535if (addr != (address)-1) {536stub = addr;537}538}539}540541#ifndef AMD64542// Execution protection violation543//544// This should be kept as the last step in the triage. We don't545// have a dedicated trap number for a no-execute fault, so be546// conservative and allow other handlers the first shot.547//548// Note: We don't test that info->si_code == SEGV_ACCERR here.549// this si_code is so generic that it is almost meaningless; and550// the si_code for this condition may change in the future.551// Furthermore, a false-positive should be harmless.552if (UnguardOnExecutionViolation > 0 &&553stub == NULL &&554(sig == SIGSEGV || sig == SIGBUS) &&555uc->context_trapno == trap_page_fault) {556int page_size = os::vm_page_size();557address addr = (address) info->si_addr;558address pc = os::Posix::ucontext_get_pc(uc);559// Make sure the pc and the faulting address are sane.560//561// If an instruction spans a page boundary, and the page containing562// the beginning of the instruction is executable but the following563// page is not, the pc and the faulting address might be slightly564// different - we still want to unguard the 2nd page in this case.565//566// 15 bytes seems to be a (very) safe value for max instruction size.567bool pc_is_near_addr =568(pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);569bool instr_spans_page_boundary =570(align_down((intptr_t) pc ^ (intptr_t) addr,571(intptr_t) page_size) > 0);572573if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {574static volatile address last_addr =575(address) os::non_memory_address_word();576577// In conservative mode, don't unguard unless the address is in the VM578if (addr != last_addr &&579(UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {580581// Set memory to RWX and retry582address page_start = align_down(addr, page_size);583bool res = os::protect_memory((char*) page_start, page_size,584os::MEM_PROT_RWX);585586log_debug(os)("Execution protection violation "587"at " INTPTR_FORMAT588", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr),589p2i(page_start), (res ? "success" : "failed"), errno);590stub = pc;591592// Set last_addr so if we fault again at the same address, we don't end593// up in an endless loop.594//595// There are two potential complications here. Two threads trapping at596// the same address at the same time could cause one of the threads to597// think it already unguarded, and abort the VM. Likely very rare.598//599// The other race involves two threads alternately trapping at600// different addresses and failing to unguard the page, resulting in601// an endless loop. This condition is probably even more unlikely than602// the first.603//604// Although both cases could be avoided by using locks or thread local605// last_addr, these solutions are unnecessary complication: this606// handler is a best-effort safety net, not a complete solution. It is607// disabled by default and should only be used as a workaround in case608// we missed any no-execute-unsafe VM code.609610last_addr = addr;611}612}613}614#endif // !AMD64615616if (stub != NULL) {617// save all thread context in case we need to restore it618if (thread != NULL) thread->set_saved_exception_pc(pc);619620os::Posix::ucontext_set_pc(uc, stub);621return true;622}623624return false;625}626627// From solaris_i486.s ported to bsd_i486.s628extern "C" void fixcw();629630void os::Bsd::init_thread_fpu_state(void) {631#ifndef AMD64632// Set fpu to 53 bit precision. This happens too early to use a stub.633fixcw();634#endif // !AMD64635}636637638// Check that the bsd kernel version is 2.4 or higher since earlier639// versions do not support SSE without patches.640bool os::supports_sse() {641return true;642}643644juint os::cpu_microcode_revision() {645juint result = 0;646char data[8];647size_t sz = sizeof(data);648int ret = sysctlbyname("machdep.cpu.microcode_version", data, &sz, NULL, 0);649if (ret == 0) {650if (sz == 4) result = *((juint*)data);651if (sz == 8) result = *((juint*)data + 1); // upper 32-bits652}653return result;654}655656////////////////////////////////////////////////////////////////////////////////657// thread stack658659// Minimum usable stack sizes required to get to user code. Space for660// HotSpot guard pages is added later.661size_t os::Posix::_compiler_thread_min_stack_allowed = 48 * K;662size_t os::Posix::_java_thread_min_stack_allowed = 48 * K;663#ifdef _LP64664size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;665#else666size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;667#endif // _LP64668669#ifndef AMD64670#ifdef __GNUC__671#define GET_GS() ({int gs; __asm__ volatile("movw %%gs, %w0":"=q"(gs)); gs&0xffff;})672#endif673#endif // AMD64674675// return default stack size for thr_type676size_t os::Posix::default_stack_size(os::ThreadType thr_type) {677// default stack size (compiler thread needs larger stack)678#ifdef AMD64679size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);680#else681size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);682#endif // AMD64683return s;684}685686687// Java thread:688//689// Low memory addresses690// +------------------------+691// | |\ Java thread created by VM does not have glibc692// | glibc guard page | - guard, attached Java thread usually has693// | |/ 1 glibc guard page.694// P1 +------------------------+ Thread::stack_base() - Thread::stack_size()695// | |\696// | HotSpot Guard Pages | - red, yellow and reserved pages697// | |/698// +------------------------+ StackOverflow::stack_reserved_zone_base()699// | |\700// | Normal Stack | -701// | |/702// P2 +------------------------+ Thread::stack_base()703//704// Non-Java thread:705//706// Low memory addresses707// +------------------------+708// | |\709// | glibc guard page | - usually 1 page710// | |/711// P1 +------------------------+ Thread::stack_base() - Thread::stack_size()712// | |\713// | Normal Stack | -714// | |/715// P2 +------------------------+ Thread::stack_base()716//717// ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from718// pthread_attr_getstack()719720static void current_stack_region(address * bottom, size_t * size) {721#ifdef __APPLE__722pthread_t self = pthread_self();723void *stacktop = pthread_get_stackaddr_np(self);724*size = pthread_get_stacksize_np(self);725// workaround for OS X 10.9.0 (Mavericks)726// pthread_get_stacksize_np returns 128 pages even though the actual size is 2048 pages727if (pthread_main_np() == 1) {728// At least on Mac OS 10.12 we have observed stack sizes not aligned729// to pages boundaries. This can be provoked by e.g. setrlimit() (ulimit -s xxxx in the730// shell). Apparently Mac OS actually rounds upwards to next multiple of page size,731// however, we round downwards here to be on the safe side.732*size = align_down(*size, getpagesize());733734if ((*size) < (DEFAULT_MAIN_THREAD_STACK_PAGES * (size_t)getpagesize())) {735char kern_osrelease[256];736size_t kern_osrelease_size = sizeof(kern_osrelease);737int ret = sysctlbyname("kern.osrelease", kern_osrelease, &kern_osrelease_size, NULL, 0);738if (ret == 0) {739// get the major number, atoi will ignore the minor amd micro portions of the version string740if (atoi(kern_osrelease) >= OS_X_10_9_0_KERNEL_MAJOR_VERSION) {741*size = (DEFAULT_MAIN_THREAD_STACK_PAGES*getpagesize());742}743}744}745}746*bottom = (address) stacktop - *size;747#elif defined(__OpenBSD__)748stack_t ss;749int rslt = pthread_stackseg_np(pthread_self(), &ss);750751if (rslt != 0)752fatal("pthread_stackseg_np failed with error = %d", rslt);753754*bottom = (address)((char *)ss.ss_sp - ss.ss_size);755*size = ss.ss_size;756#else757pthread_attr_t attr;758759int rslt = pthread_attr_init(&attr);760761// JVM needs to know exact stack location, abort if it fails762if (rslt != 0)763fatal("pthread_attr_init failed with error = %d", rslt);764765rslt = pthread_attr_get_np(pthread_self(), &attr);766767if (rslt != 0)768fatal("pthread_attr_get_np failed with error = %d", rslt);769770if (pthread_attr_getstackaddr(&attr, (void **)bottom) != 0 ||771pthread_attr_getstacksize(&attr, size) != 0) {772fatal("Can not locate current stack attributes!");773}774775pthread_attr_destroy(&attr);776#endif777assert(os::current_stack_pointer() >= *bottom &&778os::current_stack_pointer() < *bottom + *size, "just checking");779}780781address os::current_stack_base() {782address bottom;783size_t size;784current_stack_region(&bottom, &size);785return (bottom + size);786}787788size_t os::current_stack_size() {789// stack size includes normal stack and HotSpot guard pages790address bottom;791size_t size;792current_stack_region(&bottom, &size);793return size;794}795796/////////////////////////////////////////////////////////////////////////////797// helper functions for fatal error handler798799void os::print_context(outputStream *st, const void *context) {800if (context == NULL) return;801802const ucontext_t *uc = (const ucontext_t*)context;803st->print_cr("Registers:");804#ifdef AMD64805st->print( "RAX=" INTPTR_FORMAT, (intptr_t)uc->context_rax);806st->print(", RBX=" INTPTR_FORMAT, (intptr_t)uc->context_rbx);807st->print(", RCX=" INTPTR_FORMAT, (intptr_t)uc->context_rcx);808st->print(", RDX=" INTPTR_FORMAT, (intptr_t)uc->context_rdx);809st->cr();810st->print( "RSP=" INTPTR_FORMAT, (intptr_t)uc->context_rsp);811st->print(", RBP=" INTPTR_FORMAT, (intptr_t)uc->context_rbp);812st->print(", RSI=" INTPTR_FORMAT, (intptr_t)uc->context_rsi);813st->print(", RDI=" INTPTR_FORMAT, (intptr_t)uc->context_rdi);814st->cr();815st->print( "R8 =" INTPTR_FORMAT, (intptr_t)uc->context_r8);816st->print(", R9 =" INTPTR_FORMAT, (intptr_t)uc->context_r9);817st->print(", R10=" INTPTR_FORMAT, (intptr_t)uc->context_r10);818st->print(", R11=" INTPTR_FORMAT, (intptr_t)uc->context_r11);819st->cr();820st->print( "R12=" INTPTR_FORMAT, (intptr_t)uc->context_r12);821st->print(", R13=" INTPTR_FORMAT, (intptr_t)uc->context_r13);822st->print(", R14=" INTPTR_FORMAT, (intptr_t)uc->context_r14);823st->print(", R15=" INTPTR_FORMAT, (intptr_t)uc->context_r15);824st->cr();825st->print( "RIP=" INTPTR_FORMAT, (intptr_t)uc->context_rip);826st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->context_flags);827st->print(", ERR=" INTPTR_FORMAT, (intptr_t)uc->context_err);828st->cr();829st->print(" TRAPNO=" INTPTR_FORMAT, (intptr_t)uc->context_trapno);830#else831st->print( "EAX=" INTPTR_FORMAT, (intptr_t)uc->context_eax);832st->print(", EBX=" INTPTR_FORMAT, (intptr_t)uc->context_ebx);833st->print(", ECX=" INTPTR_FORMAT, (intptr_t)uc->context_ecx);834st->print(", EDX=" INTPTR_FORMAT, (intptr_t)uc->context_edx);835st->cr();836st->print( "ESP=" INTPTR_FORMAT, (intptr_t)uc->context_esp);837st->print(", EBP=" INTPTR_FORMAT, (intptr_t)uc->context_ebp);838st->print(", ESI=" INTPTR_FORMAT, (intptr_t)uc->context_esi);839st->print(", EDI=" INTPTR_FORMAT, (intptr_t)uc->context_edi);840st->cr();841st->print( "EIP=" INTPTR_FORMAT, (intptr_t)uc->context_eip);842st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->context_eflags);843#endif // AMD64844st->cr();845st->cr();846847intptr_t *sp = (intptr_t *)os::Bsd::ucontext_get_sp(uc);848st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", (intptr_t)sp);849print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));850st->cr();851852// Note: it may be unsafe to inspect memory near pc. For example, pc may853// point to garbage if entry point in an nmethod is corrupted. Leave854// this at the end, and hope for the best.855address pc = os::Posix::ucontext_get_pc(uc);856print_instructions(st, pc, sizeof(char));857st->cr();858}859860void os::print_register_info(outputStream *st, const void *context) {861if (context == NULL) return;862863const ucontext_t *uc = (const ucontext_t*)context;864865st->print_cr("Register to memory mapping:");866st->cr();867868// this is horrendously verbose but the layout of the registers in the869// context does not match how we defined our abstract Register set, so870// we can't just iterate through the gregs area871872// this is only for the "general purpose" registers873874#ifdef AMD64875st->print("RAX="); print_location(st, uc->context_rax);876st->print("RBX="); print_location(st, uc->context_rbx);877st->print("RCX="); print_location(st, uc->context_rcx);878st->print("RDX="); print_location(st, uc->context_rdx);879st->print("RSP="); print_location(st, uc->context_rsp);880st->print("RBP="); print_location(st, uc->context_rbp);881st->print("RSI="); print_location(st, uc->context_rsi);882st->print("RDI="); print_location(st, uc->context_rdi);883st->print("R8 ="); print_location(st, uc->context_r8);884st->print("R9 ="); print_location(st, uc->context_r9);885st->print("R10="); print_location(st, uc->context_r10);886st->print("R11="); print_location(st, uc->context_r11);887st->print("R12="); print_location(st, uc->context_r12);888st->print("R13="); print_location(st, uc->context_r13);889st->print("R14="); print_location(st, uc->context_r14);890st->print("R15="); print_location(st, uc->context_r15);891#else892st->print("EAX="); print_location(st, uc->context_eax);893st->print("EBX="); print_location(st, uc->context_ebx);894st->print("ECX="); print_location(st, uc->context_ecx);895st->print("EDX="); print_location(st, uc->context_edx);896st->print("ESP="); print_location(st, uc->context_esp);897st->print("EBP="); print_location(st, uc->context_ebp);898st->print("ESI="); print_location(st, uc->context_esi);899st->print("EDI="); print_location(st, uc->context_edi);900#endif // AMD64901902st->cr();903}904905void os::setup_fpu() {906#ifndef AMD64907address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std();908__asm__ volatile ( "fldcw (%0)" :909: "r" (fpu_cntrl) : "memory");910#endif // !AMD64911}912913#ifndef PRODUCT914void os::verify_stack_alignment() {915}916#endif917918int os::extra_bang_size_in_bytes() {919// JDK-8050147 requires the full cache line bang for x86.920return VM_Version::L1_line_size();921}922923924