os_bsd.hpp revision 3794:0af5da0c9d9d
1/*
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef OS_BSD_VM_OS_BSD_HPP
26#define OS_BSD_VM_OS_BSD_HPP
27
28// Bsd_OS defines the interface to Bsd operating systems
29
30/* pthread_getattr_np comes with BsdThreads-0.9-7 on RedHat 7.1 */
31typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
32
33#ifdef __APPLE__
34// Mac OS X doesn't support clock_gettime. Stub out the type, it is
35// unused
36typedef int clockid_t;
37#endif
38
39class Bsd {
40  friend class os;
41
42  // For signal-chaining
43#define MAXSIGNUM 32
44  static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
45  static unsigned int sigs;             // mask of signals that have
46                                        // preinstalled signal handlers
47  static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
48                                        // __sigaction(), signal() is loaded
49  static struct sigaction *(*get_signal_action)(int);
50  static struct sigaction *get_preinstalled_handler(int);
51  static void save_preinstalled_handler(int, struct sigaction&);
52
53  static void check_signal_handler(int sig);
54
55  // For signal flags diagnostics
56  static int sigflags[MAXSIGNUM];
57
58  static int (*_clock_gettime)(clockid_t, struct timespec *);
59
60  static GrowableArray<int>* _cpu_to_node;
61
62 protected:
63
64  static julong _physical_memory;
65  static pthread_t _main_thread;
66  static int _page_size;
67
68  static julong available_memory();
69  static julong physical_memory() { return _physical_memory; }
70  static void initialize_system_info();
71
72  static bool supports_variable_stack_size();
73
74  static void rebuild_cpu_to_node_map();
75  static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
76
77  static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
78
79 public:
80
81  static void init_thread_fpu_state();
82  static pthread_t main_thread(void)                                { return _main_thread; }
83
84  static void hotspot_sigmask(Thread* thread);
85
86  static bool is_initial_thread(void);
87
88  static int page_size(void)                                        { return _page_size; }
89  static void set_page_size(int val)                                { _page_size = val; }
90
91  static address   ucontext_get_pc(ucontext_t* uc);
92  static intptr_t* ucontext_get_sp(ucontext_t* uc);
93  static intptr_t* ucontext_get_fp(ucontext_t* uc);
94
95  // For Analyzer Forte AsyncGetCallTrace profiling support:
96  //
97  // This interface should be declared in os_bsd_i486.hpp, but
98  // that file provides extensions to the os class and not the
99  // Bsd class.
100  static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
101    intptr_t** ret_sp, intptr_t** ret_fp);
102
103  // This boolean allows users to forward their own non-matching signals
104  // to JVM_handle_bsd_signal, harmlessly.
105  static bool signal_handlers_are_installed;
106
107  static int get_our_sigflags(int);
108  static void set_our_sigflags(int, int);
109  static void signal_sets_init();
110  static void install_signal_handlers();
111  static void set_signal_handler(int, bool);
112  static bool is_sig_ignored(int sig);
113
114  static sigset_t* unblocked_signals();
115  static sigset_t* vm_signals();
116  static sigset_t* allowdebug_blocked_signals();
117
118  // For signal-chaining
119  static struct sigaction *get_chained_signal_action(int sig);
120  static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
121
122  // Minimum stack size a thread can be created with (allowing
123  // the VM to completely create the thread and enter user code)
124  static size_t min_stack_allowed;
125
126  // Return default stack size or guard size for the specified thread type
127  static size_t default_stack_size(os::ThreadType thr_type);
128  static size_t default_guard_size(os::ThreadType thr_type);
129
130  // Real-time clock functions
131  static void clock_init(void);
132
133  static inline bool supports_monotonic_clock() {
134    return _clock_gettime != NULL;
135  }
136
137  static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
138    return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
139  }
140
141  // Stack repair handling
142
143  // none present
144
145  // BsdThreads work-around for 6292965
146  static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
147
148
149  // Bsd suspend/resume support - this helper is a shadow of its former
150  // self now that low-level suspension is barely used, and old workarounds
151  // for BsdThreads are no longer needed.
152  class SuspendResume {
153  private:
154    volatile int _suspend_action;
155    // values for suspend_action:
156    #define SR_NONE               (0x00)
157    #define SR_SUSPEND            (0x01)  // suspend request
158    #define SR_CONTINUE           (0x02)  // resume request
159
160    volatile jint _state;
161    // values for _state: + SR_NONE
162    #define SR_SUSPENDED          (0x20)
163  public:
164    SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
165
166    int suspend_action() const     { return _suspend_action; }
167    void set_suspend_action(int x) { _suspend_action = x;    }
168
169    // atomic updates for _state
170    void set_suspended()           {
171      jint temp, temp2;
172      do {
173        temp = _state;
174        temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp);
175      } while (temp2 != temp);
176    }
177    void clear_suspended()        {
178      jint temp, temp2;
179      do {
180        temp = _state;
181        temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp);
182      } while (temp2 != temp);
183    }
184    bool is_suspended()            { return _state & SR_SUSPENDED;       }
185
186    #undef SR_SUSPENDED
187  };
188
189private:
190  typedef int (*sched_getcpu_func_t)(void);
191  typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
192  typedef int (*numa_max_node_func_t)(void);
193  typedef int (*numa_available_func_t)(void);
194  typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
195  typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
196
197  static sched_getcpu_func_t _sched_getcpu;
198  static numa_node_to_cpus_func_t _numa_node_to_cpus;
199  static numa_max_node_func_t _numa_max_node;
200  static numa_available_func_t _numa_available;
201  static numa_tonode_memory_func_t _numa_tonode_memory;
202  static numa_interleave_memory_func_t _numa_interleave_memory;
203  static unsigned long* _numa_all_nodes;
204
205  static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
206  static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
207  static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
208  static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
209  static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
210  static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
211  static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
212public:
213  static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
214  static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
215    return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
216  }
217  static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
218  static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
219  static int numa_tonode_memory(void *start, size_t size, int node) {
220    return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
221  }
222  static void numa_interleave_memory(void *start, size_t size) {
223    if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
224      _numa_interleave_memory(start, size, _numa_all_nodes);
225    }
226  }
227  static int get_node_by_cpu(int cpu_id);
228};
229
230
231class PlatformEvent : public CHeapObj<mtInternal> {
232  private:
233    double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
234    volatile int _Event ;
235    volatile int _nParked ;
236    pthread_mutex_t _mutex  [1] ;
237    pthread_cond_t  _cond   [1] ;
238    double PostPad  [2] ;
239    Thread * _Assoc ;
240
241  public:       // TODO-FIXME: make dtor private
242    ~PlatformEvent() { guarantee (0, "invariant") ; }
243
244  public:
245    PlatformEvent() {
246      int status;
247      status = pthread_cond_init (_cond, NULL);
248      assert_status(status == 0, status, "cond_init");
249      status = pthread_mutex_init (_mutex, NULL);
250      assert_status(status == 0, status, "mutex_init");
251      _Event   = 0 ;
252      _nParked = 0 ;
253      _Assoc   = NULL ;
254    }
255
256    // Use caution with reset() and fired() -- they may require MEMBARs
257    void reset() { _Event = 0 ; }
258    int  fired() { return _Event; }
259    void park () ;
260    void unpark () ;
261    int  TryPark () ;
262    int  park (jlong millis) ;
263    void SetAssociation (Thread * a) { _Assoc = a ; }
264} ;
265
266class PlatformParker : public CHeapObj<mtInternal> {
267  protected:
268    pthread_mutex_t _mutex [1] ;
269    pthread_cond_t  _cond  [1] ;
270
271  public:       // TODO-FIXME: make dtor private
272    ~PlatformParker() { guarantee (0, "invariant") ; }
273
274  public:
275    PlatformParker() {
276      int status;
277      status = pthread_cond_init (_cond, NULL);
278      assert_status(status == 0, status, "cond_init");
279      status = pthread_mutex_init (_mutex, NULL);
280      assert_status(status == 0, status, "mutex_init");
281    }
282} ;
283
284#endif // OS_BSD_VM_OS_BSD_HPP
285