< prev index next >

src/hotspot/os/solaris/os_solaris.cpp

Print this page




1317 // called from signal handler. Before adding something to os::abort(), make
1318 // sure it is async-safe and can handle partially initialized VM.
1319 void os::abort(bool dump_core, void* siginfo, const void* context) {
1320   os::shutdown();
1321   if (dump_core) {
1322 #ifndef PRODUCT
1323     fdStream out(defaultStream::output_fd());
1324     out.print_raw("Current thread is ");
1325     char buf[16];
1326     jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
1327     out.print_raw_cr(buf);
1328     out.print_raw_cr("Dumping core ...");
1329 #endif
1330     ::abort(); // dump core (for debugging)
1331   }
1332 
1333   ::exit(1);
1334 }
1335 
1336 // Die immediately, no exit hook, no abort hook, no cleanup.
1337 // Dump a core file, if possible, for debugging.
1338 void os::die() {
1339   if (TestUnresponsiveErrorHandler && !CreateCoredumpOnCrash) {
1340     // For TimeoutInErrorHandlingTest.java, we just kill the VM
1341     // and don't take the time to generate a core file.
1342     os::signal_raise(SIGKILL);
1343   } else {
1344     ::abort();
1345   }
1346 }
1347 
1348 // DLL functions
1349 
1350 const char* os::dll_file_extension() { return ".so"; }
1351 
1352 // This must be hard coded because it's the system's temporary
1353 // directory not the java application's temp directory, ala java.io.tmpdir.
1354 const char* os::get_temp_directory() { return "/tmp"; }
1355 
1356 // check if addr is inside libjvm.so
1357 bool os::address_is_in_vm(address addr) {
1358   static address libjvm_base_addr;
1359   Dl_info dlinfo;
1360 
1361   if (libjvm_base_addr == NULL) {
1362     if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {
1363       libjvm_base_addr = (address)dlinfo.dli_fbase;
1364     }
1365     assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");


1511   outputStream * out = (outputStream *) param;
1512   out->print_cr(PTR_FORMAT " \t%s", base_address, name);
1513   return 0;
1514 }
1515 
1516 void os::print_dll_info(outputStream * st) {
1517   st->print_cr("Dynamic libraries:"); st->flush();
1518   if (get_loaded_modules_info(_print_dll_info_cb, (void *)st)) {
1519     st->print_cr("Error: Cannot print dynamic libraries.");
1520   }
1521 }
1522 
1523 // Loads .dll/.so and
1524 // in case of error it checks if .dll/.so was built for the
1525 // same architecture as Hotspot is running on
1526 
1527 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
1528   void * result= ::dlopen(filename, RTLD_LAZY);
1529   if (result != NULL) {
1530     // Successful loading
1531     Events::log(NULL, "Loaded shared library %s", filename);
1532     return result;
1533   }
1534 
1535   Elf32_Ehdr elf_head;
1536   const char* error_report = ::dlerror();
1537   if (error_report == NULL) {
1538     error_report = "dlerror returned no error description";
1539   }
1540   if (ebuf != NULL && ebuflen > 0) {
1541     ::strncpy(ebuf, error_report, ebuflen-1);
1542     ebuf[ebuflen-1]='\0';
1543   }
1544 
1545   Events::log(NULL, "Loading shared library %s failed, %s", filename, error_report);
1546 




1547   int diag_msg_max_length=ebuflen-strlen(ebuf);
1548   char* diag_msg_buf=ebuf+strlen(ebuf);
1549 
1550   if (diag_msg_max_length==0) {
1551     // No more space in ebuf for additional diagnostics message
1552     return NULL;
1553   }
1554 
1555 
1556   int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
1557 
1558   if (file_descriptor < 0) {
1559     // Can't open library, report dlerror() message
1560     return NULL;
1561   }
1562 
1563   bool failed_to_read_elf_head=
1564     (sizeof(elf_head)!=
1565      (::read(file_descriptor, &elf_head,sizeof(elf_head))));
1566 


2024     }
2025 
2026     os::signal_notify(sig);
2027     // We do not need to reinstate the signal handler each time...
2028   }
2029 }
2030 
2031 void* os::user_handler() {
2032   return CAST_FROM_FN_PTR(void*, UserHandler);
2033 }
2034 
2035 extern "C" {
2036   typedef void (*sa_handler_t)(int);
2037   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2038 }
2039 
2040 void* os::signal(int signal_number, void* handler) {
2041   struct sigaction sigAct, oldSigAct;
2042   sigfillset(&(sigAct.sa_mask));
2043   sigAct.sa_flags = SA_RESTART & ~SA_RESETHAND;
2044   sigAct.sa_flags |= SA_SIGINFO;
2045   sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2046 
2047   if (sigaction(signal_number, &sigAct, &oldSigAct)) {
2048     // -1 means registration failed
2049     return (void *)-1;
2050   }
2051 
2052   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2053 }
2054 
2055 void os::signal_raise(int signal_number) {
2056   raise(signal_number);
2057 }
2058 
2059 // The following code is moved from os.cpp for making this
2060 // code platform specific, which it is by its very nature.
2061 
2062 // a counter for each possible signal value
2063 static int Sigexit = 0;
2064 static jint *pending_signals = NULL;


2536   guarantee(requested_addr == NULL || requested_addr == addr,
2537             "OS failed to return requested mmap address.");
2538   return addr;
2539 }
2540 
2541 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr, int file_desc) {
2542   assert(file_desc >= 0, "file_desc is not valid");
2543   char* result = pd_attempt_reserve_memory_at(bytes, requested_addr);
2544   if (result != NULL) {
2545     if (replace_existing_mapping_with_file_mapping(result, bytes, file_desc) == NULL) {
2546       vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
2547     }
2548   }
2549   return result;
2550 }
2551 
2552 // Reserve memory at an arbitrary address, only if that area is
2553 // available (and not reserved for something else).
2554 
2555 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {











2556   // Assert only that the size is a multiple of the page size, since
2557   // that's all that mmap requires, and since that's all we really know
2558   // about at this low abstraction level.  If we need higher alignment,
2559   // we can either pass an alignment to this method or verify alignment
2560   // in one of the methods further up the call chain.  See bug 5044738.
2561   assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");
2562 
2563   // Since snv_84, Solaris attempts to honor the address hint - see 5003415.

2564   char* addr = Solaris::anon_mmap(requested_addr, bytes, 0, false);
2565 
2566   volatile int err = errno;
2567   if (addr == requested_addr) {
2568     return addr;


2569   }
2570 
2571   if (addr != NULL) {
2572     pd_unmap_memory(addr, bytes);












































































2573   }
2574 
2575   return NULL;








2576 }
2577 
2578 bool os::pd_release_memory(char* addr, size_t bytes) {
2579   size_t size = bytes;
2580   return munmap(addr, size) == 0;
2581 }
2582 
2583 static bool solaris_mprotect(char* addr, size_t bytes, int prot) {
2584   assert(addr == (char*)align_down((uintptr_t)addr, os::vm_page_size()),
2585          "addr must be page aligned");
2586   Events::log(NULL, "Protecting memory [" INTPTR_FORMAT "," INTPTR_FORMAT "] with protection modes %x", p2i(addr), p2i(addr+bytes), prot);
2587   int retVal = mprotect(addr, bytes, prot);
2588   return retVal == 0;
2589 }
2590 
2591 // Protect memory (Used to pass readonly pages through
2592 // JNI GetArray<type>Elements with empty arrays.)
2593 // Also, used for serialization page and for compressed oops null pointer
2594 // checking.
2595 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,




1317 // called from signal handler. Before adding something to os::abort(), make
1318 // sure it is async-safe and can handle partially initialized VM.
1319 void os::abort(bool dump_core, void* siginfo, const void* context) {
1320   os::shutdown();
1321   if (dump_core) {
1322 #ifndef PRODUCT
1323     fdStream out(defaultStream::output_fd());
1324     out.print_raw("Current thread is ");
1325     char buf[16];
1326     jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
1327     out.print_raw_cr(buf);
1328     out.print_raw_cr("Dumping core ...");
1329 #endif
1330     ::abort(); // dump core (for debugging)
1331   }
1332 
1333   ::exit(1);
1334 }
1335 
1336 // Die immediately, no exit hook, no abort hook, no cleanup.

1337 void os::die() {
1338   ::abort(); // dump core (for debugging)






1339 }
1340 
1341 // DLL functions
1342 
1343 const char* os::dll_file_extension() { return ".so"; }
1344 
1345 // This must be hard coded because it's the system's temporary
1346 // directory not the java application's temp directory, ala java.io.tmpdir.
1347 const char* os::get_temp_directory() { return "/tmp"; }
1348 
1349 // check if addr is inside libjvm.so
1350 bool os::address_is_in_vm(address addr) {
1351   static address libjvm_base_addr;
1352   Dl_info dlinfo;
1353 
1354   if (libjvm_base_addr == NULL) {
1355     if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {
1356       libjvm_base_addr = (address)dlinfo.dli_fbase;
1357     }
1358     assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");


1504   outputStream * out = (outputStream *) param;
1505   out->print_cr(PTR_FORMAT " \t%s", base_address, name);
1506   return 0;
1507 }
1508 
1509 void os::print_dll_info(outputStream * st) {
1510   st->print_cr("Dynamic libraries:"); st->flush();
1511   if (get_loaded_modules_info(_print_dll_info_cb, (void *)st)) {
1512     st->print_cr("Error: Cannot print dynamic libraries.");
1513   }
1514 }
1515 
1516 // Loads .dll/.so and
1517 // in case of error it checks if .dll/.so was built for the
1518 // same architecture as Hotspot is running on
1519 
1520 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
1521   void * result= ::dlopen(filename, RTLD_LAZY);
1522   if (result != NULL) {
1523     // Successful loading

1524     return result;
1525   }
1526 
1527   Elf32_Ehdr elf_head;










1528 
1529   // Read system error message into ebuf
1530   // It may or may not be overwritten below
1531   ::strncpy(ebuf, ::dlerror(), ebuflen-1);
1532   ebuf[ebuflen-1]='\0';
1533   int diag_msg_max_length=ebuflen-strlen(ebuf);
1534   char* diag_msg_buf=ebuf+strlen(ebuf);
1535 
1536   if (diag_msg_max_length==0) {
1537     // No more space in ebuf for additional diagnostics message
1538     return NULL;
1539   }
1540 
1541 
1542   int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
1543 
1544   if (file_descriptor < 0) {
1545     // Can't open library, report dlerror() message
1546     return NULL;
1547   }
1548 
1549   bool failed_to_read_elf_head=
1550     (sizeof(elf_head)!=
1551      (::read(file_descriptor, &elf_head,sizeof(elf_head))));
1552 


2010     }
2011 
2012     os::signal_notify(sig);
2013     // We do not need to reinstate the signal handler each time...
2014   }
2015 }
2016 
2017 void* os::user_handler() {
2018   return CAST_FROM_FN_PTR(void*, UserHandler);
2019 }
2020 
2021 extern "C" {
2022   typedef void (*sa_handler_t)(int);
2023   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2024 }
2025 
2026 void* os::signal(int signal_number, void* handler) {
2027   struct sigaction sigAct, oldSigAct;
2028   sigfillset(&(sigAct.sa_mask));
2029   sigAct.sa_flags = SA_RESTART & ~SA_RESETHAND;

2030   sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2031 
2032   if (sigaction(signal_number, &sigAct, &oldSigAct)) {
2033     // -1 means registration failed
2034     return (void *)-1;
2035   }
2036 
2037   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2038 }
2039 
2040 void os::signal_raise(int signal_number) {
2041   raise(signal_number);
2042 }
2043 
2044 // The following code is moved from os.cpp for making this
2045 // code platform specific, which it is by its very nature.
2046 
2047 // a counter for each possible signal value
2048 static int Sigexit = 0;
2049 static jint *pending_signals = NULL;


2521   guarantee(requested_addr == NULL || requested_addr == addr,
2522             "OS failed to return requested mmap address.");
2523   return addr;
2524 }
2525 
2526 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr, int file_desc) {
2527   assert(file_desc >= 0, "file_desc is not valid");
2528   char* result = pd_attempt_reserve_memory_at(bytes, requested_addr);
2529   if (result != NULL) {
2530     if (replace_existing_mapping_with_file_mapping(result, bytes, file_desc) == NULL) {
2531       vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
2532     }
2533   }
2534   return result;
2535 }
2536 
2537 // Reserve memory at an arbitrary address, only if that area is
2538 // available (and not reserved for something else).
2539 
2540 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
2541   const int max_tries = 10;
2542   char* base[max_tries];
2543   size_t size[max_tries];
2544 
2545   // Solaris adds a gap between mmap'ed regions.  The size of the gap
2546   // is dependent on the requested size and the MMU.  Our initial gap
2547   // value here is just a guess and will be corrected later.
2548   bool had_top_overlap = false;
2549   bool have_adjusted_gap = false;
2550   size_t gap = 0x400000;
2551 
2552   // Assert only that the size is a multiple of the page size, since
2553   // that's all that mmap requires, and since that's all we really know
2554   // about at this low abstraction level.  If we need higher alignment,
2555   // we can either pass an alignment to this method or verify alignment
2556   // in one of the methods further up the call chain.  See bug 5044738.
2557   assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");
2558 
2559   // Since snv_84, Solaris attempts to honor the address hint - see 5003415.
2560   // Give it a try, if the kernel honors the hint we can return immediately.
2561   char* addr = Solaris::anon_mmap(requested_addr, bytes, 0, false);
2562 
2563   volatile int err = errno;
2564   if (addr == requested_addr) {
2565     return addr;
2566   } else if (addr != NULL) {
2567     pd_unmap_memory(addr, bytes);
2568   }
2569 
2570   if (log_is_enabled(Warning, os)) {
2571     char buf[256];
2572     buf[0] = '\0';
2573     if (addr == NULL) {
2574       jio_snprintf(buf, sizeof(buf), ": %s", os::strerror(err));
2575     }
2576     log_info(os)("attempt_reserve_memory_at: couldn't reserve " SIZE_FORMAT " bytes at "
2577             PTR_FORMAT ": reserve_memory_helper returned " PTR_FORMAT
2578             "%s", bytes, requested_addr, addr, buf);
2579   }
2580 
2581   // Address hint method didn't work.  Fall back to the old method.
2582   // In theory, once SNV becomes our oldest supported platform, this
2583   // code will no longer be needed.
2584   //
2585   // Repeatedly allocate blocks until the block is allocated at the
2586   // right spot. Give up after max_tries.
2587   int i;
2588   for (i = 0; i < max_tries; ++i) {
2589     base[i] = reserve_memory(bytes);
2590 
2591     if (base[i] != NULL) {
2592       // Is this the block we wanted?
2593       if (base[i] == requested_addr) {
2594         size[i] = bytes;
2595         break;
2596       }
2597 
2598       // check that the gap value is right
2599       if (had_top_overlap && !have_adjusted_gap) {
2600         size_t actual_gap = base[i-1] - base[i] - bytes;
2601         if (gap != actual_gap) {
2602           // adjust the gap value and retry the last 2 allocations
2603           assert(i > 0, "gap adjustment code problem");
2604           have_adjusted_gap = true;  // adjust the gap only once, just in case
2605           gap = actual_gap;
2606           log_info(os)("attempt_reserve_memory_at: adjusted gap to 0x%lx", gap);
2607           unmap_memory(base[i], bytes);
2608           unmap_memory(base[i-1], size[i-1]);
2609           i-=2;
2610           continue;
2611         }
2612       }
2613 
2614       // Does this overlap the block we wanted? Give back the overlapped
2615       // parts and try again.
2616       //
2617       // There is still a bug in this code: if top_overlap == bytes,
2618       // the overlap is offset from requested region by the value of gap.
2619       // In this case giving back the overlapped part will not work,
2620       // because we'll give back the entire block at base[i] and
2621       // therefore the subsequent allocation will not generate a new gap.
2622       // This could be fixed with a new algorithm that used larger
2623       // or variable size chunks to find the requested region -
2624       // but such a change would introduce additional complications.
2625       // It's rare enough that the planets align for this bug,
2626       // so we'll just wait for a fix for 6204603/5003415 which
2627       // will provide a mmap flag to allow us to avoid this business.
2628 
2629       size_t top_overlap = requested_addr + (bytes + gap) - base[i];
2630       if (top_overlap >= 0 && top_overlap < bytes) {
2631         had_top_overlap = true;
2632         unmap_memory(base[i], top_overlap);
2633         base[i] += top_overlap;
2634         size[i] = bytes - top_overlap;
2635       } else {
2636         size_t bottom_overlap = base[i] + bytes - requested_addr;
2637         if (bottom_overlap >= 0 && bottom_overlap < bytes) {
2638           if (bottom_overlap == 0) {
2639             log_info(os)("attempt_reserve_memory_at: possible alignment bug");
2640           }
2641           unmap_memory(requested_addr, bottom_overlap);
2642           size[i] = bytes - bottom_overlap;
2643         } else {
2644           size[i] = bytes;
2645         }
2646       }
2647     }
2648   }
2649 
2650   // Give back the unused reserved pieces.
2651 
2652   for (int j = 0; j < i; ++j) {
2653     if (base[j] != NULL) {
2654       unmap_memory(base[j], size[j]);
2655     }
2656   }
2657 
2658   return (i < max_tries) ? requested_addr : NULL;
2659 }
2660 
2661 bool os::pd_release_memory(char* addr, size_t bytes) {
2662   size_t size = bytes;
2663   return munmap(addr, size) == 0;
2664 }
2665 
2666 static bool solaris_mprotect(char* addr, size_t bytes, int prot) {
2667   assert(addr == (char*)align_down((uintptr_t)addr, os::vm_page_size()),
2668          "addr must be page aligned");
2669   Events::log(NULL, "Protecting memory [" INTPTR_FORMAT "," INTPTR_FORMAT "] with protection modes %x", p2i(addr), p2i(addr+bytes), prot);
2670   int retVal = mprotect(addr, bytes, prot);
2671   return retVal == 0;
2672 }
2673 
2674 // Protect memory (Used to pass readonly pages through
2675 // JNI GetArray<type>Elements with empty arrays.)
2676 // Also, used for serialization page and for compressed oops null pointer
2677 // checking.
2678 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,


< prev index next >