1 /*
   2  * Copyright (c) 2011, 2017, 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.core.common.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     private final Register thread;
  62 
  63     public AMD64HotSpotSafepointOp(LIRFrameState state, GraalHotSpotVMConfig config, NodeLIRBuilderTool tool, Register thread) {
  64         super(TYPE);
  65         this.state = state;
  66         this.config = config;
  67         this.thread = thread;
  68         if (config.threadLocalHandshakes || isPollingPageFar(config) || ImmutableCode.getValue(tool.getOptions())) {
  69             temp = tool.getLIRGeneratorTool().newVariable(LIRKind.value(tool.getLIRGeneratorTool().target().arch.getWordKind()));
  70         } else {
  71             // Don't waste a register if it's unneeded
  72             temp = Value.ILLEGAL;
  73         }
  74     }
  75 
  76     @Override
  77     public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm) {
  78         emitCode(crb, asm, config, false, state, thread, temp instanceof RegisterValue ? ((RegisterValue) temp).getRegister() : null);
  79     }
  80 
  81     public static void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm, GraalHotSpotVMConfig config, boolean atReturn, LIRFrameState state, Register thread, Register scratch) {
  82         if (config.threadLocalHandshakes) {
  83             emitThreadLocalPoll(crb, asm, config, atReturn, state, thread, scratch);
  84         } else {
  85             emitGlobalPoll(crb, asm, config, atReturn, state, scratch);
  86         }
  87     }
  88 
  89     /**
  90      * Tests if the polling page address can be reached from the code cache with 32-bit
  91      * displacements.
  92      */
  93     private static boolean isPollingPageFar(GraalHotSpotVMConfig config) {
  94         final long pollingPageAddress = config.safepointPollingAddress;
  95         return config.forceUnreachable || !isInt(pollingPageAddress - config.codeCacheLowBound) || !isInt(pollingPageAddress - config.codeCacheHighBound);
  96     }
  97 
  98     private static void emitGlobalPoll(CompilationResultBuilder crb, AMD64MacroAssembler asm, GraalHotSpotVMConfig config, boolean atReturn, LIRFrameState state, Register scratch) {
  99         assert !atReturn || state == null : "state is unneeded at return";
 100         if (ImmutableCode.getValue(crb.getOptions())) {
 101             JavaKind hostWordKind = JavaKind.Long;
 102             int alignment = hostWordKind.getBitCount() / Byte.SIZE;
 103             JavaConstant pollingPageAddress = JavaConstant.forIntegerKind(hostWordKind, config.safepointPollingAddress);
 104             // This move will be patched to load the safepoint page from a data segment
 105             // co-located with the immutable code.
 106             if (GeneratePIC.getValue(crb.getOptions())) {
 107                 asm.movq(scratch, asm.getPlaceholder(-1));
 108             } else {
 109                 asm.movq(scratch, (AMD64Address) crb.recordDataReferenceInCode(pollingPageAddress, alignment));
 110             }
 111             final int pos = asm.position();
 112             crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
 113             if (state != null) {
 114                 crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
 115             }
 116             asm.testl(rax, new AMD64Address(scratch));
 117         } else if (isPollingPageFar(config)) {
 118             asm.movq(scratch, config.safepointPollingAddress);
 119             crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
 120             final int pos = asm.position();
 121             if (state != null) {
 122                 crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
 123             }
 124             asm.testl(rax, new AMD64Address(scratch));
 125         } else {
 126             crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_NEAR : config.MARKID_POLL_NEAR);
 127             final int pos = asm.position();
 128             if (state != null) {
 129                 crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
 130             }
 131             // The C++ code transforms the polling page offset into an RIP displacement
 132             // to the real address at that offset in the polling page.
 133             asm.testl(rax, new AMD64Address(rip, 0));
 134         }
 135     }
 136 
 137     private static void emitThreadLocalPoll(CompilationResultBuilder crb, AMD64MacroAssembler asm, GraalHotSpotVMConfig config, boolean atReturn, LIRFrameState state, Register thread,
 138                     Register scratch) {
 139         assert !atReturn || state == null : "state is unneeded at return";
 140 
 141         assert config.threadPollingPageOffset >= 0;
 142         asm.movptr(scratch, new AMD64Address(thread, config.threadPollingPageOffset));
 143         crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
 144         final int pos = asm.position();
 145         if (state != null) {
 146             crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
 147         }
 148         asm.testl(rax, new AMD64Address(scratch));
 149     }
 150 }