From 2ba30e85c68d14dc0447639dac31265757d14a17 Mon Sep 17 00:00:00 2001 From: Aren Date: Wed, 25 Oct 2023 00:07:32 +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=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=BC=D0=B8=D1=80=D1=83=D0=B5=D0=BC=D0=BE=D0=B3=D0=BE=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D1=80=D0=B2=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=82=D0=B0=D0=B9=D0=BC=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 2 +- include/6x8_slim_font.h | 3 ++- include/arch.h | 13 +++++++++ include/lock.h | 6 ++--- include/tool.h | 16 ++++++----- include/version.h | 2 +- kernel/arch/cpu.c | 57 ++++++++++++++++++++++++++++----------- kernel/arch/gdt.c | 4 +-- kernel/arch/idt.c | 55 +++++++++---------------------------- kernel/arch/idt.h | 32 ++++++++++++++++++++++ kernel/arch/pit.c | 32 ++++++++++++++++++++++ kernel/fb.c | 18 +++++++------ kernel/main/main.c | 54 +++++++++++++++++++++++-------------- kernel/mem.c | 23 +++++++++++----- kernel/mod.c | 16 ++++++++--- kernel/start.c | 4 ++- kernel/tool.c | 3 ++- modules/helloworld/main.c | 9 +++---- modules/music/build.sh | 0 modules/music/main.c | 21 ++++++++++++++- 20 files changed, 250 insertions(+), 120 deletions(-) create mode 100644 kernel/arch/idt.h create mode 100644 kernel/arch/pit.c mode change 100644 => 100755 modules/music/build.sh diff --git a/.clang-format b/.clang-format index 5a97673..e7822f2 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,4 @@ -ColumnLimit: 100 +ColumnLimit: 80 IndentWidth: 4 UseTab: ForIndentation TabWidth: 4 diff --git a/include/6x8_slim_font.h b/include/6x8_slim_font.h index beed107..600c581 100644 --- a/include/6x8_slim_font.h +++ b/include/6x8_slim_font.h @@ -21,7 +21,8 @@ #define FONT_6X8_SLIM_CHAR_WIDTH 6 #define FONT_6X8_SLIM_CHAR_HEIGHT 8 #define FONT_6X8_SLIM_FONT_TYPE (FONT_TYPE_MONOSPACED) -#define FONT_6X8_SLIM_ARRAY_LENGTH (FONT_6X8_SLIM_LENGTH * FONT_6X8_SLIM_CHAR_HEIGHT) +#define FONT_6X8_SLIM_ARRAY_LENGTH \ + (FONT_6X8_SLIM_LENGTH * FONT_6X8_SLIM_CHAR_HEIGHT) static const unsigned char font_6x8_slim[FONT_6X8_SLIM_ARRAY_LENGTH] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Символ 32 < > diff --git a/include/arch.h b/include/arch.h index d51c53b..f4dc8bb 100644 --- a/include/arch.h +++ b/include/arch.h @@ -10,9 +10,22 @@ #ifndef ARCH_H #define ARCH_H +#include + void arch_init( ); void cpu_init( ); void gdt_init( ); void idt_init( ); +void idt_set_int(uint8_t vector, void *int_handler); + +static inline void outb(uint16_t port, uint8_t val) { + asm volatile("outb %0, %1" : : "a"(val), "Nd"(port)); +} + +static inline uint8_t inb(uint16_t port) { + uint8_t ret; + asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} #endif // arch.h \ No newline at end of file diff --git a/include/lock.h b/include/lock.h index a0e6479..2e05492 100644 --- a/include/lock.h +++ b/include/lock.h @@ -17,9 +17,9 @@ typedef struct { const char* file; } lock_t; -#define LOCK_INIT \ - (lock_t) { \ - 0, __FILE__ \ +#define LOCK_INIT \ + (lock_t) { \ + 0, __FILE__ \ } int lock_swap(lock_t* lock); diff --git a/include/tool.h b/include/tool.h index 4bce44f..f127b01 100644 --- a/include/tool.h +++ b/include/tool.h @@ -15,17 +15,19 @@ #define abs(x) ((x) < 0 ? -(x) : (x)) -#define assert(check) \ - do { \ - if (!(check)) { \ - fb_printf("\nassert() failed in %s() (%s:%d)\n", __func__, __FILE__, __LINE__); \ - for (;;) asm volatile("hlt"); \ - } \ +#define assert(check) \ + do { \ + if (!(check)) { \ + fb_printf("\nassert() failed in %s() (%s:%d)\n", __func__, \ + __FILE__, __LINE__); \ + for (;;) asm volatile("hlt"); \ + } \ } while (0) #define ALIGN_UP(NUM, ALIGN) (((NUM) + ALIGN - 1) & ~(ALIGN - 1)) #define ALIGN_DOWN(NUM, ALIGN) ((NUM) & ~(ALIGN - 1)) -#define CONTAINER_OF(PTR, TYPE, MEMBER) ((TYPE *)((void *)PTR - offsetof(TYPE, MEMBER))) +#define CONTAINER_OF(PTR, TYPE, MEMBER) \ + ((TYPE *)((void *)PTR - offsetof(TYPE, MEMBER))) #define BIT_SET(BIT) (bitmap[(BIT) / 8] |= (1 << ((BIT) % 8))) #define BIT_CLEAR(BIT) (bitmap[(BIT) / 8] &= ~(1 << ((BIT) % 8))) diff --git a/include/version.h b/include/version.h index b5a16ca..fd6b77f 100644 --- a/include/version.h +++ b/include/version.h @@ -1,3 +1,3 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 1 -#define VERSION_BUILD 293 +#define VERSION_BUILD 297 diff --git a/kernel/arch/cpu.c b/kernel/arch/cpu.c index 8679edd..bda9028 100644 --- a/kernel/arch/cpu.c +++ b/kernel/arch/cpu.c @@ -31,8 +31,11 @@ static void sse_init( ) { asm volatile("mov %%cr4, %0" : : "r"(_cr4) : "memory"); } -static void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) { - asm volatile("cpuid" : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) : "a"(leaf)); +static void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, + uint32_t *edx) { + asm volatile("cpuid" + : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) + : "a"(leaf)); } static void msr_get(uint32_t msr, uint32_t *lo, uint32_t *hi) { @@ -66,7 +69,8 @@ static void l2_cache( ) { assoc = (ecx >> 12) & 0x07; cache = (ecx >> 16) & 0xFFFF; - fb_printf("Размер строки: %u B, Тип ассоциации: %u, Размер кэша: %u КБ\n", lsize, assoc, cache); + fb_printf("Размер строки: %u B, Тип ассоциации: %u, Размер кэша: %u КБ\n", + lsize, assoc, cache); } static void do_amd( ) { @@ -95,7 +99,8 @@ static void brandname( ) { uint32_t manufacturer[4]; char manufacturer_string[13]; - cpuid(0, &manufacturer[3], &manufacturer[0], &manufacturer[2], &manufacturer[1]); + cpuid(0, &manufacturer[3], &manufacturer[0], &manufacturer[2], + &manufacturer[1]); tool_memcpy(manufacturer_string, manufacturer, 12); brand_string[48] = 0; @@ -124,7 +129,8 @@ void cpu_init( ) { if ((edx >> 22) & 1) { acpi_msrs_support = true; fb_printf("Встроенный терморегулятор MSRS для ACPI\n"); - fb_printf("Температура: %u (в QEMU/KVM всегда 0)\n", get_cpu_temperature( )); + fb_printf("Температура: %u (в QEMU/KVM всегда 0)\n", + get_cpu_temperature( )); } if ((edx >> 23) & 1) { @@ -139,7 +145,9 @@ void cpu_init( ) { } cpuid(1, &eax, &ebx, &ecx, &edx); - if ((edx >> 29) & 1) { fb_printf("Термоконтроллер автоматически ограничивает температуру\n"); } + if ((edx >> 29) & 1) { + fb_printf("Термоконтроллер автоматически ограничивает температуру\n"); + } if ((ecx >> 28) & 1) { avx_support = true; @@ -160,9 +168,13 @@ void cpu_init( ) { if ((edx >> 5) & 1) { fb_printf("Регистры MSR подерживаются!\n"); } - // if ((edx >> 6) & 1) { fb_printf("Расширение физического адреса поддерживается!\n"); } + if ((edx >> 6) & 1) { + fb_printf("Расширение физического адреса поддерживается!\n"); + } - // if ((edx >> 7) & 1) { fb_printf("Исключение проверки компьютера (MCE) поддерживается!\n"); } + if ((edx >> 7) & 1) { + fb_printf("Исключение проверки компьютера (MCE) поддерживается!\n"); + } if ((edx >> 9) & 1) { fb_printf("Усовершенствованный программируемый контроллер прерываний " @@ -170,7 +182,8 @@ void cpu_init( ) { } if ((edx >> 10) & 1) { - fb_printf("SYSCALL/SYSRET(для AMD семейства 5 линейки 7) подерживаются!\n"); + fb_printf( + "SYSCALL/SYSRET(для AMD семейства 5 линейки 7) подерживаются!\n"); } if ((edx >> 11) & 1) { fb_printf("SYSCALL/SYSRET подерживаются!\n"); } @@ -183,19 +196,31 @@ void cpu_init( ) { if ((ecx >> 7) & 1) { fb_printf("Смещенный режим SSE поддерживается!\n"); } cpuid(0x80000007, &eax, &ebx, &ecx, &edx); - // if ((ebx >> 0) & 1) { fb_printf("Восстановление после переполнения MCA поддерживается!\n"); } - // if ((ebx >> 1) & 1) { fb_printf("Возможность локализации и восстановления неисправимых - // программных ошибок поддерживается!\n"); } + if ((ebx >> 0) & 1) { + fb_printf("Восстановление после переполнения MCA поддерживается!\n"); + } + if ((ebx >> 1) & 1) { + fb_printf("Возможность локализации и восстановления неисправимых " + "программных ошибок поддерживается!\n"); + } if ((edx >> 0) & 1) { fb_printf("Датчик температуры поддерживается!\n"); } if ((edx >> 3) & 1) { fb_printf("Терморегулятор поддерживается!\n"); } - if ((edx >> 4) & 1) { fb_printf("Аппаратный терморегулятор (HTC) поддерживается!\n"); } - if ((edx >> 5) & 1) { fb_printf("Программный терморегулятор (STC) поддерживается!\n"); } - if ((edx >> 6) & 1) { fb_printf("Управление множителем 100 МГц поддерживается!\n"); } + if ((edx >> 4) & 1) { + fb_printf("Аппаратный терморегулятор (HTC) поддерживается!\n"); + } + if ((edx >> 5) & 1) { + fb_printf("Программный терморегулятор (STC) поддерживается!\n"); + } + if ((edx >> 6) & 1) { + fb_printf("Управление множителем 100 МГц поддерживается!\n"); + } // fb_printf("0x80000007[ECX] = 0x%x (%u)\n", ecx, ecx); cpuid(0xC0000000, &eax, &ebx, &ecx, &edx); - if (eax > 0xC0000000) { fb_printf("0xC0000000 [EAX] = 0x%x (%u)\n", eax, eax); } + if (eax > 0xC0000000) { + fb_printf("0xC0000000 [EAX] = 0x%x (%u)\n", eax, eax); + } brandname( ); l2_cache( ); diff --git a/kernel/arch/gdt.c b/kernel/arch/gdt.c index 840142c..d9e2fd8 100644 --- a/kernel/arch/gdt.c +++ b/kernel/arch/gdt.c @@ -38,8 +38,8 @@ void gdt_load( ) { load_gdt((uint64_t)&gdtr); } -void set_gdt_entry(gdt_entry_t *entry, uint16_t limit, uint64_t base, uint8_t access, - uint8_t granularity) { +void set_gdt_entry(gdt_entry_t *entry, uint16_t limit, uint64_t base, + uint8_t access, uint8_t granularity) { entry->limit = limit; entry->base_16 = base & 0xFFFF; entry->base_middle_16 = (base >> 16) & 0xFF; diff --git a/kernel/arch/idt.c b/kernel/arch/idt.c index 2f7d42e..aa5a9e8 100644 --- a/kernel/arch/idt.c +++ b/kernel/arch/idt.c @@ -6,6 +6,7 @@ * */ +#include "idt.h" #include #include #include @@ -57,39 +58,6 @@ void *isr[256]; extern void *isr_stubs[]; static idt_ptr_t idtr; -#define NO_NAME "Не задано название" - -const char *exception_names[] = { "Деление на ноль", - "Отладка", - "NMI", - "Точка останова", - "Переполнение", - "Выход за границы", - "Недопустимая операция", - "Устройство недоступно", - "Двойное исключение", - NO_NAME, - "Недопустимый TSS", - "Сегмент не присутствует", - "Ошибка сегмента стека", - "Общая защитная ошибка", - "Ошибка страницы", - NO_NAME, - "x87 исключение", - "Проверка выравнивания", - "Ошибка машины", - "SIMD исключение", - "Виртуализация", - NO_NAME, - NO_NAME, - NO_NAME, - NO_NAME, - NO_NAME, - NO_NAME, - NO_NAME, - NO_NAME, - "Безопасность" }; - void idt_load( ) { asm volatile("lidt %0" : : "m"(idtr)); asm volatile("sti"); @@ -121,9 +89,10 @@ static void exception_handler(struct frame state) { " RIP=%x RFLAGS=%x\n" " CS=%x SS=%x\n" " ERR=%x INT=%u", - state.rax, state.rbx, state.rcx, state.rdx, state.rsi, state.rdi, state.rbp, - state.rsp, state.r8, state.r9, state.r10, state.r11, state.r12, state.r13, state.r14, - state.r15, state.rip, state.rflags, state.cs, state.ss, state.err, state.int_number); + state.rax, state.rbx, state.rcx, state.rdx, state.rsi, state.rdi, + state.rbp, state.rsp, state.r8, state.r9, state.r10, state.r11, + state.r12, state.r13, state.r14, state.r15, state.rip, + state.rflags, state.cs, state.ss, state.err, state.int_number); asm volatile("cli; hlt"); } @@ -140,11 +109,11 @@ void idt_init( ) { idtr = (idt_ptr_t){ .limit = sizeof(idt) - 1, .base = (uint64_t)idt }; for (uint64_t i = 0; i < 256; i++) { - if (i < 32) { - encode_idt_entry(i, isr_stubs[i], 0x8e); + if (i < 31) { + encode_idt_entry(i, isr_stubs[i], 0x8E); isr[i] = (void *)exception_handler; } else { - encode_idt_entry(i, isr_stubs[i], 0x8e); + encode_idt_entry(i, isr_stubs[i], 0x8E); isr[i] = (void *)isr_generic; } } @@ -153,6 +122,8 @@ void idt_init( ) { fb_printf("IDT инициализирован\n"); } -void idt_set_ist(uint8_t vector, uint8_t ist) { - idt[vector].ist = ist; -} +void idt_set_int(uint8_t vector, void *int_handler) { + encode_idt_entry(vector, int_handler, 0x8E); + isr[vector] = int_handler; + idt_load( ); +} \ No newline at end of file diff --git a/kernel/arch/idt.h b/kernel/arch/idt.h new file mode 100644 index 0000000..5fa6932 --- /dev/null +++ b/kernel/arch/idt.h @@ -0,0 +1,32 @@ +#define NO_NAME "Не задано название" + +const char *exception_names[] = { "Деление на ноль", + "Отладка", + "NMI", + "Точка останова", + "Переполнение", + "Выход за границы", + "Недопустимая операция", + "Устройство недоступно", + "Двойное исключение", + NO_NAME, + "Недопустимый TSS", + "Сегмент не присутствует", + "Ошибка сегмента стека", + "Общая защитная ошибка", + "Ошибка страницы", + NO_NAME, + "x87 исключение", + "Проверка выравнивания", + "Ошибка машины", + "SIMD исключение", + "Виртуализация", + NO_NAME, + NO_NAME, + NO_NAME, + NO_NAME, + NO_NAME, + NO_NAME, + NO_NAME, + NO_NAME, + "Безопасность" }; \ No newline at end of file diff --git a/kernel/arch/pit.c b/kernel/arch/pit.c new file mode 100644 index 0000000..84ef4a3 --- /dev/null +++ b/kernel/arch/pit.c @@ -0,0 +1,32 @@ +/** + * pit.c + * Программируемый интервальный таймер + * + * Настройка программируемого интервального таймера и + * + */ + +#include +#include +#include +#include +#include + +static uint64_t count = 0; + +static void isr_generic( ) { + fb_printf("\nТик! %u", count++); +} + +void pit_set_interval(int hz) { + int divisor = 1193180 / hz; // Вычисляем делитель + outb(0x43, 0x36); // Устанавливаем байт команды 0x36 + outb(0x40, divisor & 0xFF); // Устанавливаем младший байт делителя + outb(0x40, divisor >> 8); // Устанавливаем старший байт делителя +} + +void pit_init( ) { + idt_set_int(32, isr_generic); + pit_set_interval(100); + asm volatile("sti"); +} \ No newline at end of file diff --git a/kernel/fb.c b/kernel/fb.c index fae152c..862260f 100644 --- a/kernel/fb.c +++ b/kernel/fb.c @@ -73,10 +73,11 @@ void fb_print_buf(uint64_t x, uint64_t y, uint64_t h, uint64_t w, uint32_t *buf) } } -// Побитовый вывод из числа -static inline void print_bits(uint64_t x, uint64_t y, uint8_t num) { - for (uint64_t i = 0; i <= 7; i++) { - if ((num >> i) & 1) { SCREEN_BUFFER[x + i + y * SCREEN_WIDTH] = text_color; } +static inline void print_bits(size_t x, size_t y, uint8_t num) { + for (size_t i = 0; i <= 7; i++) { + if ((num >> i) & 1) { + SCREEN_BUFFER[x + i + y * SCREEN_WIDTH] = text_color; + } } } @@ -92,14 +93,15 @@ static void print_char(int x, int y, char glyth) { } } -// Прокрутка буффера вверх. Нижний слой удаляется -static void scroll_fb( ) { - uint64_t last_line_index = (SCREEN_HEIGHT - (FONT_6X8_SLIM_CHAR_HEIGHT)) * SCREEN_WIDTH; +void scroll_fb( ) { + size_t last_line_index = + (SCREEN_HEIGHT - (FONT_6X8_SLIM_CHAR_HEIGHT)) * SCREEN_WIDTH; for (uint64_t y = 0; y < SCREEN_HEIGHT - (FONT_6X8_SLIM_CHAR_HEIGHT); y++) { for (uint64_t x = 0; x < SCREEN_WIDTH; x++) { SCREEN_BUFFER[x + y * SCREEN_WIDTH] = - SCREEN_BUFFER[x + (y + (FONT_6X8_SLIM_CHAR_HEIGHT)) * SCREEN_WIDTH]; + SCREEN_BUFFER[x + + (y + (FONT_6X8_SLIM_CHAR_HEIGHT)) * SCREEN_WIDTH]; } } diff --git a/kernel/main/main.c b/kernel/main/main.c index fc5b388..39f659c 100644 --- a/kernel/main/main.c +++ b/kernel/main/main.c @@ -10,7 +10,8 @@ #include #include -#define TGA_ERR( ) fb_printf("Ошибка декодирования TGA на строчке: %u\n", __LINE__); +#define TGA_ERR( ) \ + fb_printf("Ошибка декодирования TGA на строчке: %u\n", __LINE__); extern void *bootpng_ptr; extern uint64_t bootpng_size; @@ -31,7 +32,8 @@ typedef struct { unsigned int *tga_parse(unsigned char *ptr, int size) { unsigned int *data; - int i, j, k, x, y, w = (ptr[13] << 8) + ptr[12], h = (ptr[15] << 8) + ptr[14], + int i, j, k, x, y, w = (ptr[13] << 8) + ptr[12], + h = (ptr[15] << 8) + ptr[14], o = (ptr[11] << 8) + ptr[10]; int m = ((ptr[1] ? (ptr[7] >> 3) * ptr[5] : 0) + 18); @@ -39,14 +41,15 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { data = (unsigned int *)mem_alloc((w * h + 2) * sizeof(unsigned int)); if (!data) { - fb_printf("Ошибка декодирования TGA на строчке: %u, %x, %u kb\n", __LINE__, data, - ((w * h + 2) * sizeof(unsigned int)) / 1024); + fb_printf("Ошибка декодирования TGA на строчке: %u, %x, %u kb\n", + __LINE__, data, ((w * h + 2) * sizeof(unsigned int)) / 1024); return NULL; } switch (ptr[2]) { case 1: - if (ptr[6] != 0 || ptr[4] != 0 || ptr[3] != 0 || (ptr[7] != 24 && ptr[7] != 32)) { + if (ptr[6] != 0 || ptr[4] != 0 || ptr[3] != 0 || + (ptr[7] != 24 && ptr[7] != 32)) { TGA_ERR( ); mem_free(data); return NULL; @@ -56,12 +59,14 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { for (x = 0; x < w; x++) { j = ptr[m + k++] * (ptr[7] >> 3) + 18; data[2 + i++] = ((ptr[7] == 32 ? ptr[j + 3] : 0xFF) << 24) | - (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; + (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | + ptr[j]; } } break; case 2: - if (ptr[5] != 0 || ptr[6] != 0 || ptr[1] != 0 || (ptr[16] != 24 && ptr[16] != 32)) { + if (ptr[5] != 0 || ptr[6] != 0 || ptr[1] != 0 || + (ptr[16] != 24 && ptr[16] != 32)) { TGA_ERR( ); mem_free(data); return NULL; @@ -69,14 +74,16 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { for (y = i = 0; y < h; y++) { j = ((!o ? h - y - 1 : y) * w * (ptr[16] >> 3)); for (x = 0; x < w; x++) { - data[2 + i++] = ((ptr[16] == 32 ? ptr[j + 3] : 0xFF) << 24) | - (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; + data[2 + i++] = + ((ptr[16] == 32 ? ptr[j + 3] : 0xFF) << 24) | + (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; j += ptr[16] >> 3; } } break; case 9: - if (ptr[6] != 0 || ptr[4] != 0 || ptr[3] != 0 || (ptr[7] != 24 && ptr[7] != 32)) { + if (ptr[6] != 0 || ptr[4] != 0 || ptr[3] != 0 || + (ptr[7] != 24 && ptr[7] != 32)) { TGA_ERR( ); mem_free(data); return NULL; @@ -93,8 +100,9 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { i = ((!o ? h - y - 1 : y) * w); y++; } - data[2 + i++] = ((ptr[7] == 32 ? ptr[j + 3] : 0xFF) << 24) | - (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; + data[2 + i++] = + ((ptr[7] == 32 ? ptr[j + 3] : 0xFF) << 24) | + (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; } } else { k++; @@ -105,14 +113,16 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { i = ((!o ? h - y - 1 : y) * w); y++; } - data[2 + i++] = ((ptr[7] == 32 ? ptr[j + 3] : 0xFF) << 24) | - (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; + data[2 + i++] = + ((ptr[7] == 32 ? ptr[j + 3] : 0xFF) << 24) | + (ptr[j + 2] << 16) | (ptr[j + 1] << 8) | ptr[j]; } } } break; case 10: - if (ptr[5] != 0 || ptr[6] != 0 || ptr[1] != 0 || (ptr[16] != 24 && ptr[16] != 32)) { + if (ptr[5] != 0 || ptr[6] != 0 || ptr[1] != 0 || + (ptr[16] != 24 && ptr[16] != 32)) { TGA_ERR( ); mem_free(data); return NULL; @@ -128,8 +138,9 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { i = ((!o ? h - y - 1 : y) * w); y++; } - data[2 + i++] = ((ptr[16] == 32 ? ptr[m + 3] : 0xFF) << 24) | - (ptr[m + 2] << 16) | (ptr[m + 1] << 8) | ptr[m]; + data[2 + i++] = + ((ptr[16] == 32 ? ptr[m + 3] : 0xFF) << 24) | + (ptr[m + 2] << 16) | (ptr[m + 1] << 8) | ptr[m]; } m += ptr[16] >> 3; } else { @@ -140,8 +151,9 @@ unsigned int *tga_parse(unsigned char *ptr, int size) { i = ((!o ? h - y - 1 : y) * w); y++; } - data[2 + i++] = ((ptr[16] == 32 ? ptr[m + 3] : 0xFF) << 24) | - (ptr[m + 2] << 16) | (ptr[m + 1] << 8) | ptr[m]; + data[2 + i++] = + ((ptr[16] == 32 ? ptr[m + 3] : 0xFF) << 24) | + (ptr[m + 2] << 16) | (ptr[m + 1] << 8) | ptr[m]; m += ptr[16] >> 3; } } @@ -165,7 +177,9 @@ void main( ) { tga_header_t *head = (tga_header_t *)bootpng_ptr; - if (res != NULL) { fb_printf("Размер экрана загрузки: %ux%u \n", res[0], res[1]); } + if (res != NULL) { + fb_printf("Размер экрана загрузки: %ux%u \n", res[0], res[1]); + } fb_printf("Размер экрана загрузки: %ux%u \n", head->h, head->w); mem_dump_memory( ); diff --git a/kernel/mem.c b/kernel/mem.c index 1b5ab3f..01670f3 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -14,10 +14,14 @@ #include static volatile struct limine_memmap_request memmap_request = { - .id = LIMINE_MEMMAP_REQUEST, .revision = 0, .response = (struct limine_memmap_response *)0 + .id = LIMINE_MEMMAP_REQUEST, + .revision = 0, + .response = (struct limine_memmap_response *)0 }; static volatile struct limine_hhdm_request hhdm_request = { - .id = LIMINE_HHDM_REQUEST, .revision = 0, .response = (struct limine_hhdm_response *)0 + .id = LIMINE_HHDM_REQUEST, + .revision = 0, + .response = (struct limine_hhdm_response *)0 }; struct mem_entry { @@ -60,8 +64,9 @@ void mem_dump_memory( ) { mem_entry_t *curr = first_node; while (curr) { - fb_printf("->0x%x | %u.%u kb | %u | 0x%x\n", &curr->data, (curr->size) / 1024, - (curr->size) % 1024, curr->free, curr->next); + fb_printf("->0x%x | %u.%u kb | %u | 0x%x\n", &curr->data, + (curr->size) / 1024, (curr->size) % 1024, curr->free, + curr->next); curr = curr->next; } } @@ -235,7 +240,8 @@ void mem_init( ) { if (mmaps[i]->type == LIMINE_MEMMAP_FRAMEBUFFER) { fb_printf("На видеопамять BIOS/UEFI выделено: %u мегабайт + %u " "килобайт\n", - mmaps[i]->length / 1024 / 1024, (mmaps[i]->length / 1024) % 1024); + mmaps[i]->length / 1024 / 1024, + (mmaps[i]->length / 1024) % 1024); } if (!(mmaps[i]->type == LIMINE_MEMMAP_USABLE)) { continue; } @@ -263,7 +269,9 @@ void mem_init( ) { // Освобождаем все доступные фреймы памяти for (uint64_t i = 0; i < mmmap_count; i++) { - for (uint64_t t = 0; t < mmaps[i]->length; t += BLOCK_SIZE) { bitmap_limit++; } + for (uint64_t t = 0; t < mmaps[i]->length; t += BLOCK_SIZE) { + bitmap_limit++; + } if (!(mmaps[i]->type == LIMINE_MEMMAP_USABLE)) { continue; } @@ -283,7 +291,8 @@ void mem_init( ) { mem_merge_all_blocks( ); mem_dump_memory( ); fb_printf("%u МБ объем доступной памяти, %u МБ объем виртуальной памяти\n", - (bitmap_available * BLOCK_SIZE) / 1024 / 1024, available / 1024 / 1024); + (bitmap_available * BLOCK_SIZE) / 1024 / 1024, + available / 1024 / 1024); fb_printf("%u / %u блоков доступно\n", bitmap_available, bitmap_limit); fb_printf("Проверка менеджера памяти\n"); diff --git a/kernel/mod.c b/kernel/mod.c index f71149a..916b723 100644 --- a/kernel/mod.c +++ b/kernel/mod.c @@ -56,7 +56,9 @@ static void *elf_entry(void *module_bin, uint64_t size) { } static volatile struct limine_module_request module_request = { - .id = LIMINE_MODULE_REQUEST, .revision = 0, .response = (struct limine_module_response *)0 + .id = LIMINE_MODULE_REQUEST, + .revision = 0, + .response = (struct limine_module_response *)0 }; static struct limine_module_response *module_response; @@ -74,8 +76,14 @@ void mod_init( ) { fb_printf("->Размер: %u, тип носителя: %u, индекс раздела: %u\n", module_ptr->size, module_ptr->media_type, module_ptr->partition_index); #if 0 + fb_printf("[%d] %s [%s] 0x%x\n", i, module_ptr->path, + module_ptr->cmdline, module_ptr->address); + fb_printf("->Размер: %u, тип носителя: %u, индекс раздела: %u\n", + module_ptr->size, module_ptr->media_type, + module_ptr->partition_index); fb_printf("->Идентификатор диска MBR: %u, TFTP IP: %u, TFTP порт: %u\n", - module_ptr->mbr_disk_id, module_ptr->tftp_ip, module_ptr->tftp_port); + module_ptr->mbr_disk_id, module_ptr->tftp_ip, + module_ptr->tftp_port); #endif if (tool_starts_with(module_ptr->cmdline, "[BOOTIMG]")) { @@ -86,8 +94,8 @@ void mod_init( ) { } if (!tool_starts_with(module_ptr->cmdline, "[MOD]")) { continue; } modules_count++; - uint64_t (*module_init)(env_t * env) = - (module_info_t * (*)(env_t * env)) elf_entry(module_ptr->address, module_ptr->size); + uint64_t (*module_init)(env_t *env) = (module_info_t * (*)(env_t * env)) + elf_entry(module_ptr->address, module_ptr->size); fb_printf("\t->Точка входа: 0x%x\n", module_init); diff --git a/kernel/start.c b/kernel/start.c index 2448333..8b4bee4 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -27,7 +27,9 @@ void _start( ) { fb_printf("\t\t\t\t *** Базовая Модульная Платформа Операционных Систем " "версии %u.%u.%u ***\n", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD); - fb_printf("\t\t\t\t *** Дата сборки: %s %s ***\n", __DATE__, __TIME__); + fb_printf("\t\t\t\t *** Дата сборки: %s %s ***\n", __DATE__, __TIME__); + pit_init( ); + fb_printf("Готово!\n"); for (;;) { asm volatile("hlt"); } } \ No newline at end of file diff --git a/kernel/tool.c b/kernel/tool.c index 662a38c..ab35082 100644 --- a/kernel/tool.c +++ b/kernel/tool.c @@ -45,7 +45,8 @@ uint64_t tool_starts_with(const char *str, const char *prefix) { } // Функция для форматированного вывода -void tool_format(void (*putc)(char c), const char *format_string, va_list args) { +void tool_format(void (*putc)(char c), const char *format_string, + va_list args) { while (*format_string != '\0') { if (*format_string == '%') { format_string++; diff --git a/modules/helloworld/main.c b/modules/helloworld/main.c index c960734..a8e6283 100644 --- a/modules/helloworld/main.c +++ b/modules/helloworld/main.c @@ -2,12 +2,11 @@ const char name[] = "Привет мир!"; const char message[] = "Привет из модуля!"; -module_info_t static_info = { - .name = (char *)&name, .message = (char *)&message, .err_code = 2023, .func_count = 1 -}; +module_info_t info = { .name = (char *)&name, + .message = (char *)&message, + .err_code = 2023, + .func_count = 1 }; uint64_t init(env_t *env) { - init_env(env); - env->fb_printf("Модуль \"Привет мир!\" инициализирован!\n"); return 0; } diff --git a/modules/music/build.sh b/modules/music/build.sh old mode 100644 new mode 100755 diff --git a/modules/music/main.c b/modules/music/main.c index 87cc05a..547e3b4 100644 --- a/modules/music/main.c +++ b/modules/music/main.c @@ -14,7 +14,26 @@ static inline void usleep(uint64_t ticks) { for (uint64_t i = 0; i < ticks * 100; i++) { asm volatile("pause"); } } -static inline void play_sound(unsigned int frequency) {} +static inline void play_sound(unsigned int frequency) { + uint32_t div; + uint8_t tmp; + + // Set the PIT to the desired frequency + div = 1193180 / frequency; + outb(0x43, 0xb6); + outb(0x42, (uint8_t)(div)); + outb(0x42, (uint8_t)(div >> 8)); + + // And play the sound using the PC speaker + tmp = inb(0x61); + if (tmp != (tmp | 3)) { outb(0x61, tmp | 3); } +} + +static void nosound( ) { + uint8_t tmp = inb(0x61) & 0xFC; + + outb(0x61, tmp); +} int init(env_t *env) { init_env(env);