Просмотр исходного кода

hppa: match the kernel's SysV IPC time field order

tst-msgctl and tst-semctl fail on hppa, both because every time field
reads back as zero:

  FAIL tst-msgctl   Last sent time:  Not set    -> Msgctl get a error time!
  FAIL tst-semctl   sem_otime: Thu Jan  1 00:00:00 1970

Everything else in the structures -- uid, gid, mode, nsems, cbytes,
qnum -- is correct, so this is not a failing syscall but a structure
that is off by one word.

parisc puts the upper half of each 64-bit time value BEFORE the word
holding the value, see arch/parisc/include/uapi/asm/{msgbuf,sembuf,
shmbuf}.h:

	unsigned long	msg_stime_high;
	unsigned long	msg_stime;	/* last msgsnd time */

while libc/sysdeps/linux/common/bits/msq.h has it the other way round
(time first, then the pad).  uClibc therefore reads msg_stime_high,
which is zero for every date before 2106.  The reason is historical
rather than endianness: these arches had the padding in front of the
field to keep their 64-bit variant aligned, and the y2038 work put the
lower half where the old 32-bit field used to be, which on them is the
second word.  The kernel overrides the generic header for mips, parisc,
powerpc, sparc, x86 and xtensa; uClibc already carries its own headers
for five of those -- hppa was the one left out.

shmid_ds needs one word more than the others: parisc has an extra
__pad4 before shm_segsz.

That last one hid a bug no test reports.  tst-shmctl passes today
because uClibc places shm_segsz in front of the time block, so the two
shifts cancel out and the times land right by accident.  What was wrong
there is shm_segsz, which always read 0, plus shm_cpid, shm_lpid and
shm_nattch, all shifted by a word.  The test prints the segment size
without checking it.

Verified three ways.  Field offsets in words after the perm structure,
before and after:

  msg   stime 0 -> 1   rtime 2 -> 3   ctime 4 -> 5   (kernel 1, 3, 5)
  sem   otime 0 -> 1   ctime 2 -> 3                  (kernel 1, 3)
  shm   segsz 0 -> 7   cpid 7 -> 8    lpid 8 -> 9    (kernel 7, 8, 9)

A full build plus make install with toolchain-hppa-gcc-13 against
freshly installed parisc 6.1.60 headers, and a cross-compiled
translation unit asserting all 18 fields of the three structures against
the kernel's own headers with _Static_assert(offsetof(...) ==
offsetof(...)), which the compiler accepts.

In the TIME64 case the two halves are named so that _internal_1 is the
lower one, because msgctl(), semctl() and shmctl() reconstruct the value
as "_internal_1 | _internal_2 << 32" for every 32-bit target without
consulting the architecture.

Signed-off-by: Ramin Moussavi <lordrasmus@gmail.com>
ramin 1 неделя назад
Родитель
Сommit
390442a222

+ 122 - 0
libc/sysdeps/linux/hppa/bits/msq.h

@@ -0,0 +1,122 @@
+/* Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_MSG_H
+# error "Never use <bits/msq.h> directly; include <sys/msg.h> instead."
+#endif
+
+#include <bits/types.h>
+
+/* Define options for message queue functions.  */
+#define MSG_NOERROR	010000	/* no error if message is too big */
+#ifdef __USE_GNU
+# define MSG_EXCEPT	020000	/* recv any msg except of specified type */
+#endif
+
+/* Types used in the structure definition.  */
+typedef unsigned long int msgqnum_t;
+typedef unsigned long int msglen_t;
+
+
+/* Structure of record for one message inside the kernel.
+   The type `struct msg' is opaque.  */
+/* parisc places the upper half of each 64-bit time value BEFORE the word that
+   holds the value itself -- see arch/parisc/include/uapi/asm/{msgbuf,sembuf,
+   shmbuf}.h.  The generic layout in libc/sysdeps/linux/common/bits has it the
+   other way round, which shifted every time field by one word: msgctl() and
+   semctl() returned the always-zero upper half, so uClibc reported 1970.
+
+   The structure is spelled out once per configuration rather than stitched
+   together with conditionals inside it.  With a 32-bit time_t the field sits
+   where the kernel's lower word is and the upper one is padding; with a 64-bit
+   time_t the kernel's two words are kept as the pair it writes, _internal_1
+   being the lower half, and msgctl()/semctl()/shmctl() compose the value
+   behind the end of the kernel's structure as
+   "_internal_1 | _internal_2 << 32".  */
+
+#if (__WORDSIZE == 32 && defined(__UCLIBC_USE_TIME64__))
+
+struct msqid_ds
+{
+  struct ipc_perm msg_perm;	/* structure describing operation permission */
+  unsigned long int msg_stime_internal_2;	/* time of last msgsnd command */
+  unsigned long int msg_stime_internal_1;
+  unsigned long int msg_rtime_internal_2;	/* time of last msgrcv command */
+  unsigned long int msg_rtime_internal_1;
+  unsigned long int msg_ctime_internal_2;	/* time of last change */
+  unsigned long int msg_ctime_internal_1;
+  unsigned long int __msg_cbytes; /* current number of bytes on queue */
+  msgqnum_t msg_qnum;		/* number of messages currently on queue */
+  msglen_t msg_qbytes;		/* max number of bytes allowed on queue */
+  __pid_t msg_lspid;		/* pid of last msgsnd() */
+  __pid_t msg_lrpid;		/* pid of last msgrcv() */
+  /* The kernel fills only the pairs above.  msgctl() composes the __time_t
+     fields below from them once the call has returned -- and only then,
+     because the kernel's copy still covers the first of them.  */
+  __time_t msg_stime;
+  __time_t msg_rtime;
+  __time_t msg_ctime;
+  unsigned long int __uclibc_unused4;
+  unsigned long int __uclibc_unused5;
+};
+
+# define __MSQID_DS_TIME64_SPLIT 1
+
+#else
+
+struct msqid_ds
+{
+  struct ipc_perm msg_perm;	/* structure describing operation permission */
+  unsigned int __uclibc_pad1;
+  __time_t msg_stime;		/* time of last msgsnd command */
+  unsigned int __uclibc_pad2;
+  __time_t msg_rtime;		/* time of last msgrcv command */
+  unsigned int __uclibc_pad3;
+  __time_t msg_ctime;		/* time of last change */
+  unsigned long int __msg_cbytes; /* current number of bytes on queue */
+  msgqnum_t msg_qnum;		/* number of messages currently on queue */
+  msglen_t msg_qbytes;		/* max number of bytes allowed on queue */
+  __pid_t msg_lspid;		/* pid of last msgsnd() */
+  __pid_t msg_lrpid;		/* pid of last msgrcv() */
+  unsigned long int __uclibc_unused4;
+  unsigned long int __uclibc_unused5;
+};
+
+#endif
+
+#ifdef __USE_MISC
+
+# define msg_cbytes	__msg_cbytes
+
+/* ipcs ctl commands */
+# define MSG_STAT 11
+# define MSG_INFO 12
+
+/* buffer for msgctl calls IPC_INFO, MSG_INFO */
+struct msginfo
+  {
+    int msgpool;
+    int msgmap;
+    int msgmax;
+    int msgmnb;
+    int msgmni;
+    int msgssz;
+    int msgtql;
+    unsigned short int msgseg;
+  };
+
+#endif /* __USE_MISC */

+ 126 - 0
libc/sysdeps/linux/hppa/bits/sem.h

@@ -0,0 +1,126 @@
+/* Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_SEM_H
+# error "Never include <bits/sem.h> directly; use <sys/sem.h> instead."
+#endif
+
+#include <sys/types.h>
+#include <bits/wordsize.h>
+
+/* Flags for `semop'.  */
+#define SEM_UNDO	0x1000		/* undo the operation on exit */
+
+/* Commands for `semctl'.  */
+#define GETPID		11		/* get sempid */
+#define GETVAL		12		/* get semval */
+#define GETALL		13		/* get all semval's */
+#define GETNCNT		14		/* get semncnt */
+#define GETZCNT		15		/* get semzcnt */
+#define SETVAL		16		/* set semval */
+#define SETALL		17		/* set all semval's */
+
+
+/* Data structure describing a set of semaphores.  */
+/* parisc places the upper half of each 64-bit time value BEFORE the word that
+   holds the value itself -- see arch/parisc/include/uapi/asm/{msgbuf,sembuf,
+   shmbuf}.h.  The generic layout in libc/sysdeps/linux/common/bits has it the
+   other way round, which shifted every time field by one word: msgctl() and
+   semctl() returned the always-zero upper half, so uClibc reported 1970.
+
+   The structure is spelled out once per configuration rather than stitched
+   together with conditionals inside it.  With a 32-bit time_t the field sits
+   where the kernel's lower word is and the upper one is padding; with a 64-bit
+   time_t the kernel's two words are kept as the pair it writes, _internal_1
+   being the lower half, and msgctl()/semctl()/shmctl() compose the value
+   behind the end of the kernel's structure as
+   "_internal_1 | _internal_2 << 32".  */
+
+#if (__WORDSIZE == 32 && defined(__UCLIBC_USE_TIME64__))
+
+struct semid_ds
+{
+  struct ipc_perm sem_perm;		/* operation permission struct */
+  unsigned long int __sem_otime_internal_2;	/* last semop() time */
+  unsigned long int __sem_otime_internal_1;
+  unsigned long int __sem_ctime_internal_2;	/* last time changed by semctl() */
+  unsigned long int __sem_ctime_internal_1;
+  unsigned long int sem_nsems;		/* number of semaphores in set */
+  /* The kernel fills only the pairs above.  semctl() composes the __time_t
+     fields below from them once the call has returned -- and only then,
+     because the kernel's copy still covers the first of them.  */
+  __time_t sem_otime;
+  __time_t sem_ctime;
+  unsigned long int __uclibc_unused1;
+  unsigned long int __uclibc_unused2;
+};
+
+# define __SEMID_DS_TIME64_SPLIT 1
+
+#else
+
+struct semid_ds
+{
+  struct ipc_perm sem_perm;		/* operation permission struct */
+  unsigned int __uclibc_pad1;
+  __time_t sem_otime;			/* last semop() time */
+  unsigned int __uclibc_pad2;
+  __time_t sem_ctime;			/* last time changed by semctl() */
+  unsigned long int sem_nsems;		/* number of semaphores in set */
+  unsigned long int __uclibc_unused1;
+  unsigned long int __uclibc_unused2;
+};
+
+#endif
+
+/* The user should define a union like the following to use it for arguments
+   for `semctl'.
+
+   union semun
+   {
+     int val;				<= value for SETVAL
+     struct semid_ds *buf;		<= buffer for IPC_STAT & IPC_SET
+     unsigned short int *array;		<= array for GETALL & SETALL
+     struct seminfo *__buf;		<= buffer for IPC_INFO
+   };
+
+   Previous versions of this file used to define this union but this is
+   incorrect.  One can test the macro _SEM_SEMUN_UNDEFINED to see whether
+   one must define the union or not.  */
+#define _SEM_SEMUN_UNDEFINED	1
+
+#ifdef __USE_MISC
+
+/* ipcs ctl cmds */
+# define SEM_STAT 18
+# define SEM_INFO 19
+
+struct  seminfo
+{
+  int semmap;
+  int semmni;
+  int semmns;
+  int semmnu;
+  int semmsl;
+  int semopm;
+  int semume;
+  int semusz;
+  int semvmx;
+  int semaem;
+};
+
+#endif /* __USE_MISC */

+ 149 - 0
libc/sysdeps/linux/hppa/bits/shm.h

@@ -0,0 +1,149 @@
+/* Copyright (C) 1995,1996,1997,2000,2002,2004 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_SHM_H
+# error "Never include <bits/shm.h> directly; use <sys/shm.h> instead."
+#endif
+
+#include <bits/types.h>
+
+/* Permission flag for shmget.  */
+#define SHM_R		0400		/* or S_IRUGO from <linux/stat.h> */
+#define SHM_W		0200		/* or S_IWUGO from <linux/stat.h> */
+
+/* Flags for `shmat'.  */
+#define SHM_RDONLY	010000		/* attach read-only else read-write */
+#define SHM_RND		020000		/* round attach address to SHMLBA */
+#define SHM_REMAP	040000		/* take-over region on attach */
+
+/* Commands for `shmctl'.  */
+#define SHM_LOCK	11		/* lock segment (root only) */
+#define SHM_UNLOCK	12		/* unlock segment (root only) */
+
+__BEGIN_DECLS
+
+/* Segment low boundary address multiple.  */
+#define SHMLBA		(__getpagesize ())
+extern int __getpagesize (void) __THROW __attribute__ ((__const__));
+
+
+/* Type to count number of attaches.  */
+typedef unsigned long int shmatt_t;
+
+/* Data structure describing a set of semaphores.  */
+/* parisc places the upper half of each 64-bit time value BEFORE the word that
+   holds the value itself -- see arch/parisc/include/uapi/asm/{msgbuf,sembuf,
+   shmbuf}.h.  The generic layout in libc/sysdeps/linux/common/bits has it the
+   other way round, which shifted every time field by one word: msgctl() and
+   semctl() returned the always-zero upper half, so uClibc reported 1970.
+
+   The structure is spelled out once per configuration rather than stitched
+   together with conditionals inside it.  With a 32-bit time_t the field sits
+   where the kernel's lower word is and the upper one is padding; with a 64-bit
+   time_t the kernel's two words are kept as the pair it writes, _internal_1
+   being the lower half, and msgctl()/semctl()/shmctl() compose the value
+   behind the end of the kernel's structure as
+   "_internal_1 | _internal_2 << 32".  */
+
+#if (__WORDSIZE == 32 && defined(__UCLIBC_USE_TIME64__))
+
+struct shmid_ds
+  {
+    struct ipc_perm shm_perm;		/* operation permission struct */
+    unsigned long int __shm_atime_internal_2;	/* time of last shmat() */
+    unsigned long int __shm_atime_internal_1;
+    unsigned long int __shm_dtime_internal_2;	/* time of last shmdt() */
+    unsigned long int __shm_dtime_internal_1;
+    unsigned long int __shm_ctime_internal_2;	/* time of last change by shmctl() */
+    unsigned long int __shm_ctime_internal_1;
+    unsigned int __uclibc_pad4;		/* the kernel's __pad4, parisc only */
+    size_t shm_segsz;			/* size of segment in bytes */
+    __pid_t shm_cpid;			/* pid of creator */
+    __pid_t shm_lpid;			/* pid of last shmop */
+    shmatt_t shm_nattch;		/* number of current attaches */
+    unsigned long int __uclibc_unused4;
+    unsigned long int __uclibc_unused5;
+    /* The kernel fills only the pairs above, and its structure ends exactly
+       here.  shmctl() composes the __time_t fields below from them once the
+       call has returned.  */
+    __time_t shm_atime;
+    __time_t shm_dtime;
+    __time_t shm_ctime;
+  };
+
+# define __SHMID_DS_TIME64_SPLIT 1
+
+#else
+
+struct shmid_ds
+  {
+    struct ipc_perm shm_perm;		/* operation permission struct */
+    unsigned int __uclibc_pad1;
+    __time_t shm_atime;			/* time of last shmat() */
+    unsigned int __uclibc_pad2;
+    __time_t shm_dtime;			/* time of last shmdt() */
+    unsigned int __uclibc_pad3;
+    __time_t shm_ctime;			/* time of last change by shmctl() */
+    unsigned int __uclibc_pad4;		/* the kernel's __pad4, parisc only */
+    size_t shm_segsz;			/* size of segment in bytes */
+    __pid_t shm_cpid;			/* pid of creator */
+    __pid_t shm_lpid;			/* pid of last shmop */
+    shmatt_t shm_nattch;		/* number of current attaches */
+    unsigned long int __uclibc_unused4;
+    unsigned long int __uclibc_unused5;
+  };
+
+#endif
+
+#ifdef __USE_MISC
+
+/* ipcs ctl commands */
+# define SHM_STAT	13
+# define SHM_INFO	14
+
+/* shm_mode upper byte flags */
+# define SHM_DEST	01000	/* segment will be destroyed on last detach */
+# define SHM_LOCKED	02000   /* segment will not be swapped */
+# define SHM_HUGETLB	04000	/* segment is mapped via hugetlb */
+# define SHM_NORESERVE	010000	/* don't check for reservations */
+
+struct	shminfo
+  {
+    unsigned long int shmmax;
+    unsigned long int shmmin;
+    unsigned long int shmmni;
+    unsigned long int shmseg;
+    unsigned long int shmall;
+    unsigned long int __uclibc_unused1;
+    unsigned long int __uclibc_unused2;
+    unsigned long int __uclibc_unused3;
+    unsigned long int __uclibc_unused4;
+  };
+
+struct shm_info
+  {
+    int used_ids;
+    unsigned long int shm_tot;	/* total allocated shm */
+    unsigned long int shm_rss;	/* total resident shm */
+    unsigned long int shm_swp;	/* total swapped shm */
+    unsigned long int swap_attempts;
+    unsigned long int swap_successes;
+  };
+
+#endif /* __USE_MISC */
+
+__END_DECLS