1 /*
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
729 } else {
730 CppVtableCloner<InstanceKlass>::patch(ik);
731 }
732 ConstantPool* cp = ik->constants();
733 CppVtableCloner<ConstantPool>::patch(cp);
734 for (int j = 0; j < ik->methods()->length(); j++) {
735 Method* m = ik->methods()->at(j);
736 CppVtableCloner<Method>::patch(m);
737 assert(CppVtableCloner<Method>::is_valid_shared_object(m), "must be");
738 }
739 } else if (obj->is_objArray_klass()) {
740 CppVtableCloner<ObjArrayKlass>::patch(obj);
741 } else {
742 assert(obj->is_typeArray_klass(), "sanity");
743 CppVtableCloner<TypeArrayKlass>::patch(obj);
744 }
745 }
746 }
747
748 bool MetaspaceShared::is_valid_shared_method(const Method* m) {
749 assert(is_in_shared_space(m), "must be");
750 return CppVtableCloner<Method>::is_valid_shared_object(m);
751 }
752
753 // Closure for serializing initialization data out to a data area to be
754 // written to the shared file.
755
756 class WriteClosure : public SerializeClosure {
757 private:
758 DumpRegion* _dump_region;
759
760 public:
761 WriteClosure(DumpRegion* r) {
762 _dump_region = r;
763 }
764
765 void do_ptr(void** p) {
766 _dump_region->append_intptr_t((intptr_t)*p);
767 }
768
769 void do_u4(u4* p) {
1802 old_tag = (int)(intptr_t)nextPtr();
1803 // do_int(&old_tag);
1804 assert(tag == old_tag, "old tag doesn't match");
1805 FileMapInfo::assert_mark(tag == old_tag);
1806 }
1807
1808 void do_region(u_char* start, size_t size) {
1809 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
1810 assert(size % sizeof(intptr_t) == 0, "bad size");
1811 do_tag((int)size);
1812 while (size > 0) {
1813 *(intptr_t*)start = nextPtr();
1814 start += sizeof(intptr_t);
1815 size -= sizeof(intptr_t);
1816 }
1817 }
1818
1819 bool reading() const { return true; }
1820 };
1821
1822 // Return true if given address is in the mapped shared space.
1823 bool MetaspaceShared::is_in_shared_space(const void* p) {
1824 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p);
1825 }
1826
1827 // Return true if given address is in the misc data region
1828 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
1829 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
1830 }
1831
1832 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
1833 if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
1834 return true;
1835 }
1836 return false;
1837 }
1838
1839 void MetaspaceShared::print_shared_spaces() {
1840 if (UseSharedSpaces) {
1841 FileMapInfo::current_info()->print_shared_spaces();
1842 }
1843 }
1844
1845
1846 // Map shared spaces at requested addresses and return if succeeded.
1847 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
1848 size_t image_alignment = mapinfo->alignment();
1849
1850 #ifndef _WINDOWS
1851 // Map in the shared memory and then map the regions on top of it.
1852 // On Windows, don't map the memory here because it will cause the
1853 // mappings of the regions to fail.
1854 ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
1855 if (!shared_rs.is_reserved()) return false;
1856 #endif
1857
1858 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
1859
1860 char* _ro_base = NULL;
1861 char* _rw_base = NULL;
1862 char* _mc_base = NULL;
1863 char* _md_base = NULL;
1864 char* _od_base = NULL;
1865
1866 // Map each shared region
1867 if ((_mc_base = mapinfo->map_region(mc)) != NULL &&
1868 mapinfo->verify_region_checksum(mc) &&
1869 (_rw_base = mapinfo->map_region(rw)) != NULL &&
1870 mapinfo->verify_region_checksum(rw) &&
1871 (_ro_base = mapinfo->map_region(ro)) != NULL &&
1872 mapinfo->verify_region_checksum(ro) &&
1873 (_md_base = mapinfo->map_region(md)) != NULL &&
1874 mapinfo->verify_region_checksum(md) &&
1875 (_od_base = mapinfo->map_region(od)) != NULL &&
1876 mapinfo->verify_region_checksum(od) &&
1877 (image_alignment == (size_t)os::vm_allocation_granularity()) &&
1878 mapinfo->validate_classpath_entry_table()) {
1879 // Success (no need to do anything)
1880 return true;
1881 } else {
1882 // If there was a failure in mapping any of the spaces, unmap the ones
1883 // that succeeded
1884 if (_ro_base != NULL) mapinfo->unmap_region(ro);
1885 if (_rw_base != NULL) mapinfo->unmap_region(rw);
1886 if (_mc_base != NULL) mapinfo->unmap_region(mc);
1887 if (_md_base != NULL) mapinfo->unmap_region(md);
1888 if (_od_base != NULL) mapinfo->unmap_region(od);
1889 #ifndef _WINDOWS
1890 // Release the entire mapped region
1891 shared_rs.release();
1892 #endif
1893 // If -Xshare:on is specified, print out the error message and exit VM,
1894 // otherwise, set UseSharedSpaces to false and continue.
1895 if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
1896 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
1897 } else {
1898 FLAG_SET_DEFAULT(UseSharedSpaces, false);
1899 }
1900 return false;
1901 }
1902 }
1903
1904 // Read the miscellaneous data from the shared file, and
1905 // serialize it out to its various destinations.
1906
1907 void MetaspaceShared::initialize_shared_spaces() {
1908 FileMapInfo *mapinfo = FileMapInfo::current_info();
|
1 /*
2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
729 } else {
730 CppVtableCloner<InstanceKlass>::patch(ik);
731 }
732 ConstantPool* cp = ik->constants();
733 CppVtableCloner<ConstantPool>::patch(cp);
734 for (int j = 0; j < ik->methods()->length(); j++) {
735 Method* m = ik->methods()->at(j);
736 CppVtableCloner<Method>::patch(m);
737 assert(CppVtableCloner<Method>::is_valid_shared_object(m), "must be");
738 }
739 } else if (obj->is_objArray_klass()) {
740 CppVtableCloner<ObjArrayKlass>::patch(obj);
741 } else {
742 assert(obj->is_typeArray_klass(), "sanity");
743 CppVtableCloner<TypeArrayKlass>::patch(obj);
744 }
745 }
746 }
747
748 bool MetaspaceShared::is_valid_shared_method(const Method* m) {
749 assert(is_in_shared_metaspace(m), "must be");
750 return CppVtableCloner<Method>::is_valid_shared_object(m);
751 }
752
753 // Closure for serializing initialization data out to a data area to be
754 // written to the shared file.
755
756 class WriteClosure : public SerializeClosure {
757 private:
758 DumpRegion* _dump_region;
759
760 public:
761 WriteClosure(DumpRegion* r) {
762 _dump_region = r;
763 }
764
765 void do_ptr(void** p) {
766 _dump_region->append_intptr_t((intptr_t)*p);
767 }
768
769 void do_u4(u4* p) {
1802 old_tag = (int)(intptr_t)nextPtr();
1803 // do_int(&old_tag);
1804 assert(tag == old_tag, "old tag doesn't match");
1805 FileMapInfo::assert_mark(tag == old_tag);
1806 }
1807
1808 void do_region(u_char* start, size_t size) {
1809 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
1810 assert(size % sizeof(intptr_t) == 0, "bad size");
1811 do_tag((int)size);
1812 while (size > 0) {
1813 *(intptr_t*)start = nextPtr();
1814 start += sizeof(intptr_t);
1815 size -= sizeof(intptr_t);
1816 }
1817 }
1818
1819 bool reading() const { return true; }
1820 };
1821
1822 // Return true if given address is in the misc data region
1823 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
1824 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
1825 }
1826
1827 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
1828 if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
1829 return true;
1830 }
1831 return false;
1832 }
1833
1834 void MetaspaceShared::print_shared_spaces() {
1835 if (UseSharedSpaces) {
1836 FileMapInfo::current_info()->print_shared_spaces();
1837 }
1838 }
1839
1840
1841 // Map shared spaces at requested addresses and return if succeeded.
1842 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
1843 size_t image_alignment = mapinfo->alignment();
1844
1845 #ifndef _WINDOWS
1846 // Map in the shared memory and then map the regions on top of it.
1847 // On Windows, don't map the memory here because it will cause the
1848 // mappings of the regions to fail.
1849 ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
1850 if (!shared_rs.is_reserved()) return false;
1851 #endif
1852
1853 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
1854
1855 char* ro_base = NULL; char* ro_top;
1856 char* rw_base = NULL; char* rw_top;
1857 char* mc_base = NULL; char* mc_top;
1858 char* md_base = NULL; char* md_top;
1859 char* od_base = NULL; char* od_top;
1860
1861 // Map each shared region
1862 if ((mc_base = mapinfo->map_region(mc, &mc_top)) != NULL &&
1863 (rw_base = mapinfo->map_region(rw, &rw_top)) != NULL &&
1864 (ro_base = mapinfo->map_region(ro, &ro_top)) != NULL &&
1865 (md_base = mapinfo->map_region(md, &md_top)) != NULL &&
1866 (od_base = mapinfo->map_region(od, &od_top)) != NULL &&
1867 (image_alignment == (size_t)os::vm_allocation_granularity()) &&
1868 mapinfo->validate_classpath_entry_table()) {
1869 // Success -- set up MetaspaceObj::_shared_metaspace_{base,top} for
1870 // fast checking in MetaspaceShared::is_in_shared_metaspace() and
1871 // MetaspaceObj::is_shared().
1872 //
1873 // We require that mc->rw->ro->md->od to be laid out consecutively, with no
1874 // gaps between them. That way, we can ensure that the OS won't be able to
1875 // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
1876 // would mess up the simplee comparision in
1877 // MetaspaceShared::is_in_shared_metaspace().
1878 assert(mc_base < ro_base && mc_base < rw_base && mc_base < md_base && mc_base < od_base, "must be");
1879 assert(od_top > ro_top && od_top > rw_top && od_top > md_top && od_top > mc_top , "must be");
1880 assert(mc_top == rw_base, "must be");
1881 assert(rw_top == ro_base, "must be");
1882 assert(ro_top == md_base, "must be");
1883 assert(md_top == od_base, "must be");
1884
1885 MetaspaceObj::_shared_metaspace_base = (void*)mc_base;
1886 MetaspaceObj::_shared_metaspace_top = (void*)od_top;
1887 return true;
1888 } else {
1889 // If there was a failure in mapping any of the spaces, unmap the ones
1890 // that succeeded
1891 if (ro_base != NULL) mapinfo->unmap_region(ro);
1892 if (rw_base != NULL) mapinfo->unmap_region(rw);
1893 if (mc_base != NULL) mapinfo->unmap_region(mc);
1894 if (md_base != NULL) mapinfo->unmap_region(md);
1895 if (od_base != NULL) mapinfo->unmap_region(od);
1896 #ifndef _WINDOWS
1897 // Release the entire mapped region
1898 shared_rs.release();
1899 #endif
1900 // If -Xshare:on is specified, print out the error message and exit VM,
1901 // otherwise, set UseSharedSpaces to false and continue.
1902 if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
1903 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
1904 } else {
1905 FLAG_SET_DEFAULT(UseSharedSpaces, false);
1906 }
1907 return false;
1908 }
1909 }
1910
1911 // Read the miscellaneous data from the shared file, and
1912 // serialize it out to its various destinations.
1913
1914 void MetaspaceShared::initialize_shared_spaces() {
1915 FileMapInfo *mapinfo = FileMapInfo::current_info();
|