1
|
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 |
// Linux_OS defines the interface to Linux operating systems
|
|
26 |
|
|
27 |
/* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
|
|
28 |
typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
|
|
29 |
|
|
30 |
class Linux {
|
|
31 |
friend class os;
|
|
32 |
|
|
33 |
// For signal-chaining
|
|
34 |
#define MAXSIGNUM 32
|
|
35 |
static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
|
|
36 |
static unsigned int sigs; // mask of signals that have
|
|
37 |
// preinstalled signal handlers
|
|
38 |
static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
|
|
39 |
// __sigaction(), signal() is loaded
|
|
40 |
static struct sigaction *(*get_signal_action)(int);
|
|
41 |
static struct sigaction *get_preinstalled_handler(int);
|
|
42 |
static void save_preinstalled_handler(int, struct sigaction&);
|
|
43 |
|
|
44 |
static void check_signal_handler(int sig);
|
|
45 |
|
|
46 |
// For signal flags diagnostics
|
|
47 |
static int sigflags[MAXSIGNUM];
|
|
48 |
|
|
49 |
static int (*_clock_gettime)(clockid_t, struct timespec *);
|
|
50 |
static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
|
|
51 |
|
|
52 |
static address _initial_thread_stack_bottom;
|
|
53 |
static uintptr_t _initial_thread_stack_size;
|
|
54 |
|
|
55 |
static char *_glibc_version;
|
|
56 |
static char *_libpthread_version;
|
|
57 |
|
|
58 |
static bool _is_floating_stack;
|
|
59 |
static bool _is_NPTL;
|
|
60 |
static bool _supports_fast_thread_cpu_time;
|
|
61 |
|
|
62 |
protected:
|
|
63 |
|
|
64 |
static julong _physical_memory;
|
|
65 |
static pthread_t _main_thread;
|
|
66 |
static Mutex* _createThread_lock;
|
|
67 |
static int _page_size;
|
|
68 |
|
|
69 |
static julong available_memory();
|
|
70 |
static julong physical_memory() { return _physical_memory; }
|
|
71 |
static void initialize_system_info();
|
|
72 |
|
|
73 |
static void set_glibc_version(char *s) { _glibc_version = s; }
|
|
74 |
static void set_libpthread_version(char *s) { _libpthread_version = s; }
|
|
75 |
|
|
76 |
static bool supports_variable_stack_size();
|
|
77 |
|
|
78 |
static void set_is_NPTL() { _is_NPTL = true; }
|
|
79 |
static void set_is_LinuxThreads() { _is_NPTL = false; }
|
|
80 |
static void set_is_floating_stack() { _is_floating_stack = true; }
|
|
81 |
|
|
82 |
public:
|
|
83 |
|
|
84 |
static void init_thread_fpu_state();
|
|
85 |
static int get_fpu_control_word();
|
|
86 |
static void set_fpu_control_word(int fpu_control);
|
|
87 |
static pthread_t main_thread(void) { return _main_thread; }
|
|
88 |
// returns kernel thread id (similar to LWP id on Solaris), which can be
|
|
89 |
// used to access /proc
|
|
90 |
static pid_t gettid();
|
|
91 |
static void set_createThread_lock(Mutex* lk) { _createThread_lock = lk; }
|
|
92 |
static Mutex* createThread_lock(void) { return _createThread_lock; }
|
|
93 |
static void hotspot_sigmask(Thread* thread);
|
|
94 |
|
|
95 |
static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; }
|
|
96 |
static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; }
|
|
97 |
static bool is_initial_thread(void);
|
|
98 |
|
|
99 |
static int page_size(void) { return _page_size; }
|
|
100 |
static void set_page_size(int val) { _page_size = val; }
|
|
101 |
|
|
102 |
static address ucontext_get_pc(ucontext_t* uc);
|
|
103 |
static intptr_t* ucontext_get_sp(ucontext_t* uc);
|
|
104 |
static intptr_t* ucontext_get_fp(ucontext_t* uc);
|
|
105 |
|
|
106 |
// For Analyzer Forte AsyncGetCallTrace profiling support:
|
|
107 |
//
|
|
108 |
// This interface should be declared in os_linux_i486.hpp, but
|
|
109 |
// that file provides extensions to the os class and not the
|
|
110 |
// Linux class.
|
|
111 |
static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
|
|
112 |
intptr_t** ret_sp, intptr_t** ret_fp);
|
|
113 |
|
|
114 |
// This boolean allows users to forward their own non-matching signals
|
|
115 |
// to JVM_handle_linux_signal, harmlessly.
|
|
116 |
static bool signal_handlers_are_installed;
|
|
117 |
|
|
118 |
static int get_our_sigflags(int);
|
|
119 |
static void set_our_sigflags(int, int);
|
|
120 |
static void signal_sets_init();
|
|
121 |
static void install_signal_handlers();
|
|
122 |
static void set_signal_handler(int, bool);
|
|
123 |
static bool is_sig_ignored(int sig);
|
|
124 |
|
|
125 |
static sigset_t* unblocked_signals();
|
|
126 |
static sigset_t* vm_signals();
|
|
127 |
static sigset_t* allowdebug_blocked_signals();
|
|
128 |
|
|
129 |
// For signal-chaining
|
|
130 |
static struct sigaction *get_chained_signal_action(int sig);
|
|
131 |
static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
|
|
132 |
|
|
133 |
// GNU libc and libpthread version strings
|
|
134 |
static char *glibc_version() { return _glibc_version; }
|
|
135 |
static char *libpthread_version() { return _libpthread_version; }
|
|
136 |
|
|
137 |
// NPTL or LinuxThreads?
|
|
138 |
static bool is_LinuxThreads() { return !_is_NPTL; }
|
|
139 |
static bool is_NPTL() { return _is_NPTL; }
|
|
140 |
|
|
141 |
// NPTL is always floating stack. LinuxThreads could be using floating
|
|
142 |
// stack or fixed stack.
|
|
143 |
static bool is_floating_stack() { return _is_floating_stack; }
|
|
144 |
|
|
145 |
static void libpthread_init();
|
|
146 |
|
|
147 |
// Minimum stack size a thread can be created with (allowing
|
|
148 |
// the VM to completely create the thread and enter user code)
|
|
149 |
static size_t min_stack_allowed;
|
|
150 |
|
|
151 |
// Return default stack size or guard size for the specified thread type
|
|
152 |
static size_t default_stack_size(os::ThreadType thr_type);
|
|
153 |
static size_t default_guard_size(os::ThreadType thr_type);
|
|
154 |
|
|
155 |
static void capture_initial_stack(size_t max_size);
|
|
156 |
|
|
157 |
// Stack overflow handling
|
|
158 |
static bool manually_expand_stack(JavaThread * t, address addr);
|
|
159 |
static int max_register_window_saves_before_flushing();
|
|
160 |
|
|
161 |
// Real-time clock functions
|
|
162 |
static void clock_init(void);
|
|
163 |
|
|
164 |
// fast POSIX clocks support
|
|
165 |
static void fast_thread_clock_init(void);
|
|
166 |
|
|
167 |
static bool supports_monotonic_clock() {
|
|
168 |
return _clock_gettime != NULL;
|
|
169 |
}
|
|
170 |
|
|
171 |
static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
|
|
172 |
return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
|
|
173 |
}
|
|
174 |
|
|
175 |
static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
|
|
176 |
return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
|
|
177 |
}
|
|
178 |
|
|
179 |
static bool supports_fast_thread_cpu_time() {
|
|
180 |
return _supports_fast_thread_cpu_time;
|
|
181 |
}
|
|
182 |
|
|
183 |
static jlong fast_thread_cpu_time(clockid_t clockid);
|
|
184 |
|
|
185 |
// Stack repair handling
|
|
186 |
|
|
187 |
// none present
|
|
188 |
|
|
189 |
// LinuxThreads work-around for 6292965
|
|
190 |
static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
|
|
191 |
|
|
192 |
|
|
193 |
// Linux suspend/resume support - this helper is a shadow of its former
|
|
194 |
// self now that low-level suspension is barely used, and old workarounds
|
|
195 |
// for LinuxThreads are no longer needed.
|
|
196 |
class SuspendResume {
|
|
197 |
private:
|
|
198 |
volatile int _suspend_action;
|
|
199 |
// values for suspend_action:
|
|
200 |
#define SR_NONE (0x00)
|
|
201 |
#define SR_SUSPEND (0x01) // suspend request
|
|
202 |
#define SR_CONTINUE (0x02) // resume request
|
|
203 |
|
|
204 |
volatile jint _state;
|
|
205 |
// values for _state: + SR_NONE
|
|
206 |
#define SR_SUSPENDED (0x20)
|
|
207 |
public:
|
|
208 |
SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
|
|
209 |
|
|
210 |
int suspend_action() const { return _suspend_action; }
|
|
211 |
void set_suspend_action(int x) { _suspend_action = x; }
|
|
212 |
|
|
213 |
// atomic updates for _state
|
|
214 |
void set_suspended() {
|
|
215 |
jint temp, temp2;
|
|
216 |
do {
|
|
217 |
temp = _state;
|
|
218 |
temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp);
|
|
219 |
} while (temp2 != temp);
|
|
220 |
}
|
|
221 |
void clear_suspended() {
|
|
222 |
jint temp, temp2;
|
|
223 |
do {
|
|
224 |
temp = _state;
|
|
225 |
temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp);
|
|
226 |
} while (temp2 != temp);
|
|
227 |
}
|
|
228 |
bool is_suspended() { return _state & SR_SUSPENDED; }
|
|
229 |
|
|
230 |
#undef SR_SUSPENDED
|
|
231 |
};
|
|
232 |
};
|
|
233 |
|
|
234 |
|
|
235 |
class PlatformEvent : public CHeapObj {
|
|
236 |
private:
|
|
237 |
double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line
|
|
238 |
volatile int _Event ;
|
|
239 |
volatile int _nParked ;
|
|
240 |
pthread_mutex_t _mutex [1] ;
|
|
241 |
pthread_cond_t _cond [1] ;
|
|
242 |
double PostPad [2] ;
|
|
243 |
Thread * _Assoc ;
|
|
244 |
|
|
245 |
public: // TODO-FIXME: make dtor private
|
|
246 |
~PlatformEvent() { guarantee (0, "invariant") ; }
|
|
247 |
|
|
248 |
public:
|
|
249 |
PlatformEvent() {
|
|
250 |
int status;
|
|
251 |
status = pthread_cond_init (_cond, NULL);
|
|
252 |
assert_status(status == 0, status, "cond_init");
|
|
253 |
status = pthread_mutex_init (_mutex, NULL);
|
|
254 |
assert_status(status == 0, status, "mutex_init");
|
|
255 |
_Event = 0 ;
|
|
256 |
_nParked = 0 ;
|
|
257 |
_Assoc = NULL ;
|
|
258 |
}
|
|
259 |
|
|
260 |
// Use caution with reset() and fired() -- they may require MEMBARs
|
|
261 |
void reset() { _Event = 0 ; }
|
|
262 |
int fired() { return _Event; }
|
|
263 |
void park () ;
|
|
264 |
void unpark () ;
|
|
265 |
int TryPark () ;
|
|
266 |
int park (jlong millis) ;
|
|
267 |
void SetAssociation (Thread * a) { _Assoc = a ; }
|
|
268 |
} ;
|
|
269 |
|
|
270 |
class PlatformParker : public CHeapObj {
|
|
271 |
protected:
|
|
272 |
pthread_mutex_t _mutex [1] ;
|
|
273 |
pthread_cond_t _cond [1] ;
|
|
274 |
|
|
275 |
public: // TODO-FIXME: make dtor private
|
|
276 |
~PlatformParker() { guarantee (0, "invariant") ; }
|
|
277 |
|
|
278 |
public:
|
|
279 |
PlatformParker() {
|
|
280 |
int status;
|
|
281 |
status = pthread_cond_init (_cond, NULL);
|
|
282 |
assert_status(status == 0, status, "cond_init");
|
|
283 |
status = pthread_mutex_init (_mutex, NULL);
|
|
284 |
assert_status(status == 0, status, "mutex_init");
|
|
285 |
}
|
|
286 |
} ;
|