xref: /csrg-svn/lib/libc/sys/mlock.2 (revision 65100)
162905Sbostic.\" Copyright (c) 1993
262905Sbostic.\"	The Regents of the University of California.  All rights reserved.
361016Shibler.\"
461016Shibler.\" %sccs.include.redist.roff%
561016Shibler.\"
6*65100Smckusick.\"	@(#)mlock.2	8.2 (Berkeley) 12/11/93
761016Shibler.\"
861016Shibler.Dd June 2, 1993
961016Shibler.Dt MLOCK 2
1061016Shibler.Os
1161016Shibler.Sh NAME
1261016Shibler.Nm mlock ,
1361016Shibler.Nm munlock
1461016Shibler.Nd lock (unlock) physical pages in memory
1561016Shibler.Sh SYNOPSIS
1661016Shibler.Fd #include <sys/types.h>
1761057Smckusick.Fd #include <sys/mman.h>
1861016Shibler.Ft int
1961057Smckusick.Fn mlock "caddr_t addr" "size_t len"
2061016Shibler.Ft int
2161057Smckusick.Fn munlock "caddr_t addr" "size_t len"
2261016Shibler.Sh DESCRIPTION
2361016ShiblerThe
2461016Shibler.Nm mlock
2561016Shiblersystem call
2661016Shiblerlocks into memory the physical pages associated with the virtual address
2761016Shiblerrange starting at
2861016Shibler.Fa addr
2961016Shiblerfor
3061016Shibler.Fa len
3161016Shiblerbytes.
3261016ShiblerThe
3361016Shibler.Nm munlock
3461057Smckusickcall unlocks pages previously locked by one or more
3561057Smckusick.Nm mlock
3661057Smckusickcalls.
3761016ShiblerFor both, the
3861016Shibler.Fa addr
3961016Shiblerparameter should be aligned to a multiple of the page size.
4061016ShiblerIf the
4161016Shibler.Fa len
4261016Shiblerparameter is not a multiple of the page size, it will be rounded up
4361016Shiblerto be so.
4461016ShiblerThe entire range must be allocated.
4561016Shibler.Pp
4661016ShiblerAfter an
4761016Shibler.Nm mlock
4861016Shiblercall, the indicated pages will cause neither a non-resident page
49*65100Smckusicknor address-translation fault until they are unlocked.
5061016ShiblerThey may still cause protection-violation faults or TLB-miss faults on
5161016Shiblerarchitectures with software-managed TLBs.
5261016ShiblerThe physical pages remain in memory until all locked mappings for the pages
5361016Shiblerare removed.
5461016ShiblerMultiple processes may have the same physical pages locked via their own
5561016Shiblervirtual address mappings.
5661016ShiblerA single process may likewise have pages multiply-locked via different virtual
5761016Shiblermappings of the same pages or via nested
5861016Shibler.Nm mlock
5961016Shiblercalls on the same address range.
6061016ShiblerUnlocking is performed explicitly by
6161016Shibler.Nm munlock
6261057Smckusickor implicitly by a call to
6361057Smckusick.Nm munmap
6461057Smckusickwhich deallocates the unmapped address range.
6561016ShiblerLocked mappings are not inherited by the child process after a
6661016Shibler.Xr fork 2 .
6761016Shibler.Pp
6861016ShiblerSince physical memory is a potentially scarce resource, processes are
6961016Shiblerlimited in how much they can lock down.
7061016ShiblerA single process can
7161016Shibler.Nm mlock
7261016Shiblerthe minimum of
7361016Shiblera system-wide ``wired pages'' limit and
7461016Shiblerthe per-process
7561016Shibler.Li RLIMIT_MEMLOCK
7661016Shiblerresource limit.
7761016Shibler.Sh RETURN VALUES
7861016ShiblerA return value of 0 indicates that the call
7961016Shiblersucceeded and all pages in the range have either been locked or unlocked.
8061016ShiblerA return value of -1 indicates an error occurred and the locked
8161016Shiblerstatus of all pages in the range remains unchanged.
8261016ShiblerIn this case, the global location
8361016Shibler.Va errno
8461016Shibleris set to indicate the error.
8561016Shibler.Sh ERRORS
8661016Shibler.Fn Mlock
8761016Shiblerwill fail if:
8861016Shibler.Bl -tag -width Er
8961016Shibler.It Bq Er EINVAL
9061016ShiblerThe address given is not page aligned or the length is negative.
9161016Shibler.It Bq Er EAGAIN
9261016ShiblerLocking the indicated range would exceed either the system or per-process
9361057Smckusicklimit for locked memory.
9461016Shibler.It Bq Er ENOMEM
9561016ShiblerSome portion of the indicated address range is not allocated.
9661057SmckusickThere was an error faulting/mapping a page.
9761057Smckusick.El
9861057Smckusick.Fn Munlock
9961057Smckusickwill fail if:
10061057Smckusick.Bl -tag -width Er
10161057Smckusick.It Bq Er EINVAL
10261057SmckusickThe address given is not page aligned or the length is negative.
10361057Smckusick.It Bq Er ENOMEM
10461057SmckusickSome portion of the indicated address range is not allocated.
10561057SmckusickSome portion of the indicated address range is not locked.
10661057Smckusick.El
10761016Shibler.Sh "SEE ALSO"
10861016Shibler.Xr fork 2 ,
10961057Smckusick.Xr mmap 2 ,
11061057Smckusick.Xr munmap 2 ,
11161057Smckusick.Xr setrlimit 2 ,
11261057Smckusick.Xr getpagesize 3
11361016Shibler.Sh BUGS
11461016ShiblerUnlike The Sun implementation, multiple
11561016Shibler.Nm mlock
11661016Shiblercalls on the same address range require the corresponding number of
11761016Shibler.Nm munlock
11861016Shiblercalls to actually unlock the pages, i.e.
11961016Shibler.Nm mlock
12061016Shiblernests.
12161016ShiblerThis should be considered a consequence of the implementation
12261016Shiblerand not a feature.
12361016Shibler.Pp
12461016ShiblerThe per-process resource limit is a limit on the amount of virtual
12561016Shiblermemory locked, while the system-wide limit is for the number of locked
12661016Shiblerphysical pages.
12761016ShiblerHence a process with two distinct locked mappings of the same physical page
12861016Shiblercounts as 2 pages against the per-process limit and as only a single page
12961016Shiblerin the system limit.
13061016Shibler.Sh HISTORY
13161016ShiblerThe
13261016Shibler.Fn mlock
13361016Shiblerand
13461016Shibler.Fn munlock
13562904Sbosticfunctions first appeared in 4.4BSD.
136