feat(hosted): model a tagging profile's tag storage statistics and remote pointer tags over untagged hosted memory

This commit is contained in:
Tyler Smith committed 2026-10-20 15:07:21 +00:00
1 parent 61fa7c3973
commit 4e365e36eb
2 files changed
+179 -30

No files matched your search

@@ -1507,9 +1507,18 @@ const HOST_VM_INFO64: u32 = 4;
const VM_INFO_PAGE_WORDS: [usize; 6] = [0, 1, 2, 3, 12, 14];
const VM_INFO64_PAGE_WORDS: [usize; 10] = [0, 1, 2, 3, 22, 23, 32, 33, 34, 35];
const VM_INFO64_PAGE_PAIRS: [usize; 2] = [36, 38];
/// Rev3's tag storage and tagged-page statistics, from this word on: zero,
/// the host memory's.
/// Rev3's tag storage and tagged-page statistics, eleven 64-bit counts
/// from this word on: the total tag storage pages, those holding pageable
/// and wired non-tag data, the free ones and those storing tags, then the
/// tagged pages (virtual, resident, compressed), tagged compressions and
/// decompressions and the compressed tag bytes.
const VM_INFO64_TAGGING: usize = 40;
const VM_INFO64_TAG_STORAGE_FREE: usize = 46;
/// `HOST_VM_INFO64_COUNT` of rev3.
const VM_INFO64_REV3_COUNT: usize = 62;
/// Memory per byte of tag storage: a 4-bit allocation tag per 16-byte
/// granule (Arm ARM, FEAT_MTE2).
const TAG_STORAGE_RATIO: u64 = 32;
/// Host pages per guest page.
const HOST_PAGES_PER_PAGE: u32 = (memory::PAGE_BYTES / (memory::HOST_PAGE_MASK + 1)) as u32;
@@ -1530,8 +1539,8 @@ fn restate_load(words: &mut [u32], cpus: u32) {
/// machine's answer (machine accounting), the same layout in both ABIs,
/// with its page counts restated in the guest's 16 KiB pages, as an arm64
/// kernel counts them, and its Mach factors over the profile's
/// processors. The host's memory has no tag storage, so rev3's tag
/// statistics are zero on every profile.
/// processors. Rev3's tag statistics are the profile's
/// ([`tag_storage_pages`]).
fn host_statistics(
hardware: &hardware::Hardware,
sixty_four: bool,
@@ -1566,7 +1575,12 @@ fn host_statistics(
.iter()
.map(|&word| word as u32)
.collect();
restate_pages(&mut words, flavor);
if flavor == HOST_VM_INFO64 && count as usize >= VM_INFO64_REV3_COUNT {
// The arm64 kernel always keeps rev3's words, which the profile
// answers whether or not the host kernel has them.
words.resize(words.len().max(VM_INFO64_REV3_COUNT), 0);
}
restate_pages(&mut words, flavor, tag_storage_pages(hardware));
if flavor == HOST_LOAD_INFO {
restate_load(&mut words, hardware.cpus().into());
}
@@ -1577,8 +1591,22 @@ fn host_statistics(
Ok(Reply::Success(body))
}
/// Restate the page counts of a `flavor` statistics reply in guest pages.
fn restate_pages(words: &mut [u32], flavor: u32) {
/// The tag storage of the hosted machine's memory, in guest pages: on a
/// profile with memory tagging, the memory's share holding its allocation
/// tags, none of it storing tags or reused for other data, since no page
/// of the hosted machine is tagged (`VM_FLAGS_MTE` memory is refused);
/// without memory tagging, none.
fn tag_storage_pages(hardware: &hardware::Hardware) -> u64 {
if hardware.memory_tagging {
hardware.memory_bytes / TAG_STORAGE_RATIO / memory::PAGE_BYTES
} else {
0
}
}
/// Restate the page counts of a `flavor` statistics reply in guest pages,
/// with `tag_storage` free tag storage pages.
fn restate_pages(words: &mut [u32], flavor: u32, tag_storage: u64) {
let pages: &[usize] = match flavor {
HOST_VM_INFO => &VM_INFO_PAGE_WORDS,
HOST_VM_INFO64 => &VM_INFO64_PAGE_WORDS,
@@ -1603,6 +1631,12 @@ fn restate_pages(words: &mut [u32], flavor: u32) {
if let Some(tagging) = words.get_mut(VM_INFO64_TAGGING..) {
tagging.fill(0);
}
for index in [VM_INFO64_TAGGING, VM_INFO64_TAG_STORAGE_FREE] {
if let Some(pair) = words.get_mut(index..index + 2) {
pair[0] = tag_storage as u32;
pair[1] = (tag_storage >> 32) as u32;
}
}
}
/// `host_info_t`'s MIG bound (`array[*:68] of integer_t`).
@@ -1752,18 +1786,18 @@ mod tests {
#[test]
fn vm_statistics_count_guest_pages() {
let mut vm = vec![400u32; 15];
restate_pages(&mut vm, HOST_VM_INFO);
restate_pages(&mut vm, HOST_VM_INFO, 0);
assert_eq!(&vm[..5], &[100, 100, 100, 100, 400]);
assert_eq!((vm[12], vm[13], vm[14]), (100, 400, 100));
let mut vm64 = vec![8u32; 62];
vm64[36] = 0;
vm64[37] = 4;
restate_pages(&mut vm64, HOST_VM_INFO64);
restate_pages(&mut vm64, HOST_VM_INFO64, 0);
assert_eq!(&vm64[..5], &[2, 2, 2, 2, 8]);
assert_eq!(&vm64[32..42], &[2, 2, 2, 2, 0, 1, 2, 2, 0, 0]);
assert!(vm64[40..].iter().all(|&word| word == 0));
let mut load = vec![400u32; 6];
restate_pages(&mut load, 1);
restate_pages(&mut load, 1, 0);
assert_eq!(load, [400; 6]);
// The host answers the load and VM flavors, rev3's tag storage
// zero on every profile.
@@ -1784,9 +1818,19 @@ mod tests {
.iter()
.map(|word| u32::from_le_bytes(*word))
.collect();
assert!(words[0] >= 38);
assert!(words[1 + VM_INFO64_TAGGING..].iter().all(|&word| word == 0));
assert_eq!(words[0], VM_INFO64_REV3_COUNT as u32);
let storage = tag_storage_pages(profile);
let tags = &words[1 + VM_INFO64_TAGGING..];
for (index, pair) in tags.as_chunks::<2>().0.iter().enumerate() {
let value = u64::from(pair[0]) | u64::from(pair[1]) << 32;
// The total and the free tag storage.
let expected = if matches!(index, 0 | 3) { storage } else { 0 };
assert_eq!(value, expected, "tag statistic {index}");
}
}
assert_eq!(tag_storage_pages(&hardware), 0);
// 16 GiB holds 512 MiB of tags: 32768 guest pages.
assert_eq!(tag_storage_pages(&tagging), 0x8000);
}
#[test]
+123 -18
View File
@@ -131,10 +131,7 @@ pub(crate) const SERVED: &[(&str, Option<&str>)] = &[
("_kernelrpc_mach_vm_purgable_control_external", None),
("mach_vm_machine_attribute", None),
("mach_vm_range_create", None),
(
"_kernelrpc_mach_vm_update_pointers_with_remote_tags",
Some("on a profile with memory tagging the request stops the run"),
),
("_kernelrpc_mach_vm_update_pointers_with_remote_tags", None),
];
/// Routines whose request carries descriptors (`MACH_MSGH_BITS_COMPLEX`).
@@ -199,10 +196,7 @@ fn route(
"_kernelrpc_mach_vm_update_pointers_with_remote_tags" if !task.memory_tagging() => {
Reply::Forward
}
"_kernelrpc_mach_vm_update_pointers_with_remote_tags" => bail!(
"mach_vm_update_pointers_with_remote_tags: remote tags of a profile with memory \
tagging are not modeled"
),
"_kernelrpc_mach_vm_update_pointers_with_remote_tags" => remote_tags(task, request),
_ => unreachable!("every served routine is routed"),
}))
}
@@ -225,6 +219,10 @@ trait Task {
fn invalidate(&self, address: u64, bytes: u64);
/// Whether the profile's chip implements memory tagging.
fn memory_tagging(&self) -> bool;
/// Whether this task's address space is debugged
/// (`address_space_debugged_state`): developer mode is on and its code
/// signature is marked debugged (`CS_DEBUGGED`).
fn address_space_debugged(&self) -> bool;
}
/// A registry identifier no thread has (the service threads' own), so
@@ -279,6 +277,75 @@ impl Task for Kernel {
fn memory_tagging(&self) -> bool {
self.process.hardware.memory_tagging
}
fn address_space_debugged(&self) -> bool {
lock(&self.process.executable).code_signing_flags & CS_DEBUGGED != 0 && developer_mode()
}
}
/// `CS_DEBUGGED` (`osfmk/kern/cs_blobs.h`).
const CS_DEBUGGED: u32 = 0x1000_0000;
/// Whether developer mode is on: the host kernel's state, which is the
/// kernel beneath this process (`security.mac.amfi.developer_mode_status`).
fn developer_mode() -> bool {
let mut status = 0i32;
let mut size = size_of::<i32>();
// SAFETY: a NUL-terminated name and an out argument of `size` bytes.
let code = unsafe {
libc::sysctlbyname(
c"security.mac.amfi.developer_mode_status".as_ptr(),
(&raw mut status).cast(),
&raw mut size,
std::ptr::null_mut(),
0,
)
};
code == 0 && status != 0
}
/// `VM_OFFSET_LIST_MAX`, MIG's bound on a pointer list.
const VM_OFFSET_LIST_MAX: u32 = 1024;
/// A pointer's allocation tag, bits 59:56 (`MTE_TAG_SHIFT`).
const ALLOCATION_TAG: u64 = 0xf << 56;
/// `mach_vm_update_pointers_with_remote_tags(in_pointer_listCnt,
/// in_pointer_list, out_pointer_listCnt)` on a profile with memory
/// tagging: for a debugged address space, each pointer with its memory's
/// allocation tag, which is 0 for every page of the hosted machine (no
/// memory is tagged: `VM_FLAGS_MTE` is refused), and a zero slot for an
/// address that cannot be read; otherwise KERN_INVALID_ARGUMENT.
fn remote_tags(task: &dyn Task, request: &Request) -> Reply {
let Some(count) = request
.word(SIMPLE)
.filter(|&count| count <= VM_OFFSET_LIST_MAX)
else {
return Reply::Failure(MIG_BAD_ARGUMENTS);
};
let pointers: Option<Vec<u64>> = (0..count as usize)
.map(|index| request.double(SIMPLE + 4 + index * 8))
.collect();
let out_at = SIMPLE + 4 + count as usize * 8;
let (Some(pointers), Some(out_count)) = (pointers, request.word(out_at)) else {
return Reply::Failure(MIG_BAD_ARGUMENTS);
};
if request.body.len() != out_at + 4 {
return Reply::Failure(MIG_BAD_ARGUMENTS);
}
// The stub bounds the out count; the kernel wants it to match.
if out_count.min(VM_OFFSET_LIST_MAX) != count || !task.address_space_debugged() {
return Reply::Failure(KERN_INVALID_ARGUMENT);
}
let mut body = count.to_le_bytes().to_vec();
for pointer in pointers {
let tagged = if memory::read(pointer & !ALLOCATION_TAG, 1).is_some() {
pointer & !ALLOCATION_TAG
} else {
0
};
body.extend(tagged.to_le_bytes());
}
Reply::Success(body)
}
const KERN_SUCCESS: i32 = 0;
@@ -1869,9 +1936,10 @@ mod tests {
}
/// This process as a hosted task without a shared region, on a profile
/// with or without memory tagging.
/// with or without memory tagging, debugged or not.
struct Bare {
memory_tagging: bool,
debugged: bool,
}
impl Task for Bare {
@@ -1879,6 +1947,10 @@ mod tests {
self.memory_tagging
}
fn address_space_debugged(&self) -> bool {
self.debugged
}
fn port(&self) -> u32 {
host()
}
@@ -1902,28 +1974,61 @@ mod tests {
fn bare() -> Bare {
Bare {
memory_tagging: false,
debugged: false,
}
}
fn remote_tags_body(pointers: &[u64], out_count: u32) -> Vec<u8> {
let mut body = simple(&[&(pointers.len() as u32).to_le_bytes()]);
body.extend(pointers.iter().flat_map(|word| word.to_le_bytes()));
body.extend(out_count.to_le_bytes());
body
}
#[test]
fn remote_tags_follow_the_profile() {
// Two pointers in, two slots out.
let mut body = simple(&[&2u32.to_le_bytes()]);
body.extend(
[0x1000u64, 0x2000]
.iter()
.flat_map(|word| word.to_le_bytes()),
);
body.extend(2u32.to_le_bytes());
let routine = "_kernelrpc_mach_vm_update_pointers_with_remote_tags";
let mapped = allocate_pages(&mut bare(), 1);
// A tagged pointer to mapped memory, then an unmapped one.
let pointers = [mapped | 0x0700_0000_0000_0000 | 0x18, 0x10];
let body = remote_tags_body(&pointers, 2);
assert!(matches!(
call(&mut bare(), routine, false, &body).unwrap(),
Reply::Forward
));
let mut tagging = Bare {
memory_tagging: true,
debugged: false,
};
assert!(call(&mut tagging, routine, false, &body).is_err());
// Only a debugged address space is answered.
assert_eq!(
failure(call(&mut tagging, routine, false, &body).unwrap()),
KERN_INVALID_ARGUMENT
);
tagging.debugged = true;
let reply = success(call(&mut tagging, routine, false, &body).unwrap());
assert_eq!(u32::from_le_bytes(reply[..4].try_into().unwrap()), 2);
assert_eq!(u64_at(&reply, 4), mapped | 0x18);
assert_eq!(u64_at(&reply, 12), 0);
// The out count must match the in count.
assert_eq!(
failure(
call(
&mut tagging,
routine,
false,
&remote_tags_body(&pointers, 1)
)
.unwrap()
),
KERN_INVALID_ARGUMENT
);
assert_eq!(
failure(call(&mut tagging, routine, false, &body[..body.len() - 2]).unwrap()),
MIG_BAD_ARGUMENTS
);
// SAFETY: the page this test allocated.
unsafe { mach_vm_deallocate(host(), mapped, PAGE_BYTES) };
}
fn simple(fields: &[&[u8]]) -> Vec<u8> {