📜 ⬆️ ⬇️

Writing OS: Multitasking

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

Well, you probably can't 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:
')
unsigned short hz = 119; outportb(0x43, 0x34); outportb(0x40, (unsigned char)hz & 0xFF); //Low outportb(0x40, (unsigned char)(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:
 typedef struct _pralloc { void * addr; struct _pralloc * next; } processAlloc; typedef struct { void * entry; processAlloc *allocs; } ELF_Process; typedef struct __attribute__((packed)) _E { unsigned int eax;//4 unsigned int ebx;//8 unsigned int ecx;//12 unsigned int edx;//16 unsigned int ebp;//20 unsigned int esp;//24 unsigned int esi;//28 unsigned int edi;//32 unsigned int eflags;//36 unsigned int state;//40 void * startAddr;//44 void * currentAddr;//48 void * stack;//52 unsigned int sse[4 * 8];// unsigned int mmx[2 * 8];//244 unsigned int priority;//248 unsigned int priorityL;//252 void * elf_process;//256 char ** argv;//260 unsigned int argc;//264 unsigned int runnedFrom;//268 char * workingDir;//272 unsigned int cs;//276 - pop is 4 byte in IRET unsigned int 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.
 void addProcessAlloc(ELF_Process * p, void * addr) { void * z = p->allocs; p->allocs = malloc_wo_adding_to_process(sizeof(processAlloc)); p->allocs->addr = addr; 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 that you saved to the stack will be at ESP, the penultimate - ESP +4, etc.:
image
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!

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


All Articles