Re-factored sandbox into a single executable

This commit is contained in:
2022-12-29 19:47:25 +00:00
parent 2b59afcaf3
commit 2ac3e8602f
7 changed files with 44 additions and 46 deletions

View File

@@ -1,13 +1,33 @@
SANDBOX_DIR_LIST := $(filter-out ./include/,$(sort $(dir $(wildcard ./*/.)))) THIS_FILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
SANDBOX_PROG_MAKEFILE_PATH := $(abspath $(firstword $(MAKEFILE_LIST)))
SANDBOX_BASE_DIR := $(patsubst %/,%,$(dir $(THIS_FILE_PATH)))
SANDBOX_PROG_DIR := $(patsubst %/,%,$(dir $(SANDBOX_PROG_MAKEFILE_PATH)))
BUILD_DIR := $(SANDBOX_BASE_DIR)/../build/sandbox
all: include $(SANDBOX_BASE_DIR)/../tools/make/gcc-host.mk
EXEC_NAME := sandbox
DIR_LIST := memblock vm base
INCLUDE_DIRS := $(foreach dir,$(DIR_LIST),"-I$(dir)/include")
SRC := $(foreach dir,$(DIR_LIST),$(wildcard $(dir)/*.c))
OBJ := $(addprefix $(BUILD_DIR)/,$(SRC:.c=.o))
CFLAGS := $(INCLUDE_DIRS) -g
all: $(BUILD_DIR)/$(EXEC_NAME)
@for prog in $(SANDBOX_DIR_LIST); do \ @for prog in $(SANDBOX_DIR_LIST); do \
$(MAKE) -C $$prog; \ $(MAKE) -C $$prog; \
done done
clean: $(BUILD_DIR)/$(EXEC_NAME): $(OBJ)
@for prog in $(SANDBOX_DIR_LIST); do \ @mkdir -p $(@D)
$(MAKE) -C $$prog clean; \
done
.PHONY: all clean @printf " \033[1;36mHOSTLD\033[0m \033[1mbuild/sandbox/\033[35m$(EXEC_NAME)\n"
@$(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS)
$(BUILD_DIR)/%.o: %.c
@printf " \033[1;32mHOSTCC\033[0m sandbox/$<\n"
@mkdir -p $(@D)
@$(CC) $< -o $@ -c $(CFLAGS)
.PHONY: all

View File

@@ -2,16 +2,25 @@
#include <stddef.h> #include <stddef.h>
#include <inttypes.h> #include <inttypes.h>
#include <sys/mman.h> #include <sys/mman.h>
#include "memblock.h" #include <socks/memblock.h>
/* we're working with 4MiB of simulated system RAM */ /* we're working with 4MiB of simulated system RAM */
#define MEMORY_SIZE 0x400000ULL #define MEMORY_SIZE_MB 128
/* virtual address of where system memory is mapped */
static void *system_memory = NULL;
#define MEMPTR(offset) ((uintptr_t)system_memory + (offset))
#define MB_TO_BYTES(v) ((size_t)(v) * 0x100000)
#define PHYS_TO_VIRT(p) ((void *)((uintptr_t)system_memory + (p)))
#define VIRT_TO_PHYS(p) ((void *)((p) - (uintptr_t)system_memory))
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
void *system_memory = mmap( system_memory = mmap(
NULL, NULL,
MEMORY_SIZE, MB_TO_BYTES(MEMORY_SIZE_MB),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0); -1, 0);
@@ -22,9 +31,9 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
printf("allocated 0x%llx bytes of memory to act as system RAM at %p\n", MEMORY_SIZE, system_memory); printf("allocated %u MiB (0x%zx bytes) of memory to act as system RAM at %p\n", MEMORY_SIZE_MB, MB_TO_BYTES(MEMORY_SIZE_MB), system_memory);
memblock_add(0, MEMORY_SIZE); memblock_add(0, MB_TO_BYTES(MEMORY_SIZE_MB));
memblock_reserve(0x10000, 0x40000); memblock_reserve(0x10000, 0x40000);
memblock_reserve(0x60000, 0x20000); memblock_reserve(0x60000, 0x20000);
@@ -54,6 +63,6 @@ int main(int argc, const char **argv)
it.limit); it.limit);
} }
munmap(system_memory, MEMORY_SIZE); munmap(system_memory, MB_TO_BYTES(MEMORY_SIZE_MB));
return 0; return 0;
} }

View File

@@ -1,2 +0,0 @@
include ../sandbox-config.mk
include ../sandbox-template.mk

View File

@@ -1,7 +1,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include "memblock.h" #include <socks/memblock.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))

View File

@@ -1,29 +0,0 @@
#include <stdio.h>
#include <sys/mman.h>
#include "vm.h"
/* amount of memory to allocate for system RAM in megabytes */
#define SIMULATED_MEMORY_MB 16
#define MEMPTR(offset) ((void *)((uintptr_t)system_memory) + (offset))
int main(int argc, const char **argv)
{
printf("Hello, world!\n");
void *system_memory = mmap(
NULL,
SIMULATED_MEMORY_MB * 0x100000,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (system_memory == MAP_FAILED) {
perror("mmap");
fprintf(stderr, "cannot allocated simulated system RAM buffer\n");
return -1;
}
munmap(system_memory, SIMULATED_MEMORY_MB * 0x100000);
return 0;
}