Skip to content

Commit

Permalink
[GR-51891] CInterfaceWrapper Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Arash Kazemi Koohbanani committed Mar 13, 2024
1 parent ce778a5 commit a6df9b1
Show file tree
Hide file tree
Showing 12 changed files with 493 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5254,6 +5254,18 @@ public void clflush(AMD64Address adr) {
emitOperandHelper(7, adr, 0);
}

public void wrpkru() {
emitByte(0x0F);
emitByte(0x01);
emitByte(0xEF);
}

public void rdpkru() {
emitByte(0x0F);
emitByte(0x01);
emitByte(0xEE);
}

public final void vpaddd(Register dst, Register nds, Register src, AVXKind.AVXSize size) {
VexRVMOp.VPADDD.emit(this, size, dst, nds, src);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,20 @@ public void emitSpeculationFence() {
append(new AMD64LFenceOp());
}

@Override
public void emitProtectionKeyRegisterWrite(Value value) {
RegisterValue rax = AMD64.rax.asValue(value.getValueKind());
emitMove(rax, value);
append(new AMD64WriteDataToUserPageKeyRegister(rax));
}

@Override
public Value emitProtectionKeyRegisterRead() {
AMD64ReadDataFromUserPageKeyRegister rdpkru = new AMD64ReadDataFromUserPageKeyRegister();
append(rdpkru);
return emitReadRegister(AMD64.rax, rdpkru.retVal.getValueKind());
}

@Override
public void emitZeroMemory(Value address, Value length, boolean isAligned) {
RegisterValue lengthReg = AMD64.rcx.asValue(length.getValueKind());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.core.amd64;

import jdk.graal.compiler.asm.amd64.AMD64MacroAssembler;
import jdk.graal.compiler.core.common.LIRKind;
import jdk.graal.compiler.lir.LIRInstructionClass;
import jdk.graal.compiler.lir.Opcode;
import jdk.graal.compiler.lir.amd64.AMD64LIRInstruction;
import jdk.graal.compiler.lir.asm.CompilationResultBuilder;
import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.amd64.AMD64Kind;
import jdk.vm.ci.code.ValueUtil;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.Value;

import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;

/**
* Reads the value of PKRU into EAX and clears EDX. ECX must be 0 when RDPKRU is executed;
* otherwise, a general-protection exception (#GP) occurs. RDPKRU can be executed only if CR4.PKE =
* 1; otherwise, an invalid-opcode exception (#UD) occurs. Software can discover the value of
* CR4.PKE by examining CPUID.(EAX=07H,ECX=0H):ECX.OSPKE [bit 4]. On processors that support the
* Intel 64 Architecture, the high-order 32-bits of RCX are ignored and the high-order 32-bits of
* RDX and RAX are cleared.
*/
@Opcode("RDPKRU")
public class AMD64ReadDataFromUserPageKeyRegister extends AMD64LIRInstruction {
public static final LIRInstructionClass<AMD64ReadDataFromUserPageKeyRegister> TYPE = LIRInstructionClass.create(AMD64ReadDataFromUserPageKeyRegister.class);

// the result of the rdpkru is in eax
@Def protected Value retVal;

// edx will be cleared
@Temp({REG}) protected AllocatableValue edx;

// ecx must be zero
@Temp({REG}) protected AllocatableValue zeroArg1;

public AMD64ReadDataFromUserPageKeyRegister() {
super(TYPE);
this.retVal = AMD64.rax.asValue(LIRKind.value(AMD64Kind.DWORD));
this.edx = AMD64.rdx.asValue(LIRKind.value(AMD64Kind.DWORD));
this.zeroArg1 = AMD64.rcx.asValue(LIRKind.value(AMD64Kind.DWORD));
}

@Override
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
masm.xorl(ValueUtil.asRegister(zeroArg1), ValueUtil.asRegister(zeroArg1));
masm.rdpkru();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.core.amd64;

import jdk.graal.compiler.asm.amd64.AMD64MacroAssembler;
import jdk.graal.compiler.core.common.LIRKind;
import jdk.graal.compiler.lir.LIRInstructionClass;
import jdk.graal.compiler.lir.Opcode;
import jdk.graal.compiler.lir.amd64.AMD64LIRInstruction;
import jdk.graal.compiler.lir.asm.CompilationResultBuilder;
import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.amd64.AMD64Kind;
import jdk.vm.ci.code.RegisterValue;
import jdk.vm.ci.code.ValueUtil;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.Value;

import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;

/**
* Writes the value of EAX into PKRU. ECX and EDX must be 0 when WRPKRU is executed; otherwise, a
* general protection exception (#GP) occurs. WRPKRU can be executed only if CR4.PKE = 1; otherwise,
* an invalid-opcode exception (#UD) occurs. Software can discover the value of CR4.PKE by examining
* CPUID.(EAX=07H,ECX=0H):ECX.OSPKE [bit 4]. On processors that support the Intel 64 Architecture,
* the high-order 32-bits of RCX, RDX and RAX are ignored.
*/
@Opcode("WRPKRU")
public class AMD64WriteDataToUserPageKeyRegister extends AMD64LIRInstruction {
public static final LIRInstructionClass<AMD64WriteDataToUserPageKeyRegister> TYPE = LIRInstructionClass.create(AMD64WriteDataToUserPageKeyRegister.class);

// the argument to wrpkru is in eax
@Use protected Value arg;
// ecx and edx need to be zero
@Temp({REG}) protected AllocatableValue zeroArg1;
@Temp({REG}) protected AllocatableValue zeroArg2;

public AMD64WriteDataToUserPageKeyRegister(RegisterValue arg) {
super(TYPE);
assert arg.getRegister().equals(AMD64.rax);
this.arg = arg;
zeroArg1 = AMD64.rcx.asValue(LIRKind.value(AMD64Kind.DWORD));
zeroArg2 = AMD64.rdx.asValue(LIRKind.value(AMD64Kind.DWORD));
}

@Override
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
masm.xorl(ValueUtil.asRegister(zeroArg1), ValueUtil.asRegister(zeroArg1));
masm.xorl(ValueUtil.asRegister(zeroArg2), ValueUtil.asRegister(zeroArg2));
masm.wrpkru();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.core.amd64;

import jdk.graal.compiler.core.common.type.IntegerStamp;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.nodeinfo.NodeCycles;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodeinfo.NodeSize;
import jdk.graal.compiler.nodes.FixedWithNextNode;
import jdk.graal.compiler.nodes.spi.LIRLowerable;
import jdk.graal.compiler.nodes.spi.NodeLIRBuilderTool;
import jdk.vm.ci.meta.Value;

@NodeInfo(cycles = NodeCycles.CYCLES_1, size = NodeSize.SIZE_4)
public class ReadProtectionKeyRegisterNode extends FixedWithNextNode implements LIRLowerable {
public static final NodeClass<ReadProtectionKeyRegisterNode> TYPE = NodeClass.create(ReadProtectionKeyRegisterNode.class);

public ReadProtectionKeyRegisterNode() {
super(TYPE, IntegerStamp.create(32));
}

@Override
public void generate(NodeLIRBuilderTool gen) {
Value result = gen.getLIRGeneratorTool().emitProtectionKeyRegisterRead();
gen.setResult(this, result);
}

@NodeIntrinsic
public static native int readProtectionKeyRegister();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.core.amd64;

import jdk.graal.compiler.core.common.type.StampFactory;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.nodeinfo.NodeCycles;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodeinfo.NodeSize;
import jdk.graal.compiler.nodes.FixedWithNextNode;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.nodes.spi.LIRLowerable;
import jdk.graal.compiler.nodes.spi.NodeLIRBuilderTool;
import org.graalvm.word.WordBase;

@NodeInfo(cycles = NodeCycles.CYCLES_16, size = NodeSize.SIZE_4)
public class WriteProtectionKeyRegisterNode extends FixedWithNextNode implements LIRLowerable {
public static final NodeClass<WriteProtectionKeyRegisterNode> TYPE = NodeClass.create(WriteProtectionKeyRegisterNode.class);

@Input protected ValueNode value;

public WriteProtectionKeyRegisterNode(ValueNode value) {
super(TYPE, StampFactory.forVoid());
this.value = value;
}

@Override
public void generate(NodeLIRBuilderTool gen) {
gen.getLIRGeneratorTool().emitProtectionKeyRegisterWrite(gen.operand(value));
}

@NodeIntrinsic
public static native void writeProtectionKeyRegister(WordBase value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,24 @@ default void emitConvertZeroToNull(AllocatableValue result, Value input) {
*/
void emitSpeculationFence();

/**
* Write value to the protection key register.
*
* @param value to be written
*/
default void emitProtectionKeyRegisterWrite(Value value) {
throw new GraalError("Emitting code to write a value to the protection key register is not currently supported on %s", target().arch);
}

/**
* Read contents of the protection key register.
*
* @return value read from the register
*/
default Value emitProtectionKeyRegisterRead() {
throw new GraalError("Emitting code to read the contents of the protection key register is not currently supported on %s", target().arch);
}

default VirtualStackSlot allocateStackMemory(int sizeInBytes, int alignmentInBytes) {
return getResult().getFrameMapBuilder().allocateStackMemory(sizeInBytes, alignmentInBytes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.nodes.extended;

import jdk.graal.compiler.core.common.type.StampFactory;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.nodeinfo.NodeCycles;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodeinfo.NodeSize;
import jdk.graal.compiler.nodes.FixedWithNextNode;
import jdk.graal.compiler.nodes.spi.LIRLowerable;
import jdk.graal.compiler.nodes.spi.NodeLIRBuilderTool;

@NodeInfo(cycles = NodeCycles.CYCLES_4, size = NodeSize.SIZE_4)
public class SpeculationFenceNode extends FixedWithNextNode implements LIRLowerable {
public static final NodeClass<SpeculationFenceNode> TYPE = NodeClass.create(SpeculationFenceNode.class);

public SpeculationFenceNode() {
super(TYPE, StampFactory.forVoid());
}

@Override
public void generate(NodeLIRBuilderTool generator) {
generator.getLIRGeneratorTool().emitSpeculationFence();
}

@NodeIntrinsic
public static native void memoryBarrier();
}
Loading

0 comments on commit a6df9b1

Please sign in to comment.