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