os-k/include/mm/mm.h

214 lines
5.9 KiB
C
Raw Normal View History

2019-03-14 18:33:12 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-05-13 23:22:27 +02:00
// Desc: Memory manager functions //
2019-03-14 18:33:12 +01:00
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// 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/>. //
//----------------------------------------------------------------------------//
2019-03-18 17:43:41 +01:00
2019-05-13 23:22:27 +02:00
#ifndef _KERNEL_H
#include <kernel.h>
2019-03-29 10:29:05 +01:00
#endif
2019-03-18 17:43:41 +01:00
2019-05-13 23:22:27 +02:00
#ifndef _MM_MM_H
#define _MM_MM_H
//----------------------------------------------------------------------------//
2019-03-24 20:25:11 +01:00
#define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size.
2019-05-19 23:33:34 +02:00
#define IOMAP_SIZE (8 * 1024)
2019-03-24 20:25:11 +01:00
#define AVAILABLE_ZONE 1 // Fully usable RAM zone
#define RESERVED_ZONE 2 // Used by the firmware
#define ACPI_ZONE 3 // Used by ACPI but can be freed
#define NVS_ZONE 4 // Dunno
#define BADRAM_ZONE 5 // Invalid zone because material problem...
#define MAX_ENTRIES 2048 // Max number of memory map entries
2019-05-22 00:38:04 +02:00
#define KPAGESIZE (4 * KB)
2020-01-08 00:28:10 +01:00
#define UPAGESIZE (4 * KB)
2019-03-24 20:25:11 +01:00
//----------------------------------------------------------------------------//
2019-03-24 20:25:11 +01:00
// The entry structure of the map
struct MapEntry_t {
void *addr;
size_t length; // in bytes
uint type; // reserved or not
2019-03-25 17:33:51 +01:00
} __attribute__((__packed__));
2019-03-24 20:25:11 +01:00
// the map structure
struct MemoryMap_t {
size_t length;
size_t freeRamSize;
size_t nonfreeRamSize;
MapEntry_t entry[MAX_ENTRIES];
2019-03-25 17:33:51 +01:00
} __attribute__((__packed__));
2019-03-21 13:30:17 +01:00
2019-05-19 23:33:34 +02:00
// The gdt entry
2019-03-26 17:50:03 +01:00
struct GdtEntry_t
{
2019-05-13 18:07:45 +02:00
ushort lowLimit; // lower 16 bits
ushort lowBase; // lower 16 bits
uchar middleBase; // next 8 bits
uchar access; // determine what ring this segment can be used in
2019-05-19 23:33:34 +02:00
uchar flags;
2019-05-13 18:07:45 +02:00
uchar highBase; // last 8 bits
2019-04-05 10:20:10 +02:00
} __attribute__((__packed__));
2019-03-24 20:25:11 +01:00
2019-05-13 18:07:45 +02:00
struct TssDescriptor_t
{
2019-05-22 00:38:04 +02:00
ushort lowLimit;
ushort lowBase;
uchar middleBase;
uchar access;
uchar flags;
uchar highBase;
uint veryHighBase;
uint reserved;
2019-05-13 18:07:45 +02:00
} __attribute__ ((packed));
2019-05-19 23:33:34 +02:00
struct Tss_t
2019-05-13 18:07:45 +02:00
{
2019-05-19 23:33:34 +02:00
uint reserved1;
// Stack pointers (RSP) for privilege levels 0-2
ulong rsp0;
ulong rsp1;
ulong rsp2;
ulong reserved2;
// Interrupt stack table pointers
ulong ist1;
ulong ist2;
ulong ist3;
ulong ist4;
ulong ist5;
ulong ist6;
ulong ist7;
ulong reserved3;
ushort reserved4;
// Offset to the I/O permission bit map from the 64-bit TSS base
ushort iomap_base;
uchar iomap[IOMAP_SIZE];
2019-05-13 18:07:45 +02:00
2020-01-05 20:13:53 +01:00
} __attribute__ ((packed)) __attribute__((aligned(8)));
2019-05-13 18:07:45 +02:00
// The gdt pointer
2019-03-26 17:50:03 +01:00
struct GdtPtr_t
{
2019-05-20 20:28:18 +02:00
ushort limit; // upper 16 bits
2019-05-19 23:33:34 +02:00
ulong base; // address of the first entry
} __attribute__((__packed__));
2019-03-24 20:25:11 +01:00
//----------------------------------------------------------------------------//
2019-03-18 17:43:41 +01:00
//
// Initializes the memory map structure
//
2019-03-29 10:29:05 +01:00
void MmInitMemoryMap(void);
2019-03-18 17:43:41 +01:00
2019-03-30 14:58:12 +01:00
//
// Initializes the memory map structure
//
void MmPrintMemoryMap(void);
2019-03-24 20:25:11 +01:00
//
2019-03-25 17:33:51 +01:00
// Returns the size of the first available memory zone
// from the start address pointer
2019-03-24 20:25:11 +01:00
//
2019-03-24 21:16:19 +01:00
size_t MmGetAvailZoneSize(void *start);
2019-03-24 20:25:11 +01:00
//
// Returns the first available memory zone from the start address pointer
2019-03-26 17:50:03 +01:00
//
2019-03-24 21:24:21 +01:00
void *MmGetFirstAvailZone(void *start);
2019-03-24 20:25:11 +01:00
2019-03-26 17:50:03 +01:00
//
// Initializes the descriptor table
2019-03-26 17:50:03 +01:00
//
void MmInitGdt(void);
//
// Loads the descriptor table
//
2019-05-22 00:38:04 +02:00
extern void MmLoadGdt(GdtPtr_t *gdtPtr, ushort tssOffset);
2019-05-13 18:07:45 +02:00
//
// Stores the descriptor table
//
extern void MmStoreGdt(void);
2020-01-09 20:58:57 +01:00
//
// Paging misc
//
2019-05-23 14:50:05 +02:00
void MmInitPaging(void);
void MmActivatePageHandler(void);
2019-05-22 08:11:50 +02:00
//
// Returns the address of the stack guard pages
//
void *MmGetStackGuards(char rank);
//
2020-01-09 20:58:57 +01:00
// Translate a virtual address into physical address and the opposite
2019-05-22 08:11:50 +02:00
//
2020-01-09 20:58:57 +01:00
void *MmTransVirtToPhyAddr(void*);
void *MmTransPhyToVirtAddr(void* virtualAddr);
2019-05-22 08:11:50 +02:00
//
// Set flags to a page
//
void MmSetPage(void* virtualAddr, ulong flags);
void MmUnSetPage(void* virtualAddr, ulong flags);
//
// Map a page
//
void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags);
void MmUnmapPage(void* virtualAddr);
2020-01-10 22:41:25 +01:00
// Allocations
void *MmGetPhyPageBlock(size_t size, bool usermode);
2020-01-09 18:19:49 +01:00
// Page table entry
typedef ulong pte_t;
2019-11-16 22:41:46 +01:00
// Page directory offset
2020-01-09 18:19:49 +01:00
typedef pte_t* pde_t;
2019-11-16 22:41:46 +01:00
2020-01-09 18:19:49 +01:00
// Page directory pointer offset
typedef pde_t* pdpe_t;
// Page directory L4 pointer offset
typedef pdpe_t* pml4_t;
2019-11-16 22:41:46 +01:00
// paging.asm
void MmLoadPML4(void *);
void MmEnableWriteProtect(void);
void MmDisableWriteProtect(void);
//----------------------------------------------------------------------------//
#endif