From 37bb0d0fb30a3548173253b0f19861a7ee4f8fce Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Mon, 20 Jun 2016 14:39:49 +0100 Subject: kern, arm: Dump dmesg on kernel panic/oops Add helper functions to dump the guest kernel's dmesg buffer to a text file in m5out. This functionality is split into two parts. First, a dmesg dump function that can be used in other places: void Linux::dumpDmesg(ThreadContext *, std::ostream &) This function is used to implement two PCEvents: DmesgDumpEvent and KernelPanic event. The only difference between the two is that the latter produces a gem5 panic instead of a warning in addition to dumping the kernel log. Change-Id: I6d2af1d666ace57124089648ea906f6c787ac63c Signed-off-by: Andreas Sandberg Reviewed-by: Nikos Nikoleris Reviewed-by: Gabor Dozsa --- src/kern/SConscript | 1 + src/kern/linux/events.cc | 33 ++++++++++- src/kern/linux/events.hh | 52 ++++++++++++++++ src/kern/linux/helpers.cc | 148 ++++++++++++++++++++++++++++++++++++++++++++++ src/kern/linux/helpers.hh | 59 ++++++++++++++++++ 5 files changed, 291 insertions(+), 2 deletions(-) create mode 100644 src/kern/linux/helpers.cc create mode 100644 src/kern/linux/helpers.hh (limited to 'src/kern') diff --git a/src/kern/SConscript b/src/kern/SConscript index 7bcf54213..305cf6381 100644 --- a/src/kern/SConscript +++ b/src/kern/SConscript @@ -36,6 +36,7 @@ if env['TARGET_ISA'] == 'null': Source('kernel_stats.cc') Source('linux/events.cc') Source('linux/linux.cc') +Source('linux/helpers.cc') Source('linux/printk.cc') Source('freebsd/events.cc') Source('operatingsystem.cc') diff --git a/src/kern/linux/events.cc b/src/kern/linux/events.cc index 42f058a72..f4e694436 100644 --- a/src/kern/linux/events.cc +++ b/src/kern/linux/events.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 ARM Limited + * Copyright (c) 2011, 2016 ARM Limited * All rights reserved * * The license below extends only to copyright in the software and shall @@ -41,13 +41,17 @@ * Ali Saidi */ +#include "kern/linux/events.hh" + #include #include "arch/utility.hh" +#include "base/output.hh" #include "base/trace.hh" +#include "cpu/base.hh" #include "cpu/thread_context.hh" #include "debug/DebugPrintf.hh" -#include "kern/linux/events.hh" +#include "kern/linux/helpers.hh" #include "kern/linux/printk.hh" #include "kern/system_events.hh" #include "sim/arguments.hh" @@ -94,5 +98,30 @@ UDelayEvent::process(ThreadContext *tc) } } +void +DmesgDumpEvent::process(ThreadContext *tc) +{ + StringWrap name(tc->getCpuPtr()->name() + ".dmesg_dump_event"); + + inform("Dumping kernel dmesg buffer to %s...\n", fname); + OutputStream *os = simout.create(fname); + dumpDmesg(tc, *os->stream()); + simout.close(os); + + warn(descr()); +} + +void +KernelPanicEvent::process(ThreadContext *tc) +{ + StringWrap name(tc->getCpuPtr()->name() + ".dmesg_dump_event"); + + inform("Dumping kernel dmesg buffer to %s...\n", fname); + OutputStream *os = simout.create(fname); + dumpDmesg(tc, *os->stream()); + simout.close(os); + + panic(descr()); +} } // namespace linux diff --git a/src/kern/linux/events.hh b/src/kern/linux/events.hh index 3f5f2526f..b8aad9d79 100644 --- a/src/kern/linux/events.hh +++ b/src/kern/linux/events.hh @@ -1,4 +1,16 @@ /* + * Copyright (c) 2016 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright (c) 2004-2006 The Regents of The University of Michigan * All rights reserved. * @@ -44,6 +56,46 @@ class DebugPrintkEvent : public SkipFuncEvent virtual void process(ThreadContext *xc); }; +/** + * Dump the guest kernel's dmesg buffer to a file in gem5's output + * directory and print a warning. + * + * @warn This event uses Linux::dumpDmesg() and comes with the same + * limitations. Most importantly, the kernel's address mappings must + * be available to the translating proxy. + */ +class DmesgDumpEvent : public PCEvent +{ + protected: + std::string fname; + + public: + DmesgDumpEvent(PCEventQueue *q, const std::string &desc, Addr addr, + const std::string &_fname) + : PCEvent(q, desc, addr), fname(_fname) {} + virtual void process(ThreadContext *xc); +}; + +/** + * Dump the guest kernel's dmesg buffer to a file in gem5's output + * directory and panic. + * + * @warn This event uses Linux::dumpDmesg() and comes with the same + * limitations. Most importantly, the kernel's address mappings must + * be available to the translating proxy. + */ +class KernelPanicEvent : public PCEvent +{ + protected: + std::string fname; + + public: + KernelPanicEvent(PCEventQueue *q, const std::string &desc, Addr addr, + const std::string &_fname) + : PCEvent(q, desc, addr), fname(_fname) {} + virtual void process(ThreadContext *xc); +}; + /** A class to skip udelay() and related calls in the kernel. * This class has two additional parameters that take the argument to udelay and * manipulated it to come up with ns and eventually ticks to quiesce for. diff --git a/src/kern/linux/helpers.cc b/src/kern/linux/helpers.cc new file mode 100644 index 000000000..54fc42138 --- /dev/null +++ b/src/kern/linux/helpers.cc @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2016 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Andreas Sandberg + */ + +#include "kern/linux/helpers.hh" + +#include "arch/isa_traits.hh" +#include "config/the_isa.hh" +#include "cpu/thread_context.hh" +#include "mem/fs_translating_port_proxy.hh" +#include "sim/system.hh" + +struct DmesgEntry { + uint64_t ts_nsec; + uint16_t len; + uint16_t text_len; + uint16_t dict_len; + uint8_t facility; + uint8_t flags; +} M5_ATTR_PACKED; + +static int +dumpDmesgEntry(const uint8_t *base, const uint8_t *end, std::ostream &os) +{ + const size_t max_length = end - base; + DmesgEntry de; + + if (max_length < sizeof(de)) { + warn("Malformed dmesg entry\n"); + return -1; + } + + memcpy(&de, base, sizeof(de)); + de.ts_nsec = TheISA::gtoh(de.ts_nsec); + de.len = TheISA::gtoh(de.len); + de.text_len = TheISA::gtoh(de.text_len); + + if (de.len < sizeof(de) || + max_length < de.len || + max_length < sizeof(DmesgEntry) + de.text_len) { + + warn("Malformed dmesg entry:\n"); + warn("\tMax length: %i\n", max_length); + warn("\tde.len: %i\n", de.len); + warn("\tde.text_len: %i\n", de.text_len); + return -1; + } + + ccprintf(os, "[%.6f] ", de.ts_nsec * 10e-9); + os.write((char *)base + sizeof(de), de.text_len); + os << std::endl; + + return de.len; +} + +void +Linux::dumpDmesg(ThreadContext *tc, std::ostream &os) +{ + System *system = tc->getSystemPtr(); + const SymbolTable *symtab = system->kernelSymtab; + FSTranslatingPortProxy proxy(tc); + + Addr addr_lb = 0, addr_lb_len = 0, addr_first = 0, addr_next = 0; + const bool found_symbols = + symtab->findAddress("__log_buf", addr_lb) && + symtab->findAddress("log_buf_len", addr_lb_len) && + symtab->findAddress("log_first_idx", addr_first) && + symtab->findAddress("log_next_idx", addr_next); + + if (!found_symbols) { + warn("Failed to find kernel dmesg symbols.\n"); + return; + } + + uint32_t log_buf_len = proxy.readGtoH(addr_lb_len); + uint32_t log_first_idx = proxy.readGtoH(addr_first); + uint32_t log_next_idx = proxy.readGtoH(addr_next); + + if (log_first_idx >= log_buf_len || log_next_idx >= log_buf_len) { + warn("dmesg pointers/length corrupted\n"); + return; + } + + // Normalize and read the dmesg ring buffer + std::vector log_buf(log_buf_len); + int length; + if (log_first_idx < log_next_idx) { + length = log_next_idx - log_first_idx; + if (length < 0 || length > log_buf.size()) { + warn("Unexpected dmesg buffer length\n"); + return; + } + proxy.readBlob(addr_lb + log_first_idx, log_buf.data(), length); + } else { + const int length_2 = log_buf_len - log_first_idx; + if (length_2 < 0 || length_2 + log_next_idx > log_buf.size()) { + warn("Unexpected dmesg buffer length\n"); + return; + } + length = log_buf_len; + proxy.readBlob(addr_lb + log_first_idx, log_buf.data(), length_2); + proxy.readBlob(addr_lb, log_buf.data() + length_2, log_next_idx); + } + + // Print dmesg buffer content + const uint8_t *cur = log_buf.data(), *end = log_buf.data() + length; + while (cur < end) { + int ret = dumpDmesgEntry(cur, end, os); + if (ret < 0) + return; + cur += ret; + } +} diff --git a/src/kern/linux/helpers.hh b/src/kern/linux/helpers.hh new file mode 100644 index 000000000..cebda7da1 --- /dev/null +++ b/src/kern/linux/helpers.hh @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Andreas Sandberg + */ + +#ifndef __KERN_LINUX_HELPERS_HH__ +#define __KERN_LINUX_HELPERS_HH__ + +#include + +class ThreadContext; + +namespace Linux { + +/** + * Dump Linux's dmesg log buffer to the an output stream. + * + * @warn This assumes that the kernel address mappings are available + * to the translating proxy. + */ +void dumpDmesg(ThreadContext *tc, std::ostream &os); + +} // namespace Linux + +#endif // __KERN_LINUX_HELPERS_HH__ -- cgit v1.2.3