Linux Audio

Check our new training course

Loading...
v5.14.15
  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
 38	while [ "$#" -gt 0 ]; do
 39		gen_param_check "$meta" "$1"
 40		shift;
 41	done
 42}
 43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 44#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...)
 45gen_proto_order_variant()
 46{
 47	local meta="$1"; shift
 48	local pfx="$1"; shift
 49	local name="$1"; shift
 50	local sfx="$1"; shift
 51	local order="$1"; shift
 52	local atomic="$1"; shift
 53	local int="$1"; shift
 54
 55	local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
 56
 
 
 57	local ret="$(gen_ret_type "${meta}" "${int}")"
 58	local params="$(gen_params "${int}" "${atomic}" "$@")"
 59	local checks="$(gen_params_checks "${meta}" "$@")"
 60	local args="$(gen_args "$@")"
 61	local retstmt="$(gen_ret_stmt "${meta}")"
 62
 
 
 63cat <<EOF
 64static __always_inline ${ret}
 65${atomicname}(${params})
 66{
 67${checks}
 68	${retstmt}arch_${atomicname}(${args});
 69}
 
 70EOF
 71
 
 
 72	printf "\n"
 73}
 74
 75gen_xchg()
 76{
 77	local xchg="$1"; shift
 78	local mult="$1"; shift
 79
 80	if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then
 81
 82cat <<EOF
 83#define ${xchg}(ptr, oldp, ...) \\
 84({ \\
 85	typeof(ptr) __ai_ptr = (ptr); \\
 86	typeof(oldp) __ai_oldp = (oldp); \\
 87	instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
 88	instrument_atomic_write(__ai_oldp, ${mult}sizeof(*__ai_oldp)); \\
 89	arch_${xchg}(__ai_ptr, __ai_oldp, __VA_ARGS__); \\
 90})
 91EOF
 
 92
 93	else
 
 
 
 
 94
 95cat <<EOF
 96#define ${xchg}(ptr, ...) \\
 97({ \\
 98	typeof(ptr) __ai_ptr = (ptr); \\
 99	instrument_atomic_write(__ai_ptr, ${mult}sizeof(*__ai_ptr)); \\
100	arch_${xchg}(__ai_ptr, __VA_ARGS__); \\
101})
102EOF
103
104	fi
 
 
105}
106
107cat << EOF
108// SPDX-License-Identifier: GPL-2.0
109
110// Generated by $0
111// DO NOT MODIFY THIS FILE DIRECTLY
112
113/*
114 * This file provides wrappers with KASAN instrumentation for atomic operations.
115 * To use this functionality an arch's atomic.h file needs to define all
116 * atomic operations with arch_ prefix (e.g. arch_atomic_read()) and include
117 * this file at the end. This file provides atomic_read() that forwards to
118 * arch_atomic_read() for actual atomic operation.
119 * Note: if an arch atomic operation is implemented by means of other atomic
120 * operations (e.g. atomic_read()/atomic_cmpxchg() loop), then it needs to use
121 * arch_ variants (i.e. arch_atomic_read()/arch_atomic_cmpxchg()) to avoid
122 * double instrumentation.
123 */
124#ifndef _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
125#define _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
126
127#include <linux/build_bug.h>
128#include <linux/compiler.h>
129#include <linux/instrumented.h>
130
131EOF
132
133grep '^[a-z]' "$1" | while read name meta args; do
134	gen_proto "${meta}" "${name}" "atomic" "int" ${args}
135done
136
137grep '^[a-z]' "$1" | while read name meta args; do
138	gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
139done
140
141for xchg in "xchg" "cmpxchg" "cmpxchg64" "try_cmpxchg"; do
142	for order in "" "_acquire" "_release" "_relaxed"; do
143		gen_xchg "${xchg}${order}" ""
144		printf "\n"
145	done
146done
147
148for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do
149	gen_xchg "${xchg}" ""
150	printf "\n"
151done
152
153gen_xchg "cmpxchg_double" "2 * "
154
155printf "\n\n"
156
157gen_xchg "cmpxchg_double_local" "2 * "
158
159cat <<EOF
160
161#endif /* _ASM_GENERIC_ATOMIC_INSTRUMENTED_H */
162EOF
v5.4
  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(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
 23	printf "\tkasan_check_${rw}(${name}, sizeof(*${name}));\n"
 24}
 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
 87static inline ${ret}
 88${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
 
 
106cat <<EOF
107#define ${xchg}(ptr, ...)						\\
108({									\\
109	typeof(ptr) __ai_ptr = (ptr);					\\
110	kasan_check_write(__ai_ptr, ${mult}sizeof(*__ai_ptr));		\\
111	arch_${xchg}(__ai_ptr, __VA_ARGS__);				\\
 
 
112})
113EOF
114}
115
116gen_optional_xchg()
117{
118	local name="$1"; shift
119	local sfx="$1"; shift
120	local guard="defined(arch_${name}${sfx})"
121
122	[ -z "${sfx}" ] && guard="!defined(arch_${name}_relaxed) || defined(arch_${name})"
 
 
 
 
 
 
 
123
124	printf "#if ${guard}\n"
125	gen_xchg "${name}${sfx}" ""
126	printf "#endif\n\n"
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 _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
147#define _ASM_GENERIC_ATOMIC_INSTRUMENTED_H
148
149#include <linux/build_bug.h>
150#include <linux/kasan-checks.h>
 
151
152EOF
153
154grep '^[a-z]' "$1" | while read name meta args; do
155	gen_proto "${meta}" "${name}" "atomic" "int" ${args}
156done
157
158grep '^[a-z]' "$1" | while read name meta args; do
159	gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
160done
161
162for xchg in "xchg" "cmpxchg" "cmpxchg64"; do
163	for order in "" "_acquire" "_release" "_relaxed"; do
164		gen_optional_xchg "${xchg}" "${order}"
 
165	done
166done
167
168for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do
169	gen_xchg "${xchg}" ""
170	printf "\n"
171done
172
173gen_xchg "cmpxchg_double" "2 * "
174
175printf "\n\n"
176
177gen_xchg "cmpxchg_double_local" "2 * "
178
179cat <<EOF
180
181#endif /* _ASM_GENERIC_ATOMIC_INSTRUMENTED_H */
182EOF