xref: /original-bsd/share/doc/psd/05.sysman/1.2.t (revision e2944021)
Copyright (c) 1983 Regents of the University of California.
All rights reserved. The Berkeley software License Agreement
specifies the terms and conditions for redistribution.

@(#)1.2.t 6.2 (Berkeley) 05/08/86

.sh "Memory management\(dg Text, data and stack

.FS \(dg This section represents the interface planned for later releases of the system. Of the calls described in this section, only sbrk and getpagesize are included in 4.3BSD. .FE Each process begins execution with three logical areas of memory called text, data and stack. The text area is read-only and shared, while the data and stack areas are private to the process. Both the data and stack areas may be extended and contracted on program request. The call addr = sbrk(incr); result caddr_t addr; int incr; changes the size of the data area by incr bytes and returns the new end of the data area, while addr = sstk(incr); result caddr_t addr; int incr; changes the size of the stack area. The stack area is also automatically extended as needed. On the VAX the text and data areas are adjacent in the P0 region, while the stack section is in the P1 region, and grows downward. Mapping pages

The system supports sharing of data between processes by allowing pages to be mapped into memory. These mapped pages may be shared with other processes or private to the process. Protection and sharing options are defined in <mman.h> as: ._d /* protections are chosen from these bits, or-ed together */ #define PROT_READ 0x04 /* pages can be read */ #define PROT_WRITE 0x02 /* pages can be written */ #define PROT_EXEC 0x01 /* pages can be executed */ /* mapping type; choose one */ #define MAP_FILE 0x01 /* mapped from a file */ #define MAP_SWAP 0x02 /* mapped from swap space */ #define MAP_MEMORY 0x04 /* mapped from device memory */ /* sharing types; choose one */ #define MAP_SHARED 0x10 /* share changes */ #define MAP_PRIVATE 0x20 /* changes are private */ /* other flags */ #define MAP_FIXED 0x40 /* map region must be allocated at addr */ #define MAP_EXTEND 0x80 /* for MAP_FILE, the file may be extended */ The cpu-dependent size of a page is returned by the getpagesize system call: pagesize = getpagesize(); result int pagesize;

The call: maddr = mmap(addr, len, prot, flags, fd, pos); result caddr_t maddr; caddr_t addr; int *len, prot, flags, fd; off_t pos; causes the pages starting at addr and continuing for at most len bytes to be mapped from the object represented by descriptor fd, starting at byte offset pos. The starting address of the region is returned. The actual amount mapped is returned in len. The parameter flags specifies whether modifications made to this mapped copy of the page, are to be kept private, or are to be shared with other references. The parameter prot specifies the accessibility of the mapped pages. The addr, len, and pos parameters must all be multiples of the pagesize.

A process can move pages within its own memory by using the mremap call: mremap(addr, len, prot, flags, fromaddr); caddr_t addr; int len, prot, flags; caddr_t fromaddr; This call maps the pages starting at fromaddr to the address specified by addr.

A mapping can be removed by the call munmap(addr, len); caddr_t addr; int len; This call causes further references to these pages to generate invalid memory references. If the region is mapped MAP_FILE with mode PROT_WRITE, the file is truncated to the length specified by len. Page protection control

A process can control the protection of pages using the call mprotect(addr, len, prot); caddr_t addr; int len, prot; This call changes the specified pages to have protection prot\|. Giving and getting advice

A process that has knowledge of its memory behavior may use the madvise call: madvise(addr, len, behav); caddr_t addr; int len, behav; Behav describes expected behavior, as given in <mman.h>: ._d #define MADV_NORMAL 0 /* no further special treatment */ #define MADV_RANDOM 1 /* expect random page references */ #define MADV_SEQUENTIAL 2 /* expect sequential references */ #define MADV_WILLNEED 3 /* will need these pages */ #define MADV_DONTNEED 4 /* don't need these pages */ Finally, a process may obtain information about whether pages are core resident by using the call mincore(addr, len, vec) caddr_t addr; int len; result char *vec; Here the current core residency of the pages is returned in the character array vec, with a value of 1 meaning that the page is in-core. Synchronization primitives

Two routines provide services analogous to the kernel sleep and wakeup functions interpreted in the domain of shared memory. A process may relinquish the processor by calling msleep: msleep(addr) caddr_t addr; Addr must lie within a MAP_SHARED region with at least modes PROT_READ and PROT_WRITE. The process will remain in a sleeping state until some other process issues an mwakeup for the same byte within the region (possibly from a different virtual address) using the call: mwakeup(addr) caddr_t addr;

To avoid system calls for the usual case of an uncontested lock, library routines are provided to acquire and release locks. To acquire a lock a process calls: mset(addr) caddr_t addr; Mset indivisibly tests and sets the memory location addr. If the the previous value is zero, the process has acquired the lock and mset returns immediately. If the previous value is non-zero, the ``want'' bit is set and the test-and-set is retried; if the lock is still unavailable mset calls msleep and tries again.

To release a lock a process calls: mclear(addr) caddr_t addr; Mclear indivisibly tests and clears the memory location addr. If the ``want'' bit is zero in the previous value, mclear returns immediately. If the ``want'' bit is non-zero in the previous value, mclear calls mwakeup before returning.