hackclub / putting-the-you-in-cpu

A technical explainer by @kognise of how your computer runs programs, from start to finish.
https://cpu.land
MIT License
4.94k stars 158 forks source link

Questions about Chapter 1 #24

Closed FlameWolf closed 1 year ago

FlameWolf commented 1 year ago

Programs can’t directly switch privilege levels; hardware interrupts are safe because the processor has been preconfigured by the OS with where in the OS code to jump to.

This is the first time a hardware interrupt is mentioned. Does it mean other kind of interrupts exist too? Are there any differences in how programs use them?

When this kernel code finishes, it tells the CPU to switch back to user mode and return the instruction pointer to where it was when the interrupt was triggered. This is accomplished using an instruction like IRET.

From this, my understanding is that IRET is used by kernel code to transfer control back to user space. But that seems to contradict with this paragraph:

Programs can delegate control to the OS with special machine code instructions like INT and IRET.

Can user code call IRET? But it's already in the user space, and it can't access kernel space, so how does that work?

kognise commented 1 year ago

Great questions!

Programs can’t directly switch privilege levels; hardware interrupts are safe because the processor has been preconfigured by the OS with where in the OS code to jump to.

This is the first time a hardware interrupt is mentioned. Does it mean other kind of interrupts exist too? Are there any differences in how programs use them?

This was actually a mistake and should say "software interrupts are safe!" Actually fixed yesterday in #20. Great catch :)

When this kernel code finishes, it tells the CPU to switch back to user mode and return the instruction pointer to where it was when the interrupt was triggered. This is accomplished using an instruction like IRET.

From this, my understanding is that IRET is used by kernel code to transfer control back to user space. But that seems to contradict with this paragraph:

Reading the paragraph you quoted, I'm not sure I actually see a contradiction — the kernel code is using the IRET instruction "to switch back to user mode and return the instruction pointer to where it was when the interrupt was triggered". I could definitely make the paragraph a little clearer, though.

Programs can delegate control to the OS with special machine code instructions like INT and IRET.

Can user code call IRET? But it's already in the user space, and it can't access kernel space, so how does that work?

Oh yeah, I guess this is sorta confusingly written. What I meant to get across is that INT and IRET are both instructions for delegating control to the OS; programs use INT but IRET is just as important from the kernel side. Together, the two instructions allow the control delegation I was talking about.

Let me know if you have any other questions!

FlameWolf commented 1 year ago

Thank you!