Pasan lahiru dissanayake

Aug 28, 2021

4 min read

Build your own operating system-part06

integrate user modes

Loading an External Program

GRUB Modules

module /modules/program
mkdir -p iso/modules
; in file `loader.s`

MAGIC_NUMBER equ 0x1BADB002 ; define the magic number constant
ALIGN_MODULES equ 0x00000001 ; tell GRUB to align modules

; calculate the checksum (all options + checksum should equal 0)

section .text: ; start of the text (code) section
align 4 ; the code must be 4 byte aligned
dd MAGIC_NUMBER ; write the magic number
dd ALIGN_MODULES ; write the align modules instruction
dd CHECKSUM ; write the checksum

Executing a Program

A Very Simple Program

; set eax to some distinguishable number, to read from the log afterwards
mov eax, 0xDEADBEEF

; enter infinite loop, nothing more to do
; $ means "beginning of line", ie. the same instruction
jmp $


nasm -f bin program.s -o program

Finding the Program in Memory

int kmain(/* additional arguments */ unsigned int ebx)
multiboot_info_t *mbinfo = (multiboot_info_t *) ebx;
unsigned int address_of_module = mbinfo->mods_addr;

Jumping to the Code

typedef void (*call_module_t)(void);
/* ... */
call_module_t start_program = (call_module_t) address_of_module;
/* we'll never get here, unless the module code returns */

The Beginning of User Mode: