src/hotspot/os/linux/os_linux.cpp
changeset 48005 9fd89aabb6cd
parent 47903 7f22774a5f42
child 48105 8d15b1369c7a
equal deleted inserted replaced
48003:e1ec73e3325e 48005:9fd89aabb6cd
   851     if (lgrp_id != -1) {
   851     if (lgrp_id != -1) {
   852       thread->set_lgrp_id(lgrp_id);
   852       thread->set_lgrp_id(lgrp_id);
   853     }
   853     }
   854   }
   854   }
   855 
   855 
   856   if (os::Linux::is_initial_thread()) {
   856   if (os::is_primordial_thread()) {
   857     // If current thread is initial thread, its stack is mapped on demand,
   857     // If current thread is primordial thread, its stack is mapped on demand,
   858     // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
   858     // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
   859     // the entire stack region to avoid SEGV in stack banging.
   859     // the entire stack region to avoid SEGV in stack banging.
   860     // It is also useful to get around the heap-stack-gap problem on SuSE
   860     // It is also useful to get around the heap-stack-gap problem on SuSE
   861     // kernel (see 4821821 for details). We first expand stack to the top
   861     // kernel (see 4821821 for details). We first expand stack to the top
   862     // of yellow zone, then enable stack yellow zone (order is significant,
   862     // of yellow zone, then enable stack yellow zone (order is significant,
   913 
   913 
   914   delete osthread;
   914   delete osthread;
   915 }
   915 }
   916 
   916 
   917 //////////////////////////////////////////////////////////////////////////////
   917 //////////////////////////////////////////////////////////////////////////////
   918 // initial thread
   918 // primordial thread
   919 
   919 
   920 // Check if current thread is the initial thread, similar to Solaris thr_main.
   920 // Check if current thread is the primordial thread, similar to Solaris thr_main.
   921 bool os::Linux::is_initial_thread(void) {
   921 bool os::is_primordial_thread(void) {
   922   char dummy;
   922   char dummy;
   923   // If called before init complete, thread stack bottom will be null.
   923   // If called before init complete, thread stack bottom will be null.
   924   // Can be called if fatal error occurs before initialization.
   924   // Can be called if fatal error occurs before initialization.
   925   if (initial_thread_stack_bottom() == NULL) return false;
   925   if (os::Linux::initial_thread_stack_bottom() == NULL) return false;
   926   assert(initial_thread_stack_bottom() != NULL &&
   926   assert(os::Linux::initial_thread_stack_bottom() != NULL &&
   927          initial_thread_stack_size()   != 0,
   927          os::Linux::initial_thread_stack_size()   != 0,
   928          "os::init did not locate initial thread's stack region");
   928          "os::init did not locate primordial thread's stack region");
   929   if ((address)&dummy >= initial_thread_stack_bottom() &&
   929   if ((address)&dummy >= os::Linux::initial_thread_stack_bottom() &&
   930       (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size()) {
   930       (address)&dummy < os::Linux::initial_thread_stack_bottom() +
       
   931                         os::Linux::initial_thread_stack_size()) {
   931     return true;
   932     return true;
   932   } else {
   933   } else {
   933     return false;
   934     return false;
   934   }
   935   }
   935 }
   936 }
   956     fclose(fp);
   957     fclose(fp);
   957   }
   958   }
   958   return false;
   959   return false;
   959 }
   960 }
   960 
   961 
   961 // Locate initial thread stack. This special handling of initial thread stack
   962 // Locate primordial thread stack. This special handling of primordial thread stack
   962 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
   963 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
   963 // bogus value for the primordial process thread. While the launcher has created
   964 // bogus value for the primordial process thread. While the launcher has created
   964 // the VM in a new thread since JDK 6, we still have to allow for the use of the
   965 // the VM in a new thread since JDK 6, we still have to allow for the use of the
   965 // JNI invocation API from a primordial thread.
   966 // JNI invocation API from a primordial thread.
   966 void os::Linux::capture_initial_stack(size_t max_size) {
   967 void os::Linux::capture_initial_stack(size_t max_size) {
   980   size_t stack_size = rlim.rlim_cur;
   981   size_t stack_size = rlim.rlim_cur;
   981 
   982 
   982   // 6308388: a bug in ld.so will relocate its own .data section to the
   983   // 6308388: a bug in ld.so will relocate its own .data section to the
   983   //   lower end of primordial stack; reduce ulimit -s value a little bit
   984   //   lower end of primordial stack; reduce ulimit -s value a little bit
   984   //   so we won't install guard page on ld.so's data section.
   985   //   so we won't install guard page on ld.so's data section.
   985   stack_size -= 2 * page_size();
   986   //   But ensure we don't underflow the stack size - allow 1 page spare
       
   987   if (stack_size >= (size_t)(3 * page_size())) {
       
   988     stack_size -= 2 * page_size();
       
   989   }
   986 
   990 
   987   // Try to figure out where the stack base (top) is. This is harder.
   991   // Try to figure out where the stack base (top) is. This is harder.
   988   //
   992   //
   989   // When an application is started, glibc saves the initial stack pointer in
   993   // When an application is started, glibc saves the initial stack pointer in
   990   // a global variable "__libc_stack_end", which is then used by system
   994   // a global variable "__libc_stack_end", which is then used by system
  1101 #undef _UFM
  1105 #undef _UFM
  1102 #undef _DFM
  1106 #undef _DFM
  1103 
  1107 
  1104       if (i != 28 - 2) {
  1108       if (i != 28 - 2) {
  1105         assert(false, "Bad conversion from /proc/self/stat");
  1109         assert(false, "Bad conversion from /proc/self/stat");
  1106         // product mode - assume we are the initial thread, good luck in the
  1110         // product mode - assume we are the primordial thread, good luck in the
  1107         // embedded case.
  1111         // embedded case.
  1108         warning("Can't detect initial thread stack location - bad conversion");
  1112         warning("Can't detect primordial thread stack location - bad conversion");
  1109         stack_start = (uintptr_t) &rlim;
  1113         stack_start = (uintptr_t) &rlim;
  1110       }
  1114       }
  1111     } else {
  1115     } else {
  1112       // For some reason we can't open /proc/self/stat (for example, running on
  1116       // For some reason we can't open /proc/self/stat (for example, running on
  1113       // FreeBSD with a Linux emulator, or inside chroot), this should work for
  1117       // FreeBSD with a Linux emulator, or inside chroot), this should work for
  1114       // most cases, so don't abort:
  1118       // most cases, so don't abort:
  1115       warning("Can't detect initial thread stack location - no /proc/self/stat");
  1119       warning("Can't detect primordial thread stack location - no /proc/self/stat");
  1116       stack_start = (uintptr_t) &rlim;
  1120       stack_start = (uintptr_t) &rlim;
  1117     }
  1121     }
  1118   }
  1122   }
  1119 
  1123 
  1120   // Now we have a pointer (stack_start) very close to the stack top, the
  1124   // Now we have a pointer (stack_start) very close to the stack top, the
  1130     // success, "high" is the true stack top. (ignore "low", because initial
  1134     // success, "high" is the true stack top. (ignore "low", because initial
  1131     // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
  1135     // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
  1132     stack_top = (uintptr_t)high;
  1136     stack_top = (uintptr_t)high;
  1133   } else {
  1137   } else {
  1134     // failed, likely because /proc/self/maps does not exist
  1138     // failed, likely because /proc/self/maps does not exist
  1135     warning("Can't detect initial thread stack location - find_vma failed");
  1139     warning("Can't detect primordial thread stack location - find_vma failed");
  1136     // best effort: stack_start is normally within a few pages below the real
  1140     // best effort: stack_start is normally within a few pages below the real
  1137     // stack top, use it as stack top, and reduce stack size so we won't put
  1141     // stack top, use it as stack top, and reduce stack size so we won't put
  1138     // guard page outside stack.
  1142     // guard page outside stack.
  1139     stack_top = stack_start;
  1143     stack_top = stack_start;
  1140     stack_size -= 16 * page_size();
  1144     stack_size -= 16 * page_size();
  3134 
  3138 
  3135 // If the (growable) stack mapping already extends beyond the point
  3139 // If the (growable) stack mapping already extends beyond the point
  3136 // where we're going to put our guard pages, truncate the mapping at
  3140 // where we're going to put our guard pages, truncate the mapping at
  3137 // that point by munmap()ping it.  This ensures that when we later
  3141 // that point by munmap()ping it.  This ensures that when we later
  3138 // munmap() the guard pages we don't leave a hole in the stack
  3142 // munmap() the guard pages we don't leave a hole in the stack
  3139 // mapping. This only affects the main/initial thread
  3143 // mapping. This only affects the main/primordial thread
  3140 
  3144 
  3141 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
  3145 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
  3142   if (os::Linux::is_initial_thread()) {
  3146   if (os::is_primordial_thread()) {
  3143     // As we manually grow stack up to bottom inside create_attached_thread(),
  3147     // As we manually grow stack up to bottom inside create_attached_thread(),
  3144     // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
  3148     // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
  3145     // we don't need to do anything special.
  3149     // we don't need to do anything special.
  3146     // Check it first, before calling heavy function.
  3150     // Check it first, before calling heavy function.
  3147     uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
  3151     uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
  3162   return os::commit_memory(addr, size, !ExecMem);
  3166   return os::commit_memory(addr, size, !ExecMem);
  3163 }
  3167 }
  3164 
  3168 
  3165 // If this is a growable mapping, remove the guard pages entirely by
  3169 // If this is a growable mapping, remove the guard pages entirely by
  3166 // munmap()ping them.  If not, just call uncommit_memory(). This only
  3170 // munmap()ping them.  If not, just call uncommit_memory(). This only
  3167 // affects the main/initial thread, but guard against future OS changes
  3171 // affects the main/primordial thread, but guard against future OS changes.
  3168 // It's safe to always unmap guard pages for initial thread because we
  3172 // It's safe to always unmap guard pages for primordial thread because we
  3169 // always place it right after end of the mapped region
  3173 // always place it right after end of the mapped region.
  3170 
  3174 
  3171 bool os::remove_stack_guard_pages(char* addr, size_t size) {
  3175 bool os::remove_stack_guard_pages(char* addr, size_t size) {
  3172   uintptr_t stack_extent, stack_base;
  3176   uintptr_t stack_extent, stack_base;
  3173 
  3177 
  3174   if (os::Linux::is_initial_thread()) {
  3178   if (os::is_primordial_thread()) {
  3175     return ::munmap(addr, size) == 0;
  3179     return ::munmap(addr, size) == 0;
  3176   }
  3180   }
  3177 
  3181 
  3178   return os::uncommit_memory(addr, size);
  3182   return os::uncommit_memory(addr, size);
  3179 }
  3183 }
  4858 }
  4862 }
  4859 
  4863 
  4860 extern void report_error(char* file_name, int line_no, char* title,
  4864 extern void report_error(char* file_name, int line_no, char* title,
  4861                          char* format, ...);
  4865                          char* format, ...);
  4862 
  4866 
  4863 // this is called _before_ the most of global arguments have been parsed
  4867 // this is called _before_ most of the global arguments have been parsed
  4864 void os::init(void) {
  4868 void os::init(void) {
  4865   char dummy;   // used to get a guess on initial stack address
  4869   char dummy;   // used to get a guess on initial stack address
  4866 //  first_hrtime = gethrtime();
       
  4867 
  4870 
  4868   clock_tics_per_sec = sysconf(_SC_CLK_TCK);
  4871   clock_tics_per_sec = sysconf(_SC_CLK_TCK);
  4869 
  4872 
  4870   init_random(1234567);
  4873   init_random(1234567);
  4871 
  4874 
  4878 
  4881 
  4879   Linux::initialize_system_info();
  4882   Linux::initialize_system_info();
  4880 
  4883 
  4881   Linux::initialize_os_info();
  4884   Linux::initialize_os_info();
  4882 
  4885 
  4883   // main_thread points to the aboriginal thread
  4886   // _main_thread points to the thread that created/loaded the JVM.
  4884   Linux::_main_thread = pthread_self();
  4887   Linux::_main_thread = pthread_self();
  4885 
  4888 
  4886   Linux::clock_init();
  4889   Linux::clock_init();
  4887   initial_time_count = javaTimeNanos();
  4890   initial_time_count = javaTimeNanos();
  4888 
  4891 
  5849 //    of the stack size given in pthread_attr. We work around this for
  5852 //    of the stack size given in pthread_attr. We work around this for
  5850 //    threads created by the VM. (We adapt bottom to be P1 and size accordingly.)
  5853 //    threads created by the VM. (We adapt bottom to be P1 and size accordingly.)
  5851 //
  5854 //
  5852 #ifndef ZERO
  5855 #ifndef ZERO
  5853 static void current_stack_region(address * bottom, size_t * size) {
  5856 static void current_stack_region(address * bottom, size_t * size) {
  5854   if (os::Linux::is_initial_thread()) {
  5857   if (os::is_primordial_thread()) {
  5855     // initial thread needs special handling because pthread_getattr_np()
  5858     // primordial thread needs special handling because pthread_getattr_np()
  5856     // may return bogus value.
  5859     // may return bogus value.
  5857     *bottom = os::Linux::initial_thread_stack_bottom();
  5860     *bottom = os::Linux::initial_thread_stack_bottom();
  5858     *size   = os::Linux::initial_thread_stack_size();
  5861     *size   = os::Linux::initial_thread_stack_size();
  5859   } else {
  5862   } else {
  5860     pthread_attr_t attr;
  5863     pthread_attr_t attr;