fix: correction gestion mémoire virtuelle & malloc, fonction testmem opérationnelle. Correction Xprintf avec %%

This commit is contained in:
Nicolas Hordé 2018-11-30 19:06:22 +01:00
parent b6e2ead659
commit 73c70d20bd
5 changed files with 60 additions and 26 deletions

View File

@ -91,7 +91,7 @@ u8* physical_page_getfree(void);
void physical_init(void);
void initpaging(void);
void virtual_init(void);
tmalloc *mallocpage(u8 size);
tmalloc *mallocpage(u64 size);
void *vmalloc(u32 size);
void vfree(void *vaddr);
page *virtual_page_getfree(void);

View File

@ -29,3 +29,4 @@ int help();
int logo();
int detectpci();
int mem();
int testmem();

View File

@ -22,11 +22,14 @@ void panic(u8 *string)
/*******************************************************************************/
/* Alloue plusieurs pages virtuelles (size) pour le heap du noyau */
tmalloc *mallocpage(u8 size)
tmalloc *mallocpage(u64 size)
{
tmalloc *chunk;
u8 *paddr;
u32 realsize=size * PAGESIZE;
u16 nbpages=size / PAGESIZE;
u64 realsize=nbpages * PAGESIZE;
if (size%PAGESIZE!=0)
realsize+=PAGESIZE;
if ((kernelcurrentheap - KERNEL_HEAP + realsize) > MAXHEAPSIZE)
panic("Plus de memoire noyau heap disponible a allouer !\n");
chunk = (tmalloc *) kernelcurrentheap;
@ -47,7 +50,7 @@ u32 getmallocnb(void)
chunk = KERNEL_HEAP;
while (chunk < (tmalloc *) kernelcurrentheap) {
realsize++;
chunk = chunk + chunk->size;
chunk = (tmalloc *)((u8 *) chunk + chunk->size);
}
return realsize;
}
@ -64,7 +67,7 @@ u32 getmallocused(void)
while (chunk < (tmalloc *) kernelcurrentheap) {
if (chunk->used)
realsize+=chunk->size;
chunk = chunk + chunk->size;
chunk = (tmalloc *)((u8 *) chunk + chunk->size);
}
return realsize;
}
@ -80,7 +83,7 @@ u32 getmallocfree(void)
while (chunk < (tmalloc *) kernelcurrentheap) {
if (!chunk->used)
realsize+=chunk->size;
chunk = chunk + chunk->size;
chunk = (tmalloc *)((u8 *) chunk + chunk->size);
}
return realsize;
}
@ -107,16 +110,16 @@ void *vmalloc(u32 size)
while (chunk->used || chunk->size < realsize) {
if (chunk->size == 0)
panic(sprintf("Element du heap %x defectueux avec une taille nulle (heap %x) !",chunk, kernelcurrentheap));
chunk = chunk + chunk->size;
chunk = (tmalloc *)((u8 *) chunk + chunk->size);
if (chunk == (tmalloc *) kernelcurrentheap)
mallocpage((realsize / PAGESIZE) + 1);
mallocpage(realsize);
else if (chunk > (tmalloc *) kernelcurrentheap)
panic (sprintf("Element du heap %x depassant la limite %x !",chunk, kernelcurrentheap));
}
if (chunk->size - realsize < MALLOC_MINIMUM)
chunk->used = 1;
else {
new = chunk + realsize;
new = (tmalloc *)((u8 *) chunk + realsize);
new->size = chunk->size - realsize;
new->used = 0;
chunk->size = realsize;
@ -133,7 +136,7 @@ void vfree(void *vaddr)
tmalloc *chunk, *new;
chunk = (tmalloc *) (vaddr - sizeof(tmalloc));
chunk->used = 0;
while ((new = (tmalloc *) chunk + chunk->size) && new < (tmalloc *) kernelcurrentheap && new->used == 0)
while ((new = (tmalloc *)((u8 *) chunk + chunk->size)) && new < (tmalloc *) kernelcurrentheap && new->used == 0)
chunk->size += new->size;
}
@ -379,14 +382,18 @@ void virtual_range_use(pd *dst, u8 *vaddr, u8 *paddr, u64 len, u32 flags)
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;
if (dst!=NULL)
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
if (dst==NULL)
{
virtual_pd_page_add(dst, vaddr+i*PAGESIZE, paddr+i*PAGESIZE, flags);
}
else
vfree(pg);
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
{
pg = (page *) vmalloc(sizeof(page));
pg->paddr = paddr+i*PAGESIZE;
pg->vaddr = vaddr+i*PAGESIZE;
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
}
}
}
@ -416,15 +423,19 @@ void virtual_range_new(pd *dst, u8 *vaddr, u64 len, u32 flags)
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);
if (dst!=NULL)
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
if (dst==NULL)
{
virtual_pd_page_add(dst, vaddr+i*PAGESIZE, physical_page_getfree(), flags);
}
else
vfree(pg);
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
}
{
pg = (page *) vmalloc(sizeof(page));
pg->paddr = physical_page_getfree();
pg->vaddr = vaddr+i*PAGESIZE;
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
}
}
}
/*******************************************************************************/

View File

@ -41,6 +41,8 @@ static command commands[] = {
{"test3d" , "", &test3d},
{"detectpci" , "", &detectpci},
{"mem" , "", &mem},
{"testmem" , "", &testmem},
};
/*******************************************************************************/
@ -80,13 +82,29 @@ int test(void)
return;
}
/*******************************************************************************/
/* Test la memoire */
int testmem()
{
u8* test;
print("**** AVANT ALLOCATION\r\n");
mem();
test=vmalloc(4096*10); /* 10 pages */
print("**** APRES ALLOCATION\r\n");
mem();
vfree(test);
print("**** APRES LIBERATION\r\n");
mem();
}
/*******************************************************************************/
/* Affiche des informations sur la mémoire */
int mem()
{
u32 libre=getmemoryfree();
u32 total=physical_getmemorysize();
printf("Memoire physique (TOTAL)\r\n -libre \33[40D\33[15C%H\r\n -occupee \33[40D\33[15C%H\r\n -total \33[40D\33[15C%H\r\n\r\n",libre,total-libre,total);
printf("Memoire physique (TOTAL)\r\n -libre \33[40D\33[15C%H (%.2f%%)\r\n -occupee \33[40D\33[15C%H\r\n -total \33[40D\33[15C%H\r\n\r\n",libre,(float) libre/total*100,total-libre,total);
printf("Memoire HEAP (NOYAU) - % u blocs\r\n -libre \33[40D\33[15C%H\r\n -occupee \33[40D\33[15C%H\r\n -allouables \33[40D\33[15C%H\r\n\r\n",getmallocnb(),getmallocfree(),getmallocused(),getmallocnonallocated());
printf("Plan de memoire (NOYAU)\r\n -IDT \33[40D\33[15C%X\r\n -GDT \33[40D\33[15C%X\r\n -PGD \33[40D\33[15C%X\r\n -STACK \33[40D\33[15C%X\r\n -CODE \33[40D\33[15C%X\r\n -PAGES \33[40D\33[15C%X\r\n -HEAP \33[40D\33[15C%X\r\n -VESAFB \33[40D\33[15C%X\r\n\r\n",IDT_ADDR,GDT_ADDR,KERNEL_PD_ADDR,KERNEL_STACK_ADDR,KERNEL_CODE_ADDR,KERNEL_PAGES,KERNEL_HEAP,VESA_FBMEM);
printf("Memoire Virtuelle (NOYAU)\r\n -pages libres \33[40D\33[16C% u\r\n -pages occupees \33[40D\33[16C% u\r\n",virtual_getpagesfree(),virtual_getpagesused());

View File

@ -1158,6 +1158,10 @@ u32 format(const u8 * string, va_list args, u32 maxsize, u32 (*fonction)(u8* src
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case '%':
fonction(&achar,string,1);
counter++;
flag = false;
case 'c':
temp = (u8) va_arg(args, u8);
fonction(&temp,string,1);