1 /* 2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "jvm.h" 26 #include "logging/log.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "utilities/globalDefinitions.hpp" 29 #include "runtime/frame.inline.hpp" 30 #include "runtime/interfaceSupport.inline.hpp" 31 #include "runtime/os.hpp" 32 #include "services/memTracker.hpp" 33 #include "utilities/align.hpp" 34 #include "utilities/formatBuffer.hpp" 35 #include "utilities/macros.hpp" 36 #include "utilities/vmError.hpp" 37 38 #include <dirent.h> 39 #include <dlfcn.h> 40 #include <grp.h> 41 #include <pwd.h> 42 #include <pthread.h> 43 #include <signal.h> 44 #include <sys/mman.h> 45 #include <sys/resource.h> 46 #include <sys/utsname.h> 47 #include <time.h> 48 #include <unistd.h> 49 50 // Todo: provide a os::get_max_process_id() or similar. Number of processes 51 // may have been configured, can be read more accurately from proc fs etc. 52 #ifndef MAX_PID 53 #define MAX_PID INT_MAX 54 #endif 55 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID) 56 57 #define ROOT_UID 0 58 59 #ifndef MAP_ANONYMOUS 60 #define MAP_ANONYMOUS MAP_ANON 61 #endif 62 63 #define check_with_errno(check_type, cond, msg) \ 64 do { \ 65 int err = errno; \ 66 check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \ 67 os::errno_name(err)); \ 68 } while (false) 69 70 #define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg) 71 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg) 72 73 // Check core dump limit and report possible place where core can be found 74 void os::check_dump_limit(char* buffer, size_t bufferSize) { 75 if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) { 76 jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line"); 77 VMError::record_coredump_status(buffer, false); 78 return; 79 } 80 81 int n; 82 struct rlimit rlim; 83 bool success; 84 85 char core_path[PATH_MAX]; 86 n = get_core_path(core_path, PATH_MAX); 87 88 if (n <= 0) { 89 jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id()); 90 success = true; 91 #ifdef LINUX 92 } else if (core_path[0] == '"') { // redirect to user process 93 jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path); 94 success = true; 95 #endif 96 } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) { 97 jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path); 98 success = true; 99 } else { 100 switch(rlim.rlim_cur) { 101 case RLIM_INFINITY: 102 jio_snprintf(buffer, bufferSize, "%s", core_path); 103 success = true; 104 break; 105 case 0: 106 jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again"); 107 success = false; 108 break; 109 default: 110 jio_snprintf(buffer, bufferSize, "%s (max size " UINT64_FORMAT " kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, uint64_t(rlim.rlim_cur) / 1024); 111 success = true; 112 break; 113 } 114 } 115 116 VMError::record_coredump_status(buffer, success); 117 } 118 119 int os::get_native_stack(address* stack, int frames, int toSkip) { 120 int frame_idx = 0; 121 int num_of_frames; // number of frames captured 122 frame fr = os::current_frame(); 123 while (fr.pc() && frame_idx < frames) { 124 if (toSkip > 0) { 125 toSkip --; 126 } else { 127 stack[frame_idx ++] = fr.pc(); 128 } 129 if (fr.fp() == NULL || fr.cb() != NULL || 130 fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break; 131 132 if (fr.sender_pc() && !os::is_first_C_frame(&fr)) { 133 fr = os::get_sender_for_C_frame(&fr); 134 } else { 135 break; 136 } 137 } 138 num_of_frames = frame_idx; 139 for (; frame_idx < frames; frame_idx ++) { 140 stack[frame_idx] = NULL; 141 } 142 143 return num_of_frames; 144 } 145 146 147 bool os::unsetenv(const char* name) { 148 assert(name != NULL, "Null pointer"); 149 return (::unsetenv(name) == 0); 150 } 151 152 int os::get_last_error() { 153 return errno; 154 } 155 156 size_t os::lasterror(char *buf, size_t len) { 157 if (errno == 0) return 0; 158 159 const char *s = os::strerror(errno); 160 size_t n = ::strlen(s); 161 if (n >= len) { 162 n = len - 1; 163 } 164 ::strncpy(buf, s, n); 165 buf[n] = '\0'; 166 return n; 167 } 168 169 bool os::is_debugger_attached() { 170 // not implemented 171 return false; 172 } 173 174 void os::wait_for_keypress_at_exit(void) { 175 // don't do anything on posix platforms 176 return; 177 } 178 179 int os::create_file_for_heap(const char* dir) { 180 181 const char name_template[] = "/jvmheap.XXXXXX"; 182 183 char *fullname = (char*)os::malloc((strlen(dir) + strlen(name_template) + 1), mtInternal); 184 if (fullname == NULL) { 185 vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno))); 186 return -1; 187 } 188 (void)strncpy(fullname, dir, strlen(dir)+1); 189 (void)strncat(fullname, name_template, strlen(name_template)); 190 191 os::native_path(fullname); 192 193 sigset_t set, oldset; 194 int ret = sigfillset(&set); 195 assert_with_errno(ret == 0, "sigfillset returned error"); 196 197 // set the file creation mask. 198 mode_t file_mode = S_IRUSR | S_IWUSR; 199 200 // create a new file. 201 int fd = mkstemp(fullname); 202 203 if (fd < 0) { 204 warning("Could not create file for heap with template %s", fullname); 205 os::free(fullname); 206 return -1; 207 } 208 209 // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted. 210 ret = unlink(fullname); 211 assert_with_errno(ret == 0, "unlink returned error"); 212 213 os::free(fullname); 214 return fd; 215 } 216 217 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) { 218 char * addr; 219 int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS; 220 if (requested_addr != NULL) { 221 assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size"); 222 flags |= MAP_FIXED; 223 } 224 225 // Map reserved/uncommitted pages PROT_NONE so we fail early if we 226 // touch an uncommitted page. Otherwise, the read/write might 227 // succeed if we have enough swap space to back the physical page. 228 addr = (char*)::mmap(requested_addr, bytes, PROT_NONE, 229 flags, -1, 0); 230 231 if (addr != MAP_FAILED) { 232 MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC); 233 return addr; 234 } 235 return NULL; 236 } 237 238 static int util_posix_fallocate(int fd, off_t offset, off_t len) { 239 #ifdef __APPLE__ 240 fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len }; 241 // First we try to get a continuous chunk of disk space 242 int ret = fcntl(fd, F_PREALLOCATE, &store); 243 if (ret == -1) { 244 // Maybe we are too fragmented, try to allocate non-continuous range 245 store.fst_flags = F_ALLOCATEALL; 246 ret = fcntl(fd, F_PREALLOCATE, &store); 247 } 248 if(ret != -1) { 249 return ftruncate(fd, len); 250 } 251 return -1; 252 #else 253 return posix_fallocate(fd, offset, len); 254 #endif 255 } 256 257 // Map the given address range to the provided file descriptor. 258 char* os::map_memory_to_file(char* base, size_t size, int fd) { 259 assert(fd != -1, "File descriptor is not valid"); 260 261 // allocate space for the file 262 int ret = util_posix_fallocate(fd, 0, (off_t)size); 263 if (ret != 0) { 264 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret)); 265 return NULL; 266 } 267 268 int prot = PROT_READ | PROT_WRITE; 269 int flags = MAP_SHARED; 270 if (base != NULL) { 271 flags |= MAP_FIXED; 272 } 273 char* addr = (char*)mmap(base, size, prot, flags, fd, 0); 274 275 if (addr == MAP_FAILED) { 276 warning("Failed mmap to file. (%s)", os::strerror(errno)); 277 return NULL; 278 } 279 if (base != NULL && addr != base) { 280 if (!os::release_memory(addr, size)) { 281 warning("Could not release memory on unsuccessful file mapping"); 282 } 283 return NULL; 284 } 285 return addr; 286 } 287 288 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) { 289 assert(fd != -1, "File descriptor is not valid"); 290 assert(base != NULL, "Base cannot be NULL"); 291 292 return map_memory_to_file(base, size, fd); 293 } 294 295 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED, 296 // so on posix, unmap the section at the start and at the end of the chunk that we mapped 297 // rather than unmapping and remapping the whole chunk to get requested alignment. 298 char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) { 299 assert((alignment & (os::vm_allocation_granularity() - 1)) == 0, 300 "Alignment must be a multiple of allocation granularity (page size)"); 301 assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned"); 302 303 size_t extra_size = size + alignment; 304 assert(extra_size >= size, "overflow, size is too large to allow alignment"); 305 306 char* extra_base; 307 if (file_desc != -1) { 308 // For file mapping, we do not call os:reserve_memory(extra_size, NULL, alignment, file_desc) because 309 // we need to deal with shrinking of the file space later when we release extra memory after alignment. 310 // We also cannot called os:reserve_memory() with file_desc set to -1 because on aix we might get SHM memory. 311 // So here to call a helper function while reserve memory for us. After we have a aligned base, 312 // we will replace anonymous mapping with file mapping. 313 extra_base = reserve_mmapped_memory(extra_size, NULL); 314 if (extra_base != NULL) { 315 MemTracker::record_virtual_memory_reserve((address)extra_base, extra_size, CALLER_PC); 316 } 317 } else { 318 extra_base = os::reserve_memory(extra_size, NULL, alignment); 319 } 320 321 if (extra_base == NULL) { 322 return NULL; 323 } 324 325 // Do manual alignment 326 char* aligned_base = align_up(extra_base, alignment); 327 328 // [ | | ] 329 // ^ extra_base 330 // ^ extra_base + begin_offset == aligned_base 331 // extra_base + begin_offset + size ^ 332 // extra_base + extra_size ^ 333 // |<>| == begin_offset 334 // end_offset == |<>| 335 size_t begin_offset = aligned_base - extra_base; 336 size_t end_offset = (extra_base + extra_size) - (aligned_base + size); 337 338 if (begin_offset > 0) { 339 os::release_memory(extra_base, begin_offset); 340 } 341 342 if (end_offset > 0) { 343 os::release_memory(extra_base + begin_offset + size, end_offset); 344 } 345 346 if (file_desc != -1) { 347 // After we have an aligned address, we can replace anonymous mapping with file mapping 348 if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) { 349 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory")); 350 } 351 MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC); 352 } 353 return aligned_base; 354 } 355 356 int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) { 357 // All supported POSIX platforms provide C99 semantics. 358 int result = ::vsnprintf(buf, len, fmt, args); 359 // If an encoding error occurred (result < 0) then it's not clear 360 // whether the buffer is NUL terminated, so ensure it is. 361 if ((result < 0) && (len > 0)) { 362 buf[len - 1] = '\0'; 363 } 364 return result; 365 } 366 367 int os::get_fileno(FILE* fp) { 368 return NOT_AIX(::)fileno(fp); 369 } 370 371 struct tm* os::gmtime_pd(const time_t* clock, struct tm* res) { 372 return gmtime_r(clock, res); 373 } 374 375 void os::Posix::print_load_average(outputStream* st) { 376 st->print("load average:"); 377 double loadavg[3]; 378 os::loadavg(loadavg, 3); 379 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]); 380 st->cr(); 381 } 382 383 void os::Posix::print_rlimit_info(outputStream* st) { 384 st->print("rlimit:"); 385 struct rlimit rlim; 386 387 st->print(" STACK "); 388 getrlimit(RLIMIT_STACK, &rlim); 389 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 390 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 391 392 st->print(", CORE "); 393 getrlimit(RLIMIT_CORE, &rlim); 394 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 395 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 396 397 // Isn't there on solaris 398 #if defined(AIX) 399 st->print(", NPROC "); 400 st->print("%d", sysconf(_SC_CHILD_MAX)); 401 #elif !defined(SOLARIS) 402 st->print(", NPROC "); 403 getrlimit(RLIMIT_NPROC, &rlim); 404 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 405 else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); 406 #endif 407 408 st->print(", NOFILE "); 409 getrlimit(RLIMIT_NOFILE, &rlim); 410 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 411 else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); 412 413 st->print(", AS "); 414 getrlimit(RLIMIT_AS, &rlim); 415 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 416 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 417 418 st->print(", DATA "); 419 getrlimit(RLIMIT_DATA, &rlim); 420 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 421 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 422 423 st->print(", FSIZE "); 424 getrlimit(RLIMIT_FSIZE, &rlim); 425 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 426 else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); 427 428 st->cr(); 429 } 430 431 void os::Posix::print_uname_info(outputStream* st) { 432 // kernel 433 st->print("uname:"); 434 struct utsname name; 435 uname(&name); 436 st->print("%s ", name.sysname); 437 #ifdef ASSERT 438 st->print("%s ", name.nodename); 439 #endif 440 st->print("%s ", name.release); 441 st->print("%s ", name.version); 442 st->print("%s", name.machine); 443 st->cr(); 444 } 445 446 void os::Posix::print_umask(outputStream* st, mode_t umsk) { 447 st->print((umsk & S_IRUSR) ? "r" : "-"); 448 st->print((umsk & S_IWUSR) ? "w" : "-"); 449 st->print((umsk & S_IXUSR) ? "x" : "-"); 450 st->print((umsk & S_IRGRP) ? "r" : "-"); 451 st->print((umsk & S_IWGRP) ? "w" : "-"); 452 st->print((umsk & S_IXGRP) ? "x" : "-"); 453 st->print((umsk & S_IROTH) ? "r" : "-"); 454 st->print((umsk & S_IWOTH) ? "w" : "-"); 455 st->print((umsk & S_IXOTH) ? "x" : "-"); 456 } 457 458 void os::Posix::print_user_info(outputStream* st) { 459 unsigned id = (unsigned) ::getuid(); 460 st->print("uid : %u ", id); 461 id = (unsigned) ::geteuid(); 462 st->print("euid : %u ", id); 463 id = (unsigned) ::getgid(); 464 st->print("gid : %u ", id); 465 id = (unsigned) ::getegid(); 466 st->print_cr("egid : %u", id); 467 st->cr(); 468 469 mode_t umsk = ::umask(0); 470 ::umask(umsk); 471 st->print("umask: %04o (", (unsigned) umsk); 472 print_umask(st, umsk); 473 st->print_cr(")"); 474 st->cr(); 475 } 476 477 478 bool os::get_host_name(char* buf, size_t buflen) { 479 struct utsname name; 480 uname(&name); 481 jio_snprintf(buf, buflen, "%s", name.nodename); 482 return true; 483 } 484 485 bool os::has_allocatable_memory_limit(julong* limit) { 486 struct rlimit rlim; 487 int getrlimit_res = getrlimit(RLIMIT_AS, &rlim); 488 // if there was an error when calling getrlimit, assume that there is no limitation 489 // on virtual memory. 490 bool result; 491 if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) { 492 result = false; 493 } else { 494 *limit = (julong)rlim.rlim_cur; 495 result = true; 496 } 497 #ifdef _LP64 498 return result; 499 #else 500 // arbitrary virtual space limit for 32 bit Unices found by testing. If 501 // getrlimit above returned a limit, bound it with this limit. Otherwise 502 // directly use it. 503 const julong max_virtual_limit = (julong)3800*M; 504 if (result) { 505 *limit = MIN2(*limit, max_virtual_limit); 506 } else { 507 *limit = max_virtual_limit; 508 } 509 510 // bound by actually allocatable memory. The algorithm uses two bounds, an 511 // upper and a lower limit. The upper limit is the current highest amount of 512 // memory that could not be allocated, the lower limit is the current highest 513 // amount of memory that could be allocated. 514 // The algorithm iteratively refines the result by halving the difference 515 // between these limits, updating either the upper limit (if that value could 516 // not be allocated) or the lower limit (if the that value could be allocated) 517 // until the difference between these limits is "small". 518 519 // the minimum amount of memory we care about allocating. 520 const julong min_allocation_size = M; 521 522 julong upper_limit = *limit; 523 524 // first check a few trivial cases 525 if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) { 526 *limit = upper_limit; 527 } else if (!is_allocatable(min_allocation_size)) { 528 // we found that not even min_allocation_size is allocatable. Return it 529 // anyway. There is no point to search for a better value any more. 530 *limit = min_allocation_size; 531 } else { 532 // perform the binary search. 533 julong lower_limit = min_allocation_size; 534 while ((upper_limit - lower_limit) > min_allocation_size) { 535 julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit; 536 temp_limit = align_down(temp_limit, min_allocation_size); 537 if (is_allocatable(temp_limit)) { 538 lower_limit = temp_limit; 539 } else { 540 upper_limit = temp_limit; 541 } 542 } 543 *limit = lower_limit; 544 } 545 return true; 546 #endif 547 } 548 549 const char* os::get_current_directory(char *buf, size_t buflen) { 550 return getcwd(buf, buflen); 551 } 552 553 FILE* os::open(int fd, const char* mode) { 554 return ::fdopen(fd, mode); 555 } 556 557 void os::flockfile(FILE* fp) { 558 ::flockfile(fp); 559 } 560 561 void os::funlockfile(FILE* fp) { 562 ::funlockfile(fp); 563 } 564 565 DIR* os::opendir(const char* dirname) { 566 assert(dirname != NULL, "just checking"); 567 return ::opendir(dirname); 568 } 569 570 struct dirent* os::readdir(DIR* dirp) { 571 assert(dirp != NULL, "just checking"); 572 return ::readdir(dirp); 573 } 574 575 int os::closedir(DIR *dirp) { 576 assert(dirp != NULL, "just checking"); 577 return ::closedir(dirp); 578 } 579 580 // Builds a platform dependent Agent_OnLoad_<lib_name> function name 581 // which is used to find statically linked in agents. 582 // Parameters: 583 // sym_name: Symbol in library we are looking for 584 // lib_name: Name of library to look in, NULL for shared libs. 585 // is_absolute_path == true if lib_name is absolute path to agent 586 // such as "/a/b/libL.so" 587 // == false if only the base name of the library is passed in 588 // such as "L" 589 char* os::build_agent_function_name(const char *sym_name, const char *lib_name, 590 bool is_absolute_path) { 591 char *agent_entry_name; 592 size_t len; 593 size_t name_len; 594 size_t prefix_len = strlen(JNI_LIB_PREFIX); 595 size_t suffix_len = strlen(JNI_LIB_SUFFIX); 596 const char *start; 597 598 if (lib_name != NULL) { 599 name_len = strlen(lib_name); 600 if (is_absolute_path) { 601 // Need to strip path, prefix and suffix 602 if ((start = strrchr(lib_name, *os::file_separator())) != NULL) { 603 lib_name = ++start; 604 } 605 if (strlen(lib_name) <= (prefix_len + suffix_len)) { 606 return NULL; 607 } 608 lib_name += prefix_len; 609 name_len = strlen(lib_name) - suffix_len; 610 } 611 } 612 len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2; 613 agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread); 614 if (agent_entry_name == NULL) { 615 return NULL; 616 } 617 strcpy(agent_entry_name, sym_name); 618 if (lib_name != NULL) { 619 strcat(agent_entry_name, "_"); 620 strncat(agent_entry_name, lib_name, name_len); 621 } 622 return agent_entry_name; 623 } 624 625 int os::sleep(Thread* thread, jlong millis, bool interruptible) { 626 assert(thread == Thread::current(), "thread consistency check"); 627 628 ParkEvent * const slp = thread->_SleepEvent ; 629 slp->reset() ; 630 OrderAccess::fence() ; 631 632 if (interruptible) { 633 jlong prevtime = javaTimeNanos(); 634 635 for (;;) { 636 if (os::is_interrupted(thread, true)) { 637 return OS_INTRPT; 638 } 639 640 jlong newtime = javaTimeNanos(); 641 642 if (newtime - prevtime < 0) { 643 // time moving backwards, should only happen if no monotonic clock 644 // not a guarantee() because JVM should not abort on kernel/glibc bugs 645 assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected in os::sleep(interruptible)"); 646 } else { 647 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; 648 } 649 650 if (millis <= 0) { 651 return OS_OK; 652 } 653 654 prevtime = newtime; 655 656 { 657 assert(thread->is_Java_thread(), "sanity check"); 658 JavaThread *jt = (JavaThread *) thread; 659 ThreadBlockInVM tbivm(jt); 660 OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */); 661 662 jt->set_suspend_equivalent(); 663 // cleared by handle_special_suspend_equivalent_condition() or 664 // java_suspend_self() via check_and_wait_while_suspended() 665 666 slp->park(millis); 667 668 // were we externally suspended while we were waiting? 669 jt->check_and_wait_while_suspended(); 670 } 671 } 672 } else { 673 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 674 jlong prevtime = javaTimeNanos(); 675 676 for (;;) { 677 // It'd be nice to avoid the back-to-back javaTimeNanos() calls on 678 // the 1st iteration ... 679 jlong newtime = javaTimeNanos(); 680 681 if (newtime - prevtime < 0) { 682 // time moving backwards, should only happen if no monotonic clock 683 // not a guarantee() because JVM should not abort on kernel/glibc bugs 684 assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected on os::sleep(!interruptible)"); 685 } else { 686 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; 687 } 688 689 if (millis <= 0) break ; 690 691 prevtime = newtime; 692 slp->park(millis); 693 } 694 return OS_OK ; 695 } 696 } 697 698 //////////////////////////////////////////////////////////////////////////////// 699 // interrupt support 700 701 void os::interrupt(Thread* thread) { 702 debug_only(Thread::check_for_dangling_thread_pointer(thread);) 703 704 OSThread* osthread = thread->osthread(); 705 706 if (!osthread->interrupted()) { 707 osthread->set_interrupted(true); 708 // More than one thread can get here with the same value of osthread, 709 // resulting in multiple notifications. We do, however, want the store 710 // to interrupted() to be visible to other threads before we execute unpark(). 711 OrderAccess::fence(); 712 ParkEvent * const slp = thread->_SleepEvent ; 713 if (slp != NULL) slp->unpark() ; 714 } 715 716 // For JSR166. Unpark even if interrupt status already was set 717 if (thread->is_Java_thread()) 718 ((JavaThread*)thread)->parker()->unpark(); 719 720 ParkEvent * ev = thread->_ParkEvent ; 721 if (ev != NULL) ev->unpark() ; 722 } 723 724 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { 725 debug_only(Thread::check_for_dangling_thread_pointer(thread);) 726 727 OSThread* osthread = thread->osthread(); 728 729 bool interrupted = osthread->interrupted(); 730 731 // NOTE that since there is no "lock" around the interrupt and 732 // is_interrupted operations, there is the possibility that the 733 // interrupted flag (in osThread) will be "false" but that the 734 // low-level events will be in the signaled state. This is 735 // intentional. The effect of this is that Object.wait() and 736 // LockSupport.park() will appear to have a spurious wakeup, which 737 // is allowed and not harmful, and the possibility is so rare that 738 // it is not worth the added complexity to add yet another lock. 739 // For the sleep event an explicit reset is performed on entry 740 // to os::sleep, so there is no early return. It has also been 741 // recommended not to put the interrupted flag into the "event" 742 // structure because it hides the issue. 743 if (interrupted && clear_interrupted) { 744 osthread->set_interrupted(false); 745 // consider thread->_SleepEvent->reset() ... optional optimization 746 } 747 748 return interrupted; 749 } 750 751 752 753 static const struct { 754 int sig; const char* name; 755 } 756 g_signal_info[] = 757 { 758 { SIGABRT, "SIGABRT" }, 759 #ifdef SIGAIO 760 { SIGAIO, "SIGAIO" }, 761 #endif 762 { SIGALRM, "SIGALRM" }, 763 #ifdef SIGALRM1 764 { SIGALRM1, "SIGALRM1" }, 765 #endif 766 { SIGBUS, "SIGBUS" }, 767 #ifdef SIGCANCEL 768 { SIGCANCEL, "SIGCANCEL" }, 769 #endif 770 { SIGCHLD, "SIGCHLD" }, 771 #ifdef SIGCLD 772 { SIGCLD, "SIGCLD" }, 773 #endif 774 { SIGCONT, "SIGCONT" }, 775 #ifdef SIGCPUFAIL 776 { SIGCPUFAIL, "SIGCPUFAIL" }, 777 #endif 778 #ifdef SIGDANGER 779 { SIGDANGER, "SIGDANGER" }, 780 #endif 781 #ifdef SIGDIL 782 { SIGDIL, "SIGDIL" }, 783 #endif 784 #ifdef SIGEMT 785 { SIGEMT, "SIGEMT" }, 786 #endif 787 { SIGFPE, "SIGFPE" }, 788 #ifdef SIGFREEZE 789 { SIGFREEZE, "SIGFREEZE" }, 790 #endif 791 #ifdef SIGGFAULT 792 { SIGGFAULT, "SIGGFAULT" }, 793 #endif 794 #ifdef SIGGRANT 795 { SIGGRANT, "SIGGRANT" }, 796 #endif 797 { SIGHUP, "SIGHUP" }, 798 { SIGILL, "SIGILL" }, 799 { SIGINT, "SIGINT" }, 800 #ifdef SIGIO 801 { SIGIO, "SIGIO" }, 802 #endif 803 #ifdef SIGIOINT 804 { SIGIOINT, "SIGIOINT" }, 805 #endif 806 #ifdef SIGIOT 807 // SIGIOT is there for BSD compatibility, but on most Unices just a 808 // synonym for SIGABRT. The result should be "SIGABRT", not 809 // "SIGIOT". 810 #if (SIGIOT != SIGABRT ) 811 { SIGIOT, "SIGIOT" }, 812 #endif 813 #endif 814 #ifdef SIGKAP 815 { SIGKAP, "SIGKAP" }, 816 #endif 817 { SIGKILL, "SIGKILL" }, 818 #ifdef SIGLOST 819 { SIGLOST, "SIGLOST" }, 820 #endif 821 #ifdef SIGLWP 822 { SIGLWP, "SIGLWP" }, 823 #endif 824 #ifdef SIGLWPTIMER 825 { SIGLWPTIMER, "SIGLWPTIMER" }, 826 #endif 827 #ifdef SIGMIGRATE 828 { SIGMIGRATE, "SIGMIGRATE" }, 829 #endif 830 #ifdef SIGMSG 831 { SIGMSG, "SIGMSG" }, 832 #endif 833 { SIGPIPE, "SIGPIPE" }, 834 #ifdef SIGPOLL 835 { SIGPOLL, "SIGPOLL" }, 836 #endif 837 #ifdef SIGPRE 838 { SIGPRE, "SIGPRE" }, 839 #endif 840 { SIGPROF, "SIGPROF" }, 841 #ifdef SIGPTY 842 { SIGPTY, "SIGPTY" }, 843 #endif 844 #ifdef SIGPWR 845 { SIGPWR, "SIGPWR" }, 846 #endif 847 { SIGQUIT, "SIGQUIT" }, 848 #ifdef SIGRECONFIG 849 { SIGRECONFIG, "SIGRECONFIG" }, 850 #endif 851 #ifdef SIGRECOVERY 852 { SIGRECOVERY, "SIGRECOVERY" }, 853 #endif 854 #ifdef SIGRESERVE 855 { SIGRESERVE, "SIGRESERVE" }, 856 #endif 857 #ifdef SIGRETRACT 858 { SIGRETRACT, "SIGRETRACT" }, 859 #endif 860 #ifdef SIGSAK 861 { SIGSAK, "SIGSAK" }, 862 #endif 863 { SIGSEGV, "SIGSEGV" }, 864 #ifdef SIGSOUND 865 { SIGSOUND, "SIGSOUND" }, 866 #endif 867 #ifdef SIGSTKFLT 868 { SIGSTKFLT, "SIGSTKFLT" }, 869 #endif 870 { SIGSTOP, "SIGSTOP" }, 871 { SIGSYS, "SIGSYS" }, 872 #ifdef SIGSYSERROR 873 { SIGSYSERROR, "SIGSYSERROR" }, 874 #endif 875 #ifdef SIGTALRM 876 { SIGTALRM, "SIGTALRM" }, 877 #endif 878 { SIGTERM, "SIGTERM" }, 879 #ifdef SIGTHAW 880 { SIGTHAW, "SIGTHAW" }, 881 #endif 882 { SIGTRAP, "SIGTRAP" }, 883 #ifdef SIGTSTP 884 { SIGTSTP, "SIGTSTP" }, 885 #endif 886 { SIGTTIN, "SIGTTIN" }, 887 { SIGTTOU, "SIGTTOU" }, 888 #ifdef SIGURG 889 { SIGURG, "SIGURG" }, 890 #endif 891 { SIGUSR1, "SIGUSR1" }, 892 { SIGUSR2, "SIGUSR2" }, 893 #ifdef SIGVIRT 894 { SIGVIRT, "SIGVIRT" }, 895 #endif 896 { SIGVTALRM, "SIGVTALRM" }, 897 #ifdef SIGWAITING 898 { SIGWAITING, "SIGWAITING" }, 899 #endif 900 #ifdef SIGWINCH 901 { SIGWINCH, "SIGWINCH" }, 902 #endif 903 #ifdef SIGWINDOW 904 { SIGWINDOW, "SIGWINDOW" }, 905 #endif 906 { SIGXCPU, "SIGXCPU" }, 907 { SIGXFSZ, "SIGXFSZ" }, 908 #ifdef SIGXRES 909 { SIGXRES, "SIGXRES" }, 910 #endif 911 { -1, NULL } 912 }; 913 914 // Returned string is a constant. For unknown signals "UNKNOWN" is returned. 915 const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) { 916 917 const char* ret = NULL; 918 919 #ifdef SIGRTMIN 920 if (sig >= SIGRTMIN && sig <= SIGRTMAX) { 921 if (sig == SIGRTMIN) { 922 ret = "SIGRTMIN"; 923 } else if (sig == SIGRTMAX) { 924 ret = "SIGRTMAX"; 925 } else { 926 jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN); 927 return out; 928 } 929 } 930 #endif 931 932 if (sig > 0) { 933 for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) { 934 if (g_signal_info[idx].sig == sig) { 935 ret = g_signal_info[idx].name; 936 break; 937 } 938 } 939 } 940 941 if (!ret) { 942 if (!is_valid_signal(sig)) { 943 ret = "INVALID"; 944 } else { 945 ret = "UNKNOWN"; 946 } 947 } 948 949 if (out && outlen > 0) { 950 strncpy(out, ret, outlen); 951 out[outlen - 1] = '\0'; 952 } 953 return out; 954 } 955 956 int os::Posix::get_signal_number(const char* signal_name) { 957 char tmp[30]; 958 const char* s = signal_name; 959 if (s[0] != 'S' || s[1] != 'I' || s[2] != 'G') { 960 jio_snprintf(tmp, sizeof(tmp), "SIG%s", signal_name); 961 s = tmp; 962 } 963 for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) { 964 if (strcmp(g_signal_info[idx].name, s) == 0) { 965 return g_signal_info[idx].sig; 966 } 967 } 968 return -1; 969 } 970 971 int os::get_signal_number(const char* signal_name) { 972 return os::Posix::get_signal_number(signal_name); 973 } 974 975 // Returns true if signal number is valid. 976 bool os::Posix::is_valid_signal(int sig) { 977 // MacOS not really POSIX compliant: sigaddset does not return 978 // an error for invalid signal numbers. However, MacOS does not 979 // support real time signals and simply seems to have just 33 980 // signals with no holes in the signal range. 981 #ifdef __APPLE__ 982 return sig >= 1 && sig < NSIG; 983 #else 984 // Use sigaddset to check for signal validity. 985 sigset_t set; 986 sigemptyset(&set); 987 if (sigaddset(&set, sig) == -1 && errno == EINVAL) { 988 return false; 989 } 990 return true; 991 #endif 992 } 993 994 bool os::Posix::is_sig_ignored(int sig) { 995 struct sigaction oact; 996 sigaction(sig, (struct sigaction*)NULL, &oact); 997 void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction) 998 : CAST_FROM_FN_PTR(void*, oact.sa_handler); 999 if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) { 1000 return true; 1001 } else { 1002 return false; 1003 } 1004 } 1005 1006 // Returns: 1007 // NULL for an invalid signal number 1008 // "SIG<num>" for a valid but unknown signal number 1009 // signal name otherwise. 1010 const char* os::exception_name(int sig, char* buf, size_t size) { 1011 if (!os::Posix::is_valid_signal(sig)) { 1012 return NULL; 1013 } 1014 const char* const name = os::Posix::get_signal_name(sig, buf, size); 1015 if (strcmp(name, "UNKNOWN") == 0) { 1016 jio_snprintf(buf, size, "SIG%d", sig); 1017 } 1018 return buf; 1019 } 1020 1021 #define NUM_IMPORTANT_SIGS 32 1022 // Returns one-line short description of a signal set in a user provided buffer. 1023 const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) { 1024 assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size"); 1025 // Note: for shortness, just print out the first 32. That should 1026 // cover most of the useful ones, apart from realtime signals. 1027 for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) { 1028 const int rc = sigismember(set, sig); 1029 if (rc == -1 && errno == EINVAL) { 1030 buffer[sig-1] = '?'; 1031 } else { 1032 buffer[sig-1] = rc == 0 ? '0' : '1'; 1033 } 1034 } 1035 buffer[NUM_IMPORTANT_SIGS] = 0; 1036 return buffer; 1037 } 1038 1039 // Prints one-line description of a signal set. 1040 void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) { 1041 char buf[NUM_IMPORTANT_SIGS + 1]; 1042 os::Posix::describe_signal_set_short(set, buf, sizeof(buf)); 1043 st->print("%s", buf); 1044 } 1045 1046 // Writes one-line description of a combination of sigaction.sa_flags into a user 1047 // provided buffer. Returns that buffer. 1048 const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) { 1049 char* p = buffer; 1050 size_t remaining = size; 1051 bool first = true; 1052 int idx = 0; 1053 1054 assert(buffer, "invalid argument"); 1055 1056 if (size == 0) { 1057 return buffer; 1058 } 1059 1060 strncpy(buffer, "none", size); 1061 1062 const struct { 1063 // NB: i is an unsigned int here because SA_RESETHAND is on some 1064 // systems 0x80000000, which is implicitly unsigned. Assignining 1065 // it to an int field would be an overflow in unsigned-to-signed 1066 // conversion. 1067 unsigned int i; 1068 const char* s; 1069 } flaginfo [] = { 1070 { SA_NOCLDSTOP, "SA_NOCLDSTOP" }, 1071 { SA_ONSTACK, "SA_ONSTACK" }, 1072 { SA_RESETHAND, "SA_RESETHAND" }, 1073 { SA_RESTART, "SA_RESTART" }, 1074 { SA_SIGINFO, "SA_SIGINFO" }, 1075 { SA_NOCLDWAIT, "SA_NOCLDWAIT" }, 1076 { SA_NODEFER, "SA_NODEFER" }, 1077 #ifdef AIX 1078 { SA_ONSTACK, "SA_ONSTACK" }, 1079 { SA_OLDSTYLE, "SA_OLDSTYLE" }, 1080 #endif 1081 { 0, NULL } 1082 }; 1083 1084 for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) { 1085 if (flags & flaginfo[idx].i) { 1086 if (first) { 1087 jio_snprintf(p, remaining, "%s", flaginfo[idx].s); 1088 first = false; 1089 } else { 1090 jio_snprintf(p, remaining, "|%s", flaginfo[idx].s); 1091 } 1092 const size_t len = strlen(p); 1093 p += len; 1094 remaining -= len; 1095 } 1096 } 1097 1098 buffer[size - 1] = '\0'; 1099 1100 return buffer; 1101 } 1102 1103 // Prints one-line description of a combination of sigaction.sa_flags. 1104 void os::Posix::print_sa_flags(outputStream* st, int flags) { 1105 char buffer[0x100]; 1106 os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer)); 1107 st->print("%s", buffer); 1108 } 1109 1110 // Helper function for os::Posix::print_siginfo_...(): 1111 // return a textual description for signal code. 1112 struct enum_sigcode_desc_t { 1113 const char* s_name; 1114 const char* s_desc; 1115 }; 1116 1117 static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) { 1118 1119 const struct { 1120 int sig; int code; const char* s_code; const char* s_desc; 1121 } t1 [] = { 1122 { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode." }, 1123 { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand." }, 1124 { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode." }, 1125 { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap." }, 1126 { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode." }, 1127 { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register." }, 1128 { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error." }, 1129 { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error." }, 1130 #if defined(IA64) && defined(LINUX) 1131 { SIGILL, ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" }, 1132 { SIGILL, ILL_BREAK, "ILL_BREAK", "Application Break instruction" }, 1133 #endif 1134 { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero." }, 1135 { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow." }, 1136 { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating-point divide by zero." }, 1137 { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating-point overflow." }, 1138 { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating-point underflow." }, 1139 { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating-point inexact result." }, 1140 { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating-point operation." }, 1141 { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range." }, 1142 { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object." }, 1143 { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for mapped object." }, 1144 #ifdef AIX 1145 // no explanation found what keyerr would be 1146 { SIGSEGV, SEGV_KEYERR, "SEGV_KEYERR", "key error" }, 1147 #endif 1148 #if defined(IA64) && !defined(AIX) 1149 { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" }, 1150 #endif 1151 #if defined(__sparc) && defined(SOLARIS) 1152 // define Solaris Sparc M7 ADI SEGV signals 1153 #if !defined(SEGV_ACCADI) 1154 #define SEGV_ACCADI 3 1155 #endif 1156 { SIGSEGV, SEGV_ACCADI, "SEGV_ACCADI", "ADI not enabled for mapped object." }, 1157 #if !defined(SEGV_ACCDERR) 1158 #define SEGV_ACCDERR 4 1159 #endif 1160 { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." }, 1161 #if !defined(SEGV_ACCPERR) 1162 #define SEGV_ACCPERR 5 1163 #endif 1164 { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." }, 1165 #endif // defined(__sparc) && defined(SOLARIS) 1166 { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment." }, 1167 { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Nonexistent physical address." }, 1168 { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object-specific hardware error." }, 1169 { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint." }, 1170 { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap." }, 1171 { SIGCHLD, CLD_EXITED, "CLD_EXITED", "Child has exited." }, 1172 { SIGCHLD, CLD_KILLED, "CLD_KILLED", "Child has terminated abnormally and did not create a core file." }, 1173 { SIGCHLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally and created a core file." }, 1174 { SIGCHLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped." }, 1175 { SIGCHLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped." }, 1176 { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." }, 1177 #ifdef SIGPOLL 1178 { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available." }, 1179 { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available." }, 1180 { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error." }, 1181 { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available." }, 1182 { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected. [Option End]" }, 1183 #endif 1184 { -1, -1, NULL, NULL } 1185 }; 1186 1187 // Codes valid in any signal context. 1188 const struct { 1189 int code; const char* s_code; const char* s_desc; 1190 } t2 [] = { 1191 { SI_USER, "SI_USER", "Signal sent by kill()." }, 1192 { SI_QUEUE, "SI_QUEUE", "Signal sent by the sigqueue()." }, 1193 { SI_TIMER, "SI_TIMER", "Signal generated by expiration of a timer set by timer_settime()." }, 1194 { SI_ASYNCIO, "SI_ASYNCIO", "Signal generated by completion of an asynchronous I/O request." }, 1195 { SI_MESGQ, "SI_MESGQ", "Signal generated by arrival of a message on an empty message queue." }, 1196 // Linux specific 1197 #ifdef SI_TKILL 1198 { SI_TKILL, "SI_TKILL", "Signal sent by tkill (pthread_kill)" }, 1199 #endif 1200 #ifdef SI_DETHREAD 1201 { SI_DETHREAD, "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" }, 1202 #endif 1203 #ifdef SI_KERNEL 1204 { SI_KERNEL, "SI_KERNEL", "Signal sent by kernel." }, 1205 #endif 1206 #ifdef SI_SIGIO 1207 { SI_SIGIO, "SI_SIGIO", "Signal sent by queued SIGIO" }, 1208 #endif 1209 1210 #ifdef AIX 1211 { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" }, 1212 { SI_EMPTY, "SI_EMPTY", "siginfo contains no useful information" }, 1213 #endif 1214 1215 #ifdef __sun 1216 { SI_NOINFO, "SI_NOINFO", "No signal information" }, 1217 { SI_RCTL, "SI_RCTL", "kernel generated signal via rctl action" }, 1218 { SI_LWP, "SI_LWP", "Signal sent via lwp_kill" }, 1219 #endif 1220 1221 { -1, NULL, NULL } 1222 }; 1223 1224 const char* s_code = NULL; 1225 const char* s_desc = NULL; 1226 1227 for (int i = 0; t1[i].sig != -1; i ++) { 1228 if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) { 1229 s_code = t1[i].s_code; 1230 s_desc = t1[i].s_desc; 1231 break; 1232 } 1233 } 1234 1235 if (s_code == NULL) { 1236 for (int i = 0; t2[i].s_code != NULL; i ++) { 1237 if (t2[i].code == si->si_code) { 1238 s_code = t2[i].s_code; 1239 s_desc = t2[i].s_desc; 1240 } 1241 } 1242 } 1243 1244 if (s_code == NULL) { 1245 out->s_name = "unknown"; 1246 out->s_desc = "unknown"; 1247 return false; 1248 } 1249 1250 out->s_name = s_code; 1251 out->s_desc = s_desc; 1252 1253 return true; 1254 } 1255 1256 void os::print_siginfo(outputStream* os, const void* si0) { 1257 1258 const siginfo_t* const si = (const siginfo_t*) si0; 1259 1260 char buf[20]; 1261 os->print("siginfo:"); 1262 1263 if (!si) { 1264 os->print(" <null>"); 1265 return; 1266 } 1267 1268 const int sig = si->si_signo; 1269 1270 os->print(" si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf))); 1271 1272 enum_sigcode_desc_t ed; 1273 get_signal_code_description(si, &ed); 1274 os->print(", si_code: %d (%s)", si->si_code, ed.s_name); 1275 1276 if (si->si_errno) { 1277 os->print(", si_errno: %d", si->si_errno); 1278 } 1279 1280 // Output additional information depending on the signal code. 1281 1282 // Note: Many implementations lump si_addr, si_pid, si_uid etc. together as unions, 1283 // so it depends on the context which member to use. For synchronous error signals, 1284 // we print si_addr, unless the signal was sent by another process or thread, in 1285 // which case we print out pid or tid of the sender. 1286 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) { 1287 const pid_t pid = si->si_pid; 1288 os->print(", si_pid: %ld", (long) pid); 1289 if (IS_VALID_PID(pid)) { 1290 const pid_t me = getpid(); 1291 if (me == pid) { 1292 os->print(" (current process)"); 1293 } 1294 } else { 1295 os->print(" (invalid)"); 1296 } 1297 os->print(", si_uid: %ld", (long) si->si_uid); 1298 if (sig == SIGCHLD) { 1299 os->print(", si_status: %d", si->si_status); 1300 } 1301 } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 1302 sig == SIGTRAP || sig == SIGFPE) { 1303 os->print(", si_addr: " PTR_FORMAT, p2i(si->si_addr)); 1304 #ifdef SIGPOLL 1305 } else if (sig == SIGPOLL) { 1306 os->print(", si_band: %ld", si->si_band); 1307 #endif 1308 } 1309 1310 } 1311 1312 int os::Posix::unblock_thread_signal_mask(const sigset_t *set) { 1313 return pthread_sigmask(SIG_UNBLOCK, set, NULL); 1314 } 1315 1316 address os::Posix::ucontext_get_pc(const ucontext_t* ctx) { 1317 #if defined(AIX) 1318 return Aix::ucontext_get_pc(ctx); 1319 #elif defined(BSD) 1320 return Bsd::ucontext_get_pc(ctx); 1321 #elif defined(LINUX) 1322 return Linux::ucontext_get_pc(ctx); 1323 #elif defined(SOLARIS) 1324 return Solaris::ucontext_get_pc(ctx); 1325 #else 1326 VMError::report_and_die("unimplemented ucontext_get_pc"); 1327 #endif 1328 } 1329 1330 void os::Posix::ucontext_set_pc(ucontext_t* ctx, address pc) { 1331 #if defined(AIX) 1332 Aix::ucontext_set_pc(ctx, pc); 1333 #elif defined(BSD) 1334 Bsd::ucontext_set_pc(ctx, pc); 1335 #elif defined(LINUX) 1336 Linux::ucontext_set_pc(ctx, pc); 1337 #elif defined(SOLARIS) 1338 Solaris::ucontext_set_pc(ctx, pc); 1339 #else 1340 VMError::report_and_die("unimplemented ucontext_get_pc"); 1341 #endif 1342 } 1343 1344 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) { 1345 size_t stack_size = 0; 1346 size_t guard_size = 0; 1347 int detachstate = 0; 1348 pthread_attr_getstacksize(attr, &stack_size); 1349 pthread_attr_getguardsize(attr, &guard_size); 1350 // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp. 1351 LINUX_ONLY(stack_size -= guard_size); 1352 pthread_attr_getdetachstate(attr, &detachstate); 1353 jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s", 1354 stack_size / 1024, guard_size / 1024, 1355 (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable")); 1356 return buf; 1357 } 1358 1359 char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) { 1360 1361 if (filename == NULL || outbuf == NULL || outbuflen < 1) { 1362 assert(false, "os::Posix::realpath: invalid arguments."); 1363 errno = EINVAL; 1364 return NULL; 1365 } 1366 1367 char* result = NULL; 1368 1369 // This assumes platform realpath() is implemented according to POSIX.1-2008. 1370 // POSIX.1-2008 allows to specify NULL for the output buffer, in which case 1371 // output buffer is dynamically allocated and must be ::free()'d by the caller. 1372 char* p = ::realpath(filename, NULL); 1373 if (p != NULL) { 1374 if (strlen(p) < outbuflen) { 1375 strcpy(outbuf, p); 1376 result = outbuf; 1377 } else { 1378 errno = ENAMETOOLONG; 1379 } 1380 ::free(p); // *not* os::free 1381 } else { 1382 // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath 1383 // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and 1384 // that it complains about the NULL we handed down as user buffer. 1385 // In this case, use the user provided buffer but at least check whether realpath caused 1386 // a memory overwrite. 1387 if (errno == EINVAL) { 1388 outbuf[outbuflen - 1] = '\0'; 1389 p = ::realpath(filename, outbuf); 1390 if (p != NULL) { 1391 guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected."); 1392 result = p; 1393 } 1394 } 1395 } 1396 return result; 1397 1398 } 1399 1400 int os::stat(const char *path, struct stat *sbuf) { 1401 return ::stat(path, sbuf); 1402 } 1403 1404 char * os::native_path(char *path) { 1405 return path; 1406 } 1407 1408 // Check minimum allowable stack sizes for thread creation and to initialize 1409 // the java system classes, including StackOverflowError - depends on page 1410 // size. 1411 // The space needed for frames during startup is platform dependent. It 1412 // depends on word size, platform calling conventions, C frame layout and 1413 // interpreter/C1/C2 design decisions. Therefore this is given in a 1414 // platform (os/cpu) dependent constant. 1415 // To this, space for guard mechanisms is added, which depends on the 1416 // page size which again depends on the concrete system the VM is running 1417 // on. Space for libc guard pages is not included in this size. 1418 jint os::Posix::set_minimum_stack_sizes() { 1419 size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN); 1420 1421 _java_thread_min_stack_allowed = _java_thread_min_stack_allowed + 1422 JavaThread::stack_guard_zone_size() + 1423 JavaThread::stack_shadow_zone_size(); 1424 1425 _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size()); 1426 _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed); 1427 1428 size_t stack_size_in_bytes = ThreadStackSize * K; 1429 if (stack_size_in_bytes != 0 && 1430 stack_size_in_bytes < _java_thread_min_stack_allowed) { 1431 // The '-Xss' and '-XX:ThreadStackSize=N' options both set 1432 // ThreadStackSize so we go with "Java thread stack size" instead 1433 // of "ThreadStackSize" to be more friendly. 1434 tty->print_cr("\nThe Java thread stack size specified is too small. " 1435 "Specify at least " SIZE_FORMAT "k", 1436 _java_thread_min_stack_allowed / K); 1437 return JNI_ERR; 1438 } 1439 1440 // Make the stack size a multiple of the page size so that 1441 // the yellow/red zones can be guarded. 1442 JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size())); 1443 1444 // Reminder: a compiler thread is a Java thread. 1445 _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed + 1446 JavaThread::stack_guard_zone_size() + 1447 JavaThread::stack_shadow_zone_size(); 1448 1449 _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size()); 1450 _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed); 1451 1452 stack_size_in_bytes = CompilerThreadStackSize * K; 1453 if (stack_size_in_bytes != 0 && 1454 stack_size_in_bytes < _compiler_thread_min_stack_allowed) { 1455 tty->print_cr("\nThe CompilerThreadStackSize specified is too small. " 1456 "Specify at least " SIZE_FORMAT "k", 1457 _compiler_thread_min_stack_allowed / K); 1458 return JNI_ERR; 1459 } 1460 1461 _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size()); 1462 _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed); 1463 1464 stack_size_in_bytes = VMThreadStackSize * K; 1465 if (stack_size_in_bytes != 0 && 1466 stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) { 1467 tty->print_cr("\nThe VMThreadStackSize specified is too small. " 1468 "Specify at least " SIZE_FORMAT "k", 1469 _vm_internal_thread_min_stack_allowed / K); 1470 return JNI_ERR; 1471 } 1472 return JNI_OK; 1473 } 1474 1475 // Called when creating the thread. The minimum stack sizes have already been calculated 1476 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) { 1477 size_t stack_size; 1478 if (req_stack_size == 0) { 1479 stack_size = default_stack_size(thr_type); 1480 } else { 1481 stack_size = req_stack_size; 1482 } 1483 1484 switch (thr_type) { 1485 case os::java_thread: 1486 // Java threads use ThreadStackSize which default value can be 1487 // changed with the flag -Xss 1488 if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) { 1489 // no requested size and we have a more specific default value 1490 stack_size = JavaThread::stack_size_at_create(); 1491 } 1492 stack_size = MAX2(stack_size, 1493 _java_thread_min_stack_allowed); 1494 break; 1495 case os::compiler_thread: 1496 if (req_stack_size == 0 && CompilerThreadStackSize > 0) { 1497 // no requested size and we have a more specific default value 1498 stack_size = (size_t)(CompilerThreadStackSize * K); 1499 } 1500 stack_size = MAX2(stack_size, 1501 _compiler_thread_min_stack_allowed); 1502 break; 1503 case os::vm_thread: 1504 case os::pgc_thread: 1505 case os::cgc_thread: 1506 case os::watcher_thread: 1507 default: // presume the unknown thr_type is a VM internal 1508 if (req_stack_size == 0 && VMThreadStackSize > 0) { 1509 // no requested size and we have a more specific default value 1510 stack_size = (size_t)(VMThreadStackSize * K); 1511 } 1512 1513 stack_size = MAX2(stack_size, 1514 _vm_internal_thread_min_stack_allowed); 1515 break; 1516 } 1517 1518 // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size. 1519 // Be careful not to round up to 0. Align down in that case. 1520 if (stack_size <= SIZE_MAX - vm_page_size()) { 1521 stack_size = align_up(stack_size, vm_page_size()); 1522 } else { 1523 stack_size = align_down(stack_size, vm_page_size()); 1524 } 1525 1526 return stack_size; 1527 } 1528 1529 bool os::Posix::is_root(uid_t uid){ 1530 return ROOT_UID == uid; 1531 } 1532 1533 bool os::Posix::matches_effective_uid_or_root(uid_t uid) { 1534 return is_root(uid) || geteuid() == uid; 1535 } 1536 1537 bool os::Posix::matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid) { 1538 return is_root(uid) || (geteuid() == uid && getegid() == gid); 1539 } 1540 1541 Thread* os::ThreadCrashProtection::_protected_thread = NULL; 1542 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL; 1543 volatile intptr_t os::ThreadCrashProtection::_crash_mux = 0; 1544 1545 os::ThreadCrashProtection::ThreadCrashProtection() { 1546 } 1547 1548 /* 1549 * See the caveats for this class in os_posix.hpp 1550 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this 1551 * method and returns false. If none of the signals are raised, returns true. 1552 * The callback is supposed to provide the method that should be protected. 1553 */ 1554 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) { 1555 sigset_t saved_sig_mask; 1556 1557 Thread::muxAcquire(&_crash_mux, "CrashProtection"); 1558 1559 _protected_thread = Thread::current_or_null(); 1560 assert(_protected_thread != NULL, "Cannot crash protect a NULL thread"); 1561 1562 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask 1563 // since on at least some systems (OS X) siglongjmp will restore the mask 1564 // for the process, not the thread 1565 pthread_sigmask(0, NULL, &saved_sig_mask); 1566 if (sigsetjmp(_jmpbuf, 0) == 0) { 1567 // make sure we can see in the signal handler that we have crash protection 1568 // installed 1569 _crash_protection = this; 1570 cb.call(); 1571 // and clear the crash protection 1572 _crash_protection = NULL; 1573 _protected_thread = NULL; 1574 Thread::muxRelease(&_crash_mux); 1575 return true; 1576 } 1577 // this happens when we siglongjmp() back 1578 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL); 1579 _crash_protection = NULL; 1580 _protected_thread = NULL; 1581 Thread::muxRelease(&_crash_mux); 1582 return false; 1583 } 1584 1585 void os::ThreadCrashProtection::restore() { 1586 assert(_crash_protection != NULL, "must have crash protection"); 1587 siglongjmp(_jmpbuf, 1); 1588 } 1589 1590 void os::ThreadCrashProtection::check_crash_protection(int sig, 1591 Thread* thread) { 1592 1593 if (thread != NULL && 1594 thread == _protected_thread && 1595 _crash_protection != NULL) { 1596 1597 if (sig == SIGSEGV || sig == SIGBUS) { 1598 _crash_protection->restore(); 1599 } 1600 } 1601 } 1602 1603 1604 // Shared pthread_mutex/cond based PlatformEvent implementation. 1605 // Not currently usable by Solaris. 1606 1607 #ifndef SOLARIS 1608 1609 // Shared condattr object for use with relative timed-waits. Will be associated 1610 // with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes, 1611 // but otherwise whatever default is used by the platform - generally the 1612 // time-of-day clock. 1613 static pthread_condattr_t _condAttr[1]; 1614 1615 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not 1616 // all systems (e.g. FreeBSD) map the default to "normal". 1617 static pthread_mutexattr_t _mutexAttr[1]; 1618 1619 // common basic initialization that is always supported 1620 static void pthread_init_common(void) { 1621 int status; 1622 if ((status = pthread_condattr_init(_condAttr)) != 0) { 1623 fatal("pthread_condattr_init: %s", os::strerror(status)); 1624 } 1625 if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) { 1626 fatal("pthread_mutexattr_init: %s", os::strerror(status)); 1627 } 1628 if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) { 1629 fatal("pthread_mutexattr_settype: %s", os::strerror(status)); 1630 } 1631 } 1632 1633 // Not all POSIX types and API's are available on all notionally "posix" 1634 // platforms. If we have build-time support then we will check for actual 1635 // runtime support via dlopen/dlsym lookup. This allows for running on an 1636 // older OS version compared to the build platform. But if there is no 1637 // build time support then there cannot be any runtime support as we do not 1638 // know what the runtime types would be (for example clockid_t might be an 1639 // int or int64_t). 1640 // 1641 #ifdef SUPPORTS_CLOCK_MONOTONIC 1642 1643 // This means we have clockid_t, clock_gettime et al and CLOCK_MONOTONIC 1644 1645 static int (*_clock_gettime)(clockid_t, struct timespec *); 1646 static int (*_clock_getres)(clockid_t, struct timespec *); 1647 static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t); 1648 1649 static bool _use_clock_monotonic_condattr; 1650 1651 // Exported clock functionality 1652 1653 int os::Posix::clock_gettime(clockid_t clock_id, struct timespec *tp) { 1654 return _clock_gettime != NULL ? _clock_gettime(clock_id, tp) : -1; 1655 } 1656 1657 int os::Posix::clock_getres(clockid_t clock_id, struct timespec *tp) { 1658 return _clock_getres != NULL ? _clock_getres(clock_id, tp) : -1; 1659 } 1660 1661 bool os::Posix::supports_monotonic_clock() { 1662 return _clock_gettime != NULL; 1663 } 1664 1665 // Determine what POSIX API's are present and do appropriate 1666 // configuration. 1667 void os::Posix::init(void) { 1668 1669 // NOTE: no logging available when this is called. Put logging 1670 // statements in init_2(). 1671 1672 // 1. Check for CLOCK_MONOTONIC support. 1673 1674 void* handle = NULL; 1675 1676 // For linux we need librt, for other OS we can find 1677 // this function in regular libc. 1678 #ifdef NEEDS_LIBRT 1679 // We do dlopen's in this particular order due to bug in linux 1680 // dynamic loader (see 6348968) leading to crash on exit. 1681 handle = dlopen("librt.so.1", RTLD_LAZY); 1682 if (handle == NULL) { 1683 handle = dlopen("librt.so", RTLD_LAZY); 1684 } 1685 #endif 1686 1687 if (handle == NULL) { 1688 handle = RTLD_DEFAULT; 1689 } 1690 1691 _clock_gettime = NULL; 1692 _clock_getres = NULL; 1693 1694 int (*clock_getres_func)(clockid_t, struct timespec*) = 1695 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres"); 1696 int (*clock_gettime_func)(clockid_t, struct timespec*) = 1697 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime"); 1698 if (clock_getres_func != NULL && clock_gettime_func != NULL) { 1699 // We assume that if both clock_gettime and clock_getres support 1700 // CLOCK_MONOTONIC then the OS provides true high-res monotonic clock. 1701 struct timespec res; 1702 struct timespec tp; 1703 if (clock_getres_func(CLOCK_MONOTONIC, &res) == 0 && 1704 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) { 1705 // Yes, monotonic clock is supported. 1706 _clock_gettime = clock_gettime_func; 1707 _clock_getres = clock_getres_func; 1708 } else { 1709 #ifdef NEEDS_LIBRT 1710 // Close librt if there is no monotonic clock. 1711 if (handle != RTLD_DEFAULT) { 1712 dlclose(handle); 1713 } 1714 #endif 1715 } 1716 } 1717 1718 // 2. Check for pthread_condattr_setclock support. 1719 1720 _pthread_condattr_setclock = NULL; 1721 1722 // libpthread is already loaded. 1723 int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) = 1724 (int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT, 1725 "pthread_condattr_setclock"); 1726 if (condattr_setclock_func != NULL) { 1727 _pthread_condattr_setclock = condattr_setclock_func; 1728 } 1729 1730 // Now do general initialization. 1731 1732 pthread_init_common(); 1733 1734 int status; 1735 if (_pthread_condattr_setclock != NULL && _clock_gettime != NULL) { 1736 if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) { 1737 if (status == EINVAL) { 1738 _use_clock_monotonic_condattr = false; 1739 warning("Unable to use monotonic clock with relative timed-waits" \ 1740 " - changes to the time-of-day clock may have adverse affects"); 1741 } else { 1742 fatal("pthread_condattr_setclock: %s", os::strerror(status)); 1743 } 1744 } else { 1745 _use_clock_monotonic_condattr = true; 1746 } 1747 } else { 1748 _use_clock_monotonic_condattr = false; 1749 } 1750 } 1751 1752 void os::Posix::init_2(void) { 1753 log_info(os)("Use of CLOCK_MONOTONIC is%s supported", 1754 (_clock_gettime != NULL ? "" : " not")); 1755 log_info(os)("Use of pthread_condattr_setclock is%s supported", 1756 (_pthread_condattr_setclock != NULL ? "" : " not")); 1757 log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s", 1758 _use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock"); 1759 } 1760 1761 #else // !SUPPORTS_CLOCK_MONOTONIC 1762 1763 void os::Posix::init(void) { 1764 pthread_init_common(); 1765 } 1766 1767 void os::Posix::init_2(void) { 1768 log_info(os)("Use of CLOCK_MONOTONIC is not supported"); 1769 log_info(os)("Use of pthread_condattr_setclock is not supported"); 1770 log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with the default clock"); 1771 } 1772 1773 #endif // SUPPORTS_CLOCK_MONOTONIC 1774 1775 os::PlatformEvent::PlatformEvent() { 1776 int status = pthread_cond_init(_cond, _condAttr); 1777 assert_status(status == 0, status, "cond_init"); 1778 status = pthread_mutex_init(_mutex, _mutexAttr); 1779 assert_status(status == 0, status, "mutex_init"); 1780 _event = 0; 1781 _nParked = 0; 1782 } 1783 1784 // Utility to convert the given timeout to an absolute timespec 1785 // (based on the appropriate clock) to use with pthread_cond_timewait. 1786 // The clock queried here must be the clock used to manage the 1787 // timeout of the condition variable. 1788 // 1789 // The passed in timeout value is either a relative time in nanoseconds 1790 // or an absolute time in milliseconds. A relative timeout will be 1791 // associated with CLOCK_MONOTONIC if available; otherwise, or if absolute, 1792 // the default time-of-day clock will be used. 1793 1794 // Given time is a 64-bit value and the time_t used in the timespec is 1795 // sometimes a signed-32-bit value we have to watch for overflow if times 1796 // way in the future are given. Further on Solaris versions 1797 // prior to 10 there is a restriction (see cond_timedwait) that the specified 1798 // number of seconds, in abstime, is less than current_time + 100000000. 1799 // As it will be over 20 years before "now + 100000000" will overflow we can 1800 // ignore overflow and just impose a hard-limit on seconds using the value 1801 // of "now + 100000000". This places a limit on the timeout of about 3.17 1802 // years from "now". 1803 // 1804 #define MAX_SECS 100000000 1805 1806 // Calculate a new absolute time that is "timeout" nanoseconds from "now". 1807 // "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending 1808 // on which clock is being used). 1809 static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec, 1810 jlong now_part_sec, jlong unit) { 1811 time_t max_secs = now_sec + MAX_SECS; 1812 1813 jlong seconds = timeout / NANOUNITS; 1814 timeout %= NANOUNITS; // remaining nanos 1815 1816 if (seconds >= MAX_SECS) { 1817 // More seconds than we can add, so pin to max_secs. 1818 abstime->tv_sec = max_secs; 1819 abstime->tv_nsec = 0; 1820 } else { 1821 abstime->tv_sec = now_sec + seconds; 1822 long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout; 1823 if (nanos >= NANOUNITS) { // overflow 1824 abstime->tv_sec += 1; 1825 nanos -= NANOUNITS; 1826 } 1827 abstime->tv_nsec = nanos; 1828 } 1829 } 1830 1831 // Unpack the given deadline in milliseconds since the epoch, into the given timespec. 1832 // The current time in seconds is also passed in to enforce an upper bound as discussed above. 1833 static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) { 1834 time_t max_secs = now_sec + MAX_SECS; 1835 1836 jlong seconds = deadline / MILLIUNITS; 1837 jlong millis = deadline % MILLIUNITS; 1838 1839 if (seconds >= max_secs) { 1840 // Absolute seconds exceeds allowed max, so pin to max_secs. 1841 abstime->tv_sec = max_secs; 1842 abstime->tv_nsec = 0; 1843 } else { 1844 abstime->tv_sec = seconds; 1845 abstime->tv_nsec = millis * (NANOUNITS / MILLIUNITS); 1846 } 1847 } 1848 1849 static void to_abstime(timespec* abstime, jlong timeout, bool isAbsolute) { 1850 DEBUG_ONLY(int max_secs = MAX_SECS;) 1851 1852 if (timeout < 0) { 1853 timeout = 0; 1854 } 1855 1856 #ifdef SUPPORTS_CLOCK_MONOTONIC 1857 1858 if (_use_clock_monotonic_condattr && !isAbsolute) { 1859 struct timespec now; 1860 int status = _clock_gettime(CLOCK_MONOTONIC, &now); 1861 assert_status(status == 0, status, "clock_gettime"); 1862 calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS); 1863 DEBUG_ONLY(max_secs += now.tv_sec;) 1864 } else { 1865 1866 #else 1867 1868 { // Match the block scope. 1869 1870 #endif // SUPPORTS_CLOCK_MONOTONIC 1871 1872 // Time-of-day clock is all we can reliably use. 1873 struct timeval now; 1874 int status = gettimeofday(&now, NULL); 1875 assert_status(status == 0, errno, "gettimeofday"); 1876 if (isAbsolute) { 1877 unpack_abs_time(abstime, timeout, now.tv_sec); 1878 } else { 1879 calc_rel_time(abstime, timeout, now.tv_sec, now.tv_usec, MICROUNITS); 1880 } 1881 DEBUG_ONLY(max_secs += now.tv_sec;) 1882 } 1883 1884 assert(abstime->tv_sec >= 0, "tv_sec < 0"); 1885 assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs"); 1886 assert(abstime->tv_nsec >= 0, "tv_nsec < 0"); 1887 assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS"); 1888 } 1889 1890 // PlatformEvent 1891 // 1892 // Assumption: 1893 // Only one parker can exist on an event, which is why we allocate 1894 // them per-thread. Multiple unparkers can coexist. 1895 // 1896 // _event serves as a restricted-range semaphore. 1897 // -1 : thread is blocked, i.e. there is a waiter 1898 // 0 : neutral: thread is running or ready, 1899 // could have been signaled after a wait started 1900 // 1 : signaled - thread is running or ready 1901 // 1902 // Having three states allows for some detection of bad usage - see 1903 // comments on unpark(). 1904 1905 void os::PlatformEvent::park() { // AKA "down()" 1906 // Transitions for _event: 1907 // -1 => -1 : illegal 1908 // 1 => 0 : pass - return immediately 1909 // 0 => -1 : block; then set _event to 0 before returning 1910 1911 // Invariant: Only the thread associated with the PlatformEvent 1912 // may call park(). 1913 assert(_nParked == 0, "invariant"); 1914 1915 int v; 1916 1917 // atomically decrement _event 1918 for (;;) { 1919 v = _event; 1920 if (Atomic::cmpxchg(v - 1, &_event, v) == v) break; 1921 } 1922 guarantee(v >= 0, "invariant"); 1923 1924 if (v == 0) { // Do this the hard way by blocking ... 1925 int status = pthread_mutex_lock(_mutex); 1926 assert_status(status == 0, status, "mutex_lock"); 1927 guarantee(_nParked == 0, "invariant"); 1928 ++_nParked; 1929 while (_event < 0) { 1930 // OS-level "spurious wakeups" are ignored 1931 status = pthread_cond_wait(_cond, _mutex); 1932 assert_status(status == 0, status, "cond_wait"); 1933 } 1934 --_nParked; 1935 1936 _event = 0; 1937 status = pthread_mutex_unlock(_mutex); 1938 assert_status(status == 0, status, "mutex_unlock"); 1939 // Paranoia to ensure our locked and lock-free paths interact 1940 // correctly with each other. 1941 OrderAccess::fence(); 1942 } 1943 guarantee(_event >= 0, "invariant"); 1944 } 1945 1946 int os::PlatformEvent::park(jlong millis) { 1947 // Transitions for _event: 1948 // -1 => -1 : illegal 1949 // 1 => 0 : pass - return immediately 1950 // 0 => -1 : block; then set _event to 0 before returning 1951 1952 // Invariant: Only the thread associated with the Event/PlatformEvent 1953 // may call park(). 1954 assert(_nParked == 0, "invariant"); 1955 1956 int v; 1957 // atomically decrement _event 1958 for (;;) { 1959 v = _event; 1960 if (Atomic::cmpxchg(v - 1, &_event, v) == v) break; 1961 } 1962 guarantee(v >= 0, "invariant"); 1963 1964 if (v == 0) { // Do this the hard way by blocking ... 1965 struct timespec abst; 1966 // We have to watch for overflow when converting millis to nanos, 1967 // but if millis is that large then we will end up limiting to 1968 // MAX_SECS anyway, so just do that here. 1969 if (millis / MILLIUNITS > MAX_SECS) { 1970 millis = jlong(MAX_SECS) * MILLIUNITS; 1971 } 1972 to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false); 1973 1974 int ret = OS_TIMEOUT; 1975 int status = pthread_mutex_lock(_mutex); 1976 assert_status(status == 0, status, "mutex_lock"); 1977 guarantee(_nParked == 0, "invariant"); 1978 ++_nParked; 1979 1980 while (_event < 0) { 1981 status = pthread_cond_timedwait(_cond, _mutex, &abst); 1982 assert_status(status == 0 || status == ETIMEDOUT, 1983 status, "cond_timedwait"); 1984 // OS-level "spurious wakeups" are ignored unless the archaic 1985 // FilterSpuriousWakeups is set false. That flag should be obsoleted. 1986 if (!FilterSpuriousWakeups) break; 1987 if (status == ETIMEDOUT) break; 1988 } 1989 --_nParked; 1990 1991 if (_event >= 0) { 1992 ret = OS_OK; 1993 } 1994 1995 _event = 0; 1996 status = pthread_mutex_unlock(_mutex); 1997 assert_status(status == 0, status, "mutex_unlock"); 1998 // Paranoia to ensure our locked and lock-free paths interact 1999 // correctly with each other. 2000 OrderAccess::fence(); 2001 return ret; 2002 } 2003 return OS_OK; 2004 } 2005 2006 void os::PlatformEvent::unpark() { 2007 // Transitions for _event: 2008 // 0 => 1 : just return 2009 // 1 => 1 : just return 2010 // -1 => either 0 or 1; must signal target thread 2011 // That is, we can safely transition _event from -1 to either 2012 // 0 or 1. 2013 // See also: "Semaphores in Plan 9" by Mullender & Cox 2014 // 2015 // Note: Forcing a transition from "-1" to "1" on an unpark() means 2016 // that it will take two back-to-back park() calls for the owning 2017 // thread to block. This has the benefit of forcing a spurious return 2018 // from the first park() call after an unpark() call which will help 2019 // shake out uses of park() and unpark() without checking state conditions 2020 // properly. This spurious return doesn't manifest itself in any user code 2021 // but only in the correctly written condition checking loops of ObjectMonitor, 2022 // Mutex/Monitor, Thread::muxAcquire and os::sleep 2023 2024 if (Atomic::xchg(1, &_event) >= 0) return; 2025 2026 int status = pthread_mutex_lock(_mutex); 2027 assert_status(status == 0, status, "mutex_lock"); 2028 int anyWaiters = _nParked; 2029 assert(anyWaiters == 0 || anyWaiters == 1, "invariant"); 2030 status = pthread_mutex_unlock(_mutex); 2031 assert_status(status == 0, status, "mutex_unlock"); 2032 2033 // Note that we signal() *after* dropping the lock for "immortal" Events. 2034 // This is safe and avoids a common class of futile wakeups. In rare 2035 // circumstances this can cause a thread to return prematurely from 2036 // cond_{timed}wait() but the spurious wakeup is benign and the victim 2037 // will simply re-test the condition and re-park itself. 2038 // This provides particular benefit if the underlying platform does not 2039 // provide wait morphing. 2040 2041 if (anyWaiters != 0) { 2042 status = pthread_cond_signal(_cond); 2043 assert_status(status == 0, status, "cond_signal"); 2044 } 2045 } 2046 2047 // JSR166 support 2048 2049 os::PlatformParker::PlatformParker() { 2050 int status; 2051 status = pthread_cond_init(&_cond[REL_INDEX], _condAttr); 2052 assert_status(status == 0, status, "cond_init rel"); 2053 status = pthread_cond_init(&_cond[ABS_INDEX], NULL); 2054 assert_status(status == 0, status, "cond_init abs"); 2055 status = pthread_mutex_init(_mutex, _mutexAttr); 2056 assert_status(status == 0, status, "mutex_init"); 2057 _cur_index = -1; // mark as unused 2058 } 2059 2060 // Parker::park decrements count if > 0, else does a condvar wait. Unpark 2061 // sets count to 1 and signals condvar. Only one thread ever waits 2062 // on the condvar. Contention seen when trying to park implies that someone 2063 // is unparking you, so don't wait. And spurious returns are fine, so there 2064 // is no need to track notifications. 2065 2066 void Parker::park(bool isAbsolute, jlong time) { 2067 2068 // Optional fast-path check: 2069 // Return immediately if a permit is available. 2070 // We depend on Atomic::xchg() having full barrier semantics 2071 // since we are doing a lock-free update to _counter. 2072 if (Atomic::xchg(0, &_counter) > 0) return; 2073 2074 Thread* thread = Thread::current(); 2075 assert(thread->is_Java_thread(), "Must be JavaThread"); 2076 JavaThread *jt = (JavaThread *)thread; 2077 2078 // Optional optimization -- avoid state transitions if there's 2079 // an interrupt pending. 2080 if (Thread::is_interrupted(thread, false)) { 2081 return; 2082 } 2083 2084 // Next, demultiplex/decode time arguments 2085 struct timespec absTime; 2086 if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all 2087 return; 2088 } 2089 if (time > 0) { 2090 to_abstime(&absTime, time, isAbsolute); 2091 } 2092 2093 // Enter safepoint region 2094 // Beware of deadlocks such as 6317397. 2095 // The per-thread Parker:: mutex is a classic leaf-lock. 2096 // In particular a thread must never block on the Threads_lock while 2097 // holding the Parker:: mutex. If safepoints are pending both the 2098 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 2099 ThreadBlockInVM tbivm(jt); 2100 2101 // Don't wait if cannot get lock since interference arises from 2102 // unparking. Also re-check interrupt before trying wait. 2103 if (Thread::is_interrupted(thread, false) || 2104 pthread_mutex_trylock(_mutex) != 0) { 2105 return; 2106 } 2107 2108 int status; 2109 if (_counter > 0) { // no wait needed 2110 _counter = 0; 2111 status = pthread_mutex_unlock(_mutex); 2112 assert_status(status == 0, status, "invariant"); 2113 // Paranoia to ensure our locked and lock-free paths interact 2114 // correctly with each other and Java-level accesses. 2115 OrderAccess::fence(); 2116 return; 2117 } 2118 2119 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 2120 jt->set_suspend_equivalent(); 2121 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 2122 2123 assert(_cur_index == -1, "invariant"); 2124 if (time == 0) { 2125 _cur_index = REL_INDEX; // arbitrary choice when not timed 2126 status = pthread_cond_wait(&_cond[_cur_index], _mutex); 2127 assert_status(status == 0, status, "cond_timedwait"); 2128 } 2129 else { 2130 _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX; 2131 status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime); 2132 assert_status(status == 0 || status == ETIMEDOUT, 2133 status, "cond_timedwait"); 2134 } 2135 _cur_index = -1; 2136 2137 _counter = 0; 2138 status = pthread_mutex_unlock(_mutex); 2139 assert_status(status == 0, status, "invariant"); 2140 // Paranoia to ensure our locked and lock-free paths interact 2141 // correctly with each other and Java-level accesses. 2142 OrderAccess::fence(); 2143 2144 // If externally suspended while waiting, re-suspend 2145 if (jt->handle_special_suspend_equivalent_condition()) { 2146 jt->java_suspend_self(); 2147 } 2148 } 2149 2150 void Parker::unpark() { 2151 int status = pthread_mutex_lock(_mutex); 2152 assert_status(status == 0, status, "invariant"); 2153 const int s = _counter; 2154 _counter = 1; 2155 // must capture correct index before unlocking 2156 int index = _cur_index; 2157 status = pthread_mutex_unlock(_mutex); 2158 assert_status(status == 0, status, "invariant"); 2159 2160 // Note that we signal() *after* dropping the lock for "immortal" Events. 2161 // This is safe and avoids a common class of futile wakeups. In rare 2162 // circumstances this can cause a thread to return prematurely from 2163 // cond_{timed}wait() but the spurious wakeup is benign and the victim 2164 // will simply re-test the condition and re-park itself. 2165 // This provides particular benefit if the underlying platform does not 2166 // provide wait morphing. 2167 2168 if (s < 1 && index != -1) { 2169 // thread is definitely parked 2170 status = pthread_cond_signal(&_cond[index]); 2171 assert_status(status == 0, status, "invariant"); 2172 } 2173 } 2174 2175 2176 #endif // !SOLARIS