]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/l4sys/lib/src/ARCH-ppc32/atomic_ops.c
Update
[l4.git] / l4 / pkg / l4re-core / l4sys / lib / src / ARCH-ppc32 / atomic_ops.c
1 /*
2  * (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction.  Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License.  This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 #include <l4/sys/kdebug.h>
19
20 long int
21 l4_atomic_add(volatile long int* mem, long int offset)
22 {
23   long int ret = 0;
24   asm volatile ( " 1:                            \n"
25                  "  lwarx  %%r12, 0, %[ptr]      \n" //reserve
26                  "  add    %[ret], %%r12, %[val] \n" //add
27                  "  stwcx. %[ret], 0, %[ptr]     \n" //store if still reserved
28                  "  bne-   1b                    \n" //repeat if store failed
29                  : [ret]"=r" (ret),
30                    [ptr]"=r" (mem),
31                    [val]"=r" (offset)
32                  : "0" (ret),
33                    "1" (mem),
34                    "2" (offset)
35                  : "r12", "memory"
36                );
37   return ret;
38 }
39
40 long int
41 l4_atomic_cmpxchg(volatile long int* mem, long int oldval, long int newval)
42 {
43   long int ret = 0;
44   asm volatile ( " 1:                            \n"
45                  "  lwarx  %%r12, 0, %[ptr]      \n"
46                  "  cmpw   %[oldval], %%r12      \n"
47                  "  bne-   2f                    \n"
48                  "  stwcx. %[newval], 0,%[ptr]   \n"
49                  "  bne-   1b                    \n"
50                  " 2:                            \n"
51                  "  mr     %[ret], %%r12         \n"
52                  : [ret] "=r"(ret),
53                    [ptr] "=r"(mem),
54                    [oldval] "=r"(oldval),
55                    [newval] "=r"(newval)
56                  : "0" (ret),
57                    "1" (mem),
58                    "2" (oldval),
59                    "3" (newval)
60                  : "memory", "r12"
61                );
62   return (oldval == ret);
63 }
64
65 long int
66 l4_atomic_xchg(volatile long int* mem, long int newval)
67 {
68   // someone not speaking ppc has added this...
69   unsigned long r = *mem;
70   outstring("l4_atomic_xchg is not atomic!\n");
71   *mem = newval;
72   return r;
73 }