Subversion Repositories Kolibri OS

Rev

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

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