glenda.party
term% ls -F
term% cat index.txt
SEGATTACH(2)                  System Calls Manual                 SEGATTACH(2)



NAME
       segattach, segdetach, segfree - map/unmap a segment in virtual memory

SYNOPSIS
       #include <u.h>
       #include <libc.h>

       long segattach(int attr, char *class, void *va, ulong len)

       int  segdetach(void *addr)

       int  segfree(void *va, ulong len)

DESCRIPTION
       Segattach  creates  a  new  memory  segment,  adds  it  to  the calling
       process's address space, and returns its lowest address  (as  an  inte‐
       ger).   Segments  belong  to system-dependent classes.  Segment classes
       memory (plain memory) and shared (shared memory) are available  on  all
       systems.

       Shared  segments are inherited by the children of the attaching process
       and remain untouched across a  fork(2).   An  exec(2)  will  release  a
       shared  segment  if it overlaps the segments in the file being exec'ed;
       otherwise the segment will be inherited.

       Some machines provide a segment class lock.  Lock segments allow access
       to  special lock hardware provided by some multiprocessors, in particu‐
       lar the SGI Power Series machines.

       Systems may also provide interfaces to special  hardware  devices  like
       frame buffers through the segattach interface.  Device memory mapped by
       this method is typically uncached by default.

       If the specified class is unknown, segattach draws an error.

       Attr specifies the new segment's attributes.  The only  attributes  im‐
       plemented on all classes of segment is SG_RONLY, which allows only read
       access on the segment, and SG_CEXEC, which causes the segment to be de‐
       tached  when  the process does an exec(2).  Specific devices may imple‐
       ment attributes to control caching and allocation, but these will  vary
       between devices.

       Va and len specify the position of the segment in the process's address
       space.  Va is rounded down to the nearest page boundary and  va+len  is
       rounded  up.  The system does not permit segments to overlap.  If va is
       zero, the system will choose a suitable address.

       Segdetach removes a segment from a process's address space. Memory used
       by  the segment is freed.  Addr may be any address within the bounds of
       the segment.

       The system will not permit the text and stack segments to  be  detached
       from the address space.

       Segfree  allows  specific areas of a segment's memory to be returned to
       the system, but leaves that portion  of  the  process's  address  space
       valid,  to be reconnected to initialized memory of the appropriate type
       if addressed again.  Va and len are interpreted  as  in  segattach  but
       need not refer to the entire segment.

       The  MIPS  R2000  and  R3000 have no hardware instructions to implement
       locks.  The following method can be used to build them  from  software.
       First, try to segattach a segment of class lock.  If this succeeds, the
       machine is an SGI Power Series and the memory contains hardware  locks.
       Each  4096-byte  page has 64 long words at its beginning; each word im‐
       plements a test-and-set semaphore when read; the low bit of the word is
       zero  on  success, one on failure.  If the segattach fails, there is no
       hardware support but the operating system helps: Any  COP3  instruction
       will  be  trapped  by the kernel and interpreted as a test-and-set.  In
       the trap, R1 points to a long; on return, R1 is greater or  equal  zero
       on  success,  negative on failure.  The following assembly language im‐
       plements such a test-and-set.

              /*
               *      MIPS test and set
               */
                      TEXT    tas(SB), $0
                      MOVW    R1, sema+0(FP)  /* save arg on stack */
              btas:
                      MOVW    sema+0(FP), R1
                      MOVB    R0, 1(R1)
                      NOR     R0, R0, R0      /* NOP */
                      WORD    $(023<<26)      /* MFC3 R0, R0 */
                      BLTZ    R1, btas
                      RET

SOURCE
       /sys/src/libc/9syscall

SEE ALSO
       lock(2), segbrk(2), segflush(2)
       /proc/*/segment

DIAGNOSTICS
       These functions set errstr.

BUGS
       The return type of segattach is peculiar.  Also, segattach  returns  -1
       on  error;  beware  that on some systems other negative values might be
       legal addresses.
       There is a small fixed limit on the number of segments that may be  at‐
       tached, as well as a maximum segment size.



                                                                  SEGATTACH(2)