Path: blob/master/src/hotspot/share/runtime/icache.cpp
40951 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/codeBlob.hpp"26#include "memory/resourceArea.hpp"27#include "runtime/icache.hpp"28#include "utilities/align.hpp"2930// The flush stub function address31AbstractICache::flush_icache_stub_t AbstractICache::_flush_icache_stub = NULL;3233void AbstractICache::initialize() {34// Making this stub must be FIRST use of assembler35ResourceMark rm;3637BufferBlob* b = BufferBlob::create("flush_icache_stub", ICache::stub_size);38if (b == NULL) {39vm_exit_out_of_memory(ICache::stub_size, OOM_MALLOC_ERROR, "CodeCache: no space for flush_icache_stub");40}41CodeBuffer c(b);4243ICacheStubGenerator g(&c);44g.generate_icache_flush(&_flush_icache_stub);4546// The first use of flush_icache_stub must apply it to itself.47// The StubCodeMark destructor in generate_icache_flush will48// call Assembler::flush, which in turn will call invalidate_range,49// which will in turn call the flush stub. Thus we don't need an50// explicit call to invalidate_range here. This assumption is51// checked in invalidate_range.52}5354void AbstractICache::call_flush_stub(address start, int lines) {55// The business with the magic number is just a little security.56// We cannot call the flush stub when generating the flush stub57// because it isn't there yet. So, the stub also returns its third58// parameter. This is a cheap check that the stub was really executed.59static int magic = 0xbaadbabe;6061int auto_magic = magic; // Make a local copy to avoid race condition62int r = (*_flush_icache_stub)(start, lines, auto_magic);63guarantee(r == auto_magic, "flush stub routine did not execute");64++magic;65}6667void AbstractICache::invalidate_word(address addr) {68// Because this is called for instruction patching on the fly, long after69// bootstrapping, we execute the stub directly. Account for a 4-byte word70// spanning two cache lines by computing a start line address by rounding71// addr down to a line_size boundary, and an end line address by adding72// the word size - 1 and rounding the result down to a line_size boundary.73// If we just added word size, we'd mistakenly flush the next cache line74// if the word to be flushed started in the last 4 bytes of the line.75// Doing that would segv if the next line weren't mapped.7677const int word_size_in_bytes = 4; // Always, regardless of platform7879intptr_t start_line = ((intptr_t)addr + 0) & ~(ICache::line_size - 1);80intptr_t end_line = ((intptr_t)addr + word_size_in_bytes - 1)81& ~(ICache::line_size - 1);82(*_flush_icache_stub)((address)start_line, start_line == end_line ? 1 : 2, 0);83}8485void AbstractICache::invalidate_range(address start, int nbytes) {86static bool firstTime = true;87if (firstTime) {88guarantee(start == CAST_FROM_FN_PTR(address, _flush_icache_stub),89"first flush should be for flush stub");90firstTime = false;91return;92}93if (nbytes == 0) {94return;95}96// Align start address to an icache line boundary and transform97// nbytes to an icache line count.98const uint line_offset = mask_address_bits(start, ICache::line_size-1);99if (line_offset != 0) {100start -= line_offset;101nbytes += line_offset;102}103call_flush_stub(start, align_up(nbytes, (int)ICache::line_size) >>104ICache::log2_line_size);105}106107// For init.cpp108void icache_init() {109ICache::initialize();110}111112113