public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* gUSA: "g" User Space Atomicity Emulation
@ 2002-05-19 18:56 NIIBE Yutaka
  2002-05-19 23:45 ` Ulrich Drepper
  0 siblings, 1 reply; 2+ messages in thread
From: NIIBE Yutaka @ 2002-05-19 18:56 UTC (permalink / raw)
  To: GNU libc hacker

While I considered implementation of atomicity.h for SuperH, I got a
new idea.  I named it "gUSA", where g stands for generic (it can be
applied to other processors), general (it can be applied to other OS),
and etc.

gUSA is user space atomicity emulation for uniprocessor system which
doesn't have support of memory lock operation such as ll/sc.

The idea and the implementation _both_ are quite simple.  When kernel
preempts (or signal) the critical region, we let the control go back
to reentrance point.  To do that, we need to define ABI of critical
region, and modify kernel slightly (in case of Linux, do_signal and
resched).

Here's an implementation for SuperH.

Stack pointer (r15) < 0 means control is in the critical region.

On rescheduling, if kernel finds it's in the critical region and
regs->pc < r0, it sets regs->pc = r0+r15 (re-entrance point #2).
On signaling, if kernel finds it's in the critical region, and
regs->pc < r0, it sets regs->r15 = r1 (saved stack pointer)
and regs->pc = r0 + r15 -2 (re-entrance point #1) before the
setting of signal frame (so that it goes re-entrance point #1
after signal handler).

Coments are welcome.  Thanks in advance.

2002-05-20  NIIBE Yutaka  <gniibe@m17n.org>

	* sysdeps/unix/sysv/linux/sh/atomicity.h: New file.

--- /dev/null	Sat Mar 16 15:37:17 2002
+++ sysdeps/unix/sysv/linux/sh/atomicity.h	Mon May 20 10:30:59 2002
@@ -0,0 +1,110 @@
+/* Low-level functions for atomic operations.  SuperH version.
+   Copyright (C) 2002 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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+/* This is an implementation of gUSA ("g" User Space Atomicity) for SuperH
+
+   Reference: Niibe Yutaka, 
+	"gUSA: Simple and Efficient User Space Atomicity Emulation
+	       with Little Kernel Modification"
+	(to be written for Linux Conference 2002, Japan)
+
+   SuperH ABI:
+	r15:	< 0
+	r0:	end point
+	r1:	saved stack pointer
+	r0 + r15 - 2 = re-enterance point #1 (signal)
+	r0 + r15 = re-entrance point #2 (preemption)
+ */
+
+#ifndef _ATOMICITY_H
+#define _ATOMICITY_H	1
+
+#include <inttypes.h>
+
+static inline int
+__attribute__ ((unused))
+exchange_and_add (volatile uint32_t *mem, int val)
+{
+  register unsigned long end_r0 __asm__ ("r0");
+  register unsigned long ssp_r1 __asm__ ("r1");
+  unsigned long dummy;
+  int result;
+
+  __asm__ (".align 2\n\t"
+       "mova	1f,%0\n\t"
+       "mov	r15,%1\n\t"
+       "mov	#-6,r15		! critical region start: reentrance point #1\n"
+   "0:	mov.l	@%4,%2		! reentrance point #2\n\t"
+       "add	%2,%3\n\t"
+       "mov.l	%3,@%5\n"
+   "1:	mov	%1,r15		! critical region end"
+	   : "=&r" (end_r0), "=&r" (ssp_r1), "=&r" (result), "=r" (dummy)
+	   : "r" (mem), "3" (val)
+	   : "memory");
+
+  return result;
+}
+
+static inline void
+__attribute__ ((unused))
+atomic_add (volatile uint32_t *mem, int val)
+{
+  register unsigned long end_r0 __asm__ ("r0");
+  register unsigned long ssp_r1 __asm__ ("r1");
+  unsigned long dummy;
+
+  __asm__ (".align 2\n\t"
+       "mova	1f,%0\n\t"
+       "mov	r15,%1\n\t"
+       "mov	#-6,r15		! critical region start: reentrance point #1\n"
+   "0:	mov.l	@%3,%2		! reentrance point #2\n\t"
+       "add	%4,%2\n\t"
+       "mov.l	%2,@%3\n"
+   "1:	mov	%1,r15		! critical region end"
+	   : "=&r" (end_r0), "=&r" (ssp_r1), "=&r" (dummy)
+	   : "r" (mem), "r" (val)
+	   : "memory");
+}
+
+static inline int
+__attribute__ ((unused))
+compare_and_swap (volatile long int *p, long int oldval, long int newval)
+{
+  register unsigned long end_r0 __asm__ ("r0");
+  register unsigned long ssp_r1 __asm__ ("r1");
+  int value;
+
+  __asm__ (".align 2\n\t"
+       "mova	1f,%0\n\t"
+       "mov	r15,%1\n\t"
+       "mov	#-10,r15	! critical region start: reentrance point #1\n"
+   "0:	mov.l	@%3,%2		! reentrance point #2\n\t"
+       "cmp/eq	%2,%4\n\t"
+       "bf/s	1f\n\t"
+       "movt	%2\n\t"
+       "mov.l	%5,@%3\n"
+   "1:	mov	%1,r15		! critical region end\n"
+	   : "=&r" (end_r0), "=&r" (ssp_r1), "=&r" (value)
+	   : "r" (p), "r" (oldval), "r" (newval)
+	   : "memory","t");
+
+  return value;
+}
+
+#endif /* atomicity.h */

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: gUSA: "g" User Space Atomicity Emulation
  2002-05-19 18:56 gUSA: "g" User Space Atomicity Emulation NIIBE Yutaka
@ 2002-05-19 23:45 ` Ulrich Drepper
  0 siblings, 0 replies; 2+ messages in thread
From: Ulrich Drepper @ 2002-05-19 23:45 UTC (permalink / raw)
  To: NIIBE Yutaka; +Cc: GNU libc hacker

[-- Attachment #1: Type: text/plain, Size: 629 bytes --]

On Sun, 2002-05-19 at 18:56, NIIBE Yutaka wrote:

> Coments are welcome.

This is foremost a kernel issue.  If the kernel people decide on a
common scheme to handle atomicity for architecutres without support I'm
sure we can come up with a set of user-level functions which is
uniform.  Your stack pointer hack seems not too bad but the cost of the
extra check in every syscall etc seems high.

-- 
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 232 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-05-20  6:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-19 18:56 gUSA: "g" User Space Atomicity Emulation NIIBE Yutaka
2002-05-19 23:45 ` Ulrich Drepper

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).