---
# System prepended metadata

title: Kernel Development
tags: [KERNEL_DEV]

---

---
tags: KERNEL_DEV
---
Kernel Development
===
Overview
---
Source Code: [starOS](https://github.com/zoanana990/starOS.git)
## Memory
What is memoery?
- Memory is a piece of hardware that allows computers to store information
- RAM(Random Access Memory) is your computers main memory, here programs can read and write information
	- Temporary storage
	- Data is lost when you power down your computer 
- ROM(Read Only Memory) is a form of memory that can only be read from
	- Permanent storage
	- Used for storing permanent programs that will not change, for example: embedded device and BIOS

記憶體以線性的方式儲存，當我們需要進行記憶體存取時，不需要過多的擔心

## Boot
Steps to a booted system
- The BIOS is executed directly from ROM
- The BIOS loads the bootloader into address 0x7C00
- The bootloader loads the kernel

What is a bootloader?
- A bootloader is a small program responsible for loading the kernel of an operating system kernel
- They are small
- For example: Linux and GRUB

The BIOS is executed directly from ROM
- The CPU executes instructions from the BIOS's ROM
- The BIOS gernally loads itself into RAM then continues execution from RAM
- The BIOS will initialize essential hardware
- The BIOS looks for a bootloader to boot by searching all storage mediums for boot signature "0x55AA"  

> 如果先找到 0x55AA 則會啟動 bootloader，將 bootloader 放入 0x7C00 當中後執行

The BIOS is a kernel
- The BIOS contains routines to assist our bootloader in booting our kernel
- The BIOS is 16 bit code which means only 16 bit code can execute it properly
- The BIOS routines are gerneric and a standard

## Hello World
Using x86_64 print the "Hello World"

First, Check the interrupt in the [Ralf Brown's Interrupt List Indexed HTML Version - Release 61](https://www.ctyme.com/rbrown.htm)

See [VIDEO - TELETYPE OUTPUT](http://www.ctyme.com/intr/rb-0106.htm), we find that the registers need to be setting as following:

```asm
AH = 0Eh
AL = character to write
BH = page number
BL = foreground color (graphics modes only)
```

Assemble:
```shell
$nasm -f bin ./boot.asm -o ./boot.bin
```
Using qemu to simulate:
```shell
$
```

## Read Mode Development
### Real Mode
- 1 MB of RAM accessible
	- Memory access is done through the segmentation memory model, that is segments and offset
	- Only 1 MB of RAM is accessible, no matter if you have 4 GB RAM
- Based on the original x86 design
	- When in real mode, we have same limitations as the older processors from many years ago.
- No security, modern operating system can set up specific security rule, prevent user from malicious programs
	- Real mode provides no memory security
	- Real mode provides no hardware security
	- Simple user programs can destrioy our operating system with no way for us to stop them
	- Kernel is act the layer between hardware and software 
- 16 bit's Accessible at one time
	- In real mode, we can only access the 8 bit and 16 bit registers
	- We can only request memory address offsets up to 65536, that is 65 kb. We can use segments and offset to access 1 MB
	- All operations can only be done with 16 bit numbers.
- Read mode is compatability mode

### Segmentation Memory Model
- Memory is accessed by a segment and an offset
- Programs can be loaded in different areas of memory but without problems
- Multiple segments are available through the use of segment registers

8086 Segment Registers
- CS: Code Segment
- SS: Stack Segment
- DS: Data Segment
- ES: Extra Segment

Caluating Absolute Offset
- Take segment register and multiply it and add the offset
- Example:
	- Code segment: 0x7c0
	- Assembly origin "org" is zero
	- Our first instruction is at origin zero so our offset is zero
	- (0x7c0) * 16 = 0x7c00
	- 0x7c00 + 0 = 0x7c00

Different Instructions use different segment registers
- "lodsb" uses the DS:SI register combination
	```c
	Org 0
	mov ax, 0x7c0
	mov ds, ax
	mov si, 0x1f
	lodsb
	```
	- lodsb will know that the byte it need to read is in 0x7c0*16+0x1f = 0x7c1f

Programs can be loaded in different areas of memory but run without problems
- Imagine we have two programs loaded into memory, both were assembled with the origin being zero
- Program 1 uses segment 0x7c0 for all its segment registers
- Program 1 is loaded at address 0x7c00
- Program 2 uses segment 0x7d0 for all its segment registers
- Program 2 is loaded at address 0x7d00
- We swap our segment registers when switching to the other process
- We store all the registers of the process we are switching too

Stack Segment
- SS(Stack Segment) = 0x00
- SP(Stack Pointer) = 0x7c00
	- Push 0xffff
	- decrement stack pointer by 2
	- stack pointer = 0x7bfe
	- set 0x7bfe - 0x7bff to 0xffff

### Interrupt

### File System
- File does not exist
- Data is read and written in sectors
- CHS()
- 