filemap.hpp revision 6872:16286b7d7c6e
169783Smsmith/*
269783Smsmith * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
369783Smsmith * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
469783Smsmith *
569783Smsmith * This code is free software; you can redistribute it and/or modify it
669783Smsmith * under the terms of the GNU General Public License version 2 only, as
769783Smsmith * published by the Free Software Foundation.
869783Smsmith *
969783Smsmith * This code is distributed in the hope that it will be useful, but WITHOUT
1069783Smsmith * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1169783Smsmith * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1269783Smsmith * version 2 for more details (a copy is included in the LICENSE file that
1369783Smsmith * accompanied this code).
1469783Smsmith *
1569783Smsmith * You should have received a copy of the GNU General Public License version
1669783Smsmith * 2 along with this work; if not, write to the Free Software Foundation,
1769783Smsmith * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1869783Smsmith *
1969783Smsmith * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2069783Smsmith * or visit www.oracle.com if you need additional information or have any
2169783Smsmith * questions.
2269783Smsmith *
2369783Smsmith */
2469783Smsmith
2569783Smsmith#ifndef SHARE_VM_MEMORY_FILEMAP_HPP
2669783Smsmith#define SHARE_VM_MEMORY_FILEMAP_HPP
2769783Smsmith
2869783Smsmith#include "memory/metaspaceShared.hpp"
2969783Smsmith#include "memory/metaspace.hpp"
3069783Smsmith
3169783Smsmith// Layout of the file:
3269783Smsmith//  header: dump of archive instance plus versioning info, datestamp, etc.
3369783Smsmith//   [magic # = 0xF00BABA2]
3469783Smsmith//  ... padding to align on page-boundary
3569783Smsmith//  read-write space
3669783Smsmith//  read-only space
3769783Smsmith//  misc data (block offset table, string table, symbols, dictionary, etc.)
3869783Smsmith//  tag(666)
3969783Smsmith
4069783Smsmithstatic const int JVM_IDENT_MAX = 256;
4169783Smsmith
4269783Smsmithclass Metaspace;
4369783Smsmith
4469783Smsmithclass SharedClassPathEntry VALUE_OBJ_CLASS_SPEC {
4569783Smsmithpublic:
4669783Smsmith  const char *_name;
4769783Smsmith  time_t _timestamp;          // jar timestamp,  0 if is directory
4869783Smsmith  long   _filesize;           // jar file size, -1 if is directory
4969783Smsmith  bool is_dir() {
5069783Smsmith    return _filesize == -1;
5169783Smsmith  }
5269783Smsmith};
5369783Smsmith
5469783Smsmithclass FileMapInfo : public CHeapObj<mtInternal> {
5569783Smsmithprivate:
5669783Smsmith  friend class ManifestStream;
5769783Smsmith  enum {
5869783Smsmith    _invalid_version = -1,
5969783Smsmith    _current_version = 2
6069783Smsmith  };
6169783Smsmith
6269783Smsmith  bool  _file_open;
6369783Smsmith  int   _fd;
6469783Smsmith  long  _file_offset;
6569783Smsmith
6669783Smsmithprivate:
6769783Smsmith  static SharedClassPathEntry* _classpath_entry_table;
6869783Smsmith  static int                   _classpath_entry_table_size;
6969783Smsmith  static size_t                _classpath_entry_size;
7069783Smsmith  static bool                  _validating_classpath_entry_table;
7169783Smsmith
7269783Smsmith  // FileMapHeader describes the shared space data in the file to be
7369783Smsmith  // mapped.  This structure gets written to a file.  It is not a class, so
7469783Smsmith  // that the compilers don't add any compiler-private data to it.
7569783Smsmith
7669783Smsmithpublic:
7769783Smsmith  struct FileMapHeaderBase : public CHeapObj<mtClass> {
7869783Smsmith    virtual bool validate() = 0;
7969783Smsmith    virtual void populate(FileMapInfo* info, size_t alignment) = 0;
8069783Smsmith  };
8169783Smsmith  struct FileMapHeader : FileMapHeaderBase {
8269783Smsmith    // Use data() and data_size() to memcopy to/from the FileMapHeader. We need to
8369783Smsmith    // avoid read/writing the C++ vtable pointer.
8469783Smsmith    static size_t data_size();
8569783Smsmith    char* data() {
8669783Smsmith      return ((char*)this) + sizeof(FileMapHeaderBase);
8769783Smsmith    }
8869783Smsmith
8969783Smsmith    int    _magic;                    // identify file type.
9069783Smsmith    int    _version;                  // (from enum, above.)
9169783Smsmith    size_t _alignment;                // how shared archive should be aligned
9269783Smsmith    int    _obj_alignment;            // value of ObjectAlignmentInBytes
9369783Smsmith
9469783Smsmith    struct space_info {
9569783Smsmith      int    _file_offset;   // sizeof(this) rounded to vm page size
9669783Smsmith      char*  _base;          // copy-on-write base address
9769783Smsmith      size_t _capacity;      // for validity checking
9869783Smsmith      size_t _used;          // for setting space top on read
9969783Smsmith      bool   _read_only;     // read only space?
10069783Smsmith      bool   _allow_exec;    // executable code in space?
10169783Smsmith    } _space[MetaspaceShared::n_regions];
10269783Smsmith
10369783Smsmith    // The following fields are all sanity checks for whether this archive
10469783Smsmith    // will function correctly with this JVM and the bootclasspath it's
10569783Smsmith    // invoked with.
10669783Smsmith    char  _jvm_ident[JVM_IDENT_MAX];      // identifier for jvm
10769783Smsmith
10869783Smsmith    // The _paths_misc_info is a variable-size structure that records "miscellaneous"
10969783Smsmith    // information during dumping. It is generated and validated by the
11069783Smsmith    // SharedPathsMiscInfo class. See SharedPathsMiscInfo.hpp and sharedClassUtil.hpp for
11169783Smsmith    // detailed description.
11269783Smsmith    //
11369783Smsmith    // The _paths_misc_info data is stored as a byte array in the archive file header,
11469783Smsmith    // immediately after the _header field. This information is used only when
11569783Smsmith    // checking the validity of the archive and is deallocated after the archive is loaded.
11669783Smsmith    //
11769783Smsmith    // Note that the _paths_misc_info does NOT include information for JAR files
11869783Smsmith    // that existed during dump time. Their information is stored in _classpath_entry_table.
11969783Smsmith    int _paths_misc_info_size;
12069783Smsmith
12169783Smsmith    // The following is a table of all the class path entries that were used
12269783Smsmith    // during dumping. At run time, we require these files to exist and have the same
12369783Smsmith    // size/modification time, or else the archive will refuse to load.
12469783Smsmith    //
12569783Smsmith    // All of these entries must be JAR files. The dumping process would fail if a non-empty
12669783Smsmith    // directory was specified in the classpaths. If an empty directory was specified
12769783Smsmith    // it is checked by the _paths_misc_info as described above.
12869783Smsmith    //
12969783Smsmith    // FIXME -- if JAR files in the tail of the list were specified but not used during dumping,
13069783Smsmith    // they should be removed from this table, to save space and to avoid spurious
13169783Smsmith    // loading failures during runtime.
13269783Smsmith    int _classpath_entry_table_size;
13369783Smsmith    size_t _classpath_entry_size;
13469783Smsmith    SharedClassPathEntry* _classpath_entry_table;
13569908Smsmith
13669908Smsmith    virtual bool validate();
13769783Smsmith    virtual void populate(FileMapInfo* info, size_t alignment);
13869783Smsmith  };
13969783Smsmith
14069783Smsmith  FileMapHeader * _header;
14169908Smsmith
14269908Smsmith  const char* _full_path;
14369908Smsmith  char* _paths_misc_info;
14469908Smsmith
14569908Smsmith  static FileMapInfo* _current_info;
14669908Smsmith
14769908Smsmith  bool  init_from_file(int fd);
14869908Smsmith  void  align_file_position();
14969783Smsmith  bool  validate_header_impl();
15069908Smsmith
15169908Smsmithpublic:
15269908Smsmith  FileMapInfo();
15369908Smsmith  ~FileMapInfo();
15469908Smsmith
15569908Smsmith  static int current_version()        { return _current_version; }
15669908Smsmith  void   populate_header(size_t alignment);
15769908Smsmith  bool   validate_header();
15869908Smsmith  void   invalidate();
15969908Smsmith  int    version()                    { return _header->_version; }
16069908Smsmith  size_t alignment()                  { return _header->_alignment; }
16169908Smsmith  size_t space_capacity(int i)        { return _header->_space[i]._capacity; }
16269908Smsmith  char*  region_base(int i)           { return _header->_space[i]._base; }
16369908Smsmith  struct FileMapHeader* header()      { return _header; }
16469908Smsmith
16569908Smsmith  static FileMapInfo* current_info() {
16669908Smsmith    CDS_ONLY(return _current_info;)
16769908Smsmith    NOT_CDS(return NULL;)
16869908Smsmith  }
16969908Smsmith
17069908Smsmith  static void assert_mark(bool check);
17169908Smsmith
17269908Smsmith  // File manipulation.
17369908Smsmith  bool  initialize() NOT_CDS_RETURN_(false);
17469908Smsmith  bool  open_for_read();
17569908Smsmith  void  open_for_write();
17669908Smsmith  void  write_header();
17769908Smsmith  void  write_space(int i, Metaspace* space, bool read_only);
17869908Smsmith  void  write_region(int region, char* base, size_t size,
17969908Smsmith                     size_t capacity, bool read_only, bool allow_exec);
18069908Smsmith  void  write_bytes(const void* buffer, int count);
18169908Smsmith  void  write_bytes_aligned(const void* buffer, int count);
18269908Smsmith  char* map_region(int i);
18369908Smsmith  void  unmap_region(int i);
18469908Smsmith  void  close();
18569908Smsmith  bool  is_open() { return _file_open; }
18669908Smsmith  ReservedSpace reserve_shared_memory();
18769908Smsmith
18869908Smsmith  // JVM/TI RedefineClasses() support:
18969908Smsmith  // Remap the shared readonly space to shared readwrite, private.
19069908Smsmith  bool  remap_shared_readonly_as_readwrite();
19169908Smsmith
19269908Smsmith  // Errors.
19369908Smsmith  static void fail_stop(const char *msg, ...);
19469908Smsmith  static void fail_continue(const char *msg, ...);
19569908Smsmith
19669908Smsmith  // Return true if given address is in the mapped shared space.
19769783Smsmith  bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false);
19869783Smsmith  void print_shared_spaces() NOT_CDS_RETURN;
19969783Smsmith
20069783Smsmith  static size_t shared_spaces_size() {
20169783Smsmith    return align_size_up(SharedReadOnlySize + SharedReadWriteSize +
20269783Smsmith                         SharedMiscDataSize + SharedMiscCodeSize,
20369783Smsmith                         os::vm_allocation_granularity());
20469783Smsmith  }
20569783Smsmith
20669783Smsmith  // Stop CDS sharing and unmap CDS regions.
20769783Smsmith  static void stop_sharing_and_unmap(const char* msg);
20869783Smsmith
20969783Smsmith  static void allocate_classpath_entry_table();
21069783Smsmith  bool validate_classpath_entry_table();
21169783Smsmith
21269783Smsmith  static SharedClassPathEntry* shared_classpath(int index) {
21369783Smsmith    char* p = (char*)_classpath_entry_table;
21469783Smsmith    p += _classpath_entry_size * index;
21569783Smsmith    return (SharedClassPathEntry*)p;
21669783Smsmith  }
21769783Smsmith  static const char* shared_classpath_name(int index) {
21869783Smsmith    return shared_classpath(index)->_name;
21969783Smsmith  }
22069783Smsmith
22169783Smsmith  static int get_number_of_share_classpaths() {
22269783Smsmith    return _classpath_entry_table_size;
22369783Smsmith  }
22469783Smsmith};
22569783Smsmith
22669783Smsmith#endif // SHARE_VM_MEMORY_FILEMAP_HPP
22769783Smsmith