2
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2012, 2024 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
// no precompiled headers
27
#include "assembler_ppc.hpp"
28
#include "asm/assembler.inline.hpp"
29
#include "classfile/vmSymbols.hpp"
30
#include "code/codeCache.hpp"
31
#include "code/vtableStubs.hpp"
32
#include "interpreter/interpreter.hpp"
34
#include "memory/allocation.inline.hpp"
35
#include "nativeInst_ppc.hpp"
37
#include "os_posix.hpp"
38
#include "prims/jniFastGetField.hpp"
39
#include "prims/jvm_misc.hpp"
40
#include "porting_aix.hpp"
41
#include "runtime/arguments.hpp"
42
#include "runtime/frame.inline.hpp"
43
#include "runtime/interfaceSupport.inline.hpp"
44
#include "runtime/java.hpp"
45
#include "runtime/javaCalls.hpp"
46
#include "runtime/javaThread.hpp"
47
#include "runtime/mutexLocker.hpp"
48
#include "runtime/osThread.hpp"
49
#include "runtime/safepointMechanism.hpp"
50
#include "runtime/sharedRuntime.hpp"
51
#include "runtime/stubRoutines.hpp"
52
#include "runtime/timer.hpp"
53
#include "signals_posix.hpp"
54
#include "utilities/events.hpp"
55
#include "utilities/vmError.hpp"
57
#include "c1/c1_Runtime1.hpp"
60
#include "opto/runtime.hpp"
63
// put OS-includes here
66
address os::current_stack_pointer() {
67
return (address)__builtin_frame_address(0);
70
char* os::non_memory_address_word() {
71
// Must never look like an address returned by reserve_memory,
72
// even in its subfields (as defined by the CPU immediate fields,
73
// if the CPU splits constants across multiple instructions).
78
// Frame information (pc, sp, fp) retrieved via ucontext
79
// always looks like a C-frame according to the frame
80
// conventions in frame_ppc.hpp.
82
address os::Posix::ucontext_get_pc(const ucontext_t * uc) {
83
return (address)uc->uc_mcontext.jmp_context.iar;
86
intptr_t* os::Aix::ucontext_get_sp(const ucontext_t * uc) {
87
// gpr1 holds the stack pointer on aix
88
return (intptr_t*)uc->uc_mcontext.jmp_context.gpr[1/*REG_SP*/];
91
intptr_t* os::Aix::ucontext_get_fp(const ucontext_t * uc) {
95
void os::Posix::ucontext_set_pc(ucontext_t* uc, address new_pc) {
96
uc->uc_mcontext.jmp_context.iar = (uint64_t) new_pc;
99
static address ucontext_get_lr(const ucontext_t * uc) {
100
return (address)uc->uc_mcontext.jmp_context.lr;
103
address os::fetch_frame_from_context(const void* ucVoid,
104
intptr_t** ret_sp, intptr_t** ret_fp) {
107
const ucontext_t* uc = (const ucontext_t*)ucVoid;
110
epc = os::Posix::ucontext_get_pc(uc);
111
if (ret_sp) *ret_sp = os::Aix::ucontext_get_sp(uc);
112
if (ret_fp) *ret_fp = os::Aix::ucontext_get_fp(uc);
115
if (ret_sp) *ret_sp = (intptr_t *)nullptr;
116
if (ret_fp) *ret_fp = (intptr_t *)nullptr;
122
frame os::fetch_frame_from_context(const void* ucVoid) {
125
address epc = fetch_frame_from_context(ucVoid, &sp, &fp);
126
// Avoid crash during crash if pc broken.
128
frame fr(sp, epc, frame::kind::unknown);
135
frame os::fetch_compiled_frame_from_context(const void* ucVoid) {
136
const ucontext_t* uc = (const ucontext_t*)ucVoid;
137
intptr_t* sp = os::Aix::ucontext_get_sp(uc);
138
address lr = ucontext_get_lr(uc);
139
return frame(sp, lr, frame::kind::unknown);
142
frame os::get_sender_for_C_frame(frame* fr) {
143
if (*fr->sp() == (intptr_t) nullptr) {
144
// fr is the last C frame
147
return frame(fr->sender_sp(), fr->sender_pc(), frame::kind::unknown);
151
frame os::current_frame() {
152
intptr_t* csp = *(intptr_t**) __builtin_frame_address(0);
153
frame topframe(csp, CAST_FROM_FN_PTR(address, os::current_frame), frame::kind::unknown);
154
return os::get_sender_for_C_frame(&topframe);
157
bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
158
ucontext_t* uc, JavaThread* thread) {
160
// Decide if this trap can be handled by a stub.
161
address stub = nullptr;
163
// retrieve program counter
164
address const pc = uc ? os::Posix::ucontext_get_pc(uc) : nullptr;
166
// retrieve crash address
167
address const addr = info ? (const address) info->si_addr : nullptr;
169
if (info == nullptr || uc == nullptr) {
170
return false; // Fatal error
173
// If we are a java thread...
174
if (thread != nullptr) {
176
// Handle ALL stack overflow variations here
177
if (sig == SIGSEGV && thread->is_in_full_stack(addr)) {
179
if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {
180
return true; // continue
181
} else if (stub != nullptr) {
184
return false; // Fatal error
186
} // end handle SIGSEGV inside stack boundaries
188
if (thread->thread_state() == _thread_in_Java) {
189
// Java thread running in Java code
191
// The following signals are used for communicating VM events:
193
// SIGILL: the compiler generates illegal opcodes
194
// at places where it wishes to interrupt the VM:
195
// Safepoints, Unreachable Code, Entry points of not entrant nmethods,
196
// This results in a SIGILL with (*pc) == inserted illegal instruction.
198
// (so, SIGILLs with a pc inside the zero page are real errors)
201
// The ppc trap instruction raises a SIGTRAP and is very efficient if it
202
// does not trap. It is used for conditional branches that are expected
203
// to be never taken. These are:
204
// - not entrant nmethods
205
// - IC (inline cache) misses.
206
// - null checks leading to UncommonTraps.
207
// - range checks leading to Uncommon Traps.
208
// On Aix, these are especially null checks, as the ImplicitNullCheck
209
// optimization works only in rare cases, as the page at address 0 is only
210
// write protected. //
211
// Note: !UseSIGTRAP is used to prevent SIGTRAPS altogether, to facilitate debugging.
214
// used for safe point polling:
215
// To notify all threads that they have to reach a safe point, safe point polling is used:
216
// All threads poll a certain mapped memory page. Normally, this page has read access.
217
// If the VM wants to inform the threads about impending safe points, it puts this
218
// page to read only ("poisens" the page), and the threads then reach a safe point.
219
// used for null checks:
220
// If the compiler finds a store it uses it for a null check. Unfortunately this
221
// happens rarely. In heap based and disjoint base compressd oop modes also loads
222
// are used for null checks.
224
CodeBlob *cb = nullptr;
226
// Handle signal from NativeJump::patch_verified_entry().
227
if (sig == SIGILL && nativeInstruction_at(pc)->is_sigill_not_entrant()) {
229
tty->print_cr("trap: not_entrant");
231
stub = SharedRuntime::get_handle_wrong_method_stub();
235
else if ((sig == (USE_POLL_BIT_ONLY ? SIGTRAP : SIGSEGV)) &&
236
((NativeInstruction*)pc)->is_safepoint_poll() &&
237
CodeCache::contains((void*) pc) &&
238
((cb = CodeCache::find_blob(pc)) != nullptr) &&
241
tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (%s)", p2i(pc),
242
USE_POLL_BIT_ONLY ? "SIGTRAP" : "SIGSEGV");
244
stub = SharedRuntime::get_poll_stub(pc);
248
else if (UseSIGTRAP && sig == SIGTRAP &&
249
((NativeInstruction*)pc)->is_safepoint_poll_return() &&
250
CodeCache::contains((void*) pc) &&
251
((cb = CodeCache::find_blob(pc)) != nullptr) &&
254
tty->print_cr("trap: safepoint_poll at return at " INTPTR_FORMAT " (nmethod)", p2i(pc));
256
stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
260
// SIGTRAP-based ic miss check in compiled code.
261
else if (sig == SIGTRAP && TrapBasedICMissChecks &&
262
nativeInstruction_at(pc)->is_sigtrap_ic_miss_check()) {
264
tty->print_cr("trap: ic_miss_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
266
stub = SharedRuntime::get_ic_miss_stub();
270
// SIGTRAP-based implicit null check in compiled code.
271
else if (sig == SIGTRAP && TrapBasedNullChecks &&
272
nativeInstruction_at(pc)->is_sigtrap_null_check()) {
274
tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
276
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
280
// SIGSEGV-based implicit null check in compiled code.
281
else if (sig == SIGSEGV && ImplicitNullChecks &&
282
CodeCache::contains((void*) pc) &&
283
MacroAssembler::uses_implicit_null_check(info->si_addr)) {
285
tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
287
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
291
// SIGTRAP-based implicit range check in compiled code.
292
else if (sig == SIGTRAP && TrapBasedRangeChecks &&
293
nativeInstruction_at(pc)->is_sigtrap_range_check()) {
295
tty->print_cr("trap: range_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
297
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
302
else if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {
304
tty->print_raw_cr("Fix SIGFPE handler, trying divide by zero handler.");
306
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
311
else if (sig == SIGTRAP && (stop_type = nativeInstruction_at(pc)->get_stop_type()) != -1) {
312
bool msg_present = (stop_type & MacroAssembler::stop_msg_present);
313
stop_type = (stop_type &~ MacroAssembler::stop_msg_present);
315
const char *msg = nullptr;
317
case MacroAssembler::stop_stop : msg = "stop"; break;
318
case MacroAssembler::stop_untested : msg = "untested"; break;
319
case MacroAssembler::stop_unimplemented : msg = "unimplemented"; break;
320
case MacroAssembler::stop_shouldnotreachhere: msg = "shouldnotreachhere"; break;
321
default: msg = "unknown"; break;
324
const char **detail_msg_ptr = (const char**)(pc + 4);
325
const char *detail_msg = msg_present ? *detail_msg_ptr : "no details provided";
328
tty->print_cr("trap: %s: %s (SIGTRAP, stop type %d)", msg, detail_msg, stop_type);
331
// End life with a fatal error, message and detail message and the context.
332
// Note: no need to do any post-processing here (e.g. signal chaining)
333
VMError::report_and_die(thread, uc, nullptr, 0, msg, "%s", detail_msg);
335
ShouldNotReachHere();
338
else if (sig == SIGBUS) {
339
// BugId 4454115: A read from a MappedByteBuffer can fault here if the
340
// underlying file has been truncated. Do not crash the VM in such a case.
341
CodeBlob* cb = CodeCache::find_blob(pc);
342
nmethod* nm = cb ? cb->as_nmethod_or_null() : nullptr;
343
bool is_unsafe_memory_access = (thread->doing_unsafe_access() && UnsafeMemoryAccess::contains_pc(pc));
344
if ((nm != nullptr && nm->has_unsafe_access()) || is_unsafe_memory_access) {
345
address next_pc = pc + 4;
346
if (is_unsafe_memory_access) {
347
next_pc = UnsafeMemoryAccess::page_error_continue_pc(pc);
349
next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
350
os::Posix::ucontext_set_pc(uc, next_pc);
356
else { // thread->thread_state() != _thread_in_Java
357
// Detect CPU features. This is only done at the very start of the VM. Later, the
358
// VM_Version::is_determine_features_test_running() flag should be false.
360
if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {
361
// SIGILL must be caused by VM_Version::determine_features().
362
*(int *)pc = 0; // patch instruction to 0 to indicate that it causes a SIGILL,
363
// flushing of icache is not necessary.
364
stub = pc + 4; // continue with next instruction.
367
else if ((thread->thread_state() == _thread_in_vm ||
368
thread->thread_state() == _thread_in_native) &&
369
sig == SIGBUS && thread->doing_unsafe_access()) {
370
address next_pc = pc + 4;
371
if (UnsafeMemoryAccess::contains_pc(pc)) {
372
next_pc = UnsafeMemoryAccess::page_error_continue_pc(pc);
374
next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
375
os::Posix::ucontext_set_pc(uc, next_pc);
380
// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
381
// and the heap gets shrunk before the field access.
382
if ((sig == SIGSEGV) || (sig == SIGBUS)) {
383
address addr = JNI_FastGetField::find_slowcase_pc(pc);
384
if (addr != (address)-1) {
392
// One of the above code blocks ininitalized the stub, so we want to
393
// delegate control to that stub.
394
if (stub != nullptr) {
395
// Save all thread context in case we need to restore it.
396
if (thread != nullptr) thread->set_saved_exception_pc(pc);
397
os::Posix::ucontext_set_pc(uc, stub);
401
return false; // Fatal error
404
void os::Aix::init_thread_fpu_state(void) {
405
#if !defined(USE_XLC_BUILTINS)
406
// Disable FP exceptions.
407
__asm__ __volatile__ ("mtfsfi 6,0");
413
////////////////////////////////////////////////////////////////////////////////
416
// Minimum usable stack sizes required to get to user code. Space for
417
// HotSpot guard pages is added later.
418
size_t os::_compiler_thread_min_stack_allowed = 192 * K;
419
size_t os::_java_thread_min_stack_allowed = 64 * K;
420
size_t os::_vm_internal_thread_min_stack_allowed = 64 * K;
422
// Return default stack size for thr_type.
423
size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
424
// Default stack size (compiler thread needs larger stack).
425
size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
429
/////////////////////////////////////////////////////////////////////////////
430
// helper functions for fatal error handler
432
void os::print_context(outputStream *st, const void *context) {
433
if (context == nullptr) return;
435
const ucontext_t* uc = (const ucontext_t*)context;
437
st->print_cr("Registers:");
438
st->print("pc =" INTPTR_FORMAT " ", (unsigned long)uc->uc_mcontext.jmp_context.iar);
439
st->print("lr =" INTPTR_FORMAT " ", (unsigned long)uc->uc_mcontext.jmp_context.lr);
440
st->print("ctr=" INTPTR_FORMAT " ", (unsigned long)uc->uc_mcontext.jmp_context.ctr);
442
for (int i = 0; i < 32; i++) {
443
st->print("r%-2d=" INTPTR_FORMAT " ", i, (unsigned long)uc->uc_mcontext.jmp_context.gpr[i]);
444
if (i % 3 == 2) st->cr();
450
void os::print_tos_pc(outputStream *st, const void *context) {
451
if (context == nullptr) return;
453
const ucontext_t* uc = (const ucontext_t*)context;
455
address sp = (address)os::Aix::ucontext_get_sp(uc);
459
// Note: it may be unsafe to inspect memory near pc. For example, pc may
460
// point to garbage if entry point in an nmethod is corrupted. Leave
461
// this at the end, and hope for the best.
462
address pc = os::Posix::ucontext_get_pc(uc);
463
print_instructions(st, pc);
466
// Try to decode the instructions.
467
st->print_cr("Decoded instructions: (pc=" PTR_FORMAT ")", p2i(pc));
468
st->print("<TODO: PPC port - print_context>");
469
// TODO: PPC port Disassembler::decode(pc, 16, 16, st);
473
void os::print_register_info(outputStream *st, const void *context, int& continuation) {
474
const int register_count = 32 /* r0-r32 */ + 3 /* pc, lr, sp */;
475
int n = continuation;
476
assert(n >= 0 && n <= register_count, "Invalid continuation value");
477
if (context == nullptr || n == register_count) {
481
const ucontext_t *uc = (const ucontext_t*)context;
482
while (n < register_count) {
483
// Update continuation with next index before printing location
484
continuation = n + 1;
485
if (n == register_count - 1) {
486
st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.jmp_context.iar);
487
} else if (n == register_count - 2) {
488
st->print("lr ="); print_location(st, (intptr_t)uc->uc_mcontext.jmp_context.lr);
489
} else if (n == register_count - 3) {
490
st->print("sp ="); print_location(st, (intptr_t)os::Aix::ucontext_get_sp(uc));
492
st->print("r%-2d=", n);
493
print_location(st, (intptr_t)uc->uc_mcontext.jmp_context.gpr[n]);
506
void os::verify_stack_alignment() {
507
assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
511
int os::extra_bang_size_in_bytes() {
512
// PPC does not require the additional stack bang.
516
bool os::Aix::platform_print_native_stack(outputStream* st, const void* context, char *buf, int buf_size, address& lastpc) {
517
AixNativeCallstack::print_callstack_for_context(st, (const ucontext_t*)context, true, buf, (size_t) buf_size);
521
// HAVE_FUNCTION_DESCRIPTORS
522
void* os::Aix::resolve_function_descriptor(void* p) {
523
return ((const FunctionDescriptor*)p)->entry();
526
void os::setup_fpu() {}