static void __vmr_decref(vmr_t* v, unsigned dec) { if ((v->refcnt -= dec) == 0) { if (v->file) file_decref(v->file); } }
ssize_t sys_write(int fd, const char* buf, size_t n) { ssize_t r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_write(f, buf, n); file_decref(f); } return r; }
ssize_t sys_pread(int fd, char* buf, size_t n, off_t offset) { ssize_t r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_pread(f, buf, n, offset); file_decref(f); } return r; }
ssize_t sys_read(int fd, char* buf, size_t n) { ssize_t r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_read(f, buf, n); file_decref(f); } return r; }
ssize_t sys_lseek(int fd, size_t ptr, int dir) { ssize_t r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_lseek(f, ptr, dir); file_decref(f); } return r; }
int sys_dup(int fd) { int r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_dup(f); file_decref(f); } return r; }
int sys_ftruncate(int fd, off_t len) { int r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_truncate(f, len); file_decref(f); } return r; }
int sys_fcntl(int fd, int cmd, int arg) { int r = -EBADF; file_t* f = file_get(fd); if (f) { r = frontend_syscall(SYS_fcntl, f->kfd, cmd, arg, 0, 0, 0, 0); file_decref(f); } return r; }
int sys_fstat(int fd, void* st) { int r = -EBADF; file_t* f = file_get(fd); if (f) { r = file_stat(f, st); file_decref(f); } return r; }
sysret_t sys_fstat(int fd, void* st) { sysret_t r = {-1,EBADF}; file_t* f = file_get(fd); if (f) { r = file_stat(f, st); file_decref(f); } return r; }
int sys_openat(int dirfd, const char* name, int flags, int mode) { int kfd = at_kfd(dirfd); if (kfd != -1) { file_t* file = file_openat(kfd, name, flags, mode); if (IS_ERR_VALUE(file)) return PTR_ERR(file); int fd = file_dup(file); if (fd < 0) { file_decref(file); return -ENOMEM; } return fd; } return -EBADF; }
file_t* file_openat(int dirfd, const char* fn, int flags) { file_t* f = file_get_free(); if (f == NULL) return f; uint8_t mode = 0; if((flags & O_ACCMODE) == O_RDONLY) mode = FA_READ; if((flags & O_ACCMODE) == O_WRONLY) mode = FA_WRITE; if((flags & O_ACCMODE) == O_RDWR) mode = FA_READ | FA_WRITE; FRESULT rt = f_open(&f->fd, fn, mode); /* check mode */ if (rt) { panic("fail to open file %s", fn); file_decref(f); return NULL; } else { return f; } }
void load_elf(const char* fn, elf_info* info) { file_t* file = file_open(fn, O_RDONLY, 0); if (IS_ERR_VALUE(file)) goto fail; Elf64_Ehdr eh64; ssize_t ehdr_size = file_pread(file, &eh64, sizeof(eh64), 0); if (ehdr_size < (ssize_t)sizeof(eh64) || !(eh64.e_ident[0] == '\177' && eh64.e_ident[1] == 'E' && eh64.e_ident[2] == 'L' && eh64.e_ident[3] == 'F')) goto fail; uintptr_t min_vaddr = -1, max_vaddr = 0; #define LOAD_ELF do { \ eh = (typeof(eh))&eh64; \ size_t phdr_size = eh->e_phnum*sizeof(*ph); \ if (phdr_size > info->phdr_size) \ goto fail; \ ssize_t ret = file_pread(file, (void*)info->phdr, phdr_size, eh->e_phoff); \ if (ret < (ssize_t)phdr_size) \ goto fail; \ info->phnum = eh->e_phnum; \ info->phent = sizeof(*ph); \ ph = (typeof(ph))info->phdr; \ info->is_supervisor = (eh->e_entry >> (8*sizeof(eh->e_entry)-1)) != 0; \ if (info->is_supervisor) \ info->first_free_paddr = ROUNDUP(info->first_free_paddr, SUPERPAGE_SIZE); \ for (int i = 0; i < eh->e_phnum; i++) \ if (ph[i].p_type == PT_LOAD && ph[i].p_memsz && ph[i].p_vaddr < min_vaddr) \ min_vaddr = ph[i].p_vaddr; \ if (info->is_supervisor) \ min_vaddr = ROUNDDOWN(min_vaddr, SUPERPAGE_SIZE); \ else \ min_vaddr = ROUNDDOWN(min_vaddr, RISCV_PGSIZE); \ uintptr_t bias = 0; \ if (info->is_supervisor || eh->e_type == ET_DYN) \ bias = info->first_free_paddr - min_vaddr; \ info->entry = eh->e_entry; \ if (!info->is_supervisor) { \ info->entry += bias; \ min_vaddr += bias; \ } \ info->bias = bias; \ int flags = MAP_FIXED | MAP_PRIVATE; \ if (info->is_supervisor) \ flags |= MAP_POPULATE; \ for (int i = eh->e_phnum - 1; i >= 0; i--) { \ if(ph[i].p_type == PT_LOAD && ph[i].p_memsz) { \ uintptr_t prepad = ph[i].p_vaddr % RISCV_PGSIZE; \ uintptr_t vaddr = ph[i].p_vaddr + bias; \ if (vaddr + ph[i].p_memsz > max_vaddr) \ max_vaddr = vaddr + ph[i].p_memsz; \ if (info->is_supervisor) { \ if (!__valid_user_range(vaddr - prepad, vaddr + ph[i].p_memsz)) \ goto fail; \ ret = file_pread(file, (void*)vaddr, ph[i].p_filesz, ph[i].p_offset); \ if (ret < (ssize_t)ph[i].p_filesz) \ goto fail; \ memset((void*)vaddr - prepad, 0, prepad); \ memset((void*)vaddr + ph[i].p_filesz, 0, ph[i].p_memsz - ph[i].p_filesz); \ } else { \ int flags2 = flags | (prepad ? MAP_POPULATE : 0); \ if (__do_mmap(vaddr - prepad, ph[i].p_filesz + prepad, -1, flags2, file, ph[i].p_offset - prepad) != vaddr - prepad) \ goto fail; \ memset((void*)vaddr - prepad, 0, prepad); \ size_t mapped = ROUNDUP(ph[i].p_filesz + prepad, RISCV_PGSIZE) - prepad; \ if (ph[i].p_memsz > mapped) \ if (__do_mmap(vaddr + mapped, ph[i].p_memsz - mapped, -1, flags|MAP_ANONYMOUS, 0, 0) != vaddr + mapped) \ goto fail; \ } \ } \ } \ } while(0) info->elf64 = IS_ELF64(eh64); if (info->elf64) { Elf64_Ehdr* eh; Elf64_Phdr* ph; LOAD_ELF; } else if (IS_ELF32(eh64)) { Elf32_Ehdr* eh; Elf32_Phdr* ph; LOAD_ELF; } else goto fail; info->first_user_vaddr = min_vaddr; info->first_vaddr_after_user = ROUNDUP(max_vaddr - info->bias, RISCV_PGSIZE); info->brk_min = max_vaddr; file_decref(file); return; fail: panic("couldn't open ELF program: %s!", fn); }
void load_elf(const char* fn, elf_info* info) { sysret_t ret = file_open(fn, O_RDONLY, 0); file_t* file = (file_t*)ret.result; if (ret.result == -1) goto fail; Elf64_Ehdr eh64; ssize_t ehdr_size = file_pread(file, &eh64, sizeof(eh64), 0).result; if (ehdr_size < (ssize_t)sizeof(eh64) || !(eh64.e_ident[0] == '\177' && eh64.e_ident[1] == 'E' && eh64.e_ident[2] == 'L' && eh64.e_ident[3] == 'F')) goto fail; #define LOAD_ELF do { \ eh = (typeof(eh))&eh64; \ size_t phdr_size = eh->e_phnum*sizeof(*ph); \ if (info->phdr_top - phdr_size < info->stack_bottom) \ goto fail; \ info->phdr = info->phdr_top - phdr_size; \ ssize_t ret = file_pread(file, (void*)info->phdr, phdr_size, eh->e_phoff).result; \ if (ret < (ssize_t)phdr_size) goto fail; \ info->entry = eh->e_entry; \ info->phnum = eh->e_phnum; \ info->phent = sizeof(*ph); \ ph = (typeof(ph))info->phdr; \ for(int i = 0; i < eh->e_phnum; i++, ph++) { \ if(ph->p_type == SHT_PROGBITS && ph->p_memsz) { \ info->brk_min = MAX(info->brk_min, ph->p_vaddr + ph->p_memsz); \ size_t vaddr = ROUNDDOWN(ph->p_vaddr, RISCV_PGSIZE), prepad = ph->p_vaddr - vaddr; \ size_t memsz = ph->p_memsz + prepad, filesz = ph->p_filesz + prepad; \ size_t offset = ph->p_offset - prepad; \ if (__do_mmap(vaddr, filesz, -1, MAP_FIXED|MAP_PRIVATE, file, offset) != vaddr) \ goto fail; \ size_t mapped = ROUNDUP(filesz, RISCV_PGSIZE); \ if (memsz > mapped) \ if (__do_mmap(vaddr + mapped, memsz - mapped, -1, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0) != vaddr + mapped) \ goto fail; \ } \ } \ } while(0) info->elf64 = IS_ELF64(eh64); if (info->elf64) { Elf64_Ehdr* eh; Elf64_Phdr* ph; LOAD_ELF; } else if (IS_ELF32(eh64)) { Elf32_Ehdr* eh; Elf32_Phdr* ph; LOAD_ELF; } else goto fail; file_decref(file); return; fail: panic("couldn't open ELF program: %s!", fn); }
void load_elf(const char* fn, elf_info* info) { file_t* file = file_open(fn, O_RDONLY, 0); if (IS_ERR_VALUE(file)) goto fail; Elf_Ehdr eh; ssize_t ehdr_size = file_pread(file, &eh, sizeof(eh), 0); if (ehdr_size < (ssize_t)sizeof(eh) || !(eh.e_ident[0] == '\177' && eh.e_ident[1] == 'E' && eh.e_ident[2] == 'L' && eh.e_ident[3] == 'F')) goto fail; #if __riscv_xlen == 64 assert(IS_ELF64(eh)); #else assert(IS_ELF32(eh)); #endif #ifndef __riscv_compressed assert(!(eh.e_flags & EF_RISCV_RVC)); #endif size_t phdr_size = eh.e_phnum * sizeof(Elf_Phdr); if (phdr_size > info->phdr_size) goto fail; ssize_t ret = file_pread(file, (void*)info->phdr, phdr_size, eh.e_phoff); if (ret < (ssize_t)phdr_size) goto fail; info->phnum = eh.e_phnum; info->phent = sizeof(Elf_Phdr); Elf_Phdr* ph = (typeof(ph))info->phdr; // compute highest VA in ELF uintptr_t max_vaddr = 0; for (int i = 0; i < eh.e_phnum; i++) if (ph[i].p_type == PT_LOAD && ph[i].p_memsz) max_vaddr = MAX(max_vaddr, ph[i].p_vaddr + ph[i].p_memsz); max_vaddr = ROUNDUP(max_vaddr, RISCV_PGSIZE); // don't load dynamic linker at 0, else we can't catch NULL pointer derefs uintptr_t bias = 0; if (eh.e_type == ET_DYN) bias = RISCV_PGSIZE; info->entry = eh.e_entry + bias; int flags = MAP_FIXED | MAP_PRIVATE; for (int i = eh.e_phnum - 1; i >= 0; i--) { if(ph[i].p_type == PT_LOAD && ph[i].p_memsz) { uintptr_t prepad = ph[i].p_vaddr % RISCV_PGSIZE; uintptr_t vaddr = ph[i].p_vaddr + bias; if (vaddr + ph[i].p_memsz > info->brk_min) info->brk_min = vaddr + ph[i].p_memsz; int flags2 = flags | (prepad ? MAP_POPULATE : 0); int prot = get_prot(ph[i].p_flags); if (__do_mmap(vaddr - prepad, ph[i].p_filesz + prepad, prot | PROT_WRITE, flags2, file, ph[i].p_offset - prepad) != vaddr - prepad) goto fail; memset((void*)vaddr - prepad, 0, prepad); if (!(prot & PROT_WRITE)) if (do_mprotect(vaddr - prepad, ph[i].p_filesz + prepad, prot)) goto fail; size_t mapped = ROUNDUP(ph[i].p_filesz + prepad, RISCV_PGSIZE) - prepad; if (ph[i].p_memsz > mapped) if (__do_mmap(vaddr + mapped, ph[i].p_memsz - mapped, prot, flags|MAP_ANONYMOUS, 0, 0) != vaddr + mapped) goto fail; } } file_decref(file); return; fail: panic("couldn't open ELF program: %s!", fn); }