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
    USER_MODE_CODE_SEGMENT_SELECTOR equ 0x18
USER_MODE_DATA_SEGMENT_SELECTOR equ 0x20
mov cs, USER_MODE_CODE_SEGMENT_SELECTOR | 0x3
mov ss, USER_MODE_DATA_SEGMENT_SELECTOR | 0x3

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):
{
*(.data)
}
.rodata ALIGN(4):
{
*(.rodata*)
}
}
-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

Kurplazo customer care number/9883996727

The Chinese storm in Stepn!

Oracle MySQL Heatwave — You need know now!

The Evolution of mistake making

Some Thoughts on Microservices Architecture

Building Your Own Claims API

Streaming analytics with Kafka and ksqlDB

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

Go Module Decoupling: A Rescue Guide

🏋️ The Might of Microtypes

The Accidental Open Source Project

Httplog github page