When executing code which mode cannot directly access hardware or reference memory

Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the entire PC. In User mode, the executing code has no ability to directly access hardware or reference memory.

Kernel is a Middle man, between Software and hardware, its main job is to maintain the communication between hardware and software. It is a first user installed software on a computer just after to BIOS.

OS Kernels are Specific to the hardware they are installed, there are OS which are flexible for hardware [Hardware extraction layer coding] like NT kernel of Windows [First used in MS 386 and latest in Win 8] and Unix code ae based on Bell labs and this is not proprietary.

When a software makes a request to CPU, this would be a system call depending on a Kernel CPU would handle it differently.

Three types of Kernels,

OS run on two Phase, User Mode and Kernel Mode.

User Space have less privileges than kernel space, it cannot directly access hardware and resource of Kernel Space like its code, any process Data and date, it can only access its virtual space.

If any exception occurs in User space, then, it can only crash a single process but OS will still be running, but in case of Kernel Space issue, we might need to Reboot the machine or Reinstalling OS.

When We run any program in Win OS, Then It gives us a private virtual address space, that means we can’t access something, out of it.

Even some addresses are reserved for Kernel address space, Kernel address space has only one virtual address space.

When we run any program, it switches between User space and Kernel Space, I.e.—Opening a File while any operation is running, combination of both.

PS

We have already started the chapter about User mode. But there are a few more steps to complete the chapter. So this week we are going to complete the other part of User mode. And this will be the last episode of implementing Operating System article series.

Before starting this article it would be better if you can go through the last episode of user modes.

In any modern operating system, the CPU is actually spending time in two very distinct modes.

  1. Kernal Mode

In Kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the entire PC.

2. User Mode

In User Mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode must delegate to system APIs to access hardware or memory. Due to the protection afforded by this sort of isolation, crashes in user mode are always recoverable. Most of the code running on your computer will execute in user mode.

Our kernel, at this moment, is running with the processor in Kernel Mode.

Value of User mode

These two modes aren’t mere labels, they’re enforced by the CPU hardware. If code executing in User mode attempts to do something outside its purview like, say, accessing a privileged CPU instruction or modifying memory that it has no access to a trappable exception is thrown. Instead of your entire system crashing, only that particular application crashes. That’s the value of User mode.

CPU hardware actually provides four protection rings: 0, 1, 2, and 3. Only rings 0 [Kernel] and 3 [User] are typically used.

The outermost part is the least privileged and has the least access to the resources. This is usually all the User Applications. The next privilege level goes to the Device drivers. And lastly, the Kernel.

Now we have to add segmentations to user mode.

At the moment we have 3 entries in our GDT. We have to add two more entries to the GDT. Those are,

  1. User code segment
  2. User data segment

Now we have to update our memory segment file as follows.

Now we can switch to the user mode.

The only way to execute code with a lower privilege level than the current privilege level [CPL] is to execute an exception return instruction [IRET].

To enter user mode we set up the stack as if the processor had raised an inter-privilege level interrupt. The stack should look like the following:

[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

For now, we should have interrupts disabled, as it requires a little more work to get inter-privilege level interrupts to work properly.

Let’s write a C program for User mode

Since we can enter user mode from the kernel, it’s time to write a program to see its functioning.

One thing we can do to make it easier to develop user-mode programs is to allow the programs to be written in C.

Let’s add a source file with a C program.

This user_mode_program.s will be compiled to user_mode_program.o.

Then the following code shows an example of a linker script that places these instructions first in executable.

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*]
}
}

When we compile user programs we want the following GCC flags:

-m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles
-nodefaultlibs

For linking, the followings flags should be used:

-T link.ld -melf_i386  # emulate 32 bits ELF, the binary output is specified
# in the linker script

Finally, now we can write programs written in C or assembly language.

Resources:

//littleosbook.github.io/#entering-user-mode

Which mode is used for executing CPU instructions and memory management?

In kernel mode, the CPU has instructions to manage memory and how it can be accessed, plus the ability to access peripheral devices like disks and network cards. The CPU can also switch itself from one running program to another.

Which mode has complete and unrestricted access to the underlying hardware?

Kernel mode refers to the processor mode that enables software to have full and unrestricted access to the system and its resources. The OS kernel and kernel drivers, such as the file system driver, are loaded into protected memory space and operate in this highly privileged kernel mode.

What is the user mode and kernel mode?

A processor in a computer running Windows has two different modes: user mode and kernel mode. The processor switches between the two modes depending on what type of code is running on the processor. Applications run in user mode, and core operating system components run in kernel mode.

When CPU is in user mode the program does not have direct access to memory and hardware resources?

When CPU is in user mode, the programs don't have direct access to memory and hardware resources. In user mode, if any program crashes, only that particular program is halted. That means the system will be in a safe state even if a program in user mode crashes. Hence, most programs in an OS run in user mode.

Bài Viết Liên Quan

Chủ Đề