v2 initial commit
This commit is contained in:
parent
2c97dba9f7
commit
d2ef885e8d
118 changed files with 2420 additions and 9320 deletions
|
|
@ -1,52 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _ACPI_H_
|
||||
#define _ACPI_H_
|
||||
|
||||
#ifndef __ASM__
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct rsdp {
|
||||
char signature[8];
|
||||
uint8_t checksum;
|
||||
char oemid[6];
|
||||
uint8_t revision;
|
||||
uint32_t rsdt_addr;
|
||||
} PACKED;
|
||||
|
||||
struct acpi_sdt_header {
|
||||
char signature[4];
|
||||
uint32_t length;
|
||||
uint8_t revision;
|
||||
uint8_t checksum;
|
||||
char oem_id[6];
|
||||
char oem_table_id[8];
|
||||
uint32_t oem_revision;
|
||||
uint32_t creator_id;
|
||||
uint32_t creator_revision;
|
||||
} PACKED;
|
||||
|
||||
struct rsdt {
|
||||
struct acpi_sdt_header h;
|
||||
uint32_t sdt[];
|
||||
} PACKED;
|
||||
|
||||
struct mcfg {
|
||||
struct acpi_sdt_header h;
|
||||
uint64_t reserved;
|
||||
struct {
|
||||
uint64_t base_addr;
|
||||
uint16_t seg_group;
|
||||
uint8_t start_bus;
|
||||
uint8_t end_bus;
|
||||
uint32_t reserved;
|
||||
} PACKED cfg[];
|
||||
} PACKED;
|
||||
|
||||
void acpi_init();
|
||||
|
||||
extern struct madt *__madt;
|
||||
extern struct mcfg *__mcfg;
|
||||
|
||||
#endif /* __ASM__ */
|
||||
#endif /* _ACPI_H_ */
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _BITWISE_H_
|
||||
#define _BITWISE_H_
|
||||
|
||||
#ifndef __ASM__
|
||||
|
||||
#define BIT_SET(_v, _bit) ((_v) |= (1 << (_bit)))
|
||||
#define BIT_CLEAR(_v, _bit) ((_v) &= (~(1 << (_bit))))
|
||||
#define BIT_FLIP(_v, _bit) ((_v) ^= (1 << (_bit)))
|
||||
|
||||
#endif /* __ASM__ */
|
||||
#endif /* _BITWISE_H_ */
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _BLOCK_H_
|
||||
#define _BLOCK_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/gpt.h>
|
||||
|
||||
struct block_device;
|
||||
|
||||
struct block_device_ops {
|
||||
int (*read)(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||
int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||
};
|
||||
|
||||
struct block_device {
|
||||
char *name;
|
||||
struct block_device_ops ops;
|
||||
void *data;
|
||||
size_t size;
|
||||
|
||||
size_t block_count;
|
||||
size_t block_size;
|
||||
size_t lba_start;
|
||||
|
||||
size_t partition_count;
|
||||
|
||||
void *fs;
|
||||
struct block_device *parent;
|
||||
struct block_device *next_part; /* singly linked list */
|
||||
};
|
||||
|
||||
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data, size_t block_count, size_t block_size);
|
||||
struct block_device *block_get_device(const char *name);
|
||||
int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||
void kerror_print_blkdevs();
|
||||
|
||||
extern struct block_device *block_devices[];
|
||||
extern size_t block_devices_count;
|
||||
|
||||
#endif /* _BLOCK_H_ */
|
||||
11
include/kernel/buddy.h
Normal file
11
include/kernel/buddy.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _BUDDY_H_
|
||||
#define _BUDDY_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
void *buddy_alloc(size_t size);
|
||||
void buddy_free(void *ptr);
|
||||
void buddy_init();
|
||||
|
||||
#endif /* _BUDDY_H_ */
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _CHAR_H_
|
||||
#define _CHAR_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct char_device {
|
||||
char *name;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct char_device *char_device_create(const char *name, void *data);
|
||||
void char_device_destroy(struct char_device *cd);
|
||||
|
||||
#endif /* _CHAR_H_ */
|
||||
|
|
@ -6,43 +6,27 @@
|
|||
|
||||
#include <limine/limine.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <lib/stdio.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/errno.h>
|
||||
#include <lib/math.h>
|
||||
#include <lib/assert.h>
|
||||
#include <kernel/types.h>
|
||||
|
||||
#include <kernel/bitwise.h>
|
||||
#include <lib/stdio.h>
|
||||
|
||||
#define PACKED __attribute__((packed))
|
||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
#define ATTEMPT_WRITE(_x, _y) \
|
||||
if (_x) { \
|
||||
*(_x) = _y; \
|
||||
*(_x) = _y; \
|
||||
}
|
||||
|
||||
#define KB (1024ull)
|
||||
#define MB (KB * KB)
|
||||
#define GB (KB * MB)
|
||||
|
||||
#define RFLAGS_IF (1 << 9)
|
||||
|
||||
void yield();
|
||||
void panic();
|
||||
void cli();
|
||||
void sti();
|
||||
void _save_context();
|
||||
void reboot();
|
||||
void load_stack_and_jump(uintptr_t rsp, uintptr_t rbp, void *func, void *arg);
|
||||
#define panic(...) \
|
||||
{ \
|
||||
kprintf(LOG_PANIC __VA_ARGS__); \
|
||||
_panic(); \
|
||||
}
|
||||
|
||||
typedef uintptr_t paddr_t;
|
||||
typedef uint32_t paddr32_t;
|
||||
typedef int64_t ssize_t;
|
||||
typedef int mode_t;
|
||||
typedef long dev_t;
|
||||
void yield();
|
||||
void _panic();
|
||||
|
||||
#endif /* _COMMON_H_ */
|
||||
|
|
|
|||
19
include/kernel/device.h
Normal file
19
include/kernel/device.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _DEVICE_H_
|
||||
#define _DEVICE_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
|
||||
typedef u64 dev_t;
|
||||
typedef u64 dev_class_t;
|
||||
|
||||
|
||||
dev_t device_create(void *private, dev_class_t class, const char *name);
|
||||
void device_destroy(dev_t device);
|
||||
dev_class_t device_class(dev_t dev);
|
||||
void *device_private_info(dev_t dev);
|
||||
|
||||
void device_layer_init();
|
||||
|
||||
#endif /* _DEVICE_H_ */
|
||||
|
|
@ -1,136 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _ELF_H_
|
||||
#define _ELF_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/proc.h>
|
||||
|
||||
#define ET_NONE 0
|
||||
#define ET_REL 1
|
||||
#define ET_EXEC 2
|
||||
|
||||
#define EI_CLASS 4
|
||||
#define EI_DATA 5
|
||||
#define EI_VERSION 6
|
||||
#define EI_OSABI 7
|
||||
#define EI_ABIVERSION 8
|
||||
#define EI_PAD 9
|
||||
|
||||
#define EI_CLASS_64 2
|
||||
#define EI_OSABI_SYSV 0
|
||||
|
||||
#define EM_X86_64 62
|
||||
|
||||
#define EI_VERSION_CURRENT 1
|
||||
|
||||
#define ELFCLASS64 2
|
||||
#define ELFDATA2LSB 1 /* Little endian, two's complement */
|
||||
|
||||
#define PT_NULL 0
|
||||
#define PT_LOAD 1
|
||||
#define PT_DYNAMIC 2
|
||||
#define PT_INTERP 3
|
||||
#define PT_NOTE 4
|
||||
#define PT_SHLIB 5
|
||||
|
||||
#define R_X86_64_NONE 0
|
||||
#define R_X86_64_64 1
|
||||
#define R_X86_64_PC32 2
|
||||
#define R_X86_64_RELATIVE 8
|
||||
|
||||
#define SHT_NULL 0x0
|
||||
#define SHT_PROGBITS 0x1
|
||||
#define SHT_SYMTAB 0x2
|
||||
#define SHT_STRTAB 0x3
|
||||
#define SHT_RELA 0x4
|
||||
#define SHT_HASH 0x5
|
||||
#define SHT_DYNAMIC 0x6
|
||||
#define SHT_NOTE 0x7
|
||||
#define SHT_NOBITS 0x8
|
||||
#define SHT_REL 0x9
|
||||
#define SHT_SHLIB 0x0A
|
||||
#define SHT_DYNSYM 0x0B
|
||||
#define SHT_INIT_ARRAY 0x0E
|
||||
#define SHT_FINI_ARRAY 0x0F
|
||||
#define SHT_PREINIT_ARRAY 0x10
|
||||
|
||||
#define SHF_WRITE 1
|
||||
#define SHF_ALLOC 2
|
||||
#define SHF_EXECINSTR 4
|
||||
|
||||
#define STB_LOCAL 0
|
||||
#define STB_GLOBAL 1
|
||||
#define STB_WEAK 2
|
||||
|
||||
#define STT_NOTYPE 0
|
||||
#define STT_OBJECT 1
|
||||
#define STT_FUNC 2
|
||||
|
||||
#define SHN_UNDEF 0
|
||||
#define SHN_ABS 0xFFF1
|
||||
#define SHN_COMMON 0xFFF2
|
||||
|
||||
#define PF_X 0x1
|
||||
#define PF_W 0x2
|
||||
#define PF_R 0x4
|
||||
|
||||
#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
|
||||
#define ELF64_R_SYM(i) ((i) >> 32)
|
||||
|
||||
#define ELF_RELOC_ERR -1
|
||||
|
||||
typedef uint64_t Elf64_Addr;
|
||||
typedef uint64_t Elf64_Off;
|
||||
typedef uint16_t Elf64_Section;
|
||||
typedef uint16_t Elf64_Versym;
|
||||
typedef unsigned char Elf_Byte;
|
||||
typedef uint16_t Elf64_Half;
|
||||
typedef int32_t Elf64_Sword;
|
||||
typedef uint32_t Elf64_Word;
|
||||
typedef int64_t Elf64_Sxword;
|
||||
typedef uint64_t Elf64_Xword;
|
||||
|
||||
typedef struct {
|
||||
unsigned char e_ident[16];
|
||||
uint16_t e_type;
|
||||
uint16_t e_machine;
|
||||
uint32_t e_version;
|
||||
Elf64_Addr e_entry;
|
||||
Elf64_Off e_phoff;
|
||||
Elf64_Off e_shoff;
|
||||
uint32_t e_flags;
|
||||
uint16_t e_ehsize;
|
||||
uint16_t e_phentsize;
|
||||
uint16_t e_phnum;
|
||||
uint16_t e_shentsize;
|
||||
uint16_t e_shnum;
|
||||
uint16_t e_shstrndx;
|
||||
} PACKED Elf64_Ehdr;
|
||||
|
||||
typedef struct {
|
||||
uint32_t p_type;
|
||||
uint32_t p_flags;
|
||||
Elf64_Off p_offset;
|
||||
Elf64_Addr p_vaddr;
|
||||
Elf64_Addr p_paddr;
|
||||
uint64_t p_filesz;
|
||||
uint64_t p_memsz;
|
||||
uint64_t p_align;
|
||||
} PACKED Elf64_Phdr;
|
||||
|
||||
typedef struct {
|
||||
uint32_t sh_name;
|
||||
uint32_t sh_type;
|
||||
uint64_t sh_flags;
|
||||
Elf64_Addr sh_addr;
|
||||
Elf64_Off sh_offset;
|
||||
uint64_t sh_size;
|
||||
uint32_t sh_link;
|
||||
uint32_t sh_info;
|
||||
uint64_t sh_addralign;
|
||||
uint64_t sh_entsize;
|
||||
} PACKED Elf64_Shdr;
|
||||
|
||||
pid_t elf_load_proc(char *fname);
|
||||
|
||||
#endif /* _ELF_H_ */
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _GDT_H_
|
||||
#define _GDT_H_
|
||||
|
||||
#define GDT_NUMDESC 192
|
||||
#define GDT_SIZE (64 * GDT_NUMDESC)
|
||||
|
||||
#define GDT_ACCESS_DATA_RING0 0b10010010
|
||||
#define GDT_ACCESS_CODE_RING0 0b10011010
|
||||
|
||||
#define GDT_ACCESS_DATA_RING3 0b11110010
|
||||
#define GDT_ACCESS_CODE_RING3 0b11111010
|
||||
|
||||
#define GDT_SEGMENT_CODE_RING0 0x28
|
||||
#define GDT_SEGMENT_DATA_RING0 0x30
|
||||
|
||||
#define GDT_SEGMENT_DATA_RING3 0x38
|
||||
#define GDT_SEGMENT_CODE_RING3 0x40
|
||||
|
||||
#ifndef __ASM__
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct gdt_desc {
|
||||
uint16_t limit;
|
||||
uint16_t base;
|
||||
uint8_t base_2;
|
||||
uint8_t access;
|
||||
uint8_t flags;
|
||||
uint8_t base_3;
|
||||
} PACKED;
|
||||
|
||||
struct gdt_desc_sys {
|
||||
uint16_t limit;
|
||||
uint16_t base;
|
||||
uint8_t base_2;
|
||||
uint8_t access;
|
||||
uint8_t flags;
|
||||
uint8_t base_3;
|
||||
uint32_t base_4;
|
||||
uint32_t reserved;
|
||||
} PACKED;
|
||||
|
||||
struct tss {
|
||||
uint32_t reserved;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t reserved2;
|
||||
uint64_t ist1;
|
||||
uint64_t ist2;
|
||||
uint64_t ist3;
|
||||
uint64_t ist4;
|
||||
uint64_t ist5;
|
||||
uint64_t ist6;
|
||||
uint64_t ist7;
|
||||
uint64_t reserved3;
|
||||
uint16_t reserved4;
|
||||
uint16_t iomap_base;
|
||||
} PACKED;
|
||||
|
||||
struct gdtr {
|
||||
uint16_t size;
|
||||
uint64_t offset;
|
||||
} PACKED;
|
||||
|
||||
extern struct gdtr __gdtr;
|
||||
extern struct gdt_desc __gdt[GDT_NUMDESC];
|
||||
|
||||
void gdt_init();
|
||||
uint16_t gdt_insert_tss(uintptr_t rsp0);
|
||||
void ltr(uint16_t selector);
|
||||
|
||||
#endif /* __ASM__ */
|
||||
#endif /* _GDT_H_ */
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _GPT_H_
|
||||
#define _GPT_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct block_device;
|
||||
|
||||
/* GPT LBA 0 */
|
||||
struct gpt_mbr_prot {
|
||||
uint8_t boot[1];
|
||||
uint8_t chs_start[3];
|
||||
uint8_t type[1];
|
||||
uint8_t chs_end[3];
|
||||
uint8_t lba_start[4];
|
||||
uint8_t lba_size[4];
|
||||
} PACKED;
|
||||
|
||||
/* GPT LBA 1
|
||||
* checksums use CRC32
|
||||
*/
|
||||
struct gpt_header {
|
||||
uint64_t signature;
|
||||
uint8_t revision[4];
|
||||
uint8_t header_size[4];
|
||||
uint8_t checksum[4];
|
||||
uint8_t reserved[4];
|
||||
uint64_t lba_this;
|
||||
uint64_t lba_alt;
|
||||
uint64_t lba_first;
|
||||
uint64_t lba_last;
|
||||
uint8_t guid[16];
|
||||
uint64_t lba_table;
|
||||
uint32_t entry_count;
|
||||
uint32_t entry_size;
|
||||
uint32_t checksum_table;
|
||||
} PACKED;
|
||||
|
||||
/* GPT LBA 2 */
|
||||
struct gpt_entry {
|
||||
uint64_t type_guid[2];
|
||||
uint64_t unique_guid[2];
|
||||
uint64_t lba_first;
|
||||
uint64_t lba_last;
|
||||
uint64_t flags;
|
||||
uint8_t name[72];
|
||||
} PACKED;
|
||||
|
||||
void block_gpt_init(struct block_device *dev);
|
||||
|
||||
#endif /* _GPT_H_ */
|
||||
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _IDT_H_
|
||||
#define _IDT_H_
|
||||
|
||||
#define GATE_INTR 0x8E
|
||||
#define GATE_TRAP 0x8F
|
||||
|
||||
#define GATE_INTR_DPL3 0xEE
|
||||
|
||||
#define IDT_GATE_SIZE 16
|
||||
#define IDT_NUM_VECTORS 256
|
||||
#define IDT_SIZE (IDT_GATE_SIZE * IDT_NUM_VECTORS)
|
||||
|
||||
#ifndef __ASM__
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
struct irqdesc {
|
||||
uint16_t offset_low;
|
||||
uint16_t segment;
|
||||
uint8_t ist;
|
||||
uint8_t attrs;
|
||||
uint16_t offset_mid;
|
||||
uint32_t offset_high;
|
||||
uint32_t reserved;
|
||||
} PACKED;
|
||||
|
||||
struct idtr {
|
||||
uint16_t size;
|
||||
uint64_t offset;
|
||||
} PACKED;
|
||||
|
||||
void idt_init();
|
||||
void cli();
|
||||
void sti();
|
||||
|
||||
extern struct idtr __idtr;
|
||||
extern struct irqdesc __idt[IDT_NUM_VECTORS];
|
||||
|
||||
#endif /* __ASM__ */
|
||||
#endif /* _IDT_H_ */
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _IRQ_H_
|
||||
#define _IRQ_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
int irq_map(uint8_t irq, void *func);
|
||||
void irq_unmap(uint8_t irq);
|
||||
int irq_highest_free();
|
||||
|
||||
#endif /* _IRQ_H_ */
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _LOCK_H_
|
||||
#define _LOCK_H_
|
||||
|
||||
typedef int spinlock_t;
|
||||
typedef spinlock_t mtx_t;
|
||||
|
||||
int atomic_cmpxchg(volatile int *ptr, int cmpval, int newval);
|
||||
void spinlock_acquire(spinlock_t *lock);
|
||||
void spinlock_release(spinlock_t *lock);
|
||||
|
||||
void mtx_acquire(mtx_t *mtx);
|
||||
void mtx_release(mtx_t *mtx);
|
||||
|
||||
#endif /* _LOCK_H_ */
|
||||
|
|
@ -3,25 +3,12 @@
|
|||
#define _MEM_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/rbtree.h>
|
||||
|
||||
#define BBMAP_FREE 0
|
||||
#define BBMAP_SPLIT 1
|
||||
#define BBMAP_USED 2
|
||||
|
||||
#define PAGE_PRESENT 0x1
|
||||
#define PAGE_RW 0x2
|
||||
#define PAGE_USER 0x4
|
||||
#define PAGE_XD 0x8000000000000000
|
||||
|
||||
#define PAGE_PWT 0x8
|
||||
#define PAGE_PCD 0x10
|
||||
|
||||
#define MAP_ANONYMOUS 0x20
|
||||
|
||||
#ifndef __ASM__
|
||||
#include <kernel/spinlock.h>
|
||||
|
||||
#define ALIGN(_a) __attribute__((aligned(_a)))
|
||||
#define PHYSICAL_PAGE_MASK (~(hhdm_start))
|
||||
#define TO_VIRT(addr) ((addr) | hhdm_start)
|
||||
#define TO_PHYS(addr) ((addr) & PHYSICAL_PAGE_MASK)
|
||||
|
||||
#define genrw(reg) \
|
||||
uint64_t reg##_read(); \
|
||||
|
|
@ -35,90 +22,14 @@ genrw(ds);
|
|||
|
||||
#undef genrw
|
||||
|
||||
struct proc;
|
||||
|
||||
struct page { /* I would rather refer to this as a struct */
|
||||
union {
|
||||
struct {
|
||||
uint64_t present : 1;
|
||||
uint64_t rw : 1;
|
||||
uint64_t usermode : 1;
|
||||
uint64_t pwt : 1;
|
||||
uint64_t pcd : 1;
|
||||
uint64_t accessed : 1;
|
||||
uint64_t ignored : 1;
|
||||
uint64_t zero : 1;
|
||||
uint64_t ignored_2 : 4;
|
||||
uint64_t addr : 36;
|
||||
uint64_t reserved : 4;
|
||||
uint64_t ignored_3 : 11;
|
||||
uint64_t xd : 1;
|
||||
};
|
||||
uint64_t val;
|
||||
};
|
||||
};
|
||||
|
||||
struct cr3 {
|
||||
union {
|
||||
struct {
|
||||
uint64_t ignored : 3;
|
||||
uint64_t pwt : 1;
|
||||
uint64_t pcd : 1;
|
||||
uint64_t ignored_2 : 7;
|
||||
uint64_t pml4_addr : 52;
|
||||
};
|
||||
uint64_t val;
|
||||
};
|
||||
};
|
||||
|
||||
struct mem_region {
|
||||
paddr_t base;
|
||||
struct mem_zone {
|
||||
uintptr_t virt_base;
|
||||
size_t len;
|
||||
bool usable;
|
||||
};
|
||||
|
||||
struct buddy_region_header {
|
||||
paddr_t usable_base;
|
||||
size_t usable_len;
|
||||
size_t max_depth;
|
||||
|
||||
size_t bitmap_len;
|
||||
char bitmap[];
|
||||
};
|
||||
|
||||
extern uintptr_t __data_end;
|
||||
extern uintptr_t __bss_start;
|
||||
extern uintptr_t __bss_end;
|
||||
size_t mem_get_zones(struct mem_zone **ret);
|
||||
void mem_init();
|
||||
|
||||
extern uintptr_t hhdm_start;
|
||||
extern uintptr_t data_end;
|
||||
extern uintptr_t bss_start;
|
||||
extern uintptr_t bss_end;
|
||||
|
||||
extern struct mem_region *regions;
|
||||
extern size_t num_regions;
|
||||
extern size_t max_regions;
|
||||
|
||||
extern struct rbtree *kmap_tree;
|
||||
extern spinlock_t kmap_lock;
|
||||
|
||||
extern struct page *pml4;
|
||||
extern paddr_t kcr3;
|
||||
extern struct page kdefault_attrs;
|
||||
|
||||
void *buddy_alloc(size_t size);
|
||||
void buddy_free(void *paddr_hhdm);
|
||||
uintptr_t mmap_find_unmapped(struct rbtree *tree, spinlock_t *lock, uintptr_t start, size_t len);
|
||||
void kmap(paddr_t paddr, uintptr_t vaddr, size_t len, uint64_t attr);
|
||||
void *proc_mmap(struct proc *proc, paddr_t paddr, uintptr_t vaddr, size_t len, uint64_t attr);
|
||||
void *kmap_device(void *dev_paddr, size_t len);
|
||||
void proc_munmap(struct proc *proc, uintptr_t vaddr);
|
||||
paddr_t proc_clone_mmap(struct proc *in, struct proc *out);
|
||||
paddr_t virt_to_phys(uintptr_t vaddr, paddr_t page_base);
|
||||
void mem_early_init(char *mem, size_t len);
|
||||
uint64_t phys_read(paddr_t paddr);
|
||||
void phys_write(paddr_t paddr, uint64_t data);
|
||||
void *phys_memcpy(void *dest, paddr_t src, size_t num);
|
||||
|
||||
#endif /* __ASM__ */
|
||||
#endif /* _MEM_H_ */
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _MSR_H_
|
||||
#define _MSR_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define MSR_APIC_BASE 0x1B
|
||||
|
||||
#define MSR_IA32_EFER 0xC0000080
|
||||
#define MSR_IA32_STAR 0xC0000081
|
||||
#define MSR_IA32_LSTAR 0xC0000082
|
||||
#define MSR_IA32_FMASK 0xC0000084
|
||||
|
||||
#ifndef __ASM__
|
||||
|
||||
uint64_t rdmsr(uint32_t msr);
|
||||
void wrmsr(uint32_t msr, uint64_t val);
|
||||
|
||||
#endif /* __ASM__ */
|
||||
#endif /* _MSR_H_ */
|
||||
25
include/kernel/page.h
Normal file
25
include/kernel/page.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _PAGE_H_
|
||||
#define _PAGE_H_
|
||||
|
||||
#include <kernel/types.h>
|
||||
|
||||
#define PAGE_FLAGS_EMPTY(flags) (!((flags) & PAGE_PRESENT))
|
||||
|
||||
typedef struct {
|
||||
u64 pgd;
|
||||
} pgd_t; /* PML4 entries */
|
||||
typedef struct {
|
||||
u64 pud;
|
||||
} pud_t; /* PDPT entries */
|
||||
typedef struct {
|
||||
u64 pmd;
|
||||
} pmd_t; /* PD entries */
|
||||
typedef struct {
|
||||
u64 pte;
|
||||
} pte_t; /* page table entries */
|
||||
|
||||
void page_init();
|
||||
pgd_t *pgd_get();
|
||||
|
||||
#endif /* _PAGE_H_ */
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _PIO_H_
|
||||
#define _PIO_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define genio(suffix, type) \
|
||||
void out##suffix(uint16_t port, type value); \
|
||||
type in##suffix(uint16_t port);
|
||||
|
||||
genio(b, uint8_t);
|
||||
genio(w, uint16_t);
|
||||
genio(l, uint32_t);
|
||||
|
||||
#undef genio
|
||||
|
||||
void iowait();
|
||||
|
||||
#endif /* _PIO_H_ */
|
||||
|
|
@ -1,139 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _PROC_H_
|
||||
#define _PROC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <kernel/lock.h>
|
||||
#include <kernel/rbtree.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <lib/sem.h>
|
||||
|
||||
#define PROC_RUNNING 0
|
||||
#define PROC_STOPPED 1
|
||||
#define PROC_ALLOWSCHED 1
|
||||
#define PROC_BLOCKED 2
|
||||
#define PROC_YIELD 3
|
||||
#define PROC_ZOMBIE 4
|
||||
|
||||
#define PT_USER 0
|
||||
#define PT_KERN 1
|
||||
|
||||
#define STDIN_FILENO 0
|
||||
#define STDOUT_FILENO 1
|
||||
#define STDERR_FILENO 2
|
||||
|
||||
#define USER_STACK_BASE 0x0000070000000000
|
||||
#define USER_HEAP_BASE 0x0000000100000000 /* 4GB */
|
||||
|
||||
#define PM_BUD 0
|
||||
#define PM_SLB 1
|
||||
|
||||
typedef int64_t pid_t;
|
||||
|
||||
struct proc_mmap_entry {
|
||||
uintptr_t vaddr;
|
||||
paddr_t paddr;
|
||||
size_t len;
|
||||
uint64_t attr;
|
||||
uint64_t type;
|
||||
};
|
||||
|
||||
struct procregs {
|
||||
uint64_t rax;
|
||||
uint64_t rbx;
|
||||
uint64_t rcx;
|
||||
uint64_t rdx;
|
||||
uint64_t rsi;
|
||||
uint64_t rdi;
|
||||
uint64_t rbp;
|
||||
uint64_t r8;
|
||||
uint64_t r9;
|
||||
uint64_t r10;
|
||||
uint64_t r11;
|
||||
uint64_t r12;
|
||||
uint64_t r13;
|
||||
uint64_t r14;
|
||||
uint64_t r15;
|
||||
|
||||
/* IRETQ stack frame */
|
||||
uint64_t rip;
|
||||
uint64_t cs;
|
||||
uint64_t rflags;
|
||||
uint64_t rsp;
|
||||
uint64_t ss;
|
||||
|
||||
} PACKED;
|
||||
|
||||
struct proc_mapping {
|
||||
uintptr_t vaddr;
|
||||
paddr_t paddr;
|
||||
size_t len;
|
||||
uint64_t attr;
|
||||
uint64_t type;
|
||||
slab_t *slab;
|
||||
struct proc_mapping *next;
|
||||
};
|
||||
|
||||
struct proc_allocation {
|
||||
struct proc_mapping *mappings;
|
||||
struct proc_mapping *last;
|
||||
size_t num_mappings;
|
||||
};
|
||||
|
||||
struct proc {
|
||||
struct procregs regs;
|
||||
uint8_t state;
|
||||
pid_t pid;
|
||||
pid_t ppid;
|
||||
uint64_t cr3;
|
||||
spinlock_t lock;
|
||||
|
||||
bool is_kernel;
|
||||
|
||||
struct rbtree page_map;
|
||||
spinlock_t page_map_lock;
|
||||
|
||||
struct rbtree fd_map;
|
||||
spinlock_t fd_map_lock;
|
||||
|
||||
struct proc *parent;
|
||||
struct rbtree children;
|
||||
|
||||
struct proc *buddy_proc;
|
||||
|
||||
uintptr_t stack_start;
|
||||
uintptr_t stack_size;
|
||||
|
||||
uintptr_t heap_start;
|
||||
|
||||
struct rbtree umalloc_tree;
|
||||
};
|
||||
|
||||
struct procregs *proc_current_regs();
|
||||
struct proc *proc_create();
|
||||
struct proc *proc_createv(int flags);
|
||||
void proc_init_memory(struct proc *proc, uint64_t mem_flags);
|
||||
void proc_init_page_tables(struct proc *proc);
|
||||
struct proc *proc_fork(struct proc *parent);
|
||||
struct proc *proc_get(pid_t pid);
|
||||
pid_t getpid();
|
||||
pid_t getupid();
|
||||
void proc_term(pid_t pid);
|
||||
struct proc *proc_find(pid_t pid);
|
||||
void proc_save_state();
|
||||
void proc_set_current(pid_t pid);
|
||||
void proc_init(unsigned num_cpus);
|
||||
void trap_sched();
|
||||
void schedule();
|
||||
void syscall_block();
|
||||
void proc_set_exec_addr(struct proc *proc, uintptr_t addr);
|
||||
void proc_set_flags(struct proc *proc, uint64_t flags);
|
||||
void proc_set_stack(struct proc *proc, uintptr_t base, size_t size);
|
||||
void proc_set_state(pid_t pid, uint8_t state);
|
||||
|
||||
void sswtch();
|
||||
|
||||
#endif /* _PROC_H_ */
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _RBTREE_H_
|
||||
#define _RBTREE_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/lock.h>
|
||||
|
||||
#define RB_RED 0
|
||||
#define RB_BLACK 1
|
||||
|
||||
struct rbnode {
|
||||
struct rbnode *parent;
|
||||
struct rbnode *left;
|
||||
struct rbnode *right;
|
||||
int color;
|
||||
|
||||
uint64_t key;
|
||||
uint64_t value; /* extra values */
|
||||
uint64_t value2;
|
||||
uint64_t value3;
|
||||
};
|
||||
|
||||
/* we use this rbtree structure because the root node will potentially be
|
||||
* changed when we insert or delete a node
|
||||
*/
|
||||
struct rbtree {
|
||||
struct rbnode *root;
|
||||
spinlock_t lock;
|
||||
size_t num_nodes;
|
||||
};
|
||||
|
||||
struct rbnode *rbt_insert(struct rbtree *tree, uint64_t key);
|
||||
void rbt_delete(struct rbtree *tree, struct rbnode *del);
|
||||
void rbt_destroy(struct rbtree *tree);
|
||||
struct rbnode *rbt_search(struct rbtree *tree, uint64_t key);
|
||||
struct rbnode *rbt_successor(struct rbnode *node);
|
||||
uint64_t rbt_next_key(struct rbtree *tree);
|
||||
struct rbnode *rbt_range_val2(struct rbtree *tree, uint64_t sval, uint64_t len);
|
||||
struct rbnode *rbt_minimum(struct rbnode *node);
|
||||
|
||||
void rbt_slab_init();
|
||||
|
||||
#endif /* _RBTREE_H_ */
|
||||
13
include/kernel/requests.h
Normal file
13
include/kernel/requests.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _REQUESTS_H_
|
||||
#define _REQUESTS_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
extern struct limine_executable_address_response *kern_addr_res;
|
||||
extern struct limine_memmap_response *kern_map_res;
|
||||
extern struct limine_hhdm_response *kern_hhdm_res;
|
||||
|
||||
void requests_init();
|
||||
|
||||
#endif /* _REQUESTS_H_ */
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _RINGBUF_H
|
||||
#define _RINGBUF_H
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
typedef struct _ringbuf {
|
||||
char *buf;
|
||||
size_t size;
|
||||
size_t head;
|
||||
size_t tail;
|
||||
} ringbuf_t;
|
||||
|
||||
ringbuf_t *ringbuf_create(size_t size);
|
||||
void ringbuf_destroy(ringbuf_t *rb);
|
||||
|
||||
size_t ringbuf_write(ringbuf_t *rb, const char *buf, size_t count);
|
||||
size_t ringbuf_read(ringbuf_t *rb, char *buf, size_t count);
|
||||
|
||||
#endif /* _RINGBUF_H */
|
||||
|
|
@ -3,62 +3,23 @@
|
|||
#define _SLAB_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/lock.h>
|
||||
#include <kernel/spinlock.h>
|
||||
|
||||
#define SLAB_PAGE_ALIGN 0x01
|
||||
#define SLAB_DMA_64K 0x02
|
||||
typedef struct _mem_cache_t {
|
||||
size_t item_size;
|
||||
size_t cache_size;
|
||||
size_t offset;
|
||||
size_t alignment; /* must be a power of 2 */
|
||||
size_t num_members;
|
||||
size_t num_free;
|
||||
uintptr_t next_free;
|
||||
spinlock_t lock;
|
||||
struct _mem_cache_t *next;
|
||||
} mem_cache_t;
|
||||
|
||||
#define ALLOC_KERN 0x01
|
||||
#define ALLOC_DMA 0x02
|
||||
|
||||
#define UA_SLAB 0x1
|
||||
#define UA_BUD 0x2
|
||||
|
||||
#define ALLOC_USER 0x0
|
||||
#define ALLOC_USER_STACK 0x1
|
||||
#define ALLOC_USER_RDONLY 0x2
|
||||
#define ALLOC_USER_EXEC 0x4
|
||||
|
||||
#define ATTEMPT_FREE(_x) \
|
||||
if (_x) { \
|
||||
kfree(_x); \
|
||||
_x = NULL; \
|
||||
}
|
||||
|
||||
typedef struct _slab {
|
||||
size_t size; /* size of each object */
|
||||
size_t tsize; /* total size of this slab */
|
||||
size_t num; /* number of objects in this slab */
|
||||
size_t free; /* number of free objects */
|
||||
uint64_t flags;
|
||||
size_t align;
|
||||
uintptr_t *nextfree;
|
||||
struct _slab *next;
|
||||
struct _slab *prev;
|
||||
|
||||
spinlock_t lock;
|
||||
} slab_t;
|
||||
|
||||
struct kmap_entry {
|
||||
uintptr_t *ptr;
|
||||
size_t size;
|
||||
slab_t *slab;
|
||||
struct kmap_entry *next;
|
||||
};
|
||||
|
||||
slab_t *slab_create(size_t size, size_t cache_size, uint64_t flags);
|
||||
void *slab_alloc(slab_t *slab);
|
||||
void slab_free(slab_t *slab, void *ptr);
|
||||
|
||||
void kmalloc_init();
|
||||
void *kmalloc(size_t size, uint64_t flags);
|
||||
void *kzalloc(size_t size, uint64_t flags);
|
||||
void *krealloc(void *ptr, size_t size, uint64_t flags);
|
||||
void kfree(void *ptr);
|
||||
void *umalloc(struct proc *proc, size_t size, uint64_t opts, uintptr_t req_addr);
|
||||
void ufree(struct proc *proc, void *ptr);
|
||||
|
||||
void slabtypes_init();
|
||||
mem_cache_t *slab_create(size_t item_size, size_t cache_size, size_t alignment);
|
||||
void *slab_alloc(mem_cache_t *cache);
|
||||
void slab_free(mem_cache_t *cache, void *ptr);
|
||||
|
||||
#endif /* _SLAB_H_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _SYSCALL_H_
|
||||
#define _SYSCALL_H_
|
||||
|
||||
#define SYS_READ 0
|
||||
#define SYS_WRITE 1
|
||||
#define SYS_OPEN 2
|
||||
#define SYS_MMAP 9
|
||||
#define SYS_MUNMAP 11
|
||||
#define SYS_FORK 57
|
||||
#define SYS_EXIT 60
|
||||
#define SYS_MKDIR 83
|
||||
#define SYS_MKNOD 133
|
||||
#define SYS_LSDIR 254
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/proc.h>
|
||||
|
||||
void sys_set_return(struct proc *proc, uint64_t ret);
|
||||
void sswtch(); /* save and switch */
|
||||
|
||||
#endif /* _SYSCALL_H_ */
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _TRAP_H_
|
||||
#define _TRAP_H_
|
||||
|
||||
void exception(int vector, int error);
|
||||
void exception_init();
|
||||
|
||||
#endif
|
||||
27
include/kernel/types.h
Normal file
27
include/kernel/types.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _TYPES_H_
|
||||
#define _TYPES_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define KB (1024ull)
|
||||
#define MB (KB * KB)
|
||||
#define GB (MB * MB)
|
||||
|
||||
typedef uintptr_t paddr_t;
|
||||
typedef uint32_t paddr32_t;
|
||||
typedef int64_t ssize_t;
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef int32_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
#endif /* _TYPES_H_ */
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _UMEM_H_
|
||||
#define _UMEM_H_
|
||||
|
||||
#include <kernel/proc.h>
|
||||
|
||||
void copy_to_user(struct proc *proc, void *dst, const void *src, size_t size);
|
||||
|
||||
#endif /* _UMEM_H_ */
|
||||
Loading…
Reference in a new issue