Skip to content

Commit f17f3a4

Browse files
committed
[hyperlight_guest_bin] Save/restore x87 & SSE regs on context switch
We unfortunately don't have a good way of making sure that code run in exception contexts doesn't use floating point and SSE instructions. This commit simply always saves and restores the relevant registers as a workaround. We should at some point look into the alternative of ensuring that simd instructions aren't used in code reachable from an exception handler, and see if that actually improves performance. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent b46da12 commit f17f3a4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/hyperlight_guest_bin/src/exceptions/interrupt_entry.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@ macro_rules! context_save {
6767
" push r13\n",
6868
" push r14\n",
6969
" push r15\n",
70-
// Save segment registers
70+
// Save one of the segment registers to get 16-byte alignment for
71+
// FXSAVE. TODO: consider packing the segment registers
7172
" mov rax, ds\n",
7273
" push rax\n",
74+
// Save floating-point/SSE registers
75+
// TODO: Don't do this unconditionally: get the exn
76+
// handlers compiled without sse
77+
// TODO: Check if we ever generate code with ymm/zmm in
78+
// the handlers and save/restore those as well
79+
" sub rsp, 512\n",
80+
" mov rax, rsp\n",
81+
" fxsave [rax]\n",
82+
// Save the rest of the segment registers
7383
" mov rax, es\n",
7484
" push rax\n",
7585
" mov rax, fs\n",
@@ -83,13 +93,18 @@ macro_rules! context_save {
8393
macro_rules! context_restore {
8494
() => {
8595
concat!(
86-
// Restore segment registers
96+
// Restore most segment registers
8797
" pop rax\n",
8898
" mov gs, rax\n",
8999
" pop rax\n",
90100
" mov fs, rax\n",
91101
" pop rax\n",
92102
" mov es, rax\n",
103+
// Restore floating-point/SSE registers
104+
" mov rax, rsp\n",
105+
" fxrstor [rax]\n",
106+
" add rsp, 512\n",
107+
// Restore the last segment register
93108
" pop rax\n",
94109
" mov ds, rax\n",
95110
// Restore general-purpose registers

0 commit comments

Comments
 (0)