mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 20:32:44 +00:00 
			
		
		
		
	UserspaceEmulator: Support the name argument in mmap for file backed mappings
This commit is contained in:
		
							parent
							
								
									d2262b8f6d
								
							
						
					
					
						commit
						28cda567c1
					
				
					 3 changed files with 27 additions and 5 deletions
				
			
		|  | @ -874,11 +874,27 @@ u32 Emulator::virt$mmap(u32 params_addr) | |||
| 
 | ||||
|     u32 final_size = round_up_to_power_of_two(params.size, PAGE_SIZE); | ||||
|     u32 final_address = allocate_vm(final_size, params.alignment); | ||||
|     if (params.addr != 0) { | ||||
|         // NOTE: We currently do not support allocating VM at a requeted address in the emulator.
 | ||||
|         // The loader needs this functionality to load .data just after .text.
 | ||||
|         // Luckily, since the loader calls mmap for .data right after it calls mmap for .text,
 | ||||
|         // the emulator will allocate a chunk of memory that is just after what we allocated for .text
 | ||||
|         // becuase of the way we currently allocate VM.
 | ||||
|         ASSERT(params.addr == final_address); | ||||
|     } | ||||
| 
 | ||||
|     if (params.flags & MAP_ANONYMOUS) | ||||
|         mmu().add_region(MmapRegion::create_anonymous(final_address, final_size, params.prot)); | ||||
|     else | ||||
|         mmu().add_region(MmapRegion::create_file_backed(final_address, final_size, params.prot, params.flags, params.fd, params.offset)); | ||||
|     else { | ||||
|         dbgln("chars: {:p}, len: {}", params.name.characters, params.name.length); | ||||
|         String name_str; | ||||
|         if (params.name.characters) { | ||||
|             auto name = ByteBuffer::create_uninitialized(params.name.length); | ||||
|             mmu().copy_from_vm(name.data(), (FlatPtr)params.name.characters, params.name.length); | ||||
|             name_str = { name.data(), name.size() }; | ||||
|         } | ||||
|         mmu().add_region(MmapRegion::create_file_backed(final_address, final_size, params.prot, params.flags, params.fd, params.offset, name_str)); | ||||
|     } | ||||
| 
 | ||||
|     return final_address; | ||||
| } | ||||
|  |  | |||
|  | @ -39,11 +39,16 @@ NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 p | |||
|     return region; | ||||
| } | ||||
| 
 | ||||
| NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset) | ||||
| NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name) | ||||
| { | ||||
|     auto region = adopt_own(*new MmapRegion(base, size, prot)); | ||||
|     region->m_file_backed = true; | ||||
|     region->m_data = (u8*)mmap(nullptr, size, prot, flags, fd, offset); | ||||
|     if (!name.is_empty()) { | ||||
|         dbgln("name is not empty"); | ||||
|         name = String::format("%s (Emulated)", name.characters()); | ||||
|         region->m_name = name; | ||||
|     } | ||||
|     region->m_data = (u8*)mmap_with_name(nullptr, size, prot, flags, fd, offset, name.is_empty() ? nullptr : name.characters()); | ||||
|     ASSERT(region->m_data != MAP_FAILED); | ||||
|     return region; | ||||
| } | ||||
|  |  | |||
|  | @ -37,7 +37,7 @@ class MallocTracer; | |||
| class MmapRegion final : public Region { | ||||
| public: | ||||
|     static NonnullOwnPtr<MmapRegion> create_anonymous(u32 base, u32 size, u32 prot); | ||||
|     static NonnullOwnPtr<MmapRegion> create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset); | ||||
|     static NonnullOwnPtr<MmapRegion> create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name = {}); | ||||
|     virtual ~MmapRegion() override; | ||||
| 
 | ||||
|     virtual ValueWithShadow<u8> read8(u32 offset) override; | ||||
|  | @ -71,6 +71,7 @@ private: | |||
|     bool m_malloc { false }; | ||||
| 
 | ||||
|     OwnPtr<MallocRegionMetadata> m_malloc_metadata; | ||||
|     String m_name; | ||||
| }; | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Itamar
						Itamar