proc: add basic proc loading

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-17 20:11:19 -04:00
commit 6b40cca5b0
10 changed files with 187 additions and 44 deletions

View file

@ -17,6 +17,7 @@
#define PAGE_ACCESSED 0x020
#define PAGE_DIRTY 0x040
#define PAGE_PS 0x080
#define PAGE_XD (1ull << 63)
#define pgd_index(address) (((address) >> 39) & 0x1FF)
#define pud_index(address) (((address) >> 30) & 0x1FF)

View file

@ -9,6 +9,8 @@
#include <lib/rbtree.h>
#include "elf.h"
#define PROC_STATE_RUNNING 0
#define PROC_STATE_WAITING 1
#define PROC_STATE_DNS 2 /* do not schedule */
@ -43,14 +45,23 @@ struct proc {
char *name;
rbtree_t vm_tree;
spinlock_t vm_lock;
pgd_t *pgd;
pgd_t *pgd;
spinlock_t sched_lock;
spinlock_t mem_lock;
rbtree_t memmap;
struct ehdr *ehdr;
struct phdrs *phdrs;
size_t n_phdrs;
};
void proc_init();
int proc_kill(struct proc *proc, int signal);
void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags);
struct proc *proc_create(struct proc *parent);
void proc_free(struct proc *proc);
void switch_to(struct thread *from, struct thread *to);
void proc_init();
#endif /* _PROC_H_ */

View file

@ -15,8 +15,6 @@ struct tty {
struct tty_serial_ops serial_ops;
};
void tty_layer_init();
struct tty *tty_create(const char *name, void *priv);
void tty_putchar(struct tty *tty, char c);
char tty_getchar(struct tty *tty);

View file

@ -12,6 +12,7 @@
#include <kernel/requests.h>
#include <kernel/page.h>
#include <kernel/slab.h>
#include <kernel/proc.h>
#include <arch/init.h>
@ -61,6 +62,7 @@ static void kmain()
page_init();
ramfs_init();
struct limine_file *file = module_res->modules[0];
fs_t *initramfs;
@ -70,15 +72,14 @@ static void kmain()
if (res < 0)
panic("failed to load initramfs: %s\n", strerror(-res));
/* start the kernel */
/* "layer 2" kernel requirements */
devfs_init();
device_layer_init();
tty_layer_init();
proc_init();
/* init main fallback tty */
struct tty *serial_tty = NULL;
serial_port_tty_init(&serial_tty);
struct tty *visual_tty = fbcon_init();
struct tty *tee = tty_tee_create("broadcast");
@ -87,7 +88,6 @@ static void kmain()
tty_printf = tee;
do_initcalls();
arch_ap_startup();
/* do nothing, for now :) */

View file

@ -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
View 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
View 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;
}

View file

@ -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");
}

View file

@ -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");
}
}

View file

@ -4,18 +4,12 @@
#include <kernel/buddy.h>
#include <kernel/slab.h>
#include <kernel/page.h>
#include <kernel/proc.h>
#include <arch/page.h>
#include <lib/rbtree.h>
#include <lib/math.h>
#include <lib/string.h>
struct map_entry {
vaddr_t vaddr;
vaddr_t paddr;
size_t len;
};
static rbtree_t kmap_tree = {0};
static mem_cache_t *map_entry_cache = NULL;
pgd_t *kpgd;
@ -55,10 +49,10 @@ void *align(void *p, size_t align)
void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree)
{
struct map_entry *insert = slab_alloc(map_entry_cache);
insert->paddr = paddr;
insert->vaddr = vaddr;
insert->len = len;
struct vm_area *insert = slab_alloc(map_entry_cache);
insert->start = vaddr;
insert->end = vaddr + len;
insert->flags = flags;
rbnode_t *node = rbt_insert(map_tree, vaddr);
node->value = (u64)insert;
@ -76,24 +70,24 @@ void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 fl
pgd_t *pgd_entry = &pgd_table[pgd_index(cur_vaddr)];
if (!(pgd_entry->pgd & PAGE_PRESENT)) {
pgd_entry->pgd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
pgd_entry->pgd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW | (flags & PAGE_USER);
}
pud_t *pud_table = (pud_t *)TO_VIRT(pgd_entry->pgd & PAGE_MASK);
pud_t *pud_entry = &pud_table[pud_index(cur_vaddr)];
if (!(pud_entry->pud & PAGE_PRESENT)) {
pud_entry->pud = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
pud_entry->pud = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW | (flags & PAGE_USER);
}
pmd_t *pmd_table = (pmd_t *)TO_VIRT(pud_entry->pud & PAGE_MASK);
pmd_t *pmd_entry = &pmd_table[pmd_index(cur_vaddr)];
if (!(pmd_entry->pmd & PAGE_PRESENT)) {
pmd_entry->pmd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
pmd_entry->pmd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW | (flags & PAGE_USER);
}
pte_t *pte_table = (pte_t *)TO_VIRT(pmd_entry->pmd & PAGE_MASK);
pte_t *pte_entry = pte_table + pte_index(cur_vaddr);
pte_entry->pte = (cur_paddr & PAGE_MASK) | (flags & 0xFFF);
pte_entry->pte = (cur_paddr & PAGE_MASK) | PAGE_PRESENT | (flags & 0xFFF) | (flags & PAGE_XD);
cur_vaddr += PAGE_SIZE;
cur_paddr += PAGE_SIZE;
@ -155,7 +149,7 @@ void *map_mmio_dev(paddr_t base, size_t len)
void page_init()
{
map_entry_cache = slab_create("map_entry_cache", sizeof(struct map_entry), 64 * KB, 0x8);
map_entry_cache = slab_create("map_entry_cache", sizeof(struct vm_area), 64 * KB, 0x8);
kpgd = pt_alloc();
map_hhdm_zones(kpgd);
@ -181,7 +175,7 @@ pgd_t *pgd_clone(pgd_t *src)
{
pgd_t *pgd = pt_alloc();
/* don't clone a bunch of kernel stuff, just keep it */
/* don't clone a bunch of kernel stuff, just keep it */
for (int i = 256; i < 512; i++)
pgd[i] = kpgd[i];