2020-04-17 17:44:23 +02:00

224 lines
5.0 KiB
C

/* memory.h - This file is a part of NutsOS
*
* NutsOS
* Copyright (C) 2013 Free Software Foundation, Inc.
*
* NutsOS 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
* (at your option) any later version.
*
* NutsOS 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 NutsOS; If not, see <http://www.gnu.org/licenses/>.
*
* Valentin Verdier <valentin.verdier03@gmail.com>
*/
#pragma once
#ifndef MEMORY_H
#define MEMORY_H
#include "types.h"
/* ###### Main Memory Management ###### */
#define KERNEL_SPACE_SIZE 0x10000000 /* Mémoire potentiellement utilisable par le kernel, adresse virtuelle de fin de l'espace noyau, aligné sur 4 Mo */
/* Configation dynamique */
#define KERNEL_SPACE_PT_COUNT (KERNEL_SPACE_SIZE / (PAGE_PER_PT * PAGE_SIZE))
#define KERNEL_SPACE_PAGE_COUNT (KERNEL_SPACE_PT_COUNT * PAGE_PER_PT)
#define KERNEL_PHY_SPACE_SIZE (KERNEL_PT_ADDR + (KERNEL_SPACE_PT_COUNT * PT_SIZE))
#define KERNEL_PHY_SPACE_PAGE_COUNT (KERNEL_PHY_SPACE_SIZE / PAGE_SIZE)
#define KERNEL_VM_SIZE (KERNEL_SPACE_SIZE - KERNEL_PHY_SPACE_SIZE)
#define KERNEL_VM_PAGE_COUNT (KERNEL_VM_SIZE / PAGE_SIZE)
/* ----------------------- */
/* Configation statique */
#define KERNEL_PD_ADDR 0x303000
#define KERNEL_PT_ADDR 0x304000
#define PD_SIZE 4096
#define PT_SIZE 4096
#define PAGE_SIZE 4096
#define MAX_PT_COUNT 1024
#define MAX_PAGE_COUNT 1048576
#define PAGE_OFFSET_BITS 12
#define PAGE_PER_PT 1024
/* ----------------------- */
/* Special types */
typedef T_dword T_page;
typedef T_Page* T_pageTable;
typedef T_pageTable* T_pageDir;
/* ----------------------- */
#ifdef MEMORY_PRIVATE
T_PageDirectory kpageDir;
T_PageTable kageTable;
unsigned int KernPageTableMutex;
#endif
void kinitSpace();
unsigned int kaddPage(T_Page);
void kremovePage(T_Page);
T_Page kgetPage();
void* kgetPhyAddr(void*);
/* ##################################### */
/* ###### Segment Management ###### */
#define GDT_BASE 0x200000
#define SEG_DESC_SIZE 8
#define SEG_DESC_NUMBER 256
#define MAIN_KERNEL_STACK 0xA0000
#define TSS_BASE 0x201000
struct MemSegDescriptor
{
bits16 limite0_15;
bits16 base0_15;
bits8 base16_23;
bits8 acces;
bits8 limite16_19:4;
bits8 other:4;
bits8 base24_31;
}__attribute__((packed));
struct GdtReg
{
bits16 limite;
bits32 base;
}__attribute__((packed));
struct TaskStateSegment
{
bits16 prevTask;
bits16 reserved_1;
bits32 esp0;
bits16 ss0;
bits16 reserved_2;
bits32 esp1;
bits16 ss1;
bits16 reserved_3;
bits32 esp2;
bits16 ss2;
bits16 reserved_4;
bits32 cr3;
bits32 eip;
bits32 eflags;
bits32 eax;
bits32 ecx;
bits32 edx;
bits32 ebx;
bits32 esp;
bits32 ebp;
bits32 esi;
bits32 edi;
bits16 es;
bits16 reserved_5;
bits16 cs;
bits16 reserved_6;
bits16 ss;
bits16 reserved_7;
bits16 ds;
bits16 reserved_8;
bits16 fs;
bits16 reserved_9;
bits16 gs;
bits16 reserved_10;
bits16 ldtSegSelect;
bits16 reserved_11;
bits16 flagDebug;
bits16 io_map;
}__attribute__((packed));
/* Special types */
typedef struct MemSegDescriptor T_GdtDesc;
typedef struct GdtReg T_GDT;
typedef struct TaskStateSegment T_TSS;
/* ----------------------- */
T_TSS *tss;
void InitGDT();
void CreateSegDescriptor(bits32, bits32, bits8, bits8, unsigned int);
/* ##################################### */
/* ###### Page Management ###### */
#ifdef MEMORY_PRIVATE
#define PHY_PAGE_MAP_ADDR 0x203000
#define PAGE_FREE 0
#define PAGE_USED 1
#define PAGE_NOT_PRESENT 4
bits8 *PageMap;
unsigned int PhyPageMapMutex;
#endif
#define PAGE_FLAG_PRESENT 1
#define PAGE_FLAG_RW 2
#define PAGE_FLAG_USER 4
T_Page GetPhyPage();
void ReleasePhyPage(T_Page);
/* ##################################### */
/* ###### Alloc Management ###### */
#ifdef MEMORY_PRIVATE
#define FREE 0
#define USED 1
unsigned int KERNEL_HEAP_BASE;
unsigned int KERNEL_HEAP_LIMIT;
struct AllocAreaModel {
T_byte state; /* 1:used 0:free */
struct AllocAreaModel *next;
}__attribute__((packed));
unsigned int AllocMutex;
#endif
void* malloc(unsigned int);
void free(void*);
struct AllocAreaModel* EnlargeHeap();
/* ##################################### */
/* ###### Paging Management ###### */
T_PageDirectory CreateTaskPageDirectory();
void DestroyTaskPageDirectory(T_PageDirectory);
unsigned int AddPage(T_Page, bits32);
void RemovePage(T_Page);
unsigned int LinkPhyPage(T_Page, T_Page, bits32);
void UnlinkPhyPage(T_Page);
void* GetPhyAddr(void*);
/* ##################################### */
#endif