os_linux_sparc.cpp revision 1410:f03d0a26bf83
1/*
2 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25// do not include  precompiled  header file
26
27#include "incls/_os_linux_sparc.cpp.incl"
28
29// Linux/Sparc has rather obscure naming of registers in sigcontext
30// different between 32 and 64 bits
31#ifdef _LP64
32#define SIG_PC(x) ((x)->sigc_regs.tpc)
33#define SIG_NPC(x) ((x)->sigc_regs.tnpc)
34#define SIG_REGS(x) ((x)->sigc_regs)
35#else
36#define SIG_PC(x) ((x)->si_regs.pc)
37#define SIG_NPC(x) ((x)->si_regs.npc)
38#define SIG_REGS(x) ((x)->si_regs)
39#endif
40
41// those are to reference registers in sigcontext
42enum {
43  CON_G0 = 0,
44  CON_G1,
45  CON_G2,
46  CON_G3,
47  CON_G4,
48  CON_G5,
49  CON_G6,
50  CON_G7,
51  CON_O0,
52  CON_O1,
53  CON_O2,
54  CON_O3,
55  CON_O4,
56  CON_O5,
57  CON_O6,
58  CON_O7,
59};
60
61static inline void set_cont_address(sigcontext* ctx, address addr) {
62  SIG_PC(ctx)  = (intptr_t)addr;
63  SIG_NPC(ctx) = (intptr_t)(addr+4);
64}
65
66// For Forte Analyzer AsyncGetCallTrace profiling support - thread is
67// currently interrupted by SIGPROF.
68// os::Solaris::fetch_frame_from_ucontext() tries to skip nested
69// signal frames. Currently we don't do that on Linux, so it's the
70// same as os::fetch_frame_from_context().
71ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread,
72                                                ucontext_t* uc,
73                                                intptr_t** ret_sp,
74                                                intptr_t** ret_fp) {
75  assert(thread != NULL, "just checking");
76  assert(ret_sp != NULL, "just checking");
77  assert(ret_fp != NULL, "just checking");
78
79  return os::fetch_frame_from_context(uc, ret_sp, ret_fp);
80}
81
82ExtendedPC os::fetch_frame_from_context(void* ucVoid,
83                                        intptr_t** ret_sp,
84                                        intptr_t** ret_fp) {
85  ucontext_t* uc = (ucontext_t*) ucVoid;
86  ExtendedPC  epc;
87
88  if (uc != NULL) {
89    epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
90    if (ret_sp) {
91      *ret_sp = os::Linux::ucontext_get_sp(uc);
92    }
93    if (ret_fp) {
94      *ret_fp = os::Linux::ucontext_get_fp(uc);
95    }
96  } else {
97    // construct empty ExtendedPC for return value checking
98    epc = ExtendedPC(NULL);
99    if (ret_sp) {
100      *ret_sp = (intptr_t*) NULL;
101    }
102    if (ret_fp) {
103      *ret_fp = (intptr_t*) NULL;
104    }
105  }
106
107  return epc;
108}
109
110frame os::fetch_frame_from_context(void* ucVoid) {
111  intptr_t* sp;
112  intptr_t* fp;
113  ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
114  return frame(sp, fp, epc.pc());
115}
116
117frame os::get_sender_for_C_frame(frame* fr) {
118  return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
119}
120
121frame os::current_frame() {
122  fprintf(stderr, "current_frame()");
123
124  intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
125  frame myframe(sp, frame::unpatchable,
126                CAST_FROM_FN_PTR(address, os::current_frame));
127  if (os::is_first_C_frame(&myframe)) {
128    // stack is not walkable
129    return frame(NULL, frame::unpatchable, NULL);
130  } else {
131    return os::get_sender_for_C_frame(&myframe);
132  }
133}
134
135address os::current_stack_pointer() {
136  register void *sp __asm__ ("sp");
137  return (address)sp;
138}
139
140static void current_stack_region(address* bottom, size_t* size) {
141  if (os::Linux::is_initial_thread()) {
142    // initial thread needs special handling because pthread_getattr_np()
143    // may return bogus value.
144    *bottom = os::Linux::initial_thread_stack_bottom();
145    *size = os::Linux::initial_thread_stack_size();
146  } else {
147    pthread_attr_t attr;
148
149    int rslt = pthread_getattr_np(pthread_self(), &attr);
150
151    // JVM needs to know exact stack location, abort if it fails
152    if (rslt != 0) {
153      if (rslt == ENOMEM) {
154        vm_exit_out_of_memory(0, "pthread_getattr_np");
155      } else {
156        fatal(err_msg("pthread_getattr_np failed with errno = %d", rslt));
157      }
158    }
159
160    if (pthread_attr_getstack(&attr, (void**)bottom, size) != 0) {
161      fatal("Can not locate current stack attributes!");
162    }
163
164    pthread_attr_destroy(&attr);
165  }
166  assert(os::current_stack_pointer() >= *bottom &&
167         os::current_stack_pointer() < *bottom + *size, "just checking");
168}
169
170address os::current_stack_base() {
171  address bottom;
172  size_t size;
173  current_stack_region(&bottom, &size);
174  return bottom + size;
175}
176
177size_t os::current_stack_size() {
178  // stack size includes normal stack and HotSpot guard pages
179  address bottom;
180  size_t size;
181  current_stack_region(&bottom, &size);
182  return size;
183}
184
185char* os::non_memory_address_word() {
186  // Must never look like an address returned by reserve_memory,
187  // even in its subfields (as defined by the CPU immediate fields,
188  // if the CPU splits constants across multiple instructions).
189  // On SPARC, 0 != %hi(any real address), because there is no
190  // allocation in the first 1Kb of the virtual address space.
191  return (char*) 0;
192}
193
194void os::initialize_thread() {}
195
196void os::print_context(outputStream *st, void *context) {
197  if (context == NULL) return;
198
199  ucontext_t* uc = (ucontext_t*)context;
200  sigcontext* sc = (sigcontext*)context;
201  st->print_cr("Registers:");
202
203  st->print_cr(" O0=" INTPTR_FORMAT " O1=" INTPTR_FORMAT
204               " O2=" INTPTR_FORMAT " O3=" INTPTR_FORMAT,
205               SIG_REGS(sc).u_regs[CON_O0],
206               SIG_REGS(sc).u_regs[CON_O1],
207               SIG_REGS(sc).u_regs[CON_O2],
208               SIG_REGS(sc).u_regs[CON_O3]);
209  st->print_cr(" O4=" INTPTR_FORMAT " O5=" INTPTR_FORMAT
210               " O6=" INTPTR_FORMAT " O7=" INTPTR_FORMAT,
211               SIG_REGS(sc).u_regs[CON_O4],
212               SIG_REGS(sc).u_regs[CON_O5],
213               SIG_REGS(sc).u_regs[CON_O6],
214               SIG_REGS(sc).u_regs[CON_O7]);
215
216  st->print_cr(" G1=" INTPTR_FORMAT " G2=" INTPTR_FORMAT
217               " G3=" INTPTR_FORMAT " G4=" INTPTR_FORMAT,
218               SIG_REGS(sc).u_regs[CON_G1],
219               SIG_REGS(sc).u_regs[CON_G2],
220               SIG_REGS(sc).u_regs[CON_G3],
221               SIG_REGS(sc).u_regs[CON_G4]);
222  st->print_cr(" G5=" INTPTR_FORMAT " G6=" INTPTR_FORMAT
223               " G7=" INTPTR_FORMAT " Y=" INTPTR_FORMAT,
224               SIG_REGS(sc).u_regs[CON_G5],
225               SIG_REGS(sc).u_regs[CON_G6],
226               SIG_REGS(sc).u_regs[CON_G7],
227               SIG_REGS(sc).y);
228
229  st->print_cr(" PC=" INTPTR_FORMAT " nPC=" INTPTR_FORMAT,
230               SIG_PC(sc),
231               SIG_NPC(sc));
232  st->cr();
233  st->cr();
234
235  intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
236  st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
237  print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t));
238  st->cr();
239
240  // Note: it may be unsafe to inspect memory near pc. For example, pc may
241  // point to garbage if entry point in an nmethod is corrupted. Leave
242  // this at the end, and hope for the best.
243  address pc = os::Linux::ucontext_get_pc(uc);
244  st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
245  print_hex_dump(st, pc - 16, pc + 16, sizeof(char));
246}
247
248
249address os::Linux::ucontext_get_pc(ucontext_t* uc) {
250  return (address) SIG_PC((sigcontext*)uc);
251}
252
253intptr_t* os::Linux::ucontext_get_sp(ucontext_t *uc) {
254  return (intptr_t*)
255    ((intptr_t)SIG_REGS((sigcontext*)uc).u_regs[CON_O6] + STACK_BIAS);
256}
257
258// not used on Sparc
259intptr_t* os::Linux::ucontext_get_fp(ucontext_t *uc) {
260  ShouldNotReachHere();
261  return NULL;
262}
263
264// Utility functions
265
266extern "C" void Fetch32PFI();
267extern "C" void Fetch32Resume();
268extern "C" void FetchNPFI();
269extern "C" void FetchNResume();
270
271inline static bool checkPrefetch(sigcontext* uc, address pc) {
272  if (pc == (address) Fetch32PFI) {
273    set_cont_address(uc, address(Fetch32Resume));
274    return true;
275  }
276  if (pc == (address) FetchNPFI) {
277    set_cont_address(uc, address(FetchNResume));
278    return true;
279  }
280  return false;
281}
282
283inline static bool checkOverflow(sigcontext* uc,
284                                 address pc,
285                                 address addr,
286                                 JavaThread* thread,
287                                 address* stub) {
288  // check if fault address is within thread stack
289  if (addr < thread->stack_base() &&
290      addr >= thread->stack_base() - thread->stack_size()) {
291    // stack overflow
292    if (thread->in_stack_yellow_zone(addr)) {
293      thread->disable_stack_yellow_zone();
294      if (thread->thread_state() == _thread_in_Java) {
295        // Throw a stack overflow exception.  Guard pages will be reenabled
296        // while unwinding the stack.
297        *stub =
298          SharedRuntime::continuation_for_implicit_exception(thread,
299                                                             pc,
300                                                             SharedRuntime::STACK_OVERFLOW);
301      } else {
302        // Thread was in the vm or native code.  Return and try to finish.
303        return true;
304      }
305    } else if (thread->in_stack_red_zone(addr)) {
306      // Fatal red zone violation.  Disable the guard pages and fall through
307      // to handle_unexpected_exception way down below.
308      thread->disable_stack_red_zone();
309      tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
310    } else {
311      // Accessing stack address below sp may cause SEGV if current
312      // thread has MAP_GROWSDOWN stack. This should only happen when
313      // current thread was created by user code with MAP_GROWSDOWN flag
314      // and then attached to VM. See notes in os_linux.cpp.
315      if (thread->osthread()->expanding_stack() == 0) {
316        thread->osthread()->set_expanding_stack();
317        if (os::Linux::manually_expand_stack(thread, addr)) {
318          thread->osthread()->clear_expanding_stack();
319          return true;
320        }
321        thread->osthread()->clear_expanding_stack();
322      } else {
323        fatal("recursive segv. expanding stack.");
324      }
325    }
326  }
327  return false;
328}
329
330inline static bool checkPollingPage(address pc, address fault, address* stub) {
331  if (fault == os::get_polling_page()) {
332    *stub = SharedRuntime::get_poll_stub(pc);
333    return true;
334  }
335  return false;
336}
337
338inline static bool checkByteBuffer(address pc, address* stub) {
339  // BugId 4454115: A read from a MappedByteBuffer can fault
340  // here if the underlying file has been truncated.
341  // Do not crash the VM in such a case.
342  CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
343  nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL;
344  if (nm != NULL && nm->has_unsafe_access()) {
345    *stub = StubRoutines::handler_for_unsafe_access();
346    return true;
347  }
348  return false;
349}
350
351inline static bool checkVerifyOops(address pc, address fault, address* stub) {
352  if (pc >= MacroAssembler::_verify_oop_implicit_branch[0]
353      && pc <  MacroAssembler::_verify_oop_implicit_branch[1] ) {
354    *stub     =  MacroAssembler::_verify_oop_implicit_branch[2];
355    warning("fixed up memory fault in +VerifyOops at address "
356            INTPTR_FORMAT, fault);
357    return true;
358  }
359  return false;
360}
361
362inline static bool checkFPFault(address pc, int code,
363                                JavaThread* thread, address* stub) {
364  if (code == FPE_INTDIV || code == FPE_FLTDIV) {
365    *stub =
366      SharedRuntime::
367      continuation_for_implicit_exception(thread,
368                                          pc,
369                                          SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
370    return true;
371  }
372  return false;
373}
374
375inline static bool checkNullPointer(address pc, intptr_t fault,
376                                    JavaThread* thread, address* stub) {
377  if (!MacroAssembler::needs_explicit_null_check(fault)) {
378    // Determination of interpreter/vtable stub/compiled code null
379    // exception
380    *stub =
381      SharedRuntime::
382      continuation_for_implicit_exception(thread, pc,
383                                          SharedRuntime::IMPLICIT_NULL);
384    return true;
385  }
386  return false;
387}
388
389inline static bool checkFastJNIAccess(address pc, address* stub) {
390  address addr = JNI_FastGetField::find_slowcase_pc(pc);
391  if (addr != (address)-1) {
392    *stub = addr;
393    return true;
394  }
395  return false;
396}
397
398inline static bool checkSerializePage(JavaThread* thread, address addr) {
399  return os::is_memory_serialize_page(thread, addr);
400}
401
402inline static bool checkZombie(sigcontext* uc, address* pc, address* stub) {
403  if (nativeInstruction_at(*pc)->is_zombie()) {
404    // zombie method (ld [%g0],%o7 instruction)
405    *stub = SharedRuntime::get_handle_wrong_method_stub();
406
407    // At the stub it needs to look like a call from the caller of this
408    // method (not a call from the segv site).
409    *pc = (address)SIG_REGS(uc).u_regs[CON_O7];
410    return true;
411  }
412  return false;
413}
414
415inline static bool checkICMiss(sigcontext* uc, address* pc, address* stub) {
416#ifdef COMPILER2
417  if (nativeInstruction_at(*pc)->is_ic_miss_trap()) {
418#ifdef ASSERT
419#ifdef TIERED
420    CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
421    assert(cb->is_compiled_by_c2(), "Wrong compiler");
422#endif // TIERED
423#endif // ASSERT
424    // Inline cache missed and user trap "Tne G0+ST_RESERVED_FOR_USER_0+2" taken.
425    *stub = SharedRuntime::get_ic_miss_stub();
426    // At the stub it needs to look like a call from the caller of this
427    // method (not a call from the segv site).
428    *pc = (address)SIG_REGS(uc).u_regs[CON_O7];
429    return true;
430  }
431#endif  // COMPILER2
432  return false;
433}
434
435extern "C" int
436JVM_handle_linux_signal(int sig,
437                        siginfo_t* info,
438                        void* ucVoid,
439                        int abort_if_unrecognized) {
440  // in fact this isn't ucontext_t* at all, but struct sigcontext*
441  // but Linux porting layer uses ucontext_t, so to minimize code change
442  // we cast as needed
443  ucontext_t* ucFake = (ucontext_t*) ucVoid;
444  sigcontext* uc = (sigcontext*)ucVoid;
445
446  Thread* t = ThreadLocalStorage::get_thread_slow();
447
448  SignalHandlerMark shm(t);
449
450  // Note: it's not uncommon that JNI code uses signal/sigset to install
451  // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
452  // or have a SIGILL handler when detecting CPU type). When that happens,
453  // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
454  // avoid unnecessary crash when libjsig is not preloaded, try handle signals
455  // that do not require siginfo/ucontext first.
456
457  if (sig == SIGPIPE || sig == SIGXFSZ) {
458    // allow chained handler to go first
459    if (os::Linux::chained_handler(sig, info, ucVoid)) {
460      return true;
461    } else {
462      if (PrintMiscellaneous && (WizardMode || Verbose)) {
463        char buf[64];
464        warning("Ignoring %s - see bugs 4229104 or 646499219",
465                os::exception_name(sig, buf, sizeof(buf)));
466      }
467      return true;
468    }
469  }
470
471  JavaThread* thread = NULL;
472  VMThread* vmthread = NULL;
473  if (os::Linux::signal_handlers_are_installed) {
474    if (t != NULL ){
475      if(t->is_Java_thread()) {
476        thread = (JavaThread*)t;
477      }
478      else if(t->is_VM_thread()){
479        vmthread = (VMThread *)t;
480      }
481    }
482  }
483
484  // decide if this trap can be handled by a stub
485  address stub = NULL;
486  address pc = NULL;
487  address npc = NULL;
488
489  //%note os_trap_1
490  if (info != NULL && uc != NULL && thread != NULL) {
491    pc = address(SIG_PC(uc));
492    npc = address(SIG_NPC(uc));
493
494    // Check to see if we caught the safepoint code in the
495    // process of write protecting the memory serialization page.
496    // It write enables the page immediately after protecting it
497    // so we can just return to retry the write.
498    if ((sig == SIGSEGV) && checkSerializePage(thread, (address)info->si_addr)) {
499      // Block current thread until the memory serialize page permission restored.
500      os::block_on_serialize_page_trap();
501      return 1;
502    }
503
504    if (checkPrefetch(uc, pc)) {
505      return 1;
506    }
507
508    // Handle ALL stack overflow variations here
509    if (sig == SIGSEGV) {
510      if (checkOverflow(uc, pc, (address)info->si_addr, thread, &stub)) {
511        return 1;
512      }
513    }
514
515    if (sig == SIGBUS &&
516        thread->thread_state() == _thread_in_vm &&
517        thread->doing_unsafe_access()) {
518      stub = StubRoutines::handler_for_unsafe_access();
519    }
520
521    if (thread->thread_state() == _thread_in_Java) {
522      do {
523        // Java thread running in Java code => find exception handler if any
524        // a fault inside compiled code, the interpreter, or a stub
525
526        if ((sig == SIGSEGV) && checkPollingPage(pc, (address)info->si_addr, &stub)) {
527          break;
528        }
529
530        if ((sig == SIGBUS) && checkByteBuffer(pc, &stub)) {
531          break;
532        }
533
534        if ((sig == SIGSEGV || sig == SIGBUS) &&
535            checkVerifyOops(pc, (address)info->si_addr, &stub)) {
536          break;
537        }
538
539        if ((sig == SIGSEGV) && checkZombie(uc, &pc, &stub)) {
540          break;
541        }
542
543        if ((sig == SIGILL) && checkICMiss(uc, &pc, &stub)) {
544          break;
545        }
546
547        if ((sig == SIGFPE) && checkFPFault(pc, info->si_code, thread, &stub)) {
548          break;
549        }
550
551        if ((sig == SIGSEGV) &&
552            checkNullPointer(pc, (intptr_t)info->si_addr, thread, &stub)) {
553          break;
554        }
555      } while (0);
556
557      // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
558      // and the heap gets shrunk before the field access.
559      if ((sig == SIGSEGV) || (sig == SIGBUS)) {
560        checkFastJNIAccess(pc, &stub);
561      }
562    }
563
564    if (stub != NULL) {
565      // save all thread context in case we need to restore it
566      thread->set_saved_exception_pc(pc);
567      thread->set_saved_exception_npc(npc);
568      set_cont_address(uc, stub);
569      return true;
570    }
571  }
572
573  // signal-chaining
574  if (os::Linux::chained_handler(sig, info, ucVoid)) {
575    return true;
576  }
577
578  if (!abort_if_unrecognized) {
579    // caller wants another chance, so give it to him
580    return false;
581  }
582
583  if (pc == NULL && uc != NULL) {
584    pc = os::Linux::ucontext_get_pc((ucontext_t*)uc);
585  }
586
587  // unmask current signal
588  sigset_t newset;
589  sigemptyset(&newset);
590  sigaddset(&newset, sig);
591  sigprocmask(SIG_UNBLOCK, &newset, NULL);
592
593  VMError err(t, sig, pc, info, ucVoid);
594  err.report_and_die();
595
596  ShouldNotReachHere();
597}
598
599void os::Linux::init_thread_fpu_state(void) {
600  // Nothing to do
601}
602
603int os::Linux::get_fpu_control_word() {
604  return 0;
605}
606
607void os::Linux::set_fpu_control_word(int fpu) {
608  // nothing
609}
610
611bool os::is_allocatable(size_t bytes) {
612#ifdef _LP64
613  return true;
614#else
615  if (bytes < 2 * G) {
616    return true;
617  }
618
619  char* addr = reserve_memory(bytes, NULL);
620
621  if (addr != NULL) {
622    release_memory(addr, bytes);
623  }
624
625  return addr != NULL;
626#endif // _LP64
627}
628
629///////////////////////////////////////////////////////////////////////////////
630// thread stack
631
632size_t os::Linux::min_stack_allowed  = 128 * K;
633
634// pthread on Ubuntu is always in floating stack mode
635bool os::Linux::supports_variable_stack_size() {  return true; }
636
637// return default stack size for thr_type
638size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
639  // default stack size (compiler thread needs larger stack)
640  size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
641  return s;
642}
643
644size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
645  // Creating guard page is very expensive. Java thread has HotSpot
646  // guard page, only enable glibc guard page for non-Java threads.
647  return (thr_type == java_thread ? 0 : page_size());
648}
649