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 physical_init(void);
void initpaging(void); void initpaging(void);
void virtual_init(void); void virtual_init(void);
tmalloc *mallocpage(u8 size); tmalloc *mallocpage(u64 size);
void *vmalloc(u32 size); void *vmalloc(u32 size);
void vfree(void *vaddr); void vfree(void *vaddr);
page *virtual_page_getfree(void); page *virtual_page_getfree(void);

View File

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

View File

@ -22,11 +22,14 @@ void panic(u8 *string)
/*******************************************************************************/ /*******************************************************************************/
/* Alloue plusieurs pages virtuelles (size) pour le heap du noyau */ /* Alloue plusieurs pages virtuelles (size) pour le heap du noyau */
tmalloc *mallocpage(u8 size) tmalloc *mallocpage(u64 size)
{ {
tmalloc *chunk; tmalloc *chunk;
u8 *paddr; 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) if ((kernelcurrentheap - KERNEL_HEAP + realsize) > MAXHEAPSIZE)
panic("Plus de memoire noyau heap disponible a allouer !\n"); panic("Plus de memoire noyau heap disponible a allouer !\n");
chunk = (tmalloc *) kernelcurrentheap; chunk = (tmalloc *) kernelcurrentheap;
@ -47,7 +50,7 @@ u32 getmallocnb(void)
chunk = KERNEL_HEAP; chunk = KERNEL_HEAP;
while (chunk < (tmalloc *) kernelcurrentheap) { while (chunk < (tmalloc *) kernelcurrentheap) {
realsize++; realsize++;
chunk = chunk + chunk->size; chunk = (tmalloc *)((u8 *) chunk + chunk->size);
} }
return realsize; return realsize;
} }
@ -64,7 +67,7 @@ u32 getmallocused(void)
while (chunk < (tmalloc *) kernelcurrentheap) { while (chunk < (tmalloc *) kernelcurrentheap) {
if (chunk->used) if (chunk->used)
realsize+=chunk->size; realsize+=chunk->size;
chunk = chunk + chunk->size; chunk = (tmalloc *)((u8 *) chunk + chunk->size);
} }
return realsize; return realsize;
} }
@ -80,7 +83,7 @@ u32 getmallocfree(void)
while (chunk < (tmalloc *) kernelcurrentheap) { while (chunk < (tmalloc *) kernelcurrentheap) {
if (!chunk->used) if (!chunk->used)
realsize+=chunk->size; realsize+=chunk->size;
chunk = chunk + chunk->size; chunk = (tmalloc *)((u8 *) chunk + chunk->size);
} }
return realsize; return realsize;
} }
@ -107,16 +110,16 @@ void *vmalloc(u32 size)
while (chunk->used || chunk->size < realsize) { while (chunk->used || chunk->size < realsize) {
if (chunk->size == 0) if (chunk->size == 0)
panic(sprintf("Element du heap %x defectueux avec une taille nulle (heap %x) !",chunk, kernelcurrentheap)); 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) if (chunk == (tmalloc *) kernelcurrentheap)
mallocpage((realsize / PAGESIZE) + 1); mallocpage(realsize);
else if (chunk > (tmalloc *) kernelcurrentheap) else if (chunk > (tmalloc *) kernelcurrentheap)
panic (sprintf("Element du heap %x depassant la limite %x !",chunk, kernelcurrentheap)); panic (sprintf("Element du heap %x depassant la limite %x !",chunk, kernelcurrentheap));
} }
if (chunk->size - realsize < MALLOC_MINIMUM) if (chunk->size - realsize < MALLOC_MINIMUM)
chunk->used = 1; chunk->used = 1;
else { else {
new = chunk + realsize; new = (tmalloc *)((u8 *) chunk + realsize);
new->size = chunk->size - realsize; new->size = chunk->size - realsize;
new->used = 0; new->used = 0;
chunk->size = realsize; chunk->size = realsize;
@ -133,7 +136,7 @@ void vfree(void *vaddr)
tmalloc *chunk, *new; tmalloc *chunk, *new;
chunk = (tmalloc *) (vaddr - sizeof(tmalloc)); chunk = (tmalloc *) (vaddr - sizeof(tmalloc));
chunk->used = 0; 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; chunk->size += new->size;
} }
@ -378,17 +381,21 @@ void virtual_range_use(pd *dst, u8 *vaddr, u8 *paddr, u64 len, u32 flags)
page *pg; page *pg;
if (len%PAGESIZE!=0) realen++; if (len%PAGESIZE!=0) realen++;
for(i=0;i<realen;i++) for(i=0;i<realen;i++)
{
if (dst==NULL)
{
virtual_pd_page_add(dst, vaddr+i*PAGESIZE, paddr+i*PAGESIZE, flags);
}
else
{ {
pg = (page *) vmalloc(sizeof(page)); pg = (page *) vmalloc(sizeof(page));
pg->paddr = paddr+i*PAGESIZE; pg->paddr = paddr+i*PAGESIZE;
pg->vaddr = vaddr+i*PAGESIZE; pg->vaddr = vaddr+i*PAGESIZE;
if (dst!=NULL)
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq); TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
else
vfree(pg);
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags); virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
} }
} }
}
/*******************************************************************************/ /*******************************************************************************/
/* Supprime une plage virtuelle de mémoire GENERIQUE */ /* Supprime une plage virtuelle de mémoire GENERIQUE */
@ -415,17 +422,21 @@ void virtual_range_new(pd *dst, u8 *vaddr, u64 len, u32 flags)
page *pg; page *pg;
if (len%PAGESIZE!=0) realen++; if (len%PAGESIZE!=0) realen++;
for(i=0;i<realen;i++) for(i=0;i<realen;i++)
{
if (dst==NULL)
{
virtual_pd_page_add(dst, vaddr+i*PAGESIZE, physical_page_getfree(), flags);
}
else
{ {
pg = (page *) vmalloc(sizeof(page)); pg = (page *) vmalloc(sizeof(page));
pg->paddr = physical_page_getfree(); pg->paddr = physical_page_getfree();
pg->vaddr = (u8 *) (vaddr+i*PAGESIZE); pg->vaddr = vaddr+i*PAGESIZE;
if (dst!=NULL)
TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq); TAILQ_INSERT_TAIL(&dst->page_head, pg, tailq);
else
vfree(pg);
virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags); virtual_pd_page_add(dst, pg->vaddr, pg->paddr, flags);
} }
} }
}
/*******************************************************************************/ /*******************************************************************************/
/* Détermine une plage virtuelle de mémoire comme étant mappé aux adresses physiques spécifiées pour le noyau*/ /* Détermine une plage virtuelle de mémoire comme étant mappé aux adresses physiques spécifiées pour le noyau*/

View File

@ -41,6 +41,8 @@ static command commands[] = {
{"test3d" , "", &test3d}, {"test3d" , "", &test3d},
{"detectpci" , "", &detectpci}, {"detectpci" , "", &detectpci},
{"mem" , "", &mem}, {"mem" , "", &mem},
{"testmem" , "", &testmem},
}; };
/*******************************************************************************/ /*******************************************************************************/
@ -80,13 +82,29 @@ int test(void)
return; 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 */ /* Affiche des informations sur la mémoire */
int mem() int mem()
{ {
u32 libre=getmemoryfree(); u32 libre=getmemoryfree();
u32 total=physical_getmemorysize(); 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("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("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()); 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); counter += fonction(&buffer,&dest,buffersize);
flag = false; flag = false;
break; break;
case '%':
fonction(&achar,string,1);
counter++;
flag = false;
case 'c': case 'c':
temp = (u8) va_arg(args, u8); temp = (u8) va_arg(args, u8);
fonction(&temp,string,1); fonction(&temp,string,1);