Consolidate license copies
[its.git] / sysdoc / jobonl.100
1 Copyright (c) 1999 Massachusetts Institute of Technology
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 3 of the License, or (at
6 your option) any later version.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 ------------------------------
17
18 PROGRAMMING TECHNOLOGY DIVISION DOCUMENT                     SYS.xx.yy
19
20 IDENTIFICATION
21      The JOB/BOJ Device:  A Mechanism for Implementing
22                           Non-standard Devices
23      Marc S. Seriff, Jack Haverty, Richard Stallman (MIT-AI)
24      September 18, 1974
25
26 INTRODUCTION
27      It is occasionally desirable to extend the ITS system to allow
28 for the use of non-standard I/O devices.  A very desirable feature
29 would be to allow user-level programs to make use of the newly defined
30 devices with out modification of the user-level programs.  For this
31 reason the JOB/BOJ device was implemented.
32      When a job (call it the user) attempts an OPEN for a file whose
33 name is "JOB: <dir>; <fn1> <fn2>", the system creates a
34 new job, called the BOJ device handler, or BDH (this is a terrible
35 name; it USES the BOJ device and HANDLES the JOB device), and attempts
36 to load the file "DSK: <dir>; <fn1> <fn2>" into that job.
37 If the load fails, the OPEN will fail.  If this load succeeds, the
38 OPEN, as well as all further I/O operations on the user's JOB channel
39 will be referred to the BDH for execution.  At that point, it will be
40 up to the BDH to decide whether or not the open should fail.  In order
41 to get a handle on information about the user's open, The BDH opens
42 one or more channels to the "BOJ" device.  The system will insure that
43 the BDH's BOJ channel is logically linked to the user's JOB channel.
44
45 The following diagram illustrates what happens:
46
47         __________                    _________
48        |          |                  |         |
49        |   User   |-----v______+---->|   BDH   |
50        |__________| | | V--X---^ | | |_________|
51                     | |  System  | |
52                 ____| |____|_____| |____
53                |           |            |
54         The JOB channel    |     The BOJ channel
55                            |
56                            V
57                      System mediates
58
59      Once the BDH has successfully opened the BOJ channel, it will be
60 notified (via an I/O channel interrupt) every time the user attempts
61 an I/O operation on the JOB channel.  The BDH may then examine the
62 data that is made available to it about the I/O operation and may
63 simulate the operation for the user.  In this way, BDHs can be written
64 to give anything the appearance of a disk, tape or other specific type
65 of I/O device.
66
67 USING THE JOB DEVICE
68      Programs that use the JOB device should not be required to make
69 special provisions.  A BDH should, in general, be written so that the
70 non-standard device will behave exactly like some standard device.  In
71 other words, a user program may open a JOB device using a standard
72 open and may then execute any legal I/O operation.
73
74      A file name with something other than "JOB" for a device code can
75 be made to act like a JOB device in one of two ways.  The first way
76 should be used for devices that are not generally applicable or are
77 expected to be used only for a short time.  To use this method simply
78 use translates (using MONIT, DDT or directly via system calls) to
79 inform the system which file should be used as a BDH.  For instance,
80 if we translate all file names of the form "XXX:" into the file name
81 "JOB:ABC;TS RUNXXX", then the system will use the file "DSK:ABC;TS
82 RUNXXX" as the BDH for any file name whose device code is "XXX".
83
84      The other method is much simpler and is designed for non-standard
85 devices that are to be made available to the entire user community.
86 When an OPEN is attempted on a device whose name the system does not
87 recognize, the system will examine the "DEVICE;" directory for a file
88 whose first name is "JOBDEV" and whose second name is the unrecognized
89 device name.  If such a file exists, then the contents of that file
90 will be loaded by the system and used as the BDH for the user's OPEN.
91
92 USING THE BOJ DEVICE - THE BOJ DEVICE HANDLER
93
94      The BDH is the workhorse in the JOB/BOJ device scheme.  It is
95 required to interpret all of the user's I/O operations.  It must do
96 everything to make the non-standard device that it represents appear
97 standard.  The system provides five .CALLs to give the BDH some of the
98 required communcation with the user.
99
100      The first thing that the BDH should do when he is loaded and
101 started by the system is to execute an OPEN on the BOJ device.  This
102 establishs a logical link between the user and the BDH.  The BDH may
103 enable a channel interrupt on the BOJ channel, in order to be informed
104 whenever the user attempts an I/O operation.
105
106      It should be noted that the ITS system allows for the possibility
107 of a full duplex connection between the user and the BDH (i.e. a
108 channel in each direction).  If a user attempts an open for reading
109 and an open for writing on the same device, then the system will
110 present both opens to the same activation of the BDH.  Should this be
111 undesirable for a particular BDH, that BDH should use an open mode
112 with the 3.4 bit set (10 in the left half) each time it attempts to
113 open the BOJ device.
114
115 JOBCAL SYSTEM CALL
116
117      The first .CALL that will be discussed is the JOBCAL.  This call
118 enables the BDH to find out what I/O operation the user has attempted
119 to execute.  It has the following calling sequence:
120
121         .CALL   JOBCAL
122         ...             ; Error return
123         ...             ; Success return
124
125 JOBCAL: SETZ
126         SIXBIT/JOBCAL/
127         [<BOJ channel number>]
128         2000,,<opcode>
129         SETZ [-<n>,,<data>]
130
131 This .CALL, as well as the other three discussed below, will fail to
132 skip if any of the following conditions hold:
133
134         1.  <BOJ channel number> is an illegal channel number.
135         2.  <BOJ channel number> is not open on the BOJ device.
136         3.  the user job does not exist or as has closed his channel.
137         4.  the user job is no longer attempting to execute the I/O
138             operation.
139
140      <opcode> should be an address into which information about
141 the I/O operation requested will be placed.  This word contains three
142 sections.  The right half of the word contains an integer indicating
143 which operation was attempted.  The following table indicates what the
144 various integers mean:
145
146        CODE  OPERATION
147
148         0    OPEN
149         1    IOT (transfer)
150         2    STATUS (currently unused)
151         3    RESET
152         4    RCHST (complete status)
153         5    ACCESS
154         6    FDELE (for delete or rename while not open)
155         7    FDELE (for rename while open)
156         8    CALL (see below)
157
158 It is up to the BDH to insure that these I/O operations perform as
159 they would for standard ITS devices.
160
161      The top three bits (4.9-4.7) of the op-code word contain the mode
162 in which user is attempting to open his channel.  This is only
163 meaningful for op-code 0, OPEN.  If either of the next two bits (4.6
164 and 4.5) are non-zero, then the user is requesting that a channel be
165 closed.  As was mentioned above, the system allows the connection
166 between user and BDH to be full duplex.  If the 4.6 bit of the opcode
167 is set, then the user is requesting that his input channel be closed,
168 while the 4.5 bit requests a close of the user's output channel.  It
169 is possible to get an opcode in which both bits are set.  In the case
170 the BDH should perform the obvious task of closing both channels.
171
172      Most I/O operations require more information than just the name
173 of the operation.  The third argument to JOBCAL tells the system where
174 any additional information that is available should be placed.
175 <n> is the largest number of words that the BDH is willing to
176
177 accept (it should normally be 12).  <data> is the address where
178 the first word of the information should be placed.  What information
179 is placed in this buffer is determined by the I/O operation being
180 attempted.
181
182      If the op-code (in the right half of the op-code word) is less
183 than 8, then five words will be written into the buffer (beginning at
184 <data>) in response to the JOBCAL.  The meanings of the words are
185 given in the following table:
186
187 WORD          OPERATIONS               MEANING
188
189   1      IOT (1)                  Meaningful only for block IOTs.
190                                   This word will contain the user's
191                                   IOT pointer.  The left half of this
192                                   word will be the negative of the
193                                   number of words that the user wants.
194
195   1      ACCESS (5)               The address within the virtual file
196                                   that is open from (or to) which the
197                                   next IOT should be done.  The first
198                                   word of the virtual file is word
199                                   zero.
200
201   1      FDELE (6 or 7)           Zero implies delete requested.
202                                   Non-zero means that a rename is
203                                   requested.  This word is the name
204                                   (in sixbit) to be used as the new
205                                   first file name.
206
207   2      OPEN (0) or FDELE (6)    First name of the file that the user
208                                   is attempting to open, rename or
209                                   delete.
210
211   3      OPEN (0) or FDELE (6)    Second name of the file that the
212                                   user is attempting to open, rename
213                                   or delete.
214
215   4      OPEN (0) or FDELE (6)    Name of directory to be used for
216                                   open, rename or delete.
217
218   5      OPEN (0) or FDELE (6)    Name of device to be used for open,
219                                   rename or delete.
220
221   6      OPEN (0)                 The full 18 bit open mode (right
222                                   justified).
223
224   6      FDELE (6 or 7)           Zero implies delete.  Non-zero means
225                                   that a rename is requested.  This
226                                   word is the name (in sixbit) to be
227                                   used as the new second file name.
228
229      If the op-code returned by JOBCAL is 8, then the user has
230 attempted some I/O operation not recognized by the systems JOB/BOJ
231 device code (this does not mean that the operation is illegal).  In
232 this case, more information is written into the BDH's buffer (up to
233 twelve words).  The first of the data words will be the SIXBIT name of
234 the operation being performed.  (This will normally be the name of the
235 .CALL executed by the user - see Appendix 1 for a list of
236 possiblities.) The second word will contain all of the flags that the
237 user has set using the "flag" feature of the .CALL UUO.  The third
238 word will be an integer indicating how many input (to the system)
239 arguments the user supplied in his .CALL.  It should be noted that if
240 this integer is larger than <n>+3, then some information will be
241 lost.  The values of the input arguments will appear in the remainder
242 of the words in the buffer.  Remember that, in almost all cases, the
243 first of the input arguments will be the user's channel number.
244
245 JOBRET SYSTEM CALL
246
247      Once the BDH has interpreted the user's request for execution of
248 an I/O operation, the BDH must have some way of responding to the
249 user.  This is provided with the JOBRET .CALL.  This .CALL serves
250 three purposes:
251
252         1.  to unblock the user who is waiting for completion of his
253             I/O request (possibly causing the request to skip)
254
255         2.  to set lossage codes in the user's status words (e.g. for
256             failed opens).
257
258         3.  to return information requested by the user's I/O
259             operation.
260
261 The format of this CALL is as follows:
262
263         .CALL   JOBRET
264         ...             ; error return
265         ...             ; success return
266
267 JOBRET: SETZ
268         SIXBIT /JOBRET/
269         [<BOJ channel number>]
270         [<return>]
271         SETZ [-<n>,,<data>]
272
273 where <return> satisfies requirements 1 and 2 above.  If
274 <return> is zero, then the user's I/O call will not skip or set
275 status bits.  If <return> is of the form <i>,,<j> , then
276 <i> will be placed in the "open-loss" field of the status word for
277 the user's channel and the user's I/O call will skip <j> times.  A
278 list of all currently recognized open loss codes can be found in
279
280 Appendix 2.
281
282      If the user's I/O call requested data, then the BDH can supply
283 this data by supplying the third argument to the JOBRET call.  The
284 system will use the <n> words beginning at location <data> as
285 the values for the return arguments in the user's call.
286
287 SETIOC AND JOBINT -- INTERRUPT SYSTEM CALLS
288
289      There are two interrupt oriented functions that the BDH must
290 perform and there are CALLs available to perform them.  The first is
291 to notify the user when he has done something catastrophic.  The user
292 should be notified of his error by causing the system to awaken him
293 with an I/O channel error.  This done with the SETIOC call, as
294 follows:
295
296         .CALL   SETIOC
297         ...             ; error return (standard reasons
298                                 + illegal IOCERR code)
299         ...             ; success return
300
301 SETIOC: SETZ
302         SIXBIT/SETIOC/
303         [<BOJ channel number>]
304         SETZ [<IOCERR code>]
305
306 where <IOCERR code> is an integer identifying the reason for the
307 error.  This integer should be chosen from the list that can be found
308 in Appendix 2.  Since BDHs are written to handle non-standard devices,
309 these error codes will quite often not apply.  Simply choose the code
310 that comes closest.
311
312      Occassionally, the BDH must notify the user of a non-catastrophic
313 situation (e.g. the arrival of data).  The JOBINT call allows the BDH
314 to give the user a second-word I/O channel interrupt, as follows:
315
316         .CALL   JOBINT
317         ...             ; error return
318         ...             ; success return
319
320 JOBINT: SETZ
321         SIXBIT /JOBINT/
322         SETZ [<BOJ channel number>]
323
324 JOBSTS SYSTEM CALL
325
326      Finally, the BDH must have some way to notify the system what the
327 status of the pseudo-device is.  For this purpose, the JOBSTS call is
328 available.  Its calling sequence is as follows:
329
330         .CALL   JOBSTS
331         ...             ; error return
332         ...             ; success return
333
334 JOBSTS: SETZ
335         SIXBIT /JOBSTS/
336         [<BOJ channel number>]
337         SETZ [<new status>]
338
339 The right half of <new status> will be given to anyone requesting
340 the status of the user's channel until another JOBSTS is done.  It
341 should be remembered that the low order six bits (1.1-1.6) should
342 contain the device code of the pseudo-device.  Unless, you know what
343 you are doing, this should always be 22 octal.
344
345 DATA TRANSFERS - HOW TO IOT
346
347      Since the main purposes of I/O devices is transfer of
348 information, let us look at how the BDH manages to respond to the
349 user's IOTs.  The method for doing this is very simply.  If we think
350 back to the diagram presented early in this memo, we will remember
351 that the BOJ and JOB channels (i.e. the channels belonging to the BDH
352 and the user, respectively) are logically connected.  The full meaning
353 of this now becomes clear.  If the JOB channel is open for input and
354 the BOJ channel is open for output, then anything that BDH outputs on
355 the BOJ channel will be available to the user as input on the JOB
356 channel.  (For this reason, the BDH must insure that he opens the BOJ
357 channel in the opposite direction from the user's JOB channel even if
358 this requires opening the BOJ channel a second time in the correct
359 mode.)
360
361      There are two features of which the BDH implementer should be
362 aware when writing the I/O sections of the BDH.  Let us assume for the
363 discussion here, that the user has the JOB channel open for reading
364 and that the BDH has the BOJ channel open for writing.  Let us also
365 assume that the user is currently hung attempting to read 100 words on
366 his JOB channel.
367
368      First it should be noted that the BDH does not have to respond to
369 this request with a single transfer of 100 words.  The system will act
370 as an mediator between different transfer sizes.  The BDH can respond
371 to the user's request for 100 words in several ways:
372
373         1.  He can send all 100 words in small pieces (e.g. 10
374             transfers of 10 words each.
375         2.  He can send all 100 words in a single transfer.
376         3.  He can send more than 100 words.  In this case, the BDH
377             will remain hung in his IOT until the user has read all of
378             the data that the BDH is attempting to send.  (If this is
379             undesirable, set the 3.5 in the BOJ open mode.  This will
380             cause BOJ IOTs to unhang whenever the user's IOT is
381             satisfied. When that happens, the BOJ IOT pointer will
382             have been counted out only partially; the RH will point to
383             the first word not transfered)
384         4.  He may send less than 100 words.  In this case, the BDH
385             must manually awaken the user as described below.
386
387 The user, who is hung awaiting his 100 words, will stay hung until he
388 has received all 100 words.  Suppose, however, that the BDH only
389 wishes to send 50 words (e.g. the last 50 words of the virtual file).
390 To do this, he can send the 50 words normally, but must then use the
391 JOBRET call (described above) to awaken the user.  JOBRET should be
392 called with the BOJ channel number as the first argument, zero as the
393 second argument and no third argument.  Secondly, the BDH implementer
394 must be aware that the ITS system guarantees that IOTs to a channel
395 open in block mode, will never generate an I/O channel error.  In
396 other words, the following algorithm should be followed:
397
398         1.  If the user requests n words and there are n words or more
399             left in the "file", give him n words.
400         2.  If the user requests n words and there are only m words
401             (n>m), give him m words and manually awaken him (using
402             JOBRET).
403         3.  If the user requests n words and there are no words left,
404             then give him nothing and manually awaken him (using
405             JOBRET).
406
407 HINTS TO BDH IMPLEMENTERS
408
409      The following hints should ease the task of BDH implementers a
410 little.  It is hoped that anyone who attempts to a BDH implementation
411 will add his harshly acquired knowledge to this section of this memo.
412
413 1.  Remember that the BDH is essentially a disowned job and should
414     attempt to log out after a close has been requested.
415
416 2.  Be very leery of logging out for any reason other than a requested
417     CLOSE.  In particular:
418
419     a.  if a JOBCAL fails, only log out if a request for OPEN has not
420         yet been received.
421     b.  when you generate an I/O channel error for the user, only log
422         out if the error is irrecoverable.  Remember that he can
423         correct an access beyond end-of-file by doing an access before
424         attempting another IOT.
425     c.  if he requests an I/O operation that you do not recognize,
426         generate a "mode not available" error (via JOBRET) and wait
427         for his next request.
428     d.  if you decide, for your own reasons, to make the initial open
429         fail, you should log out.
430
431 THE OJB DEVICE -- AN AID FOR DEBUGGING THE BDH
432
433      One of the problems with debugging BDH programs is that when the
434 JOB device is used, it is loaded into a newly created job. That job is
435 not inferior to any DDT, and there is no way to put breakpoints in it
436 before it starts. The OJB device makes it possible to run the BDH
437 program under DDT.
438
439      The OJB device acts just like the JOB device except during the
440 initial open. When the JOB device would be creating and loading a new
441 job, the OJB device is looking for an existing job whose UNAME and
442 JNAME are the same as the filenames specified in the open.  If such a
443 job is found, it is connected to the job opening the OJB device
444 through a standard JOB-BOJ pipeline.  If such a job does not exist,
445 the open of the OJB device fails.  In order to protect innocent jobs
446 from being hacked in this matter, the job opened on the OJB device is
447 required to have set its OPTOJB bit (this is bit 4.2 in the .OPTION
448 varisble, settable with .SUSET).  Also, it must not already be a BDH,
449 for the system cannot consider one job to be a BDH through two
450 connections at once.  If either of those conditions is not met, the
451 OJB open WAITS until they are.
452
453      To remove a possible timing screw, a BOJ device open by a job
454 that is not a BDH, which usually fails, will wait instead for the job
455 to become a BDH if the job's OPTOJB bit is set.
456
457      The procedure for using the OJB device for debugging is:
458
459      1.  create a job to use for the BDH (call it J, in this example).
460      2.  run (in another job) the program which would normally open
461          the JOB device, but tell it (perhaps by means of a translation)
462          to use the OJB device instead.  This program's function is to
463          issue system calls so that the BDH's responses to them can be
464          tested.
465
466          Since the OPTOJB bit of job J is now 0, when this program reaches
467          the OJB open it will hang.
468
469      3.  ^Z that program, and ^P it. it will go back to hanging in the
470          open of the OJB device.
471      4.  switch to job J, load in the BDH program.  This RESET's the
472          job so that if it had been a BDH before, it will not be one now.
473      5.  turn on the OPTOJB bit by depositing from DDT in .OPTION.
474
475 The conditions for a successful OJB open by job JJ have now been met,
476 so that job will make some headway, turning job J into a BDH and
477 waiting for job J to issue a JOBRET.
478
479      6.  it is now possible to start running job J, with breakpoints
480 as desired, to step through the code for handling the initial open.
481 It is unlikely, but theoretically possible, for J to execute its BOJ
482 open before JJ gets around to turning J into a BDH.  In this case, J
483 will wait for JJ to do so.
484
485      7.  if it becomes necessary to try again after discovering a bug,
486 go back to step 2.  The OJB open will hang up this time not because
487 the OPTOJB bit is off (since it is still 1) but because J is already a
488 BDH.  Loading J in step 4 will make J cease to be a BDH but also turn
489 off the OPTOJB bit, so JJ will still be waiting.
490
491      8.  it is possible for job JJ to be "PCLSR'ed" or backed up out
492 of its open while job J is being traced through the handling of the
493 initial open.  For example, job JJ might receive a real-time
494 interrupt.  If that happens, J will cease to be a BDH.  This will not
495 interfere with the tracing of J until the next BOJ device system call
496 is executed; that call will probably fail.  It is impossible to
497 continue after such an occurrence, so the jobs must be restarted by
498 returning to step 2.  Of course, if there are no breakpoints before
499 the first JOBRET, this is very unlikely to happen.
500
501      9.  if the initial open has been handled successfully, to go on
502 to debug handling of other system calls, simply tell JJ to execute
503 them with J stopped while JJ is being told, then ^Z^P JJ and go back
504 to J.
505
506      10. the usual .LOGOUT in the BDH program will be a no-op when the
507 program is run as an inferior in this manner, so it should be followed
508 by a .VALUE.