Brief Explanation of Kernel(x86 architecture)
Memory Types
When it comes to a system, we all know that there are 2 main memories
RAM -> Random Access Memory
. Volatile memory, which means we will lose on power reset
. fast access for memory fetching & writing
. Usually instructions are fetched from secondary memory like Flash,system emory, hard disk and executed in RAM
ROM -> Read only Memory
. Non-volatile, usually code will be stored here, since we need it even after power reset
. we can't edit/write, since manufacturer made it read-only factory code(BIOS) will be kept inside
. It 10,000 times slower in terms of writing data, compared to RAM
Why RAM is much faster than ROM ?
How Much Faster Is RAM Compared To ROM?
RAM is significantly faster than ROM when it comes to data access speeds. Here are some key points regarding the speed difference between RAM and ROM:
RAM access speeds range from a few gigabytes per second for DDR3 RAM up to around 8 gigabytes per second for DDR4 RAM. This means RAM can access data in nanoseconds.
In contrast, ROM access speeds range from a few megabytes per second. This means ROM accesses data in microseconds - around 1000 times slower than RAM.
The difference in speed comes down to the fundamental design differences between RAM and ROM. RAM uses electrical charge to store data and can access any location randomly. This allows for very fast read/write speeds.
ROM uses physical memory cells that must be read sequentially. This limits the maximum access speed, especially as the memory size increases.
Because of its much faster speeds, RAM is used as the primary working memory for processors. Programs and data loaded into RAM can be accessed and manipulated quickly.
ROM is used for storing relatively static data and instructions, like the firmware needed for a device to boot up. This data does not need to be accessed as frequently as data in RAM.
In summary, RAM is hundreds of times faster than ROM in terms of data access speeds. This speed difference comes down to the fundamental design and working principles of RAM versus ROM. The faster speed of RAM makes it suitable for use as a computer's primary working memory, while the slower but permanent storage of ROM makes it ideal for storing firmware and other static data.
Hope this summary helps explain, at a high level, how much faster RAM is compared to ROM in terms of data access speeds!
Real Mode & Protected Mode
while system is booting, there are 2 stages
. Stage-1 : Real mode(compatibility mode)
. Stage-2 : Protected mode
In real mode, CPU has access to only 1MB of total 4GB RAM(this might change with different processor boards).1 MB = 1024 KB = 1024 * 1024 bits = 2^20 bits
Again, out of that 1MB available in real mode, only 16bits are accessible and remaining 4bits are dedicated for other segment registers.
First program to run is BIOS
BIOS will load itself from ROM to RAM and starts execution in RAM.
Do some basic hardware initialization.
Looks for bootloader in all storage mediums available by looking for Boot signature(0xAA55) in 511, 512 bytes in first sector.
Loads that sector in 0x7C00 in RAM and starts executing bootloader.
In Real mode, we are limited to little space, so we will first execute bootloader whose size if 512 bytes(510 bytes are actual bootloader code, last 2 bytes are dedicated for boot signature 0xAA55) for x86 arch.
This is enough, since we have 64kb(16bits) accessible at this stage.
Once bootloader is loaded and starts executing, then it will load kernel from secondary memory(Internal Flash/SDcard/eMMC) to main memory(RAM)
ok, but bootloader will do in case of bare-metal development ?
we won't have kernel in bare-metal. What exactly bootloader do there ?
There is a little confusion, isn't it ?Usually when system(Embedded board with embedded processor) boots,
manufacturer will hard wire boot pins provided by CPU and based on that configuration it will load bootloader from corresponding memory.
If kernel is not available, bootloader will be executed post main().
if kernel is available, it will load kernel and hands control to kernel.Right now, we are focussing embedded development with kernel, so lets move back to original topic.
In real mode, Bootloader will loads kernel into memory, switches processor to 32bit protected mode , allows to access entire 4GB RAM address space and finally executes the kernel.