kernel: remove kernel/proc
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
e41877b268
commit
695a4eb0c1
5 changed files with 0 additions and 442 deletions
|
|
@ -1,4 +0,0 @@
|
||||||
# SPDX-License-Identifier: GPL-2.0-only
|
|
||||||
obj-y += proc.o
|
|
||||||
obj-y += load.o
|
|
||||||
obj-y += sched.o
|
|
||||||
|
|
@ -1,119 +0,0 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
#ifndef _ELF_H_
|
|
||||||
#define _ELF_H_
|
|
||||||
|
|
||||||
#include <kernel/common.h>
|
|
||||||
|
|
||||||
#define ET_NONE 0
|
|
||||||
#define ET_REL 1
|
|
||||||
#define ET_EXEC 2
|
|
||||||
|
|
||||||
#define PT_NULL 0
|
|
||||||
#define PT_LOAD 1
|
|
||||||
#define PT_DYNAMIC 2
|
|
||||||
#define PT_INTERP 3
|
|
||||||
|
|
||||||
#define PF_X (1 << 0)
|
|
||||||
#define PF_W (1 << 1)
|
|
||||||
#define PF_R (1 << 2)
|
|
||||||
|
|
||||||
#define EM_X86_64 62
|
|
||||||
#define EV_CURRENT 1
|
|
||||||
|
|
||||||
#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 SHF_WRITE (1 << 0)
|
|
||||||
#define SHF_ALLOC (1 << 1)
|
|
||||||
#define SHF_EXECINSTR (1 << 2)
|
|
||||||
|
|
||||||
#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 STT_SECTION 3
|
|
||||||
|
|
||||||
#define ELF64_ST_BIND(i) ((i) >> 4)
|
|
||||||
#define ELF64_ST_TYPE(i) ((i) & 0xf)
|
|
||||||
|
|
||||||
#define R_X86_64_NONE 0
|
|
||||||
#define R_X86_64_64 1
|
|
||||||
#define R_X86_64_PC32 2
|
|
||||||
#define R_X86_64_PLT32 4
|
|
||||||
#define R_X86_64_32S 11
|
|
||||||
|
|
||||||
#define ELF64_R_SYM(i) ((i) >> 32)
|
|
||||||
#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
|
|
||||||
#define SHN_UNDEF 0
|
|
||||||
|
|
||||||
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];
|
|
||||||
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;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct phdr {
|
|
||||||
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;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct shdr {
|
|
||||||
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;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct sym {
|
|
||||||
Elf64_Word st_name;
|
|
||||||
unsigned char st_info;
|
|
||||||
unsigned char st_other;
|
|
||||||
Elf64_Half st_shndx;
|
|
||||||
Elf64_Addr st_value;
|
|
||||||
Elf64_Xword st_size;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct rela {
|
|
||||||
Elf64_Addr r_offset;
|
|
||||||
Elf64_Xword r_info;
|
|
||||||
Elf64_Xword r_addend;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry);
|
|
||||||
|
|
||||||
#endif /* _ELF_H_ */
|
|
||||||
|
|
@ -1,88 +0,0 @@
|
||||||
/* 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, uaddr_t *entry)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
*entry = ehdr->e_entry;
|
|
||||||
|
|
||||||
return ehdr->e_phnum;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t proc_load_file(const char *file, struct proc *proc)
|
|
||||||
{
|
|
||||||
struct ehdr *ehdr;
|
|
||||||
struct phdr *phdrs;
|
|
||||||
|
|
||||||
uaddr_t entry = 0;
|
|
||||||
int res = elf_get_headers(file, &ehdr, &phdrs, &entry);
|
|
||||||
|
|
||||||
if (res < 0)
|
|
||||||
return res;
|
|
||||||
|
|
||||||
proc->ehdr = ehdr;
|
|
||||||
proc->phdrs = phdrs;
|
|
||||||
proc->n_phdrs = res;
|
|
||||||
proc->entry = entry;
|
|
||||||
|
|
||||||
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_USER);
|
|
||||||
|
|
||||||
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 entry;
|
|
||||||
}
|
|
||||||
|
|
@ -1,153 +0,0 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
#include <arch/page.h>
|
|
||||||
#include <kernel/proc.h>
|
|
||||||
#include <kernel/slab.h>
|
|
||||||
#include <kernel/atomic.h>
|
|
||||||
#include <kernel/driver.h>
|
|
||||||
#include <lib/string.h>
|
|
||||||
|
|
||||||
#define USTACK_DEFAULT_SIZE 0x10000
|
|
||||||
#define P_STACK_SIZE 0x4000
|
|
||||||
|
|
||||||
mem_cache_t *proc_slab;
|
|
||||||
mem_cache_t *thread_slab;
|
|
||||||
|
|
||||||
/* PID allocator */
|
|
||||||
static u64 _pid_counter = 1;
|
|
||||||
|
|
||||||
pid_t alloc_pid()
|
|
||||||
{
|
|
||||||
return atomic_xadd(&_pid_counter, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *proc_alloc(struct proc *proc, size_t size)
|
|
||||||
{
|
|
||||||
struct proc_alloc *a = kalloc(sizeof(struct proc_alloc), ALLOC_KERN);
|
|
||||||
if (!a)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
void *ret = kalloc(size, ALLOC_USER);
|
|
||||||
if (!ret) {
|
|
||||||
kfree(a);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
rbnode_t *node = rbt_insert(&proc->alloc_map, (uintptr_t)ret);
|
|
||||||
node->ptr = ret;
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int copy_parent(struct proc *new, struct proc *parent)
|
|
||||||
{
|
|
||||||
spinlock_acquire(&parent->mem_lock);
|
|
||||||
|
|
||||||
rbnode_t *node = parent->memmap.root;
|
|
||||||
if (!node)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
while (node->left)
|
|
||||||
node = node->left;
|
|
||||||
|
|
||||||
while (node) {
|
|
||||||
struct vm_area *area = node->ptr;
|
|
||||||
|
|
||||||
void *vm_alloc = proc_alloc(new, area->len);
|
|
||||||
proc_mmap(new, TO_PHYS(vm_alloc), area->start, area->len, area->flags);
|
|
||||||
memcpy_user(parent, (void *)area->start, vm_alloc, area->len);
|
|
||||||
|
|
||||||
node = rbt_successor(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
spinlock_release(&parent->mem_lock);
|
|
||||||
|
|
||||||
if (parent->fd_array_size) {
|
|
||||||
spinlock_acquire(&parent->fd_lock);
|
|
||||||
new->fd_array_size = parent->fd_array_size;
|
|
||||||
new->fdt = kalloc(new->fd_array_size * sizeof(struct file), ALLOC_KERN);
|
|
||||||
spinlock_release(&parent->fd_lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct proc *proc_create(struct thread *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;
|
|
||||||
}
|
|
||||||
|
|
||||||
r_mt->stack_size = USTACK_DEFAULT_SIZE;
|
|
||||||
r_mt->ustack_top = 0x7FFFFFFFF000;
|
|
||||||
r_mt->proc = ret;
|
|
||||||
r_mt->state = PROC_STATE_DNS;
|
|
||||||
|
|
||||||
void *kstack = kalloc(P_STACK_SIZE, ALLOC_KERN);
|
|
||||||
if (!kstack) {
|
|
||||||
slab_free(proc_slab, ret);
|
|
||||||
slab_free(thread_slab, r_mt);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
r_mt->kstack_top = (uaddr_t)kstack + P_STACK_SIZE;
|
|
||||||
|
|
||||||
void *ustack = proc_alloc(ret, r_mt->stack_size);
|
|
||||||
|
|
||||||
if (!ustack) {
|
|
||||||
slab_free(proc_slab, ret);
|
|
||||||
slab_free(thread_slab, r_mt);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret->pgd = pgd_create_user();
|
|
||||||
if (!ret->pgd) {
|
|
||||||
slab_free(proc_slab, ret);
|
|
||||||
slab_free(thread_slab, r_mt);
|
|
||||||
kfree(ustack);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
proc_mmap(ret, TO_PHYS(ustack), r_mt->ustack_top - r_mt->stack_size, r_mt->stack_size, PAGE_RW | PAGE_USER);
|
|
||||||
|
|
||||||
ret->main_thread = r_mt;
|
|
||||||
|
|
||||||
if (parent) {
|
|
||||||
int res = copy_parent(ret, parent->proc);
|
|
||||||
if (res)
|
|
||||||
kerr("failed to copy parent memmap for new process: %s\n", strerror(-res));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void proc_free(struct proc *proc)
|
|
||||||
{
|
|
||||||
slab_free(proc_slab, proc);
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
@ -1,78 +0,0 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
#include <arch/proc.h>
|
|
||||||
#include <arch/page.h>
|
|
||||||
#include <kernel/proc.h>
|
|
||||||
#include <kernel/sched.h>
|
|
||||||
|
|
||||||
/* Run queue */
|
|
||||||
static struct {
|
|
||||||
struct thread *head;
|
|
||||||
struct thread *tail;
|
|
||||||
spinlock_t lock;
|
|
||||||
} sched_queue;
|
|
||||||
|
|
||||||
void sched_add_thread(struct thread *t)
|
|
||||||
{
|
|
||||||
spinlock_acquire(&sched_queue.lock);
|
|
||||||
t->next = NULL;
|
|
||||||
if (sched_queue.tail)
|
|
||||||
sched_queue.tail->next = t;
|
|
||||||
sched_queue.tail = t;
|
|
||||||
if (!sched_queue.head)
|
|
||||||
sched_queue.head = t;
|
|
||||||
spinlock_release(&sched_queue.lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sched_purge_proc(struct proc *proc)
|
|
||||||
{
|
|
||||||
spinlock_acquire(&sched_queue.lock);
|
|
||||||
struct thread *t = sched_queue.head;
|
|
||||||
struct thread *p = NULL;
|
|
||||||
while (t) {
|
|
||||||
if (t->proc == proc) {
|
|
||||||
if (p)
|
|
||||||
p->next = t->next;
|
|
||||||
else
|
|
||||||
sched_queue.head = t->next;
|
|
||||||
|
|
||||||
if (!t->next)
|
|
||||||
sched_queue.tail = p;
|
|
||||||
|
|
||||||
t->next = NULL;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
p = t;
|
|
||||||
}
|
|
||||||
t = t->next;
|
|
||||||
}
|
|
||||||
spinlock_release(&sched_queue.lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct thread *dequeue_thread()
|
|
||||||
{
|
|
||||||
spinlock_acquire(&sched_queue.lock);
|
|
||||||
struct thread *t = sched_queue.head;
|
|
||||||
if (t) {
|
|
||||||
sched_queue.head = t->next;
|
|
||||||
if (!sched_queue.head)
|
|
||||||
sched_queue.tail = NULL;
|
|
||||||
t->next = NULL;
|
|
||||||
}
|
|
||||||
spinlock_release(&sched_queue.lock);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
void schedule()
|
|
||||||
{
|
|
||||||
struct thread *next = dequeue_thread();
|
|
||||||
if (!next)
|
|
||||||
return;
|
|
||||||
|
|
||||||
struct cpu_local *tls = tls_get();
|
|
||||||
tls->cur_thread = next;
|
|
||||||
tls->kernel_rsp = next->kstack_top;
|
|
||||||
tls->tss.rsp0 = next->kstack_top;
|
|
||||||
pgd_load(next->proc->pgd);
|
|
||||||
|
|
||||||
sched_restore_state(next);
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue