xref: /openbmc/linux/scripts/atomic/gen-atomic-instrumented.sh (revision e40e5298e692bb6b5a200b3f0f55e6e5adf0e5ad)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4ATOMICDIR=$(dirname $0)
5
6. ${ATOMICDIR}/atomic-tbl.sh
7
8#gen_param_check(meta, arg)
9gen_param_check()
10{
11	local meta="$1"; shift
12	local arg="$1"; shift
13	local type="${arg%%:*}"
14	local name="$(gen_param_name "${arg}")"
15	local rw="write"
16
17	case "${type#c}" in
18	i) return;;
19	esac
20
21	if [ ${type#c} != ${type} ]; then
22		# We don't write to constant parameters.
23		rw="read"
24	elif [ "${meta}" != "s" ]; then
25		# An atomic RMW: if this parameter is not a constant, and this atomic is
26		# not just a 's'tore, this parameter is both read from and written to.
27		rw="read_write"
28	fi
29
30	printf "\tinstrument_atomic_${rw}(${name}, sizeof(*${name}));\n"
31}
32
33#gen_params_checks(meta, arg...)
34gen_params_checks()
35{
36	local meta="$1"; shift
37	local order="$1"; shift
38
39	if [ "${order}" = "_release" ]; then
40		printf "\tkcsan_release();\n"
41	elif [ -z "${order}" ] && ! meta_in "$meta" "slv"; then
42		# RMW with return value is fully ordered
43		printf "\tkcsan_mb();\n"
44	fi
45
46	while [ "$#" -gt 0 ]; do
47		gen_param_check "$meta" "$1"
48		shift;
49	done
50}
51
52#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
53gen_proto_order_variant()
54{
55	local meta="$1"; shift
56	local pfx="$1"; shift
57	local name="$1"; shift
58	local sfx="$1"; shift
59	local order="$1"; shift
60	local atomic="$1"; shift
61	local int="$1"; shift
62
63	local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
64
65	local ret="$(gen_ret_type "${meta}" "${int}")"
66	local params="$(gen_params "${int}" "${atomic}" "$@")"
67	local checks="$(gen_params_checks "${meta}" "${order}" "$@")"
68	local args="$(gen_args "$@")"
69	local retstmt="$(gen_ret_stmt "${meta}")"
70
71cat <<EOF
72static __always_inline ${ret}
73${atomicname}(${params})
74{
75${checks}
76	${retstmt}arch_${atomicname}(${args});
77}
78EOF
79
80	printf "\n"
81}
82
83gen_xchg()
84{
85	local xchg="$1"; shift
86	local order="$1"; shift
87
88	kcsan_barrier=""
89	if [ "${xchg%_local}" = "${xchg}" ]; then
90		case "$order" in
91		_release)	kcsan_barrier="kcsan_release()" ;;
92		"")			kcsan_barrier="kcsan_mb()" ;;
93		esac
94	fi
95
96	if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then
97
98cat <<EOF
99#define ${xchg}${order}(ptr, oldp, ...) \\
100({ \\
101	typeof(ptr) __ai_ptr = (ptr); \\
102	typeof(oldp) __ai_oldp = (oldp); \\
103EOF
104[ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"
105cat <<EOF
106	instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \\
107	instrument_read_write(__ai_oldp, sizeof(*__ai_oldp)); \\
108	arch_${xchg}${order}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\
109})
110EOF
111
112	else
113
114cat <<EOF
115#define ${xchg}${order}(ptr, ...) \\
116({ \\
117	typeof(ptr) __ai_ptr = (ptr); \\
118EOF
119[ -n "$kcsan_barrier" ] && printf "\t${kcsan_barrier}; \\\\\n"
120cat <<EOF
121	instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \\
122	arch_${xchg}${order}(__ai_ptr, __VA_ARGS__); \\
123})
124EOF
125
126	fi
127}
128
129cat << EOF
130// SPDX-License-Identifier: GPL-2.0
131
132// Generated by $0
133// DO NOT MODIFY THIS FILE DIRECTLY
134
135/*
136 * This file provides wrappers with KASAN instrumentation for atomic operations.
137 * To use this functionality an arch's atomic.h file needs to define all
138 * atomic operations with arch_ prefix (e.g. arch_atomic_read()) and include
139 * this file at the end. This file provides atomic_read() that forwards to
140 * arch_atomic_read() for actual atomic operation.
141 * Note: if an arch atomic operation is implemented by means of other atomic
142 * operations (e.g. atomic_read()/atomic_cmpxchg() loop), then it needs to use
143 * arch_ variants (i.e. arch_atomic_read()/arch_atomic_cmpxchg()) to avoid
144 * double instrumentation.
145 */
146#ifndef _LINUX_ATOMIC_INSTRUMENTED_H
147#define _LINUX_ATOMIC_INSTRUMENTED_H
148
149#include <linux/build_bug.h>
150#include <linux/compiler.h>
151#include <linux/instrumented.h>
152
153EOF
154
155grep '^[a-z]' "$1" | while read name meta args; do
156	gen_proto "${meta}" "${name}" "atomic" "int" ${args}
157done
158
159grep '^[a-z]' "$1" | while read name meta args; do
160	gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
161done
162
163grep '^[a-z]' "$1" | while read name meta args; do
164	gen_proto "${meta}" "${name}" "atomic_long" "long" ${args}
165done
166
167
168for xchg in "xchg" "cmpxchg" "cmpxchg64" "cmpxchg128" "try_cmpxchg" "try_cmpxchg64" "try_cmpxchg128"; do
169	for order in "" "_acquire" "_release" "_relaxed"; do
170		gen_xchg "${xchg}" "${order}"
171		printf "\n"
172	done
173done
174
175for xchg in "cmpxchg_local" "cmpxchg64_local" "cmpxchg128_local" "sync_cmpxchg" "try_cmpxchg_local" "try_cmpxchg64_local" "try_cmpxchg128_local"; do
176	gen_xchg "${xchg}" ""
177	printf "\n"
178done
179
180cat <<EOF
181
182#endif /* _LINUX_ATOMIC_INSTRUMENTED_H */
183EOF
184