ProcFileSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "ProcFileSystem.h"
  2. #include "Process.h"
  3. #include <VirtualFileSystem/VirtualFileSystem.h>
  4. #include "system.h"
  5. #include "MemoryManager.h"
  6. #include "StdLib.h"
  7. #include "i386.h"
  8. #include "KSyms.h"
  9. static ProcFS* s_the;
  10. ProcFS& ProcFS::the()
  11. {
  12. ASSERT(s_the);
  13. return *s_the;
  14. }
  15. RetainPtr<ProcFS> ProcFS::create()
  16. {
  17. return adopt(*new ProcFS);
  18. }
  19. ProcFS::ProcFS()
  20. {
  21. s_the = this;
  22. }
  23. ProcFS::~ProcFS()
  24. {
  25. }
  26. ByteBuffer procfs$pid_fds(Process& process)
  27. {
  28. ProcessInspectionHandle handle(process);
  29. char* buffer;
  30. auto stringImpl = StringImpl::create_uninitialized(process.number_of_open_file_descriptors() * 80, buffer);
  31. memset(buffer, 0, stringImpl->length());
  32. char* ptr = buffer;
  33. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  34. auto* descriptor = process.file_descriptor(i);
  35. if (!descriptor)
  36. continue;
  37. ptr += ksprintf(ptr, "% 3u %s\n", i, descriptor->absolute_path().characters());
  38. }
  39. *ptr = '\0';
  40. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  41. }
  42. ByteBuffer procfs$pid_vm(Process& process)
  43. {
  44. ProcessInspectionHandle handle(process);
  45. char* buffer;
  46. auto stringImpl = StringImpl::create_uninitialized(80 + process.regionCount() * 160 + 4096, buffer);
  47. memset(buffer, 0, stringImpl->length());
  48. char* ptr = buffer;
  49. ptr += ksprintf(ptr, "BEGIN END SIZE COMMIT NAME\n");
  50. for (auto& region : process.regions()) {
  51. ptr += ksprintf(ptr, "%x -- %x %x %x %s\n",
  52. region->linearAddress.get(),
  53. region->linearAddress.offset(region->size - 1).get(),
  54. region->size,
  55. region->committed(),
  56. region->name.characters());
  57. }
  58. *ptr = '\0';
  59. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  60. }
  61. ByteBuffer procfs$pid_vmo(Process& process)
  62. {
  63. ProcessInspectionHandle handle(process);
  64. char* buffer;
  65. auto stringImpl = StringImpl::create_uninitialized(80 + process.regionCount() * 160 + 4096, buffer);
  66. memset(buffer, 0, stringImpl->length());
  67. char* ptr = buffer;
  68. ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
  69. for (auto& region : process.regions()) {
  70. ptr += ksprintf(ptr, "%x -- %x %x %s\n",
  71. region->linearAddress.get(),
  72. region->linearAddress.offset(region->size - 1).get(),
  73. region->size,
  74. region->name.characters());
  75. ptr += ksprintf(ptr, "VMO: %s \"%s\" @ %x(%u)\n",
  76. region->vmo().is_anonymous() ? "anonymous" : "file-backed",
  77. region->vmo().name().characters(),
  78. &region->vmo(),
  79. region->vmo().retain_count());
  80. for (size_t i = 0; i < region->vmo().page_count(); ++i) {
  81. auto& physical_page = region->vmo().physical_pages()[i];
  82. ptr += ksprintf(ptr, "P%x%s(%u) ",
  83. physical_page ? physical_page->paddr().get() : 0,
  84. region->cow_map.get(i) ? "!" : "",
  85. physical_page ? physical_page->retain_count() : 0
  86. );
  87. }
  88. ptr += ksprintf(ptr, "\n");
  89. }
  90. *ptr = '\0';
  91. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  92. }
  93. ByteBuffer procfs$pid_stack(Process& process)
  94. {
  95. ProcessInspectionHandle handle(process);
  96. ProcessPagingScope pagingScope(process);
  97. struct RecognizedSymbol {
  98. dword address;
  99. const KSym* ksym;
  100. };
  101. Vector<RecognizedSymbol> recognizedSymbols;
  102. if (auto* eipKsym = ksymbolicate(process.tss().eip))
  103. recognizedSymbols.append({ process.tss().eip, eipKsym });
  104. for (dword* stackPtr = (dword*)process.framePtr(); process.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  105. dword retaddr = stackPtr[1];
  106. if (auto* ksym = ksymbolicate(retaddr))
  107. recognizedSymbols.append({ retaddr, ksym });
  108. }
  109. size_t bytesNeeded = 0;
  110. for (auto& symbol : recognizedSymbols) {
  111. bytesNeeded += strlen(symbol.ksym->name) + 8 + 16;
  112. }
  113. auto buffer = ByteBuffer::create_uninitialized(bytesNeeded);
  114. char* bufptr = (char*)buffer.pointer();
  115. for (auto& symbol : recognizedSymbols) {
  116. unsigned offset = symbol.address - symbol.ksym->address;
  117. bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
  118. }
  119. buffer.trim(bufptr - (char*)buffer.pointer());
  120. return buffer;
  121. }
  122. ByteBuffer procfs$pid_regs(Process& process)
  123. {
  124. ProcessInspectionHandle handle(process);
  125. auto& tss = process.tss();
  126. auto buffer = ByteBuffer::create_uninitialized(1024);
  127. char* ptr = (char*)buffer.pointer();
  128. ptr += ksprintf(ptr, "eax: %x\n", tss.eax);
  129. ptr += ksprintf(ptr, "ebx: %x\n", tss.ebx);
  130. ptr += ksprintf(ptr, "ecx: %x\n", tss.ecx);
  131. ptr += ksprintf(ptr, "edx: %x\n", tss.edx);
  132. ptr += ksprintf(ptr, "esi: %x\n", tss.esi);
  133. ptr += ksprintf(ptr, "edi: %x\n", tss.edi);
  134. ptr += ksprintf(ptr, "ebp: %x\n", tss.ebp);
  135. ptr += ksprintf(ptr, "cr3: %x\n", tss.cr3);
  136. ptr += ksprintf(ptr, "flg: %x\n", tss.eflags);
  137. ptr += ksprintf(ptr, "sp: %w:%x\n", tss.ss, tss.esp);
  138. ptr += ksprintf(ptr, "pc: %w:%x\n", tss.cs, tss.eip);
  139. buffer.trim(ptr - (char*)buffer.pointer());
  140. return buffer;
  141. }
  142. ByteBuffer procfs$pid_exe(Process& process)
  143. {
  144. ProcessInspectionHandle handle(process);
  145. auto inode = process.executable_inode();
  146. ASSERT(inode);
  147. return VFS::the().absolute_path(*inode).to_byte_buffer();
  148. }
  149. ByteBuffer procfs$pid_cwd(Process& process)
  150. {
  151. ProcessInspectionHandle handle(process);
  152. auto inode = process.cwd_inode();
  153. ASSERT(inode);
  154. return VFS::the().absolute_path(*inode).to_byte_buffer();
  155. }
  156. void ProcFS::add_process(Process& process)
  157. {
  158. InterruptDisabler disabler;
  159. char buf[16];
  160. ksprintf(buf, "%d", process.pid());
  161. auto dir = add_file(create_directory(buf));
  162. m_pid2inode.set(process.pid(), dir.index());
  163. add_file(create_generated_file("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
  164. add_file(create_generated_file("vmo", [&process] { return procfs$pid_vmo(process); }), dir.index());
  165. add_file(create_generated_file("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
  166. add_file(create_generated_file("regs", [&process] { return procfs$pid_regs(process); }), dir.index());
  167. add_file(create_generated_file("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
  168. if (process.executable_inode())
  169. add_file(create_generated_file("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
  170. add_file(create_generated_file("cwd", [&process] { return procfs$pid_cwd(process); }, 00120777), dir.index());
  171. }
  172. void ProcFS::remove_process(Process& process)
  173. {
  174. InterruptDisabler disabler;
  175. auto pid = process.pid();
  176. auto it = m_pid2inode.find(pid);
  177. if (it == m_pid2inode.end())
  178. return;
  179. bool success = remove_file((*it).value);
  180. ASSERT(success);
  181. m_pid2inode.remove(pid);
  182. }
  183. ByteBuffer procfs$mm()
  184. {
  185. // FIXME: Implement
  186. InterruptDisabler disabler;
  187. auto buffer = ByteBuffer::create_uninitialized(1024 + 80 * MM.m_vmos.size());
  188. char* ptr = (char*)buffer.pointer();
  189. for (auto* vmo : MM.m_vmos) {
  190. ptr += ksprintf(ptr, "VMO: %p %s(%u): p:%4u %s\n",
  191. vmo,
  192. vmo->is_anonymous() ? "anon" : "file",
  193. vmo->retain_count(),
  194. vmo->page_count(),
  195. vmo->name().characters());
  196. }
  197. ptr += ksprintf(ptr, "VMO count: %u\n", MM.m_vmos.size());
  198. ptr += ksprintf(ptr, "Free physical pages: %u\n", MM.m_free_physical_pages.size());
  199. ptr += ksprintf(ptr, "Free supervisor physical pages: %u\n", MM.m_free_supervisor_physical_pages.size());
  200. buffer.trim(ptr - (char*)buffer.pointer());
  201. return buffer;
  202. }
  203. ByteBuffer procfs$regions()
  204. {
  205. // FIXME: Implement
  206. InterruptDisabler disabler;
  207. auto buffer = ByteBuffer::create_uninitialized(1024 + 80 * MM.m_regions.size());
  208. char* ptr = (char*)buffer.pointer();
  209. for (auto* region : MM.m_regions) {
  210. ptr += ksprintf(ptr, "Region: %p VMO=%p %s\n",
  211. region,
  212. &region->vmo(),
  213. region->name.characters());
  214. }
  215. ptr += ksprintf(ptr, "Region count: %u\n", MM.m_regions.size());
  216. buffer.trim(ptr - (char*)buffer.pointer());
  217. return buffer;
  218. }
  219. ByteBuffer procfs$mounts()
  220. {
  221. InterruptDisabler disabler;
  222. auto buffer = ByteBuffer::create_uninitialized(VFS::the().mount_count() * 80);
  223. char* ptr = (char*)buffer.pointer();
  224. VFS::the().for_each_mount([&ptr] (auto& mount) {
  225. auto& fs = mount.guest_fs();
  226. ptr += ksprintf(ptr, "%s @ ", fs.class_name());
  227. if (!mount.host().is_valid())
  228. ptr += ksprintf(ptr, "/\n", fs.class_name());
  229. else
  230. ptr += ksprintf(ptr, "%u:%u\n", mount.host().fsid(), mount.host().index());
  231. });
  232. buffer.trim(ptr - (char*)buffer.pointer());
  233. return buffer;
  234. }
  235. ByteBuffer procfs$cpuinfo()
  236. {
  237. auto buffer = ByteBuffer::create_uninitialized(256);
  238. char* ptr = (char*)buffer.pointer();
  239. {
  240. CPUID cpuid(0);
  241. ptr += ksprintf(ptr, "cpuid: ");
  242. auto emit_dword = [&] (dword value) {
  243. ptr += ksprintf(ptr, "%c%c%c%c",
  244. value & 0xff,
  245. (value >> 8) & 0xff,
  246. (value >> 16) & 0xff,
  247. (value >> 24) & 0xff);
  248. };
  249. emit_dword(cpuid.ebx());
  250. emit_dword(cpuid.edx());
  251. emit_dword(cpuid.ecx());
  252. ptr += ksprintf(ptr, "\n");
  253. }
  254. {
  255. CPUID cpuid(1);
  256. dword stepping = cpuid.eax() & 0xf;
  257. dword model = (cpuid.eax() >> 4) & 0xf;
  258. dword family = (cpuid.eax() >> 8) & 0xf;
  259. dword type = (cpuid.eax() >> 12) & 0x3;
  260. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  261. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  262. dword display_model;
  263. dword display_family;
  264. if (family == 15) {
  265. display_family = family + extended_family;
  266. display_model = model + (extended_model << 4);
  267. } else if (family == 6) {
  268. display_family = family;
  269. display_model = model + (extended_model << 4);
  270. } else {
  271. display_family = family;
  272. display_model = model;
  273. }
  274. ptr += ksprintf(ptr, "family: %u\n", display_family);
  275. ptr += ksprintf(ptr, "model: %u\n", display_model);
  276. ptr += ksprintf(ptr, "stepping: %u\n", stepping);
  277. ptr += ksprintf(ptr, "type: %u\n", type);
  278. }
  279. {
  280. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  281. // and verifying that the returned eax>=0x80000004.
  282. char buffer[48];
  283. dword* bufptr = reinterpret_cast<dword*>(buffer);
  284. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  285. CPUID cpuid(0x80000002 + i);
  286. *bufptr++ = cpuid.eax();
  287. *bufptr++ = cpuid.ebx();
  288. *bufptr++ = cpuid.ecx();
  289. *bufptr++ = cpuid.edx();
  290. };
  291. copy_brand_string_part_to_buffer(0);
  292. copy_brand_string_part_to_buffer(1);
  293. copy_brand_string_part_to_buffer(2);
  294. ptr += ksprintf(ptr, "brandstr: \"%s\"\n", buffer);
  295. }
  296. buffer.trim(ptr - (char*)buffer.pointer());
  297. return buffer;
  298. }
  299. ByteBuffer procfs$kmalloc()
  300. {
  301. auto buffer = ByteBuffer::create_uninitialized(256);
  302. char* ptr = (char*)buffer.pointer();
  303. ptr += ksprintf(ptr, "eternal: %u\nallocated: %u\nfree: %u\n", kmalloc_sum_eternal, sum_alloc, sum_free);
  304. buffer.trim(ptr - (char*)buffer.pointer());
  305. return buffer;
  306. }
  307. ByteBuffer procfs$summary()
  308. {
  309. InterruptDisabler disabler;
  310. auto processes = Process::allProcesses();
  311. auto buffer = ByteBuffer::create_uninitialized(processes.size() * 256);
  312. char* ptr = (char*)buffer.pointer();
  313. ptr += ksprintf(ptr, "PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  314. for (auto* process : processes) {
  315. ptr += ksprintf(ptr, "% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  316. process->pid(),
  317. process->tty() ? process->tty()->pgid() : 0,
  318. process->pgid(),
  319. process->sid(),
  320. process->uid(),
  321. toString(process->state()),
  322. process->ppid(),
  323. process->timesScheduled(),
  324. process->number_of_open_file_descriptors(),
  325. process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a",
  326. process->name().characters());
  327. }
  328. *ptr = '\0';
  329. buffer.trim(ptr - (char*)buffer.pointer());
  330. return buffer;
  331. }
  332. ByteBuffer procfs$vnodes()
  333. {
  334. auto& vfs = VFS::the();
  335. auto buffer = ByteBuffer::create_uninitialized(vfs.m_max_vnode_count * 256);
  336. char* ptr = (char*)buffer.pointer();
  337. for (size_t i = 0; i < vfs.m_max_vnode_count; ++i) {
  338. auto& vnode = vfs.m_nodes[i];
  339. // FIXME: Retain the vnode while inspecting it.
  340. if (!vnode.inUse())
  341. continue;
  342. String path;
  343. if (vnode.core_inode())
  344. path = vfs.absolute_path(*vnode.core_inode());
  345. if (path.is_empty()) {
  346. if (auto* dev = vnode.characterDevice()) {
  347. if (dev->is_tty())
  348. path = static_cast<const TTY*>(dev)->tty_name();
  349. }
  350. }
  351. ptr += ksprintf(ptr, "vnode %03u: %02u:%08u (%u) %s", i, vnode.inode.fsid(), vnode.inode.index(), vnode.retain_count(), path.characters());
  352. if (vnode.characterDevice())
  353. ptr += ksprintf(ptr, " (chardev: %p)", vnode.characterDevice());
  354. ptr += ksprintf(ptr, "\n");
  355. }
  356. *ptr = '\0';
  357. buffer.trim(ptr - (char*)buffer.pointer());
  358. return buffer;
  359. }
  360. bool ProcFS::initialize()
  361. {
  362. SynthFS::initialize();
  363. add_file(create_generated_file("mm", procfs$mm));
  364. add_file(create_generated_file("regions", procfs$regions));
  365. add_file(create_generated_file("mounts", procfs$mounts));
  366. add_file(create_generated_file("kmalloc", procfs$kmalloc));
  367. add_file(create_generated_file("summary", procfs$summary));
  368. add_file(create_generated_file("cpuinfo", procfs$cpuinfo));
  369. add_file(create_generated_file("vnodes", procfs$vnodes));
  370. return true;
  371. }
  372. const char* ProcFS::class_name() const
  373. {
  374. return "procfs";
  375. }