os_solaris.cpp revision 12160:43c36489d6fe
1/*
2 * Copyright (c) 1997, 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 *
23 */
24
25// no precompiled headers
26#include "classfile/classLoader.hpp"
27#include "classfile/systemDictionary.hpp"
28#include "classfile/vmSymbols.hpp"
29#include "code/icBuffer.hpp"
30#include "code/vtableStubs.hpp"
31#include "compiler/compileBroker.hpp"
32#include "compiler/disassembler.hpp"
33#include "interpreter/interpreter.hpp"
34#include "jvm_solaris.h"
35#include "logging/log.hpp"
36#include "memory/allocation.inline.hpp"
37#include "memory/filemap.hpp"
38#include "oops/oop.inline.hpp"
39#include "os_share_solaris.hpp"
40#include "os_solaris.inline.hpp"
41#include "prims/jniFastGetField.hpp"
42#include "prims/jvm.h"
43#include "prims/jvm_misc.hpp"
44#include "runtime/arguments.hpp"
45#include "runtime/atomic.hpp"
46#include "runtime/extendedPC.hpp"
47#include "runtime/globals.hpp"
48#include "runtime/interfaceSupport.hpp"
49#include "runtime/java.hpp"
50#include "runtime/javaCalls.hpp"
51#include "runtime/mutexLocker.hpp"
52#include "runtime/objectMonitor.hpp"
53#include "runtime/orderAccess.inline.hpp"
54#include "runtime/osThread.hpp"
55#include "runtime/perfMemory.hpp"
56#include "runtime/sharedRuntime.hpp"
57#include "runtime/statSampler.hpp"
58#include "runtime/stubRoutines.hpp"
59#include "runtime/thread.inline.hpp"
60#include "runtime/threadCritical.hpp"
61#include "runtime/timer.hpp"
62#include "runtime/vm_version.hpp"
63#include "semaphore_posix.hpp"
64#include "services/attachListener.hpp"
65#include "services/memTracker.hpp"
66#include "services/runtimeService.hpp"
67#include "utilities/decoder.hpp"
68#include "utilities/defaultStream.hpp"
69#include "utilities/events.hpp"
70#include "utilities/growableArray.hpp"
71#include "utilities/macros.hpp"
72#include "utilities/vmError.hpp"
73
74// put OS-includes here
75# include <dlfcn.h>
76# include <errno.h>
77# include <exception>
78# include <link.h>
79# include <poll.h>
80# include <pthread.h>
81# include <schedctl.h>
82# include <setjmp.h>
83# include <signal.h>
84# include <stdio.h>
85# include <alloca.h>
86# include <sys/filio.h>
87# include <sys/ipc.h>
88# include <sys/lwp.h>
89# include <sys/machelf.h>     // for elf Sym structure used by dladdr1
90# include <sys/mman.h>
91# include <sys/processor.h>
92# include <sys/procset.h>
93# include <sys/pset.h>
94# include <sys/resource.h>
95# include <sys/shm.h>
96# include <sys/socket.h>
97# include <sys/stat.h>
98# include <sys/systeminfo.h>
99# include <sys/time.h>
100# include <sys/times.h>
101# include <sys/types.h>
102# include <sys/wait.h>
103# include <sys/utsname.h>
104# include <thread.h>
105# include <unistd.h>
106# include <sys/priocntl.h>
107# include <sys/rtpriocntl.h>
108# include <sys/tspriocntl.h>
109# include <sys/iapriocntl.h>
110# include <sys/fxpriocntl.h>
111# include <sys/loadavg.h>
112# include <string.h>
113# include <stdio.h>
114
115# define _STRUCTURED_PROC 1  //  this gets us the new structured proc interfaces of 5.6 & later
116# include <sys/procfs.h>     //  see comment in <sys/procfs.h>
117
118#define MAX_PATH (2 * K)
119
120// for timer info max values which include all bits
121#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
122
123
124// Here are some liblgrp types from sys/lgrp_user.h to be able to
125// compile on older systems without this header file.
126
127#ifndef MADV_ACCESS_LWP
128  #define  MADV_ACCESS_LWP   7       /* next LWP to access heavily */
129#endif
130#ifndef MADV_ACCESS_MANY
131  #define  MADV_ACCESS_MANY  8       /* many processes to access heavily */
132#endif
133
134#ifndef LGRP_RSRC_CPU
135  #define LGRP_RSRC_CPU      0       /* CPU resources */
136#endif
137#ifndef LGRP_RSRC_MEM
138  #define LGRP_RSRC_MEM      1       /* memory resources */
139#endif
140
141// Values for ThreadPriorityPolicy == 1
142int prio_policy1[CriticalPriority+1] = {
143  -99999,  0, 16,  32,  48,  64,
144          80, 96, 112, 124, 127, 127 };
145
146// System parameters used internally
147static clock_t clock_tics_per_sec = 100;
148
149// Track if we have called enable_extended_FILE_stdio (on Solaris 10u4+)
150static bool enabled_extended_FILE_stdio = false;
151
152// For diagnostics to print a message once. see run_periodic_checks
153static bool check_addr0_done = false;
154static sigset_t check_signal_done;
155static bool check_signals = true;
156
157address os::Solaris::handler_start;  // start pc of thr_sighndlrinfo
158address os::Solaris::handler_end;    // end pc of thr_sighndlrinfo
159
160address os::Solaris::_main_stack_base = NULL;  // 4352906 workaround
161
162os::Solaris::pthread_setname_np_func_t os::Solaris::_pthread_setname_np = NULL;
163
164// "default" initializers for missing libc APIs
165extern "C" {
166  static int lwp_mutex_init(mutex_t *mx, int scope, void *arg) { memset(mx, 0, sizeof(mutex_t)); return 0; }
167  static int lwp_mutex_destroy(mutex_t *mx)                 { return 0; }
168
169  static int lwp_cond_init(cond_t *cv, int scope, void *arg){ memset(cv, 0, sizeof(cond_t)); return 0; }
170  static int lwp_cond_destroy(cond_t *cv)                   { return 0; }
171}
172
173// "default" initializers for pthread-based synchronization
174extern "C" {
175  static int pthread_mutex_default_init(mutex_t *mx, int scope, void *arg) { memset(mx, 0, sizeof(mutex_t)); return 0; }
176  static int pthread_cond_default_init(cond_t *cv, int scope, void *arg){ memset(cv, 0, sizeof(cond_t)); return 0; }
177}
178
179static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
180
181static inline size_t adjust_stack_size(address base, size_t size) {
182  if ((ssize_t)size < 0) {
183    // 4759953: Compensate for ridiculous stack size.
184    size = max_intx;
185  }
186  if (size > (size_t)base) {
187    // 4812466: Make sure size doesn't allow the stack to wrap the address space.
188    size = (size_t)base;
189  }
190  return size;
191}
192
193static inline stack_t get_stack_info() {
194  stack_t st;
195  int retval = thr_stksegment(&st);
196  st.ss_size = adjust_stack_size((address)st.ss_sp, st.ss_size);
197  assert(retval == 0, "incorrect return value from thr_stksegment");
198  assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
199  assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
200  return st;
201}
202
203address os::current_stack_base() {
204  int r = thr_main();
205  guarantee(r == 0 || r == 1, "CR6501650 or CR6493689");
206  bool is_primordial_thread = r;
207
208  // Workaround 4352906, avoid calls to thr_stksegment by
209  // thr_main after the first one (it looks like we trash
210  // some data, causing the value for ss_sp to be incorrect).
211  if (!is_primordial_thread || os::Solaris::_main_stack_base == NULL) {
212    stack_t st = get_stack_info();
213    if (is_primordial_thread) {
214      // cache initial value of stack base
215      os::Solaris::_main_stack_base = (address)st.ss_sp;
216    }
217    return (address)st.ss_sp;
218  } else {
219    guarantee(os::Solaris::_main_stack_base != NULL, "Attempt to use null cached stack base");
220    return os::Solaris::_main_stack_base;
221  }
222}
223
224size_t os::current_stack_size() {
225  size_t size;
226
227  int r = thr_main();
228  guarantee(r == 0 || r == 1, "CR6501650 or CR6493689");
229  if (!r) {
230    size = get_stack_info().ss_size;
231  } else {
232    struct rlimit limits;
233    getrlimit(RLIMIT_STACK, &limits);
234    size = adjust_stack_size(os::Solaris::_main_stack_base, (size_t)limits.rlim_cur);
235  }
236  // base may not be page aligned
237  address base = current_stack_base();
238  address bottom = (address)align_size_up((intptr_t)(base - size), os::vm_page_size());;
239  return (size_t)(base - bottom);
240}
241
242struct tm* os::localtime_pd(const time_t* clock, struct tm*  res) {
243  return localtime_r(clock, res);
244}
245
246void os::Solaris::try_enable_extended_io() {
247  typedef int (*enable_extended_FILE_stdio_t)(int, int);
248
249  if (!UseExtendedFileIO) {
250    return;
251  }
252
253  enable_extended_FILE_stdio_t enabler =
254    (enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT,
255                                         "enable_extended_FILE_stdio");
256  if (enabler) {
257    enabler(-1, -1);
258  }
259}
260
261static int _processors_online = 0;
262
263jint os::Solaris::_os_thread_limit = 0;
264volatile jint os::Solaris::_os_thread_count = 0;
265
266julong os::available_memory() {
267  return Solaris::available_memory();
268}
269
270julong os::Solaris::available_memory() {
271  return (julong)sysconf(_SC_AVPHYS_PAGES) * os::vm_page_size();
272}
273
274julong os::Solaris::_physical_memory = 0;
275
276julong os::physical_memory() {
277  return Solaris::physical_memory();
278}
279
280static hrtime_t first_hrtime = 0;
281static const hrtime_t hrtime_hz = 1000*1000*1000;
282static volatile hrtime_t max_hrtime = 0;
283
284
285void os::Solaris::initialize_system_info() {
286  set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
287  _processors_online = sysconf(_SC_NPROCESSORS_ONLN);
288  _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) *
289                                     (julong)sysconf(_SC_PAGESIZE);
290}
291
292int os::active_processor_count() {
293  int online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
294  pid_t pid = getpid();
295  psetid_t pset = PS_NONE;
296  // Are we running in a processor set or is there any processor set around?
297  if (pset_bind(PS_QUERY, P_PID, pid, &pset) == 0) {
298    uint_t pset_cpus;
299    // Query the number of cpus available to us.
300    if (pset_info(pset, NULL, &pset_cpus, NULL) == 0) {
301      assert(pset_cpus > 0 && pset_cpus <= online_cpus, "sanity check");
302      _processors_online = pset_cpus;
303      return pset_cpus;
304    }
305  }
306  // Otherwise return number of online cpus
307  return online_cpus;
308}
309
310static bool find_processors_in_pset(psetid_t        pset,
311                                    processorid_t** id_array,
312                                    uint_t*         id_length) {
313  bool result = false;
314  // Find the number of processors in the processor set.
315  if (pset_info(pset, NULL, id_length, NULL) == 0) {
316    // Make up an array to hold their ids.
317    *id_array = NEW_C_HEAP_ARRAY(processorid_t, *id_length, mtInternal);
318    // Fill in the array with their processor ids.
319    if (pset_info(pset, NULL, id_length, *id_array) == 0) {
320      result = true;
321    }
322  }
323  return result;
324}
325
326// Callers of find_processors_online() must tolerate imprecise results --
327// the system configuration can change asynchronously because of DR
328// or explicit psradm operations.
329//
330// We also need to take care that the loop (below) terminates as the
331// number of processors online can change between the _SC_NPROCESSORS_ONLN
332// request and the loop that builds the list of processor ids.   Unfortunately
333// there's no reliable way to determine the maximum valid processor id,
334// so we use a manifest constant, MAX_PROCESSOR_ID, instead.  See p_online
335// man pages, which claim the processor id set is "sparse, but
336// not too sparse".  MAX_PROCESSOR_ID is used to ensure that we eventually
337// exit the loop.
338//
339// In the future we'll be able to use sysconf(_SC_CPUID_MAX), but that's
340// not available on S8.0.
341
342static bool find_processors_online(processorid_t** id_array,
343                                   uint*           id_length) {
344  const processorid_t MAX_PROCESSOR_ID = 100000;
345  // Find the number of processors online.
346  *id_length = sysconf(_SC_NPROCESSORS_ONLN);
347  // Make up an array to hold their ids.
348  *id_array = NEW_C_HEAP_ARRAY(processorid_t, *id_length, mtInternal);
349  // Processors need not be numbered consecutively.
350  long found = 0;
351  processorid_t next = 0;
352  while (found < *id_length && next < MAX_PROCESSOR_ID) {
353    processor_info_t info;
354    if (processor_info(next, &info) == 0) {
355      // NB, PI_NOINTR processors are effectively online ...
356      if (info.pi_state == P_ONLINE || info.pi_state == P_NOINTR) {
357        (*id_array)[found] = next;
358        found += 1;
359      }
360    }
361    next += 1;
362  }
363  if (found < *id_length) {
364    // The loop above didn't identify the expected number of processors.
365    // We could always retry the operation, calling sysconf(_SC_NPROCESSORS_ONLN)
366    // and re-running the loop, above, but there's no guarantee of progress
367    // if the system configuration is in flux.  Instead, we just return what
368    // we've got.  Note that in the worst case find_processors_online() could
369    // return an empty set.  (As a fall-back in the case of the empty set we
370    // could just return the ID of the current processor).
371    *id_length = found;
372  }
373
374  return true;
375}
376
377static bool assign_distribution(processorid_t* id_array,
378                                uint           id_length,
379                                uint*          distribution,
380                                uint           distribution_length) {
381  // We assume we can assign processorid_t's to uint's.
382  assert(sizeof(processorid_t) == sizeof(uint),
383         "can't convert processorid_t to uint");
384  // Quick check to see if we won't succeed.
385  if (id_length < distribution_length) {
386    return false;
387  }
388  // Assign processor ids to the distribution.
389  // Try to shuffle processors to distribute work across boards,
390  // assuming 4 processors per board.
391  const uint processors_per_board = ProcessDistributionStride;
392  // Find the maximum processor id.
393  processorid_t max_id = 0;
394  for (uint m = 0; m < id_length; m += 1) {
395    max_id = MAX2(max_id, id_array[m]);
396  }
397  // The next id, to limit loops.
398  const processorid_t limit_id = max_id + 1;
399  // Make up markers for available processors.
400  bool* available_id = NEW_C_HEAP_ARRAY(bool, limit_id, mtInternal);
401  for (uint c = 0; c < limit_id; c += 1) {
402    available_id[c] = false;
403  }
404  for (uint a = 0; a < id_length; a += 1) {
405    available_id[id_array[a]] = true;
406  }
407  // Step by "boards", then by "slot", copying to "assigned".
408  // NEEDS_CLEANUP: The assignment of processors should be stateful,
409  //                remembering which processors have been assigned by
410  //                previous calls, etc., so as to distribute several
411  //                independent calls of this method.  What we'd like is
412  //                It would be nice to have an API that let us ask
413  //                how many processes are bound to a processor,
414  //                but we don't have that, either.
415  //                In the short term, "board" is static so that
416  //                subsequent distributions don't all start at board 0.
417  static uint board = 0;
418  uint assigned = 0;
419  // Until we've found enough processors ....
420  while (assigned < distribution_length) {
421    // ... find the next available processor in the board.
422    for (uint slot = 0; slot < processors_per_board; slot += 1) {
423      uint try_id = board * processors_per_board + slot;
424      if ((try_id < limit_id) && (available_id[try_id] == true)) {
425        distribution[assigned] = try_id;
426        available_id[try_id] = false;
427        assigned += 1;
428        break;
429      }
430    }
431    board += 1;
432    if (board * processors_per_board + 0 >= limit_id) {
433      board = 0;
434    }
435  }
436  if (available_id != NULL) {
437    FREE_C_HEAP_ARRAY(bool, available_id);
438  }
439  return true;
440}
441
442void os::set_native_thread_name(const char *name) {
443  if (Solaris::_pthread_setname_np != NULL) {
444    // Only the first 31 bytes of 'name' are processed by pthread_setname_np
445    // but we explicitly copy into a size-limited buffer to avoid any
446    // possible overflow.
447    char buf[32];
448    snprintf(buf, sizeof(buf), "%s", name);
449    buf[sizeof(buf) - 1] = '\0';
450    Solaris::_pthread_setname_np(pthread_self(), buf);
451  }
452}
453
454bool os::distribute_processes(uint length, uint* distribution) {
455  bool result = false;
456  // Find the processor id's of all the available CPUs.
457  processorid_t* id_array  = NULL;
458  uint           id_length = 0;
459  // There are some races between querying information and using it,
460  // since processor sets can change dynamically.
461  psetid_t pset = PS_NONE;
462  // Are we running in a processor set?
463  if ((pset_bind(PS_QUERY, P_PID, P_MYID, &pset) == 0) && pset != PS_NONE) {
464    result = find_processors_in_pset(pset, &id_array, &id_length);
465  } else {
466    result = find_processors_online(&id_array, &id_length);
467  }
468  if (result == true) {
469    if (id_length >= length) {
470      result = assign_distribution(id_array, id_length, distribution, length);
471    } else {
472      result = false;
473    }
474  }
475  if (id_array != NULL) {
476    FREE_C_HEAP_ARRAY(processorid_t, id_array);
477  }
478  return result;
479}
480
481bool os::bind_to_processor(uint processor_id) {
482  // We assume that a processorid_t can be stored in a uint.
483  assert(sizeof(uint) == sizeof(processorid_t),
484         "can't convert uint to processorid_t");
485  int bind_result =
486    processor_bind(P_LWPID,                       // bind LWP.
487                   P_MYID,                        // bind current LWP.
488                   (processorid_t) processor_id,  // id.
489                   NULL);                         // don't return old binding.
490  return (bind_result == 0);
491}
492
493// Return true if user is running as root.
494
495bool os::have_special_privileges() {
496  static bool init = false;
497  static bool privileges = false;
498  if (!init) {
499    privileges = (getuid() != geteuid()) || (getgid() != getegid());
500    init = true;
501  }
502  return privileges;
503}
504
505
506void os::init_system_properties_values() {
507  // The next steps are taken in the product version:
508  //
509  // Obtain the JAVA_HOME value from the location of libjvm.so.
510  // This library should be located at:
511  // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
512  //
513  // If "/jre/lib/" appears at the right place in the path, then we
514  // assume libjvm.so is installed in a JDK and we use this path.
515  //
516  // Otherwise exit with message: "Could not create the Java virtual machine."
517  //
518  // The following extra steps are taken in the debugging version:
519  //
520  // If "/jre/lib/" does NOT appear at the right place in the path
521  // instead of exit check for $JAVA_HOME environment variable.
522  //
523  // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
524  // then we append a fake suffix "hotspot/libjvm.so" to this path so
525  // it looks like libjvm.so is installed there
526  // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
527  //
528  // Otherwise exit.
529  //
530  // Important note: if the location of libjvm.so changes this
531  // code needs to be changed accordingly.
532
533// Base path of extensions installed on the system.
534#define SYS_EXT_DIR     "/usr/jdk/packages"
535#define EXTENSIONS_DIR  "/lib/ext"
536
537  char cpu_arch[12];
538  // Buffer that fits several sprintfs.
539  // Note that the space for the colon and the trailing null are provided
540  // by the nulls included by the sizeof operator.
541  const size_t bufsize =
542    MAX3((size_t)MAXPATHLEN,  // For dll_dir & friends.
543         sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch), // invariant ld_library_path
544         (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR)); // extensions dir
545  char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
546
547  // sysclasspath, java_home, dll_dir
548  {
549    char *pslash;
550    os::jvm_path(buf, bufsize);
551
552    // Found the full path to libjvm.so.
553    // Now cut the path to <java_home>/jre if we can.
554    *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.
555    pslash = strrchr(buf, '/');
556    if (pslash != NULL) {
557      *pslash = '\0';            // Get rid of /{client|server|hotspot}.
558    }
559    Arguments::set_dll_dir(buf);
560
561    if (pslash != NULL) {
562      pslash = strrchr(buf, '/');
563      if (pslash != NULL) {
564        *pslash = '\0';          // Get rid of /<arch>.
565        pslash = strrchr(buf, '/');
566        if (pslash != NULL) {
567          *pslash = '\0';        // Get rid of /lib.
568        }
569      }
570    }
571    Arguments::set_java_home(buf);
572    set_boot_path('/', ':');
573  }
574
575  // Where to look for native libraries.
576  {
577    // Use dlinfo() to determine the correct java.library.path.
578    //
579    // If we're launched by the Java launcher, and the user
580    // does not set java.library.path explicitly on the commandline,
581    // the Java launcher sets LD_LIBRARY_PATH for us and unsets
582    // LD_LIBRARY_PATH_32 and LD_LIBRARY_PATH_64.  In this case
583    // dlinfo returns LD_LIBRARY_PATH + crle settings (including
584    // /usr/lib), which is exactly what we want.
585    //
586    // If the user does set java.library.path, it completely
587    // overwrites this setting, and always has.
588    //
589    // If we're not launched by the Java launcher, we may
590    // get here with any/all of the LD_LIBRARY_PATH[_32|64]
591    // settings.  Again, dlinfo does exactly what we want.
592
593    Dl_serinfo     info_sz, *info = &info_sz;
594    Dl_serpath     *path;
595    char           *library_path;
596    char           *common_path = buf;
597
598    // Determine search path count and required buffer size.
599    if (dlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info) == -1) {
600      FREE_C_HEAP_ARRAY(char, buf);
601      vm_exit_during_initialization("dlinfo SERINFOSIZE request", dlerror());
602    }
603
604    // Allocate new buffer and initialize.
605    info = (Dl_serinfo*)NEW_C_HEAP_ARRAY(char, info_sz.dls_size, mtInternal);
606    info->dls_size = info_sz.dls_size;
607    info->dls_cnt = info_sz.dls_cnt;
608
609    // Obtain search path information.
610    if (dlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info) == -1) {
611      FREE_C_HEAP_ARRAY(char, buf);
612      FREE_C_HEAP_ARRAY(char, info);
613      vm_exit_during_initialization("dlinfo SERINFO request", dlerror());
614    }
615
616    path = &info->dls_serpath[0];
617
618    // Note: Due to a legacy implementation, most of the library path
619    // is set in the launcher. This was to accomodate linking restrictions
620    // on legacy Solaris implementations (which are no longer supported).
621    // Eventually, all the library path setting will be done here.
622    //
623    // However, to prevent the proliferation of improperly built native
624    // libraries, the new path component /usr/jdk/packages is added here.
625
626    // Determine the actual CPU architecture.
627    sysinfo(SI_ARCHITECTURE, cpu_arch, sizeof(cpu_arch));
628#ifdef _LP64
629    // If we are a 64-bit vm, perform the following translations:
630    //   sparc   -> sparcv9
631    //   i386    -> amd64
632    if (strcmp(cpu_arch, "sparc") == 0) {
633      strcat(cpu_arch, "v9");
634    } else if (strcmp(cpu_arch, "i386") == 0) {
635      strcpy(cpu_arch, "amd64");
636    }
637#endif
638
639    // Construct the invariant part of ld_library_path.
640    sprintf(common_path, SYS_EXT_DIR "/lib/%s", cpu_arch);
641
642    // Struct size is more than sufficient for the path components obtained
643    // through the dlinfo() call, so only add additional space for the path
644    // components explicitly added here.
645    size_t library_path_size = info->dls_size + strlen(common_path);
646    library_path = (char *)NEW_C_HEAP_ARRAY(char, library_path_size, mtInternal);
647    library_path[0] = '\0';
648
649    // Construct the desired Java library path from the linker's library
650    // search path.
651    //
652    // For compatibility, it is optimal that we insert the additional path
653    // components specific to the Java VM after those components specified
654    // in LD_LIBRARY_PATH (if any) but before those added by the ld.so
655    // infrastructure.
656    if (info->dls_cnt == 0) { // Not sure this can happen, but allow for it.
657      strcpy(library_path, common_path);
658    } else {
659      int inserted = 0;
660      int i;
661      for (i = 0; i < info->dls_cnt; i++, path++) {
662        uint_t flags = path->dls_flags & LA_SER_MASK;
663        if (((flags & LA_SER_LIBPATH) == 0) && !inserted) {
664          strcat(library_path, common_path);
665          strcat(library_path, os::path_separator());
666          inserted = 1;
667        }
668        strcat(library_path, path->dls_name);
669        strcat(library_path, os::path_separator());
670      }
671      // Eliminate trailing path separator.
672      library_path[strlen(library_path)-1] = '\0';
673    }
674
675    // happens before argument parsing - can't use a trace flag
676    // tty->print_raw("init_system_properties_values: native lib path: ");
677    // tty->print_raw_cr(library_path);
678
679    // Callee copies into its own buffer.
680    Arguments::set_library_path(library_path);
681
682    FREE_C_HEAP_ARRAY(char, library_path);
683    FREE_C_HEAP_ARRAY(char, info);
684  }
685
686  // Extensions directories.
687  sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
688  Arguments::set_ext_dirs(buf);
689
690  FREE_C_HEAP_ARRAY(char, buf);
691
692#undef SYS_EXT_DIR
693#undef EXTENSIONS_DIR
694}
695
696void os::breakpoint() {
697  BREAKPOINT;
698}
699
700bool os::obsolete_option(const JavaVMOption *option) {
701  if (!strncmp(option->optionString, "-Xt", 3)) {
702    return true;
703  } else if (!strncmp(option->optionString, "-Xtm", 4)) {
704    return true;
705  } else if (!strncmp(option->optionString, "-Xverifyheap", 12)) {
706    return true;
707  } else if (!strncmp(option->optionString, "-Xmaxjitcodesize", 16)) {
708    return true;
709  }
710  return false;
711}
712
713bool os::Solaris::valid_stack_address(Thread* thread, address sp) {
714  address  stackStart  = (address)thread->stack_base();
715  address  stackEnd    = (address)(stackStart - (address)thread->stack_size());
716  if (sp < stackStart && sp >= stackEnd) return true;
717  return false;
718}
719
720extern "C" void breakpoint() {
721  // use debugger to set breakpoint here
722}
723
724static thread_t main_thread;
725
726// Thread start routine for all newly created threads
727extern "C" void* thread_native_entry(void* thread_addr) {
728  // Try to randomize the cache line index of hot stack frames.
729  // This helps when threads of the same stack traces evict each other's
730  // cache lines. The threads can be either from the same JVM instance, or
731  // from different JVM instances. The benefit is especially true for
732  // processors with hyperthreading technology.
733  static int counter = 0;
734  int pid = os::current_process_id();
735  alloca(((pid ^ counter++) & 7) * 128);
736
737  int prio;
738  Thread* thread = (Thread*)thread_addr;
739
740  thread->initialize_thread_current();
741
742  OSThread* osthr = thread->osthread();
743
744  osthr->set_lwp_id(_lwp_self());  // Store lwp in case we are bound
745  thread->_schedctl = (void *) schedctl_init();
746
747  log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ").",
748    os::current_thread_id());
749
750  if (UseNUMA) {
751    int lgrp_id = os::numa_get_group_id();
752    if (lgrp_id != -1) {
753      thread->set_lgrp_id(lgrp_id);
754    }
755  }
756
757  // Our priority was set when we were created, and stored in the
758  // osthread, but couldn't be passed through to our LWP until now.
759  // So read back the priority and set it again.
760
761  if (osthr->thread_id() != -1) {
762    if (UseThreadPriorities) {
763      int prio = osthr->native_priority();
764      if (ThreadPriorityVerbose) {
765        tty->print_cr("Starting Thread " INTPTR_FORMAT ", LWP is "
766                      INTPTR_FORMAT ", setting priority: %d\n",
767                      osthr->thread_id(), osthr->lwp_id(), prio);
768      }
769      os::set_native_priority(thread, prio);
770    }
771  } else if (ThreadPriorityVerbose) {
772    warning("Can't set priority in _start routine, thread id hasn't been set\n");
773  }
774
775  assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
776
777  // initialize signal mask for this thread
778  os::Solaris::hotspot_sigmask(thread);
779
780  thread->run();
781
782  // One less thread is executing
783  // When the VMThread gets here, the main thread may have already exited
784  // which frees the CodeHeap containing the Atomic::dec code
785  if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
786    Atomic::dec(&os::Solaris::_os_thread_count);
787  }
788
789  log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ").", os::current_thread_id());
790
791  // If a thread has not deleted itself ("delete this") as part of its
792  // termination sequence, we have to ensure thread-local-storage is
793  // cleared before we actually terminate. No threads should ever be
794  // deleted asynchronously with respect to their termination.
795  if (Thread::current_or_null_safe() != NULL) {
796    assert(Thread::current_or_null_safe() == thread, "current thread is wrong");
797    thread->clear_thread_current();
798  }
799
800  if (UseDetachedThreads) {
801    thr_exit(NULL);
802    ShouldNotReachHere();
803  }
804  return NULL;
805}
806
807static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
808  // Allocate the OSThread object
809  OSThread* osthread = new OSThread(NULL, NULL);
810  if (osthread == NULL) return NULL;
811
812  // Store info on the Solaris thread into the OSThread
813  osthread->set_thread_id(thread_id);
814  osthread->set_lwp_id(_lwp_self());
815  thread->_schedctl = (void *) schedctl_init();
816
817  if (UseNUMA) {
818    int lgrp_id = os::numa_get_group_id();
819    if (lgrp_id != -1) {
820      thread->set_lgrp_id(lgrp_id);
821    }
822  }
823
824  if (ThreadPriorityVerbose) {
825    tty->print_cr("In create_os_thread, Thread " INTPTR_FORMAT ", LWP is " INTPTR_FORMAT "\n",
826                  osthread->thread_id(), osthread->lwp_id());
827  }
828
829  // Initial thread state is INITIALIZED, not SUSPENDED
830  osthread->set_state(INITIALIZED);
831
832  return osthread;
833}
834
835void os::Solaris::hotspot_sigmask(Thread* thread) {
836  //Save caller's signal mask
837  sigset_t sigmask;
838  pthread_sigmask(SIG_SETMASK, NULL, &sigmask);
839  OSThread *osthread = thread->osthread();
840  osthread->set_caller_sigmask(sigmask);
841
842  pthread_sigmask(SIG_UNBLOCK, os::Solaris::unblocked_signals(), NULL);
843  if (!ReduceSignalUsage) {
844    if (thread->is_VM_thread()) {
845      // Only the VM thread handles BREAK_SIGNAL ...
846      pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL);
847    } else {
848      // ... all other threads block BREAK_SIGNAL
849      assert(!sigismember(vm_signals(), SIGINT), "SIGINT should not be blocked");
850      pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);
851    }
852  }
853}
854
855bool os::create_attached_thread(JavaThread* thread) {
856#ifdef ASSERT
857  thread->verify_not_published();
858#endif
859  OSThread* osthread = create_os_thread(thread, thr_self());
860  if (osthread == NULL) {
861    return false;
862  }
863
864  // Initial thread state is RUNNABLE
865  osthread->set_state(RUNNABLE);
866  thread->set_osthread(osthread);
867
868  // initialize signal mask for this thread
869  // and save the caller's signal mask
870  os::Solaris::hotspot_sigmask(thread);
871
872  log_info(os, thread)("Thread attached (tid: " UINTX_FORMAT ").",
873    os::current_thread_id());
874
875  return true;
876}
877
878bool os::create_main_thread(JavaThread* thread) {
879#ifdef ASSERT
880  thread->verify_not_published();
881#endif
882  if (_starting_thread == NULL) {
883    _starting_thread = create_os_thread(thread, main_thread);
884    if (_starting_thread == NULL) {
885      return false;
886    }
887  }
888
889  // The primodial thread is runnable from the start
890  _starting_thread->set_state(RUNNABLE);
891
892  thread->set_osthread(_starting_thread);
893
894  // initialize signal mask for this thread
895  // and save the caller's signal mask
896  os::Solaris::hotspot_sigmask(thread);
897
898  return true;
899}
900
901// Helper function to trace thread attributes, similar to os::Posix::describe_pthread_attr()
902static char* describe_thr_create_attributes(char* buf, size_t buflen,
903                                            size_t stacksize, long flags) {
904  stringStream ss(buf, buflen);
905  ss.print("stacksize: " SIZE_FORMAT "k, ", stacksize / 1024);
906  ss.print("flags: ");
907  #define PRINT_FLAG(f) if (flags & f) ss.print( #f " ");
908  #define ALL(X) \
909    X(THR_SUSPENDED) \
910    X(THR_DETACHED) \
911    X(THR_BOUND) \
912    X(THR_NEW_LWP) \
913    X(THR_DAEMON)
914  ALL(PRINT_FLAG)
915  #undef ALL
916  #undef PRINT_FLAG
917  return buf;
918}
919
920// return default stack size for thr_type
921size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
922  // default stack size when not specified by caller is 1M (2M for LP64)
923  size_t s = (BytesPerWord >> 2) * K * K;
924  return s;
925}
926
927bool os::create_thread(Thread* thread, ThreadType thr_type,
928                       size_t req_stack_size) {
929  // Allocate the OSThread object
930  OSThread* osthread = new OSThread(NULL, NULL);
931  if (osthread == NULL) {
932    return false;
933  }
934
935  if (ThreadPriorityVerbose) {
936    char *thrtyp;
937    switch (thr_type) {
938    case vm_thread:
939      thrtyp = (char *)"vm";
940      break;
941    case cgc_thread:
942      thrtyp = (char *)"cgc";
943      break;
944    case pgc_thread:
945      thrtyp = (char *)"pgc";
946      break;
947    case java_thread:
948      thrtyp = (char *)"java";
949      break;
950    case compiler_thread:
951      thrtyp = (char *)"compiler";
952      break;
953    case watcher_thread:
954      thrtyp = (char *)"watcher";
955      break;
956    default:
957      thrtyp = (char *)"unknown";
958      break;
959    }
960    tty->print_cr("In create_thread, creating a %s thread\n", thrtyp);
961  }
962
963  // calculate stack size if it's not specified by caller
964  size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
965
966  // Initial state is ALLOCATED but not INITIALIZED
967  osthread->set_state(ALLOCATED);
968
969  if (os::Solaris::_os_thread_count > os::Solaris::_os_thread_limit) {
970    // We got lots of threads. Check if we still have some address space left.
971    // Need to be at least 5Mb of unreserved address space. We do check by
972    // trying to reserve some.
973    const size_t VirtualMemoryBangSize = 20*K*K;
974    char* mem = os::reserve_memory(VirtualMemoryBangSize);
975    if (mem == NULL) {
976      delete osthread;
977      return false;
978    } else {
979      // Release the memory again
980      os::release_memory(mem, VirtualMemoryBangSize);
981    }
982  }
983
984  // Setup osthread because the child thread may need it.
985  thread->set_osthread(osthread);
986
987  // Create the Solaris thread
988  thread_t tid = 0;
989  long     flags = (UseDetachedThreads ? THR_DETACHED : 0) | THR_SUSPENDED;
990  int      status;
991
992  // Mark that we don't have an lwp or thread id yet.
993  // In case we attempt to set the priority before the thread starts.
994  osthread->set_lwp_id(-1);
995  osthread->set_thread_id(-1);
996
997  status = thr_create(NULL, stack_size, thread_native_entry, thread, flags, &tid);
998
999  char buf[64];
1000  if (status == 0) {
1001    log_info(os, thread)("Thread started (tid: " UINTX_FORMAT ", attributes: %s). ",
1002      (uintx) tid, describe_thr_create_attributes(buf, sizeof(buf), stack_size, flags));
1003  } else {
1004    log_warning(os, thread)("Failed to start thread - thr_create failed (%s) for attributes: %s.",
1005      os::errno_name(status), describe_thr_create_attributes(buf, sizeof(buf), stack_size, flags));
1006  }
1007
1008  if (status != 0) {
1009    thread->set_osthread(NULL);
1010    // Need to clean up stuff we've allocated so far
1011    delete osthread;
1012    return false;
1013  }
1014
1015  Atomic::inc(&os::Solaris::_os_thread_count);
1016
1017  // Store info on the Solaris thread into the OSThread
1018  osthread->set_thread_id(tid);
1019
1020  // Remember that we created this thread so we can set priority on it
1021  osthread->set_vm_created();
1022
1023  // Most thread types will set an explicit priority before starting the thread,
1024  // but for those that don't we need a valid value to read back in thread_native_entry.
1025  osthread->set_native_priority(NormPriority);
1026
1027  // Initial thread state is INITIALIZED, not SUSPENDED
1028  osthread->set_state(INITIALIZED);
1029
1030  // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
1031  return true;
1032}
1033
1034// defined for >= Solaris 10. This allows builds on earlier versions
1035// of Solaris to take advantage of the newly reserved Solaris JVM signals.
1036// With SIGJVM1, SIGJVM2, ASYNC_SIGNAL is SIGJVM2. Previously INTERRUPT_SIGNAL
1037// was SIGJVM1.
1038//
1039#if !defined(SIGJVM1)
1040  #define SIGJVM1 39
1041  #define SIGJVM2 40
1042#endif
1043
1044debug_only(static bool signal_sets_initialized = false);
1045static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
1046
1047int os::Solaris::_SIGasync = ASYNC_SIGNAL;
1048
1049bool os::Solaris::is_sig_ignored(int sig) {
1050  struct sigaction oact;
1051  sigaction(sig, (struct sigaction*)NULL, &oact);
1052  void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oact.sa_sigaction)
1053                                 : CAST_FROM_FN_PTR(void*,  oact.sa_handler);
1054  if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) {
1055    return true;
1056  } else {
1057    return false;
1058  }
1059}
1060
1061// Note: SIGRTMIN is a macro that calls sysconf() so it will
1062// dynamically detect SIGRTMIN value for the system at runtime, not buildtime
1063static bool isJVM1available() {
1064  return SIGJVM1 < SIGRTMIN;
1065}
1066
1067void os::Solaris::signal_sets_init() {
1068  // Should also have an assertion stating we are still single-threaded.
1069  assert(!signal_sets_initialized, "Already initialized");
1070  // Fill in signals that are necessarily unblocked for all threads in
1071  // the VM. Currently, we unblock the following signals:
1072  // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden
1073  //                         by -Xrs (=ReduceSignalUsage));
1074  // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all
1075  // other threads. The "ReduceSignalUsage" boolean tells us not to alter
1076  // the dispositions or masks wrt these signals.
1077  // Programs embedding the VM that want to use the above signals for their
1078  // own purposes must, at this time, use the "-Xrs" option to prevent
1079  // interference with shutdown hooks and BREAK_SIGNAL thread dumping.
1080  // (See bug 4345157, and other related bugs).
1081  // In reality, though, unblocking these signals is really a nop, since
1082  // these signals are not blocked by default.
1083  sigemptyset(&unblocked_sigs);
1084  sigemptyset(&allowdebug_blocked_sigs);
1085  sigaddset(&unblocked_sigs, SIGILL);
1086  sigaddset(&unblocked_sigs, SIGSEGV);
1087  sigaddset(&unblocked_sigs, SIGBUS);
1088  sigaddset(&unblocked_sigs, SIGFPE);
1089
1090  // Always true on Solaris 10+
1091  guarantee(isJVM1available(), "SIGJVM1/2 missing!");
1092  os::Solaris::set_SIGasync(SIGJVM2);
1093
1094  sigaddset(&unblocked_sigs, os::Solaris::SIGasync());
1095
1096  if (!ReduceSignalUsage) {
1097    if (!os::Solaris::is_sig_ignored(SHUTDOWN1_SIGNAL)) {
1098      sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);
1099      sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL);
1100    }
1101    if (!os::Solaris::is_sig_ignored(SHUTDOWN2_SIGNAL)) {
1102      sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);
1103      sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL);
1104    }
1105    if (!os::Solaris::is_sig_ignored(SHUTDOWN3_SIGNAL)) {
1106      sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);
1107      sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL);
1108    }
1109  }
1110  // Fill in signals that are blocked by all but the VM thread.
1111  sigemptyset(&vm_sigs);
1112  if (!ReduceSignalUsage) {
1113    sigaddset(&vm_sigs, BREAK_SIGNAL);
1114  }
1115  debug_only(signal_sets_initialized = true);
1116
1117  // For diagnostics only used in run_periodic_checks
1118  sigemptyset(&check_signal_done);
1119}
1120
1121// These are signals that are unblocked while a thread is running Java.
1122// (For some reason, they get blocked by default.)
1123sigset_t* os::Solaris::unblocked_signals() {
1124  assert(signal_sets_initialized, "Not initialized");
1125  return &unblocked_sigs;
1126}
1127
1128// These are the signals that are blocked while a (non-VM) thread is
1129// running Java. Only the VM thread handles these signals.
1130sigset_t* os::Solaris::vm_signals() {
1131  assert(signal_sets_initialized, "Not initialized");
1132  return &vm_sigs;
1133}
1134
1135// These are signals that are blocked during cond_wait to allow debugger in
1136sigset_t* os::Solaris::allowdebug_blocked_signals() {
1137  assert(signal_sets_initialized, "Not initialized");
1138  return &allowdebug_blocked_sigs;
1139}
1140
1141
1142void _handle_uncaught_cxx_exception() {
1143  VMError::report_and_die("An uncaught C++ exception");
1144}
1145
1146
1147// First crack at OS-specific initialization, from inside the new thread.
1148void os::initialize_thread(Thread* thr) {
1149  int r = thr_main();
1150  guarantee(r == 0 || r == 1, "CR6501650 or CR6493689");
1151  if (r) {
1152    JavaThread* jt = (JavaThread *)thr;
1153    assert(jt != NULL, "Sanity check");
1154    size_t stack_size;
1155    address base = jt->stack_base();
1156    if (Arguments::created_by_java_launcher()) {
1157      // Use 2MB to allow for Solaris 7 64 bit mode.
1158      stack_size = JavaThread::stack_size_at_create() == 0
1159        ? 2048*K : JavaThread::stack_size_at_create();
1160
1161      // There are rare cases when we may have already used more than
1162      // the basic stack size allotment before this method is invoked.
1163      // Attempt to allow for a normally sized java_stack.
1164      size_t current_stack_offset = (size_t)(base - (address)&stack_size);
1165      stack_size += ReservedSpace::page_align_size_down(current_stack_offset);
1166    } else {
1167      // 6269555: If we were not created by a Java launcher, i.e. if we are
1168      // running embedded in a native application, treat the primordial thread
1169      // as much like a native attached thread as possible.  This means using
1170      // the current stack size from thr_stksegment(), unless it is too large
1171      // to reliably setup guard pages.  A reasonable max size is 8MB.
1172      size_t current_size = current_stack_size();
1173      // This should never happen, but just in case....
1174      if (current_size == 0) current_size = 2 * K * K;
1175      stack_size = current_size > (8 * K * K) ? (8 * K * K) : current_size;
1176    }
1177    address bottom = (address)align_size_up((intptr_t)(base - stack_size), os::vm_page_size());;
1178    stack_size = (size_t)(base - bottom);
1179
1180    assert(stack_size > 0, "Stack size calculation problem");
1181
1182    if (stack_size > jt->stack_size()) {
1183#ifndef PRODUCT
1184      struct rlimit limits;
1185      getrlimit(RLIMIT_STACK, &limits);
1186      size_t size = adjust_stack_size(base, (size_t)limits.rlim_cur);
1187      assert(size >= jt->stack_size(), "Stack size problem in main thread");
1188#endif
1189      tty->print_cr("Stack size of %d Kb exceeds current limit of %d Kb.\n"
1190                    "(Stack sizes are rounded up to a multiple of the system page size.)\n"
1191                    "See limit(1) to increase the stack size limit.",
1192                    stack_size / K, jt->stack_size() / K);
1193      vm_exit(1);
1194    }
1195    assert(jt->stack_size() >= stack_size,
1196           "Attempt to map more stack than was allocated");
1197    jt->set_stack_size(stack_size);
1198  }
1199
1200  // With the T2 libthread (T1 is no longer supported) threads are always bound
1201  // and we use stackbanging in all cases.
1202
1203  os::Solaris::init_thread_fpu_state();
1204  std::set_terminate(_handle_uncaught_cxx_exception);
1205}
1206
1207
1208
1209// Free Solaris resources related to the OSThread
1210void os::free_thread(OSThread* osthread) {
1211  assert(osthread != NULL, "os::free_thread but osthread not set");
1212
1213  // We are told to free resources of the argument thread,
1214  // but we can only really operate on the current thread.
1215  assert(Thread::current()->osthread() == osthread,
1216         "os::free_thread but not current thread");
1217
1218  // Restore caller's signal mask
1219  sigset_t sigmask = osthread->caller_sigmask();
1220  pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
1221
1222  delete osthread;
1223}
1224
1225void os::pd_start_thread(Thread* thread) {
1226  int status = thr_continue(thread->osthread()->thread_id());
1227  assert_status(status == 0, status, "thr_continue failed");
1228}
1229
1230
1231intx os::current_thread_id() {
1232  return (intx)thr_self();
1233}
1234
1235static pid_t _initial_pid = 0;
1236
1237int os::current_process_id() {
1238  return (int)(_initial_pid ? _initial_pid : getpid());
1239}
1240
1241// gethrtime() should be monotonic according to the documentation,
1242// but some virtualized platforms are known to break this guarantee.
1243// getTimeNanos() must be guaranteed not to move backwards, so we
1244// are forced to add a check here.
1245inline hrtime_t getTimeNanos() {
1246  const hrtime_t now = gethrtime();
1247  const hrtime_t prev = max_hrtime;
1248  if (now <= prev) {
1249    return prev;   // same or retrograde time;
1250  }
1251  const hrtime_t obsv = Atomic::cmpxchg(now, (volatile jlong*)&max_hrtime, prev);
1252  assert(obsv >= prev, "invariant");   // Monotonicity
1253  // If the CAS succeeded then we're done and return "now".
1254  // If the CAS failed and the observed value "obsv" is >= now then
1255  // we should return "obsv".  If the CAS failed and now > obsv > prv then
1256  // some other thread raced this thread and installed a new value, in which case
1257  // we could either (a) retry the entire operation, (b) retry trying to install now
1258  // or (c) just return obsv.  We use (c).   No loop is required although in some cases
1259  // we might discard a higher "now" value in deference to a slightly lower but freshly
1260  // installed obsv value.   That's entirely benign -- it admits no new orderings compared
1261  // to (a) or (b) -- and greatly reduces coherence traffic.
1262  // We might also condition (c) on the magnitude of the delta between obsv and now.
1263  // Avoiding excessive CAS operations to hot RW locations is critical.
1264  // See https://blogs.oracle.com/dave/entry/cas_and_cache_trivia_invalidate
1265  return (prev == obsv) ? now : obsv;
1266}
1267
1268// Time since start-up in seconds to a fine granularity.
1269// Used by VMSelfDestructTimer and the MemProfiler.
1270double os::elapsedTime() {
1271  return (double)(getTimeNanos() - first_hrtime) / (double)hrtime_hz;
1272}
1273
1274jlong os::elapsed_counter() {
1275  return (jlong)(getTimeNanos() - first_hrtime);
1276}
1277
1278jlong os::elapsed_frequency() {
1279  return hrtime_hz;
1280}
1281
1282// Return the real, user, and system times in seconds from an
1283// arbitrary fixed point in the past.
1284bool os::getTimesSecs(double* process_real_time,
1285                      double* process_user_time,
1286                      double* process_system_time) {
1287  struct tms ticks;
1288  clock_t real_ticks = times(&ticks);
1289
1290  if (real_ticks == (clock_t) (-1)) {
1291    return false;
1292  } else {
1293    double ticks_per_second = (double) clock_tics_per_sec;
1294    *process_user_time = ((double) ticks.tms_utime) / ticks_per_second;
1295    *process_system_time = ((double) ticks.tms_stime) / ticks_per_second;
1296    // For consistency return the real time from getTimeNanos()
1297    // converted to seconds.
1298    *process_real_time = ((double) getTimeNanos()) / ((double) NANOUNITS);
1299
1300    return true;
1301  }
1302}
1303
1304bool os::supports_vtime() { return true; }
1305bool os::enable_vtime() { return false; }
1306bool os::vtime_enabled() { return false; }
1307
1308double os::elapsedVTime() {
1309  return (double)gethrvtime() / (double)hrtime_hz;
1310}
1311
1312// Must return millis since Jan 1 1970 for JVM_CurrentTimeMillis
1313jlong os::javaTimeMillis() {
1314  timeval t;
1315  if (gettimeofday(&t, NULL) == -1) {
1316    fatal("os::javaTimeMillis: gettimeofday (%s)", os::strerror(errno));
1317  }
1318  return jlong(t.tv_sec) * 1000  +  jlong(t.tv_usec) / 1000;
1319}
1320
1321// Must return seconds+nanos since Jan 1 1970. This must use the same
1322// time source as javaTimeMillis and can't use get_nsec_fromepoch as
1323// we need better than 1ms accuracy
1324void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
1325  timeval t;
1326  if (gettimeofday(&t, NULL) == -1) {
1327    fatal("os::javaTimeSystemUTC: gettimeofday (%s)", os::strerror(errno));
1328  }
1329  seconds = jlong(t.tv_sec);
1330  nanos = jlong(t.tv_usec) * 1000;
1331}
1332
1333
1334jlong os::javaTimeNanos() {
1335  return (jlong)getTimeNanos();
1336}
1337
1338void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
1339  info_ptr->max_value = ALL_64_BITS;      // gethrtime() uses all 64 bits
1340  info_ptr->may_skip_backward = false;    // not subject to resetting or drifting
1341  info_ptr->may_skip_forward = false;     // not subject to resetting or drifting
1342  info_ptr->kind = JVMTI_TIMER_ELAPSED;   // elapsed not CPU time
1343}
1344
1345char * os::local_time_string(char *buf, size_t buflen) {
1346  struct tm t;
1347  time_t long_time;
1348  time(&long_time);
1349  localtime_r(&long_time, &t);
1350  jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
1351               t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
1352               t.tm_hour, t.tm_min, t.tm_sec);
1353  return buf;
1354}
1355
1356// Note: os::shutdown() might be called very early during initialization, or
1357// called from signal handler. Before adding something to os::shutdown(), make
1358// sure it is async-safe and can handle partially initialized VM.
1359void os::shutdown() {
1360
1361  // allow PerfMemory to attempt cleanup of any persistent resources
1362  perfMemory_exit();
1363
1364  // needs to remove object in file system
1365  AttachListener::abort();
1366
1367  // flush buffered output, finish log files
1368  ostream_abort();
1369
1370  // Check for abort hook
1371  abort_hook_t abort_hook = Arguments::abort_hook();
1372  if (abort_hook != NULL) {
1373    abort_hook();
1374  }
1375}
1376
1377// Note: os::abort() might be called very early during initialization, or
1378// called from signal handler. Before adding something to os::abort(), make
1379// sure it is async-safe and can handle partially initialized VM.
1380void os::abort(bool dump_core, void* siginfo, const void* context) {
1381  os::shutdown();
1382  if (dump_core) {
1383#ifndef PRODUCT
1384    fdStream out(defaultStream::output_fd());
1385    out.print_raw("Current thread is ");
1386    char buf[16];
1387    jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
1388    out.print_raw_cr(buf);
1389    out.print_raw_cr("Dumping core ...");
1390#endif
1391    ::abort(); // dump core (for debugging)
1392  }
1393
1394  ::exit(1);
1395}
1396
1397// Die immediately, no exit hook, no abort hook, no cleanup.
1398void os::die() {
1399  ::abort(); // dump core (for debugging)
1400}
1401
1402// DLL functions
1403
1404const char* os::dll_file_extension() { return ".so"; }
1405
1406// This must be hard coded because it's the system's temporary
1407// directory not the java application's temp directory, ala java.io.tmpdir.
1408const char* os::get_temp_directory() { return "/tmp"; }
1409
1410static bool file_exists(const char* filename) {
1411  struct stat statbuf;
1412  if (filename == NULL || strlen(filename) == 0) {
1413    return false;
1414  }
1415  return os::stat(filename, &statbuf) == 0;
1416}
1417
1418bool os::dll_build_name(char* buffer, size_t buflen,
1419                        const char* pname, const char* fname) {
1420  bool retval = false;
1421  const size_t pnamelen = pname ? strlen(pname) : 0;
1422
1423  // Return error on buffer overflow.
1424  if (pnamelen + strlen(fname) + 10 > (size_t) buflen) {
1425    return retval;
1426  }
1427
1428  if (pnamelen == 0) {
1429    snprintf(buffer, buflen, "lib%s.so", fname);
1430    retval = true;
1431  } else if (strchr(pname, *os::path_separator()) != NULL) {
1432    int n;
1433    char** pelements = split_path(pname, &n);
1434    if (pelements == NULL) {
1435      return false;
1436    }
1437    for (int i = 0; i < n; i++) {
1438      // really shouldn't be NULL but what the heck, check can't hurt
1439      if (pelements[i] == NULL || strlen(pelements[i]) == 0) {
1440        continue; // skip the empty path values
1441      }
1442      snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname);
1443      if (file_exists(buffer)) {
1444        retval = true;
1445        break;
1446      }
1447    }
1448    // release the storage
1449    for (int i = 0; i < n; i++) {
1450      if (pelements[i] != NULL) {
1451        FREE_C_HEAP_ARRAY(char, pelements[i]);
1452      }
1453    }
1454    if (pelements != NULL) {
1455      FREE_C_HEAP_ARRAY(char*, pelements);
1456    }
1457  } else {
1458    snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);
1459    retval = true;
1460  }
1461  return retval;
1462}
1463
1464// check if addr is inside libjvm.so
1465bool os::address_is_in_vm(address addr) {
1466  static address libjvm_base_addr;
1467  Dl_info dlinfo;
1468
1469  if (libjvm_base_addr == NULL) {
1470    if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {
1471      libjvm_base_addr = (address)dlinfo.dli_fbase;
1472    }
1473    assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1474  }
1475
1476  if (dladdr((void *)addr, &dlinfo) != 0) {
1477    if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1478  }
1479
1480  return false;
1481}
1482
1483typedef int (*dladdr1_func_type)(void *, Dl_info *, void **, int);
1484static dladdr1_func_type dladdr1_func = NULL;
1485
1486bool os::dll_address_to_function_name(address addr, char *buf,
1487                                      int buflen, int * offset,
1488                                      bool demangle) {
1489  // buf is not optional, but offset is optional
1490  assert(buf != NULL, "sanity check");
1491
1492  Dl_info dlinfo;
1493
1494  // dladdr1_func was initialized in os::init()
1495  if (dladdr1_func != NULL) {
1496    // yes, we have dladdr1
1497
1498    // Support for dladdr1 is checked at runtime; it may be
1499    // available even if the vm is built on a machine that does
1500    // not have dladdr1 support.  Make sure there is a value for
1501    // RTLD_DL_SYMENT.
1502#ifndef RTLD_DL_SYMENT
1503  #define RTLD_DL_SYMENT 1
1504#endif
1505#ifdef _LP64
1506    Elf64_Sym * info;
1507#else
1508    Elf32_Sym * info;
1509#endif
1510    if (dladdr1_func((void *)addr, &dlinfo, (void **)&info,
1511                     RTLD_DL_SYMENT) != 0) {
1512      // see if we have a matching symbol that covers our address
1513      if (dlinfo.dli_saddr != NULL &&
1514          (char *)dlinfo.dli_saddr + info->st_size > (char *)addr) {
1515        if (dlinfo.dli_sname != NULL) {
1516          if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
1517            jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1518          }
1519          if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1520          return true;
1521        }
1522      }
1523      // no matching symbol so try for just file info
1524      if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
1525        if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1526                            buf, buflen, offset, dlinfo.dli_fname, demangle)) {
1527          return true;
1528        }
1529      }
1530    }
1531    buf[0] = '\0';
1532    if (offset != NULL) *offset  = -1;
1533    return false;
1534  }
1535
1536  // no, only dladdr is available
1537  if (dladdr((void *)addr, &dlinfo) != 0) {
1538    // see if we have a matching symbol
1539    if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
1540      if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
1541        jio_snprintf(buf, buflen, dlinfo.dli_sname);
1542      }
1543      if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1544      return true;
1545    }
1546    // no matching symbol so try for just file info
1547    if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
1548      if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1549                          buf, buflen, offset, dlinfo.dli_fname, demangle)) {
1550        return true;
1551      }
1552    }
1553  }
1554  buf[0] = '\0';
1555  if (offset != NULL) *offset  = -1;
1556  return false;
1557}
1558
1559bool os::dll_address_to_library_name(address addr, char* buf,
1560                                     int buflen, int* offset) {
1561  // buf is not optional, but offset is optional
1562  assert(buf != NULL, "sanity check");
1563
1564  Dl_info dlinfo;
1565
1566  if (dladdr((void*)addr, &dlinfo) != 0) {
1567    if (dlinfo.dli_fname != NULL) {
1568      jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1569    }
1570    if (dlinfo.dli_fbase != NULL && offset != NULL) {
1571      *offset = addr - (address)dlinfo.dli_fbase;
1572    }
1573    return true;
1574  }
1575
1576  buf[0] = '\0';
1577  if (offset) *offset = -1;
1578  return false;
1579}
1580
1581int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
1582  Dl_info dli;
1583  // Sanity check?
1584  if (dladdr(CAST_FROM_FN_PTR(void *, os::get_loaded_modules_info), &dli) == 0 ||
1585      dli.dli_fname == NULL) {
1586    return 1;
1587  }
1588
1589  void * handle = dlopen(dli.dli_fname, RTLD_LAZY);
1590  if (handle == NULL) {
1591    return 1;
1592  }
1593
1594  Link_map *map;
1595  dlinfo(handle, RTLD_DI_LINKMAP, &map);
1596  if (map == NULL) {
1597    dlclose(handle);
1598    return 1;
1599  }
1600
1601  while (map->l_prev != NULL) {
1602    map = map->l_prev;
1603  }
1604
1605  while (map != NULL) {
1606    // Iterate through all map entries and call callback with fields of interest
1607    if(callback(map->l_name, (address)map->l_addr, (address)0, param)) {
1608      dlclose(handle);
1609      return 1;
1610    }
1611    map = map->l_next;
1612  }
1613
1614  dlclose(handle);
1615  return 0;
1616}
1617
1618int _print_dll_info_cb(const char * name, address base_address, address top_address, void * param) {
1619  outputStream * out = (outputStream *) param;
1620  out->print_cr(PTR_FORMAT " \t%s", base_address, name);
1621  return 0;
1622}
1623
1624void os::print_dll_info(outputStream * st) {
1625  st->print_cr("Dynamic libraries:"); st->flush();
1626  if (get_loaded_modules_info(_print_dll_info_cb, (void *)st)) {
1627    st->print_cr("Error: Cannot print dynamic libraries.");
1628  }
1629}
1630
1631// Loads .dll/.so and
1632// in case of error it checks if .dll/.so was built for the
1633// same architecture as Hotspot is running on
1634
1635void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
1636  void * result= ::dlopen(filename, RTLD_LAZY);
1637  if (result != NULL) {
1638    // Successful loading
1639    return result;
1640  }
1641
1642  Elf32_Ehdr elf_head;
1643
1644  // Read system error message into ebuf
1645  // It may or may not be overwritten below
1646  ::strncpy(ebuf, ::dlerror(), ebuflen-1);
1647  ebuf[ebuflen-1]='\0';
1648  int diag_msg_max_length=ebuflen-strlen(ebuf);
1649  char* diag_msg_buf=ebuf+strlen(ebuf);
1650
1651  if (diag_msg_max_length==0) {
1652    // No more space in ebuf for additional diagnostics message
1653    return NULL;
1654  }
1655
1656
1657  int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
1658
1659  if (file_descriptor < 0) {
1660    // Can't open library, report dlerror() message
1661    return NULL;
1662  }
1663
1664  bool failed_to_read_elf_head=
1665    (sizeof(elf_head)!=
1666     (::read(file_descriptor, &elf_head,sizeof(elf_head))));
1667
1668  ::close(file_descriptor);
1669  if (failed_to_read_elf_head) {
1670    // file i/o error - report dlerror() msg
1671    return NULL;
1672  }
1673
1674  typedef struct {
1675    Elf32_Half  code;         // Actual value as defined in elf.h
1676    Elf32_Half  compat_class; // Compatibility of archs at VM's sense
1677    char        elf_class;    // 32 or 64 bit
1678    char        endianess;    // MSB or LSB
1679    char*       name;         // String representation
1680  } arch_t;
1681
1682  static const arch_t arch_array[]={
1683    {EM_386,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1684    {EM_486,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1685    {EM_IA_64,       EM_IA_64,   ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
1686    {EM_X86_64,      EM_X86_64,  ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
1687    {EM_SPARC,       EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1688    {EM_SPARC32PLUS, EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1689    {EM_SPARCV9,     EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
1690    {EM_PPC,         EM_PPC,     ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
1691    {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
1692    {EM_ARM,         EM_ARM,     ELFCLASS32, ELFDATA2LSB, (char*)"ARM 32"}
1693  };
1694
1695#if  (defined IA32)
1696  static  Elf32_Half running_arch_code=EM_386;
1697#elif   (defined AMD64)
1698  static  Elf32_Half running_arch_code=EM_X86_64;
1699#elif  (defined IA64)
1700  static  Elf32_Half running_arch_code=EM_IA_64;
1701#elif  (defined __sparc) && (defined _LP64)
1702  static  Elf32_Half running_arch_code=EM_SPARCV9;
1703#elif  (defined __sparc) && (!defined _LP64)
1704  static  Elf32_Half running_arch_code=EM_SPARC;
1705#elif  (defined __powerpc64__)
1706  static  Elf32_Half running_arch_code=EM_PPC64;
1707#elif  (defined __powerpc__)
1708  static  Elf32_Half running_arch_code=EM_PPC;
1709#elif (defined ARM)
1710  static  Elf32_Half running_arch_code=EM_ARM;
1711#else
1712  #error Method os::dll_load requires that one of following is defined:\
1713       IA32, AMD64, IA64, __sparc, __powerpc__, ARM, ARM
1714#endif
1715
1716  // Identify compatability class for VM's architecture and library's architecture
1717  // Obtain string descriptions for architectures
1718
1719  arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
1720  int running_arch_index=-1;
1721
1722  for (unsigned int i=0; i < ARRAY_SIZE(arch_array); i++) {
1723    if (running_arch_code == arch_array[i].code) {
1724      running_arch_index    = i;
1725    }
1726    if (lib_arch.code == arch_array[i].code) {
1727      lib_arch.compat_class = arch_array[i].compat_class;
1728      lib_arch.name         = arch_array[i].name;
1729    }
1730  }
1731
1732  assert(running_arch_index != -1,
1733         "Didn't find running architecture code (running_arch_code) in arch_array");
1734  if (running_arch_index == -1) {
1735    // Even though running architecture detection failed
1736    // we may still continue with reporting dlerror() message
1737    return NULL;
1738  }
1739
1740  if (lib_arch.endianess != arch_array[running_arch_index].endianess) {
1741    ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");
1742    return NULL;
1743  }
1744
1745  if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
1746    ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");
1747    return NULL;
1748  }
1749
1750  if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
1751    if (lib_arch.name!=NULL) {
1752      ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1753                 " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
1754                 lib_arch.name, arch_array[running_arch_index].name);
1755    } else {
1756      ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1757                 " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
1758                 lib_arch.code,
1759                 arch_array[running_arch_index].name);
1760    }
1761  }
1762
1763  return NULL;
1764}
1765
1766void* os::dll_lookup(void* handle, const char* name) {
1767  return dlsym(handle, name);
1768}
1769
1770void* os::get_default_process_handle() {
1771  return (void*)::dlopen(NULL, RTLD_LAZY);
1772}
1773
1774int os::stat(const char *path, struct stat *sbuf) {
1775  char pathbuf[MAX_PATH];
1776  if (strlen(path) > MAX_PATH - 1) {
1777    errno = ENAMETOOLONG;
1778    return -1;
1779  }
1780  os::native_path(strcpy(pathbuf, path));
1781  return ::stat(pathbuf, sbuf);
1782}
1783
1784static inline time_t get_mtime(const char* filename) {
1785  struct stat st;
1786  int ret = os::stat(filename, &st);
1787  assert(ret == 0, "failed to stat() file '%s': %s", filename, strerror(errno));
1788  return st.st_mtime;
1789}
1790
1791int os::compare_file_modified_times(const char* file1, const char* file2) {
1792  time_t t1 = get_mtime(file1);
1793  time_t t2 = get_mtime(file2);
1794  return t1 - t2;
1795}
1796
1797static bool _print_ascii_file(const char* filename, outputStream* st) {
1798  int fd = ::open(filename, O_RDONLY);
1799  if (fd == -1) {
1800    return false;
1801  }
1802
1803  char buf[32];
1804  int bytes;
1805  while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
1806    st->print_raw(buf, bytes);
1807  }
1808
1809  ::close(fd);
1810
1811  return true;
1812}
1813
1814void os::print_os_info_brief(outputStream* st) {
1815  os::Solaris::print_distro_info(st);
1816
1817  os::Posix::print_uname_info(st);
1818
1819  os::Solaris::print_libversion_info(st);
1820}
1821
1822void os::print_os_info(outputStream* st) {
1823  st->print("OS:");
1824
1825  os::Solaris::print_distro_info(st);
1826
1827  os::Posix::print_uname_info(st);
1828
1829  os::Solaris::print_libversion_info(st);
1830
1831  os::Posix::print_rlimit_info(st);
1832
1833  os::Posix::print_load_average(st);
1834}
1835
1836void os::Solaris::print_distro_info(outputStream* st) {
1837  if (!_print_ascii_file("/etc/release", st)) {
1838    st->print("Solaris");
1839  }
1840  st->cr();
1841}
1842
1843void os::get_summary_os_info(char* buf, size_t buflen) {
1844  strncpy(buf, "Solaris", buflen);  // default to plain solaris
1845  FILE* fp = fopen("/etc/release", "r");
1846  if (fp != NULL) {
1847    char tmp[256];
1848    // Only get the first line and chop out everything but the os name.
1849    if (fgets(tmp, sizeof(tmp), fp)) {
1850      char* ptr = tmp;
1851      // skip past whitespace characters
1852      while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')) ptr++;
1853      if (*ptr != '\0') {
1854        char* nl = strchr(ptr, '\n');
1855        if (nl != NULL) *nl = '\0';
1856        strncpy(buf, ptr, buflen);
1857      }
1858    }
1859    fclose(fp);
1860  }
1861}
1862
1863void os::Solaris::print_libversion_info(outputStream* st) {
1864  st->print("  (T2 libthread)");
1865  st->cr();
1866}
1867
1868static bool check_addr0(outputStream* st) {
1869  jboolean status = false;
1870  const int read_chunk = 200;
1871  int ret = 0;
1872  int nmap = 0;
1873  int fd = ::open("/proc/self/map",O_RDONLY);
1874  if (fd >= 0) {
1875    prmap_t *p = NULL;
1876    char *mbuff = (char *) calloc(read_chunk, sizeof(prmap_t));
1877    if (NULL == mbuff) {
1878      ::close(fd);
1879      return status;
1880    }
1881    while ((ret = ::read(fd, mbuff, read_chunk*sizeof(prmap_t))) > 0) {
1882      //check if read() has not read partial data
1883      if( 0 != ret % sizeof(prmap_t)){
1884        break;
1885      }
1886      nmap = ret / sizeof(prmap_t);
1887      p = (prmap_t *)mbuff;
1888      for(int i = 0; i < nmap; i++){
1889        if (p->pr_vaddr == 0x0) {
1890          st->print("Warning: Address: " PTR_FORMAT ", Size: " SIZE_FORMAT "K, ",p->pr_vaddr, p->pr_size/1024);
1891          st->print("Mapped file: %s, ", p->pr_mapname[0] == '\0' ? "None" : p->pr_mapname);
1892          st->print("Access: ");
1893          st->print("%s",(p->pr_mflags & MA_READ)  ? "r" : "-");
1894          st->print("%s",(p->pr_mflags & MA_WRITE) ? "w" : "-");
1895          st->print("%s",(p->pr_mflags & MA_EXEC)  ? "x" : "-");
1896          st->cr();
1897          status = true;
1898        }
1899        p++;
1900      }
1901    }
1902    free(mbuff);
1903    ::close(fd);
1904  }
1905  return status;
1906}
1907
1908void os::get_summary_cpu_info(char* buf, size_t buflen) {
1909  // Get MHz with system call. We don't seem to already have this.
1910  processor_info_t stats;
1911  processorid_t id = getcpuid();
1912  int clock = 0;
1913  if (processor_info(id, &stats) != -1) {
1914    clock = stats.pi_clock;  // pi_processor_type isn't more informative than below
1915  }
1916#ifdef AMD64
1917  snprintf(buf, buflen, "x86 64 bit %d MHz", clock);
1918#else
1919  // must be sparc
1920  snprintf(buf, buflen, "Sparcv9 64 bit %d MHz", clock);
1921#endif
1922}
1923
1924void os::pd_print_cpu_info(outputStream* st, char* buf, size_t buflen) {
1925  // Nothing to do for now.
1926}
1927
1928void os::print_memory_info(outputStream* st) {
1929  st->print("Memory:");
1930  st->print(" %dk page", os::vm_page_size()>>10);
1931  st->print(", physical " UINT64_FORMAT "k", os::physical_memory()>>10);
1932  st->print("(" UINT64_FORMAT "k free)", os::available_memory() >> 10);
1933  st->cr();
1934  (void) check_addr0(st);
1935}
1936
1937// Moved from whole group, because we need them here for diagnostic
1938// prints.
1939#define OLDMAXSIGNUM 32
1940static int Maxsignum = 0;
1941static int *ourSigFlags = NULL;
1942
1943int os::Solaris::get_our_sigflags(int sig) {
1944  assert(ourSigFlags!=NULL, "signal data structure not initialized");
1945  assert(sig > 0 && sig < Maxsignum, "vm signal out of expected range");
1946  return ourSigFlags[sig];
1947}
1948
1949void os::Solaris::set_our_sigflags(int sig, int flags) {
1950  assert(ourSigFlags!=NULL, "signal data structure not initialized");
1951  assert(sig > 0 && sig < Maxsignum, "vm signal out of expected range");
1952  ourSigFlags[sig] = flags;
1953}
1954
1955
1956static const char* get_signal_handler_name(address handler,
1957                                           char* buf, int buflen) {
1958  int offset;
1959  bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);
1960  if (found) {
1961    // skip directory names
1962    const char *p1, *p2;
1963    p1 = buf;
1964    size_t len = strlen(os::file_separator());
1965    while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
1966    jio_snprintf(buf, buflen, "%s+0x%x", p1, offset);
1967  } else {
1968    jio_snprintf(buf, buflen, PTR_FORMAT, handler);
1969  }
1970  return buf;
1971}
1972
1973static void print_signal_handler(outputStream* st, int sig,
1974                                 char* buf, size_t buflen) {
1975  struct sigaction sa;
1976
1977  sigaction(sig, NULL, &sa);
1978
1979  st->print("%s: ", os::exception_name(sig, buf, buflen));
1980
1981  address handler = (sa.sa_flags & SA_SIGINFO)
1982                  ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
1983                  : CAST_FROM_FN_PTR(address, sa.sa_handler);
1984
1985  if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {
1986    st->print("SIG_DFL");
1987  } else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) {
1988    st->print("SIG_IGN");
1989  } else {
1990    st->print("[%s]", get_signal_handler_name(handler, buf, buflen));
1991  }
1992
1993  st->print(", sa_mask[0]=");
1994  os::Posix::print_signal_set_short(st, &sa.sa_mask);
1995
1996  address rh = VMError::get_resetted_sighandler(sig);
1997  // May be, handler was resetted by VMError?
1998  if (rh != NULL) {
1999    handler = rh;
2000    sa.sa_flags = VMError::get_resetted_sigflags(sig);
2001  }
2002
2003  st->print(", sa_flags=");
2004  os::Posix::print_sa_flags(st, sa.sa_flags);
2005
2006  // Check: is it our handler?
2007  if (handler == CAST_FROM_FN_PTR(address, signalHandler)) {
2008    // It is our signal handler
2009    // check for flags
2010    if (sa.sa_flags != os::Solaris::get_our_sigflags(sig)) {
2011      st->print(
2012                ", flags was changed from " PTR32_FORMAT ", consider using jsig library",
2013                os::Solaris::get_our_sigflags(sig));
2014    }
2015  }
2016  st->cr();
2017}
2018
2019void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
2020  st->print_cr("Signal Handlers:");
2021  print_signal_handler(st, SIGSEGV, buf, buflen);
2022  print_signal_handler(st, SIGBUS , buf, buflen);
2023  print_signal_handler(st, SIGFPE , buf, buflen);
2024  print_signal_handler(st, SIGPIPE, buf, buflen);
2025  print_signal_handler(st, SIGXFSZ, buf, buflen);
2026  print_signal_handler(st, SIGILL , buf, buflen);
2027  print_signal_handler(st, ASYNC_SIGNAL, buf, buflen);
2028  print_signal_handler(st, BREAK_SIGNAL, buf, buflen);
2029  print_signal_handler(st, SHUTDOWN1_SIGNAL , buf, buflen);
2030  print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);
2031  print_signal_handler(st, SHUTDOWN3_SIGNAL, buf, buflen);
2032  print_signal_handler(st, os::Solaris::SIGasync(), buf, buflen);
2033}
2034
2035static char saved_jvm_path[MAXPATHLEN] = { 0 };
2036
2037// Find the full path to the current module, libjvm.so
2038void os::jvm_path(char *buf, jint buflen) {
2039  // Error checking.
2040  if (buflen < MAXPATHLEN) {
2041    assert(false, "must use a large-enough buffer");
2042    buf[0] = '\0';
2043    return;
2044  }
2045  // Lazy resolve the path to current module.
2046  if (saved_jvm_path[0] != 0) {
2047    strcpy(buf, saved_jvm_path);
2048    return;
2049  }
2050
2051  Dl_info dlinfo;
2052  int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
2053  assert(ret != 0, "cannot locate libjvm");
2054  if (ret != 0 && dlinfo.dli_fname != NULL) {
2055    realpath((char *)dlinfo.dli_fname, buf);
2056  } else {
2057    buf[0] = '\0';
2058    return;
2059  }
2060
2061  if (Arguments::sun_java_launcher_is_altjvm()) {
2062    // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2063    // value for buf is "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".
2064    // If "/jre/lib/" appears at the right place in the string, then
2065    // assume we are installed in a JDK and we're done.  Otherwise, check
2066    // for a JAVA_HOME environment variable and fix up the path so it
2067    // looks like libjvm.so is installed there (append a fake suffix
2068    // hotspot/libjvm.so).
2069    const char *p = buf + strlen(buf) - 1;
2070    for (int count = 0; p > buf && count < 5; ++count) {
2071      for (--p; p > buf && *p != '/'; --p)
2072        /* empty */ ;
2073    }
2074
2075    if (strncmp(p, "/jre/lib/", 9) != 0) {
2076      // Look for JAVA_HOME in the environment.
2077      char* java_home_var = ::getenv("JAVA_HOME");
2078      if (java_home_var != NULL && java_home_var[0] != 0) {
2079        char cpu_arch[12];
2080        char* jrelib_p;
2081        int   len;
2082        sysinfo(SI_ARCHITECTURE, cpu_arch, sizeof(cpu_arch));
2083#ifdef _LP64
2084        // If we are on sparc running a 64-bit vm, look in jre/lib/sparcv9.
2085        if (strcmp(cpu_arch, "sparc") == 0) {
2086          strcat(cpu_arch, "v9");
2087        } else if (strcmp(cpu_arch, "i386") == 0) {
2088          strcpy(cpu_arch, "amd64");
2089        }
2090#endif
2091        // Check the current module name "libjvm.so".
2092        p = strrchr(buf, '/');
2093        assert(strstr(p, "/libjvm") == p, "invalid library name");
2094
2095        realpath(java_home_var, buf);
2096        // determine if this is a legacy image or modules image
2097        // modules image doesn't have "jre" subdirectory
2098        len = strlen(buf);
2099        assert(len < buflen, "Ran out of buffer space");
2100        jrelib_p = buf + len;
2101        snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);
2102        if (0 != access(buf, F_OK)) {
2103          snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);
2104        }
2105
2106        if (0 == access(buf, F_OK)) {
2107          // Use current module name "libjvm.so"
2108          len = strlen(buf);
2109          snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2110        } else {
2111          // Go back to path of .so
2112          realpath((char *)dlinfo.dli_fname, buf);
2113        }
2114      }
2115    }
2116  }
2117
2118  strncpy(saved_jvm_path, buf, MAXPATHLEN);
2119  saved_jvm_path[MAXPATHLEN - 1] = '\0';
2120}
2121
2122
2123void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2124  // no prefix required, not even "_"
2125}
2126
2127
2128void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2129  // no suffix required
2130}
2131
2132// This method is a copy of JDK's sysGetLastErrorString
2133// from src/solaris/hpi/src/system_md.c
2134
2135size_t os::lasterror(char *buf, size_t len) {
2136  if (errno == 0)  return 0;
2137
2138  const char *s = os::strerror(errno);
2139  size_t n = ::strlen(s);
2140  if (n >= len) {
2141    n = len - 1;
2142  }
2143  ::strncpy(buf, s, n);
2144  buf[n] = '\0';
2145  return n;
2146}
2147
2148
2149// sun.misc.Signal
2150
2151extern "C" {
2152  static void UserHandler(int sig, void *siginfo, void *context) {
2153    // Ctrl-C is pressed during error reporting, likely because the error
2154    // handler fails to abort. Let VM die immediately.
2155    if (sig == SIGINT && is_error_reported()) {
2156      os::die();
2157    }
2158
2159    os::signal_notify(sig);
2160    // We do not need to reinstate the signal handler each time...
2161  }
2162}
2163
2164void* os::user_handler() {
2165  return CAST_FROM_FN_PTR(void*, UserHandler);
2166}
2167
2168struct timespec PosixSemaphore::create_timespec(unsigned int sec, int nsec) {
2169  struct timespec ts;
2170  unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2171
2172  return ts;
2173}
2174
2175extern "C" {
2176  typedef void (*sa_handler_t)(int);
2177  typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2178}
2179
2180void* os::signal(int signal_number, void* handler) {
2181  struct sigaction sigAct, oldSigAct;
2182  sigfillset(&(sigAct.sa_mask));
2183  sigAct.sa_flags = SA_RESTART & ~SA_RESETHAND;
2184  sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2185
2186  if (sigaction(signal_number, &sigAct, &oldSigAct)) {
2187    // -1 means registration failed
2188    return (void *)-1;
2189  }
2190
2191  return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2192}
2193
2194void os::signal_raise(int signal_number) {
2195  raise(signal_number);
2196}
2197
2198// The following code is moved from os.cpp for making this
2199// code platform specific, which it is by its very nature.
2200
2201// a counter for each possible signal value
2202static int Sigexit = 0;
2203static int Maxlibjsigsigs;
2204static jint *pending_signals = NULL;
2205static int *preinstalled_sigs = NULL;
2206static struct sigaction *chainedsigactions = NULL;
2207static sema_t sig_sem;
2208typedef int (*version_getting_t)();
2209version_getting_t os::Solaris::get_libjsig_version = NULL;
2210static int libjsigversion = NULL;
2211
2212int os::sigexitnum_pd() {
2213  assert(Sigexit > 0, "signal memory not yet initialized");
2214  return Sigexit;
2215}
2216
2217void os::Solaris::init_signal_mem() {
2218  // Initialize signal structures
2219  Maxsignum = SIGRTMAX;
2220  Sigexit = Maxsignum+1;
2221  assert(Maxsignum >0, "Unable to obtain max signal number");
2222
2223  Maxlibjsigsigs = Maxsignum;
2224
2225  // pending_signals has one int per signal
2226  // The additional signal is for SIGEXIT - exit signal to signal_thread
2227  pending_signals = (jint *)os::malloc(sizeof(jint) * (Sigexit+1), mtInternal);
2228  memset(pending_signals, 0, (sizeof(jint) * (Sigexit+1)));
2229
2230  if (UseSignalChaining) {
2231    chainedsigactions = (struct sigaction *)malloc(sizeof(struct sigaction)
2232                                                   * (Maxsignum + 1), mtInternal);
2233    memset(chainedsigactions, 0, (sizeof(struct sigaction) * (Maxsignum + 1)));
2234    preinstalled_sigs = (int *)os::malloc(sizeof(int) * (Maxsignum + 1), mtInternal);
2235    memset(preinstalled_sigs, 0, (sizeof(int) * (Maxsignum + 1)));
2236  }
2237  ourSigFlags = (int*)malloc(sizeof(int) * (Maxsignum + 1), mtInternal);
2238  memset(ourSigFlags, 0, sizeof(int) * (Maxsignum + 1));
2239}
2240
2241void os::signal_init_pd() {
2242  int ret;
2243
2244  ret = ::sema_init(&sig_sem, 0, NULL, NULL);
2245  assert(ret == 0, "sema_init() failed");
2246}
2247
2248void os::signal_notify(int signal_number) {
2249  int ret;
2250
2251  Atomic::inc(&pending_signals[signal_number]);
2252  ret = ::sema_post(&sig_sem);
2253  assert(ret == 0, "sema_post() failed");
2254}
2255
2256static int check_pending_signals(bool wait_for_signal) {
2257  int ret;
2258  while (true) {
2259    for (int i = 0; i < Sigexit + 1; i++) {
2260      jint n = pending_signals[i];
2261      if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
2262        return i;
2263      }
2264    }
2265    if (!wait_for_signal) {
2266      return -1;
2267    }
2268    JavaThread *thread = JavaThread::current();
2269    ThreadBlockInVM tbivm(thread);
2270
2271    bool threadIsSuspended;
2272    do {
2273      thread->set_suspend_equivalent();
2274      // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2275      while ((ret = ::sema_wait(&sig_sem)) == EINTR)
2276        ;
2277      assert(ret == 0, "sema_wait() failed");
2278
2279      // were we externally suspended while we were waiting?
2280      threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
2281      if (threadIsSuspended) {
2282        // The semaphore has been incremented, but while we were waiting
2283        // another thread suspended us. We don't want to continue running
2284        // while suspended because that would surprise the thread that
2285        // suspended us.
2286        ret = ::sema_post(&sig_sem);
2287        assert(ret == 0, "sema_post() failed");
2288
2289        thread->java_suspend_self();
2290      }
2291    } while (threadIsSuspended);
2292  }
2293}
2294
2295int os::signal_lookup() {
2296  return check_pending_signals(false);
2297}
2298
2299int os::signal_wait() {
2300  return check_pending_signals(true);
2301}
2302
2303////////////////////////////////////////////////////////////////////////////////
2304// Virtual Memory
2305
2306static int page_size = -1;
2307
2308// The mmap MAP_ALIGN flag is supported on Solaris 9 and later.  init_2() will
2309// clear this var if support is not available.
2310static bool has_map_align = true;
2311
2312int os::vm_page_size() {
2313  assert(page_size != -1, "must call os::init");
2314  return page_size;
2315}
2316
2317// Solaris allocates memory by pages.
2318int os::vm_allocation_granularity() {
2319  assert(page_size != -1, "must call os::init");
2320  return page_size;
2321}
2322
2323static bool recoverable_mmap_error(int err) {
2324  // See if the error is one we can let the caller handle. This
2325  // list of errno values comes from the Solaris mmap(2) man page.
2326  switch (err) {
2327  case EBADF:
2328  case EINVAL:
2329  case ENOTSUP:
2330    // let the caller deal with these errors
2331    return true;
2332
2333  default:
2334    // Any remaining errors on this OS can cause our reserved mapping
2335    // to be lost. That can cause confusion where different data
2336    // structures think they have the same memory mapped. The worst
2337    // scenario is if both the VM and a library think they have the
2338    // same memory mapped.
2339    return false;
2340  }
2341}
2342
2343static void warn_fail_commit_memory(char* addr, size_t bytes, bool exec,
2344                                    int err) {
2345  warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
2346          ", %d) failed; error='%s' (errno=%d)", addr, bytes, exec,
2347          os::strerror(err), err);
2348}
2349
2350static void warn_fail_commit_memory(char* addr, size_t bytes,
2351                                    size_t alignment_hint, bool exec,
2352                                    int err) {
2353  warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
2354          ", " SIZE_FORMAT ", %d) failed; error='%s' (errno=%d)", addr, bytes,
2355          alignment_hint, exec, os::strerror(err), err);
2356}
2357
2358int os::Solaris::commit_memory_impl(char* addr, size_t bytes, bool exec) {
2359  int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
2360  size_t size = bytes;
2361  char *res = Solaris::mmap_chunk(addr, size, MAP_PRIVATE|MAP_FIXED, prot);
2362  if (res != NULL) {
2363    if (UseNUMAInterleaving) {
2364      numa_make_global(addr, bytes);
2365    }
2366    return 0;
2367  }
2368
2369  int err = errno;  // save errno from mmap() call in mmap_chunk()
2370
2371  if (!recoverable_mmap_error(err)) {
2372    warn_fail_commit_memory(addr, bytes, exec, err);
2373    vm_exit_out_of_memory(bytes, OOM_MMAP_ERROR, "committing reserved memory.");
2374  }
2375
2376  return err;
2377}
2378
2379bool os::pd_commit_memory(char* addr, size_t bytes, bool exec) {
2380  return Solaris::commit_memory_impl(addr, bytes, exec) == 0;
2381}
2382
2383void os::pd_commit_memory_or_exit(char* addr, size_t bytes, bool exec,
2384                                  const char* mesg) {
2385  assert(mesg != NULL, "mesg must be specified");
2386  int err = os::Solaris::commit_memory_impl(addr, bytes, exec);
2387  if (err != 0) {
2388    // the caller wants all commit errors to exit with the specified mesg:
2389    warn_fail_commit_memory(addr, bytes, exec, err);
2390    vm_exit_out_of_memory(bytes, OOM_MMAP_ERROR, "%s", mesg);
2391  }
2392}
2393
2394size_t os::Solaris::page_size_for_alignment(size_t alignment) {
2395  assert(is_size_aligned(alignment, (size_t) vm_page_size()),
2396         SIZE_FORMAT " is not aligned to " SIZE_FORMAT,
2397         alignment, (size_t) vm_page_size());
2398
2399  for (int i = 0; _page_sizes[i] != 0; i++) {
2400    if (is_size_aligned(alignment, _page_sizes[i])) {
2401      return _page_sizes[i];
2402    }
2403  }
2404
2405  return (size_t) vm_page_size();
2406}
2407
2408int os::Solaris::commit_memory_impl(char* addr, size_t bytes,
2409                                    size_t alignment_hint, bool exec) {
2410  int err = Solaris::commit_memory_impl(addr, bytes, exec);
2411  if (err == 0 && UseLargePages && alignment_hint > 0) {
2412    assert(is_size_aligned(bytes, alignment_hint),
2413           SIZE_FORMAT " is not aligned to " SIZE_FORMAT, bytes, alignment_hint);
2414
2415    // The syscall memcntl requires an exact page size (see man memcntl for details).
2416    size_t page_size = page_size_for_alignment(alignment_hint);
2417    if (page_size > (size_t) vm_page_size()) {
2418      (void)Solaris::setup_large_pages(addr, bytes, page_size);
2419    }
2420  }
2421  return err;
2422}
2423
2424bool os::pd_commit_memory(char* addr, size_t bytes, size_t alignment_hint,
2425                          bool exec) {
2426  return Solaris::commit_memory_impl(addr, bytes, alignment_hint, exec) == 0;
2427}
2428
2429void os::pd_commit_memory_or_exit(char* addr, size_t bytes,
2430                                  size_t alignment_hint, bool exec,
2431                                  const char* mesg) {
2432  assert(mesg != NULL, "mesg must be specified");
2433  int err = os::Solaris::commit_memory_impl(addr, bytes, alignment_hint, exec);
2434  if (err != 0) {
2435    // the caller wants all commit errors to exit with the specified mesg:
2436    warn_fail_commit_memory(addr, bytes, alignment_hint, exec, err);
2437    vm_exit_out_of_memory(bytes, OOM_MMAP_ERROR, "%s", mesg);
2438  }
2439}
2440
2441// Uncommit the pages in a specified region.
2442void os::pd_free_memory(char* addr, size_t bytes, size_t alignment_hint) {
2443  if (madvise(addr, bytes, MADV_FREE) < 0) {
2444    debug_only(warning("MADV_FREE failed."));
2445    return;
2446  }
2447}
2448
2449bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
2450  return os::commit_memory(addr, size, !ExecMem);
2451}
2452
2453bool os::remove_stack_guard_pages(char* addr, size_t size) {
2454  return os::uncommit_memory(addr, size);
2455}
2456
2457// Change the page size in a given range.
2458void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2459  assert((intptr_t)addr % alignment_hint == 0, "Address should be aligned.");
2460  assert((intptr_t)(addr + bytes) % alignment_hint == 0, "End should be aligned.");
2461  if (UseLargePages) {
2462    size_t page_size = Solaris::page_size_for_alignment(alignment_hint);
2463    if (page_size > (size_t) vm_page_size()) {
2464      Solaris::setup_large_pages(addr, bytes, page_size);
2465    }
2466  }
2467}
2468
2469// Tell the OS to make the range local to the first-touching LWP
2470void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
2471  assert((intptr_t)addr % os::vm_page_size() == 0, "Address should be page-aligned.");
2472  if (madvise(addr, bytes, MADV_ACCESS_LWP) < 0) {
2473    debug_only(warning("MADV_ACCESS_LWP failed."));
2474  }
2475}
2476
2477// Tell the OS that this range would be accessed from different LWPs.
2478void os::numa_make_global(char *addr, size_t bytes) {
2479  assert((intptr_t)addr % os::vm_page_size() == 0, "Address should be page-aligned.");
2480  if (madvise(addr, bytes, MADV_ACCESS_MANY) < 0) {
2481    debug_only(warning("MADV_ACCESS_MANY failed."));
2482  }
2483}
2484
2485// Get the number of the locality groups.
2486size_t os::numa_get_groups_num() {
2487  size_t n = Solaris::lgrp_nlgrps(Solaris::lgrp_cookie());
2488  return n != -1 ? n : 1;
2489}
2490
2491// Get a list of leaf locality groups. A leaf lgroup is group that
2492// doesn't have any children. Typical leaf group is a CPU or a CPU/memory
2493// board. An LWP is assigned to one of these groups upon creation.
2494size_t os::numa_get_leaf_groups(int *ids, size_t size) {
2495  if ((ids[0] = Solaris::lgrp_root(Solaris::lgrp_cookie())) == -1) {
2496    ids[0] = 0;
2497    return 1;
2498  }
2499  int result_size = 0, top = 1, bottom = 0, cur = 0;
2500  for (int k = 0; k < size; k++) {
2501    int r = Solaris::lgrp_children(Solaris::lgrp_cookie(), ids[cur],
2502                                   (Solaris::lgrp_id_t*)&ids[top], size - top);
2503    if (r == -1) {
2504      ids[0] = 0;
2505      return 1;
2506    }
2507    if (!r) {
2508      // That's a leaf node.
2509      assert(bottom <= cur, "Sanity check");
2510      // Check if the node has memory
2511      if (Solaris::lgrp_resources(Solaris::lgrp_cookie(), ids[cur],
2512                                  NULL, 0, LGRP_RSRC_MEM) > 0) {
2513        ids[bottom++] = ids[cur];
2514      }
2515    }
2516    top += r;
2517    cur++;
2518  }
2519  if (bottom == 0) {
2520    // Handle a situation, when the OS reports no memory available.
2521    // Assume UMA architecture.
2522    ids[0] = 0;
2523    return 1;
2524  }
2525  return bottom;
2526}
2527
2528// Detect the topology change. Typically happens during CPU plugging-unplugging.
2529bool os::numa_topology_changed() {
2530  int is_stale = Solaris::lgrp_cookie_stale(Solaris::lgrp_cookie());
2531  if (is_stale != -1 && is_stale) {
2532    Solaris::lgrp_fini(Solaris::lgrp_cookie());
2533    Solaris::lgrp_cookie_t c = Solaris::lgrp_init(Solaris::LGRP_VIEW_CALLER);
2534    assert(c != 0, "Failure to initialize LGRP API");
2535    Solaris::set_lgrp_cookie(c);
2536    return true;
2537  }
2538  return false;
2539}
2540
2541// Get the group id of the current LWP.
2542int os::numa_get_group_id() {
2543  int lgrp_id = Solaris::lgrp_home(P_LWPID, P_MYID);
2544  if (lgrp_id == -1) {
2545    return 0;
2546  }
2547  const int size = os::numa_get_groups_num();
2548  int *ids = (int*)alloca(size * sizeof(int));
2549
2550  // Get the ids of all lgroups with memory; r is the count.
2551  int r = Solaris::lgrp_resources(Solaris::lgrp_cookie(), lgrp_id,
2552                                  (Solaris::lgrp_id_t*)ids, size, LGRP_RSRC_MEM);
2553  if (r <= 0) {
2554    return 0;
2555  }
2556  return ids[os::random() % r];
2557}
2558
2559// Request information about the page.
2560bool os::get_page_info(char *start, page_info* info) {
2561  const uint_t info_types[] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
2562  uint64_t addr = (uintptr_t)start;
2563  uint64_t outdata[2];
2564  uint_t validity = 0;
2565
2566  if (meminfo(&addr, 1, info_types, 2, outdata, &validity) < 0) {
2567    return false;
2568  }
2569
2570  info->size = 0;
2571  info->lgrp_id = -1;
2572
2573  if ((validity & 1) != 0) {
2574    if ((validity & 2) != 0) {
2575      info->lgrp_id = outdata[0];
2576    }
2577    if ((validity & 4) != 0) {
2578      info->size = outdata[1];
2579    }
2580    return true;
2581  }
2582  return false;
2583}
2584
2585// Scan the pages from start to end until a page different than
2586// the one described in the info parameter is encountered.
2587char *os::scan_pages(char *start, char* end, page_info* page_expected,
2588                     page_info* page_found) {
2589  const uint_t info_types[] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
2590  const size_t types = sizeof(info_types) / sizeof(info_types[0]);
2591  uint64_t addrs[MAX_MEMINFO_CNT], outdata[types * MAX_MEMINFO_CNT + 1];
2592  uint_t validity[MAX_MEMINFO_CNT];
2593
2594  size_t page_size = MAX2((size_t)os::vm_page_size(), page_expected->size);
2595  uint64_t p = (uint64_t)start;
2596  while (p < (uint64_t)end) {
2597    addrs[0] = p;
2598    size_t addrs_count = 1;
2599    while (addrs_count < MAX_MEMINFO_CNT && addrs[addrs_count - 1] + page_size < (uint64_t)end) {
2600      addrs[addrs_count] = addrs[addrs_count - 1] + page_size;
2601      addrs_count++;
2602    }
2603
2604    if (meminfo(addrs, addrs_count, info_types, types, outdata, validity) < 0) {
2605      return NULL;
2606    }
2607
2608    size_t i = 0;
2609    for (; i < addrs_count; i++) {
2610      if ((validity[i] & 1) != 0) {
2611        if ((validity[i] & 4) != 0) {
2612          if (outdata[types * i + 1] != page_expected->size) {
2613            break;
2614          }
2615        } else if (page_expected->size != 0) {
2616          break;
2617        }
2618
2619        if ((validity[i] & 2) != 0 && page_expected->lgrp_id > 0) {
2620          if (outdata[types * i] != page_expected->lgrp_id) {
2621            break;
2622          }
2623        }
2624      } else {
2625        return NULL;
2626      }
2627    }
2628
2629    if (i < addrs_count) {
2630      if ((validity[i] & 2) != 0) {
2631        page_found->lgrp_id = outdata[types * i];
2632      } else {
2633        page_found->lgrp_id = -1;
2634      }
2635      if ((validity[i] & 4) != 0) {
2636        page_found->size = outdata[types * i + 1];
2637      } else {
2638        page_found->size = 0;
2639      }
2640      return (char*)addrs[i];
2641    }
2642
2643    p = addrs[addrs_count - 1] + page_size;
2644  }
2645  return end;
2646}
2647
2648bool os::pd_uncommit_memory(char* addr, size_t bytes) {
2649  size_t size = bytes;
2650  // Map uncommitted pages PROT_NONE so we fail early if we touch an
2651  // uncommitted page. Otherwise, the read/write might succeed if we
2652  // have enough swap space to back the physical page.
2653  return
2654    NULL != Solaris::mmap_chunk(addr, size,
2655                                MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE,
2656                                PROT_NONE);
2657}
2658
2659char* os::Solaris::mmap_chunk(char *addr, size_t size, int flags, int prot) {
2660  char *b = (char *)mmap(addr, size, prot, flags, os::Solaris::_dev_zero_fd, 0);
2661
2662  if (b == MAP_FAILED) {
2663    return NULL;
2664  }
2665  return b;
2666}
2667
2668char* os::Solaris::anon_mmap(char* requested_addr, size_t bytes,
2669                             size_t alignment_hint, bool fixed) {
2670  char* addr = requested_addr;
2671  int flags = MAP_PRIVATE | MAP_NORESERVE;
2672
2673  assert(!(fixed && (alignment_hint > 0)),
2674         "alignment hint meaningless with fixed mmap");
2675
2676  if (fixed) {
2677    flags |= MAP_FIXED;
2678  } else if (has_map_align && (alignment_hint > (size_t) vm_page_size())) {
2679    flags |= MAP_ALIGN;
2680    addr = (char*) alignment_hint;
2681  }
2682
2683  // Map uncommitted pages PROT_NONE so we fail early if we touch an
2684  // uncommitted page. Otherwise, the read/write might succeed if we
2685  // have enough swap space to back the physical page.
2686  return mmap_chunk(addr, bytes, flags, PROT_NONE);
2687}
2688
2689char* os::pd_reserve_memory(size_t bytes, char* requested_addr,
2690                            size_t alignment_hint) {
2691  char* addr = Solaris::anon_mmap(requested_addr, bytes, alignment_hint,
2692                                  (requested_addr != NULL));
2693
2694  guarantee(requested_addr == NULL || requested_addr == addr,
2695            "OS failed to return requested mmap address.");
2696  return addr;
2697}
2698
2699// Reserve memory at an arbitrary address, only if that area is
2700// available (and not reserved for something else).
2701
2702char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
2703  const int max_tries = 10;
2704  char* base[max_tries];
2705  size_t size[max_tries];
2706
2707  // Solaris adds a gap between mmap'ed regions.  The size of the gap
2708  // is dependent on the requested size and the MMU.  Our initial gap
2709  // value here is just a guess and will be corrected later.
2710  bool had_top_overlap = false;
2711  bool have_adjusted_gap = false;
2712  size_t gap = 0x400000;
2713
2714  // Assert only that the size is a multiple of the page size, since
2715  // that's all that mmap requires, and since that's all we really know
2716  // about at this low abstraction level.  If we need higher alignment,
2717  // we can either pass an alignment to this method or verify alignment
2718  // in one of the methods further up the call chain.  See bug 5044738.
2719  assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");
2720
2721  // Since snv_84, Solaris attempts to honor the address hint - see 5003415.
2722  // Give it a try, if the kernel honors the hint we can return immediately.
2723  char* addr = Solaris::anon_mmap(requested_addr, bytes, 0, false);
2724
2725  volatile int err = errno;
2726  if (addr == requested_addr) {
2727    return addr;
2728  } else if (addr != NULL) {
2729    pd_unmap_memory(addr, bytes);
2730  }
2731
2732  if (log_is_enabled(Warning, os)) {
2733    char buf[256];
2734    buf[0] = '\0';
2735    if (addr == NULL) {
2736      jio_snprintf(buf, sizeof(buf), ": %s", os::strerror(err));
2737    }
2738    log_info(os)("attempt_reserve_memory_at: couldn't reserve " SIZE_FORMAT " bytes at "
2739            PTR_FORMAT ": reserve_memory_helper returned " PTR_FORMAT
2740            "%s", bytes, requested_addr, addr, buf);
2741  }
2742
2743  // Address hint method didn't work.  Fall back to the old method.
2744  // In theory, once SNV becomes our oldest supported platform, this
2745  // code will no longer be needed.
2746  //
2747  // Repeatedly allocate blocks until the block is allocated at the
2748  // right spot. Give up after max_tries.
2749  int i;
2750  for (i = 0; i < max_tries; ++i) {
2751    base[i] = reserve_memory(bytes);
2752
2753    if (base[i] != NULL) {
2754      // Is this the block we wanted?
2755      if (base[i] == requested_addr) {
2756        size[i] = bytes;
2757        break;
2758      }
2759
2760      // check that the gap value is right
2761      if (had_top_overlap && !have_adjusted_gap) {
2762        size_t actual_gap = base[i-1] - base[i] - bytes;
2763        if (gap != actual_gap) {
2764          // adjust the gap value and retry the last 2 allocations
2765          assert(i > 0, "gap adjustment code problem");
2766          have_adjusted_gap = true;  // adjust the gap only once, just in case
2767          gap = actual_gap;
2768          log_info(os)("attempt_reserve_memory_at: adjusted gap to 0x%lx", gap);
2769          unmap_memory(base[i], bytes);
2770          unmap_memory(base[i-1], size[i-1]);
2771          i-=2;
2772          continue;
2773        }
2774      }
2775
2776      // Does this overlap the block we wanted? Give back the overlapped
2777      // parts and try again.
2778      //
2779      // There is still a bug in this code: if top_overlap == bytes,
2780      // the overlap is offset from requested region by the value of gap.
2781      // In this case giving back the overlapped part will not work,
2782      // because we'll give back the entire block at base[i] and
2783      // therefore the subsequent allocation will not generate a new gap.
2784      // This could be fixed with a new algorithm that used larger
2785      // or variable size chunks to find the requested region -
2786      // but such a change would introduce additional complications.
2787      // It's rare enough that the planets align for this bug,
2788      // so we'll just wait for a fix for 6204603/5003415 which
2789      // will provide a mmap flag to allow us to avoid this business.
2790
2791      size_t top_overlap = requested_addr + (bytes + gap) - base[i];
2792      if (top_overlap >= 0 && top_overlap < bytes) {
2793        had_top_overlap = true;
2794        unmap_memory(base[i], top_overlap);
2795        base[i] += top_overlap;
2796        size[i] = bytes - top_overlap;
2797      } else {
2798        size_t bottom_overlap = base[i] + bytes - requested_addr;
2799        if (bottom_overlap >= 0 && bottom_overlap < bytes) {
2800          if (bottom_overlap == 0) {
2801            log_info(os)("attempt_reserve_memory_at: possible alignment bug");
2802          }
2803          unmap_memory(requested_addr, bottom_overlap);
2804          size[i] = bytes - bottom_overlap;
2805        } else {
2806          size[i] = bytes;
2807        }
2808      }
2809    }
2810  }
2811
2812  // Give back the unused reserved pieces.
2813
2814  for (int j = 0; j < i; ++j) {
2815    if (base[j] != NULL) {
2816      unmap_memory(base[j], size[j]);
2817    }
2818  }
2819
2820  return (i < max_tries) ? requested_addr : NULL;
2821}
2822
2823bool os::pd_release_memory(char* addr, size_t bytes) {
2824  size_t size = bytes;
2825  return munmap(addr, size) == 0;
2826}
2827
2828static bool solaris_mprotect(char* addr, size_t bytes, int prot) {
2829  assert(addr == (char*)align_size_down((uintptr_t)addr, os::vm_page_size()),
2830         "addr must be page aligned");
2831  int retVal = mprotect(addr, bytes, prot);
2832  return retVal == 0;
2833}
2834
2835// Protect memory (Used to pass readonly pages through
2836// JNI GetArray<type>Elements with empty arrays.)
2837// Also, used for serialization page and for compressed oops null pointer
2838// checking.
2839bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
2840                        bool is_committed) {
2841  unsigned int p = 0;
2842  switch (prot) {
2843  case MEM_PROT_NONE: p = PROT_NONE; break;
2844  case MEM_PROT_READ: p = PROT_READ; break;
2845  case MEM_PROT_RW:   p = PROT_READ|PROT_WRITE; break;
2846  case MEM_PROT_RWX:  p = PROT_READ|PROT_WRITE|PROT_EXEC; break;
2847  default:
2848    ShouldNotReachHere();
2849  }
2850  // is_committed is unused.
2851  return solaris_mprotect(addr, bytes, p);
2852}
2853
2854// guard_memory and unguard_memory only happens within stack guard pages.
2855// Since ISM pertains only to the heap, guard and unguard memory should not
2856/// happen with an ISM region.
2857bool os::guard_memory(char* addr, size_t bytes) {
2858  return solaris_mprotect(addr, bytes, PROT_NONE);
2859}
2860
2861bool os::unguard_memory(char* addr, size_t bytes) {
2862  return solaris_mprotect(addr, bytes, PROT_READ|PROT_WRITE);
2863}
2864
2865// Large page support
2866static size_t _large_page_size = 0;
2867
2868// Insertion sort for small arrays (descending order).
2869static void insertion_sort_descending(size_t* array, int len) {
2870  for (int i = 0; i < len; i++) {
2871    size_t val = array[i];
2872    for (size_t key = i; key > 0 && array[key - 1] < val; --key) {
2873      size_t tmp = array[key];
2874      array[key] = array[key - 1];
2875      array[key - 1] = tmp;
2876    }
2877  }
2878}
2879
2880bool os::Solaris::mpss_sanity_check(bool warn, size_t* page_size) {
2881  const unsigned int usable_count = VM_Version::page_size_count();
2882  if (usable_count == 1) {
2883    return false;
2884  }
2885
2886  // Find the right getpagesizes interface.  When solaris 11 is the minimum
2887  // build platform, getpagesizes() (without the '2') can be called directly.
2888  typedef int (*gps_t)(size_t[], int);
2889  gps_t gps_func = CAST_TO_FN_PTR(gps_t, dlsym(RTLD_DEFAULT, "getpagesizes2"));
2890  if (gps_func == NULL) {
2891    gps_func = CAST_TO_FN_PTR(gps_t, dlsym(RTLD_DEFAULT, "getpagesizes"));
2892    if (gps_func == NULL) {
2893      if (warn) {
2894        warning("MPSS is not supported by the operating system.");
2895      }
2896      return false;
2897    }
2898  }
2899
2900  // Fill the array of page sizes.
2901  int n = (*gps_func)(_page_sizes, page_sizes_max);
2902  assert(n > 0, "Solaris bug?");
2903
2904  if (n == page_sizes_max) {
2905    // Add a sentinel value (necessary only if the array was completely filled
2906    // since it is static (zeroed at initialization)).
2907    _page_sizes[--n] = 0;
2908    DEBUG_ONLY(warning("increase the size of the os::_page_sizes array.");)
2909  }
2910  assert(_page_sizes[n] == 0, "missing sentinel");
2911  trace_page_sizes("available page sizes", _page_sizes, n);
2912
2913  if (n == 1) return false;     // Only one page size available.
2914
2915  // Skip sizes larger than 4M (or LargePageSizeInBytes if it was set) and
2916  // select up to usable_count elements.  First sort the array, find the first
2917  // acceptable value, then copy the usable sizes to the top of the array and
2918  // trim the rest.  Make sure to include the default page size :-).
2919  //
2920  // A better policy could get rid of the 4M limit by taking the sizes of the
2921  // important VM memory regions (java heap and possibly the code cache) into
2922  // account.
2923  insertion_sort_descending(_page_sizes, n);
2924  const size_t size_limit =
2925    FLAG_IS_DEFAULT(LargePageSizeInBytes) ? 4 * M : LargePageSizeInBytes;
2926  int beg;
2927  for (beg = 0; beg < n && _page_sizes[beg] > size_limit; ++beg) /* empty */;
2928  const int end = MIN2((int)usable_count, n) - 1;
2929  for (int cur = 0; cur < end; ++cur, ++beg) {
2930    _page_sizes[cur] = _page_sizes[beg];
2931  }
2932  _page_sizes[end] = vm_page_size();
2933  _page_sizes[end + 1] = 0;
2934
2935  if (_page_sizes[end] > _page_sizes[end - 1]) {
2936    // Default page size is not the smallest; sort again.
2937    insertion_sort_descending(_page_sizes, end + 1);
2938  }
2939  *page_size = _page_sizes[0];
2940
2941  trace_page_sizes("usable page sizes", _page_sizes, end + 1);
2942  return true;
2943}
2944
2945void os::large_page_init() {
2946  if (UseLargePages) {
2947    // print a warning if any large page related flag is specified on command line
2948    bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages)        ||
2949                           !FLAG_IS_DEFAULT(LargePageSizeInBytes);
2950
2951    UseLargePages = Solaris::mpss_sanity_check(warn_on_failure, &_large_page_size);
2952  }
2953}
2954
2955bool os::Solaris::is_valid_page_size(size_t bytes) {
2956  for (int i = 0; _page_sizes[i] != 0; i++) {
2957    if (_page_sizes[i] == bytes) {
2958      return true;
2959    }
2960  }
2961  return false;
2962}
2963
2964bool os::Solaris::setup_large_pages(caddr_t start, size_t bytes, size_t align) {
2965  assert(is_valid_page_size(align), SIZE_FORMAT " is not a valid page size", align);
2966  assert(is_ptr_aligned((void*) start, align),
2967         PTR_FORMAT " is not aligned to " SIZE_FORMAT, p2i((void*) start), align);
2968  assert(is_size_aligned(bytes, align),
2969         SIZE_FORMAT " is not aligned to " SIZE_FORMAT, bytes, align);
2970
2971  // Signal to OS that we want large pages for addresses
2972  // from addr, addr + bytes
2973  struct memcntl_mha mpss_struct;
2974  mpss_struct.mha_cmd = MHA_MAPSIZE_VA;
2975  mpss_struct.mha_pagesize = align;
2976  mpss_struct.mha_flags = 0;
2977  // Upon successful completion, memcntl() returns 0
2978  if (memcntl(start, bytes, MC_HAT_ADVISE, (caddr_t) &mpss_struct, 0, 0)) {
2979    debug_only(warning("Attempt to use MPSS failed."));
2980    return false;
2981  }
2982  return true;
2983}
2984
2985char* os::reserve_memory_special(size_t size, size_t alignment, char* addr, bool exec) {
2986  fatal("os::reserve_memory_special should not be called on Solaris.");
2987  return NULL;
2988}
2989
2990bool os::release_memory_special(char* base, size_t bytes) {
2991  fatal("os::release_memory_special should not be called on Solaris.");
2992  return false;
2993}
2994
2995size_t os::large_page_size() {
2996  return _large_page_size;
2997}
2998
2999// MPSS allows application to commit large page memory on demand; with ISM
3000// the entire memory region must be allocated as shared memory.
3001bool os::can_commit_large_page_memory() {
3002  return true;
3003}
3004
3005bool os::can_execute_large_page_memory() {
3006  return true;
3007}
3008
3009// Read calls from inside the vm need to perform state transitions
3010size_t os::read(int fd, void *buf, unsigned int nBytes) {
3011  size_t res;
3012  JavaThread* thread = (JavaThread*)Thread::current();
3013  assert(thread->thread_state() == _thread_in_vm, "Assumed _thread_in_vm");
3014  ThreadBlockInVM tbiv(thread);
3015  RESTARTABLE(::read(fd, buf, (size_t) nBytes), res);
3016  return res;
3017}
3018
3019size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
3020  size_t res;
3021  JavaThread* thread = (JavaThread*)Thread::current();
3022  assert(thread->thread_state() == _thread_in_vm, "Assumed _thread_in_vm");
3023  ThreadBlockInVM tbiv(thread);
3024  RESTARTABLE(::pread(fd, buf, (size_t) nBytes, offset), res);
3025  return res;
3026}
3027
3028size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
3029  size_t res;
3030  assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_native,
3031         "Assumed _thread_in_native");
3032  RESTARTABLE(::read(fd, buf, (size_t) nBytes), res);
3033  return res;
3034}
3035
3036void os::naked_short_sleep(jlong ms) {
3037  assert(ms < 1000, "Un-interruptable sleep, short time use only");
3038
3039  // usleep is deprecated and removed from POSIX, in favour of nanosleep, but
3040  // Solaris requires -lrt for this.
3041  usleep((ms * 1000));
3042
3043  return;
3044}
3045
3046// Sleep forever; naked call to OS-specific sleep; use with CAUTION
3047void os::infinite_sleep() {
3048  while (true) {    // sleep forever ...
3049    ::sleep(100);   // ... 100 seconds at a time
3050  }
3051}
3052
3053// Used to convert frequent JVM_Yield() to nops
3054bool os::dont_yield() {
3055  if (DontYieldALot) {
3056    static hrtime_t last_time = 0;
3057    hrtime_t diff = getTimeNanos() - last_time;
3058
3059    if (diff < DontYieldALotInterval * 1000000) {
3060      return true;
3061    }
3062
3063    last_time += diff;
3064
3065    return false;
3066  } else {
3067    return false;
3068  }
3069}
3070
3071// Note that yield semantics are defined by the scheduling class to which
3072// the thread currently belongs.  Typically, yield will _not yield to
3073// other equal or higher priority threads that reside on the dispatch queues
3074// of other CPUs.
3075
3076void os::naked_yield() {
3077  thr_yield();
3078}
3079
3080// Interface for setting lwp priorities.  If we are using T2 libthread,
3081// which forces the use of BoundThreads or we manually set UseBoundThreads,
3082// all of our threads will be assigned to real lwp's.  Using the thr_setprio
3083// function is meaningless in this mode so we must adjust the real lwp's priority
3084// The routines below implement the getting and setting of lwp priorities.
3085//
3086// Note: T2 is now the only supported libthread. UseBoundThreads flag is
3087//       being deprecated and all threads are now BoundThreads
3088//
3089// Note: There are three priority scales used on Solaris.  Java priotities
3090//       which range from 1 to 10, libthread "thr_setprio" scale which range
3091//       from 0 to 127, and the current scheduling class of the process we
3092//       are running in.  This is typically from -60 to +60.
3093//       The setting of the lwp priorities in done after a call to thr_setprio
3094//       so Java priorities are mapped to libthread priorities and we map from
3095//       the latter to lwp priorities.  We don't keep priorities stored in
3096//       Java priorities since some of our worker threads want to set priorities
3097//       higher than all Java threads.
3098//
3099// For related information:
3100// (1)  man -s 2 priocntl
3101// (2)  man -s 4 priocntl
3102// (3)  man dispadmin
3103// =    librt.so
3104// =    libthread/common/rtsched.c - thrp_setlwpprio().
3105// =    ps -cL <pid> ... to validate priority.
3106// =    sched_get_priority_min and _max
3107//              pthread_create
3108//              sched_setparam
3109//              pthread_setschedparam
3110//
3111// Assumptions:
3112// +    We assume that all threads in the process belong to the same
3113//              scheduling class.   IE. an homogenous process.
3114// +    Must be root or in IA group to change change "interactive" attribute.
3115//              Priocntl() will fail silently.  The only indication of failure is when
3116//              we read-back the value and notice that it hasn't changed.
3117// +    Interactive threads enter the runq at the head, non-interactive at the tail.
3118// +    For RT, change timeslice as well.  Invariant:
3119//              constant "priority integral"
3120//              Konst == TimeSlice * (60-Priority)
3121//              Given a priority, compute appropriate timeslice.
3122// +    Higher numerical values have higher priority.
3123
3124// sched class attributes
3125typedef struct {
3126  int   schedPolicy;              // classID
3127  int   maxPrio;
3128  int   minPrio;
3129} SchedInfo;
3130
3131
3132static SchedInfo tsLimits, iaLimits, rtLimits, fxLimits;
3133
3134#ifdef ASSERT
3135static int  ReadBackValidate = 1;
3136#endif
3137static int  myClass     = 0;
3138static int  myMin       = 0;
3139static int  myMax       = 0;
3140static int  myCur       = 0;
3141static bool priocntl_enable = false;
3142
3143static const int criticalPrio = FXCriticalPriority;
3144static int java_MaxPriority_to_os_priority = 0; // Saved mapping
3145
3146
3147// lwp_priocntl_init
3148//
3149// Try to determine the priority scale for our process.
3150//
3151// Return errno or 0 if OK.
3152//
3153static int lwp_priocntl_init() {
3154  int rslt;
3155  pcinfo_t ClassInfo;
3156  pcparms_t ParmInfo;
3157  int i;
3158
3159  if (!UseThreadPriorities) return 0;
3160
3161  // If ThreadPriorityPolicy is 1, switch tables
3162  if (ThreadPriorityPolicy == 1) {
3163    for (i = 0; i < CriticalPriority+1; i++)
3164      os::java_to_os_priority[i] = prio_policy1[i];
3165  }
3166  if (UseCriticalJavaThreadPriority) {
3167    // MaxPriority always maps to the FX scheduling class and criticalPrio.
3168    // See set_native_priority() and set_lwp_class_and_priority().
3169    // Save original MaxPriority mapping in case attempt to
3170    // use critical priority fails.
3171    java_MaxPriority_to_os_priority = os::java_to_os_priority[MaxPriority];
3172    // Set negative to distinguish from other priorities
3173    os::java_to_os_priority[MaxPriority] = -criticalPrio;
3174  }
3175
3176  // Get IDs for a set of well-known scheduling classes.
3177  // TODO-FIXME: GETCLINFO returns the current # of classes in the
3178  // the system.  We should have a loop that iterates over the
3179  // classID values, which are known to be "small" integers.
3180
3181  strcpy(ClassInfo.pc_clname, "TS");
3182  ClassInfo.pc_cid = -1;
3183  rslt = priocntl(P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3184  if (rslt < 0) return errno;
3185  assert(ClassInfo.pc_cid != -1, "cid for TS class is -1");
3186  tsLimits.schedPolicy = ClassInfo.pc_cid;
3187  tsLimits.maxPrio = ((tsinfo_t*)ClassInfo.pc_clinfo)->ts_maxupri;
3188  tsLimits.minPrio = -tsLimits.maxPrio;
3189
3190  strcpy(ClassInfo.pc_clname, "IA");
3191  ClassInfo.pc_cid = -1;
3192  rslt = priocntl(P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3193  if (rslt < 0) return errno;
3194  assert(ClassInfo.pc_cid != -1, "cid for IA class is -1");
3195  iaLimits.schedPolicy = ClassInfo.pc_cid;
3196  iaLimits.maxPrio = ((iainfo_t*)ClassInfo.pc_clinfo)->ia_maxupri;
3197  iaLimits.minPrio = -iaLimits.maxPrio;
3198
3199  strcpy(ClassInfo.pc_clname, "RT");
3200  ClassInfo.pc_cid = -1;
3201  rslt = priocntl(P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3202  if (rslt < 0) return errno;
3203  assert(ClassInfo.pc_cid != -1, "cid for RT class is -1");
3204  rtLimits.schedPolicy = ClassInfo.pc_cid;
3205  rtLimits.maxPrio = ((rtinfo_t*)ClassInfo.pc_clinfo)->rt_maxpri;
3206  rtLimits.minPrio = 0;
3207
3208  strcpy(ClassInfo.pc_clname, "FX");
3209  ClassInfo.pc_cid = -1;
3210  rslt = priocntl(P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3211  if (rslt < 0) return errno;
3212  assert(ClassInfo.pc_cid != -1, "cid for FX class is -1");
3213  fxLimits.schedPolicy = ClassInfo.pc_cid;
3214  fxLimits.maxPrio = ((fxinfo_t*)ClassInfo.pc_clinfo)->fx_maxupri;
3215  fxLimits.minPrio = 0;
3216
3217  // Query our "current" scheduling class.
3218  // This will normally be IA, TS or, rarely, FX or RT.
3219  memset(&ParmInfo, 0, sizeof(ParmInfo));
3220  ParmInfo.pc_cid = PC_CLNULL;
3221  rslt = priocntl(P_PID, P_MYID, PC_GETPARMS, (caddr_t)&ParmInfo);
3222  if (rslt < 0) return errno;
3223  myClass = ParmInfo.pc_cid;
3224
3225  // We now know our scheduling classId, get specific information
3226  // about the class.
3227  ClassInfo.pc_cid = myClass;
3228  ClassInfo.pc_clname[0] = 0;
3229  rslt = priocntl((idtype)0, 0, PC_GETCLINFO, (caddr_t)&ClassInfo);
3230  if (rslt < 0) return errno;
3231
3232  if (ThreadPriorityVerbose) {
3233    tty->print_cr("lwp_priocntl_init: Class=%d(%s)...", myClass, ClassInfo.pc_clname);
3234  }
3235
3236  memset(&ParmInfo, 0, sizeof(pcparms_t));
3237  ParmInfo.pc_cid = PC_CLNULL;
3238  rslt = priocntl(P_PID, P_MYID, PC_GETPARMS, (caddr_t)&ParmInfo);
3239  if (rslt < 0) return errno;
3240
3241  if (ParmInfo.pc_cid == rtLimits.schedPolicy) {
3242    myMin = rtLimits.minPrio;
3243    myMax = rtLimits.maxPrio;
3244  } else if (ParmInfo.pc_cid == iaLimits.schedPolicy) {
3245    iaparms_t *iaInfo  = (iaparms_t*)ParmInfo.pc_clparms;
3246    myMin = iaLimits.minPrio;
3247    myMax = iaLimits.maxPrio;
3248    myMax = MIN2(myMax, (int)iaInfo->ia_uprilim);       // clamp - restrict
3249  } else if (ParmInfo.pc_cid == tsLimits.schedPolicy) {
3250    tsparms_t *tsInfo  = (tsparms_t*)ParmInfo.pc_clparms;
3251    myMin = tsLimits.minPrio;
3252    myMax = tsLimits.maxPrio;
3253    myMax = MIN2(myMax, (int)tsInfo->ts_uprilim);       // clamp - restrict
3254  } else if (ParmInfo.pc_cid == fxLimits.schedPolicy) {
3255    fxparms_t *fxInfo = (fxparms_t*)ParmInfo.pc_clparms;
3256    myMin = fxLimits.minPrio;
3257    myMax = fxLimits.maxPrio;
3258    myMax = MIN2(myMax, (int)fxInfo->fx_uprilim);       // clamp - restrict
3259  } else {
3260    // No clue - punt
3261    if (ThreadPriorityVerbose) {
3262      tty->print_cr("Unknown scheduling class: %s ... \n",
3263                    ClassInfo.pc_clname);
3264    }
3265    return EINVAL;      // no clue, punt
3266  }
3267
3268  if (ThreadPriorityVerbose) {
3269    tty->print_cr("Thread priority Range: [%d..%d]\n", myMin, myMax);
3270  }
3271
3272  priocntl_enable = true;  // Enable changing priorities
3273  return 0;
3274}
3275
3276#define IAPRI(x)        ((iaparms_t *)((x).pc_clparms))
3277#define RTPRI(x)        ((rtparms_t *)((x).pc_clparms))
3278#define TSPRI(x)        ((tsparms_t *)((x).pc_clparms))
3279#define FXPRI(x)        ((fxparms_t *)((x).pc_clparms))
3280
3281
3282// scale_to_lwp_priority
3283//
3284// Convert from the libthread "thr_setprio" scale to our current
3285// lwp scheduling class scale.
3286//
3287static int scale_to_lwp_priority(int rMin, int rMax, int x) {
3288  int v;
3289
3290  if (x == 127) return rMax;            // avoid round-down
3291  v = (((x*(rMax-rMin)))/128)+rMin;
3292  return v;
3293}
3294
3295
3296// set_lwp_class_and_priority
3297int set_lwp_class_and_priority(int ThreadID, int lwpid,
3298                               int newPrio, int new_class, bool scale) {
3299  int rslt;
3300  int Actual, Expected, prv;
3301  pcparms_t ParmInfo;                   // for GET-SET
3302#ifdef ASSERT
3303  pcparms_t ReadBack;                   // for readback
3304#endif
3305
3306  // Set priority via PC_GETPARMS, update, PC_SETPARMS
3307  // Query current values.
3308  // TODO: accelerate this by eliminating the PC_GETPARMS call.
3309  // Cache "pcparms_t" in global ParmCache.
3310  // TODO: elide set-to-same-value
3311
3312  // If something went wrong on init, don't change priorities.
3313  if (!priocntl_enable) {
3314    if (ThreadPriorityVerbose) {
3315      tty->print_cr("Trying to set priority but init failed, ignoring");
3316    }
3317    return EINVAL;
3318  }
3319
3320  // If lwp hasn't started yet, just return
3321  // the _start routine will call us again.
3322  if (lwpid <= 0) {
3323    if (ThreadPriorityVerbose) {
3324      tty->print_cr("deferring the set_lwp_class_and_priority of thread "
3325                    INTPTR_FORMAT " to %d, lwpid not set",
3326                    ThreadID, newPrio);
3327    }
3328    return 0;
3329  }
3330
3331  if (ThreadPriorityVerbose) {
3332    tty->print_cr ("set_lwp_class_and_priority("
3333                   INTPTR_FORMAT "@" INTPTR_FORMAT " %d) ",
3334                   ThreadID, lwpid, newPrio);
3335  }
3336
3337  memset(&ParmInfo, 0, sizeof(pcparms_t));
3338  ParmInfo.pc_cid = PC_CLNULL;
3339  rslt = priocntl(P_LWPID, lwpid, PC_GETPARMS, (caddr_t)&ParmInfo);
3340  if (rslt < 0) return errno;
3341
3342  int cur_class = ParmInfo.pc_cid;
3343  ParmInfo.pc_cid = (id_t)new_class;
3344
3345  if (new_class == rtLimits.schedPolicy) {
3346    rtparms_t *rtInfo  = (rtparms_t*)ParmInfo.pc_clparms;
3347    rtInfo->rt_pri     = scale ? scale_to_lwp_priority(rtLimits.minPrio,
3348                                                       rtLimits.maxPrio, newPrio)
3349                               : newPrio;
3350    rtInfo->rt_tqsecs  = RT_NOCHANGE;
3351    rtInfo->rt_tqnsecs = RT_NOCHANGE;
3352    if (ThreadPriorityVerbose) {
3353      tty->print_cr("RT: %d->%d\n", newPrio, rtInfo->rt_pri);
3354    }
3355  } else if (new_class == iaLimits.schedPolicy) {
3356    iaparms_t* iaInfo  = (iaparms_t*)ParmInfo.pc_clparms;
3357    int maxClamped     = MIN2(iaLimits.maxPrio,
3358                              cur_class == new_class
3359                              ? (int)iaInfo->ia_uprilim : iaLimits.maxPrio);
3360    iaInfo->ia_upri    = scale ? scale_to_lwp_priority(iaLimits.minPrio,
3361                                                       maxClamped, newPrio)
3362                               : newPrio;
3363    iaInfo->ia_uprilim = cur_class == new_class
3364                           ? IA_NOCHANGE : (pri_t)iaLimits.maxPrio;
3365    iaInfo->ia_mode    = IA_NOCHANGE;
3366    if (ThreadPriorityVerbose) {
3367      tty->print_cr("IA: [%d...%d] %d->%d\n",
3368                    iaLimits.minPrio, maxClamped, newPrio, iaInfo->ia_upri);
3369    }
3370  } else if (new_class == tsLimits.schedPolicy) {
3371    tsparms_t* tsInfo  = (tsparms_t*)ParmInfo.pc_clparms;
3372    int maxClamped     = MIN2(tsLimits.maxPrio,
3373                              cur_class == new_class
3374                              ? (int)tsInfo->ts_uprilim : tsLimits.maxPrio);
3375    tsInfo->ts_upri    = scale ? scale_to_lwp_priority(tsLimits.minPrio,
3376                                                       maxClamped, newPrio)
3377                               : newPrio;
3378    tsInfo->ts_uprilim = cur_class == new_class
3379                           ? TS_NOCHANGE : (pri_t)tsLimits.maxPrio;
3380    if (ThreadPriorityVerbose) {
3381      tty->print_cr("TS: [%d...%d] %d->%d\n",
3382                    tsLimits.minPrio, maxClamped, newPrio, tsInfo->ts_upri);
3383    }
3384  } else if (new_class == fxLimits.schedPolicy) {
3385    fxparms_t* fxInfo  = (fxparms_t*)ParmInfo.pc_clparms;
3386    int maxClamped     = MIN2(fxLimits.maxPrio,
3387                              cur_class == new_class
3388                              ? (int)fxInfo->fx_uprilim : fxLimits.maxPrio);
3389    fxInfo->fx_upri    = scale ? scale_to_lwp_priority(fxLimits.minPrio,
3390                                                       maxClamped, newPrio)
3391                               : newPrio;
3392    fxInfo->fx_uprilim = cur_class == new_class
3393                           ? FX_NOCHANGE : (pri_t)fxLimits.maxPrio;
3394    fxInfo->fx_tqsecs  = FX_NOCHANGE;
3395    fxInfo->fx_tqnsecs = FX_NOCHANGE;
3396    if (ThreadPriorityVerbose) {
3397      tty->print_cr("FX: [%d...%d] %d->%d\n",
3398                    fxLimits.minPrio, maxClamped, newPrio, fxInfo->fx_upri);
3399    }
3400  } else {
3401    if (ThreadPriorityVerbose) {
3402      tty->print_cr("Unknown new scheduling class %d\n", new_class);
3403    }
3404    return EINVAL;    // no clue, punt
3405  }
3406
3407  rslt = priocntl(P_LWPID, lwpid, PC_SETPARMS, (caddr_t)&ParmInfo);
3408  if (ThreadPriorityVerbose && rslt) {
3409    tty->print_cr ("PC_SETPARMS ->%d %d\n", rslt, errno);
3410  }
3411  if (rslt < 0) return errno;
3412
3413#ifdef ASSERT
3414  // Sanity check: read back what we just attempted to set.
3415  // In theory it could have changed in the interim ...
3416  //
3417  // The priocntl system call is tricky.
3418  // Sometimes it'll validate the priority value argument and
3419  // return EINVAL if unhappy.  At other times it fails silently.
3420  // Readbacks are prudent.
3421
3422  if (!ReadBackValidate) return 0;
3423
3424  memset(&ReadBack, 0, sizeof(pcparms_t));
3425  ReadBack.pc_cid = PC_CLNULL;
3426  rslt = priocntl(P_LWPID, lwpid, PC_GETPARMS, (caddr_t)&ReadBack);
3427  assert(rslt >= 0, "priocntl failed");
3428  Actual = Expected = 0xBAD;
3429  assert(ParmInfo.pc_cid == ReadBack.pc_cid, "cid's don't match");
3430  if (ParmInfo.pc_cid == rtLimits.schedPolicy) {
3431    Actual   = RTPRI(ReadBack)->rt_pri;
3432    Expected = RTPRI(ParmInfo)->rt_pri;
3433  } else if (ParmInfo.pc_cid == iaLimits.schedPolicy) {
3434    Actual   = IAPRI(ReadBack)->ia_upri;
3435    Expected = IAPRI(ParmInfo)->ia_upri;
3436  } else if (ParmInfo.pc_cid == tsLimits.schedPolicy) {
3437    Actual   = TSPRI(ReadBack)->ts_upri;
3438    Expected = TSPRI(ParmInfo)->ts_upri;
3439  } else if (ParmInfo.pc_cid == fxLimits.schedPolicy) {
3440    Actual   = FXPRI(ReadBack)->fx_upri;
3441    Expected = FXPRI(ParmInfo)->fx_upri;
3442  } else {
3443    if (ThreadPriorityVerbose) {
3444      tty->print_cr("set_lwp_class_and_priority: unexpected class in readback: %d\n",
3445                    ParmInfo.pc_cid);
3446    }
3447  }
3448
3449  if (Actual != Expected) {
3450    if (ThreadPriorityVerbose) {
3451      tty->print_cr ("set_lwp_class_and_priority(%d %d) Class=%d: actual=%d vs expected=%d\n",
3452                     lwpid, newPrio, ReadBack.pc_cid, Actual, Expected);
3453    }
3454  }
3455#endif
3456
3457  return 0;
3458}
3459
3460// Solaris only gives access to 128 real priorities at a time,
3461// so we expand Java's ten to fill this range.  This would be better
3462// if we dynamically adjusted relative priorities.
3463//
3464// The ThreadPriorityPolicy option allows us to select 2 different
3465// priority scales.
3466//
3467// ThreadPriorityPolicy=0
3468// Since the Solaris' default priority is MaximumPriority, we do not
3469// set a priority lower than Max unless a priority lower than
3470// NormPriority is requested.
3471//
3472// ThreadPriorityPolicy=1
3473// This mode causes the priority table to get filled with
3474// linear values.  NormPriority get's mapped to 50% of the
3475// Maximum priority an so on.  This will cause VM threads
3476// to get unfair treatment against other Solaris processes
3477// which do not explicitly alter their thread priorities.
3478
3479int os::java_to_os_priority[CriticalPriority + 1] = {
3480  -99999,         // 0 Entry should never be used
3481
3482  0,              // 1 MinPriority
3483  32,             // 2
3484  64,             // 3
3485
3486  96,             // 4
3487  127,            // 5 NormPriority
3488  127,            // 6
3489
3490  127,            // 7
3491  127,            // 8
3492  127,            // 9 NearMaxPriority
3493
3494  127,            // 10 MaxPriority
3495
3496  -criticalPrio   // 11 CriticalPriority
3497};
3498
3499OSReturn os::set_native_priority(Thread* thread, int newpri) {
3500  OSThread* osthread = thread->osthread();
3501
3502  // Save requested priority in case the thread hasn't been started
3503  osthread->set_native_priority(newpri);
3504
3505  // Check for critical priority request
3506  bool fxcritical = false;
3507  if (newpri == -criticalPrio) {
3508    fxcritical = true;
3509    newpri = criticalPrio;
3510  }
3511
3512  assert(newpri >= MinimumPriority && newpri <= MaximumPriority, "bad priority mapping");
3513  if (!UseThreadPriorities) return OS_OK;
3514
3515  int status = 0;
3516
3517  if (!fxcritical) {
3518    // Use thr_setprio only if we have a priority that thr_setprio understands
3519    status = thr_setprio(thread->osthread()->thread_id(), newpri);
3520  }
3521
3522  int lwp_status =
3523          set_lwp_class_and_priority(osthread->thread_id(),
3524                                     osthread->lwp_id(),
3525                                     newpri,
3526                                     fxcritical ? fxLimits.schedPolicy : myClass,
3527                                     !fxcritical);
3528  if (lwp_status != 0 && fxcritical) {
3529    // Try again, this time without changing the scheduling class
3530    newpri = java_MaxPriority_to_os_priority;
3531    lwp_status = set_lwp_class_and_priority(osthread->thread_id(),
3532                                            osthread->lwp_id(),
3533                                            newpri, myClass, false);
3534  }
3535  status |= lwp_status;
3536  return (status == 0) ? OS_OK : OS_ERR;
3537}
3538
3539
3540OSReturn os::get_native_priority(const Thread* const thread,
3541                                 int *priority_ptr) {
3542  int p;
3543  if (!UseThreadPriorities) {
3544    *priority_ptr = NormalPriority;
3545    return OS_OK;
3546  }
3547  int status = thr_getprio(thread->osthread()->thread_id(), &p);
3548  if (status != 0) {
3549    return OS_ERR;
3550  }
3551  *priority_ptr = p;
3552  return OS_OK;
3553}
3554
3555
3556// Hint to the underlying OS that a task switch would not be good.
3557// Void return because it's a hint and can fail.
3558void os::hint_no_preempt() {
3559  schedctl_start(schedctl_init());
3560}
3561
3562static void resume_clear_context(OSThread *osthread) {
3563  osthread->set_ucontext(NULL);
3564}
3565
3566static void suspend_save_context(OSThread *osthread, ucontext_t* context) {
3567  osthread->set_ucontext(context);
3568}
3569
3570static PosixSemaphore sr_semaphore;
3571
3572void os::Solaris::SR_handler(Thread* thread, ucontext_t* uc) {
3573  // Save and restore errno to avoid confusing native code with EINTR
3574  // after sigsuspend.
3575  int old_errno = errno;
3576
3577  OSThread* osthread = thread->osthread();
3578  assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread");
3579
3580  os::SuspendResume::State current = osthread->sr.state();
3581  if (current == os::SuspendResume::SR_SUSPEND_REQUEST) {
3582    suspend_save_context(osthread, uc);
3583
3584    // attempt to switch the state, we assume we had a SUSPEND_REQUEST
3585    os::SuspendResume::State state = osthread->sr.suspended();
3586    if (state == os::SuspendResume::SR_SUSPENDED) {
3587      sigset_t suspend_set;  // signals for sigsuspend()
3588
3589      // get current set of blocked signals and unblock resume signal
3590      pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
3591      sigdelset(&suspend_set, os::Solaris::SIGasync());
3592
3593      sr_semaphore.signal();
3594      // wait here until we are resumed
3595      while (1) {
3596        sigsuspend(&suspend_set);
3597
3598        os::SuspendResume::State result = osthread->sr.running();
3599        if (result == os::SuspendResume::SR_RUNNING) {
3600          sr_semaphore.signal();
3601          break;
3602        }
3603      }
3604
3605    } else if (state == os::SuspendResume::SR_RUNNING) {
3606      // request was cancelled, continue
3607    } else {
3608      ShouldNotReachHere();
3609    }
3610
3611    resume_clear_context(osthread);
3612  } else if (current == os::SuspendResume::SR_RUNNING) {
3613    // request was cancelled, continue
3614  } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {
3615    // ignore
3616  } else {
3617    // ignore
3618  }
3619
3620  errno = old_errno;
3621}
3622
3623void os::print_statistics() {
3624}
3625
3626bool os::message_box(const char* title, const char* message) {
3627  int i;
3628  fdStream err(defaultStream::error_fd());
3629  for (i = 0; i < 78; i++) err.print_raw("=");
3630  err.cr();
3631  err.print_raw_cr(title);
3632  for (i = 0; i < 78; i++) err.print_raw("-");
3633  err.cr();
3634  err.print_raw_cr(message);
3635  for (i = 0; i < 78; i++) err.print_raw("=");
3636  err.cr();
3637
3638  char buf[16];
3639  // Prevent process from exiting upon "read error" without consuming all CPU
3640  while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
3641
3642  return buf[0] == 'y' || buf[0] == 'Y';
3643}
3644
3645static int sr_notify(OSThread* osthread) {
3646  int status = thr_kill(osthread->thread_id(), os::Solaris::SIGasync());
3647  assert_status(status == 0, status, "thr_kill");
3648  return status;
3649}
3650
3651// "Randomly" selected value for how long we want to spin
3652// before bailing out on suspending a thread, also how often
3653// we send a signal to a thread we want to resume
3654static const int RANDOMLY_LARGE_INTEGER = 1000000;
3655static const int RANDOMLY_LARGE_INTEGER2 = 100;
3656
3657static bool do_suspend(OSThread* osthread) {
3658  assert(osthread->sr.is_running(), "thread should be running");
3659  assert(!sr_semaphore.trywait(), "semaphore has invalid state");
3660
3661  // mark as suspended and send signal
3662  if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) {
3663    // failed to switch, state wasn't running?
3664    ShouldNotReachHere();
3665    return false;
3666  }
3667
3668  if (sr_notify(osthread) != 0) {
3669    ShouldNotReachHere();
3670  }
3671
3672  // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED
3673  while (true) {
3674    if (sr_semaphore.timedwait(0, 2000 * NANOSECS_PER_MILLISEC)) {
3675      break;
3676    } else {
3677      // timeout
3678      os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();
3679      if (cancelled == os::SuspendResume::SR_RUNNING) {
3680        return false;
3681      } else if (cancelled == os::SuspendResume::SR_SUSPENDED) {
3682        // make sure that we consume the signal on the semaphore as well
3683        sr_semaphore.wait();
3684        break;
3685      } else {
3686        ShouldNotReachHere();
3687        return false;
3688      }
3689    }
3690  }
3691
3692  guarantee(osthread->sr.is_suspended(), "Must be suspended");
3693  return true;
3694}
3695
3696static void do_resume(OSThread* osthread) {
3697  assert(osthread->sr.is_suspended(), "thread should be suspended");
3698  assert(!sr_semaphore.trywait(), "invalid semaphore state");
3699
3700  if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) {
3701    // failed to switch to WAKEUP_REQUEST
3702    ShouldNotReachHere();
3703    return;
3704  }
3705
3706  while (true) {
3707    if (sr_notify(osthread) == 0) {
3708      if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
3709        if (osthread->sr.is_running()) {
3710          return;
3711        }
3712      }
3713    } else {
3714      ShouldNotReachHere();
3715    }
3716  }
3717
3718  guarantee(osthread->sr.is_running(), "Must be running!");
3719}
3720
3721void os::SuspendedThreadTask::internal_do_task() {
3722  if (do_suspend(_thread->osthread())) {
3723    SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext());
3724    do_task(context);
3725    do_resume(_thread->osthread());
3726  }
3727}
3728
3729class PcFetcher : public os::SuspendedThreadTask {
3730 public:
3731  PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {}
3732  ExtendedPC result();
3733 protected:
3734  void do_task(const os::SuspendedThreadTaskContext& context);
3735 private:
3736  ExtendedPC _epc;
3737};
3738
3739ExtendedPC PcFetcher::result() {
3740  guarantee(is_done(), "task is not done yet.");
3741  return _epc;
3742}
3743
3744void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) {
3745  Thread* thread = context.thread();
3746  OSThread* osthread = thread->osthread();
3747  if (osthread->ucontext() != NULL) {
3748    _epc = os::Solaris::ucontext_get_pc((const ucontext_t *) context.ucontext());
3749  } else {
3750    // NULL context is unexpected, double-check this is the VMThread
3751    guarantee(thread->is_VM_thread(), "can only be called for VMThread");
3752  }
3753}
3754
3755// A lightweight implementation that does not suspend the target thread and
3756// thus returns only a hint. Used for profiling only!
3757ExtendedPC os::get_thread_pc(Thread* thread) {
3758  // Make sure that it is called by the watcher and the Threads lock is owned.
3759  assert(Thread::current()->is_Watcher_thread(), "Must be watcher and own Threads_lock");
3760  // For now, is only used to profile the VM Thread
3761  assert(thread->is_VM_thread(), "Can only be called for VMThread");
3762  PcFetcher fetcher(thread);
3763  fetcher.run();
3764  return fetcher.result();
3765}
3766
3767
3768// This does not do anything on Solaris. This is basically a hook for being
3769// able to use structured exception handling (thread-local exception filters) on, e.g., Win32.
3770void os::os_exception_wrapper(java_call_t f, JavaValue* value,
3771                              const methodHandle& method, JavaCallArguments* args,
3772                              Thread* thread) {
3773  f(value, method, args, thread);
3774}
3775
3776// This routine may be used by user applications as a "hook" to catch signals.
3777// The user-defined signal handler must pass unrecognized signals to this
3778// routine, and if it returns true (non-zero), then the signal handler must
3779// return immediately.  If the flag "abort_if_unrecognized" is true, then this
3780// routine will never retun false (zero), but instead will execute a VM panic
3781// routine kill the process.
3782//
3783// If this routine returns false, it is OK to call it again.  This allows
3784// the user-defined signal handler to perform checks either before or after
3785// the VM performs its own checks.  Naturally, the user code would be making
3786// a serious error if it tried to handle an exception (such as a null check
3787// or breakpoint) that the VM was generating for its own correct operation.
3788//
3789// This routine may recognize any of the following kinds of signals:
3790// SIGBUS, SIGSEGV, SIGILL, SIGFPE, BREAK_SIGNAL, SIGPIPE, SIGXFSZ,
3791// os::Solaris::SIGasync
3792// It should be consulted by handlers for any of those signals.
3793//
3794// The caller of this routine must pass in the three arguments supplied
3795// to the function referred to in the "sa_sigaction" (not the "sa_handler")
3796// field of the structure passed to sigaction().  This routine assumes that
3797// the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART.
3798//
3799// Note that the VM will print warnings if it detects conflicting signal
3800// handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers".
3801//
3802extern "C" JNIEXPORT int JVM_handle_solaris_signal(int signo,
3803                                                   siginfo_t* siginfo,
3804                                                   void* ucontext,
3805                                                   int abort_if_unrecognized);
3806
3807
3808void signalHandler(int sig, siginfo_t* info, void* ucVoid) {
3809  int orig_errno = errno;  // Preserve errno value over signal handler.
3810  JVM_handle_solaris_signal(sig, info, ucVoid, true);
3811  errno = orig_errno;
3812}
3813
3814// This boolean allows users to forward their own non-matching signals
3815// to JVM_handle_solaris_signal, harmlessly.
3816bool os::Solaris::signal_handlers_are_installed = false;
3817
3818// For signal-chaining
3819bool os::Solaris::libjsig_is_loaded = false;
3820typedef struct sigaction *(*get_signal_t)(int);
3821get_signal_t os::Solaris::get_signal_action = NULL;
3822
3823struct sigaction* os::Solaris::get_chained_signal_action(int sig) {
3824  struct sigaction *actp = NULL;
3825
3826  if ((libjsig_is_loaded)  && (sig <= Maxlibjsigsigs)) {
3827    // Retrieve the old signal handler from libjsig
3828    actp = (*get_signal_action)(sig);
3829  }
3830  if (actp == NULL) {
3831    // Retrieve the preinstalled signal handler from jvm
3832    actp = get_preinstalled_handler(sig);
3833  }
3834
3835  return actp;
3836}
3837
3838static bool call_chained_handler(struct sigaction *actp, int sig,
3839                                 siginfo_t *siginfo, void *context) {
3840  // Call the old signal handler
3841  if (actp->sa_handler == SIG_DFL) {
3842    // It's more reasonable to let jvm treat it as an unexpected exception
3843    // instead of taking the default action.
3844    return false;
3845  } else if (actp->sa_handler != SIG_IGN) {
3846    if ((actp->sa_flags & SA_NODEFER) == 0) {
3847      // automaticlly block the signal
3848      sigaddset(&(actp->sa_mask), sig);
3849    }
3850
3851    sa_handler_t hand;
3852    sa_sigaction_t sa;
3853    bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0;
3854    // retrieve the chained handler
3855    if (siginfo_flag_set) {
3856      sa = actp->sa_sigaction;
3857    } else {
3858      hand = actp->sa_handler;
3859    }
3860
3861    if ((actp->sa_flags & SA_RESETHAND) != 0) {
3862      actp->sa_handler = SIG_DFL;
3863    }
3864
3865    // try to honor the signal mask
3866    sigset_t oset;
3867    pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset);
3868
3869    // call into the chained handler
3870    if (siginfo_flag_set) {
3871      (*sa)(sig, siginfo, context);
3872    } else {
3873      (*hand)(sig);
3874    }
3875
3876    // restore the signal mask
3877    pthread_sigmask(SIG_SETMASK, &oset, 0);
3878  }
3879  // Tell jvm's signal handler the signal is taken care of.
3880  return true;
3881}
3882
3883bool os::Solaris::chained_handler(int sig, siginfo_t* siginfo, void* context) {
3884  bool chained = false;
3885  // signal-chaining
3886  if (UseSignalChaining) {
3887    struct sigaction *actp = get_chained_signal_action(sig);
3888    if (actp != NULL) {
3889      chained = call_chained_handler(actp, sig, siginfo, context);
3890    }
3891  }
3892  return chained;
3893}
3894
3895struct sigaction* os::Solaris::get_preinstalled_handler(int sig) {
3896  assert((chainedsigactions != (struct sigaction *)NULL) &&
3897         (preinstalled_sigs != (int *)NULL), "signals not yet initialized");
3898  if (preinstalled_sigs[sig] != 0) {
3899    return &chainedsigactions[sig];
3900  }
3901  return NULL;
3902}
3903
3904void os::Solaris::save_preinstalled_handler(int sig,
3905                                            struct sigaction& oldAct) {
3906  assert(sig > 0 && sig <= Maxsignum, "vm signal out of expected range");
3907  assert((chainedsigactions != (struct sigaction *)NULL) &&
3908         (preinstalled_sigs != (int *)NULL), "signals not yet initialized");
3909  chainedsigactions[sig] = oldAct;
3910  preinstalled_sigs[sig] = 1;
3911}
3912
3913void os::Solaris::set_signal_handler(int sig, bool set_installed,
3914                                     bool oktochain) {
3915  // Check for overwrite.
3916  struct sigaction oldAct;
3917  sigaction(sig, (struct sigaction*)NULL, &oldAct);
3918  void* oldhand =
3919      oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oldAct.sa_sigaction)
3920                          : CAST_FROM_FN_PTR(void*,  oldAct.sa_handler);
3921  if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
3922      oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
3923      oldhand != CAST_FROM_FN_PTR(void*, signalHandler)) {
3924    if (AllowUserSignalHandlers || !set_installed) {
3925      // Do not overwrite; user takes responsibility to forward to us.
3926      return;
3927    } else if (UseSignalChaining) {
3928      if (oktochain) {
3929        // save the old handler in jvm
3930        save_preinstalled_handler(sig, oldAct);
3931      } else {
3932        vm_exit_during_initialization("Signal chaining not allowed for VM interrupt signal.");
3933      }
3934      // libjsig also interposes the sigaction() call below and saves the
3935      // old sigaction on it own.
3936    } else {
3937      fatal("Encountered unexpected pre-existing sigaction handler "
3938            "%#lx for signal %d.", (long)oldhand, sig);
3939    }
3940  }
3941
3942  struct sigaction sigAct;
3943  sigfillset(&(sigAct.sa_mask));
3944  sigAct.sa_handler = SIG_DFL;
3945
3946  sigAct.sa_sigaction = signalHandler;
3947  // Handle SIGSEGV on alternate signal stack if
3948  // not using stack banging
3949  if (!UseStackBanging && sig == SIGSEGV) {
3950    sigAct.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
3951  } else {
3952    sigAct.sa_flags = SA_SIGINFO | SA_RESTART;
3953  }
3954  os::Solaris::set_our_sigflags(sig, sigAct.sa_flags);
3955
3956  sigaction(sig, &sigAct, &oldAct);
3957
3958  void* oldhand2 = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
3959                                       : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
3960  assert(oldhand2 == oldhand, "no concurrent signal handler installation");
3961}
3962
3963
3964#define DO_SIGNAL_CHECK(sig)                      \
3965  do {                                            \
3966    if (!sigismember(&check_signal_done, sig)) {  \
3967      os::Solaris::check_signal_handler(sig);     \
3968    }                                             \
3969  } while (0)
3970
3971// This method is a periodic task to check for misbehaving JNI applications
3972// under CheckJNI, we can add any periodic checks here
3973
3974void os::run_periodic_checks() {
3975  // A big source of grief is hijacking virt. addr 0x0 on Solaris,
3976  // thereby preventing a NULL checks.
3977  if (!check_addr0_done) check_addr0_done = check_addr0(tty);
3978
3979  if (check_signals == false) return;
3980
3981  // SEGV and BUS if overridden could potentially prevent
3982  // generation of hs*.log in the event of a crash, debugging
3983  // such a case can be very challenging, so we absolutely
3984  // check for the following for a good measure:
3985  DO_SIGNAL_CHECK(SIGSEGV);
3986  DO_SIGNAL_CHECK(SIGILL);
3987  DO_SIGNAL_CHECK(SIGFPE);
3988  DO_SIGNAL_CHECK(SIGBUS);
3989  DO_SIGNAL_CHECK(SIGPIPE);
3990  DO_SIGNAL_CHECK(SIGXFSZ);
3991
3992  // ReduceSignalUsage allows the user to override these handlers
3993  // see comments at the very top and jvm_solaris.h
3994  if (!ReduceSignalUsage) {
3995    DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL);
3996    DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL);
3997    DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL);
3998    DO_SIGNAL_CHECK(BREAK_SIGNAL);
3999  }
4000
4001  // See comments above for using JVM1/JVM2
4002  DO_SIGNAL_CHECK(os::Solaris::SIGasync());
4003
4004}
4005
4006typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);
4007
4008static os_sigaction_t os_sigaction = NULL;
4009
4010void os::Solaris::check_signal_handler(int sig) {
4011  char buf[O_BUFLEN];
4012  address jvmHandler = NULL;
4013
4014  struct sigaction act;
4015  if (os_sigaction == NULL) {
4016    // only trust the default sigaction, in case it has been interposed
4017    os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction");
4018    if (os_sigaction == NULL) return;
4019  }
4020
4021  os_sigaction(sig, (struct sigaction*)NULL, &act);
4022
4023  address thisHandler = (act.sa_flags & SA_SIGINFO)
4024    ? CAST_FROM_FN_PTR(address, act.sa_sigaction)
4025    : CAST_FROM_FN_PTR(address, act.sa_handler);
4026
4027
4028  switch (sig) {
4029  case SIGSEGV:
4030  case SIGBUS:
4031  case SIGFPE:
4032  case SIGPIPE:
4033  case SIGXFSZ:
4034  case SIGILL:
4035    jvmHandler = CAST_FROM_FN_PTR(address, signalHandler);
4036    break;
4037
4038  case SHUTDOWN1_SIGNAL:
4039  case SHUTDOWN2_SIGNAL:
4040  case SHUTDOWN3_SIGNAL:
4041  case BREAK_SIGNAL:
4042    jvmHandler = (address)user_handler();
4043    break;
4044
4045  default:
4046    int asynsig = os::Solaris::SIGasync();
4047
4048    if (sig == asynsig) {
4049      jvmHandler = CAST_FROM_FN_PTR(address, signalHandler);
4050    } else {
4051      return;
4052    }
4053    break;
4054  }
4055
4056
4057  if (thisHandler != jvmHandler) {
4058    tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN));
4059    tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN));
4060    tty->print_cr("  found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN));
4061    // No need to check this sig any longer
4062    sigaddset(&check_signal_done, sig);
4063    // Running under non-interactive shell, SHUTDOWN2_SIGNAL will be reassigned SIG_IGN
4064    if (sig == SHUTDOWN2_SIGNAL && !isatty(fileno(stdin))) {
4065      tty->print_cr("Running in non-interactive shell, %s handler is replaced by shell",
4066                    exception_name(sig, buf, O_BUFLEN));
4067    }
4068  } else if(os::Solaris::get_our_sigflags(sig) != 0 && act.sa_flags != os::Solaris::get_our_sigflags(sig)) {
4069    tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN));
4070    tty->print("expected:");
4071    os::Posix::print_sa_flags(tty, os::Solaris::get_our_sigflags(sig));
4072    tty->cr();
4073    tty->print("  found:");
4074    os::Posix::print_sa_flags(tty, act.sa_flags);
4075    tty->cr();
4076    // No need to check this sig any longer
4077    sigaddset(&check_signal_done, sig);
4078  }
4079
4080  // Print all the signal handler state
4081  if (sigismember(&check_signal_done, sig)) {
4082    print_signal_handlers(tty, buf, O_BUFLEN);
4083  }
4084
4085}
4086
4087void os::Solaris::install_signal_handlers() {
4088  bool libjsigdone = false;
4089  signal_handlers_are_installed = true;
4090
4091  // signal-chaining
4092  typedef void (*signal_setting_t)();
4093  signal_setting_t begin_signal_setting = NULL;
4094  signal_setting_t end_signal_setting = NULL;
4095  begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
4096                                        dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting"));
4097  if (begin_signal_setting != NULL) {
4098    end_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
4099                                        dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));
4100    get_signal_action = CAST_TO_FN_PTR(get_signal_t,
4101                                       dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));
4102    get_libjsig_version = CAST_TO_FN_PTR(version_getting_t,
4103                                         dlsym(RTLD_DEFAULT, "JVM_get_libjsig_version"));
4104    libjsig_is_loaded = true;
4105    if (os::Solaris::get_libjsig_version != NULL) {
4106      libjsigversion =  (*os::Solaris::get_libjsig_version)();
4107    }
4108    assert(UseSignalChaining, "should enable signal-chaining");
4109  }
4110  if (libjsig_is_loaded) {
4111    // Tell libjsig jvm is setting signal handlers
4112    (*begin_signal_setting)();
4113  }
4114
4115  set_signal_handler(SIGSEGV, true, true);
4116  set_signal_handler(SIGPIPE, true, true);
4117  set_signal_handler(SIGXFSZ, true, true);
4118  set_signal_handler(SIGBUS, true, true);
4119  set_signal_handler(SIGILL, true, true);
4120  set_signal_handler(SIGFPE, true, true);
4121
4122
4123  if (os::Solaris::SIGasync() > OLDMAXSIGNUM) {
4124    // Pre-1.4.1 Libjsig limited to signal chaining signals <= 32 so
4125    // can not register overridable signals which might be > 32
4126    if (libjsig_is_loaded && libjsigversion <= JSIG_VERSION_1_4_1) {
4127      // Tell libjsig jvm has finished setting signal handlers
4128      (*end_signal_setting)();
4129      libjsigdone = true;
4130    }
4131  }
4132
4133  set_signal_handler(os::Solaris::SIGasync(), true, true);
4134
4135  if (libjsig_is_loaded && !libjsigdone) {
4136    // Tell libjsig jvm finishes setting signal handlers
4137    (*end_signal_setting)();
4138  }
4139
4140  // We don't activate signal checker if libjsig is in place, we trust ourselves
4141  // and if UserSignalHandler is installed all bets are off.
4142  // Log that signal checking is off only if -verbose:jni is specified.
4143  if (CheckJNICalls) {
4144    if (libjsig_is_loaded) {
4145      if (PrintJNIResolving) {
4146        tty->print_cr("Info: libjsig is activated, all active signal checking is disabled");
4147      }
4148      check_signals = false;
4149    }
4150    if (AllowUserSignalHandlers) {
4151      if (PrintJNIResolving) {
4152        tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
4153      }
4154      check_signals = false;
4155    }
4156  }
4157}
4158
4159
4160void report_error(const char* file_name, int line_no, const char* title,
4161                  const char* format, ...);
4162
4163// (Static) wrappers for the liblgrp API
4164os::Solaris::lgrp_home_func_t os::Solaris::_lgrp_home;
4165os::Solaris::lgrp_init_func_t os::Solaris::_lgrp_init;
4166os::Solaris::lgrp_fini_func_t os::Solaris::_lgrp_fini;
4167os::Solaris::lgrp_root_func_t os::Solaris::_lgrp_root;
4168os::Solaris::lgrp_children_func_t os::Solaris::_lgrp_children;
4169os::Solaris::lgrp_resources_func_t os::Solaris::_lgrp_resources;
4170os::Solaris::lgrp_nlgrps_func_t os::Solaris::_lgrp_nlgrps;
4171os::Solaris::lgrp_cookie_stale_func_t os::Solaris::_lgrp_cookie_stale;
4172os::Solaris::lgrp_cookie_t os::Solaris::_lgrp_cookie = 0;
4173
4174static address resolve_symbol_lazy(const char* name) {
4175  address addr = (address) dlsym(RTLD_DEFAULT, name);
4176  if (addr == NULL) {
4177    // RTLD_DEFAULT was not defined on some early versions of 2.5.1
4178    addr = (address) dlsym(RTLD_NEXT, name);
4179  }
4180  return addr;
4181}
4182
4183static address resolve_symbol(const char* name) {
4184  address addr = resolve_symbol_lazy(name);
4185  if (addr == NULL) {
4186    fatal(dlerror());
4187  }
4188  return addr;
4189}
4190
4191void os::Solaris::libthread_init() {
4192  address func = (address)dlsym(RTLD_DEFAULT, "_thr_suspend_allmutators");
4193
4194  lwp_priocntl_init();
4195
4196  // RTLD_DEFAULT was not defined on some early versions of 5.5.1
4197  if (func == NULL) {
4198    func = (address) dlsym(RTLD_NEXT, "_thr_suspend_allmutators");
4199    // Guarantee that this VM is running on an new enough OS (5.6 or
4200    // later) that it will have a new enough libthread.so.
4201    guarantee(func != NULL, "libthread.so is too old.");
4202  }
4203
4204  int size;
4205  void (*handler_info_func)(address *, int *);
4206  handler_info_func = CAST_TO_FN_PTR(void (*)(address *, int *), resolve_symbol("thr_sighndlrinfo"));
4207  handler_info_func(&handler_start, &size);
4208  handler_end = handler_start + size;
4209}
4210
4211
4212int_fnP_mutex_tP os::Solaris::_mutex_lock;
4213int_fnP_mutex_tP os::Solaris::_mutex_trylock;
4214int_fnP_mutex_tP os::Solaris::_mutex_unlock;
4215int_fnP_mutex_tP_i_vP os::Solaris::_mutex_init;
4216int_fnP_mutex_tP os::Solaris::_mutex_destroy;
4217int os::Solaris::_mutex_scope = USYNC_THREAD;
4218
4219int_fnP_cond_tP_mutex_tP_timestruc_tP os::Solaris::_cond_timedwait;
4220int_fnP_cond_tP_mutex_tP os::Solaris::_cond_wait;
4221int_fnP_cond_tP os::Solaris::_cond_signal;
4222int_fnP_cond_tP os::Solaris::_cond_broadcast;
4223int_fnP_cond_tP_i_vP os::Solaris::_cond_init;
4224int_fnP_cond_tP os::Solaris::_cond_destroy;
4225int os::Solaris::_cond_scope = USYNC_THREAD;
4226
4227void os::Solaris::synchronization_init() {
4228  if (UseLWPSynchronization) {
4229    os::Solaris::set_mutex_lock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("_lwp_mutex_lock")));
4230    os::Solaris::set_mutex_trylock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("_lwp_mutex_trylock")));
4231    os::Solaris::set_mutex_unlock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("_lwp_mutex_unlock")));
4232    os::Solaris::set_mutex_init(lwp_mutex_init);
4233    os::Solaris::set_mutex_destroy(lwp_mutex_destroy);
4234    os::Solaris::set_mutex_scope(USYNC_THREAD);
4235
4236    os::Solaris::set_cond_timedwait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP_timestruc_tP, resolve_symbol("_lwp_cond_timedwait")));
4237    os::Solaris::set_cond_wait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP, resolve_symbol("_lwp_cond_wait")));
4238    os::Solaris::set_cond_signal(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("_lwp_cond_signal")));
4239    os::Solaris::set_cond_broadcast(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("_lwp_cond_broadcast")));
4240    os::Solaris::set_cond_init(lwp_cond_init);
4241    os::Solaris::set_cond_destroy(lwp_cond_destroy);
4242    os::Solaris::set_cond_scope(USYNC_THREAD);
4243  } else {
4244    os::Solaris::set_mutex_scope(USYNC_THREAD);
4245    os::Solaris::set_cond_scope(USYNC_THREAD);
4246
4247    if (UsePthreads) {
4248      os::Solaris::set_mutex_lock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_lock")));
4249      os::Solaris::set_mutex_trylock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_trylock")));
4250      os::Solaris::set_mutex_unlock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_unlock")));
4251      os::Solaris::set_mutex_init(pthread_mutex_default_init);
4252      os::Solaris::set_mutex_destroy(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_destroy")));
4253
4254      os::Solaris::set_cond_timedwait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP_timestruc_tP, resolve_symbol("pthread_cond_timedwait")));
4255      os::Solaris::set_cond_wait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP, resolve_symbol("pthread_cond_wait")));
4256      os::Solaris::set_cond_signal(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("pthread_cond_signal")));
4257      os::Solaris::set_cond_broadcast(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("pthread_cond_broadcast")));
4258      os::Solaris::set_cond_init(pthread_cond_default_init);
4259      os::Solaris::set_cond_destroy(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("pthread_cond_destroy")));
4260    } else {
4261      os::Solaris::set_mutex_lock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("mutex_lock")));
4262      os::Solaris::set_mutex_trylock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("mutex_trylock")));
4263      os::Solaris::set_mutex_unlock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("mutex_unlock")));
4264      os::Solaris::set_mutex_init(::mutex_init);
4265      os::Solaris::set_mutex_destroy(::mutex_destroy);
4266
4267      os::Solaris::set_cond_timedwait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP_timestruc_tP, resolve_symbol("cond_timedwait")));
4268      os::Solaris::set_cond_wait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP, resolve_symbol("cond_wait")));
4269      os::Solaris::set_cond_signal(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("cond_signal")));
4270      os::Solaris::set_cond_broadcast(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("cond_broadcast")));
4271      os::Solaris::set_cond_init(::cond_init);
4272      os::Solaris::set_cond_destroy(::cond_destroy);
4273    }
4274  }
4275}
4276
4277bool os::Solaris::liblgrp_init() {
4278  void *handle = dlopen("liblgrp.so.1", RTLD_LAZY);
4279  if (handle != NULL) {
4280    os::Solaris::set_lgrp_home(CAST_TO_FN_PTR(lgrp_home_func_t, dlsym(handle, "lgrp_home")));
4281    os::Solaris::set_lgrp_init(CAST_TO_FN_PTR(lgrp_init_func_t, dlsym(handle, "lgrp_init")));
4282    os::Solaris::set_lgrp_fini(CAST_TO_FN_PTR(lgrp_fini_func_t, dlsym(handle, "lgrp_fini")));
4283    os::Solaris::set_lgrp_root(CAST_TO_FN_PTR(lgrp_root_func_t, dlsym(handle, "lgrp_root")));
4284    os::Solaris::set_lgrp_children(CAST_TO_FN_PTR(lgrp_children_func_t, dlsym(handle, "lgrp_children")));
4285    os::Solaris::set_lgrp_resources(CAST_TO_FN_PTR(lgrp_resources_func_t, dlsym(handle, "lgrp_resources")));
4286    os::Solaris::set_lgrp_nlgrps(CAST_TO_FN_PTR(lgrp_nlgrps_func_t, dlsym(handle, "lgrp_nlgrps")));
4287    os::Solaris::set_lgrp_cookie_stale(CAST_TO_FN_PTR(lgrp_cookie_stale_func_t,
4288                                                      dlsym(handle, "lgrp_cookie_stale")));
4289
4290    lgrp_cookie_t c = lgrp_init(LGRP_VIEW_CALLER);
4291    set_lgrp_cookie(c);
4292    return true;
4293  }
4294  return false;
4295}
4296
4297// int pset_getloadavg(psetid_t pset, double loadavg[], int nelem);
4298typedef long (*pset_getloadavg_type)(psetid_t pset, double loadavg[], int nelem);
4299static pset_getloadavg_type pset_getloadavg_ptr = NULL;
4300
4301void init_pset_getloadavg_ptr(void) {
4302  pset_getloadavg_ptr =
4303    (pset_getloadavg_type)dlsym(RTLD_DEFAULT, "pset_getloadavg");
4304  if (pset_getloadavg_ptr == NULL) {
4305    log_warning(os)("pset_getloadavg function not found");
4306  }
4307}
4308
4309int os::Solaris::_dev_zero_fd = -1;
4310
4311// this is called _before_ the global arguments have been parsed
4312void os::init(void) {
4313  _initial_pid = getpid();
4314
4315  max_hrtime = first_hrtime = gethrtime();
4316
4317  init_random(1234567);
4318
4319  page_size = sysconf(_SC_PAGESIZE);
4320  if (page_size == -1) {
4321    fatal("os_solaris.cpp: os::init: sysconf failed (%s)", os::strerror(errno));
4322  }
4323  init_page_sizes((size_t) page_size);
4324
4325  Solaris::initialize_system_info();
4326
4327  int fd = ::open("/dev/zero", O_RDWR);
4328  if (fd < 0) {
4329    fatal("os::init: cannot open /dev/zero (%s)", os::strerror(errno));
4330  } else {
4331    Solaris::set_dev_zero_fd(fd);
4332
4333    // Close on exec, child won't inherit.
4334    fcntl(fd, F_SETFD, FD_CLOEXEC);
4335  }
4336
4337  clock_tics_per_sec = CLK_TCK;
4338
4339  // check if dladdr1() exists; dladdr1 can provide more information than
4340  // dladdr for os::dll_address_to_function_name. It comes with SunOS 5.9
4341  // and is available on linker patches for 5.7 and 5.8.
4342  // libdl.so must have been loaded, this call is just an entry lookup
4343  void * hdl = dlopen("libdl.so", RTLD_NOW);
4344  if (hdl) {
4345    dladdr1_func = CAST_TO_FN_PTR(dladdr1_func_type, dlsym(hdl, "dladdr1"));
4346  }
4347
4348  // (Solaris only) this switches to calls that actually do locking.
4349  ThreadCritical::initialize();
4350
4351  main_thread = thr_self();
4352
4353  // Constant minimum stack size allowed. It must be at least
4354  // the minimum of what the OS supports (thr_min_stack()), and
4355  // enough to allow the thread to get to user bytecode execution.
4356  Posix::_compiler_thread_min_stack_allowed = MAX2(thr_min_stack(),
4357                                                   Posix::_compiler_thread_min_stack_allowed);
4358  Posix::_java_thread_min_stack_allowed = MAX2(thr_min_stack(),
4359                                               Posix::_java_thread_min_stack_allowed);
4360  Posix::_vm_internal_thread_min_stack_allowed = MAX2(thr_min_stack(),
4361                                                      Posix::_vm_internal_thread_min_stack_allowed);
4362
4363  // dynamic lookup of functions that may not be available in our lowest
4364  // supported Solaris release
4365  void * handle = dlopen("libc.so.1", RTLD_LAZY);
4366  if (handle != NULL) {
4367    Solaris::_pthread_setname_np =  // from 11.3
4368        (Solaris::pthread_setname_np_func_t)dlsym(handle, "pthread_setname_np");
4369  }
4370}
4371
4372// To install functions for atexit system call
4373extern "C" {
4374  static void perfMemory_exit_helper() {
4375    perfMemory_exit();
4376  }
4377}
4378
4379// this is called _after_ the global arguments have been parsed
4380jint os::init_2(void) {
4381  // try to enable extended file IO ASAP, see 6431278
4382  os::Solaris::try_enable_extended_io();
4383
4384  // Allocate a single page and mark it as readable for safepoint polling.  Also
4385  // use this first mmap call to check support for MAP_ALIGN.
4386  address polling_page = (address)Solaris::mmap_chunk((char*)page_size,
4387                                                      page_size,
4388                                                      MAP_PRIVATE | MAP_ALIGN,
4389                                                      PROT_READ);
4390  if (polling_page == NULL) {
4391    has_map_align = false;
4392    polling_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE,
4393                                                PROT_READ);
4394  }
4395
4396  os::set_polling_page(polling_page);
4397  log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4398
4399  if (!UseMembar) {
4400    address mem_serialize_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE, PROT_READ | PROT_WRITE);
4401    guarantee(mem_serialize_page != NULL, "mmap Failed for memory serialize page");
4402    os::set_memory_serialize_page(mem_serialize_page);
4403    log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4404  }
4405
4406  // Check and sets minimum stack sizes against command line options
4407  if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
4408    return JNI_ERR;
4409  }
4410
4411  Solaris::libthread_init();
4412
4413  if (UseNUMA) {
4414    if (!Solaris::liblgrp_init()) {
4415      UseNUMA = false;
4416    } else {
4417      size_t lgrp_limit = os::numa_get_groups_num();
4418      int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit, mtInternal);
4419      size_t lgrp_num = os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
4420      FREE_C_HEAP_ARRAY(int, lgrp_ids);
4421      if (lgrp_num < 2) {
4422        // There's only one locality group, disable NUMA.
4423        UseNUMA = false;
4424      }
4425    }
4426    if (!UseNUMA && ForceNUMA) {
4427      UseNUMA = true;
4428    }
4429  }
4430
4431  Solaris::signal_sets_init();
4432  Solaris::init_signal_mem();
4433  Solaris::install_signal_handlers();
4434
4435  if (libjsigversion < JSIG_VERSION_1_4_1) {
4436    Maxlibjsigsigs = OLDMAXSIGNUM;
4437  }
4438
4439  // initialize synchronization primitives to use either thread or
4440  // lwp synchronization (controlled by UseLWPSynchronization)
4441  Solaris::synchronization_init();
4442
4443  if (MaxFDLimit) {
4444    // set the number of file descriptors to max. print out error
4445    // if getrlimit/setrlimit fails but continue regardless.
4446    struct rlimit nbr_files;
4447    int status = getrlimit(RLIMIT_NOFILE, &nbr_files);
4448    if (status != 0) {
4449      log_info(os)("os::init_2 getrlimit failed: %s", os::strerror(errno));
4450    } else {
4451      nbr_files.rlim_cur = nbr_files.rlim_max;
4452      status = setrlimit(RLIMIT_NOFILE, &nbr_files);
4453      if (status != 0) {
4454        log_info(os)("os::init_2 setrlimit failed: %s", os::strerror(errno));
4455      }
4456    }
4457  }
4458
4459  // Calculate theoretical max. size of Threads to guard gainst
4460  // artifical out-of-memory situations, where all available address-
4461  // space has been reserved by thread stacks. Default stack size is 1Mb.
4462  size_t pre_thread_stack_size = (JavaThread::stack_size_at_create()) ?
4463    JavaThread::stack_size_at_create() : (1*K*K);
4464  assert(pre_thread_stack_size != 0, "Must have a stack");
4465  // Solaris has a maximum of 4Gb of user programs. Calculate the thread limit when
4466  // we should start doing Virtual Memory banging. Currently when the threads will
4467  // have used all but 200Mb of space.
4468  size_t max_address_space = ((unsigned int)4 * K * K * K) - (200 * K * K);
4469  Solaris::_os_thread_limit = max_address_space / pre_thread_stack_size;
4470
4471  // at-exit methods are called in the reverse order of their registration.
4472  // In Solaris 7 and earlier, atexit functions are called on return from
4473  // main or as a result of a call to exit(3C). There can be only 32 of
4474  // these functions registered and atexit() does not set errno. In Solaris
4475  // 8 and later, there is no limit to the number of functions registered
4476  // and atexit() sets errno. In addition, in Solaris 8 and later, atexit
4477  // functions are called upon dlclose(3DL) in addition to return from main
4478  // and exit(3C).
4479
4480  if (PerfAllowAtExitRegistration) {
4481    // only register atexit functions if PerfAllowAtExitRegistration is set.
4482    // atexit functions can be delayed until process exit time, which
4483    // can be problematic for embedded VM situations. Embedded VMs should
4484    // call DestroyJavaVM() to assure that VM resources are released.
4485
4486    // note: perfMemory_exit_helper atexit function may be removed in
4487    // the future if the appropriate cleanup code can be added to the
4488    // VM_Exit VMOperation's doit method.
4489    if (atexit(perfMemory_exit_helper) != 0) {
4490      warning("os::init2 atexit(perfMemory_exit_helper) failed");
4491    }
4492  }
4493
4494  // Init pset_loadavg function pointer
4495  init_pset_getloadavg_ptr();
4496
4497  return JNI_OK;
4498}
4499
4500// Mark the polling page as unreadable
4501void os::make_polling_page_unreadable(void) {
4502  if (mprotect((char *)_polling_page, page_size, PROT_NONE) != 0) {
4503    fatal("Could not disable polling page");
4504  }
4505}
4506
4507// Mark the polling page as readable
4508void os::make_polling_page_readable(void) {
4509  if (mprotect((char *)_polling_page, page_size, PROT_READ) != 0) {
4510    fatal("Could not enable polling page");
4511  }
4512}
4513
4514// Is a (classpath) directory empty?
4515bool os::dir_is_empty(const char* path) {
4516  DIR *dir = NULL;
4517  struct dirent *ptr;
4518
4519  dir = opendir(path);
4520  if (dir == NULL) return true;
4521
4522  // Scan the directory
4523  bool result = true;
4524  char buf[sizeof(struct dirent) + MAX_PATH];
4525  struct dirent *dbuf = (struct dirent *) buf;
4526  while (result && (ptr = readdir(dir, dbuf)) != NULL) {
4527    if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
4528      result = false;
4529    }
4530  }
4531  closedir(dir);
4532  return result;
4533}
4534
4535// This code originates from JDK's sysOpen and open64_w
4536// from src/solaris/hpi/src/system_md.c
4537
4538int os::open(const char *path, int oflag, int mode) {
4539  if (strlen(path) > MAX_PATH - 1) {
4540    errno = ENAMETOOLONG;
4541    return -1;
4542  }
4543  int fd;
4544
4545  fd = ::open64(path, oflag, mode);
4546  if (fd == -1) return -1;
4547
4548  // If the open succeeded, the file might still be a directory
4549  {
4550    struct stat64 buf64;
4551    int ret = ::fstat64(fd, &buf64);
4552    int st_mode = buf64.st_mode;
4553
4554    if (ret != -1) {
4555      if ((st_mode & S_IFMT) == S_IFDIR) {
4556        errno = EISDIR;
4557        ::close(fd);
4558        return -1;
4559      }
4560    } else {
4561      ::close(fd);
4562      return -1;
4563    }
4564  }
4565
4566  // 32-bit Solaris systems suffer from:
4567  //
4568  // - an historical default soft limit of 256 per-process file
4569  //   descriptors that is too low for many Java programs.
4570  //
4571  // - a design flaw where file descriptors created using stdio
4572  //   fopen must be less than 256, _even_ when the first limit above
4573  //   has been raised.  This can cause calls to fopen (but not calls to
4574  //   open, for example) to fail mysteriously, perhaps in 3rd party
4575  //   native code (although the JDK itself uses fopen).  One can hardly
4576  //   criticize them for using this most standard of all functions.
4577  //
4578  // We attempt to make everything work anyways by:
4579  //
4580  // - raising the soft limit on per-process file descriptors beyond
4581  //   256
4582  //
4583  // - As of Solaris 10u4, we can request that Solaris raise the 256
4584  //   stdio fopen limit by calling function enable_extended_FILE_stdio.
4585  //   This is done in init_2 and recorded in enabled_extended_FILE_stdio
4586  //
4587  // - If we are stuck on an old (pre 10u4) Solaris system, we can
4588  //   workaround the bug by remapping non-stdio file descriptors below
4589  //   256 to ones beyond 256, which is done below.
4590  //
4591  // See:
4592  // 1085341: 32-bit stdio routines should support file descriptors >255
4593  // 6533291: Work around 32-bit Solaris stdio limit of 256 open files
4594  // 6431278: Netbeans crash on 32 bit Solaris: need to call
4595  //          enable_extended_FILE_stdio() in VM initialisation
4596  // Giri Mandalika's blog
4597  // http://technopark02.blogspot.com/2005_05_01_archive.html
4598  //
4599#ifndef  _LP64
4600  if ((!enabled_extended_FILE_stdio) && fd < 256) {
4601    int newfd = ::fcntl(fd, F_DUPFD, 256);
4602    if (newfd != -1) {
4603      ::close(fd);
4604      fd = newfd;
4605    }
4606  }
4607#endif // 32-bit Solaris
4608
4609  // All file descriptors that are opened in the JVM and not
4610  // specifically destined for a subprocess should have the
4611  // close-on-exec flag set.  If we don't set it, then careless 3rd
4612  // party native code might fork and exec without closing all
4613  // appropriate file descriptors (e.g. as we do in closeDescriptors in
4614  // UNIXProcess.c), and this in turn might:
4615  //
4616  // - cause end-of-file to fail to be detected on some file
4617  //   descriptors, resulting in mysterious hangs, or
4618  //
4619  // - might cause an fopen in the subprocess to fail on a system
4620  //   suffering from bug 1085341.
4621  //
4622  // (Yes, the default setting of the close-on-exec flag is a Unix
4623  // design flaw)
4624  //
4625  // See:
4626  // 1085341: 32-bit stdio routines should support file descriptors >255
4627  // 4843136: (process) pipe file descriptor from Runtime.exec not being closed
4628  // 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9
4629  //
4630#ifdef FD_CLOEXEC
4631  {
4632    int flags = ::fcntl(fd, F_GETFD);
4633    if (flags != -1) {
4634      ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4635    }
4636  }
4637#endif
4638
4639  return fd;
4640}
4641
4642// create binary file, rewriting existing file if required
4643int os::create_binary_file(const char* path, bool rewrite_existing) {
4644  int oflags = O_WRONLY | O_CREAT;
4645  if (!rewrite_existing) {
4646    oflags |= O_EXCL;
4647  }
4648  return ::open64(path, oflags, S_IREAD | S_IWRITE);
4649}
4650
4651// return current position of file pointer
4652jlong os::current_file_offset(int fd) {
4653  return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR);
4654}
4655
4656// move file pointer to the specified offset
4657jlong os::seek_to_file_offset(int fd, jlong offset) {
4658  return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);
4659}
4660
4661jlong os::lseek(int fd, jlong offset, int whence) {
4662  return (jlong) ::lseek64(fd, offset, whence);
4663}
4664
4665char * os::native_path(char *path) {
4666  return path;
4667}
4668
4669int os::ftruncate(int fd, jlong length) {
4670  return ::ftruncate64(fd, length);
4671}
4672
4673int os::fsync(int fd)  {
4674  RESTARTABLE_RETURN_INT(::fsync(fd));
4675}
4676
4677int os::available(int fd, jlong *bytes) {
4678  assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_native,
4679         "Assumed _thread_in_native");
4680  jlong cur, end;
4681  int mode;
4682  struct stat64 buf64;
4683
4684  if (::fstat64(fd, &buf64) >= 0) {
4685    mode = buf64.st_mode;
4686    if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
4687      int n,ioctl_return;
4688
4689      RESTARTABLE(::ioctl(fd, FIONREAD, &n), ioctl_return);
4690      if (ioctl_return>= 0) {
4691        *bytes = n;
4692        return 1;
4693      }
4694    }
4695  }
4696  if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) {
4697    return 0;
4698  } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) {
4699    return 0;
4700  } else if (::lseek64(fd, cur, SEEK_SET) == -1) {
4701    return 0;
4702  }
4703  *bytes = end - cur;
4704  return 1;
4705}
4706
4707// Map a block of memory.
4708char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
4709                        char *addr, size_t bytes, bool read_only,
4710                        bool allow_exec) {
4711  int prot;
4712  int flags;
4713
4714  if (read_only) {
4715    prot = PROT_READ;
4716    flags = MAP_SHARED;
4717  } else {
4718    prot = PROT_READ | PROT_WRITE;
4719    flags = MAP_PRIVATE;
4720  }
4721
4722  if (allow_exec) {
4723    prot |= PROT_EXEC;
4724  }
4725
4726  if (addr != NULL) {
4727    flags |= MAP_FIXED;
4728  }
4729
4730  char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
4731                                     fd, file_offset);
4732  if (mapped_address == MAP_FAILED) {
4733    return NULL;
4734  }
4735  return mapped_address;
4736}
4737
4738
4739// Remap a block of memory.
4740char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
4741                          char *addr, size_t bytes, bool read_only,
4742                          bool allow_exec) {
4743  // same as map_memory() on this OS
4744  return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
4745                        allow_exec);
4746}
4747
4748
4749// Unmap a block of memory.
4750bool os::pd_unmap_memory(char* addr, size_t bytes) {
4751  return munmap(addr, bytes) == 0;
4752}
4753
4754void os::pause() {
4755  char filename[MAX_PATH];
4756  if (PauseAtStartupFile && PauseAtStartupFile[0]) {
4757    jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
4758  } else {
4759    jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
4760  }
4761
4762  int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
4763  if (fd != -1) {
4764    struct stat buf;
4765    ::close(fd);
4766    while (::stat(filename, &buf) == 0) {
4767      (void)::poll(NULL, 0, 100);
4768    }
4769  } else {
4770    jio_fprintf(stderr,
4771                "Could not open pause file '%s', continuing immediately.\n", filename);
4772  }
4773}
4774
4775#ifndef PRODUCT
4776#ifdef INTERPOSE_ON_SYSTEM_SYNCH_FUNCTIONS
4777// Turn this on if you need to trace synch operations.
4778// Set RECORD_SYNCH_LIMIT to a large-enough value,
4779// and call record_synch_enable and record_synch_disable
4780// around the computation of interest.
4781
4782void record_synch(char* name, bool returning);  // defined below
4783
4784class RecordSynch {
4785  char* _name;
4786 public:
4787  RecordSynch(char* name) :_name(name) { record_synch(_name, false); }
4788  ~RecordSynch()                       { record_synch(_name, true); }
4789};
4790
4791#define CHECK_SYNCH_OP(ret, name, params, args, inner)          \
4792extern "C" ret name params {                                    \
4793  typedef ret name##_t params;                                  \
4794  static name##_t* implem = NULL;                               \
4795  static int callcount = 0;                                     \
4796  if (implem == NULL) {                                         \
4797    implem = (name##_t*) dlsym(RTLD_NEXT, #name);               \
4798    if (implem == NULL)  fatal(dlerror());                      \
4799  }                                                             \
4800  ++callcount;                                                  \
4801  RecordSynch _rs(#name);                                       \
4802  inner;                                                        \
4803  return implem args;                                           \
4804}
4805// in dbx, examine callcounts this way:
4806// for n in $(eval whereis callcount | awk '{print $2}'); do print $n; done
4807
4808#define CHECK_POINTER_OK(p) \
4809  (!Universe::is_fully_initialized() || !Universe::is_reserved_heap((oop)(p)))
4810#define CHECK_MU \
4811  if (!CHECK_POINTER_OK(mu)) fatal("Mutex must be in C heap only.");
4812#define CHECK_CV \
4813  if (!CHECK_POINTER_OK(cv)) fatal("Condvar must be in C heap only.");
4814#define CHECK_P(p) \
4815  if (!CHECK_POINTER_OK(p))  fatal(false,  "Pointer must be in C heap only.");
4816
4817#define CHECK_MUTEX(mutex_op) \
4818  CHECK_SYNCH_OP(int, mutex_op, (mutex_t *mu), (mu), CHECK_MU);
4819
4820CHECK_MUTEX(   mutex_lock)
4821CHECK_MUTEX(  _mutex_lock)
4822CHECK_MUTEX( mutex_unlock)
4823CHECK_MUTEX(_mutex_unlock)
4824CHECK_MUTEX( mutex_trylock)
4825CHECK_MUTEX(_mutex_trylock)
4826
4827#define CHECK_COND(cond_op) \
4828  CHECK_SYNCH_OP(int, cond_op, (cond_t *cv, mutex_t *mu), (cv, mu), CHECK_MU; CHECK_CV);
4829
4830CHECK_COND( cond_wait);
4831CHECK_COND(_cond_wait);
4832CHECK_COND(_cond_wait_cancel);
4833
4834#define CHECK_COND2(cond_op) \
4835  CHECK_SYNCH_OP(int, cond_op, (cond_t *cv, mutex_t *mu, timestruc_t* ts), (cv, mu, ts), CHECK_MU; CHECK_CV);
4836
4837CHECK_COND2( cond_timedwait);
4838CHECK_COND2(_cond_timedwait);
4839CHECK_COND2(_cond_timedwait_cancel);
4840
4841// do the _lwp_* versions too
4842#define mutex_t lwp_mutex_t
4843#define cond_t  lwp_cond_t
4844CHECK_MUTEX(  _lwp_mutex_lock)
4845CHECK_MUTEX(  _lwp_mutex_unlock)
4846CHECK_MUTEX(  _lwp_mutex_trylock)
4847CHECK_MUTEX( __lwp_mutex_lock)
4848CHECK_MUTEX( __lwp_mutex_unlock)
4849CHECK_MUTEX( __lwp_mutex_trylock)
4850CHECK_MUTEX(___lwp_mutex_lock)
4851CHECK_MUTEX(___lwp_mutex_unlock)
4852
4853CHECK_COND(  _lwp_cond_wait);
4854CHECK_COND( __lwp_cond_wait);
4855CHECK_COND(___lwp_cond_wait);
4856
4857CHECK_COND2(  _lwp_cond_timedwait);
4858CHECK_COND2( __lwp_cond_timedwait);
4859#undef mutex_t
4860#undef cond_t
4861
4862CHECK_SYNCH_OP(int, _lwp_suspend2,       (int lwp, int *n), (lwp, n), 0);
4863CHECK_SYNCH_OP(int,__lwp_suspend2,       (int lwp, int *n), (lwp, n), 0);
4864CHECK_SYNCH_OP(int, _lwp_kill,           (int lwp, int n),  (lwp, n), 0);
4865CHECK_SYNCH_OP(int,__lwp_kill,           (int lwp, int n),  (lwp, n), 0);
4866CHECK_SYNCH_OP(int, _lwp_sema_wait,      (lwp_sema_t* p),   (p),  CHECK_P(p));
4867CHECK_SYNCH_OP(int,__lwp_sema_wait,      (lwp_sema_t* p),   (p),  CHECK_P(p));
4868CHECK_SYNCH_OP(int, _lwp_cond_broadcast, (lwp_cond_t* cv),  (cv), CHECK_CV);
4869CHECK_SYNCH_OP(int,__lwp_cond_broadcast, (lwp_cond_t* cv),  (cv), CHECK_CV);
4870
4871
4872// recording machinery:
4873
4874enum { RECORD_SYNCH_LIMIT = 200 };
4875char* record_synch_name[RECORD_SYNCH_LIMIT];
4876void* record_synch_arg0ptr[RECORD_SYNCH_LIMIT];
4877bool record_synch_returning[RECORD_SYNCH_LIMIT];
4878thread_t record_synch_thread[RECORD_SYNCH_LIMIT];
4879int record_synch_count = 0;
4880bool record_synch_enabled = false;
4881
4882// in dbx, examine recorded data this way:
4883// for n in name arg0ptr returning thread; do print record_synch_$n[0..record_synch_count-1]; done
4884
4885void record_synch(char* name, bool returning) {
4886  if (record_synch_enabled) {
4887    if (record_synch_count < RECORD_SYNCH_LIMIT) {
4888      record_synch_name[record_synch_count] = name;
4889      record_synch_returning[record_synch_count] = returning;
4890      record_synch_thread[record_synch_count] = thr_self();
4891      record_synch_arg0ptr[record_synch_count] = &name;
4892      record_synch_count++;
4893    }
4894    // put more checking code here:
4895    // ...
4896  }
4897}
4898
4899void record_synch_enable() {
4900  // start collecting trace data, if not already doing so
4901  if (!record_synch_enabled)  record_synch_count = 0;
4902  record_synch_enabled = true;
4903}
4904
4905void record_synch_disable() {
4906  // stop collecting trace data
4907  record_synch_enabled = false;
4908}
4909
4910#endif // INTERPOSE_ON_SYSTEM_SYNCH_FUNCTIONS
4911#endif // PRODUCT
4912
4913const intptr_t thr_time_off  = (intptr_t)(&((prusage_t *)(NULL))->pr_utime);
4914const intptr_t thr_time_size = (intptr_t)(&((prusage_t *)(NULL))->pr_ttime) -
4915                               (intptr_t)(&((prusage_t *)(NULL))->pr_utime);
4916
4917
4918// JVMTI & JVM monitoring and management support
4919// The thread_cpu_time() and current_thread_cpu_time() are only
4920// supported if is_thread_cpu_time_supported() returns true.
4921// They are not supported on Solaris T1.
4922
4923// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
4924// are used by JVM M&M and JVMTI to get user+sys or user CPU time
4925// of a thread.
4926//
4927// current_thread_cpu_time() and thread_cpu_time(Thread *)
4928// returns the fast estimate available on the platform.
4929
4930// hrtime_t gethrvtime() return value includes
4931// user time but does not include system time
4932jlong os::current_thread_cpu_time() {
4933  return (jlong) gethrvtime();
4934}
4935
4936jlong os::thread_cpu_time(Thread *thread) {
4937  // return user level CPU time only to be consistent with
4938  // what current_thread_cpu_time returns.
4939  // thread_cpu_time_info() must be changed if this changes
4940  return os::thread_cpu_time(thread, false /* user time only */);
4941}
4942
4943jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
4944  if (user_sys_cpu_time) {
4945    return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
4946  } else {
4947    return os::current_thread_cpu_time();
4948  }
4949}
4950
4951jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
4952  char proc_name[64];
4953  int count;
4954  prusage_t prusage;
4955  jlong lwp_time;
4956  int fd;
4957
4958  sprintf(proc_name, "/proc/%d/lwp/%d/lwpusage",
4959          getpid(),
4960          thread->osthread()->lwp_id());
4961  fd = ::open(proc_name, O_RDONLY);
4962  if (fd == -1) return -1;
4963
4964  do {
4965    count = ::pread(fd,
4966                    (void *)&prusage.pr_utime,
4967                    thr_time_size,
4968                    thr_time_off);
4969  } while (count < 0 && errno == EINTR);
4970  ::close(fd);
4971  if (count < 0) return -1;
4972
4973  if (user_sys_cpu_time) {
4974    // user + system CPU time
4975    lwp_time = (((jlong)prusage.pr_stime.tv_sec +
4976                 (jlong)prusage.pr_utime.tv_sec) * (jlong)1000000000) +
4977                 (jlong)prusage.pr_stime.tv_nsec +
4978                 (jlong)prusage.pr_utime.tv_nsec;
4979  } else {
4980    // user level CPU time only
4981    lwp_time = ((jlong)prusage.pr_utime.tv_sec * (jlong)1000000000) +
4982                (jlong)prusage.pr_utime.tv_nsec;
4983  }
4984
4985  return (lwp_time);
4986}
4987
4988void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4989  info_ptr->max_value = ALL_64_BITS;      // will not wrap in less than 64 bits
4990  info_ptr->may_skip_backward = false;    // elapsed time not wall time
4991  info_ptr->may_skip_forward = false;     // elapsed time not wall time
4992  info_ptr->kind = JVMTI_TIMER_USER_CPU;  // only user time is returned
4993}
4994
4995void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4996  info_ptr->max_value = ALL_64_BITS;      // will not wrap in less than 64 bits
4997  info_ptr->may_skip_backward = false;    // elapsed time not wall time
4998  info_ptr->may_skip_forward = false;     // elapsed time not wall time
4999  info_ptr->kind = JVMTI_TIMER_USER_CPU;  // only user time is returned
5000}
5001
5002bool os::is_thread_cpu_time_supported() {
5003  return true;
5004}
5005
5006// System loadavg support.  Returns -1 if load average cannot be obtained.
5007// Return the load average for our processor set if the primitive exists
5008// (Solaris 9 and later).  Otherwise just return system wide loadavg.
5009int os::loadavg(double loadavg[], int nelem) {
5010  if (pset_getloadavg_ptr != NULL) {
5011    return (*pset_getloadavg_ptr)(PS_MYID, loadavg, nelem);
5012  } else {
5013    return ::getloadavg(loadavg, nelem);
5014  }
5015}
5016
5017//---------------------------------------------------------------------------------
5018
5019bool os::find(address addr, outputStream* st) {
5020  Dl_info dlinfo;
5021  memset(&dlinfo, 0, sizeof(dlinfo));
5022  if (dladdr(addr, &dlinfo) != 0) {
5023    st->print(PTR_FORMAT ": ", addr);
5024    if (dlinfo.dli_sname != NULL && dlinfo.dli_saddr != NULL) {
5025      st->print("%s+%#lx", dlinfo.dli_sname, addr-(intptr_t)dlinfo.dli_saddr);
5026    } else if (dlinfo.dli_fbase != NULL) {
5027      st->print("<offset %#lx>", addr-(intptr_t)dlinfo.dli_fbase);
5028    } else {
5029      st->print("<absolute address>");
5030    }
5031    if (dlinfo.dli_fname != NULL) {
5032      st->print(" in %s", dlinfo.dli_fname);
5033    }
5034    if (dlinfo.dli_fbase != NULL) {
5035      st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
5036    }
5037    st->cr();
5038
5039    if (Verbose) {
5040      // decode some bytes around the PC
5041      address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());
5042      address end   = clamp_address_in_page(addr+40, addr, os::vm_page_size());
5043      address       lowest = (address) dlinfo.dli_sname;
5044      if (!lowest)  lowest = (address) dlinfo.dli_fbase;
5045      if (begin < lowest)  begin = lowest;
5046      Dl_info dlinfo2;
5047      if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr
5048          && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin) {
5049        end = (address) dlinfo2.dli_saddr;
5050      }
5051      Disassembler::decode(begin, end, st);
5052    }
5053    return true;
5054  }
5055  return false;
5056}
5057
5058// Following function has been added to support HotSparc's libjvm.so running
5059// under Solaris production JDK 1.2.2 / 1.3.0.  These came from
5060// src/solaris/hpi/native_threads in the EVM codebase.
5061//
5062// NOTE: This is no longer needed in the 1.3.1 and 1.4 production release
5063// libraries and should thus be removed. We will leave it behind for a while
5064// until we no longer want to able to run on top of 1.3.0 Solaris production
5065// JDK. See 4341971.
5066
5067#define STACK_SLACK 0x800
5068
5069extern "C" {
5070  intptr_t sysThreadAvailableStackWithSlack() {
5071    stack_t st;
5072    intptr_t retval, stack_top;
5073    retval = thr_stksegment(&st);
5074    assert(retval == 0, "incorrect return value from thr_stksegment");
5075    assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
5076    assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
5077    stack_top=(intptr_t)st.ss_sp-st.ss_size;
5078    return ((intptr_t)&stack_top - stack_top - STACK_SLACK);
5079  }
5080}
5081
5082// ObjectMonitor park-unpark infrastructure ...
5083//
5084// We implement Solaris and Linux PlatformEvents with the
5085// obvious condvar-mutex-flag triple.
5086// Another alternative that works quite well is pipes:
5087// Each PlatformEvent consists of a pipe-pair.
5088// The thread associated with the PlatformEvent
5089// calls park(), which reads from the input end of the pipe.
5090// Unpark() writes into the other end of the pipe.
5091// The write-side of the pipe must be set NDELAY.
5092// Unfortunately pipes consume a large # of handles.
5093// Native solaris lwp_park() and lwp_unpark() work nicely, too.
5094// Using pipes for the 1st few threads might be workable, however.
5095//
5096// park() is permitted to return spuriously.
5097// Callers of park() should wrap the call to park() in
5098// an appropriate loop.  A litmus test for the correct
5099// usage of park is the following: if park() were modified
5100// to immediately return 0 your code should still work,
5101// albeit degenerating to a spin loop.
5102//
5103// In a sense, park()-unpark() just provides more polite spinning
5104// and polling with the key difference over naive spinning being
5105// that a parked thread needs to be explicitly unparked() in order
5106// to wake up and to poll the underlying condition.
5107//
5108// Assumption:
5109//    Only one parker can exist on an event, which is why we allocate
5110//    them per-thread. Multiple unparkers can coexist.
5111//
5112// _Event transitions in park()
5113//   -1 => -1 : illegal
5114//    1 =>  0 : pass - return immediately
5115//    0 => -1 : block; then set _Event to 0 before returning
5116//
5117// _Event transitions in unpark()
5118//    0 => 1 : just return
5119//    1 => 1 : just return
5120//   -1 => either 0 or 1; must signal target thread
5121//         That is, we can safely transition _Event from -1 to either
5122//         0 or 1.
5123//
5124// _Event serves as a restricted-range semaphore.
5125//   -1 : thread is blocked, i.e. there is a waiter
5126//    0 : neutral: thread is running or ready,
5127//        could have been signaled after a wait started
5128//    1 : signaled - thread is running or ready
5129//
5130// Another possible encoding of _Event would be with
5131// explicit "PARKED" == 01b and "SIGNALED" == 10b bits.
5132//
5133// TODO-FIXME: add DTRACE probes for:
5134// 1.   Tx parks
5135// 2.   Ty unparks Tx
5136// 3.   Tx resumes from park
5137
5138
5139// value determined through experimentation
5140#define ROUNDINGFIX 11
5141
5142// utility to compute the abstime argument to timedwait.
5143// TODO-FIXME: switch from compute_abstime() to unpackTime().
5144
5145static timestruc_t* compute_abstime(timestruc_t* abstime, jlong millis) {
5146  // millis is the relative timeout time
5147  // abstime will be the absolute timeout time
5148  if (millis < 0)  millis = 0;
5149  struct timeval now;
5150  int status = gettimeofday(&now, NULL);
5151  assert(status == 0, "gettimeofday");
5152  jlong seconds = millis / 1000;
5153  jlong max_wait_period;
5154
5155  if (UseLWPSynchronization) {
5156    // forward port of fix for 4275818 (not sleeping long enough)
5157    // There was a bug in Solaris 6, 7 and pre-patch 5 of 8 where
5158    // _lwp_cond_timedwait() used a round_down algorithm rather
5159    // than a round_up. For millis less than our roundfactor
5160    // it rounded down to 0 which doesn't meet the spec.
5161    // For millis > roundfactor we may return a bit sooner, but
5162    // since we can not accurately identify the patch level and
5163    // this has already been fixed in Solaris 9 and 8 we will
5164    // leave it alone rather than always rounding down.
5165
5166    if (millis > 0 && millis < ROUNDINGFIX) millis = ROUNDINGFIX;
5167    // It appears that when we go directly through Solaris _lwp_cond_timedwait()
5168    // the acceptable max time threshold is smaller than for libthread on 2.5.1 and 2.6
5169    max_wait_period = 21000000;
5170  } else {
5171    max_wait_period = 50000000;
5172  }
5173  millis %= 1000;
5174  if (seconds > max_wait_period) {      // see man cond_timedwait(3T)
5175    seconds = max_wait_period;
5176  }
5177  abstime->tv_sec = now.tv_sec  + seconds;
5178  long       usec = now.tv_usec + millis * 1000;
5179  if (usec >= 1000000) {
5180    abstime->tv_sec += 1;
5181    usec -= 1000000;
5182  }
5183  abstime->tv_nsec = usec * 1000;
5184  return abstime;
5185}
5186
5187void os::PlatformEvent::park() {           // AKA: down()
5188  // Transitions for _Event:
5189  //   -1 => -1 : illegal
5190  //    1 =>  0 : pass - return immediately
5191  //    0 => -1 : block; then set _Event to 0 before returning
5192
5193  // Invariant: Only the thread associated with the Event/PlatformEvent
5194  // may call park().
5195  assert(_nParked == 0, "invariant");
5196
5197  int v;
5198  for (;;) {
5199    v = _Event;
5200    if (Atomic::cmpxchg(v-1, &_Event, v) == v) break;
5201  }
5202  guarantee(v >= 0, "invariant");
5203  if (v == 0) {
5204    // Do this the hard way by blocking ...
5205    // See http://monaco.sfbay/detail.jsf?cr=5094058.
5206    // TODO-FIXME: for Solaris SPARC set fprs.FEF=0 prior to parking.
5207    // Only for SPARC >= V8PlusA
5208#if defined(__sparc) && defined(COMPILER2)
5209    if (ClearFPUAtPark) { _mark_fpu_nosave(); }
5210#endif
5211    int status = os::Solaris::mutex_lock(_mutex);
5212    assert_status(status == 0, status, "mutex_lock");
5213    guarantee(_nParked == 0, "invariant");
5214    ++_nParked;
5215    while (_Event < 0) {
5216      // for some reason, under 2.7 lwp_cond_wait() may return ETIME ...
5217      // Treat this the same as if the wait was interrupted
5218      // With usr/lib/lwp going to kernel, always handle ETIME
5219      status = os::Solaris::cond_wait(_cond, _mutex);
5220      if (status == ETIME) status = EINTR;
5221      assert_status(status == 0 || status == EINTR, status, "cond_wait");
5222    }
5223    --_nParked;
5224    _Event = 0;
5225    status = os::Solaris::mutex_unlock(_mutex);
5226    assert_status(status == 0, status, "mutex_unlock");
5227    // Paranoia to ensure our locked and lock-free paths interact
5228    // correctly with each other.
5229    OrderAccess::fence();
5230  }
5231}
5232
5233int os::PlatformEvent::park(jlong millis) {
5234  // Transitions for _Event:
5235  //   -1 => -1 : illegal
5236  //    1 =>  0 : pass - return immediately
5237  //    0 => -1 : block; then set _Event to 0 before returning
5238
5239  guarantee(_nParked == 0, "invariant");
5240  int v;
5241  for (;;) {
5242    v = _Event;
5243    if (Atomic::cmpxchg(v-1, &_Event, v) == v) break;
5244  }
5245  guarantee(v >= 0, "invariant");
5246  if (v != 0) return OS_OK;
5247
5248  int ret = OS_TIMEOUT;
5249  timestruc_t abst;
5250  compute_abstime(&abst, millis);
5251
5252  // See http://monaco.sfbay/detail.jsf?cr=5094058.
5253  // For Solaris SPARC set fprs.FEF=0 prior to parking.
5254  // Only for SPARC >= V8PlusA
5255#if defined(__sparc) && defined(COMPILER2)
5256  if (ClearFPUAtPark) { _mark_fpu_nosave(); }
5257#endif
5258  int status = os::Solaris::mutex_lock(_mutex);
5259  assert_status(status == 0, status, "mutex_lock");
5260  guarantee(_nParked == 0, "invariant");
5261  ++_nParked;
5262  while (_Event < 0) {
5263    int status = os::Solaris::cond_timedwait(_cond, _mutex, &abst);
5264    assert_status(status == 0 || status == EINTR ||
5265                  status == ETIME || status == ETIMEDOUT,
5266                  status, "cond_timedwait");
5267    if (!FilterSpuriousWakeups) break;                // previous semantics
5268    if (status == ETIME || status == ETIMEDOUT) break;
5269    // We consume and ignore EINTR and spurious wakeups.
5270  }
5271  --_nParked;
5272  if (_Event >= 0) ret = OS_OK;
5273  _Event = 0;
5274  status = os::Solaris::mutex_unlock(_mutex);
5275  assert_status(status == 0, status, "mutex_unlock");
5276  // Paranoia to ensure our locked and lock-free paths interact
5277  // correctly with each other.
5278  OrderAccess::fence();
5279  return ret;
5280}
5281
5282void os::PlatformEvent::unpark() {
5283  // Transitions for _Event:
5284  //    0 => 1 : just return
5285  //    1 => 1 : just return
5286  //   -1 => either 0 or 1; must signal target thread
5287  //         That is, we can safely transition _Event from -1 to either
5288  //         0 or 1.
5289  // See also: "Semaphores in Plan 9" by Mullender & Cox
5290  //
5291  // Note: Forcing a transition from "-1" to "1" on an unpark() means
5292  // that it will take two back-to-back park() calls for the owning
5293  // thread to block. This has the benefit of forcing a spurious return
5294  // from the first park() call after an unpark() call which will help
5295  // shake out uses of park() and unpark() without condition variables.
5296
5297  if (Atomic::xchg(1, &_Event) >= 0) return;
5298
5299  // If the thread associated with the event was parked, wake it.
5300  // Wait for the thread assoc with the PlatformEvent to vacate.
5301  int status = os::Solaris::mutex_lock(_mutex);
5302  assert_status(status == 0, status, "mutex_lock");
5303  int AnyWaiters = _nParked;
5304  status = os::Solaris::mutex_unlock(_mutex);
5305  assert_status(status == 0, status, "mutex_unlock");
5306  guarantee(AnyWaiters == 0 || AnyWaiters == 1, "invariant");
5307  if (AnyWaiters != 0) {
5308    // Note that we signal() *after* dropping the lock for "immortal" Events.
5309    // This is safe and avoids a common class of  futile wakeups.  In rare
5310    // circumstances this can cause a thread to return prematurely from
5311    // cond_{timed}wait() but the spurious wakeup is benign and the victim
5312    // will simply re-test the condition and re-park itself.
5313    // This provides particular benefit if the underlying platform does not
5314    // provide wait morphing.
5315    status = os::Solaris::cond_signal(_cond);
5316    assert_status(status == 0, status, "cond_signal");
5317  }
5318}
5319
5320// JSR166
5321// -------------------------------------------------------
5322
5323// The solaris and linux implementations of park/unpark are fairly
5324// conservative for now, but can be improved. They currently use a
5325// mutex/condvar pair, plus _counter.
5326// Park decrements _counter if > 0, else does a condvar wait.  Unpark
5327// sets count to 1 and signals condvar.  Only one thread ever waits
5328// on the condvar. Contention seen when trying to park implies that someone
5329// is unparking you, so don't wait. And spurious returns are fine, so there
5330// is no need to track notifications.
5331
5332#define MAX_SECS 100000000
5333
5334// This code is common to linux and solaris and will be moved to a
5335// common place in dolphin.
5336//
5337// The passed in time value is either a relative time in nanoseconds
5338// or an absolute time in milliseconds. Either way it has to be unpacked
5339// into suitable seconds and nanoseconds components and stored in the
5340// given timespec structure.
5341// Given time is a 64-bit value and the time_t used in the timespec is only
5342// a signed-32-bit value (except on 64-bit Linux) we have to watch for
5343// overflow if times way in the future are given. Further on Solaris versions
5344// prior to 10 there is a restriction (see cond_timedwait) that the specified
5345// number of seconds, in abstime, is less than current_time  + 100,000,000.
5346// As it will be 28 years before "now + 100000000" will overflow we can
5347// ignore overflow and just impose a hard-limit on seconds using the value
5348// of "now + 100,000,000". This places a limit on the timeout of about 3.17
5349// years from "now".
5350//
5351static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {
5352  assert(time > 0, "convertTime");
5353
5354  struct timeval now;
5355  int status = gettimeofday(&now, NULL);
5356  assert(status == 0, "gettimeofday");
5357
5358  time_t max_secs = now.tv_sec + MAX_SECS;
5359
5360  if (isAbsolute) {
5361    jlong secs = time / 1000;
5362    if (secs > max_secs) {
5363      absTime->tv_sec = max_secs;
5364    } else {
5365      absTime->tv_sec = secs;
5366    }
5367    absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;
5368  } else {
5369    jlong secs = time / NANOSECS_PER_SEC;
5370    if (secs >= MAX_SECS) {
5371      absTime->tv_sec = max_secs;
5372      absTime->tv_nsec = 0;
5373    } else {
5374      absTime->tv_sec = now.tv_sec + secs;
5375      absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;
5376      if (absTime->tv_nsec >= NANOSECS_PER_SEC) {
5377        absTime->tv_nsec -= NANOSECS_PER_SEC;
5378        ++absTime->tv_sec; // note: this must be <= max_secs
5379      }
5380    }
5381  }
5382  assert(absTime->tv_sec >= 0, "tv_sec < 0");
5383  assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs");
5384  assert(absTime->tv_nsec >= 0, "tv_nsec < 0");
5385  assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec");
5386}
5387
5388void Parker::park(bool isAbsolute, jlong time) {
5389  // Ideally we'd do something useful while spinning, such
5390  // as calling unpackTime().
5391
5392  // Optional fast-path check:
5393  // Return immediately if a permit is available.
5394  // We depend on Atomic::xchg() having full barrier semantics
5395  // since we are doing a lock-free update to _counter.
5396  if (Atomic::xchg(0, &_counter) > 0) return;
5397
5398  // Optional fast-exit: Check interrupt before trying to wait
5399  Thread* thread = Thread::current();
5400  assert(thread->is_Java_thread(), "Must be JavaThread");
5401  JavaThread *jt = (JavaThread *)thread;
5402  if (Thread::is_interrupted(thread, false)) {
5403    return;
5404  }
5405
5406  // First, demultiplex/decode time arguments
5407  timespec absTime;
5408  if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all
5409    return;
5410  }
5411  if (time > 0) {
5412    // Warning: this code might be exposed to the old Solaris time
5413    // round-down bugs.  Grep "roundingFix" for details.
5414    unpackTime(&absTime, isAbsolute, time);
5415  }
5416
5417  // Enter safepoint region
5418  // Beware of deadlocks such as 6317397.
5419  // The per-thread Parker:: _mutex is a classic leaf-lock.
5420  // In particular a thread must never block on the Threads_lock while
5421  // holding the Parker:: mutex.  If safepoints are pending both the
5422  // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
5423  ThreadBlockInVM tbivm(jt);
5424
5425  // Don't wait if cannot get lock since interference arises from
5426  // unblocking.  Also. check interrupt before trying wait
5427  if (Thread::is_interrupted(thread, false) ||
5428      os::Solaris::mutex_trylock(_mutex) != 0) {
5429    return;
5430  }
5431
5432  int status;
5433
5434  if (_counter > 0)  { // no wait needed
5435    _counter = 0;
5436    status = os::Solaris::mutex_unlock(_mutex);
5437    assert(status == 0, "invariant");
5438    // Paranoia to ensure our locked and lock-free paths interact
5439    // correctly with each other and Java-level accesses.
5440    OrderAccess::fence();
5441    return;
5442  }
5443
5444#ifdef ASSERT
5445  // Don't catch signals while blocked; let the running threads have the signals.
5446  // (This allows a debugger to break into the running thread.)
5447  sigset_t oldsigs;
5448  sigset_t* allowdebug_blocked = os::Solaris::allowdebug_blocked_signals();
5449  pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);
5450#endif
5451
5452  OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
5453  jt->set_suspend_equivalent();
5454  // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
5455
5456  // Do this the hard way by blocking ...
5457  // See http://monaco.sfbay/detail.jsf?cr=5094058.
5458  // TODO-FIXME: for Solaris SPARC set fprs.FEF=0 prior to parking.
5459  // Only for SPARC >= V8PlusA
5460#if defined(__sparc) && defined(COMPILER2)
5461  if (ClearFPUAtPark) { _mark_fpu_nosave(); }
5462#endif
5463
5464  if (time == 0) {
5465    status = os::Solaris::cond_wait(_cond, _mutex);
5466  } else {
5467    status = os::Solaris::cond_timedwait (_cond, _mutex, &absTime);
5468  }
5469  // Note that an untimed cond_wait() can sometimes return ETIME on older
5470  // versions of the Solaris.
5471  assert_status(status == 0 || status == EINTR ||
5472                status == ETIME || status == ETIMEDOUT,
5473                status, "cond_timedwait");
5474
5475#ifdef ASSERT
5476  pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
5477#endif
5478  _counter = 0;
5479  status = os::Solaris::mutex_unlock(_mutex);
5480  assert_status(status == 0, status, "mutex_unlock");
5481  // Paranoia to ensure our locked and lock-free paths interact
5482  // correctly with each other and Java-level accesses.
5483  OrderAccess::fence();
5484
5485  // If externally suspended while waiting, re-suspend
5486  if (jt->handle_special_suspend_equivalent_condition()) {
5487    jt->java_suspend_self();
5488  }
5489}
5490
5491void Parker::unpark() {
5492  int status = os::Solaris::mutex_lock(_mutex);
5493  assert(status == 0, "invariant");
5494  const int s = _counter;
5495  _counter = 1;
5496  status = os::Solaris::mutex_unlock(_mutex);
5497  assert(status == 0, "invariant");
5498
5499  if (s < 1) {
5500    status = os::Solaris::cond_signal(_cond);
5501    assert(status == 0, "invariant");
5502  }
5503}
5504
5505extern char** environ;
5506
5507// Run the specified command in a separate process. Return its exit value,
5508// or -1 on failure (e.g. can't fork a new process).
5509// Unlike system(), this function can be called from signal handler. It
5510// doesn't block SIGINT et al.
5511int os::fork_and_exec(char* cmd) {
5512  char * argv[4];
5513  argv[0] = (char *)"sh";
5514  argv[1] = (char *)"-c";
5515  argv[2] = cmd;
5516  argv[3] = NULL;
5517
5518  // fork is async-safe, fork1 is not so can't use in signal handler
5519  pid_t pid;
5520  Thread* t = Thread::current_or_null_safe();
5521  if (t != NULL && t->is_inside_signal_handler()) {
5522    pid = fork();
5523  } else {
5524    pid = fork1();
5525  }
5526
5527  if (pid < 0) {
5528    // fork failed
5529    warning("fork failed: %s", os::strerror(errno));
5530    return -1;
5531
5532  } else if (pid == 0) {
5533    // child process
5534
5535    // try to be consistent with system(), which uses "/usr/bin/sh" on Solaris
5536    execve("/usr/bin/sh", argv, environ);
5537
5538    // execve failed
5539    _exit(-1);
5540
5541  } else  {
5542    // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
5543    // care about the actual exit code, for now.
5544
5545    int status;
5546
5547    // Wait for the child process to exit.  This returns immediately if
5548    // the child has already exited. */
5549    while (waitpid(pid, &status, 0) < 0) {
5550      switch (errno) {
5551      case ECHILD: return 0;
5552      case EINTR: break;
5553      default: return -1;
5554      }
5555    }
5556
5557    if (WIFEXITED(status)) {
5558      // The child exited normally; get its exit code.
5559      return WEXITSTATUS(status);
5560    } else if (WIFSIGNALED(status)) {
5561      // The child exited because of a signal
5562      // The best value to return is 0x80 + signal number,
5563      // because that is what all Unix shells do, and because
5564      // it allows callers to distinguish between process exit and
5565      // process death by signal.
5566      return 0x80 + WTERMSIG(status);
5567    } else {
5568      // Unknown exit code; pass it through
5569      return status;
5570    }
5571  }
5572}
5573
5574// is_headless_jre()
5575//
5576// Test for the existence of xawt/libmawt.so or libawt_xawt.so
5577// in order to report if we are running in a headless jre
5578//
5579// Since JDK8 xawt/libmawt.so was moved into the same directory
5580// as libawt.so, and renamed libawt_xawt.so
5581//
5582bool os::is_headless_jre() {
5583  struct stat statbuf;
5584  char buf[MAXPATHLEN];
5585  char libmawtpath[MAXPATHLEN];
5586  const char *xawtstr  = "/xawt/libmawt.so";
5587  const char *new_xawtstr = "/libawt_xawt.so";
5588  char *p;
5589
5590  // Get path to libjvm.so
5591  os::jvm_path(buf, sizeof(buf));
5592
5593  // Get rid of libjvm.so
5594  p = strrchr(buf, '/');
5595  if (p == NULL) {
5596    return false;
5597  } else {
5598    *p = '\0';
5599  }
5600
5601  // Get rid of client or server
5602  p = strrchr(buf, '/');
5603  if (p == NULL) {
5604    return false;
5605  } else {
5606    *p = '\0';
5607  }
5608
5609  // check xawt/libmawt.so
5610  strcpy(libmawtpath, buf);
5611  strcat(libmawtpath, xawtstr);
5612  if (::stat(libmawtpath, &statbuf) == 0) return false;
5613
5614  // check libawt_xawt.so
5615  strcpy(libmawtpath, buf);
5616  strcat(libmawtpath, new_xawtstr);
5617  if (::stat(libmawtpath, &statbuf) == 0) return false;
5618
5619  return true;
5620}
5621
5622size_t os::write(int fd, const void *buf, unsigned int nBytes) {
5623  size_t res;
5624  RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
5625  return res;
5626}
5627
5628int os::close(int fd) {
5629  return ::close(fd);
5630}
5631
5632int os::socket_close(int fd) {
5633  return ::close(fd);
5634}
5635
5636int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
5637  assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_native,
5638         "Assumed _thread_in_native");
5639  RESTARTABLE_RETURN_INT((int)::recv(fd, buf, nBytes, flags));
5640}
5641
5642int os::send(int fd, char* buf, size_t nBytes, uint flags) {
5643  assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_native,
5644         "Assumed _thread_in_native");
5645  RESTARTABLE_RETURN_INT((int)::send(fd, buf, nBytes, flags));
5646}
5647
5648int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
5649  RESTARTABLE_RETURN_INT((int)::send(fd, buf, nBytes, flags));
5650}
5651
5652// As both poll and select can be interrupted by signals, we have to be
5653// prepared to restart the system call after updating the timeout, unless
5654// a poll() is done with timeout == -1, in which case we repeat with this
5655// "wait forever" value.
5656
5657int os::connect(int fd, struct sockaddr *him, socklen_t len) {
5658  int _result;
5659  _result = ::connect(fd, him, len);
5660
5661  // On Solaris, when a connect() call is interrupted, the connection
5662  // can be established asynchronously (see 6343810). Subsequent calls
5663  // to connect() must check the errno value which has the semantic
5664  // described below (copied from the connect() man page). Handling
5665  // of asynchronously established connections is required for both
5666  // blocking and non-blocking sockets.
5667  //     EINTR            The  connection  attempt  was   interrupted
5668  //                      before  any data arrived by the delivery of
5669  //                      a signal. The connection, however, will  be
5670  //                      established asynchronously.
5671  //
5672  //     EINPROGRESS      The socket is non-blocking, and the connec-
5673  //                      tion  cannot  be completed immediately.
5674  //
5675  //     EALREADY         The socket is non-blocking,  and a previous
5676  //                      connection  attempt  has  not yet been com-
5677  //                      pleted.
5678  //
5679  //     EISCONN          The socket is already connected.
5680  if (_result == OS_ERR && errno == EINTR) {
5681    // restarting a connect() changes its errno semantics
5682    RESTARTABLE(::connect(fd, him, len), _result);
5683    // undo these changes
5684    if (_result == OS_ERR) {
5685      if (errno == EALREADY) {
5686        errno = EINPROGRESS; // fall through
5687      } else if (errno == EISCONN) {
5688        errno = 0;
5689        return OS_OK;
5690      }
5691    }
5692  }
5693  return _result;
5694}
5695
5696// Get the default path to the core file
5697// Returns the length of the string
5698int os::get_core_path(char* buffer, size_t bufferSize) {
5699  const char* p = get_current_directory(buffer, bufferSize);
5700
5701  if (p == NULL) {
5702    assert(p != NULL, "failed to get current directory");
5703    return 0;
5704  }
5705
5706  jio_snprintf(buffer, bufferSize, "%s/core or core.%d",
5707                                              p, current_process_id());
5708
5709  return strlen(buffer);
5710}
5711
5712#ifndef PRODUCT
5713void TestReserveMemorySpecial_test() {
5714  // No tests available for this platform
5715}
5716#endif
5717
5718bool os::start_debugging(char *buf, int buflen) {
5719  int len = (int)strlen(buf);
5720  char *p = &buf[len];
5721
5722  jio_snprintf(p, buflen-len,
5723               "\n\n"
5724               "Do you want to debug the problem?\n\n"
5725               "To debug, run 'dbx - %d'; then switch to thread " INTX_FORMAT "\n"
5726               "Enter 'yes' to launch dbx automatically (PATH must include dbx)\n"
5727               "Otherwise, press RETURN to abort...",
5728               os::current_process_id(), os::current_thread_id());
5729
5730  bool yes = os::message_box("Unexpected Error", buf);
5731
5732  if (yes) {
5733    // yes, user asked VM to launch debugger
5734    jio_snprintf(buf, sizeof(buf), "dbx - %d", os::current_process_id());
5735
5736    os::fork_and_exec(buf);
5737    yes = false;
5738  }
5739  return yes;
5740}
5741