areas.c File Reference

This file contains functions for manipulating regions of virtual memory known as areas. More...


Functions

void list_areas (MemContext_s *psCtx)
 Prints all areas in the specified memory context to the debug console.
void validate_kernel_pages (void)
status_t put_area (MemArea_s *psArea)
status_t alloc_area_list (uint32_t nProtection, uint32_t nLockMode, uintptr_t nAddress, uint_fast32_t nCount, const char *const *apzNames, size_t *panOffsets, size_t *panSizes, area_id *panAreas)
 Allocates an array of areas with the specified names, starting offsets and sizes.
status_t resize_area (area_id hArea, size_t nNewSize, bool bAtomic)
 Resizes the specified area.
status_t delete_area (area_id hArea)
 Deletes the specified area.
void empty_mem_context (MemContext_s *psCtx)
 Deletes all areas in the specified memory context.
void delete_mem_context (MemContext_s *psCtx)
 Deletes the specified memory context and all of its areas.
status_t clone_page_pte (pte_t *pDst, pte_t *pSrc, bool bCow)
 Copies a page-table entry, optionally setting up copy-on-write semantics.
MemContext_s * clone_mem_context (MemContext_s *psOrig)
 Clones the specified memory context.
status_t map_area_to_file (area_id hArea, File_s *psFile, flags_t nProtection __attribute__((unused)), off_t nOffset, size_t nSize)
 Maps the specified area onto a file.
area_id sys_clone_area (const char *pzName, void **ppAddress, flags_t nProtection, flags_t nLockMode, area_id hSrcArea)
 Clones the specified area at a new address.
area_id create_area (const char *pzName, void **ppAddress, size_t nSize, size_t nMaxSize, flags_t nProtection, flags_t nLockMode)
 Creates a new memory area in the current process.
area_id sys_create_area (const char *pzName, void **ppAddress, size_t nSize, flags_t nProtection, flags_t nLockMode)
 Creates a new memory area with the specified attributes.
status_t sys_delete_area (area_id hArea)
 Deletes the specified area.
status_t get_area_physical_address (area_id hArea, uintptr_t *pnAddress)
 Returns the starting physical memory address for the specified area.
status_t get_area_info (area_id hArea, AreaInfo_s *psInfo)
 Returns an AreaInfo_s for the specified area.
status_t sys_get_area_info (area_id hArea, AreaInfo_s *psInfo)
 Returns an AreaInfo_s for the specified area.
status_t sys_remap_area (area_id hArea, void *pPhysAddress)
 Remaps the specified area to use a different region of physical memory.
status_t remap_area (area_id hArea, void *pPhysAddress)
 Remaps the specified area to use a different region of physical memory.
status_t memcpy_to_user (void *pDst, const void *pSrc, size_t nSize)
 Copies a region of memory from kernel space into user space.
status_t memcpy_from_user (void *pDst, const void *pSrc, size_t nSize)
 Copies a region of memory from user space into kernel space.
status_t strncpy_from_user (char *pzDst, const char *pzSrc, size_t nMaxLen)
 Copies a string from user space into kernel space.
status_t strndup_from_user (const char *pzSrc, size_t nMaxLen, char **ppzDst)
 Copies a string from user space into a newly allocated kernel region.
status_t strcpy_to_user (char *pzDst, const char *pzSrc)
 Copies a string from kernel space into user space.
status_t strlen_from_user (const char *pzString)
 Returns the length of the specified string.
status_t verify_mem_area (const void *pAddress, size_t nSize, bool bWriteAccess)
 Verifies a region of user memory for kernel access.
void * sys_sbrk (int nDelta)
 Adjusts the upper bound of a user process's data segment.
area_id clone_from_inactive_ctx (MemArea_s *psArea, uintptr_t nOffset, size_t nLength)
 clone_from_inactive_ctx() and update_inactive_ctx() are used to access an inactive address space / memory context (the associated process is not currently running, page directory is not loaded into CR3).
status_t update_inactive_ctx (MemArea_s *psOriginalArea, area_id hCloneArea, uintptr_t nOffset)
 
See also:
clone_from_inactive_ctx()

void init_kernel_mem_context ()
 Creates the memory context for the kernel.
void init_areas (void)
 Initializes the area manager.

Variables

MultiArray_s g_sAreas
 A private shared MultiArray for managing areas.
sem_id g_hAreaTableSema = -1
 A semaphore to control access to g_sAreas.


Detailed Description

This file contains functions for manipulating regions of virtual memory known as areas.

An area can exist in one of several states depending on its access privileges and whether or not it is currently located in physical memory.

Read only:
Both the area and the PTE are marked read only.
Cloned as copy-on-write:
The PTE is marked as read-only and the area is marked as read-write.
Memory mapped/not present:
The area has a non-NULL a_psFile entry and the PTE has the value 0.
Memory mapped/present:
The area has a non-NULL a_psFile entry and the PTE is marked present.
Shared/not-present:
The area has the AREA_SHARED flag set and the PTE is marked not present.
Swapped out:
The PTE is marked not-present but the address is non-null.

Function Documentation

status_t alloc_area_list ( uint32_t  nProtection,
uint32_t  nLockMode,
uintptr_t  nAddress,
uint_fast32_t  nCount,
const char *const *  apzNames,
size_t *  panOffsets,
size_t *  panSizes,
area_id *  panAreas 
)

Allocates an array of areas with the specified names, starting offsets and sizes.

All areas will have the same protection mask and locking mode.

Parameters:
nProtection a protection bitmask containing any combination of: AREA_READ, AREA_WRITE, AREA_EXEC, AREA_KERNEL, and AREA_WRCOMB.
nLockMode the locking mode to use: AREA_NO_LOCK, AREA_LAZY_LOCK, AREA_FULL_LOCK, or AREA_CONTIGUOUS.
nAddress the base virtual memory address for the new areas. Each area will start at (nAddress + panOffsets[i]).
nCount the number of areas to create. apzNames, panOffsets, panSizes, and panAreas are arrays of this size.
apzNames an array of pointers to strings containing the names to use for each area. Any entry can be NULL to create an unnamed area, or a NULL pointer can be passed here to create all areas without names.
panOffsets an array of byte offsets from nAddress to use as the start address for each area.
panSizes an array of sizes in bytes for each area.
panAreas the area_ids of the new areas will be copied to this array.
Returns:
-EINVAL if any of the areas to create would overlap or if the starting offsets are not in sequential order, -ENOADDRSPC if there isn't enough free virtual address space for the new areas, -ENOMEM if there isn't enough physical memory available; 0 otherwise.
See also:
find_unmapped_area(), do_create_area(), do_delete_area()
Author:
Kurt Skauen ([email protected])

area_id create_area ( const char *  pzName,
void **  ppAddress,
size_t  nSize,
size_t  nMaxSize,
flags_t  nProtection,
flags_t  nLockMode 
)

Creates a new memory area in the current process.

Parameters:
pzName a pointer to the string containing the name for the new area.
ppAddress a pointer to the variable where the start address of the new area will be stored, or NULL. If non- NULL, the previous value of the variable it points to will be used as the preferred start address of the new area.
nSize the size in bytes of the requested area.
nMaxSize the maximum size in bytes of the requested area.
nProtection a protection bitmask containing any combination of: AREA_READ, AREA_WRITE, AREA_EXEC, AREA_KERNEL, and AREA_WRCOMB.
nLockMode the locking mode to use: AREA_NO_LOCK, AREA_LAZY_LOCK, AREA_FULL_LOCK, or AREA_CONTIGUOUS.
Returns:
-EINVAL if the preferred starting address at *ppAddress is not a multiple of PAGE_SIZE; -ENOADDRSPC if there isn't enough virtual address space, -ENOMEM if there isn't enough physical memory; the area_id of the new area otherwise.
See also:
alloc_area()
Author:
Kurt Skauen ([email protected])

status_t delete_area ( area_id  hArea  ) 

Deletes the specified area.

Parameters:
hArea a handle to the area to resize.
Returns:
-EINVAL if the area handle is invalid, -ENOADDRSPC if there isn't enough virtual address space available, -ENOMEM if there isn't enough physical memory available; 0 otherwise.
See also:
do_delete_area()
Author:
Kurt Skauen ([email protected])

status_t get_area_info ( area_id  hArea,
AreaInfo_s *  psInfo 
)

Returns an AreaInfo_s for the specified area.

Parameters:
hArea a handle to the area for which to return info.
psInfo a pointer to the AreaInfo_s in which to store the results.
Returns:
-EINVAL if the area handle is invalid or the area doesn't belong to this process; 0 otherwise.
Author:
Kurt Skauen ([email protected])

status_t get_area_physical_address ( area_id  hArea,
uintptr_t *  pnAddress 
)

Returns the starting physical memory address for the specified area.

Parameters:
hArea a handle to the area.
pnAddress a pointer to the variable where the starting physical address for hArea will be copied.
Returns:
-EINVAL if hArea is invalid; 0 otherwise.
See also:
claim_device(), unregister_device(), do_get_area_physical_address()
Author:
Arno Klenke ([email protected])

status_t memcpy_from_user ( void *  pDst,
const void *  pSrc,
size_t  nSize 
)

Copies a region of memory from user space into kernel space.

Parameters:
pDst the starting address of the destination region.
pSrc the starting address of the source region.
nSize the number of bytes to copy.
Returns:
The error code from verify_mem_area() on failure; 0 otherwise.
See also:
verify_mem_area()
Author:
Kurt Skauen ([email protected])

status_t memcpy_to_user ( void *  pDst,
const void *  pSrc,
size_t  nSize 
)

Copies a region of memory from kernel space into user space.

Parameters:
pDst the starting address of the destination region.
pSrc the starting address of the source region.
nSize the number of bytes to copy.
Returns:
The error code from verify_mem_area() on failure; 0 otherwise.
See also:
verify_mem_area()
Author:
Kurt Skauen ([email protected])

status_t remap_area ( area_id  hArea,
void *  pPhysAddress 
)

Remaps the specified area to use a different region of physical memory.

Parameters:
hArea a handle to the area to remap.
pPhysAddress the new start address in physical memory for the region.
Returns:
-EINVAL if the area handle is invalid or if nPhysAddress isn't a multiple of PAGE_SIZE; 0 otherwise.
See also:
do_remap_area()
Author:
Kurt Skauen ([email protected])

status_t resize_area ( area_id  hArea,
size_t  nNewSize,
bool  bAtomic 
)

Resizes the specified area.

Parameters:
hArea a handle to the area to resize.
nNewSize the new area size in bytes.
bAtomic if true, resize_area() will repeatedly shrink the block cache by 64K and try again if an -ENOMEM error is returned by alloc_area_pages() or alloc_area_page_tables().
Returns:
-EINVAL if the area handle is invalid, -ENOADDRSPC if there isn't enough virtual address space available, -ENOMEM if there isn't enough physical memory available; 0 otherwise.
See also:
alloc_area_page_tables(), alloc_area_pages(), free_area_page_tables(), free_area_pages(), shrink_caches()
Author:
Kurt Skauen ([email protected])

status_t strcpy_to_user ( char *  pzDst,
const char *  pzSrc 
)

Copies a string from kernel space into user space.

Parameters:
pzDst the starting address of the destination region.
pzSrc the address of the string to copy.
Returns:
The error code from verify_mem_area() on failure; 0 otherwise.
See also:
verify_mem_area()
Author:
Kurt Skauen ([email protected])

status_t strlen_from_user ( const char *  pzString  ) 

Returns the length of the specified string.

Parameters:
pzString the starting address of the string.
Returns:
The error code from verify_mem_area() on failure; the length of the string otherwise.
See also:
verify_mem_area()
Author:
Kurt Skauen ([email protected])

status_t strncpy_from_user ( char *  pzDst,
const char *  pzSrc,
size_t  nMaxLen 
)

Copies a string from user space into kernel space.

Parameters:
pzDst the starting address of the destination region.
pzSrc the address of the string to copy.
nMaxLen the maximum size of the string to copy.
Returns:
The error code from verify_mem_area() on failure; 0 otherwise.
See also:
verify_mem_area()
Author:
Kurt Skauen ([email protected])

status_t strndup_from_user ( const char *  pzSrc,
size_t  nMaxLen,
char **  ppzDst 
)

Copies a string from user space into a newly allocated kernel region.

Parameters:
pzSrc the address of the string to copy.
nMaxLen the maximum size of the string to copy.
ppzDst a pointer to the variable in which to store a pointer to the new string.
Returns:
-ENAMETOOLONG if the length of the string, including the trailing '\0', is greater than nMaxLen, -ENOMEM if there isn't enough free memory for kmalloc(), any error code from verify_mem_area() on failure; 0 otherwise.
See also:
verify_mem_area(), kmalloc()
Author:
Kurt Skauen ([email protected])

area_id sys_clone_area ( const char *  pzName,
void **  ppAddress,
flags_t  nProtection,
flags_t  nLockMode,
area_id  hSrcArea 
)

Clones the specified area at a new address.

Parameters:
pzName a pointer to the string containing the name of the new area.
ppAddress a pointer to the variable where the start address of the new area will be stored, or NULL. If non- NULL, the previous value of the variable it points to will be used as the preferred start address of the new area.
nProtection a protection bitmask containing any combination of: AREA_READ, AREA_WRITE, AREA_EXEC, AREA_KERNEL, and AREA_WRCOMB.
nLockMode the locking mode to use: AREA_NO_LOCK, AREA_LAZY_LOCK, AREA_FULL_LOCK, or AREA_CONTIGUOUS.
hSrcArea the area_id of the area to clone.
Returns:
-EFAULT if there is a memory access violation while copying pzName; -EINVAL if the preferred starting address at *ppAddress is not a multiple of PAGE_SIZE; the area_id of the new area otherwise.
See also:
alloc_area()
Author:
Kurt Skauen ([email protected])

area_id sys_create_area ( const char *  pzName,
void **  ppAddress,
size_t  nSize,
flags_t  nProtection,
flags_t  nLockMode 
)

Creates a new memory area with the specified attributes.

Parameters:
pzName a pointer to the string containing the name for the new area.
ppAddress a pointer to the variable where the start address of the new area will be stored, or NULL. If non- NULL, the previous value of the variable it points to will be used as the preferred start address of the new area.
nSize the size in bytes of the requested area.
nProtection a protection bitmask containing any combination of AREA_READ, AREA_WRITE, AREA_EXEC, AREA_KERNEL, and AREA_WRCOMB.
nLockMode the locking mode to use: AREA_NO_LOCK, AREA_LAZY_LOCK, AREA_FULL_LOCK, or AREA_CONTIGUOUS.
Returns:
-EFAULT if there is a memory access violation while copying pzName; -EINVAL if the preferred start address at *ppAddress is not a multiple of PAGE_SIZE; -ENOADDRSPC if there isn't enough virtual address space, -ENOMEM if there isn't enough physical memory; the area_id of the new area otherwise.
See also:
alloc_area(), do_create_area()
Author:
Kurt Skauen ([email protected])

status_t sys_delete_area ( area_id  hArea  ) 

Deletes the specified area.

Parameters:
hArea a handle to the area to delete.
Returns:
-EINVAL if the area handle is invalid or if the area doesn't belong to this process; 0 otherwise.
See also:
do_delete_area()
Author:
Kurt Skauen ([email protected])

status_t sys_get_area_info ( area_id  hArea,
AreaInfo_s *  psInfo 
)

Returns an AreaInfo_s for the specified area.

Parameters:
hArea a handle to the area for which to return info.
psInfo a pointer to the AreaInfo_s in which to store the results.
Returns:
-EINVAL if the area handle is invalid or the area doesn't belong to this process; -EFAULT if there is a memory access violation while copying to psInfo; 0 otherwise.
Author:
Kurt Skauen ([email protected])

status_t sys_remap_area ( area_id  hArea,
void *  pPhysAddress 
)

Remaps the specified area to use a different region of physical memory.

Parameters:
hArea a handle to the area to remap.
pPhysAddress the new start address in physical memory for the region.
Returns:
-EINVAL if the area handle is invalid or if nPhysAddress isn't a multiple of PAGE_SIZE; 0 otherwise.
See also:
do_remap_area()
Author:
Kurt Skauen ([email protected])

void* sys_sbrk ( int  nDelta  ) 

Adjusts the upper bound of a user process's data segment.

Parameters:
nDelta the number of bytes by which to increase or decrease the data segment.
Returns:
-1 if there was an error adjusting the data segment; otherwise, the old upper bound of the process's data segment.
Author:
Kurt Skauen ([email protected])

status_t verify_mem_area ( const void *  pAddress,
size_t  nSize,
bool  bWriteAccess 
)

Verifies a region of user memory for kernel access.

Parameters:
pAddress the start address of the region to lock.
nSize the size in bytes of the region to lock.
bWriteAccess true to lock the region for write access; false otherwise.
Returns:
-EFAULT if pAddress points to an address in kernel space, doesn't correspond to an area in user space, or if (pAddress + nSize - 1) extends beyond the end of the area; -ENOMEM if there isn't enough physical memory available; 0 otherwise.
See also:
alloc_area_pages()
Author:
Kurt Skauen ([email protected])


Generated on Sat May 9 22:55:13 2009 for Syllable Private Functions by  doxygen 1.5.1