Build Your Own OS #6

User modes

Loading an External Program

GRUB modules

module /modules/program
mkdir -p iso/modules

Creating a program

; test module à copier dans iso/modules                           
; set eax to some distinguishable number, to read from the log afterwards
bits 32
mov eax, 0xDEADBEEF ; enter infinite loop, nothing more to do
jmp start

Compiling the program

nasm -f bin program.s -o program

Instruct the GRUB

; 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

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 */




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

funcy — a Serverless Slack App using Fn

Contrasting AWS Instances at a Glance and fixing Up the right one for you !

Useful Tips for Product Teams working on Samsung Smart TV apps

Quick Sort made easy

Tip: Connecting to Azure Cosmos DB MongoDB API using the MongoDB Go driver

Container-native Load Balancing and How it helped us

Embedding files natively in Go 1.16

Programs, Processes, and Threads — What Are They, and How Do Computers Run Them?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Niroshan Pushparaj

Niroshan Pushparaj

More from Medium

Advanced GAM — Google Cloud Shell

CS373 Spring 2022: Griffin Urban

How to import mySQL database from cPanel to Amazon RDS

CS373 Spring 2022: Swapnil Shaurya