Task.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. #include "types.h"
  2. #include "Task.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. //#define DEBUG_IO
  13. Task* current;
  14. Task* s_kernelTask;
  15. static pid_t next_pid;
  16. static InlineLinkedList<Task>* s_tasks;
  17. static bool contextSwitch(Task*);
  18. static void redoKernelTaskTSS()
  19. {
  20. if (!s_kernelTask->selector())
  21. s_kernelTask->setSelector(allocateGDTEntry());
  22. auto& tssDescriptor = getGDTEntry(s_kernelTask->selector());
  23. tssDescriptor.setBase(&s_kernelTask->tss());
  24. tssDescriptor.setLimit(0xffff);
  25. tssDescriptor.dpl = 0;
  26. tssDescriptor.segment_present = 1;
  27. tssDescriptor.granularity = 1;
  28. tssDescriptor.zero = 0;
  29. tssDescriptor.operation_size = 1;
  30. tssDescriptor.descriptor_type = 0;
  31. tssDescriptor.type = 9;
  32. flushGDT();
  33. }
  34. void Task::prepForIRETToNewTask()
  35. {
  36. redoKernelTaskTSS();
  37. s_kernelTask->tss().backlink = current->selector();
  38. loadTaskRegister(s_kernelTask->selector());
  39. }
  40. void Task::initialize()
  41. {
  42. current = nullptr;
  43. next_pid = 0;
  44. s_tasks = new InlineLinkedList<Task>;
  45. s_kernelTask = new Task(0, "colonel", IPC::Handle::Any, Task::Ring0);
  46. redoKernelTaskTSS();
  47. loadTaskRegister(s_kernelTask->selector());
  48. }
  49. #ifdef TASK_SANITY_CHECKS
  50. void Task::checkSanity(const char* msg)
  51. {
  52. char ch = current->name()[0];
  53. kprintf("<%p> %s{%u}%b [%d] :%b: sanity check <%s>\n",
  54. current->name().characters(),
  55. current->name().characters(),
  56. current->name().length(),
  57. current->name()[current->name().length() - 1],
  58. current->pid(), ch, msg ? msg : "");
  59. ASSERT((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
  60. }
  61. #endif
  62. void Task::allocateLDT()
  63. {
  64. ASSERT(!m_tss.ldt);
  65. static const WORD numLDTEntries = 4;
  66. WORD newLDTSelector = allocateGDTEntry();
  67. m_ldtEntries = new Descriptor[numLDTEntries];
  68. #if 0
  69. kprintf("new ldt selector = %x\n", newLDTSelector);
  70. kprintf("new ldt table at = %p\n", m_ldtEntries);
  71. kprintf("new ldt table size = %u\n", (numLDTEntries * 8) - 1);
  72. #endif
  73. Descriptor& ldt = getGDTEntry(newLDTSelector);
  74. ldt.setBase(m_ldtEntries);
  75. ldt.setLimit(numLDTEntries * 8 - 1);
  76. ldt.dpl = 0;
  77. ldt.segment_present = 1;
  78. ldt.granularity = 0;
  79. ldt.zero = 0;
  80. ldt.operation_size = 1;
  81. ldt.descriptor_type = 0;
  82. ldt.type = Descriptor::LDT;
  83. m_tss.ldt = newLDTSelector;
  84. }
  85. Vector<Task*> Task::allTasks()
  86. {
  87. Vector<Task*> tasks;
  88. tasks.ensureCapacity(s_tasks->sizeSlow());
  89. for (auto* task = s_tasks->head(); task; task = task->next())
  90. tasks.append(task);
  91. return tasks;
  92. }
  93. Task::Region* Task::allocateRegion(size_t size, String&& name)
  94. {
  95. // FIXME: This needs sanity checks. What if this overlaps existing regions?
  96. auto zone = MemoryManager::the().createZone(size);
  97. ASSERT(zone);
  98. m_regions.append(make<Region>(m_nextRegion, size, move(zone), move(name)));
  99. m_nextRegion = m_nextRegion.offset(size).offset(16384);
  100. return m_regions.last().ptr();
  101. }
  102. int Task::sys$spawn(const char* path)
  103. {
  104. auto* child = Task::create(path, m_uid, m_gid);
  105. if (child)
  106. return child->pid();
  107. return -1;
  108. }
  109. Task* Task::create(const String& path, uid_t uid, gid_t gid)
  110. {
  111. auto parts = path.split('/');
  112. if (parts.isEmpty())
  113. return nullptr;
  114. auto handle = VirtualFileSystem::the().open(path);
  115. if (!handle)
  116. return nullptr;
  117. auto elfData = handle->readEntireFile();
  118. if (!elfData)
  119. return nullptr;
  120. cli();
  121. Task* t = new Task(parts.takeLast(), uid, gid);
  122. ExecSpace space;
  123. space.hookableAlloc = [&] (const String& name, size_t size) {
  124. if (!size)
  125. return (void*)nullptr;
  126. size = ((size / 4096) + 1) * 4096;
  127. Region* region = t->allocateRegion(size, String(name));
  128. ASSERT(region);
  129. MemoryManager::the().mapRegion(*t, *region);
  130. return (void*)region->linearAddress.asPtr();
  131. };
  132. bool success = space.loadELF(move(elfData));
  133. if (!success) {
  134. delete t;
  135. return nullptr;
  136. }
  137. t->m_tss.eip = (dword)space.symbolPtr("_start");
  138. if (!t->m_tss.eip) {
  139. delete t;
  140. return nullptr;
  141. }
  142. MemoryManager::the().unmapRegionsForTask(*t);
  143. MemoryManager::the().mapRegionsForTask(*current);
  144. s_tasks->prepend(t);
  145. system.nprocess++;
  146. kprintf("Task %u (%s) spawned @ %p\n", t->pid(), t->name().characters(), t->m_tss.eip);
  147. sti();
  148. return t;
  149. }
  150. Task::Task(String&& name, uid_t uid, gid_t gid)
  151. : m_name(move(name))
  152. , m_pid(next_pid++)
  153. , m_uid(uid)
  154. , m_gid(gid)
  155. , m_state(Runnable)
  156. , m_ring(Ring3)
  157. {
  158. m_nextRegion = LinearAddress(0x600000);
  159. memset(&m_tss, 0, sizeof(m_tss));
  160. memset(&m_ldtEntries, 0, sizeof(m_ldtEntries));
  161. allocateLDT();
  162. // Only IF is set when a task boots.
  163. m_tss.eflags = 0x0202;
  164. WORD codeSegment = 0x1b;
  165. WORD dataSegment = 0x23;
  166. WORD stackSegment = dataSegment;
  167. m_tss.ds = dataSegment;
  168. m_tss.es = dataSegment;
  169. m_tss.fs = dataSegment;
  170. m_tss.gs = dataSegment;
  171. m_tss.ss = stackSegment;
  172. m_tss.cs = codeSegment;
  173. m_tss.cr3 = MemoryManager::the().pageDirectoryBase().get();
  174. // NOTE: Each task gets 16KB of stack.
  175. static const DWORD defaultStackSize = 16384;
  176. auto* region = allocateRegion(defaultStackSize, "stack");
  177. ASSERT(region);
  178. m_stackTop = region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  179. m_tss.esp = m_stackTop;
  180. // Set up a separate stack for Ring0.
  181. // FIXME: Don't leak this stack.
  182. m_kernelStack = kmalloc(defaultStackSize);
  183. DWORD ring0StackTop = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8;
  184. m_tss.ss0 = 0x10;
  185. m_tss.esp0 = ring0StackTop;
  186. // HACK: Ring2 SS in the TSS is the current PID.
  187. m_tss.ss2 = m_pid;
  188. m_farPtr.offset = 0x98765432;
  189. ASSERT(m_pid);
  190. }
  191. Task::Task(void (*e)(), const char* n, IPC::Handle h, RingLevel ring)
  192. : m_name(n)
  193. , m_entry(e)
  194. , m_pid(next_pid++)
  195. , m_handle(h)
  196. , m_state(Runnable)
  197. , m_ring(ring)
  198. {
  199. m_nextRegion = LinearAddress(0x600000);
  200. Region* codeRegion = nullptr;
  201. if (!isRing0()) {
  202. codeRegion = allocateRegion(4096, "code");
  203. ASSERT(codeRegion);
  204. bool success = copyToZone(*codeRegion->zone, (void*)e, PAGE_SIZE);
  205. ASSERT(success);
  206. }
  207. memset(&m_tss, 0, sizeof(m_tss));
  208. memset(&m_ldtEntries, 0, sizeof(m_ldtEntries));
  209. if (ring == Ring3) {
  210. allocateLDT();
  211. }
  212. // Only IF is set when a task boots.
  213. m_tss.eflags = 0x0202;
  214. WORD dataSegment;
  215. WORD stackSegment;
  216. WORD codeSegment;
  217. if (ring == Ring0) {
  218. codeSegment = 0x08;
  219. dataSegment = 0x10;
  220. stackSegment = dataSegment;
  221. } else {
  222. codeSegment = 0x1b;
  223. dataSegment = 0x23;
  224. stackSegment = dataSegment;
  225. }
  226. m_tss.ds = dataSegment;
  227. m_tss.es = dataSegment;
  228. m_tss.fs = dataSegment;
  229. m_tss.gs = dataSegment;
  230. m_tss.ss = stackSegment;
  231. m_tss.cs = codeSegment;
  232. ASSERT((codeSegment & 3) == (stackSegment & 3));
  233. m_tss.cr3 = MemoryManager::the().pageDirectoryBase().get();
  234. if (isRing0()) {
  235. m_tss.eip = (DWORD)m_entry;
  236. } else {
  237. m_tss.eip = codeRegion->linearAddress.get();
  238. }
  239. // NOTE: Each task gets 16KB of stack.
  240. static const DWORD defaultStackSize = 16384;
  241. if (isRing0()) {
  242. // FIXME: This memory is leaked.
  243. // But uh, there's also no kernel task termination, so I guess it's not technically leaked...
  244. dword stackBottom = (dword)kmalloc(defaultStackSize);
  245. m_stackTop = (stackBottom + defaultStackSize) & 0xffffff8;
  246. m_tss.esp = m_stackTop;
  247. } else {
  248. auto* region = allocateRegion(defaultStackSize, "stack");
  249. ASSERT(region);
  250. m_stackTop = region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  251. m_tss.esp = m_stackTop;
  252. }
  253. if (ring == Ring3) {
  254. // Set up a separate stack for Ring0.
  255. // FIXME: Don't leak this stack either.
  256. m_kernelStack = kmalloc(defaultStackSize);
  257. DWORD ring0StackTop = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8;
  258. m_tss.ss0 = 0x10;
  259. m_tss.esp0 = ring0StackTop;
  260. }
  261. // HACK: Ring2 SS in the TSS is the current PID.
  262. m_tss.ss2 = m_pid;
  263. m_farPtr.offset = 0x12345678;
  264. // Don't add task 0 (kernel dummy task) to task list.
  265. // FIXME: This doesn't belong in the constructor.
  266. if (m_pid == 0)
  267. return;
  268. // Add it to head of task list (meaning it's next to run too, ATM.)
  269. s_tasks->prepend(this);
  270. system.nprocess++;
  271. kprintf("Task %u (%s) spawned @ %p\n", m_pid, m_name.characters(), m_tss.eip);
  272. }
  273. Task::~Task()
  274. {
  275. system.nprocess--;
  276. delete [] m_ldtEntries;
  277. m_ldtEntries = nullptr;
  278. // FIXME: The task's kernel stack is currently leaked, because otherwise we GPF.
  279. // This obviously needs figuring out.
  280. #if 0
  281. if (m_kernelStack) {
  282. kfree(m_kernelStack);
  283. m_kernelStack = nullptr;
  284. }
  285. #endif
  286. }
  287. void Task::dumpRegions()
  288. {
  289. kprintf("Task %s(%u) regions:\n", name().characters(), pid());
  290. kprintf("BEGIN END SIZE NAME\n");
  291. for (auto& region : m_regions) {
  292. kprintf("%x -- %x %x %s\n",
  293. region->linearAddress.get(),
  294. region->linearAddress.offset(region->size - 1).get(),
  295. region->size,
  296. region->name.characters());
  297. }
  298. }
  299. void Task::sys$exit(int status)
  300. {
  301. cli();
  302. kprintf("sys$exit: %s(%u) exit with status %d\n", name().characters(), pid(), status);
  303. setState(Exiting);
  304. MemoryManager::the().unmapRegionsForTask(*this);
  305. s_tasks->remove(this);
  306. if (!scheduleNewTask()) {
  307. kprintf("Task::taskDidCrash: Failed to schedule a new task :(\n");
  308. HANG;
  309. }
  310. delete this;
  311. switchNow();
  312. }
  313. void Task::taskDidCrash(Task* crashedTask)
  314. {
  315. // NOTE: This is called from an excepton handler, so interrupts are disabled.
  316. crashedTask->setState(Crashing);
  317. crashedTask->dumpRegions();
  318. s_tasks->remove(crashedTask);
  319. MemoryManager::the().unmapRegionsForTask(*crashedTask);
  320. if (!scheduleNewTask()) {
  321. kprintf("Task::taskDidCrash: Failed to schedule a new task :(\n");
  322. HANG;
  323. }
  324. delete crashedTask;
  325. switchNow();
  326. }
  327. void yield()
  328. {
  329. if (!current) {
  330. kprintf( "PANIC: yield() with !current" );
  331. HANG;
  332. }
  333. //kprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  334. cli();
  335. if (!scheduleNewTask()) {
  336. sti();
  337. return;
  338. }
  339. //kprintf("yield() jumping to new task: %x (%s)\n", current->farPtr().selector, current->name().characters());
  340. switchNow();
  341. }
  342. void switchNow()
  343. {
  344. Descriptor& descriptor = getGDTEntry(current->selector());
  345. descriptor.type = 9;
  346. flushGDT();
  347. asm("sti\n"
  348. "ljmp *(%%eax)\n"
  349. ::"a"(&current->farPtr())
  350. );
  351. }
  352. bool scheduleNewTask()
  353. {
  354. if (!current) {
  355. // XXX: The first ever context_switch() goes to the idle task.
  356. // This to setup a reliable place we can return to.
  357. return contextSwitch(Task::kernelTask());
  358. }
  359. #if 0
  360. kprintf("Scheduler choices:\n");
  361. for (auto* task = s_tasks->head(); task; task = task->next()) {
  362. kprintf("%p %u %s\n", task, task->pid(), task->name().characters());
  363. }
  364. #endif
  365. // Check and unblock tasks whose wait conditions have been met.
  366. for (auto* task = s_tasks->head(); task; task = task->next()) {
  367. if (task->state() == Task::BlockedReceive && (task->ipc.msg.isValid() || task->ipc.notifies)) {
  368. task->unblock();
  369. continue;
  370. }
  371. if (task->state() == Task::BlockedSend) {
  372. Task* peer = Task::fromIPCHandle(task->ipc.dst);
  373. if (peer && peer->state() == Task::BlockedReceive && peer->acceptsMessageFrom(*task)) {
  374. task->unblock();
  375. continue;
  376. }
  377. }
  378. if (task->state() == Task::BlockedSleep) {
  379. if (task->wakeupTime() <= system.uptime) {
  380. task->unblock();
  381. continue;
  382. }
  383. }
  384. }
  385. auto* prevHead = s_tasks->head();
  386. for (;;) {
  387. // Move head to tail.
  388. s_tasks->append(s_tasks->removeHead());
  389. auto* task = s_tasks->head();
  390. if (task->state() == Task::Runnable || task->state() == Task::Running) {
  391. //kprintf("switch to %s (%p vs %p)\n", task->name().characters(), task, current);
  392. return contextSwitch(task);
  393. }
  394. if (task == prevHead) {
  395. // Back at task_head, nothing wants to run.
  396. kprintf("Switch to kernel task\n");
  397. return contextSwitch(Task::kernelTask());
  398. }
  399. }
  400. }
  401. static void drawSchedulerBanner(Task& task)
  402. {
  403. return;
  404. // FIXME: We need a kernel lock to do stuff like this :(
  405. //return;
  406. auto c = vga_get_cursor();
  407. auto a = vga_get_attr();
  408. vga_set_cursor(0, 50);
  409. vga_set_attr(0x20);
  410. kprintf(" ");
  411. kprintf(" ");
  412. kprintf(" ");
  413. vga_set_cursor(0, 50);
  414. kprintf("pid: %u ", task.pid());
  415. vga_set_cursor(0, 58);
  416. kprintf("%s", task.name().characters());
  417. vga_set_cursor(0, 65);
  418. kprintf("eip: %p", task.tss().eip);
  419. vga_set_attr(a);
  420. vga_set_cursor(c);
  421. }
  422. static bool contextSwitch(Task* t)
  423. {
  424. //kprintf("c_s to %s (same:%u)\n", t->name().characters(), current == t);
  425. t->setTicksLeft(5);
  426. if (current == t)
  427. return false;
  428. // Some sanity checking to force a crash earlier.
  429. auto csRPL = t->tss().cs & 3;
  430. auto ssRPL = t->tss().ss & 3;
  431. if (csRPL != ssRPL) {
  432. kprintf("Fuckup! Switching from %s(%u) to %s(%u) has RPL mismatch\n",
  433. current->name().characters(), current->pid(),
  434. t->name().characters(), t->pid()
  435. );
  436. kprintf("code: %w:%x\n", t->tss().cs, t->tss().eip);
  437. kprintf(" stk: %w:%x\n", t->tss().ss, t->tss().esp);
  438. ASSERT(csRPL == ssRPL);
  439. }
  440. if (current) {
  441. // If the last task hasn't blocked (still marked as running),
  442. // mark it as runnable for the next round.
  443. if (current->state() == Task::Running)
  444. current->setState(Task::Runnable);
  445. bool success = MemoryManager::the().unmapRegionsForTask(*current);
  446. ASSERT(success);
  447. }
  448. bool success = MemoryManager::the().mapRegionsForTask(*t);
  449. ASSERT(success);
  450. current = t;
  451. t->setState(Task::Running);
  452. if (!t->selector())
  453. t->setSelector(allocateGDTEntry());
  454. auto& tssDescriptor = getGDTEntry(t->selector());
  455. tssDescriptor.limit_hi = 0;
  456. tssDescriptor.limit_lo = 0xFFFF;
  457. tssDescriptor.base_lo = (DWORD)(&t->tss()) & 0xFFFF;
  458. tssDescriptor.base_hi = ((DWORD)(&t->tss()) >> 16) & 0xFF;
  459. tssDescriptor.base_hi2 = ((DWORD)(&t->tss()) >> 24) & 0xFF;
  460. tssDescriptor.dpl = 0;
  461. tssDescriptor.segment_present = 1;
  462. tssDescriptor.granularity = 1;
  463. tssDescriptor.zero = 0;
  464. tssDescriptor.operation_size = 1;
  465. tssDescriptor.descriptor_type = 0;
  466. tssDescriptor.type = 11; // Busy TSS
  467. flushGDT();
  468. drawSchedulerBanner(*t);
  469. t->didSchedule();
  470. return true;
  471. }
  472. Task* Task::fromPID(pid_t pid)
  473. {
  474. for (auto* task = s_tasks->head(); task; task = task->next()) {
  475. if (task->pid() == pid)
  476. return task;
  477. }
  478. return nullptr;
  479. }
  480. Task* Task::fromIPCHandle(IPC::Handle handle)
  481. {
  482. for (auto* task = s_tasks->head(); task; task = task->next()) {
  483. if (task->handle() == handle)
  484. return task;
  485. }
  486. return nullptr;
  487. }
  488. FileHandle* Task::fileHandleIfExists(int fd)
  489. {
  490. if (fd < 0)
  491. return nullptr;
  492. if ((unsigned)fd < m_fileHandles.size())
  493. return m_fileHandles[fd].ptr();
  494. return nullptr;
  495. }
  496. int Task::sys$seek(int fd, int offset)
  497. {
  498. auto* handle = fileHandleIfExists(fd);
  499. if (!handle)
  500. return -1;
  501. return handle->seek(offset, SEEK_SET);
  502. }
  503. ssize_t Task::sys$read(int fd, void* outbuf, size_t nread)
  504. {
  505. Task::checkSanity("Task::sys$read");
  506. #ifdef DEBUG_IO
  507. kprintf("Task::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread);
  508. #endif
  509. auto* handle = fileHandleIfExists(fd);
  510. #ifdef DEBUG_IO
  511. kprintf("Task::sys$read: handle=%p\n", handle);
  512. #endif
  513. if (!handle) {
  514. kprintf("Task::sys$read: handle not found :(\n");
  515. return -1;
  516. }
  517. #ifdef DEBUG_IO
  518. kprintf("call read on handle=%p\n", handle);
  519. #endif
  520. nread = handle->read((byte*)outbuf, nread);
  521. #ifdef DEBUG_IO
  522. kprintf("Task::sys$read: nread=%u\n", nread);
  523. #endif
  524. return nread;
  525. }
  526. int Task::sys$close(int fd)
  527. {
  528. auto* handle = fileHandleIfExists(fd);
  529. if (!handle)
  530. return -1;
  531. // FIXME: Implement.
  532. return 0;
  533. }
  534. int Task::sys$open(const char* path, size_t pathLength)
  535. {
  536. Task::checkSanity("sys$open");
  537. #ifdef DEBUG_IO
  538. kprintf("Task::sys$open(): PID=%u, path=%s {%u}\n", m_pid, path, pathLength);
  539. #endif
  540. auto* handle = current->openFile(String(path, pathLength));
  541. if (handle)
  542. return handle->fd();
  543. return -1;
  544. }
  545. FileHandle* Task::openFile(String&& path)
  546. {
  547. auto handle = VirtualFileSystem::the().open(move(path));
  548. if (!handle) {
  549. #ifdef DEBUG_IO
  550. kprintf("vfs::open() failed\n");
  551. #endif
  552. return nullptr;
  553. }
  554. handle->setFD(m_fileHandles.size());
  555. #ifdef DEBUG_IO
  556. kprintf("vfs::open() worked! handle=%p, fd=%d\n", handle.ptr(), handle->fd());
  557. #endif
  558. m_fileHandles.append(move(handle)); // FIXME: allow non-move Vector::append
  559. return m_fileHandles.last().ptr();
  560. }
  561. int Task::sys$kill(pid_t pid, int sig)
  562. {
  563. (void) sig;
  564. if (pid == 0) {
  565. // FIXME: Send to same-group processes.
  566. ASSERT(pid != 0);
  567. }
  568. if (pid == -1) {
  569. // FIXME: Send to all processes.
  570. ASSERT(pid != -1);
  571. }
  572. ASSERT_NOT_REACHED();
  573. Task* peer = Task::fromPID(pid);
  574. if (!peer) {
  575. // errno = ESRCH;
  576. return -1;
  577. }
  578. #if 0
  579. send(peer->handle(), IPC::Message(SYS_KILL, DataBuffer::copy((BYTE*)&sig, sizeof(sig))));
  580. IPC::Message response = receive(peer->handle());
  581. return *(int*)response.data();
  582. #endif
  583. return -1;
  584. }
  585. uid_t Task::sys$getuid()
  586. {
  587. return m_uid;
  588. }
  589. gid_t Task::sys$getgid()
  590. {
  591. return m_gid;
  592. }
  593. pid_t Task::sys$getpid()
  594. {
  595. return m_pid;
  596. }
  597. bool Task::acceptsMessageFrom(Task& peer)
  598. {
  599. return !ipc.msg.isValid() && (ipc.src == IPC::Handle::Any || ipc.src == peer.handle());
  600. }
  601. void Task::unblock()
  602. {
  603. ASSERT(m_state != Task::Runnable && m_state != Task::Running);
  604. system.nblocked--;
  605. m_state = Task::Runnable;
  606. }
  607. void Task::block(Task::State state)
  608. {
  609. ASSERT(current->state() == Task::Running);
  610. system.nblocked++;
  611. current->setState(state);
  612. }
  613. void block(Task::State state)
  614. {
  615. current->block(state);
  616. yield();
  617. }
  618. void sleep(DWORD ticks)
  619. {
  620. ASSERT(current->state() == Task::Running);
  621. current->setWakeupTime(system.uptime + ticks);
  622. current->block(Task::BlockedSleep);
  623. yield();
  624. }
  625. void Task::sys$sleep(DWORD ticks)
  626. {
  627. ASSERT(this == current);
  628. sleep(ticks);
  629. }
  630. Task* Task::kernelTask()
  631. {
  632. ASSERT(s_kernelTask);
  633. return s_kernelTask;
  634. }
  635. void Task::setError(int error)
  636. {
  637. m_error = error;
  638. }
  639. Task::Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n)
  640. : linearAddress(a)
  641. , size(s)
  642. , zone(move(z))
  643. , name(move(n))
  644. {
  645. }
  646. Task::Region::~Region()
  647. {
  648. }