diff options
author | neal <neal> | 2005-04-06 16:30:37 +0000 |
---|---|---|
committer | neal <neal> | 2005-04-06 16:30:37 +0000 |
commit | bf11bbd527b36cca883715231a19d2ece20f17d2 (patch) | |
tree | fb856d0133fb53243d469f09d2aabfca8e602a71 /libhurd-mm/map.c | |
parent | 60099e7c8556f17f88262bbf783178a0c4147799 (diff) |
2005-04-06 Neal H. Walfield <neal@gnu.org>
* vm.h (hurd_vm_allocate): Remove declaration.
(hurd_vm_release): Rename from hurd_vm_deallocate. Update
callers.
(VM_HERE): Remove.
(VM_ZEROFILL): Remove.
* vm.c (hurd_vm_allocate): Remove function.
(hurd_vm_release): Rename from hurd_vm_deallocate.
* mm.h: Include <stdint.h>, <sys/types.h>, <l4/types.h> and
<hurd/physmem.h>.
(hurd_store_t): New typedef.
(hurd_memory_t): Likewise.
(hurd_memory_use): New declaration.
(hurd_memory_transfer): Likewise.
(hurd_store_fault_t): New typedef.
(hurd_store_init): New declaration.
(hurd_store_size): New declaration.
(HURD_VM_HERE): New definition.
(hurd_store_bind_to_vm): New declaration.
(hurd_store_flush): New declaration.
* priv.h: Include "vm.h" and "mm.h".
(here): New debugging macro.
(struct region): Move to...
* mm.h: ...here.
(struct hurd_memory): Rename from struct frame.
Rename field dc_start to cont_start. Add field cont. Remove
field refs. Update users.
(memory_slab): Rename from frame_slab. Update users.
(memory_alloc_into): Rename from frame_alloc_into. Update users.
(frame_alloc): Move to...
* priv.h (frame_insert): Move to...
* mm.h (hurd_store_cache): ...here.
* priv.h (frame_find_first): Move to...
* mm.h (hurd_store_find_cached): ...here.
* priv.h (frame_map): Move to...
* mm.h (hurd_memory_map): ...here.
* priv.h (frame_dealloc): Move to...
* mm.h (hurd_memory_dealloc): ...here.
* priv.h (struct hurd_store): Rename from struct store. Remove
fields server and handle. Add fields hook and fault. Rename
field frames to memory. Update users.
(store_find_free): Remove declaration.
(struct map): Add field store_offset.
(default_container): Likewise.
(map_init): Likewise.
(map_free): Rename from map_dealloc. Update callers.
(map_init): New declaration.
(map_find_first): Rename from map_find. Update callers.
(as_find_free): Rename from map_find_free. Update callers.
(container_find_free): Remove declaration.
(core_slab_allocate_buffer): Rename from
mem_slab_allocate_buffer. Update users.
(core_slab_deallocate_buffer): Rename from
mem_slab_deallocate_buffer. Update users.
(frame_spare): Rename to memory_spare. Update users.
(core_store): New declaration.
* map.c (VIRTUAL_MEMORY_START): Move from here...
* priv.h: ...to here.
* map.c (as): Move from here...
* as.c: ...to here.
(map_system_init): Rename from map_init. Reserve the UTCB and the
KIP.
(memory_spare): Rename from frame_spare.
(core_slab_allocate_buffer): Rename from mem_slab_allocate_buffer.
(core_slab_deallocate_buffer): Rename from
mem_slab_deallocate_buffer.
(map_init): New function.
(map_free): Rename from map_dealloc.
(map_find): Rename from map_find_first. Avoid gratuitous
hurd_btree_map_prev.
(map_find_free): Remove function.
(store_find_free): Move from here...
* as.c (as_find_free): ...to here.
* frame.c: Move from here...
* memory.c: ...to here. Include <hurd/startup.h>.
(swap_store): Remove definition.
(memory_slab): Rename from frame_slab.
(default_container): New static global.
(dc_offset): New function.
(memory_system_init): Likewise.
(memory_alloc_into): Rename from frame_alloc_into.
(hurd_memory_new): New function. Refactored from frame_alloc.
(hurd_memory_delete): New function.
(hurd_memory_alloc): Rename from frame_alloc. Rewritten to use
hurd_memory_new.
(frame_insert): Move from here...
* store.c (hurd_store_cache): ...to here.
* memory.c (hurd_memory_use): New function.
(hurd_memory_transfer): New function.
(hurd_memory_dealloc): Rename from frame_dealloc. Don't
deallocate too eagerly: split memory which is only partially
deallocated.
(frame_find_first): Move from here...
* store.c (hurd_store_find_cached): ...to here.
* memory.c (hurd_memory_map): Rename from frame_map. Loop
mapping all of the requested if a single RPC is insufficient.
* mm-init.c (default_container): Remove definition.
(hurd_mm_init): Don't initialize DEFAULT_CONTAINER. Don't
initialize the store data structures. Call memory_system_init,
core_system_init, hurd_anonymous_init, and map_system_init. Use
core_allocate rather than hurd_vm_allocate to allocate a stack for
the pager thread.
* pager.c (pager): Don't round ADDR. Call MAP->STORE->FAULT if
the physical memory is not cached.
* physmem-user.h: Include <hurd/physmem.h>. Improve
documentation.
(hurd_pm_control_t): Remove redundant typedef.
(hurd_pm_container_t): Likewise.
(hurd_pm_container_access_t): Remove typedef.
(HURD_PM_CONT_ALLOC_PARTIAL): Remove redundant define.
(HURD_PM_CONT_ALLOC_SQUASH): Likewise.
(HURD_PM_CONT_ALLOC_EXTRA): Likewise.
(HURD_PM_CONT_ALLOC_DMA): Likewise.
(hurd_pm_container_share): Change ACCESSS from a
hurd_pm_container_access_t * to a hurd_pm_container_t *.
(hurd_pm_container_map): Rename parameter rights to flags. Add
new parameter AMOUNT. Update callers.
* physmem-user.c (container_ops): Remove redundant enumeration.
(hurd_pm_container_create): Use hurd_pm_container_create_id as the
RPC identifier.
(hurd_pm_container_share): Use hurd_pm_container_share_id as the
RPC identifier.
(hurd_pm_container_allocate): Use hurd_pm_container_allocate_id as
the RPC identifier.
(hurd_pm_container_deallocate): Use
hurd_pm_container_deallocate_id as the RPC identifier.
(hurd_pm_container_map): Use hurd_pm_container_map_id as the RPC
identifier. If AMOUNTP is non-NULL, return the number of bytes
mapped in *AMOUNTP. Remove debugging code.
(hurd_pm_container_copy): Use hurd_pm_container_copy_id as the RPC
identifier.
* anonymous.h: New file.
* anonymous.c: New file.
* as.c: New file.
* core.c: New file.
* frame.c: Remove file.
* store.c: New file.
* Makefile.am (libhurd_mm_a_SOURCES): Add as.c, store.c, memory.c,
core.c, anonymous.c and anonymous.h. Remove frame.c.
* headers.m4: Install anonymous.h as <hurd/anonymous.h>.
Diffstat (limited to 'libhurd-mm/map.c')
-rw-r--r-- | libhurd-mm/map.c | 204 |
1 files changed, 80 insertions, 124 deletions
diff --git a/libhurd-mm/map.c b/libhurd-mm/map.c index 0ef9c56..ca0bb60 100644 --- a/libhurd-mm/map.c +++ b/libhurd-mm/map.c @@ -37,30 +37,38 @@ /* Initialized by the machine-specific startup-code. */ extern struct hurd_startup_data *__hurd_startup_data; - -/* We don't want to start mapping things at 0 as then we don't catch - NULL pointer dereferences. Reserving about 16k makes sense. */ -#define VIRTUAL_MEMORY_START 0x4000 - -struct as as; void -map_init (void) +map_system_init (void) { + error_t err; int i; struct map *map; pthread_mutex_init (&as.lock, NULL); hurd_btree_map_tree_init (&as.mappings); - /* Add the startup code. */ + /* Reserve the virtual memory where the startup code lies. */ + map = map_alloc (); + assert (map); + err = map_init (map, (uintptr_t) HURD_STARTUP_ADDR, HURD_STARTUP_SIZE, + NULL, 0, NULL); + assert_perror (err); + + /* The UTCB. */ map = map_alloc (); assert (map); + err = map_init (map, l4_address (__hurd_startup_data->utcb_area), + l4_size (__hurd_startup_data->utcb_area), + NULL, 0, NULL); + assert_perror (err); - map->store = NULL; - map->store_offset = (uintptr_t) HURD_STARTUP_ADDR; - map->vm.start = (uintptr_t) HURD_STARTUP_ADDR; - map->vm.size = HURD_STARTUP_SIZE; + /* And the kip. */ + map = map_alloc (); + assert (map); + err = map_init (map, (uintptr_t) l4_kip (), l4_kip_area_size (), + NULL, 0, NULL); + assert_perror (err); /* Add the program code, data segment, etc which can be found in the start up data. */ @@ -74,30 +82,27 @@ map_init (void) /* XXX: Wrong, wrong, wrong. Right now we just make sure we know about the virtual address region the mapping uses and assume that there won't be any page faults. */ - map->store = NULL; // lookup store (mapv->cont.server, mapv->cont.cap_handle) add - // these frames to it (but we would need to copy them to the + // these memory to it (but we would need to copy them to the // default container. - map->store_offset = mapv->offset & ~0x7; - map->vm.start = (l4_word_t) mapv->vaddr; - map->vm.size = mapv->size; + err = map_init (map, (l4_word_t) mapv->vaddr, mapv->size, + NULL, mapv->offset & ~0x7, 0); + assert_perror (err); /* FIXME: Do something with the access rights (in the lower three bits of offset). */ - - map_insert (map); } } bool map_spare_integrate; struct map map_spare; -struct frame frame_spare; +struct hurd_memory memory_spare; /* This is the allocation_buffer call back for the map slab. If this is called then someone has called map_alloc and hence map_lock is locked. */ error_t -mem_slab_allocate_buffer (void *hook, size_t size, void **ptr) +core_slab_allocate_buffer (void *hook, size_t size, void **ptr) { error_t err; int i; @@ -105,7 +110,7 @@ mem_slab_allocate_buffer (void *hook, size_t size, void **ptr) assert ((size & (size - 1)) == 0); - /* Find an unused offset. */ + /* Find an unused virtual memory address. */ if (EXPECT_FALSE (! mm_init_done)) { /* We are not yet initialized. Initialize the mapping database. */ @@ -162,10 +167,9 @@ mem_slab_allocate_buffer (void *hook, size_t size, void **ptr) if (! vaddr_free) continue; - /* We may have already allocated a map structure which - search would not find. This may do some extra work but - that's life. */ - map = map_find (vaddr, size); + /* We may have already allocated a map structure which the + above search would not find. */ + map = map_find_first (vaddr, size); if (map) { vaddr_free = false; @@ -176,7 +180,7 @@ mem_slab_allocate_buffer (void *hook, size_t size, void **ptr) while (! vaddr_free); } else - vaddr = map_find_free (size, size); + vaddr = as_find_free (size, size); /* Save the mapping in the spare map. We cannot call map_alloc as it is likely (indirectly) invoking us because its slab is out of @@ -188,42 +192,42 @@ mem_slab_allocate_buffer (void *hook, size_t size, void **ptr) /* We allocate and map the data ourselves even in the case where the pager is already up: clearly we are going to use the memory as soon as we return. */ - err = frame_alloc_into (&frame_spare, vaddr, size); + err = memory_alloc_into (&memory_spare, size); if (err) { - debug ("frame_alloc_into failed! %d\n", err); + debug ("memory_alloc_into failed! %d\n", err); return err; } - map_spare.store = &swap_store; - map_spare.store_offset = frame_spare.dc_start; + map_spare.store = &core_store; + map_spare.store_offset = memory_spare.cont_start; map_spare.vm.start = vaddr; map_spare.vm.size = size; map_spare_integrate = true; - err = frame_map (&frame_spare, 0, size, vaddr); + err = hurd_memory_map (&memory_spare, 0, size, vaddr); if (err) /* Hmm, failure. Nevertheless we managed to allocate the memory. Let's just ignore it and assume that whatever went wrong will go away when the memory is eventually faulted in. */ - debug ("frame_map failed! %d\n", err); + debug ("hurd_memory_map failed! %d\n", err); *ptr = (void *) vaddr; return 0; } error_t -mem_slab_deallocate_buffer (void *hook, void *buffer, size_t size) +core_slab_deallocate_buffer (void *hook, void *buffer, size_t size) { - error_t err = hurd_vm_deallocate ((uintptr_t) buffer, size); + error_t err = hurd_vm_release ((uintptr_t) buffer, size); assert_perror (err); return 0; } struct hurd_slab_space map_slab = HURD_SLAB_SPACE_INITIALIZER (struct map, - mem_slab_allocate_buffer, - mem_slab_deallocate_buffer, + core_slab_allocate_buffer, + core_slab_deallocate_buffer, NULL, NULL, NULL); struct map * @@ -242,23 +246,23 @@ map_alloc (void) another one for the caller. */ if (EXPECT_FALSE (map_spare_integrate)) { - struct frame frame, *f; + struct hurd_memory memory, *f; memcpy (map, &map_spare, sizeof (struct map)); map_insert (map); - /* We can't allocate it using frame_alloc() as that might call + /* We can't allocate it using memory_alloc() as that might call the slab's buffer allocation routine and overwrite - FRAME_SPARE. Copy it here, then allocate it and only then + MEMORY_SPARE. Copy it here, then allocate it and only then copy over. */ - memcpy (&frame, &frame_spare, sizeof (struct frame)); + memcpy (&memory, &memory_spare, sizeof (struct hurd_memory)); map_spare_integrate = false; - err = hurd_slab_alloc (&frame_slab, (void *) &f); + err = hurd_slab_alloc (&memory_slab, (void *) &f); assert_perror (err); - memcpy (f, &frame, sizeof (struct frame)); - frame_insert (&swap_store, map->store_offset, f); + memcpy (f, &memory, sizeof (struct hurd_memory)); + hurd_store_cache (&core_store, map->store_offset, f); goto start; } @@ -266,6 +270,33 @@ map_alloc (void) return map; } +error_t +map_init (struct map *map, uintptr_t vm_addr, size_t size, + hurd_store_t *store, size_t store_offset, + uintptr_t *vm_used_addr) +{ + if (vm_addr == 0) + vm_addr = as_find_free (size, getpagesize ()); + else + { + struct map *map = map_find_first (vm_addr, size); + if (map) + return EEXIST; + } + + map->vm.start = vm_addr; + map->vm.size = size; + map->store = store; + map->store_offset = store_offset; + + if (vm_used_addr) + *vm_used_addr = vm_addr; + + map_insert (map); + + return 0; +} + void map_insert (struct map *map) { @@ -281,14 +312,14 @@ map_detach (struct map *map) } void -map_dealloc (struct map *map) +map_free (struct map *map) { map_detach (map); hurd_slab_dealloc (&map_slab, (void *) map); } struct map * -map_find (uintptr_t address, size_t size) +map_find_first (uintptr_t address, size_t size) { struct region region = { address, size }; struct map *map = hurd_btree_map_find (&as.mappings, ®ion); @@ -298,88 +329,13 @@ map_find (uintptr_t address, size_t size) for (;;) { - struct map *prev = hurd_btree_map_prev (map); + if (map->vm.start <= address) + return map; + struct map *prev = hurd_btree_map_prev (map); if (prev && overlap (address, size, prev->vm.start, prev->vm.size)) map = prev; else return map; } } - -/* Find a free region of the virtual address space for a region of - size SIZE with alignment ALIGN. Must be called with the map lock. - The lock msut not be dropped until the virtual address is entered - into the mapping database (and this function should not be called - again until that has happened). */ -uintptr_t -map_find_free (size_t size, size_t align) -{ - /* Start the probe at the lowest address aligned address after - VIRTUAL_MEMORY_START. FIXME: We should use a hint. But then, we - shouldn't use linked lists either. */ - l4_word_t start = (VIRTUAL_MEMORY_START + align - 1) & ~(align - 1); - bool ok; - struct map *map; - - do - { - /* The proposed start is free unless proven not to be. */ - ok = true; - - /* Iterate over all of the maps and see if any intersect. If - none do, then we have a good address. */ - for (map = hurd_btree_map_first (&as.mappings); map; - map = hurd_btree_map_next (map)) - if (overlap (start, size, map->vm.start, map->vm.size)) - { - ok = false; - /* Use the next aligned virtual address after MAP. */ - /* FIXME: We need to check that we don't overflow. */ - start = (map->vm.start + map->vm.size + align - 1) & ~(align - 1); - break; - } - } - while (! ok); - - return start; -} - -/* Find a free region in a container for a region of size SIZE with - alignment ALIGN. Must be called with the map lock. The lock must - not be dropped until the virtual address is entered into the - mapping database (and this function should not be called again - until that has happened). */ -uintptr_t -store_find_free (struct store *store, size_t size, size_t align) -{ - /* FIXME: Optimize this search! */ - l4_word_t offset = 0; - bool ok; - struct frame *frame; - - assert (! map_spare_integrate); - - do - { - /* The proposed offset is free unless proven not to be. */ - ok = true; - - for (frame = hurd_btree_frame_first (&store->frames); frame; - frame = hurd_btree_frame_next (frame)) - if (overlap (offset, size, frame->dc_start, frame->store.size)) - { - ok = false; - /* FIXME: Check for overflow. */ - offset = (frame->dc_start + frame->store.size + align - 1) - & ~(align - 1); - break; - } - } - while (! ok); - - debug ("Sought range covering %x bytes with alignment %x. Using %x\n", - size, align, offset); - - return offset; -} |