os_linux.hpp revision 9433:149cc1f9f1aa
1253883Ssjg/*
2236769Sobrien * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
3236769Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4236769Sobrien *
5236769Sobrien * This code is free software; you can redistribute it and/or modify it
6236769Sobrien * under the terms of the GNU General Public License version 2 only, as
7236769Sobrien * published by the Free Software Foundation.
8236769Sobrien *
9236769Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
10236769Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11236769Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12236769Sobrien * version 2 for more details (a copy is included in the LICENSE file that
13236769Sobrien * accompanied this code).
14236769Sobrien *
15236769Sobrien * You should have received a copy of the GNU General Public License version
16236769Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
17236769Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18236769Sobrien *
19236769Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20236769Sobrien * or visit www.oracle.com if you need additional information or have any
21236769Sobrien * questions.
22236769Sobrien *
23236769Sobrien */
24236769Sobrien
25236769Sobrien#ifndef OS_LINUX_VM_OS_LINUX_HPP
26236769Sobrien#define OS_LINUX_VM_OS_LINUX_HPP
27236769Sobrien
28236769Sobrien// Linux_OS defines the interface to Linux operating systems
29236769Sobrien
30236769Sobrien// Information about the protection of the page at address '0' on this os.
31236769Sobrienstatic bool zero_page_read_protected() { return true; }
32236769Sobrien
33236769Sobrienclass Linux {
34236769Sobrien  friend class os;
35236769Sobrien  friend class TestReserveMemorySpecial;
36236769Sobrien
37236769Sobrien  static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
38236769Sobrien                                        // __sigaction(), signal() is loaded
39236769Sobrien  static struct sigaction *(*get_signal_action)(int);
40236769Sobrien  static struct sigaction *get_preinstalled_handler(int);
41236769Sobrien  static void save_preinstalled_handler(int, struct sigaction&);
42236769Sobrien
43236769Sobrien  static void check_signal_handler(int sig);
44236769Sobrien
45236769Sobrien  static int (*_clock_gettime)(clockid_t, struct timespec *);
46236769Sobrien  static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
47236769Sobrien  static int (*_pthread_setname_np)(pthread_t, const char*);
48236769Sobrien
49236769Sobrien  static address   _initial_thread_stack_bottom;
50236769Sobrien  static uintptr_t _initial_thread_stack_size;
51236769Sobrien
52236769Sobrien  static const char *_glibc_version;
53236769Sobrien  static const char *_libpthread_version;
54236769Sobrien
55236769Sobrien  static bool _supports_fast_thread_cpu_time;
56236769Sobrien
57236769Sobrien  static GrowableArray<int>* _cpu_to_node;
58236769Sobrien
59236769Sobrien protected:
60236769Sobrien
61236769Sobrien  static julong _physical_memory;
62236769Sobrien  static pthread_t _main_thread;
63236769Sobrien  static Mutex* _createThread_lock;
64236769Sobrien  static int _page_size;
65236769Sobrien  static const int _vm_default_page_size;
66236769Sobrien
67236769Sobrien  static julong available_memory();
68236769Sobrien  static julong physical_memory() { return _physical_memory; }
69236769Sobrien  static void initialize_system_info();
70236769Sobrien
71236769Sobrien  static int commit_memory_impl(char* addr, size_t bytes, bool exec);
72253883Ssjg  static int commit_memory_impl(char* addr, size_t bytes,
73236769Sobrien                                size_t alignment_hint, bool exec);
74236769Sobrien
75236769Sobrien  static void set_glibc_version(const char *s)      { _glibc_version = s; }
76236769Sobrien  static void set_libpthread_version(const char *s) { _libpthread_version = s; }
77236769Sobrien
78236769Sobrien  static void rebuild_cpu_to_node_map();
79236769Sobrien  static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
80236769Sobrien
81236769Sobrien  static size_t find_large_page_size();
82236769Sobrien  static size_t setup_large_page_size();
83236769Sobrien
84253883Ssjg  static bool setup_large_page_type(size_t page_size);
85236769Sobrien  static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size);
86236769Sobrien  static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
87236769Sobrien
88236769Sobrien  static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec);
89236769Sobrien  static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec);
90236769Sobrien  static char* reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec);
91236769Sobrien  static char* reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec);
92236769Sobrien
93236769Sobrien  static bool release_memory_special_impl(char* base, size_t bytes);
94236769Sobrien  static bool release_memory_special_shm(char* base, size_t bytes);
95236769Sobrien  static bool release_memory_special_huge_tlbfs(char* base, size_t bytes);
96236769Sobrien
97236769Sobrien  static void print_full_memory_info(outputStream* st);
98236769Sobrien  static void print_distro_info(outputStream* st);
99236769Sobrien  static void print_libversion_info(outputStream* st);
100236769Sobrien
101236769Sobrien public:
102236769Sobrien  static bool _stack_is_executable;
103236769Sobrien  static void *dlopen_helper(const char *name, char *ebuf, int ebuflen);
104236769Sobrien  static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen);
105236769Sobrien
106236769Sobrien  static void init_thread_fpu_state();
107236769Sobrien  static int  get_fpu_control_word();
108236769Sobrien  static void set_fpu_control_word(int fpu_control);
109236769Sobrien  static pthread_t main_thread(void)                                { return _main_thread; }
110236769Sobrien  // returns kernel thread id (similar to LWP id on Solaris), which can be
111236769Sobrien  // used to access /proc
112236769Sobrien  static pid_t gettid();
113236769Sobrien  static void set_createThread_lock(Mutex* lk)                      { _createThread_lock = lk; }
114236769Sobrien  static Mutex* createThread_lock(void)                             { return _createThread_lock; }
115236769Sobrien  static void hotspot_sigmask(Thread* thread);
116236769Sobrien
117236769Sobrien  static address   initial_thread_stack_bottom(void)                { return _initial_thread_stack_bottom; }
118236769Sobrien  static uintptr_t initial_thread_stack_size(void)                  { return _initial_thread_stack_size; }
119236769Sobrien  static bool is_initial_thread(void);
120236769Sobrien
121236769Sobrien  static int page_size(void)                                        { return _page_size; }
122236769Sobrien  static void set_page_size(int val)                                { _page_size = val; }
123236769Sobrien
124236769Sobrien  static int vm_default_page_size(void)                             { return _vm_default_page_size; }
125236769Sobrien
126253883Ssjg  static address   ucontext_get_pc(ucontext_t* uc);
127236769Sobrien  static void ucontext_set_pc(ucontext_t* uc, address pc);
128236769Sobrien  static intptr_t* ucontext_get_sp(ucontext_t* uc);
129236769Sobrien  static intptr_t* ucontext_get_fp(ucontext_t* uc);
130236769Sobrien
131253883Ssjg  // For Analyzer Forte AsyncGetCallTrace profiling support:
132236769Sobrien  //
133236769Sobrien  // This interface should be declared in os_linux_i486.hpp, but
134236769Sobrien  // that file provides extensions to the os class and not the
135236769Sobrien  // Linux class.
136236769Sobrien  static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
137236769Sobrien                                              intptr_t** ret_sp, intptr_t** ret_fp);
138236769Sobrien
139236769Sobrien  // This boolean allows users to forward their own non-matching signals
140236769Sobrien  // to JVM_handle_linux_signal, harmlessly.
141236769Sobrien  static bool signal_handlers_are_installed;
142236769Sobrien
143236769Sobrien  static int get_our_sigflags(int);
144236769Sobrien  static void set_our_sigflags(int, int);
145236769Sobrien  static void signal_sets_init();
146236769Sobrien  static void install_signal_handlers();
147236769Sobrien  static void set_signal_handler(int, bool);
148236769Sobrien  static bool is_sig_ignored(int sig);
149236769Sobrien
150236769Sobrien  static sigset_t* unblocked_signals();
151236769Sobrien  static sigset_t* vm_signals();
152236769Sobrien  static sigset_t* allowdebug_blocked_signals();
153236769Sobrien
154236769Sobrien  // For signal-chaining
155236769Sobrien  static struct sigaction *get_chained_signal_action(int sig);
156236769Sobrien  static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
157236769Sobrien
158236769Sobrien  // GNU libc and libpthread version strings
159236769Sobrien  static const char *glibc_version()          { return _glibc_version; }
160236769Sobrien  static const char *libpthread_version()     { return _libpthread_version; }
161240330Smarcel
162236769Sobrien  static void libpthread_init();
163236769Sobrien  static bool libnuma_init();
164236769Sobrien  static void* libnuma_dlsym(void* handle, const char* name);
165236769Sobrien  // Minimum stack size a thread can be created with (allowing
166236769Sobrien  // the VM to completely create the thread and enter user code)
167253883Ssjg  static size_t min_stack_allowed;
168236769Sobrien
169236769Sobrien  // Return default stack size or guard size for the specified thread type
170236769Sobrien  static size_t default_stack_size(os::ThreadType thr_type);
171236769Sobrien  static size_t default_guard_size(os::ThreadType thr_type);
172236769Sobrien
173236769Sobrien  static void capture_initial_stack(size_t max_size);
174236769Sobrien
175236769Sobrien  // Stack overflow handling
176236769Sobrien  static bool manually_expand_stack(JavaThread * t, address addr);
177236769Sobrien  static int max_register_window_saves_before_flushing();
178236769Sobrien
179236769Sobrien  // Real-time clock functions
180236769Sobrien  static void clock_init(void);
181237578Sobrien
182236769Sobrien  // fast POSIX clocks support
183236769Sobrien  static void fast_thread_clock_init(void);
184236769Sobrien
185236769Sobrien  static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
186236769Sobrien    return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
187236769Sobrien  }
188236769Sobrien
189253883Ssjg  static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
190236769Sobrien    return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
191236769Sobrien  }
192236769Sobrien
193236769Sobrien  static bool supports_fast_thread_cpu_time() {
194236769Sobrien    return _supports_fast_thread_cpu_time;
195236769Sobrien  }
196236769Sobrien
197236769Sobrien  static jlong fast_thread_cpu_time(clockid_t clockid);
198236769Sobrien
199236769Sobrien  // pthread_cond clock suppport
200236769Sobrien private:
201236769Sobrien  static pthread_condattr_t _condattr[1];
202236769Sobrien
203236769Sobrien public:
204253883Ssjg  static pthread_condattr_t* condAttr() { return _condattr; }
205253883Ssjg
206253883Ssjg  // Stack repair handling
207253883Ssjg
208253883Ssjg  // none present
209253883Ssjg
210253883Ssjg private:
211253883Ssjg  typedef int (*sched_getcpu_func_t)(void);
212253883Ssjg  typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
213253883Ssjg  typedef int (*numa_max_node_func_t)(void);
214253883Ssjg  typedef int (*numa_available_func_t)(void);
215253883Ssjg  typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
216253883Ssjg  typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
217253883Ssjg  typedef void (*numa_set_bind_policy_func_t)(int policy);
218253883Ssjg
219253883Ssjg  static sched_getcpu_func_t _sched_getcpu;
220253883Ssjg  static numa_node_to_cpus_func_t _numa_node_to_cpus;
221253883Ssjg  static numa_max_node_func_t _numa_max_node;
222253883Ssjg  static numa_available_func_t _numa_available;
223253883Ssjg  static numa_tonode_memory_func_t _numa_tonode_memory;
224253883Ssjg  static numa_interleave_memory_func_t _numa_interleave_memory;
225253883Ssjg  static numa_set_bind_policy_func_t _numa_set_bind_policy;
226253883Ssjg  static unsigned long* _numa_all_nodes;
227253883Ssjg
228253883Ssjg  static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
229253883Ssjg  static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
230253883Ssjg  static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
231253883Ssjg  static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
232253883Ssjg  static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
233253883Ssjg  static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
234253883Ssjg  static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
235253883Ssjg  static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
236236769Sobrien  static int sched_getcpu_syscall(void);
237236769Sobrien public:
238236769Sobrien  static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
239236769Sobrien  static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
240236769Sobrien    return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
241236769Sobrien  }
242236769Sobrien  static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
243236769Sobrien  static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
244236769Sobrien  static int numa_tonode_memory(void *start, size_t size, int node) {
245236769Sobrien    return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
246236769Sobrien  }
247236769Sobrien  static void numa_interleave_memory(void *start, size_t size) {
248236769Sobrien    if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
249236769Sobrien      _numa_interleave_memory(start, size, _numa_all_nodes);
250236769Sobrien    }
251236769Sobrien  }
252236769Sobrien  static void numa_set_bind_policy(int policy) {
253236769Sobrien    if (_numa_set_bind_policy != NULL) {
254236769Sobrien      _numa_set_bind_policy(policy);
255236769Sobrien    }
256236769Sobrien  }
257236769Sobrien  static int get_node_by_cpu(int cpu_id);
258236769Sobrien};
259236769Sobrien
260236769Sobrien
261236769Sobrienclass PlatformEvent : public CHeapObj<mtInternal> {
262236769Sobrien private:
263236769Sobrien  double CachePad[4];   // increase odds that _mutex is sole occupant of cache line
264236769Sobrien  volatile int _Event;
265236769Sobrien  volatile int _nParked;
266236769Sobrien  pthread_mutex_t _mutex[1];
267236769Sobrien  pthread_cond_t  _cond[1];
268236769Sobrien  double PostPad[2];
269236769Sobrien  Thread * _Assoc;
270236769Sobrien
271236769Sobrien public:       // TODO-FIXME: make dtor private
272236769Sobrien  ~PlatformEvent() { guarantee(0, "invariant"); }
273236769Sobrien
274236769Sobrien public:
275236769Sobrien  PlatformEvent() {
276236769Sobrien    int status;
277236769Sobrien    status = pthread_cond_init(_cond, os::Linux::condAttr());
278236769Sobrien    assert_status(status == 0, status, "cond_init");
279236769Sobrien    status = pthread_mutex_init(_mutex, NULL);
280236769Sobrien    assert_status(status == 0, status, "mutex_init");
281236769Sobrien    _Event   = 0;
282236769Sobrien    _nParked = 0;
283236769Sobrien    _Assoc   = NULL;
284236769Sobrien  }
285236769Sobrien
286236769Sobrien  // Use caution with reset() and fired() -- they may require MEMBARs
287236769Sobrien  void reset() { _Event = 0; }
288236769Sobrien  int  fired() { return _Event; }
289236769Sobrien  void park();
290236769Sobrien  void unpark();
291236769Sobrien  int  park(jlong millis); // relative timed-wait only
292236769Sobrien  void SetAssociation(Thread * a) { _Assoc = a; }
293236769Sobrien};
294236769Sobrien
295236769Sobrienclass PlatformParker : public CHeapObj<mtInternal> {
296236769Sobrien protected:
297236769Sobrien  enum {
298236769Sobrien    REL_INDEX = 0,
299236769Sobrien    ABS_INDEX = 1
300236769Sobrien  };
301236769Sobrien  int _cur_index;  // which cond is in use: -1, 0, 1
302236769Sobrien  pthread_mutex_t _mutex[1];
303236769Sobrien  pthread_cond_t  _cond[2]; // one for relative times and one for abs.
304236769Sobrien
305240330Smarcel public:       // TODO-FIXME: make dtor private
306240330Smarcel  ~PlatformParker() { guarantee(0, "invariant"); }
307240330Smarcel
308236769Sobrien public:
309236769Sobrien  PlatformParker() {
310236769Sobrien    int status;
311236769Sobrien    status = pthread_cond_init(&_cond[REL_INDEX], os::Linux::condAttr());
312236769Sobrien    assert_status(status == 0, status, "cond_init rel");
313236769Sobrien    status = pthread_cond_init(&_cond[ABS_INDEX], NULL);
314236769Sobrien    assert_status(status == 0, status, "cond_init abs");
315236769Sobrien    status = pthread_mutex_init(_mutex, NULL);
316236769Sobrien    assert_status(status == 0, status, "mutex_init");
317236769Sobrien    _cur_index = -1; // mark as unused
318236769Sobrien  }
319236769Sobrien};
320236769Sobrien
321236769Sobrien#endif // OS_LINUX_VM_OS_LINUX_HPP
322236769Sobrien