diff --git a/sandbox/Makefile b/sandbox/Makefile index 79d50b0..21fe1a8 100644 --- a/sandbox/Makefile +++ b/sandbox/Makefile @@ -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 \ $(MAKE) -C $$prog; \ done -clean: - @for prog in $(SANDBOX_DIR_LIST); do \ - $(MAKE) -C $$prog clean; \ - done +$(BUILD_DIR)/$(EXEC_NAME): $(OBJ) + @mkdir -p $(@D) -.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 diff --git a/sandbox/include/socks/types.h b/sandbox/base/include/socks/types.h similarity index 100% rename from sandbox/include/socks/types.h rename to sandbox/base/include/socks/types.h diff --git a/sandbox/memblock/main.c b/sandbox/base/main.c similarity index 60% rename from sandbox/memblock/main.c rename to sandbox/base/main.c index 1a3526d..d02e50f 100644 --- a/sandbox/memblock/main.c +++ b/sandbox/base/main.c @@ -2,16 +2,25 @@ #include #include #include -#include "memblock.h" +#include /* 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) { - void *system_memory = mmap( + system_memory = mmap( NULL, - MEMORY_SIZE, + MB_TO_BYTES(MEMORY_SIZE_MB), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); @@ -22,9 +31,9 @@ int main(int argc, const char **argv) 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(0x60000, 0x20000); @@ -54,6 +63,6 @@ int main(int argc, const char **argv) it.limit); } - munmap(system_memory, MEMORY_SIZE); + munmap(system_memory, MB_TO_BYTES(MEMORY_SIZE_MB)); return 0; } diff --git a/sandbox/memblock/Makefile b/sandbox/memblock/Makefile deleted file mode 100644 index 1c7d103..0000000 --- a/sandbox/memblock/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -include ../sandbox-config.mk -include ../sandbox-template.mk diff --git a/sandbox/memblock/memblock.h b/sandbox/memblock/include/socks/memblock.h similarity index 100% rename from sandbox/memblock/memblock.h rename to sandbox/memblock/include/socks/memblock.h diff --git a/sandbox/memblock/memblock.c b/sandbox/memblock/memblock.c index bde37bc..789c072 100644 --- a/sandbox/memblock/memblock.c +++ b/sandbox/memblock/memblock.c @@ -1,7 +1,7 @@ #include #include #include -#include "memblock.h" +#include #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) diff --git a/sandbox/vm/main.c b/sandbox/vm/main.c deleted file mode 100644 index 6d885a0..0000000 --- a/sandbox/vm/main.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#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; -}