libc: Add setjmp.h
All checks were successful
continuous-integration/drone/push Build is passing

Partially taken from pre-rewrite Luna, converted setjmp.asm to GNU assembly.
This commit is contained in:
apio 2023-03-28 19:40:48 +02:00
parent b8b8d20f5b
commit d00ca0d3ed
Signed by: apio
GPG Key ID: B8A7D06E42258954
8 changed files with 117 additions and 2 deletions

View File

@ -13,6 +13,7 @@ set(SOURCES
src/ctype.cpp src/ctype.cpp
src/time.cpp src/time.cpp
src/init.cpp src/init.cpp
src/setjmp.cpp
src/sys/stat.cpp src/sys/stat.cpp
src/sys/mman.cpp src/sys/mman.cpp
src/sys/wait.cpp src/sys/wait.cpp
@ -22,6 +23,7 @@ if(${LUNA_ARCH} STREQUAL "x86_64")
set(SOURCES set(SOURCES
${SOURCES} ${SOURCES}
src/arch/x86_64/syscall.S src/arch/x86_64/syscall.S
src/arch/x86_64/setjmp.S
) )
endif() endif()

View File

@ -3,7 +3,7 @@
#ifndef _BITS_ATTRS_H #ifndef _BITS_ATTRS_H
#define _BITS_ATTRS_H #define _BITS_ATTRS_H
#if !defined(_STDLIB_H) && !defined(_STRING_H) && !defined(_ASSERT_H) #if !defined(_STDLIB_H) && !defined(_STRING_H) && !defined(_ASSERT_H) && !defined(_SETJMP_H)
#error "Never include bits/attrs.h directly; use one of the standard library headers." #error "Never include bits/attrs.h directly; use one of the standard library headers."
#endif #endif

View File

@ -3,7 +3,7 @@
#ifndef _BITS_FIXED_SIZE_TYPES_H #ifndef _BITS_FIXED_SIZE_TYPES_H
#define _BITS_FIXED_SIZE_TYPES_H #define _BITS_FIXED_SIZE_TYPES_H
#ifndef _SYS_TYPES_H #if !defined(_SYS_TYPES_H) && !defined(_BITS_SETJMP_TYPES_H)
#error "Never include bits/fixed-size-types.h, use the standard <stdint.h> header instead." #error "Never include bits/fixed-size-types.h, use the standard <stdint.h> header instead."
#endif #endif

View File

@ -0,0 +1,13 @@
/* bits/platform.h: Platform defines. */
#ifndef _BITS_PLATFORM_H
#define _BITS_PLATFORM_H
#if defined(__x86_64__)
#define __libc_arch_x86_64
#define __libc_arch_x86
#else
#define __libc_arch_unknown
#endif
#endif

View File

@ -0,0 +1,20 @@
/* bits/setjmp-types.h: Architecture-dependent definitions for jmp_buf/sigjmp_buf. */
#ifndef _BITS_SETJMP_TYPES_H
#define _BITS_SETJMP_TYPES_H
#include <bits/fixed-size-types.h>
#include <bits/platform.h>
#ifndef _SETJMP_H
#error "Never use bits/setjmp-types.h directly; include setjmp.h instead."
#endif
#ifdef __libc_arch_x86_64
typedef __u64_t jmp_buf[8];
typedef __u64_t sigjmp_buf[8];
#else
#error "Unsupported architecture."
#endif
#endif

30
libc/include/setjmp.h Normal file
View File

@ -0,0 +1,30 @@
/* setjmp.h: nonlocal gotos. */
#ifndef _SETJMP_H
#define _SETJMP_H
#include <bits/attrs.h>
#include <bits/setjmp-types.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Saves the current execution state in env. */
int setjmp(jmp_buf env);
/* Right now, does the exact same thing as setjmp() (savesigs is ignored), since signals are not implemented. */
int sigsetjmp(sigjmp_buf env, int savesigs);
/* Restores the execution state saved in env by a setjmp() call. */
__noreturn void longjmp(jmp_buf env, int val);
/* Right now, does the exact same as longjmp(), since signals are not implemented. */
__noreturn void siglongjmp(sigjmp_buf env, int val);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,36 @@
.global _setjmp
.global setjmp
_setjmp:
setjmp:
mov $0, %rsi
mov %rbx, (%rdi)
mov %r12, 0x8(%rdi)
mov %r13, 0x10(%rdi)
mov %r14, 0x18(%rdi)
mov %r15, 0x20(%rdi)
mov %rbp, 0x28(%rdi)
mov %rsp, 0x30(%rdi)
mov (%rsp), %rax
mov %rax, 0x38(%rdi)
xor %rax, %rax
ret
.global _longjmp
.global longjmp
_longjmp:
longjmp:
mov %rsi, %rax
cmp $0, %rax
jne .nonzero
mov $1, %rax
.nonzero:
mov (%rdi), %rbx
mov 0x8(%rdi), %r12
mov 0x10(%rdi), %r13
mov 0x18(%rdi), %r14
mov 0x20(%rdi), %r15
mov 0x28(%rdi), %rbp
mov 0x30(%rdi), %rsp
mov 0x38(%rdi), %rcx
mov %rcx, (%rsp)
ret

14
libc/src/setjmp.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <setjmp.h>
extern "C"
{
int sigsetjmp(sigjmp_buf env, int)
{
return setjmp(env);
}
__noreturn void siglongjmp(sigjmp_buf env, int val)
{
longjmp(env, val);
}
}