Skip to content

Reset xcr0 on restore - #1718

Merged
ludfjig merged 2 commits into
hyperlight-dev:mainfrom
ludfjig:reset-xcr0-on-restore
Aug 12, 2026
Merged

Reset xcr0 on restore#1718
ludfjig merged 2 commits into
hyperlight-dev:mainfrom
ludfjig:reset-xcr0-on-restore

Conversation

@ludfjig

@ludfjig ludfjig commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

XCR0 is guest-writable and was not reset by restore(), so its value could leak between guest executions. This fixes the issue by resetting XCR0 to its architectural default, 0x1. I chose a fixed baseline rather than capturing XCR0 because Hyperlight’s default CR4 has OSXSAVE = 0, so extended state is disabled by default. We can reconsider preserving XCR0 when Hyperlight supports AVX.

If useful we could capture it in snapshot as well, I'm just not sure if necessary

@ludfjig ludfjig added the kind/bugfix For PRs that fix bugs label Aug 11, 2026
@ludfjig
ludfjig marked this pull request as ready for review August 11, 2026 22:27
Copilot AI lite review requested due to automatic review settings August 11, 2026 22:27
@syntactically

Copy link
Copy Markdown
Member

Why add new accessor functions/etc rather than handle xcr0 in sregs along with the other crN?

This comment was marked as off-topic.

This comment was marked as off-topic.

@ludfjig

ludfjig commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Why add new accessor functions/etc rather than handle xcr0 in sregs along with the other crN?

It's a different hypercall so I thought this was cleanest (except on whp). Since I also didn't add it to snapshot state, i'd need some extra code not to serialize it as well to snapshot if it's part of CommonSpecialRegisters

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig
ludfjig force-pushed the reset-xcr0-on-restore branch from 03c8e66 to a9dbf31 Compare August 11, 2026 22:42
jsturtevant
jsturtevant previously approved these changes Aug 11, 2026

@jsturtevant jsturtevant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig ludfjig added the ready-for-review PR is ready for (re-)review label Aug 11, 2026
@syntactically

Copy link
Copy Markdown
Member

Why add new accessor functions/etc rather than handle xcr0 in sregs along with the other crN?

It's a different hypercall so I thought this was cleanest (except on whp). Since I also didn't add it to snapshot state, i'd need some extra code not to serialize it as well to snapshot if it's part of CommonSpecialRegisters

Hm, I think it's also nice to avoid adding more arch specific items to #[cfg(target_arch = ...)] items to the core traits (and would like to see how many of the existing ones we can get rid of / push towards the edges), so reusing one of the existing abstractions seems nicer. It also does feel semantically like the other CRx to me? Does it end up adding any overhead to do it in the sregs path, or is it currently used 1:1? If it is 1:1, it feels like it ought to be part of the same abstraction to me.

@ludfjig

ludfjig commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Why add new accessor functions/etc rather than handle xcr0 in sregs along with the other crN?

It's a different hypercall so I thought this was cleanest (except on whp). Since I also didn't add it to snapshot state, i'd need some extra code not to serialize it as well to snapshot if it's part of CommonSpecialRegisters

Hm, I think it's also nice to avoid adding more arch specific items to #[cfg(target_arch = ...)] items to the core traits (and would like to see how many of the existing ones we can get rid of / push towards the edges), so reusing one of the existing abstractions seems nicer. It also does feel semantically like the other CRx to me? Does it end up adding any overhead to do it in the sregs path, or is it currently used 1:1? If it is 1:1, it feels like it ought to be part of the same abstraction to me.

There are some places we only call set_sregs, without needing to call set_xcr0, since we rely on it being initialized to default value (creating new vm, creating vm from snapshot) which is guaranteed by the hv. Combining them would introduce an extra hypercall here that's unnecessary. In general I agree with you though about avoiding adding more target specific cfgs to the trait. How about leaving it like this for now, and in the future if we decide to allow configuring xcr0 to different value, we can consider combining set_sregs and set_xcr0?

@syntactically

Copy link
Copy Markdown
Member

There are some places we only call set_sregs, without needing to call set_xcr0, since we rely on it being initialized to default value (creating new vm, creating vm from snapshot) which is guaranteed by the hv. Combining them would introduce an extra hypercall here that's unnecessary. In general I agree with you though about avoiding adding more target specific cfgs to the trait

When we have time to do some cleanup here we can enumerate the lifecycles of various things and find arch independent abstractions that work better then, but happy to merge this as is for now.

@ludfjig
ludfjig merged commit 9011420 into hyperlight-dev:main Aug 12, 2026
105 of 137 checks passed
@github-actions github-actions Bot removed the ready-for-review PR is ready for (re-)review label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bugfix For PRs that fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants