fix: simple multitasking work !!!!
add a stack at every task, and execute task while multitasking switching
This commit is contained in:
parent
dc84d5856b
commit
95fec015f2
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@ -9,6 +10,8 @@ extern struct task *current_task;
|
|||||||
enum status { ZOMBIE, THREAD, RUN };
|
enum status { ZOMBIE, THREAD, RUN };
|
||||||
enum owner { OWNER_KERNEL, OWNER_USER };
|
enum owner { OWNER_KERNEL, OWNER_USER };
|
||||||
|
|
||||||
|
#define STACK_SIZE PAGE_SIZE * 4
|
||||||
|
|
||||||
struct task {
|
struct task {
|
||||||
uint32_t *esp;
|
uint32_t *esp;
|
||||||
uint32_t *esp0;
|
uint32_t *esp0;
|
||||||
|
@ -41,6 +41,7 @@ static void clock_handler(struct registers *regs)
|
|||||||
if (scheduler_counter % 10 == 0) {
|
if (scheduler_counter % 10 == 0) {
|
||||||
cli();
|
cli();
|
||||||
scheduler();
|
scheduler();
|
||||||
|
sti();
|
||||||
}
|
}
|
||||||
scheduler_counter++;
|
scheduler_counter++;
|
||||||
sleep_counter--;
|
sleep_counter--;
|
||||||
|
@ -43,8 +43,9 @@ static void owo(void)
|
|||||||
|
|
||||||
static void awa(void)
|
static void awa(void)
|
||||||
{
|
{
|
||||||
|
kprintf("awa\n");
|
||||||
while (true)
|
while (true)
|
||||||
; // kprintf("awa\n");
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
void kernel_main(multiboot_info_t *mbd, uint32_t magic)
|
void kernel_main(multiboot_info_t *mbd, uint32_t magic)
|
||||||
@ -62,5 +63,8 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
|
|||||||
/* "Martin 03:50, 22 March 2009 (UTC)\n"); */
|
/* "Martin 03:50, 22 March 2009 (UTC)\n"); */
|
||||||
create_kernel_task();
|
create_kernel_task();
|
||||||
exec_fn(awa);
|
exec_fn(awa);
|
||||||
|
exec_fn(awa);
|
||||||
|
exec_fn(awa);
|
||||||
|
exec_fn(awa);
|
||||||
shell_init();
|
shell_init();
|
||||||
}
|
}
|
||||||
|
@ -14,41 +14,40 @@ switch_to_task:
|
|||||||
// save the current stack pointer to the old stack
|
// save the current stack pointer to the old stack
|
||||||
mov [eax+0], esp
|
mov [eax+0], esp
|
||||||
|
|
||||||
// save the old eip pointer
|
|
||||||
mov ebx, [[[esp_backup]]]
|
|
||||||
mov [eax+16], ebx
|
|
||||||
|
|
||||||
// stack pointer + the 4 regs pushed
|
// stack pointer + the 4 regs pushed
|
||||||
// and + 1 to get the argument (next task)
|
// and + 1 to get the argument (next task)
|
||||||
mov esi, [esp+(4+1)*4]
|
mov esi, [esp+(4+1)*4]
|
||||||
mov [current_task], esi
|
mov [current_task], esi
|
||||||
|
|
||||||
mov edi, [current_task]
|
mov eax, [current_task]
|
||||||
|
|
||||||
mov esp, [edi+0] // esp
|
mov esp, [eax+0] // get esp
|
||||||
mov eax, [edi+4] // esp0
|
|
||||||
// mov ebx, [edi+8] // cr3
|
|
||||||
// mov edi, [edi+12] // page_directory
|
|
||||||
// mov [page_directory], edi
|
|
||||||
// mov [TSS+4], eax // tss.esp0
|
|
||||||
// mov ecx, cr3
|
|
||||||
|
|
||||||
// if cr3 hasn't change, do nothing
|
|
||||||
// cmp ecx, ebx
|
|
||||||
// je .END
|
|
||||||
// mov cr3, ebx
|
|
||||||
|
|
||||||
|
|
||||||
.END:
|
|
||||||
mov eax, [edi+16]
|
|
||||||
mov ebx, [esp_backup]
|
|
||||||
mov [ebx], eax
|
|
||||||
|
|
||||||
pop esi
|
pop esi
|
||||||
pop edi
|
pop edi
|
||||||
pop ebp
|
pop ebp
|
||||||
pop ebx
|
pop ebx
|
||||||
|
|
||||||
|
|
||||||
|
mov ecx, 0
|
||||||
|
|
||||||
|
cmp [eax+16], ecx
|
||||||
|
je .END
|
||||||
|
|
||||||
sti
|
sti
|
||||||
|
|
||||||
|
push [eax+16] // store func_ptr
|
||||||
|
|
||||||
|
mov [eax+16], ecx // clear eip in current_task
|
||||||
|
|
||||||
|
push 0
|
||||||
|
call pic_send_eoi
|
||||||
|
pop edx // remove pic_send_eoi argument
|
||||||
|
|
||||||
|
pop edx // get func_ptr
|
||||||
|
call edx
|
||||||
|
|
||||||
|
// TODO HANDLE TASK RETURN
|
||||||
|
|
||||||
|
.END:
|
||||||
ret
|
ret
|
||||||
|
@ -19,9 +19,16 @@ static struct task *create_task(uint8_t owner_id)
|
|||||||
new_task->prev = current_task;
|
new_task->prev = current_task;
|
||||||
current_task->next = new_task;
|
current_task->next = new_task;
|
||||||
new_task->owner_id = owner_id;
|
new_task->owner_id = owner_id;
|
||||||
|
new_task->esp0 = alloc_pages(STACK_SIZE, NULL);
|
||||||
|
if (!new_task->esp0) {
|
||||||
|
vfree(new_task);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
new_task->esp = new_task->esp0 + STACK_SIZE;
|
||||||
new_task->pid = pid++;
|
new_task->pid = pid++;
|
||||||
new_task->heap = alloc_pages(4096, (void **)&new_task->cr3);
|
new_task->heap = alloc_pages(4096, (void **)&new_task->cr3);
|
||||||
if (!new_task->heap) {
|
if (!new_task->heap) {
|
||||||
|
free_pages(new_task->esp0, STACK_SIZE);
|
||||||
vfree(new_task);
|
vfree(new_task);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -33,9 +40,11 @@ int create_kernel_task(void)
|
|||||||
struct task *new_task = vmalloc(sizeof(struct task));
|
struct task *new_task = vmalloc(sizeof(struct task));
|
||||||
if (!new_task)
|
if (!new_task)
|
||||||
return -1;
|
return -1;
|
||||||
|
new_task->status = RUN;
|
||||||
new_task->owner_id = 0;
|
new_task->owner_id = 0;
|
||||||
new_task->pid = 0;
|
new_task->pid = 0;
|
||||||
new_task->heap = page_directory;
|
new_task->heap = page_directory;
|
||||||
|
new_task->cr3 = page_directory - KERNEL_START;
|
||||||
new_task->prev = new_task;
|
new_task->prev = new_task;
|
||||||
new_task->next = new_task;
|
new_task->next = new_task;
|
||||||
current_task = new_task;
|
current_task = new_task;
|
||||||
|
Loading…
Reference in New Issue
Block a user