How to create your own OS #6
Hi friends welcome back this is the sixth part of the series if you miss earlier parts go throw this link : List: How to create your own OS (medium.com)
In previous parts we done that the kernel boots, prints to screen and reads from keyboard — what do we do? Usually, a kernel is not supposed to do the application logic itself, but leave that for applications. The kernel creates the proper abstractions (for memory, files, devices) to make application development easier, performs tasks on behalf of applications (system calls) and schedules processes.
User mode, in contrast with kernel mode, is the environment in which the user’s programs execute. This environment is less privileged than the kernel, and will prevent (badly written) user programs from messing with other programs or the kernel. Badly written kernels are free to mess up what they want.
There’s quite a way to go until the OS created in this article can execute programs in user mode, but this article will show how to easily execute a small program in kernel mode.
Loading an External Program
Where do we get the external program from? Somehow we need to load the code we want to execute into memory. More feature-complete operating systems usually have drivers and file systems that enable them to load the software from a CD-ROM drive, a hard disk or other persistent media.
Instead of creating all these drivers and file systems we will use a feature in GRUB called modules to load the program.
GRUB Modules
GRUB can load arbitrary files into memory from the ISO image, and these files are usually referred to as modules. To make GRUB load a module, edit the file 'iso/boot/grub/menu.lst’ and add ‘module /modules/program’ at the end of the file:
Now create the folder iso/modules:
mkdir -p iso/modules
The application program will be created later.
The code that calls kmain must be updated to pass information to kmain about where it can find the modules. We also want to tell GRUB that it should align all the modules on page boundaries when loading them (see the chapter “Paging” for details about page alignment).
To instruct GRUB how to load our modules, the “multiboot header” — the first bytes of the kernel — must be updated as follows:
GRUB will also store a pointer to a struct in the register ebx that, among other things, describes at which addresses the modules are loaded. Therefore, you probably want to push ebx on the stack before calling kmain to make it an argument for kmain.
Executing a Program
A Very Simple Program
A program written at this stage can only perform a few actions. Therefore, a very short program that writes a value to a register suffices as a test program. Halting Bochs after a while and then check that register contains the correct number by looking in the Bochs log will verify that the program has run. This is an example of such a short program:
Compiling
Since our kernel cannot parse advanced executable formats we need to compile the code into a flat binary. NASM can do this with the flag -f
:
nasm -f bin program.s -o program
This is all we need. You must now move the file program
to the folder iso/modules
.
we need to create a file call ‘multiboot.h’:
Now do changes in ‘kmain.c’ file:
The program we’ve written now runs at the same privilege level as the kernel — we’ve just entered it in a somewhat peculiar way. For that we need to run our OS
after that run ‘cat com1.out’ command
this the end of this article if you need more knowledge about this refer the below references,
Reference:
The little book about OS development (littleosbook.github.io)
My GitHub repository : rragul/windOS (github.com)