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