From 95bd8f2013bada04b0a993fc16cad8bf102645d7 Mon Sep 17 00:00:00 2001
From: Robin Randhawa <robin.randhawa@arm.com>
Date: Sun, 17 Jan 2021 10:26:49 +0000
Subject: [PATCH] clone: Make stack manipulation arch specific

---
 src/syscall/process.rs | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/src/syscall/process.rs b/src/syscall/process.rs
index 535f68d3..67ff8784 100644
--- a/src/syscall/process.rs
+++ b/src/syscall/process.rs
@@ -94,28 +94,33 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
                 kfx_opt = Some(new_fx);
             }
 
-            if let Some(ref stack) = context.kstack {
-                // Get the relative offset to the return address of the function
-                // obtaining `stack_base`.
-                //
-                // (base pointer - start of stack) - one
-                offset = stack_base - stack.as_ptr() as usize - mem::size_of::<usize>(); // Add clone ret
-                let mut new_stack = stack.clone();
+            #[cfg(target_arch = "x86_64")]
+            {
+                if let Some(ref stack) = context.kstack {
+                    // Get the relative offset to the return address of the function
+                    // obtaining `stack_base`.
+                    //
+                    // (base pointer - start of stack) - one
+                    offset = stack_base - stack.as_ptr() as usize - mem::size_of::<usize>(); // Add clone ret
+                    let mut new_stack = stack.clone();
 
-                unsafe {
-                    // Set clone's return value to zero. This is done because
-                    // the clone won't return like normal, which means the value
-                    // would otherwise never get set.
-                    #[cfg(target_arch = "x86_64")] // TODO
-                    if let Some(regs) = ptrace::rebase_regs_ptr_mut(context.regs, Some(&mut new_stack)) {
-                        (*regs).scratch.rax = 0;
+                    unsafe {
+                        // Set clone's return value to zero. This is done because
+                        // the clone won't return like normal, which means the value
+                        // would otherwise never get set.
+                        if let Some(regs) = ptrace::rebase_regs_ptr_mut(context.regs, Some(&mut new_stack)) {
+                            (*regs).scratch.rax = 0;
+                        }
+
+                        // Change the return address of the child (previously
+                        // syscall) to the arch-specific clone_ret callback
+                        let func_ptr = new_stack.as_mut_ptr().add(offset);
+                        *(func_ptr as *mut usize) = interrupt::syscall::clone_ret as usize;
                     }
 
-                    // Change the return address of the child (previously
-                    // syscall) to the arch-specific clone_ret callback
-                    let func_ptr = new_stack.as_mut_ptr().add(offset);
-                    *(func_ptr as *mut usize) = interrupt::syscall::clone_ret as usize;
+                    kstack_opt = Some(new_stack);
                 }
+            }
 
                 kstack_opt = Some(new_stack);
             }
-- 
GitLab