Reorganise pmm code

This commit is contained in:
mintsuki 2020-09-20 12:03:44 +02:00
parent f59a3d67d6
commit e721c3c814
23 changed files with 132 additions and 103 deletions

Binary file not shown.

View File

@ -6,6 +6,7 @@
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/part.h> #include <lib/part.h>
#include <lib/print.h> #include <lib/print.h>
#include <mm/pmm.h>
#define SECTOR_SIZE 512 #define SECTOR_SIZE 512
#define BLOCK_SIZE_IN_SECTORS 16 #define BLOCK_SIZE_IN_SECTORS 16
@ -29,7 +30,7 @@ static int cache_block(int drive, uint64_t block) {
return 0; return 0;
if (!cache) if (!cache)
cache = balloc_aligned(BLOCK_SIZE, 16); cache = conv_mem_alloc_aligned(BLOCK_SIZE, 16);
dap.segment = rm_seg(cache); dap.segment = rm_seg(cache);
dap.offset = rm_off(cache); dap.offset = rm_off(cache);

View File

@ -2,11 +2,11 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <drivers/vbe.h> #include <drivers/vbe.h>
#include <lib/memmap.h>
#include <lib/libc.h> #include <lib/libc.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/real.h> #include <lib/real.h>
#include <lib/print.h> #include <lib/print.h>
#include <mm/pmm.h>
#define VGA_FONT_WIDTH 8 #define VGA_FONT_WIDTH 8
#define VGA_FONT_HEIGHT 16 #define VGA_FONT_HEIGHT 16
@ -22,7 +22,7 @@ static void vga_font_retrieve(void) {
r.ebx = 0x0600; r.ebx = 0x0600;
rm_int(0x10, &r, &r); rm_int(0x10, &r, &r);
vga_font = ext_mem_balloc(VGA_FONT_MAX, MEMMAP_BOOTLOADER_RECLAIMABLE); vga_font = ext_mem_alloc(VGA_FONT_MAX);
memcpy(vga_font, (void *)rm_desegment(r.es, r.ebp), VGA_FONT_MAX); memcpy(vga_font, (void *)rm_desegment(r.es, r.ebp), VGA_FONT_MAX);
} }
@ -222,7 +222,7 @@ void vbe_tty_init(int *_rows, int *_cols) {
vga_font_retrieve(); vga_font_retrieve();
*_cols = cols = vbe_width / VGA_FONT_WIDTH; *_cols = cols = vbe_width / VGA_FONT_WIDTH;
*_rows = rows = vbe_height / VGA_FONT_HEIGHT; *_rows = rows = vbe_height / VGA_FONT_HEIGHT;
grid = ext_mem_balloc(rows * cols * sizeof(struct vbe_char), MEMMAP_BOOTLOADER_RECLAIMABLE); grid = ext_mem_alloc(rows * cols * sizeof(struct vbe_char));
vbe_clear(true); vbe_clear(true);
} }

View File

@ -5,6 +5,7 @@
#include <lib/print.h> #include <lib/print.h>
#include <drivers/disk.h> #include <drivers/disk.h>
#include <stdbool.h> #include <stdbool.h>
#include <mm/pmm.h>
struct echfs_identity_table { struct echfs_identity_table {
uint8_t jmp[4]; uint8_t jmp[4];
@ -123,7 +124,7 @@ found:;
// Load the allocation map. // Load the allocation map.
uint64_t file_block_count = DIV_ROUNDUP(ret->dir_entry.size, ret->block_size); uint64_t file_block_count = DIV_ROUNDUP(ret->dir_entry.size, ret->block_size);
ret->alloc_map = balloc(file_block_count * sizeof(uint64_t)); ret->alloc_map = conv_mem_alloc(file_block_count * sizeof(uint64_t));
ret->alloc_map[0] = ret->dir_entry.payload; ret->alloc_map[0] = ret->dir_entry.payload;
for (uint64_t i = 1; i < file_block_count; i++) { for (uint64_t i = 1; i < file_block_count; i++) {

View File

@ -5,6 +5,7 @@
#include <drivers/disk.h> #include <drivers/disk.h>
#include <lib/libc.h> #include <lib/libc.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <mm/pmm.h>
/* EXT2 Filesystem States */ /* EXT2 Filesystem States */
#define EXT2_FS_UNRECOVERABLE_ERRORS 3 #define EXT2_FS_UNRECOVERABLE_ERRORS 3
@ -125,7 +126,7 @@ static int ext2_parse_dirent(struct ext2_dir_entry *dir, struct ext2_file_handle
if (*path == '/') if (*path == '/')
path++; path++;
struct ext2_inode *current_inode = balloc(sizeof(struct ext2_inode)); struct ext2_inode *current_inode = conv_mem_alloc(sizeof(struct ext2_inode));
*current_inode = fd->root_inode; *current_inode = fd->root_inode;
bool escape = false; bool escape = false;
@ -148,7 +149,7 @@ next:
fd->drive, &fd->part); fd->drive, &fd->part);
// name read // name read
char *name = balloc(dir->name_len + 1); char *name = conv_mem_alloc(dir->name_len + 1);
memset(name, 0, dir->name_len + 1); memset(name, 0, dir->name_len + 1);
inode_read(name, i + sizeof(struct ext2_dir_entry), dir->name_len, inode_read(name, i + sizeof(struct ext2_dir_entry), dir->name_len,
@ -156,11 +157,11 @@ next:
int r = strcmp(token, name); int r = strcmp(token, name);
brewind(dir->name_len); conv_mem_rewind();
if (!r) { if (!r) {
if (escape) { if (escape) {
brewind(sizeof(struct ext2_inode)); conv_mem_rewind();
return 0; return 0;
} else { } else {
// update the current inode // update the current inode
@ -172,7 +173,7 @@ next:
i += dir->rec_len; i += dir->rec_len;
} }
brewind(sizeof(struct ext2_inode)); conv_mem_rewind();
return -1; return -1;
} }

View File

@ -5,10 +5,11 @@
#include <fs/ext2.h> #include <fs/ext2.h>
#include <fs/fat32.h> #include <fs/fat32.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <mm/pmm.h>
int fopen(struct file_handle *ret, int disk, int partition, const char *filename) { int fopen(struct file_handle *ret, int disk, int partition, const char *filename) {
if (echfs_check_signature(disk, partition)) { if (echfs_check_signature(disk, partition)) {
struct echfs_file_handle *fd = balloc(sizeof(struct echfs_file_handle)); struct echfs_file_handle *fd = conv_mem_alloc(sizeof(struct echfs_file_handle));
int r = echfs_open(fd, disk, partition, filename); int r = echfs_open(fd, disk, partition, filename);
if (r) if (r)
@ -24,7 +25,7 @@ int fopen(struct file_handle *ret, int disk, int partition, const char *filename
} }
if (ext2_check_signature(disk, partition)) { if (ext2_check_signature(disk, partition)) {
struct ext2_file_handle *fd = balloc(sizeof(struct ext2_file_handle)); struct ext2_file_handle *fd = conv_mem_alloc(sizeof(struct ext2_file_handle));
int r = ext2_open(fd, disk, partition, filename); int r = ext2_open(fd, disk, partition, filename);
if (r) if (r)
@ -40,7 +41,7 @@ int fopen(struct file_handle *ret, int disk, int partition, const char *filename
} }
if (fat32_check_signature(disk, partition)) { if (fat32_check_signature(disk, partition)) {
struct fat32_file_handle *fd = balloc(sizeof(struct fat32_file_handle)); struct fat32_file_handle *fd = conv_mem_alloc(sizeof(struct fat32_file_handle));
int r = fat32_open(fd, disk, partition, filename); int r = fat32_open(fd, disk, partition, filename);

View File

@ -46,42 +46,6 @@ __attribute__((noreturn)) void panic(const char *fmt, ...) {
} }
} }
extern symbol bss_end;
static size_t bump_allocator_base = (size_t)bss_end;
static size_t bump_allocator_limit = 0;
void brewind(size_t count) {
bump_allocator_base -= count;
}
void *balloc(size_t count) {
return balloc_aligned(count, 4);
}
void *balloc_aligned(size_t count, size_t alignment) {
if (!bump_allocator_limit) {
// The balloc limit is the beginning of the GDT
struct {
uint16_t limit;
uint32_t ptr;
} __attribute__((packed)) gdtr;
asm volatile ("sgdt %0" :: "m"(gdtr));
bump_allocator_limit = gdtr.ptr;
}
size_t new_base = ALIGN_UP(bump_allocator_base, alignment);
void *ret = (void *)new_base;
new_base += count;
if (new_base >= bump_allocator_limit)
panic("Memory allocation failed");
bump_allocator_base = new_base;
// Zero out allocated space
memset(ret, 0, count);
return ret;
}
uint64_t strtoui(const char *s) { uint64_t strtoui(const char *s) {
uint64_t n = 0; uint64_t n = 0;
while (*s) while (*s)

View File

@ -13,10 +13,6 @@ __attribute__((noreturn)) void panic(const char *fmt, ...);
int pit_sleep_and_quit_on_keypress(uint32_t pit_ticks); int pit_sleep_and_quit_on_keypress(uint32_t pit_ticks);
void brewind(size_t count);
void *balloc(size_t count);
void *balloc_aligned(size_t count, size_t alignment);
#define GETCHAR_CURSOR_LEFT (-10) #define GETCHAR_CURSOR_LEFT (-10)
#define GETCHAR_CURSOR_RIGHT (-11) #define GETCHAR_CURSOR_RIGHT (-11)
#define GETCHAR_CURSOR_UP (-12) #define GETCHAR_CURSOR_UP (-12)

View File

@ -2,6 +2,7 @@
#include <lib/config.h> #include <lib/config.h>
#include <lib/libc.h> #include <lib/libc.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <mm/pmm.h>
#include <fs/file.h> #include <fs/file.h>
#define SEPARATOR '\n' #define SEPARATOR '\n'
@ -18,7 +19,7 @@ int init_config(int drive, int part) {
} }
size_t config_size = f.size + 1; size_t config_size = f.size + 1;
config_addr = balloc(config_size); config_addr = conv_mem_alloc(config_size);
fread(&f, config_addr, 0, f.size); fread(&f, config_addr, 0, f.size);

View File

@ -4,7 +4,7 @@
#include <lib/libc.h> #include <lib/libc.h>
#include <lib/elf.h> #include <lib/elf.h>
#include <lib/print.h> #include <lib/print.h>
#include <lib/memmap.h> #include <mm/pmm.h>
#include <fs/file.h> #include <fs/file.h>
#define PT_LOAD 0x00000001 #define PT_LOAD 0x00000001

View File

@ -111,8 +111,9 @@ void print(const char *fmt, ...) {
va_end(args); va_end(args);
} }
static char print_buf[PRINT_BUF_MAX];
void vprint(const char *fmt, va_list args) { void vprint(const char *fmt, va_list args) {
char *print_buf = balloc(PRINT_BUF_MAX);
size_t print_buf_i = 0; size_t print_buf_i = 0;
for (;;) { for (;;) {
@ -173,6 +174,4 @@ out:
for (size_t i = 0; i < print_buf_i; i++) for (size_t i = 0; i < print_buf_i; i++)
port_out_b(0xe9, print_buf[i]); port_out_b(0xe9, print_buf[i]);
#endif #endif
brewind(PRINT_BUF_MAX);
} }

View File

@ -4,6 +4,7 @@
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/print.h> #include <lib/print.h>
#include <lib/rand.h> #include <lib/rand.h>
#include <mm/pmm.h>
// TODO: Find where this mersenne twister implementation is inspired from // TODO: Find where this mersenne twister implementation is inspired from
// and properly credit the original author(s). // and properly credit the original author(s).
@ -64,7 +65,7 @@ static void init_rand(void) {
seed *= (seed ^ rdrand(uint32_t)); seed *= (seed ^ rdrand(uint32_t));
} }
status = balloc(n); status = conv_mem_alloc(n);
srand(seed); srand(seed);

View File

@ -6,10 +6,10 @@
#include <lib/part.h> #include <lib/part.h>
#include <lib/config.h> #include <lib/config.h>
#include <sys/e820.h> #include <sys/e820.h>
#include <lib/memmap.h>
#include <lib/print.h> #include <lib/print.h>
#include <fs/file.h> #include <fs/file.h>
#include <lib/elf.h> #include <lib/elf.h>
#include <mm/pmm.h>
#include <protos/stivale.h> #include <protos/stivale.h>
#include <protos/stivale2.h> #include <protos/stivale2.h>
#include <protos/linux.h> #include <protos/linux.h>

View File

@ -8,6 +8,7 @@
#include <lib/libc.h> #include <lib/libc.h>
#include <lib/config.h> #include <lib/config.h>
#include <lib/term.h> #include <lib/term.h>
#include <mm/pmm.h>
static char *cmdline; static char *cmdline;
#define CMDLINE_MAX 1024 #define CMDLINE_MAX 1024
@ -15,7 +16,7 @@ static char *cmdline;
static char config_entry_name[1024]; static char config_entry_name[1024];
char *menu(void) { char *menu(void) {
cmdline = balloc(CMDLINE_MAX); cmdline = conv_mem_alloc(CMDLINE_MAX);
char buf[16]; char buf[16];

View File

@ -1,9 +1,10 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <lib/memmap.h> #include <mm/pmm.h>
#include <sys/e820.h> #include <sys/e820.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/libc.h>
#include <lib/print.h> #include <lib/print.h>
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
@ -157,13 +158,20 @@ void init_memmap(void) {
sanitise_entries(); sanitise_entries();
} }
void *ext_mem_balloc(size_t count, uint32_t type) { void *ext_mem_alloc(size_t count) {
return ext_mem_balloc_aligned(count, 4, type); return ext_mem_alloc_type(count, MEMMAP_BOOTLOADER_RECLAIMABLE);
} }
// TODO: this basically only works for the 1st extended memory entry in the void *ext_mem_alloc_aligned(size_t count, size_t alignment) {
// memmap and allocates until the first hole following it. Fix that. return ext_mem_alloc_aligned_type(count, alignment, MEMMAP_BOOTLOADER_RECLAIMABLE);
void *ext_mem_balloc_aligned(size_t count, size_t alignment, uint32_t type) { }
void *ext_mem_alloc_type(size_t count, uint32_t type) {
return ext_mem_alloc_aligned_type(count, 4, type);
}
// Allocate memory top down, hopefully without bumping into kernel or modules
void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type) {
for (int i = memmap_entries - 1; i >= 0; i--) { for (int i = memmap_entries - 1; i >= 0; i--) {
if (memmap[i].type != 1) if (memmap[i].type != 1)
continue; continue;
@ -189,7 +197,12 @@ void *ext_mem_balloc_aligned(size_t count, size_t alignment, uint32_t type) {
int64_t aligned_length = entry_top - alloc_base; int64_t aligned_length = entry_top - alloc_base;
memmap_alloc_range((uint64_t)alloc_base, (uint64_t)aligned_length, type); memmap_alloc_range((uint64_t)alloc_base, (uint64_t)aligned_length, type);
return (void *)(size_t)alloc_base; void *ret = (void *)(size_t)alloc_base;
// Zero out allocated space
memset(ret, 0, count);
return ret;
} }
panic("High memory allocator: Out of memory"); panic("High memory allocator: Out of memory");
@ -246,3 +259,45 @@ void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type) {
panic("Out of memory"); panic("Out of memory");
} }
extern symbol bss_end;
static size_t bump_allocator_base = (size_t)bss_end;
static size_t bump_allocator_limit = 0;
void conv_mem_rewind(void) {
size_t *old_base = (size_t *)(bump_allocator_base - sizeof(size_t));
bump_allocator_base = *old_base;
}
void *conv_mem_alloc(size_t count) {
return conv_mem_alloc_aligned(count, 4);
}
void *conv_mem_alloc_aligned(size_t count, size_t alignment) {
if (!bump_allocator_limit) {
// The balloc limit is the beginning of the GDT
struct {
uint16_t limit;
uint32_t ptr;
} __attribute__((packed)) gdtr;
asm volatile ("sgdt %0" :: "m"(gdtr));
bump_allocator_limit = gdtr.ptr;
}
size_t new_base = ALIGN_UP(bump_allocator_base, alignment);
void *ret = (void *)new_base;
size_t *old_base = (size_t *)(new_base + count);
new_base += count + sizeof(size_t);
if (new_base >= bump_allocator_limit)
panic("Memory allocation failed");
*old_base = bump_allocator_base;
bump_allocator_base = new_base;
// Zero out allocated space
memset(ret, 0, count);
return ret;
}

View File

@ -1,5 +1,5 @@
#ifndef __LIB__MEMMAP_H__ #ifndef __MM__PMM_H__
#define __LIB__MEMMAP_H__ #define __MM__PMM_H__
#include <stdint.h> #include <stdint.h>
#include <sys/e820.h> #include <sys/e820.h>
@ -13,10 +13,17 @@
#define MEMMAP_KERNEL_AND_MODULES 0x1001 #define MEMMAP_KERNEL_AND_MODULES 0x1001
void init_memmap(void); void init_memmap(void);
void *ext_mem_balloc(size_t count, uint32_t type);
void *ext_mem_balloc_aligned(size_t count, size_t alignment, uint32_t type);
void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type);
struct e820_entry_t *get_memmap(size_t *entries); struct e820_entry_t *get_memmap(size_t *entries);
void print_memmap(struct e820_entry_t *mm, size_t size); void print_memmap(struct e820_entry_t *mm, size_t size);
void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type);
void *ext_mem_alloc(size_t count);
void *ext_mem_alloc_type(size_t count, uint32_t type);
void *ext_mem_alloc_aligned(size_t count, size_t alignment);
void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type);
void conv_mem_rewind(void);
void *conv_mem_alloc(size_t count);
void *conv_mem_alloc_aligned(size_t count, size_t alignment);
#endif #endif

View File

@ -1,6 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <mm/vmm64.h> #include <mm/vmm64.h>
#include <mm/pmm.h>
#include <lib/blib.h> #include <lib/blib.h>
#define PT_SIZE ((uint64_t)0x1000) #define PT_SIZE ((uint64_t)0x1000)
@ -15,7 +16,7 @@ static pt_entry_t *get_next_level(pt_entry_t *current_level, size_t entry) {
ret = (pt_entry_t *)(current_level[entry] & ~((pt_entry_t)0xfff)); ret = (pt_entry_t *)(current_level[entry] & ~((pt_entry_t)0xfff));
} else { } else {
// Allocate a table for the next level // Allocate a table for the next level
ret = balloc_aligned(PT_SIZE, PT_SIZE); ret = ext_mem_alloc_aligned(PT_SIZE, PT_SIZE);
// Present + writable + user (0b111) // Present + writable + user (0b111)
current_level[entry] = (pt_entry_t)ret | 0b111; current_level[entry] = (pt_entry_t)ret | 0b111;
} }
@ -26,7 +27,7 @@ static pt_entry_t *get_next_level(pt_entry_t *current_level, size_t entry) {
pagemap_t new_pagemap(int lv) { pagemap_t new_pagemap(int lv) {
pagemap_t pagemap; pagemap_t pagemap;
pagemap.levels = lv; pagemap.levels = lv;
pagemap.top_level = balloc_aligned(PT_SIZE, PT_SIZE); pagemap.top_level = ext_mem_alloc_aligned(PT_SIZE, PT_SIZE);
return pagemap; return pagemap;
} }

View File

@ -7,7 +7,7 @@
#include <lib/term.h> #include <lib/term.h>
#include <lib/config.h> #include <lib/config.h>
#include <lib/print.h> #include <lib/print.h>
#include <lib/memmap.h> #include <mm/pmm.h>
#define KERNEL_LOAD_ADDR ((size_t)0x100000) #define KERNEL_LOAD_ADDR ((size_t)0x100000)
#define INITRD_LOAD_ADDR ((size_t)0x1000000) #define INITRD_LOAD_ADDR ((size_t)0x1000000)
@ -31,12 +31,12 @@ void linux_load(char *cmdline, int boot_drive) {
} }
} }
char *kernel_path = balloc(128); char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) { if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified"); panic("KERNEL_PATH not specified");
} }
struct file_handle *fd = balloc(sizeof(struct file_handle)); struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) { if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file"); panic("Could not open kernel file");
} }
@ -63,7 +63,7 @@ void linux_load(char *cmdline, int boot_drive) {
print("linux: Real Mode code size: %x\n", real_mode_code_size); print("linux: Real Mode code size: %x\n", real_mode_code_size);
void *real_mode_code = balloc_aligned(real_mode_code_size, 0x1000); void *real_mode_code = conv_mem_alloc_aligned(real_mode_code_size, 0x1000);
fread(fd, real_mode_code, 0, real_mode_code_size); fread(fd, real_mode_code, 0, real_mode_code_size);

View File

@ -5,7 +5,6 @@
#include <lib/elf.h> #include <lib/elf.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/acpi.h> #include <lib/acpi.h>
#include <lib/memmap.h>
#include <lib/config.h> #include <lib/config.h>
#include <lib/time.h> #include <lib/time.h>
#include <lib/print.h> #include <lib/print.h>
@ -16,6 +15,7 @@
#include <sys/pic.h> #include <sys/pic.h>
#include <fs/file.h> #include <fs/file.h>
#include <mm/vmm64.h> #include <mm/vmm64.h>
#include <mm/pmm.h>
#include <stivale/stivale.h> #include <stivale/stivale.h>
#define KASLR_SLIDE_BITMASK 0x03FFFF000u #define KASLR_SLIDE_BITMASK 0x03FFFF000u
@ -43,12 +43,12 @@ void stivale_load(char *cmdline, int boot_drive) {
} }
} }
char *kernel_path = balloc(128); char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) { if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified"); panic("KERNEL_PATH not specified");
} }
struct file_handle *fd = balloc(sizeof(struct file_handle)); struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) { if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file"); panic("Could not open kernel file");
} }
@ -139,7 +139,7 @@ void stivale_load(char *cmdline, int boot_drive) {
stivale_struct.module_count++; stivale_struct.module_count++;
struct stivale_module *m = balloc(sizeof(struct stivale_module)); struct stivale_module *m = conv_mem_alloc(sizeof(struct stivale_module));
if (!config_get_value(m->string, i, 128, "MODULE_STRING")) { if (!config_get_value(m->string, i, 128, "MODULE_STRING")) {
m->string[0] = '\0'; m->string[0] = '\0';

View File

@ -3,7 +3,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <lib/memmap.h>
#include <sys/e820.h> #include <sys/e820.h>
#include <mm/vmm64.h> #include <mm/vmm64.h>

View File

@ -7,7 +7,6 @@
#include <lib/elf.h> #include <lib/elf.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/acpi.h> #include <lib/acpi.h>
#include <lib/memmap.h>
#include <lib/config.h> #include <lib/config.h>
#include <lib/time.h> #include <lib/time.h>
#include <lib/print.h> #include <lib/print.h>
@ -19,6 +18,7 @@
#include <lib/term.h> #include <lib/term.h>
#include <sys/pic.h> #include <sys/pic.h>
#include <fs/file.h> #include <fs/file.h>
#include <mm/pmm.h>
#include <stivale/stivale2.h> #include <stivale/stivale2.h>
#define KASLR_SLIDE_BITMASK 0x03FFFF000u #define KASLR_SLIDE_BITMASK 0x03FFFF000u
@ -66,12 +66,12 @@ void stivale2_load(char *cmdline, int boot_drive) {
} }
} }
char *kernel_path = balloc(128); char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) { if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified"); panic("KERNEL_PATH not specified");
} }
struct file_handle *fd = balloc(sizeof(struct file_handle)); struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) { if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file"); panic("Could not open kernel file");
} }
@ -160,7 +160,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
// Create firmware struct tag // Create firmware struct tag
////////////////////////////////////////////// //////////////////////////////////////////////
{ {
struct stivale2_struct_tag_firmware *tag = balloc(sizeof(struct stivale2_struct_tag_firmware)); struct stivale2_struct_tag_firmware *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_firmware));
tag->tag.identifier = STIVALE2_STRUCT_TAG_FIRMWARE_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_FIRMWARE_ID;
tag->flags = 1 << 0; // bit 0 = BIOS boot tag->flags = 1 << 0; // bit 0 = BIOS boot
@ -172,7 +172,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
// Create modules struct tag // Create modules struct tag
////////////////////////////////////////////// //////////////////////////////////////////////
{ {
struct stivale2_struct_tag_modules *tag = balloc(sizeof(struct stivale2_struct_tag_modules)); struct stivale2_struct_tag_modules *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_modules));
tag->tag.identifier = STIVALE2_STRUCT_TAG_MODULES_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_MODULES_ID;
tag->module_count = 0; tag->module_count = 0;
@ -184,7 +184,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
tag->module_count++; tag->module_count++;
struct stivale2_module *m = balloc_aligned(sizeof(struct stivale2_module), 1); struct stivale2_module *m = conv_mem_alloc_aligned(sizeof(struct stivale2_module), 1);
if (!config_get_value(m->string, i, 128, "MODULE_STRING")) { if (!config_get_value(m->string, i, 128, "MODULE_STRING")) {
m->string[0] = '\0'; m->string[0] = '\0';
@ -230,7 +230,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
// Create RSDP struct tag // Create RSDP struct tag
////////////////////////////////////////////// //////////////////////////////////////////////
{ {
struct stivale2_struct_tag_rsdp *tag = balloc(sizeof(struct stivale2_struct_tag_rsdp)); struct stivale2_struct_tag_rsdp *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_rsdp));
tag->tag.identifier = STIVALE2_STRUCT_TAG_RSDP_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_RSDP_ID;
tag->rsdp = (uint64_t)(size_t)acpi_get_rsdp(); tag->rsdp = (uint64_t)(size_t)acpi_get_rsdp();
@ -242,7 +242,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
// Create cmdline struct tag // Create cmdline struct tag
////////////////////////////////////////////// //////////////////////////////////////////////
{ {
struct stivale2_struct_tag_cmdline *tag = balloc(sizeof(struct stivale2_struct_tag_cmdline)); struct stivale2_struct_tag_cmdline *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_cmdline));
tag->tag.identifier = STIVALE2_STRUCT_TAG_CMDLINE_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_CMDLINE_ID;
tag->cmdline = (uint64_t)(size_t)cmdline; tag->cmdline = (uint64_t)(size_t)cmdline;
@ -254,7 +254,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
// Create epoch struct tag // Create epoch struct tag
////////////////////////////////////////////// //////////////////////////////////////////////
{ {
struct stivale2_struct_tag_epoch *tag = balloc(sizeof(struct stivale2_struct_tag_epoch)); struct stivale2_struct_tag_epoch *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_epoch));
tag->tag.identifier = STIVALE2_STRUCT_TAG_EPOCH_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_EPOCH_ID;
tag->epoch = time(); tag->epoch = time();
@ -272,7 +272,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
term_deinit(); term_deinit();
if (hdrtag != NULL) { if (hdrtag != NULL) {
struct stivale2_struct_tag_framebuffer *tag = balloc(sizeof(struct stivale2_struct_tag_framebuffer)); struct stivale2_struct_tag_framebuffer *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_framebuffer));
tag->tag.identifier = STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID;
tag->framebuffer_width = hdrtag->framebuffer_width; tag->framebuffer_width = hdrtag->framebuffer_width;
@ -298,14 +298,14 @@ void stivale2_load(char *cmdline, int boot_drive) {
// Create memmap struct tag // Create memmap struct tag
////////////////////////////////////////////// //////////////////////////////////////////////
{ {
struct stivale2_struct_tag_memmap *tag = balloc(sizeof(struct stivale2_struct_tag_memmap)); struct stivale2_struct_tag_memmap *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_memmap));
tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
memmap = get_memmap(&memmap_entries); memmap = get_memmap(&memmap_entries);
tag->entries = (uint64_t)memmap_entries; tag->entries = (uint64_t)memmap_entries;
void *tag_memmap = balloc_aligned(sizeof(struct e820_entry_t) * memmap_entries, 1); void *tag_memmap = conv_mem_alloc_aligned(sizeof(struct e820_entry_t) * memmap_entries, 1);
memcpy(tag_memmap, memmap, sizeof(struct e820_entry_t) * memmap_entries); memcpy(tag_memmap, memmap, sizeof(struct e820_entry_t) * memmap_entries);
append_tag(&stivale2_struct, (struct stivale2_tag *)tag); append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
@ -325,7 +325,7 @@ void stivale2_load(char *cmdline, int boot_drive) {
{ {
struct stivale2_header_tag_smp *smp_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_SMP_ID); struct stivale2_header_tag_smp *smp_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_SMP_ID);
if (smp_hdr_tag != NULL) { if (smp_hdr_tag != NULL) {
struct stivale2_struct_tag_smp *tag = balloc(sizeof(struct stivale2_struct_tag_smp)); struct stivale2_struct_tag_smp *tag = conv_mem_alloc(sizeof(struct stivale2_struct_tag_smp));
tag->tag.identifier = STIVALE2_STRUCT_TAG_SMP_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_SMP_ID;
init_smp((size_t*)&tag->cpu_count, bits == 64, level5pg && level5pg_requested, init_smp((size_t*)&tag->cpu_count, bits == 64, level5pg && level5pg_requested,

View File

@ -4,7 +4,7 @@
#include <lib/real.h> #include <lib/real.h>
#include <lib/blib.h> #include <lib/blib.h>
#include <lib/print.h> #include <lib/print.h>
#include <lib/memmap.h> #include <mm/pmm.h>
struct e820_entry_t *e820_map; struct e820_entry_t *e820_map;
size_t e820_entries; size_t e820_entries;
@ -12,7 +12,7 @@ size_t e820_entries;
void init_e820(void) { void init_e820(void) {
struct rm_regs r = {0}; struct rm_regs r = {0};
e820_map = balloc(sizeof(struct e820_entry_t)); e820_map = conv_mem_alloc(sizeof(struct e820_entry_t));
for (size_t i = 0; ; i++) { for (size_t i = 0; ; i++) {
struct e820_entry_t entry; struct e820_entry_t entry;
@ -34,7 +34,7 @@ void init_e820(void) {
break; break;
} }
balloc(sizeof(struct e820_entry_t)); conv_mem_alloc(sizeof(struct e820_entry_t));
} }
print("E820 memory map:\n"); print("E820 memory map:\n");

View File

@ -8,6 +8,7 @@
#include <sys/smp.h> #include <sys/smp.h>
#include <sys/lapic.h> #include <sys/lapic.h>
#include <mm/vmm64.h> #include <mm/vmm64.h>
#include <mm/pmm.h>
struct madt { struct madt {
struct sdt; struct sdt;
@ -88,7 +89,7 @@ struct smp_information *init_smp(size_t *cpu_count,
struct gdtr gdtr; struct gdtr gdtr;
asm volatile ("sgdt %0" :: "m"(gdtr)); asm volatile ("sgdt %0" :: "m"(gdtr));
struct smp_information *ret = balloc_aligned(0, 1); struct smp_information *ret = conv_mem_alloc_aligned(0, 1);
*cpu_count = 0; *cpu_count = 0;
// Parse the MADT entries // Parse the MADT entries
@ -104,7 +105,7 @@ struct smp_information *init_smp(size_t *cpu_count,
struct madt_lapic *lapic = (void *)madt_ptr; struct madt_lapic *lapic = (void *)madt_ptr;
struct smp_information *info_struct = struct smp_information *info_struct =
balloc_aligned(sizeof(struct smp_information), 1); conv_mem_alloc_aligned(sizeof(struct smp_information), 1);
info_struct->acpi_processor_uid = lapic->acpi_processor_uid; info_struct->acpi_processor_uid = lapic->acpi_processor_uid;
info_struct->lapic_id = lapic->lapic_id; info_struct->lapic_id = lapic->lapic_id;
@ -125,7 +126,7 @@ struct smp_information *init_smp(size_t *cpu_count,
if (!smp_start_ap(lapic->lapic_id, &gdtr, info_struct, if (!smp_start_ap(lapic->lapic_id, &gdtr, info_struct,
longmode, lv5, (uint32_t)pagemap.top_level)) { longmode, lv5, (uint32_t)pagemap.top_level)) {
print("smp: FAILED to bring-up AP\n"); print("smp: FAILED to bring-up AP\n");
brewind(sizeof(struct smp_information)); conv_mem_rewind();
continue; continue;
} }