182 // 4759953: Compensate for ridiculous stack size.
183 size = max_intx;
184 }
185 if (size > (size_t)base) {
186 // 4812466: Make sure size doesn't allow the stack to wrap the address space.
187 size = (size_t)base;
188 }
189 return size;
190 }
191
192 static inline stack_t get_stack_info() {
193 stack_t st;
194 int retval = thr_stksegment(&st);
195 st.ss_size = adjust_stack_size((address)st.ss_sp, st.ss_size);
196 assert(retval == 0, "incorrect return value from thr_stksegment");
197 assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
198 assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
199 return st;
200 }
201
202 bool os::is_primordial_thread(void) {
203 int r = thr_main();
204 guarantee(r == 0 || r == 1, "CR6501650 or CR6493689");
205 return r == 1;
206 }
207
208 address os::current_stack_base() {
209 bool _is_primordial_thread = is_primordial_thread();
210
211 // Workaround 4352906, avoid calls to thr_stksegment by
212 // thr_main after the first one (it looks like we trash
213 // some data, causing the value for ss_sp to be incorrect).
214 if (!_is_primordial_thread || os::Solaris::_main_stack_base == NULL) {
215 stack_t st = get_stack_info();
216 if (_is_primordial_thread) {
217 // cache initial value of stack base
218 os::Solaris::_main_stack_base = (address)st.ss_sp;
219 }
220 return (address)st.ss_sp;
221 } else {
707 return true;
708 }
709 return false;
710 }
711
712 bool os::Solaris::valid_stack_address(Thread* thread, address sp) {
713 address stackStart = (address)thread->stack_base();
714 address stackEnd = (address)(stackStart - (address)thread->stack_size());
715 if (sp < stackStart && sp >= stackEnd) return true;
716 return false;
717 }
718
719 extern "C" void breakpoint() {
720 // use debugger to set breakpoint here
721 }
722
723 static thread_t main_thread;
724
725 // Thread start routine for all newly created threads
726 extern "C" void* thread_native_entry(void* thread_addr) {
727 // Try to randomize the cache line index of hot stack frames.
728 // This helps when threads of the same stack traces evict each other's
729 // cache lines. The threads can be either from the same JVM instance, or
730 // from different JVM instances. The benefit is especially true for
731 // processors with hyperthreading technology.
732 static int counter = 0;
733 int pid = os::current_process_id();
734 alloca(((pid ^ counter++) & 7) * 128);
735
736 int prio;
737 Thread* thread = (Thread*)thread_addr;
738
739 thread->initialize_thread_current();
740
741 OSThread* osthr = thread->osthread();
742
743 osthr->set_lwp_id(_lwp_self()); // Store lwp in case we are bound
744
745 log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ").",
746 os::current_thread_id());
747
748 if (UseNUMA) {
749 int lgrp_id = os::numa_get_group_id();
750 if (lgrp_id != -1) {
751 thread->set_lgrp_id(lgrp_id);
752 }
753 }
754
755 // Our priority was set when we were created, and stored in the
756 // osthread, but couldn't be passed through to our LWP until now.
757 // So read back the priority and set it again.
758
759 if (osthr->thread_id() != -1) {
760 if (UseThreadPriorities) {
761 int prio = osthr->native_priority();
762 if (ThreadPriorityVerbose) {
763 tty->print_cr("Starting Thread " INTPTR_FORMAT ", LWP is "
764 INTPTR_FORMAT ", setting priority: %d\n",
765 osthr->thread_id(), osthr->lwp_id(), prio);
766 }
767 os::set_native_priority(thread, prio);
768 }
769 } else if (ThreadPriorityVerbose) {
770 warning("Can't set priority in _start routine, thread id hasn't been set\n");
771 }
772
773 assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
774
775 // initialize signal mask for this thread
776 os::Solaris::hotspot_sigmask(thread);
777
778 thread->run();
779
780 // One less thread is executing
781 // When the VMThread gets here, the main thread may have already exited
782 // which frees the CodeHeap containing the Atomic::dec code
783 if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
784 Atomic::dec(&os::Solaris::_os_thread_count);
785 }
786
787 log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ").", os::current_thread_id());
788
789 // If a thread has not deleted itself ("delete this") as part of its
790 // termination sequence, we have to ensure thread-local-storage is
791 // cleared before we actually terminate. No threads should ever be
792 // deleted asynchronously with respect to their termination.
793 if (Thread::current_or_null_safe() != NULL) {
794 assert(Thread::current_or_null_safe() == thread, "current thread is wrong");
795 thread->clear_thread_current();
796 }
797
798 if (UseDetachedThreads) {
799 thr_exit(NULL);
800 ShouldNotReachHere();
801 }
802 return NULL;
803 }
804
805 static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
806 // Allocate the OSThread object
807 OSThread* osthread = new OSThread(NULL, NULL);
808 if (osthread == NULL) return NULL;
809
810 // Store info on the Solaris thread into the OSThread
811 osthread->set_thread_id(thread_id);
812 osthread->set_lwp_id(_lwp_self());
813
814 if (UseNUMA) {
815 int lgrp_id = os::numa_get_group_id();
816 if (lgrp_id != -1) {
817 thread->set_lgrp_id(lgrp_id);
1073 debug_only(signal_sets_initialized = true);
1074
1075 // For diagnostics only used in run_periodic_checks
1076 sigemptyset(&check_signal_done);
1077 }
1078
1079 // These are signals that are unblocked while a thread is running Java.
1080 // (For some reason, they get blocked by default.)
1081 sigset_t* os::Solaris::unblocked_signals() {
1082 assert(signal_sets_initialized, "Not initialized");
1083 return &unblocked_sigs;
1084 }
1085
1086 // These are the signals that are blocked while a (non-VM) thread is
1087 // running Java. Only the VM thread handles these signals.
1088 sigset_t* os::Solaris::vm_signals() {
1089 assert(signal_sets_initialized, "Not initialized");
1090 return &vm_sigs;
1091 }
1092
1093 void _handle_uncaught_cxx_exception() {
1094 VMError::report_and_die("An uncaught C++ exception");
1095 }
1096
1097
1098 // First crack at OS-specific initialization, from inside the new thread.
1099 void os::initialize_thread(Thread* thr) {
1100 if (is_primordial_thread()) {
1101 JavaThread* jt = (JavaThread *)thr;
1102 assert(jt != NULL, "Sanity check");
1103 size_t stack_size;
1104 address base = jt->stack_base();
1105 if (Arguments::created_by_java_launcher()) {
1106 // Use 2MB to allow for Solaris 7 64 bit mode.
1107 stack_size = JavaThread::stack_size_at_create() == 0
1108 ? 2048*K : JavaThread::stack_size_at_create();
1109
1110 // There are rare cases when we may have already used more than
1111 // the basic stack size allotment before this method is invoked.
1112 // Attempt to allow for a normally sized java_stack.
1113 size_t current_stack_offset = (size_t)(base - (address)&stack_size);
1114 stack_size += ReservedSpace::page_align_size_down(current_stack_offset);
1115 } else {
1116 // 6269555: If we were not created by a Java launcher, i.e. if we are
1117 // running embedded in a native application, treat the primordial thread
1118 // as much like a native attached thread as possible. This means using
1119 // the current stack size from thr_stksegment(), unless it is too large
1120 // to reliably setup guard pages. A reasonable max size is 8MB.
1121 size_t current_size = current_stack_size();
1122 // This should never happen, but just in case....
1123 if (current_size == 0) current_size = 2 * K * K;
1124 stack_size = current_size > (8 * K * K) ? (8 * K * K) : current_size;
1125 }
1126 address bottom = align_up(base - stack_size, os::vm_page_size());;
1127 stack_size = (size_t)(base - bottom);
1128
1129 assert(stack_size > 0, "Stack size calculation problem");
1130
1131 if (stack_size > jt->stack_size()) {
1132 #ifndef PRODUCT
1133 struct rlimit limits;
1134 getrlimit(RLIMIT_STACK, &limits);
1135 size_t size = adjust_stack_size(base, (size_t)limits.rlim_cur);
1136 assert(size >= jt->stack_size(), "Stack size problem in main thread");
1137 #endif
1138 tty->print_cr("Stack size of %d Kb exceeds current limit of %d Kb.\n"
1139 "(Stack sizes are rounded up to a multiple of the system page size.)\n"
1140 "See limit(1) to increase the stack size limit.",
1141 stack_size / K, jt->stack_size() / K);
1142 vm_exit(1);
1143 }
1144 assert(jt->stack_size() >= stack_size,
1145 "Attempt to map more stack than was allocated");
1146 jt->set_stack_size(stack_size);
1147 }
1148
1149 // With the T2 libthread (T1 is no longer supported) threads are always bound
1150 // and we use stackbanging in all cases.
1151
1152 os::Solaris::init_thread_fpu_state();
1153 std::set_terminate(_handle_uncaught_cxx_exception);
1154 }
1155
1156
1157
1158 // Free Solaris resources related to the OSThread
1159 void os::free_thread(OSThread* osthread) {
1160 assert(osthread != NULL, "os::free_thread but osthread not set");
1161
1162 // We are told to free resources of the argument thread,
1163 // but we can only really operate on the current thread.
1164 assert(Thread::current()->osthread() == osthread,
1165 "os::free_thread but not current thread");
1166
1167 // Restore caller's signal mask
1168 sigset_t sigmask = osthread->caller_sigmask();
1169 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
1170
1171 delete osthread;
1172 }
1173
|
182 // 4759953: Compensate for ridiculous stack size.
183 size = max_intx;
184 }
185 if (size > (size_t)base) {
186 // 4812466: Make sure size doesn't allow the stack to wrap the address space.
187 size = (size_t)base;
188 }
189 return size;
190 }
191
192 static inline stack_t get_stack_info() {
193 stack_t st;
194 int retval = thr_stksegment(&st);
195 st.ss_size = adjust_stack_size((address)st.ss_sp, st.ss_size);
196 assert(retval == 0, "incorrect return value from thr_stksegment");
197 assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
198 assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
199 return st;
200 }
201
202 static void _handle_uncaught_cxx_exception() {
203 VMError::report_and_die("An uncaught C++ exception");
204 }
205
206 bool os::is_primordial_thread(void) {
207 int r = thr_main();
208 guarantee(r == 0 || r == 1, "CR6501650 or CR6493689");
209 return r == 1;
210 }
211
212 address os::current_stack_base() {
213 bool _is_primordial_thread = is_primordial_thread();
214
215 // Workaround 4352906, avoid calls to thr_stksegment by
216 // thr_main after the first one (it looks like we trash
217 // some data, causing the value for ss_sp to be incorrect).
218 if (!_is_primordial_thread || os::Solaris::_main_stack_base == NULL) {
219 stack_t st = get_stack_info();
220 if (_is_primordial_thread) {
221 // cache initial value of stack base
222 os::Solaris::_main_stack_base = (address)st.ss_sp;
223 }
224 return (address)st.ss_sp;
225 } else {
711 return true;
712 }
713 return false;
714 }
715
716 bool os::Solaris::valid_stack_address(Thread* thread, address sp) {
717 address stackStart = (address)thread->stack_base();
718 address stackEnd = (address)(stackStart - (address)thread->stack_size());
719 if (sp < stackStart && sp >= stackEnd) return true;
720 return false;
721 }
722
723 extern "C" void breakpoint() {
724 // use debugger to set breakpoint here
725 }
726
727 static thread_t main_thread;
728
729 // Thread start routine for all newly created threads
730 extern "C" void* thread_native_entry(void* thread_addr) {
731
732 Thread* thread = (Thread*)thread_addr;
733
734 thread->record_stack_base_and_size();
735
736 // Try to randomize the cache line index of hot stack frames.
737 // This helps when threads of the same stack traces evict each other's
738 // cache lines. The threads can be either from the same JVM instance, or
739 // from different JVM instances. The benefit is especially true for
740 // processors with hyperthreading technology.
741 static int counter = 0;
742 int pid = os::current_process_id();
743 alloca(((pid ^ counter++) & 7) * 128);
744
745 int prio;
746
747 thread->initialize_thread_current();
748
749 OSThread* osthr = thread->osthread();
750
751 osthr->set_lwp_id(_lwp_self()); // Store lwp in case we are bound
752
753 log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ").",
754 os::current_thread_id());
755
756 if (UseNUMA) {
757 int lgrp_id = os::numa_get_group_id();
758 if (lgrp_id != -1) {
759 thread->set_lgrp_id(lgrp_id);
760 }
761 }
762
763 // Our priority was set when we were created, and stored in the
764 // osthread, but couldn't be passed through to our LWP until now.
765 // So read back the priority and set it again.
766
767 if (osthr->thread_id() != -1) {
768 if (UseThreadPriorities) {
769 int prio = osthr->native_priority();
770 if (ThreadPriorityVerbose) {
771 tty->print_cr("Starting Thread " INTPTR_FORMAT ", LWP is "
772 INTPTR_FORMAT ", setting priority: %d\n",
773 osthr->thread_id(), osthr->lwp_id(), prio);
774 }
775 os::set_native_priority(thread, prio);
776 }
777 } else if (ThreadPriorityVerbose) {
778 warning("Can't set priority in _start routine, thread id hasn't been set\n");
779 }
780
781 assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
782
783 // initialize signal mask for this thread
784 os::Solaris::hotspot_sigmask(thread);
785
786 os::Solaris::init_thread_fpu_state();
787 std::set_terminate(_handle_uncaught_cxx_exception);
788
789 thread->call_run();
790
791 // Note: at this point the thread object may already have deleted itself.
792 // Do not dereference it from here on out.
793
794 // One less thread is executing
795 // When the VMThread gets here, the main thread may have already exited
796 // which frees the CodeHeap containing the Atomic::dec code
797 if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
798 Atomic::dec(&os::Solaris::_os_thread_count);
799 }
800
801 log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ").", os::current_thread_id());
802
803 if (UseDetachedThreads) {
804 thr_exit(NULL);
805 ShouldNotReachHere();
806 }
807 return NULL;
808 }
809
810 static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
811 // Allocate the OSThread object
812 OSThread* osthread = new OSThread(NULL, NULL);
813 if (osthread == NULL) return NULL;
814
815 // Store info on the Solaris thread into the OSThread
816 osthread->set_thread_id(thread_id);
817 osthread->set_lwp_id(_lwp_self());
818
819 if (UseNUMA) {
820 int lgrp_id = os::numa_get_group_id();
821 if (lgrp_id != -1) {
822 thread->set_lgrp_id(lgrp_id);
1078 debug_only(signal_sets_initialized = true);
1079
1080 // For diagnostics only used in run_periodic_checks
1081 sigemptyset(&check_signal_done);
1082 }
1083
1084 // These are signals that are unblocked while a thread is running Java.
1085 // (For some reason, they get blocked by default.)
1086 sigset_t* os::Solaris::unblocked_signals() {
1087 assert(signal_sets_initialized, "Not initialized");
1088 return &unblocked_sigs;
1089 }
1090
1091 // These are the signals that are blocked while a (non-VM) thread is
1092 // running Java. Only the VM thread handles these signals.
1093 sigset_t* os::Solaris::vm_signals() {
1094 assert(signal_sets_initialized, "Not initialized");
1095 return &vm_sigs;
1096 }
1097
1098 // CR 7190089: on Solaris, primordial thread's stack needs adjusting.
1099 // Without the adjustment, stack size is incorrect if stack is set to unlimited (ulimit -s unlimited).
1100 void os::Solaris::correct_stack_boundaries_for_primordial_thread(Thread* thr) {
1101 assert(is_primordial_thread(), "Call only for primordial thread");
1102
1103 JavaThread* jt = (JavaThread *)thr;
1104 assert(jt != NULL, "Sanity check");
1105 size_t stack_size;
1106 address base = jt->stack_base();
1107 if (Arguments::created_by_java_launcher()) {
1108 // Use 2MB to allow for Solaris 7 64 bit mode.
1109 stack_size = JavaThread::stack_size_at_create() == 0
1110 ? 2048*K : JavaThread::stack_size_at_create();
1111
1112 // There are rare cases when we may have already used more than
1113 // the basic stack size allotment before this method is invoked.
1114 // Attempt to allow for a normally sized java_stack.
1115 size_t current_stack_offset = (size_t)(base - (address)&stack_size);
1116 stack_size += ReservedSpace::page_align_size_down(current_stack_offset);
1117 } else {
1118 // 6269555: If we were not created by a Java launcher, i.e. if we are
1119 // running embedded in a native application, treat the primordial thread
1120 // as much like a native attached thread as possible. This means using
1121 // the current stack size from thr_stksegment(), unless it is too large
1122 // to reliably setup guard pages. A reasonable max size is 8MB.
1123 size_t current_size = os::current_stack_size();
1124 // This should never happen, but just in case....
1125 if (current_size == 0) current_size = 2 * K * K;
1126 stack_size = current_size > (8 * K * K) ? (8 * K * K) : current_size;
1127 }
1128 address bottom = align_up(base - stack_size, os::vm_page_size());;
1129 stack_size = (size_t)(base - bottom);
1130
1131 assert(stack_size > 0, "Stack size calculation problem");
1132
1133 if (stack_size > jt->stack_size()) {
1134 #ifndef PRODUCT
1135 struct rlimit limits;
1136 getrlimit(RLIMIT_STACK, &limits);
1137 size_t size = adjust_stack_size(base, (size_t)limits.rlim_cur);
1138 assert(size >= jt->stack_size(), "Stack size problem in main thread");
1139 #endif
1140 tty->print_cr("Stack size of %d Kb exceeds current limit of %d Kb.\n"
1141 "(Stack sizes are rounded up to a multiple of the system page size.)\n"
1142 "See limit(1) to increase the stack size limit.",
1143 stack_size / K, jt->stack_size() / K);
1144 vm_exit(1);
1145 }
1146 assert(jt->stack_size() >= stack_size,
1147 "Attempt to map more stack than was allocated");
1148 jt->set_stack_size(stack_size);
1149
1150 }
1151
1152
1153
1154 // Free Solaris resources related to the OSThread
1155 void os::free_thread(OSThread* osthread) {
1156 assert(osthread != NULL, "os::free_thread but osthread not set");
1157
1158 // We are told to free resources of the argument thread,
1159 // but we can only really operate on the current thread.
1160 assert(Thread::current()->osthread() == osthread,
1161 "os::free_thread but not current thread");
1162
1163 // Restore caller's signal mask
1164 sigset_t sigmask = osthread->caller_sigmask();
1165 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
1166
1167 delete osthread;
1168 }
1169
|