1 /* 2 * Copyright (c) 2011, 2016, 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 package org.graalvm.compiler.hotspot.amd64; 24 25 import static org.graalvm.compiler.asm.NumUtil.isInt; 26 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 27 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; 28 import static jdk.vm.ci.amd64.AMD64.rax; 29 import static jdk.vm.ci.amd64.AMD64.rip; 30 31 import org.graalvm.compiler.asm.amd64.AMD64Address; 32 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler; 33 import org.graalvm.compiler.core.common.LIRKind; 34 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 35 import org.graalvm.compiler.lir.LIRFrameState; 36 import org.graalvm.compiler.lir.LIRInstructionClass; 37 import org.graalvm.compiler.lir.Opcode; 38 import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction; 39 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 40 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 41 42 import jdk.vm.ci.code.Register; 43 import jdk.vm.ci.code.RegisterValue; 44 import jdk.vm.ci.code.site.InfopointReason; 45 import jdk.vm.ci.meta.AllocatableValue; 46 import jdk.vm.ci.meta.JavaConstant; 47 import jdk.vm.ci.meta.JavaKind; 48 import jdk.vm.ci.meta.Value; 49 50 /** 51 * Emits a safepoint poll. 52 */ 53 @Opcode("SAFEPOINT") 54 public final class AMD64HotSpotSafepointOp extends AMD64LIRInstruction { 55 public static final LIRInstructionClass<AMD64HotSpotSafepointOp> TYPE = LIRInstructionClass.create(AMD64HotSpotSafepointOp.class); 56 57 @State protected LIRFrameState state; 58 @Temp({OperandFlag.REG, OperandFlag.ILLEGAL}) private AllocatableValue temp; 59 60 private final GraalHotSpotVMConfig config; 61 62 public AMD64HotSpotSafepointOp(LIRFrameState state, GraalHotSpotVMConfig config, NodeLIRBuilderTool tool) { 63 super(TYPE); 64 this.state = state; 65 this.config = config; 66 if (isPollingPageFar(config) || ImmutableCode.getValue()) { 67 temp = tool.getLIRGeneratorTool().newVariable(LIRKind.value(tool.getLIRGeneratorTool().target().arch.getWordKind())); 68 } else { 69 // Don't waste a register if it's unneeded 70 temp = Value.ILLEGAL; 71 } 72 } 73 74 @Override 75 public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm) { 76 emitCode(crb, asm, config, false, state, temp instanceof RegisterValue ? ((RegisterValue) temp).getRegister() : null); 77 } 78 79 /** 80 * Tests if the polling page address can be reached from the code cache with 32-bit 81 * displacements. 82 */ 83 private static boolean isPollingPageFar(GraalHotSpotVMConfig config) { 84 final long pollingPageAddress = config.safepointPollingAddress; 85 return config.forceUnreachable || !isInt(pollingPageAddress - config.codeCacheLowBound) || !isInt(pollingPageAddress - config.codeCacheHighBound); 86 } 87 88 public static void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm, GraalHotSpotVMConfig config, boolean atReturn, LIRFrameState state, Register scratch) { 89 assert !atReturn || state == null : "state is unneeded at return"; 90 if (ImmutableCode.getValue()) { 91 JavaKind hostWordKind = JavaKind.Long; 92 int alignment = hostWordKind.getBitCount() / Byte.SIZE; 93 JavaConstant pollingPageAddress = JavaConstant.forIntegerKind(hostWordKind, config.safepointPollingAddress); 94 // This move will be patched to load the safepoint page from a data segment 95 // co-located with the immutable code. 96 if (GeneratePIC.getValue()) { 97 asm.movq(scratch, asm.getPlaceholder(-1)); 98 } else { 99 asm.movq(scratch, (AMD64Address) crb.recordDataReferenceInCode(pollingPageAddress, alignment)); 100 } 101 final int pos = asm.position(); 102 crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR); 103 if (state != null) { 104 crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT); 105 } 106 asm.testl(rax, new AMD64Address(scratch)); 107 } else if (isPollingPageFar(config)) { 108 asm.movq(scratch, config.safepointPollingAddress); 109 crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR); 110 final int pos = asm.position(); 111 if (state != null) { 112 crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT); 113 } 114 asm.testl(rax, new AMD64Address(scratch)); 115 } else { 116 crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_NEAR : config.MARKID_POLL_NEAR); 117 final int pos = asm.position(); 118 if (state != null) { 119 crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT); 120 } 121 // The C++ code transforms the polling page offset into an RIP displacement 122 // to the real address at that offset in the polling page. 123 asm.testl(rax, new AMD64Address(rip, 0)); 124 } 125 } 126 }