Commit 3e6b70c5 authored by Léo Grange's avatar Léo Grange

elf loader now use memory area for stack allocation

parent 88d1eb64
......@@ -42,21 +42,15 @@ int elfloader_load(struct file *filep, struct process *dest) {
struct elf_header header;
if(elfloader_load_all(filep, NULL, dest, &header, ELF_LOAD_SET_BRK) == 0) {
union pm_page page;
void *vmstack;
void *pageaddr;
// alloc physical page and set it as the VM process stack
vmstack = arch_pm_get_free_page(MEM_PM_CACHED);
if(vmstack == NULL) {
printk(LOG_ERR, "elfloader: no physical page\n");
return -1;
}
page.private.ppn = PM_PHYSICAL_PAGE(vmstack);
page.private.flags = MEM_PAGE_PRIVATE | MEM_PAGE_VALID | MEM_PAGE_CACHED;
mem_insert_page(& dest->dir_list , &page,
(void*)(ARCH_UNEWPROC_DEFAULT_STACK - PM_PAGE_BYTES));
// set user stack (the size used *is* a maximum, not the allocated one)
struct mem_area *user_stack;
user_stack = mem_area_alloc();
mem_area_set_anon(user_stack, (void*)(ARCH_UNEWPROC_DEFAULT_STACK
- PROCESS_DEFAULT_STACK_SIZE), PROCESS_DEFAULT_STACK_SIZE);
mem_area_insert(dest, user_stack);
// set kernel stack address, for now any physical memory
pageaddr = arch_pm_get_free_page(MEM_PM_CACHED);
......@@ -213,42 +207,6 @@ int elfloader_load_segment(struct file *filep, void *offset,
mem_area_insert(dest, area);
/*
int i;
void *vm_segaddr;
vfs_lseek(filep, ph->offset, SEEK_SET);
vm_segaddr = offset + ph->vaddr;
for(i=0; i<ph->memsz; i += PM_PAGE_BYTES, vm_segaddr += PM_PAGE_BYTES) {
ssize_t nbread;
ssize_t toread;
union pm_page page;
void *pageaddr;
pageaddr = arch_pm_get_free_page(MEM_PM_CACHED);
if(pageaddr == NULL) {
printk(LOG_ERR, "elfloader: no physical page\n");
// TODO really dirty way to exit, need to clean all done job!
return -1;
}
page.private.ppn = PM_PHYSICAL_PAGE(pageaddr);
page.private.flags = MEM_PAGE_PRIVATE | MEM_PAGE_VALID | MEM_PAGE_CACHED;
// if we have a page, copy data from file
toread = ph->filesz - i;
toread = toread > PM_PAGE_BYTES ? PM_PAGE_BYTES : toread;
if(toread > 0) {
nbread = vfs_read(filep, pageaddr, PM_PAGE_BYTES);
printk(LOG_DEBUG, "[I] %d bytes read from ELF.\n", nbread);
}
mem_insert_page(& dest->dir_list , &page, vm_segaddr);
printk(LOG_DEBUG, "[I] ELF load VM (%p -> %p)\n", pageaddr, vm_segaddr);
}*/
return 0;
}
else {
......
......@@ -33,6 +33,15 @@ void mem_area_free(struct mem_area *area) {
}
void mem_area_set_anon(struct mem_area *area, void *vmaddr, size_t size) {
// nothing more, for now...
area->flags = MEM_AREA_TYPE_ANON;
area->address = vmaddr;
area->max_size = size;
}
struct mem_area *mem_area_find(struct process *proc, void *address) {
// for now, the list is not sorted, so check for each
struct list_head *cur;
......@@ -92,10 +101,6 @@ int mem_area_copy_raw(struct mem_area *area, size_t offset, void *dest, size_t s
printk(LOG_ERR, "mem_area: attempt to copy extra bytes (anon)\n");
}
else {
if(offset + size > area->anon.size && (area->flags & MEM_AREA_MAYGROW)) {
printk(LOG_DEBUG, "mem_area: extends area (%d->%d)\n",
area->anon.size, offset + size);
}
ret = 0;
}
}
......@@ -125,6 +130,7 @@ int mem_area_copy_raw(struct mem_area *area, size_t offset, void *dest, size_t s
memset(dest + zeroed_offset, 0, zeroed_size);
}
ret = 0;
if(readsize > 0) {
vfs_lseek(area->file.filep, area->file.base_offset + offset, SEEK_SET);
nbread = vfs_read(area->file.filep, dest, readsize);
......@@ -134,6 +140,7 @@ int mem_area_copy_raw(struct mem_area *area, size_t offset, void *dest, size_t s
printk(LOG_ERR, "mem_area: failed loading %d bytes from offset 0x%x"
" [absolute 0x%x] (read returns %d)\n",
readsize, offset, area->file.base_offset + offset, nbread);
ret = -1;
}
else {
printk(LOG_DEBUG, "mem_area: loaded %d bytes @%p from file\n", readsize, dest);
......
......@@ -43,7 +43,7 @@ struct mem_area {
struct {
// current size (useful in case of growing areas)
size_t size;
//size_t size;
} anon;
// device type?
......@@ -68,6 +68,12 @@ struct mem_area *mem_area_alloc();
void mem_area_free(struct mem_area *area);
/**
* Helper to set an allocated memory area as an anonymous area.
*/
void mem_area_set_anon(struct mem_area *area, void *vmaddr, size_t size);
/**
* Look for the given address in given process address space, and return the
* corresponding memory area.
......
......@@ -51,6 +51,10 @@ struct elf_shared_lib {
#define PROC_LOAD_SAMPLES 4
#define PROC_LOAD_SAMPMAX 64
// various definitions related to processes :
// default *maximum* user stack size (should be allocated when needed)
#define PROCESS_DEFAULT_STACK_SIZE (64*1024)
struct tty;
struct process {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment