Blame src/utils/asm-x86.h

Packit Service aa3af4
/*
Packit Service aa3af4
 * Copyright (c) 2001-2020 Mellanox Technologies, Ltd. All rights reserved.
Packit Service aa3af4
 *
Packit Service aa3af4
 * This software is available to you under a choice of one of two
Packit Service aa3af4
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit Service aa3af4
 * General Public License (GPL) Version 2, available from the file
Packit Service aa3af4
 * COPYING in the main directory of this source tree, or the
Packit Service aa3af4
 * BSD license below:
Packit Service aa3af4
 *
Packit Service aa3af4
 *     Redistribution and use in source and binary forms, with or
Packit Service aa3af4
 *     without modification, are permitted provided that the following
Packit Service aa3af4
 *     conditions are met:
Packit Service aa3af4
 *
Packit Service aa3af4
 *      - Redistributions of source code must retain the above
Packit Service aa3af4
 *        copyright notice, this list of conditions and the following
Packit Service aa3af4
 *        disclaimer.
Packit Service aa3af4
 *
Packit Service aa3af4
 *      - Redistributions in binary form must reproduce the above
Packit Service aa3af4
 *        copyright notice, this list of conditions and the following
Packit Service aa3af4
 *        disclaimer in the documentation and/or other materials
Packit Service aa3af4
 *        provided with the distribution.
Packit Service aa3af4
 *
Packit Service aa3af4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Service aa3af4
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Service aa3af4
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Service aa3af4
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit Service aa3af4
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service aa3af4
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service aa3af4
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit Service aa3af4
 * SOFTWARE.
Packit Service aa3af4
 */
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
#ifndef ASMX86_H_
Packit Service aa3af4
#define ASMX86_H_
Packit Service aa3af4
Packit Service aa3af4
#include <stdint.h>
Packit Service aa3af4
#include <unistd.h>
Packit Service aa3af4
#include "utils/bullseye.h"
Packit Service aa3af4
Packit Service aa3af4
#define __xg(x) ((volatile long *)(x))
Packit Service aa3af4
Packit Service aa3af4
#define mb()	 asm volatile("" ::: "memory")
Packit Service aa3af4
#define rmb()	 mb()
Packit Service aa3af4
#define wmb()	 asm volatile("" ::: "memory")
Packit Service aa3af4
#define wc_wmb() asm volatile("sfence" ::: "memory")
Packit Service aa3af4
Packit Service aa3af4
#define COPY_64B_NT(dst, src)		\
Packit Service aa3af4
	__asm__ __volatile__ (		\
Packit Service aa3af4
	" movdqa   (%1),%%xmm0\n"	\
Packit Service aa3af4
	" movdqa 16(%1),%%xmm1\n"	\
Packit Service aa3af4
	" movdqa 32(%1),%%xmm2\n"	\
Packit Service aa3af4
	" movdqa 48(%1),%%xmm3\n"	\
Packit Service aa3af4
	" movntdq %%xmm0,   (%0)\n"	\
Packit Service aa3af4
	" movntdq %%xmm1, 16(%0)\n"	\
Packit Service aa3af4
	" movntdq %%xmm2, 32(%0)\n"	\
Packit Service aa3af4
	" movntdq %%xmm3, 48(%0)\n"	\
Packit Service aa3af4
	: : "r" (dst), "r" (src) : "memory");	\
Packit Service aa3af4
	dst += 8;			\
Packit Service aa3af4
	src += 8
Packit Service aa3af4
Packit Service aa3af4
#if _BullseyeCoverage
Packit Service aa3af4
    #pragma BullseyeCoverage off
Packit Service aa3af4
#endif
Packit Service aa3af4
/**
Packit Service aa3af4
 * Atomic swap
Packit Service aa3af4
 */
Packit Service aa3af4
static inline unsigned long xchg(unsigned long x, volatile void *ptr)
Packit Service aa3af4
{
Packit Service aa3af4
	__asm__ __volatile__("xchg %0,%1"
Packit Service aa3af4
			    :"=r" (x)
Packit Service aa3af4
			    :"m" (*__xg(ptr)), "0" (x)
Packit Service aa3af4
			    :"memory");
Packit Service aa3af4
	return x;
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Atomic compare-and-swap
Packit Service aa3af4
 */
Packit Service aa3af4
static inline bool cmpxchg(unsigned long old_value, unsigned long new_value, volatile void *ptr)
Packit Service aa3af4
{
Packit Service aa3af4
	unsigned long prev_value = old_value;
Packit Service aa3af4
	__asm__ __volatile__("lock; cmpxchg %1,%2"
Packit Service aa3af4
			    : "=a"(prev_value)
Packit Service aa3af4
			    : "r"(new_value), "m"(*__xg(ptr)), "0"(old_value)
Packit Service aa3af4
			    : "memory");
Packit Service aa3af4
	return prev_value == old_value;
Packit Service aa3af4
}
Packit Service aa3af4
#if _BullseyeCoverage
Packit Service aa3af4
    #pragma BullseyeCoverage on
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Add to the atomic variable.
Packit Service aa3af4
 * @param i integer value to add.
Packit Service aa3af4
 * @param v pointer of type atomic_t.
Packit Service aa3af4
 * @return Value before add.
Packit Service aa3af4
 */
Packit Service aa3af4
static inline int atomic_fetch_and_add(int x, volatile int *ptr)
Packit Service aa3af4
{
Packit Service aa3af4
	__asm__ __volatile__("lock; xaddl %0,%1"
Packit Service aa3af4
			    : "=r"(x)
Packit Service aa3af4
			    : "m"(*ptr), "0"(x)
Packit Service aa3af4
			    : "memory");
Packit Service aa3af4
	return x;
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Read RDTSC register
Packit Service aa3af4
 */
Packit Service aa3af4
static inline void gettimeoftsc(unsigned long long *p_tscval)
Packit Service aa3af4
{
Packit Service aa3af4
	register uint32_t upper_32, lower_32;
Packit Service aa3af4
Packit Service aa3af4
	// ReaD Time Stamp Counter (RDTCS)
Packit Service aa3af4
	__asm__ __volatile__("rdtsc" : "=a" (lower_32), "=d" (upper_32));
Packit Service aa3af4
Packit Service aa3af4
	// Copy to user
Packit Service aa3af4
	*p_tscval = (((unsigned long long)upper_32) << 32) | lower_32;
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Cache Line Prefetch - Arch specific!
Packit Service aa3af4
 */
Packit Service aa3af4
#ifndef L1_CACHE_BYTES
Packit Service aa3af4
#define L1_CACHE_BYTES		64
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
static inline void prefetch(void *x)
Packit Service aa3af4
{
Packit Service aa3af4
  #if defined __i386__ || defined __x86_64__
Packit Service aa3af4
	asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
Packit Service aa3af4
  #else
Packit Service aa3af4
	{
Packit Service aa3af4
		// Use simple memcpy to get data into cache
Packit Service aa3af4
		char temp_prefetch_block[L1_CACHE_BYTES];
Packit Service aa3af4
		memcpy(temp_prefetch_block, x, L1_CACHE_BYTES);
Packit Service aa3af4
	}
Packit Service aa3af4
  #endif
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
static inline void prefetch_range(void *addr, size_t len)
Packit Service aa3af4
{
Packit Service aa3af4
	char *cp = (char*)addr;
Packit Service aa3af4
	char *end = (char*)addr + len;
Packit Service aa3af4
	for (; cp < end; cp += L1_CACHE_BYTES)
Packit Service aa3af4
		prefetch(cp);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
#endif