os_solaris_x86.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1999-2007 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# include "incls/_os_solaris_x86.cpp.incl"
27
28// put OS-includes here
29# include <sys/types.h>
30# include <sys/mman.h>
31# include <pthread.h>
32# include <signal.h>
33# include <setjmp.h>
34# include <errno.h>
35# include <dlfcn.h>
36# include <stdio.h>
37# include <unistd.h>
38# include <sys/resource.h>
39# include <thread.h>
40# include <sys/stat.h>
41# include <sys/time.h>
42# include <sys/filio.h>
43# include <sys/utsname.h>
44# include <sys/systeminfo.h>
45# include <sys/socket.h>
46# include <sys/trap.h>
47# include <sys/lwp.h>
48# include <pwd.h>
49# include <poll.h>
50# include <sys/lwp.h>
51# include <procfs.h>     //  see comment in <sys/procfs.h>
52
53#ifndef AMD64
54// QQQ seems useless at this point
55# define _STRUCTURED_PROC 1  //  this gets us the new structured proc interfaces of 5.6 & later
56#endif // AMD64
57# include <sys/procfs.h>     //  see comment in <sys/procfs.h>
58
59
60#define MAX_PATH (2 * K)
61
62// Minimum stack size for the VM.  It's easier to document a constant value
63// but it's different for x86 and sparc because the page sizes are different.
64#ifdef AMD64
65size_t os::Solaris::min_stack_allowed = 224*K;
66#define REG_SP REG_RSP
67#define REG_PC REG_RIP
68#define REG_FP REG_RBP
69#else
70size_t os::Solaris::min_stack_allowed = 64*K;
71#define REG_SP UESP
72#define REG_PC EIP
73#define REG_FP EBP
74// 4900493 counter to prevent runaway LDTR refresh attempt
75
76static volatile int ldtr_refresh = 0;
77// the libthread instruction that faults because of the stale LDTR
78
79static const unsigned char movlfs[] = { 0x8e, 0xe0    // movl %eax,%fs
80                       };
81#endif // AMD64
82
83char* os::non_memory_address_word() {
84  // Must never look like an address returned by reserve_memory,
85  // even in its subfields (as defined by the CPU immediate fields,
86  // if the CPU splits constants across multiple instructions).
87  return (char*) -1;
88}
89
90//
91// Validate a ucontext retrieved from walking a uc_link of a ucontext.
92// There are issues with libthread giving out uc_links for different threads
93// on the same uc_link chain and bad or circular links.
94//
95bool os::Solaris::valid_ucontext(Thread* thread, ucontext_t* valid, ucontext_t* suspect) {
96  if (valid >= suspect ||
97      valid->uc_stack.ss_flags != suspect->uc_stack.ss_flags ||
98      valid->uc_stack.ss_sp    != suspect->uc_stack.ss_sp    ||
99      valid->uc_stack.ss_size  != suspect->uc_stack.ss_size) {
100    DEBUG_ONLY(tty->print_cr("valid_ucontext: failed test 1");)
101    return false;
102  }
103
104  if (thread->is_Java_thread()) {
105    if (!valid_stack_address(thread, (address)suspect)) {
106      DEBUG_ONLY(tty->print_cr("valid_ucontext: uc_link not in thread stack");)
107      return false;
108    }
109    if (!valid_stack_address(thread,  (address) suspect->uc_mcontext.gregs[REG_SP])) {
110      DEBUG_ONLY(tty->print_cr("valid_ucontext: stackpointer not in thread stack");)
111      return false;
112    }
113  }
114  return true;
115}
116
117// We will only follow one level of uc_link since there are libthread
118// issues with ucontext linking and it is better to be safe and just
119// let caller retry later.
120ucontext_t* os::Solaris::get_valid_uc_in_signal_handler(Thread *thread,
121  ucontext_t *uc) {
122
123  ucontext_t *retuc = NULL;
124
125  if (uc != NULL) {
126    if (uc->uc_link == NULL) {
127      // cannot validate without uc_link so accept current ucontext
128      retuc = uc;
129    } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
130      // first ucontext is valid so try the next one
131      uc = uc->uc_link;
132      if (uc->uc_link == NULL) {
133        // cannot validate without uc_link so accept current ucontext
134        retuc = uc;
135      } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
136        // the ucontext one level down is also valid so return it
137        retuc = uc;
138      }
139    }
140  }
141  return retuc;
142}
143
144// Assumes ucontext is valid
145ExtendedPC os::Solaris::ucontext_get_ExtendedPC(ucontext_t *uc) {
146  return ExtendedPC((address)uc->uc_mcontext.gregs[REG_PC]);
147}
148
149// Assumes ucontext is valid
150intptr_t* os::Solaris::ucontext_get_sp(ucontext_t *uc) {
151  return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];
152}
153
154// Assumes ucontext is valid
155intptr_t* os::Solaris::ucontext_get_fp(ucontext_t *uc) {
156  return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];
157}
158
159// For Forte Analyzer AsyncGetCallTrace profiling support - thread
160// is currently interrupted by SIGPROF.
161//
162// The difference between this and os::fetch_frame_from_context() is that
163// here we try to skip nested signal frames.
164ExtendedPC os::Solaris::fetch_frame_from_ucontext(Thread* thread,
165  ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
166
167  assert(thread != NULL, "just checking");
168  assert(ret_sp != NULL, "just checking");
169  assert(ret_fp != NULL, "just checking");
170
171  ucontext_t *luc = os::Solaris::get_valid_uc_in_signal_handler(thread, uc);
172  return os::fetch_frame_from_context(luc, ret_sp, ret_fp);
173}
174
175ExtendedPC os::fetch_frame_from_context(void* ucVoid,
176                    intptr_t** ret_sp, intptr_t** ret_fp) {
177
178  ExtendedPC  epc;
179  ucontext_t *uc = (ucontext_t*)ucVoid;
180
181  if (uc != NULL) {
182    epc = os::Solaris::ucontext_get_ExtendedPC(uc);
183    if (ret_sp) *ret_sp = os::Solaris::ucontext_get_sp(uc);
184    if (ret_fp) *ret_fp = os::Solaris::ucontext_get_fp(uc);
185  } else {
186    // construct empty ExtendedPC for return value checking
187    epc = ExtendedPC(NULL);
188    if (ret_sp) *ret_sp = (intptr_t *)NULL;
189    if (ret_fp) *ret_fp = (intptr_t *)NULL;
190  }
191
192  return epc;
193}
194
195frame os::fetch_frame_from_context(void* ucVoid) {
196  intptr_t* sp;
197  intptr_t* fp;
198  ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
199  return frame(sp, fp, epc.pc());
200}
201
202frame os::get_sender_for_C_frame(frame* fr) {
203  return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
204}
205
206extern "C" intptr_t *_get_previous_fp();  // in .il file.
207
208frame os::current_frame() {
209  intptr_t* fp = _get_previous_fp();
210  frame myframe((intptr_t*)os::current_stack_pointer(),
211                (intptr_t*)fp,
212                CAST_FROM_FN_PTR(address, os::current_frame));
213  if (os::is_first_C_frame(&myframe)) {
214    // stack is not walkable
215    return frame(NULL, NULL, NULL);
216  } else {
217    return os::get_sender_for_C_frame(&myframe);
218  }
219}
220
221// This is a simple callback that just fetches a PC for an interrupted thread.
222// The thread need not be suspended and the fetched PC is just a hint.
223// This one is currently used for profiling the VMThread ONLY!
224
225// Must be synchronous
226void GetThreadPC_Callback::execute(OSThread::InterruptArguments *args) {
227  Thread*     thread = args->thread();
228  ucontext_t* uc     = args->ucontext();
229  intptr_t* sp;
230
231  assert(ProfileVM && thread->is_VM_thread(), "just checking");
232
233  ExtendedPC new_addr((address)uc->uc_mcontext.gregs[REG_PC]);
234  _addr = new_addr;
235}
236
237static int threadgetstate(thread_t tid, int *flags, lwpid_t *lwp, stack_t *ss, gregset_t rs, lwpstatus_t *lwpstatus) {
238  char lwpstatusfile[PROCFILE_LENGTH];
239  int lwpfd, err;
240
241  if (err = os::Solaris::thr_getstate(tid, flags, lwp, ss, rs))
242    return (err);
243  if (*flags == TRS_LWPID) {
244    sprintf(lwpstatusfile, "/proc/%d/lwp/%d/lwpstatus", getpid(),
245            *lwp);
246    if ((lwpfd = open(lwpstatusfile, O_RDONLY)) < 0) {
247      perror("thr_mutator_status: open lwpstatus");
248      return (EINVAL);
249    }
250    if (pread(lwpfd, lwpstatus, sizeof (lwpstatus_t), (off_t)0) !=
251        sizeof (lwpstatus_t)) {
252      perror("thr_mutator_status: read lwpstatus");
253      (void) close(lwpfd);
254      return (EINVAL);
255    }
256    (void) close(lwpfd);
257  }
258  return (0);
259}
260
261#ifndef AMD64
262
263// Detecting SSE support by OS
264// From solaris_i486.s
265extern "C" bool sse_check();
266extern "C" bool sse_unavailable();
267
268enum { SSE_UNKNOWN, SSE_NOT_SUPPORTED, SSE_SUPPORTED};
269static int sse_status = SSE_UNKNOWN;
270
271
272static void  check_for_sse_support() {
273  if (!VM_Version::supports_sse()) {
274    sse_status = SSE_NOT_SUPPORTED;
275    return;
276  }
277  // looking for _sse_hw in libc.so, if it does not exist or
278  // the value (int) is 0, OS has no support for SSE
279  int *sse_hwp;
280  void *h;
281
282  if ((h=dlopen("/usr/lib/libc.so", RTLD_LAZY)) == NULL) {
283    //open failed, presume no support for SSE
284    sse_status = SSE_NOT_SUPPORTED;
285    return;
286  }
287  if ((sse_hwp = (int *)dlsym(h, "_sse_hw")) == NULL) {
288    sse_status = SSE_NOT_SUPPORTED;
289  } else if (*sse_hwp == 0) {
290    sse_status = SSE_NOT_SUPPORTED;
291  }
292  dlclose(h);
293
294  if (sse_status == SSE_UNKNOWN) {
295    bool (*try_sse)() = (bool (*)())sse_check;
296    sse_status = (*try_sse)() ? SSE_SUPPORTED : SSE_NOT_SUPPORTED;
297  }
298
299}
300
301bool os::supports_sse() {
302  if (sse_status == SSE_UNKNOWN)
303    check_for_sse_support();
304  return sse_status == SSE_SUPPORTED;
305}
306
307#endif // AMD64
308
309bool os::is_allocatable(size_t bytes) {
310#ifdef AMD64
311  return true;
312#else
313
314  if (bytes < 2 * G) {
315    return true;
316  }
317
318  char* addr = reserve_memory(bytes, NULL);
319
320  if (addr != NULL) {
321    release_memory(addr, bytes);
322  }
323
324  return addr != NULL;
325#endif // AMD64
326
327}
328
329extern "C" int JVM_handle_solaris_signal(int signo, siginfo_t* siginfo, void* ucontext, int abort_if_unrecognized);
330
331extern "C" void Fetch32PFI () ;
332extern "C" void Fetch32Resume () ;
333#ifdef AMD64
334extern "C" void FetchNPFI () ;
335extern "C" void FetchNResume () ;
336#endif // AMD64
337
338int JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid, int abort_if_unrecognized) {
339  ucontext_t* uc = (ucontext_t*) ucVoid;
340
341#ifndef AMD64
342  if (sig == SIGILL && info->si_addr == (caddr_t)sse_check) {
343    // the SSE instruction faulted. supports_sse() need return false.
344    uc->uc_mcontext.gregs[EIP] = (greg_t)sse_unavailable;
345    return true;
346  }
347#endif // !AMD64
348
349  Thread* t = ThreadLocalStorage::get_thread_slow();  // slow & steady
350
351  SignalHandlerMark shm(t);
352
353  if(sig == SIGPIPE || sig == SIGXFSZ) {
354    if (os::Solaris::chained_handler(sig, info, ucVoid)) {
355      return true;
356    } else {
357      if (PrintMiscellaneous && (WizardMode || Verbose)) {
358        char buf[64];
359        warning("Ignoring %s - see 4229104 or 6499219",
360                os::exception_name(sig, buf, sizeof(buf)));
361
362      }
363      return true;
364    }
365  }
366
367  JavaThread* thread = NULL;
368  VMThread* vmthread = NULL;
369
370  if (os::Solaris::signal_handlers_are_installed) {
371    if (t != NULL ){
372      if(t->is_Java_thread()) {
373        thread = (JavaThread*)t;
374      }
375      else if(t->is_VM_thread()){
376        vmthread = (VMThread *)t;
377      }
378    }
379  }
380
381  guarantee(sig != os::Solaris::SIGinterrupt(), "Can not chain VM interrupt signal, try -XX:+UseAltSigs");
382
383  if (sig == os::Solaris::SIGasync()) {
384    if(thread){
385      OSThread::InterruptArguments args(thread, uc);
386      thread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
387      return true;
388    }
389    else if(vmthread){
390      OSThread::InterruptArguments args(vmthread, uc);
391      vmthread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
392      return true;
393    } else if (os::Solaris::chained_handler(sig, info, ucVoid)) {
394      return true;
395    } else {
396      // If os::Solaris::SIGasync not chained, and this is a non-vm and
397      // non-java thread
398      return true;
399    }
400  }
401
402  if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
403    // can't decode this kind of signal
404    info = NULL;
405  } else {
406    assert(sig == info->si_signo, "bad siginfo");
407  }
408
409  // decide if this trap can be handled by a stub
410  address stub = NULL;
411
412  address pc          = NULL;
413
414  //%note os_trap_1
415  if (info != NULL && uc != NULL && thread != NULL) {
416    // factor me: getPCfromContext
417    pc = (address) uc->uc_mcontext.gregs[REG_PC];
418
419    // SafeFetch32() support
420    if (pc == (address) Fetch32PFI) {
421      uc->uc_mcontext.gregs[REG_PC] = intptr_t(Fetch32Resume) ;
422      return true ;
423    }
424#ifdef AMD64
425    if (pc == (address) FetchNPFI) {
426       uc->uc_mcontext.gregs [REG_PC] = intptr_t(FetchNResume) ;
427       return true ;
428    }
429#endif // AMD64
430
431    // Handle ALL stack overflow variations here
432    if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) {
433      address addr = (address) info->si_addr;
434      if (thread->in_stack_yellow_zone(addr)) {
435        thread->disable_stack_yellow_zone();
436        if (thread->thread_state() == _thread_in_Java) {
437          // Throw a stack overflow exception.  Guard pages will be reenabled
438          // while unwinding the stack.
439          stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
440        } else {
441          // Thread was in the vm or native code.  Return and try to finish.
442          return true;
443        }
444      } else if (thread->in_stack_red_zone(addr)) {
445        // Fatal red zone violation.  Disable the guard pages and fall through
446        // to handle_unexpected_exception way down below.
447        thread->disable_stack_red_zone();
448        tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
449      }
450    }
451
452    if (thread->thread_state() == _thread_in_vm) {
453      if (sig == SIGBUS && info->si_code == BUS_OBJERR && thread->doing_unsafe_access()) {
454        stub = StubRoutines::handler_for_unsafe_access();
455      }
456    }
457
458    if (thread->thread_state() == _thread_in_Java) {
459      // Support Safepoint Polling
460      if ( sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
461        stub = SharedRuntime::get_poll_stub(pc);
462      }
463      else if (sig == SIGBUS && info->si_code == BUS_OBJERR) {
464        // BugId 4454115: A read from a MappedByteBuffer can fault
465        // here if the underlying file has been truncated.
466        // Do not crash the VM in such a case.
467        CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
468        nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL;
469        if (nm != NULL && nm->has_unsafe_access()) {
470          stub = StubRoutines::handler_for_unsafe_access();
471        }
472      }
473      else
474      if (sig == SIGFPE && info->si_code == FPE_INTDIV) {
475        // integer divide by zero
476        stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
477      }
478#ifndef AMD64
479      else if (sig == SIGFPE && info->si_code == FPE_FLTDIV) {
480        // floating-point divide by zero
481        stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
482      }
483      else if (sig == SIGFPE && info->si_code == FPE_FLTINV) {
484        // The encoding of D2I in i486.ad can cause an exception prior
485        // to the fist instruction if there was an invalid operation
486        // pending. We want to dismiss that exception. From the win_32
487        // side it also seems that if it really was the fist causing
488        // the exception that we do the d2i by hand with different
489        // rounding. Seems kind of weird. QQQ TODO
490        // Note that we take the exception at the NEXT floating point instruction.
491        if (pc[0] == 0xDB) {
492            assert(pc[0] == 0xDB, "not a FIST opcode");
493            assert(pc[1] == 0x14, "not a FIST opcode");
494            assert(pc[2] == 0x24, "not a FIST opcode");
495            return true;
496        } else {
497            assert(pc[-3] == 0xDB, "not an flt invalid opcode");
498            assert(pc[-2] == 0x14, "not an flt invalid opcode");
499            assert(pc[-1] == 0x24, "not an flt invalid opcode");
500        }
501      }
502      else if (sig == SIGFPE ) {
503        tty->print_cr("caught SIGFPE, info 0x%x.", info->si_code);
504      }
505#endif // !AMD64
506
507        // QQQ It doesn't seem that we need to do this on x86 because we should be able
508        // to return properly from the handler without this extra stuff on the back side.
509
510      else if (sig == SIGSEGV && info->si_code > 0 && !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
511        // Determination of interpreter/vtable stub/compiled code null exception
512        stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
513      }
514    }
515
516    // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
517    // and the heap gets shrunk before the field access.
518    if ((sig == SIGSEGV) || (sig == SIGBUS)) {
519      address addr = JNI_FastGetField::find_slowcase_pc(pc);
520      if (addr != (address)-1) {
521        stub = addr;
522      }
523    }
524
525    // Check to see if we caught the safepoint code in the
526    // process of write protecting the memory serialization page.
527    // It write enables the page immediately after protecting it
528    // so we can just return to retry the write.
529    if ((sig == SIGSEGV) &&
530        os::is_memory_serialize_page(thread, (address)info->si_addr)) {
531      // Block current thread until the memory serialize page permission restored.
532      os::block_on_serialize_page_trap();
533      return true;
534    }
535  }
536
537  // Execution protection violation
538  //
539  // Preventative code for future versions of Solaris which may
540  // enable execution protection when running the 32-bit VM on AMD64.
541  //
542  // This should be kept as the last step in the triage.  We don't
543  // have a dedicated trap number for a no-execute fault, so be
544  // conservative and allow other handlers the first shot.
545  //
546  // Note: We don't test that info->si_code == SEGV_ACCERR here.
547  // this si_code is so generic that it is almost meaningless; and
548  // the si_code for this condition may change in the future.
549  // Furthermore, a false-positive should be harmless.
550  if (UnguardOnExecutionViolation > 0 &&
551      (sig == SIGSEGV || sig == SIGBUS) &&
552      uc->uc_mcontext.gregs[TRAPNO] == T_PGFLT) {  // page fault
553    int page_size = os::vm_page_size();
554    address addr = (address) info->si_addr;
555    address pc = (address) uc->uc_mcontext.gregs[REG_PC];
556    // Make sure the pc and the faulting address are sane.
557    //
558    // If an instruction spans a page boundary, and the page containing
559    // the beginning of the instruction is executable but the following
560    // page is not, the pc and the faulting address might be slightly
561    // different - we still want to unguard the 2nd page in this case.
562    //
563    // 15 bytes seems to be a (very) safe value for max instruction size.
564    bool pc_is_near_addr =
565      (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
566    bool instr_spans_page_boundary =
567      (align_size_down((intptr_t) pc ^ (intptr_t) addr,
568                       (intptr_t) page_size) > 0);
569
570    if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
571      static volatile address last_addr =
572        (address) os::non_memory_address_word();
573
574      // In conservative mode, don't unguard unless the address is in the VM
575      if (addr != last_addr &&
576          (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
577
578        // Unguard and retry
579        address page_start =
580          (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
581        bool res = os::unguard_memory((char*) page_start, page_size);
582
583        if (PrintMiscellaneous && Verbose) {
584          char buf[256];
585          jio_snprintf(buf, sizeof(buf), "Execution protection violation "
586                       "at " INTPTR_FORMAT
587                       ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr,
588                       page_start, (res ? "success" : "failed"), errno);
589          tty->print_raw_cr(buf);
590        }
591        stub = pc;
592
593        // Set last_addr so if we fault again at the same address, we don't end
594        // up in an endless loop.
595        //
596        // There are two potential complications here.  Two threads trapping at
597        // the same address at the same time could cause one of the threads to
598        // think it already unguarded, and abort the VM.  Likely very rare.
599        //
600        // The other race involves two threads alternately trapping at
601        // different addresses and failing to unguard the page, resulting in
602        // an endless loop.  This condition is probably even more unlikely than
603        // the first.
604        //
605        // Although both cases could be avoided by using locks or thread local
606        // last_addr, these solutions are unnecessary complication: this
607        // handler is a best-effort safety net, not a complete solution.  It is
608        // disabled by default and should only be used as a workaround in case
609        // we missed any no-execute-unsafe VM code.
610
611        last_addr = addr;
612      }
613    }
614  }
615
616  if (stub != NULL) {
617    // save all thread context in case we need to restore it
618
619    if (thread != NULL) thread->set_saved_exception_pc(pc);
620    // 12/02/99: On Sparc it appears that the full context is also saved
621    // but as yet, no one looks at or restores that saved context
622    // factor me: setPC
623    uc->uc_mcontext.gregs[REG_PC] = (greg_t)stub;
624    return true;
625  }
626
627  // signal-chaining
628  if (os::Solaris::chained_handler(sig, info, ucVoid)) {
629    return true;
630  }
631
632#ifndef AMD64
633  // Workaround (bug 4900493) for Solaris kernel bug 4966651.
634  // Handle an undefined selector caused by an attempt to assign
635  // fs in libthread getipriptr(). With the current libthread design every 512
636  // thread creations the LDT for a private thread data structure is extended
637  // and thre is a hazard that and another thread attempting a thread creation
638  // will use a stale LDTR that doesn't reflect the structure's growth,
639  // causing a GP fault.
640  // Enforce the probable limit of passes through here to guard against an
641  // infinite loop if some other move to fs caused the GP fault. Note that
642  // this loop counter is ultimately a heuristic as it is possible for
643  // more than one thread to generate this fault at a time in an MP system.
644  // In the case of the loop count being exceeded or if the poll fails
645  // just fall through to a fatal error.
646  // If there is some other source of T_GPFLT traps and the text at EIP is
647  // unreadable this code will loop infinitely until the stack is exausted.
648  // The key to diagnosis in this case is to look for the bottom signal handler
649  // frame.
650
651  if(! IgnoreLibthreadGPFault) {
652    if (sig == SIGSEGV && uc->uc_mcontext.gregs[TRAPNO] == T_GPFLT) {
653      const unsigned char *p =
654                        (unsigned const char *) uc->uc_mcontext.gregs[EIP];
655
656      // Expected instruction?
657
658      if(p[0] == movlfs[0] && p[1] == movlfs[1]) {
659
660        Atomic::inc(&ldtr_refresh);
661
662        // Infinite loop?
663
664        if(ldtr_refresh < ((2 << 16) / PAGESIZE)) {
665
666          // No, force scheduling to get a fresh view of the LDTR
667
668          if(poll(NULL, 0, 10) == 0) {
669
670            // Retry the move
671
672            return false;
673          }
674        }
675      }
676    }
677  }
678#endif // !AMD64
679
680  if (!abort_if_unrecognized) {
681    // caller wants another chance, so give it to him
682    return false;
683  }
684
685  if (!os::Solaris::libjsig_is_loaded) {
686    struct sigaction oldAct;
687    sigaction(sig, (struct sigaction *)0, &oldAct);
688    if (oldAct.sa_sigaction != signalHandler) {
689      void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oldAct.sa_sigaction)
690                                          : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
691      warning("Unexpected Signal %d occured under user-defined signal handler %#lx", sig, (long)sighand);
692    }
693  }
694
695  if (pc == NULL && uc != NULL) {
696    pc = (address) uc->uc_mcontext.gregs[REG_PC];
697  }
698
699  // unmask current signal
700  sigset_t newset;
701  sigemptyset(&newset);
702  sigaddset(&newset, sig);
703  sigprocmask(SIG_UNBLOCK, &newset, NULL);
704
705  VMError err(t, sig, pc, info, ucVoid);
706  err.report_and_die();
707
708  ShouldNotReachHere();
709}
710
711void os::print_context(outputStream *st, void *context) {
712  if (context == NULL) return;
713
714  ucontext_t *uc = (ucontext_t*)context;
715  st->print_cr("Registers:");
716#ifdef AMD64
717  st->print(  "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]);
718  st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]);
719  st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]);
720  st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]);
721  st->cr();
722  st->print(  "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]);
723  st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]);
724  st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]);
725  st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]);
726  st->cr();
727  st->print(", R8=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]);
728  st->print(", R9=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]);
729  st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]);
730  st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]);
731  st->print(", R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]);
732  st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]);
733  st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]);
734  st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]);
735  st->cr();
736  st->print(  "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]);
737  st->print(", RFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RFL]);
738#else
739  st->print(  "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EAX]);
740  st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBX]);
741  st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ECX]);
742  st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDX]);
743  st->cr();
744  st->print(  "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[UESP]);
745  st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBP]);
746  st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ESI]);
747  st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDI]);
748  st->cr();
749  st->print(  "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EIP]);
750  st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EFL]);
751#endif // AMD64
752  st->cr();
753  st->cr();
754
755  intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
756  st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
757  print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
758  st->cr();
759
760  // Note: it may be unsafe to inspect memory near pc. For example, pc may
761  // point to garbage if entry point in an nmethod is corrupted. Leave
762  // this at the end, and hope for the best.
763  ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc);
764  address pc = epc.pc();
765  st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
766  print_hex_dump(st, pc - 16, pc + 16, sizeof(char));
767}
768
769#ifdef AMD64
770void os::Solaris::init_thread_fpu_state(void) {
771  // Nothing to do
772}
773#else
774// From solaris_i486.s
775extern "C" void fixcw();
776
777void os::Solaris::init_thread_fpu_state(void) {
778  // Set fpu to 53 bit precision. This happens too early to use a stub.
779  fixcw();
780}
781
782// These routines are the initial value of atomic_xchg_entry(),
783// atomic_cmpxchg_entry(), atomic_inc_entry() and fence_entry()
784// until initialization is complete.
785// TODO - replace with .il implementation when compiler supports it.
786
787typedef jint  xchg_func_t        (jint,  volatile jint*);
788typedef jint  cmpxchg_func_t     (jint,  volatile jint*,  jint);
789typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong);
790typedef jint  add_func_t         (jint,  volatile jint*);
791typedef void  fence_func_t       ();
792
793jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) {
794  // try to use the stub:
795  xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry());
796
797  if (func != NULL) {
798    os::atomic_xchg_func = func;
799    return (*func)(exchange_value, dest);
800  }
801  assert(Threads::number_of_threads() == 0, "for bootstrap only");
802
803  jint old_value = *dest;
804  *dest = exchange_value;
805  return old_value;
806}
807
808jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) {
809  // try to use the stub:
810  cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
811
812  if (func != NULL) {
813    os::atomic_cmpxchg_func = func;
814    return (*func)(exchange_value, dest, compare_value);
815  }
816  assert(Threads::number_of_threads() == 0, "for bootstrap only");
817
818  jint old_value = *dest;
819  if (old_value == compare_value)
820    *dest = exchange_value;
821  return old_value;
822}
823
824jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) {
825  // try to use the stub:
826  cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
827
828  if (func != NULL) {
829    os::atomic_cmpxchg_long_func = func;
830    return (*func)(exchange_value, dest, compare_value);
831  }
832  assert(Threads::number_of_threads() == 0, "for bootstrap only");
833
834  jlong old_value = *dest;
835  if (old_value == compare_value)
836    *dest = exchange_value;
837  return old_value;
838}
839
840jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) {
841  // try to use the stub:
842  add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry());
843
844  if (func != NULL) {
845    os::atomic_add_func = func;
846    return (*func)(add_value, dest);
847  }
848  assert(Threads::number_of_threads() == 0, "for bootstrap only");
849
850  return (*dest) += add_value;
851}
852
853void os::fence_bootstrap() {
854  // try to use the stub:
855  fence_func_t* func = CAST_TO_FN_PTR(fence_func_t*, StubRoutines::fence_entry());
856
857  if (func != NULL) {
858    os::fence_func = func;
859    (*func)();
860    return;
861  }
862  assert(Threads::number_of_threads() == 0, "for bootstrap only");
863
864  // don't have to do anything for a single thread
865}
866
867xchg_func_t*         os::atomic_xchg_func         = os::atomic_xchg_bootstrap;
868cmpxchg_func_t*      os::atomic_cmpxchg_func      = os::atomic_cmpxchg_bootstrap;
869cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
870add_func_t*          os::atomic_add_func          = os::atomic_add_bootstrap;
871fence_func_t*        os::fence_func               = os::fence_bootstrap;
872
873extern "C" _solaris_raw_setup_fpu(address ptr);
874void os::setup_fpu() {
875  address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std();
876  _solaris_raw_setup_fpu(fpu_cntrl);
877}
878#endif // AMD64
879