Good day, dear reader, most likely, you saw my previous article that you can write a working OS yourself in a relatively short period of time. Well, today we will talk about the implementation of multitasking in my OS.

Well, you most likely can not imagine a single-task OS in 2018, that's why I decided to talk about the implementation of multitasking in my OS. And so, first - you need to decide on the type of multitasking, I chose displacing.
What is she like? Preemptive multitasking is a system for distributing the processing power of the processor between processes: each has its own time slice, each has its own priority. And the first problem is which quantum in length to choose, how to stop the process after the quantum expires? In fact, everything is easier than ever! We will use PIT with initially set frequency of 10026 with interrupt kopecks per second, right there we solve another problem: we are already stopping the previous process. And so, let's start with PIT.

## PIT

PIT - Programmable Interval Timer - a counter that, upon reaching a programmed number of increments, generates a signal. Also, with the help of this timer, you can squeak a squeak in a computer (the thing that squeaks after passing the test devices). And so, he believes with a frequency of 1193182 hertz, this means that we need to program it to 119 (1193182/119 is approximately equal to 10026). For this you need to send 2 bytes to the port of the first generator, first the low byte, and then the high byte:

``````unsignedshort hz = 119;
outportb(0x43, 0x34);
outportb(0x40, (unsignedchar)hz & 0xFF); //Low
outportb(0x40, (unsignedchar)(hz >> 8) & 0xFF); //Hight, about 10026 times per second``````

Now it’s time to start programming the PIT interrupt, it has IRQ 0, and after PIC's remap it will be 0x20m. For the IRQ of the first PIC, I wrote this macro:

``````//PIC#0; port 0x20#define IRQ_HANDLER(func) char func = 0x90;\
__asm__(#func ": \npusha \n call __"#func " \n movb \$0x20,\
%al \n outb %al, \$0x20 \n popa  \n iret \n");\
void _## func()``````

## Structure and processes

And so, as you understand, we need to develop a structure for each process, as well as a structure that allows us to remember all my allocations of memory.
This is what I have:
``````typedefstruct _pralloc
struct _pralloc * next;
} processAlloc;
typedefstruct
{void * entry;
processAlloc *allocs;
} ELF_Process;
typedefstruct __attribute__((packed)) _E {unsignedint eax;//4unsignedint ebx;//8unsignedint ecx;//12unsignedint edx;//16unsignedint ebp;//20unsignedint esp;//24unsignedint esi;//28unsignedint edi;//32unsignedint eflags;//36unsignedint state;//40void * startAddr;//44void * currentAddr;//48void * stack;//52unsignedint sse[4 * 8];//unsignedint mmx[2 * 8];//244unsignedint priority;//248unsignedint priorityL;//252void * elf_process;//256char ** argv;//260unsignedint argc;//264unsignedint runnedFrom;//268char * workingDir;//272unsignedint cs;//276 - pop is 4 byte in IRETunsignedint ds;//280
} Process;
``````

First, we need to understand the following: we can somewhere at the global address, for example, by 0xDEAD, put the number of the current running process, then when executing any code we can be sure that we have the number of the currently running process, this means that when we call malloc, we know who we allocate memory for, and we can immediately add the address of the allocated memory to the allocs list.
``````voidaddProcessAlloc(ELF_Process * p, void * addr){
void * z = p->allocs;
p->allocs->next = z;
}``````

Well, we wrote the structure of the table with the description of the processes, what next, how to switch tasks?
First I want to note that for example, in the handler, local variables are stored on the stack, which means that after entering the handler, the compiler spoils us with esp. To prevent this from happening, we will create a variable with an absolute address, and before calling the handler, we will push ESP there. In the handler, we need to send the EOI to the first PIC and find the process to which we need to switch (I will not describe the priority mechanism: it is as simple as a traffic jam). Next - we need to save all the registers and flags of the current process, so immediately before putting ESP into a variable, we save all registers (including segment ones) onto the stack. In the handler itself, we very carefully need to remove them from the stack, just saving the flags and return address. I want to note that the stack grows up (ie, ESP decreases), this means that the last register,

Now it remains for us to shove into the registers the values ​​of the registers of the process to which we switched and execute IRET. Profit!

## Running processes

When starting the process, we only need to allocate a stack for the process, after which we put argc and argv into it, the address of the function that will be given control after the process is completed. You also need to set the processor flags to the desired value, for example, for my OS it is 0x216, you can read about the register of flags on wikipedia.

Finally, I would like to wish you success, in a short time I will write about the work with memory and other articles of interest to you.
Good luck, and ethical hacking!