Build Your Own OS #09

User Mode

Segmentation for user mode

You need to add these two segments to enable the user mode

Setting Up For User Mode

  1. Page frames from the RAM for store instructions, data and stack. In here since we are focusing on basic implementation it is fine one page frame for the stack and enough page frames for the instructions or program codes.
  2. The binary from the GRUB module need to copied to the above allocated page frames for the program codes.
  3. Above mentioned page frames need to be mapped using page dictionary and page tables. For that we need at least two page tables since code and data must mapped in at 0x00000000 and increasing from there, and the stack should start from below the kernel, at 0xBFFFFFFB, growing towards lower addresses. (The U/S flag needs set to allow PL3 access.)

Entering User Mode

[esp + 16]  ss     ;the stack segment selector we want for user mode
[esp + 12] esp ;the user mode stack pointer
[esp + 8] eflags ;the control flags we want to use in user mode
[esp + 4] cs ;the code segment selector
[esp + 0] eip ;the instruction pointer of user mode code to execute

Using C for User Mode Programs

extern main    section .text
; push argv
; push argc
call main
; main has returned, eax is return value
jmp $ ; loop forever
OUTPUT_FORMAT("binary")    /* output flat binary */

. = 0; /* relocate to address 0 */

.text ALIGN(4):
start.o(.text) /* include the .text section of start.o */
*(.text) /* include all other .text sections */

.data ALIGN(4):

.rodata ALIGN(4):
-m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs
-T link.ld -melf_i386  # emulate 32 bits ELF, the binary output is specified              # in the linker script




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

Recommended from Medium

Diagnosing .NET Core app halt

Creating simple App Shortcuts for your Android App

Installing and Working with Python for Beginners

Breakout ball game using Java

5 Interesting tool/feature that you want to implement in Python (:

DevOps Adoption and Skills Insights with DevOps Institute Global Ambassador, Najib Radzuan

Docker, Kubernetes and the Butterfly Protocol

Codename Valhalla: the challenges of CI/CD + tangents upon tangents

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

Where You Can Buy Replica Sneakers Safely

Week 3 optimizing my space

Polkabridge is a decentralized application platform.

Web3 Newsletter — Edition #8