Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | |
| 4 | ATOMICDIR=$(dirname $0) |
| 5 | |
| 6 | . ${ATOMICDIR}/atomic-tbl.sh |
| 7 | |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 8 | #gen_param_check(meta, arg) |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 9 | gen_param_check() |
| 10 | { |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 11 | local meta="$1"; shift |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 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 | |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 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 |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 29 | |
Marco Elver | ed8af2e | 2020-01-21 17:05:09 +0100 | [diff] [blame] | 30 | printf "\tinstrument_atomic_${rw}(${name}, sizeof(*${name}));\n" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 33 | #gen_params_checks(meta, arg...) |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 34 | gen_params_checks() |
| 35 | { |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 36 | local meta="$1"; shift |
| 37 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 38 | while [ "$#" -gt 0 ]; do |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 39 | gen_param_check "$meta" "$1" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 40 | shift; |
| 41 | done |
| 42 | } |
| 43 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 44 | #gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, arg...) |
| 45 | gen_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 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 57 | local ret="$(gen_ret_type "${meta}" "${int}")" |
| 58 | local params="$(gen_params "${int}" "${atomic}" "$@")" |
Marco Elver | 3570a1b | 2020-07-24 09:00:08 +0200 | [diff] [blame] | 59 | local checks="$(gen_params_checks "${meta}" "$@")" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 60 | local args="$(gen_args "$@")" |
| 61 | local retstmt="$(gen_ret_stmt "${meta}")" |
| 62 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 63 | cat <<EOF |
Marco Elver | c020395 | 2019-11-26 15:04:04 +0100 | [diff] [blame] | 64 | static __always_inline ${ret} |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 65 | ${atomicname}(${params}) |
| 66 | { |
| 67 | ${checks} |
| 68 | ${retstmt}arch_${atomicname}(${args}); |
| 69 | } |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 70 | EOF |
| 71 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 72 | printf "\n" |
| 73 | } |
| 74 | |
| 75 | gen_xchg() |
| 76 | { |
| 77 | local xchg="$1"; shift |
| 78 | local mult="$1"; shift |
| 79 | |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 80 | if [ "${xchg%${xchg#try_cmpxchg}}" = "try_cmpxchg" ] ; then |
| 81 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 82 | cat <<EOF |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 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__); \\ |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 90 | }) |
| 91 | EOF |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 92 | |
| 93 | else |
| 94 | |
| 95 | cat <<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 | }) |
| 102 | EOF |
| 103 | |
| 104 | fi |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 107 | cat << 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 | */ |
Mark Rutland | e3d18ce | 2021-07-13 11:52:51 +0100 | [diff] [blame] | 124 | #ifndef _LINUX_ATOMIC_INSTRUMENTED_H |
| 125 | #define _LINUX_ATOMIC_INSTRUMENTED_H |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 126 | |
| 127 | #include <linux/build_bug.h> |
Marco Elver | c020395 | 2019-11-26 15:04:04 +0100 | [diff] [blame] | 128 | #include <linux/compiler.h> |
Marco Elver | ed8af2e | 2020-01-21 17:05:09 +0100 | [diff] [blame] | 129 | #include <linux/instrumented.h> |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 130 | |
| 131 | EOF |
| 132 | |
| 133 | grep '^[a-z]' "$1" | while read name meta args; do |
| 134 | gen_proto "${meta}" "${name}" "atomic" "int" ${args} |
| 135 | done |
| 136 | |
| 137 | grep '^[a-z]' "$1" | while read name meta args; do |
| 138 | gen_proto "${meta}" "${name}" "atomic64" "s64" ${args} |
| 139 | done |
| 140 | |
Mark Rutland | 67d1b0d | 2021-07-13 11:52:52 +0100 | [diff] [blame] | 141 | grep '^[a-z]' "$1" | while read name meta args; do |
| 142 | gen_proto "${meta}" "${name}" "atomic_long" "long" ${args} |
| 143 | done |
| 144 | |
| 145 | |
Peter Zijlstra | 29f006f | 2020-08-29 22:03:35 +0900 | [diff] [blame] | 146 | for xchg in "xchg" "cmpxchg" "cmpxchg64" "try_cmpxchg"; do |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 147 | for order in "" "_acquire" "_release" "_relaxed"; do |
Mark Rutland | bccf1ec | 2021-05-25 15:02:32 +0100 | [diff] [blame] | 148 | gen_xchg "${xchg}${order}" "" |
| 149 | printf "\n" |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 150 | done |
| 151 | done |
| 152 | |
| 153 | for xchg in "cmpxchg_local" "cmpxchg64_local" "sync_cmpxchg"; do |
| 154 | gen_xchg "${xchg}" "" |
| 155 | printf "\n" |
| 156 | done |
| 157 | |
| 158 | gen_xchg "cmpxchg_double" "2 * " |
| 159 | |
| 160 | printf "\n\n" |
| 161 | |
| 162 | gen_xchg "cmpxchg_double_local" "2 * " |
| 163 | |
| 164 | cat <<EOF |
| 165 | |
Mark Rutland | e3d18ce | 2021-07-13 11:52:51 +0100 | [diff] [blame] | 166 | #endif /* _LINUX_ATOMIC_INSTRUMENTED_H */ |
Mark Rutland | ace9bad | 2018-09-04 11:48:25 +0100 | [diff] [blame] | 167 | EOF |