1 #!/usr/bin/env bash
2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
5
6 # The syscall package provides access to the raw system call
7 # interface of the underlying operating system. Porting Go to
8 # a new architecture/operating system combination requires
9 # some manual effort, though there are tools that automate
10 # much of the process. The auto-generated files have names
11 # beginning with z.
12 #
13 # This script runs or (given -n) prints suggested commands to generate z files
14 # for the current system. Running those commands is not automatic.
15 # This script is documentation more than anything else.
16 #
17 # * asm_${GOOS}_${GOARCH}.s
18 #
19 # This hand-written assembly file implements system call dispatch.
20 # There are three entry points:
21 #
22 # func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr);
23 # func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr);
24 # func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr);
25 #
26 # The first and second are the standard ones; they differ only in
27 # how many arguments can be passed to the kernel.
28 # The third is for low-level use by the ForkExec wrapper;
29 # unlike the first two, it does not call into the scheduler to
30 # let it know that a system call is running.
31 #
32 # * syscall_${GOOS}.go
33 #
34 # This hand-written Go file implements system calls that need
35 # special handling and lists "//sys" comments giving prototypes
36 # for ones that can be auto-generated. Mksyscall reads those
37 # comments to generate the stubs.
38 #
39 # * syscall_${GOOS}_${GOARCH}.go
40 #
41 # Same as syscall_${GOOS}.go except that it contains code specific
42 # to ${GOOS} on one particular architecture.
43 #
44 # * types_${GOOS}.c
45 #
46 # This hand-written C file includes standard C headers and then
47 # creates typedef or enum names beginning with a dollar sign
48 # (use of $ in variable names is a gcc extension). The hardest
49 # part about preparing this file is figuring out which headers to
50 # include and which symbols need to be #defined to get the
51 # actual data structures that pass through to the kernel system calls.
52 # Some C libraries present alternate versions for binary compatibility
53 # and translate them on the way in and out of system calls, but
54 # there is almost always a #define that can get the real ones.
55 # See types_darwin.c and types_linux.c for examples.
56 #
57 # * zerror_${GOOS}_${GOARCH}.go
58 #
59 # This machine-generated file defines the system's error numbers,
60 # error strings, and signal numbers. The generator is "mkerrors.sh".
61 # Usually no arguments are needed, but mkerrors.sh will pass its
62 # arguments on to godefs.
63 #
64 # * zsyscall_${GOOS}_${GOARCH}.go
65 #
66 # Generated by mksyscall.pl; see syscall_${GOOS}.go above.
67 #
68 # * zsysnum_${GOOS}_${GOARCH}.go
69 #
70 # Generated by mksysnum_${GOOS}.
71 #
72 # * ztypes_${GOOS}_${GOARCH}.go
73 #
74 # Generated by godefs; see types_${GOOS}.c above.
75
76 GOOSARCH="${GOOS}_${GOARCH}"
77
78 # defaults
79 mksyscall="./mksyscall.pl"
80 mkerrors="./mkerrors.sh"
81 zerrors="zerrors_$GOOSARCH.go"
82 mksysctl=""
83 zsysctl="zsysctl_$GOOSARCH.go"
84 mksysnum=
85 mktypes=
86 mkasm=
87 run="sh"
88
89 case "$1" in
90 -syscalls)
91 shift
92 for i in ${@:-zsyscall*go}
93 do
94 # Run the command line that appears in the first line
95 # of the generated file to regenerate it.
96 sed 1q $i | sed 's;^// ;./;' | sh > _$i && gofmt < _$i > $i
97 rm _$i
98 done
99 exit 0
100 ;;
101 -n)
102 run="cat"
103 shift
104 esac
105
106 case "$#" in
107 0)
108 ;;
109 *)
110 echo 'usage: mkall.sh [-n]' 1>&2
111 exit 2
112 esac
113
114 GOOSARCH_in=syscall_$GOOSARCH.go
115 case "$GOOSARCH" in
116 _* | *_ | _)
117 echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
118 exit 1
119 ;;
120 aix_ppc64)
121 mkerrors="$mkerrors -maix64"
122 mksyscall="./mksyscall_libc.pl -aix"
123 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
124 ;;
125 darwin_amd64)
126 mkerrors="$mkerrors -m64"
127 mksyscall="./mksyscall.pl -darwin"
128 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
129 mkasm="go run mkasm.go"
130 ;;
131 darwin_arm64)
132 mkerrors="$mkerrors -m64"
133 mksyscall="./mksyscall.pl -darwin"
134 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
135 mkasm="go run mkasm.go"
136 ;;
137 dragonfly_amd64)
138 mkerrors="$mkerrors -m64"
139 mksyscall="./mksyscall.pl -dragonfly"
140 mksysnum="curl -s 'http://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master' | ./mksysnum_dragonfly.pl"
141 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
142 ;;
143 freebsd_386)
144 mkerrors="$mkerrors -m32"
145 mksyscall="./mksyscall.pl -l32"
146 mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
147 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
148 ;;
149 freebsd_amd64)
150 mkerrors="$mkerrors -m64"
151 mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
152 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
153 ;;
154 freebsd_arm)
155 mkerrors="$mkerrors"
156 mksyscall="./mksyscall.pl -l32 -arm"
157 mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
158 # Let the type of C char be signed to make the bare syscall
159 # API consistent between platforms.
160 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
161 ;;
162 freebsd_arm64)
163 mkerrors="$mkerrors"
164 mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
165 # Let the type of C char be signed to make the bare syscall
166 # API consistent between platforms.
167 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
168 ;;
169 freebsd_riscv64)
170 mkerrors="$mkerrors"
171 mksysnum="curl -s 'https://cgit.freebsd.org/src/plain/sys/kern/syscalls.master?h=stable/12' | ./mksysnum_freebsd.pl"
172 # Let the type of C char be signed to make the bare syscall
173 # API consistent between platforms.
174 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
175 ;;
176 linux_386)
177 mkerrors="$mkerrors -m32"
178 mksyscall="./mksyscall.pl -l32"
179 mksysnum="./mksysnum_linux.pl /usr/include/asm/unistd_32.h"
180 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
181 ;;
182 linux_amd64)
183 unistd_h=$(ls -1 /usr/include/asm/unistd_64.h /usr/include/x86_64-linux-gnu/asm/unistd_64.h 2>/dev/null | head -1)
184 if [ "$unistd_h" = "" ]; then
185 echo >&2 cannot find unistd_64.h
186 exit 1
187 fi
188 mkerrors="$mkerrors -m64"
189 mksysnum="./mksysnum_linux.pl $unistd_h"
190 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
191 ;;
192 linux_arm)
193 mkerrors="$mkerrors"
194 mksyscall="./mksyscall.pl -l32 -arm"
195 mksysnum="curl -s 'http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/arch/arm/include/uapi/asm/unistd.h' | ./mksysnum_linux.pl -"
196 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
197 ;;
198 linux_arm64)
199 unistd_h=$(ls -1 /usr/include/asm/unistd.h /usr/include/asm-generic/unistd.h 2>/dev/null | head -1)
200 if [ "$unistd_h" = "" ]; then
201 echo >&2 cannot find unistd_64.h
202 exit 1
203 fi
204 mksysnum="./mksysnum_linux.pl $unistd_h"
205 # Let the type of C char be signed to make the bare syscall
206 # API consistent between platforms.
207 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
208 ;;
209 linux_loong64)
210 GOOSARCH_in=syscall_linux_loong64.go
211 unistd_h=$(ls -1 /usr/include/asm/unistd.h /usr/include/asm-generic/unistd.h 2>/dev/null | head -1)
212 if [ "$unistd_h" = "" ]; then
213 echo >&2 cannot find unistd.h
214 exit 1
215 fi
216 mksysnum="./mksysnum_linux.pl $unistd_h"
217 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
218 ;;
219 linux_mips)
220 GOOSARCH_in=syscall_linux_mipsx.go
221 unistd_h=/usr/include/asm/unistd.h
222 mksyscall="./mksyscall.pl -b32 -arm"
223 mkerrors="$mkerrors"
224 mksysnum="./mksysnum_linux.pl $unistd_h"
225 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
226 ;;
227 linux_mipsle)
228 GOOSARCH_in=syscall_linux_mipsx.go
229 unistd_h=/usr/include/asm/unistd.h
230 mksyscall="./mksyscall.pl -l32 -arm"
231 mkerrors="$mkerrors"
232 mksysnum="./mksysnum_linux.pl $unistd_h"
233 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
234 ;;
235 linux_mips64)
236 GOOSARCH_in=syscall_linux_mips64x.go
237 unistd_h=/usr/include/asm/unistd.h
238 mkerrors="$mkerrors -m64"
239 mksysnum="./mksysnum_linux.pl $unistd_h"
240 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
241 ;;
242 linux_mips64le)
243 GOOSARCH_in=syscall_linux_mips64x.go
244 unistd_h=/usr/include/asm/unistd.h
245 mkerrors="$mkerrors -m64"
246 mksysnum="./mksysnum_linux.pl $unistd_h"
247 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
248 ;;
249 linux_ppc64)
250 GOOSARCH_in=syscall_linux_ppc64x.go
251 unistd_h=/usr/include/asm/unistd.h
252 mkerrors="$mkerrors -m64"
253 mksysnum="./mksysnum_linux.pl $unistd_h"
254 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
255 ;;
256 linux_ppc64le)
257 GOOSARCH_in=syscall_linux_ppc64x.go
258 unistd_h=/usr/include/powerpc64le-linux-gnu/asm/unistd.h
259 mkerrors="$mkerrors -m64"
260 mksysnum="./mksysnum_linux.pl $unistd_h"
261 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
262 ;;
263 linux_riscv64)
264 unistd_h=$(ls -1 /usr/include/asm/unistd.h /usr/include/asm-generic/unistd.h 2>/dev/null | head -1)
265 if [ "$unistd_h" = "" ]; then
266 echo >&2 cannot find unistd_64.h
267 exit 1
268 fi
269 mksysnum="./mksysnum_linux.pl $unistd_h"
270 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
271 ;;
272 linux_s390x)
273 GOOSARCH_in=syscall_linux_s390x.go
274 unistd_h=/usr/include/asm/unistd.h
275 mkerrors="$mkerrors -m64"
276 mksysnum="./mksysnum_linux.pl $unistd_h"
277 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
278 ;;
279 netbsd_386)
280 mkerrors="$mkerrors -m32"
281 mksyscall="./mksyscall.pl -l32 -netbsd"
282 mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
283 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
284 ;;
285 netbsd_amd64)
286 mkerrors="$mkerrors -m64"
287 mksyscall="./mksyscall.pl -netbsd"
288 mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
289 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
290 ;;
291 netbsd_arm)
292 mkerrors="$mkerrors -m32"
293 mksyscall="./mksyscall.pl -l32 -netbsd -arm"
294 mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
295 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
296 ;;
297 netbsd_arm64)
298 mkerrors="$mkerrors -m64"
299 mksyscall="./mksyscall.pl -netbsd"
300 mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
301 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
302 ;;
303 openbsd_386)
304 GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
305 mkerrors="$mkerrors -m32"
306 mksyscall="./mksyscall.pl -l32 -openbsd -libc"
307 mksysctl="./mksysctl_openbsd.pl"
308 zsysctl="zsysctl_openbsd.go"
309 mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
310 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
311 mkasm="go run mkasm.go"
312 ;;
313 openbsd_amd64)
314 GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
315 mkerrors="$mkerrors -m64"
316 mksyscall="./mksyscall.pl -openbsd -libc"
317 mksysctl="./mksysctl_openbsd.pl"
318 zsysctl="zsysctl_openbsd.go"
319 mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
320 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
321 mkasm="go run mkasm.go"
322 ;;
323 openbsd_arm)
324 GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
325 mkerrors="$mkerrors"
326 mksyscall="./mksyscall.pl -l32 -openbsd -arm -libc"
327 mksysctl="./mksysctl_openbsd.pl"
328 zsysctl="zsysctl_openbsd.go"
329 mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
330 # Let the type of C char be signed to make the bare syscall
331 # API consistent between platforms.
332 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
333 mkasm="go run mkasm.go"
334 ;;
335 openbsd_arm64)
336 GOOSARCH_in="syscall_openbsd_libc.go syscall_openbsd_$GOARCH.go"
337 mkerrors="$mkerrors -m64"
338 mksyscall="./mksyscall.pl -openbsd -libc"
339 mksysctl="./mksysctl_openbsd.pl"
340 zsysctl="zsysctl_openbsd.go"
341 mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
342 # Let the type of C char be signed to make the bare syscall
343 # API consistent between platforms.
344 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
345 mkasm="go run mkasm.go"
346 ;;
347 openbsd_mips64)
348 GOOSARCH_in="syscall_openbsd1.go syscall_openbsd_$GOARCH.go"
349 mkerrors="$mkerrors -m64"
350 mksyscall="./mksyscall.pl -openbsd"
351 mksysctl="./mksysctl_openbsd.pl"
352 zsysctl="zsysctl_openbsd.go"
353 mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
354 # Let the type of C char be signed to make the bare syscall
355 # API consistent between platforms.
356 mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
357 ;;
358 plan9_386)
359 mkerrors=
360 mksyscall="./mksyscall.pl -l32 -plan9"
361 mksysnum="./mksysnum_plan9.sh /n/sources/plan9/sys/src/libc/9syscall/sys.h"
362 mktypes="XXX"
363 ;;
364 solaris_amd64)
365 mksyscall="./mksyscall_libc.pl -solaris"
366 mkerrors="$mkerrors -m64"
367 mksysnum=
368 mktypes="GOARCH=$GOARCH go tool cgo -godefs"
369 ;;
370 windows_*)
371 echo 'run "go generate" instead' 1>&2
372 exit 1
373 ;;
374 *)
375 echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
376 exit 1
377 ;;
378 esac
379
380 (
381 if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
382 syscall_goos="syscall_$GOOS.go"
383 case "$GOOS" in
384 darwin | dragonfly | freebsd | netbsd | openbsd)
385 syscall_goos="syscall_bsd.go $syscall_goos"
386 ;;
387 esac
388 if [ -n "$mksyscall" ]; then echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go"; fi
389 if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
390 if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
391 if [ -n "$mktypes" ]; then
392 # ztypes_$GOOSARCH.go could be erased before "go run mkpost.go" is called.
393 # Therefore, "go run" tries to recompile syscall package but ztypes is empty and it fails.
394 echo "$mktypes types_$GOOS.go |go run mkpost.go >ztypes_$GOOSARCH.go.NEW && mv ztypes_$GOOSARCH.go.NEW ztypes_$GOOSARCH.go";
395 fi
396 if [ -n "$mkasm" ]; then echo "$mkasm $GOOS $GOARCH"; fi
397 ) | $run
398
View as plain text