xref: /freebsd/sys/conf/kern.pre.mk (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1
2# Part of a unified Makefile for building kernels.  This part contains all
3# of the definitions that need to be before %BEFORE_DEPEND.
4
5# Allow user to configure things that only effect src tree builds.
6# Note: This is duplicated from src.sys.mk to ensure that we include
7# /etc/src.conf when building the kernel. Kernels can be built without
8# the rest of /usr/src, but they still always process SRCCONF even though
9# the normal mechanisms to prevent that (compiling out of tree) won't
10# work. To ensure they do work, we have to duplicate thee few lines here.
11SRCCONF?=	/etc/src.conf
12.if (exists(${SRCCONF}) || ${SRCCONF} != "/etc/src.conf") && !target(_srcconf_included_)
13.include "${SRCCONF}"
14_srcconf_included_:
15.endif
16
17.include <bsd.own.mk>
18.include <bsd.compiler.mk>
19.include "kern.opts.mk"
20.-include <local.kern.pre.mk>
21
22# The kernel build always occurs in the object directory which is .CURDIR.
23.if ${.MAKE.MODE:Unormal:Mmeta}
24.MAKE.MODE+=	curdirOk=yes
25.endif
26
27# The kernel build always expects .OBJDIR=.CURDIR.
28.OBJDIR: ${.CURDIR}
29
30.if defined(NO_OBJWALK) || ${MK_AUTO_OBJ} == "yes"
31NO_OBJWALK=		t
32NO_MODULES_OBJ=	t
33.endif
34.if !defined(NO_OBJWALK)
35_obj=		obj
36.endif
37
38# Can be overridden by makeoptions or /etc/make.conf
39KERNEL_KO?=	kernel
40KERNEL?=	kernel
41KODIR?=		/boot/${KERNEL}
42LDSCRIPT_NAME?=	ldscript.$M
43LDSCRIPT?=	$S/conf/${LDSCRIPT_NAME}
44
45M=		${MACHINE}
46
47AWK?=		awk
48CP?=		cp
49ELFDUMP?=	elfdump
50NM?=		nm
51OBJCOPY?=	objcopy
52SIZE?=		size
53
54.if defined(DEBUG)
55CTFFLAGS+=	-g
56.endif
57.if ${MACHINE_CPUARCH} == "amd64" && ${COMPILER_TYPE} != "clang"
58_COPTFLAGS_EXTRA=-frename-registers
59.else
60_COPTFLAGS_EXTRA=
61.endif
62COPTFLAGS?=-O2 -pipe ${_COPTFLAGS_EXTRA}
63.if !empty(COPTFLAGS:M-O[23s]) && empty(COPTFLAGS:M-fno-strict-aliasing)
64COPTFLAGS+= -fno-strict-aliasing
65.endif
66.if !defined(NO_CPU_COPTFLAGS)
67COPTFLAGS+= ${_CPUCFLAGS}
68.endif
69NOSTDINC= -nostdinc
70
71INCLUDES= ${NOSTDINC} ${INCLMAGIC} -I. -I$S -I$S/contrib/ck/include
72
73CFLAGS=	${COPTFLAGS} ${DEBUG}
74CFLAGS+= ${INCLUDES} -D_KERNEL -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h
75CFLAGS_PARAM_INLINE_UNIT_GROWTH?=100
76CFLAGS_PARAM_LARGE_FUNCTION_GROWTH?=1000
77CFLAGS.gcc+= -fms-extensions -finline-limit=${INLINE_LIMIT}
78CFLAGS.gcc+= --param inline-unit-growth=${CFLAGS_PARAM_INLINE_UNIT_GROWTH}
79CFLAGS.gcc+= --param large-function-growth=${CFLAGS_PARAM_LARGE_FUNCTION_GROWTH}
80CFLAGS.gcc+= -fms-extensions
81.if defined(CFLAGS_ARCH_PARAMS)
82CFLAGS.gcc+=${CFLAGS_ARCH_PARAMS}
83.endif
84WERROR?=	-Werror
85# The following should be removed no earlier than LLVM11 being imported into the
86# tree, to ensure we don't regress the build.  LLVM11 and GCC10 will switch the
87# default over to -fno-common, making this redundant.
88CFLAGS+=	-fno-common
89
90# XXX LOCORE means "don't declare C stuff" not "for locore.s".
91ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${CFLAGS} ${ASM_CFLAGS.${.IMPSRC:T}}
92
93COMPAT_FREEBSD32_ENABLED!= grep COMPAT_FREEBSD32 opt_global.h || true ; echo
94
95KASAN_ENABLED!=	grep KASAN opt_global.h || true ; echo
96.if !empty(KASAN_ENABLED)
97SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kasan \
98		-fsanitize=kernel-address
99.if ${COMPILER_TYPE} == "clang"
100SAN_CFLAGS+=	-mllvm -asan-stack=true \
101		-mllvm -asan-instrument-dynamic-allocas=true \
102		-mllvm -asan-globals=true \
103		-mllvm -asan-use-after-scope=true \
104		-mllvm -asan-instrumentation-with-call-threshold=0 \
105		-mllvm -asan-instrument-byval=false
106.endif
107
108.if ${MACHINE_CPUARCH} == "aarch64"
109# KASAN/ARM64 TODO: -asan-mapping-offset is calculated from:
110#	   (VM_KERNEL_MIN_ADDRESS >> KASAN_SHADOW_SCALE_SHIFT) + $offset = KASAN_MIN_ADDRESS
111#
112#	This is different than amd64, where we have a different
113#	KASAN_MIN_ADDRESS, and this offset value should eventually be
114#	upstreamed similar to: https://reviews.llvm.org/D98285
115#
116.if ${COMPILER_TYPE} == "clang"
117SAN_CFLAGS+=	-mllvm -asan-mapping-offset=0xdfff208000000000
118.else
119SAN_CFLAGS+=	-fasan-shadow-offset=0xdfff208000000000
120.endif
121.elif ${MACHINE_CPUARCH} == "amd64" && \
122      ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 180000
123# Work around https://github.com/llvm/llvm-project/issues/87923, which leads to
124# an assertion failure compiling dtrace.c with asan enabled.
125SAN_CFLAGS+=	-mllvm -asan-use-stack-safety=0
126.endif
127.endif
128
129KCSAN_ENABLED!=	grep KCSAN opt_global.h || true ; echo
130.if !empty(KCSAN_ENABLED)
131SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kcsan \
132		-fsanitize=thread
133.endif
134
135KMSAN_ENABLED!= grep KMSAN opt_global.h || true ; echo
136.if !empty(KMSAN_ENABLED)
137# Disable -fno-sanitize-memory-param-retval until interceptors have been
138# updated to work properly with it.
139MSAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kmsan \
140		-fsanitize=kernel-memory
141.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 160000
142MSAN_CFLAGS+=	-fno-sanitize-memory-param-retval
143.endif
144SAN_CFLAGS+=	${MSAN_CFLAGS}
145.endif
146
147KUBSAN_ENABLED!=	grep KUBSAN opt_global.h || true ; echo
148.if !empty(KUBSAN_ENABLED)
149SAN_CFLAGS+=	-fsanitize=undefined
150.endif
151
152COVERAGE_ENABLED!=	grep COVERAGE opt_global.h || true ; echo
153.if !empty(COVERAGE_ENABLED)
154.if ${COMPILER_TYPE} == "clang" || \
155    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100)
156SAN_CFLAGS+=	-fsanitize-coverage=trace-pc,trace-cmp
157.else
158SAN_CFLAGS+=	-fsanitize-coverage=trace-pc
159.endif
160.endif
161
162CFLAGS+=	${SAN_CFLAGS}
163
164GCOV_ENABLED!=	grep GCOV opt_global.h || true ; echo
165.if !empty(GCOV_ENABLED)
166.if ${COMPILER_TYPE} == "gcc"
167GCOV_CFLAGS+=	 -fprofile-arcs -ftest-coverage
168.endif
169.endif
170
171CFLAGS+=	${GCOV_CFLAGS}
172
173# Put configuration-specific C flags last so that they can override
174# the others.
175CFLAGS+=	${CONF_CFLAGS}
176
177.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
178LDFLAGS+=	--build-id=sha1
179.endif
180
181.if (${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
182    ${MACHINE_CPUARCH} == "i386" || ${MACHINE} == "powerpc") && \
183    defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mifunc} == "" && \
184    !make(install)
185.error amd64/arm64/i386/ppc* kernel requires linker ifunc support
186.endif
187.if ${MACHINE_CPUARCH} == "amd64"
188LDFLAGS+=	-z max-page-size=2097152
189.if ${LINKER_TYPE} != "lld"
190LDFLAGS+=	-z common-page-size=4096
191.else
192.if defined(LINKER_FEATURES) && !${LINKER_FEATURES:Mifunc-noplt}
193.warning "Linker ${LD} does not support -z ifunc-noplt -> ifunc calls are unoptimized."
194.else
195LDFLAGS+=	-z notext -z ifunc-noplt
196.endif
197.endif
198.endif  # ${MACHINE_CPUARCH} == "amd64"
199
200.if ${MACHINE_CPUARCH} == "riscv"
201# Hack: Work around undefined weak symbols being out of range when linking with
202# LLD (address is a PC-relative calculation, and BFD works around this by
203# rewriting the instructions to generate an absolute address of 0); -fPIE
204# avoids this since it uses the GOT for all extern symbols, which is overly
205# inefficient for us. Drop once undefined weak symbols work with medany.
206.if ${LINKER_TYPE} == "lld"
207CFLAGS+=	-fPIE
208.endif
209.endif
210
211NORMAL_C= ${CC} -c ${CFLAGS} ${WERROR} ${.IMPSRC}
212NORMAL_S= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} ${.IMPSRC}
213NORMAL_C_NOWERROR= ${CC} -c ${CFLAGS} ${.IMPSRC}
214
215NORMAL_M= ${AWK} -f $S/tools/makeobjops.awk ${.IMPSRC} -c ; \
216	  ${CC} -c ${CFLAGS} ${WERROR} ${.PREFIX}.c
217
218NORMAL_FW= uudecode -o ${.TARGET} ${.ALLSRC}
219NORMAL_FWO= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} -o ${.TARGET} \
220	$S/kern/firmw.S -DFIRMW_FILE=\""${.ALLSRC:M*.fw}"\" \
221	-DFIRMW_SYMBOL="${.ALLSRC:M*.fw:C/[-.\/]/_/g}"
222
223# Remove sanitizer arguments. Some -fno-sanitize* and -fasan-shadow-offset*
224# arguments become an error if the appropriate sanitizer is not enabled.
225NOSAN_C= ${NORMAL_C:N-fsanitize*:N-fno-sanitize*:N-fasan-shadow-offset*}
226
227# for ZSTD in the kernel (include zstd/lib/freebsd before other CFLAGS)
228ZSTD_C= ${CC} -c -DZSTD_HEAPMODE=1 -I$S/contrib/zstd/lib/freebsd ${CFLAGS} \
229	-I$S/contrib/zstd/lib -I$S/contrib/zstd/lib/common ${WERROR} \
230	-Wno-missing-prototypes -U__BMI__ -DZSTD_NO_INTRINSICS ${.IMPSRC}
231# https://github.com/facebook/zstd/commit/812e8f2a [zstd 1.4.1]
232# "Note that [GCC] autovectorization still does not do a good job on the
233# optimized version, so it's turned off via attribute and flag.  I found
234# that neither attribute nor command-line flag were entirely successful in
235# turning off vectorization, which is why there were both."
236.if ${COMPILER_TYPE} == "gcc"
237ZSTD_DECOMPRESS_BLOCK_FLAGS= -fno-tree-vectorize
238.endif
239
240ZINCDIR=$S/contrib/openzfs/include
241# Common for dtrace / zfs
242CDDL_CFLAGS=	\
243	-DFREEBSD_NAMECACHE \
244	-D_SYS_VMEM_H_ \
245	-D__KERNEL \
246	-D__KERNEL__ \
247	-nostdinc \
248	-include $S/modules/zfs/static_ccompile.h \
249	-I${ZINCDIR} \
250	-I${ZINCDIR}/os/freebsd \
251	-I${ZINCDIR}/os/freebsd/spl \
252	-I${ZINCDIR}/os/freebsd/zfs  \
253	-I$S/modules/zfs \
254	-I$S/contrib/openzfs/module/zstd/include \
255	${CFLAGS} \
256	-Wno-cast-qual \
257	-Wno-duplicate-decl-specifier \
258	-Wno-missing-braces \
259	-Wno-missing-prototypes \
260	-Wno-parentheses \
261	-Wno-pointer-arith \
262	-Wno-strict-prototypes \
263	-Wno-switch \
264	-Wno-undef \
265	-Wno-uninitialized \
266	-Wno-unknown-pragmas \
267	-Wno-unused \
268	-include ${ZINCDIR}/os/freebsd/spl/sys/ccompile.h \
269	-I$S/cddl/contrib/opensolaris/uts/common \
270	-I$S -I$S/cddl/compat/opensolaris
271CDDL_C=		${CC} -c ${CDDL_CFLAGS} ${WERROR} ${.IMPSRC}
272
273# Special flags for managing the compat compiles for ZFS
274ZFS_CFLAGS+=	-I$S/contrib/openzfs/module/icp/include \
275	${CDDL_CFLAGS} -DBUILDING_ZFS -DHAVE_UIO_ZEROCOPY \
276	-DWITH_NETDUMP -D__KERNEL__ -D_SYS_CONDVAR_H_ -DSMP
277
278.if ${MACHINE_ARCH} == "amd64"
279ZFS_CFLAGS+= -D__x86_64 -DHAVE_SSE2 -DHAVE_SSSE3 -DHAVE_SSE4_1 -DHAVE_SSE4_2 \
280	-DHAVE_AVX -DHAVE_AVX2 -DHAVE_AVX512F -DHAVE_AVX512VL -DHAVE_AVX512BW
281.endif
282
283.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "powerpc" || \
284	${MACHINE_ARCH} == "powerpcspe" || ${MACHINE_ARCH} == "arm"
285ZFS_CFLAGS+= -DBITS_PER_LONG=32
286.else
287ZFS_CFLAGS+= -DBITS_PER_LONG=64
288.endif
289
290
291ZFS_ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${ZFS_CFLAGS}
292ZFS_C=		${CC} -c ${ZFS_CFLAGS} ${WERROR} ${.IMPSRC}
293ZFS_RPC_C=	${CC} -c ${ZFS_CFLAGS} -DHAVE_RPC_TYPES ${WERROR} ${.IMPSRC}
294ZFS_S=		${CC} -c ${ZFS_ASM_CFLAGS} ${WERROR} ${.IMPSRC}
295
296# ATH driver
297ATH_CFLAGS=	-I${SRCTOP}/sys/dev/ath ${NO_WUNUSED_BUT_SET_VARIABLE}
298ATH_C=		${CC} -c ${CFLAGS} ${WERROR} ${ATH_CFLAGS} ${.IMPSRC}
299
300# Special flags for managing the compat compiles for DTrace
301DTRACE_CFLAGS=	-DBUILDING_DTRACE ${CDDL_CFLAGS} -I$S/cddl/dev/dtrace -I$S/cddl/dev/dtrace/${MACHINE_CPUARCH}
302.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
303DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/intel -I$S/cddl/dev/dtrace/x86
304.endif
305DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/common/util -I$S -DDIS_MEM -DSMP -I$S/cddl/compat/opensolaris
306DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/common
307DTRACE_ASM_CFLAGS=	-x assembler-with-cpp -DLOCORE ${DTRACE_CFLAGS}
308DTRACE_C=	${CC} -c ${DTRACE_CFLAGS}	${WERROR} ${.IMPSRC}
309DTRACE_S=	${CC} -c ${DTRACE_ASM_CFLAGS}	${WERROR} ${.IMPSRC}
310
311# zlib code supports systems that are quite old, but will fix this issue once C2x gets radified.
312# see https://github.com/madler/zlib/issues/633 for details
313ZLIB_CFLAGS=	-Wno-cast-qual ${NO_WDEPRECATED_NON_PROTOTYPE} ${NO_WSTRICT_PROTOTYPES}
314ZLIB_C=		${CC} -c ${CFLAGS} ${WERROR} ${ZLIB_CFLAGS} ${.IMPSRC}
315
316# Special flags for managing the compat compiles for DTrace/FBT
317FBT_CFLAGS=	-DBUILDING_DTRACE -nostdinc -I$S/cddl/dev/fbt/${MACHINE_CPUARCH} -I$S/cddl/dev/fbt ${CDDL_CFLAGS} -I$S/cddl/compat/opensolaris -I$S/cddl/contrib/opensolaris/uts/common
318.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
319FBT_CFLAGS+=	-I$S/cddl/dev/fbt/x86
320.endif
321FBT_C=		${CC} -c ${FBT_CFLAGS}		${WERROR} ${.IMPSRC}
322
323.if ${MK_CTF} != "no"
324NORMAL_CTFCONVERT=	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
325.elif ${MAKE_VERSION} >= 5201111300
326NORMAL_CTFCONVERT=
327.else
328NORMAL_CTFCONVERT=	@:
329.endif
330
331# Linux Kernel Programming Interface C-flags
332LINUXKPI_INCLUDES=	-I$S/compat/linuxkpi/common/include \
333			-I$S/compat/linuxkpi/dummy/include \
334			-include $S/compat/linuxkpi/common/include/linux/kconfig.h
335LINUXKPI_C=		${NORMAL_C} ${LINUXKPI_INCLUDES}
336
337# Infiniband C flags.  Correct include paths and omit errors that linux
338# does not honor.
339OFEDINCLUDES=	-I$S/ofed/include -I$S/ofed/include/uapi ${LINUXKPI_INCLUDES}
340OFEDNOERR=	-Wno-cast-qual -Wno-pointer-arith
341OFEDCFLAGS=	${CFLAGS:N-I*} -DCONFIG_INFINIBAND_USER_MEM \
342		${OFEDINCLUDES} ${CFLAGS:M-I*} ${OFEDNOERR}
343OFED_C_NOIMP=	${CC} -c -o ${.TARGET} ${OFEDCFLAGS} ${WERROR}
344OFED_C=		${OFED_C_NOIMP} ${.IMPSRC}
345
346# mlxfw C flags.
347MLXFW_C=	${OFED_C_NOIMP} \
348		-I${SRCTOP}/sys/contrib/xz-embedded/freebsd \
349		-I${SRCTOP}/sys/contrib/xz-embedded/linux/lib/xz \
350		${.IMPSRC}
351# BNXT Driver
352BNXT_CFLAGS=	-I$S/dev/bnxt/bnxt_en ${OFEDCFLAGS}
353BNXT_C_NOIMP=	${CC} -c -o ${.TARGET} ${BNXT_CFLAGS} ${WERROR}
354BNXT_C=		${BNXT_C_NOIMP} ${.IMPSRC}
355
356GEN_CFILES= $S/$M/$M/genassym.c ${MFILES:T:S/.m$/.c/}
357SYSTEM_CFILES= config.c env.c hints.c vnode_if.c
358SYSTEM_DEP= Makefile ${SYSTEM_OBJS}
359SYSTEM_OBJS= locore.o ${MDOBJS} ${OBJS}
360SYSTEM_OBJS+= ${SYSTEM_CFILES:.c=.o}
361SYSTEM_OBJS+= force-dynamic-hack.pico
362
363KEYMAP=kbdcontrol -P ${SRCTOP}/share/vt/keymaps -P ${SRCTOP}/share/syscons/keymaps
364KEYMAP_FIX=sed -e 's/^static keymap_t.* = /static keymap_t key_map = /' -e 's/^static accentmap_t.* = /static accentmap_t accent_map = /'
365
366MD_ROOT_SIZE_CONFIGURED!=	grep MD_ROOT_SIZE opt_md.h || true ; echo
367.if ${MFS_IMAGE:Uno} != "no"
368.if empty(MD_ROOT_SIZE_CONFIGURED)
369SYSTEM_OBJS+= embedfs_${MFS_IMAGE:T:R}.o
370.endif
371.endif
372SYSTEM_LD_BASECMD= \
373	${LD} -m ${LD_EMULATION} -Bdynamic -L $S/conf -T ${LDSCRIPT} ${_LDFLAGS} \
374	--no-warn-mismatch --warn-common --export-dynamic \
375	--dynamic-linker /red/herring -X
376SYSTEM_LD= @${SYSTEM_LD_BASECMD} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
377SYSTEM_LD_TAIL= @${SIZE} ${.TARGET} ; chmod 755 ${.TARGET}
378SYSTEM_DEP+= ${LDSCRIPT}
379
380# Calculate path for .m files early, if needed.
381.if !defined(NO_MODULES) && !defined(__MPATH) && !make(install) && \
382    (empty(.MAKEFLAGS:M-V) || defined(NO_SKIP_MPATH))
383__MPATH!=find ${S:tA}/ -name \*_if.m
384.endif
385
386# MKMODULESENV is set here so that port makefiles can augment
387# them.
388
389MKMODULESENV+=	MAKEOBJDIRPREFIX=${.OBJDIR}/modules KMODDIR=${KODIR}
390MKMODULESENV+=	MACHINE_CPUARCH=${MACHINE_CPUARCH}
391MKMODULESENV+=	MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH}
392MKMODULESENV+=	MODULES_EXTRA="${MODULES_EXTRA}" WITHOUT_MODULES="${WITHOUT_MODULES}"
393MKMODULESENV+=	ARCH_FLAGS="${ARCH_FLAGS}"
394.if (${KERN_IDENT} == LINT)
395MKMODULESENV+=	ALL_MODULES=LINT
396.endif
397.if defined(MODULES_OVERRIDE)
398MKMODULESENV+=	MODULES_OVERRIDE="${MODULES_OVERRIDE}"
399.endif
400.if defined(DEBUG)
401MKMODULESENV+=	DEBUG_FLAGS="${DEBUG}"
402.endif
403.if !defined(NO_MODULES)
404MKMODULESENV+=	__MPATH="${__MPATH}"
405.endif
406
407# Detect kernel config options that force stack frames to be turned on.
408DDB_ENABLED!=	grep DDB opt_ddb.h || true ; echo
409DTRACE_ENABLED!=grep KDTRACE_FRAME opt_kdtrace.h || true ; echo
410HWPMC_ENABLED!=	grep HWPMC opt_hwpmc_hooks.h || true ; echo
411