ProcFileSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 paging_scope(process);
  89. struct RecognizedSymbol {
  90. dword address;
  91. const KSym* ksym;
  92. };
  93. Vector<RecognizedSymbol> recognized_symbols;
  94. if (auto* eip_ksym = ksymbolicate(process.tss().eip))
  95. recognized_symbols.append({ process.tss().eip, eip_ksym });
  96. for (dword* stack_ptr = (dword*)process.frame_ptr(); process.validate_read_from_kernel(LinearAddress((dword)stack_ptr)); stack_ptr = (dword*)*stack_ptr) {
  97. dword retaddr = stack_ptr[1];
  98. if (auto* ksym = ksymbolicate(retaddr))
  99. recognized_symbols.append({ retaddr, ksym });
  100. }
  101. StringBuilder builder;
  102. for (auto& symbol : recognized_symbols) {
  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. ByteBuffer procfs$self(SynthFSInode&)
  141. {
  142. char buffer[16];
  143. ksprintf(buffer, "%u", current->pid());
  144. return ByteBuffer::copy((const byte*)buffer, strlen(buffer));
  145. }
  146. void ProcFS::add_process(Process& process)
  147. {
  148. InterruptDisabler disabler;
  149. auto dir = add_file(create_directory(String::format("%d", process.pid())));
  150. m_pid2inode.set(process.pid(), dir.index());
  151. add_file(create_generated_file("vm", [&process] (SynthFSInode&) { return procfs$pid_vm(process); }), dir.index());
  152. add_file(create_generated_file("vmo", [&process] (SynthFSInode&) { return procfs$pid_vmo(process); }), dir.index());
  153. add_file(create_generated_file("stack", [&process] (SynthFSInode&) { return procfs$pid_stack(process); }), dir.index());
  154. add_file(create_generated_file("regs", [&process] (SynthFSInode&) { return procfs$pid_regs(process); }), dir.index());
  155. add_file(create_generated_file("fds", [&process] (SynthFSInode&) { return procfs$pid_fds(process); }), dir.index());
  156. if (process.executable_inode())
  157. add_file(create_generated_file("exe", [&process] (SynthFSInode&) { return procfs$pid_exe(process); }, 00120777), dir.index());
  158. if (process.cwd_inode())
  159. add_file(create_generated_file("cwd", [&process] (SynthFSInode&) { return procfs$pid_cwd(process); }, 00120777), dir.index());
  160. }
  161. void ProcFS::remove_process(Process& process)
  162. {
  163. InterruptDisabler disabler;
  164. auto pid = process.pid();
  165. auto it = m_pid2inode.find(pid);
  166. if (it == m_pid2inode.end())
  167. return;
  168. bool success = remove_file((*it).value);
  169. ASSERT(success);
  170. m_pid2inode.remove(pid);
  171. }
  172. ByteBuffer procfs$mm(SynthFSInode&)
  173. {
  174. // FIXME: Implement
  175. InterruptDisabler disabler;
  176. StringBuilder builder;
  177. for (auto* vmo : MM.m_vmos) {
  178. builder.appendf("VMO: %p %s(%u): p:%4u %s\n",
  179. vmo,
  180. vmo->is_anonymous() ? "anon" : "file",
  181. vmo->retain_count(),
  182. vmo->page_count(),
  183. vmo->name().characters());
  184. }
  185. builder.appendf("VMO count: %u\n", MM.m_vmos.size());
  186. builder.appendf("Free physical pages: %u\n", MM.m_free_physical_pages.size());
  187. builder.appendf("Free supervisor physical pages: %u\n", MM.m_free_supervisor_physical_pages.size());
  188. return builder.to_byte_buffer();
  189. }
  190. ByteBuffer procfs$dmesg(SynthFSInode&)
  191. {
  192. InterruptDisabler disabler;
  193. StringBuilder builder;
  194. for (char ch : Console::the().logbuffer())
  195. builder.append(ch);
  196. return builder.to_byte_buffer();
  197. }
  198. ByteBuffer procfs$mounts(SynthFSInode&)
  199. {
  200. // FIXME: This is obviously racy against the VFS mounts changing.
  201. StringBuilder builder;
  202. VFS::the().for_each_mount([&builder] (auto& mount) {
  203. auto& fs = mount.guest_fs();
  204. builder.appendf("%s @ ", fs.class_name());
  205. if (!mount.host().is_valid())
  206. builder.appendf("/");
  207. else {
  208. builder.appendf("%u:%u", mount.host().fsid(), mount.host().index());
  209. auto path = VFS::the().absolute_path(mount.host());
  210. builder.append(' ');
  211. builder.append(path);
  212. }
  213. builder.append('\n');
  214. });
  215. return builder.to_byte_buffer();
  216. }
  217. ByteBuffer procfs$cpuinfo(SynthFSInode&)
  218. {
  219. StringBuilder builder;
  220. {
  221. CPUID cpuid(0);
  222. builder.appendf("cpuid: ");
  223. auto emit_dword = [&] (dword value) {
  224. builder.appendf("%c%c%c%c",
  225. value & 0xff,
  226. (value >> 8) & 0xff,
  227. (value >> 16) & 0xff,
  228. (value >> 24) & 0xff);
  229. };
  230. emit_dword(cpuid.ebx());
  231. emit_dword(cpuid.edx());
  232. emit_dword(cpuid.ecx());
  233. builder.appendf("\n");
  234. }
  235. {
  236. CPUID cpuid(1);
  237. dword stepping = cpuid.eax() & 0xf;
  238. dword model = (cpuid.eax() >> 4) & 0xf;
  239. dword family = (cpuid.eax() >> 8) & 0xf;
  240. dword type = (cpuid.eax() >> 12) & 0x3;
  241. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  242. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  243. dword display_model;
  244. dword display_family;
  245. if (family == 15) {
  246. display_family = family + extended_family;
  247. display_model = model + (extended_model << 4);
  248. } else if (family == 6) {
  249. display_family = family;
  250. display_model = model + (extended_model << 4);
  251. } else {
  252. display_family = family;
  253. display_model = model;
  254. }
  255. builder.appendf("family: %u\n", display_family);
  256. builder.appendf("model: %u\n", display_model);
  257. builder.appendf("stepping: %u\n", stepping);
  258. builder.appendf("type: %u\n", type);
  259. }
  260. {
  261. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  262. // and verifying that the returned eax>=0x80000004.
  263. char buffer[48];
  264. dword* bufptr = reinterpret_cast<dword*>(buffer);
  265. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  266. CPUID cpuid(0x80000002 + i);
  267. *bufptr++ = cpuid.eax();
  268. *bufptr++ = cpuid.ebx();
  269. *bufptr++ = cpuid.ecx();
  270. *bufptr++ = cpuid.edx();
  271. };
  272. copy_brand_string_part_to_buffer(0);
  273. copy_brand_string_part_to_buffer(1);
  274. copy_brand_string_part_to_buffer(2);
  275. builder.appendf("brandstr: \"%s\"\n", buffer);
  276. }
  277. return builder.to_byte_buffer();
  278. }
  279. ByteBuffer procfs$kmalloc(SynthFSInode&)
  280. {
  281. StringBuilder builder;
  282. builder.appendf(
  283. "eternal: %u\n"
  284. "allocated: %u\n"
  285. "free: %u\n",
  286. kmalloc_sum_eternal,
  287. sum_alloc,
  288. sum_free
  289. );
  290. return builder.to_byte_buffer();
  291. }
  292. ByteBuffer procfs$summary(SynthFSInode&)
  293. {
  294. InterruptDisabler disabler;
  295. auto processes = Process::all_processes();
  296. StringBuilder builder;
  297. builder.appendf("PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  298. for (auto* process : processes) {
  299. builder.appendf("% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  300. process->pid(),
  301. process->tty() ? process->tty()->pgid() : 0,
  302. process->pgid(),
  303. process->sid(),
  304. process->uid(),
  305. to_string(process->state()),
  306. process->ppid(),
  307. process->times_scheduled(),
  308. process->number_of_open_file_descriptors(),
  309. process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a",
  310. process->name().characters());
  311. }
  312. return builder.to_byte_buffer();
  313. }
  314. ByteBuffer procfs$inodes(SynthFSInode&)
  315. {
  316. extern HashTable<Inode*>& all_inodes();
  317. auto& vfs = VFS::the();
  318. StringBuilder builder;
  319. for (auto it : all_inodes()) {
  320. RetainPtr<Inode> inode = *it;
  321. String path = vfs.absolute_path(*inode);
  322. builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters());
  323. }
  324. return builder.to_byte_buffer();
  325. }
  326. struct SysVariableData final : public SynthFSInodeCustomData {
  327. virtual ~SysVariableData() override { }
  328. enum Type {
  329. Invalid,
  330. Boolean,
  331. };
  332. Type type { Invalid };
  333. Function<void()> change_callback;
  334. void* address;
  335. };
  336. static ByteBuffer read_sys_bool(SynthFSInode& inode)
  337. {
  338. ASSERT(inode.custom_data());
  339. auto buffer = ByteBuffer::create_uninitialized(2);
  340. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  341. ASSERT(custom_data.type == SysVariableData::Boolean);
  342. ASSERT(custom_data.address);
  343. buffer[0] = *reinterpret_cast<bool*>(custom_data.address) ? '1' : '0';
  344. buffer[1] = '\n';
  345. return buffer;
  346. }
  347. static ssize_t write_sys_bool(SynthFSInode& inode, const ByteBuffer& data)
  348. {
  349. ASSERT(inode.custom_data());
  350. if (data.size() >= 1 && (data[0] == '0' || data[0] == '1')) {
  351. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  352. ASSERT(custom_data.address);
  353. bool old_value = *reinterpret_cast<bool*>(custom_data.address);
  354. bool new_value = data[0] == '1';
  355. *reinterpret_cast<bool*>(custom_data.address) = new_value;
  356. if (old_value != new_value && custom_data.change_callback)
  357. custom_data.change_callback();
  358. }
  359. return data.size();
  360. }
  361. void ProcFS::add_sys_bool(String&& name, bool* var, Function<void()>&& change_callback)
  362. {
  363. auto file = create_generated_file(move(name), move(read_sys_bool), move(write_sys_bool));
  364. auto data = make<SysVariableData>();
  365. data->type = SysVariableData::Boolean;
  366. data->change_callback = move(change_callback);
  367. data->address = var;
  368. file->set_custom_data(move(data));
  369. InterruptDisabler disabler;
  370. add_file(move(file), m_sys_dir.index());
  371. }
  372. bool ProcFS::initialize()
  373. {
  374. SynthFS::initialize();
  375. add_file(create_generated_file("mm", procfs$mm));
  376. add_file(create_generated_file("mounts", procfs$mounts));
  377. add_file(create_generated_file("kmalloc", procfs$kmalloc));
  378. add_file(create_generated_file("summary", procfs$summary));
  379. add_file(create_generated_file("cpuinfo", procfs$cpuinfo));
  380. add_file(create_generated_file("inodes", procfs$inodes));
  381. add_file(create_generated_file("dmesg", procfs$dmesg));
  382. add_file(create_generated_file("self", procfs$self, 00120777));
  383. m_sys_dir = add_file(create_directory("sys"));
  384. return true;
  385. }
  386. const char* ProcFS::class_name() const
  387. {
  388. return "ProcFS";
  389. }