icache.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_icache.cpp.incl"
27
28// The flush stub function address
29AbstractICache::flush_icache_stub_t AbstractICache::_flush_icache_stub = NULL;
30
31void AbstractICache::initialize() {
32  // Making this stub must be FIRST use of assembler
33  ResourceMark rm;
34
35  BufferBlob* b = BufferBlob::create("flush_icache_stub", ICache::stub_size);
36  CodeBuffer c(b->instructions_begin(), b->instructions_size());
37
38  ICacheStubGenerator g(&c);
39  g.generate_icache_flush(&_flush_icache_stub);
40
41  // The first use of flush_icache_stub must apply it to itself.
42  // The StubCodeMark destructor in generate_icache_flush will
43  // call Assembler::flush, which in turn will call invalidate_range,
44  // which will in turn call the flush stub.  Thus we don't need an
45  // explicit call to invalidate_range here.  This assumption is
46  // checked in invalidate_range.
47}
48
49void AbstractICache::call_flush_stub(address start, int lines) {
50  // The business with the magic number is just a little security.
51  // We cannot call the flush stub when generating the flush stub
52  // because it isn't there yet.  So, the stub also returns its third
53  // parameter.  This is a cheap check that the stub was really executed.
54  static int magic = 0xbaadbabe;
55
56  int auto_magic = magic; // Make a local copy to avoid race condition
57  int r = (*_flush_icache_stub)(start, lines, auto_magic);
58  guarantee(r == auto_magic, "flush stub routine did not execute");
59  ++magic;
60}
61
62void AbstractICache::invalidate_word(address addr) {
63  // Because this is called for instruction patching on the fly, long after
64  // bootstrapping, we execute the stub directly.  Account for a 4-byte word
65  // spanning two cache lines by computing a start line address by rounding
66  // addr down to a line_size boundary, and an end line address by adding
67  // the word size - 1 and rounding the result down to a line_size boundary.
68  // If we just added word size, we'd mistakenly flush the next cache line
69  // if the word to be flushed started in the last 4 bytes of the line.
70  // Doing that would segv if the next line weren't mapped.
71
72  const int word_size_in_bytes = 4; // Always, regardless of platform
73
74  intptr_t start_line = ((intptr_t)addr + 0) & ~(ICache::line_size - 1);
75  intptr_t end_line   = ((intptr_t)addr + word_size_in_bytes - 1)
76                                             & ~(ICache::line_size - 1);
77  (*_flush_icache_stub)((address)start_line, start_line == end_line ? 1 : 2, 0);
78}
79
80void AbstractICache::invalidate_range(address start, int nbytes) {
81  static bool firstTime = true;
82  if (firstTime) {
83    guarantee(start == CAST_FROM_FN_PTR(address, _flush_icache_stub),
84              "first flush should be for flush stub");
85    firstTime = false;
86    return;
87  }
88  if (nbytes == 0) {
89    return;
90  }
91  // Align start address to an icache line boundary and transform
92  // nbytes to an icache line count.
93  const uint line_offset = mask_address_bits(start, ICache::line_size-1);
94  if (line_offset != 0) {
95    start -= line_offset;
96    nbytes += line_offset;
97  }
98  call_flush_stub(start, round_to(nbytes, ICache::line_size) >>
99                         ICache::log2_line_size);
100}
101
102// For init.cpp
103void icache_init() {
104  ICache::initialize();
105}
106