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.11V | the last time to toss the memory before entering the protected mode

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

Share

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

This article comes from Weixin Official Accounts: Low Concurrent Programming (ID: dipingfa), Author: Flash Guest

This series will read and appreciate all the core code of Linux 0.11 with a novel mentality, from the code execution sequence after booting, to 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 delicate design, after reading this series, I hope you can sigh, the original operating system source code is this broken thing.

The following is a list of published articles, but you can start with the opening words of this series.

Opening remarks

the first time| The first two lines of code

the second time| Get yourself some space.

the third time| Do the most basic preparation work

Fourth round| Put the rest of yourself on the hard drive into memory

The GitHub address for this series is as follows:

https://github.com/sunym1993/flash-linux0.11-talk

------Start of text------

The book goes back, we said in the last book that the operating system has completed all kinds of loading from hard disk to memory and copying from memory to memory.

At this point, the entire bootsect.s mission is complete, and it is also the first operating system source file we have read. Then jump to the position 0x90200 to start execution. The code at this position is located at the beginning of setup.s. Let's look at it next.

start: mov ax,#0x9000 ; this is done in bootsect already, but mov ds,ax mov ah,#0x03 ; read cursor pos xor bh,bh ; save it in known place, con_init fetches mov [0],dx 0x90000; it is from 0x90000.

If you read the previous article carefully, you can guess what it is going to do. Remember when int 0x13 triggered the BIOS read disk interrupt? The int 0x10 is the same, it also triggers the display service interrupt handler provided by BIOS, and the ah register is assigned to 0x03 to indicate the specific read cursor position function in the display service.

Specific BIOS provides what interrupt services, how to call and get the return value, please look for information by yourself, here only say the results.

When the int 0x10 interrupt program finishes executing and returns, the value in the dx register indicates the cursor position, specifically the upper eight bits dh stores the row number and the lower eight bits dl stores the column number.

The computer automatically initializes to text mode after power-on self-test. In this mode, a screen can display 25 lines of 80 characters each, that is, 80 columns.

The next step mov [0] dx is to store the cursor position at [0]. Note that, as we said earlier, this memory address is just an offset address, plus the segment base stored in the ds register. The final memory address is 0x90000, where the cursor position is stored, so that it can be used later when initializing the console.

So it can also be seen from here that this is no different from our usual call to a method, except that the use of registers here is equivalent to the input parameter and return value, and the 0x10 interrupt number here is equivalent to the method name.

Here again should be said before a sentence, the operating system kernel is also everywhere at the beginning of the BIOS package man, there are ready-made use of it.

The next few lines of code, all the same logic as just now, call a BIOS interrupt to get some information, and then store it somewhere in memory, so we can quickly browse it.

For example, access to memory information. Get memory size (extended mem, kB) mov ah,#0x88 int 0x15 mov [2],ax Gets the graphics display mode. Get video-card data: mov ah,#0x0f int 0x10 mov [4],bx ; bh = display page mov [6],ax ; al = video mode, ah = window width Check display mode and select parameters; check for EGA/VGA and some config parameters mov ah,#0x12 mov bl,#0x10 int 0x10 mov [8],ax mov [10],bx mov [12],cx Get information about the first hard drive. Get hd0 data mov ax,#0x0000 mov ds,ax lds si,[4*0x41] mov ax,#INITSEG mov es,ax mov di,#0x0080 mov cx,#0x10 rep Movsb gets information about the second hard drive. Get hd1 data mov ax,#0x0000 mov ds,ax lds si,[4*0x46] mov ax,#INITSEG mov es,ax mov di,#0x0090 mov cx,#0x10 rep Movsb The above principles are the same.

We don't have to think about it, understanding the operating system doesn't help much. We just need to know what information is finally stored in memory and where it is, and then we will use them later.

Memory Address Length (bytes) Name 0x900002 Cursor Position 0x900022

Extended Memory 0x900042 Display Page 0x900061

Display mode 0x900071 Number of character columns 0x900082 Unknown 0x9000 A1

Display Memory 0x9000 B1

Display Status 0x9000 C2 Graphics Characteristic Parameters 0x9000 E1

Number of rows 0x9000 F1 Number of columns 0x9008016

Hard disk 1 parameter table 0x9009016 Hard disk 2 parameter table 0x901 FC2

root device number

Since C programming language will soon be used, although assembly and C language can also be used in the form of variables to pass data, but this requires the compiler to do some extra work at the time of linking, so so much data is more convenient or both sides agree on a memory address, I save here, you take from here, it's done. This is probably the most primitive and intuitive way to pass variables.

After storing this information, what does the operating system do? Let's move on.

cli No interruptions allowed ; just a line of cli, meaning to close the interruptions.

Because later we have to overwrite the interrupt vector table originally written by BIOS, that is, destroy it and write our own interrupt vector table, so interrupts are not allowed to come in at this time.

Keep looking.

first we move the system to it's rightful place mov ax,#0x0000 cld 'direction'=0, movs moves forwarddo_move: mov es,ax destination segment add ax,#0x1000 cmp ax,#0x9000 jz end_move mov ds,ax source segment sub di,di sub si,si mov cx,#0x8000 jmp do_move; then we load the segment descriptorsend_move: ... See the rep movsw familiar with the back, at the beginning we moved the operating system code from 0x7c00 to 0x90000 when this command is used, to recall the figure.

The same principle as before, also did a memory copy operation, the final result is, the memory address 0x10000 from the beginning to 0x90000 content, all copied to the memory at the beginning of the 0 position, probably is such an effect.

Due to the various loads and copies before, the memory looks very messy, it is time for a wave of trade-offs and tidying up, we re-comb the memory layout at this time.

The address at the top of the stack remains 0x9 FF00 unchanged.

0x90000 The position starting from 0x90000 was originally the code of bootsect and setup program. Now part of the code of bootsect has been overwritten by the operating system in order to record some temporary data stored in memory, hard disk, graphics card, etc.

The 512K of memory from 0 to 0x80000 is occupied by the system module. As mentioned earlier, this system module is the result of linking all programs except bootsect and setup together, which can be understood as all of the operating system.

So the current memory layout is like this.

Okay, just remember the picture above. Is it clear again this time? 0x7c00 is already in the past tense. Forget it and look forward!

Next, we have to do some technical work, that is, the conversion of the mode, from the current 16-bit real mode to the 32-bit protection mode, this is a big project! Also I think this trip to the operating system source code journey, the first quite exciting place, we are ready!

The world behind is getting more and more exciting. If you want to know what will happen next, listen to the next decomposition.

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