libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_pkey.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * @stability experimental
4 * Use of this source code is governed by the ISC License.
5 *
6 * src/inc/xtc_pkey.h
7 * Optional memory-protection-key (PKU) tier for in-process memory
8 * isolation (isolation tier c). On Linux/x86 with PKU, a region
9 * can be tagged with a protection key and a thread can then disable
10 * its own access/write to that key's pages in O(1) (no syscall on
11 * the toggle, via the PKRU register), giving coarse, domain-based
12 * in-process isolation -- a guest can be denied access to host
13 * pages without a separate address space.
14 *
15 * This is a hardening tier, NOT a portable guarantee: keys are
16 * scarce (16 on x86), it is Linux/x86-only (ARMv8.5 POE is newer
17 * and not wired here), and there is no macOS/Windows equivalent.
18 * Every call returns XTC_E_NOSYS where PKU is unavailable; probe
19 * with xtc_pkey_supported() first. For real, portable isolation
20 * use process-per-task (xtc_osproc).
21 */
22
23#ifndef XTC_PKEY_H
24#define XTC_PKEY_H
25
26#include "xtc_export.h"
27
28#include <stddef.h>
29
30#include "xtc.h"
31
32/*
33 * PUBLIC: int xtc_pkey_supported __P((void));
34 * PUBLIC: int xtc_pkey_alloc __P((int *));
35 * PUBLIC: int xtc_pkey_protect __P((void *, size_t, int));
36 * PUBLIC: int xtc_pkey_set_access __P((int, int, int));
37 * PUBLIC: int xtc_pkey_free __P((int));
38 */
39
40/* 1 if protection keys are usable on this host right now, else 0. */
41XTC_API int xtc_pkey_supported(void);
42
43/* Allocate a protection key (initially full access). XTC_OK + *out,
44 * or XTC_E_NOSYS / XTC_E_INVAL. */
45XTC_API int xtc_pkey_alloc(int *out_key);
46
47/* Tag [addr,addr+len) (page-aligned) with key, keeping read+write
48 * protection bits. Access is then gated by the key's per-thread
49 * rights (xtc_pkey_set_access). */
50XTC_API int xtc_pkey_protect(void *addr, size_t len, int key);
51
52/* Set the CALLING thread's rights for key: allow_read/allow_write
53 * (0 disables). Disabling read implies disabling write. */
54XTC_API int xtc_pkey_set_access(int key, int allow_read, int allow_write);
55
56/* Release a key. */
57XTC_API int xtc_pkey_free(int key);
58
59#endif /* XTC_PKEY_H */