src/os_cpu/linux_x86/vm/threadLS_linux_x86.cpp

Wed, 25 Sep 2013 13:58:13 +0200

author
dsimms
date
Wed, 25 Sep 2013 13:58:13 +0200
changeset 5781
899ecf76b570
parent 4299
f34d701e952e
child 6876
710a3c8b516e
permissions
-rw-r--r--

8023956: Provide a work-around to broken Linux 32 bit "Exec Shield" using CS for NX emulation (crashing with SI_KERNEL)
Summary: Execute some code at a high virtual address value, and keep mapped
Reviewed-by: coleenp, zgu

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

mercurial