Compare commits

..
35 changed files with 146 additions and 577 deletions

View file

@ -21,8 +21,6 @@ LD := $(ARCH)-elf-ld
CFLAGS := -g3 -O0 -pipe -Wall CFLAGS := -g3 -O0 -pipe -Wall
INITRAMFS := initramfs.cpio
CFLAGS += \ CFLAGS += \
-std=gnu99 \ -std=gnu99 \
-ffreestanding \ -ffreestanding \
@ -71,17 +69,15 @@ export LD
export QEMUFLAGS export QEMUFLAGS
export CFLAGS export CFLAGS
export LDFLAGS export LDFLAGS
export INITRAMFS
PHONY: default PHONY: default
default: default:
$(Q)compiledb -n $(MAKE) --no-print-directory $(KERNEL) $(Q)compiledb -n make $(KERNEL)
$(Q)compiledb -n $(MAKE) -C usr.bin $(Q)make $(KERNEL)
$(Q)$(MAKE) --no-print-directory $(KERNEL)
.PHONY: all .PHONY: all
all: $(KERNEL) all: $(KERNEL)
$(Q)compiledb -n $(MAKE) --no-print-directory $(Q)compiledb -n make
.PHONY: __build_all .PHONY: __build_all
__build_all: __build_all:
@ -89,21 +85,18 @@ __build_all:
$(KOBJ): __build_all $(KOBJ): __build_all
$(KERNEL): $(KOBJ) linker.ld $(KERNEL): $(KOBJ) built-in.o linker.ld
$(Q)$(MAKE) --no-print-directory -C usr.bin
$(Q)echo " LD $@" $(Q)echo " LD $@"
$(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@ $(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@
.PHONY: clean .PHONY: clean
clean: clean:
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _clean obj=. $(Q)$(MAKE) -f scripts/Makefile.build _clean obj=.
$(Q)$(MAKE) --no-print-directory -C usr.bin clean
$(Q)rm -rf $(IMG) $(Q)rm -rf $(IMG)
$(Q)rm -rf iso_root $(Q)rm -rf iso_root
$(Q)rm -rf os_root $(Q)rm -rf os_root
$(Q)rm -rf edk2-ovmf $(Q)rm -rf edk2-ovmf
$(Q)rm -rf os_root.img $(Q)rm -rf os_root.img
$(Q)rm -rf $(INITRAMFS)
print-%: print-%:
$(Q)echo $($(*)) $(Q)echo $($(*))

View file

@ -51,7 +51,7 @@ void arch_init()
idt_init(); idt_init();
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32)); wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
wrmsr(IA32_LSTAR, (u64)_syscall_enter); wrmsr(IA32_LSTAR, (u64)_syscall_enter);
wrmsr(IA32_FMASK, 0x3F7FD5); wrmsr(IA32_FMASK, RFLAGS_IF);
} }
void arch_ap_startup() void arch_ap_startup()
@ -60,4 +60,6 @@ void arch_ap_startup()
struct cpu_local *this_local = &cpu_local_arr[id]; struct cpu_local *this_local = &cpu_local_arr[id];
wrmsr(IA32_GS_BASE, (u64)this_local); wrmsr(IA32_GS_BASE, (u64)this_local);
yield();
} }

View file

@ -78,14 +78,15 @@ _syscall_enter:
mov rsp, gs:[8] mov rsp, gs:[8]
/* save registers into struct pt_regs */ /* save registers into struct pt_regs */
push qword ptr gs:[16] /* save user RSP */ push gs:[16] /* save user RSP */
push_all push_all
mov rdi, rsp mov rdi, rsp
call syscall call syscall
/* restore registers */ /* restore registers */
pop_all pop_all
pop rsp add rsp, 8
mov rsp, [rsp] /* load user RSP */
swapgs swapgs
sysretq sysretq
@ -98,30 +99,6 @@ switch_to:
pop_all pop_all
ret ret
.global _enter_userspace_init
_enter_userspace_init:
mov rcx, rdi
mov r11, rsi
mov rsp, rdx
xor rax, rax
xor rbx, rbx
xor rdx, rdx
xor rdi, rdi
xor rsi, rsi
xor rbp, rbp
xor r8, r8
xor r9, r9
xor r10, r10
xor r12, r12
xor r13, r13
xor r14, r14
xor r15, r15
swapgs
sysretq
/* exceptions */ /* exceptions */
exception 0x00 exception 0x00
exception 0x01 exception 0x01

View file

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

View file

@ -16,11 +16,11 @@ struct cpu_local {
struct pt_regs { struct pt_regs {
u64 rax; u64 rax;
u64 rbx; u64 rbx;
u64 rcx; u64 rcx;
u64 rdx; u64 rdx;
u64 rdi; u64 rdi;
u64 rsi; u64 rsi;
u64 rbp; u64 rbp;
u64 r8; u64 r8;
u64 r9; u64 r9;
u64 r10; u64 r10;
@ -29,12 +29,8 @@ struct pt_regs {
u64 r13; u64 r13;
u64 r14; u64 r14;
u64 r15; u64 r15;
u64 rsp; u64 rsp;
u64 rip;
} PACKED; } PACKED;
struct cpu_local *tls_get();
struct proc *get_cur_proc();
struct thread *get_cur_thread();
void enter_userspace_init(struct thread *thread, uaddr_t entry);
#endif /* _ARCH_PROC_H_ */ #endif /* _ARCH_PROC_H_ */

View file

@ -1,4 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
obj-y += atomic.o obj-y += atomic.o
obj-y += spinlock.o obj-y += spinlock.o
obj-y += proc.o

View file

@ -1,45 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/proc.h>
#include <arch/page.h>
#include <kernel/proc.h>
#include "arch/msr.h"
struct cpu_local *tls_get()
{
return (void *)rdmsr(IA32_GS_BASE);
}
struct thread *get_cur_thread()
{
struct cpu_local *l = tls_get();
if (!l)
return NULL;
if (!l->cur_thread)
return NULL;
return l->cur_thread;
}
struct proc *get_cur_proc()
{
struct thread *t = get_cur_thread();
if (!t)
return NULL;
return t->proc;
}
/* the thread that calls this will be lost, so it should only be called
* on the initial process, and while doing an exec
*/
void enter_userspace_init(struct thread *thread, uaddr_t entry)
{
struct cpu_local *tls = tls_get();
tls->cur_thread = thread;
pgd_load(thread->proc->pgd);
thread->state = PROC_STATE_RUNNING;
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top);
_enter_userspace_init(entry, 0x202, thread->stack_addr);
}

View file

@ -33,9 +33,9 @@ static struct tty_serial_ops fbcon_serial_ops = {
struct tty *fbcon_init(void) struct tty *fbcon_init(void)
{ {
static u32 ansi_colors[] = {0x000000, 0xAA0000, 0x00AA00, 0x00AAAA, 0x0000AA, 0xAA00AA, 0xAAAA00, 0xAAAAAA}; static u32 ansi_colors[] = {0x000000, 0x0000AA, 0x00AA00, 0x00AAAA, 0xAA0000, 0xAA00AA, 0xAAAA00, 0xAAAAAA};
static u32 ansi_bright_colors[] = {0x000000, 0xDD00DD, 0x00DD00, 0x00DDDD, 0x0000DD, 0xDD00DD, 0xDDDD00, 0xDDDDDD}; static u32 ansi_bright_colors[] = {0x000000, 0x0000DD, 0x00DD00, 0x00DDDD, 0xDD0000, 0xDD00DD, 0xDDDD00, 0xDDDDDD};
static u32 default_fg = 0xDDDDDDDD; static u32 default_fg = 0xDDDDDDDD;
static u32 default_bg = 0x00000000; static u32 default_bg = 0x00000000;

View file

@ -8,6 +8,8 @@
#include <lib/string.h> #include <lib/string.h>
#include "inode.h"
static struct fs_driver fat32_driver = {0}; static struct fs_driver fat32_driver = {0};
static int alloc_inode(fs_t *fs, u64 mode, inode_t **out) static int alloc_inode(fs_t *fs, u64 mode, inode_t **out)
@ -37,10 +39,6 @@ static int fs_deinit(fs_t *fs)
int fat32_create(struct block_device *bdev, u64 flags, fs_t **out) int fat32_create(struct block_device *bdev, u64 flags, fs_t **out)
{ {
(void)alloc_inode;
(void)free_inode;
(void)open_inode;
return -ENOSYS;
} }
void fat32_init() void fat32_init()

View file

@ -5,4 +5,3 @@ obj-y += inode.o
obj-y += ramfs.o obj-y += ramfs.o
obj-y += devfs.o obj-y += devfs.o
obj-y += initcpio.o obj-y += initcpio.o
obj-y += uapi.o

View file

@ -1,44 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/types.h>
#include <kernel/syscall.h>
#include <lib/errno.h>
static ssize_t sys_pread(int fd, void *buf, size_t nbyte, off_t offset)
{
return -ENOSYS;
}
static ssize_t sys_read(int fd, void *buf, size_t nbyte)
{
return -ENOSYS;
}
static ssize_t sys_pwrite(int fd, const void *buf, size_t nbyte, off_t offset)
{
return -ENOSYS;
}
static ssize_t sys_write(int fd, const void *buf, size_t nbyte)
{
return -ENOSYS;
}
static int sys_open(const char *path, int flags)
{
return -ENOSYS;
}
static int sys_close(int fd)
{
return -ENOSYS;
}
void vfs_uapi_init()
{
syscall_insert(sys_pread, SYS_pread);
syscall_insert(sys_read, SYS_read);
syscall_insert(sys_pwrite, SYS_pwrite);
syscall_insert(sys_write, SYS_write);
syscall_insert(sys_open, SYS_open);
syscall_insert(sys_close, SYS_close);
}

View file

@ -315,7 +315,4 @@ void vfs_init(void)
{ {
inode_init(); inode_init();
list_create(&fs_type_list); list_create(&fs_type_list);
void vfs_uapi_init();
vfs_uapi_init();
} }

View file

@ -72,7 +72,7 @@ struct _fs {
u64 flags; u64 flags;
size_t block_size; size_t block_size;
size_t n_blocks; size_t n_blocks;
void *priv; void *priv;
dirent_t *root_dirent; dirent_t *root_dirent;
@ -97,12 +97,6 @@ struct mount {
u64 flags; u64 flags;
}; };
struct fildes {
u64 fdno;
dirent_t *file;
struct proc *proc;
};
void vfs_init(void); void vfs_init(void);
void vfs_register(struct fs_driver *fs); void vfs_register(struct fs_driver *fs);

View file

@ -21,11 +21,18 @@ typedef struct {
u64 pte; u64 pte;
} pte_t; /* page table entries */ } pte_t; /* page table entries */
struct page {
uint8_t order;
uint32_t flags;
struct page *next;
};
vaddr_t page_lowest_free(vaddr_t after, size_t len); vaddr_t page_lowest_free(vaddr_t after, size_t len);
void *map_mmio_dev(paddr_t base, size_t len); void *map_mmio_dev(paddr_t base, size_t len);
void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree); void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree);
pgd_t *pgd_get(); pgd_t *pgd_get();
pgd_t *pgd_create_user(); pgd_t *pgd_create_user();
pgd_t *pgd_clone(pgd_t *src);
void tlb_flush(); void tlb_flush();
void page_init(); void page_init();
extern pgd_t *kpgd; extern pgd_t *kpgd;

View file

@ -9,8 +9,6 @@
#include <lib/rbtree.h> #include <lib/rbtree.h>
#include "elf.h"
#define PROC_STATE_RUNNING 0 #define PROC_STATE_RUNNING 0
#define PROC_STATE_WAITING 1 #define PROC_STATE_WAITING 1
#define PROC_STATE_DNS 2 /* do not schedule */ #define PROC_STATE_DNS 2 /* do not schedule */
@ -20,23 +18,18 @@ typedef u64 tid_t;
struct vm_area { struct vm_area {
vaddr_t start; vaddr_t start;
size_t len; vaddr_t end;
u64 flags; u64 flags;
}; };
struct proc_alloc {
void *ptr;
size_t len;
};
struct proc; struct proc;
struct thread { struct thread {
tid_t tid; tid_t tid;
uaddr_t stack_addr; vaddr_t stack_addr;
size_t stack_size;
u64 state; u64 state;
bool in_kernel;
u64 tls_base; u64 tls_base;
@ -46,33 +39,18 @@ struct thread {
struct proc { struct proc {
pid_t pid; pid_t pid;
struct thread *main_thread; struct thread main_thread;
char *name; char *name;
pgd_t *pgd; rbtree_t vm_tree;
spinlock_t vm_lock;
pgd_t *pgd;
spinlock_t sched_lock; spinlock_t sched_lock;
spinlock_t mem_lock;
rbtree_t memmap;
spinlock_t alloc_lock;
rbtree_t alloc_map;
uaddr_t entry;
struct ehdr *ehdr;
struct phdr *phdrs;
size_t n_phdrs;
}; };
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);
ssize_t proc_load_file(const char *file, struct proc *proc);
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(); void proc_init();
void switch_to(struct thread *from, struct thread *to);
#endif /* _PROC_H_ */ #endif /* _PROC_H_ */

View file

@ -4,17 +4,8 @@
#include <kernel/types.h> #include <kernel/types.h>
#define SYS_read 0
#define SYS_write 1
#define SYS_open 2
#define SYS_close 3
#define SYS_pread 4
#define SYS_pwrite 5
typedef ssize_t (*syscall_t)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6); typedef ssize_t (*syscall_t)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6);
syscall_t get_syscall(u64 no); syscall_t get_syscall(u64 no);
int syscall_insert(void *fun, u64 no);
int syscall_remove(u64 no);
#endif /* _SYSCALL_H_ */ #endif /* _SYSCALL_H_ */

View file

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

View file

@ -10,10 +10,10 @@
#define MB (KB * KB) #define MB (KB * KB)
#define GB (MB * MB) #define GB (MB * MB)
typedef uintptr_t vaddr_t; typedef uintptr_t vaddr_t;
typedef uint32_t vaddr32_t; typedef uint32_t vaddr32_t;
typedef vaddr_t paddr_t; typedef vaddr_t paddr_t;
typedef vaddr_t uaddr_t;
typedef uint32_t paddr32_t; typedef uint32_t paddr32_t;
typedef int64_t ssize_t; typedef int64_t ssize_t;

View file

@ -4,7 +4,6 @@
* *
* Copyright 2023-2026 Michael Shaw * Copyright 2023-2026 Michael Shaw
*/ */
#include <arch/proc.h>
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/buddy.h> #include <kernel/buddy.h>
@ -13,7 +12,6 @@
#include <kernel/requests.h> #include <kernel/requests.h>
#include <kernel/page.h> #include <kernel/page.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/proc.h>
#include <arch/init.h> #include <arch/init.h>
@ -63,7 +61,6 @@ static void kmain()
page_init(); page_init();
ramfs_init(); ramfs_init();
struct limine_file *file = module_res->modules[0]; struct limine_file *file = module_res->modules[0];
fs_t *initramfs; fs_t *initramfs;
@ -73,14 +70,15 @@ static void kmain()
if (res < 0) if (res < 0)
panic("failed to load initramfs: %s\n", strerror(-res)); panic("failed to load initramfs: %s\n", strerror(-res));
/* "layer 2" kernel requirements */ /* start the kernel */
devfs_init(); devfs_init();
device_layer_init(); device_layer_init();
proc_init(); tty_layer_init();
/* init main fallback tty */ /* init main fallback tty */
struct tty *serial_tty = NULL; struct tty *serial_tty = NULL;
serial_port_tty_init(&serial_tty); serial_port_tty_init(&serial_tty);
struct tty *visual_tty = fbcon_init(); struct tty *visual_tty = fbcon_init();
struct tty *tee = tty_tee_create("broadcast"); struct tty *tee = tty_tee_create("broadcast");
@ -89,33 +87,12 @@ static void kmain()
tty_printf = tee; tty_printf = tee;
do_initcalls(); do_initcalls();
arch_ap_startup(); arch_ap_startup();
dirent_t *init; /* do nothing, for now :) */
res = vfs_open("/bin/utest", &init);
if (res < 0)
panic("failed to open /bin/utest: %s\n", strerror(-res));
char *init_buf = kalloc(init->inode->file_size, ALLOC_KERN);
if (!init_buf)
panic("failed to allocate buffer for /bin/utest: %s\n", strerror(ENOMEM));
res = vfs_read(init->inode, init_buf, init->inode->file_size, 0);
if (res < 0)
panic("failed to read /bin/utest: %s\n", strerror(-res));
struct proc *init_proc = proc_create(NULL);
uaddr_t entry = proc_load_file(init_buf, init_proc);
if ((ssize_t)entry < 0)
panic("failed to load /bin/utest: %s\n", strerror(-(ssize_t)entry));
kernel_init_ready = true; kernel_init_ready = true;
yield();
init_proc->main_thread->state = PROC_STATE_RUNNING;
enter_userspace_init(init_proc->main_thread, entry);
__builtin_unreachable();
} }
void _before_stack() void _before_stack()

View file

@ -22,11 +22,10 @@ int block_device_register(char *name, size_t lba_start, struct block_ops *ops, s
strncpy(ret->name, name, sizeof(ret->name) - 1); strncpy(ret->name, name, sizeof(ret->name) - 1);
*out = ret; *out = ret;
memcpy(&ret->ops, ops, sizeof(struct block_ops)); memcpy(ops, &ret->ops, sizeof(struct block_ops));
dirent_t *dirent; dirent_t *dirent;
int res = devfs_insert_device_blk(ret->name, ret, &dirent); int res = devfs_insert_device_blk(ret->name, ret, &dirent);
(void)res;
return 0; return 0;
} }
@ -39,7 +38,7 @@ int block_check_partitions(struct block_device *bdev)
int ret = 0; int ret = 0;
if (block_has_gpt_header(bdev)) { if (block_has_gpt_header(bdev)) {
struct gpt_partition *descs = NULL; struct gpt_partition *descs;
int n_partitions = gpt_parse_partitions(bdev, &descs); int n_partitions = gpt_parse_partitions(bdev, &descs);
for (int i = 0; i < n_partitions; i++) { for (int i = 0; i < n_partitions; i++) {
@ -56,7 +55,7 @@ int block_check_partitions(struct block_device *bdev)
krdy("registered block device /dev/%s\n", blkdev->name); krdy("registered block device /dev/%s\n", blkdev->name);
} }
if (descs) if (n_partitions)
kfree(descs); kfree(descs);
} }

View file

@ -1,3 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
obj-y += proc.o obj-y += proc.o
obj-y += load.o

View file

@ -1,59 +0,0 @@
/* 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, uaddr_t *entry);
#endif /* _ELF_H_ */

View file

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

View file

@ -1,10 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/page.h>
#include <kernel/proc.h> #include <kernel/proc.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <lib/string.h>
#define USTACK_DEFAULT_SIZE 0x10000
mem_cache_t *proc_slab; mem_cache_t *proc_slab;
mem_cache_t *thread_slab; mem_cache_t *thread_slab;
@ -14,100 +10,25 @@ int proc_kill(struct proc *proc, int signal)
return -ENOSYS; 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);
node = rbt_successor(node);
}
spinlock_release(&parent->mem_lock);
return 0;
}
struct proc *proc_create(struct proc *parent) struct proc *proc_create(struct proc *parent)
{ {
struct proc *ret = slab_alloc(proc_slab); struct proc *ret = slab_alloc(proc_slab);
if (!ret) if (!ret)
return NULL; return NULL;
memset(ret, 0, sizeof(struct proc));
struct thread *r_mt = slab_alloc(thread_slab); struct thread *r_mt = slab_alloc(thread_slab);
if (!r_mt) { if (!r_mt) {
slab_free(proc_slab, ret); slab_free(proc_slab, ret);
return NULL; return NULL;
} }
r_mt->stack_size = USTACK_DEFAULT_SIZE; pgd_t *p;
r_mt->stack_addr = 0x7FFFFFFFF000; if (parent)
r_mt->proc = ret; p = parent->pgd;
r_mt->state = PROC_STATE_DNS; else
p = kpgd;
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->stack_addr - r_mt->stack_size, r_mt->stack_size, PAGE_RW);
ret->main_thread = r_mt;
if (parent) {
int res = copy_parent(ret, parent);
if (res)
kerr("failed to copy parent memmap for new process: %s\n", strerror(-res));
}
return ret; return ret;
} }
@ -121,6 +42,4 @@ void proc_init()
{ {
proc_slab = slab_create("proc_slab", sizeof(struct proc), 1 * MB, 8); proc_slab = slab_create("proc_slab", sizeof(struct proc), 1 * MB, 8);
thread_slab = slab_create("thread_slab", sizeof(struct thread), 1 * MB, 8); thread_slab = slab_create("thread_slab", sizeof(struct thread), 1 * MB, 8);
krdy("proc_init ready\n");
} }

View file

@ -12,27 +12,3 @@ syscall_t get_syscall(u64 no)
return syscalls[no]; return syscalls[no];
} }
int syscall_insert(void *fun, u64 no)
{
if (no >= ARRAY_SIZE(syscalls))
return -EINVAL;
if (syscalls[no])
return -EEXIST;
syscalls[no] = (syscall_t)fun;
return no;
}
int syscall_remove(u64 no)
{
if (no >= ARRAY_SIZE(syscalls))
return -EINVAL;
if (!syscalls[no])
return -ENOENT;
syscalls[no] = NULL;
return 0;
}

View file

@ -5,11 +5,13 @@
#include <drivers/serial.h> #include <drivers/serial.h>
#include <kernel/tty.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}; 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_create(const char *name, void *priv)
{ {
struct tty *tty = kzalloc(sizeof(struct tty), ALLOC_KERN); struct tty *tty = slab_alloc(tty_slab);
dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name); dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name);
@ -36,3 +38,12 @@ void tty_write(struct tty *tty, const char *s)
while ((c = *s++)) while ((c = *s++))
tty_putchar(tty, c); 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

@ -235,9 +235,6 @@ void buddy_zone_free(struct buddy_zone *bzone, void *ptr)
if (bzone->order_freelists[h->order] == buddy) if (bzone->order_freelists[h->order] == buddy)
bzone->order_freelists[h->order] = buddy->next; bzone->order_freelists[h->order] = buddy->next;
if ((vaddr_t)buddy < (vaddr_t)h)
h = buddy;
h->order += 1; h->order += 1;
buddy = get_buddy(h); buddy = get_buddy(h);
} }

View file

@ -4,12 +4,18 @@
#include <kernel/buddy.h> #include <kernel/buddy.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/page.h> #include <kernel/page.h>
#include <kernel/proc.h>
#include <arch/page.h> #include <arch/page.h>
#include <lib/rbtree.h> #include <lib/rbtree.h>
#include <lib/math.h> #include <lib/math.h>
#include <lib/string.h> #include <lib/string.h>
struct map_entry {
vaddr_t vaddr;
vaddr_t paddr;
size_t len;
};
static rbtree_t kmap_tree = {0}; static rbtree_t kmap_tree = {0};
static mem_cache_t *map_entry_cache = NULL; static mem_cache_t *map_entry_cache = NULL;
pgd_t *kpgd; pgd_t *kpgd;
@ -49,13 +55,13 @@ 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) void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree)
{ {
struct vm_area *insert = slab_alloc(map_entry_cache); struct map_entry *insert = slab_alloc(map_entry_cache);
insert->start = vaddr; insert->paddr = paddr;
insert->vaddr = vaddr;
insert->len = len; insert->len = len;
insert->flags = flags;
rbnode_t *node = rbt_insert(map_tree, vaddr); rbnode_t *node = rbt_insert(map_tree, vaddr);
node->ptr = insert; node->value = (u64)insert;
vaddr_t cur_vaddr = vaddr & PAGE_MASK; vaddr_t cur_vaddr = vaddr & PAGE_MASK;
paddr_t cur_paddr = paddr & PAGE_MASK; paddr_t cur_paddr = paddr & PAGE_MASK;
@ -70,24 +76,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)]; pgd_t *pgd_entry = &pgd_table[pgd_index(cur_vaddr)];
if (!(pgd_entry->pgd & PAGE_PRESENT)) { if (!(pgd_entry->pgd & PAGE_PRESENT)) {
pgd_entry->pgd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW | (flags & PAGE_USER); pgd_entry->pgd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
} }
pud_t *pud_table = (pud_t *)TO_VIRT(pgd_entry->pgd & PAGE_MASK); pud_t *pud_table = (pud_t *)TO_VIRT(pgd_entry->pgd & PAGE_MASK);
pud_t *pud_entry = &pud_table[pud_index(cur_vaddr)]; pud_t *pud_entry = &pud_table[pud_index(cur_vaddr)];
if (!(pud_entry->pud & PAGE_PRESENT)) { if (!(pud_entry->pud & PAGE_PRESENT)) {
pud_entry->pud = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW | (flags & PAGE_USER); pud_entry->pud = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
} }
pmd_t *pmd_table = (pmd_t *)TO_VIRT(pud_entry->pud & PAGE_MASK); pmd_t *pmd_table = (pmd_t *)TO_VIRT(pud_entry->pud & PAGE_MASK);
pmd_t *pmd_entry = &pmd_table[pmd_index(cur_vaddr)]; pmd_t *pmd_entry = &pmd_table[pmd_index(cur_vaddr)];
if (!(pmd_entry->pmd & PAGE_PRESENT)) { if (!(pmd_entry->pmd & PAGE_PRESENT)) {
pmd_entry->pmd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW | (flags & PAGE_USER); pmd_entry->pmd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
} }
pte_t *pte_table = (pte_t *)TO_VIRT(pmd_entry->pmd & PAGE_MASK); pte_t *pte_table = (pte_t *)TO_VIRT(pmd_entry->pmd & PAGE_MASK);
pte_t *pte_entry = pte_table + pte_index(cur_vaddr); pte_t *pte_entry = pte_table + pte_index(cur_vaddr);
pte_entry->pte = (cur_paddr & PAGE_MASK) | PAGE_PRESENT | (flags & 0xFFF) | (flags & PAGE_XD); pte_entry->pte = (cur_paddr & PAGE_MASK) | (flags & 0xFFF);
cur_vaddr += PAGE_SIZE; cur_vaddr += PAGE_SIZE;
cur_paddr += PAGE_SIZE; cur_paddr += PAGE_SIZE;
@ -149,7 +155,7 @@ void *map_mmio_dev(paddr_t base, size_t len)
void page_init() void page_init()
{ {
map_entry_cache = slab_create("map_entry_cache", sizeof(struct vm_area), 64 * KB, 0x8); map_entry_cache = slab_create("map_entry_cache", sizeof(struct map_entry), 64 * KB, 0x8);
kpgd = pt_alloc(); kpgd = pt_alloc();
map_hhdm_zones(kpgd); map_hhdm_zones(kpgd);
@ -170,3 +176,62 @@ pgd_t *pgd_create_user()
return pgd; return pgd;
} }
pgd_t *pgd_clone(pgd_t *src)
{
pgd_t *pgd = pt_alloc();
/* don't clone a bunch of kernel stuff, just keep it */
for (int i = 256; i < 512; i++)
pgd[i] = kpgd[i];
for (int i = 0; i < 256; i++) {
pgd_t *src_pgd = &src[i];
if (!(src_pgd->pgd & PAGE_PRESENT))
continue;
pud_t *src_pud = (pud_t *)TO_VIRT(src_pgd->pgd & PAGE_MASK);
pud_t *new_pud = pt_alloc();
pgd[i].pgd = TO_PHYS((u64)new_pud) | (src_pgd->pgd & 0xFFF);
for (int j = 0; j < 512; j++) {
pud_t *src_pud_e = &src_pud[j];
if (!(src_pud_e->pud & PAGE_PRESENT))
continue;
pmd_t *src_pmd = (pmd_t *)TO_VIRT(src_pud_e->pud & PAGE_MASK);
pmd_t *new_pmd = pt_alloc();
new_pud[j].pud = TO_PHYS((u64)new_pmd) | (src_pud_e->pud & 0xFFF);
for (int k = 0; k < 512; k++) {
pmd_t *src_pmd_e = &src_pmd[k];
if (!(src_pmd_e->pmd & PAGE_PRESENT))
continue;
if (src_pmd_e->pmd & PAGE_PS) {
new_pmd[k] = *src_pmd_e;
continue;
}
pte_t *src_pte = (pte_t *)TO_VIRT(src_pmd_e->pmd & PAGE_MASK);
pte_t *new_pte = pt_alloc();
new_pmd[k].pmd = TO_PHYS((u64)new_pte) | (src_pmd_e->pmd & 0xFFF);
for (int l = 0; l < 512; l++) {
pte_t pte = src_pte[l];
if (!(pte.pte & PAGE_PRESENT))
continue;
paddr_t phys = pte.pte & PAGE_MASK;
void *copy = buddy_alloc(0);
memcpy(TO_VIRT(copy), TO_VIRT(phys), PAGE_SIZE);
new_pte[l].pte = (paddr_t)copy | (pte.pte & ~PAGE_MASK);
}
}
}
}
return pgd;
}

View file

@ -4,10 +4,10 @@
# #
# Important to remember that this is run at the project-root-level, # Important to remember that this is run at the project-root-level,
# all of it, as it is invoked with $(MAKE) -f # all of it, as it is invoked with $(MAKE) -f
#ifneq ($(shell test -e .config && echo -n yes),yes) ifneq ($(shell test -e .config && echo -n yes),yes)
# $(error Please type `make config` to generate a config) $(error Please type `make config` to generate a config)
#endif endif
#-include .config -include .config
export export
obj-y := obj-y :=
@ -22,11 +22,7 @@ real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o) subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
deps := $(real-objs-y:.o=.d) deps := $(real-objs-y:.o=.d)
ifeq ($(obj),.)
_all: $(subdir-objs-y)
else
_all: $(obj)/built-in.o _all: $(obj)/built-in.o
endif
$(obj)/%.o: $(obj)/%.c $(obj)/%.o: $(obj)/%.c
$(Q)echo " CC $@" $(Q)echo " CC $@"

View file

@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
# Makefile stuff for all the booting and bootloader related stuff # Makefile stuff for all the booting and bootloader related stuff
# #
INITRAMFS := initramfs.cpio
$(IMG): limine/limine $(KERNEL) $(IMG): limine/limine $(KERNEL)
rm -rf $(IMG) rm -rf $(IMG)
@ -26,7 +27,7 @@ $(IMG): limine/limine $(KERNEL)
limine/limine: limine/limine:
rm -rf limine rm -rf limine
git clone https://codeberg.org/Limine/Limine.git limine --branch=$(LIMINE_VERSION)-binary --depth=1 git clone https://codeberg.org/Limine/Limine.git limine --branch=$(LIMINE_VERSION)-binary --depth=1
$(MAKE) --no-print-directory -C limine \ $(MAKE) -C limine \
CC="$(HOST_CC)" \ CC="$(HOST_CC)" \
CFLAGS="$(HOST_CFLAGS)" \ CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS)" \ CPPFLAGS="$(HOST_CPPFLAGS)" \
@ -67,4 +68,3 @@ edk2-ovmf:
distclean: distclean:
make clean make clean
rm -rf limine rm -rf limine
rm -rf $(INITRAMFS)

View file

@ -1,26 +0,0 @@
ifneq ($(V),1)
Q := @
else
Q :=
endif
usr_dirs := utest
usr_bins := $(foreach d,$(usr_dirs),$d/$d)
.PHONY: all $(usr_dirs)
all: initramfs
$(usr_dirs):
$(Q)$(MAKE) -C $@ DIR=usr.bin/$@
.PHONY: initramfs
initramfs: $(usr_dirs)
$(Q)mkdir -p ../initramfs/bin/
$(Q)cp $(usr_bins) ../initramfs/bin/
.PHONY: clean
clean:
$(Q)for dir in $(usr_dirs); do \
echo " CLEAN usr.bin/$$dir"; \
$(MAKE) --no-print-directory -C $$dir clean; \
done

View file

@ -1,30 +0,0 @@
ifneq ($(V),1)
Q := @
else
Q :=
endif
USR_CFLAGS ?= -g3 -O0 -pipe -Wall -ffreestanding -nostdlib \
-fno-stack-protector -m64 -march=x86-64 -mabi=sysv \
-Iinclude -MMD
USR_LDFLAGS ?= -nostdlib -static -m elf_x86_64 -z max-page-size=0x1000
$(OUT): $(UOBJ)
$(Q)echo " LD [U] $(DIR)/$@"
$(Q)$(LD) $(USR_LDFLAGS) -o $@ $^
%.o: %.c
$(Q)echo " CC [U] $(DIR)/$@"
$(Q)$(CC) $(USR_CFLAGS) -c $< -o $@
%.o: %.S
$(Q)echo " CC [U] $(DIR)/$@"
$(Q)$(CC) $(USR_CFLAGS) -c $< -o $@
clean:
$(Q)rm -rf $(UOBJ) $(OUT)
print-%:
$(Q)echo $($(*))

View file

@ -1,4 +0,0 @@
# SPDX-License-Identifier: GPL-2.0-only
OUT := utest
UOBJ := utest.o
-include ../Makefile.common

Binary file not shown.

View file

@ -1,6 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
void _start()
{
while (1)
(void)0;
}