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