|
| 1 | +/******************************************************************************/ |
| 2 | +/* Mednafen Sega Saturn Emulation Module */ |
| 3 | +/******************************************************************************/ |
| 4 | +/* jitdump.c - shared Linux perf jitdump writer for the DSP JITs |
| 5 | +** Copyright (C) 2026 pstef |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "jitdump.h" |
| 9 | + |
| 10 | +#if defined(WANT_DSP_JIT_PERF_DUMP) && (defined(__aarch64__) || defined(__arm64__)) |
| 11 | + |
| 12 | +/* |
| 13 | + * Perf jitdump writer. Produces /tmp/jit-<pid>.dump in the Linux perf |
| 14 | + * jitdump v1 format (see kernel docs Documentation/admin-guide/perf/...). |
| 15 | + * `perf record` captures the marker mmap, then `perf inject --jit` reads |
| 16 | + * the dump and emits ELF stubs so `perf report` resolves samples landing |
| 17 | + * in our code segment to per-slot symbols. |
| 18 | + * |
| 19 | + * Shared between the SCU and SCSP DSP JITs. Both backends compile under |
| 20 | + * their respective subsystem locks on the same emulator thread, so the |
| 21 | + * single fd / index counter don't need any explicit serialization here. |
| 22 | + * The atexit handler fires after that thread exits. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdint.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <string.h> |
| 29 | +#include <fcntl.h> |
| 30 | +#include <sys/mman.h> |
| 31 | +#include <sys/syscall.h> |
| 32 | +#include <sys/uio.h> |
| 33 | +#include <time.h> |
| 34 | +#include <unistd.h> |
| 35 | + |
| 36 | +#define JITDUMP_MAGIC 0x4A695444u /* "JiTD" */ |
| 37 | +#define JITDUMP_VERSION 1u |
| 38 | +#define JIT_CODE_LOAD 0u |
| 39 | +#define JIT_CODE_CLOSE 3u |
| 40 | +#define ELF_MACH_AARCH64 183u |
| 41 | + |
| 42 | +struct JitdumpHeader |
| 43 | +{ |
| 44 | + uint32_t magic; |
| 45 | + uint32_t version; |
| 46 | + uint32_t total_size; |
| 47 | + uint32_t elf_mach; |
| 48 | + uint32_t pad1; |
| 49 | + uint32_t pid; |
| 50 | + uint64_t timestamp; |
| 51 | + uint64_t flags; |
| 52 | +}; |
| 53 | + |
| 54 | +struct JitdumpRecPrefix |
| 55 | +{ |
| 56 | + uint32_t id; |
| 57 | + uint32_t total_size; |
| 58 | + uint64_t timestamp; |
| 59 | +}; |
| 60 | + |
| 61 | +struct JitdumpRecCodeLoad |
| 62 | +{ |
| 63 | + struct JitdumpRecPrefix p; |
| 64 | + uint32_t pid; |
| 65 | + uint32_t tid; |
| 66 | + uint64_t vma; |
| 67 | + uint64_t code_addr; |
| 68 | + uint64_t code_size; |
| 69 | + uint64_t code_index; |
| 70 | + /* followed by NUL-terminated name, then code_size bytes of code */ |
| 71 | +}; |
| 72 | + |
| 73 | +static int g_jitdump_fd = -1; |
| 74 | +static void* g_jitdump_marker = NULL; |
| 75 | +static size_t g_jitdump_marker_size = 0; |
| 76 | +static uint64_t g_jitdump_index = 0; |
| 77 | + |
| 78 | +static uint64_t jitdump_now_ns(void) |
| 79 | +{ |
| 80 | + struct timespec ts; |
| 81 | + clock_gettime(CLOCK_MONOTONIC, &ts); |
| 82 | + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 83 | +} |
| 84 | + |
| 85 | +static void jitdump_close(void) |
| 86 | +{ |
| 87 | + struct JitdumpRecPrefix close_rec = {0}; |
| 88 | + if(g_jitdump_fd < 0) return; |
| 89 | + close_rec.id = JIT_CODE_CLOSE; |
| 90 | + close_rec.total_size = sizeof(close_rec); |
| 91 | + close_rec.timestamp = jitdump_now_ns(); |
| 92 | + (void)write(g_jitdump_fd, &close_rec, sizeof(close_rec)); |
| 93 | + if(g_jitdump_marker) (void)munmap(g_jitdump_marker, g_jitdump_marker_size); |
| 94 | + (void)close(g_jitdump_fd); |
| 95 | + g_jitdump_fd = -1; |
| 96 | + g_jitdump_marker = NULL; |
| 97 | + g_jitdump_marker_size = 0; |
| 98 | +} |
| 99 | + |
| 100 | +void SS_JitDump_Open(void) |
| 101 | +{ |
| 102 | + char path[64]; |
| 103 | + int fd; |
| 104 | + struct JitdumpHeader hdr = {0}; |
| 105 | + long pagesz; |
| 106 | + |
| 107 | + if(g_jitdump_fd >= 0) return; |
| 108 | + snprintf(path, sizeof(path), "/tmp/jit-%d.dump", (int)getpid()); |
| 109 | + fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644); |
| 110 | + if(fd < 0) return; |
| 111 | + |
| 112 | + hdr.magic = JITDUMP_MAGIC; |
| 113 | + hdr.version = JITDUMP_VERSION; |
| 114 | + hdr.total_size = sizeof(hdr); |
| 115 | + hdr.elf_mach = ELF_MACH_AARCH64; |
| 116 | + hdr.pid = (uint32_t)getpid(); |
| 117 | + hdr.timestamp = jitdump_now_ns(); |
| 118 | + if(write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) |
| 119 | + { |
| 120 | + (void)close(fd); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + /* The marker mmap is what `perf record` sees in its MMAP events; that |
| 125 | + * is how `perf inject --jit` discovers our dump file. One page of |
| 126 | + * PROT_READ|PROT_EXEC at file offset 0 is the documented contract. */ |
| 127 | + pagesz = sysconf(_SC_PAGESIZE); |
| 128 | + g_jitdump_marker_size = (pagesz > 0) ? (size_t)pagesz : 4096u; |
| 129 | + g_jitdump_marker = mmap(NULL, g_jitdump_marker_size, |
| 130 | + PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); |
| 131 | + if(g_jitdump_marker == MAP_FAILED) g_jitdump_marker = NULL; |
| 132 | + |
| 133 | + g_jitdump_fd = fd; |
| 134 | + atexit(jitdump_close); |
| 135 | +} |
| 136 | + |
| 137 | +void SS_JitDump_Emit(const char* name, const void* code_addr, size_t code_size) |
| 138 | +{ |
| 139 | + size_t name_len; |
| 140 | + struct JitdumpRecCodeLoad rec = {0}; |
| 141 | + struct iovec iov[3]; |
| 142 | + |
| 143 | + if(g_jitdump_fd < 0 || !code_addr || code_size == 0) return; |
| 144 | + |
| 145 | + name_len = strlen(name) + 1; |
| 146 | + rec.p.id = JIT_CODE_LOAD; |
| 147 | + rec.p.total_size = (uint32_t)(sizeof(rec) + name_len + code_size); |
| 148 | + rec.p.timestamp = jitdump_now_ns(); |
| 149 | + rec.pid = (uint32_t)getpid(); |
| 150 | + rec.tid = (uint32_t)syscall(SYS_gettid); |
| 151 | + rec.vma = (uint64_t)(uintptr_t)code_addr; |
| 152 | + rec.code_addr = rec.vma; |
| 153 | + rec.code_size = code_size; |
| 154 | + rec.code_index = ++g_jitdump_index; |
| 155 | + |
| 156 | + iov[0].iov_base = &rec; |
| 157 | + iov[0].iov_len = sizeof(rec); |
| 158 | + iov[1].iov_base = (char*)name; |
| 159 | + iov[1].iov_len = name_len; |
| 160 | + iov[2].iov_base = (void*)code_addr; |
| 161 | + iov[2].iov_len = code_size; |
| 162 | + (void)writev(g_jitdump_fd, iov, 3); |
| 163 | +} |
| 164 | + |
| 165 | +#endif |
0 commit comments