1 /*
2  * Copyright 2013-2015 Samy Al Bahra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Copyright (c) 2012,2013 Intel Corporation
29  * Author: Andi Kleen
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that: (1) source code distributions
33  * retain the above copyright notice and this paragraph in its entirety, (2)
34  * distributions including binary code include the above copyright notice and
35  * this paragraph in its entirety in the documentation or other materials
36  * provided with the distribution
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
39  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
40  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41  */
42 
43 #ifndef CK_PR_X86_64_RTM_H
44 #define CK_PR_X86_64_RTM_H
45 
46 #ifndef CK_PR_X86_64_H
47 #error Do not include this file directly, use ck_pr.h
48 #endif
49 
50 #define CK_F_PR_RTM
51 
52 #include <ck_cc.h>
53 #include <ck_stdbool.h>
54 
55 #define CK_PR_RTM_STARTED	(~0U)
56 #define CK_PR_RTM_EXPLICIT	(1 << 0)
57 #define CK_PR_RTM_RETRY		(1 << 1)
58 #define CK_PR_RTM_CONFLICT	(1 << 2)
59 #define CK_PR_RTM_CAPACITY	(1 << 3)
60 #define CK_PR_RTM_DEBUG		(1 << 4)
61 #define CK_PR_RTM_NESTED	(1 << 5)
62 #define CK_PR_RTM_CODE(x)	(((x) >> 24) & 0xFF)
63 
64 CK_CC_INLINE static unsigned int
65 ck_pr_rtm_begin(void)
66 {
67 	unsigned int r = CK_PR_RTM_STARTED;
68 
69 	__asm__ __volatile__(".byte 0xc7,0xf8;"
70 			     ".long 0;"
71 				: "+a" (r)
72 				:
73 				: "memory");
74 
75 	return r;
76 }
77 
78 CK_CC_INLINE static void
79 ck_pr_rtm_end(void)
80 {
81 
82 	__asm__ __volatile__(".byte 0x0f,0x01,0xd5" ::: "memory");
83 	return;
84 }
85 
86 CK_CC_INLINE static void
87 ck_pr_rtm_abort(const unsigned int status)
88 {
89 
90 	__asm__ __volatile__(".byte 0xc6,0xf8,%P0" :: "i" (status) : "memory");
91 	return;
92 }
93 
94 CK_CC_INLINE static bool
95 ck_pr_rtm_test(void)
96 {
97 	bool r;
98 
99 	__asm__ __volatile__(".byte 0x0f,0x01,0xd6;"
100 			     "setnz %0"
101 				: "=r" (r)
102 				:
103 				: "memory");
104 
105 	return r;
106 }
107 
108 #endif /* CK_PR_X86_64_RTM_H */
109 
110