feat: ajout d'un bitmap pour la gestion fine de la mémoire physique libre
This commit is contained in:
parent
1a28e6ce45
commit
c91c2a1e45
|
@ -3,8 +3,12 @@
|
|||
/* */
|
||||
#include "types.h"
|
||||
|
||||
#define TOPAGE(addr) (addr) >> 12
|
||||
|
||||
|
||||
#define PAGESIZE 4096 /* Taille d'une page */
|
||||
#define PAGENUMBER 1024 /* Nombre de pages */
|
||||
#define KERNELSIZE PAGESIZE*PAGENUMBER*2 /* 2 pages de 4mo en identity mapping */
|
||||
|
||||
#define IDT_ADDR 0x00000000 /* adresse de la IDT */
|
||||
#define GDT_ADDR 0x00000800 /* adresse de la GDT */
|
||||
|
@ -12,6 +16,10 @@
|
|||
#define KERNEL_STACK_ADDR 0x0009FFFF /* adresse de la pile du kernel */
|
||||
#define KERNEL_CODE_ADDR 0x00100000
|
||||
|
||||
/* limites de la mémoire 32 bits */
|
||||
#define MAXMEMSIZE 0x100000000
|
||||
#define MAXMEMPAGE 1024*1024
|
||||
|
||||
/* page directory */
|
||||
#define PAGE_PRESENT 0b000000001/* page directory / table */
|
||||
#define PAGE_WRITE 0b000000010 /* page lecture ecriture */
|
||||
|
|
|
@ -392,7 +392,7 @@
|
|||
u32 getgrubinfo(u8 type);
|
||||
u8 *getgrubinfo_cmdline(void);
|
||||
u32 getgrubinfo_ram(void);
|
||||
struct multiboot_memory_map_t *getgrubinfo_mem(void);
|
||||
struct multiboot_tag_mmap *getgrubinfo_mem(void);
|
||||
struct multiboot_tag_framebuffer *getgrubinfo_fb(void);
|
||||
void getgrubinfo_all(void);
|
||||
void initmultiboot(const u32 addr);
|
||||
|
|
117
lib/memory.c
117
lib/memory.c
|
@ -3,10 +3,121 @@
|
|||
/* */
|
||||
#include "types.h"
|
||||
#include "memory.h"
|
||||
#include "multiboot2.h"
|
||||
|
||||
u32 *pd0 = (u32 *) KERNEL_PGD_ADDR; /* page directory */
|
||||
u8 *pg0 = (u8 *) 0; /* page 0 */
|
||||
u8 *pg1 = (u8 *) (PAGESIZE*PAGENUMBER); /* page 1 */
|
||||
static u32 *pd0 = (u32 *) KERNEL_PGD_ADDR; /* page directory */
|
||||
static u8 *pg0 = (u8 *) 0; /* page 0 */
|
||||
static u8 *pg1 = (u8 *) (PAGESIZE*PAGENUMBER); /* page 1 */
|
||||
static u8 bitmap[MAXMEMPAGE / 8];
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Retourne la taille de la mémoire (selon grub) */
|
||||
|
||||
u64 getmemorysize()
|
||||
{
|
||||
u64 maxaddr=0;
|
||||
struct multiboot_tag_mmap *tag=getgrubinfo_mem();
|
||||
multiboot_memory_map_t *mmap;
|
||||
for (mmap = ((struct multiboot_tag_mmap *) tag)->entries;(u8 *) mmap < (u8 *) tag + tag->size; mmap = (multiboot_memory_map_t *)
|
||||
((unsigned long) mmap + ((struct multiboot_tag_mmap *) tag)->entry_size))
|
||||
if (mmap->addr+mmap->len>maxaddr)
|
||||
maxaddr=mmap->addr+mmap->len;
|
||||
return maxaddr;
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Retourne que la page actuelle est occupée */
|
||||
|
||||
void bitmap_page_use(page)
|
||||
{
|
||||
bitmap[((u32) page)/8] |= (1 << (((u32) page)%8));
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Retourne que la page actuelle est libre */
|
||||
|
||||
void bitmap_page_free(page)
|
||||
{
|
||||
bitmap[((u32) page)/8] &= ~(1 << (((u32) page)%8));
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Reserve un espace mémoire dans le bitmap */
|
||||
|
||||
void bitmap_page_setused(u64 addr,u64 len)
|
||||
{
|
||||
u32 nbpage=TOPAGE(len);
|
||||
u32 pagesrc=TOPAGE(addr);
|
||||
if (len & 0b1111111111 > 0)
|
||||
nbpage++;
|
||||
for(u32 page=pagesrc;page<pagesrc+nbpage;page++)
|
||||
bitmap_page_use(page);
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Indique un espace mémoire libre dans le bitmap */
|
||||
|
||||
void bitmap_page_setfree(u64 addr,u64 len)
|
||||
{
|
||||
u32 nbpage=TOPAGE(len);
|
||||
u32 pagesrc=TOPAGE(addr);
|
||||
if (len & 0b1111111111 > 0)
|
||||
nbpage++;
|
||||
for(u32 page=pagesrc;page<pagesrc+nbpage;page++)
|
||||
bitmap_page_free(page);
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Retourne une page libre */
|
||||
|
||||
u8* bitmap_page_getonefree(void)
|
||||
{
|
||||
u8 byte, bit;
|
||||
u32 page = 0;
|
||||
for (byte = 0; byte < sizeof(bitmap); byte++)
|
||||
if (bitmap[byte] != 0xFF)
|
||||
for (bit = 0; bit < 8; bit++)
|
||||
if (!(bitmap[byte] & (1 << bit))) {
|
||||
page = 8 * byte + bit;
|
||||
bitmap_page_use(page);
|
||||
return (u8 *) (page * PAGESIZE);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Retourne l'espace libre */
|
||||
|
||||
u64 getmemoryfree(void)
|
||||
{
|
||||
u32 byte, bit;
|
||||
u64 free = 0;
|
||||
for (byte = 0; byte < sizeof(bitmap); byte++)
|
||||
if (bitmap[byte] != 0xFF)
|
||||
for (bit = 0; bit < 8; bit++)
|
||||
if (!(bitmap[byte] & (1 << bit)))
|
||||
free+=PAGESIZE;
|
||||
return free;
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Initialisation du bitmap */
|
||||
|
||||
void bitmap_init()
|
||||
{
|
||||
u64 page;
|
||||
for (page=0; page < sizeof(bitmap); page++)
|
||||
bitmap[page] = 0xFF;
|
||||
struct multiboot_tag_mmap *tag=getgrubinfo_mem();
|
||||
multiboot_memory_map_t *mmap;
|
||||
for (mmap = ((struct multiboot_tag_mmap *) tag)->entries;(u8 *) mmap < (u8 *) tag + tag->size; mmap = (multiboot_memory_map_t *)
|
||||
((unsigned long) mmap + ((struct multiboot_tag_mmap *) tag)->entry_size))
|
||||
if (mmap->type==1)
|
||||
bitmap_page_setfree(mmap->addr,mmap->len);
|
||||
else
|
||||
bitmap_page_setused(mmap->addr,mmap->len);
|
||||
bitmap_page_setused(0x0,KERNELSIZE);
|
||||
}
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Initialisation de la mémoire paginée */
|
||||
|
|
|
@ -48,9 +48,9 @@ u32 getgrubinfo_ram(void)
|
|||
/*******************************************************************************/
|
||||
/* Renvoie le plan de la mémoire */
|
||||
|
||||
struct multiboot_memory_map_t *getgrubinfo_mem(void)
|
||||
struct multiboot_tag_mmap *getgrubinfo_mem(void)
|
||||
{
|
||||
struct multiboot_memory_map_t *tag=getgrubinfo(MULTIBOOT_TAG_TYPE_MMAP);
|
||||
struct multiboot_tag_mmap *tag=getgrubinfo(MULTIBOOT_TAG_TYPE_MMAP);
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
|
|
@ -1183,7 +1183,7 @@ u32 format(const u8 * string, va_list args, u32 maxsize, u32 (*fonction)(u8* src
|
|||
bufferend=sitoa(num, &buffer, sizes[asize]);
|
||||
buffersize=bufferend-&buffer[0];
|
||||
counter += fonction(&buffer,&dest,buffersize);
|
||||
counter += fonction(units[unit],&dest,3);
|
||||
counter += fonction(&units[unit],&dest,3);
|
||||
flag = false;
|
||||
break;
|
||||
case 'd':
|
||||
|
|
4
makefile
4
makefile
|
@ -79,13 +79,13 @@ qemu-debug:
|
|||
(killall qemu-system-i386;qemu-system-i386 -m 1G -drive format=raw,file=./final/harddisk.img.final -s -S &)
|
||||
|
||||
qemu-debug64:
|
||||
(killall qemu-system-x86_64;qemu-system-x86_64 -m 1G -drive format=raw,file=./final/harddiskuefi.img.final --bios /usr/share/qemu/OVMF.fd -s -S &)
|
||||
(killall qemu-system-x86_64;qemu-system-x86_64 -m 5G -drive format=raw,file=./final/harddiskuefi.img.final --bios /usr/share/qemu/OVMF.fd -s -S &)
|
||||
|
||||
qemu:
|
||||
(killall qemu-system-i386;qemu-system-i386 -m 1G -drive format=raw,file=./final/harddisk.img.final --enable-kvm -cpu host -s &)
|
||||
|
||||
qemu64:
|
||||
(killall qemu-system-x86_64;qemu-system-x86_64 -m 1G -drive format=raw,file=./final/harddiskuefi.img.final --bios /usr/share/qemu/OVMF.fd --enable-kvm -cpu host -s &)
|
||||
(killall qemu-system-x86_64;qemu-system-x86_64 -m 5G -drive format=raw,file=./final/harddiskuefi.img.final --bios /usr/share/qemu/OVMF.fd --enable-kvm -cpu host -s &)
|
||||
|
||||
qemu-floppy:
|
||||
(killall qemu-system-i386;qemu-system-i386 -m 1G -fda ./final/floppy.img.final --enable-kvm -cpu host -s &)
|
||||
|
|
|
@ -69,6 +69,7 @@ int main(u32 magic, u32 addr)
|
|||
ok();
|
||||
|
||||
print("\033[37m\033[0m -Initilisation de la pagination (PAGING)");
|
||||
bitmap_init();
|
||||
//initpaging();
|
||||
ok();
|
||||
|
||||
|
|
Loading…
Reference in New Issue