001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.amd64;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.hotspot.*;
027import jdk.internal.jvmci.meta.*;
028import static com.oracle.graal.asm.NumUtil.*;
029import static com.oracle.graal.compiler.common.GraalOptions.*;
030import static jdk.internal.jvmci.amd64.AMD64.*;
031
032import com.oracle.graal.asm.amd64.*;
033import com.oracle.graal.lir.*;
034import com.oracle.graal.lir.amd64.*;
035import com.oracle.graal.lir.asm.*;
036import com.oracle.graal.nodes.spi.*;
037
038/**
039 * Emits a safepoint poll.
040 */
041@Opcode("SAFEPOINT")
042public final class AMD64HotSpotSafepointOp extends AMD64LIRInstruction {
043    public static final LIRInstructionClass<AMD64HotSpotSafepointOp> TYPE = LIRInstructionClass.create(AMD64HotSpotSafepointOp.class);
044
045    @State protected LIRFrameState state;
046    @Temp({OperandFlag.REG, OperandFlag.ILLEGAL}) private AllocatableValue temp;
047
048    private final HotSpotVMConfig config;
049
050    public AMD64HotSpotSafepointOp(LIRFrameState state, HotSpotVMConfig config, NodeLIRBuilderTool tool) {
051        super(TYPE);
052        this.state = state;
053        this.config = config;
054        if (isPollingPageFar(config) || ImmutableCode.getValue()) {
055            temp = tool.getLIRGeneratorTool().newVariable(LIRKind.value(tool.getLIRGeneratorTool().target().wordKind));
056        } else {
057            // Don't waste a register if it's unneeded
058            temp = Value.ILLEGAL;
059        }
060    }
061
062    @Override
063    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm) {
064        emitCode(crb, asm, config, false, state, temp instanceof RegisterValue ? ((RegisterValue) temp).getRegister() : null);
065    }
066
067    /**
068     * Tests if the polling page address can be reached from the code cache with 32-bit
069     * displacements.
070     */
071    private static boolean isPollingPageFar(HotSpotVMConfig config) {
072        final long pollingPageAddress = config.safepointPollingAddress;
073        return config.forceUnreachable || !isInt(pollingPageAddress - config.codeCacheLowBoundary()) || !isInt(pollingPageAddress - config.codeCacheHighBoundary());
074    }
075
076    public static void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm, HotSpotVMConfig config, boolean atReturn, LIRFrameState state, Register scratch) {
077        assert !atReturn || state == null : "state is unneeded at return";
078        if (ImmutableCode.getValue()) {
079            Kind hostWordKind = Kind.Long;
080            int alignment = hostWordKind.getBitCount() / Byte.SIZE;
081            JavaConstant pollingPageAddress = JavaConstant.forIntegerKind(hostWordKind, config.safepointPollingAddress);
082            // This move will be patched to load the safepoint page from a data segment
083            // co-located with the immutable code.
084            asm.movq(scratch, (AMD64Address) crb.recordDataReferenceInCode(pollingPageAddress, alignment));
085            final int pos = asm.position();
086            crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
087            if (state != null) {
088                crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
089            }
090            asm.testl(rax, new AMD64Address(scratch));
091        } else if (isPollingPageFar(config)) {
092            asm.movq(scratch, config.safepointPollingAddress);
093            crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
094            final int pos = asm.position();
095            if (state != null) {
096                crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
097            }
098            asm.testl(rax, new AMD64Address(scratch));
099        } else {
100            crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_NEAR : config.MARKID_POLL_NEAR);
101            final int pos = asm.position();
102            if (state != null) {
103                crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
104            }
105            // The C++ code transforms the polling page offset into an RIP displacement
106            // to the real address at that offset in the polling page.
107            asm.testl(rax, new AMD64Address(rip, 0));
108        }
109    }
110}