Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/os_cpu/linux_ppc/os_linux_ppc.cpp
40951 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2012, 2021 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
// no precompiled headers
27
#include "jvm.h"
28
#include "assembler_ppc.hpp"
29
#include "asm/assembler.inline.hpp"
30
#include "classfile/vmSymbols.hpp"
31
#include "code/codeCache.hpp"
32
#include "code/icBuffer.hpp"
33
#include "code/vtableStubs.hpp"
34
#include "interpreter/interpreter.hpp"
35
#include "memory/allocation.inline.hpp"
36
#include "nativeInst_ppc.hpp"
37
#include "os_share_linux.hpp"
38
#include "prims/jniFastGetField.hpp"
39
#include "prims/jvm_misc.hpp"
40
#include "runtime/arguments.hpp"
41
#include "runtime/frame.inline.hpp"
42
#include "runtime/interfaceSupport.inline.hpp"
43
#include "runtime/java.hpp"
44
#include "runtime/javaCalls.hpp"
45
#include "runtime/mutexLocker.hpp"
46
#include "runtime/osThread.hpp"
47
#include "runtime/safepointMechanism.hpp"
48
#include "runtime/sharedRuntime.hpp"
49
#include "runtime/stubRoutines.hpp"
50
#include "runtime/thread.inline.hpp"
51
#include "runtime/timer.hpp"
52
#include "runtime/vm_version.hpp"
53
#include "signals_posix.hpp"
54
#include "utilities/debug.hpp"
55
#include "utilities/events.hpp"
56
#include "utilities/vmError.hpp"
57
58
// put OS-includes here
59
# include <sys/types.h>
60
# include <sys/mman.h>
61
# include <pthread.h>
62
# include <signal.h>
63
# include <errno.h>
64
# include <dlfcn.h>
65
# include <stdlib.h>
66
# include <stdio.h>
67
# include <unistd.h>
68
# include <sys/resource.h>
69
# include <pthread.h>
70
# include <sys/stat.h>
71
# include <sys/time.h>
72
# include <sys/utsname.h>
73
# include <sys/socket.h>
74
# include <sys/wait.h>
75
# include <pwd.h>
76
# include <poll.h>
77
# include <ucontext.h>
78
79
80
address os::current_stack_pointer() {
81
return (address)__builtin_frame_address(0);
82
}
83
84
char* os::non_memory_address_word() {
85
// Must never look like an address returned by reserve_memory,
86
// even in its subfields (as defined by the CPU immediate fields,
87
// if the CPU splits constants across multiple instructions).
88
89
return (char*) -1;
90
}
91
92
// Frame information (pc, sp, fp) retrieved via ucontext
93
// always looks like a C-frame according to the frame
94
// conventions in frame_ppc64.hpp.
95
address os::Posix::ucontext_get_pc(const ucontext_t * uc) {
96
// On powerpc64, ucontext_t is not selfcontained but contains
97
// a pointer to an optional substructure (mcontext_t.regs) containing the volatile
98
// registers - NIP, among others.
99
// This substructure may or may not be there depending where uc came from:
100
// - if uc was handed over as the argument to a sigaction handler, a pointer to the
101
// substructure was provided by the kernel when calling the signal handler, and
102
// regs->nip can be accessed.
103
// - if uc was filled by getcontext(), it is undefined - getcontext() does not fill
104
// it because the volatile registers are not needed to make setcontext() work.
105
// Hopefully it was zero'd out beforehand.
106
guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_get_pc in sigaction context");
107
return (address)uc->uc_mcontext.regs->nip;
108
}
109
110
// modify PC in ucontext.
111
// Note: Only use this for an ucontext handed down to a signal handler. See comment
112
// in ucontext_get_pc.
113
void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {
114
guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_set_pc in sigaction context");
115
uc->uc_mcontext.regs->nip = (unsigned long)pc;
116
}
117
118
static address ucontext_get_lr(const ucontext_t * uc) {
119
return (address)uc->uc_mcontext.regs->link;
120
}
121
122
intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {
123
return (intptr_t*)uc->uc_mcontext.regs->gpr[1/*REG_SP*/];
124
}
125
126
intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {
127
return NULL;
128
}
129
130
static unsigned long ucontext_get_trap(const ucontext_t * uc) {
131
return uc->uc_mcontext.regs->trap;
132
}
133
134
address os::fetch_frame_from_context(const void* ucVoid,
135
intptr_t** ret_sp, intptr_t** ret_fp) {
136
137
address epc;
138
const ucontext_t* uc = (const ucontext_t*)ucVoid;
139
140
if (uc != NULL) {
141
epc = os::Posix::ucontext_get_pc(uc);
142
if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
143
if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
144
} else {
145
epc = NULL;
146
if (ret_sp) *ret_sp = (intptr_t *)NULL;
147
if (ret_fp) *ret_fp = (intptr_t *)NULL;
148
}
149
150
return epc;
151
}
152
153
frame os::fetch_frame_from_context(const void* ucVoid) {
154
intptr_t* sp;
155
intptr_t* fp;
156
address epc = fetch_frame_from_context(ucVoid, &sp, &fp);
157
return frame(sp, epc);
158
}
159
160
frame os::fetch_compiled_frame_from_context(const void* ucVoid) {
161
const ucontext_t* uc = (const ucontext_t*)ucVoid;
162
intptr_t* sp = os::Linux::ucontext_get_sp(uc);
163
address lr = ucontext_get_lr(uc);
164
return frame(sp, lr);
165
}
166
167
frame os::get_sender_for_C_frame(frame* fr) {
168
if (*fr->sp() == 0) {
169
// fr is the last C frame
170
return frame(NULL, NULL);
171
}
172
return frame(fr->sender_sp(), fr->sender_pc());
173
}
174
175
176
frame os::current_frame() {
177
intptr_t* csp = *(intptr_t**) __builtin_frame_address(0);
178
frame topframe(csp, CAST_FROM_FN_PTR(address, os::current_frame));
179
return os::get_sender_for_C_frame(&topframe);
180
}
181
182
bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
183
ucontext_t* uc, JavaThread* thread) {
184
185
// Make the signal handler transaction-aware by checking the existence of a
186
// second (transactional) context with MSR TS bits active. If the signal is
187
// caught during a transaction, then just return to the HTM abort handler.
188
// Please refer to Linux kernel document powerpc/transactional_memory.txt,
189
// section "Signals".
190
if (uc && uc->uc_link) {
191
ucontext_t* second_uc = uc->uc_link;
192
193
// MSR TS bits are 29 and 30 (Power ISA, v2.07B, Book III-S, pp. 857-858,
194
// 3.2.1 "Machine State Register"), however note that ISA notation for bit
195
// numbering is MSB 0, so for normal bit numbering (LSB 0) they come to be
196
// bits 33 and 34. It's not related to endianness, just a notation matter.
197
if (second_uc->uc_mcontext.regs->msr & 0x600000000) {
198
if (TraceTraps) {
199
tty->print_cr("caught signal in transaction, "
200
"ignoring to jump to abort handler");
201
}
202
// Return control to the HTM abort handler.
203
return true;
204
}
205
}
206
207
// decide if this trap can be handled by a stub
208
address stub = NULL;
209
address pc = NULL;
210
211
if (info != NULL && uc != NULL && thread != NULL) {
212
pc = (address) os::Posix::ucontext_get_pc(uc);
213
214
// Handle ALL stack overflow variations here
215
if (sig == SIGSEGV) {
216
// si_addr may not be valid due to a bug in the linux-ppc64 kernel (see
217
// comment below). Use get_stack_bang_address instead of si_addr.
218
// If SIGSEGV is caused due to a branch to an invalid address an
219
// "Instruction Storage Interrupt" is generated and 'pc' (NIP) already
220
// contains the invalid address. Otherwise, the SIGSEGV is caused due to
221
// load/store instruction trying to load/store from/to an invalid address
222
// and causing a "Data Storage Interrupt", so we inspect the intruction
223
// in order to extract the faulty data addresss.
224
address addr;
225
if ((ucontext_get_trap(uc) & 0x0F00 /* no IRQ reply bits */) == 0x0400) {
226
// Instruction Storage Interrupt (ISI)
227
addr = pc;
228
} else {
229
// Data Storage Interrupt (DSI), i.e. 0x0300: extract faulty data address
230
addr = ((NativeInstruction*)pc)->get_stack_bang_address(uc);
231
}
232
233
// Check if fault address is within thread stack.
234
if (thread->is_in_full_stack(addr)) {
235
// stack overflow
236
if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {
237
return true; // continue
238
}
239
}
240
}
241
242
if (thread->thread_state() == _thread_in_Java) {
243
// Java thread running in Java code => find exception handler if any
244
// a fault inside compiled code, the interpreter, or a stub
245
246
CodeBlob *cb = NULL;
247
int stop_type = -1;
248
// Handle signal from NativeJump::patch_verified_entry().
249
if (sig == SIGILL && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {
250
if (TraceTraps) {
251
tty->print_cr("trap: zombie_not_entrant");
252
}
253
stub = SharedRuntime::get_handle_wrong_method_stub();
254
}
255
256
else if ((sig == USE_POLL_BIT_ONLY ? SIGTRAP : SIGSEGV) &&
257
// A linux-ppc64 kernel before 2.6.6 doesn't set si_addr on some segfaults
258
// in 64bit mode (cf. http://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.6),
259
// especially when we try to read from the safepoint polling page. So the check
260
// (address)info->si_addr == os::get_standard_polling_page()
261
// doesn't work for us. We use:
262
((NativeInstruction*)pc)->is_safepoint_poll() &&
263
CodeCache::contains((void*) pc) &&
264
((cb = CodeCache::find_blob(pc)) != NULL) &&
265
cb->is_compiled()) {
266
if (TraceTraps) {
267
tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (%s)", p2i(pc),
268
USE_POLL_BIT_ONLY ? "SIGTRAP" : "SIGSEGV");
269
}
270
stub = SharedRuntime::get_poll_stub(pc);
271
}
272
273
else if (UseSIGTRAP && sig == SIGTRAP &&
274
((NativeInstruction*)pc)->is_safepoint_poll_return() &&
275
CodeCache::contains((void*) pc) &&
276
((cb = CodeCache::find_blob(pc)) != NULL) &&
277
cb->is_compiled()) {
278
if (TraceTraps) {
279
tty->print_cr("trap: safepoint_poll at return at " INTPTR_FORMAT " (nmethod)", p2i(pc));
280
}
281
stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
282
}
283
284
// SIGTRAP-based ic miss check in compiled code.
285
else if (sig == SIGTRAP && TrapBasedICMissChecks &&
286
nativeInstruction_at(pc)->is_sigtrap_ic_miss_check()) {
287
if (TraceTraps) {
288
tty->print_cr("trap: ic_miss_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
289
}
290
stub = SharedRuntime::get_ic_miss_stub();
291
}
292
293
// SIGTRAP-based implicit null check in compiled code.
294
else if (sig == SIGTRAP && TrapBasedNullChecks &&
295
nativeInstruction_at(pc)->is_sigtrap_null_check()) {
296
if (TraceTraps) {
297
tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
298
}
299
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
300
}
301
302
// SIGSEGV-based implicit null check in compiled code.
303
else if (sig == SIGSEGV && ImplicitNullChecks &&
304
CodeCache::contains((void*) pc) &&
305
MacroAssembler::uses_implicit_null_check(info->si_addr)) {
306
if (TraceTraps) {
307
tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
308
}
309
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
310
}
311
312
#ifdef COMPILER2
313
// SIGTRAP-based implicit range check in compiled code.
314
else if (sig == SIGTRAP && TrapBasedRangeChecks &&
315
nativeInstruction_at(pc)->is_sigtrap_range_check()) {
316
if (TraceTraps) {
317
tty->print_cr("trap: range_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
318
}
319
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
320
}
321
#endif
322
323
// stop on request
324
else if (sig == SIGTRAP && (stop_type = nativeInstruction_at(pc)->get_stop_type()) != -1) {
325
bool msg_present = (stop_type & MacroAssembler::stop_msg_present);
326
stop_type = (stop_type &~ MacroAssembler::stop_msg_present);
327
328
const char *msg = NULL;
329
switch (stop_type) {
330
case MacroAssembler::stop_stop : msg = "stop"; break;
331
case MacroAssembler::stop_untested : msg = "untested"; break;
332
case MacroAssembler::stop_unimplemented : msg = "unimplemented"; break;
333
case MacroAssembler::stop_shouldnotreachhere: msg = "shouldnotreachhere"; break;
334
default: msg = "unknown"; break;
335
}
336
337
const char **detail_msg_ptr = (const char**)(pc + 4);
338
const char *detail_msg = msg_present ? *detail_msg_ptr : "no details provided";
339
340
if (TraceTraps) {
341
tty->print_cr("trap: %s: %s (SIGTRAP, stop type %d)", msg, detail_msg, stop_type);
342
}
343
344
// End life with a fatal error, message and detail message and the context.
345
// Note: no need to do any post-processing here (e.g. signal chaining)
346
va_list va_dummy;
347
VMError::report_and_die(thread, uc, NULL, 0, msg, detail_msg, va_dummy);
348
va_end(va_dummy);
349
350
ShouldNotReachHere();
351
352
}
353
354
else if (sig == SIGBUS) {
355
// BugId 4454115: A read from a MappedByteBuffer can fault here if the
356
// underlying file has been truncated. Do not crash the VM in such a case.
357
CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
358
CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
359
bool is_unsafe_arraycopy = (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc));
360
if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {
361
address next_pc = pc + 4;
362
if (is_unsafe_arraycopy) {
363
next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);
364
}
365
next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
366
os::Posix::ucontext_set_pc(uc, next_pc);
367
return true;
368
}
369
}
370
}
371
372
else { // thread->thread_state() != _thread_in_Java
373
if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {
374
// SIGILL must be caused by VM_Version::determine_features().
375
*(int *)pc = 0; // patch instruction to 0 to indicate that it causes a SIGILL,
376
// flushing of icache is not necessary.
377
stub = pc + 4; // continue with next instruction.
378
}
379
else if ((thread->thread_state() == _thread_in_vm ||
380
thread->thread_state() == _thread_in_native) &&
381
sig == SIGBUS && thread->doing_unsafe_access()) {
382
address next_pc = pc + 4;
383
if (UnsafeCopyMemory::contains_pc(pc)) {
384
next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);
385
}
386
next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
387
os::Posix::ucontext_set_pc(uc, next_pc);
388
return true;
389
}
390
}
391
392
// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
393
// and the heap gets shrunk before the field access.
394
if ((sig == SIGSEGV) || (sig == SIGBUS)) {
395
address addr = JNI_FastGetField::find_slowcase_pc(pc);
396
if (addr != (address)-1) {
397
stub = addr;
398
}
399
}
400
}
401
402
if (stub != NULL) {
403
// Save all thread context in case we need to restore it.
404
if (thread != NULL) thread->set_saved_exception_pc(pc);
405
os::Posix::ucontext_set_pc(uc, stub);
406
return true;
407
}
408
409
return false;
410
}
411
412
void os::Linux::init_thread_fpu_state(void) {
413
// Disable FP exceptions.
414
__asm__ __volatile__ ("mtfsfi 6,0");
415
}
416
417
int os::Linux::get_fpu_control_word(void) {
418
// x86 has problems with FPU precision after pthread_cond_timedwait().
419
// nothing to do on ppc64.
420
return 0;
421
}
422
423
void os::Linux::set_fpu_control_word(int fpu_control) {
424
// x86 has problems with FPU precision after pthread_cond_timedwait().
425
// nothing to do on ppc64.
426
}
427
428
////////////////////////////////////////////////////////////////////////////////
429
// thread stack
430
431
// Minimum usable stack sizes required to get to user code. Space for
432
// HotSpot guard pages is added later.
433
size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K;
434
size_t os::Posix::_java_thread_min_stack_allowed = 64 * K;
435
size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;
436
437
// Return default stack size for thr_type.
438
size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
439
// Default stack size (compiler thread needs larger stack).
440
size_t s = (thr_type == os::compiler_thread ? 4 * M : 1024 * K);
441
return s;
442
}
443
444
/////////////////////////////////////////////////////////////////////////////
445
// helper functions for fatal error handler
446
447
void os::print_context(outputStream *st, const void *context) {
448
if (context == NULL) return;
449
450
const ucontext_t* uc = (const ucontext_t*)context;
451
452
st->print_cr("Registers:");
453
st->print("pc =" INTPTR_FORMAT " ", uc->uc_mcontext.regs->nip);
454
st->print("lr =" INTPTR_FORMAT " ", uc->uc_mcontext.regs->link);
455
st->print("ctr=" INTPTR_FORMAT " ", uc->uc_mcontext.regs->ctr);
456
st->cr();
457
for (int i = 0; i < 32; i++) {
458
st->print("r%-2d=" INTPTR_FORMAT " ", i, uc->uc_mcontext.regs->gpr[i]);
459
if (i % 3 == 2) st->cr();
460
}
461
st->cr();
462
st->cr();
463
464
intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
465
st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
466
print_hex_dump(st, (address)sp, (address)(sp + 128), sizeof(intptr_t));
467
st->cr();
468
469
// Note: it may be unsafe to inspect memory near pc. For example, pc may
470
// point to garbage if entry point in an nmethod is corrupted. Leave
471
// this at the end, and hope for the best.
472
address pc = os::Posix::ucontext_get_pc(uc);
473
print_instructions(st, pc, /*instrsize=*/4);
474
st->cr();
475
}
476
477
void os::print_register_info(outputStream *st, const void *context) {
478
if (context == NULL) return;
479
480
const ucontext_t *uc = (const ucontext_t*)context;
481
482
st->print_cr("Register to memory mapping:");
483
st->cr();
484
485
st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->nip);
486
st->print("lr ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->link);
487
st->print("ctr ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->ctr);
488
for (int i = 0; i < 32; i++) {
489
st->print("r%-2d=", i);
490
print_location(st, uc->uc_mcontext.regs->gpr[i]);
491
}
492
st->cr();
493
}
494
495
extern "C" {
496
int SpinPause() {
497
return 0;
498
}
499
}
500
501
#ifndef PRODUCT
502
void os::verify_stack_alignment() {
503
assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
504
}
505
#endif
506
507
int os::extra_bang_size_in_bytes() {
508
// PPC does not require the additional stack bang.
509
return 0;
510
}
511
512
#ifdef HAVE_FUNCTION_DESCRIPTORS
513
void* os::resolve_function_descriptor(void* p) {
514
return ((const FunctionDescriptor*)p)->entry();
515
}
516
#endif
517
518