helpers.cc revision 13893
113481Sgiacomo.travaglini@arm.com/*
213481Sgiacomo.travaglini@arm.com * Copyright (c) 2016 ARM Limited
313481Sgiacomo.travaglini@arm.com * All rights reserved
413481Sgiacomo.travaglini@arm.com *
513481Sgiacomo.travaglini@arm.com * The license below extends only to copyright in the software and shall
613481Sgiacomo.travaglini@arm.com * not be construed as granting a license to any other intellectual
713481Sgiacomo.travaglini@arm.com * property including but not limited to intellectual property relating
813481Sgiacomo.travaglini@arm.com * to a hardware implementation of the functionality of the software
913481Sgiacomo.travaglini@arm.com * licensed hereunder.  You may use the software subject to the license
1013481Sgiacomo.travaglini@arm.com * terms below provided that you ensure that this notice is replicated
1113481Sgiacomo.travaglini@arm.com * unmodified and in its entirety in all distributions of the software,
1213481Sgiacomo.travaglini@arm.com * modified or unmodified, in source code or in binary form.
1313481Sgiacomo.travaglini@arm.com *
1413481Sgiacomo.travaglini@arm.com * Redistribution and use in source and binary forms, with or without
1513481Sgiacomo.travaglini@arm.com * modification, are permitted provided that the following conditions are
1613481Sgiacomo.travaglini@arm.com * met: redistributions of source code must retain the above copyright
1713481Sgiacomo.travaglini@arm.com * notice, this list of conditions and the following disclaimer;
1813481Sgiacomo.travaglini@arm.com * redistributions in binary form must reproduce the above copyright
1913481Sgiacomo.travaglini@arm.com * notice, this list of conditions and the following disclaimer in the
2013481Sgiacomo.travaglini@arm.com * documentation and/or other materials provided with the distribution;
2113481Sgiacomo.travaglini@arm.com * neither the name of the copyright holders nor the names of its
2213481Sgiacomo.travaglini@arm.com * contributors may be used to endorse or promote products derived from
2313481Sgiacomo.travaglini@arm.com * this software without specific prior written permission.
2413481Sgiacomo.travaglini@arm.com *
2513481Sgiacomo.travaglini@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2613481Sgiacomo.travaglini@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2713481Sgiacomo.travaglini@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2813481Sgiacomo.travaglini@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2913481Sgiacomo.travaglini@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3013481Sgiacomo.travaglini@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3113481Sgiacomo.travaglini@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3213481Sgiacomo.travaglini@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3313481Sgiacomo.travaglini@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3413481Sgiacomo.travaglini@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3513481Sgiacomo.travaglini@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3613481Sgiacomo.travaglini@arm.com *
3713481Sgiacomo.travaglini@arm.com * Authors: Andreas Sandberg
3813481Sgiacomo.travaglini@arm.com */
3913481Sgiacomo.travaglini@arm.com
4013481Sgiacomo.travaglini@arm.com#include "kern/linux/helpers.hh"
4113481Sgiacomo.travaglini@arm.com
4213481Sgiacomo.travaglini@arm.com#include "arch/isa_traits.hh"
4313481Sgiacomo.travaglini@arm.com#include "config/the_isa.hh"
4413481Sgiacomo.travaglini@arm.com#include "cpu/thread_context.hh"
4513481Sgiacomo.travaglini@arm.com#include "mem/fs_translating_port_proxy.hh"
4613481Sgiacomo.travaglini@arm.com#include "sim/byteswap.hh"
4713481Sgiacomo.travaglini@arm.com#include "sim/system.hh"
4813481Sgiacomo.travaglini@arm.com
4913481Sgiacomo.travaglini@arm.comstruct DmesgEntry {
5013481Sgiacomo.travaglini@arm.com    uint64_t ts_nsec;
5113481Sgiacomo.travaglini@arm.com    uint16_t len;
5213481Sgiacomo.travaglini@arm.com    uint16_t text_len;
5313481Sgiacomo.travaglini@arm.com    uint16_t dict_len;
5413481Sgiacomo.travaglini@arm.com    uint8_t facility;
5513481Sgiacomo.travaglini@arm.com    uint8_t flags;
5613481Sgiacomo.travaglini@arm.com} M5_ATTR_PACKED;
5713481Sgiacomo.travaglini@arm.com
5813481Sgiacomo.travaglini@arm.comstatic int
5913481Sgiacomo.travaglini@arm.comdumpDmesgEntry(const uint8_t *base, const uint8_t *end, std::ostream &os)
60{
61    const size_t max_length = end - base;
62    DmesgEntry de;
63
64    if (max_length < sizeof(de)) {
65        warn("Malformed dmesg entry\n");
66        return -1;
67    }
68
69    memcpy(&de, base, sizeof(de));
70    de.ts_nsec = TheISA::gtoh(de.ts_nsec);
71    de.len = TheISA::gtoh(de.len);
72    de.text_len = TheISA::gtoh(de.text_len);
73
74    if (de.len < sizeof(de) ||
75        max_length < de.len ||
76        max_length < sizeof(DmesgEntry) + de.text_len) {
77
78        warn("Malformed dmesg entry:\n");
79        warn("\tMax length: %i\n", max_length);
80        warn("\tde.len: %i\n", de.len);
81        warn("\tde.text_len: %i\n", de.text_len);
82        return -1;
83    }
84
85    ccprintf(os, "[%.6f] ", de.ts_nsec * 10e-9);
86    os.write((char *)base + sizeof(de), de.text_len);
87    os << std::endl;
88
89    return de.len;
90}
91
92void
93Linux::dumpDmesg(ThreadContext *tc, std::ostream &os)
94{
95    System *system = tc->getSystemPtr();
96    const SymbolTable *symtab = system->kernelSymtab;
97    FSTranslatingPortProxy proxy(tc);
98
99    Addr addr_lb = 0, addr_lb_len = 0, addr_first = 0, addr_next = 0;
100    const bool found_symbols =
101        symtab->findAddress("__log_buf", addr_lb) &&
102        symtab->findAddress("log_buf_len", addr_lb_len) &&
103        symtab->findAddress("log_first_idx", addr_first) &&
104        symtab->findAddress("log_next_idx", addr_next);
105
106    if (!found_symbols) {
107        warn("Failed to find kernel dmesg symbols.\n");
108        return;
109    }
110
111    uint32_t log_buf_len =
112        proxy.read<uint32_t>(addr_lb_len, TheISA::GuestByteOrder);
113    uint32_t log_first_idx =
114        proxy.read<uint32_t>(addr_first, TheISA::GuestByteOrder);
115    uint32_t log_next_idx =
116        proxy.read<uint32_t>(addr_next, TheISA::GuestByteOrder);
117
118    if (log_first_idx >= log_buf_len || log_next_idx >= log_buf_len) {
119        warn("dmesg pointers/length corrupted\n");
120        return;
121    }
122
123    // Normalize and read the dmesg ring buffer
124    std::vector<uint8_t> log_buf(log_buf_len);
125    int length;
126    if (log_first_idx < log_next_idx) {
127        length = log_next_idx - log_first_idx;
128        if (length < 0 || length > log_buf.size()) {
129            warn("Unexpected dmesg buffer length\n");
130            return;
131        }
132        proxy.readBlob(addr_lb + log_first_idx, log_buf.data(), length);
133    } else {
134        const int length_2 = log_buf_len - log_first_idx;
135        if (length_2 < 0 || length_2 + log_next_idx > log_buf.size()) {
136            warn("Unexpected dmesg buffer length\n");
137            return;
138        }
139        length = log_buf_len;
140        proxy.readBlob(addr_lb + log_first_idx, log_buf.data(), length_2);
141        proxy.readBlob(addr_lb, log_buf.data() + length_2, log_next_idx);
142    }
143
144    // Print dmesg buffer content
145    const uint8_t *cur = log_buf.data(), *end = log_buf.data() + length;
146    while (cur < end) {
147        int ret = dumpDmesgEntry(cur, end, os);
148        if (ret < 0)
149            return;
150        cur += ret;
151    }
152}
153