Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/os_cpu/linux_x86/os_linux_x86.cpp
40951 views
1
/*
2
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
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.
8
*
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).
14
*
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.
18
*
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
21
* questions.
22
*
23
*/
24
25
// no precompiled headers
26
#include "jvm.h"
27
#include "asm/macroAssembler.hpp"
28
#include "classfile/vmSymbols.hpp"
29
#include "code/codeCache.hpp"
30
#include "code/icBuffer.hpp"
31
#include "code/vtableStubs.hpp"
32
#include "interpreter/interpreter.hpp"
33
#include "logging/log.hpp"
34
#include "memory/allocation.inline.hpp"
35
#include "os_share_linux.hpp"
36
#include "prims/jniFastGetField.hpp"
37
#include "prims/jvm_misc.hpp"
38
#include "runtime/frame.inline.hpp"
39
#include "runtime/interfaceSupport.inline.hpp"
40
#include "runtime/java.hpp"
41
#include "runtime/javaCalls.hpp"
42
#include "runtime/mutexLocker.hpp"
43
#include "runtime/osThread.hpp"
44
#include "runtime/safepointMechanism.hpp"
45
#include "runtime/sharedRuntime.hpp"
46
#include "runtime/stubRoutines.hpp"
47
#include "runtime/thread.inline.hpp"
48
#include "runtime/timer.hpp"
49
#include "signals_posix.hpp"
50
#include "services/memTracker.hpp"
51
#include "utilities/align.hpp"
52
#include "utilities/debug.hpp"
53
#include "utilities/events.hpp"
54
#include "utilities/vmError.hpp"
55
56
// put OS-includes here
57
# include <sys/types.h>
58
# include <sys/mman.h>
59
# include <pthread.h>
60
# include <signal.h>
61
# include <errno.h>
62
# include <dlfcn.h>
63
# include <stdlib.h>
64
# include <stdio.h>
65
# include <unistd.h>
66
# include <sys/resource.h>
67
# include <pthread.h>
68
# include <sys/stat.h>
69
# include <sys/time.h>
70
# include <sys/utsname.h>
71
# include <sys/socket.h>
72
# include <sys/wait.h>
73
# include <pwd.h>
74
# include <poll.h>
75
# include <ucontext.h>
76
#if !defined(AMD64) && !defined(__ANDROID__)
77
# include <fpu_control.h>
78
#elif defined(__ANDROID__)
79
# include "fpu_control.h"
80
#endif
81
82
#ifdef AMD64
83
#define REG_SP REG_RSP
84
#define REG_PC REG_RIP
85
#define REG_FP REG_RBP
86
#define SPELL_REG_SP "rsp"
87
#define SPELL_REG_FP "rbp"
88
#else
89
#define REG_SP REG_UESP
90
#define REG_PC REG_EIP
91
#define REG_FP REG_EBP
92
#define SPELL_REG_SP "esp"
93
#define SPELL_REG_FP "ebp"
94
#endif // AMD64
95
96
address os::current_stack_pointer() {
97
return (address)__builtin_frame_address(0);
98
}
99
100
char* os::non_memory_address_word() {
101
// Must never look like an address returned by reserve_memory,
102
// even in its subfields (as defined by the CPU immediate fields,
103
// if the CPU splits constants across multiple instructions).
104
105
return (char*) -1;
106
}
107
108
address os::Posix::ucontext_get_pc(const ucontext_t * uc) {
109
return (address)uc->uc_mcontext.gregs[REG_PC];
110
}
111
112
void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {
113
uc->uc_mcontext.gregs[REG_PC] = (intptr_t)pc;
114
}
115
116
intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {
117
return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];
118
}
119
120
intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {
121
return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];
122
}
123
124
address os::fetch_frame_from_context(const void* ucVoid,
125
intptr_t** ret_sp, intptr_t** ret_fp) {
126
127
address epc;
128
const ucontext_t* uc = (const ucontext_t*)ucVoid;
129
130
if (uc != NULL) {
131
epc = os::Posix::ucontext_get_pc(uc);
132
if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
133
if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
134
} else {
135
epc = NULL;
136
if (ret_sp) *ret_sp = (intptr_t *)NULL;
137
if (ret_fp) *ret_fp = (intptr_t *)NULL;
138
}
139
140
return epc;
141
}
142
143
frame os::fetch_frame_from_context(const void* ucVoid) {
144
intptr_t* sp;
145
intptr_t* fp;
146
address epc = fetch_frame_from_context(ucVoid, &sp, &fp);
147
return frame(sp, fp, epc);
148
}
149
150
frame os::fetch_compiled_frame_from_context(const void* ucVoid) {
151
const ucontext_t* uc = (const ucontext_t*)ucVoid;
152
intptr_t* fp = os::Linux::ucontext_get_fp(uc);
153
intptr_t* sp = os::Linux::ucontext_get_sp(uc);
154
return frame(sp + 1, fp, (address)*sp);
155
}
156
157
// By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get
158
// turned off by -fomit-frame-pointer,
159
frame os::get_sender_for_C_frame(frame* fr) {
160
return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
161
}
162
163
intptr_t* _get_previous_fp() {
164
#if defined(__clang__)
165
intptr_t **ebp;
166
__asm__ __volatile__ ("mov %%" SPELL_REG_FP ", %0":"=r"(ebp):);
167
#else
168
register intptr_t **ebp __asm__ (SPELL_REG_FP);
169
#endif
170
// ebp is for this frame (_get_previous_fp). We want the ebp for the
171
// caller of os::current_frame*(), so go up two frames. However, for
172
// optimized builds, _get_previous_fp() will be inlined, so only go
173
// up 1 frame in that case.
174
#ifdef _NMT_NOINLINE_
175
return **(intptr_t***)ebp;
176
#else
177
return *ebp;
178
#endif
179
}
180
181
182
frame os::current_frame() {
183
intptr_t* fp = _get_previous_fp();
184
frame myframe((intptr_t*)os::current_stack_pointer(),
185
(intptr_t*)fp,
186
CAST_FROM_FN_PTR(address, os::current_frame));
187
if (os::is_first_C_frame(&myframe)) {
188
// stack is not walkable
189
return frame();
190
} else {
191
return os::get_sender_for_C_frame(&myframe);
192
}
193
}
194
195
// Utility functions
196
197
// From IA32 System Programming Guide
198
enum {
199
trap_page_fault = 0xE
200
};
201
202
bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
203
ucontext_t* uc, JavaThread* thread) {
204
205
/*
206
NOTE: does not seem to work on linux.
207
if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
208
// can't decode this kind of signal
209
info = NULL;
210
} else {
211
assert(sig == info->si_signo, "bad siginfo");
212
}
213
*/
214
// decide if this trap can be handled by a stub
215
address stub = NULL;
216
217
address pc = NULL;
218
219
//%note os_trap_1
220
if (info != NULL && uc != NULL && thread != NULL) {
221
pc = (address) os::Posix::ucontext_get_pc(uc);
222
223
#ifndef AMD64
224
// Halt if SI_KERNEL before more crashes get misdiagnosed as Java bugs
225
// This can happen in any running code (currently more frequently in
226
// interpreter code but has been seen in compiled code)
227
if (sig == SIGSEGV && info->si_addr == 0 && info->si_code == SI_KERNEL) {
228
fatal("An irrecoverable SI_KERNEL SIGSEGV has occurred due "
229
"to unstable signal handling in this distribution.");
230
}
231
#endif // AMD64
232
233
// Handle ALL stack overflow variations here
234
if (sig == SIGSEGV) {
235
address addr = (address) info->si_addr;
236
237
// check if fault address is within thread stack
238
if (thread->is_in_full_stack(addr)) {
239
// stack overflow
240
if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {
241
return true; // continue
242
}
243
}
244
}
245
246
if ((sig == SIGSEGV) && VM_Version::is_cpuinfo_segv_addr(pc)) {
247
// Verify that OS save/restore AVX registers.
248
stub = VM_Version::cpuinfo_cont_addr();
249
}
250
251
if (thread->thread_state() == _thread_in_Java) {
252
// Java thread running in Java code => find exception handler if any
253
// a fault inside compiled code, the interpreter, or a stub
254
255
if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) {
256
stub = SharedRuntime::get_poll_stub(pc);
257
} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {
258
// BugId 4454115: A read from a MappedByteBuffer can fault
259
// here if the underlying file has been truncated.
260
// Do not crash the VM in such a case.
261
CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
262
CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
263
bool is_unsafe_arraycopy = thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc);
264
if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {
265
address next_pc = Assembler::locate_next_instruction(pc);
266
if (is_unsafe_arraycopy) {
267
next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);
268
}
269
stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
270
}
271
}
272
else
273
274
#ifdef AMD64
275
if (sig == SIGFPE &&
276
(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {
277
stub =
278
SharedRuntime::
279
continuation_for_implicit_exception(thread,
280
pc,
281
SharedRuntime::
282
IMPLICIT_DIVIDE_BY_ZERO);
283
#else
284
if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {
285
// HACK: si_code does not work on linux 2.2.12-20!!!
286
int op = pc[0];
287
if (op == 0xDB) {
288
// FIST
289
// TODO: The encoding of D2I in x86_32.ad can cause an exception
290
// prior to the fist instruction if there was an invalid operation
291
// pending. We want to dismiss that exception. From the win_32
292
// side it also seems that if it really was the fist causing
293
// the exception that we do the d2i by hand with different
294
// rounding. Seems kind of weird.
295
// NOTE: that we take the exception at the NEXT floating point instruction.
296
assert(pc[0] == 0xDB, "not a FIST opcode");
297
assert(pc[1] == 0x14, "not a FIST opcode");
298
assert(pc[2] == 0x24, "not a FIST opcode");
299
return true;
300
} else if (op == 0xF7) {
301
// IDIV
302
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
303
} else {
304
// TODO: handle more cases if we are using other x86 instructions
305
// that can generate SIGFPE signal on linux.
306
tty->print_cr("unknown opcode 0x%X with SIGFPE.", op);
307
fatal("please update this code.");
308
}
309
#endif // AMD64
310
} else if (sig == SIGSEGV &&
311
MacroAssembler::uses_implicit_null_check(info->si_addr)) {
312
// Determination of interpreter/vtable stub/compiled code null exception
313
stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
314
}
315
} else if ((thread->thread_state() == _thread_in_vm ||
316
thread->thread_state() == _thread_in_native) &&
317
(sig == SIGBUS && /* info->si_code == BUS_OBJERR && */
318
thread->doing_unsafe_access())) {
319
address next_pc = Assembler::locate_next_instruction(pc);
320
if (UnsafeCopyMemory::contains_pc(pc)) {
321
next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);
322
}
323
stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
324
}
325
326
// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
327
// and the heap gets shrunk before the field access.
328
if ((sig == SIGSEGV) || (sig == SIGBUS)) {
329
address addr = JNI_FastGetField::find_slowcase_pc(pc);
330
if (addr != (address)-1) {
331
stub = addr;
332
}
333
}
334
}
335
336
#ifndef AMD64
337
// Execution protection violation
338
//
339
// This should be kept as the last step in the triage. We don't
340
// have a dedicated trap number for a no-execute fault, so be
341
// conservative and allow other handlers the first shot.
342
//
343
// Note: We don't test that info->si_code == SEGV_ACCERR here.
344
// this si_code is so generic that it is almost meaningless; and
345
// the si_code for this condition may change in the future.
346
// Furthermore, a false-positive should be harmless.
347
if (UnguardOnExecutionViolation > 0 &&
348
stub == NULL &&
349
(sig == SIGSEGV || sig == SIGBUS) &&
350
uc->uc_mcontext.gregs[REG_TRAPNO] == trap_page_fault) {
351
int page_size = os::vm_page_size();
352
address addr = (address) info->si_addr;
353
address pc = os::Posix::ucontext_get_pc(uc);
354
// Make sure the pc and the faulting address are sane.
355
//
356
// If an instruction spans a page boundary, and the page containing
357
// the beginning of the instruction is executable but the following
358
// page is not, the pc and the faulting address might be slightly
359
// different - we still want to unguard the 2nd page in this case.
360
//
361
// 15 bytes seems to be a (very) safe value for max instruction size.
362
bool pc_is_near_addr =
363
(pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
364
bool instr_spans_page_boundary =
365
(align_down((intptr_t) pc ^ (intptr_t) addr,
366
(intptr_t) page_size) > 0);
367
368
if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
369
static volatile address last_addr =
370
(address) os::non_memory_address_word();
371
372
// In conservative mode, don't unguard unless the address is in the VM
373
if (addr != last_addr &&
374
(UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
375
376
// Set memory to RWX and retry
377
address page_start = align_down(addr, page_size);
378
bool res = os::protect_memory((char*) page_start, page_size,
379
os::MEM_PROT_RWX);
380
381
log_debug(os)("Execution protection violation "
382
"at " INTPTR_FORMAT
383
", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr),
384
p2i(page_start), (res ? "success" : "failed"), errno);
385
stub = pc;
386
387
// Set last_addr so if we fault again at the same address, we don't end
388
// up in an endless loop.
389
//
390
// There are two potential complications here. Two threads trapping at
391
// the same address at the same time could cause one of the threads to
392
// think it already unguarded, and abort the VM. Likely very rare.
393
//
394
// The other race involves two threads alternately trapping at
395
// different addresses and failing to unguard the page, resulting in
396
// an endless loop. This condition is probably even more unlikely than
397
// the first.
398
//
399
// Although both cases could be avoided by using locks or thread local
400
// last_addr, these solutions are unnecessary complication: this
401
// handler is a best-effort safety net, not a complete solution. It is
402
// disabled by default and should only be used as a workaround in case
403
// we missed any no-execute-unsafe VM code.
404
405
last_addr = addr;
406
}
407
}
408
}
409
#endif // !AMD64
410
411
if (stub != NULL) {
412
// save all thread context in case we need to restore it
413
if (thread != NULL) thread->set_saved_exception_pc(pc);
414
415
os::Posix::ucontext_set_pc(uc, stub);
416
return true;
417
}
418
419
return false;
420
}
421
422
void os::Linux::init_thread_fpu_state(void) {
423
#ifndef AMD64
424
// set fpu to 53 bit precision
425
set_fpu_control_word(0x27f);
426
#endif // !AMD64
427
}
428
429
int os::Linux::get_fpu_control_word(void) {
430
#ifdef AMD64
431
return 0;
432
#else
433
int fpu_control;
434
_FPU_GETCW(fpu_control);
435
return fpu_control & 0xffff;
436
#endif // AMD64
437
}
438
439
void os::Linux::set_fpu_control_word(int fpu_control) {
440
#ifndef AMD64
441
_FPU_SETCW(fpu_control);
442
#endif // !AMD64
443
}
444
445
// Check that the linux kernel version is 2.4 or higher since earlier
446
// versions do not support SSE without patches.
447
bool os::supports_sse() {
448
#ifdef AMD64
449
return true;
450
#else
451
struct utsname uts;
452
if( uname(&uts) != 0 ) return false; // uname fails?
453
char *minor_string;
454
int major = strtol(uts.release,&minor_string,10);
455
int minor = strtol(minor_string+1,NULL,10);
456
bool result = (major > 2 || (major==2 && minor >= 4));
457
log_info(os)("OS version is %d.%d, which %s support SSE/SSE2",
458
major,minor, result ? "DOES" : "does NOT");
459
return result;
460
#endif // AMD64
461
}
462
463
juint os::cpu_microcode_revision() {
464
juint result = 0;
465
char data[2048] = {0}; // lines should fit in 2K buf
466
size_t len = sizeof(data);
467
FILE *fp = fopen("/proc/cpuinfo", "r");
468
if (fp) {
469
while (!feof(fp)) {
470
if (fgets(data, len, fp)) {
471
if (strstr(data, "microcode") != NULL) {
472
char* rev = strchr(data, ':');
473
if (rev != NULL) sscanf(rev + 1, "%x", &result);
474
break;
475
}
476
}
477
}
478
fclose(fp);
479
}
480
return result;
481
}
482
483
////////////////////////////////////////////////////////////////////////////////
484
// thread stack
485
486
// Minimum usable stack sizes required to get to user code. Space for
487
// HotSpot guard pages is added later.
488
size_t os::Posix::_compiler_thread_min_stack_allowed = 48 * K;
489
size_t os::Posix::_java_thread_min_stack_allowed = 40 * K;
490
#ifdef _LP64
491
size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;
492
#else
493
size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;
494
#endif // _LP64
495
496
// return default stack size for thr_type
497
size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
498
// default stack size (compiler thread needs larger stack)
499
#ifdef AMD64
500
size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
501
#else
502
size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
503
#endif // AMD64
504
return s;
505
}
506
507
/////////////////////////////////////////////////////////////////////////////
508
// helper functions for fatal error handler
509
510
void os::print_context(outputStream *st, const void *context) {
511
if (context == NULL) return;
512
513
const ucontext_t *uc = (const ucontext_t*)context;
514
st->print_cr("Registers:");
515
#ifdef AMD64
516
st->print( "RAX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RAX]);
517
st->print(", RBX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RBX]);
518
st->print(", RCX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RCX]);
519
st->print(", RDX=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RDX]);
520
st->cr();
521
st->print( "RSP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RSP]);
522
st->print(", RBP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RBP]);
523
st->print(", RSI=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RSI]);
524
st->print(", RDI=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RDI]);
525
st->cr();
526
st->print( "R8 =" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R8]);
527
st->print(", R9 =" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R9]);
528
st->print(", R10=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R10]);
529
st->print(", R11=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R11]);
530
st->cr();
531
st->print( "R12=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R12]);
532
st->print(", R13=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R13]);
533
st->print(", R14=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R14]);
534
st->print(", R15=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_R15]);
535
st->cr();
536
st->print( "RIP=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_RIP]);
537
st->print(", EFLAGS=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_EFL]);
538
st->print(", CSGSFS=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_CSGSFS]);
539
st->print(", ERR=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_ERR]);
540
st->cr();
541
st->print(" TRAPNO=" INTPTR_FORMAT, (intptr_t)uc->uc_mcontext.gregs[REG_TRAPNO]);
542
#else
543
st->print( "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EAX]);
544
st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EBX]);
545
st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ECX]);
546
st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EDX]);
547
st->cr();
548
st->print( "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_UESP]);
549
st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EBP]);
550
st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ESI]);
551
st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EDI]);
552
st->cr();
553
st->print( "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EIP]);
554
st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EFL]);
555
st->print(", CR2=" PTR64_FORMAT, (uint64_t)uc->uc_mcontext.cr2);
556
#endif // AMD64
557
st->cr();
558
st->cr();
559
560
intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
561
st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
562
print_hex_dump(st, (address)sp, (address)(sp + 8), sizeof(intptr_t));
563
st->cr();
564
565
// Note: it may be unsafe to inspect memory near pc. For example, pc may
566
// point to garbage if entry point in an nmethod is corrupted. Leave
567
// this at the end, and hope for the best.
568
address pc = os::Posix::ucontext_get_pc(uc);
569
print_instructions(st, pc, sizeof(char));
570
st->cr();
571
}
572
573
void os::print_register_info(outputStream *st, const void *context) {
574
if (context == NULL) return;
575
576
const ucontext_t *uc = (const ucontext_t*)context;
577
578
st->print_cr("Register to memory mapping:");
579
st->cr();
580
581
// this is horrendously verbose but the layout of the registers in the
582
// context does not match how we defined our abstract Register set, so
583
// we can't just iterate through the gregs area
584
585
// this is only for the "general purpose" registers
586
587
#ifdef AMD64
588
st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);
589
st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);
590
st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);
591
st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);
592
st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);
593
st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);
594
st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);
595
st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);
596
st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);
597
st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);
598
st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);
599
st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);
600
st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);
601
st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);
602
st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);
603
st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);
604
#else
605
st->print("EAX="); print_location(st, uc->uc_mcontext.gregs[REG_EAX]);
606
st->print("EBX="); print_location(st, uc->uc_mcontext.gregs[REG_EBX]);
607
st->print("ECX="); print_location(st, uc->uc_mcontext.gregs[REG_ECX]);
608
st->print("EDX="); print_location(st, uc->uc_mcontext.gregs[REG_EDX]);
609
st->print("ESP="); print_location(st, uc->uc_mcontext.gregs[REG_ESP]);
610
st->print("EBP="); print_location(st, uc->uc_mcontext.gregs[REG_EBP]);
611
st->print("ESI="); print_location(st, uc->uc_mcontext.gregs[REG_ESI]);
612
st->print("EDI="); print_location(st, uc->uc_mcontext.gregs[REG_EDI]);
613
#endif // AMD64
614
615
st->cr();
616
}
617
618
void os::setup_fpu() {
619
#ifndef AMD64
620
address fpu_cntrl = StubRoutines::x86::addr_fpu_cntrl_wrd_std();
621
__asm__ volatile ( "fldcw (%0)" :
622
: "r" (fpu_cntrl) : "memory");
623
#endif // !AMD64
624
}
625
626
#ifndef PRODUCT
627
void os::verify_stack_alignment() {
628
#ifdef AMD64
629
assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
630
#endif
631
}
632
#endif
633
634
635
/*
636
* IA32 only: execute code at a high address in case buggy NX emulation is present. I.e. avoid CS limit
637
* updates (JDK-8023956).
638
*/
639
void os::workaround_expand_exec_shield_cs_limit() {
640
#if defined(IA32)
641
assert(Linux::initial_thread_stack_bottom() != NULL, "sanity");
642
size_t page_size = os::vm_page_size();
643
644
/*
645
* JDK-8197429
646
*
647
* Expand the stack mapping to the end of the initial stack before
648
* attempting to install the codebuf. This is needed because newer
649
* Linux kernels impose a distance of a megabyte between stack
650
* memory and other memory regions. If we try to install the
651
* codebuf before expanding the stack the installation will appear
652
* to succeed but we'll get a segfault later if we expand the stack
653
* in Java code.
654
*
655
*/
656
if (os::is_primordial_thread()) {
657
address limit = Linux::initial_thread_stack_bottom();
658
if (! DisablePrimordialThreadGuardPages) {
659
limit += StackOverflow::stack_red_zone_size() +
660
StackOverflow::stack_yellow_zone_size();
661
}
662
os::Linux::expand_stack_to(limit);
663
}
664
665
/*
666
* Take the highest VA the OS will give us and exec
667
*
668
* Although using -(pagesz) as mmap hint works on newer kernel as you would
669
* think, older variants affected by this work-around don't (search forward only).
670
*
671
* On the affected distributions, we understand the memory layout to be:
672
*
673
* TASK_LIMIT= 3G, main stack base close to TASK_LIMT.
674
*
675
* A few pages south main stack will do it.
676
*
677
* If we are embedded in an app other than launcher (initial != main stack),
678
* we don't have much control or understanding of the address space, just let it slide.
679
*/
680
char* hint = (char*)(Linux::initial_thread_stack_bottom() -
681
(StackOverflow::stack_guard_zone_size() + page_size));
682
char* codebuf = os::attempt_reserve_memory_at(hint, page_size);
683
684
if (codebuf == NULL) {
685
// JDK-8197429: There may be a stack gap of one megabyte between
686
// the limit of the stack and the nearest memory region: this is a
687
// Linux kernel workaround for CVE-2017-1000364. If we failed to
688
// map our codebuf, try again at an address one megabyte lower.
689
hint -= 1 * M;
690
codebuf = os::attempt_reserve_memory_at(hint, page_size);
691
}
692
693
if ((codebuf == NULL) || (!os::commit_memory(codebuf, page_size, true))) {
694
return; // No matter, we tried, best effort.
695
}
696
697
MemTracker::record_virtual_memory_type((address)codebuf, mtInternal);
698
699
log_info(os)("[CS limit NX emulation work-around, exec code at: %p]", codebuf);
700
701
// Some code to exec: the 'ret' instruction
702
codebuf[0] = 0xC3;
703
704
// Call the code in the codebuf
705
__asm__ volatile("call *%0" : : "r"(codebuf));
706
707
// keep the page mapped so CS limit isn't reduced.
708
#endif
709
}
710
711
int os::extra_bang_size_in_bytes() {
712
// JDK-8050147 requires the full cache line bang for x86.
713
return VM_Version::L1_line_size();
714
}
715
716