Compare commits

...
Author SHA1 Message Date
09116d65ea vfs: add syscall layer skeleton
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 19:47:41 -04:00
cd916f624b proc: enter into userspace
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 19:25:38 -04:00
1a8061e3b3 block: fix invalid free condition
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 17:48:34 -04:00
b8560b4577 buddy: fix bug in buddy_zone_free
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 17:40:11 -04:00
48ba3d4fa2 proc_create: init user stack
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 16:52:30 -04:00
19b9d68651 types: add uaddr_t
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 00:21:16 -04:00
30c14a0fef proc: copy memmap from parent
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-19 00:07:08 -04:00
14c2f27d40 page: use union element ptr
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-18 23:29:26 -04:00
cc84d4a5a2 proc: setup usr.bin
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-18 23:22:31 -04:00
f47a4bfa2c usr.bin: add usr.bin
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-18 15:28:53 -04:00
1726663cab fbcon: fix palette issue
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 21:35:18 -04:00
75645d5c12 fat32: silence warnings
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 21:29:14 -04:00
6fde139c10 block: silence warning
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 21:27:21 -04:00
6b40cca5b0 proc: add basic proc loading
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 20:11:19 -04:00
f9ddb8f379 page: remove unused struct page
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-17 16:22:55 -04:00
35 changed files with 577 additions and 146 deletions

View file

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

View file

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

View file

@ -78,15 +78,14 @@ _syscall_enter:
mov rsp, gs:[8]
/* save registers into struct pt_regs */
push gs:[16] /* save user RSP */
push qword ptr gs:[16] /* save user RSP */
push_all
mov rdi, rsp
call syscall
/* restore registers */
pop_all
add rsp, 8
mov rsp, [rsp] /* load user RSP */
pop rsp
swapgs
sysretq
@ -99,6 +98,30 @@ switch_to:
pop_all
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 */
exception 0x00
exception 0x01

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

@ -16,11 +16,11 @@ struct cpu_local {
struct pt_regs {
u64 rax;
u64 rbx;
u64 rcx;
u64 rcx;
u64 rdx;
u64 rdi;
u64 rsi;
u64 rbp;
u64 rbp;
u64 r8;
u64 r9;
u64 r10;
@ -29,8 +29,12 @@ struct pt_regs {
u64 r13;
u64 r14;
u64 r15;
u64 rsp;
u64 rip;
u64 rsp;
} 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_ */

View file

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

45
amd64/kernel/proc.c Normal file
View file

@ -0,0 +1,45 @@
/* 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)
{
static u32 ansi_colors[] = {0x000000, 0x0000AA, 0x00AA00, 0x00AAAA, 0xAA0000, 0xAA00AA, 0xAAAA00, 0xAAAAAA};
static u32 ansi_colors[] = {0x000000, 0xAA0000, 0x00AA00, 0x00AAAA, 0x0000AA, 0xAA00AA, 0xAAAA00, 0xAAAAAA};
static u32 ansi_bright_colors[] = {0x000000, 0x0000DD, 0x00DD00, 0x00DDDD, 0xDD0000, 0xDD00DD, 0xDDDD00, 0xDDDDDD};
static u32 ansi_bright_colors[] = {0x000000, 0xDD00DD, 0x00DD00, 0x00DDDD, 0x0000DD, 0xDD00DD, 0xDDDD00, 0xDDDDDD};
static u32 default_fg = 0xDDDDDDDD;
static u32 default_bg = 0x00000000;

View file

@ -8,8 +8,6 @@
#include <lib/string.h>
#include "inode.h"
static struct fs_driver fat32_driver = {0};
static int alloc_inode(fs_t *fs, u64 mode, inode_t **out)
@ -39,6 +37,10 @@ static int fs_deinit(fs_t *fs)
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()

View file

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

44
fs/vfs/uapi.c Normal file
View file

@ -0,0 +1,44 @@
/* 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,4 +315,7 @@ void vfs_init(void)
{
inode_init();
list_create(&fs_type_list);
void vfs_uapi_init();
vfs_uapi_init();
}

View file

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

View file

@ -21,18 +21,11 @@ typedef struct {
u64 pte;
} 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);
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);
pgd_t *pgd_get();
pgd_t *pgd_create_user();
pgd_t *pgd_clone(pgd_t *src);
void tlb_flush();
void page_init();
extern pgd_t *kpgd;

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 */
@ -18,18 +20,23 @@ typedef u64 tid_t;
struct vm_area {
vaddr_t start;
vaddr_t end;
size_t len;
u64 flags;
};
struct proc_alloc {
void *ptr;
size_t len;
};
struct proc;
struct thread {
tid_t tid;
vaddr_t stack_addr;
uaddr_t stack_addr;
size_t stack_size;
u64 state;
bool in_kernel;
u64 tls_base;
@ -39,18 +46,33 @@ struct thread {
struct proc {
pid_t pid;
struct thread main_thread;
struct thread *main_thread;
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;
spinlock_t alloc_lock;
rbtree_t alloc_map;
uaddr_t entry;
struct ehdr *ehdr;
struct phdr *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);
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();
#endif /* _PROC_H_ */

View file

@ -4,8 +4,17 @@
#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);
syscall_t get_syscall(u64 no);
int syscall_insert(void *fun, u64 no);
int syscall_remove(u64 no);
#endif /* _SYSCALL_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

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

View file

@ -4,6 +4,7 @@
*
* Copyright 2023-2026 Michael Shaw
*/
#include <arch/proc.h>
#include <kernel/common.h>
#include <kernel/buddy.h>
@ -12,6 +13,7 @@
#include <kernel/requests.h>
#include <kernel/page.h>
#include <kernel/slab.h>
#include <kernel/proc.h>
#include <arch/init.h>
@ -61,6 +63,7 @@ static void kmain()
page_init();
ramfs_init();
struct limine_file *file = module_res->modules[0];
fs_t *initramfs;
@ -70,15 +73,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,12 +89,33 @@ static void kmain()
tty_printf = tee;
do_initcalls();
arch_ap_startup();
/* do nothing, for now :) */
dirent_t *init;
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;
yield();
init_proc->main_thread->state = PROC_STATE_RUNNING;
enter_userspace_init(init_proc->main_thread, entry);
__builtin_unreachable();
}
void _before_stack()

View file

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

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, uaddr_t *entry);
#endif /* _ELF_H_ */

88
kernel/proc/load.c Normal file
View file

@ -0,0 +1,88 @@
/* 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,6 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/page.h>
#include <kernel/proc.h>
#include <kernel/slab.h>
#include <lib/string.h>
#define USTACK_DEFAULT_SIZE 0x10000
mem_cache_t *proc_slab;
mem_cache_t *thread_slab;
@ -10,25 +14,100 @@ 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);
node = rbt_successor(node);
}
spinlock_release(&parent->mem_lock);
return 0;
}
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;
else
p = kpgd;
r_mt->stack_size = USTACK_DEFAULT_SIZE;
r_mt->stack_addr = 0x7FFFFFFFF000;
r_mt->proc = ret;
r_mt->state = PROC_STATE_DNS;
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;
}
@ -42,4 +121,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

@ -12,3 +12,27 @@ syscall_t get_syscall(u64 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,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

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

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,13 +49,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)
{
struct map_entry *insert = slab_alloc(map_entry_cache);
insert->paddr = paddr;
insert->vaddr = vaddr;
struct vm_area *insert = slab_alloc(map_entry_cache);
insert->start = vaddr;
insert->len = len;
insert->flags = flags;
rbnode_t *node = rbt_insert(map_tree, vaddr);
node->value = (u64)insert;
node->ptr = insert;
vaddr_t cur_vaddr = vaddr & PAGE_MASK;
paddr_t cur_paddr = paddr & PAGE_MASK;
@ -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);
@ -176,62 +170,3 @@ pgd_t *pgd_create_user()
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,
# all of it, as it is invoked with $(MAKE) -f
ifneq ($(shell test -e .config && echo -n yes),yes)
$(error Please type `make config` to generate a config)
endif
-include .config
#ifneq ($(shell test -e .config && echo -n yes),yes)
# $(error Please type `make config` to generate a config)
#endif
#-include .config
export
obj-y :=
@ -22,7 +22,11 @@ real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
deps := $(real-objs-y:.o=.d)
ifeq ($(obj),.)
_all: $(subdir-objs-y)
else
_all: $(obj)/built-in.o
endif
$(obj)/%.o: $(obj)/%.c
$(Q)echo " CC $@"

View file

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

26
usr.bin/Makefile Normal file
View file

@ -0,0 +1,26 @@
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

30
usr.bin/Makefile.common Normal file
View file

@ -0,0 +1,30 @@
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 $($(*))

4
usr.bin/utest/Makefile Normal file
View file

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

BIN
usr.bin/utest/utest Executable file

Binary file not shown.

6
usr.bin/utest/utest.c Normal file
View file

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