1 /*
   2  * Copyright (c) 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.foreign.abi.x64.windows;
  26 
  27 import jdk.internal.foreign.ScopeImpl;
  28 import jdk.internal.foreign.Util;
  29 import jdk.internal.foreign.abi.*;
  30 import jdk.internal.foreign.memory.LayoutTypeImpl;
  31 
  32 import java.foreign.Library;
  33 import java.foreign.NativeMethodType;
  34 import java.foreign.NativeTypes;
  35 import java.foreign.Scope;
  36 import java.foreign.layout.Function;
  37 import java.foreign.memory.LayoutType;
  38 import java.foreign.memory.Pointer;
  39 import java.foreign.memory.Struct;
  40 import java.lang.invoke.MethodHandle;
  41 import java.lang.invoke.WrongMethodTypeException;
  42 import java.util.Collection;
  43 import java.util.List;
  44 
  45 /**
  46  * ABI implementation based on Windows ABI AMD64 supplement v.0.99.6
  47  */
  48 public class Windowsx64ABI implements SystemABI {
  49 
  50     public static final int MAX_INTEGER_ARGUMENT_REGISTERS = 4;
  51     public static final int MAX_INTEGER_RETURN_REGISTERS = 1;
  52     public static final int MAX_VECTOR_ARGUMENT_REGISTERS = 4;
  53     public static final int MAX_VECTOR_RETURN_REGISTERS = 1;
  54     public static final int MAX_REGISTER_ARGUMENTS = 4;
  55     public static final int MAX_REGISTER_RETURNS = 1;
  56 
  57     private static Windowsx64ABI instance;
  58 
  59     public static Windowsx64ABI getInstance() {
  60         if (instance == null) {
  61             instance = new Windowsx64ABI();
  62         }
  63         return instance;
  64     }
  65 
  66     static CallingSequence arrangeCall(NativeMethodType nmt) {
  67         Function f = nmt.function();
  68         CallingSequenceBuilder builder = new CallingSequenceBuilderImpl(
  69                 f.returnLayout().orElse(null));
  70         f.argumentLayouts().forEach(builder::addArgument);
  71         return builder.build();
  72     }
  73 
  74     @Override
  75     public MethodHandle downcallHandle(CallingConvention cc, Library.Symbol symbol, NativeMethodType nmt) {
  76         Util.checkNoArrays(nmt.methodType());
  77         if (nmt.isVarArgs()) {
  78             return VarargsInvoker.make(symbol, nmt, CallingSequenceBuilderImpl::new, adapter);
  79         }
  80 
  81         try {
  82             return new UniversalNativeInvoker(symbol, arrangeCall(nmt), nmt, adapter).getBoundMethodHandle();
  83         } catch (IllegalAccessException ex) {
  84             throw new IllegalStateException(ex);
  85         }
  86     }
  87 
  88     @Override
  89     public Library.Symbol upcallStub(CallingConvention cc, MethodHandle target, NativeMethodType nmt) {
  90         Util.checkNoArrays(nmt.methodType());
  91         if (!target.type().equals(nmt.methodType())) {
  92             throw new WrongMethodTypeException("Native method type has wrong type: " + nmt.methodType());
  93         }
  94 
  95         return UpcallStubs.registerUpcallStub(new UniversalUpcallHandler(target, arrangeCall(nmt), nmt, adapter));
  96     }
  97 
  98     @Override
  99     public CallingConvention defaultCallingConvention() {
 100         return null;
 101     }
 102 
 103     @Override
 104     public CallingConvention namedCallingConvention(String name) throws IllegalArgumentException {
 105         return null;
 106     }
 107 
 108     @Override
 109     public Collection<CallingConvention> callingConventions() {
 110         return null;
 111     }
 112 
 113     UniversalAdapter adapter = new UniversalAdapter() {
 114         @Override
 115         public void unboxValue(Object o, LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> dstPtrFunc,
 116                            List<ArgumentBinding> bindings) throws Throwable {
 117             if (o instanceof Struct) {
 118                 assert (bindings.size() == 1); // always for structs on windows
 119 
 120                 Pointer<?> structPtr = ((Struct<?>) o).ptr();
 121                 LayoutType<?> structType = structPtr.type();
 122                 Pointer<Long> src = Util.unsafeCast(structPtr, NativeTypes.UINT64);
 123                 ArgumentBinding binding = bindings.get(0);
 124 
 125                 if (CallingSequenceBuilderImpl.isRegisterAggregate(binding.argument().layout())) { // pass value
 126                     Pointer.copy(src, dstPtrFunc.apply(binding), structType.bytesSize());
 127                 } else { // pass a pointer
 128                     /*
 129                      * Leak memory for now
 130                      */
 131                     Scope scope = Scope.globalScope().fork();
 132                     Pointer<?> copy = scope.allocate(structType);
 133                     Pointer.copy(src, copy, structType.bytesSize());
 134 
 135                     Pointer<?> dst = dstPtrFunc.apply(binding);
 136                     Util.unsafeCast(dst, NativeTypes.UINT64).type().setter().invoke(dst, copy.addr());
 137                 }
 138             } else {
 139                 assert bindings.size() <= 2;
 140                 Pointer<?> dst = Util.unsafeCast(dstPtrFunc.apply(bindings.get(0)), type);
 141                 dst.type().setter().invoke(dst, o);
 142             }
 143         }
 144 
 145         @Override
 146         public Object boxValue(LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> srcPtrFunc,
 147                                List<ArgumentBinding> bindings) throws IllegalAccessException {
 148             assert (bindings.size() == 1); // always on windows
 149             ArgumentBinding binding = bindings.get(0);
 150             Class<?> carrier = ((LayoutTypeImpl<?>) type).carrier();
 151             if (Util.isCStruct(carrier)) {
 152 
 153                 /*
 154                  * Leak memory for now
 155                  */
 156                 Scope scope = Scope.globalScope().fork();
 157 
 158                 @SuppressWarnings({"rawtypes", "unchecked"})
 159                 Pointer<?> rtmp = ((ScopeImpl) scope).allocate(type, 8);
 160 
 161                 if (CallingSequenceBuilderImpl.isRegisterAggregate(type.layout())) {
 162                     Pointer<Long> dst = Util.unsafeCast(rtmp, NativeTypes.UINT64).offset(binding.offset() / NativeTypes.UINT64.bytesSize());
 163                     Pointer.copy(srcPtrFunc.apply(binding), dst, binding.storage().getSize());
 164                 } else {
 165                     Pointer<?> local = Util.unsafeCast(srcPtrFunc.apply(binding), type.pointer()).get();
 166                     // need defensive copy since Structs don't have value semantics on the Java side.
 167                     Pointer.copy(local, rtmp, type.bytesSize());
 168                 }
 169 
 170                 return rtmp.get();
 171             }
 172             return Util.unsafeCast(srcPtrFunc.apply(binding), type).get();
 173         }
 174     };
 175 }