xref: /aosp_15_r20/art/runtime/arch/riscv64/native_entrypoints_riscv64.S (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker/*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker#include "asm_support_riscv64.S"
18*795d594fSAndroid Build Coastguard Worker#include "interpreter/cfi_asm_support.h"
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker/*
21*795d594fSAndroid Build Coastguard Worker * This file contains all native entrypoints that are called using the native ABI and do not
22*795d594fSAndroid Build Coastguard Worker * transition to the quick ABI. For example: the switch interpreter (using the native ABI) directly
23*795d594fSAndroid Build Coastguard Worker * calls ExecuteSwitchImplAsm and this code will always return back to the switch interpreter,
24*795d594fSAndroid Build Coastguard Worker * again using the native ABI. Because of this behaviour ExecuteSwitchImplAsm should be included in
25*795d594fSAndroid Build Coastguard Worker * this file. This is done so these native entrypoints can be compiled independently to quick
26*795d594fSAndroid Build Coastguard Worker * entrypoints for cases when the kRuntimeISA and kRuntimeQuickCodeISA do not match.
27*795d594fSAndroid Build Coastguard Worker *
28*795d594fSAndroid Build Coastguard Worker * See comment on StackType (thread.h) for definitions and examples of quick ABI/code and
29*795d594fSAndroid Build Coastguard Worker * native ABI/code.
30*795d594fSAndroid Build Coastguard Worker */
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker// Wrap ExecuteSwitchImpl in assembly method which specifies DEX PC for unwinding.
33*795d594fSAndroid Build Coastguard Worker//  Argument 0: a0: The context pointer for ExecuteSwitchImpl.
34*795d594fSAndroid Build Coastguard Worker//  Argument 1: a1: Pointer to the templated ExecuteSwitchImpl to call.
35*795d594fSAndroid Build Coastguard Worker//  Argument 2: a2: The value of DEX PC (memory address of the methods bytecode).
36*795d594fSAndroid Build Coastguard WorkerENTRY ExecuteSwitchImplAsm
37*795d594fSAndroid Build Coastguard Worker    INCREASE_FRAME 16
38*795d594fSAndroid Build Coastguard Worker    SAVE_GPR s1, 0
39*795d594fSAndroid Build Coastguard Worker    SAVE_GPR ra, 8
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Worker    mv s1, a2   // s1 = DEX PC
42*795d594fSAndroid Build Coastguard Worker    CFI_DEFINE_DEX_PC_WITH_OFFSET(0 /* a0 */, 9 /* s1, a.k.a. x9 */, 0)
43*795d594fSAndroid Build Coastguard Worker    jalr a1     // Call the wrapped method.
44*795d594fSAndroid Build Coastguard Worker
45*795d594fSAndroid Build Coastguard Worker    RESTORE_GPR s1, 0
46*795d594fSAndroid Build Coastguard Worker    RESTORE_GPR ra, 8
47*795d594fSAndroid Build Coastguard Worker    DECREASE_FRAME 16
48*795d594fSAndroid Build Coastguard Worker    ret
49*795d594fSAndroid Build Coastguard WorkerEND ExecuteSwitchImplAsm
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker// JNI dlsym lookup stub.
52*795d594fSAndroid Build Coastguard Worker.extern artFindNativeMethod
53*795d594fSAndroid Build Coastguard Worker.extern artFindNativeMethodRunnable
54*795d594fSAndroid Build Coastguard WorkerENTRY art_jni_dlsym_lookup_stub
55*795d594fSAndroid Build Coastguard Worker    SAVE_ALL_ARGS_INCREASE_FRAME 2*8
56*795d594fSAndroid Build Coastguard Worker    SAVE_GPR fp, (ALL_ARGS_SIZE + 0)
57*795d594fSAndroid Build Coastguard Worker    SAVE_GPR ra, (ALL_ARGS_SIZE + 8)
58*795d594fSAndroid Build Coastguard Worker    add  fp, sp, ALL_ARGS_SIZE
59*795d594fSAndroid Build Coastguard Worker
60*795d594fSAndroid Build Coastguard Worker    // Call artFindNativeMethod for normal native.
61*795d594fSAndroid Build Coastguard Worker    // Call artFindNativeMethodRunnable for @FastNative or @CriticalNative.
62*795d594fSAndroid Build Coastguard Worker    // Both functions have a single argument: Thread::Current() in a0.
63*795d594fSAndroid Build Coastguard Worker    mv   a0, xSELF
64*795d594fSAndroid Build Coastguard Worker    ld   t0, THREAD_TOP_QUICK_FRAME_OFFSET(a0)   // uintptr_t tagged_quick_frame
65*795d594fSAndroid Build Coastguard Worker    andi t0, t0, ~TAGGED_JNI_SP_MASK             // ArtMethod** sp
66*795d594fSAndroid Build Coastguard Worker    ld   t0, (t0)                                // ArtMethod* method
67*795d594fSAndroid Build Coastguard Worker    lw   t0, ART_METHOD_ACCESS_FLAGS_OFFSET(t0)  // uint32_t access_flags
68*795d594fSAndroid Build Coastguard Worker    li   t1, (ACCESS_FLAGS_METHOD_IS_FAST_NATIVE | ACCESS_FLAGS_METHOD_IS_CRITICAL_NATIVE)
69*795d594fSAndroid Build Coastguard Worker    and  t0, t0, t1
70*795d594fSAndroid Build Coastguard Worker    bnez t0, .Llookup_stub_fast_or_critical_native
71*795d594fSAndroid Build Coastguard Worker    call artFindNativeMethod
72*795d594fSAndroid Build Coastguard Worker    j    .Llookup_stub_continue
73*795d594fSAndroid Build Coastguard Worker
74*795d594fSAndroid Build Coastguard Worker.Llookup_stub_fast_or_critical_native:
75*795d594fSAndroid Build Coastguard Worker    call  artFindNativeMethodRunnable
76*795d594fSAndroid Build Coastguard Worker
77*795d594fSAndroid Build Coastguard Worker.Llookup_stub_continue:
78*795d594fSAndroid Build Coastguard Worker    mv    t0, a0  // store result in a temp reg.
79*795d594fSAndroid Build Coastguard Worker    RESTORE_GPR fp, (ALL_ARGS_SIZE + 0)
80*795d594fSAndroid Build Coastguard Worker    RESTORE_GPR ra, (ALL_ARGS_SIZE + 8)
81*795d594fSAndroid Build Coastguard Worker    RESTORE_ALL_ARGS_DECREASE_FRAME 2*8
82*795d594fSAndroid Build Coastguard Worker
83*795d594fSAndroid Build Coastguard Worker    beqz  t0, 1f  // is method code null?
84*795d594fSAndroid Build Coastguard Worker    jr    t0      // if non-null, tail call to method code.
85*795d594fSAndroid Build Coastguard Worker1:
86*795d594fSAndroid Build Coastguard Worker    ret           // restore regs and return to caller to handle exception.
87*795d594fSAndroid Build Coastguard WorkerEND art_jni_dlsym_lookup_stub
88