CPU.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <Kernel/Debug.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/String.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/Types.h>
  32. #include <Kernel/Arch/i386/CPU.h>
  33. #include <Kernel/Arch/i386/ISRStubs.h>
  34. #include <Kernel/Arch/i386/ProcessorInfo.h>
  35. #include <Kernel/Arch/i386/SafeMem.h>
  36. #include <Kernel/IO.h>
  37. #include <Kernel/Interrupts/APIC.h>
  38. #include <Kernel/Interrupts/GenericInterruptHandler.h>
  39. #include <Kernel/Interrupts/IRQHandler.h>
  40. #include <Kernel/Interrupts/InterruptManagement.h>
  41. #include <Kernel/Interrupts/SharedIRQHandler.h>
  42. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  43. #include <Kernel/Interrupts/UnhandledInterruptHandler.h>
  44. #include <Kernel/KSyms.h>
  45. #include <Kernel/Process.h>
  46. #include <Kernel/Random.h>
  47. #include <Kernel/SpinLock.h>
  48. #include <Kernel/Thread.h>
  49. #include <Kernel/VM/MemoryManager.h>
  50. #include <Kernel/VM/PageDirectory.h>
  51. #include <Kernel/VM/ProcessPagingScope.h>
  52. #include <LibC/mallocdefs.h>
  53. namespace Kernel {
  54. static DescriptorTablePointer s_idtr;
  55. static Descriptor s_idt[256];
  56. static GenericInterruptHandler* s_interrupt_handler[GENERIC_INTERRUPT_HANDLERS_COUNT];
  57. static EntropySource s_entropy_source_interrupts{EntropySource::Static::Interrupts};
  58. // The compiler can't see the calls to these functions inside assembly.
  59. // Declare them, to avoid dead code warnings.
  60. extern "C" void enter_thread_context(Thread* from_thread, Thread* to_thread);
  61. extern "C" void context_first_init(Thread* from_thread, Thread* to_thread, TrapFrame* trap);
  62. extern "C" u32 do_init_context(Thread* thread, u32 flags);
  63. extern "C" void exit_kernel_thread(void);
  64. extern "C" void pre_init_finished(void);
  65. extern "C" void post_init_finished(void);
  66. extern "C" void handle_interrupt(TrapFrame*);
  67. #define EH_ENTRY(ec, title) \
  68. extern "C" void title##_asm_entry(); \
  69. extern "C" void title##_handler(TrapFrame*); \
  70. asm( \
  71. ".globl " #title "_asm_entry\n" \
  72. "" #title "_asm_entry: \n" \
  73. " pusha\n" \
  74. " pushl %ds\n" \
  75. " pushl %es\n" \
  76. " pushl %fs\n" \
  77. " pushl %gs\n" \
  78. " pushl %ss\n" \
  79. " mov $" __STRINGIFY(GDT_SELECTOR_DATA0) ", %ax\n" \
  80. " mov %ax, %ds\n" \
  81. " mov %ax, %es\n" \
  82. " mov $" __STRINGIFY(GDT_SELECTOR_PROC) ", %ax\n" \
  83. " mov %ax, %fs\n" \
  84. " pushl %esp \n" /* set TrapFrame::regs */ \
  85. " subl $" __STRINGIFY(TRAP_FRAME_SIZE - 4) ", %esp \n" \
  86. " pushl %esp \n" \
  87. " cld\n" \
  88. " call enter_trap_no_irq \n" \
  89. " call " #title "_handler\n" \
  90. " jmp common_trap_exit \n");
  91. #define EH_ENTRY_NO_CODE(ec, title) \
  92. extern "C" void title##_handler(TrapFrame*); \
  93. extern "C" void title##_asm_entry(); \
  94. asm( \
  95. ".globl " #title "_asm_entry\n" \
  96. "" #title "_asm_entry: \n" \
  97. " pushl $0x0\n" \
  98. " pusha\n" \
  99. " pushl %ds\n" \
  100. " pushl %es\n" \
  101. " pushl %fs\n" \
  102. " pushl %gs\n" \
  103. " pushl %ss\n" \
  104. " mov $" __STRINGIFY(GDT_SELECTOR_DATA0) ", %ax\n" \
  105. " mov %ax, %ds\n" \
  106. " mov %ax, %es\n" \
  107. " mov $" __STRINGIFY(GDT_SELECTOR_PROC) ", %ax\n" \
  108. " mov %ax, %fs\n" \
  109. " pushl %esp \n" /* set TrapFrame::regs */ \
  110. " subl $" __STRINGIFY(TRAP_FRAME_SIZE - 4) ", %esp \n" \
  111. " pushl %esp \n" \
  112. " cld\n" \
  113. " call enter_trap_no_irq \n" \
  114. " call " #title "_handler\n" \
  115. " jmp common_trap_exit \n");
  116. static void dump(const RegisterState& regs)
  117. {
  118. u16 ss;
  119. u32 esp;
  120. if (!(regs.cs & 3)) {
  121. ss = regs.ss;
  122. esp = regs.esp;
  123. } else {
  124. ss = regs.userspace_ss;
  125. esp = regs.userspace_esp;
  126. }
  127. klog() << "exception code: " << String::format("%04x", regs.exception_code) << " (isr: " << String::format("%04x", regs.isr_number);
  128. klog() << " pc=" << String::format("%04x", (u16)regs.cs) << ":" << String::format("%08x", regs.eip) << " flags=" << String::format("%04x", (u16)regs.eflags);
  129. klog() << " stk=" << String::format("%04x", ss) << ":" << String::format("%08x", esp);
  130. klog() << " ds=" << String::format("%04x", (u16)regs.ds) << " es=" << String::format("%04x", (u16)regs.es) << " fs=" << String::format("%04x", (u16)regs.fs) << " gs=" << String::format("%04x", (u16)regs.gs);
  131. klog() << "eax=" << String::format("%08x", regs.eax) << " ebx=" << String::format("%08x", regs.ebx) << " ecx=" << String::format("%08x", regs.ecx) << " edx=" << String::format("%08x", regs.edx);
  132. klog() << "ebp=" << String::format("%08x", regs.ebp) << " esp=" << String::format("%08x", regs.esp) << " esi=" << String::format("%08x", regs.esi) << " edi=" << String::format("%08x", regs.edi);
  133. u32 cr0;
  134. asm("movl %%cr0, %%eax"
  135. : "=a"(cr0));
  136. u32 cr2;
  137. asm("movl %%cr2, %%eax"
  138. : "=a"(cr2));
  139. u32 cr3 = read_cr3();
  140. u32 cr4;
  141. asm("movl %%cr4, %%eax"
  142. : "=a"(cr4));
  143. klog() << "cr0=" << String::format("%08x", cr0) << " cr2=" << String::format("%08x", cr2) << " cr3=" << String::format("%08x", cr3) << " cr4=" << String::format("%08x", cr4);
  144. auto process = Process::current();
  145. u8 code[8];
  146. void* fault_at;
  147. if (process && safe_memcpy(code, (void*)regs.eip, 8, fault_at)) {
  148. SmapDisabler disabler;
  149. klog() << "code: " << String::format("%02x", code[0]) << " " << String::format("%02x", code[1]) << " " << String::format("%02x", code[2]) << " " << String::format("%02x", code[3]) << " " << String::format("%02x", code[4]) << " " << String::format("%02x", code[5]) << " " << String::format("%02x", code[6]) << " " << String::format("%02x", code[7]);
  150. }
  151. }
  152. void handle_crash(RegisterState& regs, const char* description, int signal, bool out_of_memory)
  153. {
  154. auto process = Process::current();
  155. if (!process) {
  156. klog() << description << " with !current";
  157. Processor::halt();
  158. }
  159. // If a process crashed while inspecting another process,
  160. // make sure we switch back to the right page tables.
  161. MM.enter_process_paging_scope(*process);
  162. klog() << "CRASH: CPU #" << Processor::current().id() << " " << description << ". Ring " << (regs.cs & 3) << ".";
  163. dump(regs);
  164. if (!(regs.cs & 3)) {
  165. klog() << "Crash in ring 0 :(";
  166. dump_backtrace();
  167. Processor::halt();
  168. }
  169. cli();
  170. process->crash(signal, regs.eip, out_of_memory);
  171. }
  172. EH_ENTRY_NO_CODE(6, illegal_instruction);
  173. void illegal_instruction_handler(TrapFrame* trap)
  174. {
  175. clac();
  176. handle_crash(*trap->regs, "Illegal instruction", SIGILL);
  177. }
  178. EH_ENTRY_NO_CODE(0, divide_error);
  179. void divide_error_handler(TrapFrame* trap)
  180. {
  181. clac();
  182. handle_crash(*trap->regs, "Divide error", SIGFPE);
  183. }
  184. EH_ENTRY(13, general_protection_fault);
  185. void general_protection_fault_handler(TrapFrame* trap)
  186. {
  187. clac();
  188. handle_crash(*trap->regs, "General protection fault", SIGSEGV);
  189. }
  190. // 7: FPU not available exception
  191. EH_ENTRY_NO_CODE(7, fpu_exception);
  192. void fpu_exception_handler(TrapFrame*)
  193. {
  194. // Just clear the TS flag. We've already restored the FPU state eagerly.
  195. // FIXME: It would be nice if we didn't have to do this at all.
  196. asm volatile("clts");
  197. }
  198. // 14: Page Fault
  199. EH_ENTRY(14, page_fault);
  200. void page_fault_handler(TrapFrame* trap)
  201. {
  202. clac();
  203. auto& regs = *trap->regs;
  204. u32 fault_address;
  205. asm("movl %%cr2, %%eax"
  206. : "=a"(fault_address));
  207. if constexpr (PAGE_FAULT_DEBUG) {
  208. u32 fault_page_directory = read_cr3();
  209. dbgln("CPU #{} ring {} {} page fault in PD={:#x}, {}{} {}",
  210. Processor::is_initialized() ? Processor::current().id() : 0,
  211. regs.cs & 3,
  212. regs.exception_code & 1 ? "PV" : "NP",
  213. fault_page_directory,
  214. regs.exception_code & 8 ? "reserved-bit " : "",
  215. regs.exception_code & 2 ? "write" : "read",
  216. VirtualAddress(fault_address));
  217. dump(regs);
  218. }
  219. bool faulted_in_kernel = !(regs.cs & 3);
  220. if (faulted_in_kernel && Processor::current().in_irq()) {
  221. // If we're faulting in an IRQ handler, first check if we failed
  222. // due to safe_memcpy, safe_strnlen, or safe_memset. If we did,
  223. // gracefully continue immediately. Because we're in an IRQ handler
  224. // we can't really try to resolve the page fault in a meaningful
  225. // way, so we need to do this before calling into
  226. // MemoryManager::handle_page_fault, which would just bail and
  227. // request a crash
  228. if (handle_safe_access_fault(regs, fault_address))
  229. return;
  230. }
  231. auto current_thread = Thread::current();
  232. if (current_thread)
  233. current_thread->set_handling_page_fault(true);
  234. ScopeGuard guard = [current_thread] {
  235. if (current_thread)
  236. current_thread->set_handling_page_fault(false);
  237. };
  238. if (!faulted_in_kernel && !MM.validate_user_stack(current_thread->process(), VirtualAddress(regs.userspace_esp))) {
  239. dbgln("Invalid stack pointer: {}", VirtualAddress(regs.userspace_esp));
  240. handle_crash(regs, "Bad stack on page fault", SIGSTKFLT);
  241. ASSERT_NOT_REACHED();
  242. }
  243. auto response = MM.handle_page_fault(PageFault(regs.exception_code, VirtualAddress(fault_address)));
  244. if (response == PageFaultResponse::ShouldCrash || response == PageFaultResponse::OutOfMemory) {
  245. if (faulted_in_kernel && handle_safe_access_fault(regs, fault_address)) {
  246. // If this would be a ring0 (kernel) fault and the fault was triggered by
  247. // safe_memcpy, safe_strnlen, or safe_memset then we resume execution at
  248. // the appropriate _fault label rather than crashing
  249. return;
  250. }
  251. if (response != PageFaultResponse::OutOfMemory) {
  252. if (current_thread->has_signal_handler(SIGSEGV)) {
  253. current_thread->send_urgent_signal_to_self(SIGSEGV);
  254. return;
  255. }
  256. }
  257. dbgln("Unrecoverable page fault, {}{}{} address {}",
  258. regs.exception_code & PageFaultFlags::ReservedBitViolation ? "reserved bit violation / " : "",
  259. regs.exception_code & PageFaultFlags::InstructionFetch ? "instruction fetch / " : "",
  260. regs.exception_code & PageFaultFlags::Write ? "write to" : "read from",
  261. VirtualAddress(fault_address));
  262. u32 malloc_scrub_pattern = explode_byte(MALLOC_SCRUB_BYTE);
  263. u32 free_scrub_pattern = explode_byte(FREE_SCRUB_BYTE);
  264. u32 kmalloc_scrub_pattern = explode_byte(KMALLOC_SCRUB_BYTE);
  265. u32 kfree_scrub_pattern = explode_byte(KFREE_SCRUB_BYTE);
  266. u32 slab_alloc_scrub_pattern = explode_byte(SLAB_ALLOC_SCRUB_BYTE);
  267. u32 slab_dealloc_scrub_pattern = explode_byte(SLAB_DEALLOC_SCRUB_BYTE);
  268. if ((fault_address & 0xffff0000) == (malloc_scrub_pattern & 0xffff0000)) {
  269. dbgln("Note: Address {} looks like it may be uninitialized malloc() memory", VirtualAddress(fault_address));
  270. } else if ((fault_address & 0xffff0000) == (free_scrub_pattern & 0xffff0000)) {
  271. dbgln("Note: Address {} looks like it may be recently free()'d memory", VirtualAddress(fault_address));
  272. } else if ((fault_address & 0xffff0000) == (kmalloc_scrub_pattern & 0xffff0000)) {
  273. dbgln("Note: Address {} looks like it may be uninitialized kmalloc() memory", VirtualAddress(fault_address));
  274. } else if ((fault_address & 0xffff0000) == (kfree_scrub_pattern & 0xffff0000)) {
  275. dbgln("Note: Address {} looks like it may be recently kfree()'d memory", VirtualAddress(fault_address));
  276. } else if ((fault_address & 0xffff0000) == (slab_alloc_scrub_pattern & 0xffff0000)) {
  277. dbgln("Note: Address {} looks like it may be uninitialized slab_alloc() memory", VirtualAddress(fault_address));
  278. } else if ((fault_address & 0xffff0000) == (slab_dealloc_scrub_pattern & 0xffff0000)) {
  279. dbgln("Note: Address {} looks like it may be recently slab_dealloc()'d memory", VirtualAddress(fault_address));
  280. } else if (fault_address < 4096) {
  281. dbgln("Note: Address {} looks like a possible nullptr dereference", VirtualAddress(fault_address));
  282. }
  283. handle_crash(regs, "Page Fault", SIGSEGV, response == PageFaultResponse::OutOfMemory);
  284. } else if (response == PageFaultResponse::Continue) {
  285. #if PAGE_FAULT_DEBUG
  286. dbgln("Continuing after resolved page fault");
  287. #endif
  288. } else {
  289. ASSERT_NOT_REACHED();
  290. }
  291. }
  292. EH_ENTRY_NO_CODE(1, debug);
  293. void debug_handler(TrapFrame* trap)
  294. {
  295. clac();
  296. auto& regs = *trap->regs;
  297. auto current_thread = Thread::current();
  298. auto& process = current_thread->process();
  299. if ((regs.cs & 3) == 0) {
  300. klog() << "Debug Exception in Ring0";
  301. Processor::halt();
  302. return;
  303. }
  304. constexpr u8 REASON_SINGLESTEP = 14;
  305. bool is_reason_singlestep = (read_dr6() & (1 << REASON_SINGLESTEP));
  306. if (!is_reason_singlestep)
  307. return;
  308. if (auto tracer = process.tracer()) {
  309. tracer->set_regs(regs);
  310. }
  311. current_thread->send_urgent_signal_to_self(SIGTRAP);
  312. }
  313. EH_ENTRY_NO_CODE(3, breakpoint);
  314. void breakpoint_handler(TrapFrame* trap)
  315. {
  316. clac();
  317. auto& regs = *trap->regs;
  318. auto current_thread = Thread::current();
  319. auto& process = current_thread->process();
  320. if ((regs.cs & 3) == 0) {
  321. klog() << "Breakpoint Trap in Ring0";
  322. Processor::halt();
  323. return;
  324. }
  325. if (auto tracer = process.tracer()) {
  326. tracer->set_regs(regs);
  327. }
  328. current_thread->send_urgent_signal_to_self(SIGTRAP);
  329. }
  330. #define EH(i, msg) \
  331. static void _exception##i() \
  332. { \
  333. klog() << msg; \
  334. u32 cr0, cr2, cr3, cr4; \
  335. asm("movl %%cr0, %%eax" \
  336. : "=a"(cr0)); \
  337. asm("movl %%cr2, %%eax" \
  338. : "=a"(cr2)); \
  339. asm("movl %%cr3, %%eax" \
  340. : "=a"(cr3)); \
  341. asm("movl %%cr4, %%eax" \
  342. : "=a"(cr4)); \
  343. klog() << "CR0=" << String::format("%x", cr0) << " CR2=" << String::format("%x", cr2) << " CR3=" << String::format("%x", cr3) << " CR4=" << String::format("%x", cr4); \
  344. Processor::halt(); \
  345. }
  346. EH(2, "Unknown error")
  347. EH(4, "Overflow")
  348. EH(5, "Bounds check")
  349. EH(8, "Double fault")
  350. EH(9, "Coprocessor segment overrun")
  351. EH(10, "Invalid TSS")
  352. EH(11, "Segment not present")
  353. EH(12, "Stack exception")
  354. EH(15, "Unknown error")
  355. EH(16, "Coprocessor error")
  356. const DescriptorTablePointer& get_idtr()
  357. {
  358. return s_idtr;
  359. }
  360. static void unimp_trap()
  361. {
  362. klog() << "Unhandled IRQ.";
  363. Processor::Processor::halt();
  364. }
  365. GenericInterruptHandler& get_interrupt_handler(u8 interrupt_number)
  366. {
  367. ASSERT(s_interrupt_handler[interrupt_number] != nullptr);
  368. return *s_interrupt_handler[interrupt_number];
  369. }
  370. static void revert_to_unused_handler(u8 interrupt_number)
  371. {
  372. new UnhandledInterruptHandler(interrupt_number);
  373. }
  374. void register_generic_interrupt_handler(u8 interrupt_number, GenericInterruptHandler& handler)
  375. {
  376. ASSERT(interrupt_number < GENERIC_INTERRUPT_HANDLERS_COUNT);
  377. if (s_interrupt_handler[interrupt_number] != nullptr) {
  378. if (s_interrupt_handler[interrupt_number]->type() == HandlerType::UnhandledInterruptHandler) {
  379. s_interrupt_handler[interrupt_number] = &handler;
  380. return;
  381. }
  382. if (s_interrupt_handler[interrupt_number]->is_shared_handler() && !s_interrupt_handler[interrupt_number]->is_sharing_with_others()) {
  383. ASSERT(s_interrupt_handler[interrupt_number]->type() == HandlerType::SharedIRQHandler);
  384. static_cast<SharedIRQHandler*>(s_interrupt_handler[interrupt_number])->register_handler(handler);
  385. return;
  386. }
  387. if (!s_interrupt_handler[interrupt_number]->is_shared_handler()) {
  388. if (s_interrupt_handler[interrupt_number]->type() == HandlerType::SpuriousInterruptHandler) {
  389. static_cast<SpuriousInterruptHandler*>(s_interrupt_handler[interrupt_number])->register_handler(handler);
  390. return;
  391. }
  392. ASSERT(s_interrupt_handler[interrupt_number]->type() == HandlerType::IRQHandler);
  393. auto& previous_handler = *s_interrupt_handler[interrupt_number];
  394. s_interrupt_handler[interrupt_number] = nullptr;
  395. SharedIRQHandler::initialize(interrupt_number);
  396. static_cast<SharedIRQHandler*>(s_interrupt_handler[interrupt_number])->register_handler(previous_handler);
  397. static_cast<SharedIRQHandler*>(s_interrupt_handler[interrupt_number])->register_handler(handler);
  398. return;
  399. }
  400. ASSERT_NOT_REACHED();
  401. } else {
  402. s_interrupt_handler[interrupt_number] = &handler;
  403. }
  404. }
  405. void unregister_generic_interrupt_handler(u8 interrupt_number, GenericInterruptHandler& handler)
  406. {
  407. ASSERT(s_interrupt_handler[interrupt_number] != nullptr);
  408. if (s_interrupt_handler[interrupt_number]->type() == HandlerType::UnhandledInterruptHandler) {
  409. dbgln("Trying to unregister unused handler (?)");
  410. return;
  411. }
  412. if (s_interrupt_handler[interrupt_number]->is_shared_handler() && !s_interrupt_handler[interrupt_number]->is_sharing_with_others()) {
  413. ASSERT(s_interrupt_handler[interrupt_number]->type() == HandlerType::SharedIRQHandler);
  414. static_cast<SharedIRQHandler*>(s_interrupt_handler[interrupt_number])->unregister_handler(handler);
  415. if (!static_cast<SharedIRQHandler*>(s_interrupt_handler[interrupt_number])->sharing_devices_count()) {
  416. revert_to_unused_handler(interrupt_number);
  417. }
  418. return;
  419. }
  420. if (!s_interrupt_handler[interrupt_number]->is_shared_handler()) {
  421. ASSERT(s_interrupt_handler[interrupt_number]->type() == HandlerType::IRQHandler);
  422. revert_to_unused_handler(interrupt_number);
  423. return;
  424. }
  425. ASSERT_NOT_REACHED();
  426. }
  427. void register_interrupt_handler(u8 index, void (*f)())
  428. {
  429. s_idt[index].low = 0x00080000 | LSW((f));
  430. s_idt[index].high = ((u32)(f)&0xffff0000) | 0x8e00;
  431. }
  432. void register_user_callable_interrupt_handler(u8 index, void (*f)())
  433. {
  434. s_idt[index].low = 0x00080000 | LSW((f));
  435. s_idt[index].high = ((u32)(f)&0xffff0000) | 0xef00;
  436. }
  437. void flush_idt()
  438. {
  439. asm("lidt %0" ::"m"(s_idtr));
  440. }
  441. static void idt_init()
  442. {
  443. s_idtr.address = s_idt;
  444. s_idtr.limit = 256 * 8 - 1;
  445. register_interrupt_handler(0x00, divide_error_asm_entry);
  446. register_user_callable_interrupt_handler(0x01, debug_asm_entry);
  447. register_interrupt_handler(0x02, _exception2);
  448. register_user_callable_interrupt_handler(0x03, breakpoint_asm_entry);
  449. register_interrupt_handler(0x04, _exception4);
  450. register_interrupt_handler(0x05, _exception5);
  451. register_interrupt_handler(0x06, illegal_instruction_asm_entry);
  452. register_interrupt_handler(0x07, fpu_exception_asm_entry);
  453. register_interrupt_handler(0x08, _exception8);
  454. register_interrupt_handler(0x09, _exception9);
  455. register_interrupt_handler(0x0a, _exception10);
  456. register_interrupt_handler(0x0b, _exception11);
  457. register_interrupt_handler(0x0c, _exception12);
  458. register_interrupt_handler(0x0d, general_protection_fault_asm_entry);
  459. register_interrupt_handler(0x0e, page_fault_asm_entry);
  460. register_interrupt_handler(0x0f, _exception15);
  461. register_interrupt_handler(0x10, _exception16);
  462. for (u8 i = 0x11; i < 0x50; i++)
  463. register_interrupt_handler(i, unimp_trap);
  464. register_interrupt_handler(0x50, interrupt_80_asm_entry);
  465. register_interrupt_handler(0x51, interrupt_81_asm_entry);
  466. register_interrupt_handler(0x52, interrupt_82_asm_entry);
  467. register_interrupt_handler(0x53, interrupt_83_asm_entry);
  468. register_interrupt_handler(0x54, interrupt_84_asm_entry);
  469. register_interrupt_handler(0x55, interrupt_85_asm_entry);
  470. register_interrupt_handler(0x56, interrupt_86_asm_entry);
  471. register_interrupt_handler(0x57, interrupt_87_asm_entry);
  472. register_interrupt_handler(0x58, interrupt_88_asm_entry);
  473. register_interrupt_handler(0x59, interrupt_89_asm_entry);
  474. register_interrupt_handler(0x5a, interrupt_90_asm_entry);
  475. register_interrupt_handler(0x5b, interrupt_91_asm_entry);
  476. register_interrupt_handler(0x5c, interrupt_92_asm_entry);
  477. register_interrupt_handler(0x5d, interrupt_93_asm_entry);
  478. register_interrupt_handler(0x5e, interrupt_94_asm_entry);
  479. register_interrupt_handler(0x5f, interrupt_95_asm_entry);
  480. register_interrupt_handler(0x60, interrupt_96_asm_entry);
  481. register_interrupt_handler(0x61, interrupt_97_asm_entry);
  482. register_interrupt_handler(0x62, interrupt_98_asm_entry);
  483. register_interrupt_handler(0x63, interrupt_99_asm_entry);
  484. register_interrupt_handler(0x64, interrupt_100_asm_entry);
  485. register_interrupt_handler(0x65, interrupt_101_asm_entry);
  486. register_interrupt_handler(0x66, interrupt_102_asm_entry);
  487. register_interrupt_handler(0x67, interrupt_103_asm_entry);
  488. register_interrupt_handler(0x68, interrupt_104_asm_entry);
  489. register_interrupt_handler(0x69, interrupt_105_asm_entry);
  490. register_interrupt_handler(0x6a, interrupt_106_asm_entry);
  491. register_interrupt_handler(0x6b, interrupt_107_asm_entry);
  492. register_interrupt_handler(0x6c, interrupt_108_asm_entry);
  493. register_interrupt_handler(0x6d, interrupt_109_asm_entry);
  494. register_interrupt_handler(0x6e, interrupt_110_asm_entry);
  495. register_interrupt_handler(0x6f, interrupt_111_asm_entry);
  496. register_interrupt_handler(0x70, interrupt_112_asm_entry);
  497. register_interrupt_handler(0x71, interrupt_113_asm_entry);
  498. register_interrupt_handler(0x72, interrupt_114_asm_entry);
  499. register_interrupt_handler(0x73, interrupt_115_asm_entry);
  500. register_interrupt_handler(0x74, interrupt_116_asm_entry);
  501. register_interrupt_handler(0x75, interrupt_117_asm_entry);
  502. register_interrupt_handler(0x76, interrupt_118_asm_entry);
  503. register_interrupt_handler(0x77, interrupt_119_asm_entry);
  504. register_interrupt_handler(0x78, interrupt_120_asm_entry);
  505. register_interrupt_handler(0x79, interrupt_121_asm_entry);
  506. register_interrupt_handler(0x7a, interrupt_122_asm_entry);
  507. register_interrupt_handler(0x7b, interrupt_123_asm_entry);
  508. register_interrupt_handler(0x7c, interrupt_124_asm_entry);
  509. register_interrupt_handler(0x7d, interrupt_125_asm_entry);
  510. register_interrupt_handler(0x7e, interrupt_126_asm_entry);
  511. register_interrupt_handler(0x7f, interrupt_127_asm_entry);
  512. register_interrupt_handler(0x80, interrupt_128_asm_entry);
  513. register_interrupt_handler(0x81, interrupt_129_asm_entry);
  514. register_interrupt_handler(0x82, interrupt_130_asm_entry);
  515. register_interrupt_handler(0x83, interrupt_131_asm_entry);
  516. register_interrupt_handler(0x84, interrupt_132_asm_entry);
  517. register_interrupt_handler(0x85, interrupt_133_asm_entry);
  518. register_interrupt_handler(0x86, interrupt_134_asm_entry);
  519. register_interrupt_handler(0x87, interrupt_135_asm_entry);
  520. register_interrupt_handler(0x88, interrupt_136_asm_entry);
  521. register_interrupt_handler(0x89, interrupt_137_asm_entry);
  522. register_interrupt_handler(0x8a, interrupt_138_asm_entry);
  523. register_interrupt_handler(0x8b, interrupt_139_asm_entry);
  524. register_interrupt_handler(0x8c, interrupt_140_asm_entry);
  525. register_interrupt_handler(0x8d, interrupt_141_asm_entry);
  526. register_interrupt_handler(0x8e, interrupt_142_asm_entry);
  527. register_interrupt_handler(0x8f, interrupt_143_asm_entry);
  528. register_interrupt_handler(0x90, interrupt_144_asm_entry);
  529. register_interrupt_handler(0x91, interrupt_145_asm_entry);
  530. register_interrupt_handler(0x92, interrupt_146_asm_entry);
  531. register_interrupt_handler(0x93, interrupt_147_asm_entry);
  532. register_interrupt_handler(0x94, interrupt_148_asm_entry);
  533. register_interrupt_handler(0x95, interrupt_149_asm_entry);
  534. register_interrupt_handler(0x96, interrupt_150_asm_entry);
  535. register_interrupt_handler(0x97, interrupt_151_asm_entry);
  536. register_interrupt_handler(0x98, interrupt_152_asm_entry);
  537. register_interrupt_handler(0x99, interrupt_153_asm_entry);
  538. register_interrupt_handler(0x9a, interrupt_154_asm_entry);
  539. register_interrupt_handler(0x9b, interrupt_155_asm_entry);
  540. register_interrupt_handler(0x9c, interrupt_156_asm_entry);
  541. register_interrupt_handler(0x9d, interrupt_157_asm_entry);
  542. register_interrupt_handler(0x9e, interrupt_158_asm_entry);
  543. register_interrupt_handler(0x9f, interrupt_159_asm_entry);
  544. register_interrupt_handler(0xa0, interrupt_160_asm_entry);
  545. register_interrupt_handler(0xa1, interrupt_161_asm_entry);
  546. register_interrupt_handler(0xa2, interrupt_162_asm_entry);
  547. register_interrupt_handler(0xa3, interrupt_163_asm_entry);
  548. register_interrupt_handler(0xa4, interrupt_164_asm_entry);
  549. register_interrupt_handler(0xa5, interrupt_165_asm_entry);
  550. register_interrupt_handler(0xa6, interrupt_166_asm_entry);
  551. register_interrupt_handler(0xa7, interrupt_167_asm_entry);
  552. register_interrupt_handler(0xa8, interrupt_168_asm_entry);
  553. register_interrupt_handler(0xa9, interrupt_169_asm_entry);
  554. register_interrupt_handler(0xaa, interrupt_170_asm_entry);
  555. register_interrupt_handler(0xab, interrupt_171_asm_entry);
  556. register_interrupt_handler(0xac, interrupt_172_asm_entry);
  557. register_interrupt_handler(0xad, interrupt_173_asm_entry);
  558. register_interrupt_handler(0xae, interrupt_174_asm_entry);
  559. register_interrupt_handler(0xaf, interrupt_175_asm_entry);
  560. register_interrupt_handler(0xb0, interrupt_176_asm_entry);
  561. register_interrupt_handler(0xb1, interrupt_177_asm_entry);
  562. register_interrupt_handler(0xb2, interrupt_178_asm_entry);
  563. register_interrupt_handler(0xb3, interrupt_179_asm_entry);
  564. register_interrupt_handler(0xb4, interrupt_180_asm_entry);
  565. register_interrupt_handler(0xb5, interrupt_181_asm_entry);
  566. register_interrupt_handler(0xb6, interrupt_182_asm_entry);
  567. register_interrupt_handler(0xb7, interrupt_183_asm_entry);
  568. register_interrupt_handler(0xb8, interrupt_184_asm_entry);
  569. register_interrupt_handler(0xb9, interrupt_185_asm_entry);
  570. register_interrupt_handler(0xba, interrupt_186_asm_entry);
  571. register_interrupt_handler(0xbb, interrupt_187_asm_entry);
  572. register_interrupt_handler(0xbc, interrupt_188_asm_entry);
  573. register_interrupt_handler(0xbd, interrupt_189_asm_entry);
  574. register_interrupt_handler(0xbe, interrupt_190_asm_entry);
  575. register_interrupt_handler(0xbf, interrupt_191_asm_entry);
  576. register_interrupt_handler(0xc0, interrupt_192_asm_entry);
  577. register_interrupt_handler(0xc1, interrupt_193_asm_entry);
  578. register_interrupt_handler(0xc2, interrupt_194_asm_entry);
  579. register_interrupt_handler(0xc3, interrupt_195_asm_entry);
  580. register_interrupt_handler(0xc4, interrupt_196_asm_entry);
  581. register_interrupt_handler(0xc5, interrupt_197_asm_entry);
  582. register_interrupt_handler(0xc6, interrupt_198_asm_entry);
  583. register_interrupt_handler(0xc7, interrupt_199_asm_entry);
  584. register_interrupt_handler(0xc8, interrupt_200_asm_entry);
  585. register_interrupt_handler(0xc9, interrupt_201_asm_entry);
  586. register_interrupt_handler(0xca, interrupt_202_asm_entry);
  587. register_interrupt_handler(0xcb, interrupt_203_asm_entry);
  588. register_interrupt_handler(0xcc, interrupt_204_asm_entry);
  589. register_interrupt_handler(0xcd, interrupt_205_asm_entry);
  590. register_interrupt_handler(0xce, interrupt_206_asm_entry);
  591. register_interrupt_handler(0xcf, interrupt_207_asm_entry);
  592. register_interrupt_handler(0xd0, interrupt_208_asm_entry);
  593. register_interrupt_handler(0xd1, interrupt_209_asm_entry);
  594. register_interrupt_handler(0xd2, interrupt_210_asm_entry);
  595. register_interrupt_handler(0xd3, interrupt_211_asm_entry);
  596. register_interrupt_handler(0xd4, interrupt_212_asm_entry);
  597. register_interrupt_handler(0xd5, interrupt_213_asm_entry);
  598. register_interrupt_handler(0xd6, interrupt_214_asm_entry);
  599. register_interrupt_handler(0xd7, interrupt_215_asm_entry);
  600. register_interrupt_handler(0xd8, interrupt_216_asm_entry);
  601. register_interrupt_handler(0xd9, interrupt_217_asm_entry);
  602. register_interrupt_handler(0xda, interrupt_218_asm_entry);
  603. register_interrupt_handler(0xdb, interrupt_219_asm_entry);
  604. register_interrupt_handler(0xdc, interrupt_220_asm_entry);
  605. register_interrupt_handler(0xdd, interrupt_221_asm_entry);
  606. register_interrupt_handler(0xde, interrupt_222_asm_entry);
  607. register_interrupt_handler(0xdf, interrupt_223_asm_entry);
  608. register_interrupt_handler(0xe0, interrupt_224_asm_entry);
  609. register_interrupt_handler(0xe1, interrupt_225_asm_entry);
  610. register_interrupt_handler(0xe2, interrupt_226_asm_entry);
  611. register_interrupt_handler(0xe3, interrupt_227_asm_entry);
  612. register_interrupt_handler(0xe4, interrupt_228_asm_entry);
  613. register_interrupt_handler(0xe5, interrupt_229_asm_entry);
  614. register_interrupt_handler(0xe6, interrupt_230_asm_entry);
  615. register_interrupt_handler(0xe7, interrupt_231_asm_entry);
  616. register_interrupt_handler(0xe8, interrupt_232_asm_entry);
  617. register_interrupt_handler(0xe9, interrupt_233_asm_entry);
  618. register_interrupt_handler(0xea, interrupt_234_asm_entry);
  619. register_interrupt_handler(0xeb, interrupt_235_asm_entry);
  620. register_interrupt_handler(0xec, interrupt_236_asm_entry);
  621. register_interrupt_handler(0xed, interrupt_237_asm_entry);
  622. register_interrupt_handler(0xee, interrupt_238_asm_entry);
  623. register_interrupt_handler(0xef, interrupt_239_asm_entry);
  624. register_interrupt_handler(0xf0, interrupt_240_asm_entry);
  625. register_interrupt_handler(0xf1, interrupt_241_asm_entry);
  626. register_interrupt_handler(0xf2, interrupt_242_asm_entry);
  627. register_interrupt_handler(0xf3, interrupt_243_asm_entry);
  628. register_interrupt_handler(0xf4, interrupt_244_asm_entry);
  629. register_interrupt_handler(0xf5, interrupt_245_asm_entry);
  630. register_interrupt_handler(0xf6, interrupt_246_asm_entry);
  631. register_interrupt_handler(0xf7, interrupt_247_asm_entry);
  632. register_interrupt_handler(0xf8, interrupt_248_asm_entry);
  633. register_interrupt_handler(0xf9, interrupt_249_asm_entry);
  634. register_interrupt_handler(0xfa, interrupt_250_asm_entry);
  635. register_interrupt_handler(0xfb, interrupt_251_asm_entry);
  636. register_interrupt_handler(0xfc, interrupt_252_asm_entry);
  637. register_interrupt_handler(0xfd, interrupt_253_asm_entry);
  638. register_interrupt_handler(0xfe, interrupt_254_asm_entry);
  639. register_interrupt_handler(0xff, interrupt_255_asm_entry);
  640. dbgln("Installing Unhandled Handlers");
  641. for (u8 i = 0; i < GENERIC_INTERRUPT_HANDLERS_COUNT; ++i) {
  642. new UnhandledInterruptHandler(i);
  643. }
  644. flush_idt();
  645. }
  646. void load_task_register(u16 selector)
  647. {
  648. asm("ltr %0" ::"r"(selector));
  649. }
  650. void handle_interrupt(TrapFrame* trap)
  651. {
  652. clac();
  653. auto& regs = *trap->regs;
  654. ASSERT(regs.isr_number >= IRQ_VECTOR_BASE && regs.isr_number <= (IRQ_VECTOR_BASE + GENERIC_INTERRUPT_HANDLERS_COUNT));
  655. u8 irq = (u8)(regs.isr_number - 0x50);
  656. s_entropy_source_interrupts.add_random_event(irq);
  657. auto* handler = s_interrupt_handler[irq];
  658. ASSERT(handler);
  659. handler->increment_invoking_counter();
  660. handler->handle_interrupt(regs);
  661. handler->eoi();
  662. }
  663. void enter_trap_no_irq(TrapFrame* trap)
  664. {
  665. Processor::current().enter_trap(*trap, false);
  666. }
  667. void enter_trap(TrapFrame* trap)
  668. {
  669. Processor::current().enter_trap(*trap, true);
  670. }
  671. void exit_trap(TrapFrame* trap)
  672. {
  673. return Processor::current().exit_trap(*trap);
  674. }
  675. static void sse_init()
  676. {
  677. asm volatile(
  678. "mov %cr0, %eax\n"
  679. "andl $0xfffffffb, %eax\n"
  680. "orl $0x2, %eax\n"
  681. "mov %eax, %cr0\n"
  682. "mov %cr4, %eax\n"
  683. "orl $0x600, %eax\n"
  684. "mov %eax, %cr4\n");
  685. }
  686. u32 read_cr0()
  687. {
  688. u32 cr0;
  689. asm("movl %%cr0, %%eax"
  690. : "=a"(cr0));
  691. return cr0;
  692. }
  693. u32 read_cr3()
  694. {
  695. u32 cr3;
  696. asm("movl %%cr3, %%eax"
  697. : "=a"(cr3));
  698. return cr3;
  699. }
  700. void write_cr3(u32 cr3)
  701. {
  702. // NOTE: If you're here from a GPF crash, it's very likely that a PDPT entry is incorrect, not this!
  703. asm volatile("movl %%eax, %%cr3" ::"a"(cr3)
  704. : "memory");
  705. }
  706. u32 read_cr4()
  707. {
  708. u32 cr4;
  709. asm("movl %%cr4, %%eax"
  710. : "=a"(cr4));
  711. return cr4;
  712. }
  713. u32 read_dr6()
  714. {
  715. u32 dr6;
  716. asm("movl %%dr6, %%eax"
  717. : "=a"(dr6));
  718. return dr6;
  719. }
  720. FPUState Processor::s_clean_fpu_state;
  721. static Vector<Processor*>* s_processors;
  722. static SpinLock s_processor_lock;
  723. volatile u32 Processor::g_total_processors;
  724. static volatile bool s_smp_enabled;
  725. Vector<Processor*>& Processor::processors()
  726. {
  727. ASSERT(s_processors);
  728. return *s_processors;
  729. }
  730. Processor& Processor::by_id(u32 cpu)
  731. {
  732. // s_processors does not need to be protected by a lock of any kind.
  733. // It is populated early in the boot process, and the BSP is waiting
  734. // for all APs to finish, after which this array never gets modified
  735. // again, so it's safe to not protect access to it here
  736. auto& procs = processors();
  737. ASSERT(procs[cpu] != nullptr);
  738. ASSERT(procs.size() > cpu);
  739. return *procs[cpu];
  740. }
  741. [[noreturn]] static inline void halt_this()
  742. {
  743. for (;;) {
  744. asm volatile("cli; hlt");
  745. }
  746. }
  747. void Processor::cpu_detect()
  748. {
  749. // NOTE: This is called during Processor::early_initialize, we cannot
  750. // safely log at this point because we don't have kmalloc
  751. // initialized yet!
  752. auto set_feature =
  753. [&](CPUFeature f) {
  754. m_features = static_cast<CPUFeature>(static_cast<u32>(m_features) | static_cast<u32>(f));
  755. };
  756. m_features = static_cast<CPUFeature>(0);
  757. CPUID processor_info(0x1);
  758. if (processor_info.edx() & (1 << 4))
  759. set_feature(CPUFeature::TSC);
  760. if (processor_info.edx() & (1 << 6))
  761. set_feature(CPUFeature::PAE);
  762. if (processor_info.edx() & (1 << 13))
  763. set_feature(CPUFeature::PGE);
  764. if (processor_info.edx() & (1 << 23))
  765. set_feature(CPUFeature::MMX);
  766. if (processor_info.edx() & (1 << 25))
  767. set_feature(CPUFeature::SSE);
  768. if (processor_info.edx() & (1 << 26))
  769. set_feature(CPUFeature::SSE2);
  770. if (processor_info.ecx() & (1 << 0))
  771. set_feature(CPUFeature::SSE3);
  772. if (processor_info.ecx() & (1 << 9))
  773. set_feature(CPUFeature::SSSE3);
  774. if (processor_info.ecx() & (1 << 19))
  775. set_feature(CPUFeature::SSE4_1);
  776. if (processor_info.ecx() & (1 << 20))
  777. set_feature(CPUFeature::SSE4_2);
  778. if (processor_info.ecx() & (1 << 30))
  779. set_feature(CPUFeature::RDRAND);
  780. if (processor_info.edx() & (1 << 11)) {
  781. u32 stepping = processor_info.eax() & 0xf;
  782. u32 model = (processor_info.eax() >> 4) & 0xf;
  783. u32 family = (processor_info.eax() >> 8) & 0xf;
  784. if (!(family == 6 && model < 3 && stepping < 3))
  785. set_feature(CPUFeature::SEP);
  786. if ((family == 6 && model >= 3) || (family == 0xf && model >= 0xe))
  787. set_feature(CPUFeature::CONSTANT_TSC);
  788. }
  789. u32 max_extended_leaf = CPUID(0x80000000).eax();
  790. ASSERT(max_extended_leaf >= 0x80000001);
  791. CPUID extended_processor_info(0x80000001);
  792. if (extended_processor_info.edx() & (1 << 20))
  793. set_feature(CPUFeature::NX);
  794. if (extended_processor_info.edx() & (1 << 27))
  795. set_feature(CPUFeature::RDTSCP);
  796. if (extended_processor_info.edx() & (1 << 11)) {
  797. // Only available in 64 bit mode
  798. set_feature(CPUFeature::SYSCALL);
  799. }
  800. if (max_extended_leaf >= 0x80000007) {
  801. CPUID cpuid(0x80000007);
  802. if (cpuid.edx() & (1 << 8)) {
  803. set_feature(CPUFeature::CONSTANT_TSC);
  804. set_feature(CPUFeature::NONSTOP_TSC);
  805. }
  806. }
  807. if (max_extended_leaf >= 0x80000008) {
  808. // CPUID.80000008H:EAX[7:0] reports the physical-address width supported by the processor.
  809. CPUID cpuid(0x80000008);
  810. m_physical_address_bit_width = cpuid.eax() & 0xff;
  811. } else {
  812. // For processors that do not support CPUID function 80000008H, the width is generally 36 if CPUID.01H:EDX.PAE [bit 6] = 1 and 32 otherwise.
  813. m_physical_address_bit_width = has_feature(CPUFeature::PAE) ? 36 : 32;
  814. }
  815. CPUID extended_features(0x7);
  816. if (extended_features.ebx() & (1 << 20))
  817. set_feature(CPUFeature::SMAP);
  818. if (extended_features.ebx() & (1 << 7))
  819. set_feature(CPUFeature::SMEP);
  820. if (extended_features.ecx() & (1 << 2))
  821. set_feature(CPUFeature::UMIP);
  822. if (extended_features.ebx() & (1 << 18))
  823. set_feature(CPUFeature::RDSEED);
  824. }
  825. void Processor::cpu_setup()
  826. {
  827. // NOTE: This is called during Processor::early_initialize, we cannot
  828. // safely log at this point because we don't have kmalloc
  829. // initialized yet!
  830. cpu_detect();
  831. if (has_feature(CPUFeature::SSE))
  832. sse_init();
  833. asm volatile(
  834. "movl %%cr0, %%eax\n"
  835. "orl $0x00010000, %%eax\n"
  836. "movl %%eax, %%cr0\n" ::
  837. : "%eax", "memory");
  838. if (has_feature(CPUFeature::PGE)) {
  839. // Turn on CR4.PGE so the CPU will respect the G bit in page tables.
  840. asm volatile(
  841. "mov %cr4, %eax\n"
  842. "orl $0x80, %eax\n"
  843. "mov %eax, %cr4\n");
  844. }
  845. if (has_feature(CPUFeature::NX)) {
  846. // Turn on IA32_EFER.NXE
  847. asm volatile(
  848. "movl $0xc0000080, %ecx\n"
  849. "rdmsr\n"
  850. "orl $0x800, %eax\n"
  851. "wrmsr\n");
  852. }
  853. if (has_feature(CPUFeature::SMEP)) {
  854. // Turn on CR4.SMEP
  855. asm volatile(
  856. "mov %cr4, %eax\n"
  857. "orl $0x100000, %eax\n"
  858. "mov %eax, %cr4\n");
  859. }
  860. if (has_feature(CPUFeature::SMAP)) {
  861. // Turn on CR4.SMAP
  862. asm volatile(
  863. "mov %cr4, %eax\n"
  864. "orl $0x200000, %eax\n"
  865. "mov %eax, %cr4\n");
  866. }
  867. if (has_feature(CPUFeature::UMIP)) {
  868. asm volatile(
  869. "mov %cr4, %eax\n"
  870. "orl $0x800, %eax\n"
  871. "mov %eax, %cr4\n");
  872. }
  873. if (has_feature(CPUFeature::TSC)) {
  874. asm volatile(
  875. "mov %cr4, %eax\n"
  876. "orl $0x4, %eax\n"
  877. "mov %eax, %cr4\n");
  878. }
  879. }
  880. String Processor::features_string() const
  881. {
  882. StringBuilder builder;
  883. auto feature_to_str =
  884. [](CPUFeature f) -> const char*
  885. {
  886. switch (f) {
  887. case CPUFeature::NX:
  888. return "nx";
  889. case CPUFeature::PAE:
  890. return "pae";
  891. case CPUFeature::PGE:
  892. return "pge";
  893. case CPUFeature::RDRAND:
  894. return "rdrand";
  895. case CPUFeature::RDSEED:
  896. return "rdseed";
  897. case CPUFeature::SMAP:
  898. return "smap";
  899. case CPUFeature::SMEP:
  900. return "smep";
  901. case CPUFeature::SSE:
  902. return "sse";
  903. case CPUFeature::TSC:
  904. return "tsc";
  905. case CPUFeature::RDTSCP:
  906. return "rdtscp";
  907. case CPUFeature::CONSTANT_TSC:
  908. return "constant_tsc";
  909. case CPUFeature::NONSTOP_TSC:
  910. return "nonstop_tsc";
  911. case CPUFeature::UMIP:
  912. return "umip";
  913. case CPUFeature::SEP:
  914. return "sep";
  915. case CPUFeature::SYSCALL:
  916. return "syscall";
  917. case CPUFeature::MMX:
  918. return "mmx";
  919. case CPUFeature::SSE2:
  920. return "sse2";
  921. case CPUFeature::SSE3:
  922. return "sse3";
  923. case CPUFeature::SSSE3:
  924. return "ssse3";
  925. case CPUFeature::SSE4_1:
  926. return "sse4.1";
  927. case CPUFeature::SSE4_2:
  928. return "sse4.2";
  929. // no default statement here intentionally so that we get
  930. // a warning if a new feature is forgotten to be added here
  931. }
  932. // Shouldn't ever happen
  933. return "???";
  934. };
  935. bool first = true;
  936. for (u32 flag = 1; flag != 0; flag <<= 1) {
  937. if ((static_cast<u32>(m_features) & flag) != 0) {
  938. if (first)
  939. first = false;
  940. else
  941. builder.append(' ');
  942. auto str = feature_to_str(static_cast<CPUFeature>(flag));
  943. builder.append(str, strlen(str));
  944. }
  945. }
  946. return builder.build();
  947. }
  948. void Processor::early_initialize(u32 cpu)
  949. {
  950. m_self = this;
  951. m_cpu = cpu;
  952. m_in_irq = 0;
  953. m_in_critical = 0;
  954. m_invoke_scheduler_async = false;
  955. m_scheduler_initialized = false;
  956. m_message_queue = nullptr;
  957. m_idle_thread = nullptr;
  958. m_current_thread = nullptr;
  959. m_scheduler_data = nullptr;
  960. m_mm_data = nullptr;
  961. m_info = nullptr;
  962. m_halt_requested = false;
  963. if (cpu == 0) {
  964. s_smp_enabled = false;
  965. atomic_store(&g_total_processors, 1u, AK::MemoryOrder::memory_order_release);
  966. } else {
  967. atomic_fetch_add(&g_total_processors, 1u, AK::MemoryOrder::memory_order_acq_rel);
  968. }
  969. deferred_call_pool_init();
  970. cpu_setup();
  971. gdt_init();
  972. ASSERT(&current() == this); // sanity check
  973. }
  974. void Processor::initialize(u32 cpu)
  975. {
  976. ASSERT(m_self == this);
  977. ASSERT(&current() == this); // sanity check
  978. klog() << "CPU[" << id() << "]: Supported features: " << features_string();
  979. if (!has_feature(CPUFeature::RDRAND))
  980. klog() << "CPU[" << id() << "]: No RDRAND support detected, randomness will be poor";
  981. klog() << "CPU[" << id() << "]: Physical address bit width: " << m_physical_address_bit_width;
  982. if (cpu == 0)
  983. idt_init();
  984. else
  985. flush_idt();
  986. if (cpu == 0) {
  987. ASSERT((FlatPtr(&s_clean_fpu_state) & 0xF) == 0);
  988. asm volatile("fninit");
  989. asm volatile("fxsave %0"
  990. : "=m"(s_clean_fpu_state));
  991. }
  992. m_info = new ProcessorInfo(*this);
  993. {
  994. ScopedSpinLock lock(s_processor_lock);
  995. // We need to prevent races between APs starting up at the same time
  996. if (!s_processors)
  997. s_processors = new Vector<Processor*>();
  998. if (cpu >= s_processors->size())
  999. s_processors->resize(cpu + 1);
  1000. (*s_processors)[cpu] = this;
  1001. }
  1002. }
  1003. void Processor::write_raw_gdt_entry(u16 selector, u32 low, u32 high)
  1004. {
  1005. u16 i = (selector & 0xfffc) >> 3;
  1006. u32 prev_gdt_length = m_gdt_length;
  1007. if (i > m_gdt_length) {
  1008. m_gdt_length = i + 1;
  1009. ASSERT(m_gdt_length <= sizeof(m_gdt) / sizeof(m_gdt[0]));
  1010. m_gdtr.limit = (m_gdt_length + 1) * 8 - 1;
  1011. }
  1012. m_gdt[i].low = low;
  1013. m_gdt[i].high = high;
  1014. // clear selectors we may have skipped
  1015. while (i < prev_gdt_length) {
  1016. m_gdt[i].low = 0;
  1017. m_gdt[i].high = 0;
  1018. i++;
  1019. }
  1020. }
  1021. void Processor::write_gdt_entry(u16 selector, Descriptor& descriptor)
  1022. {
  1023. write_raw_gdt_entry(selector, descriptor.low, descriptor.high);
  1024. }
  1025. Descriptor& Processor::get_gdt_entry(u16 selector)
  1026. {
  1027. u16 i = (selector & 0xfffc) >> 3;
  1028. return *(Descriptor*)(&m_gdt[i]);
  1029. }
  1030. void Processor::flush_gdt()
  1031. {
  1032. m_gdtr.address = m_gdt;
  1033. m_gdtr.limit = (m_gdt_length * 8) - 1;
  1034. asm volatile("lgdt %0" ::"m"(m_gdtr)
  1035. : "memory");
  1036. }
  1037. const DescriptorTablePointer& Processor::get_gdtr()
  1038. {
  1039. return m_gdtr;
  1040. }
  1041. Vector<FlatPtr> Processor::capture_stack_trace(Thread& thread, size_t max_frames)
  1042. {
  1043. FlatPtr frame_ptr = 0, eip = 0;
  1044. Vector<FlatPtr, 32> stack_trace;
  1045. auto walk_stack = [&](FlatPtr stack_ptr)
  1046. {
  1047. stack_trace.append(eip);
  1048. size_t count = 1;
  1049. while (stack_ptr) {
  1050. FlatPtr retaddr;
  1051. count++;
  1052. if (max_frames != 0 && count > max_frames)
  1053. break;
  1054. if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
  1055. if (!copy_from_user(&retaddr, &((FlatPtr*)stack_ptr)[1]) || !retaddr)
  1056. break;
  1057. stack_trace.append(retaddr);
  1058. if (!copy_from_user(&stack_ptr, (FlatPtr*)stack_ptr))
  1059. break;
  1060. } else {
  1061. void* fault_at;
  1062. if (!safe_memcpy(&retaddr, &((FlatPtr*)stack_ptr)[1], sizeof(FlatPtr), fault_at) || !retaddr)
  1063. break;
  1064. stack_trace.append(retaddr);
  1065. if (!safe_memcpy(&stack_ptr, (FlatPtr*)stack_ptr, sizeof(FlatPtr), fault_at))
  1066. break;
  1067. }
  1068. }
  1069. };
  1070. auto capture_current_thread = [&]()
  1071. {
  1072. frame_ptr = (FlatPtr)__builtin_frame_address(0);
  1073. eip = (FlatPtr)__builtin_return_address(0);
  1074. walk_stack(frame_ptr);
  1075. };
  1076. // Since the thread may be running on another processor, there
  1077. // is a chance a context switch may happen while we're trying
  1078. // to get it. It also won't be entirely accurate and merely
  1079. // reflect the status at the last context switch.
  1080. ScopedSpinLock lock(g_scheduler_lock);
  1081. auto& proc = Processor::current();
  1082. if (&thread == proc.current_thread()) {
  1083. ASSERT(thread.state() == Thread::Running);
  1084. // Leave the scheduler lock. If we trigger page faults we may
  1085. // need to be preempted. Since this is our own thread it won't
  1086. // cause any problems as the stack won't change below this frame.
  1087. lock.unlock();
  1088. capture_current_thread();
  1089. } else if (thread.is_active()) {
  1090. ASSERT(thread.cpu() != proc.id());
  1091. // If this is the case, the thread is currently running
  1092. // on another processor. We can't trust the kernel stack as
  1093. // it may be changing at any time. We need to probably send
  1094. // an IPI to that processor, have it walk the stack and wait
  1095. // until it returns the data back to us
  1096. smp_unicast(thread.cpu(),
  1097. [&]() {
  1098. dbgln("CPU[{}] getting stack for cpu #{}", Processor::current().id(), proc.id());
  1099. ProcessPagingScope paging_scope(thread.process());
  1100. auto& target_proc = Processor::current();
  1101. ASSERT(&target_proc != &proc);
  1102. ASSERT(&thread == target_proc.current_thread());
  1103. // NOTE: Because the other processor is still holding the
  1104. // scheduler lock while waiting for this callback to finish,
  1105. // the current thread on the target processor cannot change
  1106. // TODO: What to do about page faults here? We might deadlock
  1107. // because the other processor is still holding the
  1108. // scheduler lock...
  1109. capture_current_thread();
  1110. }, false);
  1111. } else {
  1112. switch (thread.state()) {
  1113. case Thread::Running:
  1114. ASSERT_NOT_REACHED(); // should have been handled above
  1115. case Thread::Runnable:
  1116. case Thread::Stopped:
  1117. case Thread::Blocked:
  1118. case Thread::Dying:
  1119. case Thread::Dead: {
  1120. // We need to retrieve ebp from what was last pushed to the kernel
  1121. // stack. Before switching out of that thread, it switch_context
  1122. // pushed the callee-saved registers, and the last of them happens
  1123. // to be ebp.
  1124. ProcessPagingScope paging_scope(thread.process());
  1125. auto& tss = thread.tss();
  1126. u32* stack_top = reinterpret_cast<u32*>(tss.esp);
  1127. if (is_user_range(VirtualAddress(stack_top), sizeof(FlatPtr))) {
  1128. if (!copy_from_user(&frame_ptr, &((FlatPtr*)stack_top)[0]))
  1129. frame_ptr = 0;
  1130. } else {
  1131. void* fault_at;
  1132. if (!safe_memcpy(&frame_ptr, &((FlatPtr*)stack_top)[0], sizeof(FlatPtr), fault_at))
  1133. frame_ptr = 0;
  1134. }
  1135. eip = tss.eip;
  1136. // TODO: We need to leave the scheduler lock here, but we also
  1137. // need to prevent the target thread from being run while
  1138. // we walk the stack
  1139. lock.unlock();
  1140. walk_stack(frame_ptr);
  1141. break;
  1142. }
  1143. default:
  1144. dbgln("Cannot capture stack trace for thread {} in state {}", thread, thread.state_string());
  1145. break;
  1146. }
  1147. }
  1148. return stack_trace;
  1149. }
  1150. extern "C" void enter_thread_context(Thread* from_thread, Thread* to_thread)
  1151. {
  1152. ASSERT(from_thread == to_thread || from_thread->state() != Thread::Running);
  1153. ASSERT(to_thread->state() == Thread::Running);
  1154. auto& processor = Processor::current();
  1155. processor.set_current_thread(*to_thread);
  1156. auto& from_tss = from_thread->tss();
  1157. auto& to_tss = to_thread->tss();
  1158. asm volatile("fxsave %0"
  1159. : "=m"(from_thread->fpu_state()));
  1160. from_tss.fs = get_fs();
  1161. from_tss.gs = get_gs();
  1162. set_fs(to_tss.fs);
  1163. set_gs(to_tss.gs);
  1164. auto& tls_descriptor = processor.get_gdt_entry(GDT_SELECTOR_TLS);
  1165. tls_descriptor.set_base(to_thread->thread_specific_data().as_ptr());
  1166. tls_descriptor.set_limit(to_thread->thread_specific_region_size());
  1167. if (from_tss.cr3 != to_tss.cr3)
  1168. write_cr3(to_tss.cr3);
  1169. to_thread->set_cpu(processor.id());
  1170. processor.restore_in_critical(to_thread->saved_critical());
  1171. asm volatile("fxrstor %0"
  1172. ::"m"(to_thread->fpu_state()));
  1173. // TODO: debug registers
  1174. // TODO: ioperm?
  1175. }
  1176. #define ENTER_THREAD_CONTEXT_ARGS_SIZE (2 * 4) // to_thread, from_thread
  1177. void Processor::switch_context(Thread*& from_thread, Thread*& to_thread)
  1178. {
  1179. ASSERT(!in_irq());
  1180. ASSERT(m_in_critical == 1);
  1181. ASSERT(is_kernel_mode());
  1182. dbgln<CONTEXT_SWITCH_DEBUG>("switch_context --> switching out of: {} {}", VirtualAddress(from_thread), *from_thread);
  1183. from_thread->save_critical(m_in_critical);
  1184. // Switch to new thread context, passing from_thread and to_thread
  1185. // through to the new context using registers edx and eax
  1186. asm volatile(
  1187. // NOTE: changing how much we push to the stack affects
  1188. // SWITCH_CONTEXT_TO_STACK_SIZE and thread_context_first_enter()!
  1189. "pushfl \n"
  1190. "pushl %%ebx \n"
  1191. "pushl %%esi \n"
  1192. "pushl %%edi \n"
  1193. "pushl %%ebp \n"
  1194. "movl %%esp, %[from_esp] \n"
  1195. "movl $1f, %[from_eip] \n"
  1196. "movl %[to_esp0], %%ebx \n"
  1197. "movl %%ebx, %[tss_esp0] \n"
  1198. "movl %[to_esp], %%esp \n"
  1199. "pushl %[to_thread] \n"
  1200. "pushl %[from_thread] \n"
  1201. "pushl %[to_eip] \n"
  1202. "cld \n"
  1203. "jmp enter_thread_context \n"
  1204. "1: \n"
  1205. "popl %%edx \n"
  1206. "popl %%eax \n"
  1207. "popl %%ebp \n"
  1208. "popl %%edi \n"
  1209. "popl %%esi \n"
  1210. "popl %%ebx \n"
  1211. "popfl \n"
  1212. : [from_esp] "=m" (from_thread->tss().esp),
  1213. [from_eip] "=m" (from_thread->tss().eip),
  1214. [tss_esp0] "=m" (m_tss.esp0),
  1215. "=d" (from_thread), // needed so that from_thread retains the correct value
  1216. "=a" (to_thread) // needed so that to_thread retains the correct value
  1217. : [to_esp] "g" (to_thread->tss().esp),
  1218. [to_esp0] "g" (to_thread->tss().esp0),
  1219. [to_eip] "c" (to_thread->tss().eip),
  1220. [from_thread] "d" (from_thread),
  1221. [to_thread] "a" (to_thread)
  1222. : "memory"
  1223. );
  1224. dbgln<CONTEXT_SWITCH_DEBUG>("switch_context <-- from {} {} to {} {}", VirtualAddress(from_thread), *from_thread, VirtualAddress(to_thread), *to_thread);
  1225. }
  1226. extern "C" void context_first_init([[maybe_unused]] Thread* from_thread, [[maybe_unused]] Thread* to_thread, [[maybe_unused]] TrapFrame* trap)
  1227. {
  1228. ASSERT(!are_interrupts_enabled());
  1229. ASSERT(is_kernel_mode());
  1230. dbgln<CONTEXT_SWITCH_DEBUG>("switch_context <-- from {} {} to {} {} (context_first_init)", VirtualAddress(from_thread), *from_thread, VirtualAddress(to_thread), *to_thread);
  1231. ASSERT(to_thread == Thread::current());
  1232. Scheduler::enter_current(*from_thread, true);
  1233. // Since we got here and don't have Scheduler::context_switch in the
  1234. // call stack (because this is the first time we switched into this
  1235. // context), we need to notify the scheduler so that it can release
  1236. // the scheduler lock. We don't want to enable interrupts at this point
  1237. // as we're still in the middle of a context switch. Doing so could
  1238. // trigger a context switch within a context switch, leading to a crash.
  1239. Scheduler::leave_on_first_switch(trap->regs->eflags & ~0x200);
  1240. }
  1241. extern "C" void thread_context_first_enter(void);
  1242. asm(
  1243. // enter_thread_context returns to here first time a thread is executing
  1244. ".globl thread_context_first_enter \n"
  1245. "thread_context_first_enter: \n"
  1246. // switch_context will have pushed from_thread and to_thread to our new
  1247. // stack prior to thread_context_first_enter() being called, and the
  1248. // pointer to TrapFrame was the top of the stack before that
  1249. " movl 8(%esp), %ebx \n" // save pointer to TrapFrame
  1250. " cld \n"
  1251. " call context_first_init \n"
  1252. " addl $" __STRINGIFY(ENTER_THREAD_CONTEXT_ARGS_SIZE) ", %esp \n"
  1253. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  1254. " jmp common_trap_exit \n"
  1255. );
  1256. void exit_kernel_thread(void)
  1257. {
  1258. Thread::current()->exit();
  1259. }
  1260. u32 Processor::init_context(Thread& thread, bool leave_crit)
  1261. {
  1262. ASSERT(is_kernel_mode());
  1263. ASSERT(g_scheduler_lock.is_locked());
  1264. if (leave_crit) {
  1265. // Leave the critical section we set up in in Process::exec,
  1266. // but because we still have the scheduler lock we should end up with 1
  1267. m_in_critical--; // leave it without triggering anything or restoring flags
  1268. ASSERT(in_critical() == 1);
  1269. }
  1270. u32 kernel_stack_top = thread.kernel_stack_top();
  1271. u32 stack_top = kernel_stack_top;
  1272. // TODO: handle NT?
  1273. ASSERT((cpu_flags() & 0x24000) == 0); // Assume !(NT | VM)
  1274. auto& tss = thread.tss();
  1275. bool return_to_user = (tss.cs & 3) != 0;
  1276. // make room for an interrupt frame
  1277. if (!return_to_user) {
  1278. // userspace_esp and userspace_ss are not popped off by iret
  1279. // unless we're switching back to user mode
  1280. stack_top -= sizeof(RegisterState) - 2 * sizeof(u32);
  1281. // For kernel threads we'll push the thread function argument
  1282. // which should be in tss.esp and exit_kernel_thread as return
  1283. // address.
  1284. stack_top -= 2 * sizeof(u32);
  1285. *reinterpret_cast<u32*>(kernel_stack_top - 2 * sizeof(u32)) = tss.esp;
  1286. *reinterpret_cast<u32*>(kernel_stack_top - 3 * sizeof(u32)) = FlatPtr(&exit_kernel_thread);
  1287. } else {
  1288. stack_top -= sizeof(RegisterState);
  1289. }
  1290. // we want to end up 16-byte aligned, %esp + 4 should be aligned
  1291. stack_top -= sizeof(u32);
  1292. *reinterpret_cast<u32*>(kernel_stack_top - sizeof(u32)) = 0;
  1293. // set up the stack so that after returning from thread_context_first_enter()
  1294. // we will end up either in kernel mode or user mode, depending on how the thread is set up
  1295. // However, the first step is to always start in kernel mode with thread_context_first_enter
  1296. RegisterState& iretframe = *reinterpret_cast<RegisterState*>(stack_top);
  1297. iretframe.ss = tss.ss;
  1298. iretframe.gs = tss.gs;
  1299. iretframe.fs = tss.fs;
  1300. iretframe.es = tss.es;
  1301. iretframe.ds = tss.ds;
  1302. iretframe.edi = tss.edi;
  1303. iretframe.esi = tss.esi;
  1304. iretframe.ebp = tss.ebp;
  1305. iretframe.esp = 0;
  1306. iretframe.ebx = tss.ebx;
  1307. iretframe.edx = tss.edx;
  1308. iretframe.ecx = tss.ecx;
  1309. iretframe.eax = tss.eax;
  1310. iretframe.eflags = tss.eflags;
  1311. iretframe.eip = tss.eip;
  1312. iretframe.cs = tss.cs;
  1313. if (return_to_user) {
  1314. iretframe.userspace_esp = tss.esp;
  1315. iretframe.userspace_ss = tss.ss;
  1316. }
  1317. // make space for a trap frame
  1318. stack_top -= sizeof(TrapFrame);
  1319. TrapFrame& trap = *reinterpret_cast<TrapFrame*>(stack_top);
  1320. trap.regs = &iretframe;
  1321. trap.prev_irq_level = 0;
  1322. trap.next_trap = nullptr;
  1323. stack_top -= sizeof(u32); // pointer to TrapFrame
  1324. *reinterpret_cast<u32*>(stack_top) = stack_top + 4;
  1325. if constexpr (CONTEXT_SWITCH_DEBUG) {
  1326. if (return_to_user) {
  1327. dbgln("init_context {} ({}) set up to execute at eip={}:{}, esp={}, stack_top={}, user_top={}:{}",
  1328. thread,
  1329. VirtualAddress(&thread),
  1330. iretframe.cs, tss.eip,
  1331. VirtualAddress(tss.esp),
  1332. VirtualAddress(stack_top),
  1333. iretframe.userspace_ss,
  1334. iretframe.userspace_esp);
  1335. } else {
  1336. dbgln("init_context {} ({}) set up to execute at eip={}:{}, esp={}, stack_top={}",
  1337. thread,
  1338. VirtualAddress(&thread),
  1339. iretframe.cs, tss.eip,
  1340. VirtualAddress(tss.esp),
  1341. VirtualAddress(stack_top));
  1342. }
  1343. }
  1344. // make switch_context() always first return to thread_context_first_enter()
  1345. // in kernel mode, so set up these values so that we end up popping iretframe
  1346. // off the stack right after the context switch completed, at which point
  1347. // control is transferred to what iretframe is pointing to.
  1348. tss.eip = FlatPtr(&thread_context_first_enter);
  1349. tss.esp0 = kernel_stack_top;
  1350. tss.esp = stack_top;
  1351. tss.cs = GDT_SELECTOR_CODE0;
  1352. tss.ds = GDT_SELECTOR_DATA0;
  1353. tss.es = GDT_SELECTOR_DATA0;
  1354. tss.gs = GDT_SELECTOR_DATA0;
  1355. tss.ss = GDT_SELECTOR_DATA0;
  1356. tss.fs = GDT_SELECTOR_PROC;
  1357. return stack_top;
  1358. }
  1359. extern "C" u32 do_init_context(Thread* thread, u32 flags)
  1360. {
  1361. ASSERT_INTERRUPTS_DISABLED();
  1362. thread->tss().eflags = flags;
  1363. return Processor::current().init_context(*thread, true);
  1364. }
  1365. extern "C" void do_assume_context(Thread* thread, u32 flags);
  1366. asm(
  1367. ".global do_assume_context \n"
  1368. "do_assume_context: \n"
  1369. " movl 4(%esp), %ebx \n"
  1370. " movl 8(%esp), %esi \n"
  1371. // We're going to call Processor::init_context, so just make sure
  1372. // we have enough stack space so we don't stomp over it
  1373. " subl $(" __STRINGIFY(4 + REGISTER_STATE_SIZE + TRAP_FRAME_SIZE + 4) "), %esp \n"
  1374. " pushl %esi \n"
  1375. " pushl %ebx \n"
  1376. " cld \n"
  1377. " call do_init_context \n"
  1378. " addl $8, %esp \n"
  1379. " movl %eax, %esp \n" // move stack pointer to what Processor::init_context set up for us
  1380. " pushl %ebx \n" // push to_thread
  1381. " pushl %ebx \n" // push from_thread
  1382. " pushl $thread_context_first_enter \n" // should be same as tss.eip
  1383. " jmp enter_thread_context \n"
  1384. );
  1385. void Processor::assume_context(Thread& thread, u32 flags)
  1386. {
  1387. dbgln<CONTEXT_SWITCH_DEBUG>("Assume context for thread {} {}", VirtualAddress(&thread), thread);
  1388. ASSERT_INTERRUPTS_DISABLED();
  1389. Scheduler::prepare_after_exec();
  1390. // in_critical() should be 2 here. The critical section in Process::exec
  1391. // and then the scheduler lock
  1392. ASSERT(Processor::current().in_critical() == 2);
  1393. do_assume_context(&thread, flags);
  1394. ASSERT_NOT_REACHED();
  1395. }
  1396. extern "C" void pre_init_finished(void)
  1397. {
  1398. ASSERT(g_scheduler_lock.own_lock());
  1399. // Because init_finished() will wait on the other APs, we need
  1400. // to release the scheduler lock so that the other APs can also get
  1401. // to this point
  1402. // The target flags will get restored upon leaving the trap
  1403. u32 prev_flags = cpu_flags();
  1404. Scheduler::leave_on_first_switch(prev_flags);
  1405. }
  1406. extern "C" void post_init_finished(void)
  1407. {
  1408. // We need to re-acquire the scheduler lock before a context switch
  1409. // transfers control into the idle loop, which needs the lock held
  1410. Scheduler::prepare_for_idle_loop();
  1411. }
  1412. void Processor::initialize_context_switching(Thread& initial_thread)
  1413. {
  1414. ASSERT(initial_thread.process().is_kernel_process());
  1415. auto& tss = initial_thread.tss();
  1416. m_tss = tss;
  1417. m_tss.esp0 = tss.esp0;
  1418. m_tss.ss0 = GDT_SELECTOR_DATA0;
  1419. // user mode needs to be able to switch to kernel mode:
  1420. m_tss.cs = m_tss.ds = m_tss.es = m_tss.gs = m_tss.ss = GDT_SELECTOR_CODE0 | 3;
  1421. m_tss.fs = GDT_SELECTOR_PROC | 3;
  1422. m_scheduler_initialized = true;
  1423. asm volatile(
  1424. "movl %[new_esp], %%esp \n" // switch to new stack
  1425. "pushl %[from_to_thread] \n" // to_thread
  1426. "pushl %[from_to_thread] \n" // from_thread
  1427. "pushl $" __STRINGIFY(GDT_SELECTOR_CODE0) " \n"
  1428. "pushl %[new_eip] \n" // save the entry eip to the stack
  1429. "movl %%esp, %%ebx \n"
  1430. "addl $20, %%ebx \n" // calculate pointer to TrapFrame
  1431. "pushl %%ebx \n"
  1432. "cld \n"
  1433. "pushl %[cpu] \n" // push argument for init_finished before register is clobbered
  1434. "call pre_init_finished \n"
  1435. "call init_finished \n"
  1436. "addl $4, %%esp \n"
  1437. "call post_init_finished \n"
  1438. "call enter_trap_no_irq \n"
  1439. "addl $4, %%esp \n"
  1440. "lret \n"
  1441. :: [new_esp] "g" (tss.esp),
  1442. [new_eip] "a" (tss.eip),
  1443. [from_to_thread] "b" (&initial_thread),
  1444. [cpu] "c" (id())
  1445. );
  1446. ASSERT_NOT_REACHED();
  1447. }
  1448. void Processor::enter_trap(TrapFrame& trap, bool raise_irq)
  1449. {
  1450. InterruptDisabler disabler;
  1451. trap.prev_irq_level = m_in_irq;
  1452. if (raise_irq)
  1453. m_in_irq++;
  1454. if (m_current_thread) {
  1455. auto& current_trap = m_current_thread->current_trap();
  1456. trap.next_trap = current_trap;
  1457. current_trap = &trap;
  1458. // The cs register of this trap tells us where we will return back to
  1459. m_current_thread->set_previous_mode(((trap.regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode);
  1460. } else {
  1461. trap.next_trap = nullptr;
  1462. }
  1463. }
  1464. void Processor::exit_trap(TrapFrame& trap)
  1465. {
  1466. InterruptDisabler disabler;
  1467. ASSERT(m_in_irq >= trap.prev_irq_level);
  1468. m_in_irq = trap.prev_irq_level;
  1469. smp_process_pending_messages();
  1470. if (!m_in_irq && !m_in_critical)
  1471. check_invoke_scheduler();
  1472. if (m_current_thread) {
  1473. auto& current_trap = m_current_thread->current_trap();
  1474. current_trap = trap.next_trap;
  1475. if (current_trap) {
  1476. // If we have another higher level trap then we probably returned
  1477. // from an interrupt or irq handler. The cs register of the
  1478. // new/higher level trap tells us what the mode prior to it was
  1479. m_current_thread->set_previous_mode(((current_trap->regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode);
  1480. } else {
  1481. // If we don't have a higher level trap then we're back in user mode.
  1482. // Unless we're a kernel process, in which case we're always in kernel mode
  1483. m_current_thread->set_previous_mode(m_current_thread->process().is_kernel_process() ? Thread::PreviousMode::KernelMode : Thread::PreviousMode::UserMode);
  1484. }
  1485. }
  1486. }
  1487. void Processor::check_invoke_scheduler()
  1488. {
  1489. ASSERT(!m_in_irq);
  1490. ASSERT(!m_in_critical);
  1491. if (m_invoke_scheduler_async && m_scheduler_initialized) {
  1492. m_invoke_scheduler_async = false;
  1493. Scheduler::invoke_async();
  1494. }
  1495. }
  1496. void Processor::flush_tlb_local(VirtualAddress vaddr, size_t page_count)
  1497. {
  1498. auto ptr = vaddr.as_ptr();
  1499. while (page_count > 0) {
  1500. asm volatile("invlpg %0"
  1501. :
  1502. : "m"(*ptr)
  1503. : "memory");
  1504. ptr += PAGE_SIZE;
  1505. page_count--;
  1506. }
  1507. }
  1508. void Processor::flush_tlb(const PageDirectory* page_directory, VirtualAddress vaddr, size_t page_count)
  1509. {
  1510. if (s_smp_enabled)
  1511. smp_broadcast_flush_tlb(page_directory, vaddr, page_count);
  1512. else
  1513. flush_tlb_local(vaddr, page_count);
  1514. }
  1515. static volatile ProcessorMessage* s_message_pool;
  1516. void Processor::smp_return_to_pool(ProcessorMessage& msg)
  1517. {
  1518. ProcessorMessage* next = nullptr;
  1519. do {
  1520. msg.next = next;
  1521. } while (!atomic_compare_exchange_strong(&s_message_pool, next, &msg, AK::MemoryOrder::memory_order_acq_rel));
  1522. }
  1523. ProcessorMessage& Processor::smp_get_from_pool()
  1524. {
  1525. ProcessorMessage* msg;
  1526. // The assumption is that messages are never removed from the pool!
  1527. for (;;) {
  1528. msg = atomic_load(&s_message_pool, AK::MemoryOrder::memory_order_consume);
  1529. if (!msg) {
  1530. if (!Processor::current().smp_process_pending_messages()) {
  1531. // TODO: pause for a bit?
  1532. }
  1533. continue;
  1534. }
  1535. // If another processor were to use this message in the meanwhile,
  1536. // "msg" is still valid (because it never gets freed). We'd detect
  1537. // this because the expected value "msg" and pool would
  1538. // no longer match, and the compare_exchange will fail. But accessing
  1539. // "msg->next" is always safe here.
  1540. if (atomic_compare_exchange_strong(&s_message_pool, msg, msg->next, AK::MemoryOrder::memory_order_acq_rel)) {
  1541. // We successfully "popped" this available message
  1542. break;
  1543. }
  1544. }
  1545. ASSERT(msg != nullptr);
  1546. return *msg;
  1547. }
  1548. void Processor::smp_enable()
  1549. {
  1550. size_t msg_pool_size = Processor::count() * 100u;
  1551. size_t msg_entries_cnt = Processor::count();
  1552. auto msgs = new ProcessorMessage[msg_pool_size];
  1553. auto msg_entries = new ProcessorMessageEntry[msg_pool_size * msg_entries_cnt];
  1554. size_t msg_entry_i = 0;
  1555. for (size_t i = 0; i < msg_pool_size; i++, msg_entry_i += msg_entries_cnt) {
  1556. auto& msg = msgs[i];
  1557. msg.next = i < msg_pool_size - 1 ? &msgs[i + 1] : nullptr;
  1558. msg.per_proc_entries = &msg_entries[msg_entry_i];
  1559. for (size_t k = 0; k < msg_entries_cnt; k++)
  1560. msg_entries[msg_entry_i + k].msg = &msg;
  1561. }
  1562. atomic_store(&s_message_pool, &msgs[0], AK::MemoryOrder::memory_order_release);
  1563. // Start sending IPI messages
  1564. s_smp_enabled = true;
  1565. }
  1566. void Processor::smp_cleanup_message(ProcessorMessage& msg)
  1567. {
  1568. switch (msg.type) {
  1569. case ProcessorMessage::CallbackWithData:
  1570. if (msg.callback_with_data.free)
  1571. msg.callback_with_data.free(msg.callback_with_data.data);
  1572. break;
  1573. default:
  1574. break;
  1575. }
  1576. }
  1577. bool Processor::smp_process_pending_messages()
  1578. {
  1579. bool did_process = false;
  1580. u32 prev_flags;
  1581. enter_critical(prev_flags);
  1582. if (auto pending_msgs = atomic_exchange(&m_message_queue, nullptr, AK::MemoryOrder::memory_order_acq_rel)) {
  1583. // We pulled the stack of pending messages in LIFO order, so we need to reverse the list first
  1584. auto reverse_list =
  1585. [](ProcessorMessageEntry* list) -> ProcessorMessageEntry*
  1586. {
  1587. ProcessorMessageEntry* rev_list = nullptr;
  1588. while (list) {
  1589. auto next = list->next;
  1590. list->next = rev_list;
  1591. rev_list = list;
  1592. list = next;
  1593. }
  1594. return rev_list;
  1595. };
  1596. pending_msgs = reverse_list(pending_msgs);
  1597. // now process in the right order
  1598. ProcessorMessageEntry* next_msg;
  1599. for (auto cur_msg = pending_msgs; cur_msg; cur_msg = next_msg) {
  1600. next_msg = cur_msg->next;
  1601. auto msg = cur_msg->msg;
  1602. dbgln<SMP_DEBUG>("SMP[{}]: Processing message {}", id(), VirtualAddress(msg));
  1603. switch (msg->type) {
  1604. case ProcessorMessage::Callback:
  1605. msg->callback.handler();
  1606. break;
  1607. case ProcessorMessage::CallbackWithData:
  1608. msg->callback_with_data.handler(msg->callback_with_data.data);
  1609. break;
  1610. case ProcessorMessage::FlushTlb:
  1611. if (is_user_address(VirtualAddress(msg->flush_tlb.ptr))) {
  1612. // We assume that we don't cross into kernel land!
  1613. ASSERT(is_user_range(VirtualAddress(msg->flush_tlb.ptr), msg->flush_tlb.page_count * PAGE_SIZE));
  1614. if (read_cr3() != msg->flush_tlb.page_directory->cr3()) {
  1615. // This processor isn't using this page directory right now, we can ignore this request
  1616. dbgln<SMP_DEBUG>("SMP[{}]: No need to flush {} pages at {}", id(), msg->flush_tlb.page_count, VirtualAddress(msg->flush_tlb.ptr));
  1617. break;
  1618. }
  1619. }
  1620. flush_tlb_local(VirtualAddress(msg->flush_tlb.ptr), msg->flush_tlb.page_count);
  1621. break;
  1622. }
  1623. bool is_async = msg->async; // Need to cache this value *before* dropping the ref count!
  1624. auto prev_refs = atomic_fetch_sub(&msg->refs, 1u, AK::MemoryOrder::memory_order_acq_rel);
  1625. ASSERT(prev_refs != 0);
  1626. if (prev_refs == 1) {
  1627. // All processors handled this. If this is an async message,
  1628. // we need to clean it up and return it to the pool
  1629. if (is_async) {
  1630. smp_cleanup_message(*msg);
  1631. smp_return_to_pool(*msg);
  1632. }
  1633. }
  1634. if (m_halt_requested.load(AK::MemoryOrder::memory_order_relaxed))
  1635. halt_this();
  1636. }
  1637. did_process = true;
  1638. } else if (m_halt_requested.load(AK::MemoryOrder::memory_order_relaxed)) {
  1639. halt_this();
  1640. }
  1641. leave_critical(prev_flags);
  1642. return did_process;
  1643. }
  1644. bool Processor::smp_queue_message(ProcessorMessage& msg)
  1645. {
  1646. // Note that it's quite possible that the other processor may pop
  1647. // the queue at any given time. We rely on the fact that the messages
  1648. // are pooled and never get freed!
  1649. auto& msg_entry = msg.per_proc_entries[id()];
  1650. ASSERT(msg_entry.msg == &msg);
  1651. ProcessorMessageEntry* next = nullptr;
  1652. do {
  1653. msg_entry.next = next;
  1654. } while (!atomic_compare_exchange_strong(&m_message_queue, next, &msg_entry, AK::MemoryOrder::memory_order_acq_rel));
  1655. return next == nullptr;
  1656. }
  1657. void Processor::smp_broadcast_message(ProcessorMessage& msg)
  1658. {
  1659. auto& cur_proc = Processor::current();
  1660. dbgln<SMP_DEBUG>("SMP[{}]: Broadcast message {} to cpus: {} proc: {}", cur_proc.id(), VirtualAddress(&msg), count(), VirtualAddress(&cur_proc));
  1661. atomic_store(&msg.refs, count() - 1, AK::MemoryOrder::memory_order_release);
  1662. ASSERT(msg.refs > 0);
  1663. bool need_broadcast = false;
  1664. for_each(
  1665. [&](Processor& proc) -> IterationDecision {
  1666. if (&proc != &cur_proc) {
  1667. if (proc.smp_queue_message(msg))
  1668. need_broadcast = true;
  1669. }
  1670. return IterationDecision::Continue;
  1671. });
  1672. // Now trigger an IPI on all other APs (unless all targets already had messages queued)
  1673. if (need_broadcast)
  1674. APIC::the().broadcast_ipi();
  1675. }
  1676. void Processor::smp_broadcast_wait_sync(ProcessorMessage& msg)
  1677. {
  1678. auto& cur_proc = Processor::current();
  1679. ASSERT(!msg.async);
  1680. // If synchronous then we must cleanup and return the message back
  1681. // to the pool. Otherwise, the last processor to complete it will return it
  1682. while (atomic_load(&msg.refs, AK::MemoryOrder::memory_order_consume) != 0) {
  1683. // TODO: pause for a bit?
  1684. // We need to process any messages that may have been sent to
  1685. // us while we're waiting. This also checks if another processor
  1686. // may have requested us to halt.
  1687. cur_proc.smp_process_pending_messages();
  1688. }
  1689. smp_cleanup_message(msg);
  1690. smp_return_to_pool(msg);
  1691. }
  1692. void Processor::smp_broadcast(void (*callback)(void*), void* data, void (*free_data)(void*), bool async)
  1693. {
  1694. auto& msg = smp_get_from_pool();
  1695. msg.async = async;
  1696. msg.type = ProcessorMessage::CallbackWithData;
  1697. msg.callback_with_data.handler = callback;
  1698. msg.callback_with_data.data = data;
  1699. msg.callback_with_data.free = free_data;
  1700. smp_broadcast_message(msg);
  1701. if (!async)
  1702. smp_broadcast_wait_sync(msg);
  1703. }
  1704. void Processor::smp_broadcast(void (*callback)(), bool async)
  1705. {
  1706. auto& msg = smp_get_from_pool();
  1707. msg.async = async;
  1708. msg.type = ProcessorMessage::CallbackWithData;
  1709. msg.callback.handler = callback;
  1710. smp_broadcast_message(msg);
  1711. if (!async)
  1712. smp_broadcast_wait_sync(msg);
  1713. }
  1714. void Processor::smp_unicast_message(u32 cpu, ProcessorMessage& msg, bool async)
  1715. {
  1716. auto& cur_proc = Processor::current();
  1717. ASSERT(cpu != cur_proc.id());
  1718. auto& target_proc = processors()[cpu];
  1719. msg.async = async;
  1720. dbgln<SMP_DEBUG>("SMP[{}]: Send message {} to cpu #{} proc: {}", cur_proc.id(), VirtualAddress(&msg), cpu, VirtualAddress(&target_proc));
  1721. atomic_store(&msg.refs, 1u, AK::MemoryOrder::memory_order_release);
  1722. if (target_proc->smp_queue_message(msg)) {
  1723. APIC::the().send_ipi(cpu);
  1724. }
  1725. if (!async) {
  1726. // If synchronous then we must cleanup and return the message back
  1727. // to the pool. Otherwise, the last processor to complete it will return it
  1728. while (atomic_load(&msg.refs, AK::MemoryOrder::memory_order_consume) != 0) {
  1729. // TODO: pause for a bit?
  1730. // We need to process any messages that may have been sent to
  1731. // us while we're waiting. This also checks if another processor
  1732. // may have requested us to halt.
  1733. cur_proc.smp_process_pending_messages();
  1734. }
  1735. smp_cleanup_message(msg);
  1736. smp_return_to_pool(msg);
  1737. }
  1738. }
  1739. void Processor::smp_unicast(u32 cpu, void (*callback)(void*), void* data, void (*free_data)(void*), bool async)
  1740. {
  1741. auto& msg = smp_get_from_pool();
  1742. msg.type = ProcessorMessage::CallbackWithData;
  1743. msg.callback_with_data.handler = callback;
  1744. msg.callback_with_data.data = data;
  1745. msg.callback_with_data.free = free_data;
  1746. smp_unicast_message(cpu, msg, async);
  1747. }
  1748. void Processor::smp_unicast(u32 cpu, void (*callback)(), bool async)
  1749. {
  1750. auto& msg = smp_get_from_pool();
  1751. msg.type = ProcessorMessage::CallbackWithData;
  1752. msg.callback.handler = callback;
  1753. smp_unicast_message(cpu, msg, async);
  1754. }
  1755. void Processor::smp_broadcast_flush_tlb(const PageDirectory* page_directory, VirtualAddress vaddr, size_t page_count)
  1756. {
  1757. auto& msg = smp_get_from_pool();
  1758. msg.async = false;
  1759. msg.type = ProcessorMessage::FlushTlb;
  1760. msg.flush_tlb.page_directory = page_directory;
  1761. msg.flush_tlb.ptr = vaddr.as_ptr();
  1762. msg.flush_tlb.page_count = page_count;
  1763. smp_broadcast_message(msg);
  1764. // While the other processors handle this request, we'll flush ours
  1765. flush_tlb_local(vaddr, page_count);
  1766. // Now wait until everybody is done as well
  1767. smp_broadcast_wait_sync(msg);
  1768. }
  1769. void Processor::smp_broadcast_halt()
  1770. {
  1771. // We don't want to use a message, because this could have been triggered
  1772. // by being out of memory and we might not be able to get a message
  1773. for_each(
  1774. [&](Processor& proc) -> IterationDecision {
  1775. proc.m_halt_requested.store(true, AK::MemoryOrder::memory_order_release);
  1776. return IterationDecision::Continue;
  1777. });
  1778. // Now trigger an IPI on all other APs
  1779. APIC::the().broadcast_ipi();
  1780. }
  1781. void Processor::Processor::halt()
  1782. {
  1783. if (s_smp_enabled)
  1784. smp_broadcast_halt();
  1785. halt_this();
  1786. }
  1787. void Processor::deferred_call_pool_init()
  1788. {
  1789. size_t pool_count = sizeof(m_deferred_call_pool) / sizeof(m_deferred_call_pool[0]);
  1790. for (size_t i = 0; i < pool_count; i++) {
  1791. auto& entry = m_deferred_call_pool[i];
  1792. entry.next = i < pool_count - 1 ? &m_deferred_call_pool[i + 1] : nullptr;
  1793. entry.was_allocated = false;
  1794. }
  1795. m_pending_deferred_calls = nullptr;
  1796. m_free_deferred_call_pool_entry = &m_deferred_call_pool[0];
  1797. }
  1798. void Processor::deferred_call_return_to_pool(DeferredCallEntry* entry)
  1799. {
  1800. ASSERT(m_in_critical);
  1801. ASSERT(!entry->was_allocated);
  1802. entry->next = m_free_deferred_call_pool_entry;
  1803. m_free_deferred_call_pool_entry = entry;
  1804. }
  1805. DeferredCallEntry* Processor::deferred_call_get_free()
  1806. {
  1807. ASSERT(m_in_critical);
  1808. if (m_free_deferred_call_pool_entry) {
  1809. // Fast path, we have an entry in our pool
  1810. auto* entry = m_free_deferred_call_pool_entry;
  1811. m_free_deferred_call_pool_entry = entry->next;
  1812. ASSERT(!entry->was_allocated);
  1813. return entry;
  1814. }
  1815. auto* entry = new DeferredCallEntry;
  1816. entry->was_allocated = true;
  1817. return entry;
  1818. }
  1819. void Processor::deferred_call_execute_pending()
  1820. {
  1821. ASSERT(m_in_critical);
  1822. if (!m_pending_deferred_calls)
  1823. return;
  1824. auto* pending_list = m_pending_deferred_calls;
  1825. m_pending_deferred_calls = nullptr;
  1826. // We pulled the stack of pending deferred calls in LIFO order, so we need to reverse the list first
  1827. auto reverse_list =
  1828. [](DeferredCallEntry* list) -> DeferredCallEntry*
  1829. {
  1830. DeferredCallEntry* rev_list = nullptr;
  1831. while (list) {
  1832. auto next = list->next;
  1833. list->next = rev_list;
  1834. rev_list = list;
  1835. list = next;
  1836. }
  1837. return rev_list;
  1838. };
  1839. pending_list = reverse_list(pending_list);
  1840. do {
  1841. // Call the appropriate callback handler
  1842. if (pending_list->have_data) {
  1843. pending_list->callback_with_data.handler(pending_list->callback_with_data.data);
  1844. if (pending_list->callback_with_data.free)
  1845. pending_list->callback_with_data.free(pending_list->callback_with_data.data);
  1846. } else {
  1847. pending_list->callback.handler();
  1848. }
  1849. // Return the entry back to the pool, or free it
  1850. auto* next = pending_list->next;
  1851. if (pending_list->was_allocated)
  1852. delete pending_list;
  1853. else
  1854. deferred_call_return_to_pool(pending_list);
  1855. pending_list = next;
  1856. } while (pending_list);
  1857. }
  1858. void Processor::deferred_call_queue_entry(DeferredCallEntry* entry)
  1859. {
  1860. ASSERT(m_in_critical);
  1861. entry->next = m_pending_deferred_calls;
  1862. m_pending_deferred_calls = entry;
  1863. }
  1864. void Processor::deferred_call_queue(void (*callback)())
  1865. {
  1866. // NOTE: If we are called outside of a critical section and outside
  1867. // of an irq handler, the function will be executed before we return!
  1868. ScopedCritical critical;
  1869. auto& cur_proc = Processor::current();
  1870. auto* entry = cur_proc.deferred_call_get_free();
  1871. entry->have_data = false;
  1872. entry->callback.handler = callback;
  1873. cur_proc.deferred_call_queue_entry(entry);
  1874. }
  1875. void Processor::deferred_call_queue(void (*callback)(void*), void* data, void (*free_data)(void*))
  1876. {
  1877. // NOTE: If we are called outside of a critical section and outside
  1878. // of an irq handler, the function will be executed before we return!
  1879. ScopedCritical critical;
  1880. auto& cur_proc = Processor::current();
  1881. auto* entry = cur_proc.deferred_call_get_free();
  1882. entry->have_data = true;
  1883. entry->callback_with_data.handler = callback;
  1884. entry->callback_with_data.data = data;
  1885. entry->callback_with_data.free = free_data;
  1886. cur_proc.deferred_call_queue_entry(entry);
  1887. }
  1888. void Processor::gdt_init()
  1889. {
  1890. m_gdt_length = 0;
  1891. m_gdtr.address = nullptr;
  1892. m_gdtr.limit = 0;
  1893. write_raw_gdt_entry(0x0000, 0x00000000, 0x00000000);
  1894. write_raw_gdt_entry(GDT_SELECTOR_CODE0, 0x0000ffff, 0x00cf9a00); // code0
  1895. write_raw_gdt_entry(GDT_SELECTOR_DATA0, 0x0000ffff, 0x00cf9200); // data0
  1896. write_raw_gdt_entry(GDT_SELECTOR_CODE3, 0x0000ffff, 0x00cffa00); // code3
  1897. write_raw_gdt_entry(GDT_SELECTOR_DATA3, 0x0000ffff, 0x00cff200); // data3
  1898. Descriptor tls_descriptor;
  1899. tls_descriptor.low = tls_descriptor.high = 0;
  1900. tls_descriptor.dpl = 3;
  1901. tls_descriptor.segment_present = 1;
  1902. tls_descriptor.granularity = 0;
  1903. tls_descriptor.zero = 0;
  1904. tls_descriptor.operation_size = 1;
  1905. tls_descriptor.descriptor_type = 1;
  1906. tls_descriptor.type = 2;
  1907. write_gdt_entry(GDT_SELECTOR_TLS, tls_descriptor); // tls3
  1908. Descriptor fs_descriptor;
  1909. fs_descriptor.set_base(this);
  1910. fs_descriptor.set_limit(sizeof(Processor));
  1911. fs_descriptor.dpl = 0;
  1912. fs_descriptor.segment_present = 1;
  1913. fs_descriptor.granularity = 0;
  1914. fs_descriptor.zero = 0;
  1915. fs_descriptor.operation_size = 1;
  1916. fs_descriptor.descriptor_type = 1;
  1917. fs_descriptor.type = 2;
  1918. write_gdt_entry(GDT_SELECTOR_PROC, fs_descriptor); // fs0
  1919. Descriptor tss_descriptor;
  1920. tss_descriptor.set_base(&m_tss);
  1921. tss_descriptor.set_limit(sizeof(TSS32));
  1922. tss_descriptor.dpl = 0;
  1923. tss_descriptor.segment_present = 1;
  1924. tss_descriptor.granularity = 0;
  1925. tss_descriptor.zero = 0;
  1926. tss_descriptor.operation_size = 1;
  1927. tss_descriptor.descriptor_type = 0;
  1928. tss_descriptor.type = 9;
  1929. write_gdt_entry(GDT_SELECTOR_TSS, tss_descriptor); // tss
  1930. flush_gdt();
  1931. load_task_register(GDT_SELECTOR_TSS);
  1932. asm volatile(
  1933. "mov %%ax, %%ds\n"
  1934. "mov %%ax, %%es\n"
  1935. "mov %%ax, %%gs\n"
  1936. "mov %%ax, %%ss\n" ::"a"(GDT_SELECTOR_DATA0)
  1937. : "memory");
  1938. set_fs(GDT_SELECTOR_PROC);
  1939. // Make sure CS points to the kernel code descriptor.
  1940. asm volatile(
  1941. "ljmpl $" __STRINGIFY(GDT_SELECTOR_CODE0) ", $sanity\n"
  1942. "sanity:\n");
  1943. }
  1944. void Processor::set_thread_specific(u8* data, size_t len)
  1945. {
  1946. auto& descriptor = get_gdt_entry(GDT_SELECTOR_TLS);
  1947. descriptor.set_base(data);
  1948. descriptor.set_limit(len);
  1949. }
  1950. }
  1951. #ifdef DEBUG
  1952. void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func)
  1953. {
  1954. asm volatile("cli");
  1955. klog() << "ASSERTION FAILED: " << msg << "\n"
  1956. << file << ":" << line << " in " << func;
  1957. // Switch back to the current process's page tables if there are any.
  1958. // Otherwise stack walking will be a disaster.
  1959. auto process = Process::current();
  1960. if (process)
  1961. MM.enter_process_paging_scope(*process);
  1962. Kernel::dump_backtrace();
  1963. Processor::halt();
  1964. }
  1965. #endif
  1966. NonMaskableInterruptDisabler::NonMaskableInterruptDisabler()
  1967. {
  1968. IO::out8(0x70, IO::in8(0x70) | 0x80);
  1969. }
  1970. NonMaskableInterruptDisabler::~NonMaskableInterruptDisabler()
  1971. {
  1972. IO::out8(0x70, IO::in8(0x70) & 0x7F);
  1973. }