From adf6d4a8b9ae9cc5ebd237591e77cd70f5dfda88 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Sat, 27 Jan 2024 22:03:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/limine.cfg | 5 +++- include/arch.h | 1 + include/sys.h | 2 +- kernel/cpu/task.c | 21 ++++++++++------ kernel/mod.c | 7 +++--- kernel/start.c | 2 +- kernel/sys.c | 12 ++------- modlib/lib/system.c | 2 +- modlib/modstd.h | 1 + modlib/system.h | 2 +- modlib/types.h | 2 +- modules/imfs/main.c | 10 +------- modules/ios/build.sh | 17 +++++++++++++ modules/ios/main.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 105 insertions(+), 37 deletions(-) create mode 100644 modules/ios/build.sh create mode 100644 modules/ios/main.c diff --git a/configs/limine.cfg b/configs/limine.cfg index 452715b..aae80a7 100644 --- a/configs/limine.cfg +++ b/configs/limine.cfg @@ -44,4 +44,7 @@ TERM_WALLPAPER=boot:///mod/boot.jpg MODULE_CMDLINE=[MOD]imfs.ko MODULE_PATH=boot:///mod/hello.ko - MODULE_CMDLINE=[MOD]hello.ko \ No newline at end of file + MODULE_CMDLINE=[MOD]hello.ko + + MODULE_PATH=boot:///mod/ios.ko + MODULE_CMDLINE=[MOD][IO]ios.ko \ No newline at end of file diff --git a/include/arch.h b/include/arch.h index a1a656d..dadf4ef 100644 --- a/include/arch.h +++ b/include/arch.h @@ -64,6 +64,7 @@ void arch_init( ); void task_init( ); void task_switch( ); uint64_t task_new_thread(void (*func)(void *)); +void task_del_current( ); void cpu_init( ); void gdt_init( ); void pic_init( ); diff --git a/include/sys.h b/include/sys.h index b2dc9d9..ebd5562 100644 --- a/include/sys.h +++ b/include/sys.h @@ -77,7 +77,7 @@ typedef struct { module_info_t *(*get_module)(char *module_id); module_info_t *(*mod_list_get)(uint64_t *count); uint64_t (*new_thread)(uint64_t func); - int (*delete_thread)(uint64_t thread_id); + void (*delete_thread)( ); time_t (*get_time)( ); } __attribute__((packed)) env_t; diff --git a/kernel/cpu/task.c b/kernel/cpu/task.c index 862067a..dcba366 100644 --- a/kernel/cpu/task.c +++ b/kernel/cpu/task.c @@ -79,9 +79,19 @@ uint64_t task_new_thread(void (*func)(void *)) { return new_task->id; } -void dummy( ) { - LOG("\t\tПривет! Я поток: %u\n", current_task->id); - for (;;) { task_switch( ); } +void task_del_current( ) { + LOG("Удаление потока ID: %u\n", current_task->id); + task_t *prev = current_task->last; + task_t *next = current_task->next; + + prev->next = next; + next->last = prev; + + mem_free(current_task->stack); + mem_free(current_task); + + current_task = next; + task_switch( ); } void task_init( ) { @@ -116,10 +126,5 @@ void task_init( ) { last_task = kernel_task; - LOG("Создание потока dummy\n"); - task_new_thread(dummy); - - test_buf = mem_alloc(8 * 8 * sizeof(uint32_t)); - LOG("Потоки инициализированы\n"); } diff --git a/kernel/mod.c b/kernel/mod.c index 1654669..6ec17cb 100644 --- a/kernel/mod.c +++ b/kernel/mod.c @@ -62,8 +62,7 @@ void mod_after_init( ) { for (uint64_t i = 0; i < modules_count; i++) { if (module_list[i].after_init != 0) { LOG("%s.after_init( );\n", module_list[i].name); - module_list[i].after_init( ); - LOG("%s.after_init( );\n", module_list[i].name); + task_new_thread(module_list[i].after_init); } } } @@ -75,7 +74,7 @@ module_info_t *mod_list_get(uint64_t *count) { module_info_t *mod_find(char *tag) { for (uint64_t i = 0; i < modules_count; i++) { - if (tool_starts_with(module_list[i].name, tag)) { return &module_list[i]; } + if (tool_str_contains(module_list[i].name, tag)) { return &module_list[i]; } } return (module_info_t *)NULL; @@ -128,7 +127,7 @@ void mod_init( ) { continue; } - module_info_t (*module_init)(env_t *env) = + module_info_t (*module_init)(env_t * env) = (module_info_t(*)(env_t * env)) elf_entry((elf64_header_t *)module_ptr->address); // LOG("\t->Точка входа: 0x%x\n", module_init); diff --git a/kernel/start.c b/kernel/start.c index 917f673..7ef638c 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -19,6 +19,7 @@ uint64_t full_init = 0; void finally( ) { LOG("Готово! Для выхода из симуляции удерживайте: ESCAPE\n"); + mod_after_init( ); for (;;) { task_switch( ); asm volatile("hlt"); @@ -35,7 +36,6 @@ void _start( ) { log_init_mem( ); arch_init( ); mod_init( ); - mod_after_init( ); LOG("\t\t\t\t *** Базовая Модульная Платформа Операционных Систем " "версии %u.%u.%u %s***\n", diff --git a/kernel/sys.c b/kernel/sys.c index 2ac029c..0aff84c 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -49,14 +49,6 @@ static module_info_t *sys_get_module(char *module_id) { return (module_info_t *)mod_find(module_id); } -static uint64_t sys_new_thread(uint64_t func) { - return func; -} - -static int sys_delete_thread(uint64_t thread_id) { - return thread_id; -} - env_t *sys_install(env_t *module) { module->fb_printf = &log_printf; module->alloc_framebuffer = &sys_alloc_framebuffer; @@ -68,8 +60,8 @@ env_t *sys_install(env_t *module) { module->get_info = &sys_get_info; module->get_module = &sys_get_module; module->mod_list_get = &mod_list_get; - module->new_thread = &sys_new_thread; - module->delete_thread = &sys_delete_thread; + module->new_thread = &task_new_thread; + module->delete_thread = &task_del_current; module->get_time = &rtc_get_time; return module; diff --git a/modlib/lib/system.c b/modlib/lib/system.c index 26023ac..a09575f 100644 --- a/modlib/lib/system.c +++ b/modlib/lib/system.c @@ -20,7 +20,7 @@ void (*exit)(int code); int (*get_error)( ); sys_info_t *(*get_info)( ); uint64_t (*new_thread)(uint64_t func); -int (*delete_thread)(uint64_t thread_id); +void (*delete_thread)( ); time_t (*get_time)( ); uint64_t offset; diff --git a/modlib/modstd.h b/modlib/modstd.h index 594ea90..3d83bf8 100644 --- a/modlib/modstd.h +++ b/modlib/modstd.h @@ -22,6 +22,7 @@ int strcmp(const char *s1, const char *s2); char *trstr(char *str, char sym); char *strdup(char *str); size_t count_chars(char *str, char c); +uint64_t str_contains(const char *str, const char *substr); void memcpy(void *dest, void *src, size_t n); void *memset(void *ptr, uint8_t n, size_t size); void *memmove(void *dest, void *src, size_t n); diff --git a/modlib/system.h b/modlib/system.h index 44f572a..f61cda1 100644 --- a/modlib/system.h +++ b/modlib/system.h @@ -23,7 +23,7 @@ extern void (*exit)(int code); extern int (*get_error)( ); extern sys_info_t *(*get_info)( ); extern uint64_t (*new_thread)(uint64_t func); -extern int (*delete_thread)(uint64_t thread_id); +extern void (*delete_thread)( ); extern time_t (*get_time)( ); extern uint64_t offset; diff --git a/modlib/types.h b/modlib/types.h index 2e1ec54..5aa0483 100644 --- a/modlib/types.h +++ b/modlib/types.h @@ -118,7 +118,7 @@ typedef struct { module_info_t *(*get_module)(char *module_id); module_info_t *(*mod_list_get)(uint64_t *count); uint64_t (*new_thread)(uint64_t func); - int (*delete_thread)(uint64_t thread_id); + void (*delete_thread)( ); time_t (*get_time)( ); } __attribute__((packed)) env_t; diff --git a/modules/imfs/main.c b/modules/imfs/main.c index fca5a65..1cb316d 100644 --- a/modules/imfs/main.c +++ b/modules/imfs/main.c @@ -146,16 +146,8 @@ static void main( ) { uint64_t *mod_count = alloc(sizeof(uint64_t)); module_info_t *mod_list = mod_list_get(mod_count); fb_printf("Модулей: %u\n", *mod_count); - for (uint64_t i = 0; i < *mod_count; i++) { - if (mod_list[i].data_size > 0) { - fb_printf("Модуль: %s\n", mod_list[i].name); - add_file(mod_list[i].name, "datafile", mod_f, mod_list[i].data, mod_list[i].data_size); - } - } - fb_printf("Модулей: %u\n", *mod_count); - print_folder_contents(root_folder, 0); free(mod_count); - fb_printf("Модулей: %u\n", *mod_count); + delete_thread( ); } module_info_t __attribute__((section(".minit"))) init(env_t *env) { diff --git a/modules/ios/build.sh b/modules/ios/build.sh new file mode 100644 index 0000000..0b56286 --- /dev/null +++ b/modules/ios/build.sh @@ -0,0 +1,17 @@ +#/bin/sh +echo "Название: IOS" +echo "Лицензия: CC BY-NC 4.0" + +CC="gcc" +ARCH_FLAGS="-ffreestanding -O0 -g -fPIC -static -nostdlib " + +if [ -d "../../sdk" ]; then + CC="../../sdk/bin/x86_64-elf-gcc" +fi + + +$CC $ARCH_FLAGS -I../../modlib -finput-charset=UTF-8 -fexec-charset=cp1251 -c main.c -o ios.o +$CC $ARCH_FLAGS -Wl,--entry=init,--build-id=none -T ../link.ld ios.o -L../../modlib/lib/ -lmod -o ios.ko + +cp ios.ko ../bin/ +echo "Сборка завершена, файл: ios.ko" diff --git a/modules/ios/main.c b/modules/ios/main.c new file mode 100644 index 0000000..51a4948 --- /dev/null +++ b/modules/ios/main.c @@ -0,0 +1,58 @@ +#include + +static module_info_t *mod_list = NULL; +static uint64_t *mod_count = NULL; +static uint64_t app_count = 0; +static module_info_t *app_list = NULL; + +static void main( ) { + fb_printf("IOS (input-output shell) - оболочка ввода-вывода\n"); + mod_count = alloc(sizeof(uint64_t)); + mod_list = mod_list_get(mod_count); + + app_list = alloc((*mod_count) * sizeof(module_info_t)); + + if (app_list == NULL) { + fb_printf("Ошибка выделения памяти для app_list!\n"); + delete_thread( ); + } + + app_count = 0; + + for (uint64_t i = 0; i < *mod_count; i++) { + if (str_contains(mod_list[i].name, "[APP]")) { + fb_printf("%u. %s\n", app_count, mod_list[i].name); + app_list[app_count] = mod_list[i]; + app_count++; + } + } + + free(mod_count); + + if (app_count < 1) { + fb_printf("Модулей-программ не обнаружено!\n"); + free(app_list); + delete_thread( ); + } else { + app_list = realloc(app_list, app_count * sizeof(module_info_t)); + for (;;) { asm volatile("hlt"); } + } + + for (;;) { asm volatile("hlt"); } +} + +module_info_t __attribute__((section(".minit"))) init(env_t *env) { + init_env(env); + + return (module_info_t){ .name = (char *)"[IOS]", + .message = (char *)"IOS (input-output shell) - оболочка ввода-вывода.", + .type = 0, + .data_size = 0, + .data = (void *)0, + .err_code = 0, + .module_id = 0, + .irq = 0, + .irq_handler = 0, + .get_func = 0, + .after_init = main }; +}