2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-11-05 15:40:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-18 07:49:51 +00:00
|
|
|
#include <bits/stdint.h>
|
2019-02-26 13:05:28 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
2018-11-05 15:40:48 +00:00
|
|
|
|
|
|
|
__BEGIN_DECLS
|
|
|
|
|
2021-08-13 22:32:19 +00:00
|
|
|
//
|
|
|
|
// /!\ This structure is accessed inside setjmp.S, keep both files in sync!
|
|
|
|
//
|
|
|
|
|
2019-02-26 13:05:28 +00:00
|
|
|
struct __jmp_buf {
|
2022-12-25 17:43:58 +00:00
|
|
|
#if defined(__x86_64__)
|
2021-08-13 22:32:19 +00:00
|
|
|
uint64_t rbx;
|
|
|
|
uint64_t r12;
|
|
|
|
uint64_t r13;
|
|
|
|
uint64_t r14;
|
|
|
|
uint64_t r15;
|
|
|
|
uint64_t rbp;
|
|
|
|
uint64_t rsp;
|
|
|
|
uint64_t rip;
|
2022-10-12 20:19:01 +00:00
|
|
|
#elif defined(__aarch64__)
|
2021-08-28 02:36:38 +00:00
|
|
|
// FIXME: This is likely incorrect.
|
|
|
|
uint64_t regs[22];
|
2021-07-01 10:04:20 +00:00
|
|
|
#else
|
|
|
|
# error
|
|
|
|
#endif
|
2021-08-13 22:32:19 +00:00
|
|
|
int did_save_signal_mask;
|
2019-02-26 13:05:28 +00:00
|
|
|
sigset_t saved_signal_mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct __jmp_buf jmp_buf[1];
|
2019-11-17 20:01:12 +00:00
|
|
|
typedef struct __jmp_buf sigjmp_buf[1];
|
2018-11-05 15:40:48 +00:00
|
|
|
|
2021-08-30 22:20:10 +00:00
|
|
|
/**
|
2021-08-30 23:35:54 +00:00
|
|
|
* Since setjmp.h may be included by ports written in C, we need to guard this.
|
2021-08-30 22:20:10 +00:00
|
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
2022-12-25 17:43:58 +00:00
|
|
|
# if defined(__x86_64__)
|
2021-08-30 23:35:54 +00:00
|
|
|
static_assert(sizeof(struct __jmp_buf) == 72, "struct __jmp_buf unsynchronized with x86_64/setjmp.S");
|
2022-10-12 20:19:01 +00:00
|
|
|
# elif defined(__aarch64__)
|
2021-08-30 23:35:54 +00:00
|
|
|
static_assert(sizeof(struct __jmp_buf) == 184, "struct __jmp_buf unsynchronized with aarch64/setjmp.S");
|
|
|
|
# else
|
|
|
|
# error
|
|
|
|
# endif
|
2021-08-25 15:11:34 +00:00
|
|
|
#endif
|
|
|
|
|
2021-08-13 22:32:19 +00:00
|
|
|
/**
|
|
|
|
* Calling conventions mandates that sigsetjmp() cannot call setjmp(),
|
|
|
|
* otherwise the restored calling environment will not be the original caller's
|
|
|
|
* but sigsetjmp()'s and we'll return to the wrong call site on siglongjmp().
|
|
|
|
*
|
|
|
|
* The setjmp(), sigsetjmp() and longjmp() functions have to be implemented in
|
|
|
|
* assembly because they touch the call stack and registers in non-portable
|
|
|
|
* ways. However, we *can* implement siglongjmp() as a standard C function.
|
|
|
|
*/
|
|
|
|
|
2018-11-05 15:40:48 +00:00
|
|
|
int setjmp(jmp_buf);
|
2021-08-13 22:32:19 +00:00
|
|
|
__attribute__((noreturn)) void longjmp(jmp_buf, int val);
|
2018-11-05 15:40:48 +00:00
|
|
|
|
2019-11-17 20:01:12 +00:00
|
|
|
int sigsetjmp(sigjmp_buf, int savesigs);
|
2021-08-13 22:32:19 +00:00
|
|
|
__attribute__((noreturn)) void siglongjmp(sigjmp_buf, int val);
|
2019-02-26 13:05:28 +00:00
|
|
|
|
2021-12-24 11:28:06 +00:00
|
|
|
/**
|
|
|
|
* _setjmp() and _longjmp() are specified as behaving the exactly the same as
|
|
|
|
* setjmp() and longjmp(), except they are not supposed to modify the signal mask.
|
|
|
|
*
|
|
|
|
* Our implementations already follow this restriction, so we just map them directly
|
|
|
|
* to the same functions.
|
|
|
|
*
|
|
|
|
* https://pubs.opengroup.org/onlinepubs/9699969599/functions/_setjmp.html
|
|
|
|
*/
|
|
|
|
int _setjmp(jmp_buf);
|
|
|
|
__attribute__((noreturn)) void _longjmp(jmp_buf, int val);
|
|
|
|
|
2018-11-05 15:40:48 +00:00
|
|
|
__END_DECLS
|