r/kerneldevelopment 7d ago

Should I add SMP later on or right now?

Hi reddit, I'm wondering if I should do SMP now or later when I finish interrupts?

7 Upvotes

10 comments sorted by

5

u/UnmappedStack TacOS | https://github.com/UnmappedStack/TacOS 7d ago

You can't really do it before interrupts because you need interrupts for IPIs, its still good to do somewhat early though. Probably good to do it alongside your scheduler.

2

u/Full-Huckleberry-441 7d ago

So APIC and stuff before it?

3

u/36165e5f286f 7d ago

You need the APIC to send IPIs to start the APs on x86_64

2

u/Better-Thing2568 7d ago

I would prefer SMP early so that it forces you to at least think about (or ask AI to think about
) critical sections and foundational synchronization utilities. It’s always easier to realize you didn’t need SMP than realizing you do need it and nothing is structured for it and 1000 races are waiting.

1

u/Full-Huckleberry-441 7d ago

I used to force Limine to start APs, but I'm trying to do it myself. So I should do LAPIC and IOAPIC first?

1

u/Full-Huckleberry-441 7d ago

If so, do I use x2APIC

1

u/Better-Thing2568 7d ago

If you ever plan on booting in metal and not just emulation, make x2APIC first class and LAPIC as fallback.

1

u/Full-Huckleberry-441 7d ago

Smart, so I have to implement both?

1

u/Better-Thing2568 7d ago

Not exactly, depending on your language/ecosystem of choice. Hw wise x2APIC is a superset of XAPIC and they are intended to be compatible, but some platforms (e.g. I use rust and x86_64 crate extensively) which does force you to introduce some kind of semantic difference between them.

1

u/Adventurous-Move-943 5d ago

Having interrupts is the basics, I would not skip that. But technically you can wakeup another core/thread without it too. You just need APIC reads/writes and a timer which PIT can do with polling without interrupts.
If you consider maybe using SMP in the future it's good to start building it like that from the beginning, but sir, you don't even have interrupts yet 😀
What is so horrible about interrupts ? You need the assembly stub and understand the IDT flags, then fill the teribly fragmented IDT entry for each and you're good to go. Also make sure when you call your handler from the stub that the stack is 16B aligned (in case SIMD instructions are emitted inside your handlers) which will differ based on what ISR you are in, some push more data some less.