Sat, 19 Jul 2008 17:38:22 -0400
6716785: implicit null checks not triggering with CompressedOops
Summary: allocate alignment-sized page(s) below java heap so that memory accesses at heap_base+1page give signal and cause an implicit null check
Reviewed-by: kvn, jmasa, phh, jcoomes
1 /*
2 * Copyright 1999-2008 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 */
25 // do not include precompiled header file
26 # include "incls/_os_linux.cpp.incl"
28 // put OS-includes here
29 # include <sys/types.h>
30 # include <sys/mman.h>
31 # include <pthread.h>
32 # include <signal.h>
33 # include <errno.h>
34 # include <dlfcn.h>
35 # include <stdio.h>
36 # include <unistd.h>
37 # include <sys/resource.h>
38 # include <pthread.h>
39 # include <sys/stat.h>
40 # include <sys/time.h>
41 # include <sys/times.h>
42 # include <sys/utsname.h>
43 # include <sys/socket.h>
44 # include <sys/wait.h>
45 # include <pwd.h>
46 # include <poll.h>
47 # include <semaphore.h>
48 # include <fcntl.h>
49 # include <string.h>
50 # include <syscall.h>
51 # include <sys/sysinfo.h>
52 # include <gnu/libc-version.h>
53 # include <sys/ipc.h>
54 # include <sys/shm.h>
55 # include <link.h>
57 #define MAX_PATH (2 * K)
59 // for timer info max values which include all bits
60 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
61 #define SEC_IN_NANOSECS 1000000000LL
63 ////////////////////////////////////////////////////////////////////////////////
64 // global variables
65 julong os::Linux::_physical_memory = 0;
67 address os::Linux::_initial_thread_stack_bottom = NULL;
68 uintptr_t os::Linux::_initial_thread_stack_size = 0;
70 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;
71 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
72 Mutex* os::Linux::_createThread_lock = NULL;
73 pthread_t os::Linux::_main_thread;
74 int os::Linux::_page_size = -1;
75 bool os::Linux::_is_floating_stack = false;
76 bool os::Linux::_is_NPTL = false;
77 bool os::Linux::_supports_fast_thread_cpu_time = false;
78 const char * os::Linux::_glibc_version = NULL;
79 const char * os::Linux::_libpthread_version = NULL;
81 static jlong initial_time_count=0;
83 static int clock_tics_per_sec = 100;
85 // For diagnostics to print a message once. see run_periodic_checks
86 static sigset_t check_signal_done;
87 static bool check_signals = true;;
89 static pid_t _initial_pid = 0;
91 /* Signal number used to suspend/resume a thread */
93 /* do not use any signal number less than SIGSEGV, see 4355769 */
94 static int SR_signum = SIGUSR2;
95 sigset_t SR_sigset;
97 ////////////////////////////////////////////////////////////////////////////////
98 // utility functions
100 static int SR_initialize();
101 static int SR_finalize();
103 julong os::available_memory() {
104 return Linux::available_memory();
105 }
107 julong os::Linux::available_memory() {
108 // values in struct sysinfo are "unsigned long"
109 struct sysinfo si;
110 sysinfo(&si);
112 return (julong)si.freeram * si.mem_unit;
113 }
115 julong os::physical_memory() {
116 return Linux::physical_memory();
117 }
119 julong os::allocatable_physical_memory(julong size) {
120 #ifdef _LP64
121 return size;
122 #else
123 julong result = MIN2(size, (julong)3800*M);
124 if (!is_allocatable(result)) {
125 // See comments under solaris for alignment considerations
126 julong reasonable_size = (julong)2*G - 2 * os::vm_page_size();
127 result = MIN2(size, reasonable_size);
128 }
129 return result;
130 #endif // _LP64
131 }
133 ////////////////////////////////////////////////////////////////////////////////
134 // environment support
136 bool os::getenv(const char* name, char* buf, int len) {
137 const char* val = ::getenv(name);
138 if (val != NULL && strlen(val) < (size_t)len) {
139 strcpy(buf, val);
140 return true;
141 }
142 if (len > 0) buf[0] = 0; // return a null string
143 return false;
144 }
147 // Return true if user is running as root.
149 bool os::have_special_privileges() {
150 static bool init = false;
151 static bool privileges = false;
152 if (!init) {
153 privileges = (getuid() != geteuid()) || (getgid() != getegid());
154 init = true;
155 }
156 return privileges;
157 }
160 #ifndef SYS_gettid
161 // i386: 224, ia64: 1105, amd64: 186, sparc 143
162 #ifdef __ia64__
163 #define SYS_gettid 1105
164 #elif __i386__
165 #define SYS_gettid 224
166 #elif __amd64__
167 #define SYS_gettid 186
168 #elif __sparc__
169 #define SYS_gettid 143
170 #else
171 #error define gettid for the arch
172 #endif
173 #endif
175 // Cpu architecture string
176 #if defined(IA64)
177 static char cpu_arch[] = "ia64";
178 #elif defined(IA32)
179 static char cpu_arch[] = "i386";
180 #elif defined(AMD64)
181 static char cpu_arch[] = "amd64";
182 #elif defined(SPARC)
183 # ifdef _LP64
184 static char cpu_arch[] = "sparcv9";
185 # else
186 static char cpu_arch[] = "sparc";
187 # endif
188 #else
189 #error Add appropriate cpu_arch setting
190 #endif
193 // pid_t gettid()
194 //
195 // Returns the kernel thread id of the currently running thread. Kernel
196 // thread id is used to access /proc.
197 //
198 // (Note that getpid() on LinuxThreads returns kernel thread id too; but
199 // on NPTL, it returns the same pid for all threads, as required by POSIX.)
200 //
201 pid_t os::Linux::gettid() {
202 int rslt = syscall(SYS_gettid);
203 if (rslt == -1) {
204 // old kernel, no NPTL support
205 return getpid();
206 } else {
207 return (pid_t)rslt;
208 }
209 }
211 // Most versions of linux have a bug where the number of processors are
212 // determined by looking at the /proc file system. In a chroot environment,
213 // the system call returns 1. This causes the VM to act as if it is
214 // a single processor and elide locking (see is_MP() call).
215 static bool unsafe_chroot_detected = false;
216 static const char *unstable_chroot_error = "/proc file system not found.\n"
217 "Java may be unstable running multithreaded in a chroot "
218 "environment on Linux when /proc filesystem is not mounted.";
220 void os::Linux::initialize_system_info() {
221 _processor_count = sysconf(_SC_NPROCESSORS_CONF);
222 if (_processor_count == 1) {
223 pid_t pid = os::Linux::gettid();
224 char fname[32];
225 jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
226 FILE *fp = fopen(fname, "r");
227 if (fp == NULL) {
228 unsafe_chroot_detected = true;
229 } else {
230 fclose(fp);
231 }
232 }
233 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
234 assert(_processor_count > 0, "linux error");
235 }
237 void os::init_system_properties_values() {
238 // char arch[12];
239 // sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
241 // The next steps are taken in the product version:
242 //
243 // Obtain the JAVA_HOME value from the location of libjvm[_g].so.
244 // This library should be located at:
245 // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm[_g].so.
246 //
247 // If "/jre/lib/" appears at the right place in the path, then we
248 // assume libjvm[_g].so is installed in a JDK and we use this path.
249 //
250 // Otherwise exit with message: "Could not create the Java virtual machine."
251 //
252 // The following extra steps are taken in the debugging version:
253 //
254 // If "/jre/lib/" does NOT appear at the right place in the path
255 // instead of exit check for $JAVA_HOME environment variable.
256 //
257 // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
258 // then we append a fake suffix "hotspot/libjvm[_g].so" to this path so
259 // it looks like libjvm[_g].so is installed there
260 // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm[_g].so.
261 //
262 // Otherwise exit.
263 //
264 // Important note: if the location of libjvm.so changes this
265 // code needs to be changed accordingly.
267 // The next few definitions allow the code to be verbatim:
268 #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n))
269 #define getenv(n) ::getenv(n)
271 /*
272 * See ld(1):
273 * The linker uses the following search paths to locate required
274 * shared libraries:
275 * 1: ...
276 * ...
277 * 7: The default directories, normally /lib and /usr/lib.
278 */
279 #define DEFAULT_LIBPATH "/lib:/usr/lib"
281 #define EXTENSIONS_DIR "/lib/ext"
282 #define ENDORSED_DIR "/lib/endorsed"
283 #define REG_DIR "/usr/java/packages"
285 {
286 /* sysclasspath, java_home, dll_dir */
287 {
288 char *home_path;
289 char *dll_path;
290 char *pslash;
291 char buf[MAXPATHLEN];
292 os::jvm_path(buf, sizeof(buf));
294 // Found the full path to libjvm.so.
295 // Now cut the path to <java_home>/jre if we can.
296 *(strrchr(buf, '/')) = '\0'; /* get rid of /libjvm.so */
297 pslash = strrchr(buf, '/');
298 if (pslash != NULL)
299 *pslash = '\0'; /* get rid of /{client|server|hotspot} */
300 dll_path = malloc(strlen(buf) + 1);
301 if (dll_path == NULL)
302 return;
303 strcpy(dll_path, buf);
304 Arguments::set_dll_dir(dll_path);
306 if (pslash != NULL) {
307 pslash = strrchr(buf, '/');
308 if (pslash != NULL) {
309 *pslash = '\0'; /* get rid of /<arch> */
310 pslash = strrchr(buf, '/');
311 if (pslash != NULL)
312 *pslash = '\0'; /* get rid of /lib */
313 }
314 }
316 home_path = malloc(strlen(buf) + 1);
317 if (home_path == NULL)
318 return;
319 strcpy(home_path, buf);
320 Arguments::set_java_home(home_path);
322 if (!set_boot_path('/', ':'))
323 return;
324 }
326 /*
327 * Where to look for native libraries
328 *
329 * Note: Due to a legacy implementation, most of the library path
330 * is set in the launcher. This was to accomodate linking restrictions
331 * on legacy Linux implementations (which are no longer supported).
332 * Eventually, all the library path setting will be done here.
333 *
334 * However, to prevent the proliferation of improperly built native
335 * libraries, the new path component /usr/java/packages is added here.
336 * Eventually, all the library path setting will be done here.
337 */
338 {
339 char *ld_library_path;
341 /*
342 * Construct the invariant part of ld_library_path. Note that the
343 * space for the colon and the trailing null are provided by the
344 * nulls included by the sizeof operator (so actually we allocate
345 * a byte more than necessary).
346 */
347 ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") +
348 strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH));
349 sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
351 /*
352 * Get the user setting of LD_LIBRARY_PATH, and prepended it. It
353 * should always exist (until the legacy problem cited above is
354 * addressed).
355 */
356 char *v = getenv("LD_LIBRARY_PATH");
357 if (v != NULL) {
358 char *t = ld_library_path;
359 /* That's +1 for the colon and +1 for the trailing '\0' */
360 ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1);
361 sprintf(ld_library_path, "%s:%s", v, t);
362 }
363 Arguments::set_library_path(ld_library_path);
364 }
366 /*
367 * Extensions directories.
368 *
369 * Note that the space for the colon and the trailing null are provided
370 * by the nulls included by the sizeof operator (so actually one byte more
371 * than necessary is allocated).
372 */
373 {
374 char *buf = malloc(strlen(Arguments::get_java_home()) +
375 sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR));
376 sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR,
377 Arguments::get_java_home());
378 Arguments::set_ext_dirs(buf);
379 }
381 /* Endorsed standards default directory. */
382 {
383 char * buf;
384 buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
385 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
386 Arguments::set_endorsed_dirs(buf);
387 }
388 }
390 #undef malloc
391 #undef getenv
392 #undef EXTENSIONS_DIR
393 #undef ENDORSED_DIR
395 // Done
396 return;
397 }
399 ////////////////////////////////////////////////////////////////////////////////
400 // breakpoint support
402 void os::breakpoint() {
403 BREAKPOINT;
404 }
406 extern "C" void breakpoint() {
407 // use debugger to set breakpoint here
408 }
410 ////////////////////////////////////////////////////////////////////////////////
411 // signal support
413 debug_only(static bool signal_sets_initialized = false);
414 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
416 bool os::Linux::is_sig_ignored(int sig) {
417 struct sigaction oact;
418 sigaction(sig, (struct sigaction*)NULL, &oact);
419 void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction)
420 : CAST_FROM_FN_PTR(void*, oact.sa_handler);
421 if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN))
422 return true;
423 else
424 return false;
425 }
427 void os::Linux::signal_sets_init() {
428 // Should also have an assertion stating we are still single-threaded.
429 assert(!signal_sets_initialized, "Already initialized");
430 // Fill in signals that are necessarily unblocked for all threads in
431 // the VM. Currently, we unblock the following signals:
432 // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden
433 // by -Xrs (=ReduceSignalUsage));
434 // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all
435 // other threads. The "ReduceSignalUsage" boolean tells us not to alter
436 // the dispositions or masks wrt these signals.
437 // Programs embedding the VM that want to use the above signals for their
438 // own purposes must, at this time, use the "-Xrs" option to prevent
439 // interference with shutdown hooks and BREAK_SIGNAL thread dumping.
440 // (See bug 4345157, and other related bugs).
441 // In reality, though, unblocking these signals is really a nop, since
442 // these signals are not blocked by default.
443 sigemptyset(&unblocked_sigs);
444 sigemptyset(&allowdebug_blocked_sigs);
445 sigaddset(&unblocked_sigs, SIGILL);
446 sigaddset(&unblocked_sigs, SIGSEGV);
447 sigaddset(&unblocked_sigs, SIGBUS);
448 sigaddset(&unblocked_sigs, SIGFPE);
449 sigaddset(&unblocked_sigs, SR_signum);
451 if (!ReduceSignalUsage) {
452 if (!os::Linux::is_sig_ignored(SHUTDOWN1_SIGNAL)) {
453 sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);
454 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL);
455 }
456 if (!os::Linux::is_sig_ignored(SHUTDOWN2_SIGNAL)) {
457 sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);
458 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL);
459 }
460 if (!os::Linux::is_sig_ignored(SHUTDOWN3_SIGNAL)) {
461 sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);
462 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL);
463 }
464 }
465 // Fill in signals that are blocked by all but the VM thread.
466 sigemptyset(&vm_sigs);
467 if (!ReduceSignalUsage)
468 sigaddset(&vm_sigs, BREAK_SIGNAL);
469 debug_only(signal_sets_initialized = true);
471 }
473 // These are signals that are unblocked while a thread is running Java.
474 // (For some reason, they get blocked by default.)
475 sigset_t* os::Linux::unblocked_signals() {
476 assert(signal_sets_initialized, "Not initialized");
477 return &unblocked_sigs;
478 }
480 // These are the signals that are blocked while a (non-VM) thread is
481 // running Java. Only the VM thread handles these signals.
482 sigset_t* os::Linux::vm_signals() {
483 assert(signal_sets_initialized, "Not initialized");
484 return &vm_sigs;
485 }
487 // These are signals that are blocked during cond_wait to allow debugger in
488 sigset_t* os::Linux::allowdebug_blocked_signals() {
489 assert(signal_sets_initialized, "Not initialized");
490 return &allowdebug_blocked_sigs;
491 }
493 void os::Linux::hotspot_sigmask(Thread* thread) {
495 //Save caller's signal mask before setting VM signal mask
496 sigset_t caller_sigmask;
497 pthread_sigmask(SIG_BLOCK, NULL, &caller_sigmask);
499 OSThread* osthread = thread->osthread();
500 osthread->set_caller_sigmask(caller_sigmask);
502 pthread_sigmask(SIG_UNBLOCK, os::Linux::unblocked_signals(), NULL);
504 if (!ReduceSignalUsage) {
505 if (thread->is_VM_thread()) {
506 // Only the VM thread handles BREAK_SIGNAL ...
507 pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL);
508 } else {
509 // ... all other threads block BREAK_SIGNAL
510 pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);
511 }
512 }
513 }
515 //////////////////////////////////////////////////////////////////////////////
516 // detecting pthread library
518 void os::Linux::libpthread_init() {
519 // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION
520 // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a
521 // generic name for earlier versions.
522 // Define macros here so we can build HotSpot on old systems.
523 # ifndef _CS_GNU_LIBC_VERSION
524 # define _CS_GNU_LIBC_VERSION 2
525 # endif
526 # ifndef _CS_GNU_LIBPTHREAD_VERSION
527 # define _CS_GNU_LIBPTHREAD_VERSION 3
528 # endif
530 size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
531 if (n > 0) {
532 char *str = (char *)malloc(n);
533 confstr(_CS_GNU_LIBC_VERSION, str, n);
534 os::Linux::set_glibc_version(str);
535 } else {
536 // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()
537 static char _gnu_libc_version[32];
538 jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),
539 "glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());
540 os::Linux::set_glibc_version(_gnu_libc_version);
541 }
543 n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
544 if (n > 0) {
545 char *str = (char *)malloc(n);
546 confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);
547 // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells
548 // us "NPTL-0.29" even we are running with LinuxThreads. Check if this
549 // is the case. LinuxThreads has a hard limit on max number of threads.
550 // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.
551 // On the other hand, NPTL does not have such a limit, sysconf()
552 // will return -1 and errno is not changed. Check if it is really NPTL.
553 if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&
554 strstr(str, "NPTL") &&
555 sysconf(_SC_THREAD_THREADS_MAX) > 0) {
556 free(str);
557 os::Linux::set_libpthread_version("linuxthreads");
558 } else {
559 os::Linux::set_libpthread_version(str);
560 }
561 } else {
562 // glibc before 2.3.2 only has LinuxThreads.
563 os::Linux::set_libpthread_version("linuxthreads");
564 }
566 if (strstr(libpthread_version(), "NPTL")) {
567 os::Linux::set_is_NPTL();
568 } else {
569 os::Linux::set_is_LinuxThreads();
570 }
572 // LinuxThreads have two flavors: floating-stack mode, which allows variable
573 // stack size; and fixed-stack mode. NPTL is always floating-stack.
574 if (os::Linux::is_NPTL() || os::Linux::supports_variable_stack_size()) {
575 os::Linux::set_is_floating_stack();
576 }
577 }
579 /////////////////////////////////////////////////////////////////////////////
580 // thread stack
582 // Force Linux kernel to expand current thread stack. If "bottom" is close
583 // to the stack guard, caller should block all signals.
584 //
585 // MAP_GROWSDOWN:
586 // A special mmap() flag that is used to implement thread stacks. It tells
587 // kernel that the memory region should extend downwards when needed. This
588 // allows early versions of LinuxThreads to only mmap the first few pages
589 // when creating a new thread. Linux kernel will automatically expand thread
590 // stack as needed (on page faults).
591 //
592 // However, because the memory region of a MAP_GROWSDOWN stack can grow on
593 // demand, if a page fault happens outside an already mapped MAP_GROWSDOWN
594 // region, it's hard to tell if the fault is due to a legitimate stack
595 // access or because of reading/writing non-exist memory (e.g. buffer
596 // overrun). As a rule, if the fault happens below current stack pointer,
597 // Linux kernel does not expand stack, instead a SIGSEGV is sent to the
598 // application (see Linux kernel fault.c).
599 //
600 // This Linux feature can cause SIGSEGV when VM bangs thread stack for
601 // stack overflow detection.
602 //
603 // Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do
604 // not use this flag. However, the stack of initial thread is not created
605 // by pthread, it is still MAP_GROWSDOWN. Also it's possible (though
606 // unlikely) that user code can create a thread with MAP_GROWSDOWN stack
607 // and then attach the thread to JVM.
608 //
609 // To get around the problem and allow stack banging on Linux, we need to
610 // manually expand thread stack after receiving the SIGSEGV.
611 //
612 // There are two ways to expand thread stack to address "bottom", we used
613 // both of them in JVM before 1.5:
614 // 1. adjust stack pointer first so that it is below "bottom", and then
615 // touch "bottom"
616 // 2. mmap() the page in question
617 //
618 // Now alternate signal stack is gone, it's harder to use 2. For instance,
619 // if current sp is already near the lower end of page 101, and we need to
620 // call mmap() to map page 100, it is possible that part of the mmap() frame
621 // will be placed in page 100. When page 100 is mapped, it is zero-filled.
622 // That will destroy the mmap() frame and cause VM to crash.
623 //
624 // The following code works by adjusting sp first, then accessing the "bottom"
625 // page to force a page fault. Linux kernel will then automatically expand the
626 // stack mapping.
627 //
628 // _expand_stack_to() assumes its frame size is less than page size, which
629 // should always be true if the function is not inlined.
631 #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute
632 #define NOINLINE
633 #else
634 #define NOINLINE __attribute__ ((noinline))
635 #endif
637 static void _expand_stack_to(address bottom) NOINLINE;
639 static void _expand_stack_to(address bottom) {
640 address sp;
641 size_t size;
642 volatile char *p;
644 // Adjust bottom to point to the largest address within the same page, it
645 // gives us a one-page buffer if alloca() allocates slightly more memory.
646 bottom = (address)align_size_down((uintptr_t)bottom, os::Linux::page_size());
647 bottom += os::Linux::page_size() - 1;
649 // sp might be slightly above current stack pointer; if that's the case, we
650 // will alloca() a little more space than necessary, which is OK. Don't use
651 // os::current_stack_pointer(), as its result can be slightly below current
652 // stack pointer, causing us to not alloca enough to reach "bottom".
653 sp = (address)&sp;
655 if (sp > bottom) {
656 size = sp - bottom;
657 p = (volatile char *)alloca(size);
658 assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?");
659 p[0] = '\0';
660 }
661 }
663 bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
664 assert(t!=NULL, "just checking");
665 assert(t->osthread()->expanding_stack(), "expand should be set");
666 assert(t->stack_base() != NULL, "stack_base was not initialized");
668 if (addr < t->stack_base() && addr >= t->stack_yellow_zone_base()) {
669 sigset_t mask_all, old_sigset;
670 sigfillset(&mask_all);
671 pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset);
672 _expand_stack_to(addr);
673 pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
674 return true;
675 }
676 return false;
677 }
679 //////////////////////////////////////////////////////////////////////////////
680 // create new thread
682 static address highest_vm_reserved_address();
684 // check if it's safe to start a new thread
685 static bool _thread_safety_check(Thread* thread) {
686 if (os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack()) {
687 // Fixed stack LinuxThreads (SuSE Linux/x86, and some versions of Redhat)
688 // Heap is mmap'ed at lower end of memory space. Thread stacks are
689 // allocated (MAP_FIXED) from high address space. Every thread stack
690 // occupies a fixed size slot (usually 2Mbytes, but user can change
691 // it to other values if they rebuild LinuxThreads).
692 //
693 // Problem with MAP_FIXED is that mmap() can still succeed even part of
694 // the memory region has already been mmap'ed. That means if we have too
695 // many threads and/or very large heap, eventually thread stack will
696 // collide with heap.
697 //
698 // Here we try to prevent heap/stack collision by comparing current
699 // stack bottom with the highest address that has been mmap'ed by JVM
700 // plus a safety margin for memory maps created by native code.
701 //
702 // This feature can be disabled by setting ThreadSafetyMargin to 0
703 //
704 if (ThreadSafetyMargin > 0) {
705 address stack_bottom = os::current_stack_base() - os::current_stack_size();
707 // not safe if our stack extends below the safety margin
708 return stack_bottom - ThreadSafetyMargin >= highest_vm_reserved_address();
709 } else {
710 return true;
711 }
712 } else {
713 // Floating stack LinuxThreads or NPTL:
714 // Unlike fixed stack LinuxThreads, thread stacks are not MAP_FIXED. When
715 // there's not enough space left, pthread_create() will fail. If we come
716 // here, that means enough space has been reserved for stack.
717 return true;
718 }
719 }
721 // Thread start routine for all newly created threads
722 static void *java_start(Thread *thread) {
723 // Try to randomize the cache line index of hot stack frames.
724 // This helps when threads of the same stack traces evict each other's
725 // cache lines. The threads can be either from the same JVM instance, or
726 // from different JVM instances. The benefit is especially true for
727 // processors with hyperthreading technology.
728 static int counter = 0;
729 int pid = os::current_process_id();
730 alloca(((pid ^ counter++) & 7) * 128);
732 ThreadLocalStorage::set_thread(thread);
734 OSThread* osthread = thread->osthread();
735 Monitor* sync = osthread->startThread_lock();
737 // non floating stack LinuxThreads needs extra check, see above
738 if (!_thread_safety_check(thread)) {
739 // notify parent thread
740 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
741 osthread->set_state(ZOMBIE);
742 sync->notify_all();
743 return NULL;
744 }
746 // thread_id is kernel thread id (similar to Solaris LWP id)
747 osthread->set_thread_id(os::Linux::gettid());
749 if (UseNUMA) {
750 int lgrp_id = os::numa_get_group_id();
751 if (lgrp_id != -1) {
752 thread->set_lgrp_id(lgrp_id);
753 }
754 }
755 // initialize signal mask for this thread
756 os::Linux::hotspot_sigmask(thread);
758 // initialize floating point control register
759 os::Linux::init_thread_fpu_state();
761 // handshaking with parent thread
762 {
763 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
765 // notify parent thread
766 osthread->set_state(INITIALIZED);
767 sync->notify_all();
769 // wait until os::start_thread()
770 while (osthread->get_state() == INITIALIZED) {
771 sync->wait(Mutex::_no_safepoint_check_flag);
772 }
773 }
775 // call one more level start routine
776 thread->run();
778 return 0;
779 }
781 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
782 assert(thread->osthread() == NULL, "caller responsible");
784 // Allocate the OSThread object
785 OSThread* osthread = new OSThread(NULL, NULL);
786 if (osthread == NULL) {
787 return false;
788 }
790 // set the correct thread state
791 osthread->set_thread_type(thr_type);
793 // Initial state is ALLOCATED but not INITIALIZED
794 osthread->set_state(ALLOCATED);
796 thread->set_osthread(osthread);
798 // init thread attributes
799 pthread_attr_t attr;
800 pthread_attr_init(&attr);
801 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
803 // stack size
804 if (os::Linux::supports_variable_stack_size()) {
805 // calculate stack size if it's not specified by caller
806 if (stack_size == 0) {
807 stack_size = os::Linux::default_stack_size(thr_type);
809 switch (thr_type) {
810 case os::java_thread:
811 // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
812 if (JavaThread::stack_size_at_create() > 0) stack_size = JavaThread::stack_size_at_create();
813 break;
814 case os::compiler_thread:
815 if (CompilerThreadStackSize > 0) {
816 stack_size = (size_t)(CompilerThreadStackSize * K);
817 break;
818 } // else fall through:
819 // use VMThreadStackSize if CompilerThreadStackSize is not defined
820 case os::vm_thread:
821 case os::pgc_thread:
822 case os::cgc_thread:
823 case os::watcher_thread:
824 if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
825 break;
826 }
827 }
829 stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);
830 pthread_attr_setstacksize(&attr, stack_size);
831 } else {
832 // let pthread_create() pick the default value.
833 }
835 // glibc guard page
836 pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
838 ThreadState state;
840 {
841 // Serialize thread creation if we are running with fixed stack LinuxThreads
842 bool lock = os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack();
843 if (lock) {
844 os::Linux::createThread_lock()->lock_without_safepoint_check();
845 }
847 pthread_t tid;
848 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
850 pthread_attr_destroy(&attr);
852 if (ret != 0) {
853 if (PrintMiscellaneous && (Verbose || WizardMode)) {
854 perror("pthread_create()");
855 }
856 // Need to clean up stuff we've allocated so far
857 thread->set_osthread(NULL);
858 delete osthread;
859 if (lock) os::Linux::createThread_lock()->unlock();
860 return false;
861 }
863 // Store pthread info into the OSThread
864 osthread->set_pthread_id(tid);
866 // Wait until child thread is either initialized or aborted
867 {
868 Monitor* sync_with_child = osthread->startThread_lock();
869 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
870 while ((state = osthread->get_state()) == ALLOCATED) {
871 sync_with_child->wait(Mutex::_no_safepoint_check_flag);
872 }
873 }
875 if (lock) {
876 os::Linux::createThread_lock()->unlock();
877 }
878 }
880 // Aborted due to thread limit being reached
881 if (state == ZOMBIE) {
882 thread->set_osthread(NULL);
883 delete osthread;
884 return false;
885 }
887 // The thread is returned suspended (in state INITIALIZED),
888 // and is started higher up in the call chain
889 assert(state == INITIALIZED, "race condition");
890 return true;
891 }
893 /////////////////////////////////////////////////////////////////////////////
894 // attach existing thread
896 // bootstrap the main thread
897 bool os::create_main_thread(JavaThread* thread) {
898 assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread");
899 return create_attached_thread(thread);
900 }
902 bool os::create_attached_thread(JavaThread* thread) {
903 #ifdef ASSERT
904 thread->verify_not_published();
905 #endif
907 // Allocate the OSThread object
908 OSThread* osthread = new OSThread(NULL, NULL);
910 if (osthread == NULL) {
911 return false;
912 }
914 // Store pthread info into the OSThread
915 osthread->set_thread_id(os::Linux::gettid());
916 osthread->set_pthread_id(::pthread_self());
918 // initialize floating point control register
919 os::Linux::init_thread_fpu_state();
921 // Initial thread state is RUNNABLE
922 osthread->set_state(RUNNABLE);
924 thread->set_osthread(osthread);
926 if (UseNUMA) {
927 int lgrp_id = os::numa_get_group_id();
928 if (lgrp_id != -1) {
929 thread->set_lgrp_id(lgrp_id);
930 }
931 }
933 if (os::Linux::is_initial_thread()) {
934 // If current thread is initial thread, its stack is mapped on demand,
935 // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
936 // the entire stack region to avoid SEGV in stack banging.
937 // It is also useful to get around the heap-stack-gap problem on SuSE
938 // kernel (see 4821821 for details). We first expand stack to the top
939 // of yellow zone, then enable stack yellow zone (order is significant,
940 // enabling yellow zone first will crash JVM on SuSE Linux), so there
941 // is no gap between the last two virtual memory regions.
943 JavaThread *jt = (JavaThread *)thread;
944 address addr = jt->stack_yellow_zone_base();
945 assert(addr != NULL, "initialization problem?");
946 assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
948 osthread->set_expanding_stack();
949 os::Linux::manually_expand_stack(jt, addr);
950 osthread->clear_expanding_stack();
951 }
953 // initialize signal mask for this thread
954 // and save the caller's signal mask
955 os::Linux::hotspot_sigmask(thread);
957 return true;
958 }
960 void os::pd_start_thread(Thread* thread) {
961 OSThread * osthread = thread->osthread();
962 assert(osthread->get_state() != INITIALIZED, "just checking");
963 Monitor* sync_with_child = osthread->startThread_lock();
964 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
965 sync_with_child->notify();
966 }
968 // Free Linux resources related to the OSThread
969 void os::free_thread(OSThread* osthread) {
970 assert(osthread != NULL, "osthread not set");
972 if (Thread::current()->osthread() == osthread) {
973 // Restore caller's signal mask
974 sigset_t sigmask = osthread->caller_sigmask();
975 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
976 }
978 delete osthread;
979 }
981 //////////////////////////////////////////////////////////////////////////////
982 // thread local storage
984 int os::allocate_thread_local_storage() {
985 pthread_key_t key;
986 int rslt = pthread_key_create(&key, NULL);
987 assert(rslt == 0, "cannot allocate thread local storage");
988 return (int)key;
989 }
991 // Note: This is currently not used by VM, as we don't destroy TLS key
992 // on VM exit.
993 void os::free_thread_local_storage(int index) {
994 int rslt = pthread_key_delete((pthread_key_t)index);
995 assert(rslt == 0, "invalid index");
996 }
998 void os::thread_local_storage_at_put(int index, void* value) {
999 int rslt = pthread_setspecific((pthread_key_t)index, value);
1000 assert(rslt == 0, "pthread_setspecific failed");
1001 }
1003 extern "C" Thread* get_thread() {
1004 return ThreadLocalStorage::thread();
1005 }
1007 //////////////////////////////////////////////////////////////////////////////
1008 // initial thread
1010 // Check if current thread is the initial thread, similar to Solaris thr_main.
1011 bool os::Linux::is_initial_thread(void) {
1012 char dummy;
1013 // If called before init complete, thread stack bottom will be null.
1014 // Can be called if fatal error occurs before initialization.
1015 if (initial_thread_stack_bottom() == NULL) return false;
1016 assert(initial_thread_stack_bottom() != NULL &&
1017 initial_thread_stack_size() != 0,
1018 "os::init did not locate initial thread's stack region");
1019 if ((address)&dummy >= initial_thread_stack_bottom() &&
1020 (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size())
1021 return true;
1022 else return false;
1023 }
1025 // Find the virtual memory area that contains addr
1026 static bool find_vma(address addr, address* vma_low, address* vma_high) {
1027 FILE *fp = fopen("/proc/self/maps", "r");
1028 if (fp) {
1029 address low, high;
1030 while (!feof(fp)) {
1031 if (fscanf(fp, "%p-%p", &low, &high) == 2) {
1032 if (low <= addr && addr < high) {
1033 if (vma_low) *vma_low = low;
1034 if (vma_high) *vma_high = high;
1035 fclose (fp);
1036 return true;
1037 }
1038 }
1039 for (;;) {
1040 int ch = fgetc(fp);
1041 if (ch == EOF || ch == (int)'\n') break;
1042 }
1043 }
1044 fclose(fp);
1045 }
1046 return false;
1047 }
1049 // Locate initial thread stack. This special handling of initial thread stack
1050 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
1051 // bogus value for initial thread.
1052 void os::Linux::capture_initial_stack(size_t max_size) {
1053 // stack size is the easy part, get it from RLIMIT_STACK
1054 size_t stack_size;
1055 struct rlimit rlim;
1056 getrlimit(RLIMIT_STACK, &rlim);
1057 stack_size = rlim.rlim_cur;
1059 // 6308388: a bug in ld.so will relocate its own .data section to the
1060 // lower end of primordial stack; reduce ulimit -s value a little bit
1061 // so we won't install guard page on ld.so's data section.
1062 stack_size -= 2 * page_size();
1064 // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat
1065 // 7.1, in both cases we will get 2G in return value.
1066 // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0,
1067 // SuSE 7.2, Debian) can not handle alternate signal stack correctly
1068 // for initial thread if its stack size exceeds 6M. Cap it at 2M,
1069 // in case other parts in glibc still assumes 2M max stack size.
1070 // FIXME: alt signal stack is gone, maybe we can relax this constraint?
1071 #ifndef IA64
1072 if (stack_size > 2 * K * K) stack_size = 2 * K * K;
1073 #else
1074 // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small
1075 if (stack_size > 4 * K * K) stack_size = 4 * K * K;
1076 #endif
1078 // Try to figure out where the stack base (top) is. This is harder.
1079 //
1080 // When an application is started, glibc saves the initial stack pointer in
1081 // a global variable "__libc_stack_end", which is then used by system
1082 // libraries. __libc_stack_end should be pretty close to stack top. The
1083 // variable is available since the very early days. However, because it is
1084 // a private interface, it could disappear in the future.
1085 //
1086 // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar
1087 // to __libc_stack_end, it is very close to stack top, but isn't the real
1088 // stack top. Note that /proc may not exist if VM is running as a chroot
1089 // program, so reading /proc/<pid>/stat could fail. Also the contents of
1090 // /proc/<pid>/stat could change in the future (though unlikely).
1091 //
1092 // We try __libc_stack_end first. If that doesn't work, look for
1093 // /proc/<pid>/stat. If neither of them works, we use current stack pointer
1094 // as a hint, which should work well in most cases.
1096 uintptr_t stack_start;
1098 // try __libc_stack_end first
1099 uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end");
1100 if (p && *p) {
1101 stack_start = *p;
1102 } else {
1103 // see if we can get the start_stack field from /proc/self/stat
1104 FILE *fp;
1105 int pid;
1106 char state;
1107 int ppid;
1108 int pgrp;
1109 int session;
1110 int nr;
1111 int tpgrp;
1112 unsigned long flags;
1113 unsigned long minflt;
1114 unsigned long cminflt;
1115 unsigned long majflt;
1116 unsigned long cmajflt;
1117 unsigned long utime;
1118 unsigned long stime;
1119 long cutime;
1120 long cstime;
1121 long prio;
1122 long nice;
1123 long junk;
1124 long it_real;
1125 uintptr_t start;
1126 uintptr_t vsize;
1127 uintptr_t rss;
1128 unsigned long rsslim;
1129 uintptr_t scodes;
1130 uintptr_t ecode;
1131 int i;
1133 // Figure what the primordial thread stack base is. Code is inspired
1134 // by email from Hans Boehm. /proc/self/stat begins with current pid,
1135 // followed by command name surrounded by parentheses, state, etc.
1136 char stat[2048];
1137 int statlen;
1139 fp = fopen("/proc/self/stat", "r");
1140 if (fp) {
1141 statlen = fread(stat, 1, 2047, fp);
1142 stat[statlen] = '\0';
1143 fclose(fp);
1145 // Skip pid and the command string. Note that we could be dealing with
1146 // weird command names, e.g. user could decide to rename java launcher
1147 // to "java 1.4.2 :)", then the stat file would look like
1148 // 1234 (java 1.4.2 :)) R ... ...
1149 // We don't really need to know the command string, just find the last
1150 // occurrence of ")" and then start parsing from there. See bug 4726580.
1151 char * s = strrchr(stat, ')');
1153 i = 0;
1154 if (s) {
1155 // Skip blank chars
1156 do s++; while (isspace(*s));
1158 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */
1159 /* 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 */
1160 i = sscanf(s, "%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu",
1161 &state, /* 3 %c */
1162 &ppid, /* 4 %d */
1163 &pgrp, /* 5 %d */
1164 &session, /* 6 %d */
1165 &nr, /* 7 %d */
1166 &tpgrp, /* 8 %d */
1167 &flags, /* 9 %lu */
1168 &minflt, /* 10 %lu */
1169 &cminflt, /* 11 %lu */
1170 &majflt, /* 12 %lu */
1171 &cmajflt, /* 13 %lu */
1172 &utime, /* 14 %lu */
1173 &stime, /* 15 %lu */
1174 &cutime, /* 16 %ld */
1175 &cstime, /* 17 %ld */
1176 &prio, /* 18 %ld */
1177 &nice, /* 19 %ld */
1178 &junk, /* 20 %ld */
1179 &it_real, /* 21 %ld */
1180 &start, /* 22 %lu */
1181 &vsize, /* 23 %lu */
1182 &rss, /* 24 %ld */
1183 &rsslim, /* 25 %lu */
1184 &scodes, /* 26 %lu */
1185 &ecode, /* 27 %lu */
1186 &stack_start); /* 28 %lu */
1187 }
1189 if (i != 28 - 2) {
1190 assert(false, "Bad conversion from /proc/self/stat");
1191 // product mode - assume we are the initial thread, good luck in the
1192 // embedded case.
1193 warning("Can't detect initial thread stack location - bad conversion");
1194 stack_start = (uintptr_t) &rlim;
1195 }
1196 } else {
1197 // For some reason we can't open /proc/self/stat (for example, running on
1198 // FreeBSD with a Linux emulator, or inside chroot), this should work for
1199 // most cases, so don't abort:
1200 warning("Can't detect initial thread stack location - no /proc/self/stat");
1201 stack_start = (uintptr_t) &rlim;
1202 }
1203 }
1205 // Now we have a pointer (stack_start) very close to the stack top, the
1206 // next thing to do is to figure out the exact location of stack top. We
1207 // can find out the virtual memory area that contains stack_start by
1208 // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
1209 // and its upper limit is the real stack top. (again, this would fail if
1210 // running inside chroot, because /proc may not exist.)
1212 uintptr_t stack_top;
1213 address low, high;
1214 if (find_vma((address)stack_start, &low, &high)) {
1215 // success, "high" is the true stack top. (ignore "low", because initial
1216 // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
1217 stack_top = (uintptr_t)high;
1218 } else {
1219 // failed, likely because /proc/self/maps does not exist
1220 warning("Can't detect initial thread stack location - find_vma failed");
1221 // best effort: stack_start is normally within a few pages below the real
1222 // stack top, use it as stack top, and reduce stack size so we won't put
1223 // guard page outside stack.
1224 stack_top = stack_start;
1225 stack_size -= 16 * page_size();
1226 }
1228 // stack_top could be partially down the page so align it
1229 stack_top = align_size_up(stack_top, page_size());
1231 if (max_size && stack_size > max_size) {
1232 _initial_thread_stack_size = max_size;
1233 } else {
1234 _initial_thread_stack_size = stack_size;
1235 }
1237 _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());
1238 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;
1239 }
1241 ////////////////////////////////////////////////////////////////////////////////
1242 // time support
1244 // Time since start-up in seconds to a fine granularity.
1245 // Used by VMSelfDestructTimer and the MemProfiler.
1246 double os::elapsedTime() {
1248 return (double)(os::elapsed_counter()) * 0.000001;
1249 }
1251 jlong os::elapsed_counter() {
1252 timeval time;
1253 int status = gettimeofday(&time, NULL);
1254 return jlong(time.tv_sec) * 1000 * 1000 + jlong(time.tv_usec) - initial_time_count;
1255 }
1257 jlong os::elapsed_frequency() {
1258 return (1000 * 1000);
1259 }
1261 jlong os::javaTimeMillis() {
1262 timeval time;
1263 int status = gettimeofday(&time, NULL);
1264 assert(status != -1, "linux error");
1265 return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);
1266 }
1268 #ifndef CLOCK_MONOTONIC
1269 #define CLOCK_MONOTONIC (1)
1270 #endif
1272 void os::Linux::clock_init() {
1273 // we do dlopen's in this particular order due to bug in linux
1274 // dynamical loader (see 6348968) leading to crash on exit
1275 void* handle = dlopen("librt.so.1", RTLD_LAZY);
1276 if (handle == NULL) {
1277 handle = dlopen("librt.so", RTLD_LAZY);
1278 }
1280 if (handle) {
1281 int (*clock_getres_func)(clockid_t, struct timespec*) =
1282 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
1283 int (*clock_gettime_func)(clockid_t, struct timespec*) =
1284 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
1285 if (clock_getres_func && clock_gettime_func) {
1286 // See if monotonic clock is supported by the kernel. Note that some
1287 // early implementations simply return kernel jiffies (updated every
1288 // 1/100 or 1/1000 second). It would be bad to use such a low res clock
1289 // for nano time (though the monotonic property is still nice to have).
1290 // It's fixed in newer kernels, however clock_getres() still returns
1291 // 1/HZ. We check if clock_getres() works, but will ignore its reported
1292 // resolution for now. Hopefully as people move to new kernels, this
1293 // won't be a problem.
1294 struct timespec res;
1295 struct timespec tp;
1296 if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&
1297 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
1298 // yes, monotonic clock is supported
1299 _clock_gettime = clock_gettime_func;
1300 } else {
1301 // close librt if there is no monotonic clock
1302 dlclose(handle);
1303 }
1304 }
1305 }
1306 }
1308 #ifndef SYS_clock_getres
1310 #if defined(IA32) || defined(AMD64)
1311 #define SYS_clock_getres IA32_ONLY(266) AMD64_ONLY(229)
1312 #else
1313 #error Value of SYS_clock_getres not known on this platform
1314 #endif
1316 #endif
1318 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
1320 void os::Linux::fast_thread_clock_init() {
1321 if (!UseLinuxPosixThreadCPUClocks) {
1322 return;
1323 }
1324 clockid_t clockid;
1325 struct timespec tp;
1326 int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
1327 (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
1329 // Switch to using fast clocks for thread cpu time if
1330 // the sys_clock_getres() returns 0 error code.
1331 // Note, that some kernels may support the current thread
1332 // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
1333 // returned by the pthread_getcpuclockid().
1334 // If the fast Posix clocks are supported then the sys_clock_getres()
1335 // must return at least tp.tv_sec == 0 which means a resolution
1336 // better than 1 sec. This is extra check for reliability.
1338 if(pthread_getcpuclockid_func &&
1339 pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
1340 sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
1342 _supports_fast_thread_cpu_time = true;
1343 _pthread_getcpuclockid = pthread_getcpuclockid_func;
1344 }
1345 }
1347 jlong os::javaTimeNanos() {
1348 if (Linux::supports_monotonic_clock()) {
1349 struct timespec tp;
1350 int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
1351 assert(status == 0, "gettime error");
1352 jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
1353 return result;
1354 } else {
1355 timeval time;
1356 int status = gettimeofday(&time, NULL);
1357 assert(status != -1, "linux error");
1358 jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
1359 return 1000 * usecs;
1360 }
1361 }
1363 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
1364 if (Linux::supports_monotonic_clock()) {
1365 info_ptr->max_value = ALL_64_BITS;
1367 // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
1368 info_ptr->may_skip_backward = false; // not subject to resetting or drifting
1369 info_ptr->may_skip_forward = false; // not subject to resetting or drifting
1370 } else {
1371 // gettimeofday - based on time in seconds since the Epoch thus does not wrap
1372 info_ptr->max_value = ALL_64_BITS;
1374 // gettimeofday is a real time clock so it skips
1375 info_ptr->may_skip_backward = true;
1376 info_ptr->may_skip_forward = true;
1377 }
1379 info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time
1380 }
1382 // Return the real, user, and system times in seconds from an
1383 // arbitrary fixed point in the past.
1384 bool os::getTimesSecs(double* process_real_time,
1385 double* process_user_time,
1386 double* process_system_time) {
1387 struct tms ticks;
1388 clock_t real_ticks = times(&ticks);
1390 if (real_ticks == (clock_t) (-1)) {
1391 return false;
1392 } else {
1393 double ticks_per_second = (double) clock_tics_per_sec;
1394 *process_user_time = ((double) ticks.tms_utime) / ticks_per_second;
1395 *process_system_time = ((double) ticks.tms_stime) / ticks_per_second;
1396 *process_real_time = ((double) real_ticks) / ticks_per_second;
1398 return true;
1399 }
1400 }
1403 char * os::local_time_string(char *buf, size_t buflen) {
1404 struct tm t;
1405 time_t long_time;
1406 time(&long_time);
1407 localtime_r(&long_time, &t);
1408 jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
1409 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
1410 t.tm_hour, t.tm_min, t.tm_sec);
1411 return buf;
1412 }
1414 ////////////////////////////////////////////////////////////////////////////////
1415 // runtime exit support
1417 // Note: os::shutdown() might be called very early during initialization, or
1418 // called from signal handler. Before adding something to os::shutdown(), make
1419 // sure it is async-safe and can handle partially initialized VM.
1420 void os::shutdown() {
1422 // allow PerfMemory to attempt cleanup of any persistent resources
1423 perfMemory_exit();
1425 // needs to remove object in file system
1426 AttachListener::abort();
1428 // flush buffered output, finish log files
1429 ostream_abort();
1431 // Check for abort hook
1432 abort_hook_t abort_hook = Arguments::abort_hook();
1433 if (abort_hook != NULL) {
1434 abort_hook();
1435 }
1437 }
1439 // Note: os::abort() might be called very early during initialization, or
1440 // called from signal handler. Before adding something to os::abort(), make
1441 // sure it is async-safe and can handle partially initialized VM.
1442 void os::abort(bool dump_core) {
1443 os::shutdown();
1444 if (dump_core) {
1445 #ifndef PRODUCT
1446 fdStream out(defaultStream::output_fd());
1447 out.print_raw("Current thread is ");
1448 char buf[16];
1449 jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
1450 out.print_raw_cr(buf);
1451 out.print_raw_cr("Dumping core ...");
1452 #endif
1453 ::abort(); // dump core
1454 }
1456 ::exit(1);
1457 }
1459 // Die immediately, no exit hook, no abort hook, no cleanup.
1460 void os::die() {
1461 // _exit() on LinuxThreads only kills current thread
1462 ::abort();
1463 }
1465 // unused on linux for now.
1466 void os::set_error_file(const char *logfile) {}
1468 intx os::current_thread_id() { return (intx)pthread_self(); }
1469 int os::current_process_id() {
1471 // Under the old linux thread library, linux gives each thread
1472 // its own process id. Because of this each thread will return
1473 // a different pid if this method were to return the result
1474 // of getpid(2). Linux provides no api that returns the pid
1475 // of the launcher thread for the vm. This implementation
1476 // returns a unique pid, the pid of the launcher thread
1477 // that starts the vm 'process'.
1479 // Under the NPTL, getpid() returns the same pid as the
1480 // launcher thread rather than a unique pid per thread.
1481 // Use gettid() if you want the old pre NPTL behaviour.
1483 // if you are looking for the result of a call to getpid() that
1484 // returns a unique pid for the calling thread, then look at the
1485 // OSThread::thread_id() method in osThread_linux.hpp file
1487 return (int)(_initial_pid ? _initial_pid : getpid());
1488 }
1490 // DLL functions
1492 const char* os::dll_file_extension() { return ".so"; }
1494 const char* os::get_temp_directory() { return "/tmp/"; }
1496 const char* os::get_current_directory(char *buf, int buflen) {
1497 return getcwd(buf, buflen);
1498 }
1500 // check if addr is inside libjvm[_g].so
1501 bool os::address_is_in_vm(address addr) {
1502 static address libjvm_base_addr;
1503 Dl_info dlinfo;
1505 if (libjvm_base_addr == NULL) {
1506 dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo);
1507 libjvm_base_addr = (address)dlinfo.dli_fbase;
1508 assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1509 }
1511 if (dladdr((void *)addr, &dlinfo)) {
1512 if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1513 }
1515 return false;
1516 }
1518 bool os::dll_address_to_function_name(address addr, char *buf,
1519 int buflen, int *offset) {
1520 Dl_info dlinfo;
1522 if (dladdr((void*)addr, &dlinfo) && dlinfo.dli_sname != NULL) {
1523 if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1524 if (offset) *offset = addr - (address)dlinfo.dli_saddr;
1525 return true;
1526 } else {
1527 if (buf) buf[0] = '\0';
1528 if (offset) *offset = -1;
1529 return false;
1530 }
1531 }
1533 struct _address_to_library_name {
1534 address addr; // input : memory address
1535 size_t buflen; // size of fname
1536 char* fname; // output: library name
1537 address base; // library base addr
1538 };
1540 static int address_to_library_name_callback(struct dl_phdr_info *info,
1541 size_t size, void *data) {
1542 int i;
1543 bool found = false;
1544 address libbase = NULL;
1545 struct _address_to_library_name * d = (struct _address_to_library_name *)data;
1547 // iterate through all loadable segments
1548 for (i = 0; i < info->dlpi_phnum; i++) {
1549 address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1550 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1551 // base address of a library is the lowest address of its loaded
1552 // segments.
1553 if (libbase == NULL || libbase > segbase) {
1554 libbase = segbase;
1555 }
1556 // see if 'addr' is within current segment
1557 if (segbase <= d->addr &&
1558 d->addr < segbase + info->dlpi_phdr[i].p_memsz) {
1559 found = true;
1560 }
1561 }
1562 }
1564 // dlpi_name is NULL or empty if the ELF file is executable, return 0
1565 // so dll_address_to_library_name() can fall through to use dladdr() which
1566 // can figure out executable name from argv[0].
1567 if (found && info->dlpi_name && info->dlpi_name[0]) {
1568 d->base = libbase;
1569 if (d->fname) {
1570 jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
1571 }
1572 return 1;
1573 }
1574 return 0;
1575 }
1577 bool os::dll_address_to_library_name(address addr, char* buf,
1578 int buflen, int* offset) {
1579 Dl_info dlinfo;
1580 struct _address_to_library_name data;
1582 // There is a bug in old glibc dladdr() implementation that it could resolve
1583 // to wrong library name if the .so file has a base address != NULL. Here
1584 // we iterate through the program headers of all loaded libraries to find
1585 // out which library 'addr' really belongs to. This workaround can be
1586 // removed once the minimum requirement for glibc is moved to 2.3.x.
1587 data.addr = addr;
1588 data.fname = buf;
1589 data.buflen = buflen;
1590 data.base = NULL;
1591 int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
1593 if (rslt) {
1594 // buf already contains library name
1595 if (offset) *offset = addr - data.base;
1596 return true;
1597 } else if (dladdr((void*)addr, &dlinfo)){
1598 if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1599 if (offset) *offset = addr - (address)dlinfo.dli_fbase;
1600 return true;
1601 } else {
1602 if (buf) buf[0] = '\0';
1603 if (offset) *offset = -1;
1604 return false;
1605 }
1606 }
1608 // Loads .dll/.so and
1609 // in case of error it checks if .dll/.so was built for the
1610 // same architecture as Hotspot is running on
1612 void * os::dll_load(const char *filename, char *ebuf, int ebuflen)
1613 {
1614 void * result= ::dlopen(filename, RTLD_LAZY);
1615 if (result != NULL) {
1616 // Successful loading
1617 return result;
1618 }
1620 Elf32_Ehdr elf_head;
1622 // Read system error message into ebuf
1623 // It may or may not be overwritten below
1624 ::strncpy(ebuf, ::dlerror(), ebuflen-1);
1625 ebuf[ebuflen-1]='\0';
1626 int diag_msg_max_length=ebuflen-strlen(ebuf);
1627 char* diag_msg_buf=ebuf+strlen(ebuf);
1629 if (diag_msg_max_length==0) {
1630 // No more space in ebuf for additional diagnostics message
1631 return NULL;
1632 }
1635 int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
1637 if (file_descriptor < 0) {
1638 // Can't open library, report dlerror() message
1639 return NULL;
1640 }
1642 bool failed_to_read_elf_head=
1643 (sizeof(elf_head)!=
1644 (::read(file_descriptor, &elf_head,sizeof(elf_head)))) ;
1646 ::close(file_descriptor);
1647 if (failed_to_read_elf_head) {
1648 // file i/o error - report dlerror() msg
1649 return NULL;
1650 }
1652 typedef struct {
1653 Elf32_Half code; // Actual value as defined in elf.h
1654 Elf32_Half compat_class; // Compatibility of archs at VM's sense
1655 char elf_class; // 32 or 64 bit
1656 char endianess; // MSB or LSB
1657 char* name; // String representation
1658 } arch_t;
1660 #ifndef EM_486
1661 #define EM_486 6 /* Intel 80486 */
1662 #endif
1664 static const arch_t arch_array[]={
1665 {EM_386, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1666 {EM_486, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1667 {EM_IA_64, EM_IA_64, ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
1668 {EM_X86_64, EM_X86_64, ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
1669 {EM_SPARC, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1670 {EM_SPARC32PLUS, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1671 {EM_SPARCV9, EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
1672 {EM_PPC, EM_PPC, ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
1673 {EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"}
1674 };
1676 #if (defined IA32)
1677 static Elf32_Half running_arch_code=EM_386;
1678 #elif (defined AMD64)
1679 static Elf32_Half running_arch_code=EM_X86_64;
1680 #elif (defined IA64)
1681 static Elf32_Half running_arch_code=EM_IA_64;
1682 #elif (defined __sparc) && (defined _LP64)
1683 static Elf32_Half running_arch_code=EM_SPARCV9;
1684 #elif (defined __sparc) && (!defined _LP64)
1685 static Elf32_Half running_arch_code=EM_SPARC;
1686 #elif (defined __powerpc64__)
1687 static Elf32_Half running_arch_code=EM_PPC64;
1688 #elif (defined __powerpc__)
1689 static Elf32_Half running_arch_code=EM_PPC;
1690 #else
1691 #error Method os::dll_load requires that one of following is defined:\
1692 IA32, AMD64, IA64, __sparc, __powerpc__
1693 #endif
1695 // Identify compatability class for VM's architecture and library's architecture
1696 // Obtain string descriptions for architectures
1698 arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
1699 int running_arch_index=-1;
1701 for (unsigned int i=0 ; i < ARRAY_SIZE(arch_array) ; i++ ) {
1702 if (running_arch_code == arch_array[i].code) {
1703 running_arch_index = i;
1704 }
1705 if (lib_arch.code == arch_array[i].code) {
1706 lib_arch.compat_class = arch_array[i].compat_class;
1707 lib_arch.name = arch_array[i].name;
1708 }
1709 }
1711 assert(running_arch_index != -1,
1712 "Didn't find running architecture code (running_arch_code) in arch_array");
1713 if (running_arch_index == -1) {
1714 // Even though running architecture detection failed
1715 // we may still continue with reporting dlerror() message
1716 return NULL;
1717 }
1719 if (lib_arch.endianess != arch_array[running_arch_index].endianess) {
1720 ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");
1721 return NULL;
1722 }
1724 if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
1725 ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");
1726 return NULL;
1727 }
1729 if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
1730 if ( lib_arch.name!=NULL ) {
1731 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1732 " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
1733 lib_arch.name, arch_array[running_arch_index].name);
1734 } else {
1735 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1736 " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
1737 lib_arch.code,
1738 arch_array[running_arch_index].name);
1739 }
1740 }
1742 return NULL;
1743 }
1748 bool _print_ascii_file(const char* filename, outputStream* st) {
1749 int fd = open(filename, O_RDONLY);
1750 if (fd == -1) {
1751 return false;
1752 }
1754 char buf[32];
1755 int bytes;
1756 while ((bytes = read(fd, buf, sizeof(buf))) > 0) {
1757 st->print_raw(buf, bytes);
1758 }
1760 close(fd);
1762 return true;
1763 }
1765 void os::print_dll_info(outputStream *st) {
1766 st->print_cr("Dynamic libraries:");
1768 char fname[32];
1769 pid_t pid = os::Linux::gettid();
1771 jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);
1773 if (!_print_ascii_file(fname, st)) {
1774 st->print("Can not get library information for pid = %d\n", pid);
1775 }
1776 }
1779 void os::print_os_info(outputStream* st) {
1780 st->print("OS:");
1782 // Try to identify popular distros.
1783 // Most Linux distributions have /etc/XXX-release file, which contains
1784 // the OS version string. Some have more than one /etc/XXX-release file
1785 // (e.g. Mandrake has both /etc/mandrake-release and /etc/redhat-release.),
1786 // so the order is important.
1787 if (!_print_ascii_file("/etc/mandrake-release", st) &&
1788 !_print_ascii_file("/etc/sun-release", st) &&
1789 !_print_ascii_file("/etc/redhat-release", st) &&
1790 !_print_ascii_file("/etc/SuSE-release", st) &&
1791 !_print_ascii_file("/etc/turbolinux-release", st) &&
1792 !_print_ascii_file("/etc/gentoo-release", st) &&
1793 !_print_ascii_file("/etc/debian_version", st)) {
1794 st->print("Linux");
1795 }
1796 st->cr();
1798 // kernel
1799 st->print("uname:");
1800 struct utsname name;
1801 uname(&name);
1802 st->print(name.sysname); st->print(" ");
1803 st->print(name.release); st->print(" ");
1804 st->print(name.version); st->print(" ");
1805 st->print(name.machine);
1806 st->cr();
1808 // Print warning if unsafe chroot environment detected
1809 if (unsafe_chroot_detected) {
1810 st->print("WARNING!! ");
1811 st->print_cr(unstable_chroot_error);
1812 }
1814 // libc, pthread
1815 st->print("libc:");
1816 st->print(os::Linux::glibc_version()); st->print(" ");
1817 st->print(os::Linux::libpthread_version()); st->print(" ");
1818 if (os::Linux::is_LinuxThreads()) {
1819 st->print("(%s stack)", os::Linux::is_floating_stack() ? "floating" : "fixed");
1820 }
1821 st->cr();
1823 // rlimit
1824 st->print("rlimit:");
1825 struct rlimit rlim;
1827 st->print(" STACK ");
1828 getrlimit(RLIMIT_STACK, &rlim);
1829 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
1830 else st->print("%uk", rlim.rlim_cur >> 10);
1832 st->print(", CORE ");
1833 getrlimit(RLIMIT_CORE, &rlim);
1834 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
1835 else st->print("%uk", rlim.rlim_cur >> 10);
1837 st->print(", NPROC ");
1838 getrlimit(RLIMIT_NPROC, &rlim);
1839 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
1840 else st->print("%d", rlim.rlim_cur);
1842 st->print(", NOFILE ");
1843 getrlimit(RLIMIT_NOFILE, &rlim);
1844 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
1845 else st->print("%d", rlim.rlim_cur);
1847 st->print(", AS ");
1848 getrlimit(RLIMIT_AS, &rlim);
1849 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
1850 else st->print("%uk", rlim.rlim_cur >> 10);
1851 st->cr();
1853 // load average
1854 st->print("load average:");
1855 double loadavg[3];
1856 os::loadavg(loadavg, 3);
1857 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
1858 st->cr();
1859 }
1861 void os::print_memory_info(outputStream* st) {
1863 st->print("Memory:");
1864 st->print(" %dk page", os::vm_page_size()>>10);
1866 // values in struct sysinfo are "unsigned long"
1867 struct sysinfo si;
1868 sysinfo(&si);
1870 st->print(", physical " UINT64_FORMAT "k",
1871 os::physical_memory() >> 10);
1872 st->print("(" UINT64_FORMAT "k free)",
1873 os::available_memory() >> 10);
1874 st->print(", swap " UINT64_FORMAT "k",
1875 ((jlong)si.totalswap * si.mem_unit) >> 10);
1876 st->print("(" UINT64_FORMAT "k free)",
1877 ((jlong)si.freeswap * si.mem_unit) >> 10);
1878 st->cr();
1879 }
1881 // Taken from /usr/include/bits/siginfo.h Supposed to be architecture specific
1882 // but they're the same for all the linux arch that we support
1883 // and they're the same for solaris but there's no common place to put this.
1884 const char *ill_names[] = { "ILL0", "ILL_ILLOPC", "ILL_ILLOPN", "ILL_ILLADR",
1885 "ILL_ILLTRP", "ILL_PRVOPC", "ILL_PRVREG",
1886 "ILL_COPROC", "ILL_BADSTK" };
1888 const char *fpe_names[] = { "FPE0", "FPE_INTDIV", "FPE_INTOVF", "FPE_FLTDIV",
1889 "FPE_FLTOVF", "FPE_FLTUND", "FPE_FLTRES",
1890 "FPE_FLTINV", "FPE_FLTSUB", "FPE_FLTDEN" };
1892 const char *segv_names[] = { "SEGV0", "SEGV_MAPERR", "SEGV_ACCERR" };
1894 const char *bus_names[] = { "BUS0", "BUS_ADRALN", "BUS_ADRERR", "BUS_OBJERR" };
1896 void os::print_siginfo(outputStream* st, void* siginfo) {
1897 st->print("siginfo:");
1899 const int buflen = 100;
1900 char buf[buflen];
1901 siginfo_t *si = (siginfo_t*)siginfo;
1902 st->print("si_signo=%s: ", os::exception_name(si->si_signo, buf, buflen));
1903 if (si->si_errno != 0 && strerror_r(si->si_errno, buf, buflen) == 0) {
1904 st->print("si_errno=%s", buf);
1905 } else {
1906 st->print("si_errno=%d", si->si_errno);
1907 }
1908 const int c = si->si_code;
1909 assert(c > 0, "unexpected si_code");
1910 switch (si->si_signo) {
1911 case SIGILL:
1912 st->print(", si_code=%d (%s)", c, c > 8 ? "" : ill_names[c]);
1913 st->print(", si_addr=" PTR_FORMAT, si->si_addr);
1914 break;
1915 case SIGFPE:
1916 st->print(", si_code=%d (%s)", c, c > 9 ? "" : fpe_names[c]);
1917 st->print(", si_addr=" PTR_FORMAT, si->si_addr);
1918 break;
1919 case SIGSEGV:
1920 st->print(", si_code=%d (%s)", c, c > 2 ? "" : segv_names[c]);
1921 st->print(", si_addr=" PTR_FORMAT, si->si_addr);
1922 break;
1923 case SIGBUS:
1924 st->print(", si_code=%d (%s)", c, c > 3 ? "" : bus_names[c]);
1925 st->print(", si_addr=" PTR_FORMAT, si->si_addr);
1926 break;
1927 default:
1928 st->print(", si_code=%d", si->si_code);
1929 // no si_addr
1930 }
1932 if ((si->si_signo == SIGBUS || si->si_signo == SIGSEGV) &&
1933 UseSharedSpaces) {
1934 FileMapInfo* mapinfo = FileMapInfo::current_info();
1935 if (mapinfo->is_in_shared_space(si->si_addr)) {
1936 st->print("\n\nError accessing class data sharing archive." \
1937 " Mapped file inaccessible during execution, " \
1938 " possible disk/network problem.");
1939 }
1940 }
1941 st->cr();
1942 }
1945 static void print_signal_handler(outputStream* st, int sig,
1946 char* buf, size_t buflen);
1948 void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
1949 st->print_cr("Signal Handlers:");
1950 print_signal_handler(st, SIGSEGV, buf, buflen);
1951 print_signal_handler(st, SIGBUS , buf, buflen);
1952 print_signal_handler(st, SIGFPE , buf, buflen);
1953 print_signal_handler(st, SIGPIPE, buf, buflen);
1954 print_signal_handler(st, SIGXFSZ, buf, buflen);
1955 print_signal_handler(st, SIGILL , buf, buflen);
1956 print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen);
1957 print_signal_handler(st, SR_signum, buf, buflen);
1958 print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen);
1959 print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);
1960 print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen);
1961 print_signal_handler(st, BREAK_SIGNAL, buf, buflen);
1962 }
1964 static char saved_jvm_path[MAXPATHLEN] = {0};
1966 // Find the full path to the current module, libjvm.so or libjvm_g.so
1967 void os::jvm_path(char *buf, jint len) {
1968 // Error checking.
1969 if (len < MAXPATHLEN) {
1970 assert(false, "must use a large-enough buffer");
1971 buf[0] = '\0';
1972 return;
1973 }
1974 // Lazy resolve the path to current module.
1975 if (saved_jvm_path[0] != 0) {
1976 strcpy(buf, saved_jvm_path);
1977 return;
1978 }
1980 char dli_fname[MAXPATHLEN];
1981 bool ret = dll_address_to_library_name(
1982 CAST_FROM_FN_PTR(address, os::jvm_path),
1983 dli_fname, sizeof(dli_fname), NULL);
1984 assert(ret != 0, "cannot locate libjvm");
1985 realpath(dli_fname, buf);
1987 if (strcmp(Arguments::sun_java_launcher(), "gamma") == 0) {
1988 // Support for the gamma launcher. Typical value for buf is
1989 // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so". If "/jre/lib/" appears at
1990 // the right place in the string, then assume we are installed in a JDK and
1991 // we're done. Otherwise, check for a JAVA_HOME environment variable and fix
1992 // up the path so it looks like libjvm.so is installed there (append a
1993 // fake suffix hotspot/libjvm.so).
1994 const char *p = buf + strlen(buf) - 1;
1995 for (int count = 0; p > buf && count < 5; ++count) {
1996 for (--p; p > buf && *p != '/'; --p)
1997 /* empty */ ;
1998 }
2000 if (strncmp(p, "/jre/lib/", 9) != 0) {
2001 // Look for JAVA_HOME in the environment.
2002 char* java_home_var = ::getenv("JAVA_HOME");
2003 if (java_home_var != NULL && java_home_var[0] != 0) {
2004 // Check the current module name "libjvm.so" or "libjvm_g.so".
2005 p = strrchr(buf, '/');
2006 assert(strstr(p, "/libjvm") == p, "invalid library name");
2007 p = strstr(p, "_g") ? "_g" : "";
2009 realpath(java_home_var, buf);
2010 sprintf(buf + strlen(buf), "/jre/lib/%s", cpu_arch);
2011 if (0 == access(buf, F_OK)) {
2012 // Use current module name "libjvm[_g].so" instead of
2013 // "libjvm"debug_only("_g")".so" since for fastdebug version
2014 // we should have "libjvm.so" but debug_only("_g") adds "_g"!
2015 // It is used when we are choosing the HPI library's name
2016 // "libhpi[_g].so" in hpi::initialize_get_interface().
2017 sprintf(buf + strlen(buf), "/hotspot/libjvm%s.so", p);
2018 } else {
2019 // Go back to path of .so
2020 realpath(dli_fname, buf);
2021 }
2022 }
2023 }
2024 }
2026 strcpy(saved_jvm_path, buf);
2027 }
2029 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2030 // no prefix required, not even "_"
2031 }
2033 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2034 // no suffix required
2035 }
2037 ////////////////////////////////////////////////////////////////////////////////
2038 // sun.misc.Signal support
2040 static volatile jint sigint_count = 0;
2042 static void
2043 UserHandler(int sig, void *siginfo, void *context) {
2044 // 4511530 - sem_post is serialized and handled by the manager thread. When
2045 // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
2046 // don't want to flood the manager thread with sem_post requests.
2047 if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)
2048 return;
2050 // Ctrl-C is pressed during error reporting, likely because the error
2051 // handler fails to abort. Let VM die immediately.
2052 if (sig == SIGINT && is_error_reported()) {
2053 os::die();
2054 }
2056 os::signal_notify(sig);
2057 }
2059 void* os::user_handler() {
2060 return CAST_FROM_FN_PTR(void*, UserHandler);
2061 }
2063 extern "C" {
2064 typedef void (*sa_handler_t)(int);
2065 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2066 }
2068 void* os::signal(int signal_number, void* handler) {
2069 struct sigaction sigAct, oldSigAct;
2071 sigfillset(&(sigAct.sa_mask));
2072 sigAct.sa_flags = SA_RESTART|SA_SIGINFO;
2073 sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2075 if (sigaction(signal_number, &sigAct, &oldSigAct)) {
2076 // -1 means registration failed
2077 return (void *)-1;
2078 }
2080 return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2081 }
2083 void os::signal_raise(int signal_number) {
2084 ::raise(signal_number);
2085 }
2087 /*
2088 * The following code is moved from os.cpp for making this
2089 * code platform specific, which it is by its very nature.
2090 */
2092 // Will be modified when max signal is changed to be dynamic
2093 int os::sigexitnum_pd() {
2094 return NSIG;
2095 }
2097 // a counter for each possible signal value
2098 static volatile jint pending_signals[NSIG+1] = { 0 };
2100 // Linux(POSIX) specific hand shaking semaphore.
2101 static sem_t sig_sem;
2103 void os::signal_init_pd() {
2104 // Initialize signal structures
2105 ::memset((void*)pending_signals, 0, sizeof(pending_signals));
2107 // Initialize signal semaphore
2108 ::sem_init(&sig_sem, 0, 0);
2109 }
2111 void os::signal_notify(int sig) {
2112 Atomic::inc(&pending_signals[sig]);
2113 ::sem_post(&sig_sem);
2114 }
2116 static int check_pending_signals(bool wait) {
2117 Atomic::store(0, &sigint_count);
2118 for (;;) {
2119 for (int i = 0; i < NSIG + 1; i++) {
2120 jint n = pending_signals[i];
2121 if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
2122 return i;
2123 }
2124 }
2125 if (!wait) {
2126 return -1;
2127 }
2128 JavaThread *thread = JavaThread::current();
2129 ThreadBlockInVM tbivm(thread);
2131 bool threadIsSuspended;
2132 do {
2133 thread->set_suspend_equivalent();
2134 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2135 ::sem_wait(&sig_sem);
2137 // were we externally suspended while we were waiting?
2138 threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
2139 if (threadIsSuspended) {
2140 //
2141 // The semaphore has been incremented, but while we were waiting
2142 // another thread suspended us. We don't want to continue running
2143 // while suspended because that would surprise the thread that
2144 // suspended us.
2145 //
2146 ::sem_post(&sig_sem);
2148 thread->java_suspend_self();
2149 }
2150 } while (threadIsSuspended);
2151 }
2152 }
2154 int os::signal_lookup() {
2155 return check_pending_signals(false);
2156 }
2158 int os::signal_wait() {
2159 return check_pending_signals(true);
2160 }
2162 ////////////////////////////////////////////////////////////////////////////////
2163 // Virtual Memory
2165 int os::vm_page_size() {
2166 // Seems redundant as all get out
2167 assert(os::Linux::page_size() != -1, "must call os::init");
2168 return os::Linux::page_size();
2169 }
2171 // Solaris allocates memory by pages.
2172 int os::vm_allocation_granularity() {
2173 assert(os::Linux::page_size() != -1, "must call os::init");
2174 return os::Linux::page_size();
2175 }
2177 // Rationale behind this function:
2178 // current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable
2179 // mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get
2180 // samples for JITted code. Here we create private executable mapping over the code cache
2181 // and then we can use standard (well, almost, as mapping can change) way to provide
2182 // info for the reporting script by storing timestamp and location of symbol
2183 void linux_wrap_code(char* base, size_t size) {
2184 static volatile jint cnt = 0;
2186 if (!UseOprofile) {
2187 return;
2188 }
2190 char buf[40];
2191 int num = Atomic::add(1, &cnt);
2193 sprintf(buf, "/tmp/hs-vm-%d-%d", os::current_process_id(), num);
2194 unlink(buf);
2196 int fd = open(buf, O_CREAT | O_RDWR, S_IRWXU);
2198 if (fd != -1) {
2199 off_t rv = lseek(fd, size-2, SEEK_SET);
2200 if (rv != (off_t)-1) {
2201 if (write(fd, "", 1) == 1) {
2202 mmap(base, size,
2203 PROT_READ|PROT_WRITE|PROT_EXEC,
2204 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0);
2205 }
2206 }
2207 close(fd);
2208 unlink(buf);
2209 }
2210 }
2212 // NOTE: Linux kernel does not really reserve the pages for us.
2213 // All it does is to check if there are enough free pages
2214 // left at the time of mmap(). This could be a potential
2215 // problem.
2216 bool os::commit_memory(char* addr, size_t size) {
2217 uintptr_t res = (uintptr_t) ::mmap(addr, size,
2218 PROT_READ|PROT_WRITE|PROT_EXEC,
2219 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);
2220 return res != (uintptr_t) MAP_FAILED;
2221 }
2223 bool os::commit_memory(char* addr, size_t size, size_t alignment_hint) {
2224 return commit_memory(addr, size);
2225 }
2227 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) { }
2229 void os::free_memory(char *addr, size_t bytes) {
2230 uncommit_memory(addr, bytes);
2231 }
2233 void os::numa_make_global(char *addr, size_t bytes) { }
2235 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
2236 Linux::numa_tonode_memory(addr, bytes, lgrp_hint);
2237 }
2239 bool os::numa_topology_changed() { return false; }
2241 size_t os::numa_get_groups_num() {
2242 int max_node = Linux::numa_max_node();
2243 return max_node > 0 ? max_node + 1 : 1;
2244 }
2246 int os::numa_get_group_id() {
2247 int cpu_id = Linux::sched_getcpu();
2248 if (cpu_id != -1) {
2249 int lgrp_id = Linux::get_node_by_cpu(cpu_id);
2250 if (lgrp_id != -1) {
2251 return lgrp_id;
2252 }
2253 }
2254 return 0;
2255 }
2257 size_t os::numa_get_leaf_groups(int *ids, size_t size) {
2258 for (size_t i = 0; i < size; i++) {
2259 ids[i] = i;
2260 }
2261 return size;
2262 }
2264 bool os::get_page_info(char *start, page_info* info) {
2265 return false;
2266 }
2268 char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {
2269 return end;
2270 }
2272 extern "C" void numa_warn(int number, char *where, ...) { }
2273 extern "C" void numa_error(char *where) { }
2275 void os::Linux::libnuma_init() {
2276 // sched_getcpu() should be in libc.
2277 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
2278 dlsym(RTLD_DEFAULT, "sched_getcpu")));
2280 if (sched_getcpu() != -1) { // Does it work?
2281 void *handle = dlopen("libnuma.so", RTLD_LAZY);
2282 if (handle != NULL) {
2283 set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,
2284 dlsym(handle, "numa_node_to_cpus")));
2285 set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,
2286 dlsym(handle, "numa_max_node")));
2287 set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,
2288 dlsym(handle, "numa_available")));
2289 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,
2290 dlsym(handle, "numa_tonode_memory")));
2291 if (numa_available() != -1) {
2292 // Create a cpu -> node mapping
2293 _cpu_to_node = new (ResourceObj::C_HEAP) GrowableArray<int>(0, true);
2294 rebuild_cpu_to_node_map();
2295 }
2296 }
2297 }
2298 }
2300 // rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.
2301 // The table is later used in get_node_by_cpu().
2302 void os::Linux::rebuild_cpu_to_node_map() {
2303 int cpu_num = os::active_processor_count();
2304 cpu_to_node()->clear();
2305 cpu_to_node()->at_grow(cpu_num - 1);
2306 int node_num = numa_get_groups_num();
2307 int cpu_map_size = (cpu_num + BitsPerLong - 1) / BitsPerLong;
2308 unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size);
2309 for (int i = 0; i < node_num; i++) {
2310 if (numa_node_to_cpus(i, cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
2311 for (int j = 0; j < cpu_map_size; j++) {
2312 if (cpu_map[j] != 0) {
2313 for (int k = 0; k < BitsPerLong; k++) {
2314 if (cpu_map[j] & (1UL << k)) {
2315 cpu_to_node()->at_put(j * BitsPerLong + k, i);
2316 }
2317 }
2318 }
2319 }
2320 }
2321 }
2322 FREE_C_HEAP_ARRAY(unsigned long, cpu_map);
2323 }
2325 int os::Linux::get_node_by_cpu(int cpu_id) {
2326 if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {
2327 return cpu_to_node()->at(cpu_id);
2328 }
2329 return -1;
2330 }
2332 GrowableArray<int>* os::Linux::_cpu_to_node;
2333 os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;
2334 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;
2335 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;
2336 os::Linux::numa_available_func_t os::Linux::_numa_available;
2337 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;
2340 bool os::uncommit_memory(char* addr, size_t size) {
2341 return ::mmap(addr, size,
2342 PROT_READ|PROT_WRITE|PROT_EXEC,
2343 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0)
2344 != MAP_FAILED;
2345 }
2347 static address _highest_vm_reserved_address = NULL;
2349 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory
2350 // at 'requested_addr'. If there are existing memory mappings at the same
2351 // location, however, they will be overwritten. If 'fixed' is false,
2352 // 'requested_addr' is only treated as a hint, the return value may or
2353 // may not start from the requested address. Unlike Linux mmap(), this
2354 // function returns NULL to indicate failure.
2355 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {
2356 char * addr;
2357 int flags;
2359 flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;
2360 if (fixed) {
2361 assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address");
2362 flags |= MAP_FIXED;
2363 }
2365 addr = (char*)::mmap(requested_addr, bytes, PROT_READ|PROT_WRITE|PROT_EXEC,
2366 flags, -1, 0);
2368 if (addr != MAP_FAILED) {
2369 // anon_mmap() should only get called during VM initialization,
2370 // don't need lock (actually we can skip locking even it can be called
2371 // from multiple threads, because _highest_vm_reserved_address is just a
2372 // hint about the upper limit of non-stack memory regions.)
2373 if ((address)addr + bytes > _highest_vm_reserved_address) {
2374 _highest_vm_reserved_address = (address)addr + bytes;
2375 }
2376 }
2378 return addr == MAP_FAILED ? NULL : addr;
2379 }
2381 // Don't update _highest_vm_reserved_address, because there might be memory
2382 // regions above addr + size. If so, releasing a memory region only creates
2383 // a hole in the address space, it doesn't help prevent heap-stack collision.
2384 //
2385 static int anon_munmap(char * addr, size_t size) {
2386 return ::munmap(addr, size) == 0;
2387 }
2389 char* os::reserve_memory(size_t bytes, char* requested_addr,
2390 size_t alignment_hint) {
2391 return anon_mmap(requested_addr, bytes, (requested_addr != NULL));
2392 }
2394 bool os::release_memory(char* addr, size_t size) {
2395 return anon_munmap(addr, size);
2396 }
2398 static address highest_vm_reserved_address() {
2399 return _highest_vm_reserved_address;
2400 }
2402 static bool linux_mprotect(char* addr, size_t size, int prot) {
2403 // Linux wants the mprotect address argument to be page aligned.
2404 char* bottom = (char*)align_size_down((intptr_t)addr, os::Linux::page_size());
2406 // According to SUSv3, mprotect() should only be used with mappings
2407 // established by mmap(), and mmap() always maps whole pages. Unaligned
2408 // 'addr' likely indicates problem in the VM (e.g. trying to change
2409 // protection of malloc'ed or statically allocated memory). Check the
2410 // caller if you hit this assert.
2411 assert(addr == bottom, "sanity check");
2413 size = align_size_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());
2414 return ::mprotect(bottom, size, prot) == 0;
2415 }
2417 // Set protections specified
2418 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
2419 bool is_committed) {
2420 unsigned int p = 0;
2421 switch (prot) {
2422 case MEM_PROT_NONE: p = PROT_NONE; break;
2423 case MEM_PROT_READ: p = PROT_READ; break;
2424 case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break;
2425 case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break;
2426 default:
2427 ShouldNotReachHere();
2428 }
2429 // is_committed is unused.
2430 return linux_mprotect(addr, bytes, p);
2431 }
2433 bool os::guard_memory(char* addr, size_t size) {
2434 return linux_mprotect(addr, size, PROT_NONE);
2435 }
2437 bool os::unguard_memory(char* addr, size_t size) {
2438 return linux_mprotect(addr, size, PROT_READ|PROT_WRITE|PROT_EXEC);
2439 }
2441 // Large page support
2443 static size_t _large_page_size = 0;
2445 bool os::large_page_init() {
2446 if (!UseLargePages) return false;
2448 if (LargePageSizeInBytes) {
2449 _large_page_size = LargePageSizeInBytes;
2450 } else {
2451 // large_page_size on Linux is used to round up heap size. x86 uses either
2452 // 2M or 4M page, depending on whether PAE (Physical Address Extensions)
2453 // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
2454 // page as large as 256M.
2455 //
2456 // Here we try to figure out page size by parsing /proc/meminfo and looking
2457 // for a line with the following format:
2458 // Hugepagesize: 2048 kB
2459 //
2460 // If we can't determine the value (e.g. /proc is not mounted, or the text
2461 // format has been changed), we'll use the largest page size supported by
2462 // the processor.
2464 _large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M);
2466 FILE *fp = fopen("/proc/meminfo", "r");
2467 if (fp) {
2468 while (!feof(fp)) {
2469 int x = 0;
2470 char buf[16];
2471 if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
2472 if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
2473 _large_page_size = x * K;
2474 break;
2475 }
2476 } else {
2477 // skip to next line
2478 for (;;) {
2479 int ch = fgetc(fp);
2480 if (ch == EOF || ch == (int)'\n') break;
2481 }
2482 }
2483 }
2484 fclose(fp);
2485 }
2486 }
2488 const size_t default_page_size = (size_t)Linux::page_size();
2489 if (_large_page_size > default_page_size) {
2490 _page_sizes[0] = _large_page_size;
2491 _page_sizes[1] = default_page_size;
2492 _page_sizes[2] = 0;
2493 }
2495 // Large page support is available on 2.6 or newer kernel, some vendors
2496 // (e.g. Redhat) have backported it to their 2.4 based distributions.
2497 // We optimistically assume the support is available. If later it turns out
2498 // not true, VM will automatically switch to use regular page size.
2499 return true;
2500 }
2502 #ifndef SHM_HUGETLB
2503 #define SHM_HUGETLB 04000
2504 #endif
2506 char* os::reserve_memory_special(size_t bytes) {
2507 assert(UseLargePages, "only for large pages");
2509 key_t key = IPC_PRIVATE;
2510 char *addr;
2512 bool warn_on_failure = UseLargePages &&
2513 (!FLAG_IS_DEFAULT(UseLargePages) ||
2514 !FLAG_IS_DEFAULT(LargePageSizeInBytes)
2515 );
2516 char msg[128];
2518 // Create a large shared memory region to attach to based on size.
2519 // Currently, size is the total size of the heap
2520 int shmid = shmget(key, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
2521 if (shmid == -1) {
2522 // Possible reasons for shmget failure:
2523 // 1. shmmax is too small for Java heap.
2524 // > check shmmax value: cat /proc/sys/kernel/shmmax
2525 // > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax
2526 // 2. not enough large page memory.
2527 // > check available large pages: cat /proc/meminfo
2528 // > increase amount of large pages:
2529 // echo new_value > /proc/sys/vm/nr_hugepages
2530 // Note 1: different Linux may use different name for this property,
2531 // e.g. on Redhat AS-3 it is "hugetlb_pool".
2532 // Note 2: it's possible there's enough physical memory available but
2533 // they are so fragmented after a long run that they can't
2534 // coalesce into large pages. Try to reserve large pages when
2535 // the system is still "fresh".
2536 if (warn_on_failure) {
2537 jio_snprintf(msg, sizeof(msg), "Failed to reserve shared memory (errno = %d).", errno);
2538 warning(msg);
2539 }
2540 return NULL;
2541 }
2543 // attach to the region
2544 addr = (char*)shmat(shmid, NULL, 0);
2545 int err = errno;
2547 // Remove shmid. If shmat() is successful, the actual shared memory segment
2548 // will be deleted when it's detached by shmdt() or when the process
2549 // terminates. If shmat() is not successful this will remove the shared
2550 // segment immediately.
2551 shmctl(shmid, IPC_RMID, NULL);
2553 if ((intptr_t)addr == -1) {
2554 if (warn_on_failure) {
2555 jio_snprintf(msg, sizeof(msg), "Failed to attach shared memory (errno = %d).", err);
2556 warning(msg);
2557 }
2558 return NULL;
2559 }
2561 return addr;
2562 }
2564 bool os::release_memory_special(char* base, size_t bytes) {
2565 // detaching the SHM segment will also delete it, see reserve_memory_special()
2566 int rslt = shmdt(base);
2567 return rslt == 0;
2568 }
2570 size_t os::large_page_size() {
2571 return _large_page_size;
2572 }
2574 // Linux does not support anonymous mmap with large page memory. The only way
2575 // to reserve large page memory without file backing is through SysV shared
2576 // memory API. The entire memory region is committed and pinned upfront.
2577 // Hopefully this will change in the future...
2578 bool os::can_commit_large_page_memory() {
2579 return false;
2580 }
2582 bool os::can_execute_large_page_memory() {
2583 return false;
2584 }
2586 // Reserve memory at an arbitrary address, only if that area is
2587 // available (and not reserved for something else).
2589 char* os::attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
2590 const int max_tries = 10;
2591 char* base[max_tries];
2592 size_t size[max_tries];
2593 const size_t gap = 0x000000;
2595 // Assert only that the size is a multiple of the page size, since
2596 // that's all that mmap requires, and since that's all we really know
2597 // about at this low abstraction level. If we need higher alignment,
2598 // we can either pass an alignment to this method or verify alignment
2599 // in one of the methods further up the call chain. See bug 5044738.
2600 assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");
2602 // Repeatedly allocate blocks until the block is allocated at the
2603 // right spot. Give up after max_tries. Note that reserve_memory() will
2604 // automatically update _highest_vm_reserved_address if the call is
2605 // successful. The variable tracks the highest memory address every reserved
2606 // by JVM. It is used to detect heap-stack collision if running with
2607 // fixed-stack LinuxThreads. Because here we may attempt to reserve more
2608 // space than needed, it could confuse the collision detecting code. To
2609 // solve the problem, save current _highest_vm_reserved_address and
2610 // calculate the correct value before return.
2611 address old_highest = _highest_vm_reserved_address;
2613 // Linux mmap allows caller to pass an address as hint; give it a try first,
2614 // if kernel honors the hint then we can return immediately.
2615 char * addr = anon_mmap(requested_addr, bytes, false);
2616 if (addr == requested_addr) {
2617 return requested_addr;
2618 }
2620 if (addr != NULL) {
2621 // mmap() is successful but it fails to reserve at the requested address
2622 anon_munmap(addr, bytes);
2623 }
2625 int i;
2626 for (i = 0; i < max_tries; ++i) {
2627 base[i] = reserve_memory(bytes);
2629 if (base[i] != NULL) {
2630 // Is this the block we wanted?
2631 if (base[i] == requested_addr) {
2632 size[i] = bytes;
2633 break;
2634 }
2636 // Does this overlap the block we wanted? Give back the overlapped
2637 // parts and try again.
2639 size_t top_overlap = requested_addr + (bytes + gap) - base[i];
2640 if (top_overlap >= 0 && top_overlap < bytes) {
2641 unmap_memory(base[i], top_overlap);
2642 base[i] += top_overlap;
2643 size[i] = bytes - top_overlap;
2644 } else {
2645 size_t bottom_overlap = base[i] + bytes - requested_addr;
2646 if (bottom_overlap >= 0 && bottom_overlap < bytes) {
2647 unmap_memory(requested_addr, bottom_overlap);
2648 size[i] = bytes - bottom_overlap;
2649 } else {
2650 size[i] = bytes;
2651 }
2652 }
2653 }
2654 }
2656 // Give back the unused reserved pieces.
2658 for (int j = 0; j < i; ++j) {
2659 if (base[j] != NULL) {
2660 unmap_memory(base[j], size[j]);
2661 }
2662 }
2664 if (i < max_tries) {
2665 _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
2666 return requested_addr;
2667 } else {
2668 _highest_vm_reserved_address = old_highest;
2669 return NULL;
2670 }
2671 }
2673 size_t os::read(int fd, void *buf, unsigned int nBytes) {
2674 return ::read(fd, buf, nBytes);
2675 }
2677 // TODO-FIXME: reconcile Solaris' os::sleep with the linux variation.
2678 // Solaris uses poll(), linux uses park().
2679 // Poll() is likely a better choice, assuming that Thread.interrupt()
2680 // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with
2681 // SIGSEGV, see 4355769.
2683 const int NANOSECS_PER_MILLISECS = 1000000;
2685 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
2686 assert(thread == Thread::current(), "thread consistency check");
2688 ParkEvent * const slp = thread->_SleepEvent ;
2689 slp->reset() ;
2690 OrderAccess::fence() ;
2692 if (interruptible) {
2693 jlong prevtime = javaTimeNanos();
2695 for (;;) {
2696 if (os::is_interrupted(thread, true)) {
2697 return OS_INTRPT;
2698 }
2700 jlong newtime = javaTimeNanos();
2702 if (newtime - prevtime < 0) {
2703 // time moving backwards, should only happen if no monotonic clock
2704 // not a guarantee() because JVM should not abort on kernel/glibc bugs
2705 assert(!Linux::supports_monotonic_clock(), "time moving backwards");
2706 } else {
2707 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISECS;
2708 }
2710 if(millis <= 0) {
2711 return OS_OK;
2712 }
2714 prevtime = newtime;
2716 {
2717 assert(thread->is_Java_thread(), "sanity check");
2718 JavaThread *jt = (JavaThread *) thread;
2719 ThreadBlockInVM tbivm(jt);
2720 OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
2722 jt->set_suspend_equivalent();
2723 // cleared by handle_special_suspend_equivalent_condition() or
2724 // java_suspend_self() via check_and_wait_while_suspended()
2726 slp->park(millis);
2728 // were we externally suspended while we were waiting?
2729 jt->check_and_wait_while_suspended();
2730 }
2731 }
2732 } else {
2733 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
2734 jlong prevtime = javaTimeNanos();
2736 for (;;) {
2737 // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
2738 // the 1st iteration ...
2739 jlong newtime = javaTimeNanos();
2741 if (newtime - prevtime < 0) {
2742 // time moving backwards, should only happen if no monotonic clock
2743 // not a guarantee() because JVM should not abort on kernel/glibc bugs
2744 assert(!Linux::supports_monotonic_clock(), "time moving backwards");
2745 } else {
2746 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISECS;
2747 }
2749 if(millis <= 0) break ;
2751 prevtime = newtime;
2752 slp->park(millis);
2753 }
2754 return OS_OK ;
2755 }
2756 }
2758 int os::naked_sleep() {
2759 // %% make the sleep time an integer flag. for now use 1 millisec.
2760 return os::sleep(Thread::current(), 1, false);
2761 }
2763 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
2764 void os::infinite_sleep() {
2765 while (true) { // sleep forever ...
2766 ::sleep(100); // ... 100 seconds at a time
2767 }
2768 }
2770 // Used to convert frequent JVM_Yield() to nops
2771 bool os::dont_yield() {
2772 return DontYieldALot;
2773 }
2775 void os::yield() {
2776 sched_yield();
2777 }
2779 os::YieldResult os::NakedYield() { sched_yield(); return os::YIELD_UNKNOWN ;}
2781 void os::yield_all(int attempts) {
2782 // Yields to all threads, including threads with lower priorities
2783 // Threads on Linux are all with same priority. The Solaris style
2784 // os::yield_all() with nanosleep(1ms) is not necessary.
2785 sched_yield();
2786 }
2788 // Called from the tight loops to possibly influence time-sharing heuristics
2789 void os::loop_breaker(int attempts) {
2790 os::yield_all(attempts);
2791 }
2793 ////////////////////////////////////////////////////////////////////////////////
2794 // thread priority support
2796 // Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER
2797 // only supports dynamic priority, static priority must be zero. For real-time
2798 // applications, Linux supports SCHED_RR which allows static priority (1-99).
2799 // However, for large multi-threaded applications, SCHED_RR is not only slower
2800 // than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out
2801 // of 5 runs - Sep 2005).
2802 //
2803 // The following code actually changes the niceness of kernel-thread/LWP. It
2804 // has an assumption that setpriority() only modifies one kernel-thread/LWP,
2805 // not the entire user process, and user level threads are 1:1 mapped to kernel
2806 // threads. It has always been the case, but could change in the future. For
2807 // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
2808 // It is only used when ThreadPriorityPolicy=1 and requires root privilege.
2810 int os::java_to_os_priority[MaxPriority + 1] = {
2811 19, // 0 Entry should never be used
2813 4, // 1 MinPriority
2814 3, // 2
2815 2, // 3
2817 1, // 4
2818 0, // 5 NormPriority
2819 -1, // 6
2821 -2, // 7
2822 -3, // 8
2823 -4, // 9 NearMaxPriority
2825 -5 // 10 MaxPriority
2826 };
2828 static int prio_init() {
2829 if (ThreadPriorityPolicy == 1) {
2830 // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1
2831 // if effective uid is not root. Perhaps, a more elegant way of doing
2832 // this is to test CAP_SYS_NICE capability, but that will require libcap.so
2833 if (geteuid() != 0) {
2834 if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
2835 warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");
2836 }
2837 ThreadPriorityPolicy = 0;
2838 }
2839 }
2840 return 0;
2841 }
2843 OSReturn os::set_native_priority(Thread* thread, int newpri) {
2844 if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK;
2846 int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri);
2847 return (ret == 0) ? OS_OK : OS_ERR;
2848 }
2850 OSReturn os::get_native_priority(const Thread* const thread, int *priority_ptr) {
2851 if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) {
2852 *priority_ptr = java_to_os_priority[NormPriority];
2853 return OS_OK;
2854 }
2856 errno = 0;
2857 *priority_ptr = getpriority(PRIO_PROCESS, thread->osthread()->thread_id());
2858 return (*priority_ptr != -1 || errno == 0 ? OS_OK : OS_ERR);
2859 }
2861 // Hint to the underlying OS that a task switch would not be good.
2862 // Void return because it's a hint and can fail.
2863 void os::hint_no_preempt() {}
2865 ////////////////////////////////////////////////////////////////////////////////
2866 // suspend/resume support
2868 // the low-level signal-based suspend/resume support is a remnant from the
2869 // old VM-suspension that used to be for java-suspension, safepoints etc,
2870 // within hotspot. Now there is a single use-case for this:
2871 // - calling get_thread_pc() on the VMThread by the flat-profiler task
2872 // that runs in the watcher thread.
2873 // The remaining code is greatly simplified from the more general suspension
2874 // code that used to be used.
2875 //
2876 // The protocol is quite simple:
2877 // - suspend:
2878 // - sends a signal to the target thread
2879 // - polls the suspend state of the osthread using a yield loop
2880 // - target thread signal handler (SR_handler) sets suspend state
2881 // and blocks in sigsuspend until continued
2882 // - resume:
2883 // - sets target osthread state to continue
2884 // - sends signal to end the sigsuspend loop in the SR_handler
2885 //
2886 // Note that the SR_lock plays no role in this suspend/resume protocol.
2887 //
2889 static void resume_clear_context(OSThread *osthread) {
2890 osthread->set_ucontext(NULL);
2891 osthread->set_siginfo(NULL);
2893 // notify the suspend action is completed, we have now resumed
2894 osthread->sr.clear_suspended();
2895 }
2897 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {
2898 osthread->set_ucontext(context);
2899 osthread->set_siginfo(siginfo);
2900 }
2902 //
2903 // Handler function invoked when a thread's execution is suspended or
2904 // resumed. We have to be careful that only async-safe functions are
2905 // called here (Note: most pthread functions are not async safe and
2906 // should be avoided.)
2907 //
2908 // Note: sigwait() is a more natural fit than sigsuspend() from an
2909 // interface point of view, but sigwait() prevents the signal hander
2910 // from being run. libpthread would get very confused by not having
2911 // its signal handlers run and prevents sigwait()'s use with the
2912 // mutex granting granting signal.
2913 //
2914 // Currently only ever called on the VMThread
2915 //
2916 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {
2917 // Save and restore errno to avoid confusing native code with EINTR
2918 // after sigsuspend.
2919 int old_errno = errno;
2921 Thread* thread = Thread::current();
2922 OSThread* osthread = thread->osthread();
2923 assert(thread->is_VM_thread(), "Must be VMThread");
2924 // read current suspend action
2925 int action = osthread->sr.suspend_action();
2926 if (action == SR_SUSPEND) {
2927 suspend_save_context(osthread, siginfo, context);
2929 // Notify the suspend action is about to be completed. do_suspend()
2930 // waits until SR_SUSPENDED is set and then returns. We will wait
2931 // here for a resume signal and that completes the suspend-other
2932 // action. do_suspend/do_resume is always called as a pair from
2933 // the same thread - so there are no races
2935 // notify the caller
2936 osthread->sr.set_suspended();
2938 sigset_t suspend_set; // signals for sigsuspend()
2940 // get current set of blocked signals and unblock resume signal
2941 pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
2942 sigdelset(&suspend_set, SR_signum);
2944 // wait here until we are resumed
2945 do {
2946 sigsuspend(&suspend_set);
2947 // ignore all returns until we get a resume signal
2948 } while (osthread->sr.suspend_action() != SR_CONTINUE);
2950 resume_clear_context(osthread);
2952 } else {
2953 assert(action == SR_CONTINUE, "unexpected sr action");
2954 // nothing special to do - just leave the handler
2955 }
2957 errno = old_errno;
2958 }
2961 static int SR_initialize() {
2962 struct sigaction act;
2963 char *s;
2964 /* Get signal number to use for suspend/resume */
2965 if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {
2966 int sig = ::strtol(s, 0, 10);
2967 if (sig > 0 || sig < _NSIG) {
2968 SR_signum = sig;
2969 }
2970 }
2972 assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,
2973 "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
2975 sigemptyset(&SR_sigset);
2976 sigaddset(&SR_sigset, SR_signum);
2978 /* Set up signal handler for suspend/resume */
2979 act.sa_flags = SA_RESTART|SA_SIGINFO;
2980 act.sa_handler = (void (*)(int)) SR_handler;
2982 // SR_signum is blocked by default.
2983 // 4528190 - We also need to block pthread restart signal (32 on all
2984 // supported Linux platforms). Note that LinuxThreads need to block
2985 // this signal for all threads to work properly. So we don't have
2986 // to use hard-coded signal number when setting up the mask.
2987 pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);
2989 if (sigaction(SR_signum, &act, 0) == -1) {
2990 return -1;
2991 }
2993 // Save signal flag
2994 os::Linux::set_our_sigflags(SR_signum, act.sa_flags);
2995 return 0;
2996 }
2998 static int SR_finalize() {
2999 return 0;
3000 }
3003 // returns true on success and false on error - really an error is fatal
3004 // but this seems the normal response to library errors
3005 static bool do_suspend(OSThread* osthread) {
3006 // mark as suspended and send signal
3007 osthread->sr.set_suspend_action(SR_SUSPEND);
3008 int status = pthread_kill(osthread->pthread_id(), SR_signum);
3009 assert_status(status == 0, status, "pthread_kill");
3011 // check status and wait until notified of suspension
3012 if (status == 0) {
3013 for (int i = 0; !osthread->sr.is_suspended(); i++) {
3014 os::yield_all(i);
3015 }
3016 osthread->sr.set_suspend_action(SR_NONE);
3017 return true;
3018 }
3019 else {
3020 osthread->sr.set_suspend_action(SR_NONE);
3021 return false;
3022 }
3023 }
3025 static void do_resume(OSThread* osthread) {
3026 assert(osthread->sr.is_suspended(), "thread should be suspended");
3027 osthread->sr.set_suspend_action(SR_CONTINUE);
3029 int status = pthread_kill(osthread->pthread_id(), SR_signum);
3030 assert_status(status == 0, status, "pthread_kill");
3031 // check status and wait unit notified of resumption
3032 if (status == 0) {
3033 for (int i = 0; osthread->sr.is_suspended(); i++) {
3034 os::yield_all(i);
3035 }
3036 }
3037 osthread->sr.set_suspend_action(SR_NONE);
3038 }
3040 ////////////////////////////////////////////////////////////////////////////////
3041 // interrupt support
3043 void os::interrupt(Thread* thread) {
3044 assert(Thread::current() == thread || Threads_lock->owned_by_self(),
3045 "possibility of dangling Thread pointer");
3047 OSThread* osthread = thread->osthread();
3049 if (!osthread->interrupted()) {
3050 osthread->set_interrupted(true);
3051 // More than one thread can get here with the same value of osthread,
3052 // resulting in multiple notifications. We do, however, want the store
3053 // to interrupted() to be visible to other threads before we execute unpark().
3054 OrderAccess::fence();
3055 ParkEvent * const slp = thread->_SleepEvent ;
3056 if (slp != NULL) slp->unpark() ;
3057 }
3059 // For JSR166. Unpark even if interrupt status already was set
3060 if (thread->is_Java_thread())
3061 ((JavaThread*)thread)->parker()->unpark();
3063 ParkEvent * ev = thread->_ParkEvent ;
3064 if (ev != NULL) ev->unpark() ;
3066 }
3068 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
3069 assert(Thread::current() == thread || Threads_lock->owned_by_self(),
3070 "possibility of dangling Thread pointer");
3072 OSThread* osthread = thread->osthread();
3074 bool interrupted = osthread->interrupted();
3076 if (interrupted && clear_interrupted) {
3077 osthread->set_interrupted(false);
3078 // consider thread->_SleepEvent->reset() ... optional optimization
3079 }
3081 return interrupted;
3082 }
3084 ///////////////////////////////////////////////////////////////////////////////////
3085 // signal handling (except suspend/resume)
3087 // This routine may be used by user applications as a "hook" to catch signals.
3088 // The user-defined signal handler must pass unrecognized signals to this
3089 // routine, and if it returns true (non-zero), then the signal handler must
3090 // return immediately. If the flag "abort_if_unrecognized" is true, then this
3091 // routine will never retun false (zero), but instead will execute a VM panic
3092 // routine kill the process.
3093 //
3094 // If this routine returns false, it is OK to call it again. This allows
3095 // the user-defined signal handler to perform checks either before or after
3096 // the VM performs its own checks. Naturally, the user code would be making
3097 // a serious error if it tried to handle an exception (such as a null check
3098 // or breakpoint) that the VM was generating for its own correct operation.
3099 //
3100 // This routine may recognize any of the following kinds of signals:
3101 // SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.
3102 // It should be consulted by handlers for any of those signals.
3103 //
3104 // The caller of this routine must pass in the three arguments supplied
3105 // to the function referred to in the "sa_sigaction" (not the "sa_handler")
3106 // field of the structure passed to sigaction(). This routine assumes that
3107 // the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART.
3108 //
3109 // Note that the VM will print warnings if it detects conflicting signal
3110 // handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers".
3111 //
3112 extern "C" int
3113 JVM_handle_linux_signal(int signo, siginfo_t* siginfo,
3114 void* ucontext, int abort_if_unrecognized);
3116 void signalHandler(int sig, siginfo_t* info, void* uc) {
3117 assert(info != NULL && uc != NULL, "it must be old kernel");
3118 JVM_handle_linux_signal(sig, info, uc, true);
3119 }
3122 // This boolean allows users to forward their own non-matching signals
3123 // to JVM_handle_linux_signal, harmlessly.
3124 bool os::Linux::signal_handlers_are_installed = false;
3126 // For signal-chaining
3127 struct sigaction os::Linux::sigact[MAXSIGNUM];
3128 unsigned int os::Linux::sigs = 0;
3129 bool os::Linux::libjsig_is_loaded = false;
3130 typedef struct sigaction *(*get_signal_t)(int);
3131 get_signal_t os::Linux::get_signal_action = NULL;
3133 struct sigaction* os::Linux::get_chained_signal_action(int sig) {
3134 struct sigaction *actp = NULL;
3136 if (libjsig_is_loaded) {
3137 // Retrieve the old signal handler from libjsig
3138 actp = (*get_signal_action)(sig);
3139 }
3140 if (actp == NULL) {
3141 // Retrieve the preinstalled signal handler from jvm
3142 actp = get_preinstalled_handler(sig);
3143 }
3145 return actp;
3146 }
3148 static bool call_chained_handler(struct sigaction *actp, int sig,
3149 siginfo_t *siginfo, void *context) {
3150 // Call the old signal handler
3151 if (actp->sa_handler == SIG_DFL) {
3152 // It's more reasonable to let jvm treat it as an unexpected exception
3153 // instead of taking the default action.
3154 return false;
3155 } else if (actp->sa_handler != SIG_IGN) {
3156 if ((actp->sa_flags & SA_NODEFER) == 0) {
3157 // automaticlly block the signal
3158 sigaddset(&(actp->sa_mask), sig);
3159 }
3161 sa_handler_t hand;
3162 sa_sigaction_t sa;
3163 bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0;
3164 // retrieve the chained handler
3165 if (siginfo_flag_set) {
3166 sa = actp->sa_sigaction;
3167 } else {
3168 hand = actp->sa_handler;
3169 }
3171 if ((actp->sa_flags & SA_RESETHAND) != 0) {
3172 actp->sa_handler = SIG_DFL;
3173 }
3175 // try to honor the signal mask
3176 sigset_t oset;
3177 pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset);
3179 // call into the chained handler
3180 if (siginfo_flag_set) {
3181 (*sa)(sig, siginfo, context);
3182 } else {
3183 (*hand)(sig);
3184 }
3186 // restore the signal mask
3187 pthread_sigmask(SIG_SETMASK, &oset, 0);
3188 }
3189 // Tell jvm's signal handler the signal is taken care of.
3190 return true;
3191 }
3193 bool os::Linux::chained_handler(int sig, siginfo_t* siginfo, void* context) {
3194 bool chained = false;
3195 // signal-chaining
3196 if (UseSignalChaining) {
3197 struct sigaction *actp = get_chained_signal_action(sig);
3198 if (actp != NULL) {
3199 chained = call_chained_handler(actp, sig, siginfo, context);
3200 }
3201 }
3202 return chained;
3203 }
3205 struct sigaction* os::Linux::get_preinstalled_handler(int sig) {
3206 if ((( (unsigned int)1 << sig ) & sigs) != 0) {
3207 return &sigact[sig];
3208 }
3209 return NULL;
3210 }
3212 void os::Linux::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
3213 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
3214 sigact[sig] = oldAct;
3215 sigs |= (unsigned int)1 << sig;
3216 }
3218 // for diagnostic
3219 int os::Linux::sigflags[MAXSIGNUM];
3221 int os::Linux::get_our_sigflags(int sig) {
3222 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
3223 return sigflags[sig];
3224 }
3226 void os::Linux::set_our_sigflags(int sig, int flags) {
3227 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
3228 sigflags[sig] = flags;
3229 }
3231 void os::Linux::set_signal_handler(int sig, bool set_installed) {
3232 // Check for overwrite.
3233 struct sigaction oldAct;
3234 sigaction(sig, (struct sigaction*)NULL, &oldAct);
3236 void* oldhand = oldAct.sa_sigaction
3237 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
3238 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
3239 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
3240 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
3241 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) {
3242 if (AllowUserSignalHandlers || !set_installed) {
3243 // Do not overwrite; user takes responsibility to forward to us.
3244 return;
3245 } else if (UseSignalChaining) {
3246 // save the old handler in jvm
3247 save_preinstalled_handler(sig, oldAct);
3248 // libjsig also interposes the sigaction() call below and saves the
3249 // old sigaction on it own.
3250 } else {
3251 fatal2("Encountered unexpected pre-existing sigaction handler %#lx for signal %d.", (long)oldhand, sig);
3252 }
3253 }
3255 struct sigaction sigAct;
3256 sigfillset(&(sigAct.sa_mask));
3257 sigAct.sa_handler = SIG_DFL;
3258 if (!set_installed) {
3259 sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
3260 } else {
3261 sigAct.sa_sigaction = signalHandler;
3262 sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
3263 }
3264 // Save flags, which are set by ours
3265 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
3266 sigflags[sig] = sigAct.sa_flags;
3268 int ret = sigaction(sig, &sigAct, &oldAct);
3269 assert(ret == 0, "check");
3271 void* oldhand2 = oldAct.sa_sigaction
3272 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
3273 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
3274 assert(oldhand2 == oldhand, "no concurrent signal handler installation");
3275 }
3277 // install signal handlers for signals that HotSpot needs to
3278 // handle in order to support Java-level exception handling.
3280 void os::Linux::install_signal_handlers() {
3281 if (!signal_handlers_are_installed) {
3282 signal_handlers_are_installed = true;
3284 // signal-chaining
3285 typedef void (*signal_setting_t)();
3286 signal_setting_t begin_signal_setting = NULL;
3287 signal_setting_t end_signal_setting = NULL;
3288 begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
3289 dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting"));
3290 if (begin_signal_setting != NULL) {
3291 end_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
3292 dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));
3293 get_signal_action = CAST_TO_FN_PTR(get_signal_t,
3294 dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));
3295 libjsig_is_loaded = true;
3296 assert(UseSignalChaining, "should enable signal-chaining");
3297 }
3298 if (libjsig_is_loaded) {
3299 // Tell libjsig jvm is setting signal handlers
3300 (*begin_signal_setting)();
3301 }
3303 set_signal_handler(SIGSEGV, true);
3304 set_signal_handler(SIGPIPE, true);
3305 set_signal_handler(SIGBUS, true);
3306 set_signal_handler(SIGILL, true);
3307 set_signal_handler(SIGFPE, true);
3308 set_signal_handler(SIGXFSZ, true);
3310 if (libjsig_is_loaded) {
3311 // Tell libjsig jvm finishes setting signal handlers
3312 (*end_signal_setting)();
3313 }
3315 // We don't activate signal checker if libjsig is in place, we trust ourselves
3316 // and if UserSignalHandler is installed all bets are off
3317 if (CheckJNICalls) {
3318 if (libjsig_is_loaded) {
3319 tty->print_cr("Info: libjsig is activated, all active signal checking is disabled");
3320 check_signals = false;
3321 }
3322 if (AllowUserSignalHandlers) {
3323 tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
3324 check_signals = false;
3325 }
3326 }
3327 }
3328 }
3330 // This is the fastest way to get thread cpu time on Linux.
3331 // Returns cpu time (user+sys) for any thread, not only for current.
3332 // POSIX compliant clocks are implemented in the kernels 2.6.16+.
3333 // It might work on 2.6.10+ with a special kernel/glibc patch.
3334 // For reference, please, see IEEE Std 1003.1-2004:
3335 // http://www.unix.org/single_unix_specification
3337 jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
3338 struct timespec tp;
3339 int rc = os::Linux::clock_gettime(clockid, &tp);
3340 assert(rc == 0, "clock_gettime is expected to return 0 code");
3342 return (tp.tv_sec * SEC_IN_NANOSECS) + tp.tv_nsec;
3343 }
3345 /////
3346 // glibc on Linux platform uses non-documented flag
3347 // to indicate, that some special sort of signal
3348 // trampoline is used.
3349 // We will never set this flag, and we should
3350 // ignore this flag in our diagnostic
3351 #ifdef SIGNIFICANT_SIGNAL_MASK
3352 #undef SIGNIFICANT_SIGNAL_MASK
3353 #endif
3354 #define SIGNIFICANT_SIGNAL_MASK (~0x04000000)
3356 static const char* get_signal_handler_name(address handler,
3357 char* buf, int buflen) {
3358 int offset;
3359 bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);
3360 if (found) {
3361 // skip directory names
3362 const char *p1, *p2;
3363 p1 = buf;
3364 size_t len = strlen(os::file_separator());
3365 while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
3366 jio_snprintf(buf, buflen, "%s+0x%x", p1, offset);
3367 } else {
3368 jio_snprintf(buf, buflen, PTR_FORMAT, handler);
3369 }
3370 return buf;
3371 }
3373 static void print_signal_handler(outputStream* st, int sig,
3374 char* buf, size_t buflen) {
3375 struct sigaction sa;
3377 sigaction(sig, NULL, &sa);
3379 // See comment for SIGNIFICANT_SIGNAL_MASK define
3380 sa.sa_flags &= SIGNIFICANT_SIGNAL_MASK;
3382 st->print("%s: ", os::exception_name(sig, buf, buflen));
3384 address handler = (sa.sa_flags & SA_SIGINFO)
3385 ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
3386 : CAST_FROM_FN_PTR(address, sa.sa_handler);
3388 if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {
3389 st->print("SIG_DFL");
3390 } else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) {
3391 st->print("SIG_IGN");
3392 } else {
3393 st->print("[%s]", get_signal_handler_name(handler, buf, buflen));
3394 }
3396 st->print(", sa_mask[0]=" PTR32_FORMAT, *(uint32_t*)&sa.sa_mask);
3398 address rh = VMError::get_resetted_sighandler(sig);
3399 // May be, handler was resetted by VMError?
3400 if(rh != NULL) {
3401 handler = rh;
3402 sa.sa_flags = VMError::get_resetted_sigflags(sig) & SIGNIFICANT_SIGNAL_MASK;
3403 }
3405 st->print(", sa_flags=" PTR32_FORMAT, sa.sa_flags);
3407 // Check: is it our handler?
3408 if(handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler) ||
3409 handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler)) {
3410 // It is our signal handler
3411 // check for flags, reset system-used one!
3412 if((int)sa.sa_flags != os::Linux::get_our_sigflags(sig)) {
3413 st->print(
3414 ", flags was changed from " PTR32_FORMAT ", consider using jsig library",
3415 os::Linux::get_our_sigflags(sig));
3416 }
3417 }
3418 st->cr();
3419 }
3422 #define DO_SIGNAL_CHECK(sig) \
3423 if (!sigismember(&check_signal_done, sig)) \
3424 os::Linux::check_signal_handler(sig)
3426 // This method is a periodic task to check for misbehaving JNI applications
3427 // under CheckJNI, we can add any periodic checks here
3429 void os::run_periodic_checks() {
3431 if (check_signals == false) return;
3433 // SEGV and BUS if overridden could potentially prevent
3434 // generation of hs*.log in the event of a crash, debugging
3435 // such a case can be very challenging, so we absolutely
3436 // check the following for a good measure:
3437 DO_SIGNAL_CHECK(SIGSEGV);
3438 DO_SIGNAL_CHECK(SIGILL);
3439 DO_SIGNAL_CHECK(SIGFPE);
3440 DO_SIGNAL_CHECK(SIGBUS);
3441 DO_SIGNAL_CHECK(SIGPIPE);
3442 DO_SIGNAL_CHECK(SIGXFSZ);
3445 // ReduceSignalUsage allows the user to override these handlers
3446 // see comments at the very top and jvm_solaris.h
3447 if (!ReduceSignalUsage) {
3448 DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL);
3449 DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL);
3450 DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL);
3451 DO_SIGNAL_CHECK(BREAK_SIGNAL);
3452 }
3454 DO_SIGNAL_CHECK(SR_signum);
3455 DO_SIGNAL_CHECK(INTERRUPT_SIGNAL);
3456 }
3458 typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);
3460 static os_sigaction_t os_sigaction = NULL;
3462 void os::Linux::check_signal_handler(int sig) {
3463 char buf[O_BUFLEN];
3464 address jvmHandler = NULL;
3467 struct sigaction act;
3468 if (os_sigaction == NULL) {
3469 // only trust the default sigaction, in case it has been interposed
3470 os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction");
3471 if (os_sigaction == NULL) return;
3472 }
3474 os_sigaction(sig, (struct sigaction*)NULL, &act);
3477 act.sa_flags &= SIGNIFICANT_SIGNAL_MASK;
3479 address thisHandler = (act.sa_flags & SA_SIGINFO)
3480 ? CAST_FROM_FN_PTR(address, act.sa_sigaction)
3481 : CAST_FROM_FN_PTR(address, act.sa_handler) ;
3484 switch(sig) {
3485 case SIGSEGV:
3486 case SIGBUS:
3487 case SIGFPE:
3488 case SIGPIPE:
3489 case SIGILL:
3490 case SIGXFSZ:
3491 jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler);
3492 break;
3494 case SHUTDOWN1_SIGNAL:
3495 case SHUTDOWN2_SIGNAL:
3496 case SHUTDOWN3_SIGNAL:
3497 case BREAK_SIGNAL:
3498 jvmHandler = (address)user_handler();
3499 break;
3501 case INTERRUPT_SIGNAL:
3502 jvmHandler = CAST_FROM_FN_PTR(address, SIG_DFL);
3503 break;
3505 default:
3506 if (sig == SR_signum) {
3507 jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler);
3508 } else {
3509 return;
3510 }
3511 break;
3512 }
3514 if (thisHandler != jvmHandler) {
3515 tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN));
3516 tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN));
3517 tty->print_cr(" found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN));
3518 // No need to check this sig any longer
3519 sigaddset(&check_signal_done, sig);
3520 } else if(os::Linux::get_our_sigflags(sig) != 0 && (int)act.sa_flags != os::Linux::get_our_sigflags(sig)) {
3521 tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN));
3522 tty->print("expected:" PTR32_FORMAT, os::Linux::get_our_sigflags(sig));
3523 tty->print_cr(" found:" PTR32_FORMAT, act.sa_flags);
3524 // No need to check this sig any longer
3525 sigaddset(&check_signal_done, sig);
3526 }
3528 // Dump all the signal
3529 if (sigismember(&check_signal_done, sig)) {
3530 print_signal_handlers(tty, buf, O_BUFLEN);
3531 }
3532 }
3534 extern void report_error(char* file_name, int line_no, char* title, char* format, ...);
3536 extern bool signal_name(int signo, char* buf, size_t len);
3538 const char* os::exception_name(int exception_code, char* buf, size_t size) {
3539 if (0 < exception_code && exception_code <= SIGRTMAX) {
3540 // signal
3541 if (!signal_name(exception_code, buf, size)) {
3542 jio_snprintf(buf, size, "SIG%d", exception_code);
3543 }
3544 return buf;
3545 } else {
3546 return NULL;
3547 }
3548 }
3550 // this is called _before_ the most of global arguments have been parsed
3551 void os::init(void) {
3552 char dummy; /* used to get a guess on initial stack address */
3553 // first_hrtime = gethrtime();
3555 // With LinuxThreads the JavaMain thread pid (primordial thread)
3556 // is different than the pid of the java launcher thread.
3557 // So, on Linux, the launcher thread pid is passed to the VM
3558 // via the sun.java.launcher.pid property.
3559 // Use this property instead of getpid() if it was correctly passed.
3560 // See bug 6351349.
3561 pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();
3563 _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();
3565 clock_tics_per_sec = sysconf(_SC_CLK_TCK);
3567 init_random(1234567);
3569 ThreadCritical::initialize();
3571 Linux::set_page_size(sysconf(_SC_PAGESIZE));
3572 if (Linux::page_size() == -1) {
3573 fatal1("os_linux.cpp: os::init: sysconf failed (%s)", strerror(errno));
3574 }
3575 init_page_sizes((size_t) Linux::page_size());
3577 Linux::initialize_system_info();
3579 // main_thread points to the aboriginal thread
3580 Linux::_main_thread = pthread_self();
3582 Linux::clock_init();
3583 initial_time_count = os::elapsed_counter();
3584 }
3586 // To install functions for atexit system call
3587 extern "C" {
3588 static void perfMemory_exit_helper() {
3589 perfMemory_exit();
3590 }
3591 }
3593 // this is called _after_ the global arguments have been parsed
3594 jint os::init_2(void)
3595 {
3596 Linux::fast_thread_clock_init();
3598 // Allocate a single page and mark it as readable for safepoint polling
3599 address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
3600 guarantee( polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page" );
3602 os::set_polling_page( polling_page );
3604 #ifndef PRODUCT
3605 if(Verbose && PrintMiscellaneous)
3606 tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n", (intptr_t)polling_page);
3607 #endif
3609 if (!UseMembar) {
3610 address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
3611 guarantee( mem_serialize_page != NULL, "mmap Failed for memory serialize page");
3612 os::set_memory_serialize_page( mem_serialize_page );
3614 #ifndef PRODUCT
3615 if(Verbose && PrintMiscellaneous)
3616 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
3617 #endif
3618 }
3620 FLAG_SET_DEFAULT(UseLargePages, os::large_page_init());
3622 // initialize suspend/resume support - must do this before signal_sets_init()
3623 if (SR_initialize() != 0) {
3624 perror("SR_initialize failed");
3625 return JNI_ERR;
3626 }
3628 Linux::signal_sets_init();
3629 Linux::install_signal_handlers();
3631 size_t threadStackSizeInBytes = ThreadStackSize * K;
3632 if (threadStackSizeInBytes != 0 &&
3633 threadStackSizeInBytes < Linux::min_stack_allowed) {
3634 tty->print_cr("\nThe stack size specified is too small, "
3635 "Specify at least %dk",
3636 Linux::min_stack_allowed / K);
3637 return JNI_ERR;
3638 }
3640 // Make the stack size a multiple of the page size so that
3641 // the yellow/red zones can be guarded.
3642 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
3643 vm_page_size()));
3645 Linux::capture_initial_stack(JavaThread::stack_size_at_create());
3647 Linux::libpthread_init();
3648 if (PrintMiscellaneous && (Verbose || WizardMode)) {
3649 tty->print_cr("[HotSpot is running with %s, %s(%s)]\n",
3650 Linux::glibc_version(), Linux::libpthread_version(),
3651 Linux::is_floating_stack() ? "floating stack" : "fixed stack");
3652 }
3654 if (UseNUMA) {
3655 Linux::libnuma_init();
3656 }
3658 if (MaxFDLimit) {
3659 // set the number of file descriptors to max. print out error
3660 // if getrlimit/setrlimit fails but continue regardless.
3661 struct rlimit nbr_files;
3662 int status = getrlimit(RLIMIT_NOFILE, &nbr_files);
3663 if (status != 0) {
3664 if (PrintMiscellaneous && (Verbose || WizardMode))
3665 perror("os::init_2 getrlimit failed");
3666 } else {
3667 nbr_files.rlim_cur = nbr_files.rlim_max;
3668 status = setrlimit(RLIMIT_NOFILE, &nbr_files);
3669 if (status != 0) {
3670 if (PrintMiscellaneous && (Verbose || WizardMode))
3671 perror("os::init_2 setrlimit failed");
3672 }
3673 }
3674 }
3676 // Initialize lock used to serialize thread creation (see os::create_thread)
3677 Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));
3679 // Initialize HPI.
3680 jint hpi_result = hpi::initialize();
3681 if (hpi_result != JNI_OK) {
3682 tty->print_cr("There was an error trying to initialize the HPI library.");
3683 return hpi_result;
3684 }
3686 // at-exit methods are called in the reverse order of their registration.
3687 // atexit functions are called on return from main or as a result of a
3688 // call to exit(3C). There can be only 32 of these functions registered
3689 // and atexit() does not set errno.
3691 if (PerfAllowAtExitRegistration) {
3692 // only register atexit functions if PerfAllowAtExitRegistration is set.
3693 // atexit functions can be delayed until process exit time, which
3694 // can be problematic for embedded VM situations. Embedded VMs should
3695 // call DestroyJavaVM() to assure that VM resources are released.
3697 // note: perfMemory_exit_helper atexit function may be removed in
3698 // the future if the appropriate cleanup code can be added to the
3699 // VM_Exit VMOperation's doit method.
3700 if (atexit(perfMemory_exit_helper) != 0) {
3701 warning("os::init2 atexit(perfMemory_exit_helper) failed");
3702 }
3703 }
3705 // initialize thread priority policy
3706 prio_init();
3708 return JNI_OK;
3709 }
3711 // Mark the polling page as unreadable
3712 void os::make_polling_page_unreadable(void) {
3713 if( !guard_memory((char*)_polling_page, Linux::page_size()) )
3714 fatal("Could not disable polling page");
3715 };
3717 // Mark the polling page as readable
3718 void os::make_polling_page_readable(void) {
3719 if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {
3720 fatal("Could not enable polling page");
3721 }
3722 };
3724 int os::active_processor_count() {
3725 // Linux doesn't yet have a (official) notion of processor sets,
3726 // so just return the number of online processors.
3727 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
3728 assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
3729 return online_cpus;
3730 }
3732 bool os::distribute_processes(uint length, uint* distribution) {
3733 // Not yet implemented.
3734 return false;
3735 }
3737 bool os::bind_to_processor(uint processor_id) {
3738 // Not yet implemented.
3739 return false;
3740 }
3742 ///
3744 // Suspends the target using the signal mechanism and then grabs the PC before
3745 // resuming the target. Used by the flat-profiler only
3746 ExtendedPC os::get_thread_pc(Thread* thread) {
3747 // Make sure that it is called by the watcher for the VMThread
3748 assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
3749 assert(thread->is_VM_thread(), "Can only be called for VMThread");
3751 ExtendedPC epc;
3753 OSThread* osthread = thread->osthread();
3754 if (do_suspend(osthread)) {
3755 if (osthread->ucontext() != NULL) {
3756 epc = os::Linux::ucontext_get_pc(osthread->ucontext());
3757 } else {
3758 // NULL context is unexpected, double-check this is the VMThread
3759 guarantee(thread->is_VM_thread(), "can only be called for VMThread");
3760 }
3761 do_resume(osthread);
3762 }
3763 // failure means pthread_kill failed for some reason - arguably this is
3764 // a fatal problem, but such problems are ignored elsewhere
3766 return epc;
3767 }
3769 int os::Linux::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
3770 {
3771 if (is_NPTL()) {
3772 return pthread_cond_timedwait(_cond, _mutex, _abstime);
3773 } else {
3774 #ifndef IA64
3775 // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control
3776 // word back to default 64bit precision if condvar is signaled. Java
3777 // wants 53bit precision. Save and restore current value.
3778 int fpu = get_fpu_control_word();
3779 #endif // IA64
3780 int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
3781 #ifndef IA64
3782 set_fpu_control_word(fpu);
3783 #endif // IA64
3784 return status;
3785 }
3786 }
3788 ////////////////////////////////////////////////////////////////////////////////
3789 // debug support
3791 #ifndef PRODUCT
3792 static address same_page(address x, address y) {
3793 int page_bits = -os::vm_page_size();
3794 if ((intptr_t(x) & page_bits) == (intptr_t(y) & page_bits))
3795 return x;
3796 else if (x > y)
3797 return (address)(intptr_t(y) | ~page_bits) + 1;
3798 else
3799 return (address)(intptr_t(y) & page_bits);
3800 }
3802 bool os::find(address addr) {
3803 Dl_info dlinfo;
3804 memset(&dlinfo, 0, sizeof(dlinfo));
3805 if (dladdr(addr, &dlinfo)) {
3806 tty->print(PTR_FORMAT ": ", addr);
3807 if (dlinfo.dli_sname != NULL) {
3808 tty->print("%s+%#x", dlinfo.dli_sname,
3809 addr - (intptr_t)dlinfo.dli_saddr);
3810 } else if (dlinfo.dli_fname) {
3811 tty->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);
3812 } else {
3813 tty->print("<absolute address>");
3814 }
3815 if (dlinfo.dli_fname) {
3816 tty->print(" in %s", dlinfo.dli_fname);
3817 }
3818 if (dlinfo.dli_fbase) {
3819 tty->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
3820 }
3821 tty->cr();
3823 if (Verbose) {
3824 // decode some bytes around the PC
3825 address begin = same_page(addr-40, addr);
3826 address end = same_page(addr+40, addr);
3827 address lowest = (address) dlinfo.dli_sname;
3828 if (!lowest) lowest = (address) dlinfo.dli_fbase;
3829 if (begin < lowest) begin = lowest;
3830 Dl_info dlinfo2;
3831 if (dladdr(end, &dlinfo2) && dlinfo2.dli_saddr != dlinfo.dli_saddr
3832 && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
3833 end = (address) dlinfo2.dli_saddr;
3834 Disassembler::decode(begin, end);
3835 }
3836 return true;
3837 }
3838 return false;
3839 }
3841 #endif
3843 ////////////////////////////////////////////////////////////////////////////////
3844 // misc
3846 // This does not do anything on Linux. This is basically a hook for being
3847 // able to use structured exception handling (thread-local exception filters)
3848 // on, e.g., Win32.
3849 void
3850 os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,
3851 JavaCallArguments* args, Thread* thread) {
3852 f(value, method, args, thread);
3853 }
3855 void os::print_statistics() {
3856 }
3858 int os::message_box(const char* title, const char* message) {
3859 int i;
3860 fdStream err(defaultStream::error_fd());
3861 for (i = 0; i < 78; i++) err.print_raw("=");
3862 err.cr();
3863 err.print_raw_cr(title);
3864 for (i = 0; i < 78; i++) err.print_raw("-");
3865 err.cr();
3866 err.print_raw_cr(message);
3867 for (i = 0; i < 78; i++) err.print_raw("=");
3868 err.cr();
3870 char buf[16];
3871 // Prevent process from exiting upon "read error" without consuming all CPU
3872 while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
3874 return buf[0] == 'y' || buf[0] == 'Y';
3875 }
3877 int os::stat(const char *path, struct stat *sbuf) {
3878 char pathbuf[MAX_PATH];
3879 if (strlen(path) > MAX_PATH - 1) {
3880 errno = ENAMETOOLONG;
3881 return -1;
3882 }
3883 hpi::native_path(strcpy(pathbuf, path));
3884 return ::stat(pathbuf, sbuf);
3885 }
3887 bool os::check_heap(bool force) {
3888 return true;
3889 }
3891 int local_vsnprintf(char* buf, size_t count, const char* format, va_list args) {
3892 return ::vsnprintf(buf, count, format, args);
3893 }
3895 // Is a (classpath) directory empty?
3896 bool os::dir_is_empty(const char* path) {
3897 DIR *dir = NULL;
3898 struct dirent *ptr;
3900 dir = opendir(path);
3901 if (dir == NULL) return true;
3903 /* Scan the directory */
3904 bool result = true;
3905 char buf[sizeof(struct dirent) + MAX_PATH];
3906 while (result && (ptr = ::readdir(dir)) != NULL) {
3907 if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
3908 result = false;
3909 }
3910 }
3911 closedir(dir);
3912 return result;
3913 }
3915 // create binary file, rewriting existing file if required
3916 int os::create_binary_file(const char* path, bool rewrite_existing) {
3917 int oflags = O_WRONLY | O_CREAT;
3918 if (!rewrite_existing) {
3919 oflags |= O_EXCL;
3920 }
3921 return ::open64(path, oflags, S_IREAD | S_IWRITE);
3922 }
3924 // return current position of file pointer
3925 jlong os::current_file_offset(int fd) {
3926 return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR);
3927 }
3929 // move file pointer to the specified offset
3930 jlong os::seek_to_file_offset(int fd, jlong offset) {
3931 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);
3932 }
3934 // Map a block of memory.
3935 char* os::map_memory(int fd, const char* file_name, size_t file_offset,
3936 char *addr, size_t bytes, bool read_only,
3937 bool allow_exec) {
3938 int prot;
3939 int flags;
3941 if (read_only) {
3942 prot = PROT_READ;
3943 flags = MAP_SHARED;
3944 } else {
3945 prot = PROT_READ | PROT_WRITE;
3946 flags = MAP_PRIVATE;
3947 }
3949 if (allow_exec) {
3950 prot |= PROT_EXEC;
3951 }
3953 if (addr != NULL) {
3954 flags |= MAP_FIXED;
3955 }
3957 char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
3958 fd, file_offset);
3959 if (mapped_address == MAP_FAILED) {
3960 return NULL;
3961 }
3962 return mapped_address;
3963 }
3966 // Remap a block of memory.
3967 char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
3968 char *addr, size_t bytes, bool read_only,
3969 bool allow_exec) {
3970 // same as map_memory() on this OS
3971 return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
3972 allow_exec);
3973 }
3976 // Unmap a block of memory.
3977 bool os::unmap_memory(char* addr, size_t bytes) {
3978 return munmap(addr, bytes) == 0;
3979 }
3981 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
3983 static clockid_t thread_cpu_clockid(Thread* thread) {
3984 pthread_t tid = thread->osthread()->pthread_id();
3985 clockid_t clockid;
3987 // Get thread clockid
3988 int rc = os::Linux::pthread_getcpuclockid(tid, &clockid);
3989 assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code");
3990 return clockid;
3991 }
3993 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
3994 // are used by JVM M&M and JVMTI to get user+sys or user CPU time
3995 // of a thread.
3996 //
3997 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns
3998 // the fast estimate available on the platform.
4000 jlong os::current_thread_cpu_time() {
4001 if (os::Linux::supports_fast_thread_cpu_time()) {
4002 return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
4003 } else {
4004 // return user + sys since the cost is the same
4005 return slow_thread_cpu_time(Thread::current(), true /* user + sys */);
4006 }
4007 }
4009 jlong os::thread_cpu_time(Thread* thread) {
4010 // consistent with what current_thread_cpu_time() returns
4011 if (os::Linux::supports_fast_thread_cpu_time()) {
4012 return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
4013 } else {
4014 return slow_thread_cpu_time(thread, true /* user + sys */);
4015 }
4016 }
4018 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
4019 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
4020 return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
4021 } else {
4022 return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
4023 }
4024 }
4026 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
4027 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
4028 return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
4029 } else {
4030 return slow_thread_cpu_time(thread, user_sys_cpu_time);
4031 }
4032 }
4034 //
4035 // -1 on error.
4036 //
4038 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
4039 static bool proc_pid_cpu_avail = true;
4040 static bool proc_task_unchecked = true;
4041 static const char *proc_stat_path = "/proc/%d/stat";
4042 pid_t tid = thread->osthread()->thread_id();
4043 int i;
4044 char *s;
4045 char stat[2048];
4046 int statlen;
4047 char proc_name[64];
4048 int count;
4049 long sys_time, user_time;
4050 char string[64];
4051 int idummy;
4052 long ldummy;
4053 FILE *fp;
4055 // We first try accessing /proc/<pid>/cpu since this is faster to
4056 // process. If this file is not present (linux kernels 2.5 and above)
4057 // then we open /proc/<pid>/stat.
4058 if ( proc_pid_cpu_avail ) {
4059 sprintf(proc_name, "/proc/%d/cpu", tid);
4060 fp = fopen(proc_name, "r");
4061 if ( fp != NULL ) {
4062 count = fscanf( fp, "%s %lu %lu\n", string, &user_time, &sys_time);
4063 fclose(fp);
4064 if ( count != 3 ) return -1;
4066 if (user_sys_cpu_time) {
4067 return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
4068 } else {
4069 return (jlong)user_time * (1000000000 / clock_tics_per_sec);
4070 }
4071 }
4072 else proc_pid_cpu_avail = false;
4073 }
4075 // The /proc/<tid>/stat aggregates per-process usage on
4076 // new Linux kernels 2.6+ where NPTL is supported.
4077 // The /proc/self/task/<tid>/stat still has the per-thread usage.
4078 // See bug 6328462.
4079 // There can be no directory /proc/self/task on kernels 2.4 with NPTL
4080 // and possibly in some other cases, so we check its availability.
4081 if (proc_task_unchecked && os::Linux::is_NPTL()) {
4082 // This is executed only once
4083 proc_task_unchecked = false;
4084 fp = fopen("/proc/self/task", "r");
4085 if (fp != NULL) {
4086 proc_stat_path = "/proc/self/task/%d/stat";
4087 fclose(fp);
4088 }
4089 }
4091 sprintf(proc_name, proc_stat_path, tid);
4092 fp = fopen(proc_name, "r");
4093 if ( fp == NULL ) return -1;
4094 statlen = fread(stat, 1, 2047, fp);
4095 stat[statlen] = '\0';
4096 fclose(fp);
4098 // Skip pid and the command string. Note that we could be dealing with
4099 // weird command names, e.g. user could decide to rename java launcher
4100 // to "java 1.4.2 :)", then the stat file would look like
4101 // 1234 (java 1.4.2 :)) R ... ...
4102 // We don't really need to know the command string, just find the last
4103 // occurrence of ")" and then start parsing from there. See bug 4726580.
4104 s = strrchr(stat, ')');
4105 i = 0;
4106 if (s == NULL ) return -1;
4108 // Skip blank chars
4109 do s++; while (isspace(*s));
4111 count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
4112 &idummy, &idummy, &idummy, &idummy, &idummy, &idummy,
4113 &ldummy, &ldummy, &ldummy, &ldummy, &ldummy,
4114 &user_time, &sys_time);
4115 if ( count != 13 ) return -1;
4116 if (user_sys_cpu_time) {
4117 return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
4118 } else {
4119 return (jlong)user_time * (1000000000 / clock_tics_per_sec);
4120 }
4121 }
4123 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4124 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits
4125 info_ptr->may_skip_backward = false; // elapsed time not wall time
4126 info_ptr->may_skip_forward = false; // elapsed time not wall time
4127 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned
4128 }
4130 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4131 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits
4132 info_ptr->may_skip_backward = false; // elapsed time not wall time
4133 info_ptr->may_skip_forward = false; // elapsed time not wall time
4134 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned
4135 }
4137 bool os::is_thread_cpu_time_supported() {
4138 return true;
4139 }
4141 // System loadavg support. Returns -1 if load average cannot be obtained.
4142 // Linux doesn't yet have a (official) notion of processor sets,
4143 // so just return the system wide load average.
4144 int os::loadavg(double loadavg[], int nelem) {
4145 return ::getloadavg(loadavg, nelem);
4146 }
4148 void os::pause() {
4149 char filename[MAX_PATH];
4150 if (PauseAtStartupFile && PauseAtStartupFile[0]) {
4151 jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
4152 } else {
4153 jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
4154 }
4156 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
4157 if (fd != -1) {
4158 struct stat buf;
4159 close(fd);
4160 while (::stat(filename, &buf) == 0) {
4161 (void)::poll(NULL, 0, 100);
4162 }
4163 } else {
4164 jio_fprintf(stderr,
4165 "Could not open pause file '%s', continuing immediately.\n", filename);
4166 }
4167 }
4169 extern "C" {
4171 /**
4172 * NOTE: the following code is to keep the green threads code
4173 * in the libjava.so happy. Once the green threads is removed,
4174 * these code will no longer be needed.
4175 */
4176 int
4177 jdk_waitpid(pid_t pid, int* status, int options) {
4178 return waitpid(pid, status, options);
4179 }
4181 int
4182 fork1() {
4183 return fork();
4184 }
4186 int
4187 jdk_sem_init(sem_t *sem, int pshared, unsigned int value) {
4188 return sem_init(sem, pshared, value);
4189 }
4191 int
4192 jdk_sem_post(sem_t *sem) {
4193 return sem_post(sem);
4194 }
4196 int
4197 jdk_sem_wait(sem_t *sem) {
4198 return sem_wait(sem);
4199 }
4201 int
4202 jdk_pthread_sigmask(int how , const sigset_t* newmask, sigset_t* oldmask) {
4203 return pthread_sigmask(how , newmask, oldmask);
4204 }
4206 }
4208 // Refer to the comments in os_solaris.cpp park-unpark.
4209 //
4210 // Beware -- Some versions of NPTL embody a flaw where pthread_cond_timedwait() can
4211 // hang indefinitely. For instance NPTL 0.60 on 2.4.21-4ELsmp is vulnerable.
4212 // For specifics regarding the bug see GLIBC BUGID 261237 :
4213 // http://www.mail-archive.com/debian-glibc@lists.debian.org/msg10837.html.
4214 // Briefly, pthread_cond_timedwait() calls with an expiry time that's not in the future
4215 // will either hang or corrupt the condvar, resulting in subsequent hangs if the condvar
4216 // is used. (The simple C test-case provided in the GLIBC bug report manifests the
4217 // hang). The JVM is vulernable via sleep(), Object.wait(timo), LockSupport.parkNanos()
4218 // and monitorenter when we're using 1-0 locking. All those operations may result in
4219 // calls to pthread_cond_timedwait(). Using LD_ASSUME_KERNEL to use an older version
4220 // of libpthread avoids the problem, but isn't practical.
4221 //
4222 // Possible remedies:
4223 //
4224 // 1. Establish a minimum relative wait time. 50 to 100 msecs seems to work.
4225 // This is palliative and probabilistic, however. If the thread is preempted
4226 // between the call to compute_abstime() and pthread_cond_timedwait(), more
4227 // than the minimum period may have passed, and the abstime may be stale (in the
4228 // past) resultin in a hang. Using this technique reduces the odds of a hang
4229 // but the JVM is still vulnerable, particularly on heavily loaded systems.
4230 //
4231 // 2. Modify park-unpark to use per-thread (per ParkEvent) pipe-pairs instead
4232 // of the usual flag-condvar-mutex idiom. The write side of the pipe is set
4233 // NDELAY. unpark() reduces to write(), park() reduces to read() and park(timo)
4234 // reduces to poll()+read(). This works well, but consumes 2 FDs per extant
4235 // thread.
4236 //
4237 // 3. Embargo pthread_cond_timedwait() and implement a native "chron" thread
4238 // that manages timeouts. We'd emulate pthread_cond_timedwait() by enqueuing
4239 // a timeout request to the chron thread and then blocking via pthread_cond_wait().
4240 // This also works well. In fact it avoids kernel-level scalability impediments
4241 // on certain platforms that don't handle lots of active pthread_cond_timedwait()
4242 // timers in a graceful fashion.
4243 //
4244 // 4. When the abstime value is in the past it appears that control returns
4245 // correctly from pthread_cond_timedwait(), but the condvar is left corrupt.
4246 // Subsequent timedwait/wait calls may hang indefinitely. Given that, we
4247 // can avoid the problem by reinitializing the condvar -- by cond_destroy()
4248 // followed by cond_init() -- after all calls to pthread_cond_timedwait().
4249 // It may be possible to avoid reinitialization by checking the return
4250 // value from pthread_cond_timedwait(). In addition to reinitializing the
4251 // condvar we must establish the invariant that cond_signal() is only called
4252 // within critical sections protected by the adjunct mutex. This prevents
4253 // cond_signal() from "seeing" a condvar that's in the midst of being
4254 // reinitialized or that is corrupt. Sadly, this invariant obviates the
4255 // desirable signal-after-unlock optimization that avoids futile context switching.
4256 //
4257 // I'm also concerned that some versions of NTPL might allocate an auxilliary
4258 // structure when a condvar is used or initialized. cond_destroy() would
4259 // release the helper structure. Our reinitialize-after-timedwait fix
4260 // put excessive stress on malloc/free and locks protecting the c-heap.
4261 //
4262 // We currently use (4). See the WorkAroundNTPLTimedWaitHang flag.
4263 // It may be possible to refine (4) by checking the kernel and NTPL verisons
4264 // and only enabling the work-around for vulnerable environments.
4266 // utility to compute the abstime argument to timedwait:
4267 // millis is the relative timeout time
4268 // abstime will be the absolute timeout time
4269 // TODO: replace compute_abstime() with unpackTime()
4271 static struct timespec* compute_abstime(timespec* abstime, jlong millis) {
4272 if (millis < 0) millis = 0;
4273 struct timeval now;
4274 int status = gettimeofday(&now, NULL);
4275 assert(status == 0, "gettimeofday");
4276 jlong seconds = millis / 1000;
4277 millis %= 1000;
4278 if (seconds > 50000000) { // see man cond_timedwait(3T)
4279 seconds = 50000000;
4280 }
4281 abstime->tv_sec = now.tv_sec + seconds;
4282 long usec = now.tv_usec + millis * 1000;
4283 if (usec >= 1000000) {
4284 abstime->tv_sec += 1;
4285 usec -= 1000000;
4286 }
4287 abstime->tv_nsec = usec * 1000;
4288 return abstime;
4289 }
4292 // Test-and-clear _Event, always leaves _Event set to 0, returns immediately.
4293 // Conceptually TryPark() should be equivalent to park(0).
4295 int os::PlatformEvent::TryPark() {
4296 for (;;) {
4297 const int v = _Event ;
4298 guarantee ((v == 0) || (v == 1), "invariant") ;
4299 if (Atomic::cmpxchg (0, &_Event, v) == v) return v ;
4300 }
4301 }
4303 void os::PlatformEvent::park() { // AKA "down()"
4304 // Invariant: Only the thread associated with the Event/PlatformEvent
4305 // may call park().
4306 // TODO: assert that _Assoc != NULL or _Assoc == Self
4307 int v ;
4308 for (;;) {
4309 v = _Event ;
4310 if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
4311 }
4312 guarantee (v >= 0, "invariant") ;
4313 if (v == 0) {
4314 // Do this the hard way by blocking ...
4315 int status = pthread_mutex_lock(_mutex);
4316 assert_status(status == 0, status, "mutex_lock");
4317 guarantee (_nParked == 0, "invariant") ;
4318 ++ _nParked ;
4319 while (_Event < 0) {
4320 status = pthread_cond_wait(_cond, _mutex);
4321 // for some reason, under 2.7 lwp_cond_wait() may return ETIME ...
4322 // Treat this the same as if the wait was interrupted
4323 if (status == ETIME) { status = EINTR; }
4324 assert_status(status == 0 || status == EINTR, status, "cond_wait");
4325 }
4326 -- _nParked ;
4328 // In theory we could move the ST of 0 into _Event past the unlock(),
4329 // but then we'd need a MEMBAR after the ST.
4330 _Event = 0 ;
4331 status = pthread_mutex_unlock(_mutex);
4332 assert_status(status == 0, status, "mutex_unlock");
4333 }
4334 guarantee (_Event >= 0, "invariant") ;
4335 }
4337 int os::PlatformEvent::park(jlong millis) {
4338 guarantee (_nParked == 0, "invariant") ;
4340 int v ;
4341 for (;;) {
4342 v = _Event ;
4343 if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
4344 }
4345 guarantee (v >= 0, "invariant") ;
4346 if (v != 0) return OS_OK ;
4348 // We do this the hard way, by blocking the thread.
4349 // Consider enforcing a minimum timeout value.
4350 struct timespec abst;
4351 compute_abstime(&abst, millis);
4353 int ret = OS_TIMEOUT;
4354 int status = pthread_mutex_lock(_mutex);
4355 assert_status(status == 0, status, "mutex_lock");
4356 guarantee (_nParked == 0, "invariant") ;
4357 ++_nParked ;
4359 // Object.wait(timo) will return because of
4360 // (a) notification
4361 // (b) timeout
4362 // (c) thread.interrupt
4363 //
4364 // Thread.interrupt and object.notify{All} both call Event::set.
4365 // That is, we treat thread.interrupt as a special case of notification.
4366 // The underlying Solaris implementation, cond_timedwait, admits
4367 // spurious/premature wakeups, but the JLS/JVM spec prevents the
4368 // JVM from making those visible to Java code. As such, we must
4369 // filter out spurious wakeups. We assume all ETIME returns are valid.
4370 //
4371 // TODO: properly differentiate simultaneous notify+interrupt.
4372 // In that case, we should propagate the notify to another waiter.
4374 while (_Event < 0) {
4375 status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst);
4376 if (status != 0 && WorkAroundNPTLTimedWaitHang) {
4377 pthread_cond_destroy (_cond);
4378 pthread_cond_init (_cond, NULL) ;
4379 }
4380 assert_status(status == 0 || status == EINTR ||
4381 status == ETIME || status == ETIMEDOUT,
4382 status, "cond_timedwait");
4383 if (!FilterSpuriousWakeups) break ; // previous semantics
4384 if (status == ETIME || status == ETIMEDOUT) break ;
4385 // We consume and ignore EINTR and spurious wakeups.
4386 }
4387 --_nParked ;
4388 if (_Event >= 0) {
4389 ret = OS_OK;
4390 }
4391 _Event = 0 ;
4392 status = pthread_mutex_unlock(_mutex);
4393 assert_status(status == 0, status, "mutex_unlock");
4394 assert (_nParked == 0, "invariant") ;
4395 return ret;
4396 }
4398 void os::PlatformEvent::unpark() {
4399 int v, AnyWaiters ;
4400 for (;;) {
4401 v = _Event ;
4402 if (v > 0) {
4403 // The LD of _Event could have reordered or be satisfied
4404 // by a read-aside from this processor's write buffer.
4405 // To avoid problems execute a barrier and then
4406 // ratify the value.
4407 OrderAccess::fence() ;
4408 if (_Event == v) return ;
4409 continue ;
4410 }
4411 if (Atomic::cmpxchg (v+1, &_Event, v) == v) break ;
4412 }
4413 if (v < 0) {
4414 // Wait for the thread associated with the event to vacate
4415 int status = pthread_mutex_lock(_mutex);
4416 assert_status(status == 0, status, "mutex_lock");
4417 AnyWaiters = _nParked ;
4418 assert (AnyWaiters == 0 || AnyWaiters == 1, "invariant") ;
4419 if (AnyWaiters != 0 && WorkAroundNPTLTimedWaitHang) {
4420 AnyWaiters = 0 ;
4421 pthread_cond_signal (_cond);
4422 }
4423 status = pthread_mutex_unlock(_mutex);
4424 assert_status(status == 0, status, "mutex_unlock");
4425 if (AnyWaiters != 0) {
4426 status = pthread_cond_signal(_cond);
4427 assert_status(status == 0, status, "cond_signal");
4428 }
4429 }
4431 // Note that we signal() _after dropping the lock for "immortal" Events.
4432 // This is safe and avoids a common class of futile wakeups. In rare
4433 // circumstances this can cause a thread to return prematurely from
4434 // cond_{timed}wait() but the spurious wakeup is benign and the victim will
4435 // simply re-test the condition and re-park itself.
4436 }
4439 // JSR166
4440 // -------------------------------------------------------
4442 /*
4443 * The solaris and linux implementations of park/unpark are fairly
4444 * conservative for now, but can be improved. They currently use a
4445 * mutex/condvar pair, plus a a count.
4446 * Park decrements count if > 0, else does a condvar wait. Unpark
4447 * sets count to 1 and signals condvar. Only one thread ever waits
4448 * on the condvar. Contention seen when trying to park implies that someone
4449 * is unparking you, so don't wait. And spurious returns are fine, so there
4450 * is no need to track notifications.
4451 */
4454 #define NANOSECS_PER_SEC 1000000000
4455 #define NANOSECS_PER_MILLISEC 1000000
4456 #define MAX_SECS 100000000
4457 /*
4458 * This code is common to linux and solaris and will be moved to a
4459 * common place in dolphin.
4460 *
4461 * The passed in time value is either a relative time in nanoseconds
4462 * or an absolute time in milliseconds. Either way it has to be unpacked
4463 * into suitable seconds and nanoseconds components and stored in the
4464 * given timespec structure.
4465 * Given time is a 64-bit value and the time_t used in the timespec is only
4466 * a signed-32-bit value (except on 64-bit Linux) we have to watch for
4467 * overflow if times way in the future are given. Further on Solaris versions
4468 * prior to 10 there is a restriction (see cond_timedwait) that the specified
4469 * number of seconds, in abstime, is less than current_time + 100,000,000.
4470 * As it will be 28 years before "now + 100000000" will overflow we can
4471 * ignore overflow and just impose a hard-limit on seconds using the value
4472 * of "now + 100,000,000". This places a limit on the timeout of about 3.17
4473 * years from "now".
4474 */
4476 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {
4477 assert (time > 0, "convertTime");
4479 struct timeval now;
4480 int status = gettimeofday(&now, NULL);
4481 assert(status == 0, "gettimeofday");
4483 time_t max_secs = now.tv_sec + MAX_SECS;
4485 if (isAbsolute) {
4486 jlong secs = time / 1000;
4487 if (secs > max_secs) {
4488 absTime->tv_sec = max_secs;
4489 }
4490 else {
4491 absTime->tv_sec = secs;
4492 }
4493 absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;
4494 }
4495 else {
4496 jlong secs = time / NANOSECS_PER_SEC;
4497 if (secs >= MAX_SECS) {
4498 absTime->tv_sec = max_secs;
4499 absTime->tv_nsec = 0;
4500 }
4501 else {
4502 absTime->tv_sec = now.tv_sec + secs;
4503 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;
4504 if (absTime->tv_nsec >= NANOSECS_PER_SEC) {
4505 absTime->tv_nsec -= NANOSECS_PER_SEC;
4506 ++absTime->tv_sec; // note: this must be <= max_secs
4507 }
4508 }
4509 }
4510 assert(absTime->tv_sec >= 0, "tv_sec < 0");
4511 assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs");
4512 assert(absTime->tv_nsec >= 0, "tv_nsec < 0");
4513 assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec");
4514 }
4516 void Parker::park(bool isAbsolute, jlong time) {
4517 // Optional fast-path check:
4518 // Return immediately if a permit is available.
4519 if (_counter > 0) {
4520 _counter = 0 ;
4521 return ;
4522 }
4524 Thread* thread = Thread::current();
4525 assert(thread->is_Java_thread(), "Must be JavaThread");
4526 JavaThread *jt = (JavaThread *)thread;
4528 // Optional optimization -- avoid state transitions if there's an interrupt pending.
4529 // Check interrupt before trying to wait
4530 if (Thread::is_interrupted(thread, false)) {
4531 return;
4532 }
4534 // Next, demultiplex/decode time arguments
4535 timespec absTime;
4536 if (time < 0) { // don't wait at all
4537 return;
4538 }
4539 if (time > 0) {
4540 unpackTime(&absTime, isAbsolute, time);
4541 }
4544 // Enter safepoint region
4545 // Beware of deadlocks such as 6317397.
4546 // The per-thread Parker:: mutex is a classic leaf-lock.
4547 // In particular a thread must never block on the Threads_lock while
4548 // holding the Parker:: mutex. If safepoints are pending both the
4549 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
4550 ThreadBlockInVM tbivm(jt);
4552 // Don't wait if cannot get lock since interference arises from
4553 // unblocking. Also. check interrupt before trying wait
4554 if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) {
4555 return;
4556 }
4558 int status ;
4559 if (_counter > 0) { // no wait needed
4560 _counter = 0;
4561 status = pthread_mutex_unlock(_mutex);
4562 assert (status == 0, "invariant") ;
4563 return;
4564 }
4566 #ifdef ASSERT
4567 // Don't catch signals while blocked; let the running threads have the signals.
4568 // (This allows a debugger to break into the running thread.)
4569 sigset_t oldsigs;
4570 sigset_t* allowdebug_blocked = os::Linux::allowdebug_blocked_signals();
4571 pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);
4572 #endif
4574 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
4575 jt->set_suspend_equivalent();
4576 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
4578 if (time == 0) {
4579 status = pthread_cond_wait (_cond, _mutex) ;
4580 } else {
4581 status = os::Linux::safe_cond_timedwait (_cond, _mutex, &absTime) ;
4582 if (status != 0 && WorkAroundNPTLTimedWaitHang) {
4583 pthread_cond_destroy (_cond) ;
4584 pthread_cond_init (_cond, NULL);
4585 }
4586 }
4587 assert_status(status == 0 || status == EINTR ||
4588 status == ETIME || status == ETIMEDOUT,
4589 status, "cond_timedwait");
4591 #ifdef ASSERT
4592 pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
4593 #endif
4595 _counter = 0 ;
4596 status = pthread_mutex_unlock(_mutex) ;
4597 assert_status(status == 0, status, "invariant") ;
4598 // If externally suspended while waiting, re-suspend
4599 if (jt->handle_special_suspend_equivalent_condition()) {
4600 jt->java_suspend_self();
4601 }
4603 }
4605 void Parker::unpark() {
4606 int s, status ;
4607 status = pthread_mutex_lock(_mutex);
4608 assert (status == 0, "invariant") ;
4609 s = _counter;
4610 _counter = 1;
4611 if (s < 1) {
4612 if (WorkAroundNPTLTimedWaitHang) {
4613 status = pthread_cond_signal (_cond) ;
4614 assert (status == 0, "invariant") ;
4615 status = pthread_mutex_unlock(_mutex);
4616 assert (status == 0, "invariant") ;
4617 } else {
4618 status = pthread_mutex_unlock(_mutex);
4619 assert (status == 0, "invariant") ;
4620 status = pthread_cond_signal (_cond) ;
4621 assert (status == 0, "invariant") ;
4622 }
4623 } else {
4624 pthread_mutex_unlock(_mutex);
4625 assert (status == 0, "invariant") ;
4626 }
4627 }
4630 extern char** environ;
4632 #ifndef __NR_fork
4633 #define __NR_fork IA32_ONLY(2) IA64_ONLY(not defined) AMD64_ONLY(57)
4634 #endif
4636 #ifndef __NR_execve
4637 #define __NR_execve IA32_ONLY(11) IA64_ONLY(1033) AMD64_ONLY(59)
4638 #endif
4640 // Run the specified command in a separate process. Return its exit value,
4641 // or -1 on failure (e.g. can't fork a new process).
4642 // Unlike system(), this function can be called from signal handler. It
4643 // doesn't block SIGINT et al.
4644 int os::fork_and_exec(char* cmd) {
4645 const char * argv[4] = {"sh", "-c", cmd, NULL};
4647 // fork() in LinuxThreads/NPTL is not async-safe. It needs to run
4648 // pthread_atfork handlers and reset pthread library. All we need is a
4649 // separate process to execve. Make a direct syscall to fork process.
4650 // On IA64 there's no fork syscall, we have to use fork() and hope for
4651 // the best...
4652 pid_t pid = NOT_IA64(syscall(__NR_fork);)
4653 IA64_ONLY(fork();)
4655 if (pid < 0) {
4656 // fork failed
4657 return -1;
4659 } else if (pid == 0) {
4660 // child process
4662 // execve() in LinuxThreads will call pthread_kill_other_threads_np()
4663 // first to kill every thread on the thread list. Because this list is
4664 // not reset by fork() (see notes above), execve() will instead kill
4665 // every thread in the parent process. We know this is the only thread
4666 // in the new process, so make a system call directly.
4667 // IA64 should use normal execve() from glibc to match the glibc fork()
4668 // above.
4669 NOT_IA64(syscall(__NR_execve, "/bin/sh", argv, environ);)
4670 IA64_ONLY(execve("/bin/sh", (char* const*)argv, environ);)
4672 // execve failed
4673 _exit(-1);
4675 } else {
4676 // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
4677 // care about the actual exit code, for now.
4679 int status;
4681 // Wait for the child process to exit. This returns immediately if
4682 // the child has already exited. */
4683 while (waitpid(pid, &status, 0) < 0) {
4684 switch (errno) {
4685 case ECHILD: return 0;
4686 case EINTR: break;
4687 default: return -1;
4688 }
4689 }
4691 if (WIFEXITED(status)) {
4692 // The child exited normally; get its exit code.
4693 return WEXITSTATUS(status);
4694 } else if (WIFSIGNALED(status)) {
4695 // The child exited because of a signal
4696 // The best value to return is 0x80 + signal number,
4697 // because that is what all Unix shells do, and because
4698 // it allows callers to distinguish between process exit and
4699 // process death by signal.
4700 return 0x80 + WTERMSIG(status);
4701 } else {
4702 // Unknown exit code; pass it through
4703 return status;
4704 }
4705 }
4706 }