2018-09-28 20:35:51 +02:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* COS2000 - Compatible Operating System - LGPL v3 - Hord<72> Nicolas */
|
|
|
|
|
/* */
|
2007-04-02 16:34:45 +02:00
|
|
|
|
#include "types.h"
|
2018-10-02 02:16:14 +02:00
|
|
|
|
#include "memory.h"
|
2018-11-16 15:57:34 +01:00
|
|
|
|
#include "multiboot2.h"
|
2018-11-22 17:21:15 +01:00
|
|
|
|
#include "queue.h"
|
|
|
|
|
#include "asm.h"
|
2018-10-02 02:16:14 +02:00
|
|
|
|
|
2018-11-29 13:42:07 +01:00
|
|
|
|
static u8 *kernelcurrentheap=NULL; /* pointeur vers le heap noyau */
|
2018-11-22 17:21:15 +01:00
|
|
|
|
static u8 bitmap[MAXMEMPAGE / 8]; /* bitmap */
|
2018-11-25 23:34:38 +01:00
|
|
|
|
static vrange_t vrange_head;
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Erreur fatale */
|
|
|
|
|
void panic(u8 *string)
|
|
|
|
|
{
|
|
|
|
|
printf("KERNEL PANIC: %s\r\nSysteme arrete...\n");
|
|
|
|
|
halt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Alloue plusieurs pages virtuelles (size) pour le heap du noyau */
|
|
|
|
|
|
|
|
|
|
tmalloc *mallocpage(u8 size)
|
|
|
|
|
{
|
|
|
|
|
tmalloc *chunk;
|
|
|
|
|
u8 *paddr;
|
|
|
|
|
u32 realsize=size * PAGESIZE;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
if ((kernelcurrentheap - KERNEL_HEAP + realsize) > MAXHEAPSIZE)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
panic("Plus de memoire noyau heap disponible a allouer !\n");
|
2018-11-29 13:42:07 +01:00
|
|
|
|
chunk = (tmalloc *) kernelcurrentheap;
|
|
|
|
|
virtual_range_new_kernel(kernelcurrentheap, realsize);
|
|
|
|
|
kernelcurrentheap += realsize;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
chunk->size = realsize;
|
|
|
|
|
chunk->used = 0;
|
|
|
|
|
return chunk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Alloue de la m<>moire virtuelle au noyau de fa<66>on dynamique (heap) */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
void *vmalloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
u32 realsize;
|
|
|
|
|
tmalloc *chunk, *new;
|
|
|
|
|
realsize = sizeof(tmalloc) + size;
|
|
|
|
|
if (realsize < MALLOC_MINIMUM)
|
|
|
|
|
realsize = MALLOC_MINIMUM;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
chunk = KERNEL_HEAP;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
while (chunk->used || chunk->size < realsize) {
|
|
|
|
|
if (chunk->size == 0)
|
2018-11-29 13:42:07 +01:00
|
|
|
|
panic(sprintf("Element du heap %x defectueux avec une taille nulle (heap %x) !",chunk, kernelcurrentheap));
|
2018-11-22 17:21:15 +01:00
|
|
|
|
chunk = chunk + chunk->size;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
if (chunk == (tmalloc *) kernelcurrentheap)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
mallocpage((realsize / PAGESIZE) + 1);
|
2018-11-29 13:42:07 +01:00
|
|
|
|
else if (chunk > (tmalloc *) kernelcurrentheap)
|
|
|
|
|
panic (sprintf("Element du heap %x depassant la limite %x !",chunk, kernelcurrentheap));
|
2018-11-22 17:21:15 +01:00
|
|
|
|
}
|
|
|
|
|
if (chunk->size - realsize < MALLOC_MINIMUM)
|
|
|
|
|
chunk->used = 1;
|
|
|
|
|
else {
|
|
|
|
|
new = chunk + realsize;
|
|
|
|
|
new->size = chunk->size - realsize;
|
|
|
|
|
new->used = 0;
|
|
|
|
|
chunk->size = realsize;
|
|
|
|
|
chunk->used = 1;
|
|
|
|
|
}
|
|
|
|
|
return (u8 *) chunk + sizeof(tmalloc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Lib<69>re de la m<>moire virtuelle depuis le heap noyau */
|
|
|
|
|
|
2018-11-23 09:00:59 +01:00
|
|
|
|
void vfree(void *vaddr)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
{
|
|
|
|
|
tmalloc *chunk, *new;
|
|
|
|
|
chunk = (tmalloc *) (vaddr - sizeof(tmalloc));
|
|
|
|
|
chunk->used = 0;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
while ((new = (tmalloc *) chunk + chunk->size) && new < (tmalloc *) kernelcurrentheap && new->used == 0)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
chunk->size += new->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Retourne la taille de la m<>moire physique (selon grub) */
|
|
|
|
|
|
|
|
|
|
u64 physical_getmemorysize()
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Retourne que la page physique actuelle est occup<75>e */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
void physical_page_use(u32 page)
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
2018-11-22 17:21:15 +01:00
|
|
|
|
bitmap[(page/8)] |= (1 << (page%8));
|
2018-11-16 15:57:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Retourne que la page physique actuelle est libre */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
void physical_page_free(u32 page)
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
2018-11-22 17:21:15 +01:00
|
|
|
|
bitmap[(page/8)] &= ~(1 << (page%8));
|
2018-11-16 15:57:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Reserve un espace m<>moire physique dans le bitmap */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
void physical_range_use(u64 addr,u64 len)
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
|
|
|
|
u32 nbpage=TOPAGE(len);
|
|
|
|
|
u32 pagesrc=TOPAGE(addr);
|
|
|
|
|
if (len & 0b1111111111 > 0)
|
|
|
|
|
nbpage++;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
if (addr>=MAXMEMSIZE)
|
2018-11-21 17:25:00 +01:00
|
|
|
|
return;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
if (addr+len>=MAXMEMSIZE)
|
|
|
|
|
len=MAXMEMSIZE-addr-1;
|
2018-11-16 15:57:34 +01:00
|
|
|
|
for(u32 page=pagesrc;page<pagesrc+nbpage;page++)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
physical_page_use(page);
|
2018-11-16 15:57:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Lib<69>re un espace m<>moire physique dans le bitmap */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
void physical_range_free(u64 addr,u64 len)
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
|
|
|
|
u32 nbpage=TOPAGE(len);
|
|
|
|
|
u32 pagesrc=TOPAGE(addr);
|
|
|
|
|
if (len & 0b1111111111 > 0)
|
2018-11-21 17:25:00 +01:00
|
|
|
|
nbpage++;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
if (addr>=MAXMEMSIZE)
|
2018-11-21 17:25:00 +01:00
|
|
|
|
return;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
if (addr+len>=MAXMEMSIZE)
|
|
|
|
|
len=MAXMEMSIZE-addr-1;
|
2018-11-16 15:57:34 +01:00
|
|
|
|
for(u32 page=pagesrc;page<pagesrc+nbpage;page++)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
physical_page_free(page);
|
2018-11-16 15:57:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Retourne une page physique libre */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
u8* physical_page_getfree(void)
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
2018-11-27 16:56:21 +01:00
|
|
|
|
u32 byte;
|
|
|
|
|
u8 bit;
|
2018-11-16 15:57:34 +01:00
|
|
|
|
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;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
physical_page_use(page);
|
2018-11-16 15:57:34 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Initialisation du bitmap pour la gestion physique de la m<>moire */
|
2018-11-16 15:57:34 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
void physical_init(void)
|
2018-11-16 15:57:34 +01:00
|
|
|
|
{
|
|
|
|
|
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)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
physical_range_free(mmap->addr,mmap->len);
|
2018-11-16 15:57:34 +01:00
|
|
|
|
else
|
2018-11-22 17:21:15 +01:00
|
|
|
|
physical_range_use(mmap->addr,mmap->len);
|
2018-11-27 16:56:21 +01:00
|
|
|
|
physical_range_use(0x0,KERNELSIZE);
|
2018-11-22 17:21:15 +01:00
|
|
|
|
}
|
2018-11-27 16:56:21 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/*******************************************************************************/
|
2018-11-27 16:56:21 +01:00
|
|
|
|
/* Retourne une page virtuelle de m<>moire */
|
2018-11-25 23:34:38 +01:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
page *virtual_page_getfree(void)
|
|
|
|
|
{
|
|
|
|
|
page *pg;
|
|
|
|
|
vrange *vpages;
|
|
|
|
|
u8 *vaddr, *paddr;
|
|
|
|
|
paddr = physical_page_getfree();
|
|
|
|
|
if (paddr == NULL)
|
|
|
|
|
panic ("Plus de memoire physique disponible !\n");
|
2018-11-25 23:34:38 +01:00
|
|
|
|
if (TAILQ_EMPTY(&vrange_head))
|
2018-11-23 09:00:59 +01:00
|
|
|
|
panic("Plus de place disponible dans la reserve de page !\n");
|
2018-11-25 23:34:38 +01:00
|
|
|
|
vpages = TAILQ_FIRST(&vrange_head);
|
2018-11-22 17:21:15 +01:00
|
|
|
|
vaddr = vpages->vaddrlow;
|
|
|
|
|
vpages->vaddrlow += PAGESIZE;
|
2018-11-23 09:00:59 +01:00
|
|
|
|
if (vpages->vaddrlow == vpages->vaddrhigh) {
|
2018-11-25 23:34:38 +01:00
|
|
|
|
TAILQ_REMOVE(&vrange_head, vpages, tailq);
|
2018-11-22 17:21:15 +01:00
|
|
|
|
vfree(vpages);
|
|
|
|
|
}
|
2018-11-27 16:56:21 +01:00
|
|
|
|
virtual_pd_page_add(NULL,vaddr,paddr, 0);
|
2018-11-22 17:21:15 +01:00
|
|
|
|
pg = (page*) vmalloc(sizeof(page));
|
|
|
|
|
pg->vaddr = vaddr;
|
|
|
|
|
pg->paddr = paddr;
|
|
|
|
|
return pg;
|
2018-11-16 15:57:34 +01:00
|
|
|
|
}
|
2018-10-02 02:16:14 +02:00
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/* Cr<43>ation d'un directory pour la gestion virtuelle de la m<>moire */
|
|
|
|
|
|
|
|
|
|
pd *virtual_pd_create()
|
|
|
|
|
{
|
|
|
|
|
pd *new;
|
2018-11-23 09:00:59 +01:00
|
|
|
|
u32 *pdir,*pd0;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
u32 i;
|
2018-11-23 09:00:59 +01:00
|
|
|
|
new = (pd *) vmalloc(sizeof(pd));
|
|
|
|
|
new->addr = virtual_page_getfree();
|
2018-11-27 16:56:21 +01:00
|
|
|
|
pdir = (u32 *) new->addr->vaddr;
|
|
|
|
|
pd0 = (u32 *) KERNEL_PD_ADDR;
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
|
pdir[i] = pd0[i];
|
|
|
|
|
for (i = 256; i < 1023; i++)
|
|
|
|
|
pdir[i] = 0;
|
|
|
|
|
pdir[1023] = ((u32) new->addr->paddr | (PAGE_PRESENT | PAGE_WRITE));
|
2018-11-25 23:34:38 +01:00
|
|
|
|
TAILQ_INIT(&new->page_head);
|
2018-11-23 09:00:59 +01:00
|
|
|
|
return new;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
}
|
2018-11-23 09:00:59 +01:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Attache une page virtuelle de la m<>moire dans le directory sp<73>cifi<66> */
|
|
|
|
|
|
|
|
|
|
void virtual_pd_page_add(pd *dst, u8* vaddr, u8 * paddr, u32 flags)
|
|
|
|
|
{
|
|
|
|
|
u32 *pdir;
|
|
|
|
|
u32 *ptable;
|
|
|
|
|
u32 *pt;
|
|
|
|
|
page *pg;
|
|
|
|
|
int i;
|
|
|
|
|
if (dst==NULL)
|
|
|
|
|
if (vaddr > (u8 *) USER_CODE) {
|
|
|
|
|
print("ERREUR: Adresse %X n'est pas dans l'espace noyau !\n", vaddr);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
pdir = (u32 *) (0xFFFFF000 | (((u32) vaddr & 0xFFC00000) >> 20));
|
|
|
|
|
if ((*pdir & PAGE_PRESENT) == 0) {
|
|
|
|
|
if (dst==NULL)
|
|
|
|
|
panic(sprintf("Page table introuvable pour l'adresse %x !\r\n", vaddr));
|
|
|
|
|
pg = virtual_page_getfree();
|
|
|
|
|
pt = (u32 *) pg->vaddr;
|
|
|
|
|
for (i = 1; i < 1024; i++)
|
|
|
|
|
pt[i] = 0;
|
|
|
|
|
*pdir = (u32) pg->paddr | (PAGE_PRESENT | PAGE_WRITE | flags);
|
|
|
|
|
if (dst)
|
|
|
|
|
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
|
|
|
|
|
}
|
|
|
|
|
ptable = (u32 *) (0xFFC00000 | (((u32) vaddr & 0xFFFFF000) >> 10));
|
|
|
|
|
*ptable = ((u32) paddr) | (PAGE_PRESENT | PAGE_WRITE | flags);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Retire une page virtuelle de la m<>moire dans le directory sp<73>cifi<66> */
|
|
|
|
|
|
|
|
|
|
void virtual_pd_page_remove(u8* vaddr)
|
|
|
|
|
{
|
|
|
|
|
u32 *ptable;
|
|
|
|
|
if (virtual_to_physical(vaddr)) {
|
|
|
|
|
ptable = (u32 *) (0xFFC00000 | (((u32) vaddr & 0xFFFFF000) >> 10));
|
|
|
|
|
*ptable = (*ptable & (~PAGE_PRESENT));
|
|
|
|
|
asm("invlpg %0"::"m"(vaddr));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 23:34:38 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Renvoie l'adresse physique de la page virtuel */
|
|
|
|
|
|
|
|
|
|
u8* virtual_to_physical(u8 *vaddr)
|
|
|
|
|
{
|
|
|
|
|
u32 *pdir;
|
|
|
|
|
u32 *ptable;
|
|
|
|
|
|
|
|
|
|
pdir = (u32 *) (0xFFFFF000 | (((u32) vaddr & 0xFFC00000) >> 20));
|
|
|
|
|
if ((*pdir & PAGE_PRESENT)) {
|
|
|
|
|
ptable = (u32 *) (0xFFC00000 | (((u32) vaddr & 0xFFFFF000) >> 10));
|
|
|
|
|
if ((*ptable & PAGE_PRESENT))
|
|
|
|
|
return (u8 *) ((*ptable & 0xFFFFF000) + (TOPG((u32) vaddr)));
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* D<>termine une plage virtuelle de m<>moire comme <20>tant mapp<70> aux adresses physiques sp<73>cifi<66>es GENERIQUE*/
|
2018-11-29 13:42:07 +01:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
void virtual_range_use(pd *dst, u8 *vaddr, u8 *paddr, u64 len, u32 flags)
|
|
|
|
|
{
|
|
|
|
|
u64 i;
|
|
|
|
|
u32 realen=len/PAGESIZE;
|
|
|
|
|
page *pg;
|
|
|
|
|
if (len%PAGESIZE!=0) realen++;
|
|
|
|
|
for(i=0;i<realen;i++)
|
|
|
|
|
{
|
|
|
|
|
pg = (page *) vmalloc(sizeof(page));
|
|
|
|
|
pg->paddr = paddr+i*PAGESIZE;
|
|
|
|
|
pg->vaddr = vaddr+i*PAGESIZE;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
if (dst!=NULL)
|
|
|
|
|
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
|
|
|
|
|
else
|
|
|
|
|
vfree(pg);
|
2018-11-27 16:56:21 +01:00
|
|
|
|
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Supprime une plage virtuelle de m<>moire GENERIQUE */
|
2018-11-29 13:42:07 +01:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
void virtual_range_free(pd *dst, u8 *vaddr, u64 len)
|
|
|
|
|
{
|
|
|
|
|
u64 i;
|
|
|
|
|
u32 realen=len/PAGESIZE;
|
|
|
|
|
if (len%PAGESIZE!=0) realen++;
|
|
|
|
|
for(i=0;i<realen;i++)
|
|
|
|
|
{
|
|
|
|
|
virtual_pd_page_remove(vaddr+i*PAGESIZE);
|
|
|
|
|
virtual_page_free(vaddr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* D<>termine une plage virtuelle de m<>moire en attribuant de la m<>moire physique GENERIQUE */
|
2018-11-29 13:42:07 +01:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
void virtual_range_new(pd *dst, u8 *vaddr, u64 len, u32 flags)
|
|
|
|
|
{
|
|
|
|
|
u64 i;
|
|
|
|
|
u32 realen=len/PAGESIZE;
|
|
|
|
|
page *pg;
|
|
|
|
|
if (len%PAGESIZE!=0) realen++;
|
|
|
|
|
for(i=0;i<realen;i++)
|
|
|
|
|
{
|
|
|
|
|
pg = (page *) vmalloc(sizeof(page));
|
|
|
|
|
pg->paddr = physical_page_getfree();
|
|
|
|
|
pg->vaddr = (u8 *) (vaddr+i*PAGESIZE);
|
2018-11-29 13:42:07 +01:00
|
|
|
|
if (dst!=NULL)
|
|
|
|
|
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
|
|
|
|
|
else
|
|
|
|
|
vfree(pg);
|
2018-11-27 16:56:21 +01:00
|
|
|
|
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* D<>termine une plage virtuelle de m<>moire comme <20>tant mapp<70> aux adresses physiques sp<73>cifi<66>es pour le noyau*/
|
|
|
|
|
|
|
|
|
|
void virtual_range_use_kernel(u8 *vaddr, u8 *paddr, u64 *len, u32 flags)
|
|
|
|
|
{
|
|
|
|
|
virtual_range_use(NULL, vaddr, paddr, len, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Supprime une plage virtuelle de m<>moire pour le noyau */
|
|
|
|
|
|
|
|
|
|
void virtual_range_free_kernel(u8 *vaddr, u64 len)
|
|
|
|
|
{
|
|
|
|
|
virtual_range_free(NULL, vaddr, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* D<>termine une plage virtuelle de m<>moire en attribuant de la m<>moire physique pour le noyau */
|
|
|
|
|
|
|
|
|
|
void virtual_range_new_kernel(u8 *vaddr, u64 len, u32 flags)
|
|
|
|
|
{
|
|
|
|
|
virtual_range_new(NULL, vaddr, len, flags);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 23:34:38 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Lib<69>re une page virtuelle de la m<>moire */
|
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
void virtual_page_free(u8 *vaddr)
|
2018-11-25 23:34:38 +01:00
|
|
|
|
{
|
|
|
|
|
vrange *next, *prev, *new;
|
|
|
|
|
u8 *paddr;
|
|
|
|
|
paddr = virtual_to_physical(vaddr);
|
|
|
|
|
if (paddr)
|
2018-11-27 16:56:21 +01:00
|
|
|
|
physical_page_free(TOPAGE((u32) paddr));
|
2018-11-25 23:34:38 +01:00
|
|
|
|
else {
|
|
|
|
|
printf("Aucune page associee a l'adresse virtuelle %x\n", vaddr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
virtual_pd_page_remove(vaddr);
|
|
|
|
|
TAILQ_FOREACH(next, &vrange_head, tailq) {
|
|
|
|
|
if (next->vaddrlow > vaddr)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
prev = TAILQ_PREV(next, vrange_s, tailq);
|
|
|
|
|
if (prev->vaddrhigh == vaddr) {
|
|
|
|
|
prev->vaddrhigh += PAGESIZE;
|
|
|
|
|
if (prev->vaddrhigh == next->vaddrlow) {
|
|
|
|
|
prev->vaddrhigh = next->vaddrhigh;
|
|
|
|
|
TAILQ_REMOVE(&vrange_head, next, tailq);
|
|
|
|
|
vfree(next);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (next->vaddrlow == vaddr + PAGESIZE) {
|
|
|
|
|
next->vaddrlow = vaddr;
|
|
|
|
|
}
|
|
|
|
|
else if (next->vaddrlow > vaddr + PAGESIZE) {
|
|
|
|
|
new = (vrange*) vmalloc(sizeof(vrange));
|
|
|
|
|
new->vaddrlow = vaddr;
|
|
|
|
|
new->vaddrhigh = vaddr + PAGESIZE;
|
|
|
|
|
TAILQ_INSERT_BEFORE(prev, new, tailq);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
panic("Liste chainee corrompue !\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 09:00:59 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Destruction d'un directory pour la gestion virtuelle de la m<>moire */
|
|
|
|
|
|
2018-11-25 23:34:38 +01:00
|
|
|
|
void virtual_pd_destroy(pd *dst)
|
2018-11-23 09:00:59 +01:00
|
|
|
|
{
|
|
|
|
|
page *pg;
|
2018-11-25 23:34:38 +01:00
|
|
|
|
TAILQ_FOREACH(pg, &dst->page_head, tailq) {
|
|
|
|
|
virtual_page_free(pg->vaddr);
|
|
|
|
|
TAILQ_REMOVE(&dst->page_head, pg, tailq);
|
2018-11-23 09:00:59 +01:00
|
|
|
|
vfree(pg);
|
|
|
|
|
}
|
2018-11-25 23:34:38 +01:00
|
|
|
|
virtual_page_free(dst->addr->vaddr);
|
2018-11-23 09:00:59 +01:00
|
|
|
|
vfree(dst);
|
|
|
|
|
return 0;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
}
|
2018-10-02 02:16:14 +02:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Initialise une pages virtuelles (size) pour le heap du noyau */
|
2018-11-29 13:42:07 +01:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
void malloc_init(void)
|
|
|
|
|
{
|
|
|
|
|
tmalloc *chunk;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
chunk = (tmalloc *) KERNEL_HEAP;
|
2018-11-27 16:56:21 +01:00
|
|
|
|
virtual_pd_page_add(NULL, KERNEL_HEAP, physical_page_getfree(), PAGE_NOFLAG);
|
2018-11-29 13:42:07 +01:00
|
|
|
|
kernelcurrentheap=KERNEL_HEAP+PAGESIZE;
|
2018-11-27 16:56:21 +01:00
|
|
|
|
chunk->size = PAGESIZE;
|
|
|
|
|
chunk->used = 0;
|
2018-11-29 13:42:07 +01:00
|
|
|
|
//virtual_range_new_kernel(kernelcurrentheap, chunk->size, PAGE_NOFLAG);
|
2018-11-27 16:56:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Initialisation d'une STAILQ pour la gestion virtuelle de la m<>moire */
|
|
|
|
|
|
|
|
|
|
void virtual_init(void)
|
2018-10-02 02:16:14 +02:00
|
|
|
|
{
|
2018-11-22 17:21:15 +01:00
|
|
|
|
vrange *vpages = (vrange*) vmalloc(sizeof(vrange));
|
2018-11-29 13:42:07 +01:00
|
|
|
|
vpages->vaddrlow = (u8 *) KERNEL_HEAP+PAGESIZE;
|
2018-11-22 17:21:15 +01:00
|
|
|
|
vpages->vaddrhigh = (u8 *) KERNEL_HEAP+MAXHEAPSIZE;
|
2018-11-25 23:34:38 +01:00
|
|
|
|
TAILQ_INIT(&vrange_head);
|
|
|
|
|
TAILQ_INSERT_TAIL(&vrange_head, vpages, tailq);
|
2018-11-22 17:21:15 +01:00
|
|
|
|
}
|
2018-10-02 02:16:14 +02:00
|
|
|
|
|
2018-11-22 17:21:15 +01:00
|
|
|
|
/*******************************************************************************/
|
2018-11-27 16:56:21 +01:00
|
|
|
|
/* Initialisation des 8 premiers MB de la m<>moire en identity mapping */
|
|
|
|
|
|
|
|
|
|
void identity_init(void)
|
|
|
|
|
{
|
|
|
|
|
u32 i;
|
|
|
|
|
u32 *pd0 = KERNEL_PD_ADDR;
|
|
|
|
|
u8 *pg0 = (u8 *) 0;
|
|
|
|
|
u8 *pg1 = (u8 *) (PAGESIZE*PAGENUMBER);
|
|
|
|
|
pd0[0] = ((u32) pg0 | (PAGE_PRESENT | PAGE_WRITE | PAGE_4MB));
|
|
|
|
|
pd0[1] = ((u32) pg1 | (PAGE_PRESENT | PAGE_WRITE | PAGE_4MB));
|
|
|
|
|
for (i = 2; i < 1023; i++)
|
|
|
|
|
pd0[i] =
|
|
|
|
|
((u32) pg1 + PAGESIZE * i) | (PAGE_PRESENT | PAGE_WRITE);
|
|
|
|
|
pd0[1023] = ((u32) pd0 | (PAGE_PRESENT | PAGE_WRITE));
|
|
|
|
|
}
|
2018-10-02 02:16:14 +02:00
|
|
|
|
|
2018-11-27 16:56:21 +01:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Initialisation des registres CR0, CR3, CR4 */
|
|
|
|
|
|
|
|
|
|
void registry_init(void)
|
2018-11-22 17:21:15 +01:00
|
|
|
|
{
|
2018-11-27 16:56:21 +01:00
|
|
|
|
asm("mov %[directory_addr], %%eax \n \
|
2018-10-02 02:16:14 +02:00
|
|
|
|
mov %%eax, %%cr3 \n \
|
|
|
|
|
mov %%cr4, %%eax \n \
|
|
|
|
|
or $0x00000010, %%eax \n \
|
|
|
|
|
mov %%eax, %%cr4 \n \
|
|
|
|
|
mov %%cr0, %%eax \n \
|
|
|
|
|
or $0x80000001, %%eax \n \
|
2018-11-27 16:56:21 +01:00
|
|
|
|
mov %%eax, %%cr0"::[directory_addr]"i"(KERNEL_PD_ADDR));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Initialisation de la m<>moire pagin<69>e */
|
|
|
|
|
|
|
|
|
|
void initpaging(void)
|
|
|
|
|
{
|
|
|
|
|
identity_init();
|
|
|
|
|
registry_init();
|
|
|
|
|
physical_init();
|
|
|
|
|
malloc_init();
|
|
|
|
|
virtual_init();
|
2018-10-02 02:16:14 +02:00
|
|
|
|
}
|
2007-04-02 16:34:45 +02:00
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Copie un octet une ou plusieurs fois en m<>moire */
|
2018-09-28 20:35:51 +02:00
|
|
|
|
|
|
|
|
|
void memset(void *dst, u8 val, u32 count, u32 size)
|
|
|
|
|
{
|
2018-10-13 11:25:55 +02:00
|
|
|
|
u8 *d = (u8 *) dst;
|
|
|
|
|
if (size>0) size--;
|
|
|
|
|
for (; count != 0; count--) {
|
|
|
|
|
*(d++) = val;
|
|
|
|
|
d+=size;
|
|
|
|
|
}
|
2018-09-28 20:35:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/* Copie une portion de m<>moire vers une autre */
|
2018-09-28 20:35:51 +02:00
|
|
|
|
|
|
|
|
|
void memcpy(void *src, void *dst, u32 count, u32 size)
|
|
|
|
|
{
|
2018-10-13 11:25:55 +02:00
|
|
|
|
u8 *s = (u8 *) src;
|
|
|
|
|
u8 *d = (u8 *) dst;
|
|
|
|
|
if (size>0) size--;
|
|
|
|
|
for (; count != 0; count--) {
|
|
|
|
|
*(d++) = *(s++);
|
|
|
|
|
d+=size;
|
|
|
|
|
}
|
2018-09-28 20:35:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/* Compare 2 portions de m<>moire */
|
2018-09-28 20:35:51 +02:00
|
|
|
|
|
|
|
|
|
u32 memcmp(void *src, void *dst, u32 count, u32 size)
|
|
|
|
|
{
|
2018-10-13 11:25:55 +02:00
|
|
|
|
u8 *s = (u8 *) src;
|
|
|
|
|
u8 *d = (u8 *) dst;
|
|
|
|
|
if (size>0) size--;
|
|
|
|
|
for (; count != 0; count--) {
|
|
|
|
|
if (*(s++) != *(d++))
|
|
|
|
|
return *d - *s;
|
|
|
|
|
s+= size;
|
|
|
|
|
d+= size;
|
2018-09-28 20:35:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 16:46:56 +02:00
|
|
|
|
/*******************************************************************************/
|