Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Demangle.h>
28#include <AK/TemporaryChange.h>
29#include <Kernel/FileSystem/FileDescription.h>
30#include <Kernel/KSyms.h>
31#include <Kernel/Process.h>
32#include <Kernel/Scheduler.h>
33#include <LibELF/ELFLoader.h>
34
35namespace Kernel {
36
37FlatPtr g_lowest_kernel_symbol_address = 0xffffffff;
38FlatPtr g_highest_kernel_symbol_address = 0;
39bool g_kernel_symbols_available = false;
40
41static KernelSymbol* s_symbols;
42static size_t s_symbol_count = 0;
43
44static u8 parse_hex_digit(char nibble)
45{
46 if (nibble >= '0' && nibble <= '9')
47 return nibble - '0';
48 ASSERT(nibble >= 'a' && nibble <= 'f');
49 return 10 + (nibble - 'a');
50}
51
52u32 address_for_kernel_symbol(const StringView& name)
53{
54 for (size_t i = 0; i < s_symbol_count; ++i) {
55 if (!strncmp(name.characters_without_null_termination(), s_symbols[i].name, name.length()))
56 return s_symbols[i].address;
57 }
58 return 0;
59}
60
61const KernelSymbol* symbolicate_kernel_address(u32 address)
62{
63 if (address < g_lowest_kernel_symbol_address || address > g_highest_kernel_symbol_address)
64 return nullptr;
65 for (unsigned i = 0; i < s_symbol_count; ++i) {
66 if (address < s_symbols[i + 1].address)
67 return &s_symbols[i];
68 }
69 return nullptr;
70}
71
72static void load_kernel_sybols_from_data(const ByteBuffer& buffer)
73{
74 g_lowest_kernel_symbol_address = 0xffffffff;
75 g_highest_kernel_symbol_address = 0;
76
77 auto* bufptr = (const char*)buffer.data();
78 auto* start_of_name = bufptr;
79 FlatPtr address = 0;
80
81 for (size_t i = 0; i < 8; ++i)
82 s_symbol_count = (s_symbol_count << 4) | parse_hex_digit(*(bufptr++));
83 s_symbols = static_cast<KernelSymbol*>(kmalloc_eternal(sizeof(KernelSymbol) * s_symbol_count));
84 ++bufptr; // skip newline
85
86 klog() << "Loading kernel symbol table...";
87
88 size_t current_symbol_index = 0;
89
90 while (bufptr < buffer.end_pointer()) {
91 for (size_t i = 0; i < 8; ++i)
92 address = (address << 4) | parse_hex_digit(*(bufptr++));
93 bufptr += 3;
94 start_of_name = bufptr;
95 while (*(++bufptr)) {
96 if (*bufptr == '\n') {
97 break;
98 }
99 }
100 auto& ksym = s_symbols[current_symbol_index];
101 ksym.address = address;
102 char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
103 memcpy(name, start_of_name, bufptr - start_of_name);
104 name[bufptr - start_of_name] = '\0';
105 ksym.name = name;
106
107 if (ksym.address < g_lowest_kernel_symbol_address)
108 g_lowest_kernel_symbol_address = ksym.address;
109 if (ksym.address > g_highest_kernel_symbol_address)
110 g_highest_kernel_symbol_address = ksym.address;
111
112 ++bufptr;
113 ++current_symbol_index;
114 }
115 g_kernel_symbols_available = true;
116}
117
118[[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms)
119{
120 SmapDisabler disabler;
121#if 0
122 if (!current) {
123 //hang();
124 return;
125 }
126#endif
127 if (use_ksyms && !g_kernel_symbols_available) {
128 hang();
129 return;
130 }
131
132 OwnPtr<Process::ELFBundle> elf_bundle;
133 if (Process::current)
134 elf_bundle = Process::current->elf_bundle();
135
136 struct RecognizedSymbol {
137 FlatPtr address;
138 const KernelSymbol* symbol { nullptr };
139 };
140 size_t max_recognized_symbol_count = 256;
141 RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
142 size_t recognized_symbol_count = 0;
143 if (use_ksyms) {
144 for (FlatPtr* stack_ptr = (FlatPtr*)ebp;
145 (Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) {
146 FlatPtr retaddr = stack_ptr[1];
147 recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
148 }
149 } else {
150 for (FlatPtr* stack_ptr = (FlatPtr*)ebp;
151 (Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) {
152 FlatPtr retaddr = stack_ptr[1];
153 dbg() << String::format("%x", retaddr) << " (next: " << String::format("%x", (stack_ptr ? (u32*)*stack_ptr : 0)) << ")";
154 }
155 return;
156 }
157 ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
158 for (size_t i = 0; i < recognized_symbol_count; ++i) {
159 auto& symbol = recognized_symbols[i];
160 if (!symbol.address)
161 break;
162 if (!symbol.symbol) {
163 if (elf_bundle && elf_bundle->elf_loader->has_symbols()) {
164 dbg() << String::format("%p", symbol.address) << " " << elf_bundle->elf_loader->symbolicate(symbol.address);
165 } else {
166 dbg() << String::format("%p", symbol.address) << " (no ELF symbols for process)";
167 }
168 continue;
169 }
170 size_t offset = symbol.address - symbol.symbol->address;
171 if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
172 dbg() << String::format("%p", symbol.address);
173 else
174 dbg() << String::format("%p", symbol.address) << " " << demangle(symbol.symbol->name) << " +" << offset;
175 }
176}
177
178void dump_backtrace()
179{
180 static bool in_dump_backtrace = false;
181 if (in_dump_backtrace)
182 return;
183 TemporaryChange change(in_dump_backtrace, true);
184 TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
185 FlatPtr ebp;
186 asm volatile("movl %%ebp, %%eax"
187 : "=a"(ebp));
188 dump_backtrace_impl(ebp, g_kernel_symbols_available);
189}
190
191void load_kernel_symbol_table()
192{
193 auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
194 ASSERT(!result.is_error());
195 auto description = result.value();
196 auto buffer = description->read_entire_file();
197 ASSERT(buffer);
198 load_kernel_sybols_from_data(buffer);
199}
200
201}