diff --git a/fs/ext2.c b/fs/ext2.c index 892e1f7..0cb6f1e 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -942,6 +942,8 @@ static int ext2_read_file(struct vnode *vnode, void *buf, size_t offset, size_t offset = 0; count -= MIN(fs->block_size, count); + + block++; } return ret; diff --git a/include/kernel/elf.h b/include/kernel/elf.h index e551bcb..b54a7e8 100644 --- a/include/kernel/elf.h +++ b/include/kernel/elf.h @@ -38,16 +38,25 @@ #define R_X86_64_PC32 2 #define R_X86_64_RELATIVE 8 -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_NOBITS 8 -#define SHT_REL 9 +#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 @@ -65,19 +74,30 @@ #define PF_W 0x2 #define PF_R 0x4 -#define ELF64_R_TYPE(i) ((i)&0xffffffff) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) #define ELF64_R_SYM(i) ((i) >> 32) #define ELF_RELOC_ERR -1 -struct elf64_header { - uint8_t e_ident[16]; +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; - uint64_t e_entry; - uint64_t e_phoff; - uint64_t e_shoff; + Elf64_Addr e_entry; + Elf64_Off e_phoff; + Elf64_Off e_shoff; uint32_t e_flags; uint16_t e_ehsize; uint16_t e_phentsize; @@ -85,51 +105,31 @@ struct elf64_header { uint16_t e_shentsize; uint16_t e_shnum; uint16_t e_shstrndx; -} PACKED; +} PACKED Elf64_Ehdr; -struct elf64_program_header { +typedef struct { uint32_t p_type; uint32_t p_flags; - uint64_t p_offset; - uint64_t p_vaddr; - uint64_t p_paddr; + 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; +} PACKED Elf64_Phdr; -struct elf64_section_header { +typedef struct { uint32_t sh_name; uint32_t sh_type; uint64_t sh_flags; - uint64_t sh_addr; - uint64_t sh_offset; + 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; - -struct elf64_symbol { - uint32_t st_name; - uint8_t st_info; - uint8_t st_other; - uint16_t st_shndx; - uint64_t st_value; - uint64_t st_size; -} PACKED; - -struct elf64_rel { - uint64_t r_offset; - uint64_t r_info; -} PACKED; - -struct elf64_rela { - uint64_t r_offset; - uint64_t r_info; - int64_t r_addend; -} PACKED; +} PACKED Elf64_Shdr; pid_t elf_load_proc(char *fname); diff --git a/include/kernel/proc.h b/include/kernel/proc.h index 64a9fd3..5bd84d5 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -25,7 +25,7 @@ #define STDOUT_FILENO 1 #define STDERR_FILENO 2 -#define USER_STACK_BASE 0x00007FFFF0000000 +#define USER_STACK_BASE 0x0000070000000000 #define USER_HEAP_BASE 0x0000000100000000 /* 4GB */ #define PM_BUD 0 @@ -116,6 +116,7 @@ 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(); diff --git a/lib/stdio.c b/lib/stdio.c index 87ddd87..901ad27 100644 --- a/lib/stdio.c +++ b/lib/stdio.c @@ -96,6 +96,17 @@ int vsnprintf(char *str, const char *restrict fmt, size_t size, va_list args) goto printf_reverse; break; + case 'b': + u = va_arg(args, int); + + n = 0; + + memset(buf, 0, sizeof(buf)); + + str[0] = convert[(u>>4)&0xF]; + str[1] = convert[u&0xF]; + ret += 2; + break; case 'X': l = va_arg(args, long); for (uint64_t j = 0; j < (sizeof(l) << 1); j++) { diff --git a/mem/slab.c b/mem/slab.c index 60d1497..f0a30f0 100644 --- a/mem/slab.c +++ b/mem/slab.c @@ -424,13 +424,14 @@ void *umalloc(struct proc *proc, size_t size, uint64_t opts) size_t this_size = uslab_sizes[ARRAY_SIZE(uslab_sizes) - 1]; size_t this_index = ARRAY_SIZE(uslab_sizes) - 1; - size_t i = this_size; - size_t j = this_index; - while (i > remaining && j) { - this_size = uslab_sizes[j]; - - j--; - i = uslab_sizes[j]; + if (this_size > remaining) { + for (size_t i = 0; i < ARRAY_SIZE(uslab_sizes); i++) { + if (uslab_sizes[i] >= remaining) { + this_size = uslab_sizes[i]; + this_index = i; + break; + } + } } void *ret = slab_alloc(uslab_cache[this_index]); @@ -445,6 +446,9 @@ void *umalloc(struct proc *proc, size_t size, uint64_t opts) total_alloc += this_size; } + + if (opts & ALLOC_USER_STACK) + proc->stack_size += total_alloc; } struct rbnode *n = rbt_insert(&proc->umalloc_tree, (uint64_t)vaddr); diff --git a/sched/load.c b/sched/load.c index afce5ad..f5c1865 100644 --- a/sched/load.c +++ b/sched/load.c @@ -14,7 +14,7 @@ int elf_check_compat(char *buf) if (strncmp(buf, elf_magic, 4)) return -ENOEXEC; - struct elf64_header *hdr = (struct elf64_header *)buf; + Elf64_Ehdr *hdr = (Elf64_Ehdr *)buf; /* must be 64-bit */ if (hdr->e_ident[EI_CLASS] != ELFCLASS64) @@ -35,6 +35,11 @@ int elf_check_compat(char *buf) return 0; } +Elf64_Shdr *elf_shdr(Elf64_Ehdr *hdr) +{ + return (Elf64_Shdr *)((uintptr_t)hdr + hdr->e_shoff); +} + pid_t elf_load_proc(char *fname) { int err; @@ -52,6 +57,16 @@ pid_t elf_load_proc(char *fname) vfs_read(file, buf, 0, size); vfs_close(file); + for (int i = 0; i < size; i++) { + kprintf("%b", buf[i]); + + if((i & 0xF) == 0xF) { + kprintf("\n"); + kprintf("%x: ", i + 1); + } + } + kprintf("\n"); + int ret; if ((ret = elf_check_compat(buf))) { @@ -59,7 +74,7 @@ pid_t elf_load_proc(char *fname) return ret; } - struct elf64_header *hdr = (struct elf64_header *)buf; + Elf64_Ehdr *hdr = (Elf64_Ehdr *)buf; /* read data and text segments */ @@ -73,7 +88,7 @@ pid_t elf_load_proc(char *fname) proc->cr3 &= (~hhdm_start); - struct elf64_program_header *phdr = (struct elf64_program_header *)(buf + hdr->e_phoff); + Elf64_Phdr *phdr = (Elf64_Phdr *)(buf + hdr->e_phoff); for (unsigned int i = 0; i < hdr->e_phnum; i++) { if (phdr[i].p_type != PT_LOAD) continue; @@ -81,9 +96,6 @@ pid_t elf_load_proc(char *fname) uintptr_t vaddr = phdr[i].p_vaddr; size_t len = MAX(0x1000, npow2(phdr[i].p_filesz)); uintptr_t paddr = (uintptr_t)buddy_alloc(len); - - /* length to copy, not allocated length */ - len = phdr[i].p_filesz; uint64_t flags = PAGE_PRESENT | PAGE_USER; if (phdr[i].p_flags & PF_W) @@ -93,7 +105,15 @@ pid_t elf_load_proc(char *fname) flags |= PAGE_XD; (void)proc_mmap(proc, paddr & (~hhdm_start), vaddr, len, flags); - memcpy((void *)paddr + (vaddr & 0xFFF), buf + phdr[i].p_offset, len); + memcpy((void *)paddr + (vaddr & 0xFFF), buf + phdr[i].p_offset, phdr[i].p_filesz); + } + + Elf64_Shdr *shdr_list = (Elf64_Shdr *)(buf + hdr->e_shoff); + Elf64_Shdr *shdr_str = &shdr_list[hdr->e_shstrndx]; + + for (unsigned int i = 0; i < hdr->e_shnum; i++) { + Elf64_Shdr *section = &shdr_list[i]; + kprintf("section: %d, type=%d\n", i, section->sh_type); } /* set entry point */ @@ -107,10 +127,12 @@ pid_t elf_load_proc(char *fname) /* allocate user stack */ proc->stack_start = USER_STACK_BASE; - void *ustack = umalloc(proc, 0x4000, ALLOC_USER_STACK); + proc->stack_size = 0; /* umalloc will grow the stack */ + void *ustack = umalloc(proc, 0x10000, ALLOC_USER_STACK); /* set stack pointer */ - proc->regs.rsp = (uintptr_t)ustack + 0x3FF0; + proc->regs.rsp = proc->stack_start + proc->stack_size; + proc->regs.rbp = proc->regs.rsp; kfree(buf); diff --git a/sched/proc.c b/sched/proc.c index 7f960e1..7faae9e 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -236,6 +236,13 @@ void schedule() yield(); } +void proc_init_page_tables(struct proc *proc) +{ + proc->cr3 = (uintptr_t)buddy_alloc(0x1000); + phys_memcpy((void *)proc->cr3, kcr3, 0x1000); + proc->cr3 = virt_to_phys(proc->cr3, kcr3); +} + void proc_init_memory(struct proc *proc, uint64_t mem_flags) { if (proc->is_kernel && proc->buddy_proc) { @@ -250,8 +257,6 @@ void proc_init_memory(struct proc *proc, uint64_t mem_flags) proc->stack_start = stackaddr; if (!proc->is_kernel) { - stackaddr &= ~(hhdm_start); - proc_mmap(proc, stackaddr | hhdm_start, stackaddr, 0x4000, PAGE_XD | PAGE_RW | PAGE_PRESENT); proc->regs.cs = GDT_SEGMENT_CODE_RING3 | 3; proc->regs.ss = GDT_SEGMENT_DATA_RING3 | 3; diff --git a/usr.bin/shtest/Makefile b/usr.bin/shtest/Makefile index 3476a09..aedad1f 100644 --- a/usr.bin/shtest/Makefile +++ b/usr.bin/shtest/Makefile @@ -11,7 +11,7 @@ SSRC := syscall.S OBJ := $(CSRC:.c=.o) $(SSRC:.S=.o) DEP := $(OBJ:.o=.d) -CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD -g +CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD LDFLAGS := -nostdlib -static -m elf_x86_64 CURDIR := $(shell pwd | awk -F '/' '{print $$(NF-1),"/",$$NF}' | sed 's/ //g') diff --git a/usr.bin/shtest/shtest.c b/usr.bin/shtest/shtest.c index 08aa2c6..a825010 100644 --- a/usr.bin/shtest/shtest.c +++ b/usr.bin/shtest/shtest.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* shtest: simple userland shell test */ +#include #include +#include #define S_IFIFO 0x1000 @@ -27,6 +29,48 @@ void _start() exit(1); + char buf[256]; + int n; + + /* FIFOs!!!!!!!!! */ + write(term, "mknod\n", 5); + int r = mknod("/dev/pts", S_IFIFO, 0); + write(term, "open\n", 5); + int fifofd = open("/dev/pts", 0); + + if (fifofd < 0) + write(term, "open failed\n", 12); + + /* write hello to fifo */ + write(term, "write\n", 6); + int i = write(fifofd, "hello\n", 6); + /* read back from fifo */ + write(term, "read\n", 5); + i = read(fifofd, buf, 6); + + /* print output to terminal */ + + write(term, "read back from fifo: ", 22); + write(term, buf, i); + promptfd(term); + + /* fork test */ + while ((n = read(term, buf, 256)) > 0) { + write(term, buf, n); + + if(buf[0] == 'q') + break; + } + + exit(1234); +} +void end() +{ + int term = open("/dev/tty0", 0); + if (term < 0) + exit(1); + + char buf[256]; int n; @@ -42,24 +86,47 @@ void _start() i = read(fifofd, buf, 6); /* print output to terminal */ + write(term, "read back from fifo: ", 22); + write(term, buf, i); + promptfd(term); + + /* fork test */ + while ((n = read(term, buf, 256)) > 0) { + write(term, buf, n); + + if(buf[0] == 'q') + break; + } + + exit(1234); +} +void lol() +{ + int term = open("/dev/tty0", 0); + if (term < 0) + exit(1); + + + char buf[256]; + int n; + + /* FIFOs!!!!!!!!! */ + int r = mknod("/dev/pts", S_IFIFO, 0); + int fifofd = open("/dev/pts", 0); + if (fifofd < 0) + write(term, "open failed\n", 12); + + /* write hello to fifo */ + int i = write(fifofd, "hello\n", 6); + /* read back from fifo */ + i = read(fifofd, buf, 6); + + /* print output to terminal */ + write(term, "read back from fifo: ", 22); write(term, buf, i); promptfd(term); /* fork test */ - pid_t pid = fork(); - if (pid < 0) { - write(term, "fork failed\n", 12); - exit(1); - } - - if (pid == 0) { - write(term, "child\n", 6); - exit(1); - } else { - write(term, "parent\n", 7); - } - - /* rw test */ while ((n = read(term, buf, 256)) > 0) { write(term, buf, n);