1 /*
2  * Copyright (c) 1997, 2019, 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  *
23  */
24 
25 #ifndef OS_SOLARIS_OS_SOLARIS_HPP
26 #define OS_SOLARIS_OS_SOLARIS_HPP
27 
28 // Solaris_OS defines the interface to Solaris operating systems
29 
30 // see thr_setprio(3T) for the basis of these numbers
31 #define MinimumPriority 0
32 #define NormalPriority  64
33 #define MaximumPriority 127
34 
35 // FX/60 is critical thread class/priority on T4
36 #define FXCriticalPriority 60
37 
38 // Information about the protection of the page at address '0' on this os.
zero_page_read_protected()39 static bool zero_page_read_protected() { return true; }
40 
41 class Solaris {
42   friend class os;
43 
44  private:
45 
46   // Support for "new" libthread APIs for getting & setting thread context (2.8)
47 #define TRS_VALID       0
48 #define TRS_NONVOLATILE 1
49 #define TRS_LWPID       2
50 #define TRS_INVALID     3
51 
52   // initialized to libthread or lwp synchronization primitives depending on UseLWPSychronization
53   static int_fnP_mutex_tP _mutex_lock;
54   static int_fnP_mutex_tP _mutex_trylock;
55   static int_fnP_mutex_tP _mutex_unlock;
56   static int_fnP_mutex_tP_i_vP _mutex_init;
57   static int_fnP_mutex_tP _mutex_destroy;
58   static int _mutex_scope;
59 
60   static int_fnP_cond_tP_mutex_tP_timestruc_tP _cond_timedwait;
61   static int_fnP_cond_tP_mutex_tP _cond_wait;
62   static int_fnP_cond_tP _cond_signal;
63   static int_fnP_cond_tP _cond_broadcast;
64   static int_fnP_cond_tP_i_vP _cond_init;
65   static int_fnP_cond_tP _cond_destroy;
66   static int _cond_scope;
67 
68   static bool _synchronization_initialized;
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 
90   static lgrp_home_func_t _lgrp_home;
91   static lgrp_init_func_t _lgrp_init;
92   static lgrp_fini_func_t _lgrp_fini;
93   static lgrp_root_func_t _lgrp_root;
94   static lgrp_children_func_t _lgrp_children;
95   static lgrp_resources_func_t _lgrp_resources;
96   static lgrp_nlgrps_func_t _lgrp_nlgrps;
97   static lgrp_cookie_stale_func_t _lgrp_cookie_stale;
98   static lgrp_cookie_t _lgrp_cookie;
99 
100   // Large Page Support
101   static bool is_valid_page_size(size_t bytes);
102   static size_t page_size_for_alignment(size_t alignment);
103   static bool setup_large_pages(caddr_t start, size_t bytes, size_t align);
104 
105   static void try_enable_extended_io();
106 
107   static struct sigaction *(*get_signal_action)(int);
108   static struct sigaction *get_preinstalled_handler(int);
109   static int (*get_libjsig_version)();
110   static void save_preinstalled_handler(int, struct sigaction&);
111   static void check_signal_handler(int sig);
112 
113   typedef int (*pthread_setname_np_func_t)(pthread_t, const char*);
114   static pthread_setname_np_func_t _pthread_setname_np;
115 
116  public:
117   // Large Page Support--ISM.
118   static bool largepage_range(char* addr, size_t size);
119 
120   static address handler_start, handler_end; // start and end pc of thr_sighndlrinfo
121 
122   static bool valid_stack_address(Thread* thread, address sp);
123   static bool valid_ucontext(Thread* thread, const ucontext_t* valid, const ucontext_t* suspect);
124   static const ucontext_t* get_valid_uc_in_signal_handler(Thread* thread,
125                                                     const ucontext_t* uc);
126 
127   static ExtendedPC  ucontext_get_ExtendedPC(const ucontext_t* uc);
128   static intptr_t*   ucontext_get_sp(const ucontext_t* uc);
129   // ucontext_get_fp() is only used by Solaris X86 (see note below)
130   static intptr_t*   ucontext_get_fp(const ucontext_t* uc);
131   static address    ucontext_get_pc(const ucontext_t* uc);
132   static void ucontext_set_pc(ucontext_t* uc, address pc);
133 
134   // For Analyzer Forte AsyncGetCallTrace profiling support:
135   // Parameter ret_fp is only used by Solaris X86.
136   //
137   // We should have different declarations of this interface in
138   // os_solaris_i486.hpp and os_solaris_sparc.hpp, but that file
139   // provides extensions to the os class and not the Solaris class.
140   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, const ucontext_t* uc,
141                                               intptr_t** ret_sp, intptr_t** ret_fp);
142 
143   static bool get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr);
144 
145   static void hotspot_sigmask(Thread* thread);
146 
147   // SR_handler
148   static void SR_handler(Thread* thread, ucontext_t* uc);
149 
150   static void init_thread_fpu_state(void);
151 
152  protected:
153   // Solaris-specific interface goes here
154   static julong available_memory();
physical_memory()155   static julong physical_memory() { return _physical_memory; }
156   static julong _physical_memory;
157   static void initialize_system_info();
158   static int _dev_zero_fd;
get_dev_zero_fd()159   static int get_dev_zero_fd() { return _dev_zero_fd; }
set_dev_zero_fd(int fd)160   static void set_dev_zero_fd(int fd) { _dev_zero_fd = fd; }
161   static int commit_memory_impl(char* addr, size_t bytes, bool exec);
162   static int commit_memory_impl(char* addr, size_t bytes,
163                                 size_t alignment_hint, bool exec);
164   static char* mmap_chunk(char *addr, size_t size, int flags, int prot);
165   static char* anon_mmap(char* requested_addr, size_t bytes, size_t alignment_hint, bool fixed);
166   static bool mpss_sanity_check(bool warn, size_t * page_size);
167 
168   // Workaround for 4352906. thr_stksegment sometimes returns
169   // a bad value for the primordial thread's stack base when
170   // it is called more than one time.
171   // Workaround is to cache the initial value to avoid further
172   // calls to thr_stksegment.
173   // It appears that someone (Hotspot?) is trashing the user's
174   // proc_t structure (note that this is a system struct).
175   static address _main_stack_base;
176 
177   static void print_distro_info(outputStream* st);
178   static void print_libversion_info(outputStream* st);
179 
180  public:
181   static void libthread_init();
182   static void synchronization_init();
183   static bool liblgrp_init();
184   // This boolean allows users to forward their own non-matching signals
185   // to JVM_handle_solaris_signal, harmlessly.
186   static bool signal_handlers_are_installed;
187 
188   static void signal_sets_init();
189   static void install_signal_handlers();
190   static void set_signal_handler(int sig, bool set_installed, bool oktochain);
191   static void init_signal_mem();
192   static void set_our_sigflags(int, int);
193   static int get_our_sigflags(int);
194 
195   // For signal-chaining
196   static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
197                                  // signal(), sigset() is loaded
198   static struct sigaction *get_chained_signal_action(int sig);
199   static bool chained_handler(int sig, siginfo_t *siginfo, void *context);
200 
201   // Allows us to switch between lwp and thread -based synchronization
mutex_lock(mutex_t * mx)202   static int mutex_lock(mutex_t *mx)    { return _mutex_lock(mx); }
mutex_trylock(mutex_t * mx)203   static int mutex_trylock(mutex_t *mx) { return _mutex_trylock(mx); }
mutex_unlock(mutex_t * mx)204   static int mutex_unlock(mutex_t *mx)  { return _mutex_unlock(mx); }
mutex_init(mutex_t * mx)205   static int mutex_init(mutex_t *mx)    { return _mutex_init(mx, os::Solaris::mutex_scope(), NULL); }
mutex_destroy(mutex_t * mx)206   static int mutex_destroy(mutex_t *mx) { return _mutex_destroy(mx); }
mutex_scope()207   static int mutex_scope()              { return _mutex_scope; }
208 
set_mutex_lock(int_fnP_mutex_tP func)209   static void set_mutex_lock(int_fnP_mutex_tP func)      { _mutex_lock = func; }
set_mutex_trylock(int_fnP_mutex_tP func)210   static void set_mutex_trylock(int_fnP_mutex_tP func)   { _mutex_trylock = func; }
set_mutex_unlock(int_fnP_mutex_tP func)211   static void set_mutex_unlock(int_fnP_mutex_tP func)    { _mutex_unlock = func; }
set_mutex_init(int_fnP_mutex_tP_i_vP func)212   static void set_mutex_init(int_fnP_mutex_tP_i_vP func) { _mutex_init = func; }
set_mutex_destroy(int_fnP_mutex_tP func)213   static void set_mutex_destroy(int_fnP_mutex_tP func)   { _mutex_destroy = func; }
set_mutex_scope(int scope)214   static void set_mutex_scope(int scope)                 { _mutex_scope = scope; }
215 
cond_timedwait(cond_t * cv,mutex_t * mx,timestruc_t * abst)216   static int cond_timedwait(cond_t *cv, mutex_t *mx, timestruc_t *abst) { return _cond_timedwait(cv, mx, abst); }
cond_wait(cond_t * cv,mutex_t * mx)217   static int cond_wait(cond_t *cv, mutex_t *mx) { return _cond_wait(cv, mx); }
cond_signal(cond_t * cv)218   static int cond_signal(cond_t *cv)            { return _cond_signal(cv); }
cond_broadcast(cond_t * cv)219   static int cond_broadcast(cond_t *cv)         { return _cond_broadcast(cv); }
cond_init(cond_t * cv)220   static int cond_init(cond_t *cv)              { return _cond_init(cv, os::Solaris::cond_scope(), NULL); }
cond_destroy(cond_t * cv)221   static int cond_destroy(cond_t *cv)           { return _cond_destroy(cv); }
cond_scope()222   static int cond_scope()                       { return _cond_scope; }
223 
set_cond_timedwait(int_fnP_cond_tP_mutex_tP_timestruc_tP func)224   static void set_cond_timedwait(int_fnP_cond_tP_mutex_tP_timestruc_tP func) { _cond_timedwait = func; }
set_cond_wait(int_fnP_cond_tP_mutex_tP func)225   static void set_cond_wait(int_fnP_cond_tP_mutex_tP func) { _cond_wait = func; }
set_cond_signal(int_fnP_cond_tP func)226   static void set_cond_signal(int_fnP_cond_tP func)        { _cond_signal = func; }
set_cond_broadcast(int_fnP_cond_tP func)227   static void set_cond_broadcast(int_fnP_cond_tP func)     { _cond_broadcast = func; }
set_cond_init(int_fnP_cond_tP_i_vP func)228   static void set_cond_init(int_fnP_cond_tP_i_vP func)     { _cond_init = func; }
set_cond_destroy(int_fnP_cond_tP func)229   static void set_cond_destroy(int_fnP_cond_tP func)       { _cond_destroy = func; }
set_cond_scope(int scope)230   static void set_cond_scope(int scope)                    { _cond_scope = scope; }
231 
synchronization_initialized()232   static bool synchronization_initialized()                { return _synchronization_initialized; }
233 
set_lgrp_home(lgrp_home_func_t func)234   static void set_lgrp_home(lgrp_home_func_t func) { _lgrp_home = func; }
set_lgrp_init(lgrp_init_func_t func)235   static void set_lgrp_init(lgrp_init_func_t func) { _lgrp_init = func; }
set_lgrp_fini(lgrp_fini_func_t func)236   static void set_lgrp_fini(lgrp_fini_func_t func) { _lgrp_fini = func; }
set_lgrp_root(lgrp_root_func_t func)237   static void set_lgrp_root(lgrp_root_func_t func) { _lgrp_root = func; }
set_lgrp_children(lgrp_children_func_t func)238   static void set_lgrp_children(lgrp_children_func_t func)   { _lgrp_children = func; }
set_lgrp_resources(lgrp_resources_func_t func)239   static void set_lgrp_resources(lgrp_resources_func_t func) { _lgrp_resources = func; }
set_lgrp_nlgrps(lgrp_nlgrps_func_t func)240   static void set_lgrp_nlgrps(lgrp_nlgrps_func_t func)       { _lgrp_nlgrps = func; }
set_lgrp_cookie_stale(lgrp_cookie_stale_func_t func)241   static void set_lgrp_cookie_stale(lgrp_cookie_stale_func_t func) { _lgrp_cookie_stale = func; }
set_lgrp_cookie(lgrp_cookie_t cookie)242   static void set_lgrp_cookie(lgrp_cookie_t cookie)  { _lgrp_cookie = cookie; }
243 
lgrp_home(idtype_t type,id_t id)244   static id_t lgrp_home(idtype_t type, id_t id)      { return _lgrp_home != NULL ? _lgrp_home(type, id) : -1; }
lgrp_init(lgrp_view_t view)245   static lgrp_cookie_t lgrp_init(lgrp_view_t view)   { return _lgrp_init != NULL ? _lgrp_init(view) : 0; }
lgrp_fini(lgrp_cookie_t cookie)246   static int lgrp_fini(lgrp_cookie_t cookie)         { return _lgrp_fini != NULL ? _lgrp_fini(cookie) : -1; }
lgrp_root(lgrp_cookie_t cookie)247   static lgrp_id_t lgrp_root(lgrp_cookie_t cookie)   { return _lgrp_root != NULL ? _lgrp_root(cookie) : -1; }
lgrp_children(lgrp_cookie_t cookie,lgrp_id_t parent,lgrp_id_t * lgrp_array,uint_t lgrp_array_size)248   static int lgrp_children(lgrp_cookie_t  cookie,  lgrp_id_t  parent,
249                            lgrp_id_t *lgrp_array, uint_t lgrp_array_size) {
250     return _lgrp_children != NULL ? _lgrp_children(cookie, parent, lgrp_array, lgrp_array_size) : -1;
251   }
lgrp_resources(lgrp_cookie_t cookie,lgrp_id_t lgrp,lgrp_id_t * lgrp_array,uint_t lgrp_array_size,lgrp_rsrc_t type)252   static int lgrp_resources(lgrp_cookie_t  cookie,  lgrp_id_t  lgrp,
253                             lgrp_id_t *lgrp_array, uint_t lgrp_array_size,
254                             lgrp_rsrc_t type) {
255     return _lgrp_resources != NULL ? _lgrp_resources(cookie, lgrp, lgrp_array, lgrp_array_size, type) : -1;
256   }
257 
lgrp_nlgrps(lgrp_cookie_t cookie)258   static int lgrp_nlgrps(lgrp_cookie_t cookie)       { return _lgrp_nlgrps != NULL ? _lgrp_nlgrps(cookie) : -1; }
lgrp_cookie_stale(lgrp_cookie_t cookie)259   static int lgrp_cookie_stale(lgrp_cookie_t cookie) {
260     return _lgrp_cookie_stale != NULL ? _lgrp_cookie_stale(cookie) : -1;
261   }
lgrp_cookie()262   static lgrp_cookie_t lgrp_cookie()                 { return _lgrp_cookie; }
263 
264   static sigset_t* unblocked_signals();
265   static sigset_t* vm_signals();
266 
267   // %%% Following should be promoted to os.hpp:
268   // Trace number of created threads
269   static          jint  _os_thread_limit;
270   static volatile jint  _os_thread_count;
271 
272   static void correct_stack_boundaries_for_primordial_thread(Thread* thr);
273 
274   // Stack repair handling
275 
276   // none present
277 
278 };
279 
280 class PlatformEvent : public CHeapObj<mtSynchronizer> {
281  private:
282   double CachePad[4];   // increase odds that _mutex is sole occupant of cache line
283   volatile int _Event;
284   int _nParked;
285   int _pipev[2];
286   mutex_t _mutex[1];
287   cond_t  _cond[1];
288   double PostPad[2];
289 
290  protected:
291   // Defining a protected ctor effectively gives us an abstract base class.
292   // That is, a PlatformEvent can never be instantiated "naked" but only
293   // as a part of a ParkEvent (recall that ParkEvent extends PlatformEvent).
294   // TODO-FIXME: make dtor private
~PlatformEvent()295   ~PlatformEvent() { guarantee(0, "invariant"); }
PlatformEvent()296   PlatformEvent() {
297     int status;
298     status = os::Solaris::cond_init(_cond);
299     assert_status(status == 0, status, "cond_init");
300     status = os::Solaris::mutex_init(_mutex);
301     assert_status(status == 0, status, "mutex_init");
302     _Event   = 0;
303     _nParked = 0;
304     _pipev[0] = _pipev[1] = -1;
305   }
306 
307  public:
308   // Exercise caution using reset() and fired() -- they may require MEMBARs
reset()309   void reset() { _Event = 0; }
fired()310   int  fired() { return _Event; }
311   void park();
312   int  park(jlong millis);
313   void unpark();
314 };
315 
316 class PlatformParker : public CHeapObj<mtSynchronizer> {
317  protected:
318   mutex_t _mutex[1];
319   cond_t  _cond[1];
320 
321  public:       // TODO-FIXME: make dtor private
~PlatformParker()322   ~PlatformParker() { guarantee(0, "invariant"); }
323 
324  public:
PlatformParker()325   PlatformParker() {
326     int status;
327     status = os::Solaris::cond_init(_cond);
328     assert_status(status == 0, status, "cond_init");
329     status = os::Solaris::mutex_init(_mutex);
330     assert_status(status == 0, status, "mutex_init");
331   }
332 };
333 
334 // Platform specific implementations that underpin VM Mutex/Monitor classes
335 
336 class PlatformMutex : public CHeapObj<mtSynchronizer> {
337   NONCOPYABLE(PlatformMutex);
338 
339  protected:
340   mutex_t _mutex; // Native mutex for locking
341 
342  public:
343   PlatformMutex();
344   ~PlatformMutex();
345   void lock();
346   void unlock();
347   bool try_lock();
348 };
349 
350 class PlatformMonitor : public PlatformMutex {
351  private:
352   cond_t  _cond;  // Native condition variable for blocking
353 
354   NONCOPYABLE(PlatformMonitor);
355 
356  public:
357   PlatformMonitor();
358   ~PlatformMonitor();
359   int wait(jlong millis);
360   void notify();
361   void notify_all();
362 };
363 
364 #endif // OS_SOLARIS_OS_SOLARIS_HPP
365