diff --git a/include/arch.h b/include/arch.h index d5ffd02..d704f85 100644 --- a/include/arch.h +++ b/include/arch.h @@ -24,6 +24,7 @@ typedef struct task { uint64_t cpu_time; uint64_t cpu_time_expired; uint64_t id; + char *id_str; void *stack; struct task *last; @@ -59,12 +60,15 @@ typedef void (*int_entry_t)(struct frame *state); extern lock_t task_lock; extern uint64_t task_f_init; +extern task_t *current_task; void arch_init( ); void task_init( ); +void task_after_init( ); void task_switch( ); -uint64_t task_new_thread(void (*func)(void *)); +uint64_t task_new_thread(void (*func)(void *), char *name); void task_del_current( ); +void task_del(uint64_t id); void cpu_init( ); void pic_init( ); void pit_init( ); diff --git a/include/sys.h b/include/sys.h index 81cf567..a83d629 100644 --- a/include/sys.h +++ b/include/sys.h @@ -76,7 +76,7 @@ typedef struct { sys_info_t *(*get_info)( ); module_info_t *(*get_module)(char *module_id); module_info_t *(*mod_list_get)(uint64_t *count); - uint64_t (*new_thread)(void (*func)(void *)); + uint64_t (*new_thread)(void (*func)(void *), char *name); void (*delete_thread)( ); time_t (*get_time)( ); } __attribute__((packed)) env_t; diff --git a/kernel/mem.c b/kernel/mem.c index 833f87b..c3c0fc9 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -24,7 +24,8 @@ static volatile struct limine_hhdm_request hhdm_request = { .id = LIMINE_HHDM_RE struct mem_entry { struct mem_entry *next; bool free; - size_t size; + uint64_t task_id; + uint64_t size; uint8_t data[0]; }; @@ -47,6 +48,9 @@ static uint64_t highest = 0; // Количество записей в карте памяти static uint64_t mmmap_count = 0; +extern task_t *current_task; +extern uint64_t full_init; + static const char memory_types[8][82] = { "Доступно", "Зарезервировано", "ACPI, можно освободить", "ACPI NVS", "Плохая память", "Загрузчик, можно освободить", "Ядро и модули", "Буфер кадра" }; @@ -60,11 +64,11 @@ void mem_dump_memory( ) { while (curr) { if (curr->next) { - LOG("->0x%x | %u мегабайт | %s | 0x%x\n", &curr->data, (curr->size) / 1024 / 1024, - curr->free ? memory_types[0] : memory_types[1], curr->next); + LOG("->0x%x | %u мегабайт | %s | 0x%x | поток %u\n", &curr->data, (curr->size) / 1024 / 1024, + curr->free ? memory_types[0] : memory_types[1], curr->next, curr->task_id); } else { - LOG("->0x%x | %u мегабайт | %s | Это последний блок\n", &curr->data, (curr->size) / 1024 / 1024, - curr->free ? memory_types[0] : memory_types[1]); + LOG("->0x%x | %u мегабайт | %s | поток %u | Это последний блок\n", &curr->data, (curr->size) / 1024 / 1024, + curr->free ? memory_types[0] : memory_types[1], curr->task_id); } curr = curr->next; } @@ -153,7 +157,12 @@ static void merge_blocks(mem_entry_t *start) { mem_entry_t *block = start; while (block->next && block->next->free) { block->size += block->next->size + sizeof(mem_entry_t); - block->next = block->next->next; + + if (block->next->next) { + block->next = block->next->next; + continue; + } + block->next = NULL; } } @@ -196,33 +205,37 @@ static void *alloc_align(size_t size, size_t alignment) { mem_entry_t *curr = first_node; while (curr) { - if (curr->free) { + if (curr->free && curr->size >= (alignment + sizeof(mem_entry_t) + size)) { void *addr = curr->data + alignment - 1; addr -= (uintptr_t)addr % alignment + sizeof(mem_entry_t); mem_entry_t *second = (mem_entry_t *)addr; - if (curr->size >= (second->data - curr->data + size)) { - mem_entry_t *third = (mem_entry_t *)(second->data + size); - third->size = curr->size - (third->data - curr->data); - third->next = curr->next; - third->free = 1; + mem_entry_t *third = (mem_entry_t *)(second->data + size); + tool_memset(third, 0, sizeof(mem_entry_t)); - second->size = size; - second->next = third; - second->free = 0; + third->size = curr->size - (third->data - curr->data); + third->next = curr->next; + third->free = 1; - if (curr != second) { - curr->next = second; - curr->size = (uintptr_t)second - (uintptr_t)curr->data; - curr->free = 1; - } + second->size = size; + second->next = third; + second->free = 0; + second->task_id = 0; - return second->data; + if (full_init) { second->task_id = current_task->id; } + + if (curr != second) { + curr->next = second; + curr->size = (uintptr_t)second - (uintptr_t)curr->data; + curr->free = 1; } + + return second->data; } curr = curr->next; } + return NULL; } diff --git a/kernel/start.c b/kernel/start.c index 20d8864..a44bf5a 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -16,11 +16,16 @@ #include uint64_t full_init = 0; +uint64_t dum = 0; void finally( ) { LOG("Готово! Для выхода из симуляции удерживайте: ESCAPE\n"); - mod_after_init( ); - for (;;) { task_switch( ); } + for (;;) { asm volatile("hlt"); } +} + +void dummy( ) { + LOG("Поток %u\n", dum++); + task_del(current_task->id); } // Точка входа @@ -32,6 +37,8 @@ void _start( ) { fb_init( ); log_init_mem( ); arch_init( ); + pit_init( ); + task_init( ); mod_init( ); LOG("\t\t\t\t *** Базовая Модульная Платформа Операционных Систем " @@ -41,16 +48,22 @@ void _start( ) { LOG("\t\t\t\t *** Дата сборки: %s %s ***\n", __DATE__, __TIME__); time_t time = rtc_get_time( ); - LOG("Время: %2u:%2u.%2u, %2u.%2u.%2u\n", time.hours, time.minutes, time.second, time.day, time.month, time.year); + LOG("Время: %u:%u.%u, %u.%u.%u\n", time.hours, time.minutes, time.second, time.day, time.month, time.year); - pit_init( ); - task_init( ); - - task_new_thread(finally); + task_new_thread(finally, "fin"); full_init = 1; task_f_init = 1; + task_after_init( ); + + for (uint64_t i = 0; i < 6; i++) { + char *buf = mem_alloc(32); + tool_strcpy(buf, "dum"); + tool_uint_to_str(i, 10, buf + 3); + task_new_thread(dummy, buf); + } + asm volatile("sti"); for (;;) { asm volatile("hlt"); } diff --git a/modlib/lib/tool.c b/modlib/lib/tool.c index 1a9f241..f56548f 100644 --- a/modlib/lib/tool.c +++ b/modlib/lib/tool.c @@ -189,9 +189,7 @@ char *trstr(char *str, char sym) { else left = 0x00U; - char *res = alloc(size); - memcpy(res, str + left, size); - return res; + return str + left; } char *strdup(char *str) { diff --git a/modules/ps2/main.c b/modules/ps2/main.c index 8600363..aceccce 100644 --- a/modules/ps2/main.c +++ b/modules/ps2/main.c @@ -7,7 +7,7 @@ static int ru = 1; static char c_char = '\0'; static key_event_t keyboard_buffer; -static void virt_exit( ) { +void virt_exit( ) { fb_printf("Выход для Bochs\n"); outw(0xB004, 0x2000);