2023-10-21 20:27:23 +03:00
|
|
|
|
/**
|
|
|
|
|
* arch.h
|
|
|
|
|
* Заголовок для инициализации архитектурно-зависимых функций
|
|
|
|
|
*
|
|
|
|
|
* Данный заголовочный файл содержит определения которые используются для
|
|
|
|
|
* инициализации архитектуры
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ARCH_H
|
|
|
|
|
#define ARCH_H
|
|
|
|
|
|
2024-01-21 22:00:56 +03:00
|
|
|
|
#include <lock.h>
|
2023-10-25 00:07:32 +03:00
|
|
|
|
#include <stdint.h>
|
2024-01-22 21:45:56 +03:00
|
|
|
|
#include <sys.h>
|
2023-10-25 00:07:32 +03:00
|
|
|
|
|
2024-09-17 10:05:22 +03:00
|
|
|
|
#define STACK_SIZE 32 * 1024 // 16 килобайт на стек
|
2024-01-02 16:32:32 +03:00
|
|
|
|
|
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;
|
2024-01-21 21:40:21 +03:00
|
|
|
|
uint64_t cr3;
|
2024-01-20 19:04:26 +03:00
|
|
|
|
|
2024-01-21 22:17:37 +03:00
|
|
|
|
uint64_t cpu_time;
|
|
|
|
|
uint64_t cpu_time_expired;
|
2024-01-20 19:04:26 +03:00
|
|
|
|
uint64_t id;
|
2024-02-01 10:24:32 +03:00
|
|
|
|
char *id_str;
|
2024-01-20 19:04:26 +03:00
|
|
|
|
void *stack;
|
2024-09-17 10:05:22 +03:00
|
|
|
|
void *entry;
|
|
|
|
|
uint64_t status; // 0 - на удаление 1 - работает
|
2024-01-20 19:04:26 +03:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2024-01-21 22:00:56 +03:00
|
|
|
|
extern lock_t task_lock;
|
2024-01-22 21:45:56 +03:00
|
|
|
|
extern uint64_t task_f_init;
|
2024-02-01 10:24:32 +03:00
|
|
|
|
extern task_t *current_task;
|
2024-01-21 22:00:56 +03:00
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void arch_init( );
|
2024-01-02 16:32:32 +03:00
|
|
|
|
void task_init( );
|
2024-02-01 10:24:32 +03:00
|
|
|
|
void task_after_init( );
|
2024-01-22 21:45:56 +03:00
|
|
|
|
void task_switch( );
|
2024-09-16 16:58:28 +03:00
|
|
|
|
uint64_t task_new_thread(void (*func)(void *), char *name, void *arg);
|
2024-01-27 22:03:33 +03:00
|
|
|
|
void task_del_current( );
|
2024-02-01 10:24:32 +03:00
|
|
|
|
void task_del(uint64_t id);
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void cpu_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-09-16 21:42:02 +03:00
|
|
|
|
void com_write_byte(char byte);
|
2024-01-13 00:00:11 +03:00
|
|
|
|
void com_write_bytes(char *c, uint64_t n);
|
2024-01-22 21:45:56 +03:00
|
|
|
|
time_t rtc_get_time( );
|
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-09-16 21:42:02 +03:00
|
|
|
|
static inline void *hal_memset(void *s, char c, int64_t count) {
|
|
|
|
|
int64_t d0, d1;
|
|
|
|
|
asm volatile("rep\n\t"
|
|
|
|
|
"stosb"
|
|
|
|
|
: "=&c"(d0), "=&D"(d1)
|
|
|
|
|
: "a"(c), "1"(s), "0"(count)
|
|
|
|
|
: "memory");
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void *hal_memset_32(void *s, uint32_t c, int64_t count) {
|
|
|
|
|
int64_t d0, d1;
|
|
|
|
|
asm volatile("rep\n\t"
|
|
|
|
|
"stosl"
|
|
|
|
|
: "=&c"(d0), "=&D"(d1)
|
|
|
|
|
: "a"(c), "1"(s), "0"(count)
|
|
|
|
|
: "memory");
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 21:29:52 +03:00
|
|
|
|
static inline void *hal_memcpy(void *dest, const void *src, int64_t count) {
|
|
|
|
|
int64_t d0, d1;
|
|
|
|
|
asm volatile("rep\n\t"
|
|
|
|
|
"movsb"
|
|
|
|
|
: "=&c"(d0), "=&D"(d1)
|
|
|
|
|
: "S"(src), "0"(count), "1"(dest)
|
|
|
|
|
: "memory");
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void *hal_memcpy32(void *dest, const void *src, int64_t count) {
|
|
|
|
|
int64_t d0, d1;
|
|
|
|
|
asm volatile("rep\n\t"
|
|
|
|
|
"movsl"
|
|
|
|
|
: "=&c"(d0), "=&D"(d1)
|
|
|
|
|
: "S"(src), "0"(count), "1"(dest)
|
|
|
|
|
: "memory");
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
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
|