

Introduction:The rapid development of the IT industry has raised higher requirements for talent cultivation in universities. We can no longer be satisfied with producing talents who can only use foreign CPUs and operating systems; instead, we need to cultivate talents who can “create” their own operating systems. This is essential for achieving autonomy in the IT industry from the very beginning..
Designing and implementing a basic functional operating system kernel hands-on is an effective way to learn about operating systems.

01Teaching Version of the Operating System Based on LoongArch Architecture
The operating system, as the core of computer systems, is crucial for cultivating students’ computer skills and logical thinking. Nevertheless, many universities still face challenges in providing sufficient practical teaching resources, especially lacking basic experimental content suitable for undergraduates, which limits students’ opportunities for a deeper understanding of operating systems.
As early as 2019, Professor Zhou Qingguo from Lanzhou University, leading the Distributed and Embedded Systems Laboratory, planned to write a textbook for the operating system experimental course. The initial idea was to conduct an in-depth analysis of the Linux 0.11 kernel based on the x86 architecture and to implement a teaching version of the Linux 0.11 operating system kernel from scratch. However, following this approach required analyzing over 10,000 lines of source code, leading to slow progress.
Fortunately, Loongson Technology launched the LoongArch architecture in 2020, based on twenty years of CPU research and development experience and ecological construction. The Distributed and Embedded Systems Laboratory at Lanzhou University collaborated with Loongson Technology in 2021 on a Ministry of Education industry-university cooperation project—”Teaching Version Operating System Kernel Based on LoongArch Architecture.” In 2022, as an important outcome of this project, MaQueOS was born. MaQueOS, which translates to “Sparrow Operating System” in Chinese, signifies that “though small, the sparrow has all its organs.” Although the code size is small, it encompasses the main functions of an operating system.
02Introduction to MaQueOS
MaQueOS is an open-source teaching version operating system based on the LoongArch architecture. As a teaching version operating system, MaQueOS has over 1,000 lines of code, but it implements core operating system functions such as process management, memory management, file systems, interrupt management, and device drivers and provides 16 system call interfaces for applications.
MaQueOS is derived from a heavily trimmed version of the Linux 0.11 kernel and has completed the porting from the x86 architecture to the LoongArch architecture. All device driver programs (hard disk, keyboard, and display) have been rewritten. Additionally, unlike Linux 0.11, which supports the MINIX file system, a.out executable files, and inter-process communication mechanisms based on pipes, MaQueOS supports a custom xtfs format file system, xt format executable files, and inter-process communication mechanisms based on shared memory.
Among them, the xtfs file system is a very small file system that implements file creation/deletion, opening/closing, and reading/writing operations, as well as file system mounting functions, using just over 100 lines of code. The xt executable file includes a 512-byte executable file header and binary executable code linked together, making the loading process for xt executable files quite simple. The implementation of the inter-process communication mechanism based on shared memory is also simpler and more efficient than that based on pipe operations.
03
An Operating System Design and Implementation Textbook with “Loongson Characteristics”
After several years of practice, Professor Zhou Qingguo and the teaching team successfully authored the book “Operating System Design and Implementation: Based on LoongArch Architecture” based on the teaching achievements and experiences centered around the teaching version operating system MaQueOS. This book guides students in comprehensively analyzing the design and implementation process of a basic functional operating system kernel, making it a textbook with “Loongson characteristics” for operating system design and implementation.
This course has foundational, systematic, and practical characteristics, with theoretical content covering key areas such as memory management, device drivers, file systems, and process management. Through specific cases and problem-solving approaches, it helps students understand the core mechanisms of operating systems in a simple and straightforward manner.
The book adopts a progressive writing style, where the content of each chapter is based on the previous chapter’s content to iteratively realize functions, ultimately forming a complete operating system. The book provides source code for various key functions and features, along with detailed explanations and comments, allowing readers to understand the implementation methods of different operating system functions and how these functions work together.
The book is clearly structured and progresses step by step, highlighting both the basic principles of operating systems and the emphasis on hands-on implementation, making it suitable as a textbook for operating system-related courses in computer science programs at universities. Additionally, it can serve as an introductory reference book for readers learning about Linux kernel principles.
In-depth Analysis, Complete Code, Understanding Principles
Analyzing the design and implementation process of the operating system kernel


Authors: Zhou Qingguo, Yang Hubin, Liu Gang, Chen Yucong, Zhang Fuxin
ISBN: 978-7-111-74668-3
Book Features:
1
In-depth analysis of the implementation methods of MaQueOS’s display driver, clock interrupt, keyboard driver, creation and operation of process 0 and process 1, process suspension and awakening, hard disk driver, xtfs file system, loading executable files by process 1, page exceptions, inter-process communication, and file operations, helping readers understand the operating system’s operating principles and implementation methods..
2
Adopts a progressive writing style, where the content of each chapter is based on the previous chapter’s content to iteratively improve functionality, ultimately forming a complete operating system and enhancing readers’ engineering capabilities.
3
The book provides source code for various key functions and features, along with detailed explanations and comments, allowing readers to understand the implementation methods of different operating system functions and how these functions work together.
Table of Contents
Scroll up and down to view the table of contents ↓
Preface
Chapter 0: Introduction 1
0.1 MaQueOS’s Functions 1
0.1.1 Process Management 2
0.1.2 Memory Management 4
0.1.3 File System 4
0.1.4 Device Drivers 5
0.1.5 Interrupt Management 5
0.2 System Function Testing 5
Chapter 1: Display Driver 7
1.1 Principles of Display 7
1.1.1 Display Modes 7
1.1.2 Character Display 7
1.2 printk Function 10
1.2.1 Displaying Strings 10
1.2.2 Character Erasure 12
1.2.3 Carriage Return and Line Feed 13
1.2.4 Scroll Screen 13
1.2.5 Delete Character 14
1.2.6 Panic Function 15
1.3 Chapter Tasks 15
Chapter 2: Clock Interrupt 16
2.1 Clock Interrupt Initialization 16
2.1.1 Constant Frequency Timer 16
2.1.2 Initialization 17
2.1.3 Enable Interrupts 18
2.2 Processing of Clock Interrupt 18
2.2.1 Interrupt Hardware 18
2.2.2 Saving and Restoring Interrupt Context 19
2.2.3 Interrupt Handling 21
2.2.4 Interrupt Return 21
2.3 Chapter Tasks 22
Chapter 3: Keyboard Driver 23
3.1 Physical Memory Management 23
3.1.1 Initialization 24
3.1.2 Allocation 25
3.1.3 Release 26
3.2 Initialize Keyboard Interrupt 27
3.3 Processing of Keyboard Interrupt 29
3.4 Chapter Tasks 32
Chapter 4: Creation and Operation of Process 0 34
4.1 Virtual Memory Management 34
4.1.1 Initialization 35
4.1.2 Establish Page Table Mapping 37
4.2 Create Process 038
4.3 Operation of Process 0 41
4.3.1 Process 0 Enters User Mode 41
4.3.2 TLB Refill Exception Handling 42
4.4 Processing of Clock Interrupt 44
4.4.1 Interrupt Response and Handling 44
4.4.2 Interrupt Return 46
4.5 Chapter Tasks 46
Chapter 5: Creation and Operation of Process 1 48
5.1 Create Process 148
5.1.1 System Call 48
5.1.2 Fork System Call 51
5.1.3 System Call Return 54
5.2 Process Switching 55
5.2.1 Clock Interrupt 55
5.2.2 Switch from Process 0 to Process 156
5.2.3 Operation of Process 1 59
5.3 Chapter Tasks 59
Chapter 6: Process Suspension, Awakening, and Termination 60
6.1 Non-interruptible Suspension and Awakening 60
6.1.1 Non-interruptible Suspension 62
6.1.2 Awakening Non-interruptible Suspended Process 64
6.2 Interruptible Suspension and Awakening 65
6.3 Process Termination 66
6.3.1 Exit System Call 66
6.3.2 Release Process Resources 67
6.4 Chapter Examples 69
6.5 Chapter Tasks 72
Chapter 7: Hard Disk Driver 73
7.1 Initialize Hard Disk 73
7.2 Read and Write Hard Disk 77
7.2.1 Send Read/Write Commands 77
7.2.2 Hard Disk Interrupt Handling 78
7.3 Hard Disk Read/Write Examples 80
7.3.1 Create Hard Disk Image File 81
7.3.2 Read Hard Disk 82
7.3.3 Write Hard Disk 85
7.4 Chapter Tasks 86
Chapter 8: xtfs File System 87
8.1 Overview of xtfs File System 87
8.2 Format xtfs File System 87
8.2.1 Format xtfs.img 88
8.2.2 Formatting Example 88
8.3 File Copy 89
8.3.1 Load Data Blocks 0/1 90
8.3.2 Copy Data Blocks 90
8.3.3 Create Data Block Index Table 92
8.3.4 Initialize inode Data Structure 93
8.3.5 Write Back Data Blocks 0/1 94
8.3.6 Copy Example 94
8.4 Chapter Tasks 97
Chapter 9: Process 1 Loads Executable File 98
9.1 Mount xtfs File System 98
9.2 xt Executable File 100
9.2.1 Compile xt Executable File 100
9.2.2 Shell Program 102
9.3 Load Executable File 104
9.4 Operation of Process 1 108
9.5 Chapter Examples 109
9.6 Chapter Tasks 109
Chapter 10: Page Exceptions 111
10.1 Page Invalid Exception 111
10.1.1 Trigger Page Invalid Exception 112
10.1.2 Handle Page Invalid Exception 114
10.2 Page Modification Exception 116
10.2.1 Trigger Page Modification Exception 118
10.2.2 Handle Page Modification Exception 118
10.3 Chapter Examples 120
10.4 Chapter Tasks 122
Chapter 11: Inter-process Communication 123
11.1 Shared Memory 123
11.1.1 Initialization 123
11.1.2 Establish Mapping with Shared Page 125
11.1.3 shmem System Call 127
11.1.4 Copy Page Table 128
11.1.5 Release Page Table 129
11.1.6 Shared Memory Example 130
11.2 Software Timer 131
11.2.1 Implementation Principle of Software Timer 131
11.2.2 Software Timer Example 133
11.3 Chapter Tasks 134
Chapter 12: File Operations 135
12.1 Create File 135
12.1.1 Process of Creating File 135
12.1.2 Create File Example 137
12.2 Write File 140
12.2.1 Process of Opening File 140
12.2.2 Process of Writing File 141
12.2.3 Process of Closing File 142
12.2.4 Write File Example 144
12.3 Read File 146
12.3.1 Process of Reading File 146
12.3.2 Read File Example 146
12.4 Delete File 147
12.4.1 Process of Deleting File 147
12.4.2 Delete File Example 149
12.5 Chapter Tasks 150
Appendix 151
Appendix A: Setting Up Experiment Environment 151
Appendix B: LoongArch Assembly Instructions 153
Appendix C: LoongArch Control Status Register 157
Appendix D: MaQueOS Library Functions 159
Appendix E: Airplane War Program Design 163
Encoding
Author Introduction 



Writer: Zhou Rui
Editor: Zhang Dunhong
Reviewer: Li Shuanglei

To better serve teachers and facilitate their application for sample books and selection of teaching materials anytime and anywhere, the Mechanical Industry Press has launched the “Cloud Book Exhibition” platform. Currently, the cloud book exhibition has launched over 10,000 teaching materials, including over 2,000 national-level planning teaching materials from the “Eleventh Five-Year” and “Twelfth Five-Year” plans, covering all levels of undergraduate, higher vocational, secondary vocational, and technical schools. Most of these materials are accompanied by corresponding electronic resources.
ClickPoster to Learn About the Teaching Materials
▼



Mechanical Industry Education (ID: cmpedu) The WeChat public service account is hosted by the Mechanical Industry Press, with the mission of “spreading new knowledge and serving education,” providing free services such as textbook resource downloads, sample book applications, teacher training, topic publishing, and educational information for university teachers.