aboutsummaryrefslogtreecommitdiffstats
path: root/arch/s390/kernel/syscalls/syscalltbl
blob: fbac1732f874e8d117b9c4aaf0a66b203370b933 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
#
# Generate system call table and header files
#
# Copyright IBM Corp. 2018
# Author(s):  Hendrik Brueckner <brueckner@linux.vnet.ibm.com>

#
# File path to the system call table definition.
# You can set the path with the -i option.  If omitted,
# system call table definitions are read from standard input.
#
SYSCALL_TBL=""


create_syscall_table_entries()
{
	local nr abi name entry64 entry32 _ignore
	local temp=$(mktemp ${TMPDIR:-/tmp}/syscalltbl-common.XXXXXXXXX)

	(
	#
	# Initialize with 0 to create an NI_SYSCALL for 0
	#
	local prev_nr=0 prev_32=sys_ni_syscall prev_64=sys_ni_syscall
	while read nr abi name entry64 entry32 _ignore; do
		test x$entry32 = x- && entry32=sys_ni_syscall
		test x$entry64 = x- && entry64=sys_ni_syscall

		if test $prev_nr -eq $nr; then
			#
			# Same syscall but different ABI, just update
			# the respective entry point
			#
			case $abi in
			32)
				prev_32=$entry32
			;;
			64)
				prev_64=$entry64
			;;
			esac
			continue;
		else
			printf "%d\t%s\t%s\n" $prev_nr $prev_64 $prev_32
		fi

		prev_nr=$nr
		prev_64=$entry64
		prev_32=$entry32
	done
	printf "%d\t%s\t%s\n" $prev_nr $prev_64 $prev_32
	) >> $temp

	#
	# Check for duplicate syscall numbers
	#
	if ! cat $temp |cut -f1 |uniq -d 2>&1; then
		echo "Error: generated system call table contains duplicate entries: $temp" >&2
		exit 1
	fi

	#
	# Generate syscall table
	#
	prev_nr=0
	while read nr entry64 entry32; do
		while test $prev_nr -lt $((nr - 1)); do
			printf "NI_SYSCALL\n"
			prev_nr=$((prev_nr + 1))
		done
		if test x$entry64 = xsys_ni_syscall &&
		   test x$entry32 = xsys_ni_syscall; then
			printf "NI_SYSCALL\n"
		else
			printf "SYSCALL(%s,%s)\n" $entry64 $entry32
		fi
		prev_nr=$nr
	done < $temp
	rm $temp
}

generate_syscall_table()
{
	cat <<-EoHEADER
	/* SPDX-License-Identifier: GPL-2.0 */
	/*
	 * Definitions for sys_call_table, each line represents an
	 * entry in the table in the form
	 * SYSCALL(64 bit syscall, 31 bit emulated syscall)
	 *
	 * This file is meant to be included from entry.S.
	 */

	#define NI_SYSCALL SYSCALL(sys_ni_syscall,sys_ni_syscall)

EoHEADER
	grep -Ev '^(#|[[:blank:]]*$)' $SYSCALL_TBL	\
		|sort -k1 -n				\
		|create_syscall_table_entries
}

create_header_defines()
{
	local nr abi name _ignore

	while read nr abi name _ignore; do
		printf "#define __NR_%s %d\n" $name $nr
	done
}

normalize_fileguard()
{
	local fileguard="$1"

	echo "$1" |tr '[[:lower:]]' '[[:upper:]]' \
		  |sed -e 's/[^A-Z0-9_]/_/g' -e 's/__/_/g'
}

generate_syscall_header()
{
	local abis=$(echo "($1)" | tr ',' '|')
	local filename="$2"
	local fileguard suffix

	if test "$filename"; then
		fileguard=$(normalize_fileguard "__UAPI_ASM_S390_$2")
	else
		case "$abis" in
		*64*) suffix=64 ;;
		*32*) suffix=32 ;;
		esac
		fileguard=$(normalize_fileguard "__UAPI_ASM_S390_SYSCALLS_$suffix")
	fi

	cat <<-EoHEADER
	/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
	#ifndef ${fileguard}
	#define ${fileguard}

EoHEADER

	grep -E "^[[:digit:]]+[[:space:]]+${abis}" $SYSCALL_TBL	\
		|sort -k1 -n					\
		|create_header_defines

	cat <<-EoFOOTER

	#endif /* ${fileguard} */
EoFOOTER
}

__max_syscall_nr()
{
	local abis=$(echo "($1)" | tr ',' '|')

	grep -E "^[[:digit:]]+[[:space:]]+${abis}" $SYSCALL_TBL	 \
		|sed -ne 's/^\([[:digit:]]*\)[[:space:]].*/\1/p' \
		|sort -n					 \
		|tail -1
}


generate_syscall_nr()
{
	local abis="$1"
	local max_syscall_nr num_syscalls

	max_syscall_nr=$(__max_syscall_nr "$abis")
	num_syscalls=$((max_syscall_nr + 1))

	cat <<-EoHEADER
	/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
	#ifndef __ASM_S390_SYSCALLS_NR
	#define __ASM_S390_SYSCALLS_NR

	#define NR_syscalls ${num_syscalls}

	#endif /* __ASM_S390_SYSCALLS_NR */
EoHEADER
}


#
# Parse command line arguments
#
do_syscall_header=""
do_syscall_table=""
do_syscall_nr=""
output_file=""
abi_list="common,64"
filename=""
while getopts ":HNSXi:a:f:" arg; do
	case $arg in
	a)
		abi_list="$OPTARG"
		;;
	i)
		SYSCALL_TBL="$OPTARG"
		;;
	f)
		filename=${OPTARG##*/}
		;;
	H)
		do_syscall_header=1
		;;
	N)
		do_syscall_nr=1
		;;
	S)
		do_syscall_table=1
		;;
	X)
		set -x
		;;
	:)
		echo "Missing argument for -$OPTARG" >&2
		exit 1
	;;
	\?)
		echo "Invalid option specified" >&2
		exit 1
	;;
	esac
done

test "$do_syscall_header" && generate_syscall_header "$abi_list" "$filename"
test "$do_syscall_table" && generate_syscall_table
test "$do_syscall_nr" && generate_syscall_nr "$abi_list"

exit 0