gem5  v21.2.1.1
helpers.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "kern/linux/helpers.hh"
39 
40 #include "base/compiler.hh"
41 #include "cpu/thread_context.hh"
42 #include "mem/port_proxy.hh"
44 #include "sim/byteswap.hh"
45 #include "sim/system.hh"
46 
47 namespace gem5
48 {
49 
50 struct GEM5_PACKED DmesgEntry
51 {
52  uint64_t ts_nsec;
53  uint16_t len;
54  uint16_t text_len;
55  uint16_t dict_len;
56  uint8_t facility;
57  uint8_t flags;
58 };
59 
60 static int
61 dumpDmesgEntry(const uint8_t *base, const uint8_t *end, const ByteOrder bo,
62  std::ostream &os)
63 {
64  const size_t max_length = end - base;
65  DmesgEntry de;
66 
67  if (max_length < sizeof(de)) {
68  warn("Malformed dmesg entry\n");
69  return -1;
70  }
71 
72  memcpy(&de, base, sizeof(de));
73  de.ts_nsec = gtoh(de.ts_nsec, bo);
74  de.len = gtoh(de.len, bo);
75  de.text_len = gtoh(de.text_len, bo);
76 
77  if (de.len < sizeof(de) ||
78  max_length < de.len ||
79  max_length < sizeof(DmesgEntry) + de.text_len) {
80 
81  warn("Malformed dmesg entry:\n");
82  warn("\tMax length: %i\n", max_length);
83  warn("\tde.len: %i\n", de.len);
84  warn("\tde.text_len: %i\n", de.text_len);
85  return -1;
86  }
87 
88  ccprintf(os, "[%.6f] ", de.ts_nsec * 10e-9);
89  os.write((char *)base + sizeof(de), de.text_len);
90  os << std::endl;
91 
92  return de.len;
93 }
94 
95 void
96 linux::dumpDmesg(ThreadContext *tc, std::ostream &os)
97 {
98  System *system = tc->getSystemPtr();
99  const ByteOrder bo = system->getGuestByteOrder();
100  const auto &symtab = system->workload->symtab(tc);
101  TranslatingPortProxy proxy(tc);
102 
103  auto lb = symtab.find("__log_buf");
104  auto lb_len = symtab.find("log_buf_len");
105  auto first = symtab.find("log_first_idx");
106  auto next = symtab.find("log_next_idx");
107 
108  auto end_it = symtab.end();
109 
110  if (lb == end_it || lb_len == end_it ||
111  first == end_it || next == end_it) {
112  warn("Failed to find kernel dmesg symbols.\n");
113  return;
114  }
115 
116  uint32_t log_buf_len = proxy.read<uint32_t>(lb_len->address, bo);
117  uint32_t log_first_idx = proxy.read<uint32_t>(first->address, bo);
118  uint32_t log_next_idx = proxy.read<uint32_t>(next->address, bo);
119 
120  if (log_first_idx >= log_buf_len || log_next_idx >= log_buf_len) {
121  warn("dmesg pointers/length corrupted\n");
122  return;
123  }
124 
125  // Normalize and read the dmesg ring buffer
126  std::vector<uint8_t> log_buf(log_buf_len);
127  int length;
128  if (log_first_idx < log_next_idx) {
129  length = log_next_idx - log_first_idx;
130  if (length < 0 || length > log_buf.size()) {
131  warn("Unexpected dmesg buffer length\n");
132  return;
133  }
134  proxy.readBlob(lb->address + log_first_idx, log_buf.data(), length);
135  } else {
136  const int length_2 = log_buf_len - log_first_idx;
137  if (length_2 < 0 || length_2 + log_next_idx > log_buf.size()) {
138  warn("Unexpected dmesg buffer length\n");
139  return;
140  }
141  length = log_buf_len;
142  proxy.readBlob(lb->address + log_first_idx, log_buf.data(), length_2);
143  proxy.readBlob(lb->address, log_buf.data() + length_2, log_next_idx);
144  }
145 
146  // Print dmesg buffer content
147  const uint8_t *cur = log_buf.data(), *end = log_buf.data() + length;
148  while (cur < end) {
149  int ret = dumpDmesgEntry(cur, end, bo, os);
150  if (ret < 0)
151  return;
152  cur += ret;
153  }
154 }
155 
156 } // namespace gem5
gem5::ThreadContext::getSystemPtr
virtual System * getSystemPtr()=0
warn
#define warn(...)
Definition: logging.hh:246
gem5::DmesgEntry::text_len
uint16_t text_len
Definition: helpers.cc:54
system.hh
gem5::DmesgEntry::len
uint16_t len
Definition: helpers.cc:53
translating_port_proxy.hh
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:65
helpers.hh
gem5::X86ISA::system
Bitfield< 15 > system
Definition: misc.hh:1003
std::vector< uint8_t >
gem5::X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::DmesgEntry
Definition: helpers.cc:50
gem5::TranslatingPortProxy
This proxy attempts to translate virtual addresses using the TLBs.
Definition: translating_port_proxy.hh:60
gem5::dumpDmesgEntry
static int dumpDmesgEntry(const uint8_t *base, const uint8_t *end, const ByteOrder bo, std::ostream &os)
Definition: helpers.cc:61
gem5::X86ISA::de
Bitfield< 3 > de
Definition: misc.hh:641
gem5::System
Definition: system.hh:75
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:94
gem5::PowerISA::bo
Bitfield< 25, 21 > bo
Definition: types.hh:82
gem5::DmesgEntry::dict_len
uint16_t dict_len
Definition: helpers.cc:55
gem5::PortProxy::read
T read(Addr address) const
Read sizeof(T) bytes from address and return as object T.
Definition: port_proxy.hh:287
port_proxy.hh
gem5::gtoh
T gtoh(T value, ByteOrder guest_byte_order)
Definition: byteswap.hh:194
compiler.hh
gem5::PortProxy::readBlob
void readBlob(Addr addr, void *p, int size) const
Higher level interfaces based on the above.
Definition: port_proxy.hh:182
gem5::DmesgEntry::ts_nsec
uint64_t ts_nsec
Definition: helpers.cc:52
gem5::linux::dumpDmesg
void dumpDmesg(ThreadContext *tc, std::ostream &os)
Dump Linux's dmesg log buffer to the an output stream.
Definition: helpers.cc:96
gem5::DmesgEntry::facility
uint8_t facility
Definition: helpers.cc:56
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:809
gem5::DmesgEntry::flags
uint8_t flags
Definition: helpers.cc:57
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
thread_context.hh
byteswap.hh

Generated on Wed May 4 2022 12:13:59 for gem5 by doxygen 1.8.17