diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-27 00:14:24 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-27 00:14:24 +0200 |
commit | 2716a9e2d7ceb92e66ed486b4d6797caf8be4efd (patch) | |
tree | 63ad0a1966118432fe3146814ef7a530d8922af0 /Kernel | |
parent | c928b062180b10a9908325727e18483e029edada (diff) | |
download | serenity-2716a9e2d7ceb92e66ed486b4d6797caf8be4efd.zip |
Greatly improve /proc/PID/stack by tracing the ebp frame chain.
I also added a generator cache to FileHandle. This way, multiple
reads to a generated file (i.e in a synthfs) can transparently
handle multiple calls to read() without the contents changing
between calls.
The cache is discarded at EOF (or when the FileHandle is destroyed.)
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/ProcFileSystem.cpp | 211 | ||||
-rw-r--r-- | Kernel/StdLib.h | 10 | ||||
-rw-r--r-- | Kernel/Task.cpp | 20 | ||||
-rw-r--r-- | Kernel/Task.h | 4 | ||||
-rw-r--r-- | Kernel/_fs_contents | bin | 1024000 -> 1024000 bytes | |||
-rw-r--r-- | Kernel/i386.cpp | 6 | ||||
-rw-r--r-- | Kernel/init.cpp | 11 | ||||
-rw-r--r-- | Kernel/kmalloc.cpp | 5 | ||||
-rw-r--r-- | Kernel/kmalloc.h | 2 | ||||
-rw-r--r-- | Kernel/system.h | 1 | ||||
-rw-r--r-- | Kernel/types.h | 4 |
11 files changed, 173 insertions, 101 deletions
diff --git a/Kernel/ProcFileSystem.cpp b/Kernel/ProcFileSystem.cpp index 9560faad69..a763549e11 100644 --- a/Kernel/ProcFileSystem.cpp +++ b/Kernel/ProcFileSystem.cpp @@ -2,6 +2,7 @@ #include "Task.h" #include <VirtualFileSystem/VirtualFileSystem.h> #include "system.h" +#include "MemoryManager.h" static ProcFileSystem* s_the; @@ -25,6 +26,64 @@ ProcFileSystem::~ProcFileSystem() { } +ByteBuffer procfs$pid_vm(const Task& task) +{ + InterruptDisabler disabler; + char* buffer; + auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer); + memset(buffer, 0, stringImpl->length()); + char* ptr = buffer; + ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n"); + for (auto& region : task.regions()) { + ptr += ksprintf(ptr, "%x -- %x %x %s\n", + region->linearAddress.get(), + region->linearAddress.offset(region->size - 1).get(), + region->size, + region->name.characters()); + } + *ptr = '\0'; + return ByteBuffer::copy((byte*)buffer, ptr - buffer); +} + +ByteBuffer procfs$pid_stack(Task& task) +{ + InterruptDisabler disabler; + if (current != &task) { + MemoryManager::the().unmapRegionsForTask(*current); + MemoryManager::the().mapRegionsForTask(task); + } + struct RecognizedSymbol { + dword address; + const KSym* ksym; + }; + Vector<RecognizedSymbol> recognizedSymbols; + if (auto* eipKsym = ksymbolicate(task.tss().eip)) + recognizedSymbols.append({ task.tss().eip, eipKsym }); + for (dword* stackPtr = (dword*)task.framePtr(); task.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) { + dword retaddr = stackPtr[1]; + if (auto* ksym = ksymbolicate(retaddr)) + recognizedSymbols.append({ retaddr, ksym }); + } + size_t bytesNeeded = 0; + for (auto& symbol : recognizedSymbols) { + bytesNeeded += symbol.ksym->name.length() + 8 + 16; + } + auto buffer = ByteBuffer::createUninitialized(bytesNeeded); + char* bufptr = (char*)buffer.pointer(); + + for (auto& symbol : recognizedSymbols) { + // FIXME: This doesn't actually create a file! + unsigned offset = symbol.address - symbol.ksym->address; + bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset); + } + buffer.trim(bufptr - (char*)buffer.pointer()); + if (current != &task) { + MemoryManager::the().unmapRegionsForTask(task); + MemoryManager::the().mapRegionsForTask(*current); + } + return buffer; +} + void ProcFileSystem::addProcess(Task& task) { ASSERT_INTERRUPTS_DISABLED(); @@ -32,58 +91,8 @@ void ProcFileSystem::addProcess(Task& task) ksprintf(buf, "%d", task.pid()); auto dir = addFile(createDirectory(buf)); m_pid2inode.set(task.pid(), dir.index()); - addFile(createGeneratedFile("vm", [&task] { - InterruptDisabler disabler; - char* buffer; - auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer); - memset(buffer, 0, stringImpl->length()); - char* ptr = buffer; - ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n"); - for (auto& region : task.regions()) { - ptr += ksprintf(ptr, "%x -- %x %x %s\n", - region->linearAddress.get(), - region->linearAddress.offset(region->size - 1).get(), - region->size, - region->name.characters()); - } - *ptr = '\0'; - return ByteBuffer::copy((byte*)buffer, ptr - buffer); - }), dir.index()); - addFile(createGeneratedFile("stack", [&task] { - InterruptDisabler disabler; - auto& syms = ksyms(); - dword firstKsymAddress = syms.first().address; - dword lastKsymAddress = syms.last().address; - struct RecognizedSymbol { - dword address; - const char* name; - dword offset; - }; - Vector<RecognizedSymbol> recognizedSymbols; - size_t bytesNeeded = 0; - for (dword* stackPtr = (dword*)task.stackPtr(); (dword)stackPtr < task.stackTop(); ++stackPtr) { - if (*stackPtr < firstKsymAddress || *stackPtr > lastKsymAddress) - continue; - const char* name = nullptr; - unsigned offset = 0; - for (unsigned i = 0; i < syms.size(); ++i) { - if (*stackPtr < syms[i+1].address) { - name = syms[i].name.characters(); - offset = *stackPtr - syms[i].address; - bytesNeeded += syms[i].name.length() + 8 + 16; - break; - } - } - recognizedSymbols.append({ *stackPtr, name, offset }); - } - auto buffer = ByteBuffer::createUninitialized(bytesNeeded); - char* ptr = (char*)buffer.pointer(); - for (auto& symbol : recognizedSymbols) { - kprintf("%p %s +%u\n", symbol.address, symbol.name, symbol.offset); - } - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - }), dir.index()); + addFile(createGeneratedFile("vm", [&task] { return procfs$pid_vm(task); }), dir.index()); + addFile(createGeneratedFile("stack", [&task] { return procfs$pid_stack(task); }), dir.index()); } void ProcFileSystem::removeProcess(Task& task) @@ -97,56 +106,62 @@ void ProcFileSystem::removeProcess(Task& task) m_pid2inode.remove(pid); } -bool ProcFileSystem::initialize() +ByteBuffer procfs$mounts() { - SyntheticFileSystem::initialize(); + InterruptDisabler disabler; + auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80); + char* ptr = (char*)buffer.pointer(); + VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) { + auto& fs = mount.fileSystem(); + ptr += ksprintf(ptr, "%s @ ", fs.className()); + if (!mount.host().isValid()) + ptr += ksprintf(ptr, "/\n", fs.className()); + else + ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index()); + }); + buffer.trim(ptr - (char*)buffer.pointer()); + return buffer; +} - addFile(createGeneratedFile("mounts", [] { - InterruptDisabler disabler; - auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80); - char* ptr = (char*)buffer.pointer(); - VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) { - auto& fs = mount.fileSystem(); - ptr += ksprintf(ptr, "%s @ ", fs.className()); - if (!mount.host().isValid()) - ptr += ksprintf(ptr, "/\n", fs.className()); - else - ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index()); - }); - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - })); +ByteBuffer procfs$kmalloc() +{ + InterruptDisabler disabler; + auto buffer = ByteBuffer::createUninitialized(128); + char* ptr = (char*)buffer.pointer(); + ptr += ksprintf(ptr, "alloc: %u\nfree: %u\n", sum_alloc, sum_free); + buffer.trim(ptr - (char*)buffer.pointer()); + return buffer; +} - addFile(createGeneratedFile("kmalloc", [] { - InterruptDisabler disabler; - auto buffer = ByteBuffer::createUninitialized(128); - char* ptr = (char*)buffer.pointer(); - ptr += ksprintf(ptr, "alloc: %u\nfree: %u\n", sum_alloc, sum_free); - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - })); +ByteBuffer procfs$summary() +{ + InterruptDisabler disabler; + auto tasks = Task::allTasks(); + auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256); + char* ptr = (char*)buffer.pointer(); + ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n"); + for (auto* task : tasks) { + ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n", + task->pid(), + task->uid(), + task->gid(), + task->state(), + task->parentPID(), + task->timesScheduled(), + task->fileHandleCount(), + task->name().characters()); + } + *ptr = '\0'; + buffer.trim(ptr - (char*)buffer.pointer()); + return buffer; +} - addFile(createGeneratedFile("summary", [] { - InterruptDisabler disabler; - auto tasks = Task::allTasks(); - auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256); - char* ptr = (char*)buffer.pointer(); - ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n"); - for (auto* task : tasks) { - ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n", - task->pid(), - task->uid(), - task->gid(), - task->state(), - task->parentPID(), - task->timesScheduled(), - task->fileHandleCount(), - task->name().characters()); - } - *ptr = '\0'; - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - })); +bool ProcFileSystem::initialize() +{ + SyntheticFileSystem::initialize(); + addFile(createGeneratedFile("mounts", procfs$mounts)); + addFile(createGeneratedFile("kmalloc", procfs$kmalloc)); + addFile(createGeneratedFile("summary", procfs$summary)); return true; } diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h index f507773e55..ee7cd18c33 100644 --- a/Kernel/StdLib.h +++ b/Kernel/StdLib.h @@ -2,7 +2,17 @@ #include "types.h" +#if 0 +inline void memcpy(void *dest, const void *src, DWORD n) +{ + BYTE* bdest = (BYTE*)dest; + const BYTE* bsrc = (const BYTE*)src; + for (; n; --n) + *(bdest++) = *(bsrc++); +} +#else void memcpy(void*, const void*, DWORD); +#endif void strcpy(char*, const char*); int strcmp(char const*, const char*); DWORD strlen(const char*); diff --git a/Kernel/Task.cpp b/Kernel/Task.cpp index 79fd9d7c37..91fffcd15b 100644 --- a/Kernel/Task.cpp +++ b/Kernel/Task.cpp @@ -885,3 +885,23 @@ Task::Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n) Task::Region::~Region() { } + +bool Task::isValidAddressForKernel(LinearAddress laddr) const +{ + InterruptDisabler disabler; + if (laddr.get() >= ksyms().first().address && laddr.get() <= ksyms().last().address) + return true; + if (is_kmalloc_address((void*)laddr.get())) + return true; + return isValidAddressForUser(laddr); +} + +bool Task::isValidAddressForUser(LinearAddress laddr) const +{ + InterruptDisabler disabler; + for (auto& region: m_regions) { + if (laddr >= region->linearAddress && laddr < region->linearAddress.offset(region->size)) + return true; + } + return false; +} diff --git a/Kernel/Task.h b/Kernel/Task.h index dd06be7022..957b28699e 100644 --- a/Kernel/Task.h +++ b/Kernel/Task.h @@ -122,9 +122,13 @@ public: size_t fileHandleCount() const { return m_fileHandles.size(); } + dword framePtr() const { return m_tss.ebp; } dword stackPtr() const { return m_tss.esp; } dword stackTop() const { return m_tss.ss == 0x10 ? m_stackTop0 : m_stackTop3; } + bool isValidAddressForKernel(LinearAddress) const; + bool isValidAddressForUser(LinearAddress) const; + private: friend class MemoryManager; friend bool scheduleNewTask(); diff --git a/Kernel/_fs_contents b/Kernel/_fs_contents Binary files differindex cff5e6f491..5971aa3cec 100644 --- a/Kernel/_fs_contents +++ b/Kernel/_fs_contents diff --git a/Kernel/i386.cpp b/Kernel/i386.cpp index 047212f877..7bca2f0b93 100644 --- a/Kernel/i386.cpp +++ b/Kernel/i386.cpp @@ -196,10 +196,10 @@ void exception_14_handler() asm ("movl %%cr2, %%eax":"=a"(faultAddress)); auto& regs = *reinterpret_cast<RegisterDump*>(exception_state_dump); - kprintf("%s page fault: %u(%s), %s laddr=%p\n", - current->isRing0() ? "Kernel" : "User", - current->pid(), + kprintf("Ring%u page fault in %s(%u), %s laddr=%p\n", + regs.cs & 3, current->name().characters(), + current->pid(), exception_code & 2 ? "write" : "read", faultAddress); diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 8368e071c9..7e220a6ced 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -65,6 +65,17 @@ Vector<KSym>& ksyms() return *s_ksyms; } +const KSym* ksymbolicate(dword address) +{ + if (address < ksyms().first().address || address > ksyms().last().address) + return nullptr; + for (unsigned i = 0; i < ksyms().size(); ++i) { + if (address < ksyms()[i + 1].address) + return &ksyms()[i]; + } + return nullptr; +} + static void loadKernelMap(const ByteBuffer& buffer) { s_ksyms = new Vector<KSym>; diff --git a/Kernel/kmalloc.cpp b/Kernel/kmalloc.cpp index 082307ac3b..fbb56569c6 100644 --- a/Kernel/kmalloc.cpp +++ b/Kernel/kmalloc.cpp @@ -29,6 +29,11 @@ PRIVATE BYTE alloc_map[POOL_SIZE / CHUNK_SIZE / 8]; volatile DWORD sum_alloc = 0; volatile DWORD sum_free = POOL_SIZE; +bool is_kmalloc_address(void* ptr) +{ + return ptr >= (void*)BASE_PHYS && ptr <= ((void*)BASE_PHYS + POOL_SIZE); +} + PUBLIC void kmalloc_init() { diff --git a/Kernel/kmalloc.h b/Kernel/kmalloc.h index 3862b68aef..e0b5ac4955 100644 --- a/Kernel/kmalloc.h +++ b/Kernel/kmalloc.h @@ -4,6 +4,8 @@ void kmalloc_init(); void *kmalloc(DWORD size) __attribute__ ((malloc)); void kfree(void*); +bool is_kmalloc_address(void*); + extern volatile DWORD sum_alloc; extern volatile DWORD sum_free; diff --git a/Kernel/system.h b/Kernel/system.h index 03de208bbb..0839f28d45 100644 --- a/Kernel/system.h +++ b/Kernel/system.h @@ -10,6 +10,7 @@ struct KSym { }; Vector<KSym>& ksyms() PURE; +const KSym* ksymbolicate(dword address) PURE; struct system_t { diff --git a/Kernel/types.h b/Kernel/types.h index 63f2a22846..c7e135fbfb 100644 --- a/Kernel/types.h +++ b/Kernel/types.h @@ -74,6 +74,10 @@ public: void set(dword address) { m_address = address; } void mask(dword m) { m_address &= m; } + bool operator<=(const LinearAddress& other) const { return m_address <= other.m_address; } + bool operator>=(const LinearAddress& other) const { return m_address >= other.m_address; } + bool operator>(const LinearAddress& other) const { return m_address > other.m_address; } + bool operator<(const LinearAddress& other) const { return m_address < other.m_address; } bool operator==(const LinearAddress& other) const { return m_address == other.m_address; } byte* asPtr() { return reinterpret_cast<byte*>(m_address); } |