ProcFileSystem.cpp 14 KB

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