src/hotspot/share/runtime/icache.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46625 hotspot/src/share/vm/runtime/icache.cpp@edefffab74e2
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
6418
6671edbd230e 6978355: renaming for 6961697
twisti
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6418
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6418
diff changeset
    26
#include "memory/resourceArea.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6418
diff changeset
    27
#include "runtime/icache.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46620
diff changeset
    28
#include "utilities/align.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// The flush stub function address
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
AbstractICache::flush_icache_stub_t AbstractICache::_flush_icache_stub = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
void AbstractICache::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  // Making this stub must be FIRST use of assembler
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  BufferBlob* b = BufferBlob::create("flush_icache_stub", ICache::stub_size);
25495
aeb87692dfd0 8022968: Some codecache allocation failures don't result in invoking the sweeper
thartmann
parents: 7397
diff changeset
    38
  if (b == NULL) {
aeb87692dfd0 8022968: Some codecache allocation failures don't result in invoking the sweeper
thartmann
parents: 7397
diff changeset
    39
    vm_exit_out_of_memory(ICache::stub_size, OOM_MALLOC_ERROR, "CodeCache: no space for flush_icache_stub");
aeb87692dfd0 8022968: Some codecache allocation failures don't result in invoking the sweeper
thartmann
parents: 7397
diff changeset
    40
  }
6418
6671edbd230e 6978355: renaming for 6961697
twisti
parents: 5547
diff changeset
    41
  CodeBuffer c(b);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  ICacheStubGenerator g(&c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  g.generate_icache_flush(&_flush_icache_stub);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // The first use of flush_icache_stub must apply it to itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  // The StubCodeMark destructor in generate_icache_flush will
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  // call Assembler::flush, which in turn will call invalidate_range,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  // which will in turn call the flush stub.  Thus we don't need an
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  // explicit call to invalidate_range here.  This assumption is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  // checked in invalidate_range.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
void AbstractICache::call_flush_stub(address start, int lines) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // The business with the magic number is just a little security.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // We cannot call the flush stub when generating the flush stub
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // because it isn't there yet.  So, the stub also returns its third
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // parameter.  This is a cheap check that the stub was really executed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  static int magic = 0xbaadbabe;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  int auto_magic = magic; // Make a local copy to avoid race condition
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  int r = (*_flush_icache_stub)(start, lines, auto_magic);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  guarantee(r == auto_magic, "flush stub routine did not execute");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  ++magic;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
void AbstractICache::invalidate_word(address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  // Because this is called for instruction patching on the fly, long after
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  // bootstrapping, we execute the stub directly.  Account for a 4-byte word
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  // spanning two cache lines by computing a start line address by rounding
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // addr down to a line_size boundary, and an end line address by adding
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  // the word size - 1 and rounding the result down to a line_size boundary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // If we just added word size, we'd mistakenly flush the next cache line
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // if the word to be flushed started in the last 4 bytes of the line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  // Doing that would segv if the next line weren't mapped.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  const int word_size_in_bytes = 4; // Always, regardless of platform
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  intptr_t start_line = ((intptr_t)addr + 0) & ~(ICache::line_size - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  intptr_t end_line   = ((intptr_t)addr + word_size_in_bytes - 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                                             & ~(ICache::line_size - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  (*_flush_icache_stub)((address)start_line, start_line == end_line ? 1 : 2, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
void AbstractICache::invalidate_range(address start, int nbytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  static bool firstTime = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  if (firstTime) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    guarantee(start == CAST_FROM_FN_PTR(address, _flush_icache_stub),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
              "first flush should be for flush stub");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    firstTime = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  if (nbytes == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // Align start address to an icache line boundary and transform
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // nbytes to an icache line count.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  const uint line_offset = mask_address_bits(start, ICache::line_size-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  if (line_offset != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    start -= line_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    nbytes += line_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  }
46620
750c6edff33b 8178500: Replace usages of round_to and round_down with align_up and align_down
stefank
parents: 25495
diff changeset
   103
  call_flush_stub(start, align_up(nbytes, (int)ICache::line_size) >>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
                         ICache::log2_line_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
// For init.cpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
void icache_init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  ICache::initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
}