src/share/vm/runtime/icache.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/icache.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,106 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "memory/resourceArea.hpp"
    1.30 +#include "runtime/icache.hpp"
    1.31 +
    1.32 +// The flush stub function address
    1.33 +AbstractICache::flush_icache_stub_t AbstractICache::_flush_icache_stub = NULL;
    1.34 +
    1.35 +void AbstractICache::initialize() {
    1.36 +  // Making this stub must be FIRST use of assembler
    1.37 +  ResourceMark rm;
    1.38 +
    1.39 +  BufferBlob* b = BufferBlob::create("flush_icache_stub", ICache::stub_size);
    1.40 +  CodeBuffer c(b);
    1.41 +
    1.42 +  ICacheStubGenerator g(&c);
    1.43 +  g.generate_icache_flush(&_flush_icache_stub);
    1.44 +
    1.45 +  // The first use of flush_icache_stub must apply it to itself.
    1.46 +  // The StubCodeMark destructor in generate_icache_flush will
    1.47 +  // call Assembler::flush, which in turn will call invalidate_range,
    1.48 +  // which will in turn call the flush stub.  Thus we don't need an
    1.49 +  // explicit call to invalidate_range here.  This assumption is
    1.50 +  // checked in invalidate_range.
    1.51 +}
    1.52 +
    1.53 +void AbstractICache::call_flush_stub(address start, int lines) {
    1.54 +  // The business with the magic number is just a little security.
    1.55 +  // We cannot call the flush stub when generating the flush stub
    1.56 +  // because it isn't there yet.  So, the stub also returns its third
    1.57 +  // parameter.  This is a cheap check that the stub was really executed.
    1.58 +  static int magic = 0xbaadbabe;
    1.59 +
    1.60 +  int auto_magic = magic; // Make a local copy to avoid race condition
    1.61 +  int r = (*_flush_icache_stub)(start, lines, auto_magic);
    1.62 +  guarantee(r == auto_magic, "flush stub routine did not execute");
    1.63 +  ++magic;
    1.64 +}
    1.65 +
    1.66 +void AbstractICache::invalidate_word(address addr) {
    1.67 +  // Because this is called for instruction patching on the fly, long after
    1.68 +  // bootstrapping, we execute the stub directly.  Account for a 4-byte word
    1.69 +  // spanning two cache lines by computing a start line address by rounding
    1.70 +  // addr down to a line_size boundary, and an end line address by adding
    1.71 +  // the word size - 1 and rounding the result down to a line_size boundary.
    1.72 +  // If we just added word size, we'd mistakenly flush the next cache line
    1.73 +  // if the word to be flushed started in the last 4 bytes of the line.
    1.74 +  // Doing that would segv if the next line weren't mapped.
    1.75 +
    1.76 +  const int word_size_in_bytes = 4; // Always, regardless of platform
    1.77 +
    1.78 +  intptr_t start_line = ((intptr_t)addr + 0) & ~(ICache::line_size - 1);
    1.79 +  intptr_t end_line   = ((intptr_t)addr + word_size_in_bytes - 1)
    1.80 +                                             & ~(ICache::line_size - 1);
    1.81 +  (*_flush_icache_stub)((address)start_line, start_line == end_line ? 1 : 2, 0);
    1.82 +}
    1.83 +
    1.84 +void AbstractICache::invalidate_range(address start, int nbytes) {
    1.85 +  static bool firstTime = true;
    1.86 +  if (firstTime) {
    1.87 +    guarantee(start == CAST_FROM_FN_PTR(address, _flush_icache_stub),
    1.88 +              "first flush should be for flush stub");
    1.89 +    firstTime = false;
    1.90 +    return;
    1.91 +  }
    1.92 +  if (nbytes == 0) {
    1.93 +    return;
    1.94 +  }
    1.95 +  // Align start address to an icache line boundary and transform
    1.96 +  // nbytes to an icache line count.
    1.97 +  const uint line_offset = mask_address_bits(start, ICache::line_size-1);
    1.98 +  if (line_offset != 0) {
    1.99 +    start -= line_offset;
   1.100 +    nbytes += line_offset;
   1.101 +  }
   1.102 +  call_flush_stub(start, round_to(nbytes, ICache::line_size) >>
   1.103 +                         ICache::log2_line_size);
   1.104 +}
   1.105 +
   1.106 +// For init.cpp
   1.107 +void icache_init() {
   1.108 +  ICache::initialize();
   1.109 +}

mercurial