slab: add umalloc
This commit is contained in:
parent
a28088aea6
commit
b496366469
8 changed files with 238 additions and 12 deletions
|
|
@ -36,6 +36,7 @@ void panic();
|
|||
void cli();
|
||||
void sti();
|
||||
void _save_context();
|
||||
void reboot();
|
||||
void load_stack_and_jump(uintptr_t rsp, uintptr_t rbp, void *func, void *arg);
|
||||
|
||||
typedef uintptr_t paddr_t;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <kernel/lock.h>
|
||||
#include <kernel/rbtree.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <lib/sem.h>
|
||||
|
||||
|
|
@ -24,6 +25,12 @@
|
|||
#define STDOUT_FILENO 1
|
||||
#define STDERR_FILENO 2
|
||||
|
||||
#define USER_STACK_BASE 0x00007FFFF0000000
|
||||
#define USER_HEAP_BASE 0x0000000100000000 /* 4GB */
|
||||
|
||||
#define PM_BUD 0
|
||||
#define PM_SLB 1
|
||||
|
||||
typedef int64_t pid_t;
|
||||
|
||||
struct proc_mmap_entry {
|
||||
|
|
@ -60,6 +67,22 @@ struct procregs {
|
|||
|
||||
} PACKED;
|
||||
|
||||
struct proc_mapping {
|
||||
uintptr_t vaddr;
|
||||
paddr_t paddr;
|
||||
size_t len;
|
||||
uint64_t attr;
|
||||
uint64_t type;
|
||||
slab_t *slab;
|
||||
struct proc_mapping *next;
|
||||
};
|
||||
|
||||
struct proc_allocation {
|
||||
struct proc_mapping *mappings;
|
||||
struct proc_mapping *last;
|
||||
size_t num_mappings;
|
||||
};
|
||||
|
||||
struct proc {
|
||||
struct procregs regs;
|
||||
uint8_t state;
|
||||
|
|
@ -83,6 +106,10 @@ struct proc {
|
|||
|
||||
uintptr_t stack_start;
|
||||
uintptr_t stack_size;
|
||||
|
||||
uintptr_t heap_start;
|
||||
|
||||
struct rbtree umalloc_tree;
|
||||
};
|
||||
|
||||
struct procregs *proc_current_regs();
|
||||
|
|
|
|||
|
|
@ -12,6 +12,14 @@
|
|||
#define ALLOC_KERN 0x01
|
||||
#define ALLOC_DMA 0x02
|
||||
|
||||
#define UA_SLAB 0x1
|
||||
#define UA_BUD 0x2
|
||||
|
||||
#define ALLOC_USER 0x0
|
||||
#define ALLOC_USER_STACK 0x1
|
||||
#define ALLOC_USER_RDONLY 0x2
|
||||
#define ALLOC_USER_EXEC 0x4
|
||||
|
||||
#define ATTEMPT_FREE(_x) \
|
||||
if (_x) { \
|
||||
kfree(_x); \
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ static uint64_t *pagemap_traverse(uint64_t *this_level, size_t next_num, uint64_
|
|||
|
||||
uintptr_t mmap_find_unmapped(struct rbtree *tree, spinlock_t *lock, uintptr_t start, size_t len)
|
||||
{
|
||||
bool kern = len >= hhdm_start;
|
||||
len = MAX(len, 0x1000);
|
||||
len = len | 0xFFF;
|
||||
len += 1;
|
||||
|
|
@ -74,6 +75,10 @@ uintptr_t mmap_find_unmapped(struct rbtree *tree, spinlock_t *lock, uintptr_t st
|
|||
}
|
||||
|
||||
spinlock_release(lock);
|
||||
|
||||
if (ret >= hhdm_start && !kern)
|
||||
return 0;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
@ -131,10 +136,6 @@ void *proc_mmap(struct proc *proc, paddr_t paddr, uintptr_t vaddr, size_t len, u
|
|||
if (vaddr + len >= hhdm_start) {
|
||||
/* higher-half addresses are only for kernel use, so we need to find a lower address to map to */
|
||||
vaddr = mmap_find_unmapped(&proc->page_map, &proc->page_map_lock, 0, len);
|
||||
if (vaddr + len >= hhdm_start) {
|
||||
kprintf(LOG_WARN "proc_mmap: Tried to map higher-half address %X\n", vaddr);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* if the memory address is already mapped */
|
||||
|
|
|
|||
129
mem/slab.c
129
mem/slab.c
|
|
@ -3,17 +3,21 @@
|
|||
#include <kernel/mem.h>
|
||||
#include <kernel/rbtree.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/proc.h>
|
||||
|
||||
#define SLAB_ALIGN 7
|
||||
|
||||
#define KMT_DMA 0xDE11A000
|
||||
|
||||
static size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 };
|
||||
static slab_t *slab_cache[ARRAY_SIZE(slab_sizes)];
|
||||
static slab_t *kslab_cache[ARRAY_SIZE(slab_sizes)];
|
||||
static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000, 0x1000000 };
|
||||
static slab_t *slab_cache_dma[ARRAY_SIZE(slab_sizes)];
|
||||
static size_t uslab_sizes[] = { 0x1000, 0x4000, 0x8000, 0x10000 };
|
||||
static slab_t *uslab_cache[ARRAY_SIZE(slab_sizes)];
|
||||
|
||||
static struct rbtree kmalloc_tree = { NULL, 0, 0 };
|
||||
static struct rbtree umalloc_tree = { NULL, 0, 0 };
|
||||
|
||||
static inline void slab_range(slab_t *slab, uintptr_t *o_start, uintptr_t *o_end)
|
||||
{
|
||||
|
|
@ -159,7 +163,7 @@ void slab_free(slab_t *slab, void *ptr)
|
|||
void kmalloc_init()
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++)
|
||||
slab_cache[i] = slab_create(slab_sizes[i], 128 * KB, 0);
|
||||
kslab_cache[i] = slab_create(slab_sizes[i], 128 * KB, 0);
|
||||
for (size_t i = 0; i < ARRAY_SIZE(dma_sizes); i++)
|
||||
slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K);
|
||||
}
|
||||
|
|
@ -221,7 +225,7 @@ void *kmalloc(size_t size, uint64_t flags)
|
|||
}
|
||||
|
||||
} else {
|
||||
ret = kmalloc_table(size, slab_sizes, ARRAY_SIZE(slab_sizes), slab_cache, false);
|
||||
ret = kmalloc_table(size, slab_sizes, ARRAY_SIZE(slab_sizes), kslab_cache, false);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
@ -297,10 +301,129 @@ void kfree(void *ptr)
|
|||
rbt_delete(&kmalloc_tree, n);
|
||||
}
|
||||
|
||||
void ufree(struct proc *proc, void *addr)
|
||||
{
|
||||
}
|
||||
|
||||
void *umalloc(struct proc *proc, size_t size, uint64_t opts)
|
||||
{
|
||||
uint64_t flags = 0;
|
||||
|
||||
struct proc_allocation *a = kzalloc(sizeof(struct proc_allocation), 0);
|
||||
|
||||
if (a == NULL)
|
||||
return NULL;
|
||||
|
||||
const size_t max_slab_size = uslab_sizes[ARRAY_SIZE(uslab_sizes) - 1];
|
||||
uintptr_t vaddr = mmap_find_unmapped(&proc->page_map, &proc->page_map_lock, USER_HEAP_BASE, size);
|
||||
uint64_t attr;
|
||||
|
||||
attr = PAGE_USER | PAGE_PRESENT;
|
||||
|
||||
if (!(opts & ALLOC_USER_RDONLY))
|
||||
attr |= PAGE_RW;
|
||||
|
||||
if (!(opts & ALLOC_USER_EXEC))
|
||||
attr |= PAGE_XD;
|
||||
|
||||
if (vaddr == 0)
|
||||
return NULL;
|
||||
|
||||
/* Do the allocation
|
||||
*
|
||||
* This part of the allocator decides whether to chain slabs, or to use a buddy allocator
|
||||
*
|
||||
* Slab chaining is only used in userspace allocations, and this is the only allocator that
|
||||
* does it.
|
||||
*/
|
||||
if (size > max_slab_size * 256) {
|
||||
/* Case 1:
|
||||
* Huge allocation: Just do a buddy_alloc
|
||||
*/
|
||||
struct proc_mapping *m = kzalloc(sizeof(struct proc_mapping), 0);
|
||||
if (m == NULL) {
|
||||
ufree(proc, a);
|
||||
return NULL;
|
||||
}
|
||||
size = npow2(size);
|
||||
void *ret = buddy_alloc(size);
|
||||
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
a->mappings = m;
|
||||
a->num_mappings = 1;
|
||||
|
||||
m->vaddr = vaddr;
|
||||
m->paddr = ((paddr_t)(ret) & (~hhdm_start));
|
||||
m->len = size;
|
||||
m->type = PM_BUD;
|
||||
m->attr = attr;
|
||||
|
||||
proc_mmap(proc, m->paddr, vaddr, size, m->attr);
|
||||
|
||||
struct rbnode *n = rbt_insert(&umalloc_tree, (uint64_t)vaddr);
|
||||
n->value = (uint64_t)a;
|
||||
} else {
|
||||
/* Case 2:
|
||||
* Regular sized allocation: Use slabs
|
||||
*/
|
||||
size_t total_alloc = 0;
|
||||
while (total_alloc < size) {
|
||||
struct proc_mapping *m = kzalloc(sizeof(struct proc_mapping), 0);
|
||||
|
||||
if (m == NULL) {
|
||||
ufree(proc, a);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a->num_mappings == 0)
|
||||
a->mappings = m;
|
||||
else
|
||||
a->last->next = m;
|
||||
|
||||
a->last = m;
|
||||
|
||||
a->num_mappings++;
|
||||
|
||||
size_t remaining = size - total_alloc;
|
||||
size_t this_size = uslab_sizes[ARRAY_SIZE(uslab_sizes) - 1];
|
||||
size_t this_index = ARRAY_SIZE(uslab_sizes) - 1;
|
||||
for (int i = ARRAY_SIZE(uslab_sizes) - 2; i >= 0; i--) {
|
||||
if (remaining >= uslab_sizes[i]) {
|
||||
this_index = i + 1;
|
||||
this_size = uslab_sizes[this_index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void *ret = slab_alloc(uslab_cache[this_index]);
|
||||
m->type = PM_SLB;
|
||||
m->vaddr = vaddr + total_alloc;
|
||||
m->paddr = ((paddr_t)(ret) & (~hhdm_start));
|
||||
m->len = this_size;
|
||||
m->attr = PAGE_USER | PAGE_PRESENT;
|
||||
m->slab = uslab_cache[this_index];
|
||||
|
||||
proc_mmap(proc, m->paddr, m->vaddr, m->len, m->attr);
|
||||
}
|
||||
}
|
||||
|
||||
return (void *)vaddr;
|
||||
}
|
||||
|
||||
static void umalloc_init()
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(uslab_sizes); i++)
|
||||
uslab_cache[i] = slab_create(uslab_sizes[i], 256 * KB, SLAB_PAGE_ALIGN);
|
||||
}
|
||||
|
||||
void slabtypes_init()
|
||||
{
|
||||
rbt_slab_init();
|
||||
|
||||
umalloc_init();
|
||||
|
||||
/* LAST! */
|
||||
kmalloc_init();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,6 +66,13 @@
|
|||
movq 0(%rax), %rax
|
||||
.endm
|
||||
|
||||
.macro save_sysretq_frame
|
||||
movq %rsp, 144(%rax)
|
||||
pushfq
|
||||
popq %rdi
|
||||
movq %rdi, 136(%rax)
|
||||
.endm
|
||||
|
||||
.macro save_iretq_frame
|
||||
/* save IRETQ frame */
|
||||
popq %rdi
|
||||
|
|
@ -149,6 +156,7 @@
|
|||
_exception_\nr:
|
||||
cli
|
||||
save_context
|
||||
save_iretq_frame
|
||||
popq %rsi
|
||||
pushq %rsi
|
||||
movq $\nr, %rdi
|
||||
|
|
@ -234,15 +242,56 @@ irq 0x0F
|
|||
|
||||
.global gate_syscall
|
||||
gate_syscall:
|
||||
pushq %r11
|
||||
pushq %rcx
|
||||
cli
|
||||
save_context
|
||||
save_sysretq_frame
|
||||
|
||||
/* switch to a kernel process */
|
||||
movq %rax, %rdi
|
||||
movq $_gate_syscall_entry_ret, %rsi
|
||||
call proc_enter_kernel
|
||||
_gate_syscall_entry_ret:
|
||||
movq %rdi, %rax
|
||||
|
||||
/* restore argument registers */
|
||||
movq 40(%rax), %rdi
|
||||
movq 32(%rax), %rsi
|
||||
movq 24(%rax), %rdx
|
||||
movq 72(%rax), %r10
|
||||
movq 56(%rax), %r8
|
||||
movq 0(%rax), %rax
|
||||
|
||||
movq %r10, %rcx
|
||||
movq %rax, %r9
|
||||
|
||||
call syscall
|
||||
|
||||
popq %rcx
|
||||
popq %r11
|
||||
/* switch back to user process */
|
||||
movq %rax, %rdi
|
||||
movq $1, %rsi
|
||||
cli
|
||||
call proc_leave_kernel
|
||||
|
||||
pushq %rax
|
||||
|
||||
call proc_current_regs
|
||||
|
||||
/* restore sysretq frame */
|
||||
movq 120(%rax), %rcx /* RIP */
|
||||
movq 136(%rax), %r11 /* RFLAGS */
|
||||
|
||||
/* switch to user stack */
|
||||
popq %rbp
|
||||
movq 144(%rax), %rsp
|
||||
|
||||
restore_context
|
||||
|
||||
pushq %rbp
|
||||
movq 48(%rax), %rbp
|
||||
|
||||
/* syscall return code */
|
||||
popq %rax
|
||||
|
||||
sysretq
|
||||
|
||||
.global gate_syscall_int80
|
||||
|
|
@ -391,3 +440,8 @@ _save_context:
|
|||
yield:
|
||||
hlt
|
||||
ret
|
||||
|
||||
.global reboot
|
||||
reboot:
|
||||
movq $0, %rax
|
||||
movq %rax, %cr3
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include "kernel/mem.h"
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/rbtree.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/gdt.h>
|
||||
#include <kernel/pio.h>
|
||||
|
||||
#include <lib/sem.h>
|
||||
|
||||
|
|
@ -121,6 +122,11 @@ void proc_set_exec_addr(struct proc *proc, uintptr_t addr)
|
|||
|
||||
void proc_term(pid_t pid)
|
||||
{
|
||||
if(pid == 1) {
|
||||
/* reboot */
|
||||
reboot();
|
||||
}
|
||||
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||
|
||||
if (proc_node) {
|
||||
|
|
@ -389,3 +395,4 @@ void proc_init(unsigned num_cpus)
|
|||
proc_set_current(0);
|
||||
(void)rbt_insert(proc_tree, 0);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,5 +45,10 @@ void _start()
|
|||
/* rw test */
|
||||
while ((n = read(fd, buf, 256)) > 0) {
|
||||
write(fd, buf, n);
|
||||
|
||||
if(buf[0] == 'q')
|
||||
break;
|
||||
}
|
||||
|
||||
exit(1234);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue