I recently sorted out my computer, found an operating system I wrote two years ago, and shared it online. Some of the code at that time looked naive.
At the beginning of the system, the bootloader part was made by referring to "writing an operating system by yourself". How to set up the environment in this book is very detailed, in addition, some basic concepts of PC are also well written. We recommend that you take a look at it. From the perspective of Segmented paging, I have referred to the linux2.4 style and did not follow the steps in that book. The debugging environment is Virtual PC and bochs.
The following is a summary:
Readme.txt
A simple operating system is implemented, including:
Protection Mode of segments and pages;
The second-level Boot Process in the fat2 file system;
Parsing and loading of kernel in ELF format;
Clock interruption and keyboard interruption in protected mode;
Printf implementation, including system calls;
Display of the screen, including some scrolling functions;
Two processes are interrupted once every second.
Memory planning:
-- 0000: 03ff real-mode stack, total length: 1 K
-- 0000: 04ff rom bios parameter zone (retention segment)
-- 0000: 0fff free
-- 8000: efff kernel. Bin
8000: f000 -- 8000: FFFF hardware detection Parameters
9000: Running --9000: 01ff Boot Code, total length: 512
9000: 0200--9000: 13ff fat content, total length: 1200 h
9000: 1400--9000: 2fff root directory content, total length 1c00h
9000: 3000--9000: fbff loader.com
9000: fc00 -- 9000: FFFF extended BIOS data area (retained)
A000: 0000 -- b000: FFFF display adapter reserved (retained)
C000: 0000 -- d000: FFFF reserved for Rom expansion (retained)
E000: 0000 -- e000: FFFF expansion of system ROM (retained)
F000: 0000 -- f000: FFFF system ROM (retained)
0x100000 -- the migrated kernel. Bin
0x101000 -- 0x102000 page Directory table
0x102000 -- 0x104000 two page tables, addressing 8 m
0x104000 -- 0x105000 page 0
The hardware detection parameter area starts from 8000: f000:
The number of e820 bytes starting from the offset 0x1e8.
Offset 0x2d0 -- 0x550 0x280 is the memory detection result.
After the kernel is started, the information is copied to page 0.
Boot. Bin:
When mov ax and [Si +...] are used, bytes at the address are read into ah, and bytes at the plus 1 are read into Al. This was not previously taken into account, and this was corrected when I wrote loader.com.
When an interruption occurs in real mode, the interrupt vector table is placed in a 0-1k memory interval, with each interrupt vector 4 bytes, totaling 256.
Loader.com:
Self-check memory size.
Read kernel. bin to the memory.
Enter the protection mode.
In protection mode, move kernel. bin to H and jump. (Note that there is an elf parsing process)
Other functions are to be determined.
Kernel. Bin:
C language is used here.
There will be a lot of things to refer to the Linux kernel in the future.
2005/12/19, Monday, folder 1-1
Implements a simple function call.
2005/12/20, Tuesday, folder 1-2
Implemented code self-lifting.
2005/12/21, Wednesday, folder 1-3
I understand the structure of fat12, read the floppy disk, and copy both fat12 and root directories to the memory.
2005/12/22, Thursday, folder 1-4
Some function calls are changed to macro definitions, and the encoding is successfully compressed.
2005/12/23, Friday, folder 1-5
The specified file search is implemented, and the code returned to DOS is added.
2005/12/24, Saturday, folder 1-6
Copy the specified file to the specified memory area and perform redirection. Corrected a bug in reading a floppy disk (an error occurred during the first read ). The remaining 60 bytes are available.
, Wednesday, folder 2-1
Implements memory detection.
, Thursday, folder 2-2
The kernel. Bin is copied and an example of kernel. Bin is written. It can run successfully.
2005/01/03, folder 2-3
Now you can successfully jump into the protection mode.
2006-1-4, folder 2-4
Parses elf files.
2-5
Reorganizes the source code and overwrites the makefile.
2-6
Changed GDTR in protection mode
(It turns out that this is wrong. GDTR should be a linear address, not the address in the house)
2-7
Change the virtual address of the kernel to 0xc0100000.
Paging is performed on the first 8 MB of space.
2-8
Implements Soft Interrupt calls.
The content in gdt and IDT should be linear addresses mapped by page.
3-1
Integration with C functions.
3-2
The row and column values of the cursor are changed to global variables.
3-3
Implements initialization of the interrupt function.
3-4
Some printf functions are implemented to distinguish between % d and % x.
The system call,/n/t, and cursor movement have not yet been implemented.
Implemented some string functions.
3-5
Improved printf. Current features include
/T/N % x % d
You can obtain the memory information. Currently, no memory ing is added.
3-6
Improved the interrupt function.
Macro definition and interrupt implementation in general_intr.c are worth studying.
3-7
The operation performed 8259, implementing a clock interruption.
3-8
The cursor follows and moves.
3-9
Screen scrolling is implemented, but currently only one console is supported.
(Home) 3-10
Read the keyboard.
INB and other functions have also been implemented.
3-11
Implements switching between different permissions.
3-12
Implemented a simple trap.
3-13
The write_console is implemented by adding a system call. Currently, there is no console concept.
This system call also implements the return value function, which is interesting.
3-14
Implements process switching.