1 /*
2 * Copyright (c) 2022, 2026, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include "downcallLinker.hpp"
25 #include "runtime/interfaceSupport.inline.hpp"
26
27 #include <cerrno>
28 #ifdef _WIN64
29 #include <Windows.h>
30 #include <Winsock2.h>
31 #endif
32
33 // keep in synch with jdk.internal.foreign.abi.CapturableState
34 enum PreservableValues {
35 GET_LAST_ERROR = 1 << 0,
36 WSA_GET_LAST_ERROR = 1 << 1,
37 ERRNO = 1 << 2
38 };
39
40 // We call this from _thread_in_native, right before a downcall
41 JVM_LEAF(void, DowncallLinker::capture_state_pre(int32_t* value_ptr, int captured_state_mask))
42 #ifdef _WIN64
43 if (captured_state_mask & GET_LAST_ERROR) {
44 SetLastError(*value_ptr);
45 }
46 value_ptr++;
47 if (captured_state_mask & WSA_GET_LAST_ERROR) {
48 WSASetLastError(*value_ptr);
49 *value_ptr = WSAGetLastError();
50 }
51 value_ptr++;
52 #endif
53 if (captured_state_mask & ERRNO) {
54 errno = *value_ptr;
55 }
56 JVM_END
57
58 // We call this from _thread_in_native, right after a downcall
59 JVM_LEAF(void, DowncallLinker::capture_state_post(int32_t* value_ptr, int captured_state_mask))
60 #ifdef _WIN64
61 if (captured_state_mask & GET_LAST_ERROR) {
62 *value_ptr = GetLastError();
63 }
64 value_ptr++;
65 if (captured_state_mask & WSA_GET_LAST_ERROR) {
66 *value_ptr = WSAGetLastError();
67 }
68 value_ptr++;
69 #endif
70 if (captured_state_mask & ERRNO) {
71 *value_ptr = errno;
72 }
73 JVM_END
74
75 void DowncallLinker::StubGenerator::add_offsets_to_oops(GrowableArray<VMStorage>& java_regs, VMStorage tmp1, VMStorage tmp2) const {
76 int reg_idx = 0;
77 for (int sig_idx = 0; sig_idx < _num_args; sig_idx++) {
78 if (_signature[sig_idx] == T_OBJECT) {
79 assert(_signature[sig_idx + 1] == T_LONG, "expected offset after oop");
80 VMStorage reg_oop = java_regs.at(reg_idx++);
81 VMStorage reg_offset = java_regs.at(reg_idx++);
82 sig_idx++; // skip offset
83 pd_add_offset_to_oop(reg_oop, reg_offset, tmp1, tmp2);
84 } else if (_signature[sig_idx] != T_VOID) {
85 reg_idx++;
86 }
87 }
88 }