1 /* 2 * Copyright (c) 1999, 2020, 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/classLoader.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "classfile/vmSymbols.hpp" 31 #include "code/icBuffer.hpp" 32 #include "code/vtableStubs.hpp" 33 #include "interpreter/interpreter.hpp" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "nativeInst_x86.hpp" 37 #include "os_share_windows.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/sharedRuntime.hpp" 48 #include "runtime/stubRoutines.hpp" 49 #include "runtime/thread.inline.hpp" 50 #include "runtime/timer.hpp" 51 #include "symbolengine.hpp" 52 #include "unwind_windows_x86.hpp" 53 #include "utilities/events.hpp" 54 #include "utilities/vmError.hpp" 55 #include "windbghelp.hpp" 56 57 58 #undef REG_SP 59 #undef REG_FP 60 #undef REG_PC 61 #ifdef AMD64 62 #define REG_SP Rsp 63 #define REG_FP Rbp 64 #define REG_PC Rip 65 #else 66 #define REG_SP Esp 67 #define REG_FP Ebp 68 #define REG_PC Eip 69 #endif // AMD64 70 71 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* ); 72 73 // Install a win32 structured exception handler around thread. 74 void os::os_exception_wrapper(java_call_t f, JavaValue* value, const methodHandle& method, JavaCallArguments* args, Thread* thread) { 75 __try { 76 77 #ifndef AMD64 78 // We store the current thread in this wrapperthread location 79 // and determine how far away this address is from the structured 80 // execption pointer that FS:[0] points to. This get_thread 81 // code can then get the thread pointer via FS. 82 // 83 // Warning: This routine must NEVER be inlined since we'd end up with 84 // multiple offsets. 85 // 86 volatile Thread* wrapperthread = thread; 87 88 if (os::win32::get_thread_ptr_offset() == 0) { 89 int thread_ptr_offset; 90 __asm { 91 lea eax, dword ptr wrapperthread; 92 sub eax, dword ptr FS:[0H]; 93 mov thread_ptr_offset, eax 94 }; 95 os::win32::set_thread_ptr_offset(thread_ptr_offset); 96 } 97 #ifdef ASSERT 98 // Verify that the offset hasn't changed since we initally captured 99 // it. This might happen if we accidentally ended up with an 100 // inlined version of this routine. 101 else { 102 int test_thread_ptr_offset; 103 __asm { 104 lea eax, dword ptr wrapperthread; 105 sub eax, dword ptr FS:[0H]; 106 mov test_thread_ptr_offset, eax 107 }; 108 assert(test_thread_ptr_offset == os::win32::get_thread_ptr_offset(), 109 "thread pointer offset from SEH changed"); 110 } 111 #endif // ASSERT 112 #endif // !AMD64 113 114 f(value, method, args, thread); 115 } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) { 116 // Nothing to do. 117 } 118 } 119 120 #ifdef AMD64 121 122 // This is the language specific handler for exceptions 123 // originating from dynamically generated code. 124 // We call the standard structured exception handler 125 // We only expect Continued Execution since we cannot unwind 126 // from generated code. 127 LONG HandleExceptionFromCodeCache( 128 IN PEXCEPTION_RECORD ExceptionRecord, 129 IN ULONG64 EstablisherFrame, 130 IN OUT PCONTEXT ContextRecord, 131 IN OUT PDISPATCHER_CONTEXT DispatcherContext) { 132 EXCEPTION_POINTERS ep; 133 LONG result; 134 135 ep.ExceptionRecord = ExceptionRecord; 136 ep.ContextRecord = ContextRecord; 137 138 result = topLevelExceptionFilter(&ep); 139 140 // We better only get a CONTINUE_EXECUTION from our handler 141 // since we don't have unwind information registered. 142 143 guarantee( result == EXCEPTION_CONTINUE_EXECUTION, 144 "Unexpected result from topLevelExceptionFilter"); 145 146 return(ExceptionContinueExecution); 147 } 148 149 150 // Structure containing the Windows Data Structures required 151 // to register our Code Cache exception handler. 152 // We put these in the CodeCache since the API requires 153 // all addresses in these structures are relative to the Code 154 // area registered with RtlAddFunctionTable. 155 typedef struct { 156 char ExceptionHandlerInstr[16]; // jmp HandleExceptionFromCodeCache 157 RUNTIME_FUNCTION rt; 158 UNWIND_INFO_EH_ONLY unw; 159 } DynamicCodeData, *pDynamicCodeData; 160 161 #endif // AMD64 162 // 163 // Register our CodeCache area with the OS so it will dispatch exceptions 164 // to our topLevelExceptionFilter when we take an exception in our 165 // dynamically generated code. 166 // 167 // Arguments: low and high are the address of the full reserved 168 // codeCache area 169 // 170 bool os::register_code_area(char *low, char *high) { 171 #ifdef AMD64 172 173 ResourceMark rm; 174 175 pDynamicCodeData pDCD; 176 PRUNTIME_FUNCTION prt; 177 PUNWIND_INFO_EH_ONLY punwind; 178 179 BufferBlob* blob = BufferBlob::create("CodeCache Exception Handler", sizeof(DynamicCodeData)); 180 CodeBuffer cb(blob); 181 MacroAssembler* masm = new MacroAssembler(&cb); 182 pDCD = (pDynamicCodeData) masm->pc(); 183 184 masm->jump(ExternalAddress((address)&HandleExceptionFromCodeCache)); 185 masm->flush(); 186 187 // Create an Unwind Structure specifying no unwind info 188 // other than an Exception Handler 189 punwind = &pDCD->unw; 190 punwind->Version = 1; 191 punwind->Flags = UNW_FLAG_EHANDLER; 192 punwind->SizeOfProlog = 0; 193 punwind->CountOfCodes = 0; 194 punwind->FrameRegister = 0; 195 punwind->FrameOffset = 0; 196 punwind->ExceptionHandler = (char *)(&(pDCD->ExceptionHandlerInstr[0])) - 197 (char*)low; 198 punwind->ExceptionData[0] = 0; 199 200 // This structure describes the covered dynamic code area. 201 // Addresses are relative to the beginning on the code cache area 202 prt = &pDCD->rt; 203 prt->BeginAddress = 0; 204 prt->EndAddress = (ULONG)(high - low); 205 prt->UnwindData = ((char *)punwind - low); 206 207 guarantee(RtlAddFunctionTable(prt, 1, (ULONGLONG)low), 208 "Failed to register Dynamic Code Exception Handler with RtlAddFunctionTable"); 209 210 #endif // AMD64 211 return true; 212 } 213 214 #ifdef AMD64 215 /* 216 * Windows/x64 does not use stack frames the way expected by Java: 217 * [1] in most cases, there is no frame pointer. All locals are addressed via RSP 218 * [2] in rare cases, when alloca() is used, a frame pointer is used, but this may 219 * not be RBP. 220 * See http://msdn.microsoft.com/en-us/library/ew5tede7.aspx 221 * 222 * So it's not possible to print the native stack using the 223 * while (...) {... fr = os::get_sender_for_C_frame(&fr); } 224 * loop in vmError.cpp. We need to roll our own loop. 225 */ 226 bool os::platform_print_native_stack(outputStream* st, const void* context, 227 char *buf, int buf_size) 228 { 229 CONTEXT ctx; 230 if (context != NULL) { 231 memcpy(&ctx, context, sizeof(ctx)); 232 } else { 233 RtlCaptureContext(&ctx); 234 } 235 236 st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)"); 237 238 STACKFRAME stk; 239 memset(&stk, 0, sizeof(stk)); 240 stk.AddrStack.Offset = ctx.Rsp; 241 stk.AddrStack.Mode = AddrModeFlat; 242 stk.AddrFrame.Offset = ctx.Rbp; 243 stk.AddrFrame.Mode = AddrModeFlat; 244 stk.AddrPC.Offset = ctx.Rip; 245 stk.AddrPC.Mode = AddrModeFlat; 246 247 int count = 0; 248 address lastpc = 0; 249 while (count++ < StackPrintLimit) { 250 intptr_t* sp = (intptr_t*)stk.AddrStack.Offset; 251 intptr_t* fp = (intptr_t*)stk.AddrFrame.Offset; // NOT necessarily the same as ctx.Rbp! 252 address pc = (address)stk.AddrPC.Offset; 253 254 if (pc != NULL) { 255 if (count == 2 && lastpc == pc) { 256 // Skip it -- StackWalk64() may return the same PC 257 // (but different SP) on the first try. 258 } else { 259 // Don't try to create a frame(sp, fp, pc) -- on WinX64, stk.AddrFrame 260 // may not contain what Java expects, and may cause the frame() constructor 261 // to crash. Let's just print out the symbolic address. 262 frame::print_C_frame(st, buf, buf_size, pc); 263 // print source file and line, if available 264 char buf[128]; 265 int line_no; 266 if (SymbolEngine::get_source_info(pc, buf, sizeof(buf), &line_no)) { 267 st->print(" (%s:%d)", buf, line_no); 268 } 269 st->cr(); 270 } 271 lastpc = pc; 272 } 273 274 PVOID p = WindowsDbgHelp::symFunctionTableAccess64(GetCurrentProcess(), stk.AddrPC.Offset); 275 if (!p) { 276 // StackWalk64() can't handle this PC. Calling StackWalk64 again may cause crash. 277 break; 278 } 279 280 BOOL result = WindowsDbgHelp::stackWalk64( 281 IMAGE_FILE_MACHINE_AMD64, // __in DWORD MachineType, 282 GetCurrentProcess(), // __in HANDLE hProcess, 283 GetCurrentThread(), // __in HANDLE hThread, 284 &stk, // __inout LP STACKFRAME64 StackFrame, 285 &ctx); // __inout PVOID ContextRecord, 286 287 if (!result) { 288 break; 289 } 290 } 291 if (count > StackPrintLimit) { 292 st->print_cr("...<more frames>..."); 293 } 294 st->cr(); 295 296 return true; 297 } 298 #endif // AMD64 299 300 address os::fetch_frame_from_context(const void* ucVoid, 301 intptr_t** ret_sp, intptr_t** ret_fp) { 302 303 address epc; 304 CONTEXT* uc = (CONTEXT*)ucVoid; 305 306 if (uc != NULL) { 307 epc = (address)uc->REG_PC; 308 if (ret_sp) *ret_sp = (intptr_t*)uc->REG_SP; 309 if (ret_fp) *ret_fp = (intptr_t*)uc->REG_FP; 310 } else { 311 epc = NULL; 312 if (ret_sp) *ret_sp = (intptr_t *)NULL; 313 if (ret_fp) *ret_fp = (intptr_t *)NULL; 314 } 315 316 return epc; 317 } 318 319 frame os::fetch_frame_from_context(const void* ucVoid) { 320 intptr_t* sp; 321 intptr_t* fp; 322 address epc = fetch_frame_from_context(ucVoid, &sp, &fp); 323 return frame(sp, fp, epc); 324 } 325 326 // VC++ does not save frame pointer on stack in optimized build. It 327 // can be turned off by /Oy-. If we really want to walk C frames, 328 // we can use the StackWalk() API. 329 frame os::get_sender_for_C_frame(frame* fr) { 330 return frame(fr->sender_sp(), fr->link(), fr->sender_pc()); 331 } 332 333 #ifndef AMD64 334 // Ignore "C4172: returning address of local variable or temporary" on 32bit 335 PRAGMA_DIAG_PUSH 336 PRAGMA_DISABLE_MSVC_WARNING(4172) 337 // Returns an estimate of the current stack pointer. Result must be guaranteed 338 // to point into the calling threads stack, and be no lower than the current 339 // stack pointer. 340 address os::current_stack_pointer() { 341 int dummy; 342 address sp = (address)&dummy; 343 return sp; 344 } 345 PRAGMA_DIAG_POP 346 #else 347 // Returns the current stack pointer. Accurate value needed for 348 // os::verify_stack_alignment(). 349 address os::current_stack_pointer() { 350 typedef address get_sp_func(); 351 get_sp_func* func = CAST_TO_FN_PTR(get_sp_func*, 352 StubRoutines::x86::get_previous_sp_entry()); 353 return (*func)(); 354 } 355 #endif 356 357 bool os::win32::get_frame_at_stack_banging_point(JavaThread* thread, 358 struct _EXCEPTION_POINTERS* exceptionInfo, address pc, frame* fr) { 359 PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord; 360 address addr = (address) exceptionRecord->ExceptionInformation[1]; 361 if (Interpreter::contains(pc)) { 362 *fr = os::fetch_frame_from_context((void*)exceptionInfo->ContextRecord); 363 if (!fr->is_first_java_frame()) { 364 // get_frame_at_stack_banging_point() is only called when we 365 // have well defined stacks so java_sender() calls do not need 366 // to assert safe_for_sender() first. 367 *fr = fr->java_sender(); 368 } 369 } else { 370 // more complex code with compiled code 371 assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above"); 372 CodeBlob* cb = CodeCache::find_blob(pc); 373 if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) { 374 // Not sure where the pc points to, fallback to default 375 // stack overflow handling 376 return false; 377 } else { 378 // in compiled code, the stack banging is performed just after the return pc 379 // has been pushed on the stack 380 intptr_t* fp = (intptr_t*)exceptionInfo->ContextRecord->REG_FP; 381 intptr_t* sp = (intptr_t*)exceptionInfo->ContextRecord->REG_SP; 382 *fr = frame(sp + 1, fp, (address)*sp); 383 if (!fr->is_java_frame()) { 384 // See java_sender() comment above. 385 *fr = fr->java_sender(); 386 } 387 } 388 } 389 assert(fr->is_java_frame(), "Safety check"); 390 return true; 391 } 392 393 #ifndef AMD64 394 intptr_t* _get_previous_fp() { 395 intptr_t **frameptr; 396 __asm { 397 mov frameptr, ebp 398 }; 399 // ebp (frameptr) is for this frame (_get_previous_fp). We want the ebp for the 400 // caller of os::current_frame*(), so go up two frames. However, for 401 // optimized builds, _get_previous_fp() will be inlined, so only go 402 // up 1 frame in that case. 403 #ifdef _NMT_NOINLINE_ 404 return **(intptr_t***)frameptr; 405 #else 406 return *frameptr; 407 #endif 408 } 409 #endif // !AMD64 410 411 frame os::current_frame() { 412 413 #ifdef AMD64 414 // apparently _asm not supported on windows amd64 415 typedef intptr_t* get_fp_func (); 416 get_fp_func* func = CAST_TO_FN_PTR(get_fp_func*, 417 StubRoutines::x86::get_previous_fp_entry()); 418 if (func == NULL) return frame(); 419 intptr_t* fp = (*func)(); 420 if (fp == NULL) { 421 return frame(); 422 } 423 #else 424 intptr_t* fp = _get_previous_fp(); 425 #endif // AMD64 426 427 frame myframe((intptr_t*)os::current_stack_pointer(), 428 (intptr_t*)fp, 429 CAST_FROM_FN_PTR(address, os::current_frame)); 430 if (os::is_first_C_frame(&myframe)) { 431 // stack is not walkable 432 return frame(); 433 } else { 434 return os::get_sender_for_C_frame(&myframe); 435 } 436 } 437 438 void os::print_context(outputStream *st, const void *context) { 439 if (context == NULL) return; 440 441 const CONTEXT* uc = (const CONTEXT*)context; 442 443 st->print_cr("Registers:"); 444 #ifdef AMD64 445 st->print( "RAX=" INTPTR_FORMAT, uc->Rax); 446 st->print(", RBX=" INTPTR_FORMAT, uc->Rbx); 447 st->print(", RCX=" INTPTR_FORMAT, uc->Rcx); 448 st->print(", RDX=" INTPTR_FORMAT, uc->Rdx); 449 st->cr(); 450 st->print( "RSP=" INTPTR_FORMAT, uc->Rsp); 451 st->print(", RBP=" INTPTR_FORMAT, uc->Rbp); 452 st->print(", RSI=" INTPTR_FORMAT, uc->Rsi); 453 st->print(", RDI=" INTPTR_FORMAT, uc->Rdi); 454 st->cr(); 455 st->print( "R8 =" INTPTR_FORMAT, uc->R8); 456 st->print(", R9 =" INTPTR_FORMAT, uc->R9); 457 st->print(", R10=" INTPTR_FORMAT, uc->R10); 458 st->print(", R11=" INTPTR_FORMAT, uc->R11); 459 st->cr(); 460 st->print( "R12=" INTPTR_FORMAT, uc->R12); 461 st->print(", R13=" INTPTR_FORMAT, uc->R13); 462 st->print(", R14=" INTPTR_FORMAT, uc->R14); 463 st->print(", R15=" INTPTR_FORMAT, uc->R15); 464 st->cr(); 465 st->print( "RIP=" INTPTR_FORMAT, uc->Rip); 466 st->print(", EFLAGS=" INTPTR_FORMAT, uc->EFlags); 467 #else 468 st->print( "EAX=" INTPTR_FORMAT, uc->Eax); 469 st->print(", EBX=" INTPTR_FORMAT, uc->Ebx); 470 st->print(", ECX=" INTPTR_FORMAT, uc->Ecx); 471 st->print(", EDX=" INTPTR_FORMAT, uc->Edx); 472 st->cr(); 473 st->print( "ESP=" INTPTR_FORMAT, uc->Esp); 474 st->print(", EBP=" INTPTR_FORMAT, uc->Ebp); 475 st->print(", ESI=" INTPTR_FORMAT, uc->Esi); 476 st->print(", EDI=" INTPTR_FORMAT, uc->Edi); 477 st->cr(); 478 st->print( "EIP=" INTPTR_FORMAT, uc->Eip); 479 st->print(", EFLAGS=" INTPTR_FORMAT, uc->EFlags); 480 #endif // AMD64 481 st->cr(); 482 st->cr(); 483 484 intptr_t *sp = (intptr_t *)uc->REG_SP; 485 st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp); 486 print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t)); 487 st->cr(); 488 489 // Note: it may be unsafe to inspect memory near pc. For example, pc may 490 // point to garbage if entry point in an nmethod is corrupted. Leave 491 // this at the end, and hope for the best. 492 address pc = (address)uc->REG_PC; 493 print_instructions(st, pc, sizeof(char)); 494 st->cr(); 495 } 496 497 498 void os::print_register_info(outputStream *st, const void *context) { 499 if (context == NULL) return; 500 501 const CONTEXT* uc = (const CONTEXT*)context; 502 503 st->print_cr("Register to memory mapping:"); 504 st->cr(); 505 506 // this is only for the "general purpose" registers 507 508 #ifdef AMD64 509 st->print("RIP="); print_location(st, uc->Rip); 510 st->print("RAX="); print_location(st, uc->Rax); 511 st->print("RBX="); print_location(st, uc->Rbx); 512 st->print("RCX="); print_location(st, uc->Rcx); 513 st->print("RDX="); print_location(st, uc->Rdx); 514 st->print("RSP="); print_location(st, uc->Rsp); 515 st->print("RBP="); print_location(st, uc->Rbp); 516 st->print("RSI="); print_location(st, uc->Rsi); 517 st->print("RDI="); print_location(st, uc->Rdi); 518 st->print("R8 ="); print_location(st, uc->R8); 519 st->print("R9 ="); print_location(st, uc->R9); 520 st->print("R10="); print_location(st, uc->R10); 521 st->print("R11="); print_location(st, uc->R11); 522 st->print("R12="); print_location(st, uc->R12); 523 st->print("R13="); print_location(st, uc->R13); 524 st->print("R14="); print_location(st, uc->R14); 525 st->print("R15="); print_location(st, uc->R15); 526 #else 527 st->print("EIP="); print_location(st, uc->Eip); 528 st->print("EAX="); print_location(st, uc->Eax); 529 st->print("EBX="); print_location(st, uc->Ebx); 530 st->print("ECX="); print_location(st, uc->Ecx); 531 st->print("EDX="); print_location(st, uc->Edx); 532 st->print("ESP="); print_location(st, uc->Esp); 533 st->print("EBP="); print_location(st, uc->Ebp); 534 st->print("ESI="); print_location(st, uc->Esi); 535 st->print("EDI="); print_location(st, uc->Edi); 536 #endif 537 538 st->cr(); 539 } 540 541 extern "C" int SpinPause () { 542 #ifdef AMD64 543 return 0 ; 544 #else 545 // pause == rep:nop 546 // On systems that don't support pause a rep:nop 547 // is executed as a nop. The rep: prefix is ignored. 548 _asm { 549 pause ; 550 }; 551 return 1 ; 552 #endif // AMD64 553 } 554 555 juint os::cpu_microcode_revision() { 556 juint result = 0; 557 BYTE data[8] = {0}; 558 HKEY key; 559 DWORD status = RegOpenKey(HKEY_LOCAL_MACHINE, 560 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", &key); 561 if (status == ERROR_SUCCESS) { 562 DWORD size = sizeof(data); 563 status = RegQueryValueEx(key, "Update Revision", NULL, NULL, data, &size); 564 if (status == ERROR_SUCCESS) { 565 if (size == 4) result = *((juint*)data); 566 if (size == 8) result = *((juint*)data + 1); // upper 32-bits 567 } 568 RegCloseKey(key); 569 } 570 return result; 571 } 572 573 void os::setup_fpu() { 574 #ifndef AMD64 575 int fpu_cntrl_word = StubRoutines::fpu_cntrl_wrd_std(); 576 __asm fldcw fpu_cntrl_word; 577 #endif // !AMD64 578 } 579 580 #ifndef PRODUCT 581 void os::verify_stack_alignment() { 582 #ifdef AMD64 583 // The current_stack_pointer() calls generated get_previous_sp stub routine. 584 // Only enable the assert after the routine becomes available. 585 if (StubRoutines::code1() != NULL) { 586 assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment"); 587 } 588 #endif 589 } 590 #endif 591 592 int os::extra_bang_size_in_bytes() { 593 // JDK-8050147 requires the full cache line bang for x86. 594 return VM_Version::L1_line_size(); 595 }