1 // SPDX-License-Identifier: GPL-2.0-only
2
3 /*
4 * Copyright (c) 2021, The Linux Foundation. All rights reserved.
5 */
6
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/android_debug_symbols.h>
11 #include <asm/stacktrace.h>
12 #include <asm/sections.h>
13
14 #include <linux/cma.h>
15 #include "../../mm/slab.h"
16 #include <linux/memblock.h>
17 #include <linux/page_owner.h>
18 #include <linux/swap.h>
19 #include <linux/mm.h>
20 #include <linux/security.h>
21
22 struct ads_entry {
23 char *name;
24 void *addr;
25 };
26
27 #define _ADS_ENTRY(index, symbol) \
28 [index] = { .name = #symbol, .addr = (void *)symbol }
29 #define ADS_ENTRY(index, symbol) _ADS_ENTRY(index, symbol)
30
31 #define _ADS_PER_CPU_ENTRY(index, symbol) \
32 [index] = { .name = #symbol, .addr = (void *)&symbol }
33 #define ADS_PER_CPU_ENTRY(index, symbol) _ADS_PER_CPU_ENTRY(index, symbol)
34
35 /*
36 * This module maintains static array of symbol and address information.
37 * Add all required core kernel symbols and their addresses into ads_entries[] array,
38 * so that vendor modules can query and to find address of non-exported symbol.
39 */
40 static const struct ads_entry ads_entries[ADS_END] = {
41 ADS_ENTRY(ADS_SDATA, _sdata),
42 ADS_ENTRY(ADS_BSS_END, __bss_stop),
43 ADS_ENTRY(ADS_PER_CPU_START, __per_cpu_start),
44 ADS_ENTRY(ADS_PER_CPU_END, __per_cpu_end),
45 ADS_ENTRY(ADS_START_RO_AFTER_INIT, __start_ro_after_init),
46 ADS_ENTRY(ADS_END_RO_AFTER_INIT, __end_ro_after_init),
47 ADS_ENTRY(ADS_LINUX_BANNER, linux_banner),
48 #ifdef CONFIG_CMA
49 ADS_ENTRY(ADS_TOTAL_CMA, &totalcma_pages),
50 #endif
51 ADS_ENTRY(ADS_SLAB_CACHES, &slab_caches),
52 ADS_ENTRY(ADS_SLAB_MUTEX, &slab_mutex),
53 ADS_ENTRY(ADS_MIN_LOW_PFN, &min_low_pfn),
54 ADS_ENTRY(ADS_MAX_PFN, &max_pfn),
55 #ifdef CONFIG_PAGE_OWNER
56 ADS_ENTRY(ADS_PAGE_OWNER_ENABLED, &page_owner_enabled),
57 #endif
58 #ifdef CONFIG_SLUB_DEBUG
59 ADS_ENTRY(ADS_SLUB_DEBUG, &slub_debug),
60 #endif
61 #ifdef CONFIG_SWAP
62 ADS_ENTRY(ADS_NR_SWAP_PAGES, &nr_swap_pages),
63 #endif
64 #ifdef CONFIG_MMU
65 ADS_ENTRY(ADS_MMAP_MIN_ADDR, &mmap_min_addr),
66 #endif
67 ADS_ENTRY(ADS_STACK_GUARD_GAP, &stack_guard_gap),
68 #ifdef CONFIG_SYSCTL
69 ADS_ENTRY(ADS_SYSCTL_LEGACY_VA_LAYOUT, &sysctl_legacy_va_layout),
70 #endif
71 };
72
73 /*
74 * ads_per_cpu_entries array contains all the per_cpu variable address information.
75 */
76 static const struct ads_entry ads_per_cpu_entries[ADS_DEBUG_PER_CPU_END] = {
77 #ifdef CONFIG_ARM64
78 ADS_PER_CPU_ENTRY(ADS_IRQ_STACK_PTR, irq_stack_ptr),
79 #endif
80 #ifdef CONFIG_X86
81 ADS_PER_CPU_ENTRY(ADS_IRQ_STACK_PTR, hardirq_stack_ptr),
82 #endif
83 };
84
85 /*
86 * android_debug_symbol - Provide address inforamtion of debug symbol.
87 * @symbol: Index of debug symbol array.
88 *
89 * Return address of core kernel symbol on success and a negative errno will be
90 * returned in error cases.
91 *
92 */
android_debug_symbol(enum android_debug_symbol symbol)93 void *android_debug_symbol(enum android_debug_symbol symbol)
94 {
95 if (symbol >= ADS_END)
96 return ERR_PTR(-EINVAL);
97
98 return ads_entries[symbol].addr;
99 }
100 EXPORT_SYMBOL_GPL(android_debug_symbol);
101
102 /*
103 * android_debug_per_cpu_symbol - Provide address inforamtion of per cpu debug symbol.
104 * @symbol: Index of per cpu debug symbol array.
105 *
106 * Return address of core kernel symbol on success and a negative errno will be
107 * returned in error cases.
108 *
109 */
android_debug_per_cpu_symbol(enum android_debug_per_cpu_symbol symbol)110 void *android_debug_per_cpu_symbol(enum android_debug_per_cpu_symbol symbol)
111 {
112 if (symbol >= ADS_DEBUG_PER_CPU_END)
113 return ERR_PTR(-EINVAL);
114
115 return ads_per_cpu_entries[symbol].addr;
116 }
117 EXPORT_SYMBOL_GPL(android_debug_per_cpu_symbol);
118
debug_symbol_init(void)119 static int __init debug_symbol_init(void)
120 {
121 return 0;
122 }
123 module_init(debug_symbol_init);
124
debug_symbol_exit(void)125 static void __exit debug_symbol_exit(void)
126 { }
127 module_exit(debug_symbol_exit);
128
129 MODULE_DESCRIPTION("Debug Symbol Driver");
130 MODULE_LICENSE("GPL v2");
131