2023-10-21 20:27:23 +03:00
|
|
|
|
/**
|
|
|
|
|
* arch.h
|
|
|
|
|
* Заголовок для инициализации архитектурно-зависимых функций
|
|
|
|
|
*
|
|
|
|
|
* Данный заголовочный файл содержит определения которые используются для
|
|
|
|
|
* инициализации архитектуры
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ARCH_H
|
|
|
|
|
#define ARCH_H
|
|
|
|
|
|
2023-10-25 00:07:32 +03:00
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2024-01-02 16:32:32 +03:00
|
|
|
|
#define STACK_SIZE 8192 // 1MB
|
|
|
|
|
|
2024-01-14 15:28:26 +03:00
|
|
|
|
typedef struct task {
|
2024-01-20 19:04:26 +03:00
|
|
|
|
uint64_t rax, rbx, rcx, rdx;
|
|
|
|
|
uint64_t rsi, rdi, rsp, rbp;
|
|
|
|
|
|
|
|
|
|
uint64_t id;
|
|
|
|
|
uint64_t ret;
|
|
|
|
|
void *stack;
|
|
|
|
|
|
|
|
|
|
struct task *last;
|
|
|
|
|
struct task *next;
|
2024-01-02 16:32:32 +03:00
|
|
|
|
} __attribute__((packed)) task_t;
|
|
|
|
|
|
2023-12-14 11:25:53 +03:00
|
|
|
|
struct frame {
|
|
|
|
|
uint64_t rbp;
|
|
|
|
|
uint64_t rbx;
|
|
|
|
|
uint64_t r15;
|
|
|
|
|
uint64_t r14;
|
|
|
|
|
uint64_t r13;
|
|
|
|
|
uint64_t r12;
|
|
|
|
|
uint64_t r11;
|
|
|
|
|
uint64_t r10;
|
|
|
|
|
uint64_t r9;
|
|
|
|
|
uint64_t r8;
|
|
|
|
|
uint64_t rax;
|
|
|
|
|
uint64_t rcx;
|
|
|
|
|
uint64_t rdx;
|
|
|
|
|
uint64_t rsi;
|
|
|
|
|
uint64_t rdi;
|
|
|
|
|
uint64_t int_number;
|
|
|
|
|
uint64_t err;
|
|
|
|
|
uint64_t rip;
|
|
|
|
|
uint64_t cs;
|
|
|
|
|
uint64_t rflags;
|
|
|
|
|
uint64_t rsp;
|
|
|
|
|
uint64_t ss;
|
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
|
|
typedef void (*int_entry_t)(struct frame *state);
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void arch_init( );
|
2024-01-02 16:32:32 +03:00
|
|
|
|
void task_init( );
|
|
|
|
|
void task_switch(struct frame *state);
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void cpu_init( );
|
|
|
|
|
void gdt_init( );
|
2023-12-14 11:25:53 +03:00
|
|
|
|
void pic_init( );
|
|
|
|
|
void pit_init( );
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void idt_init( );
|
2023-12-14 11:25:53 +03:00
|
|
|
|
void idt_set_int(uint8_t vector, int_entry_t handler);
|
2023-10-31 19:06:56 +03:00
|
|
|
|
uint64_t arch_get_tick_b( );
|
|
|
|
|
uint64_t arch_get_tick_l( );
|
2023-10-29 16:12:00 +03:00
|
|
|
|
uint64_t arch_get_tick( );
|
2024-01-13 00:00:11 +03:00
|
|
|
|
void com_write_byte(uint8_t byte);
|
|
|
|
|
void com_write_bytes(char *c, uint64_t n);
|
2023-10-25 00:07:32 +03:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-10-21 20:27:23 +03:00
|
|
|
|
|
2023-12-08 18:11:59 +03:00
|
|
|
|
static inline void outw(uint16_t port, uint16_t val) {
|
|
|
|
|
asm volatile("outw %0, %1" : : "a"(val), "Nd"(port));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint16_t inw(uint16_t port) {
|
|
|
|
|
uint16_t ret;
|
|
|
|
|
asm volatile("inw %1, %0" : "=a"(ret) : "Nd"(port));
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 11:25:53 +03:00
|
|
|
|
static inline void io_wait( ) {
|
|
|
|
|
outb(0x80, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-02 16:32:32 +03:00
|
|
|
|
static inline void print_stack_trace( ) {
|
|
|
|
|
uint64_t *rsp;
|
2024-01-13 00:00:11 +03:00
|
|
|
|
asm volatile("movq %%rsp, %0" : "=g"(rsp));
|
2024-01-02 16:32:32 +03:00
|
|
|
|
|
|
|
|
|
while (rsp) {
|
|
|
|
|
// fb_printf("%x\n", *rsp);
|
|
|
|
|
rsp = (uint64_t *)(*rsp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-13 00:00:11 +03:00
|
|
|
|
#define GET_TICK_BIG arch_get_tick_b( )
|
|
|
|
|
#define GET_TICK_lOW arch_get_tick_l( )
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
#endif // arch.h
|