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