Subversion Repositories Kolibri OS

Rev

Rev 387 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; IRQ0 HANDLER (TIMER INTERRUPT) ;;
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4.  
  5.  
  6. align 32
  7. irq0:
  8.         pushfd
  9.         pushad
  10.         mov   ax, app_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.         cmp   [DONT_SWITCH], byte 1
  26.         jne   .change_task
  27.  
  28.         mov   al,0x20   ; send End Of Interrupt signal
  29.         mov   dx,0x20
  30.         out   dx,al
  31.  
  32.         mov   [DONT_SWITCH], byte 0
  33.  
  34.         popad
  35.         popfd
  36.         iretd
  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.         popad
  55.         popfd
  56.         iretd
  57.  
  58.  
  59. align 4
  60. change_task:
  61.  
  62.         pushfd
  63.         cli
  64.         pushad
  65.  
  66.         call  update_counters
  67. ; \begin{Mario79}
  68.         cmp     [dma_task_switched], 1
  69.         jne     .find_next_task
  70.         mov     [dma_task_switched], 0
  71.         mov     ebx, [dma_process]
  72.         cmp     [CURRENT_TASK], ebx
  73.         je      .return
  74.         mov     edi, [dma_slot_ptr]
  75.         mov     [CURRENT_TASK], ebx
  76.         mov     [TASK_BASE], edi
  77.         jmp     @f
  78. .find_next_task:
  79. ; \end{Mario79}
  80.         call  find_next_task
  81.         test  eax, eax    ; the same task -> skip switch
  82.         jnz    .return
  83. @@:
  84.         mov   [DONT_SWITCH],byte 1
  85.         call  do_change_task
  86.  
  87. .return:
  88.         popad
  89.         popfd
  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.         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.         rdtsc                     ;call  _rdtsc
  169.         mov   [edi+TASKDATA.counter_add],eax
  170.  
  171.         mov esi, [prev_slot]
  172.         xor   eax, eax
  173.         cmp   ebx, esi
  174.         sete  al
  175. ret
  176.  
  177. ; in: ebx = TSS selector index
  178. ;
  179. ; param
  180. ;  ebx = incoming task
  181. ;  esi = outcomig task
  182.  
  183. do_change_task:
  184.  
  185.         shl ebx, 8
  186.         add ebx, SLOT_BASE
  187.         mov [current_slot], ebx
  188.         shl esi, 8
  189.         add esi, SLOT_BASE
  190.  
  191.         mov [esi+APPDATA.saved_esp], esp
  192.         mov esp, [ebx+APPDATA.saved_esp]
  193.  
  194.         mov eax, [ebx+APPDATA.dir_table]
  195.         mov cr3, eax
  196.  
  197.         mov ebx, [ebx+APPDATA.pl0_stack]
  198.         add ebx, RING0_STACK_SIZE
  199.         mov [tss_data+TSS._esp0], ebx
  200.         mov ecx, cr0
  201.         or ecx, CR0_TS   ;set task switch flag
  202.         mov cr0, ecx
  203.         inc   [context_counter] ;noname & halyavin
  204.  
  205.         ret
  206.  
  207. ;
  208. ;        shl   ebx, 3
  209. ;        xor   eax, eax
  210. ;        add   ebx, tss0
  211. ;        mov   [far_jump.sel],  bx   ; selector
  212. ;        mov   [far_jump.offs], eax  ; offset
  213. ;        jmp   pword [far_jump]
  214. ;        inc   [context_counter] ;noname & halyavin
  215. ;ret
  216.  
  217.  
  218.  
  219. align 4
  220. updatecputimes:
  221.  
  222.         mov  eax,[idleuse]
  223.         mov  [idleusesec],eax
  224.         mov  [idleuse],dword 0
  225.         mov  ecx, [TASK_COUNT]
  226.         mov  edi, TASK_DATA
  227. .newupdate:
  228.         mov  ebx,[edi+TASKDATA.counter_sum]
  229.         mov  [edi+TASKDATA.cpu_usage],ebx
  230.         mov  [edi+TASKDATA.counter_sum],dword 0
  231.         add  edi,0x20
  232.         dec  ecx
  233.         jnz  .newupdate
  234.  
  235.         ret
  236.