Mapping and copying ELF sections in current page directory
Posted: Sat Sep 09, 2017 1:31 pm
Hi,
I've a simple ELF loader in my os. It's not bad, but some parts of my ELF loader is not optimal.
I map ELF sections to corresponding virtual addresses with map_memory(elf_task->page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER) but I can't copy them because these sections are mapped in elf_task's page directory, not in current page directory.
I fixed that by mapping these sections in current page directory instead, copying them to corresponding locations, then unmapping them in current page directory and lastly re-mapping them in elf_task->page_directory.
It looks like that:
map_memory(current_page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER);
memcpy(section->virtual_address, sect_data, section->size_in_memory);
unmap_memory(current_page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER);
map_memory(elf_task->page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER);
but double mapping for each section isn't optimal.
I thought switching to elf_task->page_directory, copying sections and switching to page directory that I switched from like that:
page_directory_t* old_page_directory = current_page_directory;
switch_page_directory(elf_task->page_directory);
memcpy(section->virtual_address, sect_data, section->size_in_memory);
switch_page_directory(old_page_directory);
but this causes a full TLB flush for each ELF file. Is there a more optimal way to do it?
Thanks in advance.
I've a simple ELF loader in my os. It's not bad, but some parts of my ELF loader is not optimal.
I map ELF sections to corresponding virtual addresses with map_memory(elf_task->page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER) but I can't copy them because these sections are mapped in elf_task's page directory, not in current page directory.
I fixed that by mapping these sections in current page directory instead, copying them to corresponding locations, then unmapping them in current page directory and lastly re-mapping them in elf_task->page_directory.
It looks like that:
map_memory(current_page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER);
memcpy(section->virtual_address, sect_data, section->size_in_memory);
unmap_memory(current_page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER);
map_memory(elf_task->page_directory, section->virtual_address, section->size_in_memory, FLAGS_RW | FLAGS_USER);
but double mapping for each section isn't optimal.
I thought switching to elf_task->page_directory, copying sections and switching to page directory that I switched from like that:
page_directory_t* old_page_directory = current_page_directory;
switch_page_directory(elf_task->page_directory);
memcpy(section->virtual_address, sect_data, section->size_in_memory);
switch_page_directory(old_page_directory);
but this causes a full TLB flush for each ELF file. Is there a more optimal way to do it?
Thanks in advance.