1/* 2 * Copyright (c) 2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Ali Saidi 29 * Nathan Binkert 30 * Dam Sunwoo 31 */ 32 33#ifndef __ARCH_GENERIC_LINUX_THREADINFO_HH__ 34#define __ARCH_GENERIC_LINUX_THREADINFO_HH__ 35 36#include "cpu/thread_context.hh" 37#include "sim/system.hh" 38#include "sim/vptr.hh" 39 40namespace Linux { 41 42class ThreadInfo 43{ 44 private: 45 ThreadContext *tc; 46 System *sys; 47 Addr pcbb; 48 49 template <typename T> 50 bool 51 get_data(const char *symbol, T &data) 52 { 53 Addr addr = 0; 54 if (!sys->kernelSymtab->findAddress(symbol, addr)) { 55 warn_once("Unable to find kernel symbol %s\n", symbol); 56 warn_once("Kernel not compiled with task_struct info; can't get " 57 "currently executing task/process/thread name/ids!\n"); 58 return false; 59 } 60
| 1/* 2 * Copyright (c) 2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Ali Saidi 29 * Nathan Binkert 30 * Dam Sunwoo 31 */ 32 33#ifndef __ARCH_GENERIC_LINUX_THREADINFO_HH__ 34#define __ARCH_GENERIC_LINUX_THREADINFO_HH__ 35 36#include "cpu/thread_context.hh" 37#include "sim/system.hh" 38#include "sim/vptr.hh" 39 40namespace Linux { 41 42class ThreadInfo 43{ 44 private: 45 ThreadContext *tc; 46 System *sys; 47 Addr pcbb; 48 49 template <typename T> 50 bool 51 get_data(const char *symbol, T &data) 52 { 53 Addr addr = 0; 54 if (!sys->kernelSymtab->findAddress(symbol, addr)) { 55 warn_once("Unable to find kernel symbol %s\n", symbol); 56 warn_once("Kernel not compiled with task_struct info; can't get " 57 "currently executing task/process/thread name/ids!\n"); 58 return false; 59 } 60
|
61 CopyOut(tc, &data, addr, sizeof(T));
| 61 data = tc->getVirtProxy().read<T>(addr, TheISA::GuestByteOrder);
|
62
| 62
|
63 data = TheISA::gtoh(data); 64
| |
65 return true; 66 } 67 68 public: 69 ThreadInfo(ThreadContext *_tc, Addr _pcbb = 0) 70 : tc(_tc), sys(tc->getSystemPtr()), pcbb(_pcbb) 71 { 72 73 } 74 ~ThreadInfo() 75 {} 76 77 inline Addr 78 curThreadInfo() 79 { 80 if (!TheISA::CurThreadInfoImplemented) 81 panic("curThreadInfo() not implemented for this ISA"); 82 83 Addr addr = pcbb; 84 Addr sp; 85 86 if (!addr) 87 addr = tc->readMiscRegNoEffect(TheISA::CurThreadInfoReg); 88 89 PortProxy &p = tc->getPhysProxy(); 90 p.readBlob(addr, &sp, sizeof(Addr)); 91 92 return sp & ~ULL(0x3fff); 93 } 94 95 inline Addr 96 curTaskInfo(Addr thread_info = 0) 97 { 98 // Note that in Linux 4.10 the thread_info struct will no longer have a 99 // pointer to the task_struct for arm64. See: 100 // https://patchwork.kernel.org/patch/9333699/
| 63 return true; 64 } 65 66 public: 67 ThreadInfo(ThreadContext *_tc, Addr _pcbb = 0) 68 : tc(_tc), sys(tc->getSystemPtr()), pcbb(_pcbb) 69 { 70 71 } 72 ~ThreadInfo() 73 {} 74 75 inline Addr 76 curThreadInfo() 77 { 78 if (!TheISA::CurThreadInfoImplemented) 79 panic("curThreadInfo() not implemented for this ISA"); 80 81 Addr addr = pcbb; 82 Addr sp; 83 84 if (!addr) 85 addr = tc->readMiscRegNoEffect(TheISA::CurThreadInfoReg); 86 87 PortProxy &p = tc->getPhysProxy(); 88 p.readBlob(addr, &sp, sizeof(Addr)); 89 90 return sp & ~ULL(0x3fff); 91 } 92 93 inline Addr 94 curTaskInfo(Addr thread_info = 0) 95 { 96 // Note that in Linux 4.10 the thread_info struct will no longer have a 97 // pointer to the task_struct for arm64. See: 98 // https://patchwork.kernel.org/patch/9333699/
|
101 int32_t offset;
| 99 int32_t offset = 0;
|
102 if (!get_data("thread_info_task", offset)) 103 return 0; 104 105 if (!thread_info) 106 thread_info = curThreadInfo(); 107
| 100 if (!get_data("thread_info_task", offset)) 101 return 0; 102 103 if (!thread_info) 104 thread_info = curThreadInfo(); 105
|
108 Addr addr; 109 CopyOut(tc, &addr, thread_info + offset, sizeof(addr)); 110 111 return addr;
| 106 return tc->getVirtProxy().read<Addr>(thread_info + offset);
|
112 } 113 114 int32_t 115 curTaskPIDFromTaskStruct(Addr task_struct) {
| 107 } 108 109 int32_t 110 curTaskPIDFromTaskStruct(Addr task_struct) {
|
116 int32_t offset;
| 111 int32_t offset = 0;
|
117 if (!get_data("task_struct_pid", offset)) 118 return -1; 119
| 112 if (!get_data("task_struct_pid", offset)) 113 return -1; 114
|
120 int32_t pid; 121 CopyOut(tc, &pid, task_struct + offset, sizeof(pid)); 122 123 return pid;
| 115 return tc->getVirtProxy().read<int32_t>(task_struct + offset);
|
124 } 125 126 int32_t 127 curTaskPID(Addr thread_info = 0) 128 { 129 return curTaskPIDFromTaskStruct(curTaskInfo(thread_info)); 130 } 131 132 int32_t 133 curTaskTGIDFromTaskStruct(Addr task_struct) 134 {
| 116 } 117 118 int32_t 119 curTaskPID(Addr thread_info = 0) 120 { 121 return curTaskPIDFromTaskStruct(curTaskInfo(thread_info)); 122 } 123 124 int32_t 125 curTaskTGIDFromTaskStruct(Addr task_struct) 126 {
|
135 int32_t offset;
| 127 int32_t offset = 0;
|
136 if (!get_data("task_struct_tgid", offset)) 137 return -1; 138
| 128 if (!get_data("task_struct_tgid", offset)) 129 return -1; 130
|
139 int32_t tgid; 140 CopyOut(tc, &tgid, task_struct + offset, sizeof(tgid)); 141 142 return tgid;
| 131 return tc->getVirtProxy().read<int32_t>(task_struct + offset);
|
143 } 144 145 int32_t 146 curTaskTGID(Addr thread_info = 0) 147 { 148 return curTaskTGIDFromTaskStruct(curTaskInfo(thread_info)); 149 } 150 151 int64_t 152 curTaskStartFromTaskStruct(Addr task_struct) 153 {
| 132 } 133 134 int32_t 135 curTaskTGID(Addr thread_info = 0) 136 { 137 return curTaskTGIDFromTaskStruct(curTaskInfo(thread_info)); 138 } 139 140 int64_t 141 curTaskStartFromTaskStruct(Addr task_struct) 142 {
|
154 int32_t offset;
| 143 int32_t offset = 0;
|
155 if (!get_data("task_struct_start_time", offset)) 156 return -1; 157
| 144 if (!get_data("task_struct_start_time", offset)) 145 return -1; 146
|
158 int64_t data;
| |
159 // start_time is actually of type timespec, but if we just 160 // grab the first long, we'll get the seconds out of it
| 147 // start_time is actually of type timespec, but if we just 148 // grab the first long, we'll get the seconds out of it
|
161 CopyOut(tc, &data, task_struct + offset, sizeof(data)); 162 163 return data;
| 149 return tc->getVirtProxy().read<int64_t>(task_struct + offset);
|
164 } 165 166 int64_t 167 curTaskStart(Addr thread_info = 0) 168 { 169 return curTaskStartFromTaskStruct(curTaskInfo(thread_info)); 170 } 171 172 std::string 173 curTaskNameFromTaskStruct(Addr task_struct) 174 {
| 150 } 151 152 int64_t 153 curTaskStart(Addr thread_info = 0) 154 { 155 return curTaskStartFromTaskStruct(curTaskInfo(thread_info)); 156 } 157 158 std::string 159 curTaskNameFromTaskStruct(Addr task_struct) 160 {
|
175 int32_t offset; 176 int32_t size;
| 161 int32_t offset = 0; 162 int32_t size = 0;
|
177 178 if (!get_data("task_struct_comm", offset)) 179 return "FailureIn_curTaskName"; 180 181 if (!get_data("task_struct_comm_size", size)) 182 return "FailureIn_curTaskName"; 183 184 char buffer[size + 1];
| 163 164 if (!get_data("task_struct_comm", offset)) 165 return "FailureIn_curTaskName"; 166 167 if (!get_data("task_struct_comm_size", size)) 168 return "FailureIn_curTaskName"; 169 170 char buffer[size + 1];
|
185 CopyStringOut(tc, buffer, task_struct + offset, size);
| 171 tc->getVirtProxy().readString(buffer, task_struct + offset, size);
|
186 187 return buffer; 188 } 189 190 std::string 191 curTaskName(Addr thread_info = 0) 192 { 193 return curTaskNameFromTaskStruct(curTaskInfo(thread_info)); 194 } 195 196 int32_t 197 curTaskMmFromTaskStruct(Addr task_struct) 198 { 199 int32_t offset; 200 if (!get_data("task_struct_mm", offset)) 201 return -1; 202
| 172 173 return buffer; 174 } 175 176 std::string 177 curTaskName(Addr thread_info = 0) 178 { 179 return curTaskNameFromTaskStruct(curTaskInfo(thread_info)); 180 } 181 182 int32_t 183 curTaskMmFromTaskStruct(Addr task_struct) 184 { 185 int32_t offset; 186 if (!get_data("task_struct_mm", offset)) 187 return -1; 188
|
203 int32_t mm_ptr; 204 CopyOut(tc, &mm_ptr, task_struct + offset, sizeof(mm_ptr)); 205 206 return mm_ptr;
| 189 return tc->getVirtProxy().read<int32_t>(task_struct + offset);
|
207 } 208 209 int32_t 210 curTaskMm(Addr thread_info = 0) 211 { 212 return curTaskMmFromTaskStruct(curTaskInfo(thread_info)); 213 } 214}; 215 216} // namespace Linux 217 218#endif // __ARCH_GENERIC_LINUX_THREADINFO_HH__
| 190 } 191 192 int32_t 193 curTaskMm(Addr thread_info = 0) 194 { 195 return curTaskMmFromTaskStruct(curTaskInfo(thread_info)); 196 } 197}; 198 199} // namespace Linux 200 201#endif // __ARCH_GENERIC_LINUX_THREADINFO_HH__
|