Basic process of this course
The basic process of the experiment is to write the application program according to the experimental requirements, modify the source code of Linux 0.11, compile with GCC, run and debug the target code in the virtual environment of Bochs.
Basic tools and introduction of the experiment (part of Google)
X86 simulator Bochs:
Bochs is a free and open source IA-32 (x86) architecture PC simulator, note thatPC simulatorSo it can simulate hardware configuration and I / O device, memory and BIOS of the whole PC. It can not only run Linux, but also simulate dos and various versions of windows and other operating systems.
*High portability: so this course chooses it
*Running efficiency is lower than that of virtual machine: it depends on its principle, but it is not obvious in this experiment.
*Compared with virtual machine: it has a powerful and incomparable ability to debug the operating system
[note] Bochs has been built into hit oslab in this experiment
It is a kind of computer program, which can convert the source code (original language) written in one programming language into another programming language (target language). The main workflow of a modern compiler is as follows: source code → preprocessor → compiler → assembler → object code The final packed file can be read and run by the computer.
Gcc compiler used in this course, GCC is a compiler growing up with Linux. The original version of Linux was compiled by GCC. Now GCC is also the most widely used compiler in the field of free software. Therefore, we also choose GCC as the specified compiler for this book’s experiment.
Basic functions of debugger
1. Control software operation
The most basic function of debugger is to interrupt a fast running program and make it execute according to the user’s wishes. The debugger does this by forcing the target program to trigger a carefully constructed exception.
2. View the information in software operation
View the current information of the software, including but not limited to the register information of the current thread, stack information, memory information, disassembly information near the current EIP, etc.
3. Modify software execution process
Modify memory information, disassembly information, stack information, register information, etc.
GDB debugger is the brother of gcc compiler. As the only debugger in the field of free software, it inherits the consistent style of * Nix operating system and adopts pure command-line operation, which is similar to the debugging under DOS. Please refer to GDB user manual for its usage.
Ubuntu is an open source GNU / Linux operating system based on desktop applications. Ubuntu is based on Debian GNU / Linux, supports x86, AMD64 (i.e. x64) and PPC architectures, and is built by a global professional development team (canonical Ltd)
The main feature is easy to use, very easy to use.
Working mode of laboratory
It is a compressed file (hit-oslab-linux-20110823. Tar. GZ).
First find the compressed file and convert it to the folder at the terminal
tar zxvf hit-oslab-linux-20110823.tar.gz
Command decompress to work
The extracted directory is shown in the figure:
How oslab works
Oslab works on a host operating system. After the development, compilation and modification of Linux are completed on this host system, a file named image will be generated under the linux-0.11 folder. (refer to the above figure for understanding) this is the compiled target file.
This file contains the boot and all kernel binaries. If you take a floppy disk and start from sector 0 of track 0, write the image file byte by byte, it can use this floppy disk to start a real computer!
Oslab uses the Bochs simulator to load the image file and simulate the execution of Linux 0.11, which saves the trouble of restarting the computer!
After decompressing the files, we see the Bochs directory, which is the execution files, data files and configuration files related to Bochs. After using the run command to run Bochs, two will be created, one is the virtual hard disk, the other is the virtual floppy drive A. in the floppy drive, the image file just mentioned is closed, while the hdc-0.11.img is mounted on the hard disk. Because it starts from floppy drive a, Linux 0.11 will be loaded automatically,
Linux 0.11 will drive the hard disk and mount the file system on the hard disk, that is, mount the file system mirrored in hdc-0.11.img to the root directory of 0.11 system – “/”. Under 0.11, accessing the file system is the virtual file system in the hdc-0.11.img file.
The format of the hdc-0.11.img file is a mirror image of the MINIX file system. All versions of Linux support the file system of this format, so you can directly access the files in the file through the mount command on the host Linux to achieve the effect of exchanging files between the host system and Linux 0.11 running in Bochs. There is no (or no) way to access MINIX file system directly under windows, so fdb.img is a 1.44M floppy disk image file with FAT12 file system inside. Mount it to Bochs floppy drive B, and you can access it in 0.11. Through filedisk or winimage, you can access its internal files under windows.
Bash shell Some basic linux commands and tools, such as CP, RM, MV, tar. VI editor GCC 1.4 compiler, which can be used to compile standard C programs As86 and ld86 The source code of Linux 0.11, which can be compiled under 0.11 and then overwrite the existing binary kernel
Warming up activities
- cd ~/oslab
Switch the current directory to oslab, confirm with PWD command, and list the directory content with “LS – L”. All contents of this experiment are completed in this directory or its subordinate directories.
- Compiling kernel
Compiling the kernel is much simpler than writing the kernel. First, enter the linux-0.11 directory, and then execute:
$ make all
Because “all” is the most commonly used parameter, it can be omitted. Only “make” is used. The effect is the same.
On the multiprocessor system, you can use the – J parameter to compile in parallel and speed up. For example, a dual CPU system can:
$ make -j 2
The make command will display a lot of information. You can try to understand it or pretend not to see it. As long as there is no “error” in the last few lines, the compilation is successful. The final target file is a floppy disk image file — linux-0.11/image. If you write this image file to a 1.44MB floppy disk, you can start a real computer.
Linux-0.11 directory is the whole source code, and many experiments are done by modifying these codes. Do you need to recompile the kernel or execute the command after modification
$ make all
The make command will automatically skip the unmodified files and directly use the target files generated from the last compilation when linking, thus saving compilation time. But if your changes don’t seem to work after recompilation, try
“make clean”It is to delete all intermediate files and target files generated in the last compilation to ensure that the whole project is compiled in a new state.
- Operation and commissioning
Running the newly compiled kernel in Bochs is very simple. Execute in the oslab Directory:
If the Bochs window appears, the Linux boot process will be displayed in it. Finally, it will stop at “[/ usr / root /]”, indicating that the operation is successful.
- Kernel debugging can be divided into two modes: assembly level debugging and C language level debugging.
Assembly level debugging requires command execution:
You can use the command help to view the basic commands for debugging the system. Please refer to the Bochs user manual for more details.
C level debugging is a little more complicated. First, execute the following command:
Then open a terminal window, enter the oslab directory, and execute:
The GDB debugger is running in the new terminal window. Please refer to the GDB user manual for GDB debugger.
File exchange between Ubuntu and Linux 0.11
Hdc-0.11-new.img in oslab is the root file system image file after the 0.11 kernel is started, which is equivalent to the hard disk loaded in the Bochs virtual machine. The way to access its content on Ubuntu is (when you use sudo, the password is shiyanlou):
$ sudo ./mount-hdc, mount the Linux 0.11 file system to the HDC directory
The 2.hdc directory is exactly the same file as the Linux 0.11 kernel.
3. Then you can operate directly in the HDC directory. For example, you can create a new hello. C in the HDC directory
$ sudo umount hdc
After sudo. / Mount HDC processing, we can create a XXX. C file in the HDC directory of Ubuntu, and then use the editing tools (such as GEDIT, etc.) on Ubuntu to edit the XXX. C file, after editing and saving. After executing sudo umount HDC and entering Linux 0.11 (i.e. after running to start Bochs), you will see this XXX. C (i.e. as shown in the figure below), which avoids the trouble of editing XXX. C on Linux 0.11. As a small operating system, the editing tool on Linux 0.11 is only VI, which is very inconvenient to use.
In addition, files generated on Linux 0.11, such as the process.log file generated in later experiments, can be “obtained in this way” and processed with Python program under Ubuntu. Of course, this Python program is obviously not easy to use on Linux 0.11, because the python interpretation environment cannot be built on Linux 0.11.
[note]: do not mount the image file when the 0.11 kernel is running, otherwise the file system may be damaged. Similarly, do not run the 0.11 kernel when it is already mounted.
[note]: before closing Bochs, you need to run “sync” on the command line of 0.11 to ensure that all cached data is saved before closing Bochs.