src/os_cpu/linux_mips/vm/os_linux_mips.cpp

Thu, 07 Sep 2017 09:12:16 +0800

author
aoqi
date
Thu, 07 Sep 2017 09:12:16 +0800
changeset 6880
52ea28d233d2
parent 414
c5f826fdfc22
child 8012
5efbe2fd5486
permissions
-rw-r--r--

#5745 [Code Reorganization] code cleanup and code style fix
This is a huge patch, but only code cleanup, code style fix and useless code deletion are included, for example:
tab -> two spaces, deleted spacees at the end of a line, delete useless comments.

This patch also included:
Declaration and definition of class MacroAssembler is moved from assembler_mips.h/cpp to macroAssembler_mips.h/cpp

     1 /*
     2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * Copyright (c) 2015, 2016, Loongson Technology. All rights reserved.
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5  *
     6  * This code is free software; you can redistribute it and/or modify it
     7  * under the terms of the GNU General Public License version 2 only, as
     8  * published by the Free Software Foundation.
     9  *
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    13  * version 2 for more details (a copy is included in the LICENSE file that
    14  * accompanied this code).
    15  *
    16  * You should have received a copy of the GNU General Public License version
    17  * 2 along with this work; if not, write to the Free Software Foundation,
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    19  *
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * or visit www.oracle.com if you need additional information or have any
    22  * questions.
    23  *
    24  */
    26 // no precompiled headers
    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 "jvm_linux.h"
    35 #include "memory/allocation.inline.hpp"
    36 #include "mutex_linux.inline.hpp"
    37 #include "os_share_linux.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/thread.inline.hpp"
    52 #include "runtime/timer.hpp"
    53 #include "utilities/events.hpp"
    54 #include "utilities/vmError.hpp"
    55 #include "utilities/debug.hpp"
    56 #include "compiler/disassembler.hpp"
    57 // put OS-includes here
    58 # include <sys/types.h>
    59 # include <sys/mman.h>
    60 # include <pthread.h>
    61 # include <signal.h>
    62 # include <errno.h>
    63 # include <dlfcn.h>
    64 # include <stdlib.h>
    65 # include <stdio.h>
    66 # include <unistd.h>
    67 # include <sys/resource.h>
    68 # include <pthread.h>
    69 # include <sys/stat.h>
    70 # include <sys/time.h>
    71 # include <sys/utsname.h>
    72 # include <sys/socket.h>
    73 # include <sys/wait.h>
    74 # include <pwd.h>
    75 # include <poll.h>
    76 # include <ucontext.h>
    77 # include <fpu_control.h>
    79 #define REG_SP 29
    80 #define REG_FP 30
    82 address os::current_stack_pointer() {
    83   register void *sp __asm__ ("$29");
    84   return (address) sp;
    85 }
    87 char* os::non_memory_address_word() {
    88   // Must never look like an address returned by reserve_memory,
    89   // even in its subfields (as defined by the CPU immediate fields,
    90   // if the CPU splits constants across multiple instructions).
    92   return (char*) -1;
    93 }
    95 void os::initialize_thread(Thread* thr) {
    96 // Nothing to do.
    97 }
    99 address os::Linux::ucontext_get_pc(ucontext_t * uc) {
   100   //return (address)uc->uc_mcontext.gregs[REG_PC];
   101   return (address)uc->uc_mcontext.pc;//aoqi:what is gregs?
   102 }
   104 intptr_t* os::Linux::ucontext_get_sp(ucontext_t * uc) {
   105   return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];
   106 }
   108 intptr_t* os::Linux::ucontext_get_fp(ucontext_t * uc) {
   109   return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];
   110 }
   112 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
   113 // is currently interrupted by SIGPROF.
   114 // os::Solaris::fetch_frame_from_ucontext() tries to skip nested signal
   115 // frames. Currently we don't do that on Linux, so it's the same as
   116 // os::fetch_frame_from_context().
   117 ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread,
   118   ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
   120   assert(thread != NULL, "just checking");
   121   assert(ret_sp != NULL, "just checking");
   122   assert(ret_fp != NULL, "just checking");
   124   return os::fetch_frame_from_context(uc, ret_sp, ret_fp);
   125 }
   127 ExtendedPC os::fetch_frame_from_context(void* ucVoid,
   128                     intptr_t** ret_sp, intptr_t** ret_fp) {
   130   ExtendedPC  epc;
   131   ucontext_t* uc = (ucontext_t*)ucVoid;
   133   address pc = (address)os::Linux::ucontext_get_pc(uc);
   135   /* Jin: to capture invalid 32-bit PC, for debbuging */
   136   if (((long)pc & 0xFFFFFFFF00000000UL) == 0)
   137   {
   138     pc = (address)((long)pc | 0x5500000000UL);
   139     tty->print_cr("<Error> 32-bit pc: %lx", pc);
   140   }
   142   if (uc != NULL) {
   143     epc = ExtendedPC(pc);
   144     if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
   145     if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
   146   } else {
   147     // construct empty ExtendedPC for return value checking
   148     epc = ExtendedPC(NULL);
   149     if (ret_sp) *ret_sp = (intptr_t *)NULL;
   150     if (ret_fp) *ret_fp = (intptr_t *)NULL;
   151   }
   153   return epc;
   154 }
   156 frame os::fetch_frame_from_context(void* ucVoid) {
   157   intptr_t* sp;
   158   intptr_t* fp;
   159   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
   160   return frame(sp, fp, epc.pc());
   161 }
   163 // By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get
   164 // turned off by -fomit-frame-pointer,
   165 frame os::get_sender_for_C_frame(frame* fr) {
   166   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
   167 }
   169 //intptr_t* _get_previous_fp() {
   170 jint* os::get_previous_fp() {
   171   int *pc;
   172   int sp;
   173   int *pc_limit = (int*)(void*)&os::get_previous_fp;
   174   int insn;
   176   {
   177     l_pc:;
   178     pc = (int*)&&l_pc;
   179     __asm__ __volatile__ ("move %0,  $sp" : "=r" (sp));
   180   }
   182   do {
   183     --pc;
   184     insn = *pc;
   185     switch(bitfield(insn, 16, 16)) {
   186       case 0x27bd:  /* addiu $sp,$sp,-i */
   187       case 0x23bd:  /* addi $sp,$sp,-i */
   188       case 0x67bd:  /* daddiu $sp,$sp,-i */
   189       case 0x63bd:  /* daddi $sp,$sp,-i */
   190         assert ((short)bitfield(insn, 0, 16)<0, "bad frame");
   191         sp -= (short)bitfield(insn, 0, 16);
   192         return (jint*)sp;
   193     }
   194   } while (pc>pc_limit);
   196   ShouldNotReachHere();
   197 }
   200 frame os::current_frame() {
   201   intptr_t* fp = (intptr_t*)get_previous_fp();
   202   frame myframe((intptr_t*)os::current_stack_pointer(),
   203                 (intptr_t*)fp,
   204                 CAST_FROM_FN_PTR(address, os::current_frame));
   205   if (os::is_first_C_frame(&myframe)) {
   206     // stack is not walkable
   207     return frame();
   208   } else {
   209     return os::get_sender_for_C_frame(&myframe);
   210   }
   211 }
   213 //x86 add 2 new assemble function here!
   214 extern "C" int
   215 JVM_handle_linux_signal(int sig,
   216                         siginfo_t* info,
   217                         void* ucVoid,
   218                         int abort_if_unrecognized) {
   219 #ifdef PRINT_SIGNAL_HANDLE
   220   tty->print_cr("Signal: signo=%d, sicode=%d, sierrno=%d, siaddr=%lx",
   221       info->si_signo,
   222       info->si_code,
   223       info->si_errno,
   224       info->si_addr);
   225 #endif
   227   ucontext_t* uc = (ucontext_t*) ucVoid;
   229   Thread* t = ThreadLocalStorage::get_thread_slow();
   231   SignalHandlerMark shm(t);
   233   // Note: it's not uncommon that JNI code uses signal/sigset to install
   234   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
   235   // or have a SIGILL handler when detecting CPU type). When that happens,
   236   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
   237   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
   238   // that do not require siginfo/ucontext first.
   240   //if (sig == SIGPIPE || sig == SIGXFSZ) {
   241   if (sig == SIGPIPE) {
   242     // allow chained handler to go first
   243     if (os::Linux::chained_handler(sig, info, ucVoid)) {
   244       return true;
   245     } else {
   246       if (PrintMiscellaneous && (WizardMode || Verbose)) {
   247         warning("Ignoring SIGPIPE - see bug 4229104");
   248       }
   249       return true;
   250     }
   251   }
   253   JavaThread* thread = NULL;
   254   VMThread* vmthread = NULL;
   255   if (os::Linux::signal_handlers_are_installed) {
   256     if (t != NULL ){
   257       if(t->is_Java_thread()) {
   258 #ifdef PRINT_SIGNAL_HANDLE
   259         //tty->print_cr("this thread is a java thread");
   260 #endif
   261         thread = (JavaThread*)t;
   262       }
   263       else if(t->is_VM_thread()){
   264 #ifdef PRINT_SIGNAL_HANDLE
   265         //tty->print_cr("this thread is a VM thread\n");
   266 #endif
   267         vmthread = (VMThread *)t;
   268       }
   269     }
   270   }
   272   // decide if this trap can be handled by a stub
   273   address stub = NULL;
   274   address pc   = NULL;
   276   pc = (address) os::Linux::ucontext_get_pc(uc);
   277 #ifdef PRINT_SIGNAL_HANDLE
   278   tty->print_cr("pc=%lx", pc);
   279   os::print_context(tty, uc);
   280 #endif
   281   //%note os_trap_1
   282   if (info != NULL && uc != NULL && thread != NULL) {
   283     pc = (address) os::Linux::ucontext_get_pc(uc);
   284     // Handle ALL stack overflow variations here
   285     if (sig == SIGSEGV) {
   286       address addr = (address) info->si_addr;
   287 #ifdef PRINT_SIGNAL_HANDLE
   288       //tty->print("handle all stack overflow variations: ");
   289       /*tty->print("addr = %lx, stack base = %lx, stack top = %lx\n",
   290         addr,
   291         thread->stack_base(),
   292         thread->stack_base() - thread->stack_size());
   293         */
   294 #endif
   296       // check if fault address is within thread stack
   297       if (addr < thread->stack_base() &&
   298           addr >= thread->stack_base() - thread->stack_size()) {
   299         // stack overflow
   300 #ifdef PRINT_SIGNAL_HANDLE
   301         tty->print("stack exception check \n");
   302 #endif
   303         if (thread->in_stack_yellow_zone(addr)) {
   304 #ifdef PRINT_SIGNAL_HANDLE
   305           tty->print("exception addr is in yellow zone\n");
   306 #endif
   307           thread->disable_stack_yellow_zone();
   308           if (thread->thread_state() == _thread_in_Java) {
   309             // Throw a stack overflow exception.  Guard pages will be reenabled
   310             // while unwinding the stack.
   311 #ifdef PRINT_SIGNAL_HANDLE
   312             tty->print("this thread is in java\n");
   313 #endif
   314             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
   315           } else {
   316             // Thread was in the vm or native code.  Return and try to finish.
   317 #ifdef PRINT_SIGNAL_HANDLE
   318             tty->print("this thread is in vm or native codes and return\n");
   319 #endif
   320             return 1;
   321           }
   322         } else if (thread->in_stack_red_zone(addr)) {
   323           // Fatal red zone violation.  Disable the guard pages and fall through
   324           // to handle_unexpected_exception way down below.
   325 #ifdef PRINT_SIGNAL_HANDLE
   326           tty->print("exception addr is in red zone\n");
   327 #endif
   328           thread->disable_stack_red_zone();
   329           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
   331           // This is a likely cause, but hard to verify. Let's just print
   332           // it as a hint.
   333           tty->print_raw_cr("Please check if any of your loaded .so files has "
   334                             "enabled executable stack (see man page execstack(8))");
   335         } else {
   336           // Accessing stack address below sp may cause SEGV if current
   337           // thread has MAP_GROWSDOWN stack. This should only happen when
   338           // current thread was created by user code with MAP_GROWSDOWN flag
   339           // and then attached to VM. See notes in os_linux.cpp.
   340 #ifdef PRINT_SIGNAL_HANDLE
   341           tty->print("exception addr is neither in yellow zone nor in the red one\n");
   342 #endif
   343           if (thread->osthread()->expanding_stack() == 0) {
   344              thread->osthread()->set_expanding_stack();
   345              if (os::Linux::manually_expand_stack(thread, addr)) {
   346                thread->osthread()->clear_expanding_stack();
   347                return 1;
   348              }
   349              thread->osthread()->clear_expanding_stack();
   350           } else {
   351              fatal("recursive segv. expanding stack.");
   352           }
   353         }
   354       } //addr <
   355     } //sig == SIGSEGV
   357     if (thread->thread_state() == _thread_in_Java) {
   358       // Java thread running in Java code => find exception handler if any
   359       // a fault inside compiled code, the interpreter, or a stub
   360 #ifdef PRINT_SIGNAL_HANDLE
   361       tty->print("java thread running in java code\n");
   362       tty->print_cr("polling address = %lx, sig=%d", os::get_polling_page(), sig);
   363 #endif
   364       if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
   366         stub = SharedRuntime::get_poll_stub(pc);
   367       } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {
   368         // BugId 4454115: A read from a MappedByteBuffer can fault
   369         // here if the underlying file has been truncated.
   370         // Do not crash the VM in such a case.
   371         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
   372         nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL;
   373 #ifdef PRINT_SIGNAL_HANDLE
   374         tty->print("cb = %lx, nm = %lx\n", cb, nm);
   375 #endif
   376         if (nm != NULL && nm->has_unsafe_access()) {
   377           stub = StubRoutines::handler_for_unsafe_access();
   378         }
   379       } else if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {
   380         // HACK: si_code does not work on linux 2.2.12-20!!!
   381         int op = pc[0] & 0x3f;
   382         int op1 = pc[3] & 0x3f;
   383         tty->print_cr("unknown opcode 0x%X -0x%X with SIGFPE.", op, op1);
   384         //FIXME, Must port to mips code!!
   385         switch (op) {
   386           case 0x1e:  //ddiv
   387           case 0x1f:  //ddivu
   388           case 0x1a:  //div
   389           case 0x1b:  //divu
   390           case 0x34:  //trap
   391             /* In MIPS, div_by_zero exception can only be triggered by explicit 'trap'.
   392              * Ref: [c1_LIRAssembler_mips.cpp] arithmetic_idiv()
   393              */
   394             stub = SharedRuntime::continuation_for_implicit_exception(thread,
   395                                     pc,
   396                                     SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
   397             break;
   398           default:
   399             // TODO: handle more cases if we are using other x86 instructions
   400             //   that can generate SIGFPE signal on linux.
   401             tty->print_cr("unknown opcode 0x%X -0x%X with SIGFPE.", op, op1);
   402             //fatal("please update this code.");
   403         }
   404       }
   405       else if (sig == SIGSEGV &&
   406           !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
   407         // Determination of interpreter/vtable stub/compiled code null exception
   408 #ifdef PRINT_SIGNAL_HANDLE
   409           tty->print("continuation for implicit exception\n");
   410 #endif
   411           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
   412       }
   413       else if (thread->thread_state() == _thread_in_Java && sig == SIGILL) {
   414         //Since kernel does not have emulation of PS instructions yet, the emulation must be handled here.
   415         //The method is to trigger kernel emulation of float emulation.
   416         int inst = *(int*)pc;
   417         int ops = (inst >> 26) & 0x3f;
   418         int ops_fmt = (inst >> 21) & 0x1f;
   419         int op = inst & 0x3f;
   420         if (ops == Assembler::cop1_op && ops_fmt == Assembler::ps_fmt) {
   421           int ft, fs, fd;
   422           ft = (inst >> 16) & 0x1f;
   423           fs = (inst >> 11) & 0x1f;
   424           fd = (inst >> 6) & 0x1f;
   425           float ft_upper, ft_lower, fs_upper, fs_lower, fd_upper, fd_lower;
   426           double ft_value, fs_value, fd_value;
   427           ft_value = uc->uc_mcontext.fpregs.fp_r.fp_dregs[ft];
   428           fs_value = uc->uc_mcontext.fpregs.fp_r.fp_dregs[fs];
   429           __asm__ __volatile__ (
   430             "cvt.s.pl %0, %4\n\t"
   431             "cvt.s.pu %1, %4\n\t"
   432             "cvt.s.pl %2, %5\n\t"
   433             "cvt.s.pu %3, %5\n\t"
   434             : "=f" (fs_lower), "=f" (fs_upper), "=f" (ft_lower), "=f" (ft_upper)
   435             : "f" (fs_value), "f" (ft_value)
   436           );
   438           switch (op) {
   439             case Assembler::fadd_op:
   440               __asm__ __volatile__ (
   441                 "add.s  %1, %3, %5\n\t"
   442                 "add.s  %2, %4, %6\n\t"
   443                 "pll.ps %0, %1, %2\n\t"
   444                 : "=f" (fd_value), "=f" (fd_upper), "=f" (fd_lower)
   445                 : "f" (fs_upper), "f" (fs_lower), "f" (ft_upper), "f" (ft_lower)
   446               );
   447               uc->uc_mcontext.fpregs.fp_r.fp_dregs[fd] = fd_value;
   448               stub = pc + 4;
   449               break;
   450             case Assembler::fsub_op:
   451               //fd = fs - ft
   452               __asm__ __volatile__ (
   453                 "sub.s  %1, %3, %5\n\t"
   454                 "sub.s  %2, %4, %6\n\t"
   455                 "pll.ps %0, %1, %2\n\t"
   456                 : "=f" (fd_value), "=f" (fd_upper), "=f" (fd_lower)
   457                 : "f" (fs_upper), "f" (fs_lower), "f" (ft_upper), "f" (ft_lower)
   458               );
   459               uc->uc_mcontext.fpregs.fp_r.fp_dregs[fd] = fd_value;
   460               stub = pc + 4;
   461               break;
   462             case Assembler::fmul_op:
   463               __asm__ __volatile__ (
   464                 "mul.s  %1, %3, %5\n\t"
   465                 "mul.s  %2, %4, %6\n\t"
   466                 "pll.ps %0, %1, %2\n\t"
   467                 : "=f" (fd_value), "=f" (fd_upper), "=f" (fd_lower)
   468                 : "f" (fs_upper), "f" (fs_lower), "f" (ft_upper), "f" (ft_lower)
   469               );
   470               uc->uc_mcontext.fpregs.fp_r.fp_dregs[fd] = fd_value;
   471               stub = pc + 4;
   472               break;
   473             default:
   474               tty->print_cr("unknown cop1 opcode 0x%x with SIGILL.", op);
   475           }
   476         }
   477         else if (ops == Assembler::cop1x_op /*&& op == Assembler::nmadd_ps_op*/) {
   478           // madd.ps is not used, the code below were not tested
   479           int fr, ft, fs, fd;
   480           float fr_upper, fr_lower, fs_upper, fs_lower, ft_upper, ft_lower, fd_upper, fd_lower;
   481           double fr_value, ft_value, fs_value, fd_value;
   482           switch (op) {
   483             case Assembler::madd_ps_op:
   484               // fd = (fs * ft) + fr
   485               fr = (inst >> 21) & 0x1f;
   486               ft = (inst >> 16) & 0x1f;
   487               fs = (inst >> 11) & 0x1f;
   488               fd = (inst >> 6) & 0x1f;
   489               fr_value = uc->uc_mcontext.fpregs.fp_r.fp_dregs[fr];
   490               ft_value = uc->uc_mcontext.fpregs.fp_r.fp_dregs[ft];
   491               fs_value = uc->uc_mcontext.fpregs.fp_r.fp_dregs[fs];
   492               __asm__ __volatile__ (
   493                 "cvt.s.pu %3, %9\n\t"
   494                 "cvt.s.pl %4, %9\n\t"
   495                 "cvt.s.pu %5, %10\n\t"
   496                 "cvt.s.pl %6, %10\n\t"
   497                 "cvt.s.pu %7, %11\n\t"
   498                 "cvt.s.pl %8, %11\n\t"
   499                 "madd.s %1, %3, %5, %7\n\t"
   500                 "madd.s %2, %4, %6, %8\n\t"
   501                 "pll.ps %0, %1, %2\n\t"
   502                 : "=f" (fd_value), "=f" (fd_upper), "=f" (fd_lower), "=f" (fr_upper), "=f" (fr_lower), "=f" (fs_upper), "=f" (fs_lower), "=f" (ft_upper), "=f" (ft_lower)
   503                 : "f" (fr_value)/*9*/, "f" (fs_value)/*10*/, "f" (ft_value)/*11*/
   504               );
   505               uc->uc_mcontext.fpregs.fp_r.fp_dregs[fd] = fd_value;
   506               stub = pc + 4;
   507               break;
   508             default:
   509               tty->print_cr("unknown cop1x opcode 0x%x with SIGILL.", op);
   510           }
   511         }
   512       } //SIGILL
   513     } else if (thread->thread_state() == _thread_in_vm &&
   514                sig == SIGBUS && /* info->si_code == BUS_OBJERR && */
   515                thread->doing_unsafe_access()) {
   516 #ifdef PRINT_SIGNAL_HANDLE
   517       tty->print_cr("SIGBUS in vm thread \n");
   518 #endif
   519         stub = StubRoutines::handler_for_unsafe_access();
   520     }
   522     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
   523     // and the heap gets shrunk before the field access.
   524     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
   525 #ifdef PRINT_SIGNAL_HANDLE
   526       //tty->print("jni fast get trap: ");
   527 #endif
   528       address addr = JNI_FastGetField::find_slowcase_pc(pc);
   529       if (addr != (address)-1) {
   530         stub = addr;
   531       }
   532 #ifdef PRINT_SIGNAL_HANDLE
   533       //tty->print_cr("addr = %d, stub = %lx", addr, stub);
   534 #endif
   535     }
   537     // Check to see if we caught the safepoint code in the
   538     // process of write protecting the memory serialization page.
   539     // It write enables the page immediately after protecting it
   540     // so we can just return to retry the write.
   541     if ((sig == SIGSEGV) &&
   542         os::is_memory_serialize_page(thread, (address) info->si_addr)) {
   543       // Block current thread until the memory serialize page permission restored.
   544 #ifdef PRINT_SIGNAL_HANDLE
   545       //tty->print("write protecting the memory serialiazation page\n");
   546 #endif
   547       os::block_on_serialize_page_trap();
   548       return true;
   549     }
   550   }
   552   // Execution protection violation
   553   //
   554   // This should be kept as the last step in the triage.  We don't
   555   // have a dedicated trap number for a no-execute fault, so be
   556   // conservative and allow other handlers the first shot.
   557   //
   558   // Note: We don't test that info->si_code == SEGV_ACCERR here.
   559   // this si_code is so generic that it is almost meaningless; and
   560   // the si_code for this condition may change in the future.
   561   // Furthermore, a false-positive should be harmless.
   562   if (UnguardOnExecutionViolation > 0 &&
   563       //(sig == SIGSEGV || sig == SIGBUS) &&
   564       //uc->uc_mcontext.gregs[REG_TRAPNO] == trap_page_fault) {
   565     (sig == SIGSEGV || sig == SIGBUS
   566 #ifdef OPT_RANGECHECK
   567      || sig == SIGSYS
   568 #endif
   569     ) &&
   570       //(uc->uc_mcontext.cause == 2 || uc->uc_mcontext.cause == 3)) {
   571       (uc->uc_mcontext.hi1 == 2 || uc->uc_mcontext.hi1 == 3)) {
   572         //aoqi: copy from jdk1.5, dont understand the struct mcontext_t.
   573 #ifdef PRINT_SIGNAL_HANDLE
   574     tty->print_cr("execution protection violation\n");
   575 #endif
   577     int page_size = os::vm_page_size();
   578     address addr = (address) info->si_addr;
   579     address pc = os::Linux::ucontext_get_pc(uc);
   580     // Make sure the pc and the faulting address are sane.
   581     //
   582     // If an instruction spans a page boundary, and the page containing
   583     // the beginning of the instruction is executable but the following
   584     // page is not, the pc and the faulting address might be slightly
   585     // different - we still want to unguard the 2nd page in this case.
   586     //
   587     // 15 bytes seems to be a (very) safe value for max instruction size.
   588     bool pc_is_near_addr =
   589       (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
   590     bool instr_spans_page_boundary =
   591       (align_size_down((intptr_t) pc ^ (intptr_t) addr,
   592                        (intptr_t) page_size) > 0);
   594     if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
   595       static volatile address last_addr =
   596         (address) os::non_memory_address_word();
   598       // In conservative mode, don't unguard unless the address is in the VM
   599       if (addr != last_addr &&
   600           (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
   602         // Set memory to RWX and retry
   603         address page_start =
   604           (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
   605         bool res = os::protect_memory((char*) page_start, page_size,
   606                                       os::MEM_PROT_RWX);
   608         if (PrintMiscellaneous && Verbose) {
   609           char buf[256];
   610           jio_snprintf(buf, sizeof(buf), "Execution protection violation "
   611                        "at " INTPTR_FORMAT
   612                        ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr,
   613                        page_start, (res ? "success" : "failed"), errno);
   614           tty->print_raw_cr(buf);
   615         }
   616         stub = pc;
   618         // Set last_addr so if we fault again at the same address, we don't end
   619         // up in an endless loop.
   620         //
   621         // There are two potential complications here.  Two threads trapping at
   622         // the same address at the same time could cause one of the threads to
   623         // think it already unguarded, and abort the VM.  Likely very rare.
   624         //
   625         // The other race involves two threads alternately trapping at
   626         // different addresses and failing to unguard the page, resulting in
   627         // an endless loop.  This condition is probably even more unlikely than
   628         // the first.
   629         //
   630         // Although both cases could be avoided by using locks or thread local
   631         // last_addr, these solutions are unnecessary complication: this
   632         // handler is a best-effort safety net, not a complete solution.  It is
   633         // disabled by default and should only be used as a workaround in case
   634         // we missed any no-execute-unsafe VM code.
   636         last_addr = addr;
   637       }
   638     }
   639   }
   641   if (stub != NULL) {
   642 #ifdef PRINT_SIGNAL_HANDLE
   643     //tty->print_cr("resolved stub=%lx\n",stub);
   644 #endif
   645     // save all thread context in case we need to restore it
   646     if (thread != NULL) thread->set_saved_exception_pc(pc);
   648     uc->uc_mcontext.pc = (greg_t)stub;
   649     return true;
   650   }
   652   // signal-chaining
   653   if (os::Linux::chained_handler(sig, info, ucVoid)) {
   654 #ifdef PRINT_SIGNAL_HANDLE
   655      tty->print_cr("signal chaining\n");
   656 #endif
   657      return true;
   658   }
   660   if (!abort_if_unrecognized) {
   661     // caller wants another chance, so give it to him
   662 #ifdef PRINT_SIGNAL_HANDLE
   663     tty->print_cr("abort becauce of unrecognized\n");
   664 #endif
   665     return false;
   666   }
   668   if (pc == NULL && uc != NULL) {
   669     pc = os::Linux::ucontext_get_pc(uc);
   670   }
   672   // unmask current signal
   673   sigset_t newset;
   674   sigemptyset(&newset);
   675   sigaddset(&newset, sig);
   676   sigprocmask(SIG_UNBLOCK, &newset, NULL);
   677 #ifdef PRINT_SIGNAL_HANDLE
   678   tty->print_cr("VMError in signal handler\n");
   679 #endif
   680   VMError err(t, sig, pc, info, ucVoid);
   681   err.report_and_die();
   683   ShouldNotReachHere();
   684 }
   686 // FCSR:...|24| 23 |22|21|...
   687 //      ...|FS|FCC0|FO|FN|...
   688 void os::Linux::init_thread_fpu_state(void) {
   689   if (SetFSFOFN == 999)
   690     return;
   691   int fs = (SetFSFOFN / 100)? 1:0;
   692   int fo = ((SetFSFOFN % 100) / 10)? 1:0;
   693   int fn = (SetFSFOFN % 10)? 1:0;
   694   int mask = fs << 24 | fo << 22 | fn << 21;
   696   int fcsr = get_fpu_control_word();
   697   fcsr = fcsr | mask;
   698   set_fpu_control_word(fcsr);
   699   /*
   700   if (fcsr != get_fpu_control_word())
   701     tty->print_cr(" fail to set to %lx, get_fpu_control_word:%lx", fcsr, get_fpu_control_word());
   702   */
   703 }
   705 int os::Linux::get_fpu_control_word(void) {
   706   int fcsr;
   707   __asm__ __volatile__ (
   708       ".set noat;"
   709       "daddiu  %0, $0, 0;"
   710       "cfc1 %0, $31;"
   711       : "=r" (fcsr)
   712       );
   713   return fcsr;
   714 }
   716 void os::Linux::set_fpu_control_word(int fpu_control) {
   717   __asm__ __volatile__ (
   718       ".set noat;"
   719       "ctc1 %0, $31;"
   720       :
   721       : "r" (fpu_control)
   722       );
   723 }
   725 bool os::is_allocatable(size_t bytes) {
   727   if (bytes < 2 * G) {
   728     return true;
   729   }
   731   char* addr = reserve_memory(bytes, NULL);
   733   if (addr != NULL) {
   734     release_memory(addr, bytes);
   735   }
   737   return addr != NULL;
   738 }
   740 ////////////////////////////////////////////////////////////////////////////////
   741 // thread stack
   743 size_t os::Linux::min_stack_allowed  = 96 * K;
   746 // Test if pthread library can support variable thread stack size. LinuxThreads
   747 // in fixed stack mode allocates 2M fixed slot for each thread. LinuxThreads
   748 // in floating stack mode and NPTL support variable stack size.
   749 bool os::Linux::supports_variable_stack_size() {
   750   if (os::Linux::is_NPTL()) {
   751      // NPTL, yes
   752      return true;
   754   } else {
   755     // Note: We can't control default stack size when creating a thread.
   756     // If we use non-default stack size (pthread_attr_setstacksize), both
   757     // floating stack and non-floating stack LinuxThreads will return the
   758     // same value. This makes it impossible to implement this function by
   759     // detecting thread stack size directly.
   760     //
   761     // An alternative approach is to check %gs. Fixed-stack LinuxThreads
   762     // do not use %gs, so its value is 0. Floating-stack LinuxThreads use
   763     // %gs (either as LDT selector or GDT selector, depending on kernel)
   764     // to access thread specific data.
   765     //
   766     // Note that %gs is a reserved glibc register since early 2001, so
   767     // applications are not allowed to change its value (Ulrich Drepper from
   768     // Redhat confirmed that all known offenders have been modified to use
   769     // either %fs or TSD). In the worst case scenario, when VM is embedded in
   770     // a native application that plays with %gs, we might see non-zero %gs
   771     // even LinuxThreads is running in fixed stack mode. As the result, we'll
   772     // return true and skip _thread_safety_check(), so we may not be able to
   773     // detect stack-heap collisions. But otherwise it's harmless.
   774     //
   775     return false;
   776   }
   777 }
   779 // return default stack size for thr_type
   780 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
   781   // default stack size (compiler thread needs larger stack)
   782   size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
   783   return s;
   784 }
   786 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
   787   // Creating guard page is very expensive. Java thread has HotSpot
   788   // guard page, only enable glibc guard page for non-Java threads.
   789   return (thr_type == java_thread ? 0 : page_size());
   790 }
   792 // Java thread:
   793 //
   794 //   Low memory addresses
   795 //    +------------------------+
   796 //    |                        |\  JavaThread created by VM does not have glibc
   797 //    |    glibc guard page    | - guard, attached Java thread usually has
   798 //    |                        |/  1 page glibc guard.
   799 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
   800 //    |                        |\
   801 //    |  HotSpot Guard Pages   | - red and yellow pages
   802 //    |                        |/
   803 //    +------------------------+ JavaThread::stack_yellow_zone_base()
   804 //    |                        |\
   805 //    |      Normal Stack      | -
   806 //    |                        |/
   807 // P2 +------------------------+ Thread::stack_base()
   808 //
   809 // Non-Java thread:
   810 //
   811 //   Low memory addresses
   812 //    +------------------------+
   813 //    |                        |\
   814 //    |  glibc guard page      | - usually 1 page
   815 //    |                        |/
   816 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
   817 //    |                        |\
   818 //    |      Normal Stack      | -
   819 //    |                        |/
   820 // P2 +------------------------+ Thread::stack_base()
   821 //
   822 // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from
   823 //    pthread_attr_getstack()
   825 static void current_stack_region(address * bottom, size_t * size) {
   826   if (os::Linux::is_initial_thread()) {
   827      // initial thread needs special handling because pthread_getattr_np()
   828      // may return bogus value.
   829      *bottom = os::Linux::initial_thread_stack_bottom();
   830      *size   = os::Linux::initial_thread_stack_size();
   831   } else {
   832      pthread_attr_t attr;
   834      int rslt = pthread_getattr_np(pthread_self(), &attr);
   836      // JVM needs to know exact stack location, abort if it fails
   837      if (rslt != 0) {
   838        if (rslt == ENOMEM) {
   839          vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");
   840        } else {
   841          fatal(err_msg("pthread_getattr_np failed with errno = %d", rslt));
   842        }
   843      }
   845      if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) {
   846          fatal("Can not locate current stack attributes!");
   847      }
   849      pthread_attr_destroy(&attr);
   851   }
   852   assert(os::current_stack_pointer() >= *bottom &&
   853          os::current_stack_pointer() < *bottom + *size, "just checking");
   854 }
   856 address os::current_stack_base() {
   857   address bottom;
   858   size_t size;
   859   current_stack_region(&bottom, &size);
   860   return (bottom + size);
   861 }
   863 size_t os::current_stack_size() {
   864   // stack size includes normal stack and HotSpot guard pages
   865   address bottom;
   866   size_t size;
   867   current_stack_region(&bottom, &size);
   868   return size;
   869 }
   871 /////////////////////////////////////////////////////////////////////////////
   872 // helper functions for fatal error handler
   873 void os::print_register_info(outputStream *st, void *context) {
   874   if (context == NULL) return;
   876   ucontext_t *uc = (ucontext_t*)context;
   878   st->print_cr("Register to memory mapping:");
   879   st->cr();
   880   // this is horrendously verbose but the layout of the registers in the
   881   //   // context does not match how we defined our abstract Register set, so
   882   //     // we can't just iterate through the gregs area
   883   //
   884   //       // this is only for the "general purpose" registers
   885   st->print("R0=" ); print_location(st, uc->uc_mcontext.gregs[0]);
   886   st->print("AT=" ); print_location(st, uc->uc_mcontext.gregs[1]);
   887   st->print("V0=" ); print_location(st, uc->uc_mcontext.gregs[2]);
   888   st->print("V1=" ); print_location(st, uc->uc_mcontext.gregs[3]);
   889   st->cr();
   890   st->print("A0=" ); print_location(st, uc->uc_mcontext.gregs[4]);
   891   st->print("A1=" ); print_location(st, uc->uc_mcontext.gregs[5]);
   892   st->print("A2=" ); print_location(st, uc->uc_mcontext.gregs[6]);
   893   st->print("A3=" ); print_location(st, uc->uc_mcontext.gregs[7]);
   894   st->cr();
   895   st->print("A4=" ); print_location(st, uc->uc_mcontext.gregs[8]);
   896   st->print("A5=" ); print_location(st, uc->uc_mcontext.gregs[9]);
   897   st->print("A6=" ); print_location(st, uc->uc_mcontext.gregs[10]);
   898   st->print("A7=" ); print_location(st, uc->uc_mcontext.gregs[11]);
   899   st->cr();
   900   st->print("T0=" ); print_location(st, uc->uc_mcontext.gregs[12]);
   901   st->print("T1=" ); print_location(st, uc->uc_mcontext.gregs[13]);
   902   st->print("T2=" ); print_location(st, uc->uc_mcontext.gregs[14]);
   903   st->print("T3=" ); print_location(st, uc->uc_mcontext.gregs[15]);
   904   st->cr();
   905   st->print("S0=" ); print_location(st, uc->uc_mcontext.gregs[16]);
   906   st->print("S1=" ); print_location(st, uc->uc_mcontext.gregs[17]);
   907   st->print("S2=" ); print_location(st, uc->uc_mcontext.gregs[18]);
   908   st->print("S3=" ); print_location(st, uc->uc_mcontext.gregs[19]);
   909   st->cr();
   910   st->print("S4=" ); print_location(st, uc->uc_mcontext.gregs[20]);
   911   st->print("S5=" ); print_location(st, uc->uc_mcontext.gregs[21]);
   912   st->print("S6=" ); print_location(st, uc->uc_mcontext.gregs[22]);
   913   st->print("S7=" ); print_location(st, uc->uc_mcontext.gregs[23]);
   914   st->cr();
   915   st->print("T8=" ); print_location(st, uc->uc_mcontext.gregs[24]);
   916   st->print("T9=" ); print_location(st, uc->uc_mcontext.gregs[25]);
   917   st->print("K0=" ); print_location(st, uc->uc_mcontext.gregs[26]);
   918   st->print("K1=" ); print_location(st, uc->uc_mcontext.gregs[27]);
   919   st->cr();
   920   st->print("GP=" ); print_location(st, uc->uc_mcontext.gregs[28]);
   921   st->print("SP=" ); print_location(st, uc->uc_mcontext.gregs[29]);
   922   st->print("FP=" ); print_location(st, uc->uc_mcontext.gregs[30]);
   923   st->print("RA=" ); print_location(st, uc->uc_mcontext.gregs[31]);
   924   st->cr();
   926 }
   927 void os::print_context(outputStream *st, void *context) {
   928   if (context == NULL) return;
   930   ucontext_t *uc = (ucontext_t*)context;
   931   st->print_cr("Registers:");
   932   st->print(  "R0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[0]);
   933   st->print(", AT=" INTPTR_FORMAT, uc->uc_mcontext.gregs[1]);
   934   st->print(", V0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[2]);
   935   st->print(", V1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[3]);
   936   st->cr();
   937   st->print(  "A0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[4]);
   938   st->print(", A1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[5]);
   939   st->print(", A2=" INTPTR_FORMAT, uc->uc_mcontext.gregs[6]);
   940   st->print(", A3=" INTPTR_FORMAT, uc->uc_mcontext.gregs[7]);
   941   st->cr();
   942   st->print(  "A4=" INTPTR_FORMAT, uc->uc_mcontext.gregs[8]);
   943   st->print(", A5=" INTPTR_FORMAT, uc->uc_mcontext.gregs[9]);
   944   st->print(", A6=" INTPTR_FORMAT, uc->uc_mcontext.gregs[10]);
   945   st->print(", A7=" INTPTR_FORMAT, uc->uc_mcontext.gregs[11]);
   946   st->cr();
   947   st->print(  "T0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[12]);
   948   st->print(", T1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[13]);
   949   st->print(", T2=" INTPTR_FORMAT, uc->uc_mcontext.gregs[14]);
   950   st->print(", T3=" INTPTR_FORMAT, uc->uc_mcontext.gregs[15]);
   951   st->cr();
   952   st->print(  "S0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[16]);
   953   st->print(", S1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[17]);
   954   st->print(", S2=" INTPTR_FORMAT, uc->uc_mcontext.gregs[18]);
   955   st->print(", S3=" INTPTR_FORMAT, uc->uc_mcontext.gregs[19]);
   956   st->cr();
   957   st->print(  "S4=" INTPTR_FORMAT, uc->uc_mcontext.gregs[20]);
   958   st->print(", S5=" INTPTR_FORMAT, uc->uc_mcontext.gregs[21]);
   959   st->print(", S6=" INTPTR_FORMAT, uc->uc_mcontext.gregs[22]);
   960   st->print(", S7=" INTPTR_FORMAT, uc->uc_mcontext.gregs[23]);
   961   st->cr();
   962   st->print(  "T8=" INTPTR_FORMAT, uc->uc_mcontext.gregs[24]);
   963   st->print(", T9=" INTPTR_FORMAT, uc->uc_mcontext.gregs[25]);
   964   st->print(", K0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[26]);
   965   st->print(", K1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[27]);
   966   st->cr();
   967   st->print(  "GP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[28]);
   968   st->print(", SP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[29]);
   969   st->print(", FP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[30]);
   970   st->print(", RA=" INTPTR_FORMAT, uc->uc_mcontext.gregs[31]);
   971   st->cr();
   972   st->cr();
   974   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
   975   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
   976   //print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
   977   print_hex_dump(st, (address)sp-32, (address)(sp + 32), sizeof(intptr_t));
   978   st->cr();
   980   // Note: it may be unsafe to inspect memory near pc. For example, pc may
   981   // point to garbage if entry point in an nmethod is corrupted. Leave
   982   // this at the end, and hope for the best.
   983   address pc = os::Linux::ucontext_get_pc(uc);
   984   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
   985   print_hex_dump(st, pc - 64, pc + 64, sizeof(char));
   986   Disassembler::decode(pc - 80, pc + 80, st);
   987 }
   989 void os::setup_fpu() {
   990   /*
   991   //no use for MIPS
   992   int fcsr;
   993   address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std();
   994   __asm__ __volatile__ (
   995       ".set noat;"
   996       "cfc1 %0, $31;"
   997       "sw   %0, 0(%1);"
   998       : "=r" (fcsr)
   999       : "r" (fpu_cntrl)
  1000       : "memory"
  1001   );
  1002   printf("fpu_cntrl:  %lx\n", fpu_cntrl);
  1003   */
  1006 #ifndef PRODUCT
  1007 void os::verify_stack_alignment() {
  1008   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
  1010 #endif

mercurial