Sponsored Content
Full Discussion: Mmap source
Operating Systems BSD Mmap source Post 302858477 by dcicc on Monday 30th of September 2013 01:43:25 PM
Old 09-30-2013
Mmap source

I'm new to kernels and C, and I am tinkering around trying to understand OpenBSD's secure memory management. I'm stumped on a couple points.

I've read up on malloc() which was apparently modified years ago to allocate memory using mmap. First question, that would be this here, right?

Code:
map_pages(size_t pages)
{
struct pdinfo *pi, *spi;
struct pginfo **pd;
u_long pidx,lidx;
void *result, *tail;
u_long index;

pages <<=malloc_pageshift;
result = MMAP(pages + malloc_guard);

For the life of me, I can't track down source code for mmap(). I know that this is a kernel system call, but where is that source? In particular, I'm interested to see how mmap() now returns a randomized location in memory.

Last edited by Scott; 09-30-2013 at 05:26 PM.. Reason: Code tags
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

mmap

Hello. I'm writing some random access i/o software on Solaris 8 using mmap64 to memory map large files (my test file is ~25 GB). The abbreviated code fragment is: fd = open(cbuf,O_RDONLY|O_LARGEFILE); struct stat statbuf; fstat(fd,&statbuf); off_t len =... (0 Replies)
Discussion started by: gusm
0 Replies

2. HP-UX

mmap failed

We recently have been seeing the following type of error on our development server. Being somewhat new to HP-UX I was hoping to get some insight. Here is what I have found. I have been doing some research. /usr/lib/dld.sl: Call to mmap() failed - TEXT /u07/mdev/lib/libCLEND.sl... (2 Replies)
Discussion started by: scotbuff
2 Replies

3. UNIX for Advanced & Expert Users

mmap and select

I'm using select() to monitor multiple file descriptors (inet sockets) in application. But this application must also collaborate with other applications on the same host via shared memory (mmap'ed file) due to performance reasons. How can I become notification that mmaped memory is changed or... (1 Reply)
Discussion started by: Hitori
1 Replies

4. Solaris

mmap() on 64 bit m/c

Dear Experts, i have a problem related to mmap(), when i run my program on sun for 64 bit which is throwing SIGBUS when it encounters mmap() function, what is the reason how to resolve this one, because it is working for 32 bit. with regards, vidya. (2 Replies)
Discussion started by: vin_pll
2 Replies

5. UNIX for Dummies Questions & Answers

mmap()

how to use mmap() to map a file to memory space. Do you have any simple program???? Because I have to implement lot of concepts into it. (3 Replies)
Discussion started by: gokult
3 Replies

6. Programming

mmap()

how to use mmap() to map a file to memory space. Do you have any simple program???? Because I have to implement lot of concepts into it. (5 Replies)
Discussion started by: gokult
5 Replies

7. Homework & Coursework Questions

mmap

Descriptions: Develop a program that uses mmap() to map a file to memory space. Prepare such a file by yourself and do the follows. <LI class=MsoNormal>Display the content of the file after mapping; <LI class=MsoNormal>Output how many digits included in the file; <LI class=MsoNormal>Replace... (1 Reply)
Discussion started by: gokult
1 Replies

8. Programming

mmap

hai, How do we map 'n' number of files into memory by using mmap system call?? Thanks in advance...... (5 Replies)
Discussion started by: andrew.paul
5 Replies

9. UNIX for Advanced & Expert Users

Regarding MMAP, MLOCK etc..

Hi I want to lock or prevent a portion of memory which I allocated. So I tried MLOCK, MPROTECT and some like this. But all these functions works only on page border. Can I know why that so. Is that possible to protect a portion of memory which is in middle of the page. Example. int A; ... (1 Reply)
Discussion started by: jionnet
1 Replies

10. Emergency UNIX and Linux Support

mmap

I want to know whether this is possile or ever been tried out. I want to obtain a chuck of memory using mmap() I do it so : n = mmap(0, 8000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); And hold on to that memory, when a process requests for memory, some memory is... (2 Replies)
Discussion started by: xerox
2 Replies
EXTENT(9)						   BSD Kernel Developer's Manual						 EXTENT(9)

NAME
extent, extent_create, extent_destroy, extent_alloc, extent_alloc_subregion, extent_alloc_region, extent_free, extent_print -- general pur- pose extent manager SYNOPSIS
#include <sys/malloc.h> #include <sys/extent.h> struct extent * extent_create(char *name, u_long start, u_long end, int mtype, void *storage, size_t storagesize, int flags); void extent_destroy(struct extent *ex); int extent_alloc(struct extent *ex, u_long size, u_long alignment, u_long boundary, int flags, u_long *result); int extent_alloc_subregion(struct extent *ex, u_long substart, u_long subend, u_long size, u_long alignment, u_long boundary, u_long flags, u_long *result); int extent_alloc1(struct extent *ex, u_long size, u_long alignment, u_long skew, u_long boundary, int flags, u_long *result); int extent_alloc_subregion1(struct extent *ex, u_long substart, u_long subend, u_long size, u_long alignment, u_long skew, u_long boundary, u_long flags, u_long *result); int extent_alloc_region(struct extent *ex, u_long start, u_long size, int flags); int extent_free(struct extent *ex, u_long start, u_long size, int flags); void extent_print(struct extent *ex); DESCRIPTION
The NetBSD extent manager provides management of areas of memory or other number spaces (such as I/O ports). An opaque structure called an extent map keeps track of allocated regions within the number space. extent_create() creates an extent map managing the space from start to end inclusive. All memory allocation will use the memory type mtype (see malloc(9)). The extent map will have the name name, used for identification in case of an error. If the flag EX_NOCOALESCE is speci- fied, only entire regions may be freed within the extent map, but internal coalescing of regions is disabled so that extent_free() will never have to allocate a region descriptor and therefore will never fail. The caller must specify one of the flags EX_NOWAIT or EX_WAITOK, speci- fying whether it is okay to wait for memory allocated for extent map overhead. There are some applications which may want to use an extent map but can't use malloc() and free(). These applications may provide pre-allo- cated storage for all descriptor overhead with the arguments storage and storagesize. An extent of this type is called a fixed extent. If the application can safely use malloc() and free(), storage should be NULL. A fixed extent has a fixed number of region descriptors, so care should be taken to provide enough storage for them; alternatively, the flag EX_MALLOCOK may be passed to allocation requests to indicate that a fixed extent map may be extended using a call to malloc(). extent_destroy() destroys the extent map ex, freeing all allocated regions. If the extent is not a fixed extent, the region and internal extent descriptors themselves are freed. This function always succeeds. extent_alloc() allocates a region in extent ex of size size that fits the provided parameters. There are two distinct allocation policies, which are selected by the flags argument: EX_FAST Allocate the first region that fits the provided parameters, regardless of resulting extent fragmentation. default Allocate the smallest region that is capable of holding the request, thus minimizing fragmentation of the extent. The caller must specify if waiting for space in the extent is allowed using the flag EX_WAITSPACE. If EX_WAITSPACE is not specified, the allocation will fail if the request can not be satisfied without sleeping. The caller must also specify, using the EX_NOWAIT or EX_WAITOK flags, if waiting for overhead allocation is allowed. The request will be aligned to alignment boundaries. Alignment values must be a power of 2. If no alignment is necessary, the value 1 should be specified. If boundary is nonzero, the allocated region will not cross any of the numbers which are a multiple of boundary. If the caller specifies the EX_BOUNDZERO flag, the boundary lines begin at zero. Otherwise, the boundary lines begin at the beginning of the extent. The allocated region may begin on a boundary address, but the end of the region will not touch nor cross it. A boundary argument smaller than the size of the request is invalid. Upon successful completion, *result will con- tain the start of the allocated region. extent_alloc_subregion() is similar to extent_alloc(), but it allows the caller to specify that the allocated region must fall within the subregion from substart to subend inclusive. The other arguments and the return values of extent_alloc_subregion() are otherwise the same as those of extent_alloc(). extent_alloc_region() allocates the specific region in the extent map ex beginning at start with the size size. The caller must specify whether it is okay to wait for the indicated region to be free using the flag EX_WAITSPACE. If EX_WAITSPACE is not specified, the allocation will fail if the request can not be satisfied without sleeping. The caller must also specify, using the EX_NOWAIT or EX_WAITOK flags, if waiting for overhead allocation is allowed. The extent_alloc1() and extent_alloc_subregion1() functions are extensions that take one additional argument, skew, that modifies the requested alignment result in the following way: the value (result - skew) is aligned to alignment boundaries. skew must be a smaller number than alignment. Also, a boundary argument smaller than the sum of the requested skew and the size of the request is invalid. extent_free() frees a region of size bytes in extent ex starting at start. If the extent has the EX_NOCOALESCE property, only entire regions may be freed. If the extent has the EX_NOCOALESCE property and the caller attempts to free a partial region, behavior is undefined. The caller must specify one of the flags EX_NOWAIT or EX_WAITOK to specify whether waiting for memory is okay; these flags have meaning in the event that allocation of a region descriptor is required during the freeing process. This situation occurs only when a partial region that begins and ends in the middle of another region is freed. Behavior is undefined if invalid arguments are provided. extent_print() Print out information about extent ex. This function always succeeds. Behavior is undefined if invalid arguments are pro- vided. LOCKING
The extent manager performs all necessary locking on the extent map itself, and any other data structures internal to the extent manager. The locks used by the extent manager are simplelocks, and will never sleep (see lock(9)). This should be taken into account when designing the locking protocol for users of the extent manager. RETURN VALUES
The behavior of all extent manager functions is undefined if given invalid arguments. extent_create() returns the extent map on success, or NULL if it fails to allocate storage for the extent map. It always succeeds when creating a fixed extent or when given the flag EX_WAITOK. extent_alloc(), extent_alloc_region(), extent_alloc_subregion(), and extent_free() return one of the following values: 0 Operation was successful. ENOMEM If EX_NOWAIT is specified, the extent manager was not able to allocate a region descriptor for the new region or to split a region when freeing a partial region. EAGAIN Requested region is not available and EX_WAITSPACE was not specified. EINTR Process received a signal while waiting for the requested region to become available in the extent. Does not apply to extent_free(). EXAMPLES
Here is an example of a (useless) function that uses several of the extent manager routines. void func() { struct extent *foo_ex; u_long region_start; int error; /* * Extent "foo" manages a 256k region starting at 0x0 and * only allows complete regions to be freed so that * extent_free() never needs to allocate memory. */ foo_ex = extent_create("foo", 0x0, 0x3ffff, M_DEVBUF, NULL, 0, EX_WAITOK | EX_NOCOALESCE); /* * Allocate an 8k region, aligned to a 4k boundary, which * does not cross any of the 3 64k boundaries (at 64k, * 128k, and 192k) within the extent. */ error = extent_alloc(foo_ex, 0x2000, 0x1000, 0x10000, EX_NOWAIT, &region_start); if (error) panic("you lose"); /* * Give up the extent. */ extent_destroy(foo_ex); } CODE REFERENCES
The extent manager itself is implemented within the file sys/kern/subr_extent.c. Function prototypes for the framework are located in sys/sys/extent.h. The i386 bus management code uses the extent manager for managing I/O ports and I/O memory. This code is in the file sys/arch/i386/i386/machdep.c. SEE ALSO
malloc(9) HISTORY
The NetBSD extent manager appeared in NetBSD 1.3. AUTHORS
The NetBSD extent manager was architected and implemented by Jason R. Thorpe <thorpej@NetBSD.org>. Matthias Drochner <drochner@zelux6.zel.kfa-juelich.de> contributed to the initial testing and optimization of the implementation. Chris Demetriou <cgd@NetBSD.org> contributed many architectural suggestions. BSD
September 23, 1996 BSD
All times are GMT -4. The time now is 07:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy