CPU Components and Interruptions

Greg
The Startup
Published in
6 min readFeb 2, 2021
https://images.frandroid.com/wp-content/uploads/2020/12/processeur-cpu.jpg

Plan

  • What is a CPU ?
  • CPU main components
  • CPU interrupts
  • AMD, Intel, ARM

What is a CPU ?

The Central Processing Unit (CPU) is the brain / heart of a computer as it executes instructions.
Instructions are coded in a language called “assembly” which is the only one spoken by the CPU.
Each CPU manufacturer has its own version of assembly language.
That’s the reason why one needs to select a specific CPU architecture in order to download the Debian operating system (Figure 1).

Figure 1 — Debian OS images depending on the CPU family

Assembly languages used by the different CPU manufacturers still share a lot of instructions in common. If you select the wrong CPU architecture for an OS it could have different consequences:

  • OS not working at all.
  • Some OS functionalities unavailable.
  • Performance issues as the OS won’t use specific instructions for this architecture.

CPU main components

Figure 2 — CPU main components

The Control Unit (CU)

The control unit is the “brain” of the CPU as its purpose is to coordinate the different components to execute an instruction.

CU operations can be divided into 3 steps:

1) Fetch
CU begins by fetching the next instruction to execute from the memory unit thanks to the Program Counter (PC) register.

2) Decode
The CU ‘reads’ the instruction and eventually copies values to be computed from RAM to dedicated registers (which are fast access memory spaces dedicated to the CPU).

3) Execute
The CU finally sends signals to relevant internal components (such as ALU) to execute the instruction.

The cycle then repeats over and over based on the CPU clock rate.

The Clock

If the Control Unit is the brain of a CPU, the clock can be compared to the heart.
Indeed a clock launches a new control unit cycle each time it activates.
The speed of the clock is expressed in GHz.
With a 3 GHz clock, 3 billions CPU instructions (thus, cycles) are executed per second !

The Memory unit (registers + cache memory)

Registers

“A processor register is a quickly accessible location available to a computer’s processor”. https://en.wikipedia.org/wiki/Processor_register

Some registers are user accessible:
→ data registers (values such as integers, characters)
→ address registers (stack pointer, …)

Some registers are internal (not accessible by a user process) such as:
→ instruction register, holding the instruction being currently executed
→ registers related to information fetching from RAM (MBR, MAR)

To get a better understanding of how registers can be used, don’t hesitate to learn a bit of assembly (such as x86 assembly language).

Cache memory

A CPU cache is a hardware cache used by the central processing unit (CPU) of a computer to reduce the average cost (time or energy) to access data from the main memory. https://en.wikipedia.org/wiki/CPU_cache

Different level of cache memory exist depending on the CPU:
→ Level 1 cache: primary cache, fast but small
→ Level 2 cache: bigger than L1 but a bit slower
→ Level 3 cache: aims at improving L1 and L2 performances. Way slower than L1 and L2

Arithmetic/Logic unit (ALU)

The ALU is the CPU component that performs calculations over operands which are stored in registers.
The ALU receives 2 operands and a code (called ‘opcode’) indicating the operation to be made.
It then outputs the result of the operation into a special register (often called “accumulator”).

Figure 3 — ALU inputs and outputs

ALU can manipulate registers for an operation such as the status register, also called flag register. The purpose of this register is to indicate the state of the processor after an operation has been made (overflow, parity, carry, sign, zero, … flag).

II. CPU interrupts

Interrupt is a signal emitted by hardware or software when a process or an event needs immediate attention. https://www.geeksforgeeks.org/interrupts/

There are 3 types of interrupts: hardware, software, exceptions.

Hardware interrupts

This type of interrupt is used by a keyboard, mouse, …

Figure 4 — Hardware interrupt (minimalist and simplified)

In Figure 4 (above):

  • IRQ means “Interruption Request”
  • IDT means “Interrupt Descriptor Table”
  • ISR means “Interrupt Service Routine”

Software interrupts

Software interrupts can be invoked by a user application or the kernel.
It is done thanks to the following assembly command:
“INT [N]” that calls the Interruption [N] in hexadecimal.

Most unix systems only use the interruption number 128 (0x80 in hexadecimal).
The interrupt 128 code is a kind of giant switch containing all the possible system calls.
Thanks to CPU registers (such as EAX) the interruption code can determine what is the system call number and its eventual parameters.

Example of what does the kernel do when a system call is invoked:

→ Put the system call number in the EAX register.
→ Store the arguments to the system call in the registers EBX, ECX, etc.
→ Call the relevant interrupt (80h).
→ The result is usually returned in the EAX register.
https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm

Figure 5 — Software interrupt

As you can see, invoking a system call is translated as multiple assembly instructions to set up the environment in the CPU. Thus the assembly interruption section code may be “atomic”.
The following website lists all the syscall numbers depending on the CPU architecture: https://fedora.juszkiewicz.com.pl/syscalls.htm

Exception interrupts

The CPU can invoke an interruption after an instruction which could be incorrect or fatal.
These kinds of special interrupts are called exceptions / traps.
Examples: division by 0, invalid access memory, …

Interrupt masking

Most interrupt numbers (software or hardware) can be ‘masked’ which simply mean disabled or not considered by the CPU. This can be done thanks to some registers.

Differences between interrupts and signals

I also take the opportunity of studying interrupts to remind that Linux signals and interrupts are not the same !

→ Interrupts can be viewed as a mean of communication between the CPU and the OS kernel.
→ Signals can be viewed as a mean of communication between the OS kernel and OS processes.
Signals are user space (shell, programm) initiated toward a process (thanks to its id) whereas Interrupts are only kernel.
https://stackoverflow.com/questions/13341870/signals-and-interrupts-a-comparison

AMD, Intel, ARM

Intel and AMD processors are generally X86 architecture whereas ARM processors are RISC processors based on ARM architecture. ARM processors offer lower performance than AMD and Intel processors but they also consume lower power. If you need a processor for a desktop, you have to go with INTEL or AMD. ARM processors are generally used in mobile phones, smart watches and other low power devices. https://www.quora.com/What-is-the-difference-between-Intel-AMD-and-ARM-processor-what-could-be-a-better-option-to-take

Conclusion

I hope you enjoyed this post, if you detect any error, don’t hesitate to let me know, it would be greatly appreciated.

You can also follow me on twitter and check out the references below if you want to dive deeper into the subject.

Images

Figure 1
https://www.debian.org/CD/http-ftp/
Figure 3
https://www.sawakinome.com/img/images/difference-between-cpu-and-alu.gif

References

https://en.wikipedia.org/wiki/Central_processing_unit
https://en.wikipedia.org/wiki/Control_unit
https://www.geeksforgeeks.org/introduction-of-control-unit-and-its-design/
https://www.tutorialspoint.com/control-unit-and-cpu
https://en.wikipedia.org/wiki/Instruction_cycle
https://en.wikipedia.org/wiki/Processor_register
https://en.wikipedia.org/wiki/CPU_cache
https://en.wikipedia.org/wiki/Arithmetic_logic_unit
https://en.wikipedia.org/wiki/Status_register
https://alex.dzyoba.com/blog/os-interrupts/
https://www.geeksforgeeks.org/interrupts/
https://en.wikibooks.org/wiki/X86_Assembly/X86_Interrupts
https://en.wikipedia.org/wiki/Interrupt
https://wiki.osdev.org/Interrupt_Service_Routines
https://stackoverflow.com/questions/13341870/signals-and-interrupts-a-comparison
https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm
https://linux-kernel-labs.github.io/refs/heads/master/lectures/interrupts.html
https://fedora.juszkiewicz.com.pl/syscalls.html
https://pdos.csail.mit.edu/6.828/2011/lec/l-interrupt.html
https://en.wikipedia.org/wiki/INT_(x86_instruction)

--

--