< prev index next >

src/os/linux/vm/os_linux.cpp

Print this page


   1 /*
   2  * Copyright (c) 1999, 2016, 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  *


 929 
 930   // Store pthread info into the OSThread
 931   osthread->set_thread_id(os::Linux::gettid());
 932   osthread->set_pthread_id(::pthread_self());
 933 
 934   // initialize floating point control register
 935   os::Linux::init_thread_fpu_state();
 936 
 937   // Initial thread state is RUNNABLE
 938   osthread->set_state(RUNNABLE);
 939 
 940   thread->set_osthread(osthread);
 941 
 942   if (UseNUMA) {
 943     int lgrp_id = os::numa_get_group_id();
 944     if (lgrp_id != -1) {
 945       thread->set_lgrp_id(lgrp_id);
 946     }
 947   }
 948 
 949   if (os::Linux::is_initial_thread()) {
 950     // If current thread is initial thread, its stack is mapped on demand,
 951     // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
 952     // the entire stack region to avoid SEGV in stack banging.
 953     // It is also useful to get around the heap-stack-gap problem on SuSE
 954     // kernel (see 4821821 for details). We first expand stack to the top
 955     // of yellow zone, then enable stack yellow zone (order is significant,
 956     // enabling yellow zone first will crash JVM on SuSE Linux), so there
 957     // is no gap between the last two virtual memory regions.
 958 
 959     JavaThread *jt = (JavaThread *)thread;
 960     address addr = jt->stack_yellow_zone_base();
 961     assert(addr != NULL, "initialization problem?");
 962     assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
 963 
 964     osthread->set_expanding_stack();
 965     os::Linux::manually_expand_stack(jt, addr);
 966     osthread->clear_expanding_stack();
 967   }
 968 
 969   // initialize signal mask for this thread
 970   // and save the caller's signal mask


1015   return (int)key;
1016 }
1017 
1018 // Note: This is currently not used by VM, as we don't destroy TLS key
1019 // on VM exit.
1020 void os::free_thread_local_storage(int index) {
1021   int rslt = pthread_key_delete((pthread_key_t)index);
1022   assert(rslt == 0, "invalid index");
1023 }
1024 
1025 void os::thread_local_storage_at_put(int index, void* value) {
1026   int rslt = pthread_setspecific((pthread_key_t)index, value);
1027   assert(rslt == 0, "pthread_setspecific failed");
1028 }
1029 
1030 extern "C" Thread* get_thread() {
1031   return ThreadLocalStorage::thread();
1032 }
1033 
1034 //////////////////////////////////////////////////////////////////////////////
1035 // initial thread
1036 
1037 // Check if current thread is the initial thread, similar to Solaris thr_main.
1038 bool os::Linux::is_initial_thread(void) {
1039   char dummy;
1040   // If called before init complete, thread stack bottom will be null.
1041   // Can be called if fatal error occurs before initialization.
1042   if (initial_thread_stack_bottom() == NULL) return false;
1043   assert(initial_thread_stack_bottom() != NULL &&
1044          initial_thread_stack_size()   != 0,
1045          "os::init did not locate initial thread's stack region");
1046   if ((address)&dummy >= initial_thread_stack_bottom() &&
1047       (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size())

1048        return true;
1049   else return false;


1050 }
1051 
1052 // Find the virtual memory area that contains addr
1053 static bool find_vma(address addr, address* vma_low, address* vma_high) {
1054   FILE *fp = fopen("/proc/self/maps", "r");
1055   if (fp) {
1056     address low, high;
1057     while (!feof(fp)) {
1058       if (fscanf(fp, "%p-%p", &low, &high) == 2) {
1059         if (low <= addr && addr < high) {
1060            if (vma_low)  *vma_low  = low;
1061            if (vma_high) *vma_high = high;
1062            fclose (fp);
1063            return true;
1064         }
1065       }
1066       for (;;) {
1067         int ch = fgetc(fp);
1068         if (ch == EOF || ch == (int)'\n') break;
1069       }
1070     }
1071     fclose(fp);
1072   }
1073   return false;
1074 }
1075 
1076 // Locate initial thread stack. This special handling of initial thread stack
1077 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
1078 // bogus value for the primordial process thread. While the launcher has created
1079 // the VM in a new thread since JDK 6, we still have to allow for the use of the
1080 // JNI invocation API from a primordial thread.
1081 void os::Linux::capture_initial_stack(size_t max_size) {
1082 
1083   // max_size is either 0 (which means accept OS default for thread stacks) or
1084   // a user-specified value known to be at least the minimum needed. If we
1085   // are actually on the primordial thread we can make it appear that we have a
1086   // smaller max_size stack by inserting the guard pages at that location. But we
1087   // cannot do anything to emulate a larger stack than what has been provided by
1088   // the OS or threading library. In fact if we try to use a stack greater than
1089   // what is set by rlimit then we will crash the hosting process.
1090 
1091   // Maximum stack size is the easy part, get it from RLIMIT_STACK.
1092   // If this is "unlimited" then it will be a huge value.
1093   struct rlimit rlim;
1094   getrlimit(RLIMIT_STACK, &rlim);
1095   size_t stack_size = rlim.rlim_cur;
1096 
1097   // 6308388: a bug in ld.so will relocate its own .data section to the
1098   //   lower end of primordial stack; reduce ulimit -s value a little bit
1099   //   so we won't install guard page on ld.so's data section.


1100   stack_size -= 2 * page_size();

1101 
1102   // Try to figure out where the stack base (top) is. This is harder.
1103   //
1104   // When an application is started, glibc saves the initial stack pointer in
1105   // a global variable "__libc_stack_end", which is then used by system
1106   // libraries. __libc_stack_end should be pretty close to stack top. The
1107   // variable is available since the very early days. However, because it is
1108   // a private interface, it could disappear in the future.
1109   //
1110   // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar
1111   // to __libc_stack_end, it is very close to stack top, but isn't the real
1112   // stack top. Note that /proc may not exist if VM is running as a chroot
1113   // program, so reading /proc/<pid>/stat could fail. Also the contents of
1114   // /proc/<pid>/stat could change in the future (though unlikely).
1115   //
1116   // We try __libc_stack_end first. If that doesn't work, look for
1117   // /proc/<pid>/stat. If neither of them works, we use current stack pointer
1118   // as a hint, which should work well in most cases.
1119 
1120   uintptr_t stack_start;


1201              &cutime,         /* 16 %ld  */
1202              &cstime,         /* 17 %ld  */
1203              &prio,           /* 18 %ld  */
1204              &nice,           /* 19 %ld  */
1205              &junk,           /* 20 %ld  */
1206              &it_real,        /* 21 %ld  */
1207              &start,          /* 22 UINTX_FORMAT */
1208              &vsize,          /* 23 UINTX_FORMAT */
1209              &rss,            /* 24 INTX_FORMAT  */
1210              &rsslim,         /* 25 UINTX_FORMAT */
1211              &scodes,         /* 26 UINTX_FORMAT */
1212              &ecode,          /* 27 UINTX_FORMAT */
1213              &stack_start);   /* 28 UINTX_FORMAT */
1214       }
1215 
1216 #undef _UFM
1217 #undef _DFM
1218 
1219       if (i != 28 - 2) {
1220          assert(false, "Bad conversion from /proc/self/stat");
1221          // product mode - assume we are the initial thread, good luck in the
1222          // embedded case.
1223          warning("Can't detect initial thread stack location - bad conversion");
1224          stack_start = (uintptr_t) &rlim;
1225       }
1226     } else {
1227       // For some reason we can't open /proc/self/stat (for example, running on
1228       // FreeBSD with a Linux emulator, or inside chroot), this should work for
1229       // most cases, so don't abort:
1230       warning("Can't detect initial thread stack location - no /proc/self/stat");
1231       stack_start = (uintptr_t) &rlim;
1232     }
1233   }
1234 
1235   // Now we have a pointer (stack_start) very close to the stack top, the
1236   // next thing to do is to figure out the exact location of stack top. We
1237   // can find out the virtual memory area that contains stack_start by
1238   // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
1239   // and its upper limit is the real stack top. (again, this would fail if
1240   // running inside chroot, because /proc may not exist.)
1241 
1242   uintptr_t stack_top;
1243   address low, high;
1244   if (find_vma((address)stack_start, &low, &high)) {
1245     // success, "high" is the true stack top. (ignore "low", because initial
1246     // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
1247     stack_top = (uintptr_t)high;
1248   } else {
1249     // failed, likely because /proc/self/maps does not exist
1250     warning("Can't detect initial thread stack location - find_vma failed");
1251     // best effort: stack_start is normally within a few pages below the real
1252     // stack top, use it as stack top, and reduce stack size so we won't put
1253     // guard page outside stack.
1254     stack_top = stack_start;
1255     stack_size -= 16 * page_size();
1256   }
1257 
1258   // stack_top could be partially down the page so align it
1259   stack_top = align_size_up(stack_top, page_size());
1260 
1261   // Allowed stack value is minimum of max_size and what we derived from rlimit
1262   if (max_size > 0) {
1263     _initial_thread_stack_size = MIN2(max_size, stack_size);
1264   } else {
1265     // Accept the rlimit max, but if stack is unlimited then it will be huge, so
1266     // clamp it at 8MB as we do on Solaris
1267     _initial_thread_stack_size = MIN2(stack_size, 8*M);
1268   }
1269 
1270   _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());


3049 // mapped.  If at some point later in the process the stack expands to
3050 // that point, the Linux kernel cannot expand the stack any further
3051 // because the guard pages are in the way, and a segfault occurs.
3052 //
3053 // However, it's essential not to split the stack region by unmapping
3054 // a region (leaving a hole) that's already part of the stack mapping,
3055 // so if the stack mapping has already grown beyond the guard pages at
3056 // the time we create them, we have to truncate the stack mapping.
3057 // So, we need to know the extent of the stack mapping when
3058 // create_stack_guard_pages() is called.
3059 
3060 // We only need this for stacks that are growable: at the time of
3061 // writing thread stacks don't use growable mappings (i.e. those
3062 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
3063 // only applies to the main thread.
3064 
3065 // If the (growable) stack mapping already extends beyond the point
3066 // where we're going to put our guard pages, truncate the mapping at
3067 // that point by munmap()ping it.  This ensures that when we later
3068 // munmap() the guard pages we don't leave a hole in the stack
3069 // mapping. This only affects the main/initial thread
3070 
3071 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
3072 
3073   if (os::Linux::is_initial_thread()) {
3074     // As we manually grow stack up to bottom inside create_attached_thread(),
3075     // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
3076     // we don't need to do anything special.
3077     // Check it first, before calling heavy function.
3078     uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
3079     unsigned char vec[1];
3080 
3081     if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {
3082       // Fallback to slow path on all errors, including EAGAIN
3083       stack_extent = (uintptr_t) get_stack_commited_bottom(
3084                                     os::Linux::initial_thread_stack_bottom(),
3085                                     (size_t)addr - stack_extent);
3086     }
3087 
3088     if (stack_extent < (uintptr_t)addr) {
3089       ::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));
3090     }
3091   }
3092 
3093   return os::commit_memory(addr, size, !ExecMem);
3094 }
3095 
3096 // If this is a growable mapping, remove the guard pages entirely by
3097 // munmap()ping them.  If not, just call uncommit_memory(). This only
3098 // affects the main/initial thread, but guard against future OS changes
3099 // It's safe to always unmap guard pages for initial thread because we
3100 // always place it right after end of the mapped region
3101 
3102 bool os::remove_stack_guard_pages(char* addr, size_t size) {
3103   uintptr_t stack_extent, stack_base;
3104 
3105   if (os::Linux::is_initial_thread()) {
3106     return ::munmap(addr, size) == 0;
3107   }
3108 
3109   return os::uncommit_memory(addr, size);
3110 }
3111 
3112 static address _highest_vm_reserved_address = NULL;
3113 
3114 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory
3115 // at 'requested_addr'. If there are existing memory mappings at the same
3116 // location, however, they will be overwritten. If 'fixed' is false,
3117 // 'requested_addr' is only treated as a hint, the return value may or
3118 // may not start from the requested address. Unlike Linux mmap(), this
3119 // function returns NULL to indicate failure.
3120 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {
3121   char * addr;
3122   int flags;
3123 
3124   flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;
3125   if (fixed) {


4860     print_signal_handlers(tty, buf, O_BUFLEN);
4861   }
4862 }
4863 
4864 extern void report_error(char* file_name, int line_no, char* title, char* format, ...);
4865 
4866 extern bool signal_name(int signo, char* buf, size_t len);
4867 
4868 const char* os::exception_name(int exception_code, char* buf, size_t size) {
4869   if (0 < exception_code && exception_code <= SIGRTMAX) {
4870     // signal
4871     if (!signal_name(exception_code, buf, size)) {
4872       jio_snprintf(buf, size, "SIG%d", exception_code);
4873     }
4874     return buf;
4875   } else {
4876     return NULL;
4877   }
4878 }
4879 
4880 // this is called _before_ the most of global arguments have been parsed
4881 void os::init(void) {
4882   char dummy;   /* used to get a guess on initial stack address */
4883 //  first_hrtime = gethrtime();
4884 
4885   // With LinuxThreads the JavaMain thread pid (primordial thread)
4886   // is different than the pid of the java launcher thread.
4887   // So, on Linux, the launcher thread pid is passed to the VM
4888   // via the sun.java.launcher.pid property.
4889   // Use this property instead of getpid() if it was correctly passed.
4890   // See bug 6351349.
4891   pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();
4892 
4893   _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();
4894 
4895   clock_tics_per_sec = sysconf(_SC_CLK_TCK);
4896 
4897   init_random(1234567);
4898 
4899   ThreadCritical::initialize();
4900 
4901   Linux::set_page_size(sysconf(_SC_PAGESIZE));
4902   if (Linux::page_size() == -1) {
4903     fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)",
4904                   strerror(errno)));
4905   }
4906   init_page_sizes((size_t) Linux::page_size());
4907 
4908   Linux::initialize_system_info();
4909 
4910   // main_thread points to the aboriginal thread
4911   Linux::_main_thread = pthread_self();
4912 
4913   Linux::clock_init();
4914   initial_time_count = javaTimeNanos();
4915 
4916   // pthread_condattr initialization for monotonic clock
4917   int status;
4918   pthread_condattr_t* _condattr = os::Linux::condAttr();
4919   if ((status = pthread_condattr_init(_condattr)) != 0) {
4920     fatal(err_msg("pthread_condattr_init: %s", strerror(status)));
4921   }
4922   // Only set the clock if CLOCK_MONOTONIC is available
4923   if (Linux::supports_monotonic_clock()) {
4924     if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) {
4925       if (status == EINVAL) {
4926         warning("Unable to use monotonic clock with relative timed-waits" \
4927                 " - changes to the time-of-day clock may have adverse affects");
4928       } else {
4929         fatal(err_msg("pthread_condattr_setclock: %s", strerror(status)));
4930       }


   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  *


 929 
 930   // Store pthread info into the OSThread
 931   osthread->set_thread_id(os::Linux::gettid());
 932   osthread->set_pthread_id(::pthread_self());
 933 
 934   // initialize floating point control register
 935   os::Linux::init_thread_fpu_state();
 936 
 937   // Initial thread state is RUNNABLE
 938   osthread->set_state(RUNNABLE);
 939 
 940   thread->set_osthread(osthread);
 941 
 942   if (UseNUMA) {
 943     int lgrp_id = os::numa_get_group_id();
 944     if (lgrp_id != -1) {
 945       thread->set_lgrp_id(lgrp_id);
 946     }
 947   }
 948 
 949   if (os::is_primordial_thread()) {
 950     // If current thread is primordial thread, its stack is mapped on demand,
 951     // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
 952     // the entire stack region to avoid SEGV in stack banging.
 953     // It is also useful to get around the heap-stack-gap problem on SuSE
 954     // kernel (see 4821821 for details). We first expand stack to the top
 955     // of yellow zone, then enable stack yellow zone (order is significant,
 956     // enabling yellow zone first will crash JVM on SuSE Linux), so there
 957     // is no gap between the last two virtual memory regions.
 958 
 959     JavaThread *jt = (JavaThread *)thread;
 960     address addr = jt->stack_yellow_zone_base();
 961     assert(addr != NULL, "initialization problem?");
 962     assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
 963 
 964     osthread->set_expanding_stack();
 965     os::Linux::manually_expand_stack(jt, addr);
 966     osthread->clear_expanding_stack();
 967   }
 968 
 969   // initialize signal mask for this thread
 970   // and save the caller's signal mask


1015   return (int)key;
1016 }
1017 
1018 // Note: This is currently not used by VM, as we don't destroy TLS key
1019 // on VM exit.
1020 void os::free_thread_local_storage(int index) {
1021   int rslt = pthread_key_delete((pthread_key_t)index);
1022   assert(rslt == 0, "invalid index");
1023 }
1024 
1025 void os::thread_local_storage_at_put(int index, void* value) {
1026   int rslt = pthread_setspecific((pthread_key_t)index, value);
1027   assert(rslt == 0, "pthread_setspecific failed");
1028 }
1029 
1030 extern "C" Thread* get_thread() {
1031   return ThreadLocalStorage::thread();
1032 }
1033 
1034 //////////////////////////////////////////////////////////////////////////////
1035 // primordial thread
1036 
1037 // Check if current thread is the primordial thread, similar to Solaris thr_main.
1038 bool os::is_primordial_thread(void) {
1039   char dummy;
1040   // If called before init complete, thread stack bottom will be null.
1041   // Can be called if fatal error occurs before initialization.
1042   if (os::Linux::initial_thread_stack_bottom() == NULL) return false;
1043   assert(os::Linux::initial_thread_stack_bottom() != NULL &&
1044          os::Linux::initial_thread_stack_size()   != 0,
1045          "os::init did not locate primordial thread's stack region");
1046   if ((address)&dummy >= os::Linux::initial_thread_stack_bottom() &&
1047       (address)&dummy < os::Linux::initial_thread_stack_bottom() +
1048                         os::Linux::initial_thread_stack_size()) {
1049        return true;
1050   } else {
1051        return false;
1052   }
1053 }
1054 
1055 // Find the virtual memory area that contains addr
1056 static bool find_vma(address addr, address* vma_low, address* vma_high) {
1057   FILE *fp = fopen("/proc/self/maps", "r");
1058   if (fp) {
1059     address low, high;
1060     while (!feof(fp)) {
1061       if (fscanf(fp, "%p-%p", &low, &high) == 2) {
1062         if (low <= addr && addr < high) {
1063            if (vma_low)  *vma_low  = low;
1064            if (vma_high) *vma_high = high;
1065            fclose (fp);
1066            return true;
1067         }
1068       }
1069       for (;;) {
1070         int ch = fgetc(fp);
1071         if (ch == EOF || ch == (int)'\n') break;
1072       }
1073     }
1074     fclose(fp);
1075   }
1076   return false;
1077 }
1078 
1079 // Locate primordial thread stack. This special handling of primordial thread stack
1080 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
1081 // bogus value for the primordial process thread. While the launcher has created
1082 // the VM in a new thread since JDK 6, we still have to allow for the use of the
1083 // JNI invocation API from a primordial thread.
1084 void os::Linux::capture_initial_stack(size_t max_size) {
1085 
1086   // max_size is either 0 (which means accept OS default for thread stacks) or
1087   // a user-specified value known to be at least the minimum needed. If we
1088   // are actually on the primordial thread we can make it appear that we have a
1089   // smaller max_size stack by inserting the guard pages at that location. But we
1090   // cannot do anything to emulate a larger stack than what has been provided by
1091   // the OS or threading library. In fact if we try to use a stack greater than
1092   // what is set by rlimit then we will crash the hosting process.
1093 
1094   // Maximum stack size is the easy part, get it from RLIMIT_STACK.
1095   // If this is "unlimited" then it will be a huge value.
1096   struct rlimit rlim;
1097   getrlimit(RLIMIT_STACK, &rlim);
1098   size_t stack_size = rlim.rlim_cur;
1099 
1100   // 6308388: a bug in ld.so will relocate its own .data section to the
1101   //   lower end of primordial stack; reduce ulimit -s value a little bit
1102   //   so we won't install guard page on ld.so's data section.
1103   //   But ensure we don't underflow the stack size - allow 1 page spare
1104   if (stack_size >= (size_t)(3 * page_size())) {
1105     stack_size -= 2 * page_size();
1106   }
1107 
1108   // Try to figure out where the stack base (top) is. This is harder.
1109   //
1110   // When an application is started, glibc saves the initial stack pointer in
1111   // a global variable "__libc_stack_end", which is then used by system
1112   // libraries. __libc_stack_end should be pretty close to stack top. The
1113   // variable is available since the very early days. However, because it is
1114   // a private interface, it could disappear in the future.
1115   //
1116   // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar
1117   // to __libc_stack_end, it is very close to stack top, but isn't the real
1118   // stack top. Note that /proc may not exist if VM is running as a chroot
1119   // program, so reading /proc/<pid>/stat could fail. Also the contents of
1120   // /proc/<pid>/stat could change in the future (though unlikely).
1121   //
1122   // We try __libc_stack_end first. If that doesn't work, look for
1123   // /proc/<pid>/stat. If neither of them works, we use current stack pointer
1124   // as a hint, which should work well in most cases.
1125 
1126   uintptr_t stack_start;


1207              &cutime,         /* 16 %ld  */
1208              &cstime,         /* 17 %ld  */
1209              &prio,           /* 18 %ld  */
1210              &nice,           /* 19 %ld  */
1211              &junk,           /* 20 %ld  */
1212              &it_real,        /* 21 %ld  */
1213              &start,          /* 22 UINTX_FORMAT */
1214              &vsize,          /* 23 UINTX_FORMAT */
1215              &rss,            /* 24 INTX_FORMAT  */
1216              &rsslim,         /* 25 UINTX_FORMAT */
1217              &scodes,         /* 26 UINTX_FORMAT */
1218              &ecode,          /* 27 UINTX_FORMAT */
1219              &stack_start);   /* 28 UINTX_FORMAT */
1220       }
1221 
1222 #undef _UFM
1223 #undef _DFM
1224 
1225       if (i != 28 - 2) {
1226          assert(false, "Bad conversion from /proc/self/stat");
1227          // product mode - assume we are the primordial thread, good luck in the
1228          // embedded case.
1229          warning("Can't detect primordial thread stack location - bad conversion");
1230          stack_start = (uintptr_t) &rlim;
1231       }
1232     } else {
1233       // For some reason we can't open /proc/self/stat (for example, running on
1234       // FreeBSD with a Linux emulator, or inside chroot), this should work for
1235       // most cases, so don't abort:
1236       warning("Can't detect primordial thread stack location - no /proc/self/stat");
1237       stack_start = (uintptr_t) &rlim;
1238     }
1239   }
1240 
1241   // Now we have a pointer (stack_start) very close to the stack top, the
1242   // next thing to do is to figure out the exact location of stack top. We
1243   // can find out the virtual memory area that contains stack_start by
1244   // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
1245   // and its upper limit is the real stack top. (again, this would fail if
1246   // running inside chroot, because /proc may not exist.)
1247 
1248   uintptr_t stack_top;
1249   address low, high;
1250   if (find_vma((address)stack_start, &low, &high)) {
1251     // success, "high" is the true stack top. (ignore "low", because initial
1252     // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
1253     stack_top = (uintptr_t)high;
1254   } else {
1255     // failed, likely because /proc/self/maps does not exist
1256     warning("Can't detect primordial thread stack location - find_vma failed");
1257     // best effort: stack_start is normally within a few pages below the real
1258     // stack top, use it as stack top, and reduce stack size so we won't put
1259     // guard page outside stack.
1260     stack_top = stack_start;
1261     stack_size -= 16 * page_size();
1262   }
1263 
1264   // stack_top could be partially down the page so align it
1265   stack_top = align_size_up(stack_top, page_size());
1266 
1267   // Allowed stack value is minimum of max_size and what we derived from rlimit
1268   if (max_size > 0) {
1269     _initial_thread_stack_size = MIN2(max_size, stack_size);
1270   } else {
1271     // Accept the rlimit max, but if stack is unlimited then it will be huge, so
1272     // clamp it at 8MB as we do on Solaris
1273     _initial_thread_stack_size = MIN2(stack_size, 8*M);
1274   }
1275 
1276   _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());


3055 // mapped.  If at some point later in the process the stack expands to
3056 // that point, the Linux kernel cannot expand the stack any further
3057 // because the guard pages are in the way, and a segfault occurs.
3058 //
3059 // However, it's essential not to split the stack region by unmapping
3060 // a region (leaving a hole) that's already part of the stack mapping,
3061 // so if the stack mapping has already grown beyond the guard pages at
3062 // the time we create them, we have to truncate the stack mapping.
3063 // So, we need to know the extent of the stack mapping when
3064 // create_stack_guard_pages() is called.
3065 
3066 // We only need this for stacks that are growable: at the time of
3067 // writing thread stacks don't use growable mappings (i.e. those
3068 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
3069 // only applies to the main thread.
3070 
3071 // If the (growable) stack mapping already extends beyond the point
3072 // where we're going to put our guard pages, truncate the mapping at
3073 // that point by munmap()ping it.  This ensures that when we later
3074 // munmap() the guard pages we don't leave a hole in the stack
3075 // mapping. This only affects the main/primordial thread
3076 
3077 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
3078 
3079   if (os::is_primordial_thread()) {
3080     // As we manually grow stack up to bottom inside create_attached_thread(),
3081     // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
3082     // we don't need to do anything special.
3083     // Check it first, before calling heavy function.
3084     uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
3085     unsigned char vec[1];
3086 
3087     if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {
3088       // Fallback to slow path on all errors, including EAGAIN
3089       stack_extent = (uintptr_t) get_stack_commited_bottom(
3090                                     os::Linux::initial_thread_stack_bottom(),
3091                                     (size_t)addr - stack_extent);
3092     }
3093 
3094     if (stack_extent < (uintptr_t)addr) {
3095       ::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));
3096     }
3097   }
3098 
3099   return os::commit_memory(addr, size, !ExecMem);
3100 }
3101 
3102 // If this is a growable mapping, remove the guard pages entirely by
3103 // munmap()ping them.  If not, just call uncommit_memory(). This only
3104 // affects the main/primordial thread, but guard against future OS changes.
3105 // It's safe to always unmap guard pages for primordial thread because we
3106 // always place it right after end of the mapped region.
3107 
3108 bool os::remove_stack_guard_pages(char* addr, size_t size) {
3109   uintptr_t stack_extent, stack_base;
3110 
3111   if (os::is_primordial_thread()) {
3112     return ::munmap(addr, size) == 0;
3113   }
3114 
3115   return os::uncommit_memory(addr, size);
3116 }
3117 
3118 static address _highest_vm_reserved_address = NULL;
3119 
3120 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory
3121 // at 'requested_addr'. If there are existing memory mappings at the same
3122 // location, however, they will be overwritten. If 'fixed' is false,
3123 // 'requested_addr' is only treated as a hint, the return value may or
3124 // may not start from the requested address. Unlike Linux mmap(), this
3125 // function returns NULL to indicate failure.
3126 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {
3127   char * addr;
3128   int flags;
3129 
3130   flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;
3131   if (fixed) {


4866     print_signal_handlers(tty, buf, O_BUFLEN);
4867   }
4868 }
4869 
4870 extern void report_error(char* file_name, int line_no, char* title, char* format, ...);
4871 
4872 extern bool signal_name(int signo, char* buf, size_t len);
4873 
4874 const char* os::exception_name(int exception_code, char* buf, size_t size) {
4875   if (0 < exception_code && exception_code <= SIGRTMAX) {
4876     // signal
4877     if (!signal_name(exception_code, buf, size)) {
4878       jio_snprintf(buf, size, "SIG%d", exception_code);
4879     }
4880     return buf;
4881   } else {
4882     return NULL;
4883   }
4884 }
4885 
4886 // this is called _before_ most of the global arguments have been parsed
4887 void os::init(void) {
4888   char dummy;   /* used to get a guess on initial stack address */

4889 
4890   // With LinuxThreads the JavaMain thread pid (primordial thread)
4891   // is different than the pid of the java launcher thread.
4892   // So, on Linux, the launcher thread pid is passed to the VM
4893   // via the sun.java.launcher.pid property.
4894   // Use this property instead of getpid() if it was correctly passed.
4895   // See bug 6351349.
4896   pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();
4897 
4898   _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();
4899 
4900   clock_tics_per_sec = sysconf(_SC_CLK_TCK);
4901 
4902   init_random(1234567);
4903 
4904   ThreadCritical::initialize();
4905 
4906   Linux::set_page_size(sysconf(_SC_PAGESIZE));
4907   if (Linux::page_size() == -1) {
4908     fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)",
4909                   strerror(errno)));
4910   }
4911   init_page_sizes((size_t) Linux::page_size());
4912 
4913   Linux::initialize_system_info();
4914 
4915   // _main_thread points to the thread that created/loaded the JVM.
4916   Linux::_main_thread = pthread_self();
4917 
4918   Linux::clock_init();
4919   initial_time_count = javaTimeNanos();
4920 
4921   // pthread_condattr initialization for monotonic clock
4922   int status;
4923   pthread_condattr_t* _condattr = os::Linux::condAttr();
4924   if ((status = pthread_condattr_init(_condattr)) != 0) {
4925     fatal(err_msg("pthread_condattr_init: %s", strerror(status)));
4926   }
4927   // Only set the clock if CLOCK_MONOTONIC is available
4928   if (Linux::supports_monotonic_clock()) {
4929     if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) {
4930       if (status == EINVAL) {
4931         warning("Unable to use monotonic clock with relative timed-waits" \
4932                 " - changes to the time-of-day clock may have adverse affects");
4933       } else {
4934         fatal(err_msg("pthread_condattr_setclock: %s", strerror(status)));
4935       }


< prev index next >