os-k/kaleid/kernel/mm/map.c

290 lines
9.5 KiB
C
Raw Normal View History

2019-03-18 17:43:54 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-03-24 20:25:11 +01:00
// Desc: Mapping and checking memory related functions //
2019-03-18 17:43:54 +01:00
// //
// //
// Copyright © 2018-2020 The OS/K Team //
2019-03-18 17:43:54 +01:00
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
2020-01-12 16:53:23 +01:00
#include <mm/map.h>
2019-05-13 23:22:27 +02:00
#include <init/boot.h>
#include <init/mboot.h>
2019-05-22 18:15:23 +02:00
#include <io/vga.h>
2019-03-18 17:43:54 +01:00
2019-03-29 10:29:05 +01:00
// Initializes globally the memory map
2019-03-24 21:16:19 +01:00
MemoryMap_t memoryMap = { 0 };
2019-03-24 20:25:11 +01:00
2019-03-29 10:29:05 +01:00
static error_t InitMemoryMap(void);
2019-03-24 20:25:11 +01:00
//
// Initilization of the memory map, and computation of the available ram size
//
2019-03-29 10:29:05 +01:00
void MmInitMemoryMap(void)
{
error_t rc;
2019-05-14 14:39:44 +02:00
rc = InitMemoryMap();
if (rc)
2019-05-18 13:39:58 +02:00
KeStartPanic("Failed to initialize the memory map\nError : %s",
2019-05-14 14:39:44 +02:00
strerror(rc) );
2019-03-29 10:29:05 +01:00
}
static error_t InitMemoryMap(void)
2019-03-18 17:43:54 +01:00
{
2019-03-24 20:25:11 +01:00
multiboot_memory_map_t *currentEntry;
multiboot_memory_map_t *mapEnd;
uint i = 0;
// sanity checks
2019-04-06 07:53:58 +02:00
if (!BtMemoryInfo.memValid && BtMemoryInfo.mapValid)
2019-03-21 13:30:17 +01:00
return ENXIO;
2019-04-06 07:53:58 +02:00
if ((BtMemoryInfo.upMemory / (MB/KB)) <= MINIMUM_RAM_SIZE)
2019-03-21 13:30:17 +01:00
return ENOMEM;
2019-03-24 20:25:11 +01:00
2019-05-07 22:32:17 +02:00
// Ok then we can work
2019-03-24 20:25:11 +01:00
// the memory map provided by GRUB via the BIOS
2019-04-06 07:53:58 +02:00
currentEntry = (multiboot_memory_map_t*)BtMemoryInfo.mapAddr;
2019-03-24 20:25:11 +01:00
// End address of the map
mapEnd = (multiboot_memory_map_t*)
2019-04-06 07:53:58 +02:00
((ulong)currentEntry + (ulong)BtMemoryInfo.mapLength);
2019-03-24 20:25:11 +01:00
2020-02-06 17:45:44 +01:00
DebugLog("Initiliazing memory map...\n");
2019-03-24 20:25:11 +01:00
// fill the map
while (currentEntry < mapEnd) {
2019-04-01 17:58:58 +02:00
2019-03-24 20:25:11 +01:00
// memory zone address
2019-04-01 17:58:58 +02:00
memoryMap.entry[i].addr = (void*)((ullong)currentEntry->addr_low +
(((ullong)currentEntry->addr_high) << 32 ));
2019-03-24 20:25:11 +01:00
// memory zone size in bytes
2019-03-25 17:33:51 +01:00
memoryMap.entry[i].length = (ulong)currentEntry->len_low +
(((ulong)currentEntry->len_high) << 32);
2019-03-24 20:25:11 +01:00
// memory availability
memoryMap.entry[i].type = (uint)currentEntry->type;
// Adding the size to the size (yup)
memoryMap.length++;
2020-02-06 17:45:44 +01:00
DebugLog("Zone: %lp type %d with length: %4luMB+%4luKB+%4luB\n",
memoryMap.entry[i].addr,
memoryMap.entry[i].type,
_ADDR_TO_MB(memoryMap.entry[i].length),
_ADDR_TO_KB(memoryMap.entry[i].length),
_ADDR_TO_B(memoryMap.entry[i].length)
);
2019-03-24 20:25:11 +01:00
// moving up !
2019-03-25 17:33:51 +01:00
currentEntry = (multiboot_memory_map_t*) ((ulong)currentEntry +
currentEntry->size + sizeof(currentEntry->size));
2019-03-24 20:25:11 +01:00
i++;
}
// 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;
2020-02-06 13:18:22 +01:00
DebugLog("Available RAM size : %u MB\n",
2019-05-14 14:39:44 +02:00
memoryMap.freeRamSize / MB);
2019-03-21 13:30:17 +01:00
2019-03-19 13:38:09 +01:00
return EOK;
2019-03-18 17:43:54 +01:00
}
2020-01-15 01:34:46 +01:00
size_t MmGetAvailZoneSize(void *start)
{
2019-03-24 20:25:11 +01:00
uint i;
// Because the kernel is the kernel
2019-05-13 15:18:00 +02:00
if (start < BtLoaderInfo.stackEndAddr + 16)
2019-03-24 20:25:11 +01:00
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 &&
2019-03-25 17:33:51 +01:00
(ulong)start >= (ulong)memoryMap.entry[i].addr &&
(ulong)start < ((ulong)memoryMap.entry[i].addr +
(ulong)memoryMap.entry[i].length)
2019-03-24 20:25:11 +01:00
) {
2019-03-25 17:33:51 +01:00
return (size_t)((ulong)memoryMap.entry[i].length - (ulong)start);
2019-03-24 20:25:11 +01:00
}
2020-01-14 00:37:30 +01:00
}
// If there is no zone, we return a 0 size
return 0;
}
bool MmIsBusyZone(void *start)
{
uint i;
// Because the kernel is the kernel
if (start < BtLoaderInfo.stackEndAddr + 16)
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 &&
(ulong)start >= (ulong)memoryMap.entry[i].addr &&
(ulong)start < ((ulong)memoryMap.entry[i].addr +
(ulong)memoryMap.entry[i].length)
) {
return 1;
}
}
// The zone is free
return 0;
}
2020-01-15 01:34:46 +01:00
bool MmIsFailingZoneSize(void *start)
{
2020-01-14 00:37:30 +01:00
uint i;
// Because the kernel is the kernel
if (start < BtLoaderInfo.stackEndAddr + 16)
return 0;
// Search the zone where the start address is
for (i = 0; i < memoryMap.length; i++) {
// if the address is in a failing zone, we can return 1
if (
(memoryMap.entry[i].type == BADRAM_ZONE) &&
(ulong)start >= (ulong)memoryMap.entry[i].addr &&
(ulong)start < ((ulong)memoryMap.entry[i].addr +
(ulong)memoryMap.entry[i].length)
) {
return 1;
}
2019-03-24 20:25:11 +01:00
}
// If there is no zone, we return a 0 size
return 0;
}
2020-01-15 01:34:46 +01:00
void *MmGetFirstAvailZone(void *start)
{
2019-03-24 20:25:11 +01:00
uint i;
void *current = 0;
// Because the kernel is the kernel
2019-05-15 15:55:57 +02:00
if ((ulong)start < (ulong)BtLoaderInfo.stackEndAddr+4096) {
return MmGetFirstAvailZone(BtLoaderInfo.stackEndAddr+4096);
2019-03-24 20:25:11 +01:00
}
// 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 &&
2019-03-25 17:33:51 +01:00
(ulong)start >= (ulong)memoryMap.entry[i].addr &&
(ulong)start < ((ulong)memoryMap.entry[i].addr +
(ulong)memoryMap.entry[i].length)
2019-03-24 20:25:11 +01:00
) {
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 &&
2019-03-25 17:33:51 +01:00
(ulong)start <= (ulong)memoryMap.entry[i].addr
2019-03-24 20:25:11 +01:00
) {
current = memoryMap.entry[i].addr;
break;
}
}
return current;
2019-03-18 17:43:54 +01:00
}
2019-03-30 14:58:12 +01:00
2020-01-15 01:34:46 +01:00
void MmPrintMemoryMap(void)
{
2019-05-22 18:15:23 +02:00
char avStr[15];
extern int shcol;
2019-03-30 14:58:12 +01:00
2019-04-01 12:02:17 +02:00
for (uint i=0; i < memoryMap.length; i++) {
2019-03-30 14:58:12 +01:00
switch (memoryMap.entry[i].type) {
2019-05-22 18:15:23 +02:00
case AVAILABLE_ZONE: snprintf(avStr, 15, "%CAvailable%C",
VGA_COLOR_GREEN,
shcol);
2019-03-30 14:58:12 +01:00
break;
2019-05-22 18:15:23 +02:00
case RESERVED_ZONE: snprintf(avStr, 15, "%CReserved %C",
VGA_COLOR_RED,
shcol);
2019-03-30 14:58:12 +01:00
break;
2019-05-22 18:15:23 +02:00
case ACPI_ZONE: snprintf(avStr, 15, "%CACPI %C",
VGA_COLOR_LIGHT_BROWN,
shcol);
2019-03-30 14:58:12 +01:00
break;
2019-05-22 18:15:23 +02:00
case NVS_ZONE: snprintf(avStr, 15, "%CNVS %C",
VGA_COLOR_LIGHT_BROWN,
shcol);
2019-03-30 14:58:12 +01:00
break;
2019-05-22 18:15:23 +02:00
case BADRAM_ZONE: snprintf(avStr, 15, "%CBAD RAM %C",
VGA_COLOR_LIGHT_RED,
shcol);
2019-03-30 14:58:12 +01:00
break;
default:;
}
2019-05-08 20:45:28 +02:00
ulong len = memoryMap.entry[i].length;
2019-05-22 18:15:23 +02:00
KernLog("%Cmem zone:%C %lp %s %Cwith length:%C %4lu%CMB"
"+%C%4lu%CKB+%C%4lu%CB\n",
VGA_COLOR_DARK_GREY,
shcol,
2019-05-08 20:45:28 +02:00
memoryMap.entry[i].addr, avStr,
2019-05-22 18:15:23 +02:00
VGA_COLOR_DARK_GREY,
shcol,
_ADDR_TO_MB(len),
VGA_COLOR_DARK_GREY,
shcol,
_ADDR_TO_KB(len),
VGA_COLOR_DARK_GREY,
shcol,
_ADDR_TO_B(len),
VGA_COLOR_DARK_GREY,
shcol
2019-03-30 14:58:12 +01:00
);
}
2019-05-22 18:15:23 +02:00
KernLog("\n");
2019-03-30 14:58:12 +01:00
}