Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux 0.114th | move all operating system codes from hard disk to memory

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >

Share

Shulou(Shulou.com)11/24 Report--

This article comes from the official account of Wechat: low concurrency programming (ID:dibingfa). The original title: "the fourth time | put other parts of your hard drive into memory, too." author: flash.

With a novel-reading mentality, this series will read and appreciate all the core code of Linux 0.11 from the order of code execution after boot, and understand the technical details and design ideas of the operating system.

You will follow me, watching an operating system start from nothing, step by step to achieve its complex and exquisite design, after reading this series, I hope you can sigh that the original operating system source code is this stupid thing. The following is a list of published articles. For more information about this series, you can start with the opening words.

Opening words

The first two lines of code for the first time

The second time I moved a place for myself.

Do the most basic preparatory work for the third time

The GitHub address of this series: click here to visit

-the text begins-

In the last book, as we mentioned in the last book, some of the most basic preparations for the operating system are ready.

As shown in this figure, the operating system sets the data segment register ds and code segment register cs to 0x9000 in just a few lines of code at this time to facilitate code jump and data access. Moreover, the stack top address ss:sp is set at the 0x9FF00 far enough from the code location 0x90000 to ensure that the development of the stack downward will not easily bump into the code location.

To put it simply, it sets how to access the data segment, how to access the code segment, and how to access the top pointer of the stack, that is, a preliminary memory planning, from the CPU point of view, access memory, just these three places.

After doing all this basic work, it's time for a new twist, and let's move on.

Load_setup: mov dx,#0x0000; drive 0, head 0 mov cx,#0x0002; sector 2, track 0 mov bx,#0x0200; address = 512, in 0x9000 mov ax,#0x0200+4; service 2, nr of sectors int 0x13; read it jnc ok_load_setup; ok-continue mov dx,#0x0000 mov ax,#0x0000; reset the diskette int 0x13 jmp load_setupok_load_setup: Here are two int instructions we haven't seen yet.

Note that this int is an assembly instruction, but not an integer variable of a high-level language. Int 0x13 initiates the 0x13 interrupt. This instruction assigns values to dx, cx, bx, and ax as parameters to the interrupt program.

If you don't understand what interruption is, don't worry about it, if you just can't let it go, you can take a look at my previous article: seriously talk about interruptions, which is very detailed.

In short, after the interrupt is initiated, CPU will use the interrupt number to find the entry address of the corresponding interrupt handler and jump to execute it, which is logically equivalent to executing a function. The handler of the 0x13 interrupt is written by BIOS in advance and is a function of reading the related functions of the disk.

After entering the operating system kernel, the interrupt handler needs to be rewritten by ourselves. In later chapters, you will continue to see that each module registers its own relevant interrupt handler, so don't worry. At this time, for convenience, we first use BIOS to write the program for us in advance.

It can be seen that even the source code of the operating system, sometimes need to call ready-made functions for their own convenience, not the person who built the wheel has to build it completely from scratch.

The comments of this code have been written very clear, directly say the ultimate role, is to start the second sector of the hard disk, load the data into the memory 0x90200, a total of 4 sectors are loaded, the diagram is actually like this.

In order to clearly express the meaning of the picture, the proportion may not be so strict, we do not have to struggle.

As you can see, if the copy succeeds, it jumps to the ok_load_setup tag, and if it fails, the code will be executed over and over again, that is, try again. Then let's forget the retry logic and look directly at the code after the ok_load_setup tag that jumps after success.

Ok_load_setup:... Mov ax,#0x1000 mov es,ax; segment of 0x10000 call read_it... Jmpi 0re0x9020 this code omits a lot of non-main logic code, such as outputting Loading system on the screen. This string to prevent users from getting tired of waiting.

The rest of the main code is written here, just a few lines, and its function is to load the 240sectors from the sixth sector of the hard disk into the memory 0x10000, just like the previous tampering from the hard disk to memory.

At this point, all the code of the entire operating system has been moved from the hard disk to memory.

Then through a familiar inter-segment jump instruction jmpi 0Jol 0x9020, jump to 0x90200, which is the beginning of the second sector of the hard disk.

So what's the content here? Don't worry, let's take this opportunity to talk about the compilation process of the entire operating system. The whole compilation process, which is completed through the cooperation of Makefile and build.c, will eventually:

1. Compile bootsect.s into bootsect and put it in sector 1 of the hard drive.

two。 Compile the setup.s into setup and put it in the 2'5 sector of the hard drive.

3. Compile the rest of the code (head.s as the beginning) into system and put it in the next 240sectors of the hard drive.

So that's what the whole path looks like.

So, the code at the 0x90200 in the memory to which we are about to jump is loaded into memory from the beginning of the second sector of the hard disk. At the beginning of the second sector, that is the first line of code in the setup.s file.

So what is this code? We'll talk about it later, but let's open the setup.s file first.

Start: mov ax,#0x9000; this is done in bootsect already, but mov ds,ax mov ah,#0x03; read cursor pos xor bh,bh int 0x10; save it in known place, con_init fetches mov [0], dx; it from 0x90000. Well, so far, do you think, I go, the front compilation is placed in the location of the hard disk, and the jump address written in the back code is so strongly coupled? What if the whole thing is wrong.

Yeah, that's it. What do you think? At the beginning of the establishment of the operating system, it is completely self-arranged before and after the relationship, can not deviate a byte, it is so strong coupling, need to be careful, the brain needs to stay awake all the time, plan where the code you write will be compiled and stored in the hard disk, and then it will be loaded into the memory location, can not be confused.

But this is also very beneficial, that is, at this stage, you fully know how every step of the jump, every step of the data access is designed and planned, there is no black box.

Unlike when we write high-level languages, we have no idea how much work the bottom has done for us. Although this frees programmers from worrying about the underlying details, it is annoying when they encounter problems or want to know the principle. So cherish this stage!

Moreover, the reason why you can be so freewheeling in the upper layer, many of the bottom details do not need to be considered at all, and it is very easy to worry, precisely because the underlying code like today and in each chapter after that has laid the groundwork carefully.

Well, that's the end of this article. This also marks that we have finished the first operating system source file bootsect.s, and began to move on to the next file, setup.s!

The world behind is becoming more and more wonderful. If you want to know what happens in the future, let's listen to the decomposition next time.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

IT Information

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report