Subversion Repositories Kolibri OS

Rev

Rev 381 | Rev 431 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. $Revision: 425 $
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;; IRQ0 HANDLER (TIMER INTERRUPT) ;;
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5.  
  6.  
  7. align 32
  8. irq0:
  9.         save_ring3_context
  10.         mov   ax, os_data
  11.         mov   ds, ax
  12.         mov   es, ax
  13.  
  14.         inc   dword [timer_ticks]
  15.  
  16.         mov   eax, [timer_ticks]
  17.         call  playNote           ; <<<--- Speaker driver
  18.  
  19.         cmp   eax,[next_usage_update]
  20.         jb    .nocounter
  21.         add   eax,100
  22.         mov   [next_usage_update],eax
  23.         call  updatecputimes
  24.      .nocounter:
  25.  
  26.         cmp   [DONT_SWITCH], byte 1
  27.         jne   .change_task
  28.  
  29.         mov   al,0x20   ; send End Of Interrupt signal
  30.         mov   dx,0x20
  31.         out   dx,al
  32.  
  33.         mov   [DONT_SWITCH], byte 0
  34.  
  35.         restore_ring3_context
  36.         iret
  37.  
  38.      .change_task:
  39.         call  update_counters
  40.  
  41.         call  find_next_task
  42.         mov   ecx, eax
  43.  
  44.         mov   al,0x20   ; send End Of Interrupt signal
  45.         mov   dx,0x20
  46.         out   dx,al
  47.  
  48.         test  ecx, ecx  ; if there is only one running process
  49.         jnz   .return
  50.  
  51.         call  do_change_task
  52.  
  53.      .return:
  54.         restore_ring3_context
  55.         iret
  56.  
  57.  
  58. align 4
  59. change_task:
  60.  
  61.         pushfd
  62.         cli
  63.         pushad
  64.  
  65.         call  update_counters
  66. ; \begin{Mario79}
  67.         cmp     [dma_task_switched], 1
  68.         jne     .find_next_task
  69.         mov     [dma_task_switched], 0
  70.         mov     ebx, [dma_process]
  71.         cmp     [CURRENT_TASK], ebx
  72.         je      .return
  73.         mov     edi, [dma_slot_ptr]
  74.         mov     [CURRENT_TASK], ebx
  75.         mov     [TASK_BASE], edi
  76.         jmp     @f
  77. .find_next_task:
  78. ; \end{Mario79}
  79.         call  find_next_task
  80.         test  eax, eax    ; the same task -> skip switch
  81.         jnz    .return
  82. @@:
  83.         mov   [DONT_SWITCH],byte 1
  84.         call  do_change_task
  85.  
  86.      .return:
  87.         popad
  88.         popfd
  89.  
  90.         ret
  91.  
  92.  
  93. uglobal
  94.    align 4
  95.    far_jump:
  96.     .offs dd ?
  97.     .sel  dw ?
  98.    context_counter     dd ? ;noname & halyavin
  99.    next_usage_update   dd ?
  100.    timer_ticks         dd ?
  101.    prev_slot           dd ?
  102.    event_sched         dd ?
  103. endg
  104.  
  105.  
  106. update_counters:
  107.         mov   edi, [TASK_BASE]
  108.         mov   ebx, [edi+TASKDATA.counter_add] ; time stamp counter add
  109.         call  _rdtsc
  110.         sub   eax, ebx
  111.         add   eax, [edi+TASKDATA.counter_sum] ; counter sum
  112.         mov   [edi+TASKDATA.counter_sum], eax
  113. ret
  114.  
  115.  
  116. ; Find next task to execute
  117. ; result: ebx = number of the selected task
  118. ;         eax = 1  if the task is the same
  119. ;         edi = address of the data for the task in ebx
  120. ;         [0x3000] = ebx and [0x3010] = edi
  121. ;         corrupts other regs
  122. find_next_task:
  123.         mov   ebx, [CURRENT_TASK]
  124.         mov   edi, [TASK_BASE]
  125.         mov   [prev_slot], ebx
  126.  
  127.       .waiting_for_termination:
  128.       .waiting_for_reuse:
  129.       .waiting_for_event:
  130.       .suspended:
  131.         cmp   ebx, [TASK_COUNT]
  132.         jb    @f
  133.         mov   edi, CURRENT_TASK
  134.         xor   ebx, ebx
  135.       @@:
  136.  
  137.         add   edi,0x20
  138.         inc   ebx
  139.  
  140.         mov   al, byte [edi+TASKDATA.state]
  141.         test  al, al
  142.         jz    .found
  143.         cmp   al, 1
  144.         jz    .suspended
  145.         cmp   al, 2
  146.         jz    .suspended
  147.         cmp   al, 3
  148.         je    .waiting_for_termination
  149.         cmp   al, 4
  150.         je    .waiting_for_termination
  151.         cmp   al, 9
  152.         je    .waiting_for_reuse
  153.  
  154.         mov   [CURRENT_TASK],ebx
  155.         mov   [TASK_BASE],edi
  156.  
  157.         cmp   al, 5
  158.         jne   .noevents
  159.         call  get_event_for_app
  160.         test  eax, eax
  161.         jz    .waiting_for_event
  162.         mov   [event_sched], eax
  163.         mov   [edi+TASKDATA.state], byte 0
  164.       .noevents:
  165.       .found:
  166.         mov   [CURRENT_TASK],ebx
  167.         mov   [TASK_BASE],edi
  168.         call  _rdtsc
  169.         mov   [edi+TASKDATA.counter_add],eax
  170.  
  171.         xor   eax, eax
  172.         cmp   ebx, [prev_slot]
  173.         sete  al
  174. ret
  175.  
  176. ; in: ebx = TSS selector index
  177. do_change_task:
  178.         shl   ebx, 3
  179.         xor   eax, eax
  180.         add   ebx, tss0
  181.         mov   [far_jump.sel],  bx   ; selector
  182.         mov   [far_jump.offs], eax  ; offset
  183.         jmp   pword [far_jump]
  184.         inc   [context_counter] ;noname & halyavin
  185. ret
  186.  
  187.  
  188.  
  189. align 4
  190. updatecputimes:
  191.  
  192.         mov  eax,[idleuse]
  193.         mov  [idleusesec],eax
  194.         mov  [idleuse],dword 0
  195.         mov  ecx, [TASK_COUNT]
  196.         mov  edi, TASK_DATA
  197.       .newupdate:
  198.         mov  ebx,[edi+TASKDATA.counter_sum]
  199.         mov  [edi+TASKDATA.cpu_usage],ebx
  200.         mov  [edi+TASKDATA.counter_sum],dword 0
  201.         add  edi,0x20
  202.         dec  ecx
  203.         jnz  .newupdate
  204.  
  205.         ret
  206.