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-only OR BSD-2-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Based on:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Minimal BPF JIT image disassembler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * debugging or verification purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright 2013 Daniel Borkmann <daniel@iogearbox.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Licensed under the GNU General Public License, version 2.0 (GPLv2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <bfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <dis-asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "json_writer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static void get_exec_path(char *tpath, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	const char *path = "/proc/self/exe";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	len = readlink(path, tpath, size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	assert(len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	tpath[len] = 0;
^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) static int oper_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int fprintf_json(void *out, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	err = vasprintf(&s, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (!oper_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		/* Strip trailing spaces */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		i = strlen(s) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		while (s[i] == ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			s[i--] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		jsonw_string_field(json_wtr, "operation", s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		jsonw_name(json_wtr, "operands");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		oper_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	} else if (!strcmp(fmt, ",")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		   /* Skip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		jsonw_string(json_wtr, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		oper_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	free(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		       const char *arch, const char *disassembler_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		       const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		       const struct bpf_prog_linfo *prog_linfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		       __u64 func_ksym, unsigned int func_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		       bool linum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	const struct bpf_line_info *linfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	disassembler_ftype disassemble;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct disassemble_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned int nr_skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	int count, i, pc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	char tpath[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	bfd *bfdf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	memset(tpath, 0, sizeof(tpath));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	get_exec_path(tpath, sizeof(tpath));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	bfdf = bfd_openr(tpath, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	assert(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	assert(bfd_check_format(bfdf, bfd_object));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		init_disassemble_info(&info, stdout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				      (fprintf_ftype) fprintf_json);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		init_disassemble_info(&info, stdout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				      (fprintf_ftype) fprintf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* Update architecture info for offload. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (arch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		const bfd_arch_info_type *inf = bfd_scan_arch(arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		if (inf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			bfdf->arch_info = inf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			p_err("No libbfd support for %s", arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	info.arch = bfd_get_arch(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	info.mach = bfd_get_mach(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (disassembler_options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		info.disassembler_options = disassembler_options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	info.buffer = image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	info.buffer_length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	disassemble_init_for_target(&info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #ifdef DISASM_FOUR_ARGS_SIGNATURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	disassemble = disassembler(info.arch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				   bfd_big_endian(bfdf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				   info.mach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				   bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	disassemble = disassembler(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	assert(disassemble);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (prog_linfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 								func_ksym + pc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 								func_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 								nr_skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			if (linfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				nr_skip++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			jsonw_start_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			oper_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			if (linfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				btf_dump_linfo_json(btf, linfo, linum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			jsonw_name(json_wtr, "pc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			jsonw_printf(json_wtr, "\"0x%x\"", pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			if (linfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				btf_dump_linfo_plain(btf, linfo, "; ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 						     linum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			printf("%4x:\t", pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		count = disassemble(pc, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			/* Operand array, was started in fprintf_json. Before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			 * that, make sure we have a _null_ value if no operand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			 * other than operation code was present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			if (oper_count == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		if (opcodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				jsonw_name(json_wtr, "opcodes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				for (i = 0; i < count; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					jsonw_printf(json_wtr, "\"0x%02hhx\"",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 						     (uint8_t)image[pc + i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				printf("\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				for (i = 0; i < count; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 					printf("%02x ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					       (uint8_t)image[pc + i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			jsonw_end_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		pc += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	} while (count > 0 && pc < len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	bfd_close(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int disasm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	bfd_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }