Path: blob/master/src/hotspot/os_cpu/linux_x86/os_linux_x86.cpp
64440 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_linux.hpp"35#include "prims/jniFastGetField.hpp"36#include "prims/jvm_misc.hpp"37#include "runtime/frame.inline.hpp"38#include "runtime/interfaceSupport.inline.hpp"39#include "runtime/java.hpp"40#include "runtime/javaCalls.hpp"41#include "runtime/mutexLocker.hpp"42#include "runtime/osThread.hpp"43#include "runtime/safepointMechanism.hpp"44#include "runtime/sharedRuntime.hpp"45#include "runtime/stubRoutines.hpp"46#include "runtime/thread.inline.hpp"47#include "runtime/timer.hpp"48#include "signals_posix.hpp"49#include "services/memTracker.hpp"50#include "utilities/align.hpp"51#include "utilities/debug.hpp"52#include "utilities/events.hpp"53#include "utilities/vmError.hpp"5455// put OS-includes here56# include <sys/types.h>57# include <sys/mman.h>58# include <pthread.h>59# include <signal.h>60# include <errno.h>61# include <dlfcn.h>62# include <stdlib.h>63# include <stdio.h>64# include <unistd.h>65# include <sys/resource.h>66# include <pthread.h>67# include <sys/stat.h>68# include <sys/time.h>69# include <sys/utsname.h>70# include <sys/socket.h>71# include <sys/wait.h>72# include <pwd.h>73# include <poll.h>74# include <ucontext.h>75#ifndef AMD6476# include <fpu_control.h>77#endif7879#ifdef AMD6480#define REG_SP REG_RSP81#define REG_PC REG_RIP82#define REG_FP REG_RBP83#define SPELL_REG_SP "rsp"84#define SPELL_REG_FP "rbp"85#else86#define REG_SP REG_UESP87#define REG_PC REG_EIP88#define REG_FP REG_EBP89#define SPELL_REG_SP "esp"90#define SPELL_REG_FP "ebp"91#endif // AMD649293address os::current_stack_pointer() {94return (address)__builtin_frame_address(0);95}9697char* os::non_memory_address_word() {98// Must never look like an address returned by reserve_memory,99// even in its subfields (as defined by the CPU immediate fields,100// if the CPU splits constants across multiple instructions).101102return (char*) -1;103}104105address os::Posix::ucontext_get_pc(const ucontext_t * uc) {106return (address)uc->uc_mcontext.gregs[REG_PC];107}108109void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {110uc->uc_mcontext.gregs[REG_PC] = (intptr_t)pc;111}112113intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {114return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];115}116117intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {118return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];119}120121address os::fetch_frame_from_context(const void* ucVoid,122intptr_t** ret_sp, intptr_t** ret_fp) {123124address epc;125const ucontext_t* uc = (const ucontext_t*)ucVoid;126127if (uc != NULL) {128epc = os::Posix::ucontext_get_pc(uc);129if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);130if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);131} else {132epc = NULL;133if (ret_sp) *ret_sp = (intptr_t *)NULL;134if (ret_fp) *ret_fp = (intptr_t *)NULL;135}136137return epc;138}139140frame os::fetch_frame_from_context(const void* ucVoid) {141intptr_t* sp;142intptr_t* fp;143address epc = fetch_frame_from_context(ucVoid, &sp, &fp);144return frame(sp, fp, epc);145}146147frame os::fetch_compiled_frame_from_context(const void* ucVoid) {148const ucontext_t* uc = (const ucontext_t*)ucVoid;149intptr_t* fp = os::Linux::ucontext_get_fp(uc);150intptr_t* sp = os::Linux::ucontext_get_sp(uc);151return frame(sp + 1, fp, (address)*sp);152}153154// By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get155// turned off by -fomit-frame-pointer,156frame os::get_sender_for_C_frame(frame* fr) {157return frame(fr->sender_sp(), fr->link(), fr->sender_pc());158}159160intptr_t* _get_previous_fp() {161#if defined(__clang__)162intptr_t **ebp;163__asm__ __volatile__ ("mov %%" SPELL_REG_FP ", %0":"=r"(ebp):);164#else165register intptr_t **ebp __asm__ (SPELL_REG_FP);166#endif167// ebp is for this frame (_get_previous_fp). We want the ebp for the168// caller of os::current_frame*(), so go up two frames. However, for169// optimized builds, _get_previous_fp() will be inlined, so only go170// up 1 frame in that case.171#ifdef _NMT_NOINLINE_172return **(intptr_t***)ebp;173#else174return *ebp;175#endif176}177178179frame os::current_frame() {180intptr_t* fp = _get_previous_fp();181frame myframe((intptr_t*)os::current_stack_pointer(),182(intptr_t*)fp,183CAST_FROM_FN_PTR(address, os::current_frame));184if (os::is_first_C_frame(&myframe)) {185// stack is not walkable186return frame();187} else {188return os::get_sender_for_C_frame(&myframe);189}190}191192// Utility functions193194// From IA32 System Programming Guide195enum {196trap_page_fault = 0xE197};198199bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,200ucontext_t* uc, JavaThread* thread) {201202/*203NOTE: does not seem to work on linux.204if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {205// can't decode this kind of signal206info = NULL;207} else {208assert(sig == info->si_signo, "bad siginfo");209}210*/211// decide if this trap can be handled by a stub212address stub = NULL;213214address pc = NULL;215216//%note os_trap_1217if (info != NULL && uc != NULL && thread != NULL) {218pc = (address) os::Posix::ucontext_get_pc(uc);219220#ifndef AMD64221// Halt if SI_KERNEL before more crashes get misdiagnosed as Java bugs222// This can happen in any running code (currently more frequently in223// interpreter code but has been seen in compiled code)224if (sig == SIGSEGV && info->si_addr == 0 && info->si_code == SI_KERNEL) {225fatal("An irrecoverable SI_KERNEL SIGSEGV has occurred due "226"to unstable signal handling in this distribution.");227}228#endif // AMD64229230// Handle ALL stack overflow variations here231if (sig == SIGSEGV) {232address addr = (address) info->si_addr;233234// check if fault address is within thread stack235if (thread->is_in_full_stack(addr)) {236// stack overflow237if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {238return true; // continue239}240}241}242243if ((sig == SIGSEGV) && VM_Version::is_cpuinfo_segv_addr(pc)) {244// Verify that OS save/restore AVX registers.245stub = VM_Version::cpuinfo_cont_addr();246}247248if (thread->thread_state() == _thread_in_Java) {249// Java thread running in Java code => find exception handler if any250// a fault inside compiled code, the interpreter, or a stub251252if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) {253stub = SharedRuntime::get_poll_stub(pc);254} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {255// BugId 4454115: A read from a MappedByteBuffer can fault256// here if the underlying file has been truncated.257// Do not crash the VM in such a case.258CodeBlob* cb = CodeCache::find_blob_unsafe(pc);259CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;260bool is_unsafe_arraycopy = thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc);261if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {262address next_pc = Assembler::locate_next_instruction(pc);263if (is_unsafe_arraycopy) {264next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);265}266stub = SharedRuntime::handle_unsafe_access(thread, next_pc);267}268}269else270271#ifdef AMD64272if (sig == SIGFPE &&273(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {274stub =275SharedRuntime::276continuation_for_implicit_exception(thread,277pc,278SharedRuntime::279IMPLICIT_DIVIDE_BY_ZERO);280#else281if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {282// HACK: si_code does not work on linux 2.2.12-20!!!283int op = pc[0];284if (op == 0xDB) {285// FIST286// TODO: The encoding of D2I in x86_32.ad can cause an exception287// prior to the fist instruction if there was an invalid operation288// pending. We want to dismiss that exception. From the win_32289// side it also seems that if it really was the fist causing290// the exception that we do the d2i by hand with different291// rounding. Seems kind of weird.292// NOTE: that we take the exception at the NEXT floating point instruction.293assert(pc[0] == 0xDB, "not a FIST opcode");294assert(pc[1] == 0x14, "not a FIST opcode");295assert(pc[2] == 0x24, "not a FIST opcode");296return true;297} else if (op == 0xF7) {298// IDIV299stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);300} else {301// TODO: handle more cases if we are using other x86 instructions302// that can generate SIGFPE signal on linux.303tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);304fatal("please update this code.");305}306#endif // AMD64307} else if (sig == SIGSEGV &&308MacroAssembler::uses_implicit_null_check(info->si_addr)) {309// Determination of interpreter/vtable stub/compiled code null exception310stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);311}312} else if ((thread->thread_state() == _thread_in_vm ||313thread->thread_state() == _thread_in_native) &&314(sig == SIGBUS && /* info->si_code == BUS_OBJERR && */315thread->doing_unsafe_access())) {316address next_pc = Assembler::locate_next_instruction(pc);317if (UnsafeCopyMemory::contains_pc(pc)) {318next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);319}320stub = SharedRuntime::handle_unsafe_access(thread, next_pc);321}322323// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in324// and the heap gets shrunk before the field access.325if ((sig == SIGSEGV) || (sig == SIGBUS)) {326address addr = JNI_FastGetField::find_slowcase_pc(pc);327if (addr != (address)-1) {328stub = addr;329}330}331}332333#ifndef AMD64334// Execution protection violation335//336// This should be kept as the last step in the triage. We don't337// have a dedicated trap number for a no-execute fault, so be338// conservative and allow other handlers the first shot.339//340// Note: We don't test that info->si_code == SEGV_ACCERR here.341// this si_code is so generic that it is almost meaningless; and342// the si_code for this condition may change in the future.343// Furthermore, a false-positive should be harmless.344if (UnguardOnExecutionViolation > 0 &&345stub == NULL &&346(sig == SIGSEGV || sig == SIGBUS) &&347uc->uc_mcontext.gregs[REG_TRAPNO] == trap_page_fault) {348int page_size = os::vm_page_size();349address addr = (address) info->si_addr;350address pc = os::Posix::ucontext_get_pc(uc);351// Make sure the pc and the faulting address are sane.352//353// If an instruction spans a page boundary, and the page containing354// the beginning of the instruction is executable but the following355// page is not, the pc and the faulting address might be slightly356// different - we still want to unguard the 2nd page in this case.357//358// 15 bytes seems to be a (very) safe value for max instruction size.359bool pc_is_near_addr =360(pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);361bool instr_spans_page_boundary =362(align_down((intptr_t) pc ^ (intptr_t) addr,363(intptr_t) page_size) > 0);364365if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {366static volatile address last_addr =367(address) os::non_memory_address_word();368369// In conservative mode, don't unguard unless the address is in the VM370if (addr != last_addr &&371(UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {372373// Set memory to RWX and retry374address page_start = align_down(addr, page_size);375bool res = os::protect_memory((char*) page_start, page_size,376os::MEM_PROT_RWX);377378log_debug(os)("Execution protection violation "379"at " INTPTR_FORMAT380", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr),381p2i(page_start), (res ? "success" : "failed"), errno);382stub = pc;383384// Set last_addr so if we fault again at the same address, we don't end385// up in an endless loop.386//387// There are two potential complications here. Two threads trapping at388// the same address at the same time could cause one of the threads to389// think it already unguarded, and abort the VM. Likely very rare.390//391// The other race involves two threads alternately trapping at392// different addresses and failing to unguard the page, resulting in393// an endless loop. This condition is probably even more unlikely than394// the first.395//396// Although both cases could be avoided by using locks or thread local397// last_addr, these solutions are unnecessary complication: this398// handler is a best-effort safety net, not a complete solution. It is399// disabled by default and should only be used as a workaround in case400// we missed any no-execute-unsafe VM code.401402last_addr = addr;403}404}405}406#endif // !AMD64407408if (stub != NULL) {409// save all thread context in case we need to restore it410if (thread != NULL) thread->set_saved_exception_pc(pc);411412os::Posix::ucontext_set_pc(uc, stub);413return true;414}415416return false;417}418419void os::Linux::init_thread_fpu_state(void) {420#ifndef AMD64421// set fpu to 53 bit precision422set_fpu_control_word(0x27f);423#endif // !AMD64424}425426int os::Linux::get_fpu_control_word(void) {427#ifdef AMD64428return 0;429#else430int fpu_control;431_FPU_GETCW(fpu_control);432return fpu_control & 0xffff;433#endif // AMD64434}435436void os::Linux::set_fpu_control_word(int fpu_control) {437#ifndef AMD64438_FPU_SETCW(fpu_control);439#endif // !AMD64440}441442// Check that the linux kernel version is 2.4 or higher since earlier443// versions do not support SSE without patches.444bool os::supports_sse() {445#ifdef AMD64446return true;447#else448struct utsname uts;449if( uname(&uts) != 0 ) return false; // uname fails?450char *minor_string;451int major = strtol(uts.release,&minor_string,10);452int minor = strtol(minor_string+1,NULL,10);453bool result = (major > 2 || (major==2 && minor >= 4));454log_info(os)("OS version is %d.%d, which %s support SSE/SSE2",455major,minor, result ? "DOES" : "does NOT");456return result;457#endif // AMD64458}459460juint os::cpu_microcode_revision() {461juint result = 0;462char data[2048] = {0}; // lines should fit in 2K buf463size_t len = sizeof(data);464FILE *fp = fopen("/proc/cpuinfo", "r");465if (fp) {466while (!feof(fp)) {467if (fgets(data, len, fp)) {468if (strstr(data, "microcode") != NULL) {469char* rev = strchr(data, ':');470if (rev != NULL) sscanf(rev + 1, "%x", &result);471break;472}473}474}475fclose(fp);476}477return result;478}479480////////////////////////////////////////////////////////////////////////////////481// thread stack482483// Minimum usable stack sizes required to get to user code. Space for484// HotSpot guard pages is added later.485size_t os::Posix::_compiler_thread_min_stack_allowed = 48 * K;486size_t os::Posix::_java_thread_min_stack_allowed = 40 * K;487#ifdef _LP64488size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;489#else490size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;491#endif // _LP64492493// return default stack size for thr_type494size_t os::Posix::default_stack_size(os::ThreadType thr_type) {495// default stack size (compiler thread needs larger stack)496#ifdef AMD64497size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);498#else499size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);500#endif // AMD64501return s;502}503504/////////////////////////////////////////////////////////////////////////////505// helper functions for fatal error handler506507void os::print_context(outputStream *st, const void *context) {508if (context == NULL) return;509510const ucontext_t *uc = (const ucontext_t*)context;511st->print_cr("Registers:");512#ifdef AMD64513st->print( "RAX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RAX]);514st->print(", RBX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RBX]);515st->print(", RCX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RCX]);516st->print(", RDX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RDX]);517st->cr();518st->print( "RSP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RSP]);519st->print(", RBP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RBP]);520st->print(", RSI=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RSI]);521st->print(", RDI=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RDI]);522st->cr();523st->print( "R8 =" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R8]);524st->print(", R9 =" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R9]);525st->print(", R10=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R10]);526st->print(", R11=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R11]);527st->cr();528st->print( "R12=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R12]);529st->print(", R13=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R13]);530st->print(", R14=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R14]);531st->print(", R15=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R15]);532st->cr();533st->print( "RIP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RIP]);534st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_EFL]);535st->print(", CSGSFS=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_CSGSFS]);536st->print(", ERR=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_ERR]);537st->cr();538st->print(" TRAPNO=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_TRAPNO]);539#else540st->print( "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EAX]);541st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EBX]);542st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ECX]);543st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EDX]);544st->cr();545st->print( "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_UESP]);546st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EBP]);547st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ESI]);548st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EDI]);549st->cr();550st->print( "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EIP]);551st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EFL]);552st->print(", CR2=" PTR64_FORMAT, (uint64_t)uc->uc_mcontext.cr2);553#endif // AMD64554st->cr();555st->cr();556557intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);558st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));559print_hex_dump(st, (address)sp, (address)(sp + 8), sizeof(intptr_t));560st->cr();561562// Note: it may be unsafe to inspect memory near pc. For example, pc may563// point to garbage if entry point in an nmethod is corrupted. Leave564// this at the end, and hope for the best.565address pc = os::Posix::ucontext_get_pc(uc);566print_instructions(st, pc, sizeof(char));567st->cr();568}569570void os::print_register_info(outputStream *st, const void *context) {571if (context == NULL) return;572573const ucontext_t *uc = (const ucontext_t*)context;574575st->print_cr("Register to memory mapping:");576st->cr();577578// this is horrendously verbose but the layout of the registers in the579// context does not match how we defined our abstract Register set, so580// we can't just iterate through the gregs area581582// this is only for the "general purpose" registers583584#ifdef AMD64585st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);586st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);587st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);588st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);589st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);590st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);591st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);592st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);593st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);594st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);595st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);596st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);597st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);598st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);599st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);600st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);601#else602st->print("EAX="); print_location(st, uc->uc_mcontext.gregs[REG_EAX]);603st->print("EBX="); print_location(st, uc->uc_mcontext.gregs[REG_EBX]);604st->print("ECX="); print_location(st, uc->uc_mcontext.gregs[REG_ECX]);605st->print("EDX="); print_location(st, uc->uc_mcontext.gregs[REG_EDX]);606st->print("ESP="); print_location(st, uc->uc_mcontext.gregs[REG_ESP]);607st->print("EBP="); print_location(st, uc->uc_mcontext.gregs[REG_EBP]);608st->print("ESI="); print_location(st, uc->uc_mcontext.gregs[REG_ESI]);609st->print("EDI="); print_location(st, uc->uc_mcontext.gregs[REG_EDI]);610#endif // AMD64611612st->cr();613}614615void os::setup_fpu() {616#ifndef AMD64617address fpu_cntrl = StubRoutines::x86::addr_fpu_cntrl_wrd_std();618__asm__ volatile ( "fldcw (%0)" :619: "r" (fpu_cntrl) : "memory");620#endif // !AMD64621}622623#ifndef PRODUCT624void os::verify_stack_alignment() {625#ifdef AMD64626assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");627#endif628}629#endif630631632/*633* IA32 only: execute code at a high address in case buggy NX emulation is present. I.e. avoid CS limit634* updates (JDK-8023956).635*/636void os::workaround_expand_exec_shield_cs_limit() {637#if defined(IA32)638assert(Linux::initial_thread_stack_bottom() != NULL, "sanity");639size_t page_size = os::vm_page_size();640641/*642* JDK-8197429643*644* Expand the stack mapping to the end of the initial stack before645* attempting to install the codebuf. This is needed because newer646* Linux kernels impose a distance of a megabyte between stack647* memory and other memory regions. If we try to install the648* codebuf before expanding the stack the installation will appear649* to succeed but we'll get a segfault later if we expand the stack650* in Java code.651*652*/653if (os::is_primordial_thread()) {654address limit = Linux::initial_thread_stack_bottom();655if (! DisablePrimordialThreadGuardPages) {656limit += StackOverflow::stack_red_zone_size() +657StackOverflow::stack_yellow_zone_size();658}659os::Linux::expand_stack_to(limit);660}661662/*663* Take the highest VA the OS will give us and exec664*665* Although using -(pagesz) as mmap hint works on newer kernel as you would666* think, older variants affected by this work-around don't (search forward only).667*668* On the affected distributions, we understand the memory layout to be:669*670* TASK_LIMIT= 3G, main stack base close to TASK_LIMT.671*672* A few pages south main stack will do it.673*674* If we are embedded in an app other than launcher (initial != main stack),675* we don't have much control or understanding of the address space, just let it slide.676*/677char* hint = (char*)(Linux::initial_thread_stack_bottom() -678(StackOverflow::stack_guard_zone_size() + page_size));679char* codebuf = os::attempt_reserve_memory_at(hint, page_size);680681if (codebuf == NULL) {682// JDK-8197429: There may be a stack gap of one megabyte between683// the limit of the stack and the nearest memory region: this is a684// Linux kernel workaround for CVE-2017-1000364. If we failed to685// map our codebuf, try again at an address one megabyte lower.686hint -= 1 * M;687codebuf = os::attempt_reserve_memory_at(hint, page_size);688}689690if ((codebuf == NULL) || (!os::commit_memory(codebuf, page_size, true))) {691return; // No matter, we tried, best effort.692}693694MemTracker::record_virtual_memory_type((address)codebuf, mtInternal);695696log_info(os)("[CS limit NX emulation work-around, exec code at: %p]", codebuf);697698// Some code to exec: the 'ret' instruction699codebuf[0] = 0xC3;700701// Call the code in the codebuf702__asm__ volatile("call *%0" : : "r"(codebuf));703704// keep the page mapped so CS limit isn't reduced.705#endif706}707708int os::extra_bang_size_in_bytes() {709// JDK-8050147 requires the full cache line bang for x86.710return VM_Version::L1_line_size();711}712713714