Skip to content

Commit 99efcde

Browse files
committed
8317545: AIX PPC64: Implementation of Foreign Function & Memory API
Reviewed-by: jvernee
1 parent e9d19d0 commit 99efcde

File tree

12 files changed

+209
-30
lines changed

12 files changed

+209
-30
lines changed

src/hotspot/cpu/ppc/foreignGlobals_ppc.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ bool ABIDescriptor::is_volatile_reg(FloatRegister reg) const {
4747
}
4848

4949
bool ForeignGlobals::is_foreign_linker_supported() {
50-
#ifdef LINUX
5150
return true;
52-
#else
53-
return false;
54-
#endif
5551
}
5652

5753
// Stubbed out, implement later

src/java.base/share/classes/jdk/internal/foreign/CABI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public enum CABI {
3939
LINUX_AARCH_64,
4040
MAC_OS_AARCH_64,
4141
WIN_AARCH_64,
42+
AIX_PPC_64,
4243
LINUX_PPC_64,
4344
LINUX_PPC_64_LE,
4445
LINUX_RISCV_64,
@@ -78,6 +79,8 @@ private static CABI computeCurrent() {
7879
} else if (arch.equals("ppc64")) {
7980
if (OperatingSystem.isLinux()) {
8081
return LINUX_PPC_64;
82+
} else if (OperatingSystem.isAix()) {
83+
return AIX_PPC_64;
8184
}
8285
} else if (arch.equals("ppc64le")) {
8386
if (OperatingSystem.isLinux()) {

src/java.base/share/classes/jdk/internal/foreign/abi/AbstractLinker.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import jdk.internal.foreign.abi.aarch64.macos.MacOsAArch64Linker;
3131
import jdk.internal.foreign.abi.aarch64.windows.WindowsAArch64Linker;
3232
import jdk.internal.foreign.abi.fallback.FallbackLinker;
33+
import jdk.internal.foreign.abi.ppc64.aix.AixPPC64Linker;
3334
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64Linker;
3435
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64leLinker;
3536
import jdk.internal.foreign.abi.riscv64.linux.LinuxRISCV64Linker;
@@ -62,7 +63,7 @@
6263

6364
public abstract sealed class AbstractLinker implements Linker permits LinuxAArch64Linker, MacOsAArch64Linker,
6465
SysVx64Linker, WindowsAArch64Linker,
65-
Windowsx64Linker,
66+
Windowsx64Linker, AixPPC64Linker,
6667
LinuxPPC64Linker, LinuxPPC64leLinker,
6768
LinuxRISCV64Linker, LinuxS390Linker,
6869
FallbackLinker {
@@ -179,6 +180,11 @@ private void checkLayout(MemoryLayout layout) {
179180
}
180181
}
181182

183+
// some ABIs have special handling for struct members
184+
protected void checkStructMember(MemoryLayout member, long offset) {
185+
checkLayoutRecursive(member);
186+
}
187+
182188
private void checkLayoutRecursive(MemoryLayout layout) {
183189
if (layout instanceof ValueLayout vl) {
184190
checkSupported(vl);
@@ -190,7 +196,7 @@ private void checkLayoutRecursive(MemoryLayout layout) {
190196
// check element offset before recursing so that an error points at the
191197
// outermost layout first
192198
checkMemberOffset(sl, member, lastUnpaddedOffset, offset);
193-
checkLayoutRecursive(member);
199+
checkStructMember(member, offset);
194200

195201
offset += member.byteSize();
196202
if (!(member instanceof PaddingLayout)) {

src/java.base/share/classes/jdk/internal/foreign/abi/SharedUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import jdk.internal.foreign.abi.aarch64.macos.MacOsAArch64Linker;
3434
import jdk.internal.foreign.abi.aarch64.windows.WindowsAArch64Linker;
3535
import jdk.internal.foreign.abi.fallback.FallbackLinker;
36+
import jdk.internal.foreign.abi.ppc64.aix.AixPPC64Linker;
3637
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64Linker;
3738
import jdk.internal.foreign.abi.ppc64.linux.LinuxPPC64leLinker;
3839
import jdk.internal.foreign.abi.riscv64.linux.LinuxRISCV64Linker;
@@ -244,6 +245,7 @@ public static Linker getSystemLinker() {
244245
case LINUX_AARCH_64 -> LinuxAArch64Linker.getInstance();
245246
case MAC_OS_AARCH_64 -> MacOsAArch64Linker.getInstance();
246247
case WIN_AARCH_64 -> WindowsAArch64Linker.getInstance();
248+
case AIX_PPC_64 -> AixPPC64Linker.getInstance();
247249
case LINUX_PPC_64 -> LinuxPPC64Linker.getInstance();
248250
case LINUX_PPC_64_LE -> LinuxPPC64leLinker.getInstance();
249251
case LINUX_RISCV_64 -> LinuxRISCV64Linker.getInstance();

src/java.base/share/classes/jdk/internal/foreign/abi/ppc64/CallArranger.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import jdk.internal.foreign.abi.LinkerOptions;
3636
import jdk.internal.foreign.abi.SharedUtils;
3737
import jdk.internal.foreign.abi.VMStorage;
38+
import jdk.internal.foreign.abi.ppc64.aix.AixCallArranger;
39+
import jdk.internal.foreign.abi.ppc64.linux.ABIv1CallArranger;
40+
import jdk.internal.foreign.abi.ppc64.linux.ABIv2CallArranger;
3841

3942
import java.lang.foreign.AddressLayout;
4043
import java.lang.foreign.FunctionDescriptor;
@@ -62,6 +65,7 @@
6265
*/
6366
public abstract class CallArranger {
6467
final boolean useABIv2 = useABIv2();
68+
final boolean isAIX = isAIX();
6569

6670
private static final int STACK_SLOT_SIZE = 8;
6771
private static final int MAX_COPY_SIZE = 8;
@@ -91,11 +95,13 @@ protected CallArranger() {}
9195

9296
public static final CallArranger ABIv1 = new ABIv1CallArranger();
9397
public static final CallArranger ABIv2 = new ABIv2CallArranger();
98+
public static final CallArranger AIX = new AixCallArranger();
9499

95100
/**
96101
* Select ABI version
97102
*/
98103
protected abstract boolean useABIv2();
104+
protected abstract boolean isAIX();
99105

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

344350
@Override
345351
List<Binding> getBindings(Class<?> carrier, MemoryLayout layout) {
346-
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2);
352+
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2, isAIX);
347353
Binding.Builder bindings = Binding.builder();
348354
switch (argumentClass) {
349355
case STRUCT_REGISTER -> {
350356
assert carrier == MemorySegment.class;
351357
VMStorage[] regs = storageCalculator.structAlloc(layout);
352-
final boolean isLargeABIv1Struct = !useABIv2 && layout.byteSize() > MAX_COPY_SIZE;
358+
final boolean isLargeABIv1Struct = !useABIv2 &&
359+
(isAIX || layout.byteSize() > MAX_COPY_SIZE);
353360
long offset = 0;
354361
for (VMStorage storage : regs) {
355362
// Last slot may be partly used.
@@ -430,14 +437,15 @@ class BoxBindingCalculator extends BindingCalculator {
430437

431438
@Override
432439
List<Binding> getBindings(Class<?> carrier, MemoryLayout layout) {
433-
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2);
440+
TypeClass argumentClass = TypeClass.classifyLayout(layout, useABIv2, isAIX);
434441
Binding.Builder bindings = Binding.builder();
435442
switch (argumentClass) {
436443
case STRUCT_REGISTER -> {
437444
assert carrier == MemorySegment.class;
438445
bindings.allocate(layout);
439446
VMStorage[] regs = storageCalculator.structAlloc(layout);
440-
final boolean isLargeABIv1Struct = !useABIv2 && layout.byteSize() > MAX_COPY_SIZE;
447+
final boolean isLargeABIv1Struct = !useABIv2 &&
448+
(isAIX || layout.byteSize() > MAX_COPY_SIZE);
441449
long offset = 0;
442450
for (VMStorage storage : regs) {
443451
// Last slot may be partly used.

src/java.base/share/classes/jdk/internal/foreign/abi/ppc64/TypeClass.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ static boolean isHomogeneousFloatAggregate(MemoryLayout type, boolean useABIv2)
112112
return true;
113113
}
114114

115-
private static TypeClass classifyStructType(MemoryLayout layout, boolean useABIv2) {
116-
if (isHomogeneousFloatAggregate(layout, useABIv2)) {
115+
private static TypeClass classifyStructType(MemoryLayout layout, boolean useABIv2, boolean isAIX) {
116+
if (!isAIX && isHomogeneousFloatAggregate(layout, useABIv2)) {
117117
return TypeClass.STRUCT_HFA;
118118
}
119119
return TypeClass.STRUCT_REGISTER;
@@ -124,11 +124,11 @@ static boolean isStructHFAorReturnRegisterAggregate(MemoryLayout layout, boolean
124124
return isHomogeneousFloatAggregate(layout, true) || isReturnRegisterAggregate(layout);
125125
}
126126

127-
public static TypeClass classifyLayout(MemoryLayout type, boolean useABIv2) {
127+
public static TypeClass classifyLayout(MemoryLayout type, boolean useABIv2, boolean isAIX) {
128128
if (type instanceof ValueLayout) {
129129
return classifyValueType((ValueLayout) type);
130130
} else if (type instanceof GroupLayout) {
131-
return classifyStructType(type, useABIv2);
131+
return classifyStructType(type, useABIv2, isAIX);
132132
} else {
133133
throw new IllegalArgumentException("Unhandled type " + type);
134134
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2023 SAP SE. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
package jdk.internal.foreign.abi.ppc64.aix;
27+
28+
import jdk.internal.foreign.abi.ppc64.CallArranger;
29+
30+
/**
31+
* PPC64 CallArranger specialized for AIX.
32+
*/
33+
public class AixCallArranger extends CallArranger {
34+
35+
@Override
36+
protected boolean useABIv2() {
37+
return false;
38+
}
39+
40+
@Override
41+
protected boolean isAIX() {
42+
return true;
43+
}
44+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2023 SAP SE. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
package jdk.internal.foreign.abi.ppc64.aix;
27+
28+
import jdk.internal.foreign.abi.AbstractLinker;
29+
import jdk.internal.foreign.abi.LinkerOptions;
30+
import jdk.internal.foreign.abi.SharedUtils;
31+
import jdk.internal.foreign.abi.ppc64.CallArranger;
32+
33+
import java.lang.foreign.FunctionDescriptor;
34+
import java.lang.foreign.MemoryLayout;
35+
import java.lang.foreign.ValueLayout;
36+
import java.lang.invoke.MethodHandle;
37+
import java.lang.invoke.MethodType;
38+
import java.nio.ByteOrder;
39+
import java.util.Map;
40+
41+
public final class AixPPC64Linker extends AbstractLinker {
42+
43+
static final Map<String, MemoryLayout> CANONICAL_LAYOUTS =
44+
SharedUtils.canonicalLayouts(ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG, ValueLayout.JAVA_INT);
45+
46+
public static AixPPC64Linker getInstance() {
47+
final class Holder {
48+
private static final AixPPC64Linker INSTANCE = new AixPPC64Linker();
49+
}
50+
51+
return Holder.INSTANCE;
52+
}
53+
54+
private AixPPC64Linker() {
55+
// Ensure there is only one instance
56+
}
57+
58+
@Override
59+
protected void checkStructMember(MemoryLayout member, long offset) {
60+
// special case double members that are not the first member
61+
// see: https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=data-using-alignment-modes
62+
// Note: It is possible to enforce 8-byte alignment by #pragma align (natural)
63+
// Therefore, we use normal checks if we are already 8-byte aligned.
64+
if ((offset % 8 != 0) && (member instanceof ValueLayout vl && vl.carrier() == double.class)) {
65+
if (vl.byteAlignment() != 4) {
66+
throw new IllegalArgumentException("double struct member " + vl + " at offset " + offset + " should be 4-byte aligned");
67+
}
68+
if (vl.order() != linkerByteOrder()) {
69+
throw new IllegalArgumentException("double struct member " + vl + " at offset " + offset + " has an unexpected byte order");
70+
}
71+
} else {
72+
super.checkStructMember(member, offset);
73+
}
74+
}
75+
76+
@Override
77+
protected MethodHandle arrangeDowncall(MethodType inferredMethodType, FunctionDescriptor function, LinkerOptions options) {
78+
return CallArranger.AIX.arrangeDowncall(inferredMethodType, function, options);
79+
}
80+
81+
@Override
82+
protected UpcallStubFactory arrangeUpcall(MethodType targetType, FunctionDescriptor function, LinkerOptions options) {
83+
return CallArranger.AIX.arrangeUpcall(targetType, function, options);
84+
}
85+
86+
@Override
87+
protected ByteOrder linkerByteOrder() {
88+
return ByteOrder.BIG_ENDIAN;
89+
}
90+
91+
@Override
92+
public Map<String, MemoryLayout> canonicalLayouts() {
93+
return CANONICAL_LAYOUTS;
94+
}
95+
}

src/java.base/share/classes/jdk/internal/foreign/abi/ppc64/ABIv1CallArranger.java renamed to src/java.base/share/classes/jdk/internal/foreign/abi/ppc64/linux/ABIv1CallArranger.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
* or visit www.oracle.com if you need additional information or have any
2424
* questions.
2525
*/
26-
package jdk.internal.foreign.abi.ppc64;
26+
package jdk.internal.foreign.abi.ppc64.linux;
27+
28+
import jdk.internal.foreign.abi.ppc64.CallArranger;
2729

2830
/**
2931
* PPC64 CallArranger specialized for ABI v1.
@@ -34,4 +36,9 @@ public class ABIv1CallArranger extends CallArranger {
3436
protected boolean useABIv2() {
3537
return false;
3638
}
39+
40+
@Override
41+
protected boolean isAIX() {
42+
return false;
43+
}
3744
}

src/java.base/share/classes/jdk/internal/foreign/abi/ppc64/ABIv2CallArranger.java renamed to src/java.base/share/classes/jdk/internal/foreign/abi/ppc64/linux/ABIv2CallArranger.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
* or visit www.oracle.com if you need additional information or have any
2424
* questions.
2525
*/
26-
package jdk.internal.foreign.abi.ppc64;
26+
package jdk.internal.foreign.abi.ppc64.linux;
27+
28+
import jdk.internal.foreign.abi.ppc64.CallArranger;
2729

2830
/**
2931
* PPC64 CallArranger specialized for ABI v2.
@@ -34,4 +36,9 @@ public class ABIv2CallArranger extends CallArranger {
3436
protected boolean useABIv2() {
3537
return true;
3638
}
39+
40+
@Override
41+
protected boolean isAIX() {
42+
return false;
43+
}
3744
}

0 commit comments

Comments
 (0)