📜 ⬆️ ⬇️

How to start and do not stop writing OS

Another bike Reading Habr during the last two years, I have seen only a few attempts to develop the OS (if specifically: from pehat and iley users (postponed indefinitely) and Igor1024 (not abandoned, but so far more like the description of the work of protected mode x86-compatible processors, which undoubtedly also needs to be known for writing an OS under x86; and a description of the finished system from alman (though not from scratch, although there is nothing bad about it, maybe even vice versa)). For some reason I think that almost all system programmers (and some of them are applied) at least once, but thought about writing their own operating system. In this connection, 3 OS from a large community of this resource seems a ridiculous number. Apparently, most people who think about their own OS do not go further than that, and a small part stops after writing the bootloader, few write kernel pieces, and only hopelessly hard-nosed ones create something remotely resembling an OS (if compared with something like Windows / Linux) . There are many reasons for this, but the main thing in my opinion is that people abandon the development (some even fail to start) due to the small number of descriptions of the process of writing and debugging the OS, which is quite different from what happens during development application software.

I would like to show this small note that, if I start correctly, there is nothing particularly difficult in developing my own OS. Under the cut is a brief and fairly general guide to action on writing the OS from scratch.


How not to start

Don't do that Please do not take the following text as an obvious criticism of someone's articles or manuals on writing the OS. It’s just too often in such articles under loud headlines the emphasis is on the implementation of some minimal workpiece, and it is served as a prototype of the core. In fact, you should think about the structure of the kernel and the interaction of parts of the OS as a whole, and consider that prototype as a standard “Hello, World!” - an application in the world of application software. As a small excuse for these remarks, it should be said that below there is a subsection “Hello, World!”, Which in this case is given exactly as much attention as needed, and no more.
')
Do not write a bootloader. Smart people came up with the Multiboot Specification , implemented and described in detail what it is and how to use it. I do not want to repeat myself, I just say that it works, makes life easier, and it should be used. Specification, by the way, it is better to read completely, it is small and even contains examples.

No need to write the OS completely in assembler. It's not that bad, rather the opposite - fast and small programs will always be in high esteem. Just because this language requires much more development, the use of assembler will only lead to a decrease in enthusiasm and, as a result, to throwing the OS sources into a long box.

No need to load a custom font in the video memory and display something in Russian. No sense from this. It is much easier and more versatile to use English, and leave the font change for later, loading it from the hard disk through the file system driver (at the same time there will be an additional incentive to do more than just start).

Training

preparations To begin with, as always, you should familiarize yourself with the general theory in order to have some ideas about the forthcoming amount of work. Good sources on this issue are the books of E. Tanenbaum, which have already been mentioned in other articles on writing OS on Habré. There are also articles describing existing systems, and there are various guides / newsletters / articles / examples / sites with a bias in OS development, links to some of which are given at the end of the article.

After the initial educational program, you need to decide on the main issues:

Further it is necessary to deepen knowledge according to the chosen and in the following areas:

Instruments

tools Considering the chosen language and development tools, you should choose such a set of utilities and their settings, which in future will allow, by writing scripts, to simplify and speed up the assembly, preparing the image and launching the virtual machine with the project as much as possible. Let us dwell in more detail on each of these points:

Hello, World!

Hello, World! If all the previous steps are completed, you should write a minimal program that will boot as a kernel and display something on the screen. In case of inconvenience or shortcomings of the chosen means, it is necessary to eliminate them (shortcomings), or, at worst, take them for granted.

In this step, you need to test as many features of the development tools that you plan to use in the future. For example, loading modules into GRUB or using a virtual machine with a physical disk / partition / flash drive instead of an image.

After this stage is successful, the real development begins.

Providing run-time support

runtime Since it is proposed to write in high-level languages, care should be taken to provide support for some of the language tools that are usually implemented by the authors of the compiler package. For example for C ++, these include:

When writing “Hello, World!”, The absence of these functions may not be felt in any way, but as you add code, the linker will start complaining about unmet dependencies.

Naturally, you should immediately mention the standard library. A full implementation is not necessary, but the main subset of functions is worth realizing. Then writing code will be much more familiar and faster.

Debugging

debugging Do not look at debugging at the end of the article. In fact, this is a very serious and difficult question in the development of the OS, since the usual means are not applicable here (with some exceptions).

Can advise the following:

Without the debugger built into the kernel, finding errors has a very real chance of becoming a nightmare. So from its writing at some stage of development just can not escape. And since this is unavoidable, it is better to start writing it in advance and thus significantly facilitate development and save a lot of time. It is important to be able to implement the debugger in a manner independent of the kernel, so that debugging has a minimal effect on the normal operation of the system. Here are some types of commands that may be helpful:

Development

devel Next, you need to write and debug the basic elements of the OS, which at the moment should ensure its stable operation, and in the future - easy extensibility and flexibility. In addition to memory managers / processes / (something else), the interface of drivers and file systems is very important. Their design should be approached with great care, taking into account the variety of types of devices / FS. Over time, they can of course be changed, but this is a very painful and error-prone process (and debugging the kernel is not an easy task), so just remember - think about these interfaces at least ten times before you take up their implementation.

Similarity SDK

Sdk As the project develops, new drivers and programs should be added to it. Most likely, some common features (directory structure, assembly management files, specification of dependencies between modules, duplicate code in main or in system request handlers (for example, if the drivers themselves check their compatibility with the device )). If so, then this is a sign of the need to develop templates for various types of programs for your OS.

There is no need for documentation describing the process of writing one or another type of program. But it is worth making a blank of standard elements. This will not only make it easier to add programs (which you can do by copying existing programs and then changing them, but it will take more time), but it will also make it easier to update them with changes in interfaces, formats or something else. It is clear that such changes ideally should not be, but since the development of the OS is not a typical thing, there are a lot of places for potentially wrong decisions. But the understanding of the fallacy of the decisions taken will always come some time after their implementation.

Next steps

continue In short, then: read about the operating systems (and first of all, about their device), develop your system (the pace is not really important, the main thing is not to stop at all and return to the project from time to time with new forces and ideas) and it is natural to correct errors in it (in order to find which it is sometimes necessary to start the system and “play” with it). Over time, the development process will become easier and easier, mistakes will occur less frequently, and you will be enrolled in the list of "hopelessly hard-nosed", those few who, despite some absurdity in the idea of ​​developing their own OS, have done so.

useful links

On Habré:
Writing Your OS: Release 1
Writing Your OS: Release 2
We continue to write OSes. Step by step
What is Protected Mode and what it is eaten with
We learn system of paging addressing and interrupt handling
We start talking about multitasking
Memory and tasks

Other sources with a lot of information on OS development:
wasm.ru
sysbin.com
wiki on osdev.org
iakovlev.org
Bona fide os

Source: https://habr.com/ru/post/129176/


All Articles