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