home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / kernel.zip / K3.ASM < prev    next >
Assembly Source File  |  1986-11-25  |  8KB  |  306 lines

  1. ;Listing 3    Scheduling Algorithm (Assembly Subroutines)
  2. ;(C) Copyright 1986 Ken Berry.
  3. ;All rights reserved.
  4. ;Copies may be made for non-commercial, private use only.
  5.  
  6.  
  7.       include tele.mac ; system definitions (listing 2)
  8.  
  9.       extrn t_astrm:byte ; application task termination flag
  10.       extrn t_rtmark:near ; update pseudo time accumulator
  11.       extrn t__krnl:near ; security kernel
  12.  
  13.       public sys_task ; current task pointer
  14.       public sys_tsch ; task scheduling table pointer
  15.       public sys_ilck ; system task interlock
  16.       public sys_asbs ; application stack base
  17.       public sys_dgrp ; data segment storage
  18.       public sys_ssbs ; system stack base
  19.       public sys_sssz ; system stack size
  20.       public sys_sstp ; system stack top
  21.       public sys_stat ; original register block
  22.       public t_mnxtm ; minimum execution time
  23.       public t_rels  ; release
  24.       public t_spnd  ; suspend
  25.       public t_syxtm ; system pseudo time accumulator
  26.       public t_term  ; reschedule
  27.       public t_wait  ; wait
  28.       public t_wqupd ; wait queue update task pointer
  29.       public t__crtss ; current task ss storage
  30.       public t__crtsp ; current task sp storage
  31.       public t__dspap ; dispatch application
  32.       public t__dspsy ; dispatch system
  33.       public t__sstk ; establish system stack
  34.       public t__syntr ; enter system state
  35.       public t__syxit ; exit system state
  36.  
  37. MINXTM      equ 500     ; minimum execution time
  38. STKLN      equ 1024     ; system stack size
  39.  
  40.       dseg
  41.  
  42. tmrdx      dw 0         ; dx storage
  43. spdss      dw 0         ; ss storage
  44. spdsp      dw 0         ; sp storage
  45.  
  46. t__crtss  dw 0         ; current task ss storage
  47. t__crtsp  dw 0         ; current task sp storage
  48.  
  49. sys_stat  db type sys_parm dup (0) ; original register block
  50. sys_dgrp  dw 0         ; data segment storage
  51. sys_task  dw 0         ; current task pointer
  52. sys_tsch  dw 0         ; task scheduling table pointer
  53. sys_asbs  dw 0         ; application stack base
  54. sys_ssbs  dw stkbs     ; system stack base
  55. sys_sssz  dw STKLN     ; system stack length
  56. sys_sstp  dw STKLN     ; system stack top
  57. sys_ilck  db 0FFh     ; system task interlock
  58.  
  59. t_wqupd   dw 0         ; wait queue update task pointer
  60.  
  61. t_syxtm   dw 3 dup (0)     ; system pseudo time accumulator
  62.  
  63. t_mnxtm   dw MINXTM     ; minimum execution time
  64.  
  65. stkbs      db STKLN dup (0) ; system stack
  66.       db type t_task dup (0) ; main task control table
  67.  
  68.       endds
  69.  
  70.       pseg
  71.  
  72. comment ~
  73.  
  74. t__dspap(ss,sp)
  75. selector ss;
  76. unsigned sp;
  77.  
  78. ss  and  sp are placed in the stack registers.    Then the other    registers  are
  79. restored from the new stack.  Control passes to the restored task.  The return
  80. address  is left at the top of the system stack.  Therefore the restored  task
  81. may use the system stack to return to the caller of t__dspap. ax may contain a
  82. return code in this case.
  83. ~
  84.  
  85. t__dspap  proc near
  86.       push bp     ; protect bp
  87.       mov bp,sp     ; establish parameter addressability
  88.       mov ax,[bp].t_np0 ; set application stack
  89.       mov bx,[bp].t_np1
  90.       mov sys_sstp,sp ; store current top of system stack
  91.       cli
  92.       mov ss,ax
  93.       mov sp,bx
  94.       mov bp,sp     ; enable interrupts
  95.       or [bp].t_xpf,0200h
  96.       pop sys_asbs
  97.       sti
  98.       sys__rctx     ; restore context
  99.       cli         ; interrupts off
  100.       mov byte ptr sys_ilck,0 ; exit system state
  101.       mov byte ptr t_astrm,0 ; initialize application interval
  102.       pop ds     ; restore ds
  103.       iret         ; execute task
  104. t__dspap  endp
  105.  
  106. comment ~
  107.  
  108. t_term()       _F
  109. t_spnd(tp)     tp
  110. t_wait(tp)     tp
  111. unsigned tp;
  112. t_rels()       _E
  113.  
  114. All of these functions are similar.  The processor registers are stored on
  115. the stack,  which  is  then  adjusted  to  match the pattern for interrupt
  116. returns. Finally the system stack is established.  The    functions  differ  in
  117. the  code returned  to    the caller of function t__dspap.  t__dspap restores
  118. the registers and returns control to the caller of these functions.  The
  119. returned  value  is shown with the appropriate call above.  tp is only used
  120. with t_spnd and t_wait.  It is the number of system ticks to  wait  before
  121. executing  the    task  again.  t_wait functions like t_spnd, except that t_rels
  122. is invoked immediately.  ~
  123.  
  124. t_term      proc near
  125.       call t__trmap  ; protect registers
  126.       xor ax,ax     ; return _F
  127.       ret
  128. t_term      endp
  129.  
  130. t_spnd      proc near
  131.       mov spdss,ss     ; store stack pointers
  132.       mov spdsp,sp
  133.       call t__trmap  ; protect registers
  134.       mov es,spdss     ; return tick count
  135.       mov si,spdsp
  136.       mov ax,word ptr es:[si+2]
  137.       push ds     ; set es = ds
  138.       pop es
  139.       ret         ; return
  140. t_spnd      endp
  141.  
  142. t_wait      proc near
  143.       push bp     ; protect bp
  144.       mov bp,sp     ; establish stack addressability
  145.       mov ax,[bp].t_np0 ; suspend task
  146.       push ax
  147.       call t_spnd
  148.       mov sp,bp     ; unload stack
  149.       pop bp     ; restore bp
  150. t_rels      proc near
  151.       call t__trmap  ; protect registers
  152.       xor ax,ax     ; return _E
  153.       dec ax
  154.       ret
  155. t_rels      endp
  156. t_wait      endp
  157.  
  158. comment ~
  159.  
  160. t__dspsy()
  161.  
  162. A call to function t__trmap is made so that after the registers are stored in
  163. the  application stack (and the system stack is made current),    control passes
  164. to  function t__krnl,  the system security kernel.  Control will  return  from
  165. t__dspsy when the calling task is resumed. Nothing is returned.
  166. ~
  167.  
  168. t__dspsy  proc near
  169.       mov ax,offset pgroup:t__krnl ; branch to system
  170.       push ax
  171.       sub sys_sstp,2 ; adjust system stack (for "pop bp" in t__sstk)
  172.  
  173. comment ~
  174.  
  175. t__trmap()
  176.  
  177. The  machine registers are stored on the application stack.  Then the system
  178. stack is made current.    The return address from the call to t__trmap is put on
  179. the system stack before returning to it. Nothing is returned.
  180. ~
  181.  
  182. t__trmap  proc near
  183.       mov byte ptr sys_ilck,0FFh ; force system state
  184.       mov tmrdx,dx     ; save dx
  185.       pop dx     ; set return address (from t__trmap)
  186.       push cs     ; protect cs
  187.       pushf      ; protect flags
  188.       push ds     ; protect ds
  189.       push ax     ; protect ax
  190.       push bx     ; protect bx
  191.       push cx     ; protect cx
  192.       push tmrdx     ; protect dx
  193.       push si     ; protect si
  194.       push di     ; protect di
  195.       push bp     ; protect bp
  196.       push es     ; protect es
  197.       push dx     ; restore return address to stack
  198.       mov bp,sp     ; establish stack addressability
  199.       mov ax,[bp].t_xip ; adjust stack for interrupt return
  200.       xchg ax,[bp].t_xpf
  201.       mov [bp].t_xip,ax
  202.  
  203. comment ~
  204.  
  205. t__sstk()
  206.  
  207. The  current application stack pointers are stored.  Then the system stack is
  208. established as the current stack.  The return address from the call is placed
  209. on the system stack before returning into it. Nothing is returned.
  210. ~
  211.  
  212. t__sstk   proc near
  213.       pop dx     ; unload return address
  214.       push sys_asbs  ; protect stack protection reference
  215.       mov bx,ss     ; set application stack registers
  216.       mov cx,sp
  217.       mov ax,sys_ssbs ; set system stack
  218.       cli
  219.       mov sys_asbs,ax
  220.       push ds
  221.       pop ss
  222.       mov sp,sys_sstp
  223.       sti
  224.       pop bp     ; restore bp
  225.       mov t__crtss,bx ; store current ss
  226.       mov t__crtsp,cx ; store current bp
  227.       push dx     ; return to caller
  228.       ret
  229. t__sstk   endp
  230. t__trmap  endp
  231. t__dspsy  endp
  232.  
  233. comment ~
  234.  
  235. t_sync(flg)
  236. char *flg;
  237.  
  238. A wait loop will be entered until the required resource is available. This is
  239. indicated  by flg containing 0x00.  0xFF is stored to prevent any other tasks
  240. from acquiring the resource. The resource is released by resetting flg to
  241. 0x00.
  242. ~
  243.  
  244. t_sync      proc near
  245.       push bp     ; protect bp
  246.       mov bp,sp     ; establish stack addressability
  247.       mov bx,[bp].t_np0 ; set pointer to resource flag
  248.  
  249. sync1:      mov al,0FFh     ; interlock token
  250.       ilck al,<byte ptr [bx]>
  251.       or al,al     ; test for token acquired
  252.       jz sync2
  253.       xor ax,ax     ; wait for 1 system tick
  254.       inc ax
  255.       push ax
  256.       call t_spnd
  257.       mov sp,bp
  258.       jmp sync1     ; continue
  259.  
  260. sync2:      pop bp     ; restore bp
  261.       call t_rels     ; release task
  262.       ret         ; return
  263. t_sync      endp
  264.  
  265. comment ~
  266.  
  267. t__syntr(flg)
  268. char *flg;
  269.  
  270. This function expands the sys_entr macro for use by c functions.
  271. ~
  272.  
  273. t__syntr  proc near
  274.       push bp     ; protect bp
  275.       mov bp,sp     ; establish stack addressability
  276.       mov bx,[bp].t_np0 ; set flag address
  277.       sys_entr <byte ptr [bx]> ; enter system state
  278.       pop bp     ; restore bp
  279.       ret         ; return
  280. t__syntr  endp
  281.  
  282.  
  283. comment ~
  284.  
  285. t__syxit(flg)
  286. char *flg;
  287.  
  288. This function expands the sys_exit macro for use by c functions.
  289. ~
  290.  
  291. t__syxit  proc near
  292.       push bp     ; protect bp
  293.       mov bp,sp     ; establish stack addressability
  294.       mov bx,[bp].t_np0 ; set flag address
  295.       sys_exit <byte ptr [bx]> ; exit system state
  296.       pop bp     ; restore bp
  297.       ret         ; return
  298. t__syxit  endp
  299.  
  300.       endps
  301.  
  302.       end
  303.  
  304.  
  305.  
  306.