Implement your own operating system. Part 3

Source: Internet
Author: User

3-1

Integration with C functions.

The sequence of OBJ after LD-ttext org seems to be exquisite.

Impact of the Call Command on the stack:
In the 16-bit real-world mode, Cs and IP are pressed to the stack, so the stack is reduced by 4.
In the 32-bit protection mode, if it is called within a segment, only the EIP is pushed to the stack. If it is called between segments, the EIP and Cs are pushed to the stack, however, we have not encountered this situation yet.

C function call conventions
The parameters are pushed from the right to the left to the stack. After a moderate call to the Assembly, you must manually reset the ESP value.
When the returned value is 4 bytes, it is transmitted through eax; 2 bytes are transmitted through ax; 1 byte is passed through Al.

3-2

The row and column values of the cursor are changed to global variables.

In the Assembly, if a symbolic variable is input to the stack, the pointer to this variable will be input to the stack.
However, when the global variable declared in assembly is called in C language, the variable itself is obtained instead of its pointer.

When using Global to declare a global variable in the Assembly, the global declaration should be placed in the corresponding data or BSS segment.

3-3

Implements initialization of the interrupt function.

Interrupt functions and functions are different concepts.
The latter can be called directly, but the former cannot.

From the Assembly level, arrays and pointers are different.
For example:
Assembly:
Global g_idtr
G_idtr:
% Rep 256
Dd 0
Dd 0
% Endrep

In C, if the statement is as follows:
Extern long g_idtr [256];
Then you can operate normally.

However, if you use
Extern long * g_idtr;
Then the program will go wrong.

The reason is that when a pointer is used, the program will allocate a space to save the value of the pointer. When an array is used, this label is not saved as a variable.

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.

When an interruption occurs in protected mode, the system automatically switches off after the door is interrupted, and the interruption is automatically enabled in the iret.

It took a lot of time to define outb and other Macros in Io. h.

On x86, pointers are differentiated from assembly commands such as in and out. Pointers cannot be used for port operations, which is different from the arm structure.

The memory segment starting with 0xb8000 is mapped by BIOS, so you can use the pointer. This is only a special case.

3-8

The cursor follows and moves.

If-O optimization is specified during compilation, be careful when using ASM embedded assembly.
That is, when the parameter in the Assembly is specified as any register and the Assembly itself uses registers, conflicts may occur.
For example:
# Define outb (port, value )/
_ ASM _ volatile __(/
"Movb % 1, % Al/N "/
"Movw % 0, % dx/N "/
"Outb % Al, % dx/N "/
: "R" (port), "R" (value )/
)
It can be run when the number of parameters is immediate, but an error occurs when the parameter is a variable.
Cause: both internal assembly and calling parameters use Al to store data.
If "R" is specified as "M", it can also be successful, but it is not ruled out that problems will arise due to optimization in the future,
So the current solution: No optimization is used.

3-9

Screen scrolling is implemented, but currently only one console is supported.

It is planned to implement a total of four consoles, each taking up one page.
All the consoles can be mapped to the current console, which occupies 4 pages.

3-10

Read the keyboard.

INB and other functions have also been implemented.

3-11

Implements switching between different permissions.

Note that the LDT command must be replaced with GDTR in the kernel before it can be used. This wastes 5 hours.

The next step is to add a system call.

3-12

Implemented a simple trap.

0x80 in Linux is a trap door. It is used to complete system call operations, rather than call the door.
Therefore, the system will not implement the call gate.

Like the interrupt door, the trap door also uses iret for return. The difference is that CLD is automatically triggered when the door is disconnected.

When users enter the privileged level, only CS and SS belong to the current level, and other segment registers still belong to the user level. Therefore, you need to manually change them,
This is done by the saveall macro in Linux.

Note that only DS and ES, FS, and GS reserve original values are changed in saveall, and the last two seem to never need them.

Currently, interruption cannot be nested, but can be interrupted in the trap door.

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.

The most critical aspect of process switching is that the kernel state stack is always at the same offset when the system returns to the user space.
Based on this, you can determine whether the CPU is returned to the user space or only nested return in the system space.
With this understanding, it is not very difficult to switch the process.

In addition, the ESP in TSS should be used when the system enters the kernel space. This value must be set in advance during process switching.

So far, the compilation of the operating system has come to an end.

Code address:

Http://d.download.csdn.net/filedown2008/aHR0cDovL2RsMi5jc2RuLm5ldC9kb3duNC8yMDA4MDMzMC8zMDIxMzU0MTgyMi56aXA=! 398454

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.