|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include "precompiled.hpp" |
| 26 | +#include "asm/macroAssembler.hpp" |
| 27 | +#include "c2_intelJccErratum_x86.hpp" |
| 28 | +#include "opto/cfgnode.hpp" |
| 29 | +#include "opto/compile.hpp" |
| 30 | +#include "opto/machnode.hpp" |
| 31 | +#include "opto/node.hpp" |
| 32 | +#include "opto/regalloc.hpp" |
| 33 | +#include "utilities/align.hpp" |
| 34 | +#include "utilities/debug.hpp" |
| 35 | + |
| 36 | +// Compute which 32 byte boundary an address corresponds to |
| 37 | +uintptr_t IntelJccErratum::boundary(uintptr_t addr) { |
| 38 | + return addr >> 5; |
| 39 | +} |
| 40 | + |
| 41 | +bool IntelJccErratum::is_crossing_or_ending_at_32_byte_boundary(uintptr_t start_pc, uintptr_t end_pc) { |
| 42 | + int jcc_size = int(end_pc - start_pc); |
| 43 | + assert(jcc_size <= largest_jcc_size(), "invalid jcc size: %d", jcc_size); |
| 44 | + return boundary(start_pc) != boundary(end_pc); |
| 45 | +} |
| 46 | + |
| 47 | +bool IntelJccErratum::is_jcc_erratum_branch(const Block* block, const MachNode* node, uint node_index) { |
| 48 | + if (node->is_MachCall() && !node->is_MachCallJava()) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + return node_index == (block->number_of_nodes() - 1); |
| 52 | +} |
| 53 | + |
| 54 | +int IntelJccErratum::jcc_erratum_taint_node(MachNode* node, PhaseRegAlloc* regalloc) { |
| 55 | + node->add_flag(Node::Flag_intel_jcc_erratum); |
| 56 | + return node->size(regalloc); |
| 57 | +} |
| 58 | + |
| 59 | +int IntelJccErratum::tag_affected_machnodes(Compile* C, PhaseCFG* cfg, PhaseRegAlloc* regalloc) { |
| 60 | + ResourceMark rm; |
| 61 | + int nop_size = 0; |
| 62 | + MachNode* last_m = NULL; |
| 63 | + |
| 64 | + for (uint i = 0; i < cfg->number_of_blocks(); ++i) { |
| 65 | + const Block* const block = cfg->get_block(i); |
| 66 | + for (uint j = 0; j < block->number_of_nodes(); ++j) { |
| 67 | + const Node* const node = block->get_node(j); |
| 68 | + if (!node->is_Mach()) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + MachNode* m = node->as_Mach(); |
| 72 | + if (is_jcc_erratum_branch(block, m, j)) { |
| 73 | + // Found a root jcc erratum branch, flag it as problematic |
| 74 | + nop_size += jcc_erratum_taint_node(m, regalloc); |
| 75 | + |
| 76 | + if (!m->is_MachReturn() && !m->is_MachCall()) { |
| 77 | + // We might fuse a problematic jcc erratum branch with a preceding |
| 78 | + // ALU instruction - we must catch such problematic macro fusions |
| 79 | + // and flag the ALU instruction as problematic too. |
| 80 | + for (uint k = 1; k < m->req(); ++k) { |
| 81 | + const Node* const use = m->in(k); |
| 82 | + if (use == last_m && !m->is_MachReturn()) { |
| 83 | + // Flag fused conditions too |
| 84 | + nop_size += jcc_erratum_taint_node(last_m, regalloc); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + last_m = NULL; |
| 89 | + } else { |
| 90 | + last_m = m; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return nop_size; |
| 95 | +} |
| 96 | + |
| 97 | +int IntelJccErratum::compute_padding(uintptr_t current_offset, const MachNode* mach, Block* block, uint index_in_block, PhaseRegAlloc* regalloc) { |
| 98 | + int jcc_size = mach->size(regalloc); |
| 99 | + if (index_in_block < block->number_of_nodes() - 1) { |
| 100 | + Node* next = block->get_node(index_in_block + 1); |
| 101 | + if (next->is_Mach() && (next->as_Mach()->flags() & Node::Flag_intel_jcc_erratum)) { |
| 102 | + jcc_size += mach->size(regalloc); |
| 103 | + } |
| 104 | + } |
| 105 | + if (jcc_size > largest_jcc_size()) { |
| 106 | + // Let's not try fixing this for nodes that seem unreasonably large |
| 107 | + return false; |
| 108 | + } |
| 109 | + if (is_crossing_or_ending_at_32_byte_boundary(current_offset, current_offset + jcc_size)) { |
| 110 | + return int(align_up(current_offset, 32) - current_offset); |
| 111 | + } else { |
| 112 | + return 0; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#define __ _masm. |
| 117 | + |
| 118 | +uintptr_t IntelJccErratumAlignment::pc() { |
| 119 | + return (uintptr_t)__ pc(); |
| 120 | +} |
| 121 | + |
| 122 | +IntelJccErratumAlignment::IntelJccErratumAlignment(MacroAssembler& masm, int jcc_size) : |
| 123 | + _masm(masm), |
| 124 | + _start_pc(pc()) { |
| 125 | + if (!VM_Version::has_intel_jcc_erratum()) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + if (Compile::current()->in_scratch_emit_size()) { |
| 130 | + // When we measure the size of this 32 byte alignment, we apply a conservative guess. |
| 131 | + __ nop(jcc_size); |
| 132 | + } else if (IntelJccErratum::is_crossing_or_ending_at_32_byte_boundary(_start_pc, _start_pc + jcc_size)) { |
| 133 | + // The affected branch might get slowed down by micro code mitigations |
| 134 | + // as it could be susceptible to the erratum. Place nops until the next |
| 135 | + // 32 byte boundary to make sure the branch will be cached. |
| 136 | + const int alignment_nops = (int)(align_up(_start_pc, 32) - _start_pc); |
| 137 | + __ nop(alignment_nops); |
| 138 | + _start_pc = pc(); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +IntelJccErratumAlignment::~IntelJccErratumAlignment() { |
| 143 | + if (!VM_Version::has_intel_jcc_erratum() || |
| 144 | + Compile::current()->in_scratch_emit_size()) { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + assert(!IntelJccErratum::is_crossing_or_ending_at_32_byte_boundary(_start_pc, pc()), "Invalid jcc_size estimate"); |
| 149 | +} |
0 commit comments