1 /* 2 * Copyright (c) 1999, 2010, 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 "assembler_x86.inline.hpp" 27 #include "classfile/classLoader.hpp" 28 #include "classfile/systemDictionary.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "code/icBuffer.hpp" 31 #include "code/vtableStubs.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "jvm_windows.h" 34 #include "memory/allocation.inline.hpp" 35 #include "mutex_windows.inline.hpp" 36 #include "nativeInst_x86.hpp" 37 #include "os_share_windows.hpp" 38 #include "prims/jniFastGetField.hpp" 39 #include "prims/jvm.h" 40 #include "prims/jvm_misc.hpp" 41 #include "runtime/arguments.hpp" 42 #include "runtime/extendedPC.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/hpi.hpp" 45 #include "runtime/interfaceSupport.hpp" 46 #include "runtime/java.hpp" 47 #include "runtime/javaCalls.hpp" 48 #include "runtime/mutexLocker.hpp" 49 #include "runtime/osThread.hpp" 50 #include "runtime/sharedRuntime.hpp" 51 #include "runtime/stubRoutines.hpp" 52 #include "runtime/timer.hpp" 53 #include "thread_windows.inline.hpp" 54 #include "utilities/events.hpp" 55 #include "utilities/vmError.hpp" 56 #ifdef COMPILER1 57 #include "c1/c1_Runtime1.hpp" 58 #endif 59 #ifdef COMPILER2 60 #include "opto/runtime.hpp" 61 #endif 62 63 # include "unwind_windows_x86.hpp" 64 #undef REG_SP 65 #undef REG_FP 66 #undef REG_PC 67 #ifdef AMD64 68 #define REG_SP Rsp 69 #define REG_FP Rbp 70 #define REG_PC Rip 71 #else 72 #define REG_SP Esp 73 #define REG_FP Ebp 74 #define REG_PC Eip 75 #endif // AMD64 76 77 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* ); 78 79 // Install a win32 structured exception handler around thread. 80 void os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method, JavaCallArguments* args, Thread* thread) { 81 __try { 82 83 #ifndef AMD64 84 // We store the current thread in this wrapperthread location 85 // and determine how far away this address is from the structured 86 // execption pointer that FS:[0] points to. This get_thread 87 // code can then get the thread pointer via FS. 88 // 89 // Warning: This routine must NEVER be inlined since we'd end up with 90 // multiple offsets. 91 // 92 volatile Thread* wrapperthread = thread; 93 94 if ( ThreadLocalStorage::get_thread_ptr_offset() == 0 ) { 95 int thread_ptr_offset; 96 __asm { 97 lea eax, dword ptr wrapperthread; 98 sub eax, dword ptr FS:[0H]; 99 mov thread_ptr_offset, eax 100 }; 101 ThreadLocalStorage::set_thread_ptr_offset(thread_ptr_offset); 102 } 103 #ifdef ASSERT 104 // Verify that the offset hasn't changed since we initally captured 105 // it. This might happen if we accidentally ended up with an 106 // inlined version of this routine. 107 else { 108 int test_thread_ptr_offset; 109 __asm { 110 lea eax, dword ptr wrapperthread; 111 sub eax, dword ptr FS:[0H]; 112 mov test_thread_ptr_offset, eax 113 }; 114 assert(test_thread_ptr_offset == ThreadLocalStorage::get_thread_ptr_offset(), 115 "thread pointer offset from SEH changed"); 116 } 117 #endif // ASSERT 118 #endif // !AMD64 119 120 f(value, method, args, thread); 121 } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) { 122 // Nothing to do. 123 } 124 } 125 126 #ifdef AMD64 127 128 // This is the language specific handler for exceptions 129 // originating from dynamically generated code. 130 // We call the standard structured exception handler 131 // We only expect Continued Execution since we cannot unwind 132 // from generated code. 133 LONG HandleExceptionFromCodeCache( 134 IN PEXCEPTION_RECORD ExceptionRecord, 135 IN ULONG64 EstablisherFrame, 136 IN OUT PCONTEXT ContextRecord, 137 IN OUT PDISPATCHER_CONTEXT DispatcherContext) { 138 EXCEPTION_POINTERS ep; 139 LONG result; 140 141 ep.ExceptionRecord = ExceptionRecord; 142 ep.ContextRecord = ContextRecord; 143 144 result = topLevelExceptionFilter(&ep); 145 146 // We better only get a CONTINUE_EXECUTION from our handler 147 // since we don't have unwind information registered. 148 149 guarantee( result == EXCEPTION_CONTINUE_EXECUTION, 150 "Unexpected result from topLevelExceptionFilter"); 151 152 return(ExceptionContinueExecution); 153 } 154 155 156 // Structure containing the Windows Data Structures required 157 // to register our Code Cache exception handler. 158 // We put these in the CodeCache since the API requires 159 // all addresses in these structures are relative to the Code 160 // area registered with RtlAddFunctionTable. 161 typedef struct { 162 char ExceptionHandlerInstr[16]; // jmp HandleExceptionFromCodeCache 163 RUNTIME_FUNCTION rt; 164 UNWIND_INFO_EH_ONLY unw; 165 } DynamicCodeData, *pDynamicCodeData; 166 167 #endif // AMD64 168 // 169 // Register our CodeCache area with the OS so it will dispatch exceptions 170 // to our topLevelExceptionFilter when we take an exception in our 171 // dynamically generated code. 172 // 173 // Arguments: low and high are the address of the full reserved 174 // codeCache area 175 // 176 bool os::register_code_area(char *low, char *high) { 177 #ifdef AMD64 178 179 ResourceMark rm; 180 181 pDynamicCodeData pDCD; 182 PRUNTIME_FUNCTION prt; 183 PUNWIND_INFO_EH_ONLY punwind; 184 185 // If we are using Vectored Exceptions we don't need this registration 186 if (UseVectoredExceptions) return true; 187 188 BufferBlob* blob = BufferBlob::create("CodeCache Exception Handler", sizeof(DynamicCodeData)); 189 CodeBuffer cb(blob); 190 MacroAssembler* masm = new MacroAssembler(&cb); 191 pDCD = (pDynamicCodeData) masm->pc(); 192 193 masm->jump(ExternalAddress((address)&HandleExceptionFromCodeCache)); 194 masm->flush(); 195 196 // Create an Unwind Structure specifying no unwind info 197 // other than an Exception Handler 198 punwind = &pDCD->unw; 199 punwind->Version = 1; 200 punwind->Flags = UNW_FLAG_EHANDLER; 201 punwind->SizeOfProlog = 0; 202 punwind->CountOfCodes = 0; 203 punwind->FrameRegister = 0; 204 punwind->FrameOffset = 0; 205 punwind->ExceptionHandler = (char *)(&(pDCD->ExceptionHandlerInstr[0])) - 206 (char*)low; 207 punwind->ExceptionData[0] = 0; 208 209 // This structure describes the covered dynamic code area. 210 // Addresses are relative to the beginning on the code cache area 211 prt = &pDCD->rt; 212 prt->BeginAddress = 0; 213 prt->EndAddress = (ULONG)(high - low); 214 prt->UnwindData = ((char *)punwind - low); 215 216 guarantee(RtlAddFunctionTable(prt, 1, (ULONGLONG)low), 217 "Failed to register Dynamic Code Exception Handler with RtlAddFunctionTable"); 218 219 #endif // AMD64 220 return true; 221 } 222 223 void os::initialize_thread() { 224 // Nothing to do. 225 } 226 227 // Atomics and Stub Functions 228 229 typedef jint xchg_func_t (jint, volatile jint*); 230 typedef intptr_t xchg_ptr_func_t (intptr_t, volatile intptr_t*); 231 typedef jint cmpxchg_func_t (jint, volatile jint*, jint); 232 typedef jlong cmpxchg_long_func_t (jlong, volatile jlong*, jlong); 233 typedef jint add_func_t (jint, volatile jint*); 234 typedef intptr_t add_ptr_func_t (intptr_t, volatile intptr_t*); 235 236 #ifdef AMD64 237 238 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) { 239 // try to use the stub: 240 xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry()); 241 242 if (func != NULL) { 243 os::atomic_xchg_func = func; 244 return (*func)(exchange_value, dest); 245 } 246 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 247 248 jint old_value = *dest; 249 *dest = exchange_value; 250 return old_value; 251 } 252 253 intptr_t os::atomic_xchg_ptr_bootstrap(intptr_t exchange_value, volatile intptr_t* dest) { 254 // try to use the stub: 255 xchg_ptr_func_t* func = CAST_TO_FN_PTR(xchg_ptr_func_t*, StubRoutines::atomic_xchg_ptr_entry()); 256 257 if (func != NULL) { 258 os::atomic_xchg_ptr_func = func; 259 return (*func)(exchange_value, dest); 260 } 261 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 262 263 intptr_t old_value = *dest; 264 *dest = exchange_value; 265 return old_value; 266 } 267 268 269 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) { 270 // try to use the stub: 271 cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry()); 272 273 if (func != NULL) { 274 os::atomic_cmpxchg_func = func; 275 return (*func)(exchange_value, dest, compare_value); 276 } 277 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 278 279 jint old_value = *dest; 280 if (old_value == compare_value) 281 *dest = exchange_value; 282 return old_value; 283 } 284 #endif // AMD64 285 286 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) { 287 // try to use the stub: 288 cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry()); 289 290 if (func != NULL) { 291 os::atomic_cmpxchg_long_func = func; 292 return (*func)(exchange_value, dest, compare_value); 293 } 294 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 295 296 jlong old_value = *dest; 297 if (old_value == compare_value) 298 *dest = exchange_value; 299 return old_value; 300 } 301 302 #ifdef AMD64 303 304 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) { 305 // try to use the stub: 306 add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry()); 307 308 if (func != NULL) { 309 os::atomic_add_func = func; 310 return (*func)(add_value, dest); 311 } 312 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 313 314 return (*dest) += add_value; 315 } 316 317 intptr_t os::atomic_add_ptr_bootstrap(intptr_t add_value, volatile intptr_t* dest) { 318 // try to use the stub: 319 add_ptr_func_t* func = CAST_TO_FN_PTR(add_ptr_func_t*, StubRoutines::atomic_add_ptr_entry()); 320 321 if (func != NULL) { 322 os::atomic_add_ptr_func = func; 323 return (*func)(add_value, dest); 324 } 325 assert(Threads::number_of_threads() == 0, "for bootstrap only"); 326 327 return (*dest) += add_value; 328 } 329 330 xchg_func_t* os::atomic_xchg_func = os::atomic_xchg_bootstrap; 331 xchg_ptr_func_t* os::atomic_xchg_ptr_func = os::atomic_xchg_ptr_bootstrap; 332 cmpxchg_func_t* os::atomic_cmpxchg_func = os::atomic_cmpxchg_bootstrap; 333 add_func_t* os::atomic_add_func = os::atomic_add_bootstrap; 334 add_ptr_func_t* os::atomic_add_ptr_func = os::atomic_add_ptr_bootstrap; 335 336 #endif // AMD64 337 338 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap; 339 340 ExtendedPC os::fetch_frame_from_context(void* ucVoid, 341 intptr_t** ret_sp, intptr_t** ret_fp) { 342 343 ExtendedPC epc; 344 CONTEXT* uc = (CONTEXT*)ucVoid; 345 346 if (uc != NULL) { 347 epc = ExtendedPC((address)uc->REG_PC); 348 if (ret_sp) *ret_sp = (intptr_t*)uc->REG_SP; 349 if (ret_fp) *ret_fp = (intptr_t*)uc->REG_FP; 350 } else { 351 // construct empty ExtendedPC for return value checking 352 epc = ExtendedPC(NULL); 353 if (ret_sp) *ret_sp = (intptr_t *)NULL; 354 if (ret_fp) *ret_fp = (intptr_t *)NULL; 355 } 356 357 return epc; 358 } 359 360 frame os::fetch_frame_from_context(void* ucVoid) { 361 intptr_t* sp; 362 intptr_t* fp; 363 ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp); 364 return frame(sp, fp, epc.pc()); 365 } 366 367 // VC++ does not save frame pointer on stack in optimized build. It 368 // can be turned off by /Oy-. If we really want to walk C frames, 369 // we can use the StackWalk() API. 370 frame os::get_sender_for_C_frame(frame* fr) { 371 return frame(fr->sender_sp(), fr->link(), fr->sender_pc()); 372 } 373 374 375 #ifndef AMD64 376 intptr_t* _get_previous_fp() { 377 intptr_t **frameptr; 378 __asm { 379 mov frameptr, ebp 380 }; 381 return *frameptr; 382 } 383 #endif // !AMD64 384 385 frame os::current_frame() { 386 387 #ifdef AMD64 388 // apparently _asm not supported on windows amd64 389 typedef intptr_t* get_fp_func (); 390 get_fp_func* func = CAST_TO_FN_PTR(get_fp_func*, 391 StubRoutines::x86::get_previous_fp_entry()); 392 if (func == NULL) return frame(NULL, NULL, NULL); 393 intptr_t* fp = (*func)(); 394 #else 395 intptr_t* fp = _get_previous_fp(); 396 #endif // AMD64 397 398 frame myframe((intptr_t*)os::current_stack_pointer(), 399 (intptr_t*)fp, 400 CAST_FROM_FN_PTR(address, os::current_frame)); 401 if (os::is_first_C_frame(&myframe)) { 402 // stack is not walkable 403 return frame(NULL, NULL, NULL); 404 } else { 405 return os::get_sender_for_C_frame(&myframe); 406 } 407 } 408 409 void os::print_context(outputStream *st, void *context) { 410 if (context == NULL) return; 411 412 CONTEXT* uc = (CONTEXT*)context; 413 414 st->print_cr("Registers:"); 415 #ifdef AMD64 416 st->print( "RAX=" INTPTR_FORMAT, uc->Rax); 417 st->print(", RBX=" INTPTR_FORMAT, uc->Rbx); 418 st->print(", RCX=" INTPTR_FORMAT, uc->Rcx); 419 st->print(", RDX=" INTPTR_FORMAT, uc->Rdx); 420 st->cr(); 421 st->print( "RSP=" INTPTR_FORMAT, uc->Rsp); 422 st->print(", RBP=" INTPTR_FORMAT, uc->Rbp); 423 st->print(", RSI=" INTPTR_FORMAT, uc->Rsi); 424 st->print(", RDI=" INTPTR_FORMAT, uc->Rdi); 425 st->cr(); 426 st->print( "R8=" INTPTR_FORMAT, uc->R8); 427 st->print(", R9=" INTPTR_FORMAT, uc->R9); 428 st->print(", R10=" INTPTR_FORMAT, uc->R10); 429 st->print(", R11=" INTPTR_FORMAT, uc->R11); 430 st->cr(); 431 st->print( "R12=" INTPTR_FORMAT, uc->R12); 432 st->print(", R13=" INTPTR_FORMAT, uc->R13); 433 st->print(", R14=" INTPTR_FORMAT, uc->R14); 434 st->print(", R15=" INTPTR_FORMAT, uc->R15); 435 st->cr(); 436 st->print( "RIP=" INTPTR_FORMAT, uc->Rip); 437 st->print(", EFLAGS=" INTPTR_FORMAT, uc->EFlags); 438 439 st->cr(); 440 st->cr(); 441 442 st->print_cr("Register to memory mapping:"); 443 st->cr(); 444 445 // this is only for the "general purpose" registers 446 447 st->print_cr("RAX=" INTPTR_FORMAT, uc->Rax); 448 print_location(st, uc->Rax); 449 st->cr(); 450 st->print_cr("RBX=" INTPTR_FORMAT, uc->Rbx); 451 print_location(st, uc->Rbx); 452 st->cr(); 453 st->print_cr("RCX=" INTPTR_FORMAT, uc->Rcx); 454 print_location(st, uc->Rcx); 455 st->cr(); 456 st->print_cr("RDX=" INTPTR_FORMAT, uc->Rdx); 457 print_location(st, uc->Rdx); 458 st->cr(); 459 st->print_cr("RSP=" INTPTR_FORMAT, uc->Rsp); 460 print_location(st, uc->Rsp); 461 st->cr(); 462 st->print_cr("RBP=" INTPTR_FORMAT, uc->Rbp); 463 print_location(st, uc->Rbp); 464 st->cr(); 465 st->print_cr("RSI=" INTPTR_FORMAT, uc->Rsi); 466 print_location(st, uc->Rsi); 467 st->cr(); 468 st->print_cr("RDI=" INTPTR_FORMAT, uc->Rdi); 469 print_location(st, uc->Rdi); 470 st->cr(); 471 st->print_cr("R8 =" INTPTR_FORMAT, uc->R8); 472 print_location(st, uc->R8); 473 st->cr(); 474 st->print_cr("R9 =" INTPTR_FORMAT, uc->R9); 475 print_location(st, uc->R9); 476 st->cr(); 477 st->print_cr("R10=" INTPTR_FORMAT, uc->R10); 478 print_location(st, uc->R10); 479 st->cr(); 480 st->print_cr("R11=" INTPTR_FORMAT, uc->R11); 481 print_location(st, uc->R11); 482 st->cr(); 483 st->print_cr("R12=" INTPTR_FORMAT, uc->R12); 484 print_location(st, uc->R12); 485 st->cr(); 486 st->print_cr("R13=" INTPTR_FORMAT, uc->R13); 487 print_location(st, uc->R13); 488 st->cr(); 489 st->print_cr("R14=" INTPTR_FORMAT, uc->R14); 490 print_location(st, uc->R14); 491 st->cr(); 492 st->print_cr("R15=" INTPTR_FORMAT, uc->R15); 493 print_location(st, uc->R15); 494 #else 495 st->print( "EAX=" INTPTR_FORMAT, uc->Eax); 496 st->print(", EBX=" INTPTR_FORMAT, uc->Ebx); 497 st->print(", ECX=" INTPTR_FORMAT, uc->Ecx); 498 st->print(", EDX=" INTPTR_FORMAT, uc->Edx); 499 st->cr(); 500 st->print( "ESP=" INTPTR_FORMAT, uc->Esp); 501 st->print(", EBP=" INTPTR_FORMAT, uc->Ebp); 502 st->print(", ESI=" INTPTR_FORMAT, uc->Esi); 503 st->print(", EDI=" INTPTR_FORMAT, uc->Edi); 504 st->cr(); 505 st->print( "EIP=" INTPTR_FORMAT, uc->Eip); 506 st->print(", EFLAGS=" INTPTR_FORMAT, uc->EFlags); 507 508 st->cr(); 509 st->cr(); 510 511 st->print_cr("Register to memory mapping:"); 512 st->cr(); 513 514 // this is only for the "general purpose" registers 515 516 st->print_cr("EAX=" INTPTR_FORMAT, uc->Eax); 517 print_location(st, uc->Eax); 518 st->cr(); 519 st->print_cr("EBX=" INTPTR_FORMAT, uc->Ebx); 520 print_location(st, uc->Ebx); 521 st->cr(); 522 st->print_cr("ECX=" INTPTR_FORMAT, uc->Ecx); 523 print_location(st, uc->Ecx); 524 st->cr(); 525 st->print_cr("EDX=" INTPTR_FORMAT, uc->Edx); 526 print_location(st, uc->Edx); 527 st->cr(); 528 st->print_cr("ESP=" INTPTR_FORMAT, uc->Esp); 529 print_location(st, uc->Esp); 530 st->cr(); 531 st->print_cr("EBP=" INTPTR_FORMAT, uc->Ebp); 532 print_location(st, uc->Ebp); 533 st->cr(); 534 st->print_cr("ESI=" INTPTR_FORMAT, uc->Esi); 535 print_location(st, uc->Esi); 536 st->cr(); 537 st->print_cr("EDI=" INTPTR_FORMAT, uc->Edi); 538 print_location(st, uc->Edi); 539 #endif // AMD64 540 st->cr(); 541 st->cr(); 542 543 intptr_t *sp = (intptr_t *)uc->REG_SP; 544 st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp); 545 print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t)); 546 st->cr(); 547 548 // Note: it may be unsafe to inspect memory near pc. For example, pc may 549 // point to garbage if entry point in an nmethod is corrupted. Leave 550 // this at the end, and hope for the best. 551 address pc = (address)uc->REG_PC; 552 st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc); 553 print_hex_dump(st, pc - 16, pc + 16, sizeof(char)); 554 st->cr(); 555 } 556 557 extern "C" int SafeFetch32 (int * adr, int Err) { 558 int rv = Err ; 559 _try { 560 rv = *((volatile int *) adr) ; 561 } __except(EXCEPTION_EXECUTE_HANDLER) { 562 } 563 return rv ; 564 } 565 566 extern "C" intptr_t SafeFetchN (intptr_t * adr, intptr_t Err) { 567 intptr_t rv = Err ; 568 _try { 569 rv = *((volatile intptr_t *) adr) ; 570 } __except(EXCEPTION_EXECUTE_HANDLER) { 571 } 572 return rv ; 573 } 574 575 extern "C" int SpinPause () { 576 #ifdef AMD64 577 return 0 ; 578 #else 579 // pause == rep:nop 580 // On systems that don't support pause a rep:nop 581 // is executed as a nop. The rep: prefix is ignored. 582 _asm { 583 pause ; 584 }; 585 return 1 ; 586 #endif // AMD64 587 } 588 589 590 void os::setup_fpu() { 591 #ifndef AMD64 592 int fpu_cntrl_word = StubRoutines::fpu_cntrl_wrd_std(); 593 __asm fldcw fpu_cntrl_word; 594 #endif // !AMD64 595 }