This series is about CVE-2025-43520 in XNU, the kernel behind iOS and macOS. Parts 2 and 3 take the issue apart. This first part is background only: it builds the mental model of memory that the later parts lean on and says nothing yet about what goes wrong. It is written for a reader who knows C and has met virtual memory in an operating systems course but has never opened a kernel source tree. All references are to the public XNU source at tag xnu-10002.41.9.

The tour has five stops: what a virtual address is, how XNU describes an address space, how it tracks physical pages, how device memory enters the same system, and how I/O code asks the virtual memory layer for a list of pages.

An address is not a place in RAM

When a program dereferences a pointer, the number in it is a virtual address. It names a slot in the process’s private view of memory, not a cell in the RAM chips. Between the two sits the memory management unit (MMU), which translates every load and store from a virtual address to a physical one.

Four adjacent virtual pages map to four physical frames that are not adjacent in RAM.

The MMU works on pages: fixed-size, aligned blocks of address space that map to equally sized blocks of physical memory, called frames. On Apple’s arm64 chips the page is 16 KiB: when the SoC header defines __ARM_16K_PG__, the page shift is 14, otherwise 12 (osfmk/arm64/proc_reg.h:1201-1206). The low 14 bits of an address are the offset inside the page and pass through untouched; the bits above them pick the page.

The translation lives in memory, in a tree of translation tables. Each table is one page of 2048 eight-byte entries, so each level resolves 11 bits of the address. A user address space on Apple SoCs is 39 bits wide, so the hardware walk starts at the level XNU calls L1, where one entry covers 64 GiB. An L2 entry covers 32 MiB, and an L3 entry, the leaf, maps one 16 KiB page. Inner entries point at the next table; a leaf holds the physical frame number plus permission and cache attribute bits. If any entry on the path is invalid, the MMU raises a fault and the kernel decides what happens next.

The CPU caches finished walks in the translation lookaside buffer (TLB). So when the kernel changes or removes a valid leaf entry, cores may keep using the old translation until the kernel invalidates it. Writing a descriptor is always paired with a barrier and, if the old one was valid, a TLB invalidation for that range. User translations are tagged with an address space identifier (ASID), so switching processes does not flush everything; kernel translations are global.

XNU keeps this machine-specific work in one layer, the pmap, short for physical map. There is one struct pmap per address space, holding the root table (as a kernel pointer and as a physical address for the hardware register), the ASIDs and the table geometry. The kernel pmap is loaded into the register that translates the upper half of the address space; each process’s pmap goes into the register for the lower half on a context switch. Its interface is small: enter a mapping from a virtual page to a frame with given permissions, remove a range, change protection, mark a mapping wired.

The split comes from Mach, the research kernel XNU grew out of. Above the pmap sits a machine-independent layer that decides what each address means; the pmap only holds the hardware form of those decisions. In this design a missing translation is a page fault: the fault handler asks the upper layer what belongs at the address and tells the pmap to enter it. The tables are where translation happens, but the authoritative description of an address space lives one level up.

The next section looks at that description: the map, its entries, and the objects that hold the pages.

The map, its entries and the objects behind them

Every task, the Mach name for the kernel side of a process, points to a vm_map. The map describes the address space as a set of non-overlapping entries. Each entry covers a half-open range, from vme_start up to but not including vme_end, and records the current and maximum protection, the inheritance rule for fork, a wired count, some state bits, and a pointer to the thing that holds the data plus an offset into it.

A vm_map holds its lock and a chain of entries, each pointing to a vm_object that owns its resident pages.

The map starts with a reader-writer lock, then the header of its entry collection, then a pointer to its pmap (osfmk/vm/vm_map.h:549). Entries sit on a sorted, circular doubly linked list and, at the same time, in a red-black tree keyed on address. Every lookup goes through the tree: vm_map_lookup_entry returns the entry containing an address, or on a miss the closest entry below it. The tree also enforces the no-overlap rule; inserting an overlapping entry panics instead of corrupting the map.

Mapping memory creates entries. mach_vm_allocate ends up in vm_map_enter, which takes the map lock exclusively, finds a gap and links a new entry. Unmapping goes through vm_map_delete, which splits (“clips”) entries so their edges match the range, tells the pmap to drop the translations, and moves the entries to a list that is freed after the lock is released. Entries are created, split and removed all the time, so a pointer to one means something only while the map is locked.

The map lock and its version number

Readers such as a page fault take the map lock shared; anything that changes entries takes it exclusive. Each time a writer releases it, the macro bumps a counter called timestamp, whose comment reads “Version number” (osfmk/vm/vm_map.h:760-765). Releasing a shared hold does not change it.

Long operations need that counter because they cannot sleep with the lock held. Wiring a range, for example, may have to read pages from disk. The code follows a fixed pattern. Under the exclusive lock it sets in_transition on the entry, which makes other threads wait instead of changing the range, and it saves the timestamp and a copy of the entry. It unlocks, does the slow work and relocks. If the timestamp is exactly one higher than saved, only its own unlock happened and the old entry pointer is still good. Otherwise another writer ran, and the code looks the entry up again by address. A comment in vm_map_delete says the entry “could have been clipped or it may not exist anymore”.

The rule for code that drops the map lock is simple: what it learned under the lock is only a memory of the map and has to be confirmed before it is relied on. in_transition keeps the range covered by entries; it does not freeze their other attributes.

VM objects

An entry does not hold pages. It points to a VM object, struct vm_object (osfmk/vm/vm_object.h:142), and an offset into it. The object owns the pages behind a mapping. Its first field, memq, is the queue of its resident pages. It also has a size, a reference count, its own lock, an optional pager that supplies missing data, and an optional shadow object to fall back on.

Separating entries from objects makes sharing cheap. Two entries, in one map or in the maps of different tasks, can point at the same object and then see the same pages. Properties of the object, such as where its pages come from, are the same for every entry that maps it.

Pages are found through a global hash table keyed on (object, offset). A page fault resolves in two steps: the map turns the address into an entry, and the entry’s object and offset, plus the distance from vme_start, give the pair to look up. If the page is resident, the fault handler enters it into the pmap. If not, it walks down the shadow chain and finally asks the pager.

Shadow chains implement copy-on-write. After a fork, a new object is placed in front of the original. Reads fall through; the first write to a page copies it into the front object, so the writer gets a private copy. File-backed objects use a related scheme that pushes old contents into a separate “copy object” before the original changes. The scheme is recorded in the object’s copy_strategy field, and one value, COPY_DELAY, comes back later.

A memory entry, or named entry, is a Mach port that wraps a range of an object so it can be handed to another task and mapped there with mach_vm_map. IOKit uses memory entries when it maps driver memory, which is how an object created inside a driver can end up behind an entry in an ordinary user map.

Objects own pages, but each page is itself a record the kernel manages. The next section follows those records.

Pages, the free and active queues, and wiring

At boot the kernel allocates one array of struct vm_page, one element per physical frame. The header comment is nearly unchanged from the Mach papers: “A small structure is kept for each resident page, indexed by page number”.

A vm_page records who owns the frame and what state it is in. Its identity is vmp_object and vmp_offset, the same pair the fault handler looks up. It has a link into the owning object’s page list, a link for whichever global queue it is on, a 16-bit vmp_wire_count, a 4-bit vmp_q_state, and bits such as vmp_busy and vmp_dirty. Each field is protected either by the owning object’s lock or by the global page queues lock.

vmp_q_state is the single source of truth for where a page is: not on any queue, wired, free, on a per-CPU free list, active, on one of the inactive queues, and a few more (osfmk/vm/vm_page.h:169-183). Asserts throughout the code check that a page is in the state a transition expects.

Free frames sit on per-color lists, with a per-CPU cache in front so that most allocations take no lock. vm_page_grab pops a free frame and the caller inserts it into an object. A page in use and pageable lives on the active queue. When free memory runs low, the pageout daemon moves pages from active to inactive and reads their hardware reference and modify bits through the pmap. A page referenced again goes back to active. A clean page is freed; a dirty one is first written to the compressor (anonymous memory) or to its file. Freeing removes the page from its object and queue and puts it on a free list, where the next vm_page_grab anywhere in the kernel can take it.

A physical frame therefore has no permanent owner. The same 16 KiB can hold a process’s heap now and a kernel data structure a moment later. That is safe only because of bookkeeping: before a frame returns to the free list, every translation to it has been removed from every pmap and the VM layer has dropped its references. The pmap keeps a reverse map from frame to virtual addresses for this purpose.

Wiring

Sometimes a page must not move. A driver doing direct memory access (DMA) gives a physical address to a device, which reads or writes that frame later without going through the MMU. If the daemon reclaimed the frame meanwhile, the device would write into whatever the frame had become. Wiring prevents that.

vm_page_wire runs with the object lock and the page queues lock held. On the first wire it removes the page from its paging queue and sets vmp_q_state to VM_PAGE_IS_WIRED (osfmk/vm/vm_resident.c:4591-4600); every call increments vmp_wire_count. Because wired is a queue state, the daemon never sees a wired page: it only scans the active and inactive queues. vm_page_unwire decrements the count and, at zero, returns the page to a pageable queue. The pmap has a matching software bit in the leaf entry and a per-table wired counter.

Wiring is thus a reference count on residency: while someone holds a wire, the frame keeps its identity. It says nothing about frames the VM layer does not track with a vm_page at all, and that is the next section’s subject.

When the object is a device: device pagers and contiguity

So far every object got its data from somewhere the kernel manages. The thing that supplies it is the object’s pager. In the original Mach design a pager could be a user task answering messages; in XNU it is a kernel object with a table of function pointers, mo_pager_ops, that the VM layer calls to fetch or return data. There is a vnode pager for files, a compressor pager for anonymous memory, and a few others, among them the device pager.

A one-range physical memory descriptor makes the device pager set private and phys_contiguous on its VM object.

From a driver buffer to a VM object

IOKit drivers describe memory with IOMemoryDescriptor. A descriptor built with a task describes virtual ranges in that task’s map, and the objects behind them already exist. A descriptor built without a task describes physical ranges directly. There is no map to look anything up in, so to make such memory mappable the descriptor creates a VM object, using the device pager.

The contiguity decision is made there, in one comparison: if the descriptor has exactly one physical range, the pager flags get DEVICE_PAGER_CONTIGUOUS (iokit/Kernel/IOMemoryDescriptor.cpp:749-758). The flags also carry the cache mode as the four WIMG bits (write-through, cache-inhibited, coherent, guarded). device_pager_setup allocates the pager, records the descriptor as its device handle along with size and flags, and creates a named VM object. IOKit wraps the pager in a memory entry so it can be mapped.

When the VM layer attaches the pager to its object, device_pager_init copies the flags into the object (osfmk/vm/device_vm.c:273-276). It sets private, documented as a “magic device_pager object, holds private pages only”, and phys_contiguous if the contiguous flag was passed. It copies the cache attributes into wimg_bits and sets the copy strategy to COPY_DELAY. From then on, any code holding the object can tell what it is by testing two bits.

Where the physical addresses go

The frames already exist; the driver owns them. They are installed when the descriptor is mapped: IOKit walks its physical segments and hands each one to vm_object_populate_with_private, through the device pager.

For a non-contiguous object, that function creates one page structure per frame: a fictitious vm_page, marked private, holding the device’s frame number. These look like resident pages to the fault handler, but they never come from or return to the free lists.

For a contiguous object, it creates no page structures. A contiguous object may not have a shadow, so the field that would hold the shadow offset is free and gets reused: the base physical address goes into vo_shadow_offset and the size into vo_size (osfmk/vm/vm_object.c:5288-5294). Byte N of the object is the byte at physical address base plus N.

The fault path takes a matching shortcut. Finding no page in a phys_contiguous object, it computes the physical address as vo_shadow_offset plus the entry’s offset and asks the pmap to map a whole block at once, with the cache attributes from wimg_bits. Unwiring such a range is a no-op, because, as the comment in vm_fault.c puts it, such memory “is wired by default”.

Three meanings of contiguous

“Contiguous” ends up meaning three things. The descriptor that built the object had one range. The frames behind the object form one linear run starting at vo_shadow_offset. And the VM layer keeps no per-page records for it: no vm_page, no wire counts, no queue state. The first is a fact about how the object was built. The second is a promise made by whoever built the descriptor; XNU never checks it. The third is what makes the object cheap, and it means the bookkeeping of the previous section does not apply.

The comments disagree on what such memory is. The object header says it “is not device memory and obeys normal virtual memory rules”, while the fault path says “i.e. device memory”. Both hold in their place, because x86 superpages reuse the flag and the base address encoding. Only three lines in the tree set phys_contiguous: the superpage path in vm_map_enter, closed on arm64 because the arm pmap declares no superpage support; the device pager; and a test file. On an iPhone a contiguous object comes from the device pager.

The iOS drivers that build physical descriptors, such as graphics, display and IOSurface, are not in the XNU source; which ranges they wrap and whether they use one range or several is (not verified in the tree).

That completes how memory is represented. The last piece is how file systems and drivers ask the VM layer for the pages behind a range, and how a contiguous object answers.

The page list: how I/O asks for pages

A file system writing user data, or a driver setting up DMA, needs the physical frames behind an address, needs them to stay put while the device works, and needs their state. XNU’s answer is the universal page list, or UPL: “A UPL describes a bounded set of physical pages associated with some range of an object or map and a snapshot of the attributes associated with each of those pages” (osfmk/mach/memory_object_types.h:466-471).

A UPL for a contiguous object is marked device memory, records the highest page and fills only page entry 0.

Two words carry the meaning. “Bounded”: a UPL covers a fixed range, at most 64 MiB for an ordinary object. “Snapshot”: the attributes are copied when the list is built. The VM layer owns the pages and the I/O code owns the work; the UPL is what one hands the other.

struct upl holds a lock, reference counts, flags, the offset and size of the range, the object it covers, and a trailing array of upl_page_info records. Each record holds a physical page number and bits copied from the vm_page: dirty, absent, precious, device and a few code-signing bits. A record stores no offset; its position in the array is the offset, so entry N describes the page N pages after the start. A page number of zero means “no page here”.

There are two ways in. Pagers that already hold an object call vm_object_upl_request. Code that starts from an address in a map, as the I/O stack does with user buffers, calls vm_map_get_upl, which leads to vm_map_create_upl and then vm_object_iopl_request.

The flags

One 64-bit flag word shapes the request. Few flags have a comment in the header, so the families below are grouped by how the call sites use them.

Family What it tells the kernel Examples
Direction which way the data moves UPL_COPYOUT_FROM
Structure where the page list lives, how presence is tracked UPL_SET_INTERNAL, UPL_SET_LITE
I/O intent file I/O, or device I/O that needs wiring UPL_FILE_IO, UPL_SET_IO_WIRE
Page handling how dirty or precious pages are treated UPL_CLEAN_IN_PLACE, UPL_PRECIOUS
Selection gather only pages in a given state UPL_RET_ONLY_DIRTY, UPL_RET_ONLY_ABSENT
Access keep others away while the list exists UPL_BLOCK_ACCESS
Modification the caller will change the pages UPL_WILL_MODIFY
Query build nothing, report the object type UPL_QUERY_OBJECT_TYPE
Returned state written back by the kernel UPL_DEV_MEMORY, UPL_PHYS_CONTIG

The last row differs in kind. UPL_DEV_MEMORY (0x1) and UPL_PHYS_CONTIG (0x2) sit under the comment “flags for return of state from vm_map_get_upl”, and the kernel writes them back through the pointer the caller used for its request. Every other row is an instruction given before the work starts; this one reports what the kernel found.

Query mode

UPL_QUERY_OBJECT_TYPE asks only for that report. vm_map_create_upl takes the map lock shared, looks up the entry, and trims the requested size to the end of that entry. With the query bit set, it clears the returned flags, sets UPL_DEV_MEMORY if the entry’s object is private and UPL_PHYS_CONTIG if it is phys_contiguous, releases the lock and returns (osfmk/vm/vm_pageout.c:6712-6727). No UPL is built and no page is touched. The only caller of this mode in the tree is the cluster layer in bsd/vfs/vfs_cluster.c, which uses the answer to choose how to move a user buffer to or from a file: a contiguous path, a direct path, or a copy.

Building the real list

Without the query bit, vm_map_create_upl continues under the shared lock and finds the object and offset behind the entry. For an ordinary object it forces a sharing mode first, under this comment: “After we unlock the map, anything could happen to that mapping, including some copy-on-write activity.” It then takes a reference on the object, releases the map lock, and calls vm_object_iopl_request on the object. From there on the work concerns the object, not the map.

For an ordinary object, vm_object_iopl_request walks the range page by page, faults in missing pages, wires each one when asked, and fills one record per page. When the I/O ends, the caller commits or aborts the UPL, which updates the pages and drops the wires.

For a phys_contiguous object there are no pages to walk, so it takes a shortcut (osfmk/vm/vm_pageout.c:9334-9352):

if (object->phys_contiguous) {
	...
	upl->flags |= UPL_DEVICE_MEMORY;
	upl->highest_page = (ppnum_t) ((offset + object->vo_shadow_offset + size - 1) >> PAGE_SHIFT);
	if (user_page_list) {
		user_page_list[0].phys_addr = (ppnum_t) ((offset + object->vo_shadow_offset) >> PAGE_SHIFT);
		user_page_list[0].device = TRUE;
	}

The UPL is marked as device memory, its highest page is the last frame of the range, and only the first record is filled: the first frame, flagged as a device page. A comment explains that no shadow mappings are needed “since it is already I/O memory”. Nothing is wired. Every page after the first follows from the contiguity rule: page N is the first frame plus N, up to the highest page. The map path also exempts contiguous objects from the 64 MiB cap.

For memory that really is one run of frames, owned by a driver for as long as the list lives, this is good engineering: two numbers and a rule replace thousands of records and wire operations. The cost is that the list now describes a range by its endpoints plus an assumption about everything between them, read from the object at one moment and used by other code later.

Closing

That is the model part 2 needs. A virtual address means something only through the map. Entries point at objects. Objects own pages, which are frames with careful bookkeeping and a wire count that pins them. A device object can skip that bookkeeping and describe its memory as a base and a size. A page list is a snapshot of whatever the I/O code found when it asked.

Each step reads the map under its lock and then lets the lock go: a query reports what kind of object stood behind an address, a later request builds a page list for that address, and a device works on the frames the list names. Part 2 starts from one question: if the mapping at an address changes after the kernel has checked what kind of memory lies behind it, but before it uses that answer, which of these layers notices?