Skip to content

Instantly share code, notes, and snippets.

@thakurvivek
Last active October 28, 2016 06:50
Show Gist options
  • Select an option

  • Save thakurvivek/5b8fb8ba012e91f1e5ef4226068675ea to your computer and use it in GitHub Desktop.

Select an option

Save thakurvivek/5b8fb8ba012e91f1e5ef4226068675ea to your computer and use it in GitHub Desktop.
Notes from http://arjunsreedharan.org/post/82710718100/kernel-101-lets-write-a-kernel
Overview:
EIP(Instruction Pointer) with value 0xFFFF FFF0, also known as Reset Vector (RV)
RV's Value points to a memory location in BIOS
Shadowing begins: Copy BIOS to RAM for fsater access
Execute copied BIOS code
Search for Bootable devices (flag: 0xAA55 in byte 512 and 511 of the first sector)
Copy Bootable device's first sector starting from 0x7C00 to RAM, -> Bootloader
Execute Bootloader code
Load kernel from 0x100000 (start address for all x86 based kernels) [Kernel entry point]
Build commands:
nasm -f elf32 kernel.asm -o kasm.o
gcc -m32 -c kernel.c -o kc.o //Dont link using -c flag
ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o
;;kernel.asm
bits 32 ;nasm directive - 32 bit
section .text
global start
extern kmain ;kmain is defined in the c file
start:
cli ;block interrupts
mov esp, stack_space ;set stack pointer
call kmain
hlt ;halt the CPU
section .bss
resb 8192 ;8KB for stack
stack_space:
/*
* kernel.c
*/
void kmain(void)
{
const char *str = "my first kernel";
char *vidptr = (char*)0xb8000; //video mem begins here.
unsigned int i = 0;
unsigned int j = 0;
/* this loops clears the screen
* there are 25 lines each of 80 columns; each element takes 2 bytes */
while(j < 80 * 25 * 2) {
/* blank character */
vidptr[j] = ' ';
/* attribute-byte - light grey on black screen */
vidptr[j+1] = 0x07;
j = j + 2;
}
j = 0;
/* this loop writes the string to video memory */
while(str[j] != '\0') {
/* the character's ascii */
vidptr[i] = str[j];
/* attribute-byte: give character black bg and light grey fg */
vidptr[i+1] = 0x07;
++j;
i = i + 2;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment