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