blob: 85dc25685c0de193dceee447ad4d7347bb77f8a1 [file] [log] [blame]
Mark Rutlandace9bad2018-09-04 11:48:25 +01001#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4ATOMICDIR=$(dirname $0)
5
6. ${ATOMICDIR}/atomic-tbl.sh
7
8#gen_param_check(arg)
9gen_param_check()
10{
11 local arg="$1"; shift
12 local type="${arg%%:*}"
13 local name="$(gen_param_name "${arg}")"
14 local rw="write"
15
16 case "${type#c}" in
17 i) return;;
18 esac
19
20 # We don't write to constant parameters
21 [ ${type#c} != ${type} ] && rw="read"
22
Marco Elvered8af2e2020-01-21 17:05:09 +010023 printf "\tinstrument_atomic_${rw}(${name}, sizeof(*${name}));\n"
Mark Rutlandace9bad2018-09-04 11:48:25 +010024}
25
26#gen_param_check(arg...)
27gen_params_checks()
28{
29 while [ "$#" -gt 0 ]; do
30 gen_param_check "$1"
31 shift;
32 done
33}
34
35# gen_guard(meta, atomic, pfx, name, sfx, order)
36gen_guard()
37{
38 local meta="$1"; shift
39 local atomic="$1"; shift
40 local pfx="$1"; shift
41 local name="$1"; shift
42 local sfx="$1"; shift
43 local order="$1"; shift
44
45 local atomicname="arch_${atomic}_${pfx}${name}${sfx}${order}"
46
47 local template="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
48
49 # We definitely need a preprocessor symbol for this atomic if it is an
50 # ordering variant, or if there's a generic fallback.
51 if [ ! -z "${order}" ] || [ ! -z "${template}" ]; then
52 printf "defined(${atomicname})"
53 return
54 fi
55
56 # If this is a base variant, but a relaxed variant *may* exist, then we
57 # only have a preprocessor symbol if the relaxed variant isn't defined
58 if meta_has_relaxed "${meta}"; then
59 printf "!defined(${atomicname}_relaxed) || defined(${atomicname})"
60 fi
61}
62
63#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
64gen_proto_order_variant()
65{
66 local meta="$1"; shift
67 local pfx="$1"; shift
68 local name="$1"; shift
69 local sfx="$1"; shift
70 local order="$1"; shift
71 local atomic="$1"; shift
72 local int="$1"; shift
73
74 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
75
76 local guard="$(gen_guard "${meta}" "${atomic}" "${pfx}" "${name}" "${sfx}" "${order}")"
77
78 local ret="$(gen_ret_type "${meta}" "${int}")"
79 local params="$(gen_params "${int}" "${atomic}" "$@")"
80 local checks="$(gen_params_checks "$@")"
81 local args="$(gen_args "$@")"
82 local retstmt="$(gen_ret_stmt "${meta}")"
83
84 [ ! -z "${guard}" ] && printf "#if ${guard}\n"
85
86cat <<EOF
Marco Elverc0203952019-11-26 15:04:04 +010087static __always_inline ${ret}
Mark Rutlandace9bad2018-09-04 11:48:25 +010088${atomicname}(${params})
89{
90${checks}
91 ${retstmt}arch_${atomicname}(${args});
92}
93#define ${atomicname} ${atomicname}
94EOF
95
96 [ ! -z "${guard}" ] && printf "#endif\n"
97
98 printf "\n"
99}
100
101gen_xchg()
102{
103 local xchg="$1"; shift
104 local mult="$1"; shift
105
Peter Zijlstra29f006f2020-08-29 22:03:35 +0900106 if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then
107
Mark Rutlandace9bad2018-09-04 11:48:25 +0100108cat <<EOF
Peter Zijlstra29f006f2020-08-29 22:03:35 +0900109#define ${xchg}(ptr, oldp, ...) \\
110({ \\
111 typeof(ptr) __ai_ptr = (ptr); \\
112 typeof(oldp) __ai_oldp = (oldp); \\
113 instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
114 instrument_atomic_write(__ai_oldp, ${mult}sizeof(*__ai_oldp)); \\
115 arch_${xchg}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\
Mark Rutlandace9bad2018-09-04 11:48:25 +0100116})
117EOF
Peter Zijlstra29f006f2020-08-29 22:03:35 +0900118
119 else
120
121cat <<EOF
122#define ${xchg}(ptr, ...) \\
123({ \\
124 typeof(ptr) __ai_ptr = (ptr); \\
125 instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
126 arch_${xchg}(__ai_ptr, __VA_ARGS__); \\
127})
128EOF
129
130 fi
Mark Rutlandace9bad2018-09-04 11:48:25 +0100131}
132
133gen_optional_xchg()
134{
135 local name="$1"; shift
136 local sfx="$1"; shift
137 local guard="defined(arch_${name}${sfx})"
138
139 [ -z "${sfx}" ] && guard="!defined(arch_${name}_relaxed) || defined(arch_${name})"
140
141 printf "#if ${guard}\n"
142 gen_xchg "${name}${sfx}" ""
143 printf "#endif\n\n"
144}
145
146cat << EOF
147// SPDX-License-Identifier: GPL-2.0
148
149// Generated by $0
150// DO NOT MODIFY THIS FILE DIRECTLY
151
152/*
153 * This file provides wrappers with KASAN instrumentation for atomic operations.
154 * To use this functionality an arch's atomic.h file needs to define all
155 * atomic operations with arch_ prefix (e.g. arch_atomic_read()) and include
156 * this file at the end. This file provides atomic_read() that forwards to
157 * arch_atomic_read() for actual atomic operation.
158 * Note: if an arch atomic operation is implemented by means of other atomic
159 * operations (e.g. atomic_read()/atomic_cmpxchg() loop), then it needs to use
160 * arch_ variants (i.e. arch_atomic_read()/arch_atomic_cmpxchg()) to avoid
161 * double instrumentation.
162 */
163#ifndef _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
164#define _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
165
166#include <linux/build_bug.h>
Marco Elverc0203952019-11-26 15:04:04 +0100167#include <linux/compiler.h>
Marco Elvered8af2e2020-01-21 17:05:09 +0100168#include <linux/instrumented.h>
Mark Rutlandace9bad2018-09-04 11:48:25 +0100169
170EOF
171
172grep '^[a-z]' "$1" | while read name meta args; do
173 gen_proto "${meta}" "${name}" "atomic" "int" ${args}
174done
175
176grep '^[a-z]' "$1" | while read name meta args; do
177 gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
178done
179
Peter Zijlstra29f006f2020-08-29 22:03:35 +0900180for xchg in "xchg" "cmpxchg" "cmpxchg64" "try_cmpxchg"; do
Mark Rutlandace9bad2018-09-04 11:48:25 +0100181 for order in "" "_acquire" "_release" "_relaxed"; do
182 gen_optional_xchg "${xchg}" "${order}"
183 done
184done
185
186for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do
187 gen_xchg "${xchg}" ""
188 printf "\n"
189done
190
191gen_xchg "cmpxchg_double" "2 * "
192
193printf "\n\n"
194
195gen_xchg "cmpxchg_double_local" "2 * "
196
197cat <<EOF
198
199#endif /* _ASM_GENERIC_ATOMIC_INSTRUMENTED_H */
200EOF