1 /* 2 * Copyright (c) 2012, 2019, 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 package org.graalvm.compiler.hotspot.amd64; 26 27 import static jdk.vm.ci.amd64.AMD64.r10; 28 import static jdk.vm.ci.amd64.AMD64.rax; 29 import static jdk.vm.ci.amd64.AMD64.rsp; 30 import static jdk.vm.ci.code.ValueUtil.asRegister; 31 import static org.graalvm.compiler.core.common.GraalOptions.CanOmitFrame; 32 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 33 import static org.graalvm.compiler.core.common.GraalOptions.ZapStackOnMethodEntry; 34 35 import jdk.internal.vm.compiler.collections.EconomicSet; 36 import org.graalvm.compiler.asm.Assembler; 37 import org.graalvm.compiler.asm.Label; 38 import org.graalvm.compiler.asm.amd64.AMD64Address; 39 import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag; 40 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler; 41 import org.graalvm.compiler.code.CompilationResult; 42 import org.graalvm.compiler.core.amd64.AMD64NodeMatchRules; 43 import org.graalvm.compiler.core.common.LIRKind; 44 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig; 45 import org.graalvm.compiler.core.gen.LIRGenerationProvider; 46 import org.graalvm.compiler.debug.DebugContext; 47 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 48 import org.graalvm.compiler.hotspot.HotSpotDataBuilder; 49 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; 50 import org.graalvm.compiler.hotspot.HotSpotHostBackend; 51 import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult; 52 import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction; 53 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider; 54 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 55 import org.graalvm.compiler.hotspot.stubs.Stub; 56 import org.graalvm.compiler.lir.LIR; 57 import org.graalvm.compiler.lir.amd64.AMD64Call; 58 import org.graalvm.compiler.lir.amd64.AMD64FrameMap; 59 import org.graalvm.compiler.lir.amd64.AMD64FrameMapBuilder; 60 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 61 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; 62 import org.graalvm.compiler.lir.asm.DataBuilder; 63 import org.graalvm.compiler.lir.asm.FrameContext; 64 import org.graalvm.compiler.lir.framemap.FrameMap; 65 import org.graalvm.compiler.lir.framemap.FrameMapBuilder; 66 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 67 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 68 import org.graalvm.compiler.nodes.StructuredGraph; 69 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 70 import org.graalvm.compiler.options.OptionValues; 71 72 import jdk.vm.ci.amd64.AMD64; 73 import jdk.vm.ci.amd64.AMD64Kind; 74 import jdk.vm.ci.code.CallingConvention; 75 import jdk.vm.ci.code.Register; 76 import jdk.vm.ci.code.RegisterConfig; 77 import jdk.vm.ci.code.StackSlot; 78 import jdk.vm.ci.hotspot.HotSpotCallingConventionType; 79 import jdk.vm.ci.hotspot.HotSpotSentinelConstant; 80 import jdk.vm.ci.meta.JavaKind; 81 import jdk.vm.ci.meta.JavaType; 82 import jdk.vm.ci.meta.ResolvedJavaMethod; 83 84 /** 85 * HotSpot AMD64 specific backend. 86 */ 87 public class AMD64HotSpotBackend extends HotSpotHostBackend implements LIRGenerationProvider { 88 89 public AMD64HotSpotBackend(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) { 90 super(config, runtime, providers); 91 } 92 93 @Override 94 protected FrameMapBuilder newFrameMapBuilder(RegisterConfig registerConfig) { 95 RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig; 96 FrameMap frameMap = new AMD64FrameMap(getCodeCache(), registerConfigNonNull, this); 97 return new AMD64FrameMapBuilder(frameMap, getCodeCache(), registerConfigNonNull); 98 } 99 100 @Override 101 public LIRGeneratorTool newLIRGenerator(LIRGenerationResult lirGenRes) { 102 return new AMD64HotSpotLIRGenerator(getProviders(), config, lirGenRes); 103 } 104 105 @Override 106 public NodeLIRBuilderTool newNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool lirGen) { 107 return new AMD64HotSpotNodeLIRBuilder(graph, lirGen, new AMD64NodeMatchRules(lirGen)); 108 } 109 110 @Override 111 protected void bangStackWithOffset(CompilationResultBuilder crb, int bangOffset) { 112 AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm; 113 int pos = asm.position(); 114 asm.movl(new AMD64Address(rsp, -bangOffset), AMD64.rax); 115 assert asm.position() - pos >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE; 116 } 117 118 /** 119 * The size of the instruction used to patch the verified entry point of an nmethod when the 120 * nmethod is made non-entrant or a zombie (e.g. during deopt or class unloading). The first 121 * instruction emitted at an nmethod's verified entry point must be at least this length to 122 * ensure mt-safe patching. 123 */ 124 public static final int PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE = 5; 125 126 /** 127 * Emits code at the verified entry point and return point(s) of a method. 128 */ 129 class HotSpotFrameContext implements FrameContext { 130 131 final boolean isStub; 132 final boolean omitFrame; 133 134 HotSpotFrameContext(boolean isStub, boolean omitFrame) { 135 this.isStub = isStub; 136 this.omitFrame = omitFrame; 137 } 138 139 @Override 140 public boolean hasFrame() { 141 return !omitFrame; 142 } 143 144 @Override 145 public void enter(CompilationResultBuilder crb) { 146 FrameMap frameMap = crb.frameMap; 147 int frameSize = frameMap.frameSize(); 148 AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm; 149 if (omitFrame) { 150 if (!isStub) { 151 asm.nop(PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE); 152 } 153 } else { 154 int verifiedEntryPointOffset = asm.position(); 155 if (!isStub) { 156 emitStackOverflowCheck(crb); 157 // assert asm.position() - verifiedEntryPointOffset >= 158 // PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE; 159 } 160 if (!isStub && asm.position() == verifiedEntryPointOffset) { 161 asm.subqWide(rsp, frameSize); 162 assert asm.position() - verifiedEntryPointOffset >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE; 163 } else { 164 asm.decrementq(rsp, frameSize); 165 } 166 if (ZapStackOnMethodEntry.getValue(crb.getOptions())) { 167 final int intSize = 4; 168 for (int i = 0; i < frameSize / intSize; ++i) { 169 asm.movl(new AMD64Address(rsp, i * intSize), 0xC1C1C1C1); 170 } 171 } 172 assert frameMap.getRegisterConfig().getCalleeSaveRegisters() == null; 173 } 174 } 175 176 @Override 177 public void leave(CompilationResultBuilder crb) { 178 if (!omitFrame) { 179 AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm; 180 assert crb.frameMap.getRegisterConfig().getCalleeSaveRegisters() == null; 181 182 int frameSize = crb.frameMap.frameSize(); 183 asm.incrementq(rsp, frameSize); 184 } 185 } 186 } 187 188 @Override 189 public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult lirGenRen, FrameMap frameMap, CompilationResult compilationResult, CompilationResultBuilderFactory factory) { 190 // Omit the frame if the method: 191 // - has no spill slots or other slots allocated during register allocation 192 // - has no callee-saved registers 193 // - has no incoming arguments passed on the stack 194 // - has no deoptimization points 195 // - makes no foreign calls (which require an aligned stack) 196 HotSpotLIRGenerationResult gen = (HotSpotLIRGenerationResult) lirGenRen; 197 LIR lir = gen.getLIR(); 198 assert gen.getDeoptimizationRescueSlot() == null || frameMap.frameNeedsAllocating() : "method that can deoptimize must have a frame"; 199 OptionValues options = lir.getOptions(); 200 DebugContext debug = lir.getDebug(); 201 boolean omitFrame = CanOmitFrame.getValue(options) && !frameMap.frameNeedsAllocating() && !lir.hasArgInCallerFrame() && !gen.hasForeignCall(); 202 203 Stub stub = gen.getStub(); 204 Assembler masm = new AMD64MacroAssembler(getTarget()); 205 HotSpotFrameContext frameContext = new HotSpotFrameContext(stub != null, omitFrame); 206 DataBuilder dataBuilder = new HotSpotDataBuilder(getCodeCache().getTarget()); 207 CompilationResultBuilder crb = factory.createBuilder(getCodeCache(), getForeignCalls(), frameMap, masm, dataBuilder, frameContext, options, debug, compilationResult, Register.None); 208 crb.setTotalFrameSize(frameMap.totalFrameSize()); 209 crb.setMaxInterpreterFrameSize(gen.getMaxInterpreterFrameSize()); 210 StackSlot deoptimizationRescueSlot = gen.getDeoptimizationRescueSlot(); 211 if (deoptimizationRescueSlot != null && stub == null) { 212 crb.compilationResult.setCustomStackAreaOffset(deoptimizationRescueSlot); 213 } 214 215 if (stub != null) { 216 updateStub(stub, gen, frameMap); 217 } 218 219 return crb; 220 } 221 222 @Override 223 public void emitCode(CompilationResultBuilder crb, LIR lir, ResolvedJavaMethod installedCodeOwner) { 224 AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm; 225 FrameMap frameMap = crb.frameMap; 226 RegisterConfig regConfig = frameMap.getRegisterConfig(); 227 Label verifiedEntry = new Label(); 228 229 // Emit the prefix 230 emitCodePrefix(installedCodeOwner, crb, asm, regConfig, verifiedEntry); 231 232 // Emit code for the LIR 233 emitCodeBody(installedCodeOwner, crb, lir); 234 235 // Emit the suffix 236 emitCodeSuffix(installedCodeOwner, crb, asm, frameMap); 237 238 // Profile assembler instructions 239 profileInstructions(lir, crb); 240 } 241 242 /** 243 * Emits the code prior to the verified entry point. 244 * 245 * @param installedCodeOwner see {@link LIRGenerationProvider#emitCode} 246 */ 247 public void emitCodePrefix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, RegisterConfig regConfig, Label verifiedEntry) { 248 HotSpotProviders providers = getProviders(); 249 if (installedCodeOwner != null && !installedCodeOwner.isStatic()) { 250 crb.recordMark(config.MARKID_UNVERIFIED_ENTRY); 251 CallingConvention cc = regConfig.getCallingConvention(HotSpotCallingConventionType.JavaCallee, null, new JavaType[]{providers.getMetaAccess().lookupJavaType(Object.class)}, this); 252 Register inlineCacheKlass = rax; // see definition of IC_Klass in 253 // c1_LIRAssembler_x86.cpp 254 Register receiver = asRegister(cc.getArgument(0)); 255 AMD64Address src = new AMD64Address(receiver, config.hubOffset); 256 257 if (config.useCompressedClassPointers) { 258 Register register = r10; 259 Register heapBase = providers.getRegisters().getHeapBaseRegister(); 260 AMD64HotSpotMove.decodeKlassPointer(crb, asm, register, heapBase, src, config); 261 if (GeneratePIC.getValue(crb.getOptions())) { 262 asm.movq(heapBase, asm.getPlaceholder(-1)); 263 crb.recordMark(config.MARKID_NARROW_OOP_BASE_ADDRESS); 264 } else { 265 if (config.narrowKlassBase != 0) { 266 // The heap base register was destroyed above, so restore it 267 if (config.narrowOopBase == 0L) { 268 asm.xorq(heapBase, heapBase); 269 } else { 270 asm.movq(heapBase, config.narrowOopBase); 271 } 272 } 273 } 274 asm.cmpq(inlineCacheKlass, register); 275 } else { 276 asm.cmpq(inlineCacheKlass, src); 277 } 278 AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(IC_MISS_HANDLER), ConditionFlag.NotEqual); 279 } 280 281 asm.align(config.codeEntryAlignment); 282 crb.recordMark(config.MARKID_OSR_ENTRY); 283 asm.bind(verifiedEntry); 284 crb.recordMark(config.MARKID_VERIFIED_ENTRY); 285 286 if (GeneratePIC.getValue(crb.getOptions())) { 287 // Check for method state 288 HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext; 289 if (!frameContext.isStub) { 290 crb.recordInlineDataInCodeWithNote(new HotSpotSentinelConstant(LIRKind.value(AMD64Kind.QWORD), JavaKind.Long), HotSpotConstantLoadAction.MAKE_NOT_ENTRANT); 291 asm.movq(AMD64.rax, asm.getPlaceholder(-1)); 292 asm.testq(AMD64.rax, AMD64.rax); 293 AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(WRONG_METHOD_HANDLER), ConditionFlag.NotZero); 294 } 295 } 296 } 297 298 /** 299 * Emits the code which starts at the verified entry point. 300 * 301 * @param installedCodeOwner see {@link LIRGenerationProvider#emitCode} 302 */ 303 public void emitCodeBody(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, LIR lir) { 304 crb.emit(lir); 305 } 306 307 /** 308 * @param installedCodeOwner see {@link LIRGenerationProvider#emitCode} 309 */ 310 public void emitCodeSuffix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, FrameMap frameMap) { 311 HotSpotProviders providers = getProviders(); 312 HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext; 313 if (!frameContext.isStub) { 314 HotSpotForeignCallsProvider foreignCalls = providers.getForeignCalls(); 315 crb.recordMark(config.MARKID_EXCEPTION_HANDLER_ENTRY); 316 AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(EXCEPTION_HANDLER), null, false, null); 317 crb.recordMark(config.MARKID_DEOPT_HANDLER_ENTRY); 318 AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(DEOPTIMIZATION_HANDLER), null, false, null); 319 } else { 320 // No need to emit the stubs for entries back into the method since 321 // it has no calls that can cause such "return" entries 322 323 if (frameContext.omitFrame) { 324 // Cannot access slots in caller's frame if my frame is omitted 325 assert !frameMap.accessesCallerFrame(); 326 } 327 } 328 } 329 330 @Override 331 public RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig, String[] allocationRestrictedTo) { 332 RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig; 333 return new AMD64HotSpotRegisterAllocationConfig(registerConfigNonNull, allocationRestrictedTo); 334 } 335 336 @Override 337 public EconomicSet<Register> translateToCallerRegisters(EconomicSet<Register> calleeRegisters) { 338 return calleeRegisters; 339 } 340 }