public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: NIIBE Yutaka <gniibe@m17n.org>
To: GNU libc hacker <libc-hacker@sources.redhat.com>
Subject: gUSA: "g" User Space Atomicity Emulation
Date: Sun, 19 May 2002 18:56:00 -0000	[thread overview]
Message-ID: <200205200156.g4K1uec18188@mule.m17n.org> (raw)

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

             reply	other threads:[~2002-05-20  1:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-19 18:56 NIIBE Yutaka [this message]
2002-05-19 23:45 ` Ulrich Drepper

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=200205200156.g4K1uec18188@mule.m17n.org \
    --to=gniibe@m17n.org \
    --cc=libc-hacker@sources.redhat.com \
    /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).