1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "setjmp_thunks.h"
18
19 #include <setjmp.h>
20
21 #include "berberis/base/tracing.h"
22 #include "berberis/guest_os_primitives/guest_setjmp.h"
23 #include "berberis/guest_os_primitives/guest_thread.h"
24 #include "berberis/guest_os_primitives/guest_thread_manager.h"
25
26 namespace berberis {
27
28 // _longjmp(buf, ret) = siglongjmp(buf, ret) (see
29 // bionic/libc/arch-arm/bionic/setjmp.S)
DoThunk__longjmp(void * guest_buf,int value)30 void DoThunk__longjmp(void* guest_buf, int value) {
31 DoThunk_siglongjmp(guest_buf, value);
32 }
33
34 // _setjmp(buf) = sigsetjmp(buf, 0) (see bionic/libc/arch-arm/bionic/setjmp.S)
DoThunk__setjmp(void * guest_buf)35 int DoThunk__setjmp(void* guest_buf) { return DoThunk_sigsetjmp(guest_buf, 0); }
36
37 // longjmp(buf, ret) = siglongjmp(buf, ret) (see
38 // bionic/libc/arch-arm/bionic/setjmp.S)
DoThunk_longjmp(void * guest_buf,int value)39 void DoThunk_longjmp(void* guest_buf, int value) {
40 DoThunk_siglongjmp(guest_buf, value);
41 }
42
43 // setjmp(buf) = sigsetjmp(buf, 1) (see bionic/libc/arch-arm/bionic/setjmp.S)
DoThunk_setjmp(void * guest_buf)44 int DoThunk_setjmp(void* guest_buf) { return DoThunk_sigsetjmp(guest_buf, 1); }
45
DoThunk_siglongjmp(void * guest_buf,int value)46 void DoThunk_siglongjmp(void* guest_buf, int value) {
47 TRACE("DoThunk_siglongjmp, guest_buf=%p", guest_buf);
48 GuestThread* thread = GetCurrentGuestThread();
49 RestoreRegsFromJumpBuf(thread->state(), guest_buf, value);
50 // ATTENTION: don't restore signal mask, it is already restored!
51 siglongjmp(**GetHostJmpBufPtr(guest_buf), 0);
52 }
53
DoThunk_sigsetjmp(void * guest_buf,int save_sig_mask)54 int DoThunk_sigsetjmp(void* guest_buf, int save_sig_mask) {
55 TRACE("DoThunk_sigsetjmp, guest_buf=%p", guest_buf);
56 GuestThread* thread = GetCurrentGuestThread();
57 SaveRegsToJumpBuf(thread->state(), guest_buf, save_sig_mask);
58 *GetHostJmpBufPtr(guest_buf) = &thread->guest_call_execution()->buf;
59 return 0;
60 }
61
62 } // namespace berberis
63