2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-05-18 18:31:52 +03:00
|
|
|
#include <AK/TemporaryChange.h>
|
2021-10-15 00:53:48 +03:00
|
|
|
#include <Kernel/Arch/SmapDisabler.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2019-11-06 15:42:38 +03:00
|
|
|
#include <Kernel/KSyms.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Scheduler.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2018-12-25 00:59:10 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-04-08 14:30:50 +03:00
|
|
|
FlatPtr g_lowest_kernel_symbol_address = 0xffffffff;
|
|
|
|
FlatPtr g_highest_kernel_symbol_address = 0;
|
|
|
|
bool g_kernel_symbols_available = false;
|
|
|
|
|
2021-07-14 22:04:18 +03:00
|
|
|
extern "C" {
|
|
|
|
__attribute__((section(".kernel_symbols"))) char kernel_symbols[5 * MiB] {};
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:50 +03:00
|
|
|
static KernelSymbol* s_symbols;
|
|
|
|
static size_t s_symbol_count = 0;
|
2018-12-25 00:59:10 +03:00
|
|
|
|
2021-12-24 16:19:13 +03:00
|
|
|
UNMAP_AFTER_INIT static u8 parse_hex_digit(char nibble)
|
2018-12-25 00:59:10 +03:00
|
|
|
{
|
|
|
|
if (nibble >= '0' && nibble <= '9')
|
|
|
|
return nibble - '0';
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(nibble >= 'a' && nibble <= 'f');
|
2018-12-25 00:59:10 +03:00
|
|
|
return 10 + (nibble - 'a');
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
FlatPtr address_for_kernel_symbol(StringView name)
|
2019-11-28 23:30:20 +03:00
|
|
|
{
|
2020-04-08 14:30:50 +03:00
|
|
|
for (size_t i = 0; i < s_symbol_count; ++i) {
|
2021-04-28 21:05:35 +03:00
|
|
|
const auto& symbol = s_symbols[i];
|
|
|
|
if (name == symbol.name)
|
|
|
|
return symbol.address;
|
2019-11-28 23:30:20 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-25 18:18:36 +03:00
|
|
|
const KernelSymbol* symbolicate_kernel_address(FlatPtr address)
|
2018-12-25 00:59:10 +03:00
|
|
|
{
|
2020-04-08 14:30:50 +03:00
|
|
|
if (address < g_lowest_kernel_symbol_address || address > g_highest_kernel_symbol_address)
|
2018-12-25 00:59:10 +03:00
|
|
|
return nullptr;
|
2020-04-08 14:30:50 +03:00
|
|
|
for (unsigned i = 0; i < s_symbol_count; ++i) {
|
|
|
|
if (address < s_symbols[i + 1].address)
|
|
|
|
return &s_symbols[i];
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-17 11:52:06 +03:00
|
|
|
UNMAP_AFTER_INIT static void load_kernel_symbols_from_data(Bytes buffer)
|
2018-12-25 00:59:10 +03:00
|
|
|
{
|
2020-04-08 14:30:50 +03:00
|
|
|
g_lowest_kernel_symbol_address = 0xffffffff;
|
|
|
|
g_highest_kernel_symbol_address = 0;
|
|
|
|
|
2021-12-17 11:52:06 +03:00
|
|
|
auto* bufptr = (char*)buffer.data();
|
2018-12-25 00:59:10 +03:00
|
|
|
auto* start_of_name = bufptr;
|
2020-04-08 14:30:50 +03:00
|
|
|
FlatPtr address = 0;
|
2018-12-25 00:59:10 +03:00
|
|
|
|
2020-04-08 14:30:50 +03:00
|
|
|
for (size_t i = 0; i < 8; ++i)
|
|
|
|
s_symbol_count = (s_symbol_count << 4) | parse_hex_digit(*(bufptr++));
|
2021-12-28 21:12:22 +03:00
|
|
|
s_symbols = static_cast<KernelSymbol*>(kmalloc(sizeof(KernelSymbol) * s_symbol_count));
|
2018-12-25 00:59:10 +03:00
|
|
|
++bufptr; // skip newline
|
|
|
|
|
2021-03-12 16:06:37 +03:00
|
|
|
dmesgln("Loading kernel symbol table...");
|
2018-12-25 00:59:10 +03:00
|
|
|
|
2020-04-08 14:30:50 +03:00
|
|
|
size_t current_symbol_index = 0;
|
2018-12-25 00:59:10 +03:00
|
|
|
|
2021-07-14 22:04:18 +03:00
|
|
|
while ((u8 const*)bufptr < buffer.data() + buffer.size()) {
|
2021-12-13 06:28:24 +03:00
|
|
|
for (size_t i = 0; i < sizeof(void*) * 2; ++i)
|
2018-12-25 00:59:10 +03:00
|
|
|
address = (address << 4) | parse_hex_digit(*(bufptr++));
|
|
|
|
bufptr += 3;
|
|
|
|
start_of_name = bufptr;
|
|
|
|
while (*(++bufptr)) {
|
|
|
|
if (*bufptr == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 14:30:50 +03:00
|
|
|
auto& ksym = s_symbols[current_symbol_index];
|
2021-07-26 16:10:51 +03:00
|
|
|
ksym.address = kernel_load_base + address;
|
2021-12-17 11:52:06 +03:00
|
|
|
ksym.name = start_of_name;
|
|
|
|
|
|
|
|
*bufptr = '\0';
|
2018-12-25 00:59:10 +03:00
|
|
|
|
2020-04-08 14:30:50 +03:00
|
|
|
if (ksym.address < g_lowest_kernel_symbol_address)
|
|
|
|
g_lowest_kernel_symbol_address = ksym.address;
|
|
|
|
if (ksym.address > g_highest_kernel_symbol_address)
|
|
|
|
g_highest_kernel_symbol_address = ksym.address;
|
2018-12-25 00:59:10 +03:00
|
|
|
|
|
|
|
++bufptr;
|
2020-04-08 14:30:50 +03:00
|
|
|
++current_symbol_index;
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
2020-04-08 14:30:50 +03:00
|
|
|
g_kernel_symbols_available = true;
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
|
|
|
|
2021-08-04 18:52:14 +03:00
|
|
|
NEVER_INLINE static void dump_backtrace_impl(FlatPtr base_pointer, bool use_ksyms, PrintToScreen print_to_screen)
|
2018-12-25 00:59:10 +03:00
|
|
|
{
|
2021-08-04 18:52:14 +03:00
|
|
|
#define PRINT_LINE(fmtstr, ...) \
|
|
|
|
do { \
|
|
|
|
if (print_to_screen == PrintToScreen::No) \
|
|
|
|
dbgln(fmtstr, __VA_ARGS__); \
|
|
|
|
else \
|
|
|
|
critical_dmesgln(fmtstr, __VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
|
2020-01-05 20:00:15 +03:00
|
|
|
SmapDisabler disabler;
|
2021-10-07 20:51:24 +03:00
|
|
|
if (use_ksyms && !g_kernel_symbols_available)
|
2020-07-06 16:27:22 +03:00
|
|
|
Processor::halt();
|
2020-03-02 12:40:40 +03:00
|
|
|
|
2018-12-25 00:59:10 +03:00
|
|
|
struct RecognizedSymbol {
|
2020-04-08 14:30:50 +03:00
|
|
|
FlatPtr address;
|
|
|
|
const KernelSymbol* symbol { nullptr };
|
2018-12-25 00:59:10 +03:00
|
|
|
};
|
2021-03-10 18:23:11 +03:00
|
|
|
constexpr size_t max_recognized_symbol_count = 256;
|
2019-04-16 00:50:25 +03:00
|
|
|
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
|
2020-04-08 14:30:50 +03:00
|
|
|
size_t recognized_symbol_count = 0;
|
2018-12-25 00:59:10 +03:00
|
|
|
if (use_ksyms) {
|
2020-09-12 06:11:07 +03:00
|
|
|
FlatPtr copied_stack_ptr[2];
|
|
|
|
for (FlatPtr* stack_ptr = (FlatPtr*)base_pointer; stack_ptr && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (FlatPtr*)copied_stack_ptr[0]) {
|
2021-07-26 16:10:51 +03:00
|
|
|
if ((FlatPtr)stack_ptr < kernel_load_base)
|
2020-11-06 11:09:51 +03:00
|
|
|
break;
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
void* fault_at;
|
|
|
|
if (!safe_memcpy(copied_stack_ptr, stack_ptr, sizeof(copied_stack_ptr), fault_at))
|
|
|
|
break;
|
|
|
|
FlatPtr retaddr = copied_stack_ptr[1];
|
2020-04-08 14:30:50 +03:00
|
|
|
recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
2019-04-16 00:50:25 +03:00
|
|
|
} else {
|
2020-09-12 06:11:07 +03:00
|
|
|
void* fault_at;
|
|
|
|
FlatPtr copied_stack_ptr[2];
|
|
|
|
FlatPtr* stack_ptr = (FlatPtr*)base_pointer;
|
|
|
|
while (stack_ptr && safe_memcpy(copied_stack_ptr, stack_ptr, sizeof(copied_stack_ptr), fault_at)) {
|
|
|
|
FlatPtr retaddr = copied_stack_ptr[1];
|
2021-08-04 18:52:14 +03:00
|
|
|
PRINT_LINE("{:p} (next: {:p})", retaddr, stack_ptr ? (FlatPtr*)copied_stack_ptr[0] : 0);
|
2020-09-12 06:11:07 +03:00
|
|
|
stack_ptr = (FlatPtr*)copied_stack_ptr[0];
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(recognized_symbol_count <= max_recognized_symbol_count);
|
2020-04-08 14:30:50 +03:00
|
|
|
for (size_t i = 0; i < recognized_symbol_count; ++i) {
|
2019-04-16 00:50:25 +03:00
|
|
|
auto& symbol = recognized_symbols[i];
|
2019-04-30 15:47:22 +03:00
|
|
|
if (!symbol.address)
|
|
|
|
break;
|
2020-04-08 14:30:50 +03:00
|
|
|
if (!symbol.symbol) {
|
2021-08-04 18:52:14 +03:00
|
|
|
PRINT_LINE("Kernel + {:p}", symbol.address - kernel_load_base);
|
2019-04-30 15:47:22 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-08 14:30:50 +03:00
|
|
|
size_t offset = symbol.address - symbol.symbol->address;
|
|
|
|
if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
|
2021-08-04 18:52:14 +03:00
|
|
|
PRINT_LINE("Kernel + {:p}", symbol.address - kernel_load_base);
|
2019-04-30 15:47:22 +03:00
|
|
|
else
|
2021-08-04 18:52:14 +03:00
|
|
|
PRINT_LINE("Kernel + {:p} {} +{:#x}", symbol.address - kernel_load_base, symbol.symbol->name, offset);
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:52:14 +03:00
|
|
|
void dump_backtrace(PrintToScreen print_to_screen)
|
2019-02-24 16:51:48 +03:00
|
|
|
{
|
2019-05-18 18:31:52 +03:00
|
|
|
static bool in_dump_backtrace = false;
|
2019-11-08 19:36:29 +03:00
|
|
|
if (in_dump_backtrace)
|
2019-05-18 18:31:52 +03:00
|
|
|
return;
|
|
|
|
TemporaryChange change(in_dump_backtrace, true);
|
2019-08-07 21:35:41 +03:00
|
|
|
TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
|
2021-12-13 06:28:24 +03:00
|
|
|
FlatPtr base_pointer;
|
|
|
|
#if ARCH(I386)
|
2019-06-07 12:43:58 +03:00
|
|
|
asm volatile("movl %%ebp, %%eax"
|
2021-12-13 06:28:24 +03:00
|
|
|
: "=a"(base_pointer));
|
|
|
|
#else
|
|
|
|
asm volatile("movq %%rbp, %%rax"
|
|
|
|
: "=a"(base_pointer));
|
|
|
|
#endif
|
|
|
|
dump_backtrace_impl(base_pointer, g_kernel_symbols_available, print_to_screen);
|
2019-02-24 16:51:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:29:46 +03:00
|
|
|
UNMAP_AFTER_INIT void load_kernel_symbol_table()
|
2018-12-25 00:59:10 +03:00
|
|
|
{
|
2021-07-14 22:04:18 +03:00
|
|
|
auto kernel_symbols_size = strnlen(kernel_symbols, sizeof(kernel_symbols));
|
|
|
|
// If we're hitting this VERIFY the kernel symbol file has grown beyond
|
|
|
|
// the array size of kernel_symbols. Try making the array larger.
|
|
|
|
VERIFY(kernel_symbols_size != sizeof(kernel_symbols));
|
|
|
|
load_kernel_symbols_from_data({ kernel_symbols, kernel_symbols_size });
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|