proc: add basic proc loading
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
f9ddb8f379
commit
6b40cca5b0
10 changed files with 187 additions and 44 deletions
|
|
@ -1,2 +1,3 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += proc.o
|
||||
obj-y += load.o
|
||||
|
|
|
|||
59
kernel/proc/elf.h
Normal file
59
kernel/proc/elf.h
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _ELF_H_
|
||||
#define _ELF_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define ET_NONE 0 /* No file type */
|
||||
#define ET_EXEC 2 /* Executable file */
|
||||
|
||||
#define PT_NULL 0 /* Program header table entry unused */
|
||||
#define PT_LOAD 1 /* Loadable program segment */
|
||||
#define PT_DYNAMIC 2 /* Dynamic linking information */
|
||||
#define PT_INTERP 3 /* Program interpreter */
|
||||
|
||||
#define PF_X (1 << 0) /* Segment is executable */
|
||||
#define PF_W (1 << 1) /* Segment is writable */
|
||||
#define PF_R (1 << 2) /* Segment is readable */
|
||||
|
||||
#define EM_X86_64 62
|
||||
|
||||
#define EV_CURRENT 1
|
||||
|
||||
typedef u16 Elf64_Half;
|
||||
typedef u32 Elf64_Word;
|
||||
typedef u64 Elf64_Xword;
|
||||
typedef u64 Elf64_Addr;
|
||||
typedef u64 Elf64_Off;
|
||||
|
||||
struct ehdr {
|
||||
u8 e_ident[16]; /* Magic number and other info */
|
||||
Elf64_Half e_type; /* Object file type */
|
||||
Elf64_Half e_machine; /* Architecture */
|
||||
Elf64_Word e_version; /* Object file version */
|
||||
Elf64_Addr e_entry; /* Entry point virtual address */
|
||||
Elf64_Off e_phoff; /* Program header table file offset */
|
||||
Elf64_Off e_shoff; /* Section header table file offset */
|
||||
Elf64_Word e_flags; /* Processor-specific flags */
|
||||
Elf64_Half e_ehsize; /* ELF header size in bytes */
|
||||
Elf64_Half e_phentsize; /* Program header table entry size */
|
||||
Elf64_Half e_phnum; /* Program header table entry count */
|
||||
Elf64_Half e_shentsize; /* Section header table entry size */
|
||||
Elf64_Half e_shnum; /* Section header table entry count */
|
||||
Elf64_Half e_shstrndx; /* Section header string table index */
|
||||
} PACKED;
|
||||
|
||||
struct phdr {
|
||||
Elf64_Word p_type; /* Segment type */
|
||||
Elf64_Word p_flags; /* Segment flags */
|
||||
Elf64_Off p_offset; /* Segment file offset */
|
||||
Elf64_Addr p_vaddr; /* Segment virtual address */
|
||||
Elf64_Addr p_paddr; /* Segment physical address */
|
||||
Elf64_Xword p_filesz; /* Segment size in file */
|
||||
Elf64_Xword p_memsz; /* Segment size in memory */
|
||||
Elf64_Xword p_align; /* Segment alignment */
|
||||
} PACKED;
|
||||
|
||||
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs);
|
||||
|
||||
#endif /* _ELF_H_ */
|
||||
81
kernel/proc/load.c
Normal file
81
kernel/proc/load.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/page.h>
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/page.h>
|
||||
#include <kernel/mem.h>
|
||||
|
||||
#include <lib/string.h>
|
||||
|
||||
#include "elf.h"
|
||||
|
||||
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs)
|
||||
{
|
||||
struct ehdr *ehdr = kalloc(sizeof(struct ehdr), ALLOC_KERN);
|
||||
if (!ehdr)
|
||||
return -ENOMEM;
|
||||
|
||||
struct phdr *phdrs = kalloc(ehdr->e_phnum * sizeof(struct phdr), ALLOC_KERN);
|
||||
if (!phdrs) {
|
||||
kfree(ehdr);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memcpy(ehdr, file, sizeof(struct ehdr));
|
||||
|
||||
static char elf_signature[4] = {0x7f, 'E', 'L', 'F'};
|
||||
if (memcmp(elf_signature, ehdr->e_ident, 4) || ehdr->e_type != ET_EXEC) {
|
||||
kfree(ehdr);
|
||||
kfree(phdrs);
|
||||
return -ENOEXEC;
|
||||
}
|
||||
|
||||
memcpy(phdrs, file + ehdr->e_phoff, ehdr->e_phnum * sizeof(struct phdr));
|
||||
|
||||
*out_ehdr = ehdr;
|
||||
*out_phdrs = phdrs;
|
||||
|
||||
return ehdr->e_phnum;
|
||||
}
|
||||
|
||||
int proc_load_file(const char *file, struct proc *proc)
|
||||
{
|
||||
struct ehdr *ehdr;
|
||||
struct phdr *phdrs;
|
||||
|
||||
int res = elf_get_headers(file, &ehdr, &phdrs);
|
||||
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
for (int i = 0; i < res; i++) {
|
||||
struct phdr *cur = phdrs + i;
|
||||
if (cur->p_type != PT_LOAD)
|
||||
continue;
|
||||
int flags = PAGE_USER;
|
||||
if (!(cur->p_flags & PF_R))
|
||||
continue;
|
||||
if (cur->p_flags & PF_W)
|
||||
flags |= PAGE_RW;
|
||||
if (!(cur->p_flags & PF_X))
|
||||
flags |= PAGE_XD;
|
||||
|
||||
vaddr_t page_vaddr = cur->p_vaddr & PAGE_MASK;
|
||||
size_t vaddr_offset = cur->p_vaddr & (PAGE_SIZE - 1);
|
||||
size_t map_size = ALIGN_UP(cur->p_memsz + vaddr_offset, PAGE_SIZE);
|
||||
|
||||
char *program = kalloc(map_size, ALLOC_KERN);
|
||||
|
||||
if (!program)
|
||||
return -ENOMEM;
|
||||
|
||||
memcpy(program + vaddr_offset, file + cur->p_offset, cur->p_filesz);
|
||||
memset(program + vaddr_offset + cur->p_filesz, 0, cur->p_memsz - cur->p_filesz);
|
||||
|
||||
proc_mmap(proc, TO_PHYS(program), page_vaddr, map_size, flags);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
mem_cache_t *proc_slab;
|
||||
mem_cache_t *thread_slab;
|
||||
|
|
@ -10,25 +11,31 @@ int proc_kill(struct proc *proc, int signal)
|
|||
return -ENOSYS;
|
||||
}
|
||||
|
||||
void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags)
|
||||
{
|
||||
spinlock_acquire(&proc->mem_lock);
|
||||
page_map(proc->pgd, p, v, len, flags, &proc->memmap);
|
||||
spinlock_release(&proc->mem_lock);
|
||||
}
|
||||
|
||||
struct proc *proc_create(struct proc *parent)
|
||||
{
|
||||
struct proc *ret = slab_alloc(proc_slab);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
memset(ret, 0, sizeof(struct proc));
|
||||
|
||||
struct thread *r_mt = slab_alloc(thread_slab);
|
||||
if (!r_mt) {
|
||||
slab_free(proc_slab, ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pgd_t *p;
|
||||
if (parent)
|
||||
p = parent->pgd;
|
||||
ret->pgd = pgd_clone(parent->pgd);
|
||||
else
|
||||
p = kpgd;
|
||||
|
||||
|
||||
ret->pgd = pgd_clone(kpgd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -42,4 +49,6 @@ void proc_init()
|
|||
{
|
||||
proc_slab = slab_create("proc_slab", sizeof(struct proc), 1 * MB, 8);
|
||||
thread_slab = slab_create("thread_slab", sizeof(struct thread), 1 * MB, 8);
|
||||
|
||||
krdy("proc_init ready\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,11 @@
|
|||
#include <drivers/serial.h>
|
||||
#include <kernel/tty.h>
|
||||
|
||||
static mem_cache_t *tty_slab;
|
||||
|
||||
static struct tty_serial_ops arch_serial_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write};
|
||||
|
||||
struct tty *tty_create(const char *name, void *priv)
|
||||
{
|
||||
struct tty *tty = slab_alloc(tty_slab);
|
||||
struct tty *tty = kzalloc(sizeof(struct tty), ALLOC_KERN);
|
||||
|
||||
dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name);
|
||||
|
||||
|
|
@ -38,12 +36,3 @@ void tty_write(struct tty *tty, const char *s)
|
|||
while ((c = *s++))
|
||||
tty_putchar(tty, c);
|
||||
}
|
||||
|
||||
void tty_layer_init()
|
||||
{
|
||||
tty_slab = slab_create("tty_layer", sizeof(struct tty), 256 * KB, 8);
|
||||
|
||||
if (!tty_slab) {
|
||||
panic("tty_slab alloc failed\n");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue