setjmp.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <bits/stdint.h>
  8. #include <signal.h>
  9. #include <stdbool.h>
  10. #include <sys/cdefs.h>
  11. #include <sys/types.h>
  12. __BEGIN_DECLS
  13. //
  14. // /!\ This structure is accessed inside setjmp.S, keep both files in sync!
  15. //
  16. struct __jmp_buf {
  17. #ifdef __i386__
  18. uint32_t ebx;
  19. uint32_t esi;
  20. uint32_t edi;
  21. uint32_t ebp;
  22. uint32_t esp;
  23. uint32_t eip;
  24. #elif __x86_64__
  25. uint64_t rbx;
  26. uint64_t r12;
  27. uint64_t r13;
  28. uint64_t r14;
  29. uint64_t r15;
  30. uint64_t rbp;
  31. uint64_t rsp;
  32. uint64_t rip;
  33. #elif __aarch64__
  34. // FIXME: This is likely incorrect.
  35. uint64_t regs[22];
  36. #else
  37. # error
  38. #endif
  39. int did_save_signal_mask;
  40. sigset_t saved_signal_mask;
  41. };
  42. typedef struct __jmp_buf jmp_buf[1];
  43. typedef struct __jmp_buf sigjmp_buf[1];
  44. /**
  45. * Since setjmp.h may be included by ports written in C, we need to guard this.
  46. */
  47. #ifdef __cplusplus
  48. # ifdef __i386__
  49. static_assert(sizeof(struct __jmp_buf) == 32, "struct __jmp_buf unsynchronized with i386/setjmp.S");
  50. # elif __x86_64__
  51. static_assert(sizeof(struct __jmp_buf) == 72, "struct __jmp_buf unsynchronized with x86_64/setjmp.S");
  52. # elif __aarch64__
  53. static_assert(sizeof(struct __jmp_buf) == 184, "struct __jmp_buf unsynchronized with aarch64/setjmp.S");
  54. # else
  55. # error
  56. # endif
  57. #endif
  58. /**
  59. * Calling conventions mandates that sigsetjmp() cannot call setjmp(),
  60. * otherwise the restored calling environment will not be the original caller's
  61. * but sigsetjmp()'s and we'll return to the wrong call site on siglongjmp().
  62. *
  63. * The setjmp(), sigsetjmp() and longjmp() functions have to be implemented in
  64. * assembly because they touch the call stack and registers in non-portable
  65. * ways. However, we *can* implement siglongjmp() as a standard C function.
  66. */
  67. int setjmp(jmp_buf);
  68. __attribute__((noreturn)) void longjmp(jmp_buf, int val);
  69. int sigsetjmp(sigjmp_buf, int savesigs);
  70. __attribute__((noreturn)) void siglongjmp(sigjmp_buf, int val);
  71. /**
  72. * _setjmp() and _longjmp() are specified as behaving the exactly the same as
  73. * setjmp() and longjmp(), except they are not supposed to modify the signal mask.
  74. *
  75. * Our implementations already follow this restriction, so we just map them directly
  76. * to the same functions.
  77. *
  78. * https://pubs.opengroup.org/onlinepubs/9699969599/functions/_setjmp.html
  79. */
  80. int _setjmp(jmp_buf);
  81. __attribute__((noreturn)) void _longjmp(jmp_buf, int val);
  82. __END_DECLS