Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an OS communicate with the CPU? [closed]

How does OS communicate with CPU?

There are drivers in the OS, ok I understand that part. OS uses drivers--> communicate--> device controller.

How does the communication happen? Does the OS touch the CPU directly with its commands, or is it using BIOS as the interface?

Let's assume that I will make my own OS. Its only task is to send arithmetic operations to the CPU and print results to the screen. I will tell the CPU to put memory words to registers, count them, and then put them back to memory. How can I do that?

like image 795
user3521129 Avatar asked May 25 '14 16:05

user3521129


1 Answers

The CPU just runs instructions from memory starting at some offset and then keeps fetching the next instruction and repeating. The bootloader sets up the CPU to start running the OS entry point when your computer starts. Actions like keyboard or mouse input cause interrupts which the interrupt controller use to lookup special code setup by the OS to handle these interrupts. These interrupts are also used to allow the OS to switch which thread is currently running on the CPU using special privileged instructions that can only be run in kernel mode. The interrupt causes the CPU to switch to kernel mode before calling into the OS interrupt handler code so that the OS can use the necessary privileged instructions for controlling various behavior user mode code is not allowed to.

There are a lot of details concerning what registers are used for what purposes and much much more but it would take a whole book to cover everything.

Here is a free book that covers many details at a relatively introductory level.

like image 194
mclaassen Avatar answered Sep 28 '22 16:09

mclaassen