pmm: Add option to randomise memory contents at boot-up
diff --git a/stage23/entry.s3.c b/stage23/entry.s3.c
index b573d950..955ed966 100644
--- a/stage23/entry.s3.c
+++ b/stage23/entry.s3.c
@@ -96,6 +96,13 @@ void stage3_common(void) {
char *verbose_str = config_get_value(NULL, 0, "VERBOSE");
verbose = verbose_str != NULL && strcmp(verbose_str, "yes") == 0;
+ char *randomise_mem_str = config_get_value(NULL, 0, "RANDOMISE_MEMORY");
+ if (randomise_mem_str == NULL)
+ randomise_mem_str = config_get_value(NULL, 0, "RANDOMIZE_MEMORY");
+ bool randomise_mem = randomise_mem_str != NULL && strcmp(randomise_mem_str, "yes") == 0;
+ if (randomise_mem)
+ pmm_randomise_memory();
+
if (verbose) {
print("Boot drive: %x\n", boot_volume->index);
print("Boot partition: %d\n", boot_volume->partition);
diff --git a/stage23/mm/pmm.c b/stage23/mm/pmm.c
new file mode 100644
index 00000000..a351afeb
--- /dev/null
+++ b/stage23/mm/pmm.c
@@ -0,0 +1,34 @@
+#include <mm/pmm.h>
+#include <lib/rand.h>
+#include <lib/print.h>
+
+void pmm_randomise_memory(void) {
+ print("pmm: Randomising memory contents...");
+
+ for (size_t i = 0; i < memmap_entries; i++) {
+ if (memmap[i].type != MEMMAP_USABLE)
+ continue;
+
+ uint8_t *ptr = (void *)(uintptr_t)memmap[i].base;
+ size_t len = memmap[i].length;
+
+ for (size_t j = 0;;) {
+ uint32_t random = rand32();
+ uint8_t *rnd_data = (void *)&random;
+ if (j >= len)
+ break;
+ ptr[j++] = rnd_data[0];
+ if (j >= len)
+ break;
+ ptr[j++] = rnd_data[1];
+ if (j >= len)
+ break;
+ ptr[j++] = rnd_data[2];
+ if (j >= len)
+ break;
+ ptr[j++] = rnd_data[3];
+ }
+ }
+
+ print("\n");
+}
diff --git a/stage23/mm/pmm.h b/stage23/mm/pmm.h
index ee1b6da3..dd32e9fa 100644
--- a/stage23/mm/pmm.h
+++ b/stage23/mm/pmm.h
@@ -30,6 +30,7 @@ struct e820_entry_t *get_memmap(size_t *entries);
struct e820_entry_t *get_raw_memmap(size_t *entry_count);
void print_memmap(struct e820_entry_t *mm, size_t size);
bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free_only, bool panic, bool simulation, bool new_entry);
+void pmm_randomise_memory(void);
void *ext_mem_alloc(size_t count);
void *ext_mem_alloc_type(size_t count, uint32_t type);
