Build Your Own OS #09

User Mode

I think you already have some knowledge about user mode. We explained that in the 6th part of the series. Please read this again.

Segmentation for user mode

The very first thing that we need to do is add two more segments to the GDT. They are very similar to what we have done in segmentation stage when we were setting up the GDT for the first time.

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

Setting Up For User Mode

There are few things that user mode process required.

  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

The only way to execute code with a lower privilege level than the current privilege level (CPL) is to execute an iret or lret instruction - interrupt return or long return, respectively.

[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

When C is used as the programming language for user mode programs, it is important to think about the structure of the file that will be the result of the compilation.

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



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