Subversion Repositories Kolibri OS

Rev

Rev 430 | 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.  
  68. if 0
  69.  
  70. ; \begin{Mario79}
  71.         cmp     [dma_task_switched], 1
  72.         jne     .find_next_task
  73.         mov     [dma_task_switched], 0
  74.         mov     ebx, [dma_process]
  75.         cmp     [CURRENT_TASK], ebx
  76.         je      .return
  77.         mov     edi, [dma_slot_ptr]
  78.         mov     [CURRENT_TASK], ebx
  79.         mov     [TASK_BASE], edi
  80.         jmp     @f
  81. .find_next_task:
  82. ; \end{Mario79}
  83.  
  84. end if
  85.  
  86.         call  find_next_task
  87.         test  eax, eax    ; the same task -> skip switch
  88.         jnz    .return
  89. @@:
  90.         mov   [DONT_SWITCH],byte 1
  91.         call  do_change_task
  92.  
  93. .return:
  94.         popad
  95.         popfd
  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.         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.         rdtsc                     ;call  _rdtsc
  175.         mov   [edi+TASKDATA.counter_add],eax
  176.  
  177.         mov esi, [prev_slot]
  178.         xor   eax, eax
  179.         cmp   ebx, esi
  180.         sete  al
  181. ret
  182.  
  183. ; param
  184. ;  ebx = incoming task
  185. ;  esi = outcomig task
  186.  
  187. do_change_task:
  188.  
  189.         shl ebx, 8
  190.         add ebx, SLOT_BASE
  191.         mov [current_slot], ebx
  192.         shl esi, 8
  193.         add esi, SLOT_BASE
  194.  
  195.         mov [esi+APPDATA.saved_esp], esp
  196.         mov esp, [ebx+APPDATA.saved_esp]
  197.  
  198. ; set thread io map
  199.  
  200.         mov ecx, [ebx+APPDATA.io_map]
  201.         mov edx, [ebx+APPDATA.io_map+4]
  202.         mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], ecx
  203.         mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], edx
  204.  
  205.         mov eax, [ebx+APPDATA.dir_table]
  206.         mov cr3, eax
  207.         mov ebx, [ebx+APPDATA.pl0_stack]
  208.         add ebx, RING0_STACK_SIZE
  209.         mov [tss._esp0], ebx
  210.         mov ecx, cr0
  211.         or ecx, CR0_TS   ;set task switch flag
  212.         mov cr0, ecx
  213.         inc   [context_counter] ;noname & halyavin
  214.  
  215.         ret
  216.  
  217. align 4
  218. updatecputimes:
  219.  
  220.         mov  eax,[idleuse]
  221.         mov  [idleusesec],eax
  222.         mov  [idleuse],dword 0
  223.         mov  ecx, [TASK_COUNT]
  224.         mov  edi, TASK_DATA
  225. .newupdate:
  226.         mov  ebx,[edi+TASKDATA.counter_sum]
  227.         mov  [edi+TASKDATA.cpu_usage],ebx
  228.         mov  [edi+TASKDATA.counter_sum],dword 0
  229.         add  edi,0x20
  230.         dec  ecx
  231.         jnz  .newupdate
  232.  
  233.         ret
  234.  
  235. if 0
  236.  
  237.  
  238. struc TIMER
  239. {
  240.   .next      dd ?
  241.   .exp_time  dd ?
  242.   .func      dd ?
  243.   .arg       dd ?
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254. MAX_PROIRITY         0   ; highest, used for kernel tasks
  255. MAX_USER_PRIORITY    0   ; highest priority for user processes
  256. USER_PRIORITY        7   ; default (should correspond to nice 0)
  257. MIN_USER_PRIORITY   14   ; minimum priority for user processes
  258. IDLE_PRIORITY       15   ; lowest, only IDLE process goes here
  259. NR_SCHED_QUEUES     16   ; MUST equal IDLE_PRIORYTY + 1
  260.  
  261. rdy_head   rd 16
  262.  
  263.  
  264. align 4
  265. pick_task:
  266.  
  267.            xor eax, eax
  268. .pick:
  269.            mov ebx, [rdy_head+eax*4]
  270.            test ebx, ebx
  271.            jz .next
  272.  
  273.            mov [next_task], ebx
  274.            test [ebx+flags.billable]
  275.            jz @F
  276.            mov [bill_task], ebx
  277. @@:
  278.            ret
  279. .next:
  280.            inc eax
  281.            jmp .pick
  282.  
  283.  
  284. ; param
  285. ;  eax= task
  286. ;
  287. ; retval
  288. ;  eax= task
  289. ;  ebx= queue
  290. ;  ecx= front if 1 or back if 0
  291.  
  292. align 4
  293. shed:
  294.            cmp [eax+.tics_left], 0 ;signed compare
  295.            mov ebx, [eax+.priority]
  296.            setg ecx
  297.            jg @F
  298.  
  299.            mov edx, [eax+.tics_quantum]
  300.            mov [eax+.ticks_left], edx
  301.            cmp ebx, (IDLE_PRIORITY-1)
  302.            je @F
  303.            inc ebx
  304. @@:
  305.            ret
  306.  
  307. ; param
  308. ;  eax= task
  309.  
  310. align 4
  311. enqueue:
  312.           call shed  ;eax
  313.           cmp [rdy_head+ebx*4],0
  314.           jnz @F
  315.  
  316.           mov [rdy_head+ebx*4], eax
  317.           mov [rdy_tail+ebx*4], eax
  318.           mov [eax+.next_ready], 0
  319.           jmp .pick
  320. @@:
  321.           test ecx, ecx
  322.           jz .back
  323.  
  324.           mov ecx, [rdy_head+ebx*4]
  325.           mov [eax+.next_ready], ecx
  326.           mov [rdy_head+ebx*4], eax
  327.           jmp .pick
  328. .back:
  329.           mov ecx, [rdy_tail+ebx*4]
  330.           mov [ecx+.next_ready], eax
  331.           mov [rdy_tail+ebx*4], eax
  332.           mov [eax+.next_ready], 0
  333. .pick:
  334.           call pick_proc     ;select next task
  335.           ret
  336.  
  337. end if
  338.  
  339.