aboutsummaryrefslogtreecommitdiff
path: root/src/os_cpu/solaris_x86/vm/threadLS_solaris_x86.cpp
blob: e2ce144a3c799bd91111c47909123174270d579c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadLocalStorage.hpp"

#ifdef AMD64
extern "C" Thread*  fs_load(ptrdiff_t tlsOffset);
extern "C" intptr_t fs_thread();
#else
// From solaris_i486.s
extern "C" Thread*  gs_load(ptrdiff_t tlsOffset);
extern "C" intptr_t gs_thread();
#endif // AMD64

// tlsMode encoding:
//
// pd_tlsAccessUndefined : uninitialized
// pd_tlsAccessSlow      : not available
// pd_tlsAccessIndirect  :
//   old-style indirect access - present in "T1" libthread.
//   use thr_slot_sync_allocate() to attempt to allocate a slot.
// pd_tlsAccessDirect    :
//   new-style direct access - present in late-model "T2" libthread.
//   Allocate the offset (slot) via _thr_slot_offset() or by
//   defining an IE- or LE-mode TLS/TSD slot in the launcher and then passing
//   that offset into libjvm.so.
//   See http://sac.eng/Archives/CaseLog/arc/PSARC/2003/159/.
//
// Note that we have a capability gap - some early model T2 forms
// (e.g., unpatched S9) have neither _thr_slot_sync_allocate() nor
// _thr_slot_offset().  In that case we revert to the usual
// thr_getspecific accessor.
//

static ThreadLocalStorage::pd_tlsAccessMode tlsMode = ThreadLocalStorage::pd_tlsAccessUndefined ;
static ptrdiff_t tlsOffset = 0 ;
static thread_key_t tlsKey ;

typedef int (*TSSA_Entry) (ptrdiff_t *, int, int) ;
typedef ptrdiff_t (*TSO_Entry) (int) ;

ThreadLocalStorage::pd_tlsAccessMode ThreadLocalStorage::pd_getTlsAccessMode ()
{
   guarantee (tlsMode != pd_tlsAccessUndefined, "tlsMode not set") ;
   return tlsMode ;
}

ptrdiff_t ThreadLocalStorage::pd_getTlsOffset () {
   guarantee (tlsMode != pd_tlsAccessUndefined, "tlsMode not set") ;
   return tlsOffset ;
}

// TODO: Consider the following improvements:
//
// 1.   Convert from thr_*specific* to pthread_*specific*.
//      The pthread_ forms are slightly faster.  Also, the
//      pthread_ forms have a pthread_key_delete() API which
//      would aid in clean JVM shutdown and the eventual goal
//      of permitting a JVM to reinstantiate itself withing a process.
//
// 2.   See ThreadLocalStorage::init().  We end up allocating
//      two TLS keys during VM startup.  That's benign, but we could collapse
//      down to one key without too much trouble.
//
// 3.   MacroAssembler::get_thread() currently emits calls to thr_getspecific().
//      Modify get_thread() to call Thread::current() instead.
//
// 4.   Thread::current() currently uses a cache keyed by %gs:[0].
//      (The JVM has PSARC permission to use %g7/%gs:[0]
//      as an opaque temporally unique thread identifier).
//      For C++ access to a thread's reflexive "self" pointer we
//      should consider using one of the following:
//      a. a radix tree keyed by %esp - as in EVM.
//         This requires two loads (the 2nd dependent on the 1st), but
//         is easily inlined and doesn't require a "miss" slow path.
//      b. a fast TLS/TSD slot allocated by _thr_slot_offset
//         or _thr_slot_sync_allocate.
//
// 5.   'generate_code_for_get_thread' is a misnomer.
//      We should change it to something more general like
//      pd_ThreadSelf_Init(), for instance.
//

static void AllocateTLSOffset ()
{
   int rslt ;
   TSSA_Entry tssa ;
   TSO_Entry  tso ;
   ptrdiff_t off ;

   guarantee (tlsMode == ThreadLocalStorage::pd_tlsAccessUndefined, "tlsMode not set") ;
   tlsMode = ThreadLocalStorage::pd_tlsAccessSlow ;
   tlsOffset = 0 ;
#ifndef AMD64

   tssa = (TSSA_Entry) dlsym (RTLD_DEFAULT, "thr_slot_sync_allocate") ;
   if (tssa != NULL) {
        off = -1 ;
        rslt = (*tssa)(&off, NULL, NULL) ;                // (off,dtor,darg)
        if (off != -1) {
           tlsOffset = off ;
           tlsMode = ThreadLocalStorage::pd_tlsAccessIndirect ;
           return ;
        }
    }

    rslt = thr_keycreate (&tlsKey, NULL) ;
    if (rslt != 0) {
        tlsMode = ThreadLocalStorage::pd_tlsAccessSlow ;   // revert to slow mode
        return ;
    }

    tso = (TSO_Entry) dlsym (RTLD_DEFAULT, "_thr_slot_offset") ;
    if (tso != NULL) {
        off = (*tso)(tlsKey) ;
        if (off >= 0) {
           tlsOffset = off ;
           tlsMode = ThreadLocalStorage::pd_tlsAccessDirect ;
           return ;
        }
    }

    // Failure: Too bad ... we've allocated a TLS slot we don't need and there's
    // no provision in the ABI for returning the slot.
    //
    // If we didn't find a slot then then:
    // 1. We might be on liblwp.
    // 2. We might be on T2 libthread, but all "fast" slots are already
    //    consumed
    // 3. We might be on T1, and all TSD (thr_slot_sync_allocate) slots are
    //    consumed.
    // 4. We might be on T2 libthread, but it's be re-architected
    //    so that fast slots are no longer g7-relative.
    //

    tlsMode = ThreadLocalStorage::pd_tlsAccessSlow ;
    return ;
#endif // AMD64
}

void ThreadLocalStorage::generate_code_for_get_thread() {
    AllocateTLSOffset() ;
}

void ThreadLocalStorage::set_thread_in_slot(Thread *thread) {
  guarantee (tlsMode != pd_tlsAccessUndefined, "tlsMode not set") ;
  if (tlsMode == pd_tlsAccessIndirect) {
#ifdef AMD64
        intptr_t tbase = fs_thread();
#else
        intptr_t tbase = gs_thread();
#endif // AMD64
        *((Thread**) (tbase + tlsOffset)) = thread ;
  } else
  if (tlsMode == pd_tlsAccessDirect) {
        thr_setspecific (tlsKey, (void *) thread) ;
        // set with thr_setspecific and then readback with gs_load to validate.
#ifdef AMD64
        guarantee (thread == fs_load(tlsOffset), "tls readback failure") ;
#else
        guarantee (thread == gs_load(tlsOffset), "tls readback failure") ;
#endif // AMD64
  }
}


extern "C" Thread* get_thread() {
  return ThreadLocalStorage::thread();
}