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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Manage printing of source lines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2017, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Andi Kleen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "srccode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <internal/lib.h> // page_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "fncache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define MAXSRCCACHE (32*1024*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define MAXSRCFILES     64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define SRC_HTAB_SZ	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) struct srcfile {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct hlist_node hash_nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct list_head nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	char *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	char **lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned numlines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	size_t maplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static struct hlist_head srcfile_htab[SRC_HTAB_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static LIST_HEAD(srcfile_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static long map_total_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static int num_srcfiles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static int countlines(char *map, int maplen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int numl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	char *end = map + maplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	char *p = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (maplen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	numl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		numl++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (p < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		numl++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return numl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void fill_lines(char **lines, int maxline, char *map, int maplen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	char *end = map + maplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	char *p = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (maplen == 0 || maxline == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	lines[l++] = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		if (l >= maxline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		lines[l++] = ++p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (p < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		lines[l] = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void free_srcfile(struct srcfile *sf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	list_del_init(&sf->nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	hlist_del(&sf->hash_nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	map_total_sz -= sf->maplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	munmap(sf->map, sf->maplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	zfree(&sf->lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	zfree(&sf->fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	free(sf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	num_srcfiles--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static struct srcfile *find_srcfile(char *fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct srcfile *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	unsigned long sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	unsigned hval = shash((unsigned char *)fn) % SRC_HTAB_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	hlist_for_each_entry (h, &srcfile_htab[hval], hash_nd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (!strcmp(fn, h->fn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			/* Move to front */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			list_del(&h->nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			list_add(&h->nd, &srcfile_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* Only prune if there is more than one entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	while ((num_srcfiles > MAXSRCFILES || map_total_sz > MAXSRCCACHE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	       srcfile_list.next != &srcfile_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		assert(!list_empty(&srcfile_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		h = list_entry(srcfile_list.prev, struct srcfile, nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		free_srcfile(h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	fd = open(fn, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (fd < 0 || fstat(fd, &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		pr_debug("cannot open source file %s\n", fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return NULL;
^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) 	h = malloc(sizeof(struct srcfile));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	h->fn = strdup(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (!h->fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		goto out_h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	h->maplen = st.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	sz = (h->maplen + page_size - 1) & ~(page_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	h->map = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (h->map == (char *)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		pr_debug("cannot mmap source file %s\n", fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		goto out_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	h->numlines = countlines(h->map, h->maplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	h->lines = calloc(h->numlines, sizeof(char *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (!h->lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		goto out_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	fill_lines(h->lines, h->numlines, h->map, h->maplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	list_add(&h->nd, &srcfile_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	hlist_add_head(&h->hash_nd, &srcfile_htab[hval]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	map_total_sz += h->maplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	num_srcfiles++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) out_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	munmap(h->map, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) out_fn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	zfree(&h->fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) out_h:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	free(h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return NULL;
^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) /* Result is not 0 terminated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) char *find_sourceline(char *fn, unsigned line, int *lenp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	char *l, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct srcfile *sf = find_srcfile(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (!sf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	line--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (line >= sf->numlines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	l = sf->lines[line];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (!l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	p = memchr(l, '\n', sf->map + sf->maplen - l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	*lenp = p - l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }