Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/freebsd/sys/_ucontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef struct __ucontext {
* note: the union is not defined, though.
*/
__sigset_t uc_sigmask;
/* PS5 signal frames place uc_mcontext at offset 64 (observed on 12.09). */
int __ps5_reserved[12];
mcontext_t uc_mcontext;

struct __ucontext *uc_link;
Expand Down
48 changes: 48 additions & 0 deletions samples/test_signal_context/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (C) 2023 John Törnblom
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not see
# <http://www.gnu.org/licenses/>.

PS5_HOST ?= ps5
PS5_PORT ?= 9021

ifdef PS5_PAYLOAD_SDK
include $(PS5_PAYLOAD_SDK)/toolchain/prospero.mk
else
$(error PS5_PAYLOAD_SDK is undefined)
endif

ELF := test_signal_context.elf

CFLAGS := -Wall -Werror -g

all: $(ELF)

$(ELF): main.c
$(CC) $(CFLAGS) -o $@ $^

clean:
rm -f $(ELF)

test: $(ELF)
$(PS5_DEPLOY) -h $(PS5_HOST) -p $(PS5_PORT) $^

debug: $(ELF)
gdb-multiarch \
-ex "set architecture i386:x86-64" \
-ex "target extended-remote $(PS5_HOST):2159" \
-ex "file $(ELF)" \
-ex "remote put $(ELF) /data/$(ELF)" \
-ex "set remote exec-file /data/$(ELF)" \
-ex "start"
64 changes: 64 additions & 0 deletions samples/test_signal_context/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright (C) 2026 Mihawk
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <inttypes.h>
#include <setjmp.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <ucontext.h>

_Static_assert(offsetof(ucontext_t, uc_mcontext) == 64, "PS5 mcontext offset");
_Static_assert(offsetof(ucontext_t, uc_mcontext.mc_rip) == 224, "PS5 RIP offset");

static sigjmp_buf escape;
static volatile uint64_t saved_rax;
static volatile uintptr_t saved_rip;
extern void context_trigger(void);
extern const char context_fault[];

/* UD2 gives a known faulting instruction and a caller-saved register marker. */
__asm__(".text\n"
".global context_trigger\n"
".type context_trigger,@function\n"
"context_trigger:\n"
"movabs $0x5a17000000001234, %rax\n"
".global context_fault\n"
"context_fault:\n"
"ud2\n"
"ret\n");

static void
handle_fault(int signal_number, siginfo_t *info, void *opaque) {
const ucontext_t *context = opaque;
(void)signal_number;
(void)info;
saved_rax = context->uc_mcontext.mc_rax;
saved_rip = context->uc_mcontext.mc_rip;
siglongjmp(escape, 1);
}

int
main(void) {
struct sigaction action = {0}, previous;
action.sa_sigaction = handle_fault;
action.sa_flags = SA_SIGINFO;
sigemptyset(&action.sa_mask);
if(sigaction(SIGILL, &action, &previous) != 0) {
perror("sigaction");
return 1;
}
if(sigsetjmp(escape, 1) == 0) {
context_trigger();
}
if(sigaction(SIGILL, &previous, NULL) != 0) {
perror("restore sigaction");
return 1;
}
printf("RAX=%016" PRIx64 " RIP=%" PRIxPTR " expected RIP=%" PRIxPTR "\n",
saved_rax, saved_rip, (uintptr_t)context_fault);
return saved_rax != UINT64_C(0x5a17000000001234) ||
saved_rip != (uintptr_t)context_fault;
}