From 60d6137e7345f05cc94462ac1d873a28d5c672fc Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 22 Jul 2021 18:41:52 +0200 Subject: [PATCH] Userland: Use /proc/kernel_base to determine the kernel base address This removes all the hard-coded kernel base addresses from userspace tools. One downside for this is that e.g. Profiler no longer uses a different color for kernel symbols when run as a non-root user. --- Userland/DevTools/HackStudio/CMakeLists.txt | 2 +- .../HackStudio/Debugger/DisassemblyModel.cpp | 10 ++-- Userland/DevTools/Profiler/CMakeLists.txt | 2 +- .../DevTools/Profiler/DisassemblyModel.cpp | 10 ++-- Userland/DevTools/Profiler/Profile.cpp | 12 ++--- Userland/DevTools/Profiler/ProfileModel.cpp | 10 ++-- .../LibSymbolication/Symbolication.cpp | 54 +++++++++++++++---- .../LibSymbolication/Symbolication.h | 1 + Userland/Utilities/bt.cpp | 10 ++-- 9 files changed, 63 insertions(+), 48 deletions(-) diff --git a/Userland/DevTools/HackStudio/CMakeLists.txt b/Userland/DevTools/HackStudio/CMakeLists.txt index a4e8040c19f..062f37c36b1 100644 --- a/Userland/DevTools/HackStudio/CMakeLists.txt +++ b/Userland/DevTools/HackStudio/CMakeLists.txt @@ -49,5 +49,5 @@ set(SOURCES ) serenity_app(HackStudio ICON app-hack-studio) -target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibCpp LibGfx LibCore LibVT LibDebug LibX86 LibDiff LibShell LibRegex) +target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibCpp LibGfx LibCore LibVT LibDebug LibX86 LibDiff LibShell LibSymbolication LibRegex) add_dependencies(HackStudio CppLanguageServer) diff --git a/Userland/DevTools/HackStudio/Debugger/DisassemblyModel.cpp b/Userland/DevTools/HackStudio/Debugger/DisassemblyModel.cpp index da6ae10f78c..d69b3664e93 100644 --- a/Userland/DevTools/HackStudio/Debugger/DisassemblyModel.cpp +++ b/Userland/DevTools/HackStudio/Debugger/DisassemblyModel.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -30,14 +31,9 @@ DisassemblyModel::DisassemblyModel(const Debug::DebugSession& debug_session, con OwnPtr kernel_elf; const ELF::Image* elf = nullptr; - // FIXME: Use /proc for this -#if ARCH(I386) - FlatPtr kernel_base = 0xc0000000; -#else - FlatPtr kernel_base = 0x2000000000; -#endif + auto maybe_kernel_base = Symbolication::kernel_base(); - if (containing_function.value().address_low >= kernel_base) { + if (maybe_kernel_base.has_value() && containing_function.value().address_low >= maybe_kernel_base.value()) { auto file_or_error = MappedFile::map("/boot/Kernel.debug"); if (file_or_error.is_error()) return; diff --git a/Userland/DevTools/Profiler/CMakeLists.txt b/Userland/DevTools/Profiler/CMakeLists.txt index 3b17cc360d4..f08eebaa153 100644 --- a/Userland/DevTools/Profiler/CMakeLists.txt +++ b/Userland/DevTools/Profiler/CMakeLists.txt @@ -19,4 +19,4 @@ set(SOURCES ) serenity_app(Profiler ICON app-profiler) -target_link_libraries(Profiler LibGUI LibDesktop LibX86) +target_link_libraries(Profiler LibGUI LibDesktop LibX86 LibSymbolication) diff --git a/Userland/DevTools/Profiler/DisassemblyModel.cpp b/Userland/DevTools/Profiler/DisassemblyModel.cpp index fdbdf1656af..f52241bb34d 100644 --- a/Userland/DevTools/Profiler/DisassemblyModel.cpp +++ b/Userland/DevTools/Profiler/DisassemblyModel.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -40,13 +41,8 @@ DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node) OwnPtr kernel_elf; const ELF::Image* elf; FlatPtr base_address = 0; - // FIXME: Use /proc for this -#if ARCH(I386) - FlatPtr kernel_base = 0xc0000000; -#else - FlatPtr kernel_base = 0x2000000000; -#endif - if (m_node.address() >= kernel_base) { + auto maybe_kernel_base = Symbolication::kernel_base(); + if (maybe_kernel_base.has_value() && m_node.address() >= maybe_kernel_base.value()) { if (!m_kernel_file) { auto file_or_error = MappedFile::map("/boot/Kernel.debug"); if (file_or_error.is_error()) diff --git a/Userland/DevTools/Profiler/Profile.cpp b/Userland/DevTools/Profiler/Profile.cpp index 9bcb63a020d..f4a7f630f48 100644 --- a/Userland/DevTools/Profiler/Profile.cpp +++ b/Userland/DevTools/Profiler/Profile.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include namespace Profiler { @@ -301,12 +302,7 @@ Result, String> Profile::load_from_perfcore_file(const St continue; } - // FIXME: Use /proc for this -#if ARCH(I386) - FlatPtr kernel_base = 0xc0000000; -#else - FlatPtr kernel_base = 0x2000000000; -#endif + auto maybe_kernel_base = Symbolication::kernel_base(); auto* stack = perf_event.get_ptr("stack"); VERIFY(stack); @@ -318,7 +314,7 @@ Result, String> Profile::load_from_perfcore_file(const St FlyString object_name; String symbol; - if (ptr >= kernel_base) { + if (maybe_kernel_base.has_value() && ptr >= maybe_kernel_base.value()) { if (kernel_elf) { symbol = kernel_elf->symbolicate(ptr, &offset); } else { @@ -345,7 +341,7 @@ Result, String> Profile::load_from_perfcore_file(const St continue; FlatPtr innermost_frame_address = event.frames.at(1).address; - event.in_kernel = innermost_frame_address >= kernel_base; + event.in_kernel = maybe_kernel_base.has_value() && innermost_frame_address >= maybe_kernel_base.value(); events.append(move(event)); } diff --git a/Userland/DevTools/Profiler/ProfileModel.cpp b/Userland/DevTools/Profiler/ProfileModel.cpp index eb0d311d75a..fdb8c1dd9b5 100644 --- a/Userland/DevTools/Profiler/ProfileModel.cpp +++ b/Userland/DevTools/Profiler/ProfileModel.cpp @@ -8,6 +8,7 @@ #include "Profile.h" #include #include +#include #include #include @@ -105,13 +106,8 @@ GUI::Variant ProfileModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol if (node->is_root()) { return GUI::FileIconProvider::icon_for_executable(node->process().executable); } - // FIXME: Use /proc for this -#if ARCH(I386) - FlatPtr kernel_base = 0xc0000000; -#else - FlatPtr kernel_base = 0x2000000000; -#endif - if (node->address() >= kernel_base) + auto maybe_kernel_base = Symbolication::kernel_base(); + if (maybe_kernel_base.has_value() && node->address() >= maybe_kernel_base.value()) return m_kernel_frame_icon; return m_user_frame_icon; } diff --git a/Userland/Libraries/LibSymbolication/Symbolication.cpp b/Userland/Libraries/LibSymbolication/Symbolication.cpp index 40983c1988e..4199790ecc9 100644 --- a/Userland/Libraries/LibSymbolication/Symbolication.cpp +++ b/Userland/Libraries/LibSymbolication/Symbolication.cpp @@ -21,6 +21,42 @@ struct CachedELF { static HashMap> s_cache; +enum class KernelBaseState { + Uninitialized, + Valid, + Invalid, +}; + +static FlatPtr s_kernel_base; +static KernelBaseState s_kernel_base_state = KernelBaseState::Uninitialized; + +Optional kernel_base() +{ + if (s_kernel_base_state == KernelBaseState::Uninitialized) { + auto file = Core::File::open("/proc/kernel_base", Core::OpenMode::ReadOnly); + if (file.is_error()) { + s_kernel_base_state = KernelBaseState::Invalid; + return {}; + } + auto kernel_base_str = String { file.value()->read_all(), NoChomp }; +#if ARCH(I386) + using AddressType = u32; +#else + using AddressType = u64; +#endif + auto maybe_kernel_base = kernel_base_str.to_uint(); + if (!maybe_kernel_base.has_value()) { + s_kernel_base_state = KernelBaseState::Invalid; + return {}; + } + s_kernel_base = maybe_kernel_base.value(); + s_kernel_base_state = KernelBaseState::Valid; + } + if (s_kernel_base_state == KernelBaseState::Invalid) + return {}; + return s_kernel_base; +} + Optional symbolicate(String const& path, FlatPtr address) { if (!s_cache.contains(path)) { @@ -81,16 +117,14 @@ Vector symbolicate_thread(pid_t pid, pid_t tid) Vector stack; Vector regions; - regions.append(RegionWithSymbols { - // FIXME: Use /proc for this -#if ARCH(I386) - .base = 0xc0000000, -#else - .base = 0x2000000000, -#endif - .size = 0x3fffffff, - .path = "/boot/Kernel.debug", - .is_relative = false }); + if (auto maybe_kernel_base = kernel_base(); maybe_kernel_base.has_value()) { + regions.append(RegionWithSymbols { + .base = maybe_kernel_base.value(), + .size = 0x3fffffff, + .path = "/boot/Kernel.debug", + .is_relative = false, + }); + } { auto stack_path = String::formatted("/proc/{}/stacks/{}", pid, tid); diff --git a/Userland/Libraries/LibSymbolication/Symbolication.h b/Userland/Libraries/LibSymbolication/Symbolication.h index 346dedd3a27..a5a128ecad0 100644 --- a/Userland/Libraries/LibSymbolication/Symbolication.h +++ b/Userland/Libraries/LibSymbolication/Symbolication.h @@ -18,6 +18,7 @@ struct Symbol { Vector source_positions; }; +Optional kernel_base(); Vector symbolicate_thread(pid_t pid, pid_t tid); Optional symbolicate(String const& path, FlatPtr address); diff --git a/Userland/Utilities/bt.cpp b/Userland/Utilities/bt.cpp index 1963547f7c6..b598343c951 100644 --- a/Userland/Utilities/bt.cpp +++ b/Userland/Utilities/bt.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -44,13 +45,8 @@ int main(int argc, char** argv) auto frame_number = symbols.size() - 1; for (auto& symbol : symbols) { // Make kernel stack frames stand out. - // FIXME: Use /proc for this -#if ARCH(I386) - FlatPtr kernel_base = 0xc0000000; -#else - FlatPtr kernel_base = 0x2000000000; -#endif - int color = symbol.address < kernel_base ? 35 : 31; + auto maybe_kernel_base = Symbolication::kernel_base(); + int color = maybe_kernel_base.has_value() && symbol.address < maybe_kernel_base.value() ? 35 : 31; out("{:3}: \033[{};1m{:p}\033[0m | ", frame_number, color, symbol.address); if (!symbol.name.is_empty()) out("{} ", symbol.name);