kernel: port sandbox components

This commit is contained in:
2023-02-03 20:51:23 +00:00
parent 40f83922da
commit 247bb2b530
14 changed files with 16 additions and 85 deletions

View File

@@ -22,7 +22,7 @@ ARCH_OBJ := $(addprefix $(BUILD_DIR)/,$(ARCH_C_FILES:.c=.o) $(ARCH_ASM_FILES:.S=
# Platform-independent kernel source files # Platform-independent kernel source files
#################################### ####################################
KERNEL_SRC_DIRS := init kernel vm KERNEL_SRC_DIRS := init kernel vm ds util
KERNEL_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.c)) KERNEL_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.c))
KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o)) KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o))
@@ -45,7 +45,7 @@ LDFLAGS := $(LDFLAGS) -g
ALL_KERNEL_OBJECT_FILES := $(KERNEL_OBJ) $(ARCH_OBJ) $(LIBC_OBJ) ALL_KERNEL_OBJECT_FILES := $(KERNEL_OBJ) $(ARCH_OBJ) $(LIBC_OBJ)
ALL_KERNEL_DEPS := $(ALL_KERNEL_OBJECT_FILES:.o=.d) ALL_KERNEL_DEPS := $(ALL_KERNEL_OBJECT_FILES:.o=.d)
all: $(BUILD_DIR)/$(KERNEL_EXEC) sandbox all: $(BUILD_DIR)/$(KERNEL_EXEC)
-include $(ALL_KERNEL_DEPS) -include $(ALL_KERNEL_DEPS)

View File

@@ -59,9 +59,6 @@
#include <socks/btree.h> #include <socks/btree.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -130,15 +127,7 @@ static inline int bf(btree_node_t *x)
*/ */
static void rotate_left(btree_t *tree, btree_node_t *x) static void rotate_left(btree_t *tree, btree_node_t *x)
{ {
assert(x != NULL);
btree_node_t *y = x->b_right; btree_node_t *y = x->b_right;
assert(y != NULL);
assert(y == x->b_left || y == x->b_right);
if (x->b_parent) {
assert(x == x->b_parent->b_left || x == x->b_parent->b_right);
}
btree_node_t *p = x->b_parent; btree_node_t *p = x->b_parent;
@@ -197,15 +186,7 @@ static void update_height_to_root(btree_node_t *x)
*/ */
static void rotate_right(btree_t *tree, btree_node_t *y) static void rotate_right(btree_t *tree, btree_node_t *y)
{ {
assert(y);
btree_node_t *x = y->b_left; btree_node_t *x = y->b_left;
assert(x);
assert(x == y->b_left || x == y->b_right);
if (y->b_parent) {
assert(y == y->b_parent->b_left || y == y->b_parent->b_right);
}
btree_node_t *p = y->b_parent; btree_node_t *p = y->b_parent;
@@ -338,10 +319,6 @@ static void insert_fixup(btree_t *tree, btree_node_t *w)
goto next_ancestor; goto next_ancestor;
} }
assert(x && y && z);
assert(x == y->b_left || x == y->b_right);
assert(y == z->b_left || y == z->b_right);
if (IS_LEFT_CHILD(z, y)) { if (IS_LEFT_CHILD(z, y)) {
if (IS_LEFT_CHILD(y, x)) { if (IS_LEFT_CHILD(y, x)) {
rotate_right(tree, z); rotate_right(tree, z);

View File

@@ -1,6 +1,4 @@
#include <socks/queue.h> #include <socks/queue.h>
#include <assert.h>
#include <stdio.h>
size_t queue_length(queue_t *q) size_t queue_length(queue_t *q)
{ {

View File

@@ -1,7 +1,7 @@
#ifndef SOCKS_QUEUE_H_ #ifndef SOCKS_QUEUE_H_
#define SOCKS_QUEUE_H_ #define SOCKS_QUEUE_H_
#include <string.h> #include <socks/libc/string.h>
#include <stdbool.h> #include <stdbool.h>
#define QUEUE_CONTAINER(t, m, v) ((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0)) #define QUEUE_CONTAINER(t, m, v) ((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0))

View File

@@ -1,23 +1,13 @@
#include <stdint.h> #include <stdint.h>
#include <socks/libc/stdio.h>
#include <socks/init.h> #include <socks/init.h>
#include <socks/console.h> #include <socks/printk.h>
#include <socks/machine/init.h> #include <socks/machine/init.h>
#include <socks/machine/cpu.h>
static ml_cpu_block g_bootstrap_cpu = {0};
void bootstrap_cpu_init(void)
{
ml_cpu_block_init(&g_bootstrap_cpu);
ml_cpu_block_use(&g_bootstrap_cpu);
}
void kernel_init(uintptr_t arg) void kernel_init(uintptr_t arg)
{ {
ml_init(arg); ml_init(arg);
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
printf("Line %d\n", i); printk("Line %d\n", i);
} }
} }

View File

@@ -5,5 +5,6 @@ static char log_buffer[LOG_BUFFER_SIZE] = {0};
int printk(const char *format, ...) int printk(const char *format, ...)
{ {
(void)log_buffer;
return 0; return 0;
} }

View File

@@ -8,9 +8,6 @@
extern "C" { extern "C" {
#endif #endif
#define printf printf_
int printf_(const char *format, ...);
#define sprintf sprintf_ #define sprintf sprintf_
int sprintf_(char *buffer, const char *format, ...); int sprintf_(char *buffer, const char *format, ...);

View File

@@ -1,5 +1,5 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <socks/libc/stdio.h>
struct magnitude { struct magnitude {
size_t threshold; size_t threshold;

View File

@@ -5,7 +5,6 @@
#include <stddef.h> #include <stddef.h>
#include <limits.h> #include <limits.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
/* One vm_pg_data_t per NUMA node. */ /* One vm_pg_data_t per NUMA node. */
static vm_pg_data_t *node_data = NULL; static vm_pg_data_t *node_data = NULL;

View File

@@ -1,6 +1,6 @@
#include <socks/queue.h> #include <socks/queue.h>
#include <stdlib.h> #include <socks/compiler.h>
#include <assert.h> #include <stddef.h>
#include <socks/vm.h> #include <socks/vm.h>
#define FREELIST_END ((unsigned int)-1) #define FREELIST_END ((unsigned int)-1)
@@ -103,7 +103,7 @@ static vm_slab_t *alloc_slab(vm_cache_t *cache, vm_flags_t flags)
return slab_hdr; return slab_hdr;
} }
static void destroy_slab(vm_slab_t *slab) static void __used destroy_slab(vm_slab_t *slab)
{ {
} }
@@ -149,11 +149,9 @@ void *vm_cache_alloc(vm_cache_t *cache, vm_flags_t flags)
if (!queue_empty(&cache->c_slabs_partial)) { if (!queue_empty(&cache->c_slabs_partial)) {
/* prefer using up partially-full slabs before taking a fresh one */ /* prefer using up partially-full slabs before taking a fresh one */
queue_entry_t *slab_entry = queue_pop_front(&cache->c_slabs_partial); queue_entry_t *slab_entry = queue_pop_front(&cache->c_slabs_partial);
assert(slab_entry);
slab = QUEUE_CONTAINER(vm_slab_t, s_list, slab_entry); slab = QUEUE_CONTAINER(vm_slab_t, s_list, slab_entry);
} else if (!queue_empty(&cache->c_slabs_empty)) { } else if (!queue_empty(&cache->c_slabs_empty)) {
queue_entry_t *slab_entry = queue_pop_front(&cache->c_slabs_empty); queue_entry_t *slab_entry = queue_pop_front(&cache->c_slabs_empty);
assert(slab_entry);
slab = QUEUE_CONTAINER(vm_slab_t, s_list, slab_entry); slab = QUEUE_CONTAINER(vm_slab_t, s_list, slab_entry);
} else { } else {
/* we've run out of slabs. create a new one */ /* we've run out of slabs. create a new one */

View File

@@ -1,5 +1,5 @@
#include <socks/vm.h> #include <socks/vm.h>
#include <string.h> #include <socks/libc/string.h>
#define SIZE_N_CACHE(s) \ #define SIZE_N_CACHE(s) \
{ .c_name = "size-" # s, .c_obj_size = s, .c_page_order = VM_PAGE_16K } { .c_name = "size-" # s, .c_obj_size = s, .c_page_order = VM_PAGE_16K }

View File

@@ -19,12 +19,10 @@
contributors may be used to endorse or promote products derived from this contributors may be used to endorse or promote products derived from this
software without specific prior written permission. software without specific prior written permission.
*/ */
#include "socks/types.h"
#include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <socks/types.h>
#include <string.h> #include <socks/libc/string.h>
#include <socks/memblock.h> #include <socks/memblock.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -252,8 +250,7 @@ static phys_addr_t do_alloc(size_t size)
} }
if (allocated_base == ADDR_MAX) { if (allocated_base == ADDR_MAX) {
fprintf(stderr, "memblock: cannot allocate %zu byte buffer!\n", size); return 0;
abort();
} }
int status = memblock_add_range(&memblock.reserved, allocated_base, size, MEMBLOCK_ALLOC); int status = memblock_add_range(&memblock.reserved, allocated_base, size, MEMBLOCK_ALLOC);

View File

@@ -1,9 +1,7 @@
#include <socks/types.h> #include <socks/types.h>
#include <socks/memblock.h> #include <socks/memblock.h>
#include <socks/vm.h> #include <socks/vm.h>
#include <string.h> #include <socks/libc/string.h>
#include <assert.h>
#include <stdio.h>
/* array of pages, one for each physical page frame present in RAM */ /* array of pages, one for each physical page frame present in RAM */
static vm_page_t *page_array = NULL; static vm_page_t *page_array = NULL;
@@ -52,7 +50,6 @@ void tmp_set_vaddr_base(void *p, size_t len)
phys_addr_t vm_virt_to_phys(void *p) phys_addr_t vm_virt_to_phys(void *p)
{ {
phys_addr_t x = (phys_addr_t)p - (phys_addr_t)tmp_vaddr_base; phys_addr_t x = (phys_addr_t)p - (phys_addr_t)tmp_vaddr_base;
assert(x < tmp_vaddr_len);
return x; return x;
} }
@@ -74,8 +71,6 @@ void vm_page_init_array()
page_array = memblock_alloc(sizeof(vm_page_t) * nr_pages); page_array = memblock_alloc(sizeof(vm_page_t) * nr_pages);
page_array_count = nr_pages; page_array_count = nr_pages;
printf("page_array covers 0x%zx bytes, %zu page frames\n", pmem_size, pmem_size / VM_PAGE_SIZE);
printf("page_array is %zu bytes long\n", sizeof(vm_page_t) * nr_pages);
for (size_t i = 0; i < nr_pages; i++) { for (size_t i = 0; i < nr_pages; i++) {
memset(&page_array[i], 0x0, sizeof page_array[i]); memset(&page_array[i], 0x0, sizeof page_array[i]);
@@ -90,8 +85,6 @@ void vm_page_init_array()
nr_reserved++; nr_reserved++;
} }
} }
printf("%zu reserved page frames\n", nr_reserved);
} }
vm_page_t *vm_page_get(phys_addr_t addr) vm_page_t *vm_page_get(phys_addr_t addr)

View File

@@ -2,11 +2,7 @@
#include <socks/queue.h> #include <socks/queue.h>
#include <socks/types.h> #include <socks/types.h>
#include <socks/vm.h> #include <socks/vm.h>
#include <string.h> #include <socks/libc/string.h>
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
#include <stdlib.h>
static vm_page_t *group_pages_into_block(vm_zone_t *z, phys_addr_t base, phys_addr_t limit, int order) static vm_page_t *group_pages_into_block(vm_zone_t *z, phys_addr_t base, phys_addr_t limit, int order)
{ {
@@ -36,8 +32,6 @@ static void convert_region_to_blocks(vm_zone_t *zone,
int reserved) int reserved)
{ {
size_t block_frames = vm_bytes_to_pages(limit - base + 1); size_t block_frames = vm_bytes_to_pages(limit - base + 1);
printf("adding region %08zx-%08zx (%zu frames) to zone %s\n",
base, limit, block_frames, zone->z_info.zd_name);
int reset_order = 0; int reset_order = 0;
for (int order = VM_PAGE_MAX_ORDER; order >= VM_PAGE_MIN_ORDER; ) { for (int order = VM_PAGE_MAX_ORDER; order >= VM_PAGE_MIN_ORDER; ) {
@@ -55,12 +49,6 @@ static void convert_region_to_blocks(vm_zone_t *zone,
continue; continue;
} }
printf("%s: %zu %s pages at %08" PRIxPTR "\n",
zone->z_info.zd_name,
order_frames,
reserved == 1 ? "reserved" : "free",
base);
phys_addr_t block_limit = base + (order_frames * VM_PAGE_SIZE) - 1; phys_addr_t block_limit = base + (order_frames * VM_PAGE_SIZE) - 1;
vm_page_t *block_page = group_pages_into_block(zone, base, block_limit, order); vm_page_t *block_page = group_pages_into_block(zone, base, block_limit, order);
@@ -76,11 +64,6 @@ static void convert_region_to_blocks(vm_zone_t *zone,
reset_order = 0; reset_order = 0;
} }
if (base > limit + 1) {
printf("too many pages created! %zx > %zx\n", base, limit);
abort();
}
if (base == limit) { if (base == limit) {
break; break;
} }
@@ -93,8 +76,6 @@ void vm_zone_init(vm_zone_t *z, const vm_zone_descriptor_t *zone_info)
return; return;
} }
printf("initialising zone %s (%08zx-%08zx)\n",
zone_info->zd_name, zone_info->zd_base, zone_info->zd_limit);
memset(z, 0x0, sizeof *z); memset(z, 0x0, sizeof *z);
memcpy(&z->z_info, zone_info, sizeof *zone_info); memcpy(&z->z_info, zone_info, sizeof *zone_info);
z->z_lock = SPIN_LOCK_INIT; z->z_lock = SPIN_LOCK_INIT;