src/os_cpu/windows_x86/vm/os_windows_x86.cpp

Tue, 03 Aug 2010 08:13:38 -0400

author
bobv
date
Tue, 03 Aug 2010 08:13:38 -0400
changeset 2036
126ea7725993
parent 1907
c18cbe5936b8
child 2103
3e8fbc61cee8
permissions
-rw-r--r--

6953477: Increase portability and flexibility of building Hotspot
Summary: A collection of portability improvements including shared code support for PPC, ARM platforms, software floating point, cross compilation support and improvements in error crash detail.
Reviewed-by: phh, never, coleenp, dholmes

     1 /*
     2  * Copyright (c) 1999, 2009, 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  */
    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
    41 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* );
    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 {
    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;
    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
    84     f(value, method, args, thread);
    85   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
    86       // Nothing to do.
    87   }
    88 }
    90 #ifdef AMD64
    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;
   105   ep.ExceptionRecord = ExceptionRecord;
   106   ep.ContextRecord = ContextRecord;
   108   result = topLevelExceptionFilter(&ep);
   110   // We better only get a CONTINUE_EXECUTION from our handler
   111   // since we don't have unwind information registered.
   113   guarantee( result == EXCEPTION_CONTINUE_EXECUTION,
   114              "Unexpected result from topLevelExceptionFilter");
   116   return(ExceptionContinueExecution);
   117 }
   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;
   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
   143   ResourceMark rm;
   145   pDynamicCodeData pDCD;
   146   PRUNTIME_FUNCTION prt;
   147   PUNWIND_INFO_EH_ONLY punwind;
   149   // If we are using Vectored Exceptions we don't need this registration
   150   if (UseVectoredExceptions) return true;
   152   BufferBlob* b = BufferBlob::create("CodeCache Exception Handler", sizeof (DynamicCodeData));
   153   CodeBuffer cb(b->instructions_begin(), b->instructions_size());
   154   MacroAssembler* masm = new MacroAssembler(&cb);
   155   pDCD = (pDynamicCodeData) masm->pc();
   157   masm->jump(ExternalAddress((address)&HandleExceptionFromCodeCache));
   158   masm->flush();
   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;
   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);
   180   guarantee(RtlAddFunctionTable(prt, 1, (ULONGLONG)low),
   181             "Failed to register Dynamic Code Exception Handler with RtlAddFunctionTable");
   183 #endif // AMD64
   184   return true;
   185 }
   187 void os::initialize_thread() {
   188 // Nothing to do.
   189 }
   191 // Atomics and Stub Functions
   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*);
   200 #ifdef AMD64
   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());
   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");
   212   jint old_value = *dest;
   213   *dest = exchange_value;
   214   return old_value;
   215 }
   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());
   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");
   227   intptr_t old_value = *dest;
   228   *dest = exchange_value;
   229   return old_value;
   230 }
   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());
   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");
   243   jint old_value = *dest;
   244   if (old_value == compare_value)
   245     *dest = exchange_value;
   246   return old_value;
   247 }
   248 #endif // AMD64
   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());
   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");
   260   jlong old_value = *dest;
   261   if (old_value == compare_value)
   262     *dest = exchange_value;
   263   return old_value;
   264 }
   266 #ifdef AMD64
   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());
   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");
   278   return (*dest) += add_value;
   279 }
   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());
   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");
   291   return (*dest) += add_value;
   292 }
   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;
   300 #endif // AMD64
   302 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
   304 ExtendedPC os::fetch_frame_from_context(void* ucVoid,
   305                     intptr_t** ret_sp, intptr_t** ret_fp) {
   307   ExtendedPC  epc;
   308   CONTEXT* uc = (CONTEXT*)ucVoid;
   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   }
   321   return epc;
   322 }
   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 }
   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 }
   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
   349 frame os::current_frame() {
   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
   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 }
   373 void os::print_context(outputStream *st, void *context) {
   374   if (context == NULL) return;
   376   CONTEXT* uc = (CONTEXT*)context;
   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);
   403   st->cr();
   404   st->cr();
   406   st->print_cr("Register to memory mapping:");
   407   st->cr();
   409   // this is only for the "general purpose" registers
   411   st->print_cr("RAX=" INTPTR_FORMAT, uc->Rax);
   412   print_location(st, uc->Rax);
   413   st->cr();
   414   st->print_cr("RBX=" INTPTR_FORMAT, uc->Rbx);
   415   print_location(st, uc->Rbx);
   416   st->cr();
   417   st->print_cr("RCX=" INTPTR_FORMAT, uc->Rcx);
   418   print_location(st, uc->Rcx);
   419   st->cr();
   420   st->print_cr("RDX=" INTPTR_FORMAT, uc->Rdx);
   421   print_location(st, uc->Rdx);
   422   st->cr();
   423   st->print_cr("RSP=" INTPTR_FORMAT, uc->Rsp);
   424   print_location(st, uc->Rsp);
   425   st->cr();
   426   st->print_cr("RBP=" INTPTR_FORMAT, uc->Rbp);
   427   print_location(st, uc->Rbp);
   428   st->cr();
   429   st->print_cr("RSI=" INTPTR_FORMAT, uc->Rsi);
   430   print_location(st, uc->Rsi);
   431   st->cr();
   432   st->print_cr("RDI=" INTPTR_FORMAT, uc->Rdi);
   433   print_location(st, uc->Rdi);
   434   st->cr();
   435   st->print_cr("R8 =" INTPTR_FORMAT, uc->R8);
   436   print_location(st, uc->R8);
   437   st->cr();
   438   st->print_cr("R9 =" INTPTR_FORMAT, uc->R9);
   439   print_location(st, uc->R9);
   440   st->cr();
   441   st->print_cr("R10=" INTPTR_FORMAT, uc->R10);
   442   print_location(st, uc->R10);
   443   st->cr();
   444   st->print_cr("R11=" INTPTR_FORMAT, uc->R11);
   445   print_location(st, uc->R11);
   446   st->cr();
   447   st->print_cr("R12=" INTPTR_FORMAT, uc->R12);
   448   print_location(st, uc->R12);
   449   st->cr();
   450   st->print_cr("R13=" INTPTR_FORMAT, uc->R13);
   451   print_location(st, uc->R13);
   452   st->cr();
   453   st->print_cr("R14=" INTPTR_FORMAT, uc->R14);
   454   print_location(st, uc->R14);
   455   st->cr();
   456   st->print_cr("R15=" INTPTR_FORMAT, uc->R15);
   457   print_location(st, uc->R15);
   458 #else
   459   st->print(  "EAX=" INTPTR_FORMAT, uc->Eax);
   460   st->print(", EBX=" INTPTR_FORMAT, uc->Ebx);
   461   st->print(", ECX=" INTPTR_FORMAT, uc->Ecx);
   462   st->print(", EDX=" INTPTR_FORMAT, uc->Edx);
   463   st->cr();
   464   st->print(  "ESP=" INTPTR_FORMAT, uc->Esp);
   465   st->print(", EBP=" INTPTR_FORMAT, uc->Ebp);
   466   st->print(", ESI=" INTPTR_FORMAT, uc->Esi);
   467   st->print(", EDI=" INTPTR_FORMAT, uc->Edi);
   468   st->cr();
   469   st->print(  "EIP=" INTPTR_FORMAT, uc->Eip);
   470   st->print(", EFLAGS=" INTPTR_FORMAT, uc->EFlags);
   472   st->cr();
   473   st->cr();
   475   st->print_cr("Register to memory mapping:");
   476   st->cr();
   478   // this is only for the "general purpose" registers
   480   st->print_cr("EAX=" INTPTR_FORMAT, uc->Eax);
   481   print_location(st, uc->Eax);
   482   st->cr();
   483   st->print_cr("EBX=" INTPTR_FORMAT, uc->Ebx);
   484   print_location(st, uc->Ebx);
   485   st->cr();
   486   st->print_cr("ECX=" INTPTR_FORMAT, uc->Ecx);
   487   print_location(st, uc->Ecx);
   488   st->cr();
   489   st->print_cr("EDX=" INTPTR_FORMAT, uc->Edx);
   490   print_location(st, uc->Edx);
   491   st->cr();
   492   st->print_cr("ESP=" INTPTR_FORMAT, uc->Esp);
   493   print_location(st, uc->Esp);
   494   st->cr();
   495   st->print_cr("EBP=" INTPTR_FORMAT, uc->Ebp);
   496   print_location(st, uc->Ebp);
   497   st->cr();
   498   st->print_cr("ESI=" INTPTR_FORMAT, uc->Esi);
   499   print_location(st, uc->Esi);
   500   st->cr();
   501   st->print_cr("EDI=" INTPTR_FORMAT, uc->Edi);
   502   print_location(st, uc->Edi);
   503 #endif // AMD64
   504   st->cr();
   505   st->cr();
   507   intptr_t *sp = (intptr_t *)uc->REG_SP;
   508   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
   509   print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t));
   510   st->cr();
   512   // Note: it may be unsafe to inspect memory near pc. For example, pc may
   513   // point to garbage if entry point in an nmethod is corrupted. Leave
   514   // this at the end, and hope for the best.
   515   address pc = (address)uc->REG_PC;
   516   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
   517   print_hex_dump(st, pc - 16, pc + 16, sizeof(char));
   518   st->cr();
   519 }
   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 }
   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 }
   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 }
   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 }

mercurial