1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
#include "ProcFileSystem.h"
#include "Process.h"
#include <VirtualFileSystem/VirtualFileSystem.h>
#include "system.h"
#include "MemoryManager.h"
#include "StdLib.h"
#include "i386.h"
static ProcFileSystem* s_the;
ProcFileSystem& ProcFileSystem::the()
{
ASSERT(s_the);
return *s_the;
}
RetainPtr<ProcFileSystem> ProcFileSystem::create()
{
return adopt(*new ProcFileSystem);
}
ProcFileSystem::ProcFileSystem()
{
s_the = this;
}
ProcFileSystem::~ProcFileSystem()
{
}
ByteBuffer procfs$pid_fds(Process& process)
{
ProcessInspectionScope scope(process);
char* buffer;
auto stringImpl = StringImpl::createUninitialized(process.number_of_open_file_descriptors() * 80, buffer);
memset(buffer, 0, stringImpl->length());
char* ptr = buffer;
for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
auto* descriptor = process.file_descriptor(i);
if (!descriptor)
continue;
ptr += ksprintf(ptr, "% 3u %s\n", i, descriptor->absolute_path().characters());
}
*ptr = '\0';
return ByteBuffer::copy((byte*)buffer, ptr - buffer);
}
ByteBuffer procfs$pid_vm(Process& process)
{
ProcessInspectionScope scope(process);
char* buffer;
auto stringImpl = StringImpl::createUninitialized(80 + process.regionCount() * 80 + 4096, buffer);
memset(buffer, 0, stringImpl->length());
char* ptr = buffer;
ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
for (auto& region : process.regions()) {
ptr += ksprintf(ptr, "%x -- %x %x %s\n",
region->linearAddress.get(),
region->linearAddress.offset(region->size - 1).get(),
region->size,
region->name.characters());
for (size_t i = 0; i < region->physical_pages.size(); ++i) {
auto& physical_page = region->physical_pages[i];
ptr += ksprintf(ptr, "P%x%s(%u) ",
physical_page ? physical_page->paddr().get() : 0,
region->cow_map.get(i) ? "!" : "",
physical_page->retain_count()
);
}
ptr += ksprintf(ptr, "\n");
}
*ptr = '\0';
return ByteBuffer::copy((byte*)buffer, ptr - buffer);
}
ByteBuffer procfs$pid_stack(Process& process)
{
ProcessInspectionScope scope(process);
ProcessPagingScope pagingScope(process);
struct RecognizedSymbol {
dword address;
const KSym* ksym;
};
Vector<RecognizedSymbol> recognizedSymbols;
if (auto* eipKsym = ksymbolicate(process.tss().eip))
recognizedSymbols.append({ process.tss().eip, eipKsym });
for (dword* stackPtr = (dword*)process.framePtr(); process.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());
return buffer;
}
ByteBuffer procfs$pid_regs(Process& process)
{
ProcessInspectionScope scope(process);
auto& tss = process.tss();
auto buffer = ByteBuffer::createUninitialized(1024);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "eax: %x\n", tss.eax);
ptr += ksprintf(ptr, "ebx: %x\n", tss.ebx);
ptr += ksprintf(ptr, "ecx: %x\n", tss.ecx);
ptr += ksprintf(ptr, "edx: %x\n", tss.edx);
ptr += ksprintf(ptr, "esi: %x\n", tss.esi);
ptr += ksprintf(ptr, "edi: %x\n", tss.edi);
ptr += ksprintf(ptr, "ebp: %x\n", tss.ebp);
ptr += ksprintf(ptr, "cr3: %x\n", tss.cr3);
ptr += ksprintf(ptr, "flg: %x\n", tss.eflags);
ptr += ksprintf(ptr, "sp: %w:%x\n", tss.ss, tss.esp);
ptr += ksprintf(ptr, "pc: %w:%x\n", tss.cs, tss.eip);
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
ByteBuffer procfs$pid_exe(Process& process)
{
ProcessInspectionScope scope(process);
auto inode = process.executableInode();
return VirtualFileSystem::the().absolutePath(inode).toByteBuffer();
}
void ProcFileSystem::addProcess(Process& process)
{
InterruptDisabler disabler;
char buf[16];
ksprintf(buf, "%d", process.pid());
auto dir = addFile(createDirectory(buf));
m_pid2inode.set(process.pid(), dir.index());
addFile(createGeneratedFile("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
addFile(createGeneratedFile("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
addFile(createGeneratedFile("regs", [&process] { return procfs$pid_regs(process); }), dir.index());
addFile(createGeneratedFile("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
if (process.executableInode().isValid())
addFile(createGeneratedFile("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
}
void ProcFileSystem::removeProcess(Process& process)
{
InterruptDisabler disabler;
auto pid = process.pid();
auto it = m_pid2inode.find(pid);
ASSERT(it != m_pid2inode.end());
bool success = removeFile((*it).value);
ASSERT(success);
m_pid2inode.remove(pid);
}
ByteBuffer procfs$mm()
{
// FIXME: Implement
#if 0
InterruptDisabler disabler;
size_t zonePageCount = 0;
for (auto* zone : MM.m_zones)
zonePageCount += zone->m_pages.size();
auto buffer = ByteBuffer::createUninitialized(1024 + 80 * MM.m_zones.size() + zonePageCount * 10);
char* ptr = (char*)buffer.pointer();
for (auto* zone : MM.m_zones) {
ptr += ksprintf(ptr, "Zone %p size: %u\n ", zone, zone->size());
for (auto page : zone->m_pages)
ptr += ksprintf(ptr, "%x ", page);
ptr += ksprintf(ptr, "\n");
}
ptr += ksprintf(ptr, "Zone count: %u\n", MM.m_zones.size());
ptr += ksprintf(ptr, "Free physical pages: %u\n", MM.m_free_physical_pages.size());
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
#endif
return { };
}
ByteBuffer procfs$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$cpuinfo()
{
auto buffer = ByteBuffer::createUninitialized(256);
char* ptr = (char*)buffer.pointer();
{
CPUID cpuid(0);
ptr += ksprintf(ptr, "cpuid: ");
auto emit_dword = [&] (dword value) {
ptr += ksprintf(ptr, "%c%c%c%c",
value & 0xff,
(value >> 8) & 0xff,
(value >> 16) & 0xff,
(value >> 24) & 0xff);
};
emit_dword(cpuid.ebx());
emit_dword(cpuid.edx());
emit_dword(cpuid.ecx());
ptr += ksprintf(ptr, "\n");
}
{
CPUID cpuid(1);
dword stepping = cpuid.eax() & 0xf;
dword model = (cpuid.eax() >> 4) & 0xf;
dword family = (cpuid.eax() >> 8) & 0xf;
dword type = (cpuid.eax() >> 12) & 0x3;
dword extended_model = (cpuid.eax() >> 16) & 0xf;
dword extended_family = (cpuid.eax() >> 20) & 0xff;
dword display_model;
dword display_family;
if (family == 15) {
display_family = family + extended_family;
display_model = model + (extended_model << 4);
} else if (family == 6) {
display_family = family;
display_model = model + (extended_model << 4);
} else {
display_family = family;
display_model = model;
}
ptr += ksprintf(ptr, "family: %u\n", display_family);
ptr += ksprintf(ptr, "model: %u\n", display_model);
ptr += ksprintf(ptr, "stepping: %u\n", stepping);
ptr += ksprintf(ptr, "type: %u\n", type);
}
{
// FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
// and verifying that the returned eax>=0x80000004.
char buffer[48];
dword* bufptr = reinterpret_cast<dword*>(buffer);
auto copy_brand_string_part_to_buffer = [&] (dword i) {
CPUID cpuid(0x80000002 + i);
*bufptr++ = cpuid.eax();
*bufptr++ = cpuid.ebx();
*bufptr++ = cpuid.ecx();
*bufptr++ = cpuid.edx();
};
copy_brand_string_part_to_buffer(0);
copy_brand_string_part_to_buffer(1);
copy_brand_string_part_to_buffer(2);
ptr += ksprintf(ptr, "brandstr: \"%s\"\n", buffer);
}
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
ByteBuffer procfs$kmalloc()
{
InterruptDisabler disabler;
auto buffer = ByteBuffer::createUninitialized(256);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "eternal: %u\npage-aligned: %u\nallocated: %u\nfree: %u\n", kmalloc_sum_eternal, sum_alloc, sum_free);
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
ByteBuffer procfs$summary()
{
InterruptDisabler disabler;
auto processes = Process::allProcesses();
auto buffer = ByteBuffer::createUninitialized(processes.size() * 256);
char* ptr = (char*)buffer.pointer();
ptr += ksprintf(ptr, "PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
for (auto* process : processes) {
ptr += ksprintf(ptr, "% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
process->pid(),
process->tty() ? process->tty()->pgid() : 0,
process->pgid(),
process->sid(),
process->uid(),
toString(process->state()),
process->ppid(),
process->timesScheduled(),
process->number_of_open_file_descriptors(),
process->tty() ? strrchr(process->tty()->ttyName().characters(), '/') + 1 : "n/a",
process->name().characters());
}
*ptr = '\0';
buffer.trim(ptr - (char*)buffer.pointer());
return buffer;
}
bool ProcFileSystem::initialize()
{
SyntheticFileSystem::initialize();
addFile(createGeneratedFile("mm", procfs$mm));
addFile(createGeneratedFile("mounts", procfs$mounts));
addFile(createGeneratedFile("kmalloc", procfs$kmalloc));
addFile(createGeneratedFile("summary", procfs$summary));
addFile(createGeneratedFile("cpuinfo", procfs$cpuinfo));
return true;
}
const char* ProcFileSystem::className() const
{
return "procfs";
}
|