Study guides · Advanced

Guest entry and exit: save state and resume

Follow a round trip from EL2 into an EL1 guest and back, including register ownership and the return location.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Entry installs an execution context
  2. Keep guest and host values separate
  3. Prepare the state consumed by ERET
  4. Classify an exit before choosing a response
  5. Advancing the PC is a policy for a specific cause
  6. Check continuity across a round trip
  7. Exercise: retry or continue after the operation
  8. Ask Logeia about this articleCheck your understanding

Entry installs an execution context

Starting a guest is more than calling an ordinary function. The entry path prepares the PC, general registers, stack, processor state, and relevant translation context. This lesson limits the model to one guest and one vCPU, following an EL2-to-EL1 entry and a selected trap back to EL2. Scheduling many vCPUs and booting Linux are later concerns.

Preparing a target context is different from successfully executing its first instruction. A selected start PC can still refer to absent code or a page without execution permission. The demonstration begins with its stated prerequisites satisfied. When moving to implementation, turn code placement, translation, permissions, and stack setup into separate pre-entry checks. A visible transition to the guest level is not by itself proof that meaningful guest code ran or that its execution environment is complete.

Keep guest and host values separate

The name x0 does not identify one permanent value. If management code has x0=99 and the guest context has x0=7, guest entry restores 7 while retaining any needed management value elsewhere. If the guest increments x0 to 8, a later re-entry must preserve 8 rather than reload the initial 7. The model shows stored context, live CPU values, and the values selected for restoration.

A different bug saves the contexts of two guests into the same storage. It may survive a single-guest test yet appear when guests initialized with distinct patterns are resumed alternately. Although this first model teaches one vCPU, it is useful to ask how the context identity maps to its storage. Adding a guest number to a display does not establish separate storage. The underlying saved-state selection must preserve that distinction too.

python
guest = {'x0': 7, 'pc': 0x4000}
cpu = dict(guest)  # restore selected teaching state
cpu['x0'] += 1
guest = dict(cpu)  # save selected teaching state on exit
cpu = {'x0': 99, 'pc': 0x9000}  # management context
cpu = dict(guest)  # resume the guest
assert cpu == {'x0': 8, 'pc': 0x4000}
print(cpu)
StepLive x0Saved guest x0
Before entry997
Enter guest77
Guest increments87
Save on exitSwitch to management context8

Prepare the state consumed by ERET

ERET returns using the relevant saved return state, including ELR_EL2 and SPSR_EL2 for this path. Initial guest entry prepares a context that can be entered as such a return. A correct branch target alone is insufficient: stack and memory access must also be usable. The lesson exposes those prerequisites rather than presenting ERET as an unconditional safe jump.

Real entry code must satisfy the architectural requirements surrounding register updates, context synchronization, and exception return. A diagram listing register names is not complete assembly ready to run on hardware. Its purpose is to divide responsibilities into state restored, configuration changed, and control transferred. Implementation-specific instructions should then be checked against those responsibilities and the applicable architectural rules, rather than inferred from the animation order or from a convenient pseudocode sequence.

Classify an exit before choosing a response

EL2 exits can arise from selected trapped operations or stage-2 access faults. An exit alone does not identify success, device emulation, or an invalid access. The handler inspects saved context and valid exception information before selecting a response. The model distinguishes resuming, emulating an operation, and rejecting an access rather than returning success for every exit.

Alongside the exit class, preserve the accessed target and requested operation. Two stage-2 failures can need different responses if one targets a registered UART location and the other an unrelated RAM range. A function called exit_handler does not explain that decision. A four-column record of class, target range, operation, and outcome makes missing cases and overly broad acceptance visible. It also identifies which condition a regression test needs to alter.

Advancing the PC is a policy for a specific cause

After software completes a trapped AArch64 instruction, resuming at the following instruction may be appropriate. After repairing a mapping, advancing would instead skip the access that needs to be retried. The saved return location must also be interpreted for the exception class. This model labels the two cases: resume after an emulated instruction, or retry the same instruction after repair.

A duplicate character is easiest to detect by checking the output count together with the resumed location. Successful emulation adds one output and advances according to the selected instruction contract. Rejection adds no output and does not pretend that the operation completed. A test that merely observes a return to EL1 would miss duplicated side effects or a skipped operation. Pair control-flow assertions with assertions about the work the guest requested.

Check continuity across a round trip

Set guest x0 to 7, increment it to 8, exit, and re-enter. Seeing 8 demonstrates preservation of that value, not of the entire architectural state. Distinct register patterns, stack values, and PC observations can reveal different omissions. The exercise removes one save operation and asks which observation exposes the missing state.

Give adjacent operations different observable markers when checking a resume point. A sequence intended to emit A then B can reveal duplication as AA or a skipped operation as B alone. Such output still does not verify every saved register, but it makes the error classes covered by the test explicit. After observing one successful round trip, introduce a deliberately omitted save or incorrect resume location while keeping the remaining input unchanged.

Exercise: retry or continue after the operation

If management code repairs a missing RAM mapping, this example retries the still-unfinished guest store. If management code has already emulated a character write to the virtual UART, retrying that same operation could duplicate the character. The answer therefore depends on whether the trapped operation has been completed and on the exception/instruction contract.

Use separate verbs in the answer: repair the mapping, complete the operation, and resume execution. This distinguishes advancing after repair from retrying after completed emulation. A proposed fix should name the exit class and completion condition to which it applies, rather than simply saying change the PC. The branch may be small in management code, but it determines whether the guest observes one operation, a skipped operation, or a repeated side effect.

Try the interactive model

Guest output and MMIO traps

Loading demo checkpoint…

Reading and practice are recorded separately in this browser, for each language.

Choose a learning path