1. Environment Configuration
We need a basic environment to design and develop our kernel. In this tutorial, I assume that you are using * NIX operating system and GNU tool set. If you are using a Windows operating system, you must use a * Nux simulation environment such as cygwin or djgpp. Even so, the file generation and command in this tutorial may still fail.
1.1 directory structure
My directory layout is as follows:
Tutorial
|
+ -- SRC
|
+ -- Docs
All source files are put into SRC, and all documents are put into docs (Do you write documents during development ?)
1.2 compile
All examples in this tutorial have been successfully compiled (GCC, LD, gas, etc) through the GNU tool set. All examplesCodeIntel-style assembly is used for writing, because I personally think that intel-style assembly is much more readable than at&t-style assembly when GNU is used. We need to use NASM for compilation.Program.
This tutorial is not a kernel boot tutorial. We use grub to load the kernel. To do this, we need to have grub preload a soft drive image. There are many tutorials to guide us through using grub. What's more, you can find a standard soft drive image I have made here and put it under your tutorial directory.
1.3 run
Nothing can replace the kernel test on a real machine, but unfortunately, the running result of the real machine cannot tell you what went wrong (of course, you may have written the correct code for the first time, right ?). Bochs is an open-source, 32-bit and 64-bit virtual machine. When an error occurs in your program, bochs will notify you and save some useful register statuses to the log. In addition, bochs are much faster to start than a real machine. All my examples can be successfully run on bochs.
1.4 bochs
To run bochs, you need a bochs configuration file (bochsrc.txt). Coincidentally, the following is a configuration example.
Note the sequence of these BIOS files. These seem to change between installations (different versions may be different ???). If you useSource codeCompile bochs directly, and you may not have these files. Google the file name and download it from the official website.
Megs: 32
Romimage: file =/usr/share/bochs/BIOS-bochs-Latest, address = 0xf0000
Vgaromimage:/usr/share/bochs/VGABIOS-elpin-2.40
Floppya: bytes 44 =/dev/loop0, status = inserted
Boot:
Log: bochsout.txt
Mouse: enabled = 0
Clock: Sync = realtime
CPU: IPS = 500000
The above settings enable bochs to virtualize a 32 M memory, 350 MHz P2 CPU. A lot of information will be refreshed every second-but I like a slow Virtual Machine for a simple reason, so that I can see a lot of text rolling.
1.5 use scripts
We will do something frequently-compile and connect our projects and write the generated binary kernel into the soft drive image.
1.5.1 generate scripts
Code
# Makefile for jamesm ' S kernel tutorials.
# The C and C ++ rules are already setup by default.
# The only one that needs changing is the caller
# Rule, as we use NASM instead of GNU.
Sources = boot. o
Cflags =
Ldflags =-tlink. LD
Asflags =-felf
ALL: $ (sources) Link
Clean:
»-RM *. O Kernel
Link:
» LD $ (ldflags)-O kernel $ (sources)
. S. o:
» NASM $ (asflags) $ <
This generation script will compile every file in sources and connect them together to generate a binary kernel. Of course, a connection script is required to do this. The link. LD mentioned below is used to do this.
1.5.2 link. LD
Code
/* Link. LD -- linker script for the kernel-ensure everything goes In The */
/* Correct place .*/
/* Original file taken from bran ' S kernel development */
/* Tutorials: http://www.osdever.net/bkerndev/index.php .*/
Entry (start)
Sections
{
. Text 0x100000:
{
Code =.; _ code =.; _ code = .;
* (. Text)
. = Align (4096 );
}
. Data:
{
Data =.; _ DATA =.; _ DATA = .;
* (. Data)
* (. Rodata)
. = Align (4096 );
}
. BSS:
{
BSS =.; _ BSS =.; _ BSS = .;
* (. BSS)
. = Align (4096 );
}
End =.; _ end =.; _ end = .;
}
This script tells LD how to create a kernel image. First, it indicates that the start position of the LD should be at the start position. Then, he told lD that. Text (where your code is located) will be executed from 0x100000. Then run. data Segment and. BSS segment, both of which are page-aligned (align (4096). Linux GCC also adds an additional data segment: fodata, which is only used for initialization, just like a constant. For simplicity, we use them together.