r/computerscience • • Mar 11 '25
Discussion How does CPU knows how to notify OS when a SysCall happen?
Supposing P1 has an instruction that makes a Syscall to read from storage, for example. In reality, the OS manage this resource, but my doubt is, the program is already in memory and read to be executed by the CPU which will take that operation and send it to the storage controller to perform it, in this case, an i/o operation. Suppose the OS wants to deny the program from accessing the resource it wants, how the OS sits in between the program and CPU to block it if the program is already in CPU and ready to be executed?

I don't know if I was clear in my questioning, please let me know and I will try to explain it better.
Also,if you did understand it, please be as deep as you can in the subject while answering, I will be very grateful.
51
u/ThunderChaser Mar 11 '25
Typically via an interrupt. On Linux for example a system call is performed via interrupt
0x80with a register set to the syscall number and the other registers set to the syscall’s arguments. Once that occurs the CPU jumps to the interrupt handler (which is kernel code) and the kernel services the syscall before handing control back over to the process.Typically the kernel is always loaded in the virtual address space so the CPU can always jump into kernel code, a common design is known as a “higher half kernel” where the lower half of the virtual address space is useable by the process and the upper half is reserved solely for the kernel and can’t be accessed by a userspace process.
x86_64 nowadays has a special
syscallinduction which is used over the older interrupt approach, but the general concept is the same.5
u/kleiner_schussel Mar 11 '25
There is a special register in the cpu that holds the memory location of the kernel code that handles system interrupts. This register will be filled once the kernel is loaded into memory on boot. There are also other registers that are filled before the interrupt to store the requested interrupt type and context. A system interrupt is just a basic cpu instruction that will make the execution flow jump to the kernel code that handles the interrupt.
2
u/MasterGeekMX Bachelors in CS Mar 11 '25
Here, this video by the godsent YT channel "Core Dumped" explains that really well: https://youtu.be/H4SDPLiUnv4
2
u/tcpukl Mar 11 '25
Normally interrupts.
2
u/N0Zzel Mar 11 '25 edited Mar 11 '25
Via kernel traps. When a process in user mode attempts to perform a privileged instruction (for example, requesting a page that is not physically present in ram) the memory controller will emit a trap which the hardware will suspend the execution context of the user process to the data structure used by the kernel for tracking processes. The kernel then decides what to do with the process (update the memory mapping or terminate the process)
2
u/[deleted] Mar 11 '25
So ur program is loaded into memory and its called to be executed. But you have another program that acts as a manager for your system. Say the sys call takes to long or exceeds some other condition and the manager decides time to free up space and continue executing other operations. The manager program sends an interrupt call to cancel the syscall or perhaps pauses its execution or takes some other action for resolution. But ur computer is never stuck in one execution. Different executions can happen in different threads and these threads can be canceled, paused, etc.
0
u/istarian Mar 11 '25
The modern CPU is far more complex than your over-simplified diagram...
1
u/trailing_zero_count Mar 13 '25
Userspace can't typically access the storage controller directly. It has to make a syscall to do this, which requires a transition to kernel-mode. If the user doesn't have access to do that specific operation, the OS would return the appropriate error from the syscall.
Typically this initiates an async process with the storage controller loading data directly into a memory buffer (DMA) and then notifying the OS by an interrupt. Then the OS will wake up or notify the userspace application that the data is ready.
Here is an article that is written to counter a different assumption, but nonetheless includes a decent overview of the pieces involved in such a process. https://blog.stephencleary.com/2013/11/there-is-no-thread.html