segment 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. .TH SEGMENT 3
  2. .SH NAME
  3. segment \- long lived memory segments
  4. .SH SYNOPSIS
  5. .nf
  6. .B bind '#g' /mnt/segment
  7. .BI #g/ segn
  8. .BI #g/ segn /ctl
  9. .BI #g/ segn /data
  10. .BI #g/ segn /free
  11. ...
  12. .fi
  13. .SH DESCRIPTION
  14. .PP
  15. The
  16. .I segment
  17. device provides a 2-level file system representing
  18. long-lived sharable segments that processes may
  19. .IR segattach (2).
  20. The name of the directory is the
  21. .I class
  22. argument to
  23. .IR segattach .
  24. .PP
  25. New segments are created under the top level
  26. using
  27. .B create
  28. (see
  29. .IR open (2)).
  30. The
  31. .B DMDIR
  32. bit must be set in the permissions.
  33. .IR Remove (2)'ing
  34. the directory makes the segment no longer
  35. available for
  36. .IR segattach .
  37. However, the segment will continue to exist until all
  38. processes using it either exit or
  39. .I segdetach
  40. it.
  41. .PP
  42. Within each segment directory are three files,
  43. .BR data ,
  44. .BR ctl ,
  45. and
  46. .BR free .
  47. Reading and writing
  48. .B data
  49. affects the contents of the segment.
  50. Reading and writing
  51. .B ctl
  52. retrieves and sets the segment's properties.
  53. The
  54. .B free
  55. file is used to manage deallocation of buffers for zero-copy.
  56. .PP
  57. There are only three control messages, which sets the segment's
  58. virtual address and length in bytes:
  59. .EX
  60. addr \fIaddress length\fP
  61. umsg \fIaddress length\fP
  62. kmsg \fIaddress length\fP
  63. .EE
  64. The first one creates a shared segment. The second one creates a shared segment
  65. for use with zero-copy. The third one creates a shared segment for use with
  66. zero copy, identified as the one for use by the kernel. See below for an explanation.
  67. .I Address
  68. is automatically rounded down to a page boundary and
  69. .I length
  70. is rounded up to end the segment at a page boundary.
  71. If the address given is zero, the system picks a unique address
  72. (system-wide) for the segment.
  73. The segment will reside at the same virtual address in
  74. all processes sharing it.
  75. When the segment
  76. is attached using
  77. .IR segattach,
  78. the address and length arguments are ignored in the call;
  79. they are defined only by the
  80. control message.
  81. Once the address and length are set, they cannot be reset.
  82. .PP
  83. Reading the control file
  84. returns a message of the same format with the segment's actual
  85. start address and length.
  86. .PP
  87. Opening
  88. .B data
  89. or reading
  90. .B ctl
  91. before setting the virtual address yields the error
  92. ``segment not yet allocated''.
  93. .PP
  94. The permissions check when
  95. .IR segattach ing
  96. is equivalent to the one performed when opening
  97. .B data
  98. with mode ORDWR.
  99. .PP
  100. Segments for zero copy (either
  101. .I umsg
  102. or
  103. .I kmsg
  104. segments) make use of the
  105. .B free
  106. file to control data buffers for zero-copy. Each process exchanging zero-copy
  107. buffers must attach at least to a
  108. .I kmsg
  109. segment, also known as a KZIO (Kernel zero-copy I/O) segment. It might also attach
  110. to one or more
  111. .I umsg
  112. segments, also known as ZIO (zero-copy I/O) segments. ZIO system calls assume
  113. that addresses contained in ZIO (or KZIO) segments may be exchanged for I/O without
  114. requiring data copies. This is not so for addresses referring to any other segment.
  115. .PP
  116. Allocation of buffers within a ZIO segment is handled by the process that created
  117. the segment. Other processes, and the kernel, are expected only to use such buffers,
  118. but they do not allocate or deallocate buffers directly. Allocation of buffers within a KZIO
  119. segment is handled only the kernel. User processes are expected to use such buffers, but
  120. not to allocate or deallocate them.
  121. .PP
  122. When a buffer is no longer in use, the
  123. .B free
  124. file can be used to write its virtual address. As a result, the kernel will notify
  125. any process reading such file of the address for the, now unused, data buffer. If the
  126. address refers to a KZIO segment, the kernel handles deallocation on its own.
  127. .SH EXAMPLE
  128. .PP
  129. Create a one megabyte segment at address 0x10000000:
  130. .EX
  131. % bind '#g' /mnt/segment
  132. % mkdir /mnt/segment/example
  133. % echo 'va 0x10000000 0x100000' > /mnt/segment/example/ctl
  134. .EE
  135. .PP
  136. Put the string ``hi mom'' at the start of the segment:
  137. .EX
  138. % echo -n hi mom > /mnt/segment/example/data
  139. .EE
  140. .PP
  141. Attach the segment to a process:
  142. .EX
  143. {
  144. ulong va;
  145. va = segattach(0, "example", 0, 0);
  146. }
  147. .EE
  148. .SH "SEE ALSO
  149. .IR segattach (2)
  150. .SH SOURCE
  151. .B /sys/src/9/port/devsegment.c