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