Process.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. ProcessPagingScope pagingScope(*t);
  251. space.hookableAlloc = [&] (const String& name, size_t size) {
  252. if (!size)
  253. return (void*)nullptr;
  254. size = ((size / 4096) + 1) * 4096; // FIXME: Use ceil_div?
  255. region = t->allocateRegion(size, String(name));
  256. return (void*)region->linearAddress.get();
  257. };
  258. bool success = space.loadELF(move(elfData));
  259. if (!success) {
  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. delete t;
  283. error = -ENOEXEC;
  284. return nullptr;
  285. }
  286. ASSERT(region);
  287. ProcFileSystem::the().addProcess(*t);
  288. s_processes->prepend(t);
  289. system.nprocess++;
  290. #ifdef TASK_DEBUG
  291. kprintf("Process %u (%s) spawned @ %p\n", t->pid(), t->name().characters(), t->m_tss.eip);
  292. #endif
  293. error = 0;
  294. return t;
  295. }
  296. int Process::sys$get_environment(char*** environ)
  297. {
  298. auto* region = allocateRegion(4096, "environ");
  299. if (!region)
  300. return -ENOMEM;
  301. MM.mapRegion(*this, *region);
  302. char* envpage = (char*)region->linearAddress.get();
  303. *environ = (char**)envpage;
  304. char* bufptr = envpage + (sizeof(char*) * (m_initialEnvironment.size() + 1));
  305. for (size_t i = 0; i < m_initialEnvironment.size(); ++i) {
  306. (*environ)[i] = bufptr;
  307. memcpy(bufptr, m_initialEnvironment[i].characters(), m_initialEnvironment[i].length());
  308. bufptr += m_initialEnvironment[i].length();
  309. *(bufptr++) = '\0';
  310. }
  311. (*environ)[m_initialEnvironment.size()] = nullptr;
  312. return 0;
  313. }
  314. int Process::sys$get_arguments(int* argc, char*** argv)
  315. {
  316. auto* region = allocateRegion(4096, "argv");
  317. if (!region)
  318. return -ENOMEM;
  319. MM.mapRegion(*this, *region);
  320. char* argpage = (char*)region->linearAddress.get();
  321. *argc = m_arguments.size();
  322. *argv = (char**)argpage;
  323. char* bufptr = argpage + (sizeof(char*) * m_arguments.size());
  324. for (size_t i = 0; i < m_arguments.size(); ++i) {
  325. (*argv)[i] = bufptr;
  326. memcpy(bufptr, m_arguments[i].characters(), m_arguments[i].length());
  327. bufptr += m_arguments[i].length();
  328. *(bufptr++) = '\0';
  329. }
  330. return 0;
  331. }
  332. Process* Process::createKernelProcess(void (*e)(), String&& name)
  333. {
  334. auto* process = new Process(move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
  335. process->m_tss.eip = (dword)e;
  336. if (process->pid() != 0) {
  337. InterruptDisabler disabler;
  338. s_processes->prepend(process);
  339. system.nprocess++;
  340. ProcFileSystem::the().addProcess(*process);
  341. #ifdef TASK_DEBUG
  342. kprintf("Kernel process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), process->m_tss.eip);
  343. #endif
  344. }
  345. return process;
  346. }
  347. 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)
  348. : m_name(move(name))
  349. , m_pid(next_pid++)
  350. , m_uid(uid)
  351. , m_gid(gid)
  352. , m_state(Runnable)
  353. , m_ring(ring)
  354. , m_cwd(move(cwd))
  355. , m_executable(move(executable))
  356. , m_tty(tty)
  357. , m_parentPID(parentPID)
  358. {
  359. m_page_directory = (PageDirectory*)kmalloc_page_aligned(sizeof(PageDirectory));
  360. MM.populate_page_directory(*this);
  361. m_file_descriptors.resize(m_max_open_file_descriptors);
  362. if (tty) {
  363. m_file_descriptors[0] = tty->open(O_RDONLY);
  364. m_file_descriptors[1] = tty->open(O_WRONLY);
  365. m_file_descriptors[2] = tty->open(O_WRONLY);
  366. }
  367. m_nextRegion = LinearAddress(0x10000000);
  368. memset(&m_tss, 0, sizeof(m_tss));
  369. if (isRing3()) {
  370. memset(&m_ldtEntries, 0, sizeof(m_ldtEntries));
  371. allocateLDT();
  372. }
  373. // Only IF is set when a process boots.
  374. m_tss.eflags = 0x0202;
  375. word cs, ds, ss;
  376. if (isRing0()) {
  377. cs = 0x08;
  378. ds = 0x10;
  379. ss = 0x10;
  380. } else {
  381. cs = 0x1b;
  382. ds = 0x23;
  383. ss = 0x23;
  384. }
  385. m_tss.ds = ds;
  386. m_tss.es = ds;
  387. m_tss.fs = ds;
  388. m_tss.gs = ds;
  389. m_tss.ss = ss;
  390. m_tss.cs = cs;
  391. m_tss.cr3 = (dword)m_page_directory;
  392. if (isRing0()) {
  393. // FIXME: This memory is leaked.
  394. // But uh, there's also no kernel process termination, so I guess it's not technically leaked...
  395. dword stackBottom = (dword)kmalloc_eternal(defaultStackSize);
  396. m_stackTop0 = (stackBottom + defaultStackSize) & 0xffffff8;
  397. m_tss.esp = m_stackTop0;
  398. } else {
  399. auto* region = allocateRegion(defaultStackSize, "stack");
  400. ASSERT(region);
  401. m_stackTop3 = region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  402. m_tss.esp = m_stackTop3;
  403. }
  404. if (isRing3()) {
  405. // Ring3 processes need a separate stack for Ring0.
  406. m_kernelStack = kmalloc(defaultStackSize);
  407. m_stackTop0 = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8;
  408. m_tss.ss0 = 0x10;
  409. m_tss.esp0 = m_stackTop0;
  410. }
  411. // HACK: Ring2 SS in the TSS is the current PID.
  412. m_tss.ss2 = m_pid;
  413. m_farPtr.offset = 0x98765432;
  414. }
  415. Process::~Process()
  416. {
  417. InterruptDisabler disabler;
  418. ProcFileSystem::the().removeProcess(*this);
  419. system.nprocess--;
  420. if (isRing3()) {
  421. delete [] m_ldtEntries;
  422. m_ldtEntries = nullptr;
  423. gdt_free_entry(m_ldt_selector);
  424. }
  425. gdt_free_entry(selector());
  426. if (m_kernelStack) {
  427. kfree(m_kernelStack);
  428. m_kernelStack = nullptr;
  429. }
  430. MM.release_page_directory(*this);
  431. }
  432. void Process::dumpRegions()
  433. {
  434. kprintf("Process %s(%u) regions:\n", name().characters(), pid());
  435. kprintf("BEGIN END SIZE NAME\n");
  436. for (auto& region : m_regions) {
  437. kprintf("%x -- %x %x %s\n",
  438. region->linearAddress.get(),
  439. region->linearAddress.offset(region->size - 1).get(),
  440. region->size,
  441. region->name.characters());
  442. }
  443. kprintf("Process %s(%u) subregions:\n", name().characters(), pid());
  444. kprintf("REGION OFFSET BEGIN END SIZE NAME\n");
  445. for (auto& subregion : m_subregions) {
  446. kprintf("%x %x %x -- %x %x %s\n",
  447. subregion->region->linearAddress.get(),
  448. subregion->offset,
  449. subregion->linearAddress.get(),
  450. subregion->linearAddress.offset(subregion->size - 1).get(),
  451. subregion->size,
  452. subregion->name.characters());
  453. }
  454. }
  455. void Process::notify_waiters(pid_t waitee, int exit_status, int signal)
  456. {
  457. ASSERT_INTERRUPTS_DISABLED();
  458. for (auto* process = s_processes->head(); process; process = process->next()) {
  459. if (process->waitee() == waitee)
  460. process->m_waiteeStatus = (exit_status << 8) | (signal);
  461. }
  462. }
  463. void Process::sys$exit(int status)
  464. {
  465. cli();
  466. #ifdef TASK_DEBUG
  467. kprintf("sys$exit: %s(%u) exit with status %d\n", name().characters(), pid(), status);
  468. #endif
  469. set_state(Exiting);
  470. s_processes->remove(this);
  471. notify_waiters(m_pid, status, 0);
  472. if (!scheduleNewProcess()) {
  473. kprintf("Process::sys$exit: Failed to schedule a new process :(\n");
  474. HANG;
  475. }
  476. s_deadProcesses->append(this);
  477. switchNow();
  478. }
  479. void Process::murder(int signal)
  480. {
  481. ASSERT_INTERRUPTS_DISABLED();
  482. bool wasCurrent = current == this;
  483. set_state(Exiting);
  484. s_processes->remove(this);
  485. notify_waiters(m_pid, 0, signal);
  486. if (wasCurrent) {
  487. kprintf("Current process committing suicide!\n");
  488. if (!scheduleNewProcess()) {
  489. kprintf("Process::murder: Failed to schedule a new process :(\n");
  490. HANG;
  491. }
  492. }
  493. s_deadProcesses->append(this);
  494. if (wasCurrent)
  495. switchNow();
  496. }
  497. void Process::processDidCrash(Process* crashedProcess)
  498. {
  499. ASSERT_INTERRUPTS_DISABLED();
  500. if (crashedProcess->state() == Crashing) {
  501. kprintf("Double crash :(\n");
  502. HANG;
  503. }
  504. crashedProcess->set_state(Crashing);
  505. crashedProcess->dumpRegions();
  506. s_processes->remove(crashedProcess);
  507. notify_waiters(crashedProcess->m_pid, 0, SIGSEGV);
  508. if (!scheduleNewProcess()) {
  509. kprintf("Process::processDidCrash: Failed to schedule a new process :(\n");
  510. HANG;
  511. }
  512. s_deadProcesses->append(crashedProcess);
  513. switchNow();
  514. }
  515. void Process::doHouseKeeping()
  516. {
  517. if (s_deadProcesses->isEmpty())
  518. return;
  519. InterruptDisabler disabler;
  520. Process* next = nullptr;
  521. for (auto* deadProcess = s_deadProcesses->head(); deadProcess; deadProcess = next) {
  522. next = deadProcess->next();
  523. delete deadProcess;
  524. }
  525. s_deadProcesses->clear();
  526. }
  527. void yield()
  528. {
  529. if (!current) {
  530. kprintf( "PANIC: yield() with !current" );
  531. HANG;
  532. }
  533. //kprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  534. InterruptDisabler disabler;
  535. if (!scheduleNewProcess())
  536. return;
  537. //kprintf("yield() jumping to new process: %x (%s)\n", current->farPtr().selector, current->name().characters());
  538. switchNow();
  539. }
  540. void switchNow()
  541. {
  542. Descriptor& descriptor = getGDTEntry(current->selector());
  543. descriptor.type = 9;
  544. flushGDT();
  545. asm("sti\n"
  546. "ljmp *(%%eax)\n"
  547. ::"a"(&current->farPtr())
  548. );
  549. }
  550. bool scheduleNewProcess()
  551. {
  552. ASSERT_INTERRUPTS_DISABLED();
  553. if (!current) {
  554. // XXX: The first ever context_switch() goes to the idle process.
  555. // This to setup a reliable place we can return to.
  556. return contextSwitch(Process::kernelProcess());
  557. }
  558. // Check and unblock processes whose wait conditions have been met.
  559. for (auto* process = s_processes->head(); process; process = process->next()) {
  560. if (process->state() == Process::BlockedSleep) {
  561. if (process->wakeupTime() <= system.uptime) {
  562. process->unblock();
  563. continue;
  564. }
  565. }
  566. if (process->state() == Process::BlockedWait) {
  567. if (!Process::fromPID(process->waitee())) {
  568. process->unblock();
  569. continue;
  570. }
  571. }
  572. if (process->state() == Process::BlockedRead) {
  573. ASSERT(process->m_fdBlockedOnRead != -1);
  574. if (process->m_file_descriptors[process->m_fdBlockedOnRead]->hasDataAvailableForRead()) {
  575. process->unblock();
  576. continue;
  577. }
  578. }
  579. }
  580. #ifdef SCHEDULER_DEBUG
  581. dbgprintf("Scheduler choices:\n");
  582. for (auto* process = s_processes->head(); process; process = process->next()) {
  583. //if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
  584. // continue;
  585. dbgprintf("%w %s(%u)\n", process->state(), process->name().characters(), process->pid());
  586. }
  587. #endif
  588. auto* prevHead = s_processes->head();
  589. for (;;) {
  590. // Move head to tail.
  591. s_processes->append(s_processes->removeHead());
  592. auto* process = s_processes->head();
  593. if (process->state() == Process::Runnable || process->state() == Process::Running) {
  594. #ifdef SCHEDULER_DEBUG
  595. dbgprintf("switch to %s(%u) (%p vs %p)\n", process->name().characters(), process->pid(), process, current);
  596. #endif
  597. return contextSwitch(process);
  598. }
  599. if (process == prevHead) {
  600. // Back at process_head, nothing wants to run.
  601. kprintf("Nothing wants to run!\n");
  602. kprintf("PID OWNER STATE NSCHED NAME\n");
  603. for (auto* process = s_processes->head(); process; process = process->next()) {
  604. kprintf("%w %w:%w %b %w %s\n",
  605. process->pid(),
  606. process->uid(),
  607. process->gid(),
  608. process->state(),
  609. process->timesScheduled(),
  610. process->name().characters());
  611. }
  612. kprintf("Switch to kernel process @ %w:%x\n", s_kernelProcess->tss().cs, s_kernelProcess->tss().eip);
  613. return contextSwitch(Process::kernelProcess());
  614. }
  615. }
  616. }
  617. static bool contextSwitch(Process* t)
  618. {
  619. t->setTicksLeft(5);
  620. t->didSchedule();
  621. if (current == t)
  622. return false;
  623. #ifdef SCHEDULER_DEBUG
  624. // Some sanity checking to force a crash earlier.
  625. auto csRPL = t->tss().cs & 3;
  626. auto ssRPL = t->tss().ss & 3;
  627. if (csRPL != ssRPL) {
  628. kprintf("Fuckup! Switching from %s(%u) to %s(%u) has RPL mismatch\n",
  629. current->name().characters(), current->pid(),
  630. t->name().characters(), t->pid()
  631. );
  632. kprintf("code: %w:%x\n", t->tss().cs, t->tss().eip);
  633. kprintf(" stk: %w:%x\n", t->tss().ss, t->tss().esp);
  634. ASSERT(csRPL == ssRPL);
  635. }
  636. #endif
  637. if (current) {
  638. // If the last process hasn't blocked (still marked as running),
  639. // mark it as runnable for the next round.
  640. if (current->state() == Process::Running)
  641. current->set_state(Process::Runnable);
  642. }
  643. current = t;
  644. t->set_state(Process::Running);
  645. if (!t->selector()) {
  646. t->setSelector(gdt_alloc_entry());
  647. auto& descriptor = getGDTEntry(t->selector());
  648. descriptor.setBase(&t->tss());
  649. descriptor.setLimit(0xffff);
  650. descriptor.dpl = 0;
  651. descriptor.segment_present = 1;
  652. descriptor.granularity = 1;
  653. descriptor.zero = 0;
  654. descriptor.operation_size = 1;
  655. descriptor.descriptor_type = 0;
  656. }
  657. auto& descriptor = getGDTEntry(t->selector());
  658. descriptor.type = 11; // Busy TSS
  659. flushGDT();
  660. return true;
  661. }
  662. Process* Process::fromPID(pid_t pid)
  663. {
  664. ASSERT_INTERRUPTS_DISABLED();
  665. for (auto* process = s_processes->head(); process; process = process->next()) {
  666. if (process->pid() == pid)
  667. return process;
  668. }
  669. return nullptr;
  670. }
  671. FileHandle* Process::fileHandleIfExists(int fd)
  672. {
  673. if (fd < 0)
  674. return nullptr;
  675. if ((unsigned)fd < m_file_descriptors.size())
  676. return m_file_descriptors[fd].ptr();
  677. return nullptr;
  678. }
  679. ssize_t Process::sys$get_dir_entries(int fd, void* buffer, size_t size)
  680. {
  681. VALIDATE_USER_WRITE(buffer, size);
  682. auto* handle = fileHandleIfExists(fd);
  683. if (!handle)
  684. return -EBADF;
  685. return handle->get_dir_entries((byte*)buffer, size);
  686. }
  687. int Process::sys$lseek(int fd, off_t offset, int whence)
  688. {
  689. auto* handle = fileHandleIfExists(fd);
  690. if (!handle)
  691. return -EBADF;
  692. return handle->seek(offset, whence);
  693. }
  694. int Process::sys$ttyname_r(int fd, char* buffer, size_t size)
  695. {
  696. VALIDATE_USER_WRITE(buffer, size);
  697. auto* handle = fileHandleIfExists(fd);
  698. if (!handle)
  699. return -EBADF;
  700. if (!handle->isTTY())
  701. return -ENOTTY;
  702. auto ttyName = handle->tty()->ttyName();
  703. if (size < ttyName.length() + 1)
  704. return -ERANGE;
  705. strcpy(buffer, ttyName.characters());
  706. return 0;
  707. }
  708. ssize_t Process::sys$write(int fd, const void* data, size_t size)
  709. {
  710. VALIDATE_USER_READ(data, size);
  711. #ifdef DEBUG_IO
  712. kprintf("Process::sys$write: called(%d, %p, %u)\n", fd, data, size);
  713. #endif
  714. auto* handle = fileHandleIfExists(fd);
  715. #ifdef DEBUG_IO
  716. kprintf("Process::sys$write: handle=%p\n", handle);
  717. #endif
  718. if (!handle)
  719. return -EBADF;
  720. auto nwritten = handle->write((const byte*)data, size);
  721. #ifdef DEBUG_IO
  722. kprintf("Process::sys$write: nwritten=%u\n", nwritten);
  723. #endif
  724. return nwritten;
  725. }
  726. ssize_t Process::sys$read(int fd, void* outbuf, size_t nread)
  727. {
  728. VALIDATE_USER_WRITE(outbuf, nread);
  729. #ifdef DEBUG_IO
  730. kprintf("Process::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread);
  731. #endif
  732. auto* handle = fileHandleIfExists(fd);
  733. #ifdef DEBUG_IO
  734. kprintf("Process::sys$read: handle=%p\n", handle);
  735. #endif
  736. if (!handle)
  737. return -EBADF;
  738. if (handle->isBlocking()) {
  739. if (!handle->hasDataAvailableForRead()) {
  740. m_fdBlockedOnRead = fd;
  741. block(BlockedRead);
  742. yield();
  743. }
  744. }
  745. nread = handle->read((byte*)outbuf, nread);
  746. #ifdef DEBUG_IO
  747. kprintf("Process::sys$read: nread=%u\n", nread);
  748. #endif
  749. return nread;
  750. }
  751. int Process::sys$close(int fd)
  752. {
  753. auto* handle = fileHandleIfExists(fd);
  754. if (!handle)
  755. return -EBADF;
  756. int rc = handle->close();
  757. m_file_descriptors[fd] = nullptr;
  758. return rc;
  759. }
  760. int Process::sys$lstat(const char* path, Unix::stat* statbuf)
  761. {
  762. VALIDATE_USER_WRITE(statbuf, sizeof(Unix::stat));
  763. int error;
  764. auto handle = VirtualFileSystem::the().open(move(path), error, O_NOFOLLOW_NOERROR, cwdInode());
  765. if (!handle)
  766. return error;
  767. handle->stat(statbuf);
  768. return 0;
  769. }
  770. int Process::sys$stat(const char* path, Unix::stat* statbuf)
  771. {
  772. VALIDATE_USER_WRITE(statbuf, sizeof(Unix::stat));
  773. int error;
  774. auto handle = VirtualFileSystem::the().open(move(path), error, 0, cwdInode());
  775. if (!handle)
  776. return error;
  777. handle->stat(statbuf);
  778. return 0;
  779. }
  780. int Process::sys$readlink(const char* path, char* buffer, size_t size)
  781. {
  782. VALIDATE_USER_READ(path, strlen(path));
  783. VALIDATE_USER_WRITE(buffer, size);
  784. int error;
  785. auto handle = VirtualFileSystem::the().open(path, error, O_RDONLY | O_NOFOLLOW_NOERROR, cwdInode());
  786. if (!handle)
  787. return error;
  788. if (!handle->metadata().isSymbolicLink())
  789. return -EINVAL;
  790. auto contents = handle->readEntireFile();
  791. if (!contents)
  792. return -EIO; // FIXME: Get a more detailed error from VFS.
  793. memcpy(buffer, contents.pointer(), min(size, contents.size()));
  794. if (contents.size() + 1 < size)
  795. buffer[contents.size()] = '\0';
  796. return 0;
  797. }
  798. int Process::sys$chdir(const char* path)
  799. {
  800. VALIDATE_USER_READ(path, strlen(path));
  801. int error;
  802. auto handle = VirtualFileSystem::the().open(path, error, 0, cwdInode());
  803. if (!handle)
  804. return error;
  805. if (!handle->isDirectory())
  806. return -ENOTDIR;
  807. m_cwd = handle->vnode();
  808. return 0;
  809. }
  810. int Process::sys$getcwd(char* buffer, size_t size)
  811. {
  812. VALIDATE_USER_WRITE(buffer, size);
  813. auto path = VirtualFileSystem::the().absolutePath(cwdInode());
  814. if (path.isNull())
  815. return -EINVAL;
  816. if (size < path.length() + 1)
  817. return -ERANGE;
  818. strcpy(buffer, path.characters());
  819. return -ENOTIMPL;
  820. }
  821. size_t Process::number_of_open_file_descriptors() const
  822. {
  823. size_t count = 0;
  824. for (auto& handle : m_file_descriptors) {
  825. if (handle)
  826. ++count;
  827. }
  828. return count;
  829. }
  830. int Process::sys$open(const char* path, int options)
  831. {
  832. #ifdef DEBUG_IO
  833. kprintf("Process::sys$open(): PID=%u, path=%s {%u}\n", m_pid, path, pathLength);
  834. #endif
  835. VALIDATE_USER_READ(path, strlen(path));
  836. if (number_of_open_file_descriptors() >= m_max_open_file_descriptors)
  837. return -EMFILE;
  838. int error;
  839. auto handle = VirtualFileSystem::the().open(path, error, options, cwdInode());
  840. if (!handle)
  841. return error;
  842. if (options & O_DIRECTORY && !handle->isDirectory())
  843. return -ENOTDIR; // FIXME: This should be handled by VFS::open.
  844. int fd = 0;
  845. for (; fd < m_max_open_file_descriptors; ++fd) {
  846. if (!m_file_descriptors[fd])
  847. break;
  848. }
  849. handle->setFD(fd);
  850. m_file_descriptors[fd] = move(handle);
  851. return fd;
  852. }
  853. int Process::sys$uname(utsname* buf)
  854. {
  855. VALIDATE_USER_WRITE(buf, sizeof(utsname));
  856. strcpy(buf->sysname, "Serenity");
  857. strcpy(buf->release, "1.0-dev");
  858. strcpy(buf->version, "FIXME");
  859. strcpy(buf->machine, "i386");
  860. strcpy(buf->nodename, getHostname().characters());
  861. return 0;
  862. }
  863. int Process::sys$kill(pid_t pid, int sig)
  864. {
  865. (void) sig;
  866. if (pid == 0) {
  867. // FIXME: Send to same-group processes.
  868. ASSERT(pid != 0);
  869. }
  870. if (pid == -1) {
  871. // FIXME: Send to all processes.
  872. ASSERT(pid != -1);
  873. }
  874. ASSERT(pid != current->pid()); // FIXME: Support this scenario.
  875. InterruptDisabler disabler;
  876. auto* peer = Process::fromPID(pid);
  877. if (!peer)
  878. return -ESRCH;
  879. if (sig == SIGKILL) {
  880. peer->murder(SIGKILL);
  881. return 0;
  882. } else {
  883. ASSERT_NOT_REACHED();
  884. }
  885. return -1;
  886. }
  887. int Process::sys$sleep(unsigned seconds)
  888. {
  889. if (!seconds)
  890. return 0;
  891. sleep(seconds * TICKS_PER_SECOND);
  892. return 0;
  893. }
  894. int Process::sys$gettimeofday(timeval* tv)
  895. {
  896. VALIDATE_USER_WRITE(tv, sizeof(tv));
  897. InterruptDisabler disabler;
  898. auto now = RTC::now();
  899. tv->tv_sec = now;
  900. tv->tv_usec = 0;
  901. return 0;
  902. }
  903. uid_t Process::sys$getuid()
  904. {
  905. return m_uid;
  906. }
  907. gid_t Process::sys$getgid()
  908. {
  909. return m_gid;
  910. }
  911. pid_t Process::sys$getpid()
  912. {
  913. return m_pid;
  914. }
  915. pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
  916. {
  917. if (wstatus)
  918. VALIDATE_USER_WRITE(wstatus, sizeof(int));
  919. InterruptDisabler disabler;
  920. if (!Process::fromPID(waitee))
  921. return -1;
  922. m_waitee = waitee;
  923. m_waiteeStatus = 0;
  924. block(BlockedWait);
  925. yield();
  926. if (wstatus)
  927. *wstatus = m_waiteeStatus;
  928. return m_waitee;
  929. }
  930. void Process::unblock()
  931. {
  932. ASSERT(m_state != Process::Runnable && m_state != Process::Running);
  933. system.nblocked--;
  934. m_state = Process::Runnable;
  935. }
  936. void Process::block(Process::State state)
  937. {
  938. ASSERT(current->state() == Process::Running);
  939. system.nblocked++;
  940. current->set_state(state);
  941. }
  942. void block(Process::State state)
  943. {
  944. current->block(state);
  945. yield();
  946. }
  947. void sleep(DWORD ticks)
  948. {
  949. ASSERT(current->state() == Process::Running);
  950. current->setWakeupTime(system.uptime + ticks);
  951. current->block(Process::BlockedSleep);
  952. yield();
  953. }
  954. Process* Process::kernelProcess()
  955. {
  956. ASSERT(s_kernelProcess);
  957. return s_kernelProcess;
  958. }
  959. Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n)
  960. : linearAddress(a)
  961. , size(s)
  962. , zone(move(z))
  963. , name(move(n))
  964. {
  965. }
  966. Region::~Region()
  967. {
  968. }
  969. Subregion::Subregion(Region& r, dword o, size_t s, LinearAddress l, String&& n)\
  970. : region(r)
  971. , offset(o)
  972. , size(s)
  973. , linearAddress(l)
  974. , name(move(n))
  975. {
  976. }
  977. Subregion::~Subregion()
  978. {
  979. }
  980. bool Process::isValidAddressForKernel(LinearAddress laddr) const
  981. {
  982. // We check extra carefully here since the first 4MB of the address space is identity-mapped.
  983. // This code allows access outside of the known used address ranges to get caught.
  984. InterruptDisabler disabler;
  985. if (laddr.get() >= ksyms().first().address && laddr.get() <= ksyms().last().address)
  986. return true;
  987. if (is_kmalloc_address((void*)laddr.get()))
  988. return true;
  989. return validate_user_read(laddr);
  990. }
  991. bool Process::validate_user_read(LinearAddress laddr) const
  992. {
  993. InterruptDisabler disabler;
  994. return MM.validate_user_read(*this, laddr);
  995. }
  996. bool Process::validate_user_write(LinearAddress laddr) const
  997. {
  998. InterruptDisabler disabler;
  999. return MM.validate_user_write(*this, laddr);
  1000. }