1 /* Machine-dependent pthreads configuration and inline functions.
2    x86-64 version.
3    Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef _PT_MACHINE_H
21 #define _PT_MACHINE_H   1
22 
23 #include <features.h>
24 
25 #ifndef __ASSEMBLER__
26 # include <stddef.h>	/* For offsetof.  */
27 # include <stdlib.h>	/* For abort().  */
28 # include <asm/prctl.h>
29 
30 
31 # ifndef PT_EI
32 #  define PT_EI __extern_always_inline
33 # endif
34 
35 extern long int testandset (int *);
36 extern int __compare_and_swap (long int *, long int, long int);
37 
38 /* Get some notion of the current stack.  Need not be exactly the top
39    of the stack, just something somewhere in the current frame.  */
40 # define CURRENT_STACK_FRAME  stack_pointer
41 register char * stack_pointer __asm__ ("%rsp") __attribute_used__;
42 
43 
44 /* Spinlock implementation; required.  */
45 PT_EI long int
testandset(int * __spinlock)46 testandset (int *__spinlock)
47 {
48   long int ret;
49 
50   __asm__ __volatile__ (
51 	"xchgl %k0, %1"
52 	: "=r"(ret), "=m"(*__spinlock)
53 	: "0"(1), "m"(*__spinlock)
54 	: "memory");
55 
56   return ret;
57 }
58 
59 
60 /* Compare-and-swap for semaphores.  */
61 # define HAS_COMPARE_AND_SWAP
62 
63 PT_EI int
__compare_and_swap(long int * __p,long int __oldval,long int __newval)64 __compare_and_swap (long int *__p, long int __oldval, long int __newval)
65 {
66   char ret;
67   long int readval;
68 
69   __asm__ __volatile__ ("lock; cmpxchgq %3, %1; sete %0"
70 			: "=q" (ret), "=m" (*__p), "=a" (readval)
71 			: "r" (__newval), "m" (*__p), "a" (__oldval)
72 			: "memory");
73   return ret;
74 }
75 
76 #endif /* !__ASSEMBLER__ */
77 
78 /* We want the OS to assign stack addresses.  */
79 #define FLOATING_STACKS	1
80 
81 /* Maximum size of the stack if the rlimit is unlimited.  */
82 #define ARCH_STACK_MAX_SIZE	32*1024*1024
83 
84 /* The ia32e really want some help to prevent overheating.  */
85 #define BUSY_WAIT_NOP	__asm__ ("rep; nop")
86 
87 #endif /* pt-machine.h */
88