From 113a7164bbb3f4a8a7ce696f884577811f23253e Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Wed, 24 Aug 2022 11:11:50 -0600
Subject: [PATCH] Use push/pup scratch for x86_64 signal handler

---
 src/context/arch/x86_64.rs | 46 +++++++++++---------------------------
 1 file changed, 13 insertions(+), 33 deletions(-)

diff --git a/src/context/arch/x86_64.rs b/src/context/arch/x86_64.rs
index 9b3f366c..629179bc 100644
--- a/src/context/arch/x86_64.rs
+++ b/src/context/arch/x86_64.rs
@@ -3,6 +3,8 @@ use core::sync::atomic::AtomicBool;
 
 use alloc::sync::Arc;
 
+use crate::{push_scratch, pop_scratch};
+use crate::interrupt::handler::ScratchRegisters;
 use crate::paging::{RmmA, RmmArch, TableKind};
 use crate::syscall::FloatRegisters;
 
@@ -252,15 +254,7 @@ unsafe extern "sysv64" fn switch_to_inner(_prev: &mut Context, _next: &mut Conte
 #[allow(dead_code)]
 #[repr(packed)]
 pub struct SignalHandlerStack {
-    r11: usize,
-    r10: usize,
-    r9: usize,
-    r8: usize,
-    rsi: usize,
-    rdi: usize,
-    rdx: usize,
-    rcx: usize,
-    rax: usize,
+    scratch: ScratchRegisters,
     handler: extern fn(usize),
     sig: usize,
     rip: usize,
@@ -275,34 +269,20 @@ unsafe extern fn signal_handler_wrapper() {
 
     // Push scratch registers
     core::arch::asm!(
-        "
-            push rax
-            push rcx
-            push rdx
-            push rdi
-            push rsi
-            push r8
-            push r9
-            push r10
-            push r11
-
+        concat!(
+            "push rax",
+            push_scratch!(),
+            "
             mov rdi, rsp
             call {inner}
-
-            pop r11
-            pop r10
-            pop r9
-            pop r8
-            pop rsi
-            pop rdi
-            pop rdx
-            pop rcx
-            pop rax
+            ",
+            pop_scratch!(),
+            "
             add rsp, 16
             ret
-        ",
-
+            "
+        ),
         inner = sym inner,
-        options(noreturn),
+        options(noreturn)
     );
 }
-- 
GitLab