1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)os_solaris.hpp       1.121 07/06/29 04:05:00 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // Solaris_OS defines the interface to Solaris operating systems
  29 
  30 class Solaris {
  31   friend class os;
  32 
  33  private:
  34   
  35   // Support for "new" libthread APIs for getting & setting thread context (2.8)
  36   #define TRS_VALID       0
  37   #define TRS_NONVOLATILE 1
  38   #define TRS_LWPID       2
  39   #define TRS_INVALID     3
  40 
  41   // _T2_libthread is true if we believe we are running with the newer
  42   // SunSoft lib/lwp/libthread: default Solaris 9, available Solaris 8
  43   // which is a lightweight libthread that also supports all T1
  44   static bool _T2_libthread;
  45   // These refer to new libthread interface functions
  46   // They get intialized if we dynamically detect new libthread
  47   static int_fnP_thread_t_iP_uP_stack_tP_gregset_t _thr_getstate;
  48   static int_fnP_thread_t_i_gregset_t _thr_setstate;
  49   static int_fnP_thread_t_i _thr_setmutator;
  50   static int_fnP_thread_t _thr_suspend_mutator;
  51   static int_fnP_thread_t _thr_continue_mutator;
  52   // libthread_init sets the above, if the new functionality is detected
  53 
  54   // initialized to libthread or lwp synchronization primitives depending on UseLWPSychronization
  55   static int_fnP_mutex_tP _mutex_lock;
  56   static int_fnP_mutex_tP _mutex_trylock;
  57   static int_fnP_mutex_tP _mutex_unlock;
  58   static int_fnP_mutex_tP_i_vP _mutex_init;
  59   static int_fnP_mutex_tP _mutex_destroy;
  60   static int _mutex_scope;
  61 
  62   static int_fnP_cond_tP_mutex_tP_timestruc_tP _cond_timedwait;
  63   static int_fnP_cond_tP_mutex_tP _cond_wait;
  64   static int_fnP_cond_tP _cond_signal;
  65   static int_fnP_cond_tP _cond_broadcast;
  66   static int_fnP_cond_tP_i_vP _cond_init;
  67   static int_fnP_cond_tP _cond_destroy;
  68   static int _cond_scope;
  69 
  70   typedef uintptr_t       lgrp_cookie_t;
  71   typedef id_t            lgrp_id_t;
  72   typedef int             lgrp_rsrc_t;
  73   typedef enum lgrp_view {
  74         LGRP_VIEW_CALLER,       /* what's available to the caller */
  75         LGRP_VIEW_OS            /* what's available to operating system */
  76   } lgrp_view_t;
  77 
  78   typedef lgrp_id_t (*lgrp_home_func_t)(idtype_t idtype, id_t id);
  79   typedef lgrp_cookie_t (*lgrp_init_func_t)(lgrp_view_t view);
  80   typedef int (*lgrp_fini_func_t)(lgrp_cookie_t cookie);
  81   typedef lgrp_id_t (*lgrp_root_func_t)(lgrp_cookie_t cookie);
  82   typedef int (*lgrp_children_func_t)(lgrp_cookie_t  cookie,  lgrp_id_t  parent,
  83                                       lgrp_id_t *lgrp_array, uint_t lgrp_array_size);
  84   typedef int (*lgrp_resources_func_t)(lgrp_cookie_t  cookie,  lgrp_id_t  lgrp,
  85                                       lgrp_id_t *lgrp_array, uint_t lgrp_array_size,
  86                                       lgrp_rsrc_t type);
  87   typedef int (*lgrp_nlgrps_func_t)(lgrp_cookie_t cookie);
  88   typedef int (*lgrp_cookie_stale_func_t)(lgrp_cookie_t cookie);
  89   typedef int (*meminfo_func_t)(const uint64_t inaddr[],   int addr_count,
  90                                 const uint_t  info_req[],  int info_count,
  91                                 uint64_t  outdata[], uint_t validity[]);
  92 
  93   static lgrp_home_func_t _lgrp_home;
  94   static lgrp_init_func_t _lgrp_init;
  95   static lgrp_fini_func_t _lgrp_fini;
  96   static lgrp_root_func_t _lgrp_root;
  97   static lgrp_children_func_t _lgrp_children;
  98   static lgrp_resources_func_t _lgrp_resources;
  99   static lgrp_nlgrps_func_t _lgrp_nlgrps;
 100   static lgrp_cookie_stale_func_t _lgrp_cookie_stale;
 101   static lgrp_cookie_t _lgrp_cookie;
 102 
 103   static meminfo_func_t _meminfo;
 104 
 105   // Large Page Support--mpss.
 106   static bool set_mpss_range(caddr_t start, size_t bytes, size_t align);
 107 
 108   static void init_thread_fpu_state(void);
 109 
 110   static void try_enable_extended_io();
 111 
 112   // For signal-chaining
 113   static unsigned long sigs;                 // mask of signals that have
 114                                              // preinstalled signal handlers
 115   static struct sigaction *(*get_signal_action)(int);
 116   static struct sigaction *get_preinstalled_handler(int);
 117   static int (*get_libjsig_version)();
 118   static void save_preinstalled_handler(int, struct sigaction&);
 119   static void check_signal_handler(int sig);
 120   // For overridable signals
 121   static int _SIGinterrupt;                  // user-overridable INTERRUPT_SIGNAL
 122   static int _SIGasync;                      // user-overridable ASYNC_SIGNAL
 123   static void set_SIGinterrupt(int newsig) { _SIGinterrupt = newsig; }
 124   static void set_SIGasync(int newsig) { _SIGasync = newsig; }
 125 
 126 
 127  public:
 128   // Large Page Support--ISM.
 129   static bool largepage_range(char* addr, size_t size); 
 130 
 131   static int SIGinterrupt() { return _SIGinterrupt; }
 132   static int SIGasync() { return _SIGasync; }
 133   static address handler_start, handler_end; // start and end pc of thr_sighndlrinfo
 134 
 135   static bool valid_stack_address(Thread* thread, address sp);
 136   static bool valid_ucontext(Thread* thread, ucontext_t* valid, ucontext_t* suspect);
 137   static ucontext_t* get_valid_uc_in_signal_handler(Thread* thread,
 138     ucontext_t* uc);
 139 
 140   static ExtendedPC  ucontext_get_ExtendedPC(ucontext_t* uc);
 141   static intptr_t*   ucontext_get_sp(ucontext_t* uc);
 142   // ucontext_get_fp() is only used by Solaris X86 (see note below)
 143   static intptr_t*   ucontext_get_fp(ucontext_t* uc);
 144 
 145   // For Analyzer Forte AsyncGetCallTrace profiling support:
 146   // Parameter ret_fp is only used by Solaris X86.
 147   //
 148   // We should have different declarations of this interface in
 149   // os_solaris_i486.hpp and os_solaris_sparc.hpp, but that file
 150   // provides extensions to the os class and not the Solaris class.
 151   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
 152     intptr_t** ret_sp, intptr_t** ret_fp);
 153 
 154   static void hotspot_sigmask(Thread* thread);
 155 
 156  protected:
 157   // Solaris-specific interface goes here
 158   static julong available_memory();
 159   static julong physical_memory() { return _physical_memory; }
 160   static julong _physical_memory;
 161   static void initialize_system_info();
 162   static int _dev_zero_fd;
 163   static int get_dev_zero_fd() { return _dev_zero_fd; }
 164   static void set_dev_zero_fd(int fd) { _dev_zero_fd = fd; }
 165   static char* mmap_chunk(char *addr, size_t size, int flags, int prot);
 166   static char* anon_mmap(char* requested_addr, size_t bytes, size_t alignment_hint, bool fixed);
 167   static bool mpss_sanity_check(bool warn, size_t * page_size);
 168   static bool ism_sanity_check (bool warn, size_t * page_size);
 169 
 170   // Workaround for 4352906. thr_stksegment sometimes returns
 171   // a bad value for the primordial thread's stack base when
 172   // it is called more than one time. 
 173   // Workaround is to cache the initial value to avoid further 
 174   // calls to thr_stksegment. 
 175   // It appears that someone (Hotspot?) is trashing the user's
 176   // proc_t structure (note that this is a system struct).
 177   static address _main_stack_base;
 178 
 179  public:
 180   static void libthread_init();
 181   static void synchronization_init();
 182   static bool liblgrp_init();
 183   // Load miscellaneous symbols.
 184   static void misc_sym_init();
 185   // This boolean allows users to forward their own non-matching signals
 186   // to JVM_handle_solaris_signal, harmlessly.
 187   static bool signal_handlers_are_installed;
 188 
 189   static void signal_sets_init();
 190   static void install_signal_handlers();
 191   static void set_signal_handler(int sig, bool set_installed, bool oktochain);
 192   static void init_signal_mem();
 193   static bool is_sig_ignored(int sig);
 194   static void set_our_sigflags(int, int);
 195   static int get_our_sigflags(int);
 196 
 197   // For signal-chaining
 198   static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
 199                                  // signal(), sigset() is loaded
 200   static struct sigaction *get_chained_signal_action(int sig);
 201   static bool chained_handler(int sig, siginfo_t *siginfo, void *context);
 202 
 203   // The following allow us to link against both the old and new libthread (2.8)
 204   // and exploit the new libthread functionality if available.
 205 
 206   static bool T2_libthread()                                { return _T2_libthread; }
 207   static void set_T2_libthread(bool T2_libthread) { _T2_libthread = T2_libthread; }
 208 
 209   static int thr_getstate(thread_t tid, int *flag, unsigned *lwp, stack_t *ss, gregset_t rs)
 210     { return _thr_getstate(tid, flag, lwp, ss, rs); }
 211   static void set_thr_getstate(int_fnP_thread_t_iP_uP_stack_tP_gregset_t func)
 212     { _thr_getstate = func; }
 213 
 214   static int thr_setstate(thread_t tid, int flag, gregset_t rs)   { return _thr_setstate(tid, flag, rs); }
 215   static void set_thr_setstate(int_fnP_thread_t_i_gregset_t func) { _thr_setstate = func; }
 216   
 217   static int thr_setmutator(thread_t tid, int enabled)    { return _thr_setmutator(tid, enabled); }
 218   static void set_thr_setmutator(int_fnP_thread_t_i func) { _thr_setmutator = func; }
 219 
 220   static int  thr_suspend_mutator(thread_t tid)              { return _thr_suspend_mutator(tid); }
 221   static void set_thr_suspend_mutator(int_fnP_thread_t func) { _thr_suspend_mutator = func; }
 222 
 223   static int  thr_continue_mutator(thread_t tid)              { return _thr_continue_mutator(tid); }
 224   static void set_thr_continue_mutator(int_fnP_thread_t func) { _thr_continue_mutator = func; }
 225 
 226   // Allows us to switch between lwp and thread -based synchronization
 227   static int mutex_lock(mutex_t *mx)    { return _mutex_lock(mx); }
 228   static int mutex_trylock(mutex_t *mx) { return _mutex_trylock(mx); }
 229   static int mutex_unlock(mutex_t *mx)  { return _mutex_unlock(mx); }
 230   static int mutex_init(mutex_t *mx)    { return _mutex_init(mx, os::Solaris::mutex_scope(), NULL); }
 231   static int mutex_destroy(mutex_t *mx) { return _mutex_destroy(mx); }
 232   static int mutex_scope()              { return _mutex_scope; }
 233 
 234   static void set_mutex_lock(int_fnP_mutex_tP func)      { _mutex_lock = func; }
 235   static void set_mutex_trylock(int_fnP_mutex_tP func)   { _mutex_trylock = func; }
 236   static void set_mutex_unlock(int_fnP_mutex_tP func)    { _mutex_unlock = func; }
 237   static void set_mutex_init(int_fnP_mutex_tP_i_vP func) { _mutex_init = func; }
 238   static void set_mutex_destroy(int_fnP_mutex_tP func)   { _mutex_destroy = func; }
 239   static void set_mutex_scope(int scope)                 { _mutex_scope = scope; }
 240 
 241   static int cond_timedwait(cond_t *cv, mutex_t *mx, timestruc_t *abst)
 242                                                 { return _cond_timedwait(cv, mx, abst); }
 243   static int cond_wait(cond_t *cv, mutex_t *mx) { return _cond_wait(cv, mx); }
 244   static int cond_signal(cond_t *cv)            { return _cond_signal(cv); }
 245   static int cond_broadcast(cond_t *cv)         { return _cond_broadcast(cv); }
 246   static int cond_init(cond_t *cv)              { return _cond_init(cv, os::Solaris::cond_scope(), NULL); }
 247   static int cond_destroy(cond_t *cv)           { return _cond_destroy(cv); }
 248   static int cond_scope()                       { return _cond_scope; }
 249 
 250   static void set_cond_timedwait(int_fnP_cond_tP_mutex_tP_timestruc_tP func)
 251                                                            { _cond_timedwait = func; }
 252   static void set_cond_wait(int_fnP_cond_tP_mutex_tP func) { _cond_wait = func; }
 253   static void set_cond_signal(int_fnP_cond_tP func)        { _cond_signal = func; }
 254   static void set_cond_broadcast(int_fnP_cond_tP func)     { _cond_broadcast = func; }
 255   static void set_cond_init(int_fnP_cond_tP_i_vP func)     { _cond_init = func; }
 256   static void set_cond_destroy(int_fnP_cond_tP func)       { _cond_destroy = func; }
 257   static void set_cond_scope(int scope)                    { _cond_scope = scope; }
 258 
 259   static void set_lgrp_home(lgrp_home_func_t func) { _lgrp_home = func; }
 260   static void set_lgrp_init(lgrp_init_func_t func) { _lgrp_init = func; }
 261   static void set_lgrp_fini(lgrp_fini_func_t func) { _lgrp_fini = func; }
 262   static void set_lgrp_root(lgrp_root_func_t func) { _lgrp_root = func; }
 263   static void set_lgrp_children(lgrp_children_func_t func)   { _lgrp_children = func; }
 264   static void set_lgrp_resources(lgrp_resources_func_t func) { _lgrp_resources = func; }
 265   static void set_lgrp_nlgrps(lgrp_nlgrps_func_t func)       { _lgrp_nlgrps = func; }
 266   static void set_lgrp_cookie_stale(lgrp_cookie_stale_func_t func) { _lgrp_cookie_stale = func; }
 267   static void set_lgrp_cookie(lgrp_cookie_t cookie)  { _lgrp_cookie = cookie; }
 268 
 269   static id_t lgrp_home(idtype_t type, id_t id)      { return _lgrp_home != NULL ? _lgrp_home(type, id) : -1; }
 270   static lgrp_cookie_t lgrp_init(lgrp_view_t view)   { return _lgrp_init != NULL ? _lgrp_init(view) : 0; }
 271   static int lgrp_fini(lgrp_cookie_t cookie)         { return _lgrp_fini != NULL ? _lgrp_fini(cookie) : -1; }
 272   static lgrp_id_t lgrp_root(lgrp_cookie_t cookie)   { return _lgrp_root != NULL ? _lgrp_root(cookie) : -1; };
 273   static int lgrp_children(lgrp_cookie_t  cookie,  lgrp_id_t  parent,
 274                     lgrp_id_t *lgrp_array, uint_t lgrp_array_size) {
 275     return _lgrp_children != NULL ? _lgrp_children(cookie, parent, lgrp_array, lgrp_array_size) : -1;
 276   }
 277   static int lgrp_resources(lgrp_cookie_t  cookie,  lgrp_id_t  lgrp,
 278                             lgrp_id_t *lgrp_array, uint_t lgrp_array_size,
 279                             lgrp_rsrc_t type) {
 280     return _lgrp_resources != NULL ? _lgrp_resources(cookie, lgrp, lgrp_array, lgrp_array_size, type) : -1;
 281   }
 282 
 283   static int lgrp_nlgrps(lgrp_cookie_t cookie)       { return _lgrp_nlgrps != NULL ? _lgrp_nlgrps(cookie) : -1; }
 284   static int lgrp_cookie_stale(lgrp_cookie_t cookie) {
 285     return _lgrp_cookie_stale != NULL ? _lgrp_cookie_stale(cookie) : -1; 
 286   }
 287   static lgrp_cookie_t lgrp_cookie()                 { return _lgrp_cookie; }
 288 
 289   static void set_meminfo(meminfo_func_t func)       { _meminfo = func; }
 290   static int meminfo (const uint64_t inaddr[],   int addr_count,
 291                      const uint_t  info_req[],  int info_count,
 292                      uint64_t  outdata[], uint_t validity[]) {
 293     return _meminfo != NULL ? _meminfo(inaddr, addr_count, info_req, info_count,
 294                                        outdata, validity) : -1;
 295   }
 296 
 297   enum {
 298     clear_interrupted = true
 299   };
 300   static void setup_interruptible(JavaThread* thread);
 301   static void setup_interruptible_already_blocked(JavaThread* thread);
 302   static JavaThread* setup_interruptible();
 303   static void cleanup_interruptible(JavaThread* thread);
 304 
 305   // perf counter incrementers used by _INTERRUPTIBLE 
 306 
 307   static void bump_interrupted_before_count();
 308   static void bump_interrupted_during_count();
 309 
 310 #ifdef ASSERT
 311   static JavaThread* setup_interruptible_native();
 312   static void cleanup_interruptible_native(JavaThread* thread);
 313 #endif
 314 
 315   static sigset_t* unblocked_signals();
 316   static sigset_t* vm_signals();
 317   static sigset_t* allowdebug_blocked_signals();
 318 
 319   // %%% Following should be promoted to os.hpp:
 320   // Trace number of created threads
 321   static          jint  _os_thread_limit;
 322   static volatile jint  _os_thread_count;
 323 
 324   // Minimum stack size a thread can be created with (allowing
 325   // the VM to completely create the thread and enter user code)
 326 
 327   static size_t min_stack_allowed;
 328 
 329   // Stack overflow handling
 330 
 331   static int max_register_window_saves_before_flushing();
 332 
 333   // Stack repair handling
 334 
 335   // none present 
 336 
 337 };
 338 
 339 class PlatformEvent : public CHeapObj {
 340   private:
 341     double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
 342     volatile int _Event ;
 343     int _nParked ;
 344     int _pipev [2] ; 
 345     mutex_t _mutex  [1] ;
 346     cond_t  _cond   [1] ;
 347     double PostPad  [2] ;  
 348 
 349   protected:
 350     // Defining a protected ctor effectively gives us an abstract base class.
 351     // That is, a PlatformEvent can never be instantiated "naked" but only
 352     // as a part of a ParkEvent (recall that ParkEvent extends PlatformEvent).  
 353     // TODO-FIXME: make dtor private
 354     ~PlatformEvent() { guarantee (0, "invariant") ; }
 355     PlatformEvent() {
 356       int status;
 357       status = os::Solaris::cond_init(_cond);
 358       assert_status(status == 0, status, "cond_init");
 359       status = os::Solaris::mutex_init(_mutex);
 360       assert_status(status == 0, status, "mutex_init");
 361       _Event   = 0 ;  
 362       _nParked = 0 ;
 363       _pipev[0] = _pipev[1] = -1 ; 
 364     }
 365 
 366   public:
 367     // Exercise caution using reset() and fired() -- they may require MEMBARs
 368     void reset() { _Event = 0 ; } 
 369     int  fired() { return _Event; } 
 370     void park () ; 
 371     int  park (jlong millis) ; 
 372     int  TryPark () ; 
 373     void unpark () ; 
 374 } ; 
 375 
 376 class PlatformParker : public CHeapObj { 
 377   protected:
 378     mutex_t _mutex [1] ; 
 379     cond_t  _cond  [1] ; 
 380 
 381   public:       // TODO-FIXME: make dtor private
 382     ~PlatformParker() { guarantee (0, "invariant") ; }
 383 
 384   public:
 385     PlatformParker() {
 386       int status;
 387       status = os::Solaris::cond_init(_cond);
 388       assert_status(status == 0, status, "cond_init");
 389       status = os::Solaris::mutex_init(_mutex);
 390       assert_status(status == 0, status, "mutex_init");
 391     }
 392 } ; 
 393 
 394 
 395 
 396     
 397 
 398