summaryrefslogtreecommitdiff
path: root/src/agent/signal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent/signal.zig')
-rw-r--r--src/agent/signal.zig97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/agent/signal.zig b/src/agent/signal.zig
index 66baf2d..9a14222 100644
--- a/src/agent/signal.zig
+++ b/src/agent/signal.zig
@@ -51,6 +51,103 @@ pub const Ucontext = extern struct {
}
};
+/// What a handler has to take back before it acts for the thread it
+/// interrupted. The kernel starts every handler with defaults (rights to
+/// memory behind protection keys: key 0 only; floating-point modes: the
+/// initial ones) and keeps the thread's own in the frame until the handler
+/// returns. A syscall made for the thread, a thread started for it, its
+/// memory read for it: all would happen as somebody else.
+pub fn adopt(uc: *const Ucontext) void {
+ if (uc.fpstate == 0) return;
+ const area: [*]const u8 = @ptrFromInt(uc.fpstate);
+ // FXSAVE: the x87 control word first, MXCSR at 24.
+ asm volatile (
+ \\ fldcw (%[area])
+ \\ ldmxcsr 24(%[area])
+ :
+ : [area] "r" (area),
+ );
+ if (keys.inFrame(uc)) |in| keys.set(if (in.present.* & keys.component != 0) in.pkru.* else 0);
+}
+
+/// Memory protection keys: a thread's rights to protected memory are a
+/// register, PKRU, two bits a key.
+pub const keys = struct {
+ /// Where XSAVE keeps PKRU; 0 when the CPU or the kernel has no protection keys.
+ var offset: u32 = 0;
+ /// PKRU's bit among XSAVE's state components.
+ const component: u64 = 1 << 9;
+
+ /// Once, before any handler is installed.
+ pub fn detect() void {
+ // Leaf 7: the kernel has turned the keys on (OSPKE). Leaf 0xD, component 9: where PKRU is saved.
+ if (cpuid(7, 0)[2] & (1 << 4) != 0) offset = cpuid(0xd, 9)[1];
+ }
+
+ /// eax, ebx, ecx, edx.
+ fn cpuid(leaf: u32, subleaf: u32) [4]u32 {
+ var eax: u32 = undefined;
+ var ebx: u32 = undefined;
+ var ecx: u32 = undefined;
+ var edx: u32 = undefined;
+ asm volatile ("cpuid"
+ : [a] "={eax}" (eax),
+ [b] "={ebx}" (ebx),
+ [c] "={ecx}" (ecx),
+ [d] "={edx}" (edx),
+ : [leaf] "{eax}" (leaf),
+ [subleaf] "{ecx}" (subleaf),
+ );
+ return .{ eax, ebx, ecx, edx };
+ }
+
+ pub fn get() u32 {
+ return asm volatile ("rdpkru"
+ : [pkru] "={eax}" (-> u32),
+ : [zero] "{ecx}" (@as(u32, 0)),
+ : .{ .edx = true });
+ }
+
+ pub fn set(pkru: u32) void {
+ asm volatile ("wrpkru"
+ :
+ : [pkru] "{eax}" (pkru),
+ [c] "{ecx}" (@as(u32, 0)),
+ [d] "{edx}" (@as(u32, 0)),
+ );
+ }
+
+ /// Every key's memory readable and writable to this thread, for the
+ /// agent's own look at what is watched or hooked. Returns what to `set`
+ /// back; null without protection keys.
+ pub fn open() ?u32 {
+ if (offset == 0) return null;
+ const was = get();
+ set(0);
+ return was;
+ }
+
+ /// The interrupted thread's PKRU in its signal frame, and the header
+ /// word that says whether it is there at all (absent, it is 0).
+ fn inFrame(uc: *const Ucontext) ?struct { pkru: *align(1) u32, present: *align(1) u64 } {
+ if (offset == 0 or uc.fpstate == 0) return null;
+ const area: [*]u8 = @ptrFromInt(uc.fpstate);
+ // After FXSAVE's 512 bytes comes XSAVE's header, when the kernel says so at 464.
+ const extended: *align(1) const extern struct { magic: u32, size: u32, features: u64, state_size: u32 } = @ptrCast(area + 464);
+ if (extended.magic != 0x46505853 or extended.features & component == 0 or extended.state_size < offset + 4) return null;
+ return .{ .pkru = @ptrCast(area + offset), .present = @ptrCast(area + 512) };
+ }
+
+ /// The rights as they are now are the thread's from the handler's
+ /// return on: a pkey_alloc made in the handler changed them here, and
+ /// the frame would put the old ones back.
+ pub fn keep(uc: *const Ucontext) void {
+ const in = inFrame(uc) orelse return;
+ in.pkru.* = get();
+ in.present.* |= component;
+ }
+};
+
/// The trap flag: one instruction, then SIGTRAP.
pub const trap_flag = 0x100;