1 /* Copyright (C) 2005-2019 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3 
4    This file is part of the GNU Offloading and Multi Processing Library
5    (libgomp).
6 
7    Libgomp is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15    more details.
16 
17    Under Section 7 of GPL version 3, you are granted additional
18    permissions described in the GCC Runtime Library Exception, version
19    3.1, as published by the Free Software Foundation.
20 
21    You should have received a copy of the GNU General Public License and
22    a copy of the GCC Runtime Library Exception along with this program;
23    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24    <http://www.gnu.org/licenses/>.  */
25 
26 /* Provide target-specific access to the futex system call.  */
27 
28 #include <sys/syscall.h>
29 
30 
31 
32 static inline long
sys_futex0(int * addr,int op,int val)33 sys_futex0(int *addr, int op, int val)
34 {
35   register long out0 asm ("out0") = (long) addr;
36   register long out1 asm ("out1") = op;
37   register long out2 asm ("out2") = val;
38   register long out3 asm ("out3") = 0;
39   register long r8 asm ("r8");
40   register long r10 asm ("r10");
41   register long r15 asm ("r15") = SYS_futex;
42 
43   __asm __volatile ("break 0x100000"
44 	: "=r"(r15), "=r"(out0), "=r"(out1), "=r"(out2), "=r"(out3),
45 	  "=r"(r8), "=r"(r10)
46 	: "r"(r15), "r"(out0), "r"(out1), "r"(out2), "r"(out3)
47 	: "memory", "out4", "out5", "out6", "out7",
48 	  /* Non-stacked integer registers, minus r8, r10, r12, r15.  */
49 	  "r2", "r3", "r9", "r11", "r13", "r14", "r16", "r17", "r18",
50 	  "r19", "r20", "r21", "r22", "r23", "r24", "r25", "r26", "r27",
51 	  "r28", "r29", "r30", "r31",
52 	  /* Predicate registers.  */
53 	  "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
54 	  /* Non-rotating fp registers.  */
55 	  "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
56 	  /* Branch registers.  */
57 	  "b6");
58   return r8 & r10;
59 }
60 
61 static inline void
futex_wait(int * addr,int val)62 futex_wait (int *addr, int val)
63 {
64   long err = sys_futex0 (addr, gomp_futex_wait, val);
65   if (__builtin_expect (err == ENOSYS, 0))
66     {
67       gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
68       gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
69       sys_futex0 (addr, gomp_futex_wait, val);
70     }
71 }
72 
73 static inline void
futex_wake(int * addr,int count)74 futex_wake (int *addr, int count)
75 {
76   long err = sys_futex0 (addr, gomp_futex_wake, count);
77   if (__builtin_expect (err == ENOSYS, 0))
78     {
79       gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
80       gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
81       sys_futex0 (addr, gomp_futex_wake, count);
82     }
83 }
84 
85 static inline void
cpu_relax(void)86 cpu_relax (void)
87 {
88   __asm volatile ("hint @pause" : : : "memory");
89 }
90