src/os/linux/vm/os_linux.cpp

Print this page
rev 7258 : 8064611: AARCH64: Changes to HotSpot shared code
Summary: Everything except cpu/ and os_cpu/.
Reviewed-by: kvn


 258 #if   defined(ZERO)
 259 static char cpu_arch[] = ZERO_LIBARCH;
 260 #elif defined(IA64)
 261 static char cpu_arch[] = "ia64";
 262 #elif defined(IA32)
 263 static char cpu_arch[] = "i386";
 264 #elif defined(AMD64)
 265 static char cpu_arch[] = "amd64";
 266 #elif defined(ARM)
 267 static char cpu_arch[] = "arm";
 268 #elif defined(PPC32)
 269 static char cpu_arch[] = "ppc";
 270 #elif defined(PPC64)
 271 static char cpu_arch[] = "ppc64";
 272 #elif defined(SPARC)
 273   #ifdef _LP64
 274 static char cpu_arch[] = "sparcv9";
 275   #else
 276 static char cpu_arch[] = "sparc";
 277   #endif


 278 #else
 279   #error Add appropriate cpu_arch setting
 280 #endif
 281 
 282 
 283 // pid_t gettid()
 284 //
 285 // Returns the kernel thread id of the currently running thread. Kernel
 286 // thread id is used to access /proc.
 287 //
 288 // (Note that getpid() on LinuxThreads returns kernel thread id too; but
 289 // on NPTL, it returns the same pid for all threads, as required by POSIX.)
 290 //
 291 pid_t os::Linux::gettid() {
 292   int rslt = syscall(SYS_gettid);
 293   if (rslt == -1) {
 294     // old kernel, no NPTL support
 295     return getpid();
 296   } else {
 297     return (pid_t)rslt;


1909     (sizeof(elf_head)!=
1910      (::read(file_descriptor, &elf_head,sizeof(elf_head))));
1911 
1912   ::close(file_descriptor);
1913   if (failed_to_read_elf_head) {
1914     // file i/o error - report dlerror() msg
1915     return NULL;
1916   }
1917 
1918   typedef struct {
1919     Elf32_Half  code;         // Actual value as defined in elf.h
1920     Elf32_Half  compat_class; // Compatibility of archs at VM's sense
1921     char        elf_class;    // 32 or 64 bit
1922     char        endianess;    // MSB or LSB
1923     char*       name;         // String representation
1924   } arch_t;
1925 
1926 #ifndef EM_486
1927   #define EM_486          6               /* Intel 80486 */
1928 #endif



1929 
1930   static const arch_t arch_array[]={
1931     {EM_386,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1932     {EM_486,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1933     {EM_IA_64,       EM_IA_64,   ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
1934     {EM_X86_64,      EM_X86_64,  ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
1935     {EM_SPARC,       EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1936     {EM_SPARC32PLUS, EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1937     {EM_SPARCV9,     EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
1938     {EM_PPC,         EM_PPC,     ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
1939 #if defined(VM_LITTLE_ENDIAN)
1940     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2LSB, (char*)"Power PC 64"},
1941 #else
1942     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
1943 #endif
1944     {EM_ARM,         EM_ARM,     ELFCLASS32,   ELFDATA2LSB, (char*)"ARM"},
1945     {EM_S390,        EM_S390,    ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},
1946     {EM_ALPHA,       EM_ALPHA,   ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},
1947     {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},
1948     {EM_MIPS,        EM_MIPS,    ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},
1949     {EM_PARISC,      EM_PARISC,  ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},
1950     {EM_68K,         EM_68K,     ELFCLASS32, ELFDATA2MSB, (char*)"M68k"}

1951   };
1952 
1953 #if  (defined IA32)
1954   static  Elf32_Half running_arch_code=EM_386;
1955 #elif   (defined AMD64)
1956   static  Elf32_Half running_arch_code=EM_X86_64;
1957 #elif  (defined IA64)
1958   static  Elf32_Half running_arch_code=EM_IA_64;
1959 #elif  (defined __sparc) && (defined _LP64)
1960   static  Elf32_Half running_arch_code=EM_SPARCV9;
1961 #elif  (defined __sparc) && (!defined _LP64)
1962   static  Elf32_Half running_arch_code=EM_SPARC;
1963 #elif  (defined __powerpc64__)
1964   static  Elf32_Half running_arch_code=EM_PPC64;
1965 #elif  (defined __powerpc__)
1966   static  Elf32_Half running_arch_code=EM_PPC;
1967 #elif  (defined ARM)
1968   static  Elf32_Half running_arch_code=EM_ARM;
1969 #elif  (defined S390)
1970   static  Elf32_Half running_arch_code=EM_S390;
1971 #elif  (defined ALPHA)
1972   static  Elf32_Half running_arch_code=EM_ALPHA;
1973 #elif  (defined MIPSEL)
1974   static  Elf32_Half running_arch_code=EM_MIPS_RS3_LE;
1975 #elif  (defined PARISC)
1976   static  Elf32_Half running_arch_code=EM_PARISC;
1977 #elif  (defined MIPS)
1978   static  Elf32_Half running_arch_code=EM_MIPS;
1979 #elif  (defined M68K)
1980   static  Elf32_Half running_arch_code=EM_68K;


1981 #else
1982     #error Method os::dll_load requires that one of following is defined:\
1983          IA32, AMD64, IA64, __sparc, __powerpc__, ARM, S390, ALPHA, MIPS, MIPSEL, PARISC, M68K
1984 #endif
1985 
1986   // Identify compatability class for VM's architecture and library's architecture
1987   // Obtain string descriptions for architectures
1988 
1989   arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
1990   int running_arch_index=-1;
1991 
1992   for (unsigned int i=0; i < ARRAY_SIZE(arch_array); i++) {
1993     if (running_arch_code == arch_array[i].code) {
1994       running_arch_index    = i;
1995     }
1996     if (lib_arch.code == arch_array[i].code) {
1997       lib_arch.compat_class = arch_array[i].compat_class;
1998       lib_arch.name         = arch_array[i].name;
1999     }
2000   }
2001 
2002   assert(running_arch_index != -1,
2003          "Didn't find running architecture code (running_arch_code) in arch_array");


3290 static size_t _large_page_size = 0;
3291 
3292 size_t os::Linux::find_large_page_size() {
3293   size_t large_page_size = 0;
3294 
3295   // large_page_size on Linux is used to round up heap size. x86 uses either
3296   // 2M or 4M page, depending on whether PAE (Physical Address Extensions)
3297   // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
3298   // page as large as 256M.
3299   //
3300   // Here we try to figure out page size by parsing /proc/meminfo and looking
3301   // for a line with the following format:
3302   //    Hugepagesize:     2048 kB
3303   //
3304   // If we can't determine the value (e.g. /proc is not mounted, or the text
3305   // format has been changed), we'll use the largest page size supported by
3306   // the processor.
3307 
3308 #ifndef ZERO
3309   large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M)
3310                      ARM_ONLY(2 * M) PPC_ONLY(4 * M);
3311 #endif // ZERO
3312 
3313   FILE *fp = fopen("/proc/meminfo", "r");
3314   if (fp) {
3315     while (!feof(fp)) {
3316       int x = 0;
3317       char buf[16];
3318       if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
3319         if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
3320           large_page_size = x * K;
3321           break;
3322         }
3323       } else {
3324         // skip to next line
3325         for (;;) {
3326           int ch = fgetc(fp);
3327           if (ch == EOF || ch == (int)'\n') break;
3328         }
3329       }
3330     }


5853       } else {
5854         status = pthread_mutex_unlock(_mutex);
5855         assert(status == 0, "invariant");
5856         status = pthread_cond_signal(&_cond[_cur_index]);
5857         assert(status == 0, "invariant");
5858       }
5859     } else {
5860       pthread_mutex_unlock(_mutex);
5861       assert(status == 0, "invariant");
5862     }
5863   } else {
5864     pthread_mutex_unlock(_mutex);
5865     assert(status == 0, "invariant");
5866   }
5867 }
5868 
5869 
5870 extern char** environ;
5871 
5872 #ifndef __NR_fork
5873   #define __NR_fork IA32_ONLY(2) IA64_ONLY(not defined) AMD64_ONLY(57)
5874 #endif
5875 
5876 #ifndef __NR_execve
5877   #define __NR_execve IA32_ONLY(11) IA64_ONLY(1033) AMD64_ONLY(59)
5878 #endif
5879 
5880 // Run the specified command in a separate process. Return its exit value,
5881 // or -1 on failure (e.g. can't fork a new process).
5882 // Unlike system(), this function can be called from signal handler. It
5883 // doesn't block SIGINT et al.
5884 int os::fork_and_exec(char* cmd) {
5885   const char * argv[4] = {"sh", "-c", cmd, NULL};
5886 
5887   // fork() in LinuxThreads/NPTL is not async-safe. It needs to run
5888   // pthread_atfork handlers and reset pthread library. All we need is a
5889   // separate process to execve. Make a direct syscall to fork process.
5890   // On IA64 there's no fork syscall, we have to use fork() and hope for
5891   // the best...
5892   pid_t pid = NOT_IA64(syscall(__NR_fork);)
5893   IA64_ONLY(fork();)
5894 
5895   if (pid < 0) {
5896     // fork failed
5897     return -1;




 258 #if   defined(ZERO)
 259 static char cpu_arch[] = ZERO_LIBARCH;
 260 #elif defined(IA64)
 261 static char cpu_arch[] = "ia64";
 262 #elif defined(IA32)
 263 static char cpu_arch[] = "i386";
 264 #elif defined(AMD64)
 265 static char cpu_arch[] = "amd64";
 266 #elif defined(ARM)
 267 static char cpu_arch[] = "arm";
 268 #elif defined(PPC32)
 269 static char cpu_arch[] = "ppc";
 270 #elif defined(PPC64)
 271 static char cpu_arch[] = "ppc64";
 272 #elif defined(SPARC)
 273   #ifdef _LP64
 274 static char cpu_arch[] = "sparcv9";
 275   #else
 276 static char cpu_arch[] = "sparc";
 277   #endif
 278 #elif defined(AARCH64)
 279 static char cpu_arch[] = "aarch64";
 280 #else
 281   #error Add appropriate cpu_arch setting
 282 #endif
 283 
 284 
 285 // pid_t gettid()
 286 //
 287 // Returns the kernel thread id of the currently running thread. Kernel
 288 // thread id is used to access /proc.
 289 //
 290 // (Note that getpid() on LinuxThreads returns kernel thread id too; but
 291 // on NPTL, it returns the same pid for all threads, as required by POSIX.)
 292 //
 293 pid_t os::Linux::gettid() {
 294   int rslt = syscall(SYS_gettid);
 295   if (rslt == -1) {
 296     // old kernel, no NPTL support
 297     return getpid();
 298   } else {
 299     return (pid_t)rslt;


1911     (sizeof(elf_head)!=
1912      (::read(file_descriptor, &elf_head,sizeof(elf_head))));
1913 
1914   ::close(file_descriptor);
1915   if (failed_to_read_elf_head) {
1916     // file i/o error - report dlerror() msg
1917     return NULL;
1918   }
1919 
1920   typedef struct {
1921     Elf32_Half  code;         // Actual value as defined in elf.h
1922     Elf32_Half  compat_class; // Compatibility of archs at VM's sense
1923     char        elf_class;    // 32 or 64 bit
1924     char        endianess;    // MSB or LSB
1925     char*       name;         // String representation
1926   } arch_t;
1927 
1928 #ifndef EM_486
1929   #define EM_486          6               /* Intel 80486 */
1930 #endif
1931 #ifndef EM_AARCH64
1932   #define EM_AARCH64    183               /* ARM AARCH64 */
1933 #endif
1934 
1935   static const arch_t arch_array[]={
1936     {EM_386,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1937     {EM_486,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
1938     {EM_IA_64,       EM_IA_64,   ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
1939     {EM_X86_64,      EM_X86_64,  ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
1940     {EM_SPARC,       EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1941     {EM_SPARC32PLUS, EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
1942     {EM_SPARCV9,     EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
1943     {EM_PPC,         EM_PPC,     ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
1944 #if defined(VM_LITTLE_ENDIAN)
1945     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2LSB, (char*)"Power PC 64"},
1946 #else
1947     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
1948 #endif
1949     {EM_ARM,         EM_ARM,     ELFCLASS32,   ELFDATA2LSB, (char*)"ARM"},
1950     {EM_S390,        EM_S390,    ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},
1951     {EM_ALPHA,       EM_ALPHA,   ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},
1952     {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},
1953     {EM_MIPS,        EM_MIPS,    ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},
1954     {EM_PARISC,      EM_PARISC,  ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},
1955     {EM_68K,         EM_68K,     ELFCLASS32, ELFDATA2MSB, (char*)"M68k"},
1956     {EM_AARCH64,     EM_AARCH64, ELFCLASS64, ELFDATA2LSB, (char*)"AARCH64"},
1957   };
1958 
1959 #if  (defined IA32)
1960   static  Elf32_Half running_arch_code=EM_386;
1961 #elif   (defined AMD64)
1962   static  Elf32_Half running_arch_code=EM_X86_64;
1963 #elif  (defined IA64)
1964   static  Elf32_Half running_arch_code=EM_IA_64;
1965 #elif  (defined __sparc) && (defined _LP64)
1966   static  Elf32_Half running_arch_code=EM_SPARCV9;
1967 #elif  (defined __sparc) && (!defined _LP64)
1968   static  Elf32_Half running_arch_code=EM_SPARC;
1969 #elif  (defined __powerpc64__)
1970   static  Elf32_Half running_arch_code=EM_PPC64;
1971 #elif  (defined __powerpc__)
1972   static  Elf32_Half running_arch_code=EM_PPC;
1973 #elif  (defined ARM)
1974   static  Elf32_Half running_arch_code=EM_ARM;
1975 #elif  (defined S390)
1976   static  Elf32_Half running_arch_code=EM_S390;
1977 #elif  (defined ALPHA)
1978   static  Elf32_Half running_arch_code=EM_ALPHA;
1979 #elif  (defined MIPSEL)
1980   static  Elf32_Half running_arch_code=EM_MIPS_RS3_LE;
1981 #elif  (defined PARISC)
1982   static  Elf32_Half running_arch_code=EM_PARISC;
1983 #elif  (defined MIPS)
1984   static  Elf32_Half running_arch_code=EM_MIPS;
1985 #elif  (defined M68K)
1986   static  Elf32_Half running_arch_code=EM_68K;
1987 #elif  (defined AARCH64)
1988   static  Elf32_Half running_arch_code=EM_AARCH64;
1989 #else
1990     #error Method os::dll_load requires that one of following is defined:\
1991          IA32, AMD64, IA64, __sparc, __powerpc__, ARM, S390, ALPHA, MIPS, MIPSEL, PARISC, M68K, AARCH64
1992 #endif
1993 
1994   // Identify compatability class for VM's architecture and library's architecture
1995   // Obtain string descriptions for architectures
1996 
1997   arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
1998   int running_arch_index=-1;
1999 
2000   for (unsigned int i=0; i < ARRAY_SIZE(arch_array); i++) {
2001     if (running_arch_code == arch_array[i].code) {
2002       running_arch_index    = i;
2003     }
2004     if (lib_arch.code == arch_array[i].code) {
2005       lib_arch.compat_class = arch_array[i].compat_class;
2006       lib_arch.name         = arch_array[i].name;
2007     }
2008   }
2009 
2010   assert(running_arch_index != -1,
2011          "Didn't find running architecture code (running_arch_code) in arch_array");


3298 static size_t _large_page_size = 0;
3299 
3300 size_t os::Linux::find_large_page_size() {
3301   size_t large_page_size = 0;
3302 
3303   // large_page_size on Linux is used to round up heap size. x86 uses either
3304   // 2M or 4M page, depending on whether PAE (Physical Address Extensions)
3305   // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
3306   // page as large as 256M.
3307   //
3308   // Here we try to figure out page size by parsing /proc/meminfo and looking
3309   // for a line with the following format:
3310   //    Hugepagesize:     2048 kB
3311   //
3312   // If we can't determine the value (e.g. /proc is not mounted, or the text
3313   // format has been changed), we'll use the largest page size supported by
3314   // the processor.
3315 
3316 #ifndef ZERO
3317   large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M)
3318                      ARM_ONLY(2 * M) PPC_ONLY(4 * M) AARCH64_ONLY(2 * M);
3319 #endif // ZERO
3320 
3321   FILE *fp = fopen("/proc/meminfo", "r");
3322   if (fp) {
3323     while (!feof(fp)) {
3324       int x = 0;
3325       char buf[16];
3326       if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
3327         if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
3328           large_page_size = x * K;
3329           break;
3330         }
3331       } else {
3332         // skip to next line
3333         for (;;) {
3334           int ch = fgetc(fp);
3335           if (ch == EOF || ch == (int)'\n') break;
3336         }
3337       }
3338     }


5861       } else {
5862         status = pthread_mutex_unlock(_mutex);
5863         assert(status == 0, "invariant");
5864         status = pthread_cond_signal(&_cond[_cur_index]);
5865         assert(status == 0, "invariant");
5866       }
5867     } else {
5868       pthread_mutex_unlock(_mutex);
5869       assert(status == 0, "invariant");
5870     }
5871   } else {
5872     pthread_mutex_unlock(_mutex);
5873     assert(status == 0, "invariant");
5874   }
5875 }
5876 
5877 
5878 extern char** environ;
5879 
5880 #ifndef __NR_fork
5881   #define __NR_fork IA32_ONLY(2) IA64_ONLY(not defined) AMD64_ONLY(57) AARCH64_ONLY(1079)
5882 #endif
5883 
5884 #ifndef __NR_execve
5885   #define __NR_execve IA32_ONLY(11) IA64_ONLY(1033) AMD64_ONLY(59) AARCH64_ONLY(221)
5886 #endif
5887 
5888 // Run the specified command in a separate process. Return its exit value,
5889 // or -1 on failure (e.g. can't fork a new process).
5890 // Unlike system(), this function can be called from signal handler. It
5891 // doesn't block SIGINT et al.
5892 int os::fork_and_exec(char* cmd) {
5893   const char * argv[4] = {"sh", "-c", cmd, NULL};
5894 
5895   // fork() in LinuxThreads/NPTL is not async-safe. It needs to run
5896   // pthread_atfork handlers and reset pthread library. All we need is a
5897   // separate process to execve. Make a direct syscall to fork process.
5898   // On IA64 there's no fork syscall, we have to use fork() and hope for
5899   // the best...
5900   pid_t pid = NOT_IA64(syscall(__NR_fork);)
5901   IA64_ONLY(fork();)
5902 
5903   if (pid < 0) {
5904     // fork failed
5905     return -1;