2
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
// no precompiled headers
26
#include "asm/macroAssembler.hpp"
27
#include "classfile/vmSymbols.hpp"
28
#include "code/codeCache.hpp"
29
#include "code/vtableStubs.hpp"
30
#include "interpreter/interpreter.hpp"
32
#include "logging/log.hpp"
33
#include "memory/allocation.inline.hpp"
35
#include "os_posix.hpp"
36
#include "prims/jniFastGetField.hpp"
37
#include "prims/jvm_misc.hpp"
38
#include "runtime/arguments.hpp"
39
#include "runtime/frame.inline.hpp"
40
#include "runtime/interfaceSupport.inline.hpp"
41
#include "runtime/java.hpp"
42
#include "runtime/javaCalls.hpp"
43
#include "runtime/javaThread.hpp"
44
#include "runtime/mutexLocker.hpp"
45
#include "runtime/osThread.hpp"
46
#include "runtime/safepointMechanism.hpp"
47
#include "runtime/sharedRuntime.hpp"
48
#include "runtime/stubRoutines.hpp"
49
#include "runtime/timer.hpp"
50
#include "signals_posix.hpp"
51
#include "utilities/align.hpp"
52
#include "utilities/events.hpp"
53
#include "utilities/vmError.hpp"
55
// put OS-includes here
56
# include <sys/types.h>
65
# include <sys/resource.h>
68
# include <sys/utsname.h>
69
# include <sys/socket.h>
77
#if !defined(__APPLE__) && !defined(__NetBSD__)
78
# include <pthread_np.h>
81
// needed by current_stack_base_and_size() workaround for Mavericks
84
# include <sys/types.h>
85
# include <sys/sysctl.h>
86
# define DEFAULT_MAIN_THREAD_STACK_PAGES 2048
87
# define OS_X_10_9_0_KERNEL_MAJOR_VERSION 13
91
#define SPELL_REG_SP "rsp"
92
#define SPELL_REG_FP "rbp"
94
#define SPELL_REG_SP "esp"
95
#define SPELL_REG_FP "ebp"
99
# define context_trapno uc_mcontext.mc_trapno
101
# define context_pc uc_mcontext.mc_rip
102
# define context_sp uc_mcontext.mc_rsp
103
# define context_fp uc_mcontext.mc_rbp
104
# define context_rip uc_mcontext.mc_rip
105
# define context_rsp uc_mcontext.mc_rsp
106
# define context_rbp uc_mcontext.mc_rbp
107
# define context_rax uc_mcontext.mc_rax
108
# define context_rbx uc_mcontext.mc_rbx
109
# define context_rcx uc_mcontext.mc_rcx
110
# define context_rdx uc_mcontext.mc_rdx
111
# define context_rsi uc_mcontext.mc_rsi
112
# define context_rdi uc_mcontext.mc_rdi
113
# define context_r8 uc_mcontext.mc_r8
114
# define context_r9 uc_mcontext.mc_r9
115
# define context_r10 uc_mcontext.mc_r10
116
# define context_r11 uc_mcontext.mc_r11
117
# define context_r12 uc_mcontext.mc_r12
118
# define context_r13 uc_mcontext.mc_r13
119
# define context_r14 uc_mcontext.mc_r14
120
# define context_r15 uc_mcontext.mc_r15
121
# define context_flags uc_mcontext.mc_flags
122
# define context_err uc_mcontext.mc_err
124
# define context_pc uc_mcontext.mc_eip
125
# define context_sp uc_mcontext.mc_esp
126
# define context_fp uc_mcontext.mc_ebp
127
# define context_eip uc_mcontext.mc_eip
128
# define context_esp uc_mcontext.mc_esp
129
# define context_eax uc_mcontext.mc_eax
130
# define context_ebx uc_mcontext.mc_ebx
131
# define context_ecx uc_mcontext.mc_ecx
132
# define context_edx uc_mcontext.mc_edx
133
# define context_ebp uc_mcontext.mc_ebp
134
# define context_esi uc_mcontext.mc_esi
135
# define context_edi uc_mcontext.mc_edi
136
# define context_eflags uc_mcontext.mc_eflags
137
# define context_trapno uc_mcontext.mc_trapno
142
# if __DARWIN_UNIX03 && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
143
// 10.5 UNIX03 member name prefixes
144
#define DU3_PREFIX(s, m) __ ## s.__ ## m
146
#define DU3_PREFIX(s, m) s ## . ## m
150
# define context_pc context_rip
151
# define context_sp context_rsp
152
# define context_fp context_rbp
153
# define context_rip uc_mcontext->DU3_PREFIX(ss,rip)
154
# define context_rsp uc_mcontext->DU3_PREFIX(ss,rsp)
155
# define context_rax uc_mcontext->DU3_PREFIX(ss,rax)
156
# define context_rbx uc_mcontext->DU3_PREFIX(ss,rbx)
157
# define context_rcx uc_mcontext->DU3_PREFIX(ss,rcx)
158
# define context_rdx uc_mcontext->DU3_PREFIX(ss,rdx)
159
# define context_rbp uc_mcontext->DU3_PREFIX(ss,rbp)
160
# define context_rsi uc_mcontext->DU3_PREFIX(ss,rsi)
161
# define context_rdi uc_mcontext->DU3_PREFIX(ss,rdi)
162
# define context_r8 uc_mcontext->DU3_PREFIX(ss,r8)
163
# define context_r9 uc_mcontext->DU3_PREFIX(ss,r9)
164
# define context_r10 uc_mcontext->DU3_PREFIX(ss,r10)
165
# define context_r11 uc_mcontext->DU3_PREFIX(ss,r11)
166
# define context_r12 uc_mcontext->DU3_PREFIX(ss,r12)
167
# define context_r13 uc_mcontext->DU3_PREFIX(ss,r13)
168
# define context_r14 uc_mcontext->DU3_PREFIX(ss,r14)
169
# define context_r15 uc_mcontext->DU3_PREFIX(ss,r15)
170
# define context_flags uc_mcontext->DU3_PREFIX(ss,rflags)
171
# define context_trapno uc_mcontext->DU3_PREFIX(es,trapno)
172
# define context_err uc_mcontext->DU3_PREFIX(es,err)
174
# define context_pc context_eip
175
# define context_sp context_esp
176
# define context_fp context_ebp
177
# define context_eip uc_mcontext->DU3_PREFIX(ss,eip)
178
# define context_esp uc_mcontext->DU3_PREFIX(ss,esp)
179
# define context_eax uc_mcontext->DU3_PREFIX(ss,eax)
180
# define context_ebx uc_mcontext->DU3_PREFIX(ss,ebx)
181
# define context_ecx uc_mcontext->DU3_PREFIX(ss,ecx)
182
# define context_edx uc_mcontext->DU3_PREFIX(ss,edx)
183
# define context_ebp uc_mcontext->DU3_PREFIX(ss,ebp)
184
# define context_esi uc_mcontext->DU3_PREFIX(ss,esi)
185
# define context_edi uc_mcontext->DU3_PREFIX(ss,edi)
186
# define context_eflags uc_mcontext->DU3_PREFIX(ss,eflags)
187
# define context_trapno uc_mcontext->DU3_PREFIX(es,trapno)
192
# define context_trapno sc_trapno
194
# define context_pc sc_rip
195
# define context_sp sc_rsp
196
# define context_fp sc_rbp
197
# define context_rip sc_rip
198
# define context_rsp sc_rsp
199
# define context_rbp sc_rbp
200
# define context_rax sc_rax
201
# define context_rbx sc_rbx
202
# define context_rcx sc_rcx
203
# define context_rdx sc_rdx
204
# define context_rsi sc_rsi
205
# define context_rdi sc_rdi
206
# define context_r8 sc_r8
207
# define context_r9 sc_r9
208
# define context_r10 sc_r10
209
# define context_r11 sc_r11
210
# define context_r12 sc_r12
211
# define context_r13 sc_r13
212
# define context_r14 sc_r14
213
# define context_r15 sc_r15
214
# define context_flags sc_rflags
215
# define context_err sc_err
217
# define context_pc sc_eip
218
# define context_sp sc_esp
219
# define context_fp sc_ebp
220
# define context_eip sc_eip
221
# define context_esp sc_esp
222
# define context_eax sc_eax
223
# define context_ebx sc_ebx
224
# define context_ecx sc_ecx
225
# define context_edx sc_edx
226
# define context_ebp sc_ebp
227
# define context_esi sc_esi
228
# define context_edi sc_edi
229
# define context_eflags sc_eflags
230
# define context_trapno sc_trapno
235
# define context_trapno uc_mcontext.__gregs[_REG_TRAPNO]
237
# define __register_t __greg_t
238
# define context_pc uc_mcontext.__gregs[_REG_RIP]
239
# define context_sp uc_mcontext.__gregs[_REG_URSP]
240
# define context_fp uc_mcontext.__gregs[_REG_RBP]
241
# define context_rip uc_mcontext.__gregs[_REG_RIP]
242
# define context_rsp uc_mcontext.__gregs[_REG_URSP]
243
# define context_rax uc_mcontext.__gregs[_REG_RAX]
244
# define context_rbx uc_mcontext.__gregs[_REG_RBX]
245
# define context_rcx uc_mcontext.__gregs[_REG_RCX]
246
# define context_rdx uc_mcontext.__gregs[_REG_RDX]
247
# define context_rbp uc_mcontext.__gregs[_REG_RBP]
248
# define context_rsi uc_mcontext.__gregs[_REG_RSI]
249
# define context_rdi uc_mcontext.__gregs[_REG_RDI]
250
# define context_r8 uc_mcontext.__gregs[_REG_R8]
251
# define context_r9 uc_mcontext.__gregs[_REG_R9]
252
# define context_r10 uc_mcontext.__gregs[_REG_R10]
253
# define context_r11 uc_mcontext.__gregs[_REG_R11]
254
# define context_r12 uc_mcontext.__gregs[_REG_R12]
255
# define context_r13 uc_mcontext.__gregs[_REG_R13]
256
# define context_r14 uc_mcontext.__gregs[_REG_R14]
257
# define context_r15 uc_mcontext.__gregs[_REG_R15]
258
# define context_flags uc_mcontext.__gregs[_REG_RFL]
259
# define context_err uc_mcontext.__gregs[_REG_ERR]
261
# define context_pc uc_mcontext.__gregs[_REG_EIP]
262
# define context_sp uc_mcontext.__gregs[_REG_UESP]
263
# define context_fp uc_mcontext.__gregs[_REG_EBP]
264
# define context_eip uc_mcontext.__gregs[_REG_EIP]
265
# define context_esp uc_mcontext.__gregs[_REG_UESP]
266
# define context_eax uc_mcontext.__gregs[_REG_EAX]
267
# define context_ebx uc_mcontext.__gregs[_REG_EBX]
268
# define context_ecx uc_mcontext.__gregs[_REG_ECX]
269
# define context_edx uc_mcontext.__gregs[_REG_EDX]
270
# define context_ebp uc_mcontext.__gregs[_REG_EBP]
271
# define context_esi uc_mcontext.__gregs[_REG_ESI]
272
# define context_edi uc_mcontext.__gregs[_REG_EDI]
273
# define context_eflags uc_mcontext.__gregs[_REG_EFL]
274
# define context_trapno uc_mcontext.__gregs[_REG_TRAPNO]
278
address os::current_stack_pointer() {
279
#if defined(__clang__) || defined(__llvm__)
281
__asm__("mov %%" SPELL_REG_SP ", %0":"=r"(esp));
282
return (address) esp;
284
register void *esp __asm__ (SPELL_REG_SP);
285
return (address) esp;
289
char* os::non_memory_address_word() {
290
// Must never look like an address returned by reserve_memory,
291
// even in its subfields (as defined by the CPU immediate fields,
292
// if the CPU splits constants across multiple instructions).
297
address os::Posix::ucontext_get_pc(const ucontext_t * uc) {
298
return (address)uc->context_pc;
301
void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {
302
uc->context_pc = (intptr_t)pc ;
305
intptr_t* os::Bsd::ucontext_get_sp(const ucontext_t * uc) {
306
return (intptr_t*)uc->context_sp;
309
intptr_t* os::Bsd::ucontext_get_fp(const ucontext_t * uc) {
310
return (intptr_t*)uc->context_fp;
313
address os::fetch_frame_from_context(const void* ucVoid,
314
intptr_t** ret_sp, intptr_t** ret_fp) {
317
const ucontext_t* uc = (const ucontext_t*)ucVoid;
320
epc = os::Posix::ucontext_get_pc(uc);
321
if (ret_sp) *ret_sp = os::Bsd::ucontext_get_sp(uc);
322
if (ret_fp) *ret_fp = os::Bsd::ucontext_get_fp(uc);
325
if (ret_sp) *ret_sp = (intptr_t *)nullptr;
326
if (ret_fp) *ret_fp = (intptr_t *)nullptr;
332
frame os::fetch_frame_from_context(const void* ucVoid) {
335
address epc = fetch_frame_from_context(ucVoid, &sp, &fp);
336
return frame(sp, fp, epc);
339
frame os::fetch_compiled_frame_from_context(const void* ucVoid) {
340
const ucontext_t* uc = (const ucontext_t*)ucVoid;
341
frame fr = os::fetch_frame_from_context(uc);
342
// in compiled code, the stack banging is performed just after the return pc
343
// has been pushed on the stack
344
return frame(fr.sp() + 1, fr.fp(), (address)*(fr.sp()));
347
// By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get
348
// turned off by -fomit-frame-pointer,
349
frame os::get_sender_for_C_frame(frame* fr) {
350
return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
353
static intptr_t* _get_previous_fp() {
354
#if defined(__clang__) || defined(__llvm__)
356
__asm__("mov %%" SPELL_REG_FP ", %0":"=r"(ebp));
358
register intptr_t **ebp __asm__ (SPELL_REG_FP);
360
// ebp is for this frame (_get_previous_fp). We want the ebp for the
361
// caller of os::current_frame*(), so go up two frames. However, for
362
// optimized builds, _get_previous_fp() will be inlined, so only go
363
// up 1 frame in that case.
365
return **(intptr_t***)ebp;
372
frame os::current_frame() {
373
intptr_t* fp = _get_previous_fp();
374
frame myframe((intptr_t*)os::current_stack_pointer(),
376
CAST_FROM_FN_PTR(address, os::current_frame));
377
if (os::is_first_C_frame(&myframe)) {
378
// stack is not walkable
381
return os::get_sender_for_C_frame(&myframe);
385
// From IA32 System Programming Guide
387
trap_page_fault = 0xE
390
bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
391
ucontext_t* uc, JavaThread* thread) {
392
// decide if this trap can be handled by a stub
393
address stub = nullptr;
395
address pc = nullptr;
398
if (info != nullptr && uc != nullptr && thread != nullptr) {
399
pc = (address) os::Posix::ucontext_get_pc(uc);
401
// Handle ALL stack overflow variations here
402
if (sig == SIGSEGV || sig == SIGBUS) {
403
address addr = (address) info->si_addr;
405
// check if fault address is within thread stack
406
if (thread->is_in_full_stack(addr)) {
408
if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {
409
return true; // continue
414
if ((sig == SIGSEGV || sig == SIGBUS) && VM_Version::is_cpuinfo_segv_addr(pc)) {
415
// Verify that OS save/restore AVX registers.
416
stub = VM_Version::cpuinfo_cont_addr();
419
#if !defined(PRODUCT) && defined(_LP64)
420
if ((sig == SIGSEGV || sig == SIGBUS) && VM_Version::is_cpuinfo_segv_addr_apx(pc)) {
421
// Verify that OS save/restore APX registers.
422
stub = VM_Version::cpuinfo_cont_addr_apx();
423
VM_Version::clear_apx_test_state();
427
// We test if stub is already set (by the stack overflow code
428
// above) so it is not overwritten by the code that follows. This
429
// check is not required on other platforms, because on other
430
// platforms we check for SIGSEGV only or SIGBUS only, where here
431
// we have to check for both SIGSEGV and SIGBUS.
432
if (thread->thread_state() == _thread_in_Java && stub == nullptr) {
433
// Java thread running in Java code => find exception handler if any
434
// a fault inside compiled code, the interpreter, or a stub
436
if ((sig == SIGSEGV || sig == SIGBUS) && SafepointMechanism::is_poll_address((address)info->si_addr)) {
437
stub = SharedRuntime::get_poll_stub(pc);
438
#if defined(__APPLE__)
439
// 32-bit Darwin reports a SIGBUS for nearly all memory access exceptions.
440
// 64-bit Darwin may also use a SIGBUS (seen with compressed oops).
441
// Catching SIGBUS here prevents the implicit SIGBUS null check below from
442
// being called, so only do so if the implicit null check is not necessary.
443
} else if (sig == SIGBUS && !MacroAssembler::uses_implicit_null_check(info->si_addr)) {
445
} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {
447
// BugId 4454115: A read from a MappedByteBuffer can fault
448
// here if the underlying file has been truncated.
449
// Do not crash the VM in such a case.
450
CodeBlob* cb = CodeCache::find_blob(pc);
451
nmethod* nm = (cb != nullptr) ? cb->as_nmethod_or_null() : nullptr;
452
bool is_unsafe_memory_access = thread->doing_unsafe_access() && UnsafeMemoryAccess::contains_pc(pc);
453
if ((nm != nullptr && nm->has_unsafe_access()) || is_unsafe_memory_access) {
454
address next_pc = Assembler::locate_next_instruction(pc);
455
if (is_unsafe_memory_access) {
456
next_pc = UnsafeMemoryAccess::page_error_continue_pc(pc);
458
stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
463
(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV
464
// Workaround for macOS ARM incorrectly reporting FPE_FLTINV for "div by 0"
465
// instead of the expected FPE_FLTDIV when running x86_64 binary under Rosetta emulation
466
MACOS_ONLY(|| (VM_Version::is_cpu_emulated() && info->si_code == FPE_FLTINV)))) {
469
continuation_for_implicit_exception(thread,
472
IMPLICIT_DIVIDE_BY_ZERO);
474
} else if (sig == SIGFPE && info->si_code == FPE_NOOP) {
478
if ((pc[0] & 0xf0) == 0x40) {
486
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime:: IMPLICIT_DIVIDE_BY_ZERO);
488
// TODO: handle more cases if we are using other x86 instructions
489
// that can generate SIGFPE signal.
490
tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);
491
fatal("please update this code.");
493
#endif /* __APPLE__ */
496
if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {
497
// HACK: si_code does not work on bsd 2.2.12-20!!!
501
// TODO: The encoding of D2I in x86_32.ad can cause an exception
502
// prior to the fist instruction if there was an invalid operation
503
// pending. We want to dismiss that exception. From the win_32
504
// side it also seems that if it really was the fist causing
505
// the exception that we do the d2i by hand with different
506
// rounding. Seems kind of weird.
507
// NOTE: that we take the exception at the NEXT floating point instruction.
508
assert(pc[0] == 0xDB, "not a FIST opcode");
509
assert(pc[1] == 0x14, "not a FIST opcode");
510
assert(pc[2] == 0x24, "not a FIST opcode");
512
} else if (op == 0xF7) {
514
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
516
// TODO: handle more cases if we are using other x86 instructions
517
// that can generate SIGFPE signal on bsd.
518
tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);
519
fatal("please update this code.");
522
} else if ((sig == SIGSEGV || sig == SIGBUS) &&
523
MacroAssembler::uses_implicit_null_check(info->si_addr)) {
524
// Determination of interpreter/vtable stub/compiled code null exception
525
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
527
} else if ((thread->thread_state() == _thread_in_vm ||
528
thread->thread_state() == _thread_in_native) &&
529
sig == SIGBUS && /* info->si_code == BUS_OBJERR && */
530
thread->doing_unsafe_access()) {
531
address next_pc = Assembler::locate_next_instruction(pc);
532
if (UnsafeMemoryAccess::contains_pc(pc)) {
533
next_pc = UnsafeMemoryAccess::page_error_continue_pc(pc);
535
stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
538
// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
539
// and the heap gets shrunk before the field access.
540
if ((sig == SIGSEGV) || (sig == SIGBUS)) {
541
address addr = JNI_FastGetField::find_slowcase_pc(pc);
542
if (addr != (address)-1) {
549
// Execution protection violation
551
// This should be kept as the last step in the triage. We don't
552
// have a dedicated trap number for a no-execute fault, so be
553
// conservative and allow other handlers the first shot.
555
// Note: We don't test that info->si_code == SEGV_ACCERR here.
556
// this si_code is so generic that it is almost meaningless; and
557
// the si_code for this condition may change in the future.
558
// Furthermore, a false-positive should be harmless.
559
if (UnguardOnExecutionViolation > 0 &&
561
(sig == SIGSEGV || sig == SIGBUS) &&
562
uc->context_trapno == trap_page_fault) {
563
size_t page_size = os::vm_page_size();
564
address addr = (address) info->si_addr;
565
address pc = os::Posix::ucontext_get_pc(uc);
566
// Make sure the pc and the faulting address are sane.
568
// If an instruction spans a page boundary, and the page containing
569
// the beginning of the instruction is executable but the following
570
// page is not, the pc and the faulting address might be slightly
571
// different - we still want to unguard the 2nd page in this case.
573
// 15 bytes seems to be a (very) safe value for max instruction size.
574
bool pc_is_near_addr =
575
(pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
576
bool instr_spans_page_boundary =
577
(align_down((intptr_t) pc ^ (intptr_t) addr,
578
(intptr_t) page_size) > 0);
580
if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
581
static volatile address last_addr =
582
(address) os::non_memory_address_word();
584
// In conservative mode, don't unguard unless the address is in the VM
585
if (addr != last_addr &&
586
(UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
588
// Set memory to RWX and retry
589
address page_start = align_down(addr, page_size);
590
bool res = os::protect_memory((char*) page_start, page_size,
593
log_debug(os)("Execution protection violation "
595
", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr),
596
p2i(page_start), (res ? "success" : "failed"), errno);
599
// Set last_addr so if we fault again at the same address, we don't end
600
// up in an endless loop.
602
// There are two potential complications here. Two threads trapping at
603
// the same address at the same time could cause one of the threads to
604
// think it already unguarded, and abort the VM. Likely very rare.
606
// The other race involves two threads alternately trapping at
607
// different addresses and failing to unguard the page, resulting in
608
// an endless loop. This condition is probably even more unlikely than
611
// Although both cases could be avoided by using locks or thread local
612
// last_addr, these solutions are unnecessary complication: this
613
// handler is a best-effort safety net, not a complete solution. It is
614
// disabled by default and should only be used as a workaround in case
615
// we missed any no-execute-unsafe VM code.
623
if (stub != nullptr) {
624
// save all thread context in case we need to restore it
625
if (thread != nullptr) thread->set_saved_exception_pc(pc);
627
os::Posix::ucontext_set_pc(uc, stub);
634
// From solaris_i486.s ported to bsd_i486.s
635
extern "C" void fixcw();
637
void os::Bsd::init_thread_fpu_state(void) {
639
// Set fpu to 53 bit precision. This happens too early to use a stub.
644
juint os::cpu_microcode_revision() {
647
size_t sz = sizeof(data);
648
int ret = sysctlbyname("machdep.cpu.microcode_version", data, &sz, nullptr, 0);
650
if (sz == 4) result = *((juint*)data);
651
if (sz == 8) result = *((juint*)data + 1); // upper 32-bits
656
////////////////////////////////////////////////////////////////////////////////
659
// Minimum usable stack sizes required to get to user code. Space for
660
// HotSpot guard pages is added later.
661
size_t os::_compiler_thread_min_stack_allowed = 48 * K;
662
size_t os::_java_thread_min_stack_allowed = 48 * K;
664
size_t os::_vm_internal_thread_min_stack_allowed = 64 * K;
666
size_t os::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;
671
#define GET_GS() ({int gs; __asm__ volatile("movw %%gs, %w0":"=q"(gs)); gs&0xffff;})
675
// return default stack size for thr_type
676
size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
677
// default stack size (compiler thread needs larger stack)
679
size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
681
size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
689
// Low memory addresses
690
// +------------------------+
691
// | |\ Java thread created by VM does not have glibc
692
// | glibc guard page | - guard, attached Java thread usually has
693
// | |/ 1 glibc guard page.
694
// P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
696
// | HotSpot Guard Pages | - red, yellow and reserved pages
698
// +------------------------+ StackOverflow::stack_reserved_zone_base()
702
// P2 +------------------------+ Thread::stack_base()
706
// Low memory addresses
707
// +------------------------+
709
// | glibc guard page | - usually 1 page
711
// P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
715
// P2 +------------------------+ Thread::stack_base()
717
// ** P1 (aka bottom) and size are the address and stack size
718
// returned from pthread_attr_getstack().
719
// ** P2 (aka stack top or base) = P1 + size
721
void os::current_stack_base_and_size(address* base, size_t* size) {
724
pthread_t self = pthread_self();
725
*base = (address) pthread_get_stackaddr_np(self);
726
*size = pthread_get_stacksize_np(self);
727
// workaround for OS X 10.9.0 (Mavericks)
728
// pthread_get_stacksize_np returns 128 pages even though the actual size is 2048 pages
729
if (pthread_main_np() == 1) {
730
// At least on Mac OS 10.12 we have observed stack sizes not aligned
731
// to pages boundaries. This can be provoked by e.g. setrlimit() (ulimit -s xxxx in the
732
// shell). Apparently Mac OS actually rounds upwards to next multiple of page size,
733
// however, we round downwards here to be on the safe side.
734
*size = align_down(*size, getpagesize());
736
if ((*size) < (DEFAULT_MAIN_THREAD_STACK_PAGES * (size_t)getpagesize())) {
737
char kern_osrelease[256];
738
size_t kern_osrelease_size = sizeof(kern_osrelease);
739
int ret = sysctlbyname("kern.osrelease", kern_osrelease, &kern_osrelease_size, nullptr, 0);
741
// get the major number, atoi will ignore the minor amd micro portions of the version string
742
if (atoi(kern_osrelease) >= OS_X_10_9_0_KERNEL_MAJOR_VERSION) {
743
*size = (DEFAULT_MAIN_THREAD_STACK_PAGES*getpagesize());
748
bottom = *base - *size;
749
#elif defined(__OpenBSD__)
751
int rslt = pthread_stackseg_np(pthread_self(), &ss);
754
fatal("pthread_stackseg_np failed with error = %d", rslt);
756
*base = (address) ss.ss_sp;
758
bottom = *base - *size;
762
int rslt = pthread_attr_init(&attr);
764
// JVM needs to know exact stack location, abort if it fails
766
fatal("pthread_attr_init failed with error = %d", rslt);
768
rslt = pthread_attr_get_np(pthread_self(), &attr);
771
fatal("pthread_attr_get_np failed with error = %d", rslt);
773
if (pthread_attr_getstackaddr(&attr, (void **)&bottom) != 0 ||
774
pthread_attr_getstacksize(&attr, size) != 0) {
775
fatal("Can not locate current stack attributes!");
778
*base = bottom + *size;
780
pthread_attr_destroy(&attr);
782
assert(os::current_stack_pointer() >= bottom &&
783
os::current_stack_pointer() < *base, "just checking");
786
/////////////////////////////////////////////////////////////////////////////
787
// helper functions for fatal error handler
789
void os::print_context(outputStream *st, const void *context) {
790
if (context == nullptr) return;
792
const ucontext_t *uc = (const ucontext_t*)context;
794
st->print_cr("Registers:");
796
st->print( "RAX=" INTPTR_FORMAT, (intptr_t)uc->context_rax);
797
st->print(", RBX=" INTPTR_FORMAT, (intptr_t)uc->context_rbx);
798
st->print(", RCX=" INTPTR_FORMAT, (intptr_t)uc->context_rcx);
799
st->print(", RDX=" INTPTR_FORMAT, (intptr_t)uc->context_rdx);
801
st->print( "RSP=" INTPTR_FORMAT, (intptr_t)uc->context_rsp);
802
st->print(", RBP=" INTPTR_FORMAT, (intptr_t)uc->context_rbp);
803
st->print(", RSI=" INTPTR_FORMAT, (intptr_t)uc->context_rsi);
804
st->print(", RDI=" INTPTR_FORMAT, (intptr_t)uc->context_rdi);
806
st->print( "R8 =" INTPTR_FORMAT, (intptr_t)uc->context_r8);
807
st->print(", R9 =" INTPTR_FORMAT, (intptr_t)uc->context_r9);
808
st->print(", R10=" INTPTR_FORMAT, (intptr_t)uc->context_r10);
809
st->print(", R11=" INTPTR_FORMAT, (intptr_t)uc->context_r11);
811
st->print( "R12=" INTPTR_FORMAT, (intptr_t)uc->context_r12);
812
st->print(", R13=" INTPTR_FORMAT, (intptr_t)uc->context_r13);
813
st->print(", R14=" INTPTR_FORMAT, (intptr_t)uc->context_r14);
814
st->print(", R15=" INTPTR_FORMAT, (intptr_t)uc->context_r15);
816
st->print( "RIP=" INTPTR_FORMAT, (intptr_t)uc->context_rip);
817
st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->context_flags);
818
st->print(", ERR=" INTPTR_FORMAT, (intptr_t)uc->context_err);
820
st->print(" TRAPNO=" INTPTR_FORMAT, (intptr_t)uc->context_trapno);
822
st->print( "EAX=" INTPTR_FORMAT, (intptr_t)uc->context_eax);
823
st->print(", EBX=" INTPTR_FORMAT, (intptr_t)uc->context_ebx);
824
st->print(", ECX=" INTPTR_FORMAT, (intptr_t)uc->context_ecx);
825
st->print(", EDX=" INTPTR_FORMAT, (intptr_t)uc->context_edx);
827
st->print( "ESP=" INTPTR_FORMAT, (intptr_t)uc->context_esp);
828
st->print(", EBP=" INTPTR_FORMAT, (intptr_t)uc->context_ebp);
829
st->print(", ESI=" INTPTR_FORMAT, (intptr_t)uc->context_esi);
830
st->print(", EDI=" INTPTR_FORMAT, (intptr_t)uc->context_edi);
832
st->print( "EIP=" INTPTR_FORMAT, (intptr_t)uc->context_eip);
833
st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->context_eflags);
839
void os::print_tos_pc(outputStream *st, const void *context) {
840
if (context == nullptr) return;
842
const ucontext_t* uc = (const ucontext_t*)context;
844
address sp = (address)os::Bsd::ucontext_get_sp(uc);
848
// Note: it may be unsafe to inspect memory near pc. For example, pc may
849
// point to garbage if entry point in an nmethod is corrupted. Leave
850
// this at the end, and hope for the best.
851
address pc = os::Posix::ucontext_get_pc(uc);
852
print_instructions(st, pc);
856
void os::print_register_info(outputStream *st, const void *context, int& continuation) {
857
const int register_count = AMD64_ONLY(16) NOT_AMD64(8);
858
int n = continuation;
859
assert(n >= 0 && n <= register_count, "Invalid continuation value");
860
if (context == nullptr || n == register_count) {
864
const ucontext_t *uc = (const ucontext_t*)context;
865
while (n < register_count) {
866
// Update continuation with next index before printing location
867
continuation = n + 1;
868
# define CASE_PRINT_REG(n, str, id) case n: st->print(str); print_location(st, uc->context_##id);
871
CASE_PRINT_REG( 0, "RAX=", rax); break;
872
CASE_PRINT_REG( 1, "RBX=", rbx); break;
873
CASE_PRINT_REG( 2, "RCX=", rcx); break;
874
CASE_PRINT_REG( 3, "RDX=", rdx); break;
875
CASE_PRINT_REG( 4, "RSP=", rsp); break;
876
CASE_PRINT_REG( 5, "RBP=", rbp); break;
877
CASE_PRINT_REG( 6, "RSI=", rsi); break;
878
CASE_PRINT_REG( 7, "RDI=", rdi); break;
879
CASE_PRINT_REG( 8, "R8 =", r8); break;
880
CASE_PRINT_REG( 9, "R9 =", r9); break;
881
CASE_PRINT_REG(10, "R10=", r10); break;
882
CASE_PRINT_REG(11, "R11=", r11); break;
883
CASE_PRINT_REG(12, "R12=", r12); break;
884
CASE_PRINT_REG(13, "R13=", r13); break;
885
CASE_PRINT_REG(14, "R14=", r14); break;
886
CASE_PRINT_REG(15, "R15=", r15); break;
888
CASE_PRINT_REG(0, "EAX=", eax); break;
889
CASE_PRINT_REG(1, "EBX=", ebx); break;
890
CASE_PRINT_REG(2, "ECX=", ecx); break;
891
CASE_PRINT_REG(3, "EDX=", edx); break;
892
CASE_PRINT_REG(4, "ESP=", esp); break;
893
CASE_PRINT_REG(5, "EBP=", ebp); break;
894
CASE_PRINT_REG(6, "ESI=", esi); break;
895
CASE_PRINT_REG(7, "EDI=", edi); break;
898
# undef CASE_PRINT_REG
903
void os::setup_fpu() {
905
address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std();
906
__asm__ volatile ( "fldcw (%0)" :
907
: "r" (fpu_cntrl) : "memory");
912
void os::verify_stack_alignment() {
916
int os::extra_bang_size_in_bytes() {
917
// JDK-8050147 requires the full cache line bang for x86.
918
return VM_Version::L1_line_size();