i386.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include <AK/Types.h>
  2. #include "i386.h"
  3. #include "Assertions.h"
  4. #include "Process.h"
  5. #include <Kernel/VM/MemoryManager.h>
  6. #include "IRQHandler.h"
  7. #include "PIC.h"
  8. #include "Scheduler.h"
  9. #include <Kernel/KSyms.h>
  10. //#define PAGE_FAULT_DEBUG
  11. struct [[gnu::packed]] DescriptorTablePointer {
  12. word limit;
  13. void* address;
  14. };
  15. static DescriptorTablePointer s_idtr;
  16. static DescriptorTablePointer s_gdtr;
  17. static Descriptor s_idt[256];
  18. static Descriptor s_gdt[256];
  19. static IRQHandler* s_irq_handler[16];
  20. static Vector<word>* s_gdt_freelist;
  21. static word s_gdt_length;
  22. word gdt_alloc_entry()
  23. {
  24. ASSERT(s_gdt_freelist);
  25. ASSERT(!s_gdt_freelist->is_empty());
  26. return s_gdt_freelist->take_last();
  27. }
  28. void gdt_free_entry(word entry)
  29. {
  30. s_gdt_freelist->append(entry);
  31. }
  32. extern "C" void handle_irq();
  33. extern "C" void asm_irq_entry();
  34. asm(
  35. ".globl asm_irq_entry\n"
  36. "asm_irq_entry: \n"
  37. " pusha\n"
  38. " pushw %ds\n"
  39. " pushw %es\n"
  40. " pushw %ss\n"
  41. " pushw %ss\n"
  42. " popw %ds\n"
  43. " popw %es\n"
  44. " call handle_irq\n"
  45. " popw %es\n"
  46. " popw %ds\n"
  47. " popa\n"
  48. " iret\n"
  49. );
  50. #define EH_ENTRY(ec) \
  51. extern "C" void exception_ ## ec ## _handler(RegisterDumpWithExceptionCode&); \
  52. extern "C" void exception_ ## ec ## _entry(); \
  53. asm( \
  54. ".globl exception_" # ec "_entry\n" \
  55. "exception_" # ec "_entry: \n" \
  56. " pusha\n" \
  57. " pushw %ds\n" \
  58. " pushw %es\n" \
  59. " pushw %fs\n" \
  60. " pushw %gs\n" \
  61. " pushw %ss\n" \
  62. " pushw %ss\n" \
  63. " pushw %ss\n" \
  64. " pushw %ss\n" \
  65. " pushw %ss\n" \
  66. " popw %ds\n" \
  67. " popw %es\n" \
  68. " popw %fs\n" \
  69. " popw %gs\n" \
  70. " mov %esp, %eax\n" \
  71. " call exception_" # ec "_handler\n" \
  72. " popw %gs\n" \
  73. " popw %gs\n" \
  74. " popw %fs\n" \
  75. " popw %es\n" \
  76. " popw %ds\n" \
  77. " popa\n" \
  78. " add $0x4, %esp\n" \
  79. " iret\n" \
  80. );
  81. #define EH_ENTRY_NO_CODE(ec) \
  82. extern "C" void exception_ ## ec ## _handler(RegisterDump&); \
  83. extern "C" void exception_ ## ec ## _entry(); \
  84. asm( \
  85. ".globl exception_" # ec "_entry\n" \
  86. "exception_" # ec "_entry: \n" \
  87. " pusha\n" \
  88. " pushw %ds\n" \
  89. " pushw %es\n" \
  90. " pushw %fs\n" \
  91. " pushw %gs\n" \
  92. " pushw %ss\n" \
  93. " pushw %ss\n" \
  94. " pushw %ss\n" \
  95. " pushw %ss\n" \
  96. " pushw %ss\n" \
  97. " popw %ds\n" \
  98. " popw %es\n" \
  99. " popw %fs\n" \
  100. " popw %gs\n" \
  101. " mov %esp, %eax\n" \
  102. " call exception_" # ec "_handler\n" \
  103. " popw %gs\n" \
  104. " popw %gs\n" \
  105. " popw %fs\n" \
  106. " popw %es\n" \
  107. " popw %ds\n" \
  108. " popa\n" \
  109. " iret\n" \
  110. );
  111. template<typename DumpType>
  112. static void dump(const DumpType& regs)
  113. {
  114. word ss;
  115. dword esp;
  116. if (!current || current->process().is_ring0()) {
  117. ss = regs.ds;
  118. esp = regs.esp;
  119. } else {
  120. ss = regs.ss_if_crossRing;
  121. esp = regs.esp_if_crossRing;
  122. }
  123. if constexpr (IsSame<DumpType, RegisterDumpWithExceptionCode>::value) {
  124. kprintf("exception code: %w\n", regs.exception_code);
  125. }
  126. kprintf(" pc=%w:%x ds=%w es=%w fs=%w gs=%w\n", regs.cs, regs.eip, regs.ds, regs.es, regs.fs, regs.gs);
  127. kprintf(" stk=%w:%x\n", ss, esp);
  128. if (current)
  129. kprintf("kstk=%w:%x, base=%x, sigbase=%x\n", current->tss().ss0, current->tss().esp0, current->kernel_stack_base(), current->kernel_stack_for_signal_handler_base());
  130. kprintf("eax=%x ebx=%x ecx=%x edx=%x\n", regs.eax, regs.ebx, regs.ecx, regs.edx);
  131. kprintf("ebp=%x esp=%x esi=%x edi=%x\n", regs.ebp, esp, regs.esi, regs.edi);
  132. if (current && current->process().validate_read((void*)regs.eip, 8)) {
  133. byte* codeptr = (byte*)regs.eip;
  134. kprintf("code: %b %b %b %b %b %b %b %b\n",
  135. codeptr[0],
  136. codeptr[1],
  137. codeptr[2],
  138. codeptr[3],
  139. codeptr[4],
  140. codeptr[5],
  141. codeptr[6],
  142. codeptr[7]);
  143. }
  144. }
  145. // 6: Invalid Opcode
  146. EH_ENTRY_NO_CODE(6);
  147. void exception_6_handler(RegisterDump& regs)
  148. {
  149. if (!current) {
  150. kprintf("#UD with !current\n");
  151. hang();
  152. }
  153. kprintf("%s invalid opcode: %u(%s)\n", current->process().is_ring0() ? "Kernel" : "Process", current->pid(), current->process().name().characters());
  154. dump(regs);
  155. if (current->process().is_ring0()) {
  156. kprintf("Oh shit, we've crashed in ring 0 :(\n");
  157. hang();
  158. }
  159. hang();
  160. current->process().crash();
  161. }
  162. // 7: FPU not available exception
  163. EH_ENTRY_NO_CODE(7);
  164. void exception_7_handler(RegisterDump& regs)
  165. {
  166. (void)regs;
  167. asm volatile("clts");
  168. if (g_last_fpu_thread == current)
  169. return;
  170. if (g_last_fpu_thread) {
  171. asm volatile("fxsave %0":"=m"(g_last_fpu_thread->fpu_state()));
  172. } else {
  173. asm volatile("fnclex");
  174. }
  175. g_last_fpu_thread = current;
  176. if (current->has_used_fpu()) {
  177. asm volatile("fxrstor %0"::"m"(current->fpu_state()));
  178. } else {
  179. asm volatile("fninit");
  180. current->set_has_used_fpu(true);
  181. }
  182. #ifdef FPU_EXCEPTION_DEBUG
  183. kprintf("%s FPU not available exception: %u(%s)\n", current->process().is_ring0() ? "Kernel" : "Process", current->pid(), current->process().name().characters());
  184. dump(regs);
  185. #endif
  186. }
  187. // 0: Divide error
  188. EH_ENTRY_NO_CODE(0);
  189. void exception_0_handler(RegisterDump& regs)
  190. {
  191. kprintf("%s DIVIDE ERROR: %u(%s)\n", current->process().is_ring0() ? "Kernel" : "User", current->pid(), current->process().name().characters());
  192. dump(regs);
  193. if (current->process().is_ring0()) {
  194. kprintf("Oh shit, we've crashed in ring 0 :(\n");
  195. hang();
  196. }
  197. current->process().crash();
  198. }
  199. // 13: General Protection Fault
  200. EH_ENTRY(13);
  201. void exception_13_handler(RegisterDumpWithExceptionCode& regs)
  202. {
  203. kprintf("%s GPF: %u(%s)\n", current->process().is_ring0() ? "Kernel" : "User", current->pid(), current->process().name().characters());
  204. dump(regs);
  205. if (current->process().is_ring0()) {
  206. kprintf("Oh shit, we've crashed in ring 0 :(\n");
  207. hang();
  208. }
  209. current->process().crash();
  210. }
  211. // 14: Page Fault
  212. EH_ENTRY(14);
  213. void exception_14_handler(RegisterDumpWithExceptionCode& regs)
  214. {
  215. ASSERT(current);
  216. dword faultAddress;
  217. asm ("movl %%cr2, %%eax":"=a"(faultAddress));
  218. dword fault_page_directory;
  219. asm ("movl %%cr3, %%eax":"=a"(fault_page_directory));
  220. #ifdef PAGE_FAULT_DEBUG
  221. dbgprintf("%s(%u): ring%u %s page fault in PD=%x, %s L%x\n",
  222. current->process().name().characters(),
  223. current->pid(),
  224. regs.cs & 3,
  225. regs.exception_code & 1 ? "PV" : "NP",
  226. fault_page_directory,
  227. regs.exception_code & 2 ? "write" : "read",
  228. faultAddress);
  229. #endif
  230. #ifdef PAGE_FAULT_DEBUG
  231. dump(regs);
  232. #endif
  233. auto response = MM.handle_page_fault(PageFault(regs.exception_code, LinearAddress(faultAddress)));
  234. if (response == PageFaultResponse::ShouldCrash) {
  235. kprintf("%s(%u:%u) unrecoverable page fault, %s laddr=%p\n",
  236. current->process().name().characters(),
  237. current->pid(),
  238. current->tid(),
  239. regs.exception_code & 2 ? "write" : "read",
  240. faultAddress);
  241. dump(regs);
  242. current->process().crash();
  243. } else if (response == PageFaultResponse::Continue) {
  244. #ifdef PAGE_FAULT_DEBUG
  245. dbgprintf("Continuing after resolved page fault\n");
  246. #endif
  247. } else {
  248. ASSERT_NOT_REACHED();
  249. }
  250. }
  251. #define EH(i, msg) \
  252. static void _exception ## i () \
  253. { \
  254. kprintf(msg"\n"); \
  255. dword cr0, cr2, cr3, cr4; \
  256. asm ("movl %%cr0, %%eax":"=a"(cr0)); \
  257. asm ("movl %%cr2, %%eax":"=a"(cr2)); \
  258. asm ("movl %%cr3, %%eax":"=a"(cr3)); \
  259. asm ("movl %%cr4, %%eax":"=a"(cr4)); \
  260. kprintf("CR0=%x CR2=%x CR3=%x CR4=%x\n", cr0, cr2, cr3, cr4); \
  261. hang(); \
  262. }
  263. EH(1, "Debug exception")
  264. EH(2, "Unknown error")
  265. EH(3, "Breakpoint")
  266. EH(4, "Overflow")
  267. EH(5, "Bounds check")
  268. EH(8, "Double fault")
  269. EH(9, "Coprocessor segment overrun")
  270. EH(10, "Invalid TSS")
  271. EH(11, "Segment not present")
  272. EH(12, "Stack exception")
  273. EH(15, "Unknown error")
  274. EH(16, "Coprocessor error")
  275. static void write_raw_gdt_entry(word selector, dword low, dword high)
  276. {
  277. word i = (selector & 0xfffc) >> 3;
  278. s_gdt[i].low = low;
  279. s_gdt[i].high = high;
  280. if (i > s_gdt_length)
  281. s_gdtr.limit = (s_gdt_length + 1) * 8 - 1;
  282. }
  283. void write_gdt_entry(word selector, Descriptor& descriptor)
  284. {
  285. write_raw_gdt_entry(selector, descriptor.low, descriptor.high);
  286. }
  287. Descriptor& get_gdt_entry(word selector)
  288. {
  289. word i = (selector & 0xfffc) >> 3;
  290. return *(Descriptor*)(&s_gdt[i]);
  291. }
  292. void flush_gdt()
  293. {
  294. s_gdtr.address = s_gdt;
  295. s_gdtr.limit = (s_gdt_length * 8) - 1;
  296. asm("lgdt %0"::"m"(s_gdtr):"memory");
  297. }
  298. void gdt_init()
  299. {
  300. s_gdt_length = 5;
  301. s_gdt_freelist = new Vector<word>();
  302. s_gdt_freelist->ensure_capacity(256);
  303. for (size_t i = s_gdt_length; i < 256; ++i)
  304. s_gdt_freelist->append(i * 8);
  305. s_gdt_length = 256;
  306. s_gdtr.address = s_gdt;
  307. s_gdtr.limit = (s_gdt_length * 8) - 1;
  308. write_raw_gdt_entry(0x0000, 0x00000000, 0x00000000);
  309. write_raw_gdt_entry(0x0008, 0x0000ffff, 0x00cf9a00);
  310. write_raw_gdt_entry(0x0010, 0x0000ffff, 0x00cf9200);
  311. write_raw_gdt_entry(0x0018, 0x0000ffff, 0x00cffa00);
  312. write_raw_gdt_entry(0x0020, 0x0000ffff, 0x00cff200);
  313. flush_gdt();
  314. asm volatile(
  315. "mov %%ax, %%ds\n"
  316. "mov %%ax, %%es\n"
  317. "mov %%ax, %%fs\n"
  318. "mov %%ax, %%gs\n"
  319. "mov %%ax, %%ss\n"
  320. :: "a"(0x10)
  321. : "memory"
  322. );
  323. // Make sure CS points to the kernel code descriptor.
  324. asm volatile(
  325. "ljmpl $0x8, $sanity\n"
  326. "sanity:\n"
  327. );
  328. }
  329. static void unimp_trap()
  330. {
  331. kprintf("Unhandled IRQ.");
  332. hang();
  333. }
  334. void register_irq_handler(byte irq, IRQHandler& handler)
  335. {
  336. ASSERT(!s_irq_handler[irq]);
  337. s_irq_handler[irq] = &handler;
  338. register_interrupt_handler(IRQ_VECTOR_BASE + irq, asm_irq_entry);
  339. }
  340. void unregister_irq_handler(byte irq, IRQHandler& handler)
  341. {
  342. ASSERT(s_irq_handler[irq] == &handler);
  343. s_irq_handler[irq] = nullptr;
  344. }
  345. void register_interrupt_handler(byte index, void (*f)())
  346. {
  347. s_idt[index].low = 0x00080000 | LSW((f));
  348. s_idt[index].high = ((dword)(f) & 0xffff0000) | 0x8e00;
  349. flush_idt();
  350. }
  351. void register_user_callable_interrupt_handler(byte index, void (*f)())
  352. {
  353. s_idt[index].low = 0x00080000 | LSW((f));
  354. s_idt[index].high = ((dword)(f) & 0xffff0000) | 0xef00;
  355. flush_idt();
  356. }
  357. void flush_idt()
  358. {
  359. asm("lidt %0"::"m"(s_idtr));
  360. }
  361. /* If an 8259 gets cranky, it'll generate a spurious IRQ7.
  362. * ATM I don't have a clear grasp on when/why this happens,
  363. * so I ignore them and assume it makes no difference.
  364. */
  365. extern "C" void irq7_handler();
  366. asm(
  367. ".globl irq7_handler \n"
  368. "irq7_handler: \n"
  369. " iret\n"
  370. );
  371. void idt_init()
  372. {
  373. s_idtr.address = s_idt;
  374. s_idtr.limit = 0x100 * 8 - 1;
  375. for (byte i = 0xff; i > 0x10; --i)
  376. register_interrupt_handler(i, unimp_trap);
  377. register_interrupt_handler(0x00, exception_0_entry);
  378. register_interrupt_handler(0x01, _exception1);
  379. register_interrupt_handler(0x02, _exception2);
  380. register_interrupt_handler(0x03, _exception3);
  381. register_interrupt_handler(0x04, _exception4);
  382. register_interrupt_handler(0x05, _exception5);
  383. register_interrupt_handler(0x06, exception_6_entry);
  384. register_interrupt_handler(0x07, exception_7_entry);
  385. register_interrupt_handler(0x08, _exception8);
  386. register_interrupt_handler(0x09, _exception9);
  387. register_interrupt_handler(0x0a, _exception10);
  388. register_interrupt_handler(0x0b, _exception11);
  389. register_interrupt_handler(0x0c, _exception12);
  390. register_interrupt_handler(0x0d, exception_13_entry);
  391. register_interrupt_handler(0x0e, exception_14_entry);
  392. register_interrupt_handler(0x0f, _exception15);
  393. register_interrupt_handler(0x10, _exception16);
  394. register_interrupt_handler(0x57, irq7_handler);
  395. for (byte i = 0; i < 16; ++i) {
  396. s_irq_handler[i] = nullptr;
  397. }
  398. flush_idt();
  399. }
  400. void load_task_register(word selector)
  401. {
  402. asm("ltr %0"::"r"(selector));
  403. }
  404. void handle_irq()
  405. {
  406. word isr = PIC::get_isr();
  407. if (!isr) {
  408. kprintf("Spurious IRQ\n");
  409. return;
  410. }
  411. byte irq = 0;
  412. for (byte i = 0; i < 16; ++i) {
  413. if (i == 2)
  414. continue;
  415. if (isr & (1 << i)) {
  416. irq = i;
  417. break;
  418. }
  419. }
  420. if (s_irq_handler[irq])
  421. s_irq_handler[irq]->handle_irq();
  422. PIC::eoi(irq);
  423. }
  424. #ifdef DEBUG
  425. void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func)
  426. {
  427. asm volatile("cli");
  428. kprintf("ASSERTION FAILED: %s\n%s:%u in %s\n", msg, file, line, func);
  429. dump_backtrace();
  430. asm volatile("hlt");
  431. for (;;);
  432. }
  433. #endif
  434. void sse_init()
  435. {
  436. asm volatile(
  437. "mov %cr0, %eax\n"
  438. "andl $0xfffffffb, %eax\n"
  439. "orl $0x2, %eax\n"
  440. "mov %eax, %cr0\n"
  441. "mov %cr4, %eax\n"
  442. "orl $0x600, %eax\n"
  443. "mov %eax, %cr4\n"
  444. );
  445. }