Process.cpp 31 KB

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