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