Process.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. #include "types.h"
  2. #include "Process.h"
  3. #include "kmalloc.h"
  4. #include "VGA.h"
  5. #include "StdLib.h"
  6. #include "i386.h"
  7. #include "system.h"
  8. #include <VirtualFileSystem/FileHandle.h>
  9. #include <VirtualFileSystem/VirtualFileSystem.h>
  10. #include <ELFLoader/ExecSpace.h>
  11. #include "MemoryManager.h"
  12. #include "errno.h"
  13. #include "i8253.h"
  14. #include "RTC.h"
  15. #include "ProcFileSystem.h"
  16. #include <AK/StdLib.h>
  17. //#define DEBUG_IO
  18. //#define TASK_DEBUG
  19. //#define SCHEDULER_DEBUG
  20. // FIXME: Only do a single validation for accesses that don't span multiple pages.
  21. // FIXME: Some places pass strlen(arg1) as arg2. This doesn't seem entirely perfect..
  22. #define VALIDATE_USER_READ(b, s) \
  23. do { \
  24. LinearAddress laddr((dword)(b)); \
  25. if (!validate_user_read(laddr) || !validate_user_read(laddr.offset((s) - 1))) \
  26. return -EFAULT; \
  27. } while(0)
  28. #define VALIDATE_USER_WRITE(b, s) \
  29. do { \
  30. LinearAddress laddr((dword)(b)); \
  31. if (!validate_user_write(laddr) || !validate_user_write(laddr.offset((s) - 1))) \
  32. return -EFAULT; \
  33. } while(0)
  34. static const DWORD defaultStackSize = 16384;
  35. Process* current;
  36. Process* s_kernelProcess;
  37. static pid_t next_pid;
  38. static InlineLinkedList<Process>* s_processes;
  39. static InlineLinkedList<Process>* s_deadProcesses;
  40. static String* s_hostname;
  41. static String& hostnameStorage(InterruptDisabler&)
  42. {
  43. ASSERT(s_hostname);
  44. return *s_hostname;
  45. }
  46. static String getHostname()
  47. {
  48. InterruptDisabler disabler;
  49. return hostnameStorage(disabler).isolatedCopy();
  50. }
  51. static bool contextSwitch(Process*);
  52. static void redoKernelProcessTSS()
  53. {
  54. if (!s_kernelProcess->selector())
  55. s_kernelProcess->setSelector(gdt_alloc_entry());
  56. auto& tssDescriptor = getGDTEntry(s_kernelProcess->selector());
  57. tssDescriptor.setBase(&s_kernelProcess->tss());
  58. tssDescriptor.setLimit(0xffff);
  59. tssDescriptor.dpl = 0;
  60. tssDescriptor.segment_present = 1;
  61. tssDescriptor.granularity = 1;
  62. tssDescriptor.zero = 0;
  63. tssDescriptor.operation_size = 1;
  64. tssDescriptor.descriptor_type = 0;
  65. tssDescriptor.type = 9;
  66. flushGDT();
  67. }
  68. void Process::prepForIRETToNewProcess()
  69. {
  70. redoKernelProcessTSS();
  71. s_kernelProcess->tss().backlink = current->selector();
  72. loadTaskRegister(s_kernelProcess->selector());
  73. }
  74. static void hlt_loop()
  75. {
  76. for (;;) {
  77. asm volatile("hlt");
  78. }
  79. }
  80. void Process::initialize()
  81. {
  82. current = nullptr;
  83. next_pid = 0;
  84. s_processes = new InlineLinkedList<Process>;
  85. s_deadProcesses = new InlineLinkedList<Process>;
  86. s_kernelProcess = Process::createKernelProcess(hlt_loop, "colonel");
  87. s_hostname = new String("birx");
  88. redoKernelProcessTSS();
  89. loadTaskRegister(s_kernelProcess->selector());
  90. }
  91. void Process::allocateLDT()
  92. {
  93. ASSERT(!m_tss.ldt);
  94. static const WORD numLDTEntries = 4;
  95. m_ldt_selector = gdt_alloc_entry();
  96. m_ldtEntries = new Descriptor[numLDTEntries];
  97. #if 0
  98. kprintf("new ldt selector = %x\n", m_ldt_selector);
  99. kprintf("new ldt table at = %p\n", m_ldtEntries);
  100. kprintf("new ldt table size = %u\n", (numLDTEntries * 8) - 1);
  101. #endif
  102. Descriptor& ldt = getGDTEntry(m_ldt_selector);
  103. ldt.setBase(m_ldtEntries);
  104. ldt.setLimit(numLDTEntries * 8 - 1);
  105. ldt.dpl = 0;
  106. ldt.segment_present = 1;
  107. ldt.granularity = 0;
  108. ldt.zero = 0;
  109. ldt.operation_size = 1;
  110. ldt.descriptor_type = 0;
  111. ldt.type = Descriptor::LDT;
  112. m_tss.ldt = m_ldt_selector;
  113. }
  114. Vector<Process*> Process::allProcesses()
  115. {
  116. InterruptDisabler disabler;
  117. Vector<Process*> processes;
  118. processes.ensureCapacity(s_processes->sizeSlow());
  119. for (auto* process = s_processes->head(); process; process = process->next())
  120. processes.append(process);
  121. return processes;
  122. }
  123. Region* Process::allocateRegion(size_t size, String&& name)
  124. {
  125. // FIXME: This needs sanity checks. What if this overlaps existing regions?
  126. auto zone = MM.createZone(size);
  127. ASSERT(zone);
  128. m_regions.append(adopt(*new Region(m_nextRegion, size, move(zone), move(name))));
  129. m_nextRegion = m_nextRegion.offset(size).offset(16384);
  130. MM.mapRegion(*this, *m_regions.last());
  131. return m_regions.last().ptr();
  132. }
  133. bool Process::deallocateRegion(Region& region)
  134. {
  135. InterruptDisabler disabler;
  136. for (size_t i = 0; i < m_regions.size(); ++i) {
  137. if (m_regions[i].ptr() == &region) {
  138. MM.unmapRegion(*this, region);
  139. m_regions.remove(i);
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. Region* Process::regionFromRange(LinearAddress laddr, size_t size)
  146. {
  147. for (auto& region : m_regions) {
  148. if (region->linearAddress == laddr && region->size == size)
  149. return region.ptr();
  150. }
  151. return nullptr;
  152. }
  153. int Process::sys$set_mmap_name(void* addr, size_t size, const char* name)
  154. {
  155. VALIDATE_USER_READ(name, strlen(name));
  156. auto* region = regionFromRange(LinearAddress((dword)addr), size);
  157. if (!region)
  158. return -EINVAL;
  159. region->name = name;
  160. return 0;
  161. }
  162. void* Process::sys$mmap(void* addr, size_t size)
  163. {
  164. InterruptDisabler disabler;
  165. // FIXME: Implement mapping at a client-preferred address.
  166. ASSERT(addr == nullptr);
  167. auto* region = allocateRegion(size, "mmap");
  168. if (!region)
  169. return (void*)-1;
  170. MM.mapRegion(*this, *region);
  171. return (void*)region->linearAddress.get();
  172. }
  173. int Process::sys$munmap(void* addr, size_t size)
  174. {
  175. InterruptDisabler disabler;
  176. auto* region = regionFromRange(LinearAddress((dword)addr), size);
  177. if (!region)
  178. return -1;
  179. if (!deallocateRegion(*region))
  180. return -1;
  181. return 0;
  182. }
  183. int Process::sys$gethostname(char* buffer, size_t size)
  184. {
  185. VALIDATE_USER_WRITE(buffer, size);
  186. auto hostname = getHostname();
  187. if (size < (hostname.length() + 1))
  188. return -ENAMETOOLONG;
  189. memcpy(buffer, hostname.characters(), size);
  190. return 0;
  191. }
  192. int Process::sys$spawn(const char* path, const char** args)
  193. {
  194. if (args) {
  195. for (size_t i = 0; args[i]; ++i) {
  196. VALIDATE_USER_READ(args[i], strlen(args[i]));
  197. }
  198. }
  199. int error = 0;
  200. auto* child = Process::createUserProcess(path, m_uid, m_gid, m_pid, error, args, m_tty);
  201. if (child)
  202. return child->pid();
  203. return error;
  204. }
  205. Process* Process::createUserProcess(const String& path, uid_t uid, gid_t gid, pid_t parentPID, int& error, const char** args, TTY* tty)
  206. {
  207. auto parts = path.split('/');
  208. if (parts.isEmpty()) {
  209. error = -ENOENT;
  210. return nullptr;
  211. }
  212. RetainPtr<VirtualFileSystem::Node> cwd;
  213. {
  214. InterruptDisabler disabler;
  215. if (auto* parentProcess = Process::fromPID(parentPID))
  216. cwd = parentProcess->m_cwd.copyRef();
  217. if (!cwd)
  218. cwd = VirtualFileSystem::the().root();
  219. }
  220. auto handle = VirtualFileSystem::the().open(path, error, 0, cwd ? cwd->inode : InodeIdentifier());
  221. if (!handle)
  222. return nullptr;
  223. if (!handle->metadata().mayExecute(uid, gid)) {
  224. error = -EACCES;
  225. return nullptr;
  226. }
  227. auto elfData = handle->readEntireFile();
  228. if (!elfData) {
  229. error = -EIO; // FIXME: Get a more detailed error from VFS.
  230. return nullptr;
  231. }
  232. Vector<String> processArguments;
  233. if (args) {
  234. for (size_t i = 0; args[i]; ++i) {
  235. processArguments.append(args[i]);
  236. }
  237. } else {
  238. processArguments.append(parts.last());
  239. }
  240. Vector<String> processEnvironment;
  241. processEnvironment.append("PATH=/bin");
  242. processEnvironment.append("SHELL=/bin/sh");
  243. processEnvironment.append("TERM=console");
  244. processEnvironment.append("HOME=/");
  245. auto* t = new Process(parts.takeLast(), uid, gid, parentPID, Ring3, move(cwd), handle->vnode(), tty);
  246. t->m_arguments = move(processArguments);
  247. t->m_initialEnvironment = move(processEnvironment);
  248. ExecSpace space;
  249. Region* region = nullptr;
  250. byte* region_alias = nullptr;
  251. KernelPagingScope pagingScope;
  252. space.hookableAlloc = [&] (const String& name, size_t size) {
  253. if (!size)
  254. return (void*)nullptr;
  255. size = ((size / 4096) + 1) * 4096; // FIXME: Use ceil_div?
  256. region = t->allocateRegion(size, String(name));
  257. ASSERT(region);
  258. region_alias = MM.create_kernel_alias_for_region(*region);
  259. return (void*)region_alias;
  260. };
  261. bool success = space.loadELF(move(elfData));
  262. if (!success) {
  263. if (region)
  264. MM.remove_kernel_alias_for_region(*region, region_alias);
  265. delete t;
  266. kprintf("Failure loading ELF %s\n", path.characters());
  267. error = -ENOEXEC;
  268. return nullptr;
  269. }
  270. space.forEachArea([&] (const String& name, dword offset, size_t size, LinearAddress laddr) {
  271. if (laddr.isNull())
  272. return;
  273. dword roundedOffset = offset & 0xfffff000;
  274. size_t roundedSize = 4096 * ceilDiv((offset - roundedOffset) + size, 4096u);
  275. LinearAddress roundedLaddr = laddr;
  276. roundedLaddr.mask(0xfffff000);
  277. t->m_subregions.append(make<Subregion>(*region, roundedOffset, roundedSize, roundedLaddr, String(name)));
  278. #ifdef SUBREGION_DEBUG
  279. kprintf(" req subregion %s (offset: %u, size: %u) @ %p\n", name.characters(), offset, size, laddr.get());
  280. kprintf("actual subregion %s (offset: %u, size: %u) @ %p\n", name.characters(), roundedOffset, roundedSize, roundedLaddr.get());
  281. #endif
  282. MM.mapSubregion(*t, *t->m_subregions.last());
  283. });
  284. t->m_tss.eip = (dword)space.symbolPtr("_start");
  285. if (!t->m_tss.eip) {
  286. // FIXME: This is ugly. If we need to do this, it should be at a different level.
  287. if (region)
  288. MM.remove_kernel_alias_for_region(*region, region_alias);
  289. delete t;
  290. error = -ENOEXEC;
  291. return nullptr;
  292. }
  293. ASSERT(region);
  294. MM.remove_kernel_alias_for_region(*region, region_alias);
  295. ProcFileSystem::the().addProcess(*t);
  296. s_processes->prepend(t);
  297. system.nprocess++;
  298. #ifdef TASK_DEBUG
  299. kprintf("Process %u (%s) spawned @ %p\n", t->pid(), t->name().characters(), t->m_tss.eip);
  300. #endif
  301. error = 0;
  302. return t;
  303. }
  304. int Process::sys$get_environment(char*** environ)
  305. {
  306. auto* region = allocateRegion(4096, "environ");
  307. if (!region)
  308. return -ENOMEM;
  309. MM.mapRegion(*this, *region);
  310. char* envpage = (char*)region->linearAddress.get();
  311. *environ = (char**)envpage;
  312. char* bufptr = envpage + (sizeof(char*) * (m_initialEnvironment.size() + 1));
  313. for (size_t i = 0; i < m_initialEnvironment.size(); ++i) {
  314. (*environ)[i] = bufptr;
  315. memcpy(bufptr, m_initialEnvironment[i].characters(), m_initialEnvironment[i].length());
  316. bufptr += m_initialEnvironment[i].length();
  317. *(bufptr++) = '\0';
  318. }
  319. (*environ)[m_initialEnvironment.size()] = nullptr;
  320. return 0;
  321. }
  322. int Process::sys$get_arguments(int* argc, char*** argv)
  323. {
  324. auto* region = allocateRegion(4096, "argv");
  325. if (!region)
  326. return -ENOMEM;
  327. MM.mapRegion(*this, *region);
  328. char* argpage = (char*)region->linearAddress.get();
  329. *argc = m_arguments.size();
  330. *argv = (char**)argpage;
  331. char* bufptr = argpage + (sizeof(char*) * m_arguments.size());
  332. for (size_t i = 0; i < m_arguments.size(); ++i) {
  333. (*argv)[i] = bufptr;
  334. memcpy(bufptr, m_arguments[i].characters(), m_arguments[i].length());
  335. bufptr += m_arguments[i].length();
  336. *(bufptr++) = '\0';
  337. }
  338. return 0;
  339. }
  340. Process* Process::createKernelProcess(void (*e)(), String&& name)
  341. {
  342. auto* process = new Process(move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
  343. process->m_tss.eip = (dword)e;
  344. if (process->pid() != 0) {
  345. InterruptDisabler disabler;
  346. s_processes->prepend(process);
  347. system.nprocess++;
  348. ProcFileSystem::the().addProcess(*process);
  349. #ifdef TASK_DEBUG
  350. kprintf("Kernel process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), process->m_tss.eip);
  351. #endif
  352. }
  353. return process;
  354. }
  355. Process::Process(String&& name, uid_t uid, gid_t gid, pid_t parentPID, RingLevel ring, RetainPtr<VirtualFileSystem::Node>&& cwd, RetainPtr<VirtualFileSystem::Node>&& executable, TTY* tty)
  356. : m_name(move(name))
  357. , m_pid(next_pid++)
  358. , m_uid(uid)
  359. , m_gid(gid)
  360. , m_state(Runnable)
  361. , m_ring(ring)
  362. , m_cwd(move(cwd))
  363. , m_executable(move(executable))
  364. , m_tty(tty)
  365. , m_parentPID(parentPID)
  366. {
  367. m_pageDirectory = (dword*)kmalloc_page_aligned(4096);
  368. MM.populate_page_directory(*this);
  369. m_file_descriptors.resize(m_max_open_file_descriptors);
  370. if (tty) {
  371. m_file_descriptors[0] = tty->open(O_RDONLY);
  372. m_file_descriptors[1] = tty->open(O_WRONLY);
  373. m_file_descriptors[2] = tty->open(O_WRONLY);
  374. }
  375. m_nextRegion = LinearAddress(0x10000000);
  376. memset(&m_tss, 0, sizeof(m_tss));
  377. if (isRing3()) {
  378. memset(&m_ldtEntries, 0, sizeof(m_ldtEntries));
  379. allocateLDT();
  380. }
  381. // Only IF is set when a process boots.
  382. m_tss.eflags = 0x0202;
  383. word cs, ds, ss;
  384. if (isRing0()) {
  385. cs = 0x08;
  386. ds = 0x10;
  387. ss = 0x10;
  388. } else {
  389. cs = 0x1b;
  390. ds = 0x23;
  391. ss = 0x23;
  392. }
  393. m_tss.ds = ds;
  394. m_tss.es = ds;
  395. m_tss.fs = ds;
  396. m_tss.gs = ds;
  397. m_tss.ss = ss;
  398. m_tss.cs = cs;
  399. m_tss.cr3 = (dword)m_pageDirectory;
  400. if (isRing0()) {
  401. // FIXME: This memory is leaked.
  402. // But uh, there's also no kernel process termination, so I guess it's not technically leaked...
  403. dword stackBottom = (dword)kmalloc_eternal(defaultStackSize);
  404. m_stackTop0 = (stackBottom + defaultStackSize) & 0xffffff8;
  405. m_tss.esp = m_stackTop0;
  406. } else {
  407. auto* region = allocateRegion(defaultStackSize, "stack");
  408. ASSERT(region);
  409. m_stackTop3 = region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  410. m_tss.esp = m_stackTop3;
  411. }
  412. if (isRing3()) {
  413. // Ring3 processes need a separate stack for Ring0.
  414. m_kernelStack = kmalloc(defaultStackSize);
  415. m_stackTop0 = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8;
  416. m_tss.ss0 = 0x10;
  417. m_tss.esp0 = m_stackTop0;
  418. }
  419. // HACK: Ring2 SS in the TSS is the current PID.
  420. m_tss.ss2 = m_pid;
  421. m_farPtr.offset = 0x98765432;
  422. }
  423. Process::~Process()
  424. {
  425. InterruptDisabler disabler;
  426. ProcFileSystem::the().removeProcess(*this);
  427. system.nprocess--;
  428. if (isRing3()) {
  429. delete [] m_ldtEntries;
  430. m_ldtEntries = nullptr;
  431. gdt_free_entry(m_ldt_selector);
  432. }
  433. gdt_free_entry(selector());
  434. if (m_kernelStack) {
  435. kfree(m_kernelStack);
  436. m_kernelStack = nullptr;
  437. }
  438. }
  439. void Process::dumpRegions()
  440. {
  441. kprintf("Process %s(%u) regions:\n", name().characters(), pid());
  442. kprintf("BEGIN END SIZE NAME\n");
  443. for (auto& region : m_regions) {
  444. kprintf("%x -- %x %x %s\n",
  445. region->linearAddress.get(),
  446. region->linearAddress.offset(region->size - 1).get(),
  447. region->size,
  448. region->name.characters());
  449. }
  450. kprintf("Process %s(%u) subregions:\n", name().characters(), pid());
  451. kprintf("REGION OFFSET BEGIN END SIZE NAME\n");
  452. for (auto& subregion : m_subregions) {
  453. kprintf("%x %x %x -- %x %x %s\n",
  454. subregion->region->linearAddress.get(),
  455. subregion->offset,
  456. subregion->linearAddress.get(),
  457. subregion->linearAddress.offset(subregion->size - 1).get(),
  458. subregion->size,
  459. subregion->name.characters());
  460. }
  461. }
  462. void Process::notify_waiters(pid_t waitee, int exit_status, int signal)
  463. {
  464. ASSERT_INTERRUPTS_DISABLED();
  465. for (auto* process = s_processes->head(); process; process = process->next()) {
  466. if (process->waitee() == waitee)
  467. process->m_waiteeStatus = (exit_status << 8) | (signal);
  468. }
  469. }
  470. void Process::sys$exit(int status)
  471. {
  472. cli();
  473. #ifdef TASK_DEBUG
  474. kprintf("sys$exit: %s(%u) exit with status %d\n", name().characters(), pid(), status);
  475. #endif
  476. set_state(Exiting);
  477. s_processes->remove(this);
  478. notify_waiters(m_pid, status, 0);
  479. if (!scheduleNewProcess()) {
  480. kprintf("Process::sys$exit: Failed to schedule a new process :(\n");
  481. HANG;
  482. }
  483. s_deadProcesses->append(this);
  484. switchNow();
  485. }
  486. void Process::murder(int signal)
  487. {
  488. ASSERT_INTERRUPTS_DISABLED();
  489. bool wasCurrent = current == this;
  490. set_state(Exiting);
  491. s_processes->remove(this);
  492. notify_waiters(m_pid, 0, signal);
  493. if (wasCurrent) {
  494. kprintf("Current process committing suicide!\n");
  495. if (!scheduleNewProcess()) {
  496. kprintf("Process::murder: Failed to schedule a new process :(\n");
  497. HANG;
  498. }
  499. }
  500. s_deadProcesses->append(this);
  501. if (wasCurrent)
  502. switchNow();
  503. }
  504. void Process::processDidCrash(Process* crashedProcess)
  505. {
  506. ASSERT_INTERRUPTS_DISABLED();
  507. if (crashedProcess->state() == Crashing) {
  508. kprintf("Double crash :(\n");
  509. HANG;
  510. }
  511. crashedProcess->set_state(Crashing);
  512. crashedProcess->dumpRegions();
  513. s_processes->remove(crashedProcess);
  514. notify_waiters(crashedProcess->m_pid, 0, SIGSEGV);
  515. if (!scheduleNewProcess()) {
  516. kprintf("Process::processDidCrash: Failed to schedule a new process :(\n");
  517. HANG;
  518. }
  519. s_deadProcesses->append(crashedProcess);
  520. switchNow();
  521. }
  522. void Process::doHouseKeeping()
  523. {
  524. InterruptDisabler disabler;
  525. if (s_deadProcesses->isEmpty())
  526. return;
  527. Process* next = nullptr;
  528. for (auto* deadProcess = s_deadProcesses->head(); deadProcess; deadProcess = next) {
  529. next = deadProcess->next();
  530. delete deadProcess;
  531. }
  532. s_deadProcesses->clear();
  533. }
  534. void yield()
  535. {
  536. if (!current) {
  537. kprintf( "PANIC: yield() with !current" );
  538. HANG;
  539. }
  540. //kprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  541. InterruptDisabler disabler;
  542. if (!scheduleNewProcess())
  543. return;
  544. //kprintf("yield() jumping to new process: %x (%s)\n", current->farPtr().selector, current->name().characters());
  545. switchNow();
  546. }
  547. void switchNow()
  548. {
  549. Descriptor& descriptor = getGDTEntry(current->selector());
  550. descriptor.type = 9;
  551. flushGDT();
  552. asm("sti\n"
  553. "ljmp *(%%eax)\n"
  554. ::"a"(&current->farPtr())
  555. );
  556. }
  557. bool scheduleNewProcess()
  558. {
  559. ASSERT_INTERRUPTS_DISABLED();
  560. if (!current) {
  561. // XXX: The first ever context_switch() goes to the idle process.
  562. // This to setup a reliable place we can return to.
  563. return contextSwitch(Process::kernelProcess());
  564. }
  565. // Check and unblock processes whose wait conditions have been met.
  566. for (auto* process = s_processes->head(); process; process = process->next()) {
  567. if (process->state() == Process::BlockedSleep) {
  568. if (process->wakeupTime() <= system.uptime) {
  569. process->unblock();
  570. continue;
  571. }
  572. }
  573. if (process->state() == Process::BlockedWait) {
  574. if (!Process::fromPID(process->waitee())) {
  575. process->unblock();
  576. continue;
  577. }
  578. }
  579. if (process->state() == Process::BlockedRead) {
  580. ASSERT(process->m_fdBlockedOnRead != -1);
  581. if (process->m_file_descriptors[process->m_fdBlockedOnRead]->hasDataAvailableForRead()) {
  582. process->unblock();
  583. continue;
  584. }
  585. }
  586. }
  587. #ifdef SCHEDULER_DEBUG
  588. dbgprintf("Scheduler choices:\n");
  589. for (auto* process = s_processes->head(); process; process = process->next()) {
  590. //if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
  591. // continue;
  592. dbgprintf("%w %s(%u)\n", process->state(), process->name().characters(), process->pid());
  593. }
  594. #endif
  595. auto* prevHead = s_processes->head();
  596. for (;;) {
  597. // Move head to tail.
  598. s_processes->append(s_processes->removeHead());
  599. auto* process = s_processes->head();
  600. if (process->state() == Process::Runnable || process->state() == Process::Running) {
  601. #ifdef SCHEDULER_DEBUG
  602. dbgprintf("switch to %s(%u) (%p vs %p)\n", process->name().characters(), process->pid(), process, current);
  603. #endif
  604. return contextSwitch(process);
  605. }
  606. if (process == prevHead) {
  607. // Back at process_head, nothing wants to run.
  608. kprintf("Nothing wants to run!\n");
  609. kprintf("PID OWNER STATE NSCHED NAME\n");
  610. for (auto* process = s_processes->head(); process; process = process->next()) {
  611. kprintf("%w %w:%w %b %w %s\n",
  612. process->pid(),
  613. process->uid(),
  614. process->gid(),
  615. process->state(),
  616. process->timesScheduled(),
  617. process->name().characters());
  618. }
  619. kprintf("Switch to kernel process @ %w:%x\n", s_kernelProcess->tss().cs, s_kernelProcess->tss().eip);
  620. return contextSwitch(Process::kernelProcess());
  621. }
  622. }
  623. }
  624. static bool contextSwitch(Process* t)
  625. {
  626. t->setTicksLeft(5);
  627. t->didSchedule();
  628. if (current == t)
  629. return false;
  630. #ifdef SCHEDULER_DEBUG
  631. // Some sanity checking to force a crash earlier.
  632. auto csRPL = t->tss().cs & 3;
  633. auto ssRPL = t->tss().ss & 3;
  634. if (csRPL != ssRPL) {
  635. kprintf("Fuckup! Switching from %s(%u) to %s(%u) has RPL mismatch\n",
  636. current->name().characters(), current->pid(),
  637. t->name().characters(), t->pid()
  638. );
  639. kprintf("code: %w:%x\n", t->tss().cs, t->tss().eip);
  640. kprintf(" stk: %w:%x\n", t->tss().ss, t->tss().esp);
  641. ASSERT(csRPL == ssRPL);
  642. }
  643. #endif
  644. if (current) {
  645. // If the last process hasn't blocked (still marked as running),
  646. // mark it as runnable for the next round.
  647. if (current->state() == Process::Running)
  648. current->set_state(Process::Runnable);
  649. }
  650. current = t;
  651. t->set_state(Process::Running);
  652. if (!t->selector()) {
  653. t->setSelector(gdt_alloc_entry());
  654. auto& descriptor = getGDTEntry(t->selector());
  655. descriptor.setBase(&t->tss());
  656. descriptor.setLimit(0xffff);
  657. descriptor.dpl = 0;
  658. descriptor.segment_present = 1;
  659. descriptor.granularity = 1;
  660. descriptor.zero = 0;
  661. descriptor.operation_size = 1;
  662. descriptor.descriptor_type = 0;
  663. }
  664. auto& descriptor = getGDTEntry(t->selector());
  665. descriptor.type = 11; // Busy TSS
  666. flushGDT();
  667. return true;
  668. }
  669. Process* Process::fromPID(pid_t pid)
  670. {
  671. ASSERT_INTERRUPTS_DISABLED();
  672. for (auto* process = s_processes->head(); process; process = process->next()) {
  673. if (process->pid() == pid)
  674. return process;
  675. }
  676. return nullptr;
  677. }
  678. FileHandle* Process::fileHandleIfExists(int fd)
  679. {
  680. if (fd < 0)
  681. return nullptr;
  682. if ((unsigned)fd < m_file_descriptors.size())
  683. return m_file_descriptors[fd].ptr();
  684. return nullptr;
  685. }
  686. ssize_t Process::sys$get_dir_entries(int fd, void* buffer, size_t size)
  687. {
  688. VALIDATE_USER_WRITE(buffer, size);
  689. auto* handle = fileHandleIfExists(fd);
  690. if (!handle)
  691. return -EBADF;
  692. return handle->get_dir_entries((byte*)buffer, size);
  693. }
  694. int Process::sys$lseek(int fd, off_t offset, int whence)
  695. {
  696. auto* handle = fileHandleIfExists(fd);
  697. if (!handle)
  698. return -EBADF;
  699. return handle->seek(offset, whence);
  700. }
  701. int Process::sys$ttyname_r(int fd, char* buffer, size_t size)
  702. {
  703. VALIDATE_USER_WRITE(buffer, size);
  704. auto* handle = fileHandleIfExists(fd);
  705. if (!handle)
  706. return -EBADF;
  707. if (!handle->isTTY())
  708. return -ENOTTY;
  709. auto ttyName = handle->tty()->ttyName();
  710. if (size < ttyName.length() + 1)
  711. return -ERANGE;
  712. strcpy(buffer, ttyName.characters());
  713. return 0;
  714. }
  715. ssize_t Process::sys$write(int fd, const void* data, size_t size)
  716. {
  717. VALIDATE_USER_READ(data, size);
  718. #ifdef DEBUG_IO
  719. kprintf("Process::sys$write: called(%d, %p, %u)\n", fd, data, size);
  720. #endif
  721. auto* handle = fileHandleIfExists(fd);
  722. #ifdef DEBUG_IO
  723. kprintf("Process::sys$write: handle=%p\n", handle);
  724. #endif
  725. if (!handle)
  726. return -EBADF;
  727. auto nwritten = handle->write((const byte*)data, size);
  728. #ifdef DEBUG_IO
  729. kprintf("Process::sys$write: nwritten=%u\n", nwritten);
  730. #endif
  731. return nwritten;
  732. }
  733. ssize_t Process::sys$read(int fd, void* outbuf, size_t nread)
  734. {
  735. VALIDATE_USER_WRITE(outbuf, nread);
  736. #ifdef DEBUG_IO
  737. kprintf("Process::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread);
  738. #endif
  739. auto* handle = fileHandleIfExists(fd);
  740. #ifdef DEBUG_IO
  741. kprintf("Process::sys$read: handle=%p\n", handle);
  742. #endif
  743. if (!handle)
  744. return -EBADF;
  745. if (handle->isBlocking()) {
  746. if (!handle->hasDataAvailableForRead()) {
  747. m_fdBlockedOnRead = fd;
  748. block(BlockedRead);
  749. yield();
  750. }
  751. }
  752. nread = handle->read((byte*)outbuf, nread);
  753. #ifdef DEBUG_IO
  754. kprintf("Process::sys$read: nread=%u\n", nread);
  755. #endif
  756. return nread;
  757. }
  758. int Process::sys$close(int fd)
  759. {
  760. auto* handle = fileHandleIfExists(fd);
  761. if (!handle)
  762. return -EBADF;
  763. int rc = handle->close();
  764. m_file_descriptors[fd] = nullptr;
  765. return rc;
  766. }
  767. int Process::sys$lstat(const char* path, Unix::stat* statbuf)
  768. {
  769. VALIDATE_USER_WRITE(statbuf, sizeof(Unix::stat));
  770. int error;
  771. auto handle = VirtualFileSystem::the().open(move(path), error, O_NOFOLLOW_NOERROR, cwdInode());
  772. if (!handle)
  773. return error;
  774. handle->stat(statbuf);
  775. return 0;
  776. }
  777. int Process::sys$stat(const char* path, Unix::stat* statbuf)
  778. {
  779. VALIDATE_USER_WRITE(statbuf, sizeof(Unix::stat));
  780. int error;
  781. auto handle = VirtualFileSystem::the().open(move(path), error, 0, cwdInode());
  782. if (!handle)
  783. return error;
  784. handle->stat(statbuf);
  785. return 0;
  786. }
  787. int Process::sys$readlink(const char* path, char* buffer, size_t size)
  788. {
  789. VALIDATE_USER_READ(path, strlen(path));
  790. VALIDATE_USER_WRITE(buffer, size);
  791. int error;
  792. auto handle = VirtualFileSystem::the().open(path, error, O_RDONLY | O_NOFOLLOW_NOERROR, cwdInode());
  793. if (!handle)
  794. return error;
  795. if (!handle->metadata().isSymbolicLink())
  796. return -EINVAL;
  797. auto contents = handle->readEntireFile();
  798. if (!contents)
  799. return -EIO; // FIXME: Get a more detailed error from VFS.
  800. memcpy(buffer, contents.pointer(), min(size, contents.size()));
  801. if (contents.size() + 1 < size)
  802. buffer[contents.size()] = '\0';
  803. return 0;
  804. }
  805. int Process::sys$chdir(const char* path)
  806. {
  807. VALIDATE_USER_READ(path, strlen(path));
  808. int error;
  809. auto handle = VirtualFileSystem::the().open(path, error, 0, cwdInode());
  810. if (!handle)
  811. return error;
  812. if (!handle->isDirectory())
  813. return -ENOTDIR;
  814. m_cwd = handle->vnode();
  815. return 0;
  816. }
  817. int Process::sys$getcwd(char* buffer, size_t size)
  818. {
  819. VALIDATE_USER_WRITE(buffer, size);
  820. auto path = VirtualFileSystem::the().absolutePath(cwdInode());
  821. if (path.isNull())
  822. return -EINVAL;
  823. if (size < path.length() + 1)
  824. return -ERANGE;
  825. strcpy(buffer, path.characters());
  826. return -ENOTIMPL;
  827. }
  828. size_t Process::number_of_open_file_descriptors() const
  829. {
  830. size_t count = 0;
  831. for (auto& handle : m_file_descriptors) {
  832. if (handle)
  833. ++count;
  834. }
  835. return count;
  836. }
  837. int Process::sys$open(const char* path, int options)
  838. {
  839. #ifdef DEBUG_IO
  840. kprintf("Process::sys$open(): PID=%u, path=%s {%u}\n", m_pid, path, pathLength);
  841. #endif
  842. VALIDATE_USER_READ(path, strlen(path));
  843. if (number_of_open_file_descriptors() >= m_max_open_file_descriptors)
  844. return -EMFILE;
  845. int error;
  846. auto handle = VirtualFileSystem::the().open(path, error, options, cwdInode());
  847. if (!handle)
  848. return error;
  849. if (options & O_DIRECTORY && !handle->isDirectory())
  850. return -ENOTDIR; // FIXME: This should be handled by VFS::open.
  851. int fd = 0;
  852. for (; fd < m_max_open_file_descriptors; ++fd) {
  853. if (!m_file_descriptors[fd])
  854. break;
  855. }
  856. handle->setFD(fd);
  857. m_file_descriptors[fd] = move(handle);
  858. return fd;
  859. }
  860. int Process::sys$uname(utsname* buf)
  861. {
  862. VALIDATE_USER_WRITE(buf, sizeof(utsname));
  863. strcpy(buf->sysname, "Serenity");
  864. strcpy(buf->release, "1.0-dev");
  865. strcpy(buf->version, "FIXME");
  866. strcpy(buf->machine, "i386");
  867. strcpy(buf->nodename, getHostname().characters());
  868. return 0;
  869. }
  870. int Process::sys$kill(pid_t pid, int sig)
  871. {
  872. (void) sig;
  873. if (pid == 0) {
  874. // FIXME: Send to same-group processes.
  875. ASSERT(pid != 0);
  876. }
  877. if (pid == -1) {
  878. // FIXME: Send to all processes.
  879. ASSERT(pid != -1);
  880. }
  881. ASSERT(pid != current->pid()); // FIXME: Support this scenario.
  882. InterruptDisabler disabler;
  883. auto* peer = Process::fromPID(pid);
  884. if (!peer)
  885. return -ESRCH;
  886. if (sig == SIGKILL) {
  887. peer->murder(SIGKILL);
  888. return 0;
  889. } else {
  890. ASSERT_NOT_REACHED();
  891. }
  892. return -1;
  893. }
  894. int Process::sys$sleep(unsigned seconds)
  895. {
  896. if (!seconds)
  897. return 0;
  898. sleep(seconds * TICKS_PER_SECOND);
  899. return 0;
  900. }
  901. int Process::sys$gettimeofday(timeval* tv)
  902. {
  903. VALIDATE_USER_WRITE(tv, sizeof(tv));
  904. InterruptDisabler disabler;
  905. auto now = RTC::now();
  906. tv->tv_sec = now;
  907. tv->tv_usec = 0;
  908. return 0;
  909. }
  910. uid_t Process::sys$getuid()
  911. {
  912. return m_uid;
  913. }
  914. gid_t Process::sys$getgid()
  915. {
  916. return m_gid;
  917. }
  918. pid_t Process::sys$getpid()
  919. {
  920. return m_pid;
  921. }
  922. pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
  923. {
  924. if (wstatus)
  925. VALIDATE_USER_WRITE(wstatus, sizeof(int));
  926. InterruptDisabler disabler;
  927. if (!Process::fromPID(waitee))
  928. return -1;
  929. m_waitee = waitee;
  930. m_waiteeStatus = 0;
  931. block(BlockedWait);
  932. yield();
  933. if (wstatus)
  934. *wstatus = m_waiteeStatus;
  935. return m_waitee;
  936. }
  937. void Process::unblock()
  938. {
  939. ASSERT(m_state != Process::Runnable && m_state != Process::Running);
  940. system.nblocked--;
  941. m_state = Process::Runnable;
  942. }
  943. void Process::block(Process::State state)
  944. {
  945. ASSERT(current->state() == Process::Running);
  946. system.nblocked++;
  947. current->set_state(state);
  948. }
  949. void block(Process::State state)
  950. {
  951. current->block(state);
  952. yield();
  953. }
  954. void sleep(DWORD ticks)
  955. {
  956. ASSERT(current->state() == Process::Running);
  957. current->setWakeupTime(system.uptime + ticks);
  958. current->block(Process::BlockedSleep);
  959. yield();
  960. }
  961. Process* Process::kernelProcess()
  962. {
  963. ASSERT(s_kernelProcess);
  964. return s_kernelProcess;
  965. }
  966. Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n)
  967. : linearAddress(a)
  968. , size(s)
  969. , zone(move(z))
  970. , name(move(n))
  971. {
  972. }
  973. Region::~Region()
  974. {
  975. }
  976. Subregion::Subregion(Region& r, dword o, size_t s, LinearAddress l, String&& n)\
  977. : region(r)
  978. , offset(o)
  979. , size(s)
  980. , linearAddress(l)
  981. , name(move(n))
  982. {
  983. }
  984. Subregion::~Subregion()
  985. {
  986. }
  987. bool Process::isValidAddressForKernel(LinearAddress laddr) const
  988. {
  989. // We check extra carefully here since the first 4MB of the address space is identity-mapped.
  990. // This code allows access outside of the known used address ranges to get caught.
  991. InterruptDisabler disabler;
  992. if (laddr.get() >= ksyms().first().address && laddr.get() <= ksyms().last().address)
  993. return true;
  994. if (is_kmalloc_address((void*)laddr.get()))
  995. return true;
  996. return validate_user_read(laddr);
  997. }
  998. bool Process::validate_user_read(LinearAddress laddr) const
  999. {
  1000. InterruptDisabler disabler;
  1001. return MM.validate_user_read(*this, laddr);
  1002. }
  1003. bool Process::validate_user_write(LinearAddress laddr) const
  1004. {
  1005. InterruptDisabler disabler;
  1006. return MM.validate_user_write(*this, laddr);
  1007. }