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

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

mercurial