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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 // do not include precompiled header file 26 # include "incls/_os_linux.cpp.incl" 27 28 // put OS-includes here 29 # include <sys/types.h> 30 # include <sys/mman.h> 31 # include <pthread.h> 32 # include <signal.h> 33 # include <errno.h> 34 # include <dlfcn.h> 35 # include <stdio.h> 36 # include <unistd.h> 37 # include <sys/resource.h> 38 # include <pthread.h> 39 # include <sys/stat.h> 40 # include <sys/time.h> 41 # include <sys/times.h> 42 # include <sys/utsname.h> 43 # include <sys/socket.h> 44 # include <sys/wait.h> 45 # include <pwd.h> 46 # include <poll.h> 47 # include <semaphore.h> 48 # include <fcntl.h> 49 # include <string.h> 50 # include <syscall.h> 51 # include <sys/sysinfo.h> 52 # include <gnu/libc-version.h> 53 # include <sys/ipc.h> 54 # include <sys/shm.h> 55 # include <link.h> 56 57 #define MAX_PATH (2 * K) 58 59 // for timer info max values which include all bits 60 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 61 #define SEC_IN_NANOSECS 1000000000LL 62 63 //////////////////////////////////////////////////////////////////////////////// 64 // global variables 65 julong os::Linux::_physical_memory = 0; 66 67 address os::Linux::_initial_thread_stack_bottom = NULL; 68 uintptr_t os::Linux::_initial_thread_stack_size = 0; 69 70 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL; 71 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL; 72 Mutex* os::Linux::_createThread_lock = NULL; 73 pthread_t os::Linux::_main_thread; 74 int os::Linux::_page_size = -1; 75 bool os::Linux::_is_floating_stack = false; 2475 return cpu_to_node()->at(cpu_id); 2476 } 2477 return -1; 2478 } 2479 2480 GrowableArray<int>* os::Linux::_cpu_to_node; 2481 os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu; 2482 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus; 2483 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node; 2484 os::Linux::numa_available_func_t os::Linux::_numa_available; 2485 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory; 2486 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory; 2487 unsigned long* os::Linux::_numa_all_nodes; 2488 2489 bool os::uncommit_memory(char* addr, size_t size) { 2490 return ::mmap(addr, size, PROT_NONE, 2491 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0) 2492 != MAP_FAILED; 2493 } 2494 2495 static address _highest_vm_reserved_address = NULL; 2496 2497 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory 2498 // at 'requested_addr'. If there are existing memory mappings at the same 2499 // location, however, they will be overwritten. If 'fixed' is false, 2500 // 'requested_addr' is only treated as a hint, the return value may or 2501 // may not start from the requested address. Unlike Linux mmap(), this 2502 // function returns NULL to indicate failure. 2503 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) { 2504 char * addr; 2505 int flags; 2506 2507 flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS; 2508 if (fixed) { 2509 assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address"); 2510 flags |= MAP_FIXED; 2511 } 2512 2513 // Map uncommitted pages PROT_READ and PROT_WRITE, change access 2514 // to PROT_EXEC if executable when we commit the page. | 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 # define __STDC_FORMAT_MACROS 26 27 // do not include precompiled header file 28 # include "incls/_os_linux.cpp.incl" 29 30 // put OS-includes here 31 # include <sys/types.h> 32 # include <sys/mman.h> 33 # include <pthread.h> 34 # include <signal.h> 35 # include <errno.h> 36 # include <dlfcn.h> 37 # include <stdio.h> 38 # include <unistd.h> 39 # include <sys/resource.h> 40 # include <pthread.h> 41 # include <sys/stat.h> 42 # include <sys/time.h> 43 # include <sys/times.h> 44 # include <sys/utsname.h> 45 # include <sys/socket.h> 46 # include <sys/wait.h> 47 # include <pwd.h> 48 # include <poll.h> 49 # include <semaphore.h> 50 # include <fcntl.h> 51 # include <string.h> 52 # include <syscall.h> 53 # include <sys/sysinfo.h> 54 # include <gnu/libc-version.h> 55 # include <sys/ipc.h> 56 # include <sys/shm.h> 57 # include <link.h> 58 # include <stdint.h> 59 # include <inttypes.h> 60 61 #define MAX_PATH (2 * K) 62 63 // for timer info max values which include all bits 64 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 65 #define SEC_IN_NANOSECS 1000000000LL 66 67 //////////////////////////////////////////////////////////////////////////////// 68 // global variables 69 julong os::Linux::_physical_memory = 0; 70 71 address os::Linux::_initial_thread_stack_bottom = NULL; 72 uintptr_t os::Linux::_initial_thread_stack_size = 0; 73 74 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL; 75 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL; 76 Mutex* os::Linux::_createThread_lock = NULL; 77 pthread_t os::Linux::_main_thread; 78 int os::Linux::_page_size = -1; 79 bool os::Linux::_is_floating_stack = false; 2479 return cpu_to_node()->at(cpu_id); 2480 } 2481 return -1; 2482 } 2483 2484 GrowableArray<int>* os::Linux::_cpu_to_node; 2485 os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu; 2486 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus; 2487 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node; 2488 os::Linux::numa_available_func_t os::Linux::_numa_available; 2489 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory; 2490 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory; 2491 unsigned long* os::Linux::_numa_all_nodes; 2492 2493 bool os::uncommit_memory(char* addr, size_t size) { 2494 return ::mmap(addr, size, PROT_NONE, 2495 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0) 2496 != MAP_FAILED; 2497 } 2498 2499 // Linux uses a growable mapping for the stack, and if the mapping for 2500 // the stack guard pages is not removed when we detach a thread the 2501 // stack cannot grow beyond the pages where the stack guard was 2502 // mapped. If at some point later in the process the stack expands to 2503 // that point, the Linux kernel cannot expand the stack any further 2504 // because the guard pages are in the way, and a segfault occurs. 2505 // 2506 // However, it's essential not to split the stack region by unmapping 2507 // a region (leaving a hole) that's already part of the stack mapping, 2508 // so if the stack mapping has already grown beyond the guard pages at 2509 // the time we create them, we have to truncate the stack mapping. 2510 // So, we need to know the extent of the stack mapping when 2511 // create_stack_guard_pages() is called. 2512 2513 // Find the bounds of the stack mapping. Return true for success. 2514 // 2515 // We only need this for stacks that are growable: at the time of 2516 // writing thread stacks don't use growable mappings (i.e. those 2517 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this 2518 // only applies to the main thread. 2519 static bool 2520 get_stack_bounds(uintptr_t *bottom, uintptr_t *top) 2521 { 2522 char filename[sizeof "/proc/" + sizeof "4294967295" + sizeof "/maps"]; 2523 snprintf(filename, sizeof filename, 2524 "/proc/%d/maps", syscall(SYS_gettid)); 2525 FILE *f = fopen(filename, "r"); 2526 if (f == NULL) 2527 return false; 2528 2529 while (!feof(f)) { 2530 size_t dummy; 2531 char *str = NULL; 2532 ssize_t len = getline(&str, &dummy, f); 2533 if (len == -1) { 2534 return false; 2535 } 2536 2537 if (len > 0 && str[len-1] == '\n') { 2538 str[len-1] = 0; 2539 len--; 2540 } 2541 2542 static const char *stack_str = "[stack]"; 2543 if (len > (ssize_t)strlen(stack_str) 2544 && (strcmp(str + len - strlen(stack_str), stack_str) 2545 == 0)) { 2546 if (sscanf(str, "%" SCNxPTR "-%" SCNxPTR, bottom, top) == 2) { 2547 uintptr_t sp = (uintptr_t)__builtin_frame_address(0); 2548 if (sp >= *bottom && sp <= *top) { 2549 free(str); 2550 return true; 2551 } 2552 } 2553 } 2554 2555 free(str); 2556 } 2557 2558 return false; 2559 } 2560 2561 // If the (growable) stack mapping already extends beyond the point 2562 // where we're going to put our guard pages, truncate the mapping at 2563 // that point my munmap()ping it. This ensures that when we later 2564 // munmap() the guard pages we don't leave a hole in the stack 2565 // mapping. 2566 bool os::create_stack_guard_pages(char* addr, size_t size) { 2567 uintptr_t stack_extent, stack_base; 2568 if (get_stack_bounds(&stack_extent, &stack_base)) { 2569 if (stack_extent < (uintptr_t)addr) 2570 ::munmap((void*)stack_extent, (uintptr_t)addr - stack_extent); 2571 } 2572 2573 return os::commit_memory(addr, size); 2574 } 2575 2576 // If this is a growable mapping, remove the guard pages entirely by 2577 // munmap()ping them. If not, just call uncommit_memory(). 2578 bool os::remove_stack_guard_pages(char* addr, size_t size) { 2579 uintptr_t stack_extent, stack_base; 2580 if (get_stack_bounds(&stack_extent, &stack_base)) { 2581 return ::munmap(addr, size) == 0; 2582 } 2583 2584 return os::uncommit_memory(addr, size); 2585 } 2586 2587 static address _highest_vm_reserved_address = NULL; 2588 2589 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory 2590 // at 'requested_addr'. If there are existing memory mappings at the same 2591 // location, however, they will be overwritten. If 'fixed' is false, 2592 // 'requested_addr' is only treated as a hint, the return value may or 2593 // may not start from the requested address. Unlike Linux mmap(), this 2594 // function returns NULL to indicate failure. 2595 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) { 2596 char * addr; 2597 int flags; 2598 2599 flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS; 2600 if (fixed) { 2601 assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address"); 2602 flags |= MAP_FIXED; 2603 } 2604 2605 // Map uncommitted pages PROT_READ and PROT_WRITE, change access 2606 // to PROT_EXEC if executable when we commit the page. |