CPU.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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::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::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. InterruptDisabler disable;
  666. Processor::current().enter_trap(*trap, false);
  667. }
  668. void enter_trap(TrapFrame* trap)
  669. {
  670. InterruptDisabler disable;
  671. Processor::current().enter_trap(*trap, true);
  672. }
  673. void exit_trap(TrapFrame* trap)
  674. {
  675. InterruptDisabler disable;
  676. return Processor::current().exit_trap(*trap);
  677. }
  678. static void sse_init()
  679. {
  680. asm volatile(
  681. "mov %cr0, %eax\n"
  682. "andl $0xfffffffb, %eax\n"
  683. "orl $0x2, %eax\n"
  684. "mov %eax, %cr0\n"
  685. "mov %cr4, %eax\n"
  686. "orl $0x600, %eax\n"
  687. "mov %eax, %cr4\n");
  688. }
  689. u32 read_cr0()
  690. {
  691. u32 cr0;
  692. asm("movl %%cr0, %%eax"
  693. : "=a"(cr0));
  694. return cr0;
  695. }
  696. u32 read_cr3()
  697. {
  698. u32 cr3;
  699. asm("movl %%cr3, %%eax"
  700. : "=a"(cr3));
  701. return cr3;
  702. }
  703. void write_cr3(u32 cr3)
  704. {
  705. // NOTE: If you're here from a GPF crash, it's very likely that a PDPT entry is incorrect, not this!
  706. asm volatile("movl %%eax, %%cr3" ::"a"(cr3)
  707. : "memory");
  708. }
  709. u32 read_cr4()
  710. {
  711. u32 cr4;
  712. asm("movl %%cr4, %%eax"
  713. : "=a"(cr4));
  714. return cr4;
  715. }
  716. u32 read_dr6()
  717. {
  718. u32 dr6;
  719. asm("movl %%dr6, %%eax"
  720. : "=a"(dr6));
  721. return dr6;
  722. }
  723. FPUState Processor::s_clean_fpu_state;
  724. static Vector<Processor*>* s_processors;
  725. static SpinLock s_processor_lock;
  726. volatile u32 Processor::g_total_processors;
  727. static volatile bool s_smp_enabled;
  728. Vector<Processor*>& Processor::processors()
  729. {
  730. ASSERT(s_processors);
  731. return *s_processors;
  732. }
  733. Processor& Processor::by_id(u32 cpu)
  734. {
  735. // s_processors does not need to be protected by a lock of any kind.
  736. // It is populated early in the boot process, and the BSP is waiting
  737. // for all APs to finish, after which this array never gets modified
  738. // again, so it's safe to not protect access to it here
  739. auto& procs = processors();
  740. ASSERT(procs[cpu] != nullptr);
  741. ASSERT(procs.size() > cpu);
  742. return *procs[cpu];
  743. }
  744. [[noreturn]] static inline void halt_this()
  745. {
  746. for (;;) {
  747. asm volatile("cli; hlt");
  748. }
  749. }
  750. void Processor::cpu_detect()
  751. {
  752. // NOTE: This is called during Processor::early_initialize, we cannot
  753. // safely log at this point because we don't have kmalloc
  754. // initialized yet!
  755. auto set_feature =
  756. [&](CPUFeature f) {
  757. m_features = static_cast<CPUFeature>(static_cast<u32>(m_features) | static_cast<u32>(f));
  758. };
  759. m_features = static_cast<CPUFeature>(0);
  760. CPUID processor_info(0x1);
  761. if (processor_info.edx() & (1 << 4))
  762. set_feature(CPUFeature::TSC);
  763. if (processor_info.edx() & (1 << 6))
  764. set_feature(CPUFeature::PAE);
  765. if (processor_info.edx() & (1 << 13))
  766. set_feature(CPUFeature::PGE);
  767. if (processor_info.edx() & (1 << 23))
  768. set_feature(CPUFeature::MMX);
  769. if (processor_info.edx() & (1 << 25))
  770. set_feature(CPUFeature::SSE);
  771. if (processor_info.edx() & (1 << 26))
  772. set_feature(CPUFeature::SSE2);
  773. if (processor_info.ecx() & (1 << 0))
  774. set_feature(CPUFeature::SSE3);
  775. if (processor_info.ecx() & (1 << 9))
  776. set_feature(CPUFeature::SSSE3);
  777. if (processor_info.ecx() & (1 << 19))
  778. set_feature(CPUFeature::SSE4_1);
  779. if (processor_info.ecx() & (1 << 20))
  780. set_feature(CPUFeature::SSE4_2);
  781. if (processor_info.ecx() & (1 << 30))
  782. set_feature(CPUFeature::RDRAND);
  783. if (processor_info.edx() & (1 << 11)) {
  784. u32 stepping = processor_info.eax() & 0xf;
  785. u32 model = (processor_info.eax() >> 4) & 0xf;
  786. u32 family = (processor_info.eax() >> 8) & 0xf;
  787. if (!(family == 6 && model < 3 && stepping < 3))
  788. set_feature(CPUFeature::SEP);
  789. if ((family == 6 && model >= 3) || (family == 0xf && model >= 0xe))
  790. set_feature(CPUFeature::CONSTANT_TSC);
  791. }
  792. u32 max_extended_leaf = CPUID(0x80000000).eax();
  793. ASSERT(max_extended_leaf >= 0x80000001);
  794. CPUID extended_processor_info(0x80000001);
  795. if (extended_processor_info.edx() & (1 << 20))
  796. set_feature(CPUFeature::NX);
  797. if (extended_processor_info.edx() & (1 << 27))
  798. set_feature(CPUFeature::RDTSCP);
  799. if (extended_processor_info.edx() & (1 << 11)) {
  800. // Only available in 64 bit mode
  801. set_feature(CPUFeature::SYSCALL);
  802. }
  803. if (max_extended_leaf >= 0x80000007) {
  804. CPUID cpuid(0x80000007);
  805. if (cpuid.edx() & (1 << 8)) {
  806. set_feature(CPUFeature::CONSTANT_TSC);
  807. set_feature(CPUFeature::NONSTOP_TSC);
  808. }
  809. }
  810. if (max_extended_leaf >= 0x80000008) {
  811. // CPUID.80000008H:EAX[7:0] reports the physical-address width supported by the processor.
  812. CPUID cpuid(0x80000008);
  813. m_physical_address_bit_width = cpuid.eax() & 0xff;
  814. } else {
  815. // 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.
  816. m_physical_address_bit_width = has_feature(CPUFeature::PAE) ? 36 : 32;
  817. }
  818. CPUID extended_features(0x7);
  819. if (extended_features.ebx() & (1 << 20))
  820. set_feature(CPUFeature::SMAP);
  821. if (extended_features.ebx() & (1 << 7))
  822. set_feature(CPUFeature::SMEP);
  823. if (extended_features.ecx() & (1 << 2))
  824. set_feature(CPUFeature::UMIP);
  825. if (extended_features.ebx() & (1 << 18))
  826. set_feature(CPUFeature::RDSEED);
  827. }
  828. void Processor::cpu_setup()
  829. {
  830. // NOTE: This is called during Processor::early_initialize, we cannot
  831. // safely log at this point because we don't have kmalloc
  832. // initialized yet!
  833. cpu_detect();
  834. if (has_feature(CPUFeature::SSE))
  835. sse_init();
  836. asm volatile(
  837. "movl %%cr0, %%eax\n"
  838. "orl $0x00010000, %%eax\n"
  839. "movl %%eax, %%cr0\n" ::
  840. : "%eax", "memory");
  841. if (has_feature(CPUFeature::PGE)) {
  842. // Turn on CR4.PGE so the CPU will respect the G bit in page tables.
  843. asm volatile(
  844. "mov %cr4, %eax\n"
  845. "orl $0x80, %eax\n"
  846. "mov %eax, %cr4\n");
  847. }
  848. if (has_feature(CPUFeature::NX)) {
  849. // Turn on IA32_EFER.NXE
  850. asm volatile(
  851. "movl $0xc0000080, %ecx\n"
  852. "rdmsr\n"
  853. "orl $0x800, %eax\n"
  854. "wrmsr\n");
  855. }
  856. if (has_feature(CPUFeature::SMEP)) {
  857. // Turn on CR4.SMEP
  858. asm volatile(
  859. "mov %cr4, %eax\n"
  860. "orl $0x100000, %eax\n"
  861. "mov %eax, %cr4\n");
  862. }
  863. if (has_feature(CPUFeature::SMAP)) {
  864. // Turn on CR4.SMAP
  865. asm volatile(
  866. "mov %cr4, %eax\n"
  867. "orl $0x200000, %eax\n"
  868. "mov %eax, %cr4\n");
  869. }
  870. if (has_feature(CPUFeature::UMIP)) {
  871. asm volatile(
  872. "mov %cr4, %eax\n"
  873. "orl $0x800, %eax\n"
  874. "mov %eax, %cr4\n");
  875. }
  876. if (has_feature(CPUFeature::TSC)) {
  877. asm volatile(
  878. "mov %cr4, %eax\n"
  879. "orl $0x4, %eax\n"
  880. "mov %eax, %cr4\n");
  881. }
  882. }
  883. String Processor::features_string() const
  884. {
  885. StringBuilder builder;
  886. auto feature_to_str =
  887. [](CPUFeature f) -> const char*
  888. {
  889. switch (f) {
  890. case CPUFeature::NX:
  891. return "nx";
  892. case CPUFeature::PAE:
  893. return "pae";
  894. case CPUFeature::PGE:
  895. return "pge";
  896. case CPUFeature::RDRAND:
  897. return "rdrand";
  898. case CPUFeature::RDSEED:
  899. return "rdseed";
  900. case CPUFeature::SMAP:
  901. return "smap";
  902. case CPUFeature::SMEP:
  903. return "smep";
  904. case CPUFeature::SSE:
  905. return "sse";
  906. case CPUFeature::TSC:
  907. return "tsc";
  908. case CPUFeature::RDTSCP:
  909. return "rdtscp";
  910. case CPUFeature::CONSTANT_TSC:
  911. return "constant_tsc";
  912. case CPUFeature::NONSTOP_TSC:
  913. return "nonstop_tsc";
  914. case CPUFeature::UMIP:
  915. return "umip";
  916. case CPUFeature::SEP:
  917. return "sep";
  918. case CPUFeature::SYSCALL:
  919. return "syscall";
  920. case CPUFeature::MMX:
  921. return "mmx";
  922. case CPUFeature::SSE2:
  923. return "sse2";
  924. case CPUFeature::SSE3:
  925. return "sse3";
  926. case CPUFeature::SSSE3:
  927. return "ssse3";
  928. case CPUFeature::SSE4_1:
  929. return "sse4.1";
  930. case CPUFeature::SSE4_2:
  931. return "sse4.2";
  932. // no default statement here intentionally so that we get
  933. // a warning if a new feature is forgotten to be added here
  934. }
  935. // Shouldn't ever happen
  936. return "???";
  937. };
  938. bool first = true;
  939. for (u32 flag = 1; flag != 0; flag <<= 1) {
  940. if ((static_cast<u32>(m_features) & flag) != 0) {
  941. if (first)
  942. first = false;
  943. else
  944. builder.append(' ');
  945. auto str = feature_to_str(static_cast<CPUFeature>(flag));
  946. builder.append(str, strlen(str));
  947. }
  948. }
  949. return builder.build();
  950. }
  951. void Processor::early_initialize(u32 cpu)
  952. {
  953. m_self = this;
  954. m_cpu = cpu;
  955. m_in_irq = 0;
  956. m_in_critical = 0;
  957. m_invoke_scheduler_async = false;
  958. m_scheduler_initialized = false;
  959. m_message_queue = nullptr;
  960. m_idle_thread = nullptr;
  961. m_current_thread = nullptr;
  962. m_scheduler_data = nullptr;
  963. m_mm_data = nullptr;
  964. m_info = nullptr;
  965. m_halt_requested = false;
  966. if (cpu == 0) {
  967. s_smp_enabled = false;
  968. atomic_store(&g_total_processors, 1u, AK::MemoryOrder::memory_order_release);
  969. } else {
  970. atomic_fetch_add(&g_total_processors, 1u, AK::MemoryOrder::memory_order_acq_rel);
  971. }
  972. deferred_call_pool_init();
  973. cpu_setup();
  974. gdt_init();
  975. ASSERT(is_initialized()); // sanity check
  976. ASSERT(&current() == this); // sanity check
  977. }
  978. void Processor::initialize(u32 cpu)
  979. {
  980. ASSERT(m_self == this);
  981. ASSERT(&current() == this); // sanity check
  982. klog() << "CPU[" << id() << "]: Supported features: " << features_string();
  983. if (!has_feature(CPUFeature::RDRAND))
  984. klog() << "CPU[" << id() << "]: No RDRAND support detected, randomness will be poor";
  985. klog() << "CPU[" << id() << "]: Physical address bit width: " << m_physical_address_bit_width;
  986. if (cpu == 0)
  987. idt_init();
  988. else
  989. flush_idt();
  990. if (cpu == 0) {
  991. ASSERT((FlatPtr(&s_clean_fpu_state) & 0xF) == 0);
  992. asm volatile("fninit");
  993. asm volatile("fxsave %0"
  994. : "=m"(s_clean_fpu_state));
  995. }
  996. m_info = new ProcessorInfo(*this);
  997. {
  998. ScopedSpinLock lock(s_processor_lock);
  999. // We need to prevent races between APs starting up at the same time
  1000. if (!s_processors)
  1001. s_processors = new Vector<Processor*>();
  1002. if (cpu >= s_processors->size())
  1003. s_processors->resize(cpu + 1);
  1004. (*s_processors)[cpu] = this;
  1005. }
  1006. }
  1007. void Processor::write_raw_gdt_entry(u16 selector, u32 low, u32 high)
  1008. {
  1009. u16 i = (selector & 0xfffc) >> 3;
  1010. u32 prev_gdt_length = m_gdt_length;
  1011. if (i > m_gdt_length) {
  1012. m_gdt_length = i + 1;
  1013. ASSERT(m_gdt_length <= sizeof(m_gdt) / sizeof(m_gdt[0]));
  1014. m_gdtr.limit = (m_gdt_length + 1) * 8 - 1;
  1015. }
  1016. m_gdt[i].low = low;
  1017. m_gdt[i].high = high;
  1018. // clear selectors we may have skipped
  1019. while (i < prev_gdt_length) {
  1020. m_gdt[i].low = 0;
  1021. m_gdt[i].high = 0;
  1022. i++;
  1023. }
  1024. }
  1025. void Processor::write_gdt_entry(u16 selector, Descriptor& descriptor)
  1026. {
  1027. write_raw_gdt_entry(selector, descriptor.low, descriptor.high);
  1028. }
  1029. Descriptor& Processor::get_gdt_entry(u16 selector)
  1030. {
  1031. u16 i = (selector & 0xfffc) >> 3;
  1032. return *(Descriptor*)(&m_gdt[i]);
  1033. }
  1034. void Processor::flush_gdt()
  1035. {
  1036. m_gdtr.address = m_gdt;
  1037. m_gdtr.limit = (m_gdt_length * 8) - 1;
  1038. asm volatile("lgdt %0" ::"m"(m_gdtr)
  1039. : "memory");
  1040. }
  1041. const DescriptorTablePointer& Processor::get_gdtr()
  1042. {
  1043. return m_gdtr;
  1044. }
  1045. Vector<FlatPtr> Processor::capture_stack_trace(Thread& thread, size_t max_frames)
  1046. {
  1047. FlatPtr frame_ptr = 0, eip = 0;
  1048. Vector<FlatPtr, 32> stack_trace;
  1049. auto walk_stack = [&](FlatPtr stack_ptr)
  1050. {
  1051. static constexpr size_t max_stack_frames = 4096;
  1052. stack_trace.append(eip);
  1053. size_t count = 1;
  1054. while (stack_ptr && stack_trace.size() < max_stack_frames) {
  1055. FlatPtr retaddr;
  1056. count++;
  1057. if (max_frames != 0 && count > max_frames)
  1058. break;
  1059. if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
  1060. if (!copy_from_user(&retaddr, &((FlatPtr*)stack_ptr)[1]) || !retaddr)
  1061. break;
  1062. stack_trace.append(retaddr);
  1063. if (!copy_from_user(&stack_ptr, (FlatPtr*)stack_ptr))
  1064. break;
  1065. } else {
  1066. void* fault_at;
  1067. if (!safe_memcpy(&retaddr, &((FlatPtr*)stack_ptr)[1], sizeof(FlatPtr), fault_at) || !retaddr)
  1068. break;
  1069. stack_trace.append(retaddr);
  1070. if (!safe_memcpy(&stack_ptr, (FlatPtr*)stack_ptr, sizeof(FlatPtr), fault_at))
  1071. break;
  1072. }
  1073. }
  1074. };
  1075. auto capture_current_thread = [&]()
  1076. {
  1077. frame_ptr = (FlatPtr)__builtin_frame_address(0);
  1078. eip = (FlatPtr)__builtin_return_address(0);
  1079. walk_stack(frame_ptr);
  1080. };
  1081. // Since the thread may be running on another processor, there
  1082. // is a chance a context switch may happen while we're trying
  1083. // to get it. It also won't be entirely accurate and merely
  1084. // reflect the status at the last context switch.
  1085. ScopedSpinLock lock(g_scheduler_lock);
  1086. if (&thread == Processor::current_thread()) {
  1087. ASSERT(thread.state() == Thread::Running);
  1088. // Leave the scheduler lock. If we trigger page faults we may
  1089. // need to be preempted. Since this is our own thread it won't
  1090. // cause any problems as the stack won't change below this frame.
  1091. lock.unlock();
  1092. capture_current_thread();
  1093. } else if (thread.is_active()) {
  1094. ASSERT(thread.cpu() != Processor::id());
  1095. // If this is the case, the thread is currently running
  1096. // on another processor. We can't trust the kernel stack as
  1097. // it may be changing at any time. We need to probably send
  1098. // an IPI to that processor, have it walk the stack and wait
  1099. // until it returns the data back to us
  1100. auto& proc = Processor::current();
  1101. smp_unicast(thread.cpu(),
  1102. [&]() {
  1103. dbgln("CPU[{}] getting stack for cpu #{}", Processor::id(), proc.get_id());
  1104. ProcessPagingScope paging_scope(thread.process());
  1105. ASSERT(&Processor::current() != &proc);
  1106. ASSERT(&thread == Processor::current_thread());
  1107. // NOTE: Because the other processor is still holding the
  1108. // scheduler lock while waiting for this callback to finish,
  1109. // the current thread on the target processor cannot change
  1110. // TODO: What to do about page faults here? We might deadlock
  1111. // because the other processor is still holding the
  1112. // scheduler lock...
  1113. capture_current_thread();
  1114. }, false);
  1115. } else {
  1116. switch (thread.state()) {
  1117. case Thread::Running:
  1118. ASSERT_NOT_REACHED(); // should have been handled above
  1119. case Thread::Runnable:
  1120. case Thread::Stopped:
  1121. case Thread::Blocked:
  1122. case Thread::Dying:
  1123. case Thread::Dead: {
  1124. // We need to retrieve ebp from what was last pushed to the kernel
  1125. // stack. Before switching out of that thread, it switch_context
  1126. // pushed the callee-saved registers, and the last of them happens
  1127. // to be ebp.
  1128. ProcessPagingScope paging_scope(thread.process());
  1129. auto& tss = thread.tss();
  1130. u32* stack_top = reinterpret_cast<u32*>(tss.esp);
  1131. if (is_user_range(VirtualAddress(stack_top), sizeof(FlatPtr))) {
  1132. if (!copy_from_user(&frame_ptr, &((FlatPtr*)stack_top)[0]))
  1133. frame_ptr = 0;
  1134. } else {
  1135. void* fault_at;
  1136. if (!safe_memcpy(&frame_ptr, &((FlatPtr*)stack_top)[0], sizeof(FlatPtr), fault_at))
  1137. frame_ptr = 0;
  1138. }
  1139. eip = tss.eip;
  1140. // TODO: We need to leave the scheduler lock here, but we also
  1141. // need to prevent the target thread from being run while
  1142. // we walk the stack
  1143. lock.unlock();
  1144. walk_stack(frame_ptr);
  1145. break;
  1146. }
  1147. default:
  1148. dbgln("Cannot capture stack trace for thread {} in state {}", thread, thread.state_string());
  1149. break;
  1150. }
  1151. }
  1152. return stack_trace;
  1153. }
  1154. extern "C" void enter_thread_context(Thread* from_thread, Thread* to_thread)
  1155. {
  1156. ASSERT(from_thread == to_thread || from_thread->state() != Thread::Running);
  1157. ASSERT(to_thread->state() == Thread::Running);
  1158. Processor::set_current_thread(*to_thread);
  1159. auto& from_tss = from_thread->tss();
  1160. auto& to_tss = to_thread->tss();
  1161. asm volatile("fxsave %0"
  1162. : "=m"(from_thread->fpu_state()));
  1163. from_tss.fs = get_fs();
  1164. from_tss.gs = get_gs();
  1165. set_fs(to_tss.fs);
  1166. set_gs(to_tss.gs);
  1167. auto& processor = Processor::current();
  1168. auto& tls_descriptor = processor.get_gdt_entry(GDT_SELECTOR_TLS);
  1169. tls_descriptor.set_base(to_thread->thread_specific_data().as_ptr());
  1170. tls_descriptor.set_limit(to_thread->thread_specific_region_size());
  1171. if (from_tss.cr3 != to_tss.cr3)
  1172. write_cr3(to_tss.cr3);
  1173. to_thread->set_cpu(processor.get_id());
  1174. processor.restore_in_critical(to_thread->saved_critical());
  1175. asm volatile("fxrstor %0"
  1176. ::"m"(to_thread->fpu_state()));
  1177. // TODO: debug registers
  1178. // TODO: ioperm?
  1179. }
  1180. #define ENTER_THREAD_CONTEXT_ARGS_SIZE (2 * 4) // to_thread, from_thread
  1181. void Processor::switch_context(Thread*& from_thread, Thread*& to_thread)
  1182. {
  1183. ASSERT(!in_irq());
  1184. ASSERT(m_in_critical == 1);
  1185. ASSERT(is_kernel_mode());
  1186. dbgln<CONTEXT_SWITCH_DEBUG>("switch_context --> switching out of: {} {}", VirtualAddress(from_thread), *from_thread);
  1187. from_thread->save_critical(m_in_critical);
  1188. // Switch to new thread context, passing from_thread and to_thread
  1189. // through to the new context using registers edx and eax
  1190. asm volatile(
  1191. // NOTE: changing how much we push to the stack affects
  1192. // SWITCH_CONTEXT_TO_STACK_SIZE and thread_context_first_enter()!
  1193. "pushfl \n"
  1194. "pushl %%ebx \n"
  1195. "pushl %%esi \n"
  1196. "pushl %%edi \n"
  1197. "pushl %%ebp \n"
  1198. "movl %%esp, %[from_esp] \n"
  1199. "movl $1f, %[from_eip] \n"
  1200. "movl %[to_esp0], %%ebx \n"
  1201. "movl %%ebx, %[tss_esp0] \n"
  1202. "movl %[to_esp], %%esp \n"
  1203. "pushl %[to_thread] \n"
  1204. "pushl %[from_thread] \n"
  1205. "pushl %[to_eip] \n"
  1206. "cld \n"
  1207. "jmp enter_thread_context \n"
  1208. "1: \n"
  1209. "popl %%edx \n"
  1210. "popl %%eax \n"
  1211. "popl %%ebp \n"
  1212. "popl %%edi \n"
  1213. "popl %%esi \n"
  1214. "popl %%ebx \n"
  1215. "popfl \n"
  1216. : [from_esp] "=m" (from_thread->tss().esp),
  1217. [from_eip] "=m" (from_thread->tss().eip),
  1218. [tss_esp0] "=m" (m_tss.esp0),
  1219. "=d" (from_thread), // needed so that from_thread retains the correct value
  1220. "=a" (to_thread) // needed so that to_thread retains the correct value
  1221. : [to_esp] "g" (to_thread->tss().esp),
  1222. [to_esp0] "g" (to_thread->tss().esp0),
  1223. [to_eip] "c" (to_thread->tss().eip),
  1224. [from_thread] "d" (from_thread),
  1225. [to_thread] "a" (to_thread)
  1226. : "memory"
  1227. );
  1228. dbgln<CONTEXT_SWITCH_DEBUG>("switch_context <-- from {} {} to {} {}", VirtualAddress(from_thread), *from_thread, VirtualAddress(to_thread), *to_thread);
  1229. Processor::current().restore_in_critical(to_thread->saved_critical());
  1230. }
  1231. extern "C" void context_first_init([[maybe_unused]] Thread* from_thread, [[maybe_unused]] Thread* to_thread, [[maybe_unused]] TrapFrame* trap)
  1232. {
  1233. ASSERT(!are_interrupts_enabled());
  1234. ASSERT(is_kernel_mode());
  1235. dbgln<CONTEXT_SWITCH_DEBUG>("switch_context <-- from {} {} to {} {} (context_first_init)", VirtualAddress(from_thread), *from_thread, VirtualAddress(to_thread), *to_thread);
  1236. ASSERT(to_thread == Thread::current());
  1237. Scheduler::enter_current(*from_thread, true);
  1238. // Since we got here and don't have Scheduler::context_switch in the
  1239. // call stack (because this is the first time we switched into this
  1240. // context), we need to notify the scheduler so that it can release
  1241. // the scheduler lock. We don't want to enable interrupts at this point
  1242. // as we're still in the middle of a context switch. Doing so could
  1243. // trigger a context switch within a context switch, leading to a crash.
  1244. Scheduler::leave_on_first_switch(trap->regs->eflags & ~0x200);
  1245. }
  1246. extern "C" void thread_context_first_enter(void);
  1247. asm(
  1248. // enter_thread_context returns to here first time a thread is executing
  1249. ".globl thread_context_first_enter \n"
  1250. "thread_context_first_enter: \n"
  1251. // switch_context will have pushed from_thread and to_thread to our new
  1252. // stack prior to thread_context_first_enter() being called, and the
  1253. // pointer to TrapFrame was the top of the stack before that
  1254. " movl 8(%esp), %ebx \n" // save pointer to TrapFrame
  1255. " cld \n"
  1256. " call context_first_init \n"
  1257. " addl $" __STRINGIFY(ENTER_THREAD_CONTEXT_ARGS_SIZE) ", %esp \n"
  1258. " movl %ebx, 0(%esp) \n" // push pointer to TrapFrame
  1259. " jmp common_trap_exit \n"
  1260. );
  1261. void exit_kernel_thread(void)
  1262. {
  1263. Thread::current()->exit();
  1264. }
  1265. u32 Processor::init_context(Thread& thread, bool leave_crit)
  1266. {
  1267. ASSERT(is_kernel_mode());
  1268. ASSERT(g_scheduler_lock.is_locked());
  1269. if (leave_crit) {
  1270. // Leave the critical section we set up in in Process::exec,
  1271. // but because we still have the scheduler lock we should end up with 1
  1272. m_in_critical--; // leave it without triggering anything or restoring flags
  1273. ASSERT(in_critical() == 1);
  1274. }
  1275. u32 kernel_stack_top = thread.kernel_stack_top();
  1276. u32 stack_top = kernel_stack_top;
  1277. // TODO: handle NT?
  1278. ASSERT((cpu_flags() & 0x24000) == 0); // Assume !(NT | VM)
  1279. auto& tss = thread.tss();
  1280. bool return_to_user = (tss.cs & 3) != 0;
  1281. // make room for an interrupt frame
  1282. if (!return_to_user) {
  1283. // userspace_esp and userspace_ss are not popped off by iret
  1284. // unless we're switching back to user mode
  1285. stack_top -= sizeof(RegisterState) - 2 * sizeof(u32);
  1286. // For kernel threads we'll push the thread function argument
  1287. // which should be in tss.esp and exit_kernel_thread as return
  1288. // address.
  1289. stack_top -= 2 * sizeof(u32);
  1290. *reinterpret_cast<u32*>(kernel_stack_top - 2 * sizeof(u32)) = tss.esp;
  1291. *reinterpret_cast<u32*>(kernel_stack_top - 3 * sizeof(u32)) = FlatPtr(&exit_kernel_thread);
  1292. } else {
  1293. stack_top -= sizeof(RegisterState);
  1294. }
  1295. // we want to end up 16-byte aligned, %esp + 4 should be aligned
  1296. stack_top -= sizeof(u32);
  1297. *reinterpret_cast<u32*>(kernel_stack_top - sizeof(u32)) = 0;
  1298. // set up the stack so that after returning from thread_context_first_enter()
  1299. // we will end up either in kernel mode or user mode, depending on how the thread is set up
  1300. // However, the first step is to always start in kernel mode with thread_context_first_enter
  1301. RegisterState& iretframe = *reinterpret_cast<RegisterState*>(stack_top);
  1302. iretframe.ss = tss.ss;
  1303. iretframe.gs = tss.gs;
  1304. iretframe.fs = tss.fs;
  1305. iretframe.es = tss.es;
  1306. iretframe.ds = tss.ds;
  1307. iretframe.edi = tss.edi;
  1308. iretframe.esi = tss.esi;
  1309. iretframe.ebp = tss.ebp;
  1310. iretframe.esp = 0;
  1311. iretframe.ebx = tss.ebx;
  1312. iretframe.edx = tss.edx;
  1313. iretframe.ecx = tss.ecx;
  1314. iretframe.eax = tss.eax;
  1315. iretframe.eflags = tss.eflags;
  1316. iretframe.eip = tss.eip;
  1317. iretframe.cs = tss.cs;
  1318. if (return_to_user) {
  1319. iretframe.userspace_esp = tss.esp;
  1320. iretframe.userspace_ss = tss.ss;
  1321. }
  1322. // make space for a trap frame
  1323. stack_top -= sizeof(TrapFrame);
  1324. TrapFrame& trap = *reinterpret_cast<TrapFrame*>(stack_top);
  1325. trap.regs = &iretframe;
  1326. trap.prev_irq_level = 0;
  1327. trap.next_trap = nullptr;
  1328. stack_top -= sizeof(u32); // pointer to TrapFrame
  1329. *reinterpret_cast<u32*>(stack_top) = stack_top + 4;
  1330. if constexpr (CONTEXT_SWITCH_DEBUG) {
  1331. if (return_to_user) {
  1332. dbgln("init_context {} ({}) set up to execute at eip={}:{}, esp={}, stack_top={}, user_top={}:{}",
  1333. thread,
  1334. VirtualAddress(&thread),
  1335. iretframe.cs, tss.eip,
  1336. VirtualAddress(tss.esp),
  1337. VirtualAddress(stack_top),
  1338. iretframe.userspace_ss,
  1339. iretframe.userspace_esp);
  1340. } else {
  1341. dbgln("init_context {} ({}) set up to execute at eip={}:{}, esp={}, stack_top={}",
  1342. thread,
  1343. VirtualAddress(&thread),
  1344. iretframe.cs, tss.eip,
  1345. VirtualAddress(tss.esp),
  1346. VirtualAddress(stack_top));
  1347. }
  1348. }
  1349. // make switch_context() always first return to thread_context_first_enter()
  1350. // in kernel mode, so set up these values so that we end up popping iretframe
  1351. // off the stack right after the context switch completed, at which point
  1352. // control is transferred to what iretframe is pointing to.
  1353. tss.eip = FlatPtr(&thread_context_first_enter);
  1354. tss.esp0 = kernel_stack_top;
  1355. tss.esp = stack_top;
  1356. tss.cs = GDT_SELECTOR_CODE0;
  1357. tss.ds = GDT_SELECTOR_DATA0;
  1358. tss.es = GDT_SELECTOR_DATA0;
  1359. tss.gs = GDT_SELECTOR_DATA0;
  1360. tss.ss = GDT_SELECTOR_DATA0;
  1361. tss.fs = GDT_SELECTOR_PROC;
  1362. return stack_top;
  1363. }
  1364. extern "C" u32 do_init_context(Thread* thread, u32 flags)
  1365. {
  1366. ASSERT_INTERRUPTS_DISABLED();
  1367. thread->tss().eflags = flags;
  1368. return Processor::current().init_context(*thread, true);
  1369. }
  1370. extern "C" void do_assume_context(Thread* thread, u32 flags);
  1371. asm(
  1372. ".global do_assume_context \n"
  1373. "do_assume_context: \n"
  1374. " movl 4(%esp), %ebx \n"
  1375. " movl 8(%esp), %esi \n"
  1376. // We're going to call Processor::init_context, so just make sure
  1377. // we have enough stack space so we don't stomp over it
  1378. " subl $(" __STRINGIFY(4 + REGISTER_STATE_SIZE + TRAP_FRAME_SIZE + 4) "), %esp \n"
  1379. " pushl %esi \n"
  1380. " pushl %ebx \n"
  1381. " cld \n"
  1382. " call do_init_context \n"
  1383. " addl $8, %esp \n"
  1384. " movl %eax, %esp \n" // move stack pointer to what Processor::init_context set up for us
  1385. " pushl %ebx \n" // push to_thread
  1386. " pushl %ebx \n" // push from_thread
  1387. " pushl $thread_context_first_enter \n" // should be same as tss.eip
  1388. " jmp enter_thread_context \n"
  1389. );
  1390. void Processor::assume_context(Thread& thread, u32 flags)
  1391. {
  1392. dbgln<CONTEXT_SWITCH_DEBUG>("Assume context for thread {} {}", VirtualAddress(&thread), thread);
  1393. ASSERT_INTERRUPTS_DISABLED();
  1394. Scheduler::prepare_after_exec();
  1395. // in_critical() should be 2 here. The critical section in Process::exec
  1396. // and then the scheduler lock
  1397. ASSERT(Processor::current().in_critical() == 2);
  1398. do_assume_context(&thread, flags);
  1399. ASSERT_NOT_REACHED();
  1400. }
  1401. extern "C" void pre_init_finished(void)
  1402. {
  1403. ASSERT(g_scheduler_lock.own_lock());
  1404. // Because init_finished() will wait on the other APs, we need
  1405. // to release the scheduler lock so that the other APs can also get
  1406. // to this point
  1407. // The target flags will get restored upon leaving the trap
  1408. u32 prev_flags = cpu_flags();
  1409. Scheduler::leave_on_first_switch(prev_flags);
  1410. }
  1411. extern "C" void post_init_finished(void)
  1412. {
  1413. // We need to re-acquire the scheduler lock before a context switch
  1414. // transfers control into the idle loop, which needs the lock held
  1415. Scheduler::prepare_for_idle_loop();
  1416. }
  1417. void Processor::initialize_context_switching(Thread& initial_thread)
  1418. {
  1419. ASSERT(initial_thread.process().is_kernel_process());
  1420. auto& tss = initial_thread.tss();
  1421. m_tss = tss;
  1422. m_tss.esp0 = tss.esp0;
  1423. m_tss.ss0 = GDT_SELECTOR_DATA0;
  1424. // user mode needs to be able to switch to kernel mode:
  1425. m_tss.cs = m_tss.ds = m_tss.es = m_tss.gs = m_tss.ss = GDT_SELECTOR_CODE0 | 3;
  1426. m_tss.fs = GDT_SELECTOR_PROC | 3;
  1427. m_scheduler_initialized = true;
  1428. asm volatile(
  1429. "movl %[new_esp], %%esp \n" // switch to new stack
  1430. "pushl %[from_to_thread] \n" // to_thread
  1431. "pushl %[from_to_thread] \n" // from_thread
  1432. "pushl $" __STRINGIFY(GDT_SELECTOR_CODE0) " \n"
  1433. "pushl %[new_eip] \n" // save the entry eip to the stack
  1434. "movl %%esp, %%ebx \n"
  1435. "addl $20, %%ebx \n" // calculate pointer to TrapFrame
  1436. "pushl %%ebx \n"
  1437. "cld \n"
  1438. "pushl %[cpu] \n" // push argument for init_finished before register is clobbered
  1439. "call pre_init_finished \n"
  1440. "call init_finished \n"
  1441. "addl $4, %%esp \n"
  1442. "call post_init_finished \n"
  1443. "call enter_trap_no_irq \n"
  1444. "addl $4, %%esp \n"
  1445. "lret \n"
  1446. :: [new_esp] "g" (tss.esp),
  1447. [new_eip] "a" (tss.eip),
  1448. [from_to_thread] "b" (&initial_thread),
  1449. [cpu] "c" (id())
  1450. );
  1451. ASSERT_NOT_REACHED();
  1452. }
  1453. void Processor::enter_trap(TrapFrame& trap, bool raise_irq)
  1454. {
  1455. ASSERT_INTERRUPTS_DISABLED();
  1456. ASSERT(&Processor::current() == this);
  1457. trap.prev_irq_level = m_in_irq;
  1458. if (raise_irq)
  1459. m_in_irq++;
  1460. auto* current_thread = Processor::current_thread();
  1461. if (current_thread) {
  1462. auto& current_trap = current_thread->current_trap();
  1463. trap.next_trap = current_trap;
  1464. current_trap = &trap;
  1465. // The cs register of this trap tells us where we will return back to
  1466. current_thread->set_previous_mode(((trap.regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode);
  1467. } else {
  1468. trap.next_trap = nullptr;
  1469. }
  1470. }
  1471. void Processor::exit_trap(TrapFrame& trap)
  1472. {
  1473. ASSERT_INTERRUPTS_DISABLED();
  1474. ASSERT(&Processor::current() == this);
  1475. ASSERT(m_in_irq >= trap.prev_irq_level);
  1476. m_in_irq = trap.prev_irq_level;
  1477. smp_process_pending_messages();
  1478. if (!m_in_irq && !m_in_critical)
  1479. check_invoke_scheduler();
  1480. auto* current_thread = Processor::current_thread();
  1481. if (current_thread) {
  1482. auto& current_trap = current_thread->current_trap();
  1483. current_trap = trap.next_trap;
  1484. if (current_trap) {
  1485. ASSERT(current_trap->regs);
  1486. // If we have another higher level trap then we probably returned
  1487. // from an interrupt or irq handler. The cs register of the
  1488. // new/higher level trap tells us what the mode prior to it was
  1489. current_thread->set_previous_mode(((current_trap->regs->cs & 3) != 0) ? Thread::PreviousMode::UserMode : Thread::PreviousMode::KernelMode);
  1490. } else {
  1491. // If we don't have a higher level trap then we're back in user mode.
  1492. // Unless we're a kernel process, in which case we're always in kernel mode
  1493. current_thread->set_previous_mode(current_thread->process().is_kernel_process() ? Thread::PreviousMode::KernelMode : Thread::PreviousMode::UserMode);
  1494. }
  1495. }
  1496. }
  1497. void Processor::check_invoke_scheduler()
  1498. {
  1499. ASSERT(!m_in_irq);
  1500. ASSERT(!m_in_critical);
  1501. if (m_invoke_scheduler_async && m_scheduler_initialized) {
  1502. m_invoke_scheduler_async = false;
  1503. Scheduler::invoke_async();
  1504. }
  1505. }
  1506. void Processor::flush_tlb_local(VirtualAddress vaddr, size_t page_count)
  1507. {
  1508. auto ptr = vaddr.as_ptr();
  1509. while (page_count > 0) {
  1510. asm volatile("invlpg %0"
  1511. :
  1512. : "m"(*ptr)
  1513. : "memory");
  1514. ptr += PAGE_SIZE;
  1515. page_count--;
  1516. }
  1517. }
  1518. void Processor::flush_tlb(const PageDirectory* page_directory, VirtualAddress vaddr, size_t page_count)
  1519. {
  1520. if (s_smp_enabled)
  1521. smp_broadcast_flush_tlb(page_directory, vaddr, page_count);
  1522. else
  1523. flush_tlb_local(vaddr, page_count);
  1524. }
  1525. static volatile ProcessorMessage* s_message_pool;
  1526. void Processor::smp_return_to_pool(ProcessorMessage& msg)
  1527. {
  1528. ProcessorMessage* next = nullptr;
  1529. do {
  1530. msg.next = next;
  1531. } while (!atomic_compare_exchange_strong(&s_message_pool, next, &msg, AK::MemoryOrder::memory_order_acq_rel));
  1532. }
  1533. ProcessorMessage& Processor::smp_get_from_pool()
  1534. {
  1535. ProcessorMessage* msg;
  1536. // The assumption is that messages are never removed from the pool!
  1537. for (;;) {
  1538. msg = atomic_load(&s_message_pool, AK::MemoryOrder::memory_order_consume);
  1539. if (!msg) {
  1540. if (!Processor::current().smp_process_pending_messages()) {
  1541. // TODO: pause for a bit?
  1542. }
  1543. continue;
  1544. }
  1545. // If another processor were to use this message in the meanwhile,
  1546. // "msg" is still valid (because it never gets freed). We'd detect
  1547. // this because the expected value "msg" and pool would
  1548. // no longer match, and the compare_exchange will fail. But accessing
  1549. // "msg->next" is always safe here.
  1550. if (atomic_compare_exchange_strong(&s_message_pool, msg, msg->next, AK::MemoryOrder::memory_order_acq_rel)) {
  1551. // We successfully "popped" this available message
  1552. break;
  1553. }
  1554. }
  1555. ASSERT(msg != nullptr);
  1556. return *msg;
  1557. }
  1558. Atomic<u32> Processor::s_idle_cpu_mask{ 0 };
  1559. u32 Processor::smp_wake_n_idle_processors(u32 wake_count)
  1560. {
  1561. ASSERT(Processor::current().in_critical());
  1562. ASSERT(wake_count > 0);
  1563. if (!s_smp_enabled)
  1564. return 0;
  1565. // Wake at most N - 1 processors
  1566. if (wake_count >= Processor::count()) {
  1567. wake_count = Processor::count() - 1;
  1568. ASSERT(wake_count > 0);
  1569. }
  1570. u32 current_id = Processor::current().id();
  1571. u32 did_wake_count = 0;
  1572. auto& apic = APIC::the();
  1573. while (did_wake_count < wake_count) {
  1574. // Try to get a set of idle CPUs and flip them to busy
  1575. u32 idle_mask = s_idle_cpu_mask.load(AK::MemoryOrder::memory_order_relaxed) & ~(1u << current_id);
  1576. u32 idle_count = __builtin_popcountl(idle_mask);
  1577. if (idle_count == 0)
  1578. break; // No (more) idle processor available
  1579. u32 found_mask = 0;
  1580. for (u32 i = 0; i < idle_count; i++) {
  1581. u32 cpu = __builtin_ffsl(idle_mask) - 1;
  1582. idle_mask &= ~(1u << cpu);
  1583. found_mask |= 1u << cpu;
  1584. }
  1585. idle_mask = s_idle_cpu_mask.fetch_and(~found_mask, AK::MemoryOrder::memory_order_acq_rel) & found_mask;
  1586. if (idle_mask == 0)
  1587. continue; // All of them were flipped to busy, try again
  1588. idle_count = __builtin_popcountl(idle_mask);
  1589. for (u32 i = 0; i < idle_count; i++) {
  1590. u32 cpu = __builtin_ffsl(idle_mask) - 1;
  1591. idle_mask &= ~(1u << cpu);
  1592. // Send an IPI to that CPU to wake it up. There is a possibility
  1593. // someone else woke it up as well, or that it woke up due to
  1594. // a timer interrupt. But we tried hard to avoid this...
  1595. apic.send_ipi(cpu);
  1596. did_wake_count++;
  1597. }
  1598. }
  1599. return did_wake_count;
  1600. }
  1601. void Processor::smp_enable()
  1602. {
  1603. size_t msg_pool_size = Processor::count() * 100u;
  1604. size_t msg_entries_cnt = Processor::count();
  1605. auto msgs = new ProcessorMessage[msg_pool_size];
  1606. auto msg_entries = new ProcessorMessageEntry[msg_pool_size * msg_entries_cnt];
  1607. size_t msg_entry_i = 0;
  1608. for (size_t i = 0; i < msg_pool_size; i++, msg_entry_i += msg_entries_cnt) {
  1609. auto& msg = msgs[i];
  1610. msg.next = i < msg_pool_size - 1 ? &msgs[i + 1] : nullptr;
  1611. msg.per_proc_entries = &msg_entries[msg_entry_i];
  1612. for (size_t k = 0; k < msg_entries_cnt; k++)
  1613. msg_entries[msg_entry_i + k].msg = &msg;
  1614. }
  1615. atomic_store(&s_message_pool, &msgs[0], AK::MemoryOrder::memory_order_release);
  1616. // Start sending IPI messages
  1617. s_smp_enabled = true;
  1618. }
  1619. void Processor::smp_cleanup_message(ProcessorMessage& msg)
  1620. {
  1621. switch (msg.type) {
  1622. case ProcessorMessage::CallbackWithData:
  1623. if (msg.callback_with_data.free)
  1624. msg.callback_with_data.free(msg.callback_with_data.data);
  1625. break;
  1626. default:
  1627. break;
  1628. }
  1629. }
  1630. bool Processor::smp_process_pending_messages()
  1631. {
  1632. bool did_process = false;
  1633. u32 prev_flags;
  1634. enter_critical(prev_flags);
  1635. if (auto pending_msgs = atomic_exchange(&m_message_queue, nullptr, AK::MemoryOrder::memory_order_acq_rel)) {
  1636. // We pulled the stack of pending messages in LIFO order, so we need to reverse the list first
  1637. auto reverse_list =
  1638. [](ProcessorMessageEntry* list) -> ProcessorMessageEntry*
  1639. {
  1640. ProcessorMessageEntry* rev_list = nullptr;
  1641. while (list) {
  1642. auto next = list->next;
  1643. list->next = rev_list;
  1644. rev_list = list;
  1645. list = next;
  1646. }
  1647. return rev_list;
  1648. };
  1649. pending_msgs = reverse_list(pending_msgs);
  1650. // now process in the right order
  1651. ProcessorMessageEntry* next_msg;
  1652. for (auto cur_msg = pending_msgs; cur_msg; cur_msg = next_msg) {
  1653. next_msg = cur_msg->next;
  1654. auto msg = cur_msg->msg;
  1655. dbgln<SMP_DEBUG>("SMP[{}]: Processing message {}", id(), VirtualAddress(msg));
  1656. switch (msg->type) {
  1657. case ProcessorMessage::Callback:
  1658. msg->callback.handler();
  1659. break;
  1660. case ProcessorMessage::CallbackWithData:
  1661. msg->callback_with_data.handler(msg->callback_with_data.data);
  1662. break;
  1663. case ProcessorMessage::FlushTlb:
  1664. if (is_user_address(VirtualAddress(msg->flush_tlb.ptr))) {
  1665. // We assume that we don't cross into kernel land!
  1666. ASSERT(is_user_range(VirtualAddress(msg->flush_tlb.ptr), msg->flush_tlb.page_count * PAGE_SIZE));
  1667. if (read_cr3() != msg->flush_tlb.page_directory->cr3()) {
  1668. // This processor isn't using this page directory right now, we can ignore this request
  1669. dbgln<SMP_DEBUG>("SMP[{}]: No need to flush {} pages at {}", id(), msg->flush_tlb.page_count, VirtualAddress(msg->flush_tlb.ptr));
  1670. break;
  1671. }
  1672. }
  1673. flush_tlb_local(VirtualAddress(msg->flush_tlb.ptr), msg->flush_tlb.page_count);
  1674. break;
  1675. }
  1676. bool is_async = msg->async; // Need to cache this value *before* dropping the ref count!
  1677. auto prev_refs = atomic_fetch_sub(&msg->refs, 1u, AK::MemoryOrder::memory_order_acq_rel);
  1678. ASSERT(prev_refs != 0);
  1679. if (prev_refs == 1) {
  1680. // All processors handled this. If this is an async message,
  1681. // we need to clean it up and return it to the pool
  1682. if (is_async) {
  1683. smp_cleanup_message(*msg);
  1684. smp_return_to_pool(*msg);
  1685. }
  1686. }
  1687. if (m_halt_requested.load(AK::MemoryOrder::memory_order_relaxed))
  1688. halt_this();
  1689. }
  1690. did_process = true;
  1691. } else if (m_halt_requested.load(AK::MemoryOrder::memory_order_relaxed)) {
  1692. halt_this();
  1693. }
  1694. leave_critical(prev_flags);
  1695. return did_process;
  1696. }
  1697. bool Processor::smp_queue_message(ProcessorMessage& msg)
  1698. {
  1699. // Note that it's quite possible that the other processor may pop
  1700. // the queue at any given time. We rely on the fact that the messages
  1701. // are pooled and never get freed!
  1702. auto& msg_entry = msg.per_proc_entries[id()];
  1703. ASSERT(msg_entry.msg == &msg);
  1704. ProcessorMessageEntry* next = nullptr;
  1705. do {
  1706. msg_entry.next = next;
  1707. } while (!atomic_compare_exchange_strong(&m_message_queue, next, &msg_entry, AK::MemoryOrder::memory_order_acq_rel));
  1708. return next == nullptr;
  1709. }
  1710. void Processor::smp_broadcast_message(ProcessorMessage& msg)
  1711. {
  1712. auto& cur_proc = Processor::current();
  1713. dbgln<SMP_DEBUG>("SMP[{}]: Broadcast message {} to cpus: {} proc: {}", cur_proc.get_id(), VirtualAddress(&msg), count(), VirtualAddress(&cur_proc));
  1714. atomic_store(&msg.refs, count() - 1, AK::MemoryOrder::memory_order_release);
  1715. ASSERT(msg.refs > 0);
  1716. bool need_broadcast = false;
  1717. for_each(
  1718. [&](Processor& proc) -> IterationDecision {
  1719. if (&proc != &cur_proc) {
  1720. if (proc.smp_queue_message(msg))
  1721. need_broadcast = true;
  1722. }
  1723. return IterationDecision::Continue;
  1724. });
  1725. // Now trigger an IPI on all other APs (unless all targets already had messages queued)
  1726. if (need_broadcast)
  1727. APIC::the().broadcast_ipi();
  1728. }
  1729. void Processor::smp_broadcast_wait_sync(ProcessorMessage& msg)
  1730. {
  1731. auto& cur_proc = Processor::current();
  1732. ASSERT(!msg.async);
  1733. // If synchronous then we must cleanup and return the message back
  1734. // to the pool. Otherwise, the last processor to complete it will return it
  1735. while (atomic_load(&msg.refs, AK::MemoryOrder::memory_order_consume) != 0) {
  1736. // TODO: pause for a bit?
  1737. // We need to process any messages that may have been sent to
  1738. // us while we're waiting. This also checks if another processor
  1739. // may have requested us to halt.
  1740. cur_proc.smp_process_pending_messages();
  1741. }
  1742. smp_cleanup_message(msg);
  1743. smp_return_to_pool(msg);
  1744. }
  1745. void Processor::smp_broadcast(void (*callback)(void*), void* data, void (*free_data)(void*), bool async)
  1746. {
  1747. auto& msg = smp_get_from_pool();
  1748. msg.async = async;
  1749. msg.type = ProcessorMessage::CallbackWithData;
  1750. msg.callback_with_data.handler = callback;
  1751. msg.callback_with_data.data = data;
  1752. msg.callback_with_data.free = free_data;
  1753. smp_broadcast_message(msg);
  1754. if (!async)
  1755. smp_broadcast_wait_sync(msg);
  1756. }
  1757. void Processor::smp_broadcast(void (*callback)(), bool async)
  1758. {
  1759. auto& msg = smp_get_from_pool();
  1760. msg.async = async;
  1761. msg.type = ProcessorMessage::CallbackWithData;
  1762. msg.callback.handler = callback;
  1763. smp_broadcast_message(msg);
  1764. if (!async)
  1765. smp_broadcast_wait_sync(msg);
  1766. }
  1767. void Processor::smp_unicast_message(u32 cpu, ProcessorMessage& msg, bool async)
  1768. {
  1769. auto& cur_proc = Processor::current();
  1770. ASSERT(cpu != cur_proc.get_id());
  1771. auto& target_proc = processors()[cpu];
  1772. msg.async = async;
  1773. dbgln<SMP_DEBUG>("SMP[{}]: Send message {} to cpu #{} proc: {}", cur_proc.get_id(), VirtualAddress(&msg), cpu, VirtualAddress(&target_proc));
  1774. atomic_store(&msg.refs, 1u, AK::MemoryOrder::memory_order_release);
  1775. if (target_proc->smp_queue_message(msg)) {
  1776. APIC::the().send_ipi(cpu);
  1777. }
  1778. if (!async) {
  1779. // If synchronous then we must cleanup and return the message back
  1780. // to the pool. Otherwise, the last processor to complete it will return it
  1781. while (atomic_load(&msg.refs, AK::MemoryOrder::memory_order_consume) != 0) {
  1782. // TODO: pause for a bit?
  1783. // We need to process any messages that may have been sent to
  1784. // us while we're waiting. This also checks if another processor
  1785. // may have requested us to halt.
  1786. cur_proc.smp_process_pending_messages();
  1787. }
  1788. smp_cleanup_message(msg);
  1789. smp_return_to_pool(msg);
  1790. }
  1791. }
  1792. void Processor::smp_unicast(u32 cpu, void (*callback)(void*), void* data, void (*free_data)(void*), bool async)
  1793. {
  1794. auto& msg = smp_get_from_pool();
  1795. msg.type = ProcessorMessage::CallbackWithData;
  1796. msg.callback_with_data.handler = callback;
  1797. msg.callback_with_data.data = data;
  1798. msg.callback_with_data.free = free_data;
  1799. smp_unicast_message(cpu, msg, async);
  1800. }
  1801. void Processor::smp_unicast(u32 cpu, void (*callback)(), bool async)
  1802. {
  1803. auto& msg = smp_get_from_pool();
  1804. msg.type = ProcessorMessage::CallbackWithData;
  1805. msg.callback.handler = callback;
  1806. smp_unicast_message(cpu, msg, async);
  1807. }
  1808. void Processor::smp_broadcast_flush_tlb(const PageDirectory* page_directory, VirtualAddress vaddr, size_t page_count)
  1809. {
  1810. auto& msg = smp_get_from_pool();
  1811. msg.async = false;
  1812. msg.type = ProcessorMessage::FlushTlb;
  1813. msg.flush_tlb.page_directory = page_directory;
  1814. msg.flush_tlb.ptr = vaddr.as_ptr();
  1815. msg.flush_tlb.page_count = page_count;
  1816. smp_broadcast_message(msg);
  1817. // While the other processors handle this request, we'll flush ours
  1818. flush_tlb_local(vaddr, page_count);
  1819. // Now wait until everybody is done as well
  1820. smp_broadcast_wait_sync(msg);
  1821. }
  1822. void Processor::smp_broadcast_halt()
  1823. {
  1824. // We don't want to use a message, because this could have been triggered
  1825. // by being out of memory and we might not be able to get a message
  1826. for_each(
  1827. [&](Processor& proc) -> IterationDecision {
  1828. proc.m_halt_requested.store(true, AK::MemoryOrder::memory_order_release);
  1829. return IterationDecision::Continue;
  1830. });
  1831. // Now trigger an IPI on all other APs
  1832. APIC::the().broadcast_ipi();
  1833. }
  1834. void Processor::Processor::halt()
  1835. {
  1836. if (s_smp_enabled)
  1837. smp_broadcast_halt();
  1838. halt_this();
  1839. }
  1840. void Processor::deferred_call_pool_init()
  1841. {
  1842. size_t pool_count = sizeof(m_deferred_call_pool) / sizeof(m_deferred_call_pool[0]);
  1843. for (size_t i = 0; i < pool_count; i++) {
  1844. auto& entry = m_deferred_call_pool[i];
  1845. entry.next = i < pool_count - 1 ? &m_deferred_call_pool[i + 1] : nullptr;
  1846. entry.was_allocated = false;
  1847. }
  1848. m_pending_deferred_calls = nullptr;
  1849. m_free_deferred_call_pool_entry = &m_deferred_call_pool[0];
  1850. }
  1851. void Processor::deferred_call_return_to_pool(DeferredCallEntry* entry)
  1852. {
  1853. ASSERT(m_in_critical);
  1854. ASSERT(!entry->was_allocated);
  1855. entry->next = m_free_deferred_call_pool_entry;
  1856. m_free_deferred_call_pool_entry = entry;
  1857. }
  1858. DeferredCallEntry* Processor::deferred_call_get_free()
  1859. {
  1860. ASSERT(m_in_critical);
  1861. if (m_free_deferred_call_pool_entry) {
  1862. // Fast path, we have an entry in our pool
  1863. auto* entry = m_free_deferred_call_pool_entry;
  1864. m_free_deferred_call_pool_entry = entry->next;
  1865. ASSERT(!entry->was_allocated);
  1866. return entry;
  1867. }
  1868. auto* entry = new DeferredCallEntry;
  1869. entry->was_allocated = true;
  1870. return entry;
  1871. }
  1872. void Processor::deferred_call_execute_pending()
  1873. {
  1874. ASSERT(m_in_critical);
  1875. if (!m_pending_deferred_calls)
  1876. return;
  1877. auto* pending_list = m_pending_deferred_calls;
  1878. m_pending_deferred_calls = nullptr;
  1879. // We pulled the stack of pending deferred calls in LIFO order, so we need to reverse the list first
  1880. auto reverse_list =
  1881. [](DeferredCallEntry* list) -> DeferredCallEntry*
  1882. {
  1883. DeferredCallEntry* rev_list = nullptr;
  1884. while (list) {
  1885. auto next = list->next;
  1886. list->next = rev_list;
  1887. rev_list = list;
  1888. list = next;
  1889. }
  1890. return rev_list;
  1891. };
  1892. pending_list = reverse_list(pending_list);
  1893. do {
  1894. // Call the appropriate callback handler
  1895. if (pending_list->have_data) {
  1896. pending_list->callback_with_data.handler(pending_list->callback_with_data.data);
  1897. if (pending_list->callback_with_data.free)
  1898. pending_list->callback_with_data.free(pending_list->callback_with_data.data);
  1899. } else {
  1900. pending_list->callback.handler();
  1901. }
  1902. // Return the entry back to the pool, or free it
  1903. auto* next = pending_list->next;
  1904. if (pending_list->was_allocated)
  1905. delete pending_list;
  1906. else
  1907. deferred_call_return_to_pool(pending_list);
  1908. pending_list = next;
  1909. } while (pending_list);
  1910. }
  1911. void Processor::deferred_call_queue_entry(DeferredCallEntry* entry)
  1912. {
  1913. ASSERT(m_in_critical);
  1914. entry->next = m_pending_deferred_calls;
  1915. m_pending_deferred_calls = entry;
  1916. }
  1917. void Processor::deferred_call_queue(void (*callback)())
  1918. {
  1919. // NOTE: If we are called outside of a critical section and outside
  1920. // of an irq handler, the function will be executed before we return!
  1921. ScopedCritical critical;
  1922. auto& cur_proc = Processor::current();
  1923. auto* entry = cur_proc.deferred_call_get_free();
  1924. entry->have_data = false;
  1925. entry->callback.handler = callback;
  1926. cur_proc.deferred_call_queue_entry(entry);
  1927. }
  1928. void Processor::deferred_call_queue(void (*callback)(void*), void* data, void (*free_data)(void*))
  1929. {
  1930. // NOTE: If we are called outside of a critical section and outside
  1931. // of an irq handler, the function will be executed before we return!
  1932. ScopedCritical critical;
  1933. auto& cur_proc = Processor::current();
  1934. auto* entry = cur_proc.deferred_call_get_free();
  1935. entry->have_data = true;
  1936. entry->callback_with_data.handler = callback;
  1937. entry->callback_with_data.data = data;
  1938. entry->callback_with_data.free = free_data;
  1939. cur_proc.deferred_call_queue_entry(entry);
  1940. }
  1941. void Processor::gdt_init()
  1942. {
  1943. m_gdt_length = 0;
  1944. m_gdtr.address = nullptr;
  1945. m_gdtr.limit = 0;
  1946. write_raw_gdt_entry(0x0000, 0x00000000, 0x00000000);
  1947. write_raw_gdt_entry(GDT_SELECTOR_CODE0, 0x0000ffff, 0x00cf9a00); // code0
  1948. write_raw_gdt_entry(GDT_SELECTOR_DATA0, 0x0000ffff, 0x00cf9200); // data0
  1949. write_raw_gdt_entry(GDT_SELECTOR_CODE3, 0x0000ffff, 0x00cffa00); // code3
  1950. write_raw_gdt_entry(GDT_SELECTOR_DATA3, 0x0000ffff, 0x00cff200); // data3
  1951. Descriptor tls_descriptor;
  1952. tls_descriptor.low = tls_descriptor.high = 0;
  1953. tls_descriptor.dpl = 3;
  1954. tls_descriptor.segment_present = 1;
  1955. tls_descriptor.granularity = 0;
  1956. tls_descriptor.zero = 0;
  1957. tls_descriptor.operation_size = 1;
  1958. tls_descriptor.descriptor_type = 1;
  1959. tls_descriptor.type = 2;
  1960. write_gdt_entry(GDT_SELECTOR_TLS, tls_descriptor); // tls3
  1961. Descriptor fs_descriptor;
  1962. fs_descriptor.set_base(this);
  1963. fs_descriptor.set_limit(sizeof(Processor));
  1964. fs_descriptor.dpl = 0;
  1965. fs_descriptor.segment_present = 1;
  1966. fs_descriptor.granularity = 0;
  1967. fs_descriptor.zero = 0;
  1968. fs_descriptor.operation_size = 1;
  1969. fs_descriptor.descriptor_type = 1;
  1970. fs_descriptor.type = 2;
  1971. write_gdt_entry(GDT_SELECTOR_PROC, fs_descriptor); // fs0
  1972. Descriptor tss_descriptor;
  1973. tss_descriptor.set_base(&m_tss);
  1974. tss_descriptor.set_limit(sizeof(TSS32));
  1975. tss_descriptor.dpl = 0;
  1976. tss_descriptor.segment_present = 1;
  1977. tss_descriptor.granularity = 0;
  1978. tss_descriptor.zero = 0;
  1979. tss_descriptor.operation_size = 1;
  1980. tss_descriptor.descriptor_type = 0;
  1981. tss_descriptor.type = 9;
  1982. write_gdt_entry(GDT_SELECTOR_TSS, tss_descriptor); // tss
  1983. flush_gdt();
  1984. load_task_register(GDT_SELECTOR_TSS);
  1985. asm volatile(
  1986. "mov %%ax, %%ds\n"
  1987. "mov %%ax, %%es\n"
  1988. "mov %%ax, %%gs\n"
  1989. "mov %%ax, %%ss\n" ::"a"(GDT_SELECTOR_DATA0)
  1990. : "memory");
  1991. set_fs(GDT_SELECTOR_PROC);
  1992. // Make sure CS points to the kernel code descriptor.
  1993. asm volatile(
  1994. "ljmpl $" __STRINGIFY(GDT_SELECTOR_CODE0) ", $sanity\n"
  1995. "sanity:\n");
  1996. }
  1997. void Processor::set_thread_specific(u8* data, size_t len)
  1998. {
  1999. auto& descriptor = get_gdt_entry(GDT_SELECTOR_TLS);
  2000. descriptor.set_base(data);
  2001. descriptor.set_limit(len);
  2002. }
  2003. }
  2004. #ifdef DEBUG
  2005. void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func)
  2006. {
  2007. asm volatile("cli");
  2008. klog() << "ASSERTION FAILED: " << msg << "\n"
  2009. << file << ":" << line << " in " << func;
  2010. // Switch back to the current process's page tables if there are any.
  2011. // Otherwise stack walking will be a disaster.
  2012. auto process = Process::current();
  2013. if (process)
  2014. MM.enter_process_paging_scope(*process);
  2015. Kernel::dump_backtrace();
  2016. Processor::halt();
  2017. }
  2018. #endif
  2019. NonMaskableInterruptDisabler::NonMaskableInterruptDisabler()
  2020. {
  2021. IO::out8(0x70, IO::in8(0x70) | 0x80);
  2022. }
  2023. NonMaskableInterruptDisabler::~NonMaskableInterruptDisabler()
  2024. {
  2025. IO::out8(0x70, IO::in8(0x70) & 0x7F);
  2026. }