os_bsd_zero.cpp revision 3794:0af5da0c9d9d
1/*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * Copyright 2007, 2008, 2009, 2010 Red Hat, Inc.
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 */
25
26#if !defined(__APPLE__) && !defined(__NetBSD__)
27#include <pthread.h>
28# include <pthread_np.h> /* For pthread_attr_get_np */
29#endif
30
31// no precompiled headers
32#include "assembler_zero.inline.hpp"
33#include "classfile/classLoader.hpp"
34#include "classfile/systemDictionary.hpp"
35#include "classfile/vmSymbols.hpp"
36#include "code/icBuffer.hpp"
37#include "code/vtableStubs.hpp"
38#include "interpreter/interpreter.hpp"
39#include "jvm_bsd.h"
40#include "memory/allocation.inline.hpp"
41#include "mutex_bsd.inline.hpp"
42#include "nativeInst_zero.hpp"
43#include "os_share_bsd.hpp"
44#include "prims/jniFastGetField.hpp"
45#include "prims/jvm.h"
46#include "prims/jvm_misc.hpp"
47#include "runtime/arguments.hpp"
48#include "runtime/extendedPC.hpp"
49#include "runtime/frame.inline.hpp"
50#include "runtime/interfaceSupport.hpp"
51#include "runtime/java.hpp"
52#include "runtime/javaCalls.hpp"
53#include "runtime/mutexLocker.hpp"
54#include "runtime/osThread.hpp"
55#include "runtime/sharedRuntime.hpp"
56#include "runtime/stubRoutines.hpp"
57#include "runtime/timer.hpp"
58#include "thread_bsd.inline.hpp"
59#include "utilities/events.hpp"
60#include "utilities/vmError.hpp"
61
62address os::current_stack_pointer() {
63  address dummy = (address) &dummy;
64  return dummy;
65}
66
67frame os::get_sender_for_C_frame(frame* fr) {
68  ShouldNotCallThis();
69}
70
71frame os::current_frame() {
72  // The only thing that calls this is the stack printing code in
73  // VMError::report:
74  //   - Step 110 (printing stack bounds) uses the sp in the frame
75  //     to determine the amount of free space on the stack.  We
76  //     set the sp to a close approximation of the real value in
77  //     order to allow this step to complete.
78  //   - Step 120 (printing native stack) tries to walk the stack.
79  //     The frame we create has a NULL pc, which is ignored as an
80  //     invalid frame.
81  frame dummy = frame();
82  dummy.set_sp((intptr_t *) current_stack_pointer());
83  return dummy;
84}
85
86char* os::non_memory_address_word() {
87  // Must never look like an address returned by reserve_memory,
88  // even in its subfields (as defined by the CPU immediate fields,
89  // if the CPU splits constants across multiple instructions).
90#ifdef SPARC
91  // On SPARC, 0 != %hi(any real address), because there is no
92  // allocation in the first 1Kb of the virtual address space.
93  return (char *) 0;
94#else
95  // This is the value for x86; works pretty well for PPC too.
96  return (char *) -1;
97#endif // SPARC
98}
99
100void os::initialize_thread(Thread* thr) {
101  // Nothing to do.
102}
103
104address os::Bsd::ucontext_get_pc(ucontext_t* uc) {
105  ShouldNotCallThis();
106}
107
108ExtendedPC os::fetch_frame_from_context(void* ucVoid,
109                                        intptr_t** ret_sp,
110                                        intptr_t** ret_fp) {
111  ShouldNotCallThis();
112}
113
114frame os::fetch_frame_from_context(void* ucVoid) {
115  ShouldNotCallThis();
116}
117
118extern "C" JNIEXPORT int
119JVM_handle_bsd_signal(int sig,
120                        siginfo_t* info,
121                        void* ucVoid,
122                        int abort_if_unrecognized) {
123  ucontext_t* uc = (ucontext_t*) ucVoid;
124
125  Thread* t = ThreadLocalStorage::get_thread_slow();
126
127  SignalHandlerMark shm(t);
128
129  // Note: it's not uncommon that JNI code uses signal/sigset to
130  // install then restore certain signal handler (e.g. to temporarily
131  // block SIGPIPE, or have a SIGILL handler when detecting CPU
132  // type). When that happens, JVM_handle_bsd_signal() might be
133  // invoked with junk info/ucVoid. To avoid unnecessary crash when
134  // libjsig is not preloaded, try handle signals that do not require
135  // siginfo/ucontext first.
136
137  if (sig == SIGPIPE || sig == SIGXFSZ) {
138    // allow chained handler to go first
139    if (os::Bsd::chained_handler(sig, info, ucVoid)) {
140      return true;
141    } else {
142      if (PrintMiscellaneous && (WizardMode || Verbose)) {
143        char buf[64];
144        warning("Ignoring %s - see bugs 4229104 or 646499219",
145                os::exception_name(sig, buf, sizeof(buf)));
146      }
147      return true;
148    }
149  }
150
151  JavaThread* thread = NULL;
152  VMThread* vmthread = NULL;
153  if (os::Bsd::signal_handlers_are_installed) {
154    if (t != NULL ){
155      if(t->is_Java_thread()) {
156        thread = (JavaThread*)t;
157      }
158      else if(t->is_VM_thread()){
159        vmthread = (VMThread *)t;
160      }
161    }
162  }
163
164  if (info != NULL && thread != NULL) {
165    // Handle ALL stack overflow variations here
166    if (sig == SIGSEGV || sig == SIGBUS) {
167      address addr = (address) info->si_addr;
168
169      // check if fault address is within thread stack
170      if (addr < thread->stack_base() &&
171          addr >= thread->stack_base() - thread->stack_size()) {
172        // stack overflow
173        if (thread->in_stack_yellow_zone(addr)) {
174          thread->disable_stack_yellow_zone();
175          ShouldNotCallThis();
176        }
177        else if (thread->in_stack_red_zone(addr)) {
178          thread->disable_stack_red_zone();
179          ShouldNotCallThis();
180        }
181      }
182    }
183
184    /*if (thread->thread_state() == _thread_in_Java) {
185      ShouldNotCallThis();
186    }
187    else*/ if (thread->thread_state() == _thread_in_vm &&
188               sig == SIGBUS && thread->doing_unsafe_access()) {
189      ShouldNotCallThis();
190    }
191
192    // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC
193    // kicks in and the heap gets shrunk before the field access.
194    /*if (sig == SIGSEGV || sig == SIGBUS) {
195      address addr = JNI_FastGetField::find_slowcase_pc(pc);
196      if (addr != (address)-1) {
197        stub = addr;
198      }
199    }*/
200
201    // Check to see if we caught the safepoint code in the process
202    // of write protecting the memory serialization page.  It write
203    // enables the page immediately after protecting it so we can
204    // just return to retry the write.
205    if ((sig == SIGSEGV || sig == SIGBUS) &&
206        os::is_memory_serialize_page(thread, (address) info->si_addr)) {
207      // Block current thread until permission is restored.
208      os::block_on_serialize_page_trap();
209      return true;
210    }
211  }
212
213  // signal-chaining
214  if (os::Bsd::chained_handler(sig, info, ucVoid)) {
215     return true;
216  }
217
218  if (!abort_if_unrecognized) {
219    // caller wants another chance, so give it to him
220    return false;
221  }
222
223#ifndef PRODUCT
224  if (sig == SIGSEGV) {
225    fatal("\n#"
226          "\n#    /--------------------\\"
227          "\n#    | segmentation fault |"
228          "\n#    \\---\\ /--------------/"
229          "\n#        /"
230          "\n#    [-]        |\\_/|    "
231          "\n#    (+)=C      |o o|__  "
232          "\n#    | |        =-*-=__\\ "
233          "\n#    OOO        c_c_(___)");
234  }
235#endif // !PRODUCT
236
237  const char *fmt =
238      "caught unhandled signal " INT32_FORMAT " at address " PTR_FORMAT;
239  char buf[128];
240
241  sprintf(buf, fmt, sig, info->si_addr);
242  fatal(buf);
243}
244
245void os::Bsd::init_thread_fpu_state(void) {
246  // Nothing to do
247}
248
249bool os::is_allocatable(size_t bytes) {
250#ifdef _LP64
251  return true;
252#else
253  if (bytes < 2 * G) {
254    return true;
255  }
256
257  char* addr = reserve_memory(bytes, NULL);
258
259  if (addr != NULL) {
260    release_memory(addr, bytes);
261  }
262
263  return addr != NULL;
264#endif // _LP64
265}
266
267///////////////////////////////////////////////////////////////////////////////
268// thread stack
269
270size_t os::Bsd::min_stack_allowed = 64 * K;
271
272bool os::Bsd::supports_variable_stack_size() {
273  return true;
274}
275
276size_t os::Bsd::default_stack_size(os::ThreadType thr_type) {
277#ifdef _LP64
278  size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
279#else
280  size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
281#endif // _LP64
282  return s;
283}
284
285size_t os::Bsd::default_guard_size(os::ThreadType thr_type) {
286  // Only enable glibc guard pages for non-Java threads
287  // (Java threads have HotSpot guard pages)
288  return (thr_type == java_thread ? 0 : page_size());
289}
290
291static void current_stack_region(address *bottom, size_t *size) {
292  address stack_bottom;
293  address stack_top;
294  size_t stack_bytes;
295
296#ifdef __APPLE__
297  pthread_t self = pthread_self();
298  stack_top = (address) pthread_get_stackaddr_np(self);
299  stack_bytes = pthread_get_stacksize_np(self);
300  stack_bottom = stack_top - stack_bytes;
301#elif defined(__OpenBSD__)
302  stack_t ss;
303  int rslt = pthread_stackseg_np(pthread_self(), &ss);
304
305  if (rslt != 0)
306    fatal(err_msg("pthread_stackseg_np failed with err = " INT32_FORMAT,
307          rslt));
308
309  stack_top = (address) ss.ss_sp;
310  stack_bytes  = ss.ss_size;
311  stack_bottom = stack_top - stack_bytes;
312#else
313  pthread_attr_t attr;
314
315  int rslt = pthread_attr_init(&attr);
316
317  // JVM needs to know exact stack location, abort if it fails
318  if (rslt != 0)
319    fatal(err_msg("pthread_attr_init failed with err = " INT32_FORMAT, rslt));
320
321  rslt = pthread_attr_get_np(pthread_self(), &attr);
322
323  if (rslt != 0)
324    fatal(err_msg("pthread_attr_get_np failed with err = " INT32_FORMAT,
325          rslt));
326
327  if (pthread_attr_getstackaddr(&attr, (void **) &stack_bottom) != 0 ||
328      pthread_attr_getstacksize(&attr, &stack_bytes) != 0) {
329    fatal("Can not locate current stack attributes!");
330  }
331
332  pthread_attr_destroy(&attr);
333
334  stack_top = stack_bottom + stack_bytes;
335#endif
336
337  assert(os::current_stack_pointer() >= stack_bottom, "should do");
338  assert(os::current_stack_pointer() < stack_top, "should do");
339
340  *bottom = stack_bottom;
341  *size = stack_top - stack_bottom;
342}
343
344address os::current_stack_base() {
345  address bottom;
346  size_t size;
347  current_stack_region(&bottom, &size);
348  return bottom + size;
349}
350
351size_t os::current_stack_size() {
352  // stack size includes normal stack and HotSpot guard pages
353  address bottom;
354  size_t size;
355  current_stack_region(&bottom, &size);
356  return size;
357}
358
359/////////////////////////////////////////////////////////////////////////////
360// helper functions for fatal error handler
361
362void os::print_context(outputStream* st, void* context) {
363  ShouldNotCallThis();
364}
365
366void os::print_register_info(outputStream *st, void *context) {
367  ShouldNotCallThis();
368}
369
370/////////////////////////////////////////////////////////////////////////////
371// Stubs for things that would be in bsd_zero.s if it existed.
372// You probably want to disassemble these monkeys to check they're ok.
373
374extern "C" {
375  int SpinPause() {
376  }
377
378  int SafeFetch32(int *adr, int errValue) {
379    int value = errValue;
380    value = *adr;
381    return value;
382  }
383  intptr_t SafeFetchN(intptr_t *adr, intptr_t errValue) {
384    intptr_t value = errValue;
385    value = *adr;
386    return value;
387  }
388
389  void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
390    if (from > to) {
391      jshort *end = from + count;
392      while (from < end)
393        *(to++) = *(from++);
394    }
395    else if (from < to) {
396      jshort *end = from;
397      from += count - 1;
398      to   += count - 1;
399      while (from >= end)
400        *(to--) = *(from--);
401    }
402  }
403  void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
404    if (from > to) {
405      jint *end = from + count;
406      while (from < end)
407        *(to++) = *(from++);
408    }
409    else if (from < to) {
410      jint *end = from;
411      from += count - 1;
412      to   += count - 1;
413      while (from >= end)
414        *(to--) = *(from--);
415    }
416  }
417  void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
418    if (from > to) {
419      jlong *end = from + count;
420      while (from < end)
421        os::atomic_copy64(from++, to++);
422    }
423    else if (from < to) {
424      jlong *end = from;
425      from += count - 1;
426      to   += count - 1;
427      while (from >= end)
428        os::atomic_copy64(from--, to--);
429    }
430  }
431
432  void _Copy_arrayof_conjoint_bytes(HeapWord* from,
433                                    HeapWord* to,
434                                    size_t    count) {
435    memmove(to, from, count);
436  }
437  void _Copy_arrayof_conjoint_jshorts(HeapWord* from,
438                                      HeapWord* to,
439                                      size_t    count) {
440    memmove(to, from, count * 2);
441  }
442  void _Copy_arrayof_conjoint_jints(HeapWord* from,
443                                    HeapWord* to,
444                                    size_t    count) {
445    memmove(to, from, count * 4);
446  }
447  void _Copy_arrayof_conjoint_jlongs(HeapWord* from,
448                                     HeapWord* to,
449                                     size_t    count) {
450    memmove(to, from, count * 8);
451  }
452};
453
454/////////////////////////////////////////////////////////////////////////////
455// Implementations of atomic operations not supported by processors.
456//  -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html
457
458#ifndef _LP64
459extern "C" {
460  long long unsigned int __sync_val_compare_and_swap_8(
461    volatile void *ptr,
462    long long unsigned int oldval,
463    long long unsigned int newval) {
464    ShouldNotCallThis();
465  }
466};
467#endif // !_LP64
468
469#ifndef PRODUCT
470void os::verify_stack_alignment() {
471}
472#endif
473