jdk
494 строки · 17.6 Кб
1/*
2* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
3* Copyright (c) 2016, 2023 SAP SE. All rights reserved.
4* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*
6* This code is free software; you can redistribute it and/or modify it
7* under the terms of the GNU General Public License version 2 only, as
8* published by the Free Software Foundation.
9*
10* This code is distributed in the hope that it will be useful, but WITHOUT
11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13* version 2 for more details (a copy is included in the LICENSE file that
14* accompanied this code).
15*
16* You should have received a copy of the GNU General Public License version
17* 2 along with this work; if not, write to the Free Software Foundation,
18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19*
20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21* or visit www.oracle.com if you need additional information or have any
22* questions.
23*
24*/
25
26// This file is organized as os_linux_x86.cpp.
27
28// no precompiled headers
29#include "asm/assembler.inline.hpp"30#include "classfile/vmSymbols.hpp"31#include "code/nativeInst.hpp"32#include "code/vtableStubs.hpp"33#include "compiler/disassembler.hpp"34#include "interpreter/interpreter.hpp"35#include "jvm.h"36#include "memory/allocation.inline.hpp"37#include "nativeInst_s390.hpp"38#include "os_linux.hpp"39#include "os_posix.hpp"40#include "prims/jniFastGetField.hpp"41#include "prims/jvm_misc.hpp"42#include "runtime/arguments.hpp"43#include "runtime/frame.inline.hpp"44#include "runtime/interfaceSupport.inline.hpp"45#include "runtime/java.hpp"46#include "runtime/javaCalls.hpp"47#include "runtime/javaThread.hpp"48#include "runtime/mutexLocker.hpp"49#include "runtime/osThread.hpp"50#include "runtime/safepointMechanism.hpp"51#include "runtime/sharedRuntime.hpp"52#include "runtime/stubRoutines.hpp"53#include "runtime/timer.hpp"54#include "signals_posix.hpp"55#include "utilities/events.hpp"56#include "utilities/debug.hpp"57#include "utilities/vmError.hpp"58
59// put OS-includes here
60# include <sys/types.h>61# include <sys/mman.h>62# include <pthread.h>63# include <signal.h>64# include <errno.h>65# include <dlfcn.h>66# include <stdlib.h>67# include <stdio.h>68# include <unistd.h>69# include <sys/resource.h>70# include <pthread.h>71# include <sys/stat.h>72# include <sys/time.h>73# include <sys/utsname.h>74# include <sys/socket.h>75# include <sys/wait.h>76# include <pwd.h>77# include <poll.h>78# include <ucontext.h>79
80address os::current_stack_pointer() {81intptr_t* csp;82
83// Inline assembly for `z_lgr regno(csp), Z_SP' (Z_SP = Z_R15):84__asm__ __volatile__ ("lgr %0, 15":"=r"(csp):);85
86assert(((uint64_t)csp & (frame::alignment_in_bytes-1)) == 0, "SP must be aligned");87return (address) csp;88}
89
90char* os::non_memory_address_word() {91// Must never look like an address returned by reserve_memory,92// even in its subfields (as defined by the CPU immediate fields,93// if the CPU splits constants across multiple instructions).94return (char*) -1;95}
96
97// Frame information (pc, sp, fp) retrieved via ucontext
98// always looks like a C-frame according to the frame
99// conventions in frame_s390.hpp.
100address os::Posix::ucontext_get_pc(const ucontext_t * uc) {101return (address)uc->uc_mcontext.psw.addr;102}
103
104void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {105uc->uc_mcontext.psw.addr = (unsigned long)pc;106}
107
108static address ucontext_get_lr(const ucontext_t * uc) {109return (address)uc->uc_mcontext.gregs[14/*LINK*/];110}
111
112intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {113return (intptr_t*)uc->uc_mcontext.gregs[15/*REG_SP*/];114}
115
116intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {117return nullptr;118}
119
120address os::fetch_frame_from_context(const void* ucVoid,121intptr_t** ret_sp, intptr_t** ret_fp) {122
123address epc;124const ucontext_t* uc = (const ucontext_t*)ucVoid;125
126if (uc != nullptr) {127epc = os::Posix::ucontext_get_pc(uc);128if (ret_sp) { *ret_sp = os::Linux::ucontext_get_sp(uc); }129if (ret_fp) { *ret_fp = os::Linux::ucontext_get_fp(uc); }130} else {131epc = nullptr;132if (ret_sp) { *ret_sp = (intptr_t *)nullptr; }133if (ret_fp) { *ret_fp = (intptr_t *)nullptr; }134}135
136return epc;137}
138
139frame os::fetch_frame_from_context(const void* ucVoid) {140intptr_t* sp;141intptr_t* fp;142address epc = fetch_frame_from_context(ucVoid, &sp, &fp);143return frame(sp, epc);144}
145
146frame os::fetch_compiled_frame_from_context(const void* ucVoid) {147const ucontext_t* uc = (const ucontext_t*)ucVoid;148intptr_t* sp = os::Linux::ucontext_get_sp(uc);149address lr = ucontext_get_lr(uc);150return frame(sp, lr);151}
152
153frame os::get_sender_for_C_frame(frame* fr) {154if (*fr->sp() == 0) {155// fr is the last C frame.156return frame();157}158
159// If its not one of our frames, the return pc is saved at gpr14160// stack slot. The call_stub stores the return_pc to the stack slot161// of gpr10.162if ((Interpreter::code() != nullptr && Interpreter::contains(fr->pc())) ||163(CodeCache::contains(fr->pc()) && !StubRoutines::contains(fr->pc()))) {164return frame(fr->sender_sp(), fr->sender_pc());165} else {166if (StubRoutines::contains(fr->pc())) {167StubCodeDesc* desc = StubCodeDesc::desc_for(fr->pc());168if (desc && !strcmp(desc->name(),"call_stub")) {169return frame(fr->sender_sp(), fr->callstub_sender_pc());170} else {171return frame(fr->sender_sp(), fr->sender_pc());172}173} else {174intptr_t* sender_sp = fr->sender_sp();175address sender_fp = (address)*sender_sp;176ptrdiff_t entry_len = sender_fp - (address)sender_sp;177if (entry_len < frame::z_abi_160_size) {178return frame(sender_sp, fr->sender_pc());179} else {180return frame(sender_sp, fr->native_sender_pc());181}182}183}184}
185
186frame os::current_frame() {187// Expected to return the stack pointer of this method.188// But if inlined, returns the stack pointer of our caller!189intptr_t* csp = (intptr_t*) *((intptr_t*) os::current_stack_pointer());190assert (csp != nullptr, "sp should not be null");191// Pass a dummy pc. This way we don't have to load it from the192// stack, since we don't know in which slot we can find it.193frame topframe(csp, (address)0x8);194if (os::is_first_C_frame(&topframe)) {195// Stack is not walkable.196return frame();197} else {198frame senderFrame = os::get_sender_for_C_frame(&topframe);199assert(senderFrame.pc() != nullptr, "Sender pc should not be null");200// Return sender of sender of current topframe which hopefully201// both have pc != nullptr.202#ifdef _NMT_NOINLINE_ // Is set in slowdebug builds.203// Current_stack_pointer is not inlined, we must pop one more frame.204frame tmp = os::get_sender_for_C_frame(&topframe);205return os::get_sender_for_C_frame(&tmp);206#else207return os::get_sender_for_C_frame(&topframe);208#endif209}210}
211
212bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,213ucontext_t* uc, JavaThread* thread) {214
215// Decide if this trap can be handled by a stub.216address stub = nullptr;217address pc = nullptr; // Pc as retrieved from PSW. Usually points past failing instruction.218address trap_pc = nullptr; // Pc of the instruction causing the trap.219
220//%note os_trap_1221if (info != nullptr && uc != nullptr && thread != nullptr) {222pc = os::Posix::ucontext_get_pc(uc);223if (TraceTraps) {224tty->print_cr(" pc at " INTPTR_FORMAT, p2i(pc));225}226if ((unsigned long)(pc - (address)info->si_addr) <= (unsigned long)Assembler::instr_maxlen() ) {227trap_pc = (address)info->si_addr;228if (TraceTraps) {229tty->print_cr("trap_pc at " INTPTR_FORMAT, p2i(trap_pc));230}231}232
233// Handle ALL stack overflow variations here234if (sig == SIGSEGV) {235address addr = (address)info->si_addr; // Address causing SIGSEGV, usually mem ref target.236
237// Check if fault address is within thread stack.238if (thread->is_in_full_stack(addr)) {239// stack overflow240if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {241return true; // continue242}243}244}245
246if (thread->thread_state() == _thread_in_Java) {247// Java thread running in Java code => find exception handler if any248// a fault inside compiled code, the interpreter, or a stub249
250// Handle signal from NativeJump::patch_verified_entry().251if (sig == SIGILL && nativeInstruction_at(pc)->is_sigill_not_entrant()) {252if (TraceTraps) {253tty->print_cr("trap: not_entrant (SIGILL)");254}255stub = SharedRuntime::get_handle_wrong_method_stub();256}257
258else if (sig == SIGSEGV &&259SafepointMechanism::is_poll_address((address)info->si_addr)) {260if (TraceTraps) {261tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));262}263stub = SharedRuntime::get_poll_stub(pc);264
265// Info->si_addr only points to the page base address, so we266// must extract the real si_addr from the instruction and the267// ucontext.268assert(((NativeInstruction*)pc)->is_safepoint_poll(), "must be safepoint poll");269const address real_si_addr = ((NativeInstruction*)pc)->get_poll_address(uc);270}271
272// SIGTRAP-based implicit null check in compiled code.273else if ((sig == SIGFPE) &&274TrapBasedNullChecks &&275(trap_pc != nullptr) &&276Assembler::is_sigtrap_zero_check(trap_pc)) {277if (TraceTraps) {278tty->print_cr("trap: NULL_CHECK at " INTPTR_FORMAT " (SIGFPE)", p2i(trap_pc));279}280stub = SharedRuntime::continuation_for_implicit_exception(thread, trap_pc, SharedRuntime::IMPLICIT_NULL);281}282
283else if (sig == SIGSEGV && ImplicitNullChecks &&284CodeCache::contains((void*) pc) &&285MacroAssembler::uses_implicit_null_check(info->si_addr)) {286if (TraceTraps) {287tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));288}289stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);290}291
292#ifdef COMPILER2293// SIGTRAP-based implicit range check in compiled code.294else if (sig == SIGFPE && TrapBasedRangeChecks &&295(trap_pc != nullptr) &&296Assembler::is_sigtrap_range_check(trap_pc)) {297if (TraceTraps) {298tty->print_cr("trap: RANGE_CHECK at " INTPTR_FORMAT " (SIGFPE)", p2i(trap_pc));299}300stub = SharedRuntime::continuation_for_implicit_exception(thread, trap_pc, SharedRuntime::IMPLICIT_NULL);301}302#endif303
304else if (sig == SIGFPE && info->si_code == FPE_INTDIV) {305stub = SharedRuntime::continuation_for_implicit_exception(thread, trap_pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);306}307
308else if (sig == SIGBUS) {309// BugId 4454115: A read from a MappedByteBuffer can fault here if the310// underlying file has been truncated. Do not crash the VM in such a case.311CodeBlob* cb = CodeCache::find_blob(pc);312nmethod* nm = (cb != nullptr) ? cb->as_nmethod_or_null() : nullptr;313if (nm != nullptr && nm->has_unsafe_access()) {314// We don't really need a stub here! Just set the pending exception and315// continue at the next instruction after the faulting read. Returning316// garbage from this read is ok.317thread->set_pending_unsafe_access_error();318uc->uc_mcontext.psw.addr = ((unsigned long)pc) + Assembler::instr_len(pc);319return true;320}321}322}323
324else { // thread->thread_state() != _thread_in_Java325if ((sig == SIGILL) && VM_Version::is_determine_features_test_running()) {326// SIGILL must be caused by VM_Version::determine_features()327// when attempting to execute a non-existing instruction.328//*(int *) (pc-6)=0; // Patch instruction to 0 to indicate that it causes a SIGILL.329// Flushing of icache is not necessary.330stub = pc; // Continue with next instruction.331} else if ((sig == SIGFPE) && VM_Version::is_determine_features_test_running()) {332// SIGFPE is known to be caused by trying to execute a vector instruction333// when the vector facility is installed, but operating system support is missing.334VM_Version::reset_has_VectorFacility();335stub = pc; // Continue with next instruction.336} else if ((thread->thread_state() == _thread_in_vm ||337thread->thread_state() == _thread_in_native) &&338sig == SIGBUS && thread->doing_unsafe_access()) {339// We don't really need a stub here! Just set the pending exception and340// continue at the next instruction after the faulting read. Returning341// garbage from this read is ok.342thread->set_pending_unsafe_access_error();343os::Posix::ucontext_set_pc(uc, pc + Assembler::instr_len(pc));344return true;345}346}347
348// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in349// and the heap gets shrunk before the field access.350if ((sig == SIGSEGV) || (sig == SIGBUS)) {351address addr = JNI_FastGetField::find_slowcase_pc(pc);352if (addr != (address)-1) {353stub = addr;354}355}356}357
358if (stub != nullptr) {359// Save all thread context in case we need to restore it.360if (thread != nullptr) thread->set_saved_exception_pc(pc);361os::Posix::ucontext_set_pc(uc, stub);362return true;363}364
365return false;366}
367
368void os::Linux::init_thread_fpu_state(void) {369// Nothing to do on z/Architecture.370}
371
372int os::Linux::get_fpu_control_word(void) {373// Nothing to do on z/Architecture.374return 0;375}
376
377void os::Linux::set_fpu_control_word(int fpu_control) {378// Nothing to do on z/Architecture.379}
380
381////////////////////////////////////////////////////////////////////////////////
382// thread stack
383
384// Minimum usable stack sizes required to get to user code. Space for
385// HotSpot guard pages is added later.
386size_t os::_compiler_thread_min_stack_allowed = (52 DEBUG_ONLY(+ 32)) * K;387size_t os::_java_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 8)) * K;388size_t os::_vm_internal_thread_min_stack_allowed = 32 * K;389
390// Return default stack size for thr_type.
391size_t os::Posix::default_stack_size(os::ThreadType thr_type) {392// Default stack size (compiler thread needs larger stack).393size_t s = (thr_type == os::compiler_thread ? 4 * M : 1024 * K);394return s;395}
396
397/////////////////////////////////////////////////////////////////////////////
398// helper functions for fatal error handler
399
400void os::print_context(outputStream *st, const void *context) {401if (context == nullptr) return;402
403const ucontext_t* uc = (const ucontext_t*)context;404
405st->print_cr("Processor state:");406st->print_cr("----------------");407st->print_cr(" ip = " INTPTR_FORMAT " ", uc->uc_mcontext.psw.addr);408st->print_cr(" proc mask = " INTPTR_FORMAT " ", uc->uc_mcontext.psw.mask);409st->print_cr(" fpc reg = 0x%8.8x " , uc->uc_mcontext.fpregs.fpc);410st->cr();411
412st->print_cr("General Purpose Registers:");413st->print_cr("--------------------------");414for( int i = 0; i < 16; i+=2 ) {415st->print(" r%-2d = " INTPTR_FORMAT " " , i, uc->uc_mcontext.gregs[i]);416st->print(" r%-2d = " INTPTR_FORMAT " |", i+1, uc->uc_mcontext.gregs[i+1]);417st->print(" r%-2d = %23.1ld " , i, uc->uc_mcontext.gregs[i]);418st->print(" r%-2d = %23.1ld " , i+1, uc->uc_mcontext.gregs[i+1]);419st->cr();420}421st->cr();422
423st->print_cr("Access Registers:");424st->print_cr("-----------------");425for( int i = 0; i < 16; i+=2 ) {426st->print(" ar%-2d = 0x%8.8x ", i, uc->uc_mcontext.aregs[i]);427st->print(" ar%-2d = 0x%8.8x ", i+1, uc->uc_mcontext.aregs[i+1]);428st->cr();429}430st->cr();431
432st->print_cr("Float Registers:");433st->print_cr("----------------");434for (int i = 0; i < 16; i += 2) {435st->print(" fr%-2d = " INTPTR_FORMAT " " , i, (int64_t)(uc->uc_mcontext.fpregs.fprs[i].d));436st->print(" fr%-2d = " INTPTR_FORMAT " |", i+1, (int64_t)(uc->uc_mcontext.fpregs.fprs[i+1].d));437st->print(" fr%-2d = %23.15e " , i, (uc->uc_mcontext.fpregs.fprs[i].d));438st->print(" fr%-2d = %23.15e " , i+1, (uc->uc_mcontext.fpregs.fprs[i+1].d));439st->cr();440}441st->cr();442st->cr();443}
444
445void os::print_tos_pc(outputStream *st, const void *context) {446if (context == nullptr) return;447
448const ucontext_t* uc = (const ucontext_t*)context;449
450address sp = (address)os::Linux::ucontext_get_sp(uc);451print_tos(st, sp);452st->cr();453
454// Note: it may be unsafe to inspect memory near pc. For example, pc may455// point to garbage if entry point in an nmethod is corrupted. Leave456// this at the end, and hope for the best.457address pc = os::Posix::ucontext_get_pc(uc);458print_instructions(st, pc);459st->cr();460}
461
462void os::print_register_info(outputStream *st, const void *context, int& continuation) {463const int register_count = 16 /* r0-r15 */ + 1 /* pc */;464int n = continuation;465assert(n >= 0 && n <= register_count, "Invalid continuation value");466if (context == nullptr || n == register_count) {467return;468}469
470const ucontext_t *uc = (const ucontext_t*)context;471while (n < register_count) {472// Update continuation with next index before printing location473continuation = n + 1;474if (n == register_count - 1) {475st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.psw.addr);476} else {477st->print("r%-2d=", n);478print_location(st, uc->uc_mcontext.gregs[n]);479}480++n;481}482}
483
484#ifndef PRODUCT485void os::verify_stack_alignment() {486}
487#endif488
489int os::extra_bang_size_in_bytes() {490// z/Architecture does not require the additional stack bang.491return 0;492}
493
494void os::setup_fpu() {}495