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