xref: /freebsd/sys/conf/kern.mk (revision a259b98fa211ed87bfee58c575de4e2de94ee0fa)
1
2#
3# Warning flags for compiling the kernel and components of the kernel:
4#
5CWARNFLAGS?=	-Wall -Wstrict-prototypes \
6		-Wmissing-prototypes -Wpointer-arith -Wcast-qual \
7		-Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
8		-Wmissing-include-dirs -fdiagnostics-show-option \
9		-Wno-unknown-pragmas -Wswitch \
10		${CWARNEXTRA}
11#
12# The following flags are next up for working on:
13#	-Wextra
14
15# Disable a few warnings for clang, since there are several places in the
16# kernel where fixing them is more trouble than it is worth, or where there is
17# a false positive.
18.if ${COMPILER_TYPE} == "clang"
19NO_WCONSTANT_CONVERSION=	-Wno-error=constant-conversion
20NO_WSHIFT_COUNT_NEGATIVE=	-Wno-shift-count-negative
21NO_WSHIFT_COUNT_OVERFLOW=	-Wno-shift-count-overflow
22NO_WSELF_ASSIGN=		-Wno-self-assign
23NO_WUNNEEDED_INTERNAL_DECL=	-Wno-error=unneeded-internal-declaration
24NO_WSOMETIMES_UNINITIALIZED=	-Wno-error=sometimes-uninitialized
25NO_WCAST_QUAL=			-Wno-error=cast-qual
26NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
27.if ${COMPILER_VERSION} >= 100000
28NO_WMISLEADING_INDENTATION=	-Wno-misleading-indentation
29.endif
30.if ${COMPILER_VERSION} >= 130000
31NO_WUNUSED_BUT_SET_VARIABLE=	-Wno-unused-but-set-variable
32.endif
33.if ${COMPILER_VERSION} >= 140000
34NO_WBITWISE_INSTEAD_OF_LOGICAL=	-Wno-bitwise-instead-of-logical
35.endif
36.if ${COMPILER_VERSION} >= 150000
37NO_WSTRICT_PROTOTYPES=		-Wno-strict-prototypes
38NO_WDEPRECATED_NON_PROTOTYPE=	-Wno-deprecated-non-prototype
39.endif
40.if ${COMPILER_VERSION} >= 210000
41NO_WDEFAULT_CONST_INIT_FIELD_UNSAFE= -Wno-default-const-init-field-unsafe
42.endif
43# Several other warnings which might be useful in some cases, but not severe
44# enough to error out the whole kernel build.  Display them anyway, so there is
45# some incentive to fix them eventually.
46CWARNEXTRA?=	-Wno-error=tautological-compare -Wno-error=empty-body \
47		-Wno-error=parentheses-equality -Wno-error=unused-function \
48		-Wno-error=pointer-sign
49CWARNEXTRA+=	-Wno-error=shift-negative-value
50CWARNEXTRA+=	-Wno-address-of-packed-member
51.endif	# clang
52
53.if ${COMPILER_TYPE} == "gcc"
54# Catch-all for all the things that are in our tree, but for which we're
55# not yet ready for this compiler.
56NO_WUNUSED_BUT_SET_VARIABLE=-Wno-unused-but-set-variable
57CWARNEXTRA?=	-Wno-error=address				\
58		-Wno-error=aggressive-loop-optimizations	\
59		-Wno-error=array-bounds				\
60		-Wno-error=attributes				\
61		-Wno-error=cast-qual				\
62		-Wno-error=enum-compare				\
63		-Wno-error=maybe-uninitialized			\
64		-Wno-error=misleading-indentation		\
65		-Wno-error=nonnull-compare			\
66		-Wno-error=overflow				\
67		-Wno-error=sequence-point			\
68		-Wno-error=shift-overflow			\
69		-Wno-error=tautological-compare			\
70		-Wno-error=unused-function
71.if ${COMPILER_VERSION} >= 70100
72CWARNEXTRA+=	-Wno-error=stringop-overflow
73.endif
74.if ${COMPILER_VERSION} >= 70200
75CWARNEXTRA+=	-Wno-error=memset-elt-size
76.endif
77.if ${COMPILER_VERSION} >= 80000
78CWARNEXTRA+=	-Wno-error=packed-not-aligned
79.endif
80.if ${COMPILER_VERSION} >= 90100
81CWARNEXTRA+=	-Wno-address-of-packed-member			\
82		-Wno-alloc-size-larger-than			\
83		-Wno-error=alloca-larger-than=
84.if ${COMPILER_VERSION} >= 120100
85CWARNEXTRA+=	-Wno-error=nonnull				\
86		-Wno-dangling-pointer				\
87		-Wno-zero-length-bounds
88NO_WINFINITE_RECURSION=	-Wno-infinite-recursion
89NO_WSTRINGOP_OVERREAD=	-Wno-stringop-overread
90.endif
91.endif
92
93# GCC produces false positives for functions that switch on an
94# enum (GCC bug 87950)
95CWARNFLAGS+=	-Wno-return-type
96.endif	# gcc
97
98# This warning is utter nonsense
99CWARNFLAGS+=	-Wno-format-zero-length
100
101# External compilers may not support our format extensions.  Allow them
102# to be disabled.  WARNING: format checking is disabled in this case.
103.if ${MK_FORMAT_EXTENSIONS} == "no"
104FORMAT_EXTENSIONS=	-Wno-format
105.elif ${COMPILER_TYPE} == "clang" || \
106    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 120100)
107FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
108.else
109FORMAT_EXTENSIONS=	-fformat-extensions
110.endif
111
112#
113# On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
114# and above adds code to the entry and exit point of every function to align the
115# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
116# per function call.  While the 16-byte alignment may benefit micro benchmarks,
117# it is probably an overall loss as it makes the code bigger (less efficient
118# use of code cache tag lines) and uses more stack (less efficient use of data
119# cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
120# operations inside the kernel itself.  These operations are exclusively
121# reserved for user applications.
122#
123# gcc:
124# Setting -mno-mmx implies -mno-3dnow
125# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
126#
127# clang:
128# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
129# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
130#
131.if ${MACHINE_CPUARCH} == "i386"
132CFLAGS.gcc+=	-mpreferred-stack-boundary=2
133CFLAGS.clang+=	-mno-aes -mno-avx
134CFLAGS+=	-mno-mmx -mno-sse -msoft-float
135INLINE_LIMIT?=	8000
136.endif
137
138.if ${MACHINE_CPUARCH} == "arm"
139INLINE_LIMIT?=	8000
140.endif
141
142.if ${MACHINE_CPUARCH} == "aarch64"
143# We generally don't want fpu instructions in the kernel.
144CFLAGS += -mgeneral-regs-only
145# Reserve x18 for pcpu data
146CFLAGS += -ffixed-x18
147# Build with BTI+PAC
148CFLAGS += -mbranch-protection=standard
149.if ${LINKER_FEATURES:Mbti-report}
150LDFLAGS += -Wl,-zbti-report=error
151.endif
152# TODO: support outline atomics
153CFLAGS += -mno-outline-atomics
154INLINE_LIMIT?=	8000
155.endif
156
157#
158# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
159# point registers within the kernel. However, we include the F and D extensions
160# in -march so we can have limited floating point support in context switching
161# code. This is different than userland where we use a hard-float ABI (lp64d).
162#
163# We also specify the "medium" code model, which generates code suitable for a
164# 2GiB addressing range located at any offset, allowing modules to be located
165# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
166# code model as "medium" and "medany" respectively.
167#
168.if ${MACHINE_CPUARCH} == "riscv"
169CFLAGS+=	-march=rv64imafdch_zifencei_svinval
170CFLAGS+=	-mabi=lp64
171CFLAGS.clang+=	-mcmodel=medium
172CFLAGS.gcc+=	-mcmodel=medany
173INLINE_LIMIT?=	8000
174
175.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
176CFLAGS+=	-mno-relax
177.endif
178.endif
179
180#
181# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
182# operations inside the kernel itself.  These operations are exclusively
183# reserved for user applications.
184#
185# gcc:
186# Setting -mno-mmx implies -mno-3dnow
187# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
188#
189# clang:
190# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
191# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
192# (-mfpmath= is not supported)
193#
194.if ${MACHINE_CPUARCH} == "amd64"
195CFLAGS.clang+=	-mno-aes -mno-avx
196CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
197		-fno-asynchronous-unwind-tables
198INLINE_LIMIT?=	8000
199.endif
200
201#
202# For PowerPC we tell gcc to use floating point emulation.  This avoids using
203# floating point registers for integer operations which it has a tendency to do.
204# Also explicitly disable Altivec instructions inside the kernel.
205#
206.if ${MACHINE_CPUARCH} == "powerpc"
207CFLAGS+=	-mno-altivec -msoft-float
208INLINE_LIMIT?=	15000
209.endif
210
211#
212# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
213# DDB happy. ELFv2, if available, has some other efficiency benefits.
214#
215.if ${MACHINE_ARCH:Mpowerpc64*} != "" && \
216    ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
217CFLAGS+=	-mabi=elfv2
218.endif
219
220#
221# GCC 3.0 and above like to do certain optimizations based on the
222# assumption that the program is linked against libc.  Stop this.
223#
224CFLAGS+=	-ffreestanding
225
226#
227# The C standard leaves signed integer overflow behavior undefined.
228# gcc and clang opimizers take advantage of this.  The kernel makes
229# use of signed integer wraparound mechanics so we need the compiler
230# to treat it as a wraparound and not take shortcuts.
231#
232CFLAGS+=	-fwrapv
233
234#
235# Stack Smashing Protection (SSP) support
236#
237.if ${MK_SSP} != "no"
238CFLAGS+=	-fstack-protector-strong
239.endif
240
241#
242# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
243#
244.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
245    ${MK_KERNEL_RETPOLINE} != "no"
246CFLAGS+=	-mretpoline
247.endif
248
249#
250# Kernel Address SANitizer support
251#
252.if !empty(KASAN_ENABLED)
253SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kasan \
254		-fsanitize=kernel-address
255.if ${COMPILER_TYPE} == "clang"
256SAN_CFLAGS+=	-mllvm -asan-stack=true \
257		-mllvm -asan-instrument-dynamic-allocas=true \
258		-mllvm -asan-globals=true \
259		-mllvm -asan-use-after-scope=true \
260		-mllvm -asan-instrumentation-with-call-threshold=0 \
261		-mllvm -asan-instrument-byval=false
262.endif
263
264.if ${MACHINE_CPUARCH} == "aarch64"
265# KASAN/ARM64 TODO: -asan-mapping-offset is calculated from:
266#	   (VM_KERNEL_MIN_ADDRESS >> KASAN_SHADOW_SCALE_SHIFT) + $offset = KASAN_MIN_ADDRESS
267#
268#	This is different than amd64, where we have a different
269#	KASAN_MIN_ADDRESS, and this offset value should eventually be
270#	upstreamed similar to: https://reviews.llvm.org/D98285
271#
272.if ${COMPILER_TYPE} == "clang"
273SAN_CFLAGS+=	-mllvm -asan-mapping-offset=0xdfff208000000000
274.else
275SAN_CFLAGS+=	-fasan-shadow-offset=0xdfff208000000000
276.endif
277.elif ${MACHINE_CPUARCH} == "amd64" && \
278      ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 180000
279# Work around https://github.com/llvm/llvm-project/issues/87923, which leads to
280# an assertion failure compiling dtrace.c with asan enabled.
281SAN_CFLAGS+=	-mllvm -asan-use-stack-safety=0
282.endif
283.endif # !empty(KASAN_ENABLED)
284
285#
286# Kernel Concurrency SANitizer support
287#
288.if !empty(KCSAN_ENABLED)
289SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kcsan \
290		-fsanitize=thread
291.endif
292
293#
294# Kernel Memory SANitizer support
295#
296.if !empty(KMSAN_ENABLED)
297# Disable -fno-sanitize-memory-param-retval until interceptors have been
298# updated to work properly with it.
299MSAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kmsan \
300		-fsanitize=kernel-memory
301.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 160000
302MSAN_CFLAGS+=	-fno-sanitize-memory-param-retval
303.endif
304SAN_CFLAGS+=	${MSAN_CFLAGS}
305.endif # !empty(KMSAN_ENABLED)
306
307#
308# Kernel Undefined Behavior SANitizer support
309#
310.if !empty(KUBSAN_ENABLED)
311SAN_CFLAGS+=	-fsanitize=undefined
312.endif
313
314#
315# Generic Kernel Coverage support
316#
317.if !empty(COVERAGE_ENABLED)
318.if ${COMPILER_TYPE} == "clang" || \
319    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100)
320SAN_CFLAGS+=	-fsanitize-coverage=trace-pc,trace-cmp
321.else
322SAN_CFLAGS+=	-fsanitize-coverage=trace-pc
323.endif
324.endif # !empty(COVERAGE_ENABLED)
325
326# Add the sanitizer C flags
327CFLAGS+=	${SAN_CFLAGS}
328
329#
330# Initialize stack variables on function entry
331#
332.if ${OPT_INIT_ALL} != "none"
333.if ${COMPILER_FEATURES:Minit-all}
334CFLAGS+= -ftrivial-auto-var-init=${OPT_INIT_ALL}
335.if ${OPT_INIT_ALL} == "zero" && ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
336CFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
337.endif
338.else
339.warning INIT_ALL (${OPT_INIT_ALL}) requested but not supported by compiler
340.endif
341.endif
342
343#
344# Some newer toolchains default to DWARF 5, which isn't supported by some build
345# tools yet.
346#
347.if (${CFLAGS:M-g} != "" || ${CFLAGS:M-g[0-3]} != "") && ${CFLAGS:M-gdwarf*} == ""
348CFLAGS+=	-gdwarf-4
349.endif
350
351CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
352CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
353CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
354
355# Tell bmake not to mistake standard targets for things to be searched for
356# or expect to ever be up-to-date.
357PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
358		beforelinking build build-tools buildfiles buildincludes \
359		checkdpadd clean cleandepend cleandir cleanobj configure \
360		depend distclean distribute exe \
361		html includes install installfiles installincludes \
362		obj objlink objs objwarn \
363		realinstall regress \
364		tags whereobj
365
366.PHONY: ${PHONY_NOTMAIN}
367.NOTMAIN: ${PHONY_NOTMAIN}
368
369CSTD?=		gnu17
370
371# c99/gnu99 is the minimum C standard version supported for kernel build
372.if ${CSTD} == "k&r" || ${CSTD} == "c89" || ${CSTD} == "c90" || \
373    ${CSTD} == "c94" || ${CSTD} == "c95"
374.error "Only c99/gnu99 or later is supported"
375.else # CSTD
376CFLAGS+=        -std=${CSTD}
377.endif # CSTD
378
379NOSAN_CFLAGS= ${CFLAGS:N-fsanitize*:N-fno-sanitize*:N-fasan-shadow-offset*}
380
381# Please keep this if in sync with bsd.sys.mk
382.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
383# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
384.if ${COMPILER_TYPE} == "clang"
385# Note: Clang does not like relative paths for ld so we map ld.lld -> lld.
386.if ${COMPILER_VERSION} >= 120000
387CCLDFLAGS+=	--ld-path=${LD:[1]:S/^ld.//1W}
388.else
389CCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
390.endif
391.else
392# GCC does not support an absolute path for -fuse-ld so we just print this
393# warning instead and let the user add the required symlinks.
394# However, we can avoid this warning if -B is set appropriately (e.g. for
395# CROSS_TOOLCHAIN=...-gcc).
396.if !(${LD:[1]:T} == "ld" && ${CC:tw:M-B${LD:[1]:H}/})
397.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
398.endif
399.endif
400.endif
401
402# Set target-specific linker emulation name.
403LD_EMULATION_aarch64=aarch64elf
404LD_EMULATION_amd64=elf_x86_64_fbsd
405LD_EMULATION_arm=armelf_fbsd
406LD_EMULATION_armv7=armelf_fbsd
407LD_EMULATION_i386=elf_i386_fbsd
408LD_EMULATION_powerpc= elf32ppc_fbsd
409LD_EMULATION_powerpc64= elf64ppc_fbsd
410LD_EMULATION_powerpc64le= elf64lppc_fbsd
411LD_EMULATION_riscv64= elf64lriscv
412LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
413