# One permission bit. A fault, a repair, the same store.

This **424-byte literal payload** builds a legacy page directory and table,
identity maps the low 4 MiB, enables paging and supervisor write protection,
then attempts a store to a read-only page. The CPU raises a real QEMU **#PF**.
The handler records the error and frame, changes only that page's RW permission,
uses INVLPG, removes the hardware error-code slot and returns with IRETD.
The unchanged store retries and writes decimal 42 before the final halt.

[program.bits](program.bits) contains every instruction and descriptor byte;
[bytes.md](bytes.md) explains every byte. Page tables are **runtime data** written
by those instructions, not instructions generated by an assembler. The host
packer adds zero sector padding and the BIOS signature, producing a 512-byte image.

## Two independent one-bit controls

| Source | Changed byte / mask | Native result |
| --- | --- | --- |
| [program](program.bits) | Baseline | One #PF, error 3, CR2=B000; permission repair and exact store retry |
| [writable](writable.bits) | 123 / 02: PTE RW=1 | The store succeeds without entering the handler |
| [wp-off](wp-off.bits) | 148 / 01: CR0.WP=0 | The ring-zero store succeeds while its PTE remains read-only |

All three boots finish with the target doubleword equal to `0000002A` and
ESP=`9000`. The two no-fault controls retain an entirely zero six-word fault log.
The WP-off result demonstrates supervisor behavior; it does not mean user-mode
code can write supervisor or read-only pages.

## Inspect the actual structures

CR3 points to physical `4000`. Its first directory entry points to `5000`,
with P=1, RW=1 and U/S=0; all other 1023 directory entries remain zero. The
table contains 1024 identity mappings, each with supervisor permissions.
Entry 11, at `502C`, governs linear page `B000`.

The source initially makes that PTE read-only in the baseline and WP-off cases.
The baseline enables CR0.PG and CR0.WP. Its attempted supervisor write produces
error **3**: P=1, W/R=1, U/S=0. CR2 records the attempted linear address `B000`.
The saved EIP stays `7C99`, the ten-byte store's original address. The handler
sets PTE.RW, invalidates the page translation and retries without advancing EIP.

Native tests inspect **every entry in both 4 KiB structures**, allowing only
CPU-managed accessed/dirty bits to vary. The successful target store sets both
bits on PTE 11. After completion its value is `0000B063` in the baseline and
writable case, or `0000B061` when WP is off and RW remains zero. Complete raw
directory/table dumps are captured as data assets, distinct from the payload.

## The hardware frame is retained

The fault log contains six doublewords: count, error, original EIP, CR2, CS and
saved EFLAGS. The baseline records count=1, error=3, EIP=7C99, CR2=B000, CS=8.
The actual saved flags are retained without replacing system bits with a model
prediction. IF and DF remain zero throughout this experiment.

A same-privilege #PF places four doublewords below ESP: error, EIP, CS and
EFLAGS. The handler reads those fields, then adds four to ESP to discard only
the error slot before IRETD consumes the remaining frame. Its bytes remain at
`8FF0–8FFF` and match the log after return. Final halt at `7CA4`, a completed
target store and exactly one handler entry jointly verify the successful retry.

## Memory map and reproduction

All addresses below are physical hexadecimal. Segments are flat ring-zero
descriptors; paging is legacy 32-bit, non-PAE, with 4 KiB pages.

| Range | Role |
| --- | --- |
| 4000–4FFF | Page directory, all 1024 entries inspected |
| 5000–5FFF | Page table, all 1024 entries inspected |
| 6000–6017 | Six-doubleword initialized fault log |
| 7C00–7DA7 | Literal 424-byte payload, including GDT and fifteen IDT gates |
| 8FF0–8FFF | Retained native page-fault frame |
| 9000 | Balanced stack top |
| B000–B003 | Target value after the store |

```sh
make build
python3 -m unittest tests.test_paging -v
node tests/test_paging_scope.mjs
python3 tools/record_paging.py
```

[Actual capture provenance](../../assets/paging-recording.json) includes three
independent QMP snapshots, raw register output, both complete paging structures,
fault/frame/payload RAM bytes, independent mixed-mode NDISASM goldens and
source/image hashes. Normal tests validate the committed goldens; capture
regeneration uses the read-only NDISASM utility from the `nasm` package.
Instruction bytes, source and read-only disk images stay unchanged; loaded
GDT accessed bits can change as managed by the CPU.

## Scope

This verifies one controlled supervisor write fault in QEMU. It is not user-mode
isolation, copy-on-write, a process address space, an allocator, a general fault
handler, PAE, long mode, NX, a TLB benchmark or physical-hardware proof. Only
IDT vector 0E is present, and EAX is handler scratch state.

The literal payload fits the CPU lab's input bound, but its model stops after
seven setup instructions at unsupported LGDT. It supplies no protected-mode,
paging, TLB or page-fault emulation. Native snapshots provide no rewind or
instruction-by-instruction trace. Tagged releases retain their original contents.

Primary references: [Intel paging permissions and supervisor WP](https://cdrdv2-public.intel.com/874249/253668-090-sdm-vol-3a.pdf),
[page-fault error fields and CR2](https://cdrdv2-public.intel.com/671190/253668-sdm-vol-3a.pdf),
[INVLPG and IRETD instruction semantics](https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html),
and [QEMU monitor inspection](https://www.qemu.org/docs/master/system/monitor.html).
