Skip to content

Commit

Permalink
Working at optimizations and bug fixing (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimnik98 authored Jun 4, 2024
2 parents cc0519f + 3e8dce4 commit d9c3a59
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 71 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,12 @@ clean:
-rm -f $(KERNEL_NEED)
-rm -f $(DEPS)
-rm -f iso/boot/ramdisk
-rm -f rust/target -r

# Линковка файлов
$(KERNEL): $(KERNEL_NEED) # $(RUST_SOURCES) rust/Cargo.toml
# $(MAKE) build_rust
$(KERNEL): $(KERNEL_NEED)
@echo -e '\x1b[32mLINK \x1b[0m' $(KERNEL)
@rm -f $(KERNEL)
@$(LD) $(LDFLAGS) -o $(KERNEL) $(KERNEL_NEED) # $(RUST_OBJ_RELEASE)
@#llvm-strip -s $(KERNEL) # I know I strip all symbols so making unwind useless. (Fix it later)
@$(LD) $(LDFLAGS) -o $(KERNEL) $(KERNEL_NEED)
@bash tools/genmap.sh
@bash tools/insertmap.sh
@ls -lh $(KERNEL) kernel.map
Expand Down
1 change: 0 additions & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ QEMU_FLAGS = -cdrom kernel.iso -m $(MEMORY_SIZE) \
-device hda-output,audiodev=pa0 \
-trace "hda*" \
-boot d \
-cpu core2duo-v1 \
-object filter-dump,id=dump0,netdev=net0,file=netdump.pcap \
$(KVM_QEMU_FLAGS) \
$(ADDITIONAL_QEMU_FLAGS)
Expand Down
2 changes: 2 additions & 0 deletions kernel/include/io/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,5 @@ inline static __attribute__((always_inline)) uint32_t getDisplaySize(){

void setPixelAlpha(uint32_t x, uint32_t y, rgba_color color);
void rect_copy(int x, int y, int width, int height);
void graphics_update(uint32_t new_width, uint32_t new_height, uint32_t new_pitch);

18 changes: 17 additions & 1 deletion kernel/include/net/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,20 @@ typedef struct {
uint16_t urg_ptr;
} tcp_packet_t;

void tcp_handle_packet(netcard_entry_t *card, tcp_packet_t *packet);
typedef enum {
EIKS_CREATED,
EIKS_LISTENING,
EIKS_ESTABLISHED
} tcp_connection_status_t;

typedef struct {
netcard_entry_t* card;
uint32_t dest_ip_addr;
uint16_t source_port;
uint16_t dest_port;
uint32_t seq;
uint32_t dst_seq;
tcp_connection_status_t status;
} tcp_connection_t;

void tcp_handle_packet(netcard_entry_t *card, tcp_packet_t *packet);
4 changes: 3 additions & 1 deletion kernel/include/sys/mtrr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ void mtrr_init();
void read_mtrr(size_t index, uint32_t* base, uint32_t* mask);
void write_mtrr(size_t index, uint32_t base, uint32_t mask);
void write_mtrr_size(size_t index, uint32_t base, uint32_t size, size_t type);
size_t find_free_mtrr();
size_t find_free_mtrr();

uint32_t get_mtrr_index(uint32_t address);
21 changes: 2 additions & 19 deletions kernel/src/drv/disk/ahci.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ void ahci_init() {
while(port->command_and_status & (1 << 15));

ahci_rebase_memory_for(i);

// Additional initialization here
}
}

Expand Down Expand Up @@ -425,16 +427,6 @@ void ahci_read_sectors(size_t port_num, uint64_t location, size_t sector_count,

cmdfis->device = 1 << 6; // LBA mode

cmdfis->lba3 = (location >> 24) & 0xFF;

#ifdef SAYORI64
cmdfis->lba4 = (location >> 32) & 0xFF;
cmdfis->lba5 = (location >> 40) & 0xFF;
#else
cmdfis->lba4 = 0;
cmdfis->lba5 = 0;
#endif

cmdfis->countl = sector_count & 0xff;
cmdfis->counth = (sector_count >> 8) & 0xff;

Expand Down Expand Up @@ -525,15 +517,6 @@ void ahci_write_sectors(size_t port_num, size_t location, size_t sector_count, v
cmdfis->device = 1 << 6; // LBA mode

cmdfis->lba3 = (location >> 24) & 0xFF;

#ifdef SAYORI64
cmdfis->lba4 = (location >> 32) & 0xFF;
cmdfis->lba5 = (location >> 40) & 0xFF;
#else
cmdfis->lba4 = 0;
cmdfis->lba5 = 0;
#endif

cmdfis->countl = sector_count & 0xff;
cmdfis->counth = (sector_count >> 8) & 0xff;

Expand Down
10 changes: 5 additions & 5 deletions kernel/src/drv/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ extern void tty_backspace();

bool SHIFT = false, ///< Включен ли SHIFT
RU = false; ///< Печатаем русскими?
int lastKey = 0; ///< Последний индекс клавишы
volatile int lastKey = 0; ///< Последний индекс клавишы
uint8_t kbdstatus = 0; ///< Статус клавиатуры
bool echo = true; ///< Включен ли вывод?
bool key_ctrl = false;

char kmode = 0;
char* curbuf = 0;
uint32_t chartyped = 0;
volatile char kmode = 0;
volatile char* curbuf = 0;
volatile uint32_t chartyped = 0;

/**
* @brief Выводит правильный символ, в зависимости от языка и шифта
Expand Down Expand Up @@ -410,4 +410,4 @@ void ps2_keyboard_install_irq() {
// Register interrupts
register_interrupt_handler(IRQ1, &keyboardHandler);
qemu_log("Keyboard installed");
}
}
4 changes: 2 additions & 2 deletions kernel/src/drv/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ vector_t* pci_device_list = 0;
*/
uint16_t pci_read_confspc_word(uint8_t bus, uint8_t slot, uint8_t function, uint8_t offset) {
uint32_t addr;
uint32_t bus32 = bus;
// uint32_t bus32 = bus;
uint32_t slot32 = slot;
uint32_t func32 = function;

addr = (uint32_t)((bus32 << 16) | (slot32 << 11) |
addr = (uint32_t)(((uint32_t)bus << 16) | (slot32 << 11) |
(func32 << 8) | (offset & 0xfc) | ((uint32_t)0x80000000)); //yes, this line is copied from osdev

outl(PCI_ADDRESS_PORT, addr);
Expand Down
9 changes: 9 additions & 0 deletions kernel/src/extra/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ uint32_t CLI_CMD_REBOOT(uint32_t argc, char* argv[]) {
return 0;
}

uint32_t CLI_CMD_MTRR(uint32_t argc, char* argv[]) {
list_mtrrs();

return 0;
}

uint32_t CLI_RD(uint32_t argc, char* argv[]) {
if(argc < 2) {
tty_error("No arguments.\n");
Expand Down Expand Up @@ -508,6 +514,7 @@ CLI_CMD_ELEM G_CLI_CMD[] = {
{"DESKTOP", "desktop", parallel_desktop_start, "Рабочий стол"},
{"MALA", "mala", mala_draw, "Нарисовать рисунок"},
{"MINESWEEPER", "minesweeper", minesweeper, "Сапёр"},
{"MTRR", "mtrr", CLI_CMD_MTRR, "MTRR"},
{"PAVI", "pavi", pavi_view, "Программа для просмотра изображений"},
{"PCI", "pci", pci_print_list, "Список PCI устройств"},
// {"RS", "rs", rust_command, "Rust command"},
Expand Down Expand Up @@ -604,6 +611,8 @@ void cli(){

if(delta > 0) {
qemu_err("Memory leak!");
} else if(delta == 0) {
qemu_ok("All right! No memory leaks! Keep it up, buddy!");
}

qemu_note("Time elapsed: %d milliseconds", timestamp() - current_time);
Expand Down
44 changes: 23 additions & 21 deletions kernel/src/gfx/intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,29 @@ void igfx_init() {
IGFX_WRITE(xaddr + 0x10180, IGFX_READ(xaddr + 0x10180) | (1 << 31)); // enable Display Plane A


unmap_pages_overlapping(get_kernel_page_directory(), (virtual_addr_t)framebuffer_addr, framebuffer_size);

framebuffer_size = ALIGN((igfx_width + 32) * igfx_height * 4, PAGE_SIZE);

map_pages(get_kernel_page_directory(),
(physical_addr_t)framebuffer_addr,
(virtual_addr_t)framebuffer_addr,
framebuffer_size,
PAGE_WRITEABLE);

memset(framebuffer_addr, 0xff, framebuffer_size);

extern uint32_t framebuffer_width;
extern uint32_t framebuffer_height;

framebuffer_width = igfx_width;
framebuffer_height = igfx_height;
framebuffer_pitch = scanline_w;

back_framebuffer_addr = krealloc(back_framebuffer_addr, framebuffer_size);
memset(back_framebuffer_addr, 0x00, framebuffer_size);
// unmap_pages_overlapping(get_kernel_page_directory(), (virtual_addr_t)framebuffer_addr, framebuffer_size);
//
// framebuffer_size = ALIGN((igfx_width + 32) * igfx_height * 4, PAGE_SIZE);
//
// map_pages(get_kernel_page_directory(),
// (physical_addr_t)framebuffer_addr,
// (virtual_addr_t)framebuffer_addr,
// framebuffer_size,
// PAGE_WRITEABLE);
//
// memset(framebuffer_addr, 0xff, framebuffer_size);
//
// extern uint32_t framebuffer_width;
// extern uint32_t framebuffer_height;
//
// framebuffer_width = igfx_width;
// framebuffer_height = igfx_height;
// framebuffer_pitch = scanline_w;
//
// back_framebuffer_addr = krealloc(back_framebuffer_addr, framebuffer_size);
// memset(back_framebuffer_addr, 0x00, framebuffer_size);

graphics_update(igfx_width, igfx_height, scanline_w);

clean_tty_screen();

Expand Down
63 changes: 52 additions & 11 deletions kernel/src/io/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <common.h>
#include "mem/pmm.h"
#include "mem/vmm.h"
#include "sys/mtrr.h"

uint8_t *framebuffer_addr = 0; ///< Точка монтирования
uint32_t framebuffer_pitch; ///< Частота обновления экрана
Expand All @@ -17,6 +18,9 @@ uint8_t *back_framebuffer_addr = 0; ///< Позиция буфера экран
bool lazyDraw = true; ///< Включен ли режим ленивой прорисовки
bool tty_oem_mode = false; ///< Режим работы

size_t fb_mtrr_idx = 0;
size_t bfb_mtrr_idx = 0;

/**
* @brief Получение адреса расположения драйвера экрана
*
Expand Down Expand Up @@ -50,22 +54,26 @@ uint32_t getDisplayBpp(){

void create_back_framebuffer() {
qemu_log("^---- 1. Allocating");

back_framebuffer_addr = (uint8_t*)kmalloc_common(framebuffer_size, PAGE_SIZE);
memset(back_framebuffer_addr, 0, framebuffer_size);

/*
size_t phys_bfb = virt2phys(get_kernel_page_directory(), (virtual_addr_t) back_framebuffer_addr);

qemu_log("Physical is: %x", phys_bfb);

map_pages(
get_kernel_page_directory(),
phys_bfb,
(virtual_addr_t) back_framebuffer_addr,
framebuffer_size,
PAGE_WRITEABLE | PAGE_WRITE_THROUGH
);
*/
bfb_mtrr_idx = find_free_mtrr();
write_mtrr_size(bfb_mtrr_idx, phys_bfb, framebuffer_size, 1);

// map_pages(
// get_kernel_page_directory(),
// phys_bfb,
// (virtual_addr_t) back_framebuffer_addr,
// framebuffer_size,
// PAGE_WRITEABLE | PAGE_CACHE_DISABLE
// );

phys_set_flags(get_kernel_page_directory(), (virtual_addr_t)back_framebuffer_addr, PAGE_WRITEABLE | PAGE_CACHE_DISABLE);

qemu_log("framebuffer_size = %d (%dK) (%dM)", framebuffer_size, framebuffer_size/1024, framebuffer_size/(1024*1024));
qemu_log("back_framebuffer_addr = %x", back_framebuffer_addr);
Expand Down Expand Up @@ -122,13 +130,19 @@ void init_vbe(multiboot_header_t *mboot) {
frame,
virt,
framebuffer_size,
PAGE_WRITEABLE);
PAGE_WRITEABLE | PAGE_CACHE_DISABLE);

qemu_log("Okay mapping! (took %d millis)", (getTicks() - start_tk)/(getFrequency()/1000));

qemu_log("Creating framebuffer");
qemu_log("Creating second framebuffer");

create_back_framebuffer();

qemu_log("^---- OKAY");

fb_mtrr_idx = find_free_mtrr();

write_mtrr_size(fb_mtrr_idx, frame, framebuffer_size, 1);
}

/**
Expand Down Expand Up @@ -219,6 +233,33 @@ uint32_t getScreenHeight(){
return framebuffer_height;
}

void graphics_update(uint32_t new_width, uint32_t new_height, uint32_t new_pitch) {
unmap_pages_overlapping(get_kernel_page_directory(), (virtual_addr_t)framebuffer_addr, framebuffer_size);

framebuffer_size = ALIGN((new_width + 32) * new_height * 4, PAGE_SIZE);

map_pages(get_kernel_page_directory(),
(physical_addr_t)framebuffer_addr,
(virtual_addr_t)framebuffer_addr,
framebuffer_size,
PAGE_WRITEABLE | PAGE_CACHE_DISABLE
);

framebuffer_width = new_width;
framebuffer_height = new_height;
framebuffer_pitch = new_pitch;

back_framebuffer_addr = krealloc(back_framebuffer_addr, framebuffer_size);
memset(back_framebuffer_addr, 0x00, framebuffer_size);

phys_set_flags(get_kernel_page_directory(), (virtual_addr_t)back_framebuffer_addr, PAGE_WRITEABLE | PAGE_CACHE_DISABLE);

uint32_t bfb_new_phys = virt2phys(get_kernel_page_directory(), (virtual_addr_t)back_framebuffer_addr);

write_mtrr_size(fb_mtrr_idx, (uint32_t)framebuffer_addr, framebuffer_size, 1);
write_mtrr_size(bfb_mtrr_idx, (uint32_t)bfb_new_phys, framebuffer_size, 1);
}

/**
* @brief Очистка экрана
*
Expand Down
10 changes: 10 additions & 0 deletions kernel/src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,20 @@ void __attribute__((noreturn)) kmain(multiboot_header_t* mboot, uint32_t initia
igfx_init();

// hda_init();
void k();

// create_process(k, "process", false, true);

qemu_log("System initialized everything at: %f seconds.", (double) (getTicks() - kernel_start_time) / getFrequency());

cli();

while(1);
}

// void k() {
// for(int i = 0; i < 10; i++) {
// qemu_err("HELLO");
// sleep_ms(250);
// }
// }
8 changes: 7 additions & 1 deletion kernel/src/net/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
// Created by ndraey on 21.4.2024.
//

#include "net/ethernet.h"
#include "net/tcp.h"
#include "net/endianess.h"
#include "io/ports.h"

void tcp_handle_packet(netcard_entry_t *card, tcp_packet_t *packet) {
qemu_note("!!!!!!!!!!!!!!!!!!!!!!!! TCP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

ETH_IPv4_PKG *ipv4 = (ETH_IPv4_PKG *)((size_t)packet - sizeof(ETH_IPv4_PKG));
size_t data_payload_size = ipv4->TotalLength - sizeof(ETH_IPv4_PKG);

qemu_log("Data payload size: %d", data_payload_size);

packet->source = ntohs(packet->source);
packet->destination = ntohs(packet->destination);

qemu_note("SRC: %d; DEST: %d", packet->source, packet->destination);

qemu_note("FLAGS: SYN: %d; ACK: %d; PSH: %d; FIN: %d", packet->syn, packet->ack, packet->psh, packet->fin);
}
}
4 changes: 4 additions & 0 deletions kernel/src/sys/cpu_isr.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ _Noreturn void bsod_screen(registers_t regs, char* title, char* msg, uint32_t co
qemu_log("======================================================\n");

qemu_err("PROCESS CAUSED THE EXCEPTION: nr. %d", get_current_proc()->pid);

if(get_current_proc()->pid != 0) {
qemu_note("TODO: Kill that process and return to system.");
}

unwind_stack(10);

Expand Down
Loading

0 comments on commit d9c3a59

Please sign in to comment.