Subversion Repositories Kolibri OS

Rev

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

  1. $Revision: 434 $
  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.  
  14. align 32
  15. irq0:
  16.    ;     pushfd
  17.         pushad
  18.         mov   ax, app_data  ;
  19.         mov   ds, ax
  20.         mov   es, ax
  21.  
  22.         inc   dword [timer_ticks]
  23.  
  24.         mov   eax, [timer_ticks]
  25.         call  playNote           ; <<<--- Speaker driver
  26.  
  27.         cmp   eax,[next_usage_update]
  28.         jb    .nocounter
  29.         add   eax,100
  30.         mov   [next_usage_update],eax
  31.         call  updatecputimes
  32. .nocounter:
  33.         cmp   [DONT_SWITCH], byte 1
  34.         jne   .change_task
  35.  
  36.         mov   al,0x20   ; send End Of Interrupt signal
  37.         mov   dx,0x20
  38.         out   dx,al
  39.  
  40.         mov   [DONT_SWITCH], byte 0
  41.  
  42.         popad
  43.  ;       popfd
  44.         iretd
  45.  
  46. .change_task:
  47.         call  update_counters
  48.  
  49.         call  find_next_task
  50.         mov   ecx, eax
  51.  
  52.         mov   al,0x20   ; send End Of Interrupt signal
  53.         mov   dx,0x20
  54.         out   dx,al
  55.  
  56.         test  ecx, ecx  ; if there is only one running process
  57.         jnz   .return
  58.  
  59.         call  do_change_task
  60.  
  61. .return:
  62.         popad
  63.  ;       popfd
  64.         iretd
  65.  
  66.  
  67. align 4
  68. change_task:
  69.  
  70.         pushfd
  71.         cli
  72.         pushad
  73.  
  74.         call  update_counters
  75.  
  76. if 0
  77.  
  78. ; \begin{Mario79}
  79.         cmp     [dma_task_switched], 1
  80.         jne     .find_next_task
  81.         mov     [dma_task_switched], 0
  82.         mov     ebx, [dma_process]
  83.         cmp     [CURRENT_TASK], ebx
  84.         je      .return
  85.         mov     edi, [dma_slot_ptr]
  86.         mov     [CURRENT_TASK], ebx
  87.         mov     [TASK_BASE], edi
  88.         jmp     @f
  89. .find_next_task:
  90. ; \end{Mario79}
  91.  
  92. end if
  93.  
  94.         call  find_next_task
  95.         test  eax, eax    ; the same task -> skip switch
  96.         jnz    .return
  97. @@:
  98.         mov   [DONT_SWITCH],byte 1
  99.         call  do_change_task
  100.  
  101. .return:
  102.         popad
  103.         popfd
  104.         ret
  105.  
  106.  
  107. uglobal
  108.    align 4
  109.    far_jump:
  110.     .offs dd ?
  111.     .sel  dw ?
  112.    context_counter     dd ? ;noname & halyavin
  113.    next_usage_update   dd ?
  114.    timer_ticks         dd ?
  115.    prev_slot           dd ?
  116.    event_sched         dd ?
  117. endg
  118.  
  119.  
  120. update_counters:
  121.         mov   edi, [TASK_BASE]
  122.         mov   ebx, [edi+TASKDATA.counter_add] ; time stamp counter add
  123.         rdtsc
  124.         sub   eax, ebx
  125.         add   eax, [edi+TASKDATA.counter_sum] ; counter sum
  126.         mov   [edi+TASKDATA.counter_sum], eax
  127. ret
  128.  
  129.  
  130. ; Find next task to execute
  131. ; result: ebx = number of the selected task
  132. ;         eax = 1  if the task is the same
  133. ;         edi = address of the data for the task in ebx
  134. ;         [0x3000] = ebx and [0x3010] = edi
  135. ;         corrupts other regs
  136. find_next_task:
  137.         mov   ebx, [CURRENT_TASK]
  138.         mov   edi, [TASK_BASE]
  139.         mov   [prev_slot], ebx
  140.  
  141. .waiting_for_termination:
  142. .waiting_for_reuse:
  143. .waiting_for_event:
  144. .suspended:
  145.         cmp   ebx, [TASK_COUNT]
  146.         jb    @f
  147.         mov   edi, CURRENT_TASK
  148.         xor   ebx, ebx
  149. @@:
  150.  
  151.         add   edi,0x20
  152.         inc   ebx
  153.  
  154.         mov   al, byte [edi+TASKDATA.state]
  155.         test  al, al
  156.         jz    .found
  157.         cmp   al, 1
  158.         jz    .suspended
  159.         cmp   al, 2
  160.         jz    .suspended
  161.         cmp   al, 3
  162.         je    .waiting_for_termination
  163.         cmp   al, 4
  164.         je    .waiting_for_termination
  165.         cmp   al, 9
  166.         je    .waiting_for_reuse
  167.  
  168.         mov   [CURRENT_TASK],ebx
  169.         mov   [TASK_BASE],edi
  170.  
  171.         cmp   al, 5
  172.         jne   .noevents
  173.         call  get_event_for_app
  174.         test  eax, eax
  175.         jz    .waiting_for_event
  176.         mov   [event_sched], eax
  177.         mov   [edi+TASKDATA.state], byte 0
  178. .noevents:
  179. .found:
  180.         mov   [CURRENT_TASK],ebx
  181.         mov   [TASK_BASE],edi
  182.         rdtsc                     ;call  _rdtsc
  183.         mov   [edi+TASKDATA.counter_add],eax
  184.  
  185.         mov esi, [prev_slot]
  186.         xor   eax, eax
  187.         cmp   ebx, esi
  188.         sete  al
  189. ret
  190.  
  191. ; param
  192. ;  ebx = incoming task
  193. ;  esi = outcomig task
  194.  
  195. do_change_task:
  196.  
  197.         shl ebx, 8
  198.         add ebx, SLOT_BASE
  199.         mov [current_slot], ebx
  200.  
  201.         shl esi, 8
  202.         add esi, SLOT_BASE
  203.  
  204.         mov [esi+APPDATA.saved_esp], esp
  205.         mov esp, [ebx+APPDATA.saved_esp]
  206.  
  207. ; set thread io map
  208.  
  209.         mov ecx, [ebx+APPDATA.io_map]
  210.         mov edx, [ebx+APPDATA.io_map+4]
  211.         mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], ecx
  212.         mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], edx
  213.  
  214.         mov eax, [ebx+APPDATA.dir_table]
  215.         mov cr3, eax
  216.         mov ebx, [ebx+APPDATA.pl0_stack]
  217.         add ebx, RING0_STACK_SIZE
  218.         mov [tss._esp0], ebx
  219.         mov ecx, cr0
  220.         or ecx, CR0_TS   ;set task switch flag
  221.         mov cr0, ecx
  222.         inc   [context_counter] ;noname & halyavin
  223.  
  224.         test [ebx+APPDATA.dbg_state], 1
  225.         jnz @F
  226.         ret
  227. @@:
  228.         mov eax, [ebx+APPDATA.dbg_regs.dr0]
  229.         mov dr0, eax
  230.         mov eax, [ebx+APPDATA.dbg_regs.dr1]
  231.         mov dr1, eax
  232.         mov eax, [ebx+APPDATA.dbg_regs.dr2]
  233.         mov dr2, eax
  234.         mov eax, [ebx+APPDATA.dbg_regs.dr3]
  235.         mov dr3, eax
  236.         xor eax, eax
  237.         mov dr6, eax
  238.         mov eax, [ebx+APPDATA.dbg_regs.dr7]
  239.         mov dr7, eax
  240.         ret
  241.  
  242. align 4
  243. updatecputimes:
  244.  
  245.         mov  eax,[idleuse]
  246.         mov  [idleusesec],eax
  247.         mov  [idleuse],dword 0
  248.         mov  ecx, [TASK_COUNT]
  249.         mov  edi, TASK_DATA
  250. .newupdate:
  251.         mov  ebx,[edi+TASKDATA.counter_sum]
  252.         mov  [edi+TASKDATA.cpu_usage],ebx
  253.         mov  [edi+TASKDATA.counter_sum],dword 0
  254.         add  edi,0x20
  255.         dec  ecx
  256.         jnz  .newupdate
  257.  
  258.         ret
  259.  
  260. if 0
  261.  
  262.  
  263. struc TIMER
  264. {
  265.   .next      dd ?
  266.   .exp_time  dd ?
  267.   .func      dd ?
  268.   .arg       dd ?
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. MAX_PROIRITY         0   ; highest, used for kernel tasks
  280. MAX_USER_PRIORITY    0   ; highest priority for user processes
  281. USER_PRIORITY        7   ; default (should correspond to nice 0)
  282. MIN_USER_PRIORITY   14   ; minimum priority for user processes
  283. IDLE_PRIORITY       15   ; lowest, only IDLE process goes here
  284. NR_SCHED_QUEUES     16   ; MUST equal IDLE_PRIORYTY + 1
  285.  
  286. rdy_head   rd 16
  287.  
  288.  
  289. align 4
  290. pick_task:
  291.  
  292.            xor eax, eax
  293. .pick:
  294.            mov ebx, [rdy_head+eax*4]
  295.            test ebx, ebx
  296.            jz .next
  297.  
  298.            mov [next_task], ebx
  299.            test [ebx+flags.billable]
  300.            jz @F
  301.            mov [bill_task], ebx
  302. @@:
  303.            ret
  304. .next:
  305.            inc eax
  306.            jmp .pick
  307.  
  308.  
  309. ; param
  310. ;  eax= task
  311. ;
  312. ; retval
  313. ;  eax= task
  314. ;  ebx= queue
  315. ;  ecx= front if 1 or back if 0
  316.  
  317. align 4
  318. shed:
  319.            cmp [eax+.tics_left], 0 ;signed compare
  320.            mov ebx, [eax+.priority]
  321.            setg ecx
  322.            jg @F
  323.  
  324.            mov edx, [eax+.tics_quantum]
  325.            mov [eax+.ticks_left], edx
  326.            cmp ebx, (IDLE_PRIORITY-1)
  327.            je @F
  328.            inc ebx
  329. @@:
  330.            ret
  331.  
  332. ; param
  333. ;  eax= task
  334.  
  335. align 4
  336. enqueue:
  337.           call shed  ;eax
  338.           cmp [rdy_head+ebx*4],0
  339.           jnz @F
  340.  
  341.           mov [rdy_head+ebx*4], eax
  342.           mov [rdy_tail+ebx*4], eax
  343.           mov [eax+.next_ready], 0
  344.           jmp .pick
  345. @@:
  346.           test ecx, ecx
  347.           jz .back
  348.  
  349.           mov ecx, [rdy_head+ebx*4]
  350.           mov [eax+.next_ready], ecx
  351.           mov [rdy_head+ebx*4], eax
  352.           jmp .pick
  353. .back:
  354.           mov ecx, [rdy_tail+ebx*4]
  355.           mov [ecx+.next_ready], eax
  356.           mov [rdy_tail+ebx*4], eax
  357.           mov [eax+.next_ready], 0
  358. .pick:
  359.           call pick_proc     ;select next task
  360.           ret
  361.  
  362. end if
  363.  
  364.