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