Path: blob/master/src/hotspot/os_cpu/linux_x86/os_linux_x86.cpp
40951 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#if !defined(AMD64) && !defined(__ANDROID__)76# include <fpu_control.h>77#elif defined(__ANDROID__)78# include "fpu_control.h"79#endif8081#ifdef AMD6482#define REG_SP REG_RSP83#define REG_PC REG_RIP84#define REG_FP REG_RBP85#define SPELL_REG_SP "rsp"86#define SPELL_REG_FP "rbp"87#else88#define REG_SP REG_UESP89#define REG_PC REG_EIP90#define REG_FP REG_EBP91#define SPELL_REG_SP "esp"92#define SPELL_REG_FP "ebp"93#endif // AMD649495address os::current_stack_pointer() {96return (address)__builtin_frame_address(0);97}9899char* os::non_memory_address_word() {100// Must never look like an address returned by reserve_memory,101// even in its subfields (as defined by the CPU immediate fields,102// if the CPU splits constants across multiple instructions).103104return (char*) -1;105}106107address os::Posix::ucontext_get_pc(const ucontext_t * uc) {108return (address)uc->uc_mcontext.gregs[REG_PC];109}110111void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {112uc->uc_mcontext.gregs[REG_PC] = (intptr_t)pc;113}114115intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {116return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];117}118119intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {120return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];121}122123address os::fetch_frame_from_context(const void* ucVoid,124intptr_t** ret_sp, intptr_t** ret_fp) {125126address epc;127const ucontext_t* uc = (const ucontext_t*)ucVoid;128129if (uc != NULL) {130epc = os::Posix::ucontext_get_pc(uc);131if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);132if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);133} else {134epc = NULL;135if (ret_sp) *ret_sp = (intptr_t *)NULL;136if (ret_fp) *ret_fp = (intptr_t *)NULL;137}138139return epc;140}141142frame os::fetch_frame_from_context(const void* ucVoid) {143intptr_t* sp;144intptr_t* fp;145address epc = fetch_frame_from_context(ucVoid, &sp, &fp);146return frame(sp, fp, epc);147}148149frame os::fetch_compiled_frame_from_context(const void* ucVoid) {150const ucontext_t* uc = (const ucontext_t*)ucVoid;151intptr_t* fp = os::Linux::ucontext_get_fp(uc);152intptr_t* sp = os::Linux::ucontext_get_sp(uc);153return frame(sp + 1, fp, (address)*sp);154}155156// By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get157// turned off by -fomit-frame-pointer,158frame os::get_sender_for_C_frame(frame* fr) {159return frame(fr->sender_sp(), fr->link(), fr->sender_pc());160}161162intptr_t* _get_previous_fp() {163#if defined(__clang__)164intptr_t **ebp;165__asm__ __volatile__ ("mov %%" SPELL_REG_FP ", %0":"=r"(ebp):);166#else167register intptr_t **ebp __asm__ (SPELL_REG_FP);168#endif169// ebp is for this frame (_get_previous_fp). We want the ebp for the170// caller of os::current_frame*(), so go up two frames. However, for171// optimized builds, _get_previous_fp() will be inlined, so only go172// up 1 frame in that case.173#ifdef _NMT_NOINLINE_174return **(intptr_t***)ebp;175#else176return *ebp;177#endif178}179180181frame os::current_frame() {182intptr_t* fp = _get_previous_fp();183frame myframe((intptr_t*)os::current_stack_pointer(),184(intptr_t*)fp,185CAST_FROM_FN_PTR(address, os::current_frame));186if (os::is_first_C_frame(&myframe)) {187// stack is not walkable188return frame();189} else {190return os::get_sender_for_C_frame(&myframe);191}192}193194// Utility functions195196// From IA32 System Programming Guide197enum {198trap_page_fault = 0xE199};200201bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,202ucontext_t* uc, JavaThread* thread) {203204/*205NOTE: does not seem to work on linux.206if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {207// can't decode this kind of signal208info = NULL;209} else {210assert(sig == info->si_signo, "bad siginfo");211}212*/213// decide if this trap can be handled by a stub214address stub = NULL;215216address pc = NULL;217218//%note os_trap_1219if (info != NULL && uc != NULL && thread != NULL) {220pc = (address) os::Posix::ucontext_get_pc(uc);221222#ifndef AMD64223// Halt if SI_KERNEL before more crashes get misdiagnosed as Java bugs224// This can happen in any running code (currently more frequently in225// interpreter code but has been seen in compiled code)226if (sig == SIGSEGV && info->si_addr == 0 && info->si_code == SI_KERNEL) {227fatal("An irrecoverable SI_KERNEL SIGSEGV has occurred due "228"to unstable signal handling in this distribution.");229}230#endif // AMD64231232// Handle ALL stack overflow variations here233if (sig == SIGSEGV) {234address addr = (address) info->si_addr;235236// check if fault address is within thread stack237if (thread->is_in_full_stack(addr)) {238// stack overflow239if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {240return true; // continue241}242}243}244245if ((sig == SIGSEGV) && VM_Version::is_cpuinfo_segv_addr(pc)) {246// Verify that OS save/restore AVX registers.247stub = VM_Version::cpuinfo_cont_addr();248}249250if (thread->thread_state() == _thread_in_Java) {251// Java thread running in Java code => find exception handler if any252// a fault inside compiled code, the interpreter, or a stub253254if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) {255stub = SharedRuntime::get_poll_stub(pc);256} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {257// BugId 4454115: A read from a MappedByteBuffer can fault258// here if the underlying file has been truncated.259// Do not crash the VM in such a case.260CodeBlob* cb = CodeCache::find_blob_unsafe(pc);261CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;262bool is_unsafe_arraycopy = thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc);263if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {264address next_pc = Assembler::locate_next_instruction(pc);265if (is_unsafe_arraycopy) {266next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);267}268stub = SharedRuntime::handle_unsafe_access(thread, next_pc);269}270}271else272273#ifdef AMD64274if (sig == SIGFPE &&275(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {276stub =277SharedRuntime::278continuation_for_implicit_exception(thread,279pc,280SharedRuntime::281IMPLICIT_DIVIDE_BY_ZERO);282#else283if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {284// HACK: si_code does not work on linux 2.2.12-20!!!285int op = pc[0];286if (op == 0xDB) {287// FIST288// TODO: The encoding of D2I in x86_32.ad can cause an exception289// prior to the fist instruction if there was an invalid operation290// pending. We want to dismiss that exception. From the win_32291// side it also seems that if it really was the fist causing292// the exception that we do the d2i by hand with different293// rounding. Seems kind of weird.294// NOTE: that we take the exception at the NEXT floating point instruction.295assert(pc[0] == 0xDB, "not a FIST opcode");296assert(pc[1] == 0x14, "not a FIST opcode");297assert(pc[2] == 0x24, "not a FIST opcode");298return true;299} else if (op == 0xF7) {300// IDIV301stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);302} else {303// TODO: handle more cases if we are using other x86 instructions304// that can generate SIGFPE signal on linux.305tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);306fatal("please update this code.");307}308#endif // AMD64309} else if (sig == SIGSEGV &&310MacroAssembler::uses_implicit_null_check(info->si_addr)) {311// Determination of interpreter/vtable stub/compiled code null exception312stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);313}314} else if ((thread->thread_state() == _thread_in_vm ||315thread->thread_state() == _thread_in_native) &&316(sig == SIGBUS && /* info->si_code == BUS_OBJERR && */317thread->doing_unsafe_access())) {318address next_pc = Assembler::locate_next_instruction(pc);319if (UnsafeCopyMemory::contains_pc(pc)) {320next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);321}322stub = SharedRuntime::handle_unsafe_access(thread, next_pc);323}324325// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in326// and the heap gets shrunk before the field access.327if ((sig == SIGSEGV) || (sig == SIGBUS)) {328address addr = JNI_FastGetField::find_slowcase_pc(pc);329if (addr != (address)-1) {330stub = addr;331}332}333}334335#ifndef AMD64336// Execution protection violation337//338// This should be kept as the last step in the triage. We don't339// have a dedicated trap number for a no-execute fault, so be340// conservative and allow other handlers the first shot.341//342// Note: We don't test that info->si_code == SEGV_ACCERR here.343// this si_code is so generic that it is almost meaningless; and344// the si_code for this condition may change in the future.345// Furthermore, a false-positive should be harmless.346if (UnguardOnExecutionViolation > 0 &&347stub == NULL &&348(sig == SIGSEGV || sig == SIGBUS) &&349uc->uc_mcontext.gregs[REG_TRAPNO] == trap_page_fault) {350int page_size = os::vm_page_size();351address addr = (address) info->si_addr;352address pc = os::Posix::ucontext_get_pc(uc);353// Make sure the pc and the faulting address are sane.354//355// If an instruction spans a page boundary, and the page containing356// the beginning of the instruction is executable but the following357// page is not, the pc and the faulting address might be slightly358// different - we still want to unguard the 2nd page in this case.359//360// 15 bytes seems to be a (very) safe value for max instruction size.361bool pc_is_near_addr =362(pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);363bool instr_spans_page_boundary =364(align_down((intptr_t) pc ^ (intptr_t) addr,365(intptr_t) page_size) > 0);366367if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {368static volatile address last_addr =369(address) os::non_memory_address_word();370371// In conservative mode, don't unguard unless the address is in the VM372if (addr != last_addr &&373(UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {374375// Set memory to RWX and retry376address page_start = align_down(addr, page_size);377bool res = os::protect_memory((char*) page_start, page_size,378os::MEM_PROT_RWX);379380log_debug(os)("Execution protection violation "381"at " INTPTR_FORMAT382", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr),383p2i(page_start), (res ? "success" : "failed"), errno);384stub = pc;385386// Set last_addr so if we fault again at the same address, we don't end387// up in an endless loop.388//389// There are two potential complications here. Two threads trapping at390// the same address at the same time could cause one of the threads to391// think it already unguarded, and abort the VM. Likely very rare.392//393// The other race involves two threads alternately trapping at394// different addresses and failing to unguard the page, resulting in395// an endless loop. This condition is probably even more unlikely than396// the first.397//398// Although both cases could be avoided by using locks or thread local399// last_addr, these solutions are unnecessary complication: this400// handler is a best-effort safety net, not a complete solution. It is401// disabled by default and should only be used as a workaround in case402// we missed any no-execute-unsafe VM code.403404last_addr = addr;405}406}407}408#endif // !AMD64409410if (stub != NULL) {411// save all thread context in case we need to restore it412if (thread != NULL) thread->set_saved_exception_pc(pc);413414os::Posix::ucontext_set_pc(uc, stub);415return true;416}417418return false;419}420421void os::Linux::init_thread_fpu_state(void) {422#ifndef AMD64423// set fpu to 53 bit precision424set_fpu_control_word(0x27f);425#endif // !AMD64426}427428int os::Linux::get_fpu_control_word(void) {429#ifdef AMD64430return 0;431#else432int fpu_control;433_FPU_GETCW(fpu_control);434return fpu_control & 0xffff;435#endif // AMD64436}437438void os::Linux::set_fpu_control_word(int fpu_control) {439#ifndef AMD64440_FPU_SETCW(fpu_control);441#endif // !AMD64442}443444// Check that the linux kernel version is 2.4 or higher since earlier445// versions do not support SSE without patches.446bool os::supports_sse() {447#ifdef AMD64448return true;449#else450struct utsname uts;451if( uname(&uts) != 0 ) return false; // uname fails?452char *minor_string;453int major = strtol(uts.release,&minor_string,10);454int minor = strtol(minor_string+1,NULL,10);455bool result = (major > 2 || (major==2 && minor >= 4));456log_info(os)("OS version is %d.%d, which %s support SSE/SSE2",457major,minor, result ? "DOES" : "does NOT");458return result;459#endif // AMD64460}461462juint os::cpu_microcode_revision() {463juint result = 0;464char data[2048] = {0}; // lines should fit in 2K buf465size_t len = sizeof(data);466FILE *fp = fopen("/proc/cpuinfo", "r");467if (fp) {468while (!feof(fp)) {469if (fgets(data, len, fp)) {470if (strstr(data, "microcode") != NULL) {471char* rev = strchr(data, ':');472if (rev != NULL) sscanf(rev + 1, "%x", &result);473break;474}475}476}477fclose(fp);478}479return result;480}481482////////////////////////////////////////////////////////////////////////////////483// thread stack484485// Minimum usable stack sizes required to get to user code. Space for486// HotSpot guard pages is added later.487size_t os::Posix::_compiler_thread_min_stack_allowed = 48 * K;488size_t os::Posix::_java_thread_min_stack_allowed = 40 * K;489#ifdef _LP64490size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;491#else492size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;493#endif // _LP64494495// return default stack size for thr_type496size_t os::Posix::default_stack_size(os::ThreadType thr_type) {497// default stack size (compiler thread needs larger stack)498#ifdef AMD64499size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);500#else501size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);502#endif // AMD64503return s;504}505506/////////////////////////////////////////////////////////////////////////////507// helper functions for fatal error handler508509void os::print_context(outputStream *st, const void *context) {510if (context == NULL) return;511512const ucontext_t *uc = (const ucontext_t*)context;513st->print_cr("Registers:");514#ifdef AMD64515st->print( "RAX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RAX]);516st->print(", RBX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RBX]);517st->print(", RCX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RCX]);518st->print(", RDX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RDX]);519st->cr();520st->print( "RSP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RSP]);521st->print(", RBP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RBP]);522st->print(", RSI=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RSI]);523st->print(", RDI=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RDI]);524st->cr();525st->print( "R8 =" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R8]);526st->print(", R9 =" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R9]);527st->print(", R10=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R10]);528st->print(", R11=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R11]);529st->cr();530st->print( "R12=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R12]);531st->print(", R13=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R13]);532st->print(", R14=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R14]);533st->print(", R15=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R15]);534st->cr();535st->print( "RIP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RIP]);536st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_EFL]);537st->print(", CSGSFS=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_CSGSFS]);538st->print(", ERR=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_ERR]);539st->cr();540st->print(" TRAPNO=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_TRAPNO]);541#else542st->print( "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EAX]);543st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EBX]);544st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ECX]);545st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EDX]);546st->cr();547st->print( "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_UESP]);548st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EBP]);549st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ESI]);550st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EDI]);551st->cr();552st->print( "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EIP]);553st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EFL]);554st->print(", CR2=" PTR64_FORMAT, (uint64_t)uc->uc_mcontext.cr2);555#endif // AMD64556st->cr();557st->cr();558559intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);560st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));561print_hex_dump(st, (address)sp, (address)(sp + 8), sizeof(intptr_t));562st->cr();563564// Note: it may be unsafe to inspect memory near pc. For example, pc may565// point to garbage if entry point in an nmethod is corrupted. Leave566// this at the end, and hope for the best.567address pc = os::Posix::ucontext_get_pc(uc);568print_instructions(st, pc, sizeof(char));569st->cr();570}571572void os::print_register_info(outputStream *st, const void *context) {573if (context == NULL) return;574575const ucontext_t *uc = (const ucontext_t*)context;576577st->print_cr("Register to memory mapping:");578st->cr();579580// this is horrendously verbose but the layout of the registers in the581// context does not match how we defined our abstract Register set, so582// we can't just iterate through the gregs area583584// this is only for the "general purpose" registers585586#ifdef AMD64587st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);588st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);589st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);590st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);591st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);592st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);593st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);594st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);595st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);596st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);597st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);598st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);599st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);600st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);601st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);602st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);603#else604st->print("EAX="); print_location(st, uc->uc_mcontext.gregs[REG_EAX]);605st->print("EBX="); print_location(st, uc->uc_mcontext.gregs[REG_EBX]);606st->print("ECX="); print_location(st, uc->uc_mcontext.gregs[REG_ECX]);607st->print("EDX="); print_location(st, uc->uc_mcontext.gregs[REG_EDX]);608st->print("ESP="); print_location(st, uc->uc_mcontext.gregs[REG_ESP]);609st->print("EBP="); print_location(st, uc->uc_mcontext.gregs[REG_EBP]);610st->print("ESI="); print_location(st, uc->uc_mcontext.gregs[REG_ESI]);611st->print("EDI="); print_location(st, uc->uc_mcontext.gregs[REG_EDI]);612#endif // AMD64613614st->cr();615}616617void os::setup_fpu() {618#ifndef AMD64619address fpu_cntrl = StubRoutines::x86::addr_fpu_cntrl_wrd_std();620__asm__ volatile ( "fldcw (%0)" :621: "r" (fpu_cntrl) : "memory");622#endif // !AMD64623}624625#ifndef PRODUCT626void os::verify_stack_alignment() {627#ifdef AMD64628assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");629#endif630}631#endif632633634/*635* IA32 only: execute code at a high address in case buggy NX emulation is present. I.e. avoid CS limit636* updates (JDK-8023956).637*/638void os::workaround_expand_exec_shield_cs_limit() {639#if defined(IA32)640assert(Linux::initial_thread_stack_bottom() != NULL, "sanity");641size_t page_size = os::vm_page_size();642643/*644* JDK-8197429645*646* Expand the stack mapping to the end of the initial stack before647* attempting to install the codebuf. This is needed because newer648* Linux kernels impose a distance of a megabyte between stack649* memory and other memory regions. If we try to install the650* codebuf before expanding the stack the installation will appear651* to succeed but we'll get a segfault later if we expand the stack652* in Java code.653*654*/655if (os::is_primordial_thread()) {656address limit = Linux::initial_thread_stack_bottom();657if (! DisablePrimordialThreadGuardPages) {658limit += StackOverflow::stack_red_zone_size() +659StackOverflow::stack_yellow_zone_size();660}661os::Linux::expand_stack_to(limit);662}663664/*665* Take the highest VA the OS will give us and exec666*667* Although using -(pagesz) as mmap hint works on newer kernel as you would668* think, older variants affected by this work-around don't (search forward only).669*670* On the affected distributions, we understand the memory layout to be:671*672* TASK_LIMIT= 3G, main stack base close to TASK_LIMT.673*674* A few pages south main stack will do it.675*676* If we are embedded in an app other than launcher (initial != main stack),677* we don't have much control or understanding of the address space, just let it slide.678*/679char* hint = (char*)(Linux::initial_thread_stack_bottom() -680(StackOverflow::stack_guard_zone_size() + page_size));681char* codebuf = os::attempt_reserve_memory_at(hint, page_size);682683if (codebuf == NULL) {684// JDK-8197429: There may be a stack gap of one megabyte between685// the limit of the stack and the nearest memory region: this is a686// Linux kernel workaround for CVE-2017-1000364. If we failed to687// map our codebuf, try again at an address one megabyte lower.688hint -= 1 * M;689codebuf = os::attempt_reserve_memory_at(hint, page_size);690}691692if ((codebuf == NULL) || (!os::commit_memory(codebuf, page_size, true))) {693return; // No matter, we tried, best effort.694}695696MemTracker::record_virtual_memory_type((address)codebuf, mtInternal);697698log_info(os)("[CS limit NX emulation work-around, exec code at: %p]", codebuf);699700// Some code to exec: the 'ret' instruction701codebuf[0] = 0xC3;702703// Call the code in the codebuf704__asm__ volatile("call *%0" : : "r"(codebuf));705706// keep the page mapped so CS limit isn't reduced.707#endif708}709710int os::extra_bang_size_in_bytes() {711// JDK-8050147 requires the full cache line bang for x86.712return VM_Version::L1_line_size();713}714715716