# x86Linux_2022 **Repository Path**: bessiechen2022/x86-linux_2022 ## Basic Information - **Project Name**: x86Linux_2022 - **Description**: Implementing the Linux 0.11 kernel on the x86 architecture, completing the development of several submodules: bootloader/kernel loader/interrupt and exception handling/process scheduling... - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-09-15 - **Last Updated**: 2023-06-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Linux1.0 based on the x86 architecture This project is an operating system based on the x86 architecture. In terms of development tools and environments, the main tools used are the Visual Studio Code integrated development environment (with GCC as the compiler) on the Linux platform and the QEMU simulator. The main reference materials for this project include GCC inline assembly, x86 kernel registers, and cursor control, among others. The project is divided into the following main parts: boot program, loader, memory management, process management, interrupt and exception handling, input/output devices, file system, and command-line interpreter. The following will provide a detailed introduction to these modules. ### Achievements #### 1. Boot the operating system ![Boot](image/img2023-05-01%2012-31-48%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 2. Display the log info: author is Beixi(Bessie) ![Author: Beixi(Bessie)](image/img2023-05-01%2012-32-06%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 3. Echo command ![echo -n 5 beixi](image/img2023-05-01%2012-32-47%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 4. Help command: display all the available command ![输入图片说明](image/img2023-05-01%2012-33-18%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 5. ls command: list all the .elf file ![输入图片说明](image/img2023-05-01%2012-33-48%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 6. cp/rm: copy the loop file and then remove it ![输入图片说明](image/img2023-05-01%2012-44-01%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 7. Display the first couple of lines of an .elf file ![输入图片说明](image/img2023-05-01%2012-44-46%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) #### 8. Snake game ![输入图片说明](image/img2023-05-01%2012-57-52%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) ![输入图片说明](image/img2023-05-01%2012-58-38%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) ![输入图片说明](image/img2023-05-01%2013-01-18%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) ![输入图片说明](image/img2023-05-01%2013-03-44%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png) ### Boot First, the project implements the function of the boot program (Boot). This part involves an introduction to the computer startup process, taking control of execution, and creating a bootable startup program. At the same time, the project uses BIOS interrupts to display characters and read disks, and finally enters the C language environment and jumps to the Loader. ### Loader The implementation of the Loader includes using GCC inline assembly to display strings, detecting memory capacity, switching to protected mode, using LBA to read disks, creating a kernel project, passing boot information to the kernel, code/data segment and link script, and loading kernel image files, etc. ### Interrupt and exception handling In the part of interrupt and exception handling, the project creates a GDT table and its entries, introduces memory management in protected mode, reloads GDT, triggers exceptions and exception introduction, adds interrupt gate descriptors, initializes IDT table, captures divide-by-zero exception, parses exception stack information, reuses exception handling code with .macro macro, handles other types of exceptions, initializes interrupt controller, turns on and off interrupts, and starts the timer and turns on interrupts, etc. ### Log Next is the log and printf formatted output part. The project creates a log output interface, implements basic information output, implements a number of string and memory operation functions, implements formatted output of strings, supports integer and character formatting, prints information in the exception handling program, and uses assert to assist debugging, etc. ### Process management The project implements a simple process switching function, including creating two small programs, adding task state segments, initializing tasks, simple dual-task switching, more straightforward and efficient task switching methods, and the concept of process, etc. To implement process management, the project implements a linked list data structure, including defining nodes and linked lists, implementing linked list query functions, linked list insertion and deletion operations, and obtaining the structure where the nodes are located, etc. In the process management and delay part, the project adds a task manager, adds processes to the ready queue, implements the yield() function for processes to actively give up CPU, processes passively give up CPU (time slice operation - simple scheduling algorithm), critical resource protection and simple protection, allows processes to delay operation, and allows all processes to delay, etc. ### Synchronization and mutual exclusion mechanism The project implements the synchronization and mutual exclusion mechanism between processes, solving the resource competition between multiple processes by introducing counting semaphores and mutex locks. Counting semaphores can be used to synchronize the execution order of multiple processes, ensuring that processes run in the expected order. Mutex locks are used to protect critical resources, ensuring that only one process can access these resources at the same time, preventing data inconsistency and race conditions. ### Virtual memory management The implementation of virtual memory management and protected mode makes the project more efficient in managing memory resources. The project introduces a bitmap data structure to describe the use of memory and implements a memory paging mechanism, allowing memory to be allocated on-demand to different processes. At the same time, by creating kernel page tables and creating page tables for processes, the project achieves address space isolation between the kernel and processes, ensuring the stability and security of the operating system. ### System call mechanism To further improve the functionality of the operating system, the project implements a system call mechanism. By creating call gates and implementing calls and returns, user processes can easily request services from the operating system. The project also implements various system calls, such as msleep, getid, print_msg, etc., providing a rich functional interface for user processes. ### fork() and exec() In the part of loading and creating new processes, the project implements the fork and exec system calls, making the operating system support the creation of new processes and loading different program codes. In addition, the project also implements support for printf output in the C standard library, adjusting file system calls and importing the newlib C library, allowing user processes to use a more user-friendly output interface. ### I/O The project implements an input/output module based on the console and keyboard, supporting the display of strings on the console, handling line breaks and screen clearing operations, and inputting commands through the keyboard. The project also implements a device management and file system module, providing users with convenient device and file operation interfaces. ### Device management In the device management and file system part, the project builds a device management framework for the unified management of various hardware devices. By adding tty devices, the dev layer interface operation is realized, and a structure description is added to the tty device, allowing the operating system to communicate with the tty device. In addition, the project also implements the opening of tty devices and the reading and writing of display data through tty devices. ### Command-line interpreter In this project, a simple command-line interpreter is implemented, through which users can enter commands and perform some basic operations, such as viewing help, executing echo commands, etc. The project also adds exit and wait interfaces for processes, realizing normal process exit and waiting. The command-line interpreter also supports the creation of child processes and the inheritance of files opened by the parent process. The project also implements a file system for describing hardware devices, allowing users to operate hardware devices through the file system, such as opening and reading/writing the /dev/tty0 file. ### File system To better manage files, the project implements a file system module, including initialization, adding file open tables for processes, opening tty devices and writing data to them, etc. The project also implements reading key-value strings from tty and displaying them, as well as opening standard output and error output files. By allowing tty window switching, the project achieves process protection between multiple tty windows. In the later stages of the project, support for the FAT16 file system is implemented, allowing the operating system to recognize and mount FAT16 file systems. In addition, some basic file operation commands are implemented, such as ls, less, cp, and rm, allowing users to perform file operations in the command-line environment. To demonstrate the practical application of the operating system, a simple snake game is implemented, which can be loaded and run through the operating system's command-line interface. In summary, the project successfully implements a simple operating system based on the x86 architecture. By implementing various aspects such as boot program, loader, kernel, interrupt and exception handling, process management, memory management, device management, and file system, the project builds a basic functional operating system. Although there are many areas for optimization and improvement in this operating system, it provides an excellent learning and practice platform for the project, allowing for a deeper understanding of computer system operation principles and laying the foundation for the development of more complex operating systems in the future.