1/* os-unix-sysdep.c                  -*-C-*-
2 *
3 *************************************************************************
4 *
5 *  Copyright (C) 2009-2014, Intel Corporation
6 *  All rights reserved.
7 *
8 *  Redistribution and use in source and binary forms, with or without
9 *  modification, are permitted provided that the following conditions
10 *  are met:
11 *
12 *    * Redistributions of source code must retain the above copyright
13 *      notice, this list of conditions and the following disclaimer.
14 *    * Redistributions in binary form must reproduce the above copyright
15 *      notice, this list of conditions and the following disclaimer in
16 *      the documentation and/or other materials provided with the
17 *      distribution.
18 *    * Neither the name of Intel Corporation nor the names of its
19 *      contributors may be used to endorse or promote products derived
20 *      from this software without specific prior written permission.
21 *
22 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
32 *  WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 *  POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************
35 *
36 * This file contains system-specific code for Unix systems
37 */
38
39#include "os.h"
40#include "sysdep.h"
41#include <internal/abi.h>
42
43// On x86 processors (but not MIC processors), the compiler generated code to
44// save the FP state (rounding mode and the like) before calling setjmp.  We
45// will need to restore that state when we resume.
46#ifndef __MIC__
47# if defined(__i386__) || defined(__x86_64)
48#   define RESTORE_X86_FP_STATE
49# endif // defined(__i386__) || defined(__x86_64)
50#endif  // __MIC__
51
52/* timer support */
53COMMON_SYSDEP unsigned long long __cilkrts_getticks(void)
54{
55#if defined __i386__ || defined __x86_64
56    unsigned a, d;
57    __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
58    return ((unsigned long long)a) | (((unsigned long long)d) << 32);
59#else
60#   warning "unimplemented cycle counter"
61    return 0;
62#endif
63}
64
65COMMON_SYSDEP void __cilkrts_short_pause(void)
66{
67#if __ICC >= 1110
68#   if __MIC__ || __MIC2__
69    _mm_delay_32(16); // stall for 16 cycles
70#   else
71    _mm_pause();
72#   endif
73#elif defined __i386__ || defined __x86_64
74    __asm__("pause");
75#else
76#   warning __cilkrts_short_pause empty
77#endif
78}
79
80COMMON_SYSDEP int __cilkrts_xchg(volatile int *ptr, int x)
81{
82#if defined __i386__ || defined __x86_64
83    /* asm statement here works around icc bugs */
84    __asm__("xchgl %0,%a1" :"=r" (x) : "r" (ptr), "0" (x) :"memory");
85#else
86    x = __sync_lock_test_and_set(ptr, x);
87#endif
88    return x;
89}
90
91/*
92 * The Intel compiler distribution assumes newer CPUs and doesn't yet support
93 * the __builtin_cpu_supports intrinsic added by GCC 4.8, so just return 1 in
94 * that environment.
95 *
96 * This declaration should generate an error when the Intel compiler adds
97 * supprt for the intrinsic.
98 */
99#ifdef __INTEL_COMPILER
100static inline int __builtin_cpu_supports(const char *feature)
101{
102    return 1;
103}
104#endif
105
106/*
107 * Restore the floating point state that is stored in a stack frame at each
108 * spawn.  This should be called each time a frame is resumed.
109 *
110 * Only valid for IA32 and Intel64 processors.
111 */
112void restore_x86_fp_state (__cilkrts_stack_frame *sf) {
113#ifdef RESTORE_X86_FP_STATE
114    if (__builtin_cpu_supports("sse"))
115    {
116        __asm__ ("ldmxcsr %0"
117                 :
118                 : "m" (sf->mxcsr));
119    }
120    __asm__ ("fnclex\n\t"
121             "fldcw %0"
122             :
123             : "m" (sf->fpcsr));
124#endif
125}
126
127
128void sysdep_save_fp_ctrl_state(__cilkrts_stack_frame *sf)
129{
130// If we're not going to restore, don't bother saving it
131#ifdef RESTORE_X86_FP_STATE
132    if (CILK_FRAME_VERSION_VALUE(sf->flags) >= 1)
133    {
134        if (__builtin_cpu_supports("sse"))
135        {
136            __asm__ ("stmxcsr %0" : "=m" (sf->mxcsr));
137        }
138        __asm__ ("fnstsw %0" : "=m" (sf->fpcsr));
139    }
140#endif
141}
142
143