hotspot/src/share/vm/runtime/thread.cpp
changeset 35201 996db89f378e
parent 35071 a0910b1d3e0d
child 35216 71c463a17b3b
equal deleted inserted replaced
35192:76f4de26388d 35201:996db89f378e
   317   // set up any platform-specific state.
   317   // set up any platform-specific state.
   318   os::initialize_thread(this);
   318   os::initialize_thread(this);
   319 
   319 
   320 #if INCLUDE_NMT
   320 #if INCLUDE_NMT
   321   // record thread's native stack, stack grows downward
   321   // record thread's native stack, stack grows downward
   322   address stack_low_addr = stack_base() - stack_size();
   322   MemTracker::record_thread_stack(stack_end(), stack_size());
   323   MemTracker::record_thread_stack(stack_low_addr, stack_size());
       
   324 #endif // INCLUDE_NMT
   323 #endif // INCLUDE_NMT
   325 }
   324 }
   326 
   325 
   327 
   326 
   328 Thread::~Thread() {
   327 Thread::~Thread() {
   335   // record_stack_base_and_size called. Although, we would like to ensure
   334   // record_stack_base_and_size called. Although, we would like to ensure
   336   // that all started threads do call record_stack_base_and_size(), there is
   335   // that all started threads do call record_stack_base_and_size(), there is
   337   // not proper way to enforce that.
   336   // not proper way to enforce that.
   338 #if INCLUDE_NMT
   337 #if INCLUDE_NMT
   339   if (_stack_base != NULL) {
   338   if (_stack_base != NULL) {
   340     address low_stack_addr = stack_base() - stack_size();
   339     MemTracker::release_thread_stack(stack_end(), stack_size());
   341     MemTracker::release_thread_stack(low_stack_addr, stack_size());
       
   342 #ifdef ASSERT
   340 #ifdef ASSERT
   343     set_stack_base(NULL);
   341     set_stack_base(NULL);
   344 #endif
   342 #endif
   345   }
   343   }
   346 #endif // INCLUDE_NMT
   344 #endif // INCLUDE_NMT
   819   else if (is_Watcher_thread())       st->print("WatcherThread");
   817   else if (is_Watcher_thread())       st->print("WatcherThread");
   820   else if (is_ConcurrentGC_thread())  st->print("ConcurrentGCThread");
   818   else if (is_ConcurrentGC_thread())  st->print("ConcurrentGCThread");
   821   else                                st->print("Thread");
   819   else                                st->print("Thread");
   822 
   820 
   823   st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
   821   st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
   824             p2i(_stack_base - _stack_size), p2i(_stack_base));
   822             p2i(stack_end()), p2i(stack_base()));
   825 
   823 
   826   if (osthread()) {
   824   if (osthread()) {
   827     st->print(" [id=%d]", osthread()->thread_id());
   825     st->print(" [id=%d]", osthread()->thread_id());
   828   }
   826   }
   829 }
   827 }
   905   if (stack_base() >= adr && adr >= end) return true;
   903   if (stack_base() >= adr && adr >= end) return true;
   906 
   904 
   907   return false;
   905   return false;
   908 }
   906 }
   909 
   907 
   910 
       
   911 bool Thread::is_in_usable_stack(address adr) const {
   908 bool Thread::is_in_usable_stack(address adr) const {
   912   size_t stack_guard_size = os::uses_stack_guard_pages() ? (StackReservedPages + StackYellowPages + StackRedPages) * os::vm_page_size() : 0;
   909   size_t stack_guard_size = os::uses_stack_guard_pages() ? JavaThread::stack_guard_zone_size() : 0;
   913   size_t usable_stack_size = _stack_size - stack_guard_size;
   910   size_t usable_stack_size = _stack_size - stack_guard_size;
   914 
   911 
   915   return ((adr < stack_base()) && (adr >= stack_base() - usable_stack_size));
   912   return ((adr < stack_base()) && (adr >= stack_base() - usable_stack_size));
   916 }
   913 }
   917 
   914 
  1532   }
  1529   }
  1533   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
  1530   assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
  1534 }
  1531 }
  1535 
  1532 
  1536 bool JavaThread::reguard_stack(address cur_sp) {
  1533 bool JavaThread::reguard_stack(address cur_sp) {
  1537   if (_stack_guard_state != stack_guard_yellow_disabled
  1534   if (_stack_guard_state != stack_guard_yellow_reserved_disabled
  1538       && _stack_guard_state != stack_guard_reserved_disabled) {
  1535       && _stack_guard_state != stack_guard_reserved_disabled) {
  1539     return true; // Stack already guarded or guard pages not needed.
  1536     return true; // Stack already guarded or guard pages not needed.
  1540   }
  1537   }
  1541 
  1538 
  1542   if (register_stack_overflow()) {
  1539   if (register_stack_overflow()) {
  1549   // Java code never executes within the yellow zone: the latter is only
  1546   // Java code never executes within the yellow zone: the latter is only
  1550   // there to provoke an exception during stack banging.  If java code
  1547   // there to provoke an exception during stack banging.  If java code
  1551   // is executing there, either StackShadowPages should be larger, or
  1548   // is executing there, either StackShadowPages should be larger, or
  1552   // some exception code in c1, c2 or the interpreter isn't unwinding
  1549   // some exception code in c1, c2 or the interpreter isn't unwinding
  1553   // when it should.
  1550   // when it should.
  1554   guarantee(cur_sp > stack_yellow_zone_base(), "not enough space to reguard - increase StackShadowPages");
  1551   guarantee(cur_sp > stack_reserved_zone_base(),
  1555   if (_stack_guard_state == stack_guard_yellow_disabled) {
  1552             "not enough space to reguard - increase StackShadowPages");
  1556     enable_stack_yellow_zone();
  1553   if (_stack_guard_state == stack_guard_yellow_reserved_disabled) {
       
  1554     enable_stack_yellow_reserved_zone();
  1557     if (reserved_stack_activation() != stack_base()) {
  1555     if (reserved_stack_activation() != stack_base()) {
  1558       set_reserved_stack_activation(stack_base());
  1556       set_reserved_stack_activation(stack_base());
  1559     }
  1557     }
  1560   } else if (_stack_guard_state == stack_guard_reserved_disabled) {
  1558   } else if (_stack_guard_state == stack_guard_reserved_disabled) {
  1561     set_reserved_stack_activation(stack_base());
  1559     set_reserved_stack_activation(stack_base());
  2478     clear_ext_suspended();
  2476     clear_ext_suspended();
  2479     SR_lock()->notify_all();
  2477     SR_lock()->notify_all();
  2480   }
  2478   }
  2481 }
  2479 }
  2482 
  2480 
       
  2481 size_t JavaThread::_stack_red_zone_size = 0;
       
  2482 size_t JavaThread::_stack_yellow_zone_size = 0;
       
  2483 size_t JavaThread::_stack_reserved_zone_size = 0;
       
  2484 size_t JavaThread::_stack_shadow_zone_size = 0;
       
  2485 
  2483 void JavaThread::create_stack_guard_pages() {
  2486 void JavaThread::create_stack_guard_pages() {
  2484   if (! os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) return;
  2487   if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }
  2485   address low_addr = stack_base() - stack_size();
  2488   address low_addr = stack_end();
  2486   size_t len = (StackReservedPages + StackYellowPages + StackRedPages) * os::vm_page_size();
  2489   size_t len = stack_guard_zone_size();
  2487 
  2490 
  2488   int allocate = os::allocate_stack_guard_pages();
  2491   int allocate = os::allocate_stack_guard_pages();
  2489   // warning("Guarding at " PTR_FORMAT " for len " SIZE_FORMAT "\n", low_addr, len);
  2492   // warning("Guarding at " PTR_FORMAT " for len " SIZE_FORMAT "\n", low_addr, len);
  2490 
  2493 
  2491   if (allocate && !os::create_stack_guard_pages((char *) low_addr, len)) {
  2494   if (allocate && !os::create_stack_guard_pages((char *) low_addr, len)) {
  2504 }
  2507 }
  2505 
  2508 
  2506 void JavaThread::remove_stack_guard_pages() {
  2509 void JavaThread::remove_stack_guard_pages() {
  2507   assert(Thread::current() == this, "from different thread");
  2510   assert(Thread::current() == this, "from different thread");
  2508   if (_stack_guard_state == stack_guard_unused) return;
  2511   if (_stack_guard_state == stack_guard_unused) return;
  2509   address low_addr = stack_base() - stack_size();
  2512   address low_addr = stack_end();
  2510   size_t len = (StackReservedPages + StackYellowPages + StackRedPages) * os::vm_page_size();
  2513   size_t len = stack_guard_zone_size();
  2511 
  2514 
  2512   if (os::allocate_stack_guard_pages()) {
  2515   if (os::allocate_stack_guard_pages()) {
  2513     if (os::remove_stack_guard_pages((char *) low_addr, len)) {
  2516     if (os::remove_stack_guard_pages((char *) low_addr, len)) {
  2514       _stack_guard_state = stack_guard_unused;
  2517       _stack_guard_state = stack_guard_unused;
  2515     } else {
  2518     } else {
  2561     warning("Attempt to unguard stack reserved zone failed.");
  2564     warning("Attempt to unguard stack reserved zone failed.");
  2562   }
  2565   }
  2563   disable_register_stack_guard();
  2566   disable_register_stack_guard();
  2564 }
  2567 }
  2565 
  2568 
  2566 void JavaThread::enable_stack_yellow_zone() {
  2569 void JavaThread::enable_stack_yellow_reserved_zone() {
  2567   assert(_stack_guard_state != stack_guard_unused, "must be using guard pages.");
  2570   assert(_stack_guard_state != stack_guard_unused, "must be using guard pages.");
  2568   assert(_stack_guard_state != stack_guard_enabled, "already enabled");
  2571   assert(_stack_guard_state != stack_guard_enabled, "already enabled");
  2569 
  2572 
  2570   // The base notation is from the stacks point of view, growing downward.
  2573   // The base notation is from the stacks point of view, growing downward.
  2571   // We need to adjust it to work correctly with guard_memory()
  2574   // We need to adjust it to work correctly with guard_memory()
  2572   address base = stack_yellow_zone_base() - stack_yellow_zone_size();
  2575   address base = stack_red_zone_base();
  2573 
  2576 
  2574   guarantee(base < stack_base(), "Error calculating stack yellow zone");
  2577   guarantee(base < stack_base(), "Error calculating stack yellow zone");
  2575   guarantee(base < os::current_stack_pointer(), "Error calculating stack yellow zone");
  2578   guarantee(base < os::current_stack_pointer(), "Error calculating stack yellow zone");
  2576 
  2579 
  2577   if (os::guard_memory((char *) base, stack_yellow_zone_size())) {
  2580   if (os::guard_memory((char *) base, stack_yellow_reserved_zone_size())) {
  2578     _stack_guard_state = stack_guard_enabled;
  2581     _stack_guard_state = stack_guard_enabled;
  2579   } else {
  2582   } else {
  2580     warning("Attempt to guard stack yellow zone failed.");
  2583     warning("Attempt to guard stack yellow zone failed.");
  2581   }
  2584   }
  2582   enable_register_stack_guard();
  2585   enable_register_stack_guard();
  2583 }
  2586 }
  2584 
  2587 
  2585 void JavaThread::disable_stack_yellow_zone() {
  2588 void JavaThread::disable_stack_yellow_reserved_zone() {
  2586   assert(_stack_guard_state != stack_guard_unused, "must be using guard pages.");
  2589   assert(_stack_guard_state != stack_guard_unused, "must be using guard pages.");
  2587   assert(_stack_guard_state != stack_guard_yellow_disabled, "already disabled");
  2590   assert(_stack_guard_state != stack_guard_yellow_reserved_disabled, "already disabled");
  2588 
  2591 
  2589   // Simply return if called for a thread that does not use guard pages.
  2592   // Simply return if called for a thread that does not use guard pages.
  2590   if (_stack_guard_state == stack_guard_unused) return;
  2593   if (_stack_guard_state == stack_guard_unused) return;
  2591 
  2594 
  2592   // The base notation is from the stacks point of view, growing downward.
  2595   // The base notation is from the stacks point of view, growing downward.
  2593   // We need to adjust it to work correctly with guard_memory()
  2596   // We need to adjust it to work correctly with guard_memory()
  2594   address base = stack_yellow_zone_base() - stack_yellow_zone_size();
  2597   address base = stack_red_zone_base();
  2595 
  2598 
  2596   if (os::unguard_memory((char *)base, stack_yellow_zone_size())) {
  2599   if (os::unguard_memory((char *)base, stack_yellow_reserved_zone_size())) {
  2597     _stack_guard_state = stack_guard_yellow_disabled;
  2600     _stack_guard_state = stack_guard_yellow_reserved_disabled;
  2598   } else {
  2601   } else {
  2599     warning("Attempt to unguard stack yellow zone failed.");
  2602     warning("Attempt to unguard stack yellow zone failed.");
  2600   }
  2603   }
  2601   disable_register_stack_guard();
  2604   disable_register_stack_guard();
  2602 }
  2605 }
  2897   st->print("%s", _get_thread_state_name(_thread_state));
  2900   st->print("%s", _get_thread_state_name(_thread_state));
  2898   if (osthread()) {
  2901   if (osthread()) {
  2899     st->print(", id=%d", osthread()->thread_id());
  2902     st->print(", id=%d", osthread()->thread_id());
  2900   }
  2903   }
  2901   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
  2904   st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
  2902             p2i(_stack_base - _stack_size), p2i(_stack_base));
  2905             p2i(stack_end()), p2i(stack_base()));
  2903   st->print("]");
  2906   st->print("]");
  2904   return;
  2907   return;
  2905 }
  2908 }
  2906 
  2909 
  2907 // Verification
  2910 // Verification