Redesign userspace memory management
All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s
This commit is contained in:
@@ -7,11 +7,9 @@
|
|||||||
#define SYS_UNMAP 4
|
#define SYS_UNMAP 4
|
||||||
#define SYS_CLONE 5
|
#define SYS_CLONE 5
|
||||||
#define SYS_SCHED 6
|
#define SYS_SCHED 6
|
||||||
#define SYS_CREATE_MEM 7
|
#define SYS_CREATE_MUTEX 7
|
||||||
#define SYS_UNLINK_MEM 8
|
#define SYS_UNLINK_MUTEX 8
|
||||||
#define SYS_CREATE_MUTEX 9
|
#define SYS_LOCK_MUTEX 9
|
||||||
#define SYS_UNLINK_MUTEX 10
|
#define SYS_UNLOCK_MUTEX 10
|
||||||
#define SYS_LOCK_MUTEX 11
|
|
||||||
#define SYS_UNLOCK_MUTEX 12
|
|
||||||
|
|
||||||
#endif // _M_SYSCALL_DEFS_H
|
#endif // _M_SYSCALL_DEFS_H
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string/string.h>
|
#include <string/string.h>
|
||||||
|
|
||||||
#define EXAMPLE 1
|
#define EXAMPLE 2
|
||||||
|
|
||||||
#if EXAMPLE == 1
|
#if EXAMPLE == 1
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ static atomic_int pids = 1;
|
|||||||
|
|
||||||
struct proc* proc_from_elf (uint8_t* elf_contents) {
|
struct proc* proc_from_elf (uint8_t* elf_contents) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
int rid;
|
|
||||||
|
|
||||||
struct proc* proc = malloc (sizeof (*proc));
|
struct proc* proc = malloc (sizeof (*proc));
|
||||||
if (proc == NULL)
|
if (proc == NULL)
|
||||||
@@ -39,32 +38,11 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
|
|||||||
}
|
}
|
||||||
procgroup_attach (proc->procgroup, proc);
|
procgroup_attach (proc->procgroup, proc);
|
||||||
|
|
||||||
rid = procgroup_get_sys_rid (proc->procgroup);
|
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
||||||
struct proc_resource* kstk_r =
|
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
||||||
proc_create_resource_mem (proc->procgroup, rid, KSTACK_SIZE / PAGE_SIZE, 0, false);
|
|
||||||
if (kstk_r == NULL) {
|
|
||||||
procgroup_detach (proc->procgroup, proc);
|
|
||||||
free (proc);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
proc->pdata.kernel_stack = kstk_r->u.mem.paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
procgroup_map (proc->procgroup, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
|
||||||
|
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW, NULL);
|
||||||
rid = procgroup_get_sys_rid (proc->procgroup);
|
|
||||||
struct proc_resource* ustk_r =
|
|
||||||
proc_create_resource_mem (proc->procgroup, rid, USTACK_SIZE / PAGE_SIZE, 0, false);
|
|
||||||
if (ustk_r == NULL) {
|
|
||||||
kstk_r->ops.cleanup (kstk_r);
|
|
||||||
free (kstk_r);
|
|
||||||
procgroup_detach (proc->procgroup, proc);
|
|
||||||
free (proc);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
proc->pdata.user_stack = ustk_r->u.mem.paddr;
|
|
||||||
|
|
||||||
proc_map (proc, proc->pdata.user_stack, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
|
|
||||||
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW);
|
|
||||||
|
|
||||||
proc->flags |= PROC_USTK_PREALLOC;
|
proc->flags |= PROC_USTK_PREALLOC;
|
||||||
|
|
||||||
@@ -83,7 +61,6 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, size_t stack_
|
|||||||
uintptr_t entry) {
|
uintptr_t entry) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
spin_lock_ctx_t ctxprt;
|
spin_lock_ctx_t ctxprt;
|
||||||
int rid;
|
|
||||||
|
|
||||||
struct proc* proc = malloc (sizeof (*proc));
|
struct proc* proc = malloc (sizeof (*proc));
|
||||||
if (proc == NULL)
|
if (proc == NULL)
|
||||||
@@ -102,27 +79,8 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, size_t stack_
|
|||||||
|
|
||||||
spin_unlock (&proto->lock, &ctxprt);
|
spin_unlock (&proto->lock, &ctxprt);
|
||||||
|
|
||||||
uintptr_t vstack_bottom = vstack_top - stack_size;
|
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
||||||
|
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
||||||
uintptr_t pstack_bottom = mm_v2p (&proc->procgroup->pd, vstack_bottom, MM_PD_LOCK);
|
|
||||||
if (pstack_bottom == 0) {
|
|
||||||
procgroup_detach (proc->procgroup, proc);
|
|
||||||
free (proc);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
rid = procgroup_get_sys_rid (proc->procgroup);
|
|
||||||
struct proc_resource* kstk_r =
|
|
||||||
proc_create_resource_mem (proc->procgroup, rid, KSTACK_SIZE / PAGE_SIZE, 0, false);
|
|
||||||
if (kstk_r == NULL) {
|
|
||||||
procgroup_detach (proc->procgroup, proc);
|
|
||||||
free (proc);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
proc->pdata.kernel_stack = kstk_r->u.mem.paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
|
||||||
|
|
||||||
proc->pdata.user_stack = pstack_bottom + stack_size;
|
|
||||||
|
|
||||||
proc->pdata.regs.ss = GDT_UDATA | 0x03;
|
proc->pdata.regs.ss = GDT_UDATA | 0x03;
|
||||||
proc->pdata.regs.rsp = (uint64_t)vstack_top;
|
proc->pdata.regs.rsp = (uint64_t)vstack_top;
|
||||||
@@ -161,24 +119,7 @@ void proc_cleanup (struct proc* proc) {
|
|||||||
|
|
||||||
procgroup_detach (proc->procgroup, proc);
|
procgroup_detach (proc->procgroup, proc);
|
||||||
|
|
||||||
/* clean virtual address space */
|
pmm_free (proc->pdata.kernel_stack, KSTACK_SIZE / PAGE_SIZE);
|
||||||
/* if (atomic_fetch_sub (&proc->pd->refs, 1) == 1) { */
|
|
||||||
/* DEBUG ("PID %d Free virtual address space\n", proc->pid); */
|
|
||||||
/* struct list_node_link *mapping_link, *mapping_link_tmp; */
|
|
||||||
/* spin_lock (&proc->pd->lock, &ctxprpd); */
|
|
||||||
|
|
||||||
/* list_foreach (proc->mappings, mapping_link, mapping_link_tmp) { */
|
|
||||||
/* struct proc_mapping* mapping = */
|
|
||||||
/* list_entry (mapping_link, struct proc_mapping, proc_mappings_link); */
|
|
||||||
|
|
||||||
/* list_remove (proc->mappings, mapping_link); */
|
|
||||||
/* free (mapping); */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
/* pmm_free (proc->pd->cr3_paddr, 1); */
|
|
||||||
/* spin_unlock (&proc->pd->lock, &ctxprpd); */
|
|
||||||
/* free (proc->pd); */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
/* clean the process */
|
/* clean the process */
|
||||||
free (proc);
|
free (proc);
|
||||||
|
|||||||
@@ -4,15 +4,16 @@
|
|||||||
#include <amd64/intr.h>
|
#include <amd64/intr.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
|
||||||
/// Top of userspace process' stack
|
/* Top of userspace process' stack */
|
||||||
#define PROC_USTACK_TOP 0x00007FFFFFFFF000ULL
|
#define PROC_USTACK_TOP 0x00007FFFFFFFF000ULL
|
||||||
/// Size of userspace process' stack
|
/* Size of userspace process' stack */
|
||||||
#define USTACK_SIZE (256 * PAGE_SIZE)
|
#define USTACK_SIZE (256 * PAGE_SIZE)
|
||||||
|
/* proc_map () base address */
|
||||||
|
#define PROC_MAP_BASE 0x0000700000000000
|
||||||
|
|
||||||
/// Platform-dependent process data
|
/* Platform-dependent process data */
|
||||||
struct proc_platformdata {
|
struct proc_platformdata {
|
||||||
struct saved_regs regs;
|
struct saved_regs regs;
|
||||||
uintptr_t user_stack;
|
|
||||||
uintptr_t kernel_stack;
|
uintptr_t kernel_stack;
|
||||||
uint64_t gs_base;
|
uint64_t gs_base;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
extern void amd64_syscall_entry (void);
|
extern void amd64_syscall_entry (void);
|
||||||
|
|
||||||
int amd64_syscall_dispatch (void* stack_ptr) {
|
uintptr_t amd64_syscall_dispatch (void* stack_ptr) {
|
||||||
spin_lock_ctx_t ctxcpu, ctxpr;
|
spin_lock_ctx_t ctxcpu, ctxpr;
|
||||||
|
|
||||||
amd64_load_kernel_cr3 ();
|
amd64_load_kernel_cr3 ();
|
||||||
@@ -35,8 +35,7 @@ int amd64_syscall_dispatch (void* stack_ptr) {
|
|||||||
return -ST_SYSCALL_NOT_FOUND;
|
return -ST_SYSCALL_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = func (caller, regs, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
|
return func (caller, regs, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void syscall_init (void) {
|
void syscall_init (void) {
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef _KERNEL_PROC_KPPROC_FB_H
|
|
||||||
#define _KERNEL_PROC_KPPROC_FB_H
|
|
||||||
|
|
||||||
#include <aux/compiler.h>
|
|
||||||
#include <libk/std.h>
|
|
||||||
|
|
||||||
/* data to expose as a kpproc resource */
|
|
||||||
struct kpproc_fb {
|
|
||||||
uintptr_t paddr;
|
|
||||||
uint64_t w, h, pitch;
|
|
||||||
uint16_t bpp;
|
|
||||||
uint8_t red_mask_size;
|
|
||||||
uint8_t red_mask_shift;
|
|
||||||
uint8_t green_mask_size;
|
|
||||||
uint8_t green_mask_shift;
|
|
||||||
uint8_t blue_mask_size;
|
|
||||||
uint8_t blue_mask_shift;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // _KERNEL_PROC_KPPROC_FB_H
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#include <libk/std.h>
|
|
||||||
#include <mm/pmm.h>
|
|
||||||
#include <proc/mem.h>
|
|
||||||
#include <proc/proc.h>
|
|
||||||
#include <proc/resource.h>
|
|
||||||
#include <sync/spin_lock.h>
|
|
||||||
|
|
||||||
void proc_cleanup_resource_mem (struct proc_resource* resource) {
|
|
||||||
if (!resource->u.mem.managed)
|
|
||||||
pmm_free (resource->u.mem.paddr, resource->u.mem.pages);
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef _KERNEL_PROC_MEM_H
|
|
||||||
#define _KERNEL_PROC_MEM_H
|
|
||||||
|
|
||||||
#include <libk/std.h>
|
|
||||||
|
|
||||||
struct proc;
|
|
||||||
struct proc_resource;
|
|
||||||
|
|
||||||
struct proc_resource_mem {
|
|
||||||
struct proc_resource* resource;
|
|
||||||
|
|
||||||
uintptr_t paddr;
|
|
||||||
size_t pages;
|
|
||||||
ptrdiff_t alive_pages;
|
|
||||||
bool managed;
|
|
||||||
};
|
|
||||||
|
|
||||||
void proc_cleanup_resource_mem (struct proc_resource* resource);
|
|
||||||
|
|
||||||
#endif // _KERNEL_PROC_MEM_H
|
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
#include <limine/requests.h>
|
#include <limine/requests.h>
|
||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
#include <mm/pmm.h>
|
#include <mm/pmm.h>
|
||||||
#include <proc/kpproc_fb.h>
|
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
#include <proc/procgroup.h>
|
#include <proc/procgroup.h>
|
||||||
#include <proc/resource.h>
|
#include <proc/resource.h>
|
||||||
@@ -34,104 +33,12 @@ static rw_spin_lock_t proc_tree_lock = RW_SPIN_LOCK_INIT;
|
|||||||
|
|
||||||
static atomic_int sched_cycles = 0;
|
static atomic_int sched_cycles = 0;
|
||||||
|
|
||||||
/* kernel pseudo process */
|
|
||||||
static struct proc kpproc;
|
|
||||||
|
|
||||||
static bool proc_check_elf (uint8_t* elf) {
|
static bool proc_check_elf (uint8_t* elf) {
|
||||||
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
|
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
|
|
||||||
uint32_t flags) {
|
|
||||||
spin_lock_ctx_t ctxprpd;
|
|
||||||
struct proc_mapping* mapping = malloc (sizeof (*mapping));
|
|
||||||
|
|
||||||
if (mapping == NULL)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
mapping->paddr = start_paddr;
|
|
||||||
mapping->vaddr = start_vaddr;
|
|
||||||
mapping->size = pages * PAGE_SIZE;
|
|
||||||
|
|
||||||
flags &= ~(MM_PD_LOCK | MM_PD_RELOAD); /* clear LOCK flag if present, because we lock manualy */
|
|
||||||
|
|
||||||
spin_lock (&proc->procgroup->pd.lock, &ctxprpd);
|
|
||||||
|
|
||||||
list_append (proc->procgroup->mappings, &mapping->proc_mappings_link);
|
|
||||||
|
|
||||||
for (uintptr_t vpage = start_vaddr, ppage = start_paddr; vpage < start_vaddr + pages * PAGE_SIZE;
|
|
||||||
vpage += PAGE_SIZE, ppage += PAGE_SIZE) {
|
|
||||||
mm_map_page (&proc->procgroup->pd, ppage, vpage, flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_unlock (&proc->procgroup->pd.lock, &ctxprpd);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages) {
|
|
||||||
size_t unmap_size = pages * PAGE_SIZE;
|
|
||||||
uintptr_t end_vaddr = start_vaddr + unmap_size;
|
|
||||||
struct list_node_link *mapping_link, *mapping_link_tmp;
|
|
||||||
bool used_tail_mapping = false;
|
|
||||||
spin_lock_ctx_t ctxprpd;
|
|
||||||
|
|
||||||
struct proc_mapping* tail_mapping = malloc (sizeof (*tail_mapping));
|
|
||||||
if (tail_mapping == NULL)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
spin_lock (&proc->procgroup->pd.lock, &ctxprpd);
|
|
||||||
|
|
||||||
list_foreach (proc->procgroup->mappings, mapping_link, mapping_link_tmp) {
|
|
||||||
struct proc_mapping* mapping =
|
|
||||||
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
|
|
||||||
|
|
||||||
uintptr_t m_end = mapping->vaddr + mapping->size;
|
|
||||||
|
|
||||||
/* check overlap */
|
|
||||||
if ((start_vaddr < m_end) && (end_vaddr > mapping->vaddr)) {
|
|
||||||
/* split in the middle */
|
|
||||||
if ((start_vaddr > mapping->vaddr) && (end_vaddr < m_end)) {
|
|
||||||
tail_mapping->vaddr = end_vaddr;
|
|
||||||
tail_mapping->paddr = mapping->paddr + (end_vaddr - mapping->vaddr);
|
|
||||||
tail_mapping->size = m_end - end_vaddr;
|
|
||||||
|
|
||||||
mapping->size = start_vaddr - mapping->vaddr;
|
|
||||||
|
|
||||||
list_insert_after (proc->procgroup->mappings, &mapping->proc_mappings_link,
|
|
||||||
&tail_mapping->proc_mappings_link);
|
|
||||||
|
|
||||||
used_tail_mapping = true;
|
|
||||||
|
|
||||||
break;
|
|
||||||
} else if ((start_vaddr <= mapping->vaddr) && (end_vaddr < m_end)) { /* shrink left */
|
|
||||||
size_t diff = end_vaddr - mapping->vaddr;
|
|
||||||
mapping->vaddr += diff;
|
|
||||||
mapping->paddr += diff;
|
|
||||||
mapping->size -= diff;
|
|
||||||
} else if ((start_vaddr > mapping->vaddr) && (end_vaddr >= m_end)) { /* shrink right */
|
|
||||||
mapping->size = start_vaddr - mapping->vaddr;
|
|
||||||
} else { /* full overlap */
|
|
||||||
list_remove (proc->procgroup->mappings, &mapping->proc_mappings_link);
|
|
||||||
free (mapping);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!used_tail_mapping)
|
|
||||||
free (tail_mapping);
|
|
||||||
|
|
||||||
for (uintptr_t vpage = start_vaddr; vpage < end_vaddr; vpage += PAGE_SIZE) {
|
|
||||||
mm_unmap_page (&proc->procgroup->pd, vpage, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_unlock (&proc->procgroup->pd.lock, &ctxprpd);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
||||||
struct elf_aux aux;
|
struct elf_aux aux;
|
||||||
|
|
||||||
@@ -156,23 +63,16 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
|||||||
|
|
||||||
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
|
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
|
||||||
|
|
||||||
int rid = procgroup_get_sys_rid (proc->procgroup);
|
|
||||||
struct proc_resource* r = proc_create_resource_mem (proc->procgroup, rid, blks, 0, false);
|
|
||||||
if (r == NULL) {
|
|
||||||
DEBUG ("pmm oom error while loading ELF segments! (tried to alloc %zu blks)\n", blks);
|
|
||||||
}
|
|
||||||
|
|
||||||
uintptr_t p_addr = r->u.mem.paddr;
|
|
||||||
|
|
||||||
memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
|
|
||||||
memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
|
|
||||||
(void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
|
|
||||||
|
|
||||||
uint32_t pg_flags = MM_PG_USER | MM_PG_PRESENT;
|
uint32_t pg_flags = MM_PG_USER | MM_PG_PRESENT;
|
||||||
if (phdr->p_flags & PF_W)
|
if (phdr->p_flags & PF_W)
|
||||||
pg_flags |= MM_PG_RW;
|
pg_flags |= MM_PG_RW;
|
||||||
|
|
||||||
proc_map (proc, p_addr, v_addr, blks, pg_flags);
|
uintptr_t p_addr;
|
||||||
|
procgroup_map (proc->procgroup, v_addr, blks, pg_flags, &p_addr);
|
||||||
|
|
||||||
|
memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
|
||||||
|
memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
|
||||||
|
(void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -347,59 +247,12 @@ static void proc_irq_sched (void* arg, void* regs) {
|
|||||||
proc_sched ();
|
proc_sched ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void proc_kpproc_init (void) {
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
||||||
|
|
||||||
memset (&kpproc, 0, sizeof (kpproc));
|
|
||||||
|
|
||||||
kpproc.lock = SPIN_LOCK_INIT;
|
|
||||||
kpproc.state = PROC_PSEUDO;
|
|
||||||
kpproc.pid = 0;
|
|
||||||
|
|
||||||
kpproc.cpu = thiscpu;
|
|
||||||
kpproc.procgroup = procgroup_create ();
|
|
||||||
procgroup_attach (kpproc.procgroup, &kpproc);
|
|
||||||
|
|
||||||
rbtree_insert (struct proc, &proc_tree, &kpproc.proc_tree_link, proc_tree_link, pid);
|
|
||||||
|
|
||||||
/* prepare kernel resources */
|
|
||||||
{
|
|
||||||
/* frame buffer */
|
|
||||||
|
|
||||||
struct limine_framebuffer_response* fb = limine_framebuffer_request.response;
|
|
||||||
struct kpproc_fb fb_info = {
|
|
||||||
.paddr = (uintptr_t)fb->framebuffers[0]->address - (uintptr_t)hhdm->offset,
|
|
||||||
.w = fb->framebuffers[0]->width,
|
|
||||||
.h = fb->framebuffers[0]->height,
|
|
||||||
.pitch = fb->framebuffers[0]->pitch,
|
|
||||||
.bpp = fb->framebuffers[0]->bpp,
|
|
||||||
.red_mask_size = fb->framebuffers[0]->red_mask_size,
|
|
||||||
.red_mask_shift = fb->framebuffers[0]->red_mask_shift,
|
|
||||||
.green_mask_size = fb->framebuffers[0]->green_mask_size,
|
|
||||||
.green_mask_shift = fb->framebuffers[0]->green_mask_shift,
|
|
||||||
.blue_mask_size = fb->framebuffers[0]->blue_mask_size,
|
|
||||||
.blue_mask_shift = fb->framebuffers[0]->blue_mask_shift,
|
|
||||||
};
|
|
||||||
|
|
||||||
DEBUG ("Framebuffer address %p\n", fb_info.paddr);
|
|
||||||
|
|
||||||
size_t pages = align_up (sizeof (fb_info), PAGE_SIZE) / PAGE_SIZE;
|
|
||||||
uintptr_t fb_info_memblk_paddr = pmm_alloc (pages);
|
|
||||||
memcpy ((struct kpproc_fb*)((uintptr_t)hhdm->offset + fb_info_memblk_paddr), &fb_info,
|
|
||||||
sizeof (fb_info));
|
|
||||||
|
|
||||||
proc_create_resource_mem (kpproc.procgroup, 0, pages, fb_info_memblk_paddr, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void proc_init (void) {
|
void proc_init (void) {
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER);
|
irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER);
|
||||||
irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
|
irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
proc_kpproc_init ();
|
|
||||||
|
|
||||||
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
|
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
|
||||||
proc_register (spin_proc, thiscpu);
|
proc_register (spin_proc, thiscpu);
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
#define PROC_READY 0
|
#define PROC_READY 0
|
||||||
#define PROC_DEAD 1
|
#define PROC_DEAD 1
|
||||||
#define PROC_SUSPENDED 2
|
#define PROC_SUSPENDED 2
|
||||||
#define PROC_PSEUDO 3
|
|
||||||
|
|
||||||
/* process flags */
|
/* process flags */
|
||||||
#define PROC_USTK_PREALLOC (1 << 0)
|
#define PROC_USTK_PREALLOC (1 << 0)
|
||||||
@@ -53,9 +52,6 @@ struct proc {
|
|||||||
|
|
||||||
void proc_sched (void);
|
void proc_sched (void);
|
||||||
void proc_kill (struct proc* proc);
|
void proc_kill (struct proc* proc);
|
||||||
bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
|
|
||||||
uint32_t flags);
|
|
||||||
bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages);
|
|
||||||
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
|
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
|
||||||
void proc_register (struct proc* proc, struct cpu* cpu);
|
void proc_register (struct proc* proc, struct cpu* cpu);
|
||||||
struct proc* proc_find_pid (int pid);
|
struct proc* proc_find_pid (int pid);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <libk/rbtree.h>
|
#include <libk/rbtree.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
|
#include <mm/pmm.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
#include <proc/procgroup.h>
|
#include <proc/procgroup.h>
|
||||||
#include <sync/rw_spin_lock.h>
|
#include <sync/rw_spin_lock.h>
|
||||||
@@ -12,6 +13,116 @@ static struct rb_node_link* procgroup_tree = NULL;
|
|||||||
static rw_spin_lock_t procgroup_tree_lock = RW_SPIN_LOCK_INIT;
|
static rw_spin_lock_t procgroup_tree_lock = RW_SPIN_LOCK_INIT;
|
||||||
static atomic_int pgids = 0;
|
static atomic_int pgids = 0;
|
||||||
|
|
||||||
|
uintptr_t procgroup_map (struct procgroup* procgroup, uintptr_t vaddr, size_t pages, uint32_t flags,
|
||||||
|
uintptr_t* out_paddr) {
|
||||||
|
spin_lock_ctx_t ctxprpd;
|
||||||
|
|
||||||
|
vaddr = (vaddr == 0) ? PROC_MAP_BASE : vaddr;
|
||||||
|
|
||||||
|
struct proc_mapping* mapping = malloc (sizeof (*mapping));
|
||||||
|
|
||||||
|
if (mapping == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
uintptr_t paddr = pmm_alloc (pages);
|
||||||
|
|
||||||
|
if (paddr == PMM_ALLOC_ERR) {
|
||||||
|
free (mapping);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out_paddr != NULL)
|
||||||
|
*out_paddr = paddr;
|
||||||
|
|
||||||
|
mapping->paddr = paddr;
|
||||||
|
mapping->vaddr = vaddr;
|
||||||
|
mapping->size = pages * PAGE_SIZE;
|
||||||
|
|
||||||
|
flags &= ~(MM_PD_LOCK | MM_PD_RELOAD); /* clear LOCK flag if present, because we lock manualy */
|
||||||
|
|
||||||
|
spin_lock (&procgroup->pd.lock, &ctxprpd);
|
||||||
|
|
||||||
|
list_append (procgroup->mappings, &mapping->proc_mappings_link);
|
||||||
|
|
||||||
|
for (uintptr_t vpage = vaddr, ppage = paddr; vpage < vaddr + pages * PAGE_SIZE;
|
||||||
|
vpage += PAGE_SIZE, ppage += PAGE_SIZE) {
|
||||||
|
mm_map_page (&procgroup->pd, ppage, vpage, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&procgroup->pd.lock, &ctxprpd);
|
||||||
|
|
||||||
|
return vaddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool procgroup_unmap (struct procgroup* procgroup, uintptr_t start_vaddr, size_t pages) {
|
||||||
|
size_t unmap_size = pages * PAGE_SIZE;
|
||||||
|
uintptr_t end_vaddr = start_vaddr + unmap_size;
|
||||||
|
struct list_node_link *mapping_link, *mapping_link_tmp;
|
||||||
|
bool used_tail_mapping = false;
|
||||||
|
spin_lock_ctx_t ctxprpd;
|
||||||
|
|
||||||
|
struct proc_mapping* tail_mapping = malloc (sizeof (*tail_mapping));
|
||||||
|
if (tail_mapping == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->pd.lock, &ctxprpd);
|
||||||
|
|
||||||
|
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
|
||||||
|
struct proc_mapping* mapping =
|
||||||
|
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
|
||||||
|
|
||||||
|
uintptr_t m_start = mapping->vaddr;
|
||||||
|
uintptr_t m_end = mapping->vaddr + mapping->size;
|
||||||
|
|
||||||
|
/* check overlap */
|
||||||
|
if ((start_vaddr < m_end) && (end_vaddr > mapping->vaddr)) {
|
||||||
|
uintptr_t free_vstart = (start_vaddr > m_start) ? start_vaddr : m_start;
|
||||||
|
uintptr_t free_vend = (end_vaddr < m_end) ? end_vaddr : m_end;
|
||||||
|
size_t free_size = free_vend - free_vstart;
|
||||||
|
|
||||||
|
uintptr_t ppage_to_free = mapping->paddr + (free_vstart - m_start);
|
||||||
|
pmm_free (ppage_to_free, free_size / PAGE_SIZE);
|
||||||
|
|
||||||
|
/* split in the middle */
|
||||||
|
if ((start_vaddr > m_start) && (end_vaddr < m_end)) {
|
||||||
|
tail_mapping->vaddr = end_vaddr;
|
||||||
|
tail_mapping->paddr = mapping->paddr + (end_vaddr - m_start);
|
||||||
|
tail_mapping->size = m_end - end_vaddr;
|
||||||
|
|
||||||
|
mapping->size = start_vaddr - m_start;
|
||||||
|
|
||||||
|
list_insert_after (procgroup->mappings, &mapping->proc_mappings_link,
|
||||||
|
&tail_mapping->proc_mappings_link);
|
||||||
|
|
||||||
|
used_tail_mapping = true;
|
||||||
|
|
||||||
|
break;
|
||||||
|
} else if ((start_vaddr <= m_start) && (end_vaddr < m_end)) { /* shrink left */
|
||||||
|
size_t diff = end_vaddr - m_start;
|
||||||
|
mapping->vaddr += diff;
|
||||||
|
mapping->paddr += diff;
|
||||||
|
mapping->size -= diff;
|
||||||
|
} else if ((start_vaddr > m_start) && (end_vaddr >= m_end)) { /* shrink right */
|
||||||
|
mapping->size = start_vaddr - m_start;
|
||||||
|
} else { /* full overlap */
|
||||||
|
list_remove (procgroup->mappings, &mapping->proc_mappings_link);
|
||||||
|
free (mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!used_tail_mapping)
|
||||||
|
free (tail_mapping);
|
||||||
|
|
||||||
|
for (uintptr_t vpage = start_vaddr; vpage < end_vaddr; vpage += PAGE_SIZE) {
|
||||||
|
mm_unmap_page (&procgroup->pd, vpage, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&procgroup->pd.lock, &ctxprpd);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
struct procgroup* procgroup_create (void) {
|
struct procgroup* procgroup_create (void) {
|
||||||
spin_lock_ctx_t ctxpgtr;
|
spin_lock_ctx_t ctxpgtr;
|
||||||
|
|
||||||
@@ -87,6 +198,17 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc) {
|
|||||||
proc_resource_unlink (resource);
|
proc_resource_unlink (resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct list_node_link *mapping_link, *mapping_link_tmp;
|
||||||
|
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
|
||||||
|
struct proc_mapping* mapping =
|
||||||
|
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
|
||||||
|
|
||||||
|
pmm_free (mapping->paddr, mapping->size / PAGE_SIZE);
|
||||||
|
free (mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
pmm_free (procgroup->pd.cr3_paddr, 1);
|
||||||
|
|
||||||
free (procgroup);
|
free (procgroup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,5 +34,8 @@ struct procgroup* procgroup_create (void);
|
|||||||
void procgroup_attach (struct procgroup* procgroup, struct proc* proc);
|
void procgroup_attach (struct procgroup* procgroup, struct proc* proc);
|
||||||
void procgroup_detach (struct procgroup* procgroup, struct proc* proc);
|
void procgroup_detach (struct procgroup* procgroup, struct proc* proc);
|
||||||
int procgroup_get_sys_rid (struct procgroup* procgroup);
|
int procgroup_get_sys_rid (struct procgroup* procgroup);
|
||||||
|
uintptr_t procgroup_map (struct procgroup* procgroup, uintptr_t vaddr, size_t pages, uint32_t flags,
|
||||||
|
uintptr_t* out_paddr);
|
||||||
|
bool procgroup_unmap (struct procgroup* procgroup, uintptr_t start_vaddr, size_t pages);
|
||||||
|
|
||||||
#endif // _KERNEL_PROC_PROCGROUP_H
|
#endif // _KERNEL_PROC_PROCGROUP_H
|
||||||
|
|||||||
@@ -24,50 +24,6 @@ struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid)
|
|||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct proc_resource* proc_create_resource_mem (struct procgroup* procgroup, int rid, size_t pages,
|
|
||||||
uintptr_t paddr, bool managed) {
|
|
||||||
spin_lock_ctx_t ctxpg;
|
|
||||||
struct proc_resource* resource;
|
|
||||||
|
|
||||||
if (pages == 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
resource = proc_find_resource (procgroup, rid);
|
|
||||||
if (resource != NULL)
|
|
||||||
return resource;
|
|
||||||
|
|
||||||
resource = malloc (sizeof (*resource));
|
|
||||||
if (resource == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
memset (resource, 0, sizeof (*resource));
|
|
||||||
resource->lock = SPIN_LOCK_INIT;
|
|
||||||
resource->ops.cleanup = &proc_cleanup_resource_mem;
|
|
||||||
resource->rid = rid;
|
|
||||||
resource->type = PR_MEM;
|
|
||||||
resource->u.mem.resource = resource;
|
|
||||||
if (managed) {
|
|
||||||
resource->u.mem.managed = true;
|
|
||||||
} else {
|
|
||||||
paddr = pmm_alloc (pages);
|
|
||||||
if (paddr == PMM_ALLOC_ERR) {
|
|
||||||
free (resource);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
resource->u.mem.managed = false;
|
|
||||||
}
|
|
||||||
resource->u.mem.paddr = paddr;
|
|
||||||
resource->u.mem.pages = resource->u.mem.alive_pages = pages;
|
|
||||||
resource->refs = 1;
|
|
||||||
|
|
||||||
spin_lock (&procgroup->lock, &ctxpg);
|
|
||||||
rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
|
|
||||||
resource_tree_link, rid);
|
|
||||||
spin_unlock (&procgroup->lock, &ctxpg);
|
|
||||||
|
|
||||||
return resource;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, int rid) {
|
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, int rid) {
|
||||||
spin_lock_ctx_t ctxpg;
|
spin_lock_ctx_t ctxpg;
|
||||||
struct proc_resource* resource;
|
struct proc_resource* resource;
|
||||||
@@ -83,7 +39,7 @@ struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, i
|
|||||||
memset (resource, 0, sizeof (*resource));
|
memset (resource, 0, sizeof (*resource));
|
||||||
resource->lock = SPIN_LOCK_INIT;
|
resource->lock = SPIN_LOCK_INIT;
|
||||||
resource->ops.cleanup = &proc_cleanup_resource_mutex;
|
resource->ops.cleanup = &proc_cleanup_resource_mutex;
|
||||||
resource->u.mem.resource = resource;
|
resource->u.mutex.resource = resource;
|
||||||
resource->rid = rid;
|
resource->rid = rid;
|
||||||
resource->type = PR_MUTEX;
|
resource->type = PR_MUTEX;
|
||||||
resource->refs = 1;
|
resource->refs = 1;
|
||||||
|
|||||||
@@ -4,11 +4,9 @@
|
|||||||
#include <libk/list.h>
|
#include <libk/list.h>
|
||||||
#include <libk/rbtree.h>
|
#include <libk/rbtree.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <proc/mem.h>
|
|
||||||
#include <proc/mutex.h>
|
#include <proc/mutex.h>
|
||||||
#include <sync/spin_lock.h>
|
#include <sync/spin_lock.h>
|
||||||
|
|
||||||
#define PR_MEM 0
|
|
||||||
#define PR_MUTEX 1
|
#define PR_MUTEX 1
|
||||||
|
|
||||||
struct proc;
|
struct proc;
|
||||||
@@ -21,7 +19,6 @@ struct proc_resource {
|
|||||||
spin_lock_t lock;
|
spin_lock_t lock;
|
||||||
struct rb_node_link resource_tree_link;
|
struct rb_node_link resource_tree_link;
|
||||||
union {
|
union {
|
||||||
struct proc_resource_mem mem;
|
|
||||||
struct proc_mutex mutex;
|
struct proc_mutex mutex;
|
||||||
} u;
|
} u;
|
||||||
struct {
|
struct {
|
||||||
@@ -30,8 +27,6 @@ struct proc_resource {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid);
|
struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid);
|
||||||
struct proc_resource* proc_create_resource_mem (struct procgroup* procgroup, int rid, size_t pages,
|
|
||||||
uintptr_t paddr, bool managed);
|
|
||||||
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, int rid);
|
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, int rid);
|
||||||
void proc_resource_unlink (struct proc_resource* resource);
|
void proc_resource_unlink (struct proc_resource* resource);
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
c += proc/proc.c \
|
c += proc/proc.c \
|
||||||
proc/resource.c \
|
proc/resource.c \
|
||||||
proc/mutex.c \
|
proc/mutex.c \
|
||||||
proc/mem.c \
|
|
||||||
proc/procgroup.c
|
proc/procgroup.c
|
||||||
|
|
||||||
o += proc/proc.o \
|
o += proc/proc.o \
|
||||||
proc/resource.o \
|
proc/resource.o \
|
||||||
proc/mutex.o \
|
proc/mutex.o \
|
||||||
proc/mem.o \
|
|
||||||
proc/procgroup.o
|
proc/procgroup.o
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include <m/status.h>
|
#include <m/status.h>
|
||||||
#include <m/syscall_defs.h>
|
#include <m/syscall_defs.h>
|
||||||
#include <mm/pmm.h>
|
#include <mm/pmm.h>
|
||||||
#include <proc/mem.h>
|
|
||||||
#include <proc/mutex.h>
|
#include <proc/mutex.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
#include <proc/procgroup.h>
|
#include <proc/procgroup.h>
|
||||||
@@ -17,8 +16,11 @@
|
|||||||
#include <syscall/syscall.h>
|
#include <syscall/syscall.h>
|
||||||
|
|
||||||
#define DEFINE_SYSCALL(name) \
|
#define DEFINE_SYSCALL(name) \
|
||||||
int name (struct proc* UNUSED proc, void* UNUSED regs, uintptr_t UNUSED a1, uintptr_t UNUSED a2, \
|
uintptr_t name (struct proc* UNUSED proc, void* UNUSED regs, uintptr_t UNUSED a1, \
|
||||||
uintptr_t UNUSED a3, uintptr_t UNUSED a4, uintptr_t UNUSED a5, uintptr_t UNUSED a6)
|
uintptr_t UNUSED a2, uintptr_t UNUSED a3, uintptr_t UNUSED a4, \
|
||||||
|
uintptr_t UNUSED a5, uintptr_t UNUSED a6)
|
||||||
|
|
||||||
|
#define SYSRESULT(x) ((uintptr_t)(x))
|
||||||
|
|
||||||
static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t size) {
|
static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t size) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
@@ -43,46 +45,26 @@ static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t si
|
|||||||
/* int quit (void) */
|
/* int quit (void) */
|
||||||
DEFINE_SYSCALL (sys_quit) {
|
DEFINE_SYSCALL (sys_quit) {
|
||||||
proc_kill (proc);
|
proc_kill (proc);
|
||||||
return ST_OK;
|
return SYSRESULT (ST_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int test (void) */
|
/* int test (void) */
|
||||||
DEFINE_SYSCALL (sys_test) {
|
DEFINE_SYSCALL (sys_test) {
|
||||||
char c = (char)a1;
|
char c = (char)a1;
|
||||||
DEBUG ("test syscall from %d! %c\n", proc->pid, c);
|
DEBUG ("test syscall from %d! %c\n", proc->pid, c);
|
||||||
return ST_OK;
|
return SYSRESULT (ST_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int map (int mem_rid, uintptr_t vaddr, uint32_t flags) */
|
/* int map (uintptr_t vaddr, size_t pages, uint32_t flags) */
|
||||||
DEFINE_SYSCALL (sys_map) {
|
DEFINE_SYSCALL (sys_map) {
|
||||||
spin_lock_ctx_t ctxrs;
|
uintptr_t vaddr = a1;
|
||||||
|
size_t pages = (size_t)a2;
|
||||||
int mem_rid = (int)a1;
|
|
||||||
uintptr_t vaddr = a2;
|
|
||||||
uint32_t flags = (uint32_t)a3;
|
uint32_t flags = (uint32_t)a3;
|
||||||
|
|
||||||
if (vaddr % PAGE_SIZE != 0)
|
if (vaddr % PAGE_SIZE != 0)
|
||||||
return -ST_UNALIGNED;
|
return SYSRESULT (-ST_UNALIGNED);
|
||||||
|
|
||||||
struct proc_resource* mem_resource = proc_find_resource (proc->procgroup, mem_rid);
|
return SYSRESULT (procgroup_map (proc->procgroup, vaddr, pages, flags, NULL));
|
||||||
|
|
||||||
if (mem_resource == NULL) {
|
|
||||||
return -ST_NOT_FOUND;
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_lock (&mem_resource->lock, &ctxrs);
|
|
||||||
|
|
||||||
if (mem_resource->type != PR_MEM) {
|
|
||||||
spin_unlock (&mem_resource->lock, &ctxrs);
|
|
||||||
return -ST_BAD_RESOURCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
uintptr_t paddr = mem_resource->u.mem.paddr;
|
|
||||||
size_t pages = mem_resource->u.mem.pages;
|
|
||||||
|
|
||||||
spin_unlock (&mem_resource->lock, &ctxrs);
|
|
||||||
|
|
||||||
return proc_map (proc, paddr, vaddr, pages, flags) ? ST_OK : -ST_OOM_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int unmap (uintptr_t vaddr, size_t pages) */
|
/* int unmap (uintptr_t vaddr, size_t pages) */
|
||||||
@@ -91,59 +73,9 @@ DEFINE_SYSCALL (sys_unmap) {
|
|||||||
size_t pages = (size_t)a2;
|
size_t pages = (size_t)a2;
|
||||||
|
|
||||||
if (vaddr % PAGE_SIZE != 0)
|
if (vaddr % PAGE_SIZE != 0)
|
||||||
return -ST_UNALIGNED;
|
return SYSRESULT (-ST_UNALIGNED);
|
||||||
|
|
||||||
return proc_unmap (proc, vaddr, pages) ? ST_OK : -ST_OOM_ERROR;
|
return SYSRESULT (procgroup_unmap (proc->procgroup, vaddr, pages));
|
||||||
}
|
|
||||||
|
|
||||||
/* int create_mem (int rid, size_t pages) */
|
|
||||||
DEFINE_SYSCALL (sys_create_mem) {
|
|
||||||
int rid = (int)a1;
|
|
||||||
size_t pages = (size_t)a2;
|
|
||||||
|
|
||||||
if (pages == 0)
|
|
||||||
return ST_OK;
|
|
||||||
|
|
||||||
/* int rid1 = rid < 0 ? atomic_fetch_add (&proc->resources->sys_rids, 1) : rid; */
|
|
||||||
int rid1 = rid < 0 ? procgroup_get_sys_rid (proc->procgroup) : rid;
|
|
||||||
struct proc_resource* mem_resource =
|
|
||||||
proc_create_resource_mem (proc->procgroup, rid1, pages, 0, false);
|
|
||||||
|
|
||||||
if (mem_resource == NULL)
|
|
||||||
return -ST_OOM_ERROR;
|
|
||||||
|
|
||||||
return mem_resource->rid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* int unlink_mem (int rid, size_t pages) */
|
|
||||||
DEFINE_SYSCALL (sys_unlink_mem) {
|
|
||||||
/* spin_lock_ctx_t ctxrs; */
|
|
||||||
|
|
||||||
/* int rid = (int)a1; */
|
|
||||||
/* size_t pages = (size_t)a2; */
|
|
||||||
|
|
||||||
/* if (!(vis == RV_PUBLIC || vis == RV_PRIVATE)) */
|
|
||||||
/* return -ST_BAD_RESOURCE; */
|
|
||||||
|
|
||||||
/* struct proc_resource* mem_resource = proc_find_resource (proc, rid, vis); */
|
|
||||||
|
|
||||||
/* if (mem_resource == NULL) */
|
|
||||||
/* return -ST_NOT_FOUND; */
|
|
||||||
|
|
||||||
/* spin_lock (&mem_resource->lock, &ctxrs); */
|
|
||||||
|
|
||||||
/* if (mem_resource->type != PR_MEM) { */
|
|
||||||
/* spin_unlock (&mem_resource->lock, &ctxrs); */
|
|
||||||
/* return -ST_BAD_RESOURCE; */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
/* mem_resource->u.mem.alive_pages -= pages; */
|
|
||||||
/* if (mem_resource->u.mem.alive_pages < 0) { */
|
|
||||||
/* spin_unlock (&mem_resource->lock, &ctxrs); */
|
|
||||||
/* proc_drop_resource (proc, mem_resource, true); */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
return ST_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int clone (uintptr_t vstack_top, size_t stack_size, void* entry) */
|
/* int clone (uintptr_t vstack_top, size_t stack_size, void* entry) */
|
||||||
@@ -157,20 +89,20 @@ DEFINE_SYSCALL (sys_clone) {
|
|||||||
DEBUG ("new=%p\n", new);
|
DEBUG ("new=%p\n", new);
|
||||||
|
|
||||||
if (new == NULL) {
|
if (new == NULL) {
|
||||||
return -ST_OOM_ERROR;
|
return SYSRESULT (-ST_OOM_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
int pid = new->pid;
|
int pid = new->pid;
|
||||||
|
|
||||||
proc_register (new, NULL);
|
proc_register (new, NULL);
|
||||||
|
|
||||||
return pid;
|
return SYSRESULT (pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int sched (void) */
|
/* int sched (void) */
|
||||||
DEFINE_SYSCALL (sys_sched) {
|
DEFINE_SYSCALL (sys_sched) {
|
||||||
proc_sched ();
|
proc_sched ();
|
||||||
return ST_OK;
|
return SYSRESULT (ST_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int create_mutex (int mutex_rid) */
|
/* int create_mutex (int mutex_rid) */
|
||||||
@@ -180,9 +112,9 @@ DEFINE_SYSCALL (sys_create_mutex) {
|
|||||||
struct proc_resource* mutex_resource = proc_create_resource_mutex (proc->procgroup, mutex_rid);
|
struct proc_resource* mutex_resource = proc_create_resource_mutex (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
if (mutex_resource == NULL)
|
if (mutex_resource == NULL)
|
||||||
return -ST_OOM_ERROR;
|
return SYSRESULT (-ST_OOM_ERROR);
|
||||||
|
|
||||||
return mutex_resource->rid;
|
return SYSRESULT (mutex_resource->rid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int unlink_mutex (int mutex_rid) */
|
/* int unlink_mutex (int mutex_rid) */
|
||||||
@@ -192,11 +124,9 @@ DEFINE_SYSCALL (sys_unlink_mutex) {
|
|||||||
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
if (mutex_resource == NULL)
|
if (mutex_resource == NULL)
|
||||||
return -ST_NOT_FOUND;
|
return SYSRESULT (-ST_NOT_FOUND);
|
||||||
|
|
||||||
/* proc_drop_resource (proc, mutex_resource, true); */
|
return SYSRESULT (ST_OK);
|
||||||
|
|
||||||
return ST_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int lock_mutex (int mutex_rid) */
|
/* int lock_mutex (int mutex_rid) */
|
||||||
@@ -206,11 +136,11 @@ DEFINE_SYSCALL (sys_lock_mutex) {
|
|||||||
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
if (mutex_resource == NULL)
|
if (mutex_resource == NULL)
|
||||||
return -ST_NOT_FOUND;
|
return SYSRESULT (-ST_NOT_FOUND);
|
||||||
|
|
||||||
proc_mutex_lock (proc, &mutex_resource->u.mutex);
|
proc_mutex_lock (proc, &mutex_resource->u.mutex);
|
||||||
|
|
||||||
return ST_OK;
|
return SYSRESULT (ST_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int unlock_mutex (int mutex_rid) */
|
/* int unlock_mutex (int mutex_rid) */
|
||||||
@@ -220,9 +150,10 @@ DEFINE_SYSCALL (sys_unlock_mutex) {
|
|||||||
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
if (mutex_resource == NULL)
|
if (mutex_resource == NULL)
|
||||||
return -ST_NOT_FOUND;
|
return SYSRESULT (-ST_NOT_FOUND);
|
||||||
|
|
||||||
return proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK : -ST_PERMISSION_ERROR;
|
return SYSRESULT (proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK
|
||||||
|
: -ST_PERMISSION_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
static syscall_handler_func_t handler_table[] = {
|
static syscall_handler_func_t handler_table[] = {
|
||||||
@@ -232,8 +163,6 @@ static syscall_handler_func_t handler_table[] = {
|
|||||||
[SYS_UNMAP] = &sys_unmap,
|
[SYS_UNMAP] = &sys_unmap,
|
||||||
[SYS_CLONE] = &sys_clone,
|
[SYS_CLONE] = &sys_clone,
|
||||||
[SYS_SCHED] = &sys_sched,
|
[SYS_SCHED] = &sys_sched,
|
||||||
[SYS_CREATE_MEM] = &sys_create_mem,
|
|
||||||
[SYS_UNLINK_MEM] = &sys_unlink_mem,
|
|
||||||
[SYS_CREATE_MUTEX] = &sys_create_mutex,
|
[SYS_CREATE_MUTEX] = &sys_create_mutex,
|
||||||
[SYS_UNLINK_MUTEX] = &sys_unlink_mutex,
|
[SYS_UNLINK_MUTEX] = &sys_unlink_mutex,
|
||||||
[SYS_LOCK_MUTEX] = &sys_lock_mutex,
|
[SYS_LOCK_MUTEX] = &sys_lock_mutex,
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
|
||||||
typedef int (*syscall_handler_func_t) (struct proc* proc, void* regs, uintptr_t a1, uintptr_t a2,
|
typedef uintptr_t (*syscall_handler_func_t) (struct proc* proc, void* regs, uintptr_t a1,
|
||||||
uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6);
|
uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5,
|
||||||
|
uintptr_t a6);
|
||||||
|
|
||||||
syscall_handler_func_t syscall_find_handler (int syscall_num);
|
syscall_handler_func_t syscall_find_handler (int syscall_num);
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@
|
|||||||
|
|
||||||
#define LIBALLOC_MUTEX 500
|
#define LIBALLOC_MUTEX 500
|
||||||
|
|
||||||
static uintptr_t liballoc_map_base = PROC_MAP_BASE;
|
|
||||||
static int mem_rid_base = 1000000;
|
|
||||||
static int liballoc_mutex;
|
static int liballoc_mutex;
|
||||||
|
|
||||||
void liballoc_init (void) { liballoc_mutex = create_mutex (LIBALLOC_MUTEX); }
|
void liballoc_init (void) { liballoc_mutex = create_mutex (LIBALLOC_MUTEX); }
|
||||||
@@ -18,30 +16,9 @@ int liballoc_lock (void) { return lock_mutex (liballoc_mutex); }
|
|||||||
|
|
||||||
int liballoc_unlock (void) { return unlock_mutex (liballoc_mutex); }
|
int liballoc_unlock (void) { return unlock_mutex (liballoc_mutex); }
|
||||||
|
|
||||||
void* liballoc_alloc (int pages, int* mem_rid) {
|
void* liballoc_alloc (int pages) { return map (0, pages, MAP_FLAGS | MAP_RW); }
|
||||||
uintptr_t current_base = liballoc_map_base;
|
|
||||||
|
|
||||||
*mem_rid = create_mem (mem_rid_base++, pages);
|
int liballoc_free (void* ptr, int pages) { return unmap ((uintptr_t)ptr, pages); }
|
||||||
if (*mem_rid < 0) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (map (*mem_rid, current_base, MAP_FLAGS | MAP_RW) < 0) {
|
|
||||||
unlink_mem (*mem_rid, pages);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uintptr_t old_base = current_base;
|
|
||||||
current_base += pages * PAGE_SIZE;
|
|
||||||
|
|
||||||
return (void*)old_base;
|
|
||||||
}
|
|
||||||
|
|
||||||
int liballoc_free (void* ptr, int pages, int mem_rid) {
|
|
||||||
unmap ((uintptr_t)ptr, pages);
|
|
||||||
unlink_mem (mem_rid, pages);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Durand's Ridiculously Amazing Super Duper Memory functions. */
|
/** Durand's Ridiculously Amazing Super Duper Memory functions. */
|
||||||
|
|
||||||
@@ -208,7 +185,6 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
|
|||||||
unsigned int pages;
|
unsigned int pages;
|
||||||
unsigned int usage;
|
unsigned int usage;
|
||||||
struct boundary_tag* tag;
|
struct boundary_tag* tag;
|
||||||
int mem_rid;
|
|
||||||
|
|
||||||
// This is how much space is required.
|
// This is how much space is required.
|
||||||
usage = size + sizeof (struct boundary_tag);
|
usage = size + sizeof (struct boundary_tag);
|
||||||
@@ -222,7 +198,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
|
|||||||
if (pages < (unsigned int)l_pageCount)
|
if (pages < (unsigned int)l_pageCount)
|
||||||
pages = l_pageCount;
|
pages = l_pageCount;
|
||||||
|
|
||||||
tag = (struct boundary_tag*)liballoc_alloc (pages, &mem_rid);
|
tag = (struct boundary_tag*)liballoc_alloc (pages);
|
||||||
|
|
||||||
if (tag == NULL)
|
if (tag == NULL)
|
||||||
return NULL; // uh oh, we ran out of memory.
|
return NULL; // uh oh, we ran out of memory.
|
||||||
@@ -231,7 +207,6 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
|
|||||||
tag->size = size;
|
tag->size = size;
|
||||||
tag->real_size = pages * l_pageSize;
|
tag->real_size = pages * l_pageSize;
|
||||||
tag->index = -1;
|
tag->index = -1;
|
||||||
tag->mem_rid = mem_rid;
|
|
||||||
|
|
||||||
tag->next = NULL;
|
tag->next = NULL;
|
||||||
tag->prev = NULL;
|
tag->prev = NULL;
|
||||||
@@ -354,7 +329,7 @@ void free (void* ptr) {
|
|||||||
if (pages < (unsigned int)l_pageCount)
|
if (pages < (unsigned int)l_pageCount)
|
||||||
pages = l_pageCount;
|
pages = l_pageCount;
|
||||||
|
|
||||||
liballoc_free (tag, pages, tag->mem_rid);
|
liballoc_free (tag, pages);
|
||||||
|
|
||||||
liballoc_unlock ();
|
liballoc_unlock ();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -41,8 +41,6 @@ struct boundary_tag {
|
|||||||
|
|
||||||
struct boundary_tag* next; //< Linked list info.
|
struct boundary_tag* next; //< Linked list info.
|
||||||
struct boundary_tag* prev; //< Linked list info.
|
struct boundary_tag* prev; //< Linked list info.
|
||||||
|
|
||||||
int mem_rid;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** This function is supposed to lock the memory data structures. It
|
/** This function is supposed to lock the memory data structures. It
|
||||||
@@ -69,7 +67,7 @@ extern int liballoc_unlock (void);
|
|||||||
* \return NULL if the pages were not allocated.
|
* \return NULL if the pages were not allocated.
|
||||||
* \return A pointer to the allocated memory.
|
* \return A pointer to the allocated memory.
|
||||||
*/
|
*/
|
||||||
extern void* liballoc_alloc (int pages, int* mem_rid);
|
extern void* liballoc_alloc (int pages);
|
||||||
|
|
||||||
/** This frees previously allocated memory. The void* parameter passed
|
/** This frees previously allocated memory. The void* parameter passed
|
||||||
* to the function is the exact same value returned from a previous
|
* to the function is the exact same value returned from a previous
|
||||||
@@ -79,7 +77,7 @@ extern void* liballoc_alloc (int pages, int* mem_rid);
|
|||||||
*
|
*
|
||||||
* \return 0 if the memory was successfully freed.
|
* \return 0 if the memory was successfully freed.
|
||||||
*/
|
*/
|
||||||
extern int liballoc_free (void* ptr, int pages, int mem_rid);
|
extern int liballoc_free (void* ptr, int pages);
|
||||||
|
|
||||||
void* malloc (size_t); //< The standard function.
|
void* malloc (size_t); //< The standard function.
|
||||||
void* realloc (void*, size_t); //< The standard function.
|
void* realloc (void*, size_t); //< The standard function.
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
uintptr_t amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
||||||
uintptr_t a5, uintptr_t a6) {
|
uintptr_t a5, uintptr_t a6) {
|
||||||
uint64_t result;
|
uint64_t result;
|
||||||
__asm__ volatile ("movq %[a4], %%r10\n"
|
__asm__ volatile ("movq %[a4], %%r10\n"
|
||||||
"movq %[a5], %%r8\n"
|
"movq %[a5], %%r8\n"
|
||||||
@@ -13,5 +13,5 @@ int amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, ui
|
|||||||
: "a"(syscall_num), "D"(a1), "S"(a2),
|
: "a"(syscall_num), "D"(a1), "S"(a2),
|
||||||
"d"(a3), [a4] "r"(a4), [a5] "r"(a5), [a6] "r"(a6)
|
"d"(a3), [a4] "r"(a4), [a5] "r"(a5), [a6] "r"(a6)
|
||||||
: "r10", "r8", "r9", "r11", "rcx", "cc", "memory");
|
: "r10", "r8", "r9", "r11", "rcx", "cc", "memory");
|
||||||
return (int)result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
uintptr_t amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
||||||
uintptr_t a5, uintptr_t a6);
|
uintptr_t a5, uintptr_t a6);
|
||||||
|
|
||||||
#endif // _LIBMSL_AMD64_SYSCALL_H
|
#endif // _LIBMSL_AMD64_SYSCALL_H
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ int test (char c) { return do_syscall (SYS_TEST, c); }
|
|||||||
|
|
||||||
int sched (void) { return do_syscall (SYS_SCHED, 0); }
|
int sched (void) { return do_syscall (SYS_SCHED, 0); }
|
||||||
|
|
||||||
int map (int mem_rid, uintptr_t vaddr, uint32_t flags) {
|
void* map (uintptr_t vaddr, size_t pages, uint32_t flags) {
|
||||||
return do_syscall (SYS_MAP, mem_rid, vaddr, flags);
|
return (void*)do_syscall (SYS_MAP, vaddr, pages, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
int unmap (uintptr_t vaddr, size_t pages) { return do_syscall (SYS_UNMAP, vaddr, pages); }
|
int unmap (uintptr_t vaddr, size_t pages) { return do_syscall (SYS_UNMAP, vaddr, pages); }
|
||||||
|
|||||||
@@ -5,8 +5,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
#define PROC_MAP_BASE 0x0000700000000000
|
#define PAGE_SIZE 4096
|
||||||
#define PAGE_SIZE 4096
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MAP_PRESENT (1 << 0)
|
#define MAP_PRESENT (1 << 0)
|
||||||
@@ -17,10 +16,8 @@
|
|||||||
int quit (void);
|
int quit (void);
|
||||||
int test (char c);
|
int test (char c);
|
||||||
int sched (void);
|
int sched (void);
|
||||||
int map (int mem_rid, uintptr_t vaddr, uint32_t flags);
|
void* map (uintptr_t vaddr, size_t pages, uint32_t flags);
|
||||||
int unmap (uintptr_t vaddr, size_t pages);
|
int unmap (uintptr_t vaddr, size_t pages);
|
||||||
int create_mem (int mem_rid, size_t pages);
|
|
||||||
int unlink_mem (int mem_rid, size_t pages);
|
|
||||||
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void));
|
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void));
|
||||||
int create_mutex (int mutex_rid);
|
int create_mutex (int mutex_rid);
|
||||||
int unlink_mutex (int mutex_rid);
|
int unlink_mutex (int mutex_rid);
|
||||||
|
|||||||
Reference in New Issue
Block a user