Skip to content

[lldb][debugserver] Save and restore the SVE/SME register state #134184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jasonmolenda
Copy link
Collaborator

debugserver isn't saving and restoring the SVE/SME register state around inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally a poor idea because a NEON instruction can be hit and crash the expression execution, which is how I missed this, but they should be handled correctly if the user knows it is safe to do.

rdar://146886210

debugserver isn't saving and restoring the SVE/SME register state
around inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is
generally a poor idea because a NEON instruction can be hit and
crash the expression execution, which is how I missed this, but
they should be handled correctly if the user knows it is safe
to do.

rdar://146886210
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-lldb

Author: Jason Molenda (jasonmolenda)

Changes

debugserver isn't saving and restoring the SVE/SME register state around inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally a poor idea because a NEON instruction can be hit and crash the expression execution, which is how I missed this, but they should be handled correctly if the user knows it is safe to do.

rdar://146886210


Full diff: https://github.com/llvm/llvm-project/pull/134184.diff

1 Files Affected:

  • (modified) lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp (+29-1)
diff --git a/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp b/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp
index 34a4ee21f8502..fb23744393d43 100644
--- a/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp
+++ b/lldb/tools/debugserver/source/MacOSX/arm64/DNBArchImplARM64.cpp
@@ -2953,7 +2953,14 @@ kern_return_t DNBArchMachARM64::SetRegisterState(int set) {
 
   switch (set) {
   case e_regSetALL:
-    return SetGPRState() | SetVFPState() | SetEXCState() | SetDBGState(false);
+  {
+    kern_return_t ret = SetGPRState() | SetVFPState() | SetEXCState() | SetDBGState(false);
+    if (CPUHasSME()) {
+      ret |= SetSVEState();
+      ret |= SetSMEState();
+    }
+    return ret;
+  }
   case e_regSetGPR:
     return SetGPRState();
   case e_regSetVFP:
@@ -3122,6 +3129,15 @@ uint32_t DNBArchMachARM64::SaveRegisterState() {
     DNBLogThreadedIf(LOG_THREAD, "DNBArchMachARM64::SaveRegisterState () "
                                  "error: %s regs failed to read: %u",
                      "VFP", kret);
+  } else if (CPUHasSME() && (kret = SetSVEState() != KERN_SUCCESS)) {
+    DNBLogThreadedIf(LOG_THREAD, "DNBArchMachARM64::SaveRegisterState () "
+                                 "error: %s regs failed to read: %u",
+                     "SVE", kret);
+  }
+  else if (CPUHasSME() && (kret = SetSMEState() != KERN_SUCCESS)) {
+    DNBLogThreadedIf(LOG_THREAD, "DNBArchMachARM64::SaveRegisterState () "
+                                 "error: %s regs failed to read: %u",
+                     "SME", kret);
   } else {
     const uint32_t save_id = GetNextRegisterStateSaveID();
     m_saved_register_states[save_id] = m_state.context;
@@ -3149,6 +3165,18 @@ bool DNBArchMachARM64::RestoreRegisterState(uint32_t save_id) {
                                    "write: %u",
                        save_id, "VFP", kret);
       success = false;
+    } else if ((kret = SetSVEState()) != KERN_SUCCESS) {
+      DNBLogThreadedIf(LOG_THREAD, "DNBArchMachARM64::RestoreRegisterState "
+                                   "(save_id = %u) error: %s regs failed to "
+                                   "write: %u",
+                       save_id, "SVE", kret);
+      success = false;
+    } else if ((kret = SetSMEState()) != KERN_SUCCESS) {
+      DNBLogThreadedIf(LOG_THREAD, "DNBArchMachARM64::RestoreRegisterState "
+                                   "(save_id = %u) error: %s regs failed to "
+                                   "write: %u",
+                       save_id, "SME", kret);
+      success = false;
     }
     m_saved_register_states.erase(pos);
     return success;

Copy link

github-actions bot commented Apr 3, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@jasonmolenda jasonmolenda merged commit 4e40c7c into llvm:main Apr 3, 2025
10 checks passed
@jasonmolenda jasonmolenda deleted the save-and-restore-sve-and-sme-register-states branch April 3, 2025 03:37
jasonmolenda added a commit that referenced this pull request Apr 3, 2025
…te (#134184)"

This reverts commit 4e40c7c.

arm64 CI is getting a failure in
lldb-api.tools/lldb-server.TestGdbRemoteRegisterState.py
with this commit, need to investigate and re-land.
@jasonmolenda
Copy link
Collaborator Author

CI bots showed a fail in lldb-api.tools/lldb-server.TestGdbRemoteRegisterState.py , will debug and update. https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/23305/

jasonmolenda added a commit that referenced this pull request Apr 3, 2025
)

debugserver isn't saving and restoring the SVE/SME register state around
inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally
a poor idea because a NEON instruction can be hit and crash the
expression execution, which is how I missed this, but they should be
handled correctly if the user knows it is safe to do.

Re-landing this change after fixing an incorrect behavior on systems
without SME support.

rdar://146886210
jasonmolenda added a commit to jasonmolenda/llvm-project that referenced this pull request Apr 3, 2025
…#134184)

debugserver isn't saving and restoring the SVE/SME register state around
inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally
a poor idea because a NEON instruction can be hit and crash the
expression execution, which is how I missed this, but they should be
handled correctly if the user knows it is safe to do.

Re-landing this change after fixing an incorrect behavior on systems
without SME support.

rdar://146886210
(cherry picked from commit f1c6612)
jasonmolenda added a commit to jasonmolenda/llvm-project that referenced this pull request Apr 3, 2025
…#134184)

debugserver isn't saving and restoring the SVE/SME register state around
inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally
a poor idea because a NEON instruction can be hit and crash the
expression execution, which is how I missed this, but they should be
handled correctly if the user knows it is safe to do.

Re-landing this change after fixing an incorrect behavior on systems
without SME support.

rdar://146886210
(cherry picked from commit f1c6612)
jasonmolenda added a commit to swiftlang/llvm-project that referenced this pull request Apr 4, 2025
…#134184) (#10418)

debugserver isn't saving and restoring the SVE/SME register state around
inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally
a poor idea because a NEON instruction can be hit and crash the
expression execution, which is how I missed this, but they should be
handled correctly if the user knows it is safe to do.

Re-landing this change after fixing an incorrect behavior on systems
without SME support.

rdar://146886210
(cherry picked from commit f1c6612)
jasonmolenda added a commit to jasonmolenda/llvm-project that referenced this pull request Apr 4, 2025
…#134184)

debugserver isn't saving and restoring the SVE/SME register state around
inferior function calls.

Making arbitrary function calls while in Streaming SVE mode is generally
a poor idea because a NEON instruction can be hit and crash the
expression execution, which is how I missed this, but they should be
handled correctly if the user knows it is safe to do.

Re-landing this change after fixing an incorrect behavior on systems
without SME support.

rdar://146886210
(cherry picked from commit f1c6612)
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Apr 4, 2025
…store-sme-registers-around-function-calls-61

[lldb][debugserver] Save and restore the SVE/SME register state (llvm#134184)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants