Skip to content

Commit

Permalink
8317545: AIX PPC64: Implementation of Foreign Function & Memory API
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee
  • Loading branch information
TheRealMDoerr committed Nov 2, 2023
1 parent e9d19d0 commit 99efcde
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 30 deletions.
4 changes: 0 additions & 4 deletions src/hotspot/cpu/ppc/foreignGlobals_ppc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ bool ABIDescriptor::is_volatile_reg(FloatRegister reg) const {
}

bool ForeignGlobals::is_foreign_linker_supported() {
#ifdef LINUX
return true;
#else
return false;
#endif
}

// Stubbed out, implement later
Expand Down
3 changes: 3 additions & 0 deletions src/java.base/share/classes/jdk/internal/foreign/CABI.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum CABI {
LINUX_AARCH_64,
MAC_OS_AARCH_64,
WIN_AARCH_64,
AIX_PPC_64,
LINUX_PPC_64,
LINUX_PPC_64_LE,
LINUX_RISCV_64,
Expand Down Expand Up @@ -78,6 +79,8 @@ private static CABI computeCurrent() {
} else if (arch.equals("ppc64")) {
if (OperatingSystem.isLinux()) {
return LINUX_PPC_64;
} else if (OperatingSystem.isAix()) {
return AIX_PPC_64;
}
} else if (arch.equals("ppc64le")) {
if (OperatingSystem.isLinux()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import jdk.internal.foreign.abi.aarch64.macos.MacOsAArch64Linker;
import jdk.internal.foreign.abi.aarch64.windows.WindowsAArch64Linker;
import jdk.internal.foreign.abi.fallback.FallbackLinker;
import jdk.internal.foreign.abi.ppc64.aix.AixPPC64Linker;
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64Linker;
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64leLinker;
import jdk.internal.foreign.abi.riscv64.linux.LinuxRISCV64Linker;
Expand Down Expand Up @@ -62,7 +63,7 @@

public abstract sealed class AbstractLinker implements Linker permits LinuxAArch64Linker, MacOsAArch64Linker,
SysVx64Linker, WindowsAArch64Linker,
Windowsx64Linker,
Windowsx64Linker, AixPPC64Linker,
LinuxPPC64Linker, LinuxPPC64leLinker,
LinuxRISCV64Linker, LinuxS390Linker,
FallbackLinker {
Expand Down Expand Up @@ -179,6 +180,11 @@ private void checkLayout(MemoryLayout layout) {
}
}

// some ABIs have special handling for struct members
protected void checkStructMember(MemoryLayout member, long offset) {
checkLayoutRecursive(member);
}

private void checkLayoutRecursive(MemoryLayout layout) {
if (layout instanceof ValueLayout vl) {
checkSupported(vl);
Expand All @@ -190,7 +196,7 @@ private void checkLayoutRecursive(MemoryLayout layout) {
// check element offset before recursing so that an error points at the
// outermost layout first
checkMemberOffset(sl, member, lastUnpaddedOffset, offset);
checkLayoutRecursive(member);
checkStructMember(member, offset);

offset += member.byteSize();
if (!(member instanceof PaddingLayout)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import jdk.internal.foreign.abi.aarch64.macos.MacOsAArch64Linker;
import jdk.internal.foreign.abi.aarch64.windows.WindowsAArch64Linker;
import jdk.internal.foreign.abi.fallback.FallbackLinker;
import jdk.internal.foreign.abi.ppc64.aix.AixPPC64Linker;
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64Linker;
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64leLinker;
import jdk.internal.foreign.abi.riscv64.linux.LinuxRISCV64Linker;
Expand Down Expand Up @@ -244,6 +245,7 @@ public static Linker getSystemLinker() {
case LINUX_AARCH_64 -> LinuxAArch64Linker.getInstance();
case MAC_OS_AARCH_64 -> MacOsAArch64Linker.getInstance();
case WIN_AARCH_64 -> WindowsAArch64Linker.getInstance();
case AIX_PPC_64 -> AixPPC64Linker.getInstance();
case LINUX_PPC_64 -> LinuxPPC64Linker.getInstance();
case LINUX_PPC_64_LE -> LinuxPPC64leLinker.getInstance();
case LINUX_RISCV_64 -> LinuxRISCV64Linker.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import jdk.internal.foreign.abi.LinkerOptions;
import jdk.internal.foreign.abi.SharedUtils;
import jdk.internal.foreign.abi.VMStorage;
import jdk.internal.foreign.abi.ppc64.aix.AixCallArranger;
import jdk.internal.foreign.abi.ppc64.linux.ABIv1CallArranger;
import jdk.internal.foreign.abi.ppc64.linux.ABIv2CallArranger;

import java.lang.foreign.AddressLayout;
import java.lang.foreign.FunctionDescriptor;
Expand Down Expand Up @@ -62,6 +65,7 @@
*/
public abstract class CallArranger {
final boolean useABIv2 = useABIv2();
final boolean isAIX = isAIX();

private static final int STACK_SLOT_SIZE = 8;
private static final int MAX_COPY_SIZE = 8;
Expand Down Expand Up @@ -91,11 +95,13 @@ protected CallArranger() {}

public static final CallArranger ABIv1 = new ABIv1CallArranger();
public static final CallArranger ABIv2 = new ABIv2CallArranger();
public static final CallArranger AIX = new AixCallArranger();

/**
* Select ABI version
*/
protected abstract boolean useABIv2();
protected abstract boolean isAIX();

public Bindings getBindings(MethodType mt, FunctionDescriptor cDesc, boolean forUpcall) {
return getBindings(mt, cDesc, forUpcall, LinkerOptions.empty());
Expand Down Expand Up @@ -206,7 +212,7 @@ VMStorage nextStorage(int type, boolean is32Bit) {
// offset for the next argument which will really use the stack.
// The reserved space for the Parameter Save Area is determined by the DowncallStubGenerator.
VMStorage stack;
if (!useABIv2 && is32Bit) {
if (!useABIv2 && !isAIX && is32Bit) {
stackAlloc(4, STACK_SLOT_SIZE); // Skip first half of stack slot.
stack = stackAlloc(4, 4);
} else {
Expand Down Expand Up @@ -343,13 +349,14 @@ class UnboxBindingCalculator extends BindingCalculator {

@Override
List<Binding> getBindings(Class<?> carrier, MemoryLayout layout) {
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2);
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2, isAIX);
Binding.Builder bindings = Binding.builder();
switch (argumentClass) {
case STRUCT_REGISTER -> {
assert carrier == MemorySegment.class;
VMStorage[] regs = storageCalculator.structAlloc(layout);
final boolean isLargeABIv1Struct = !useABIv2 && layout.byteSize() > MAX_COPY_SIZE;
final boolean isLargeABIv1Struct = !useABIv2 &&
(isAIX || layout.byteSize() > MAX_COPY_SIZE);
long offset = 0;
for (VMStorage storage : regs) {
// Last slot may be partly used.
Expand Down Expand Up @@ -430,14 +437,15 @@ class BoxBindingCalculator extends BindingCalculator {

@Override
List<Binding> getBindings(Class<?> carrier, MemoryLayout layout) {
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2);
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2, isAIX);
Binding.Builder bindings = Binding.builder();
switch (argumentClass) {
case STRUCT_REGISTER -> {
assert carrier == MemorySegment.class;
bindings.allocate(layout);
VMStorage[] regs = storageCalculator.structAlloc(layout);
final boolean isLargeABIv1Struct = !useABIv2 && layout.byteSize() > MAX_COPY_SIZE;
final boolean isLargeABIv1Struct = !useABIv2 &&
(isAIX || layout.byteSize() > MAX_COPY_SIZE);
long offset = 0;
for (VMStorage storage : regs) {
// Last slot may be partly used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ static boolean isHomogeneousFloatAggregate(MemoryLayout type, boolean useABIv2)
return true;
}

private static TypeClass classifyStructType(MemoryLayout layout, boolean useABIv2) {
if (isHomogeneousFloatAggregate(layout, useABIv2)) {
private static TypeClass classifyStructType(MemoryLayout layout, boolean useABIv2, boolean isAIX) {
if (!isAIX && isHomogeneousFloatAggregate(layout, useABIv2)) {
return TypeClass.STRUCT_HFA;
}
return TypeClass.STRUCT_REGISTER;
Expand All @@ -124,11 +124,11 @@ static boolean isStructHFAorReturnRegisterAggregate(MemoryLayout layout, boolean
return isHomogeneousFloatAggregate(layout, true) || isReturnRegisterAggregate(layout);
}

public static TypeClass classifyLayout(MemoryLayout type, boolean useABIv2) {
public static TypeClass classifyLayout(MemoryLayout type, boolean useABIv2, boolean isAIX) {
if (type instanceof ValueLayout) {
return classifyValueType((ValueLayout) type);
} else if (type instanceof GroupLayout) {
return classifyStructType(type, useABIv2);
return classifyStructType(type, useABIv2, isAIX);
} else {
throw new IllegalArgumentException("Unhandled type " + type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023 SAP SE. 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.internal.foreign.abi.ppc64.aix;

import jdk.internal.foreign.abi.ppc64.CallArranger;

/**
* PPC64 CallArranger specialized for AIX.
*/
public class AixCallArranger extends CallArranger {

@Override
protected boolean useABIv2() {
return false;
}

@Override
protected boolean isAIX() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023 SAP SE. 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.internal.foreign.abi.ppc64.aix;

import jdk.internal.foreign.abi.AbstractLinker;
import jdk.internal.foreign.abi.LinkerOptions;
import jdk.internal.foreign.abi.SharedUtils;
import jdk.internal.foreign.abi.ppc64.CallArranger;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.nio.ByteOrder;
import java.util.Map;

public final class AixPPC64Linker extends AbstractLinker {

static final Map<String, MemoryLayout> CANONICAL_LAYOUTS =
SharedUtils.canonicalLayouts(ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG, ValueLayout.JAVA_INT);

public static AixPPC64Linker getInstance() {
final class Holder {
private static final AixPPC64Linker INSTANCE = new AixPPC64Linker();
}

return Holder.INSTANCE;
}

private AixPPC64Linker() {
// Ensure there is only one instance
}

@Override
protected void checkStructMember(MemoryLayout member, long offset) {
// special case double members that are not the first member
// see: https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=data-using-alignment-modes
// Note: It is possible to enforce 8-byte alignment by #pragma align (natural)
// Therefore, we use normal checks if we are already 8-byte aligned.
if ((offset % 8 != 0) && (member instanceof ValueLayout vl && vl.carrier() == double.class)) {
if (vl.byteAlignment() != 4) {
throw new IllegalArgumentException("double struct member " + vl + " at offset " + offset + " should be 4-byte aligned");
}
if (vl.order() != linkerByteOrder()) {
throw new IllegalArgumentException("double struct member " + vl + " at offset " + offset + " has an unexpected byte order");
}
} else {
super.checkStructMember(member, offset);
}
}

@Override
protected MethodHandle arrangeDowncall(MethodType inferredMethodType, FunctionDescriptor function, LinkerOptions options) {
return CallArranger.AIX.arrangeDowncall(inferredMethodType, function, options);
}

@Override
protected UpcallStubFactory arrangeUpcall(MethodType targetType, FunctionDescriptor function, LinkerOptions options) {
return CallArranger.AIX.arrangeUpcall(targetType, function, options);
}

@Override
protected ByteOrder linkerByteOrder() {
return ByteOrder.BIG_ENDIAN;
}

@Override
public Map<String, MemoryLayout> canonicalLayouts() {
return CANONICAL_LAYOUTS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.foreign.abi.ppc64;
package jdk.internal.foreign.abi.ppc64.linux;

import jdk.internal.foreign.abi.ppc64.CallArranger;

/**
* PPC64 CallArranger specialized for ABI v1.
Expand All @@ -34,4 +36,9 @@ public class ABIv1CallArranger extends CallArranger {
protected boolean useABIv2() {
return false;
}

@Override
protected boolean isAIX() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.foreign.abi.ppc64;
package jdk.internal.foreign.abi.ppc64.linux;

import jdk.internal.foreign.abi.ppc64.CallArranger;

/**
* PPC64 CallArranger specialized for ABI v2.
Expand All @@ -34,4 +36,9 @@ public class ABIv2CallArranger extends CallArranger {
protected boolean useABIv2() {
return true;
}

@Override
protected boolean isAIX() {
return false;
}
}
Loading

1 comment on commit 99efcde

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.