How To Build An Operating System: User Mode - Part 09

Index       Offset Name             Address range          type DPL
3 0x18user code segment 0x00000000 - 0xFFFFFFFFRX PL3
4 0x20user data segment 0x00000000 - 0xFFFFFFFFRW PL3

Setting Up For User Mode

  • Page frames for code, data, and stack. It suffices to allocate a one-page frame for the stack and enough page frames to fit the program’s code. Get basic implementation work first.
  • The binary from the GRUB module should be copied to the page frames used for the program's code.
  • Above mentioned page frames need to be mapped using a page dictionary and page tables. For that, we need at least two-page tables since code and data must be 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 for user mode
[esp + 12] esp ; the user mode stack pointer
[esp + 8] eflags ; the control flags want to use in user mode
[esp + 4] cs ; the code segment selector
[esp + 0] eip ; instruction pointer of user mode 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 */SECTIONS
= 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




Software Engineering Undergraduate at University of Kelaniya

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

Recommended from Medium

Can NGK overtake Filecoin?

How dos the Decision Tree algorithm work?

Spring Security using JWT token based authentication

Proactive monitoring with Monit + Sengrid + Slack

What I actually use from RxJava on Android

3 Best coding projects for job | Project ideas for beginners

5 key items from Day 3 of AWS re:Invent 2020

What Is Data Management Platform, How It Works and Why You Really Need It in Your Business

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
Pubudu Wickramathunge

Pubudu Wickramathunge

Software Engineering Undergraduate at University of Kelaniya

More from Medium

Migrate SVN to GIT with history

Speed Up Your CI With Docker (Travis-CI)

DesktopInfo — Conky for Windows

Save Friday Night Using Chaos Engineering