bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] hurd: Fully remove the ecx kludge


From: Sergey Bugaev
Subject: [PATCH 1/2] hurd: Fully remove the ecx kludge
Date: Tue, 28 Feb 2023 22:44:08 +0300

"We don't need it any more"

The INTR_MSG_TRAP macro in intr-msg.h used to play little trick with
the stack pointer: it would temporarily save the "real" stack pointer
into ecx, while setting esp to point to just before the message buffer,
and then invoke the mach_msg trap. This way, INTR_MSG_TRAP reused the
on-stack arguments laid out for the containing call of
_hurd_intr_rpc_mach_msg (), passing them to the mach_msg trap directly.

This, however, required special support in hurdsig.c and trampoline.c,
since they now had to recognize when a thread is inside the piece of
code where esp doesn't point to the real tip of the stack, and handle
this situation specially.

Commit 1d20f33ff4fb634310f27493b7b87d0b20f4a0b0 has removed the actual
temporary change of esp by actually re-pushing mach_msg arguments onto
the stack, and popping them back at end. It did not, however, deal with
the rest of "the ecx kludge" code in other files, resulting in potential
crashes if a signal arrives in the middle of pushing arguments onto the
stack.

This was made more confusing because of a comment in hurdsig.c, which
seemed to indicate that if a thread is about to enter the mach_msg trap,
the trap has to be skipped while setting the return code as if the
thread has already entered the trap and the operation has been
interrupted. In fact, skipping the trap was not a goal in and of itself,
but rather a way to get the thread back into a consistent state with
respect to what the value of esp is and what the code expects it to be.
So it is fine to remove this, since the value of esp is no longer
inconsistent.

Fix the issue by removing all traces of "the ecx kludge", which also
simplifies things nicely and paves the way for a future x86_64 port of
this code.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 hurd/hurdsig.c                      | 23 +++++----------------
 sysdeps/mach/hurd/i386/intr-msg.h   | 31 ++++++-----------------------
 sysdeps/mach/hurd/i386/trampoline.c | 21 -------------------
 3 files changed, 11 insertions(+), 64 deletions(-)

diff --git a/hurd/hurdsig.c b/hurd/hurdsig.c
index ea79ffb5..3e759ae5 100644
--- a/hurd/hurdsig.c
+++ b/hurd/hurdsig.c
@@ -414,7 +414,6 @@ _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, 
int sigthread,
                     struct machine_thread_all_state *state, int *state_change,
                     void (*reply) (void))
 {
-  extern const void _hurd_intr_rpc_msg_about_to;
   extern const void _hurd_intr_rpc_msg_in_trap;
   mach_port_t rcv_port = MACH_PORT_NULL;
   mach_port_t intr_port;
@@ -430,23 +429,11 @@ _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, 
int sigthread,
      receive completes immediately or aborts.  */
   abort_thread (ss, state, reply);
 
-  if (state->basic.PC >= (uintptr_t) &_hurd_intr_rpc_msg_about_to
-      && state->basic.PC < (uintptr_t) &_hurd_intr_rpc_msg_in_trap)
-    {
-      /* The thread is about to do the RPC, but hasn't yet entered
-        mach_msg.  Mutate the thread's state so it knows not to try
-        the RPC.  */
-      INTR_MSG_BACK_OUT (&state->basic);
-      MACHINE_THREAD_STATE_SET_PC (&state->basic,
-                                  &_hurd_intr_rpc_msg_in_trap);
-      state->basic.SYSRETURN = MACH_SEND_INTERRUPTED;
-      *state_change = 1;
-    }
-  else if (state->basic.PC == (uintptr_t) &_hurd_intr_rpc_msg_in_trap
-          /* The thread was blocked in the system call.  After thread_abort,
-             the return value register indicates what state the RPC was in
-             when interrupted.  */
-          && state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
+  if (state->basic.PC == (uintptr_t) &_hurd_intr_rpc_msg_in_trap
+      /* The thread was blocked in the system call.  After thread_abort, the
+         return value register indicates what state the RPC was in when
+         interrupted.  */
+      && state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
       {
        /* The RPC request message was sent and the thread was waiting for
           the reply message; now the message receive has been aborted, so
diff --git a/sysdeps/mach/hurd/i386/intr-msg.h 
b/sysdeps/mach/hurd/i386/intr-msg.h
index 29cb4620..953e4553 100644
--- a/sysdeps/mach/hurd/i386/intr-msg.h
+++ b/sysdeps/mach/hurd/i386/intr-msg.h
@@ -24,12 +24,7 @@
 #define INTR_MSG_TRAP(msg, option, send_size, rcv_size, rcv_name, timeout, 
notify, cancel_p, intr_port_p) \
 ({                                                                           \
   error_t err;                                                               \
-  asm (".globl _hurd_intr_rpc_msg_about_to\n"                                \
-       ".globl _hurd_intr_rpc_msg_cx_sp\n"                                   \
-       ".globl _hurd_intr_rpc_msg_do_trap\n"                                 \
-       ".globl _hurd_intr_rpc_msg_in_trap\n"                                 \
-       ".globl _hurd_intr_rpc_msg_sp_restored\n"                             \
-       "_hurd_intr_rpc_msg_about_to:"                                        \
+  asm (".globl _hurd_intr_rpc_msg_in_trap\n"                                 \
        /* We need to make a last check of cancel, in case we got interrupted
           right before _hurd_intr_rpc_msg_about_to.  */                        
      \
        "                               cmpl $0, %5\n"                        \
@@ -37,7 +32,7 @@
        /* We got interrupted, note so and return EINTR.  */                  \
        "                               movl $0, %3\n"                        \
        "                               movl %6, %%eax\n"                     \
-       "                               jmp _hurd_intr_rpc_msg_sp_restored\n" \
+       "                               jmp _hurd_intr_rpc_msg_out\n"         \
        "_hurd_intr_rpc_msg_do:"                                                
      \
        /* Ok, push the mach_msg_trap arguments.  */                          \
        "                               pushl 24(%4)\n"                       \
@@ -48,10 +43,8 @@
        "                               pushl %1\n"                           \
        "                               pushl (%4)\n"                         \
        "                               pushl $0\n"                           \
-       /* TODO: remove this ecx kludge, we don't need it any more */         \
-       "                               movl %%esp, %%ecx\n"                  \
-       "_hurd_intr_rpc_msg_cx_sp:      movl $-25, %%eax\n"                   \
-       "_hurd_intr_rpc_msg_do_trap:    lcall $7, $0 # status in %0\n"        \
+       "                               movl $-25, %%eax\n"                   \
+       "                               lcall $7, $0 # status in %0\n"        \
        "_hurd_intr_rpc_msg_in_trap:"                                         \
        /* Ok, clean the arguments and update OPTION and TIMEOUT.  */         \
        "                               addl $8, %%esp\n"                     \
@@ -59,23 +52,11 @@
        "                               addl $12, %%esp\n"                    \
        "                               popl %2\n"                            \
        "                               addl $4, %%esp\n"                     \
-       "_hurd_intr_rpc_msg_sp_restored:"                                     \
+       "_hurd_intr_rpc_msg_out:"                                             \
        : "=a" (err), "+r" (option), "+r" (timeout), "=m" (*intr_port_p)        
      \
-       : "r" (&msg), "m" (*cancel_p), "i" (EINTR)                            \
-       : "ecx");                                                             \
+       : "r" (&msg), "m" (*cancel_p), "i" (EINTR));                          \
   err;                                                                       \
 })
-
-
-static void inline
-INTR_MSG_BACK_OUT (struct i386_thread_state *state)
-{
-  extern const void _hurd_intr_rpc_msg_cx_sp;
-  if (state->eip >= (natural_t) &_hurd_intr_rpc_msg_cx_sp)
-    state->uesp = state->ecx;
-  else
-    state->ecx = state->uesp;
-}
 
 #include "hurdfault.h"
 
diff --git a/sysdeps/mach/hurd/i386/trampoline.c 
b/sysdeps/mach/hurd/i386/trampoline.c
index 42c9d732..8f481e79 100644
--- a/sysdeps/mach/hurd/i386/trampoline.c
+++ b/sysdeps/mach/hurd/i386/trampoline.c
@@ -89,8 +89,6 @@ _hurd_setup_sighandler (struct hurd_sigstate *ss, const 
struct sigaction *action
   void trampoline (void);
   void rpc_wait_trampoline (void);
   void firewall (void);
-  extern const void _hurd_intr_rpc_msg_cx_sp;
-  extern const void _hurd_intr_rpc_msg_sp_restored;
   void *volatile sigsp;
   struct sigcontext *scp;
   struct
@@ -146,25 +144,6 @@ _hurd_setup_sighandler (struct hurd_sigstate *ss, const 
struct sigaction *action
      interrupted RPC frame.  */
   state->basic.esp = state->basic.uesp;
 
-  /* This code has intimate knowledge of the special mach_msg system call
-     done in intr-msg.c; that code does (see intr-msg.h):
-                                       movl %esp, %ecx
-                                       leal ARGS, %esp
-       _hurd_intr_rpc_msg_cx_sp:       movl $-25, %eax
-       _hurd_intr_rpc_msg_do_trap:     lcall $7, $0
-       _hurd_intr_rpc_msg_in_trap:     movl %ecx, %esp
-       _hurd_intr_rpc_msg_sp_restored:
-     We must check for the window during which %esp points at the
-     mach_msg arguments.  The space below until %ecx is used by
-     the _hurd_intr_rpc_mach_msg frame, and must not be clobbered.  */
-  if (state->basic.eip >= (int) &_hurd_intr_rpc_msg_cx_sp
-      && state->basic.eip < (int) &_hurd_intr_rpc_msg_sp_restored)
-  /* The SP now points at the mach_msg args, but there is more stack
-     space used below it.  The real SP is saved in %ecx; we must push the
-     new frame below there (if not on the altstack), and restore that value as
-     the SP on sigreturn.  */
-    state->basic.uesp = state->basic.ecx;
-
   if ((action->sa_flags & SA_ONSTACK)
       && !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
     {
-- 
2.39.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]