hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp

Print this page
rev 611 : Merge
   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)os_linux_x86.cpp     1.98 07/11/15 11:29:19 JVM"
   3 #endif
   4 /*
   5  * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  


  48 # include <pwd.h>
  49 # include <poll.h>
  50 # include <ucontext.h>
  51 # include <fpu_control.h>
  52 
  53 #ifdef AMD64
  54 #define REG_SP REG_RSP
  55 #define REG_PC REG_RIP
  56 #define REG_FP REG_RBP
  57 #define SPELL_REG_SP "rsp"
  58 #define SPELL_REG_FP "rbp"
  59 #else
  60 #define REG_SP REG_UESP
  61 #define REG_PC REG_EIP
  62 #define REG_FP REG_EBP
  63 #define SPELL_REG_SP "esp"
  64 #define SPELL_REG_FP "ebp"
  65 #endif // AMD64
  66 
  67 address os::current_stack_pointer() {





  68   register void *esp __asm__ (SPELL_REG_SP);
  69   return (address) esp;

  70 }
  71 
  72 char* os::non_memory_address_word() {
  73   // Must never look like an address returned by reserve_memory,
  74   // even in its subfields (as defined by the CPU immediate fields,
  75   // if the CPU splits constants across multiple instructions).
  76 
  77   return (char*) -1;
  78 }
  79 
  80 void os::initialize_thread() {
  81 // Nothing to do.
  82 }
  83 
  84 address os::Linux::ucontext_get_pc(ucontext_t * uc) {
  85   return (address)uc->uc_mcontext.gregs[REG_PC];
  86 }
  87 
  88 intptr_t* os::Linux::ucontext_get_sp(ucontext_t * uc) {
  89   return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];


 125     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 126   }
 127 
 128   return epc;
 129 }
 130 
 131 frame os::fetch_frame_from_context(void* ucVoid) {
 132   intptr_t* sp;
 133   intptr_t* fp;
 134   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 135   return frame(sp, fp, epc.pc());
 136 }
 137 
 138 // By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get
 139 // turned off by -fomit-frame-pointer,
 140 frame os::get_sender_for_C_frame(frame* fr) {
 141   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
 142 }
 143 
 144 intptr_t* _get_previous_fp() {




 145   register intptr_t **ebp __asm__ (SPELL_REG_FP);

 146   return (intptr_t*) *ebp;   // we want what it points to.
 147 }
 148 
 149 
 150 frame os::current_frame() {
 151   intptr_t* fp = _get_previous_fp();
 152   frame myframe((intptr_t*)os::current_stack_pointer(), 
 153                 (intptr_t*)fp,
 154                 CAST_FROM_FN_PTR(address, os::current_frame));
 155   if (os::is_first_C_frame(&myframe)) {
 156     // stack is not walkable
 157     return frame(NULL, NULL, NULL);
 158   } else {
 159     return os::get_sender_for_C_frame(&myframe);
 160   }
 161 }
 162 
 163 // Utility functions
 164 
 165 // From IA32 System Programming Guide


 397     // If an instruction spans a page boundary, and the page containing
 398     // the beginning of the instruction is executable but the following
 399     // page is not, the pc and the faulting address might be slightly
 400     // different - we still want to unguard the 2nd page in this case.
 401     //
 402     // 15 bytes seems to be a (very) safe value for max instruction size.
 403     bool pc_is_near_addr = 
 404       (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
 405     bool instr_spans_page_boundary =
 406       (align_size_down((intptr_t) pc ^ (intptr_t) addr,
 407                        (intptr_t) page_size) > 0);
 408     
 409     if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
 410       static volatile address last_addr =
 411         (address) os::non_memory_address_word();
 412       
 413       // In conservative mode, don't unguard unless the address is in the VM
 414       if (addr != last_addr &&
 415           (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
 416         
 417         // Unguard and retry
 418         address page_start =
 419           (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
 420         bool res = os::unguard_memory((char*) page_start, page_size);

 421 
 422         if (PrintMiscellaneous && Verbose) {
 423           char buf[256];
 424           jio_snprintf(buf, sizeof(buf), "Execution protection violation "
 425                        "at " INTPTR_FORMAT
 426                        ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr,
 427                        page_start, (res ? "success" : "failed"), errno);
 428           tty->print_raw_cr(buf);
 429         }
 430         stub = pc;
 431 
 432         // Set last_addr so if we fault again at the same address, we don't end
 433         // up in an endless loop.
 434         // 
 435         // There are two potential complications here.  Two threads trapping at
 436         // the same address at the same time could cause one of the threads to
 437         // think it already unguarded, and abort the VM.  Likely very rare.
 438         // 
 439         // The other race involves two threads alternately trapping at
 440         // different addresses and failing to unguard the page, resulting in


 546 
 547   if (addr != NULL) {
 548     release_memory(addr, bytes);
 549   }
 550 
 551   return addr != NULL;
 552 #endif // AMD64
 553 }
 554 
 555 ////////////////////////////////////////////////////////////////////////////////
 556 // thread stack
 557 
 558 #ifdef AMD64
 559 size_t os::Linux::min_stack_allowed  = 64 * K;
 560 
 561 // amd64: pthread on amd64 is always in floating stack mode
 562 bool os::Linux::supports_variable_stack_size() {  return true; }
 563 #else
 564 size_t os::Linux::min_stack_allowed  =  (48 DEBUG_ONLY(+4))*K;
 565 

 566 #define GET_GS() ({int gs; __asm__ volatile("movw %%gs, %w0":"=q"(gs)); gs&0xffff;})

 567 
 568 // Test if pthread library can support variable thread stack size. LinuxThreads
 569 // in fixed stack mode allocates 2M fixed slot for each thread. LinuxThreads 
 570 // in floating stack mode and NPTL support variable stack size.
 571 bool os::Linux::supports_variable_stack_size() {
 572   if (os::Linux::is_NPTL()) {
 573      // NPTL, yes
 574      return true;
 575 
 576   } else {
 577     // Note: We can't control default stack size when creating a thread.
 578     // If we use non-default stack size (pthread_attr_setstacksize), both 
 579     // floating stack and non-floating stack LinuxThreads will return the 
 580     // same value. This makes it impossible to implement this function by
 581     // detecting thread stack size directly.
 582     // 
 583     // An alternative approach is to check %gs. Fixed-stack LinuxThreads
 584     // do not use %gs, so its value is 0. Floating-stack LinuxThreads use
 585     // %gs (either as LDT selector or GDT selector, depending on kernel)
 586     // to access thread specific data. 
 587     //
 588     // Note that %gs is a reserved glibc register since early 2001, so 
 589     // applications are not allowed to change its value (Ulrich Drepper from 
 590     // Redhat confirmed that all known offenders have been modified to use
 591     // either %fs or TSD). In the worst case scenario, when VM is embedded in 
 592     // a native application that plays with %gs, we might see non-zero %gs 
 593     // even LinuxThreads is running in fixed stack mode. As the result, we'll 
 594     // return true and skip _thread_safety_check(), so we may not be able to 
 595     // detect stack-heap collisions. But otherwise it's harmless.
 596     //

 597     return (GET_GS() != 0);



 598   }
 599 }
 600 #endif // AMD64
 601 
 602 // return default stack size for thr_type
 603 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
 604   // default stack size (compiler thread needs larger stack)
 605 #ifdef AMD64
 606   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 607 #else
 608   size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
 609 #endif // AMD64
 610   return s;
 611 } 
 612   
 613 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
 614   // Creating guard page is very expensive. Java thread has HotSpot
 615   // guard page, only enable glibc guard page for non-Java threads.
 616   return (thr_type == java_thread ? 0 : page_size());
 617 }





   1 /*
   2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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  *


  45 # include <pwd.h>
  46 # include <poll.h>
  47 # include <ucontext.h>
  48 # include <fpu_control.h>
  49 
  50 #ifdef AMD64
  51 #define REG_SP REG_RSP
  52 #define REG_PC REG_RIP
  53 #define REG_FP REG_RBP
  54 #define SPELL_REG_SP "rsp"
  55 #define SPELL_REG_FP "rbp"
  56 #else
  57 #define REG_SP REG_UESP
  58 #define REG_PC REG_EIP
  59 #define REG_FP REG_EBP
  60 #define SPELL_REG_SP "esp"
  61 #define SPELL_REG_FP "ebp"
  62 #endif // AMD64
  63 
  64 address os::current_stack_pointer() {
  65 #ifdef SPARC_WORKS
  66   register void *esp;
  67   __asm__("mov %%"SPELL_REG_SP", %0":"=r"(esp));
  68   return (address) ((char*)esp + sizeof(long)*2);
  69 #else
  70   register void *esp __asm__ (SPELL_REG_SP);
  71   return (address) esp;
  72 #endif
  73 }
  74 
  75 char* os::non_memory_address_word() {
  76   // Must never look like an address returned by reserve_memory,
  77   // even in its subfields (as defined by the CPU immediate fields,
  78   // if the CPU splits constants across multiple instructions).
  79 
  80   return (char*) -1;
  81 }
  82 
  83 void os::initialize_thread() {
  84 // Nothing to do.
  85 }
  86 
  87 address os::Linux::ucontext_get_pc(ucontext_t * uc) {
  88   return (address)uc->uc_mcontext.gregs[REG_PC];
  89 }
  90 
  91 intptr_t* os::Linux::ucontext_get_sp(ucontext_t * uc) {
  92   return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];


 128     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 129   }
 130 
 131   return epc;
 132 }
 133 
 134 frame os::fetch_frame_from_context(void* ucVoid) {
 135   intptr_t* sp;
 136   intptr_t* fp;
 137   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 138   return frame(sp, fp, epc.pc());
 139 }
 140 
 141 // By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get
 142 // turned off by -fomit-frame-pointer,
 143 frame os::get_sender_for_C_frame(frame* fr) {
 144   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
 145 }
 146 
 147 intptr_t* _get_previous_fp() {
 148 #ifdef SPARC_WORKS
 149   register intptr_t **ebp;
 150   __asm__("mov %%"SPELL_REG_FP", %0":"=r"(ebp));
 151 #else
 152   register intptr_t **ebp __asm__ (SPELL_REG_FP);
 153 #endif
 154   return (intptr_t*) *ebp;   // we want what it points to.
 155 }
 156 
 157 
 158 frame os::current_frame() {
 159   intptr_t* fp = _get_previous_fp();
 160   frame myframe((intptr_t*)os::current_stack_pointer(),
 161                 (intptr_t*)fp,
 162                 CAST_FROM_FN_PTR(address, os::current_frame));
 163   if (os::is_first_C_frame(&myframe)) {
 164     // stack is not walkable
 165     return frame(NULL, NULL, NULL);
 166   } else {
 167     return os::get_sender_for_C_frame(&myframe);
 168   }
 169 }
 170 
 171 // Utility functions
 172 
 173 // From IA32 System Programming Guide


 405     // If an instruction spans a page boundary, and the page containing
 406     // the beginning of the instruction is executable but the following
 407     // page is not, the pc and the faulting address might be slightly
 408     // different - we still want to unguard the 2nd page in this case.
 409     //
 410     // 15 bytes seems to be a (very) safe value for max instruction size.
 411     bool pc_is_near_addr =
 412       (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
 413     bool instr_spans_page_boundary =
 414       (align_size_down((intptr_t) pc ^ (intptr_t) addr,
 415                        (intptr_t) page_size) > 0);
 416 
 417     if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
 418       static volatile address last_addr =
 419         (address) os::non_memory_address_word();
 420 
 421       // In conservative mode, don't unguard unless the address is in the VM
 422       if (addr != last_addr &&
 423           (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
 424 
 425         // Set memory to RWX and retry
 426         address page_start =
 427           (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
 428         bool res = os::protect_memory((char*) page_start, page_size,
 429                                       os::MEM_PROT_RWX);
 430 
 431         if (PrintMiscellaneous && Verbose) {
 432           char buf[256];
 433           jio_snprintf(buf, sizeof(buf), "Execution protection violation "
 434                        "at " INTPTR_FORMAT
 435                        ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr,
 436                        page_start, (res ? "success" : "failed"), errno);
 437           tty->print_raw_cr(buf);
 438         }
 439         stub = pc;
 440 
 441         // Set last_addr so if we fault again at the same address, we don't end
 442         // up in an endless loop.
 443         //
 444         // There are two potential complications here.  Two threads trapping at
 445         // the same address at the same time could cause one of the threads to
 446         // think it already unguarded, and abort the VM.  Likely very rare.
 447         //
 448         // The other race involves two threads alternately trapping at
 449         // different addresses and failing to unguard the page, resulting in


 555 
 556   if (addr != NULL) {
 557     release_memory(addr, bytes);
 558   }
 559 
 560   return addr != NULL;
 561 #endif // AMD64
 562 }
 563 
 564 ////////////////////////////////////////////////////////////////////////////////
 565 // thread stack
 566 
 567 #ifdef AMD64
 568 size_t os::Linux::min_stack_allowed  = 64 * K;
 569 
 570 // amd64: pthread on amd64 is always in floating stack mode
 571 bool os::Linux::supports_variable_stack_size() {  return true; }
 572 #else
 573 size_t os::Linux::min_stack_allowed  =  (48 DEBUG_ONLY(+4))*K;
 574 
 575 #ifdef __GNUC__
 576 #define GET_GS() ({int gs; __asm__ volatile("movw %%gs, %w0":"=q"(gs)); gs&0xffff;})
 577 #endif
 578 
 579 // Test if pthread library can support variable thread stack size. LinuxThreads
 580 // in fixed stack mode allocates 2M fixed slot for each thread. LinuxThreads
 581 // in floating stack mode and NPTL support variable stack size.
 582 bool os::Linux::supports_variable_stack_size() {
 583   if (os::Linux::is_NPTL()) {
 584      // NPTL, yes
 585      return true;
 586 
 587   } else {
 588     // Note: We can't control default stack size when creating a thread.
 589     // If we use non-default stack size (pthread_attr_setstacksize), both
 590     // floating stack and non-floating stack LinuxThreads will return the
 591     // same value. This makes it impossible to implement this function by
 592     // detecting thread stack size directly.
 593     //
 594     // An alternative approach is to check %gs. Fixed-stack LinuxThreads
 595     // do not use %gs, so its value is 0. Floating-stack LinuxThreads use
 596     // %gs (either as LDT selector or GDT selector, depending on kernel)
 597     // to access thread specific data.
 598     //
 599     // Note that %gs is a reserved glibc register since early 2001, so
 600     // applications are not allowed to change its value (Ulrich Drepper from
 601     // Red Hat confirmed that all known offenders have been modified to use
 602     // either %fs or TSD). In the worst case scenario, when VM is embedded in
 603     // a native application that plays with %gs, we might see non-zero %gs
 604     // even LinuxThreads is running in fixed stack mode. As the result, we'll
 605     // return true and skip _thread_safety_check(), so we may not be able to
 606     // detect stack-heap collisions. But otherwise it's harmless.
 607     //
 608 #ifdef __GNUC__
 609     return (GET_GS() != 0);
 610 #else
 611     return false;
 612 #endif
 613   }
 614 }
 615 #endif // AMD64
 616 
 617 // return default stack size for thr_type
 618 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
 619   // default stack size (compiler thread needs larger stack)
 620 #ifdef AMD64
 621   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 622 #else
 623   size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
 624 #endif // AMD64
 625   return s;
 626 }
 627 
 628 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
 629   // Creating guard page is very expensive. Java thread has HotSpot
 630   // guard page, only enable glibc guard page for non-Java threads.
 631   return (thr_type == java_thread ? 0 : page_size());
 632 }