This commit is contained in:
p-w-rs
2022-09-07 11:18:56 -05:00
commit 7bb91e666d
121 changed files with 5306 additions and 0 deletions

232
TTOS/include/elf.h Normal file
View File

@@ -0,0 +1,232 @@
#ifndef _ELF_H
#define _ELF_H
#include <stdint.h>
#include "errorCode.h"
/* https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html */
/* ELF Data Types */
typedef uint32_t Elf32_Addr;
typedef uint32_t Elf32_Off;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Word;
typedef int32_t Elf32_Sword;
typedef uint64_t Elf64_Addr;
typedef uint64_t Elf64_Off;
typedef uint16_t Elf64_Half;
typedef uint32_t Elf64_Word;
typedef int32_t Elf64_Sword;
typedef uint64_t Elf64_Xword;
typedef int64_t Elf64_Sxword;
/* ELF Magic */
#define ELFMAG0 0x7f
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
/* Class */
#define ELFCLASSNONE 0
#define ELFCLASS32 1
#define ELFCLASS64 2
/* Data encoding */
#define ELFDATANONE 0
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
/* Version */
#define EV_NONE 0
#define EV_CURRENT 1
/* Type */
#define ET_NONE 0
#define ET_REL 1
#define ET_EXEC 2
#define ET_DYN 3
#define ET_CORE 4
#define ET_LOOS 0xfe00
#define ET_HIOS 0xfeff
#define ET_LOPROC 0xff00
#define ET_HIPROC 0xffff
#define EI_NIDENT 16
typedef struct {
uint8_t e_ident[EI_NIDENT];
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Elf64_Addr e_entry;
Elf64_Off e_phoff;
Elf64_Off e_shoff;
Elf64_Word e_flags;
Elf64_Half e_ehsize;
Elf64_Half e_phentsize;
Elf64_Half e_phnum;
Elf64_Half e_shentsize;
Elf64_Half e_shnum;
Elf64_Half e_shstrndx;
} Elf64_Ehdr;
/* Program header types */
#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 PT_PHDR 6
#define PT_TLS 7
#define PT_LOOS 0x60000000
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
typedef struct {
Elf64_Word p_type;
Elf64_Word p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
Elf64_Xword p_filesz;
Elf64_Xword p_memsz;
Elf64_Xword p_align;
} Elf64_Phdr;
/* Section header type */
#define SHT_NULL 0
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2
#define SHT_STRTAB 3
#define SHT_RELA 4
#define SHT_HASH 5
#define SHT_DYNAMIC 6
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_REL 9
#define SHT_SHLIB 10
#define SHT_DYNSYM 11
#define SHT_INIT_ARRAY 14
#define SHT_FINI_ARRAY 15
#define SHT_PREINIT_ARRAY 16
#define SHT_GROUP 17
#define SHT_SYMTAB_SHNDX 18
#define SHT_LOOS 0x60000000
#define SHT_HIOS 0x6fffffff
#define SHT_LOPROC 0x70000000
#define SHT_HIPROC 0x7fffffff
#define SHT_LOUSER 0x80000000
#define SHT_HIUSER 0xffffffff
/* Section header name index */
#define SHN_UNDEF 0
#define SHN_LORESERVE 0xff00
#define SHN_LOPROC 0xff00
#define SHN_HIPROC 0xff1f
#define SHN_ABS 0xfff1
#define SHN_COMMON 0xfff2
#define SHN_HIRESERVE 0xffff
/* Section header flags */
#define SHF_WRITE 0x1
#define SHF_ALLOC 0x2
#define SHF_EXECINSTR 0x4
#define SHF_MERGE 0x10
#define SHF_STRINGS 0x20
#define SHF_INFO_LINK 0x40
#define SHF_LINK_ORDER 0x80
#define SHF_OS_NONCONFORMING 0x100
#define SHF_GROUP 0x200
#define SHF_TLS 0x400
#define SHF_MASKOS 0x0ff00000
#define SHF_MASKPROC 0xf0000000
typedef struct {
Elf64_Word sh_name;
Elf64_Word sh_type;
Elf64_Xword sh_flags;
Elf64_Addr sh_addr;
Elf64_Off sh_offset;
Elf64_Xword sh_size;
Elf64_Word sh_link;
Elf64_Word sh_info;
Elf64_Xword sh_addralign;
Elf64_Xword sh_entsize;
} Elf64_Shdr;
/* Symbol table index values */
#define STN_UNDEF 0
typedef struct {
Elf64_Word st_name;
uint8_t st_info;
uint8_t st_other;
Elf64_Half st_shndx;
Elf64_Addr st_value;
Elf64_Xword st_size;
} Elf64_Sym;
typedef struct {
Elf64_Addr r_offset;
Elf64_Xword r_info;
} Elf64_Rel;
typedef struct {
Elf64_Addr r_offset;
Elf64_Xword r_info;
Elf64_Sxword r_addend;
} Elf64_Rela;
#define R_X86_64_NONE 0
#define R_X86_64_64 1
#define R_X86_64_PC32 2
#define R_X86_64_GOT32 3
#define R_X86_64_PLT32 4
#define R_X86_64_COPY 5
#define R_X86_64_GLOB_DAT 6
#define R_X86_64_JUMP_SLOT 7
#define R_X86_64_RELATIVE 8
#define R_X86_64_GOTPCREL 9
#define R_X86_64_32 10
#define R_X86_64_32S 11
#define R_X86_64_16 12
#define R_X86_64_PC16 13
#define R_X86_64_8 14
#define R_X86_64_PC8 15
#define R_X86_64_DPTMOD64 16
#define R_X86_64_DTPOFF64 17
#define R_X86_64_TPOFF64 18
#define R_X86_64_TLSGD 19
#define R_X86_64_TLSLD 20
#define R_X86_64_DTPOFF32 21
#define R_X86_64_GOTTPOFF 22
#define R_X86_64_TPOFF32 23
#define R_X86_64_PC64 24
#define R_X86_64_GOTOFF64 25
#define R_X86_64_GOTPC32 26
#define R_X86_64_SIZE32 32
#define R_X86_64_SIZE64 33
#define ELF64_R_SYM(i) ((i)>>32)
#define ELF64_R_TYPE(i) ((i)&0xffffffffL)
#define ELF64_R_INFO(s,t) (((s)<<32)+((t)&0xffffffffL))
typedef struct elf_file {
int size;
void* contents;
Elf64_Ehdr* ehdr;
Elf64_Phdr* phdr;
Elf64_Shdr* shdr;
} elf_file;
errorCode_t load_elf(const char* filename, elf_file* file);
errorCode_t get_elf_proc_size(elf_file* file, uint64_t* size);
errorCode_t populate_elf_proc(elf_file* file, void* tgt_addr);
errorCode_t find_symbol(elf_file* file, const char* name, uint64_t* offset);
errorCode_t free_elf(elf_file* file);
#endif

15
TTOS/include/errorCode.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _ERRORCODE_H
#define _ERRORCODE_H
typedef int errorCode_t;
#define IS_ERROR(n) (n < SUCCESS)
#define SUCCESS 0
#define EBADPARM -1
#define ENOTFOUND -2
#define EBADEXEC -3
#define EMEMORY -4
#define EFILESYS -5
#endif

22
TTOS/include/fs/fs.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef _FS_H
#define _FS_H
#include "errorCode.h"
/* fs_open flags */
#define O_RDONLY 00000000
#define O_WRONLY 00000001
#define O_RDWR 00000002
/* fs_seek whence */
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
errorCode_t fs_open(const char* path, int flags, int* fd);
errorCode_t fs_write(int fd, const void *buf, int count, int* bytes_written);
errorCode_t fs_read(int fd, void *buf, int count, int* bytes_read);
errorCode_t fs_seek(int fd, int offset, int whence, int* distance);
errorCode_t fs_close(int fd);
#endif

15
TTOS/include/memory.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _MEMORY_H
#define _MEMORY_H
#include "errorCode.h"
#define PAGE_SIZE 4096
#define PAGE_ALIGN(n) (n + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1)
errorCode_t allocate_user_memory(int size, void** addr);
errorCode_t free_user_memory(void* addr, int size);
errorCode_t get_kernel_memory(int size, void** addr);
errorCode_t free_kernel_memory(void* addr, int size);
#endif

19
TTOS/include/process.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef _PROCESS_H
#define _PROCESS_H
#define STACK_SIZE 8192
#include "elf.h"
typedef struct process {
void* entry_point;
void* exec_start;
void* stack_start;
void* stack_end;
int size;
} process;
errorCode_t create_process(const char* file, process* proc);
errorCode_t free_process(process* proc);
#endif

6
TTOS/include/syscall.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef _SYSCALL_H
#define _SYSCALL_H
long system_call(long n, ...);
#endif