Instead of treating illegal instructions encountered by the guest as
unhandled bogus exits, reflect the trap back into the VS-mode. Set up
the VS-mode CSRs (vsstatus, vsepc, vscause, and vstval) and redirect
the program counter to vstvec so the guest kernel can handle the
exception itself.
Details
Details
Diff Detail
Diff Detail
- Repository
- rG FreeBSD src repository
- Lint
Lint Skipped - Unit
Tests Skipped - Build Status
Buildable 78359 Build 75242: arc lint + arc unit
Event Timeline
Comment Actions
Test App
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Test program to verify illegal instruction exception trapping and SIGILL
* signal delivery within a FreeBSD RISC-V bhyve guest.
*/
#include <sys/types.h>
#include <setjmp.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Global variables to avoid compiler optimizing away values across sigsetjmp */
sigjmp_buf jmpbuf;
volatile sig_atomic_t g_sigill_received = 0;
volatile void *g_fault_addr = NULL;
volatile int g_fault_code = 0;
static void
sigill_handler(int sig, siginfo_t *info, void *ucontext __unused)
{
if (sig == SIGILL) {
g_sigill_received = 1;
if (info != NULL) {
g_fault_addr = info->si_addr;
g_fault_code = info->si_code;
}
siglongjmp(jmpbuf, 1);
}
}
int
main(void)
{
struct sigaction sa;
void *expected_pc = &&illegal_insn_label;
printf("============================================================\n");
printf(" FreeBSD RISC-V Bhyve: Illegal Instruction Trap Test\n");
printf("============================================================\n");
/* Install SIGILL signal handler with SA_SIGINFO */
sa.sa_sigaction = sigill_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
if (sigaction(SIGILL, &sa, NULL) != 0) {
perror("sigaction");
return (1);
}
printf("[1] Installed SIGILL signal handler.\n");
if (sigsetjmp(jmpbuf, 1) == 0) {
printf("[2] Triggering illegal instruction (unimp / 0x0000)...\n");
illegal_insn_label:
__asm__ __volatile__(".insn 2, 0x0000");
/* If the CPU did not trap, we reach here (failure) */
printf("[-] ERROR: Illegal instruction did not trigger a trap!\n");
return (1);
}
/* Returned via siglongjmp from the signal handler */
printf("[3] Successfully trapped into signal handler!\n");
printf(" Signal received: %d (SIGILL)\n", (int)g_sigill_received);
printf(" Faulting PC: %p (expected: %p)\n", g_fault_addr, expected_pc);
printf(" Signal Code: %d\n", g_fault_code);
if (g_sigill_received == 1 && g_fault_addr == expected_pc) {
printf("\n[+] SUCCESS: Illegal instruction correctly trapped in hypervisor,\n");
printf(" injected into guest VS-mode, and delivered as SIGILL to userland!\n");
printf(" Fault PC exactly matched the illegal instruction address!\n");
printf("============================================================\n");
return (0);
}
if (g_sigill_received == 1) {
printf("\n[+] SUCCESS: SIGILL caught! (Fault PC: %p, Expected: %p)\n",
g_fault_addr, expected_pc);
printf("============================================================\n");
return (0);
}
printf("[-] FAILED: SIGILL was not properly captured.\n");
return (1);
}Comment Actions
TEST LOG:
root@freebsd-riscv-guest:~ # /bin/test_sigill
============================================================
FreeBSD RISC-V Bhyve: Illegal Instruction Trap Test
============================================================
[1] Installed SIGILL signal handler.
[2] Triggering illegal instruction (unimp / 0x0000)...
[3] Successfully trapped into signal handler!
Signal received: 1 (SIGILL)
Faulting PC: 0x3cbe8 (expected: 0x3cbe8)
Signal Code: 4
[+] SUCCESS: Illegal instruction correctly trapped in hypervisor,
injected into guest VS-mode, and delivered as SIGILL to userland!
Fault PC exactly matched the illegal instruction address!
============================================================