hotspot/src/os_cpu/bsd_x86/vm/threadLS_bsd_x86.cpp
author prr
Fri, 23 May 2014 09:05:24 -0700
changeset 24567 a0ebe5fd56ff
parent 14583 d70ee55535f4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     2
 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     4
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     8
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    13
 * accompanied this code).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    14
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    18
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    21
 * questions.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    22
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    23
 */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    24
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    25
#include "precompiled.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 10565
diff changeset
    26
#include "runtime/thread.inline.hpp"
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
#include "runtime/threadLocalStorage.hpp"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    28
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    29
// Map stack pointer (%esp) to thread pointer for faster TLS access
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    31
// Here we use a flat table for better performance. Getting current thread
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    32
// is down to one memory access (read _sp_map[%esp>>12]) in generated code
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    33
// and two in runtime code (-fPIC code needs an extra load for _sp_map).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    34
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    35
// This code assumes stack page is not shared by different threads. It works
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    36
// in 32-bit VM when page size is 4K (or a multiple of 4K, if that matters).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
// Notice that _sp_map is allocated in the bss segment, which is ZFOD
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
// (zero-fill-on-demand). While it reserves 4M address space upfront,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
// actual memory pages are committed on demand.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
// If an application creates and destroys a lot of threads, usually the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    43
// stack space freed by a thread will soon get reused by new thread
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    44
// (this is especially true in NPTL or BsdThreads in fixed-stack mode).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    45
// No memory page in _sp_map is wasted.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    46
//
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    47
// However, it's still possible that we might end up populating &
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    48
// committing a large fraction of the 4M table over time, but the actual
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    49
// amount of live data in the table could be quite small. The max wastage
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    50
// is less than 4M bytes. If it becomes an issue, we could use madvise()
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    51
// with MADV_DONTNEED to reclaim unused (i.e. all-zero) pages in _sp_map.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    52
// MADV_DONTNEED on Bsd keeps the virtual memory mapping, but zaps the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    53
// physical memory page (i.e. similar to MADV_FREE on Solaris).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    54
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    55
#ifndef AMD64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    56
Thread* ThreadLocalStorage::_sp_map[1UL << (SP_BITLENGTH - PAGE_SHIFT)];
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    57
#endif // !AMD64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    58
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    59
void ThreadLocalStorage::generate_code_for_get_thread() {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
    // nothing we can do here for user-level thread
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    61
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    62
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    63
void ThreadLocalStorage::pd_init() {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    64
#ifndef AMD64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    65
  assert(align_size_down(os::vm_page_size(), PAGE_SIZE) == os::vm_page_size(),
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    66
         "page size must be multiple of PAGE_SIZE");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    67
#endif // !AMD64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    68
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    69
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    70
void ThreadLocalStorage::pd_set_thread(Thread* thread) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    71
  os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    72
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    73
#ifndef AMD64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    74
  address stack_top = os::current_stack_base();
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    75
  size_t stack_size = os::current_stack_size();
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    76
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    77
  for (address p = stack_top - stack_size; p < stack_top; p += PAGE_SIZE) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    78
    // pd_set_thread() is called with non-NULL value when a new thread is
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    79
    // created/attached, or with NULL value when a thread is about to exit.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    80
    // If both "thread" and the corresponding _sp_map[] entry are non-NULL,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    81
    // they should have the same value. Otherwise it might indicate that the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    82
    // stack page is shared by multiple threads. However, a more likely cause
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    83
    // for this assertion to fail is that an attached thread exited without
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    84
    // detaching itself from VM, which is a program error and could cause VM
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    85
    // to crash.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    86
    assert(thread == NULL || _sp_map[(uintptr_t)p >> PAGE_SHIFT] == NULL ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    87
           thread == _sp_map[(uintptr_t)p >> PAGE_SHIFT],
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    88
           "thread exited without detaching from VM??");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    89
    _sp_map[(uintptr_t)p >> PAGE_SHIFT] = thread;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    90
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    91
#endif // !AMD64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    92
}