Big enhancement, memory detection !

This commit is contained in:
Adrien Bourmault 2019-03-24 20:25:11 +01:00
parent cd3856083f
commit 77ad2ef52c
9 changed files with 186 additions and 38 deletions

View File

@ -171,7 +171,7 @@ $(KOBJDIR)/kernel/malloc.o: $(KERNELDIR)/kernel/mm/malloc.c $(KERNELDIR)/include
.PHONY: test .PHONY: test
test: all test: all
@qemu-system-x86_64 -m 5G -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log & @qemu-system-x86_64 -m 5G -mem-prealloc -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log &
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm
.PHONY: test32 .PHONY: test32

View File

@ -56,15 +56,6 @@
│   │   │   └── loader.o │   │   │   └── loader.o
│   │   └── kaleid │   │   └── kaleid
│   │   ├── kernel │   │   ├── kernel
│   │   │   ├── init
│   │   │   │   ├── init.o
│   │   │   │   └── table.o
│   │   │   ├── io
│   │   │   │   ├── cursor.o
│   │   │   │   ├── term.o
│   │   │   │   └── vga.o
│   │   │   ├── ke
│   │   │   │   └── panic.o
│   │   │   ├── cpuid.o │   │   │   ├── cpuid.o
│   │   │   ├── cursor.o │   │   │   ├── cursor.o
│   │   │   ├── heap.o │   │   │   ├── heap.o
@ -167,4 +158,4 @@
├── qemu.log ├── qemu.log
└── Readme.md └── Readme.md
30 directories, 112 files 27 directories, 106 files

View File

@ -61,6 +61,8 @@ SECTIONS {
*(.rodata) /* all rodata sections from all files */ *(.rodata) /* all rodata sections from all files */
} }
kernelEnd = .;
/DISCARD/ : /DISCARD/ :
{ {
*(.comment) *(.comment)

View File

@ -108,6 +108,7 @@ struct BootInfo_t
void *modulesAddr; //mods_addr void *modulesAddr; //mods_addr
char *grubName; //boot_loader_name char *grubName; //boot_loader_name
void *kernelAddr; void *kernelAddr;
void *kernelEndAddr;
} btldr; } btldr;
// Informations about drives // Informations about drives

View File

@ -22,19 +22,54 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <kernel/multiboot.h>
#include <kernel/base.h> #include <kernel/base.h>
#define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size. #define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size.
// #define AVAILABLE_ZONE 1 // Fully usable RAM zone
// Returns a pointer to the first entry of the memory map #define RESERVED_ZONE 2 // Used by the firmware
// #define ACPI_ZONE 3 // Used by ACPI but can be freed
void *GetMemoryMap(void); #define NVS_ZONE 4 // Dunno
#define BADRAM_ZONE 5 // Invalid zone because material problem...
#define MAX_ENTRIES 2048 // Max number of memory map entries
// -------------------------------------------------------------------------- //
typedef struct MemoryMap_t MemoryMap_t;
typedef struct MapEntry_t MapEntry_t;
// -------------------------------------------------------------------------- //
// The entry structure of the map
struct MapEntry_t {
void *addr;
size_t length; // in bytes
uint type; // reserved or not
} __attribute__((packed));
// the map structure
struct MemoryMap_t {
size_t length;
size_t freeRamSize;
size_t nonfreeRamSize;
MapEntry_t entry[MAX_ENTRIES];
} __attribute__((packed));
// -------------------------------------------------------------------------- //
// //
// Initializes the memory map structure // Initializes the memory map structure
// //
error_t InitMemoryMap(void); error_t InitMemoryMap(void);
//
// Returns the size of the first available memory zone from the start address pointer
//
size_t GetAvailZoneSize(void *start);
//
// Returns the first available memory zone from the start address pointer
void *GetFirstAvailZone(void *start);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //

View File

@ -232,15 +232,12 @@ struct multiboot_color
struct multiboot_mmap_entry struct multiboot_mmap_entry
{ {
uint size; uint size;
ullong addr; uint addr_low;
ullong len; uint addr_high;
#define MULTIBOOT_MEMORY_AVAILABLE 1 uint len_low;
#define MULTIBOOT_MEMORY_RESERVED 2 uint len_high;
#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3
#define MULTIBOOT_MEMORY_NVS 4
#define MULTIBOOT_MEMORY_BADRAM 5
uint type; uint type;
} __attribute__((packed)); } __attribute__((packed)) __attribute__((aligned (4)));
typedef struct multiboot_mmap_entry multiboot_memory_map_t; typedef struct multiboot_mmap_entry multiboot_memory_map_t;
struct multiboot_mod_list struct multiboot_mod_list

View File

@ -22,8 +22,9 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <kernel/term.h> #include <kernel/multiboot.h>
#include <kernel/panic.h> #include <kernel/panic.h>
#include <kernel/term.h>
#include <kernel/mm.h> #include <kernel/mm.h>
// //
@ -32,7 +33,8 @@
// //
void InitBootInfo(multiboot_info_t *mbi) void InitBootInfo(multiboot_info_t *mbi)
{ {
extern void MB_header(void); extern ullong MB_header;
extern ullong kernelEnd;
// We need the multiboot structure // We need the multiboot structure
KalAlwaysAssert(mbi); KalAlwaysAssert(mbi);
@ -43,6 +45,7 @@ void InitBootInfo(multiboot_info_t *mbi)
if (GetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_BOOT_LOADER_NAME) { if (GetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
GetBootInfo(btldr).grubName = (char*)(ullong)(mbi->boot_loader_name); GetBootInfo(btldr).grubName = (char*)(ullong)(mbi->boot_loader_name);
GetBootInfo(btldr).kernelAddr = (void*)&MB_header; GetBootInfo(btldr).kernelAddr = (void*)&MB_header;
GetBootInfo(btldr).kernelEndAddr = (void*)&kernelEnd;
GetBootInfo(btldr).valid = 1; GetBootInfo(btldr).valid = 1;
} }
if (GetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MODS) { if (GetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MODS) {
@ -135,6 +138,11 @@ noreturn void StartKern(multiboot_info_t *mbInfo, int mbMagic)
mapBad mapBad
); );
KernLog("[Init] TEST First zone from %p : %p\n", (void*)0xB8010, GetFirstAvailZone((void*)0xB8010));
KernLog("[Init] TEST Size of zone : %u Kio\n\n", GetAvailZoneSize(GetFirstAvailZone((void*)0xB8010)) / KB);
// We're out // We're out
KernLog("\n[Init] Evil never dies !\n"); KernLog("\n[Init] Evil never dies !\n");
CrashSystem(); //yay CrashSystem(); //yay

View File

@ -23,6 +23,7 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <kernel/heap.h> #include <kernel/heap.h>
#include <kernel/mm.h>
// Least address out of the heap // Least address out of the heap
static void *_heap_end; static void *_heap_end;
@ -33,9 +34,6 @@ static size_t _heap_max;
// Lock NOT used internally, but used by KalAllocMemory() & co. // Lock NOT used internally, but used by KalAllocMemory() & co.
static Lock_t _heap_lock = INITLOCK(KLOCK_SPINLOCK); static Lock_t _heap_lock = INITLOCK(KLOCK_SPINLOCK);
// Debugging stub
size_t GetAvailZoneSize(void *x) { (void)x; return 8 * MB; }
// //
// Initializes heap managment // Initializes heap managment
// //

View File

@ -1,7 +1,7 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// GNU GPL OS/K // // GNU GPL OS/K //
// // // //
// Desc: // // Desc: Mapping and checking memory related functions //
// // // //
// // // //
// Copyright © 2018-2019 The OS/K Team // // Copyright © 2018-2019 The OS/K Team //
@ -24,23 +24,139 @@
#include <kernel/mm.h> #include <kernel/mm.h>
#include <kernel/term.h> #include <kernel/term.h>
#include <kernel/multiboot.h>
MemoryMap_t memoryMap = { 0 };
//
// Initilization of the memory map, and computation of the available ram size
//
error_t InitMemoryMap(void) error_t InitMemoryMap(void)
{ {
multiboot_memory_map_t *currentEntry;
multiboot_memory_map_t *mapEnd;
uint i = 0;
// sanity checks
if (!GetBootInfo(memory).memValid && GetBootInfo(memory).mapValid) if (!GetBootInfo(memory).memValid && GetBootInfo(memory).mapValid)
return ENXIO; return ENXIO;
DebugLog("[InitMemoryMap] Memory map address : %p, length : %d\n",
GetBootInfo(memory).mapAddr, GetBootInfo(memory).mapLength);
if ((GetBootInfo(memory).upMemory / (MB/KB)) <= MINIMUM_RAM_SIZE) if ((GetBootInfo(memory).upMemory / (MB/KB)) <= MINIMUM_RAM_SIZE)
return ENOMEM; return ENOMEM;
DebugLog("[InitMemoryMap] Low memory : %d Kio, Up memory : %d Mio\n",
GetBootInfo(memory).lowMemory, GetBootInfo(memory).upMemory / (MB/KB)); // Ok then we can work ------------------------------------------------------ //
// the memory map provided by GRUB via the BIOS
currentEntry = (multiboot_memory_map_t*)GetBootInfo(memory).mapAddr;
// End address of the map
mapEnd = (multiboot_memory_map_t*)
((ullong)currentEntry + (ullong)GetBootInfo(memory).mapLength);
// fill the map
while (currentEntry < mapEnd) {
// memory zone address
memoryMap.entry[i].addr = (void*)( (ullong)currentEntry->addr_low +
(((ullong)currentEntry->addr_high) << 32 ));
// memory zone size in bytes
memoryMap.entry[i].length = (ullong)currentEntry->len_low +
(((ullong)currentEntry->len_high) << 32);
// memory availability
memoryMap.entry[i].type = (uint)currentEntry->type;
// Adding the size to the size (yup)
memoryMap.length++;
// moving up !
currentEntry = (multiboot_memory_map_t*) ((ullong)currentEntry +
currentEntry->size + sizeof(currentEntry->size));
i++;
}
DebugLog("[InitMemoryMap] %d entries detected in the memory map\n",
memoryMap.length);
// compute the free ram size
for (i = 0; i < memoryMap.length; i++) {
if (memoryMap.entry[i].type == AVAILABLE_ZONE) {
memoryMap.freeRamSize += memoryMap.entry[i].length;
} else {
memoryMap.nonfreeRamSize += memoryMap.entry[i].length;
}
}
// Trully strange if it happens...
if (memoryMap.freeRamSize < MINIMUM_RAM_SIZE)
return ENOMEM;
KernLog("[InitMemoryMap] Available Ram Size : %u Mio, Used Ram Size : %u Kio\n",
memoryMap.freeRamSize / MB, memoryMap.nonfreeRamSize / KB);
KernLog("[InitMemoryMap] Physical Ram Size : %d Mio\n\n",
(memoryMap.freeRamSize + memoryMap.nonfreeRamSize) / MB);
return EOK; return EOK;
} }
void *GetMemoryMap(void) size_t GetAvailZoneSize(void *start) {
{ uint i;
return (void*)0;
// Because the kernel is the kernel
if (start < GetBootInfo(btldr).kernelEndAddr)
return 0;
// Search the zone where the start address is
for (i = 0; i < memoryMap.length; i++) {
// if the address is in an available zone, we can return the length
if (
memoryMap.entry[i].type == AVAILABLE_ZONE &&
(ullong)start >= (ullong)memoryMap.entry[i].addr &&
(ullong)start < ((ullong)memoryMap.entry[i].addr +
(ullong)memoryMap.entry[i].length)
) {
return (size_t)((ullong)memoryMap.entry[i].length - (ullong)start);
}
}
// If there is no zone, we return a 0 size
return 0;
}
void *GetFirstAvailZone(void *start) {
uint i;
void *current = 0;
// Because the kernel is the kernel
if ((ullong)start < (ullong)GetBootInfo(btldr).kernelEndAddr) {
return GetFirstAvailZone(GetBootInfo(btldr).kernelEndAddr);
}
// Search the zone where the start address is
for (i = 0; i < memoryMap.length; i++) {
// if the address is in an available zone, we can return the start address
if (
memoryMap.entry[i].type == AVAILABLE_ZONE &&
(ullong)start >= (ullong)memoryMap.entry[i].addr &&
(ullong)start < ((ullong)memoryMap.entry[i].addr +
(ullong)memoryMap.entry[i].length)
) {
current = start;
break;
}
}
if (current)
return current;
// Search the first zone from start
for (i = 0; i < memoryMap.length; i++) {
// Return the first zone that is after start
if (
memoryMap.entry[i].type == AVAILABLE_ZONE &&
(ullong)start <= (ullong)memoryMap.entry[i].addr
) {
current = memoryMap.entry[i].addr;
break;
}
}
return current;
} }