Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2015, Huawei Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "llvm-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "config.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <subcmd/exec-cmd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define CLANG_BPF_CMD_DEFAULT_TEMPLATE				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		"$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		"-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE "	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		"$CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		"-Wno-unused-value -Wno-pointer-sign "		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		"-working-directory $WORKING_DIR "		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		"-c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct llvm_param llvm_param = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	.clang_path = "clang",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	.llc_path = "llc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	.clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	.clang_opt = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	.opts = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	.kbuild_dir = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	.kbuild_opts = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	.user_set_param = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) int perf_llvm_config(const char *var, const char *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (!strstarts(var, "llvm."))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	var += sizeof("llvm.") - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (!strcmp(var, "clang-path"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		llvm_param.clang_path = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	else if (!strcmp(var, "clang-bpf-cmd-template"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		llvm_param.clang_bpf_cmd_template = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	else if (!strcmp(var, "clang-opt"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		llvm_param.clang_opt = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	else if (!strcmp(var, "kbuild-dir"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		llvm_param.kbuild_dir = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	else if (!strcmp(var, "kbuild-opts"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		llvm_param.kbuild_opts = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	else if (!strcmp(var, "dump-obj"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		llvm_param.dump_obj = !!perf_config_bool(var, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	else if (!strcmp(var, "opts"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		llvm_param.opts = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		pr_debug("Invalid LLVM config option: %s\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	llvm_param.user_set_param = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) search_program(const char *def, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	       char *output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	char *env, *path, *tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	char buf[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	output[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (def && def[0] != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (def[0] == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			if (access(def, F_OK) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				strlcpy(output, def, PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		} else if (def[0] != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			name = def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	env = getenv("PATH");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (!env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	env = strdup(env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (!env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	path = strtok_r(env, ":",  &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	while (path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		scnprintf(buf, sizeof(buf), "%s/%s", path, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (access(buf, F_OK) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			strlcpy(output, buf, PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		path = strtok_r(NULL, ":", &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	free(env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define READ_SIZE	4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	void *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	FILE *file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	size_t read_sz = 0, buf_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	char serr[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	file = popen(cmd, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		pr_err("ERROR: unable to popen cmd: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		       str_error_r(errno, serr, sizeof(serr)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	while (!feof(file) && !ferror(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		 * Make buf_sz always have obe byte extra space so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		 * can put '\0' there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (buf_sz - read_sz < READ_SIZE + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			void *new_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			buf_sz = read_sz + READ_SIZE + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			new_buf = realloc(buf, buf_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			if (!new_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				pr_err("ERROR: failed to realloc memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 				goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			buf = new_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (buf_sz - read_sz < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		pr_err("ERROR: internal error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (ferror(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		pr_err("ERROR: error occurred when reading from pipe: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		       str_error_r(errno, serr, sizeof(serr)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	err = WEXITSTATUS(pclose(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * If buf is string, give it terminal '\0' to make our life
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * easier. If buf is not string, that '\0' is out of space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * indicated by read_sz so caller won't even notice it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	((char *)buf)[read_sz] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!p_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		*p_buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (p_read_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		*p_read_sz = read_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		pclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (p_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		*p_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (p_read_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		*p_read_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) force_set_env(const char *var, const char *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		setenv(var, value, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		pr_debug("set env: %s=%s\n", var, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		unsetenv(var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		pr_debug("unset env: %s\n", var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) version_notice(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) "     \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) "     \tYou may want to try git trunk:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) "     \t\tgit clone http://llvm.org/git/llvm.git\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) "     \t\t     and\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) "     \t\tgit clone http://llvm.org/git/clang.git\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) "     \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) "     \tdebian/ubuntu:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) "     \t\thttp://llvm.org/apt\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) "     \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) "     \toption in [llvm] section of ~/.perfconfig to:\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) "     \t  \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS \\\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) "     \t     -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) "     \t     -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) "     \t(Replace /path/to/llc with path to your llc)\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int detect_kbuild_dir(char **kbuild_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	const char *test_dir = llvm_param.kbuild_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	const char *prefix_dir = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	const char *suffix_dir = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	/* _UTSNAME_LENGTH is 65 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	char release[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	char *autoconf_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (!test_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		err = fetch_kernel_version(NULL, release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 					   sizeof(release));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		test_dir = release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		prefix_dir = "/lib/modules/";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		suffix_dir = "/build";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		       prefix_dir, test_dir, suffix_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (access(autoconf_path, R_OK) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		free(autoconf_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			       suffix_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		 __func__, autoconf_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	free(autoconf_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static const char *kinc_fetch_script =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) "#!/usr/bin/env sh\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) "if ! test -d \"$KBUILD_DIR\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) "then\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) "	exit 1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) "fi\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) "then\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) "	exit 1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) "fi\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) "TMPDIR=`mktemp -d`\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) "if test -z \"$TMPDIR\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) "then\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) "    exit 1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) "fi\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) "cat << EOF > $TMPDIR/Makefile\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) "obj-y := dummy.o\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) "\\$(obj)/%.o: \\$(src)/%.c\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) "\t\\$(CC) -c -o \\$@ \\$<\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) "EOF\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) "touch $TMPDIR/dummy.c\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) "RET=$?\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) "rm -rf $TMPDIR\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) "exit $RET\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	static char *saved_kbuild_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	static char *saved_kbuild_include_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (!kbuild_dir || !kbuild_include_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	*kbuild_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	*kbuild_include_opts = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (saved_kbuild_dir && saved_kbuild_include_opts &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	    !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		*kbuild_dir = strdup(saved_kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		*kbuild_include_opts = strdup(saved_kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (*kbuild_dir && *kbuild_include_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		zfree(kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		zfree(kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		 * Don't fall through: it may breaks saved_kbuild_dir and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		 * saved_kbuild_include_opts if detect them again when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		 * memory is low.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		pr_debug("Skip kbuild options detection.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	err = detect_kbuild_dir(kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		pr_warning(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) "WARNING:\tunable to get correct kernel building directory.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) "     \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) "     \tdetection.\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	force_set_env("KBUILD_DIR", *kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	err = read_from_pipe(kinc_fetch_script,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			     (void **)kbuild_include_opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			     NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		pr_warning(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) "WARNING:\tunable to get kernel include directories from '%s'\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) "     \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) "     \toption in [llvm] to \"\" to suppress this detection.\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			*kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		zfree(kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	pr_debug("include option is set to %s\n", *kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	saved_kbuild_dir = strdup(*kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	saved_kbuild_include_opts = strdup(*kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		zfree(&saved_kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		zfree(&saved_kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	saved_kbuild_dir = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	saved_kbuild_include_opts = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) int llvm__get_nr_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	static int nr_cpus_avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	char serr[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (nr_cpus_avail > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return nr_cpus_avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (nr_cpus_avail <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) "WARNING:\tunable to get available CPUs in this system: %s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) "        \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		nr_cpus_avail = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	return nr_cpus_avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	char *obj_path = strdup(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!obj_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		pr_warning("WARNING: Not enough memory, skip object dumping\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	p = strrchr(obj_path, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (!p || (strcmp(p, ".c") != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			   obj_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	p[1] = 'o';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	fp = fopen(obj_path, "wb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			   obj_path, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	pr_debug("LLVM: dumping %s\n", obj_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (fwrite(obj_buf, size, 1, fp) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		pr_debug("WARNING: failed to write to file '%s': %s, skip object dumping\n", obj_path, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	free(obj_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) int llvm__compile_bpf(const char *path, void **p_obj_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		      size_t *p_obj_buf_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	size_t obj_buf_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	void *obj_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	int err, nr_cpus_avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	unsigned int kernel_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	char linux_version_code_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	const char *clang_opt = llvm_param.clang_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	char clang_path[PATH_MAX], llc_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	char serr[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	char *kbuild_dir = NULL, *kbuild_include_opts = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	     *perf_bpf_include_opts = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	const char *template = llvm_param.clang_bpf_cmd_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	char *pipe_template = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	const char *opts = llvm_param.opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	char *command_echo = NULL, *command_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	char *perf_include_dir = system_path(PERF_INCLUDE_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (path[0] != '-' && realpath(path, abspath) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		err = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		pr_err("ERROR: problems with path %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		       path, str_error_r(err, serr, sizeof(serr)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		return -err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (!template)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	err = search_program(llvm_param.clang_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			     "clang", clang_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) "ERROR:\tunable to find clang.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) "     \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		version_notice();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	 * This is an optional work. Even it fail we can continue our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	 * work. Needn't to check error return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	nr_cpus_avail = llvm__get_nr_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		 nr_cpus_avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (fetch_kernel_version(&kernel_version, NULL, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		kernel_version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	snprintf(linux_version_code_str, sizeof(linux_version_code_str),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		 "0x%x", kernel_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (asprintf(&perf_bpf_include_opts, "-I%s/bpf", perf_include_dir) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	force_set_env("NR_CPUS", nr_cpus_avail_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	force_set_env("CLANG_EXEC", clang_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	force_set_env("CLANG_OPTIONS", clang_opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	force_set_env("PERF_BPF_INC_OPTIONS", perf_bpf_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	force_set_env("WORKING_DIR", kbuild_dir ? : ".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	if (opts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		err = search_program(llvm_param.llc_path, "llc", llc_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			pr_err("ERROR:\tunable to find llc.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			       "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			       "     \tand 'llc-path' option in [llvm] section of ~/.perfconfig.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			version_notice();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		if (asprintf(&pipe_template, "%s -emit-llvm | %s -march=bpf %s -filetype=obj -o -",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			      template, llc_path, opts) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			pr_err("ERROR:\tnot enough memory to setup command line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		template = pipe_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	 * Since we may reset clang's working dir, path of source file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	 * should be transferred into absolute path, except we want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	 * stdin to be source file (testing).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	force_set_env("CLANG_SOURCE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		      (path[0] == '-') ? path : abspath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	pr_debug("llvm compiling command template: %s\n", template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	err = read_from_pipe(command_echo, (void **) &command_out, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	pr_debug("llvm compiling command : %s\n", command_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		pr_err("ERROR:\tunable to compile %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		pr_err("Hint:\tCheck error message shown above.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		pr_err("     \t\tclang -target bpf -O2 -c %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		pr_err("     \twith proper -I and -D options.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	free(command_echo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	free(command_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	free(kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	free(kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	free(perf_bpf_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	free(perf_include_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	if (!p_obj_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		free(obj_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		*p_obj_buf = obj_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (p_obj_buf_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		*p_obj_buf_sz = obj_buf_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	free(command_echo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	free(kbuild_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	free(kbuild_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	free(obj_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	free(perf_bpf_include_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	free(perf_include_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	free(pipe_template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (p_obj_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		*p_obj_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	if (p_obj_buf_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		*p_obj_buf_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) int llvm__search_clang(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	char clang_path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	return search_program(llvm_param.clang_path, "clang", clang_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }