Mercurial > hg > graal-compiler
comparison src/os/linux/vm/os_linux.cpp @ 8883:b9a918201d47
Merge with hsx25
author | Gilles Duboscq <duboscq@ssw.jku.at> |
---|---|
date | Sat, 06 Apr 2013 20:04:06 +0200 |
parents | b8f261ba79c6 15c04fe93c18 |
children | 89e4d67fdd2a |
comparison
equal
deleted
inserted
replaced
8660:d47b52b0ff68 | 8883:b9a918201d47 |
---|---|
42 #include "prims/jvm_misc.hpp" | 42 #include "prims/jvm_misc.hpp" |
43 #include "runtime/arguments.hpp" | 43 #include "runtime/arguments.hpp" |
44 #include "runtime/extendedPC.hpp" | 44 #include "runtime/extendedPC.hpp" |
45 #include "runtime/globals.hpp" | 45 #include "runtime/globals.hpp" |
46 #include "runtime/interfaceSupport.hpp" | 46 #include "runtime/interfaceSupport.hpp" |
47 #include "runtime/init.hpp" | |
47 #include "runtime/java.hpp" | 48 #include "runtime/java.hpp" |
48 #include "runtime/javaCalls.hpp" | 49 #include "runtime/javaCalls.hpp" |
49 #include "runtime/mutexLocker.hpp" | 50 #include "runtime/mutexLocker.hpp" |
50 #include "runtime/objectMonitor.hpp" | 51 #include "runtime/objectMonitor.hpp" |
51 #include "runtime/osThread.hpp" | 52 #include "runtime/osThread.hpp" |
55 #include "runtime/stubRoutines.hpp" | 56 #include "runtime/stubRoutines.hpp" |
56 #include "runtime/thread.inline.hpp" | 57 #include "runtime/thread.inline.hpp" |
57 #include "runtime/threadCritical.hpp" | 58 #include "runtime/threadCritical.hpp" |
58 #include "runtime/timer.hpp" | 59 #include "runtime/timer.hpp" |
59 #include "services/attachListener.hpp" | 60 #include "services/attachListener.hpp" |
61 #include "services/memTracker.hpp" | |
60 #include "services/runtimeService.hpp" | 62 #include "services/runtimeService.hpp" |
61 #include "utilities/decoder.hpp" | 63 #include "utilities/decoder.hpp" |
62 #include "utilities/defaultStream.hpp" | 64 #include "utilities/defaultStream.hpp" |
63 #include "utilities/events.hpp" | 65 #include "utilities/events.hpp" |
66 #include "utilities/elfFile.hpp" | |
64 #include "utilities/growableArray.hpp" | 67 #include "utilities/growableArray.hpp" |
65 #include "utilities/vmError.hpp" | 68 #include "utilities/vmError.hpp" |
66 | 69 |
67 // put OS-includes here | 70 // put OS-includes here |
68 # include <sys/types.h> | 71 # include <sys/types.h> |
187 return (julong)si.freeram * si.mem_unit; | 190 return (julong)si.freeram * si.mem_unit; |
188 } | 191 } |
189 | 192 |
190 julong os::physical_memory() { | 193 julong os::physical_memory() { |
191 return Linux::physical_memory(); | 194 return Linux::physical_memory(); |
192 } | |
193 | |
194 julong os::allocatable_physical_memory(julong size) { | |
195 #ifdef _LP64 | |
196 return size; | |
197 #else | |
198 julong result = MIN2(size, (julong)3800*M); | |
199 if (!is_allocatable(result)) { | |
200 // See comments under solaris for alignment considerations | |
201 julong reasonable_size = (julong)2*G - 2 * os::vm_page_size(); | |
202 result = MIN2(size, reasonable_size); | |
203 } | |
204 return result; | |
205 #endif // _LP64 | |
206 } | 195 } |
207 | 196 |
208 //////////////////////////////////////////////////////////////////////////////// | 197 //////////////////////////////////////////////////////////////////////////////// |
209 // environment support | 198 // environment support |
210 | 199 |
1794 | 1783 |
1795 // Loads .dll/.so and | 1784 // Loads .dll/.so and |
1796 // in case of error it checks if .dll/.so was built for the | 1785 // in case of error it checks if .dll/.so was built for the |
1797 // same architecture as Hotspot is running on | 1786 // same architecture as Hotspot is running on |
1798 | 1787 |
1788 | |
1789 // Remember the stack's state. The Linux dynamic linker will change | |
1790 // the stack to 'executable' at most once, so we must safepoint only once. | |
1791 bool os::Linux::_stack_is_executable = false; | |
1792 | |
1793 // VM operation that loads a library. This is necessary if stack protection | |
1794 // of the Java stacks can be lost during loading the library. If we | |
1795 // do not stop the Java threads, they can stack overflow before the stacks | |
1796 // are protected again. | |
1797 class VM_LinuxDllLoad: public VM_Operation { | |
1798 private: | |
1799 const char *_filename; | |
1800 char *_ebuf; | |
1801 int _ebuflen; | |
1802 void *_lib; | |
1803 public: | |
1804 VM_LinuxDllLoad(const char *fn, char *ebuf, int ebuflen) : | |
1805 _filename(fn), _ebuf(ebuf), _ebuflen(ebuflen), _lib(NULL) {} | |
1806 VMOp_Type type() const { return VMOp_LinuxDllLoad; } | |
1807 void doit() { | |
1808 _lib = os::Linux::dll_load_in_vmthread(_filename, _ebuf, _ebuflen); | |
1809 os::Linux::_stack_is_executable = true; | |
1810 } | |
1811 void* loaded_library() { return _lib; } | |
1812 }; | |
1813 | |
1799 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) | 1814 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) |
1800 { | 1815 { |
1801 void * result= ::dlopen(filename, RTLD_LAZY); | 1816 void * result = NULL; |
1817 bool load_attempted = false; | |
1818 | |
1819 // Check whether the library to load might change execution rights | |
1820 // of the stack. If they are changed, the protection of the stack | |
1821 // guard pages will be lost. We need a safepoint to fix this. | |
1822 // | |
1823 // See Linux man page execstack(8) for more info. | |
1824 if (os::uses_stack_guard_pages() && !os::Linux::_stack_is_executable) { | |
1825 ElfFile ef(filename); | |
1826 if (!ef.specifies_noexecstack()) { | |
1827 if (!is_init_completed()) { | |
1828 os::Linux::_stack_is_executable = true; | |
1829 // This is OK - No Java threads have been created yet, and hence no | |
1830 // stack guard pages to fix. | |
1831 // | |
1832 // This should happen only when you are building JDK7 using a very | |
1833 // old version of JDK6 (e.g., with JPRT) and running test_gamma. | |
1834 // | |
1835 // Dynamic loader will make all stacks executable after | |
1836 // this function returns, and will not do that again. | |
1837 assert(Threads::first() == NULL, "no Java threads should exist yet."); | |
1838 } else { | |
1839 warning("You have loaded library %s which might have disabled stack guard. " | |
1840 "The VM will try to fix the stack guard now.\n" | |
1841 "It's highly recommended that you fix the library with " | |
1842 "'execstack -c <libfile>', or link it with '-z noexecstack'.", | |
1843 filename); | |
1844 | |
1845 assert(Thread::current()->is_Java_thread(), "must be Java thread"); | |
1846 JavaThread *jt = JavaThread::current(); | |
1847 if (jt->thread_state() != _thread_in_native) { | |
1848 // This happens when a compiler thread tries to load a hsdis-<arch>.so file | |
1849 // that requires ExecStack. Cannot enter safe point. Let's give up. | |
1850 warning("Unable to fix stack guard. Giving up."); | |
1851 } else { | |
1852 if (!LoadExecStackDllInVMThread) { | |
1853 // This is for the case where the DLL has an static | |
1854 // constructor function that executes JNI code. We cannot | |
1855 // load such DLLs in the VMThread. | |
1856 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen); | |
1857 } | |
1858 | |
1859 ThreadInVMfromNative tiv(jt); | |
1860 debug_only(VMNativeEntryWrapper vew;) | |
1861 | |
1862 VM_LinuxDllLoad op(filename, ebuf, ebuflen); | |
1863 VMThread::execute(&op); | |
1864 if (LoadExecStackDllInVMThread) { | |
1865 result = op.loaded_library(); | |
1866 } | |
1867 load_attempted = true; | |
1868 } | |
1869 } | |
1870 } | |
1871 } | |
1872 | |
1873 if (!load_attempted) { | |
1874 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen); | |
1875 } | |
1876 | |
1802 if (result != NULL) { | 1877 if (result != NULL) { |
1803 // Successful loading | 1878 // Successful loading |
1804 return result; | 1879 return result; |
1805 } | 1880 } |
1806 | 1881 |
1807 Elf32_Ehdr elf_head; | 1882 Elf32_Ehdr elf_head; |
1808 | |
1809 // Read system error message into ebuf | |
1810 // It may or may not be overwritten below | |
1811 ::strncpy(ebuf, ::dlerror(), ebuflen-1); | |
1812 ebuf[ebuflen-1]='\0'; | |
1813 int diag_msg_max_length=ebuflen-strlen(ebuf); | 1883 int diag_msg_max_length=ebuflen-strlen(ebuf); |
1814 char* diag_msg_buf=ebuf+strlen(ebuf); | 1884 char* diag_msg_buf=ebuf+strlen(ebuf); |
1815 | 1885 |
1816 if (diag_msg_max_length==0) { | 1886 if (diag_msg_max_length==0) { |
1817 // No more space in ebuf for additional diagnostics message | 1887 // No more space in ebuf for additional diagnostics message |
1950 } | 2020 } |
1951 | 2021 |
1952 return NULL; | 2022 return NULL; |
1953 } | 2023 } |
1954 | 2024 |
2025 void * os::Linux::dlopen_helper(const char *filename, char *ebuf, int ebuflen) { | |
2026 void * result = ::dlopen(filename, RTLD_LAZY); | |
2027 if (result == NULL) { | |
2028 ::strncpy(ebuf, ::dlerror(), ebuflen - 1); | |
2029 ebuf[ebuflen-1] = '\0'; | |
2030 } | |
2031 return result; | |
2032 } | |
2033 | |
2034 void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf, int ebuflen) { | |
2035 void * result = NULL; | |
2036 if (LoadExecStackDllInVMThread) { | |
2037 result = dlopen_helper(filename, ebuf, ebuflen); | |
2038 } | |
2039 | |
2040 // Since 7019808, libjvm.so is linked with -noexecstack. If the VM loads a | |
2041 // library that requires an executable stack, or which does not have this | |
2042 // stack attribute set, dlopen changes the stack attribute to executable. The | |
2043 // read protection of the guard pages gets lost. | |
2044 // | |
2045 // Need to check _stack_is_executable again as multiple VM_LinuxDllLoad | |
2046 // may have been queued at the same time. | |
2047 | |
2048 if (!_stack_is_executable) { | |
2049 JavaThread *jt = Threads::first(); | |
2050 | |
2051 while (jt) { | |
2052 if (!jt->stack_guard_zone_unused() && // Stack not yet fully initialized | |
2053 jt->stack_yellow_zone_enabled()) { // No pending stack overflow exceptions | |
2054 if (!os::guard_memory((char *) jt->stack_red_zone_base() - jt->stack_red_zone_size(), | |
2055 jt->stack_yellow_zone_size() + jt->stack_red_zone_size())) { | |
2056 warning("Attempt to reguard stack yellow zone failed."); | |
2057 } | |
2058 } | |
2059 jt = jt->next(); | |
2060 } | |
2061 } | |
2062 | |
2063 return result; | |
2064 } | |
2065 | |
1955 /* | 2066 /* |
1956 * glibc-2.0 libdl is not MT safe. If you are building with any glibc, | 2067 * glibc-2.0 libdl is not MT safe. If you are building with any glibc, |
1957 * chances are you might want to run the generated bits against glibc-2.0 | 2068 * chances are you might want to run the generated bits against glibc-2.0 |
1958 * libdl.so, so always use locking for any version of glibc. | 2069 * libdl.so, so always use locking for any version of glibc. |
1959 */ | 2070 */ |
3092 | 3203 |
3093 if ((addr != NULL) && UseNUMAInterleaving) { | 3204 if ((addr != NULL) && UseNUMAInterleaving) { |
3094 numa_make_global(addr, bytes); | 3205 numa_make_global(addr, bytes); |
3095 } | 3206 } |
3096 | 3207 |
3208 // The memory is committed | |
3209 address pc = CALLER_PC; | |
3210 MemTracker::record_virtual_memory_reserve((address)addr, bytes, pc); | |
3211 MemTracker::record_virtual_memory_commit((address)addr, bytes, pc); | |
3212 | |
3097 return addr; | 3213 return addr; |
3098 } | 3214 } |
3099 | 3215 |
3100 bool os::release_memory_special(char* base, size_t bytes) { | 3216 bool os::release_memory_special(char* base, size_t bytes) { |
3101 // detaching the SHM segment will also delete it, see reserve_memory_special() | 3217 // detaching the SHM segment will also delete it, see reserve_memory_special() |
3102 int rslt = shmdt(base); | 3218 int rslt = shmdt(base); |
3103 return rslt == 0; | 3219 if (rslt == 0) { |
3220 MemTracker::record_virtual_memory_uncommit((address)base, bytes); | |
3221 MemTracker::record_virtual_memory_release((address)base, bytes); | |
3222 return true; | |
3223 } else { | |
3224 return false; | |
3225 } | |
3104 } | 3226 } |
3105 | 3227 |
3106 size_t os::large_page_size() { | 3228 size_t os::large_page_size() { |
3107 return _large_page_size; | 3229 return _large_page_size; |
3108 } | 3230 } |
3459 Thread* thread = Thread::current(); | 3581 Thread* thread = Thread::current(); |
3460 OSThread* osthread = thread->osthread(); | 3582 OSThread* osthread = thread->osthread(); |
3461 assert(thread->is_VM_thread(), "Must be VMThread"); | 3583 assert(thread->is_VM_thread(), "Must be VMThread"); |
3462 // read current suspend action | 3584 // read current suspend action |
3463 int action = osthread->sr.suspend_action(); | 3585 int action = osthread->sr.suspend_action(); |
3464 if (action == SR_SUSPEND) { | 3586 if (action == os::Linux::SuspendResume::SR_SUSPEND) { |
3465 suspend_save_context(osthread, siginfo, context); | 3587 suspend_save_context(osthread, siginfo, context); |
3466 | 3588 |
3467 // Notify the suspend action is about to be completed. do_suspend() | 3589 // Notify the suspend action is about to be completed. do_suspend() |
3468 // waits until SR_SUSPENDED is set and then returns. We will wait | 3590 // waits until SR_SUSPENDED is set and then returns. We will wait |
3469 // here for a resume signal and that completes the suspend-other | 3591 // here for a resume signal and that completes the suspend-other |
3481 | 3603 |
3482 // wait here until we are resumed | 3604 // wait here until we are resumed |
3483 do { | 3605 do { |
3484 sigsuspend(&suspend_set); | 3606 sigsuspend(&suspend_set); |
3485 // ignore all returns until we get a resume signal | 3607 // ignore all returns until we get a resume signal |
3486 } while (osthread->sr.suspend_action() != SR_CONTINUE); | 3608 } while (osthread->sr.suspend_action() != os::Linux::SuspendResume::SR_CONTINUE); |
3487 | 3609 |
3488 resume_clear_context(osthread); | 3610 resume_clear_context(osthread); |
3489 | 3611 |
3490 } else { | 3612 } else { |
3491 assert(action == SR_CONTINUE, "unexpected sr action"); | 3613 assert(action == os::Linux::SuspendResume::SR_CONTINUE, "unexpected sr action"); |
3492 // nothing special to do - just leave the handler | 3614 // nothing special to do - just leave the handler |
3493 } | 3615 } |
3494 | 3616 |
3495 errno = old_errno; | 3617 errno = old_errno; |
3496 } | 3618 } |
3540 | 3662 |
3541 // returns true on success and false on error - really an error is fatal | 3663 // returns true on success and false on error - really an error is fatal |
3542 // but this seems the normal response to library errors | 3664 // but this seems the normal response to library errors |
3543 static bool do_suspend(OSThread* osthread) { | 3665 static bool do_suspend(OSThread* osthread) { |
3544 // mark as suspended and send signal | 3666 // mark as suspended and send signal |
3545 osthread->sr.set_suspend_action(SR_SUSPEND); | 3667 osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_SUSPEND); |
3546 int status = pthread_kill(osthread->pthread_id(), SR_signum); | 3668 int status = pthread_kill(osthread->pthread_id(), SR_signum); |
3547 assert_status(status == 0, status, "pthread_kill"); | 3669 assert_status(status == 0, status, "pthread_kill"); |
3548 | 3670 |
3549 // check status and wait until notified of suspension | 3671 // check status and wait until notified of suspension |
3550 if (status == 0) { | 3672 if (status == 0) { |
3551 for (int i = 0; !osthread->sr.is_suspended(); i++) { | 3673 for (int i = 0; !osthread->sr.is_suspended(); i++) { |
3552 os::yield_all(i); | 3674 os::yield_all(i); |
3553 } | 3675 } |
3554 osthread->sr.set_suspend_action(SR_NONE); | 3676 osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_NONE); |
3555 return true; | 3677 return true; |
3556 } | 3678 } |
3557 else { | 3679 else { |
3558 osthread->sr.set_suspend_action(SR_NONE); | 3680 osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_NONE); |
3559 return false; | 3681 return false; |
3560 } | 3682 } |
3561 } | 3683 } |
3562 | 3684 |
3563 static void do_resume(OSThread* osthread) { | 3685 static void do_resume(OSThread* osthread) { |
3564 assert(osthread->sr.is_suspended(), "thread should be suspended"); | 3686 assert(osthread->sr.is_suspended(), "thread should be suspended"); |
3565 osthread->sr.set_suspend_action(SR_CONTINUE); | 3687 osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_CONTINUE); |
3566 | 3688 |
3567 int status = pthread_kill(osthread->pthread_id(), SR_signum); | 3689 int status = pthread_kill(osthread->pthread_id(), SR_signum); |
3568 assert_status(status == 0, status, "pthread_kill"); | 3690 assert_status(status == 0, status, "pthread_kill"); |
3569 // check status and wait unit notified of resumption | 3691 // check status and wait unit notified of resumption |
3570 if (status == 0) { | 3692 if (status == 0) { |
3571 for (int i = 0; osthread->sr.is_suspended(); i++) { | 3693 for (int i = 0; osthread->sr.is_suspended(); i++) { |
3572 os::yield_all(i); | 3694 os::yield_all(i); |
3573 } | 3695 } |
3574 } | 3696 } |
3575 osthread->sr.set_suspend_action(SR_NONE); | 3697 osthread->sr.set_suspend_action(os::Linux::SuspendResume::SR_NONE); |
3576 } | 3698 } |
3577 | 3699 |
3578 //////////////////////////////////////////////////////////////////////////////// | 3700 //////////////////////////////////////////////////////////////////////////////// |
3579 // interrupt support | 3701 // interrupt support |
3580 | 3702 |