1  /* Copyright (C) 2005-2023 Free Software Foundation, Inc.
       2     Contributed by Ilie Garbacea <ilie@mips.com>, Chao-ying Fu <fu@mips.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  #if !defined (SYS_futex)
      31  #define SYS_futex __NR_futex
      32  #endif
      33  
      34  #define FUTEX_WAIT 0
      35  #define FUTEX_WAKE 1
      36  
      37  #ifdef __mips16
      38  static void __attribute__((noinline,nomips16))
      39  #else
      40  static inline void
      41  #endif
      42  sys_futex0 (int *addr, int op, int val)
      43  {
      44    register unsigned long __v0 asm("$2");
      45    register unsigned long __a0 asm("$4") = (unsigned long) addr;
      46    register unsigned long __a1 asm("$5") = (unsigned long) op;
      47    register unsigned long __a2 asm("$6") = (unsigned long) val;
      48    register unsigned long __a3 asm("$7") = 0;
      49  
      50    __asm volatile ("li $2, %6\n\t"
      51  		  "syscall"
      52  		  /* returns $a3 (errno), $v0 (return value) */
      53  		  : "=r" (__v0), "=r" (__a3)
      54  		  /* arguments in a0-a3, and syscall number */
      55  		  : "r" (__a0), "r" (__a1), "r" (__a2), "r" (__a3),
      56                      "IK" (SYS_futex)
      57  		  /* clobbers at, v1, t0-t9, memory */
      58  		  : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13", "$14",
      59  		    "$15", "$24", "$25", "memory");
      60  }
      61  
      62  static inline void
      63  futex_wait (int *addr, int val)
      64  {
      65    sys_futex0 (addr, FUTEX_WAIT, val);
      66  }
      67  
      68  static inline void
      69  futex_wake (int *addr, int count)
      70  {
      71    sys_futex0 (addr, FUTEX_WAKE, count);
      72  }
      73  
      74  static inline void
      75  cpu_relax (void)
      76  {
      77    __asm volatile ("" : : : "memory");
      78  }