]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4sys/include/ARCH-arm/__semaphore_impl.h
bcabf742d9230bc5c033d06edfa97a8b6d264c45
[l4.git] / l4 / pkg / l4sys / include / ARCH-arm / __semaphore_impl.h
1 /**
2  * \file
3  * \brief  User-lock implementation for ARM
4  */
5 /*
6  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
7  *               Alexander Warg <warg@os.inf.tu-dresden.de>
8  *     economic rights: Technische Universität Dresden (Germany)
9  *
10  * This file is part of TUD:OS and distributed under the terms of the
11  * GNU General Public License 2.
12  * Please see the COPYING-GPL-2 file for details.
13  *
14  * As a special exception, you may use this file as part of a free software
15  * library without restriction.  Specifically, if other files instantiate
16  * templates or use macros or inline functions from this file, or you compile
17  * this file and link it with other files to produce an executable, this
18  * file does not by itself cause the resulting executable to be covered by
19  * the GNU General Public License.  This exception does not however
20  * invalidate any other reasons why the executable file might be covered by
21  * the GNU General Public License.
22  */
23 #pragma once
24
25 #include <l4/sys/utcb.h>
26 // dumb, however atomic sequences are defined in kdebug.h
27 #include <l4/sys/kdebug.h>
28
29 #include <l4/sys/compiler.h>
30 #include <l4/sys/ipc.h>
31
32 L4_INLINE l4_msgtag_t
33 l4_usem_down_to(l4_cap_idx_t lock, l4_u_semaphore_t *sem, l4_timeout_s timeout) L4_NOTHROW
34 {
35   do
36     {
37       if (__builtin_expect(l4_atomic_add((long*)&(sem->counter), -1) >= 0, 1))
38         return l4_msgtag(0, 0, 0, 0);
39
40       l4_utcb_mr()->mr[0] = (l4_addr_t)sem;
41
42       register unsigned long _lock    __asm__ ("r2") = lock | L4_SYSF_CALL;
43       register unsigned long _timeout __asm__ ("r3") = timeout.t;
44       register unsigned long _flags   __asm__ ("r4") = 0;
45       register l4_msgtag_t   _tag     __asm__ ("r0") =
46         l4_msgtag(0 /*L4_SEMAPHORE_DOWN*/, 1, 0, 0);
47
48       __asm__ __volatile__
49         ("@ l4_usem_down_to (semaphore block) \n\t"
50          PIC_SAVE_ASM
51          "mov     lr, pc       \n\t"
52          "mov     pc, %[sys]   \n\t"
53          PIC_RESTORE_ASM
54          :
55          "=r" (_lock),
56          "=r" (_timeout),
57          "=r" (_flags),
58          "=r" (_tag.raw)
59          :
60          "0" (_lock),
61          "1" (_timeout),
62          "2" (_flags),
63          "3" (_tag.raw),
64          [sys] "i" (L4_SYSCALL_INVOKE)
65          :
66          "cc", "memory", "lr"
67         );
68
69       if (l4_msgtag_label(_tag) != L4_USEM_RETRY)
70         {
71           l4_msgtag_t t;
72           t.raw = _tag.raw;
73           return t;
74         }
75     }
76   while (1);
77 }
78
79 L4_INLINE l4_msgtag_t
80 l4_usem_up(l4_cap_idx_t lock, l4_u_semaphore_t *sem) L4_NOTHROW
81 {
82   l4_atomic_add((long*)&(sem->counter), 1);
83
84   if (__builtin_expect(sem->flags == 0, 1))
85     return l4_msgtag(0,0,0,0);
86
87   l4_utcb_mr()->mr[0] = (l4_addr_t)sem;
88
89   register unsigned long _lock    __asm__ ("r2") = lock | L4_SYSF_CALL;
90   register unsigned long _timeout __asm__ ("r3") = 0;
91   register unsigned long _flags   __asm__ ("r4") = 0;
92   register unsigned long _tag     __asm__ ("r0") =
93     l4_msgtag(1 /* L4_SEMAPHORE_UP_WITHOUT_YIELD */, 1, 0, 0).raw;
94
95   __asm__ __volatile__
96     ("@ l4_usem_up (semaphore block) \n\t"
97      PIC_SAVE_ASM
98      "mov     lr, pc       \n\t"
99      "mov     pc, %[sys]   \n\t"
100      PIC_RESTORE_ASM
101      :
102      "=r"(_lock),
103      "=r"(_timeout),
104      "=r"(_flags),
105      "=r"(_tag)
106      :
107      "0"(_lock),
108      "1"(_timeout),
109      "2"(_flags),
110      "3"(_tag),
111      [sys] "i" (L4_SYSCALL_INVOKE)
112      :
113      "cc", "memory", "lr");
114
115   l4_msgtag_t t; t.raw = _tag;
116   return t;
117 }
118