This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

gUSA: "g" User Space Atomicity Emulation


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 */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]