public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: caiyinyu <caiyinyu@loongson.cn>
To: libc-alpha@sourceware.org
Cc: caiyinyu <caiyinyu@loongson.cn>, Xi Ruoyao <xry111@xry111.site>
Subject: [PATCH] LoongArch: Ensure consistency with kernel by using union for struct members in mcontext_t and ucontext_t.
Date: Mon,  3 Apr 2023 20:01:53 +0800	[thread overview]
Message-ID: <20230403120153.3332463-1-caiyinyu@loongson.cn> (raw)

During the construction of the LoongArch Alpine system,
we found that there is an inconsistency in the member
names of mcontext_t and ucontext_t between musl and glibc,
which can cause compilation errors. After testing, we decided
to use union to keep these member names consistency.

Reference: 4fa9b3bfe6759c82beb4b043a54a3598ca467289

Co-authored-by: Xi Ruoyao <xry111@xry111.site>
---
 .../unix/sysv/linux/loongarch/makecontext.c   | 26 ++++++++--------
 .../sysv/linux/loongarch/sigcontextinfo.h     |  2 +-
 .../unix/sysv/linux/loongarch/sys/ucontext.h  | 31 ++++++++++++++++---
 .../unix/sysv/linux/loongarch/ucontext_i.sym  |  6 ++--
 4 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/loongarch/makecontext.c b/sysdeps/unix/sysv/linux/loongarch/makecontext.c
index a17f6ccc51..153be5a680 100644
--- a/sysdeps/unix/sysv/linux/loongarch/makecontext.c
+++ b/sysdeps/unix/sysv/linux/loongarch/makecontext.c
@@ -41,19 +41,19 @@ __makecontext (ucontext_t *ucp, void (*func) (void), int argc, long int a0,
      ra = s0 = 0, terminating the stack for backtracing purposes.
      s1 = the function we must call.
      s2 = the subsequent context to run.  */
-  ucp->uc_mcontext.__gregs[LARCH_REG_RA] = (uintptr_t) 0;
-  ucp->uc_mcontext.__gregs[LARCH_REG_S0] = (uintptr_t) 0;
-  ucp->uc_mcontext.__gregs[LARCH_REG_S1] = (uintptr_t) func;
-  ucp->uc_mcontext.__gregs[LARCH_REG_S2] = (uintptr_t) ucp->uc_link;
-  ucp->uc_mcontext.__gregs[LARCH_REG_SP] = (uintptr_t) sp;
-  ucp->uc_mcontext.__pc = (uintptr_t) &__start_context;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_RA] = (uintptr_t) 0;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_S0] = (uintptr_t) 0;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_S1] = (uintptr_t) func;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_S2] = (uintptr_t) ucp->uc_link;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_SP] = (uintptr_t) sp;
+  ucp->uc_mcontext.sc_pc = (uintptr_t) &__start_context;
 
   /* Put args in a0-a7, then put any remaining args on the stack.  */
-  ucp->uc_mcontext.__gregs[LARCH_REG_A0 + 0] = (uintptr_t) a0;
-  ucp->uc_mcontext.__gregs[LARCH_REG_A0 + 1] = (uintptr_t) a1;
-  ucp->uc_mcontext.__gregs[LARCH_REG_A0 + 2] = (uintptr_t) a2;
-  ucp->uc_mcontext.__gregs[LARCH_REG_A0 + 3] = (uintptr_t) a3;
-  ucp->uc_mcontext.__gregs[LARCH_REG_A0 + 4] = (uintptr_t) a4;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_A0 + 0] = (uintptr_t) a0;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_A0 + 1] = (uintptr_t) a1;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_A0 + 2] = (uintptr_t) a2;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_A0 + 3] = (uintptr_t) a3;
+  ucp->uc_mcontext.sc_regs[LARCH_REG_A0 + 4] = (uintptr_t) a4;
 
   if (__glibc_unlikely (argc > 5))
     {
@@ -62,14 +62,14 @@ __makecontext (ucontext_t *ucp, void (*func) (void), int argc, long int a0,
 
       long int reg_args = argc < LARCH_REG_NARGS ? argc : LARCH_REG_NARGS;
       for (long int i = 5; i < reg_args; i++)
-	ucp->uc_mcontext.__gregs[LARCH_REG_A0 + i] = va_arg (vl, unsigned long int);
+	ucp->uc_mcontext.sc_regs[LARCH_REG_A0 + i] = va_arg (vl, unsigned long int);
 
       long int stack_args = argc - reg_args;
       if (stack_args > 0)
 	{
 	  sp = (unsigned long int *)
 	       (((uintptr_t) sp - stack_args * sizeof (long int)) & ALMASK);
-	  ucp->uc_mcontext.__gregs[LARCH_REG_SP] = (uintptr_t) sp;
+	  ucp->uc_mcontext.sc_regs[LARCH_REG_SP] = (uintptr_t) sp;
 	  for (long int i = 0; i < stack_args; i++)
 	    sp[i] = va_arg (vl, unsigned long int);
 	}
diff --git a/sysdeps/unix/sysv/linux/loongarch/sigcontextinfo.h b/sysdeps/unix/sysv/linux/loongarch/sigcontextinfo.h
index 4cfb87da76..3d6fe08e57 100644
--- a/sysdeps/unix/sysv/linux/loongarch/sigcontextinfo.h
+++ b/sysdeps/unix/sysv/linux/loongarch/sigcontextinfo.h
@@ -26,7 +26,7 @@
 static inline uintptr_t
 sigcontext_get_pc (const ucontext_t *ctx)
 {
-  return ctx->uc_mcontext.__pc;
+  return ctx->uc_mcontext.sc_pc;
 }
 
 #endif
diff --git a/sysdeps/unix/sysv/linux/loongarch/sys/ucontext.h b/sysdeps/unix/sysv/linux/loongarch/sys/ucontext.h
index 8790265e74..6dad539ee2 100644
--- a/sysdeps/unix/sysv/linux/loongarch/sys/ucontext.h
+++ b/sysdeps/unix/sysv/linux/loongarch/sys/ucontext.h
@@ -43,22 +43,43 @@ typedef unsigned long int greg_t;
 typedef greg_t gregset_t[32];
 #endif
 
+#ifdef __USE_MISC
+# define __ctx(fld) fld
+#else
+# define __ctx(fld) __ ## fld
+#endif
+
 typedef struct mcontext_t
 {
-  unsigned long long __pc;
-  unsigned long long __gregs[32];
-  unsigned int __flags;
-  unsigned long long __extcontext[0] __attribute__((__aligned__(16)));
+  union
+    {
+      struct
+        {
+	  unsigned long long __ctx(sc_pc);
+	  unsigned long long __ctx(sc_regs)[32];
+	  unsigned int __ctx(sc_flags);
+	  unsigned long long __ctx(sc_extcontext)[0] __attribute__((__aligned__(16)));
+	};
+      struct
+        {
+	  unsigned long long __pc;
+	  unsigned long long __gregs[32];
+	  unsigned int __flags;
+	  unsigned long long __extcontext[0] __attribute__((__aligned__(16)));
+        };
+    };
 } mcontext_t;
 
 /* Userlevel context.  */
 typedef struct ucontext_t
 {
-  unsigned long int __uc_flags;
+  unsigned long int __ctx(uc_flags);
   struct ucontext_t *uc_link;
   stack_t uc_stack;
   sigset_t uc_sigmask;
   mcontext_t uc_mcontext;
 } ucontext_t;
 
+#undef __ctx
+
 #endif /* sys/ucontext.h */
diff --git a/sysdeps/unix/sysv/linux/loongarch/ucontext_i.sym b/sysdeps/unix/sysv/linux/loongarch/ucontext_i.sym
index f27afad56f..dfe5199542 100644
--- a/sysdeps/unix/sysv/linux/loongarch/ucontext_i.sym
+++ b/sysdeps/unix/sysv/linux/loongarch/ucontext_i.sym
@@ -15,7 +15,7 @@ _NSIG8				(_NSIG / 8)
 #define stack(member)		ucontext (uc_stack.member)
 #define mcontext(member)	ucontext (uc_mcontext.member)
 
-UCONTEXT_FLAGS			ucontext (__uc_flags)
+UCONTEXT_FLAGS			ucontext (uc_flags)
 UCONTEXT_LINK			ucontext (uc_link)
 UCONTEXT_STACK			ucontext (uc_stack)
 UCONTEXT_MCONTEXT		ucontext (uc_mcontext)
@@ -25,7 +25,7 @@ STACK_SP			stack (ss_sp)
 STACK_SIZE			stack (ss_size)
 STACK_FLAGS			stack (ss_flags)
 
-MCONTEXT_PC			mcontext (__pc)
-MCONTEXT_GREGS			mcontext (__gregs)
+MCONTEXT_PC			mcontext (sc_pc)
+MCONTEXT_GREGS			mcontext (sc_regs)
 
 UCONTEXT_SIZE			sizeof (ucontext_t)
-- 
2.31.1


             reply	other threads:[~2023-04-03 12:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-03 12:01 caiyinyu [this message]
2023-04-04 17:38 ` Szabolcs Nagy
2023-04-06  9:59   ` caiyinyu
2023-04-06 11:02     ` Szabolcs Nagy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230403120153.3332463-1-caiyinyu@loongson.cn \
    --to=caiyinyu@loongson.cn \
    --cc=libc-alpha@sourceware.org \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).