ProcFileSystem.cpp 11 KB

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