Refactor fault handling.

This commit is contained in:
Brad Nelson
2023-01-14 22:28:38 -08:00
parent f8cfaedc84
commit 2f79192ea0
9 changed files with 110 additions and 79 deletions

40
posix/faults.h Normal file
View File

@ -0,0 +1,40 @@
// Copyright 2023 Bradley D. Nelson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <setjmp.h>
#include <signal.h>
static __thread jmp_buf g_forth_fault;
static __thread int g_forth_signal;
#define FAULT_ENTRY \
if (setjmp(g_forth_fault)) { THROWIT(-g_forth_signal); }
static void forth_signal_handler(int sig) {
g_forth_signal = sig;
sigset_t ss;
sigemptyset(&ss);
sigprocmask(SIG_SETMASK, &ss, 0);
longjmp(g_forth_fault, 1);
}
static void forth_faults_setup(void) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = forth_signal_handler;
sigaction(SIGSEGV, &sa, 0);
sigaction(SIGBUS, &sa, 0);
sigaction(SIGINT, &sa, 0);
sigaction(SIGFPE, &sa, 0);
}

View File

@ -34,6 +34,7 @@
#include "common/bits.h"
#include "common/core.h"
#include "posix/faults.h"
#include "common/interp.h"
#include "gen/posix_boot.h"