Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;;
  4. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa      ;;
  5. ;; Distributed under terms of the GNU General Public License    ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 9590 $
  9.  
  10.  
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;; IRQ0 HANDLER (TIMER INTERRUPT) ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15.  
  16. align 32
  17. irq0:
  18.         pushad
  19.         mov     ax, app_data
  20.         mov     ds, ax
  21.         mov     es, ax
  22.         inc     [timer_ticks]
  23.         mov     eax, [timer_ticks]
  24.         call    playNote       ; <<<--- Speaker driver
  25.         sub     eax, [next_usage_update]
  26.         cmp     eax, 100
  27.         jb      .nocounter
  28.         add     [next_usage_update], 100
  29.         call    updatecputimes
  30.   .nocounter:
  31.         xor     ecx, ecx        ; send End Of Interrupt signal
  32.         call    irq_eoi
  33.  
  34.         mov     bl, SCHEDULE_ANY_PRIORITY
  35.         call    find_next_task
  36.         jz      .return  ; if there is only one running process
  37.         call    do_change_task
  38.   .return:
  39.         popad
  40.         iretd
  41.  
  42. align 4
  43. change_task:
  44.         pushfd
  45.         cli
  46.         pushad
  47.         mov     bl, SCHEDULE_ANY_PRIORITY
  48.         call    find_next_task
  49.         jz      .return  ; the same task -> skip switch
  50.  
  51.         call    do_change_task
  52.   .return:
  53.         popad
  54.         popfd
  55.         ret
  56.  
  57. uglobal
  58. align 4
  59. ;  far_jump:
  60. ;   .offs dd ?
  61. ;   .sel  dw ?
  62.    context_counter     dd 0 ;noname & halyavin
  63.    next_usage_update   dd 0
  64.    timer_ticks         dd 0
  65. ;  prev_slot           dd ?
  66. ;  event_sched         dd ?
  67. endg
  68.  
  69. align 4
  70. update_counters:
  71.         mov     edi, [TASK_BASE]
  72.         rdtsc
  73.         sub     eax, [edi+TASKDATA.counter_add] ; time stamp counter add
  74.         add     [edi+TASKDATA.counter_sum], eax ; counter sum
  75.         ret
  76. align 4
  77. updatecputimes:
  78.         mov     ecx, [thread_count]
  79.         ;mov     edi, TASK_DATA
  80.         mov     edi, SLOT_BASE
  81.   .newupdate:
  82.         xor     eax, eax
  83.         xchg    eax, [edi+TASKDATA.counter_sum]
  84.         ;mov     [edi+TASKDATA.cpu_usage], eax
  85.         ;add     edi, 0x20
  86.         mov     [edi+APPDATA.cpu_usage], eax
  87.         add     edi, 0x100 ;sizeof.APPDATA
  88.         loop    .newupdate
  89.         ret
  90.  
  91. ;TODO: Надо бы убрать использование do_change_task из V86...
  92. ; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task
  93.  
  94. align 4
  95. do_change_task:
  96. ;param:
  97. ;   ebx = address of the APPDATA for incoming task (new)
  98. ;warning:
  99. ;   [current_slot_idx] and [TASK_BASE] must be changed before (e.g. in find_next_task)
  100. ;   [current_slot] is the outcoming (old), and set here to a new value (ebx)
  101. ;scratched: eax,ecx,esi
  102.         mov     esi, ebx
  103.         xchg    esi, [current_slot]
  104. ; set new stack after saving old
  105.         mov     [esi+APPDATA.saved_esp], esp
  106.         mov     esp, [ebx+APPDATA.saved_esp]
  107. ; set new thread io-map
  108.         mov     eax, [ebx+APPDATA.io_map]
  109.         mov     dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], eax
  110.         mov     eax, [ebx+APPDATA.io_map+4]
  111.         mov     dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], eax
  112. ; set new thread memory-map
  113.         mov     eax, [ebx+APPDATA.process]
  114.         cmp     eax, [current_process]
  115.         je      @f
  116.         mov     [current_process], eax
  117.         mov     eax, [eax+PROC.pdt_0_phys]
  118.         mov     cr3, eax
  119. @@:
  120. ; set tss.esp0
  121.  
  122.         mov     eax, [ebx+APPDATA.saved_esp0]
  123.         mov     [tss._esp0], eax
  124.  
  125.         mov     edx, [ebx+APPDATA.tls_base]
  126.  
  127.         mov     [tls_data_l+2], dx
  128.         shr     edx, 16
  129.         mov     [tls_data_l+4], dl
  130.         mov     [tls_data_l+7], dh
  131.  
  132.         mov     dx, app_tls
  133.         mov     fs, dx
  134.  
  135. ; set gs selector unconditionally
  136.         Mov     ax, graph_data
  137.         Mov     gs, ax
  138.         ; TS flag is not triggered by AVX* instructions, therefore
  139.         ; we have to xsave/xrstor SIMD registers each task change
  140.         bt      [cpu_caps+(CAPS_OSXSAVE/32)*4], CAPS_OSXSAVE mod 32
  141.         jnc     .no_xsave
  142.         mov     ecx, [esi+APPDATA.fpu_state]
  143.         mov     eax, [xsave_eax]
  144.         mov     edx, [xsave_edx]
  145.         xsave   [ecx]
  146.         mov     ecx, [current_slot_idx]
  147.         mov     [fpu_owner], ecx
  148.         mov     ecx, [current_slot]
  149.         mov     ecx, [ecx+APPDATA.fpu_state]
  150.         xrstor  [ecx]
  151.   .no_xsave:
  152.       ; set CR0.TS
  153.         cmp     bh, byte[fpu_owner] ;bh == incoming task (new)
  154.         clts                        ;clear a task switch flag
  155.         je      @f
  156.         mov     eax, cr0            ;and set it again if the owner
  157.         or      eax, CR0_TS         ;of a fpu has changed
  158.         mov     cr0, eax
  159.   @@: ; set context_counter (only for user pleasure ???)
  160.         inc     [context_counter]   ;noname & halyavin
  161.       ; set debug-registers, if it's necessary
  162.         test    byte[ebx+APPDATA.dbg_state], 1
  163.         jz      @f
  164.         xor     eax, eax
  165.         mov     dr6, eax
  166.         lea     esi, [ebx+APPDATA.dbg_regs]
  167.         cld
  168.   macro lodsReg [reg] {
  169.         lodsd
  170.         mov     reg, eax
  171.   }     lodsReg dr0, dr1, dr2, dr3, dr7
  172.   purge lodsReg
  173.   @@:
  174.         ret
  175. ;end.
  176.  
  177.  
  178.  
  179.  
  180. MAX_PRIORITY      = 0   ; highest, used for kernel tasks
  181. USER_PRIORITY     = 1   ; default
  182. IDLE_PRIORITY     = 2   ; lowest, only IDLE thread goes here
  183. NR_SCHED_QUEUES   = 3   ; MUST equal IDLE_PRIORYTY + 1
  184.  
  185. uglobal
  186. ; [scheduler_current + i*4] = zero if there are no threads with priority i,
  187. ;  pointer to APPDATA of the current thread with priority i otherwise.
  188. align 4
  189. scheduler_current       rd      NR_SCHED_QUEUES
  190. endg
  191.  
  192. ; Add the given thread to the given priority list for the scheduler.
  193. ; in: edx -> APPDATA, ecx = priority
  194. proc scheduler_add_thread
  195. ; 1. Acquire the lock.
  196.         spin_lock_irqsave SchedulerLock
  197. ; 2. Store the priority in APPDATA structure.
  198.         mov     [edx+APPDATA.priority], ecx
  199. ; 3. There are two different cases: the given list is empty or not empty.
  200. ; In first case, go to 6. Otherwise, advance to 4.
  201.         mov     eax, [scheduler_current+ecx*4]
  202.         test    eax, eax
  203.         jz      .new_list
  204. ; 4. Insert the new item immediately before the current item.
  205.         mov     ecx, [eax+APPDATA.in_schedule.prev]
  206.         mov     [edx+APPDATA.in_schedule.next], eax
  207.         mov     [edx+APPDATA.in_schedule.prev], ecx
  208.         mov     [eax+APPDATA.in_schedule.prev], edx
  209.         mov     [ecx+APPDATA.in_schedule.next], edx
  210. ; 5. Release the lock and return.
  211.         spin_unlock_irqrestore SchedulerLock
  212.         ret
  213. .new_list:
  214. ; 6. Initialize the list with one item and make it the current item.
  215.         mov     [edx+APPDATA.in_schedule.next], edx
  216.         mov     [edx+APPDATA.in_schedule.prev], edx
  217.         mov     [scheduler_current+ecx*4], edx
  218. ; 7. Release the lock and return.
  219.         spin_unlock_irqrestore SchedulerLock
  220.         ret
  221. endp
  222.  
  223. ; Remove the given thread from the corresponding priority list for the scheduler.
  224. ; in: edx -> APPDATA
  225. proc scheduler_remove_thread
  226. ; 1. Acquire the lock.
  227.         spin_lock_irqsave SchedulerLock
  228. ; 2. Remove the item from the corresponding list.
  229.         mov     eax, [edx+APPDATA.in_schedule.next]
  230.         mov     ecx, [edx+APPDATA.in_schedule.prev]
  231.         mov     [eax+APPDATA.in_schedule.prev], ecx
  232.         mov     [ecx+APPDATA.in_schedule.next], eax
  233. ; 3. If the given thread is the current item in the list,
  234. ; advance the current item.
  235. ; 3a. Check whether the given thread is the current item;
  236. ; if no, skip the rest of this step.
  237.         mov     ecx, [edx+APPDATA.priority]
  238.         cmp     [scheduler_current+ecx*4], edx
  239.         jnz     .return
  240. ; 3b. Set the current item to eax; step 2 has set eax = next item.
  241.         mov     [scheduler_current+ecx*4], eax
  242. ; 3c. If there were only one item in the list, zero the current item.
  243.         cmp     eax, edx
  244.         jnz     .return
  245.         mov     [scheduler_current+ecx*4], 0
  246. .return:
  247. ; 4. Release the lock and return.
  248.         spin_unlock_irqrestore SchedulerLock
  249.         ret
  250. endp
  251.  
  252. SCHEDULE_ANY_PRIORITY = 0
  253. SCHEDULE_HIGHER_PRIORITY = 1
  254. ;info:
  255. ;   Find next task to execute
  256. ;in:
  257. ;   bl = SCHEDULE_ANY_PRIORITY:
  258. ;        consider threads with any priority
  259. ;   bl = SCHEDULE_HIGHER_PRIORITY:
  260. ;        consider only threads with strictly higher priority than the current one,
  261. ;        keep running the current thread if other ready threads have the same or lower priority
  262. ;retval:
  263. ;   ebx = address of the APPDATA for the selected task (slot-base)
  264. ;   edi = address of the TASKDATA for the selected task
  265. ;   ZF  = 1  if the task is the same
  266. ;warning:
  267. ;   [current_slot_idx] = bh , [TASK_BASE] = edi -- as result
  268. ;   [current_slot] is not set to new value (ebx)!!!
  269. ;scratched: eax,ecx
  270. proc find_next_task
  271.         call    update_counters
  272.         spin_lock_irqsave SchedulerLock
  273.         push    NR_SCHED_QUEUES
  274. ; If bl == SCHEDULE_ANY_PRIORITY = 0, loop over all NR_SCHED lists.
  275. ; Otherwise, loop over first [APPDATA.priority] lists.
  276.         test    bl, bl
  277.         jz      .start
  278.         mov     ebx, [current_slot]
  279.         mov     edi, [TASK_BASE]
  280.         mov     eax, [ebx+APPDATA.priority]
  281.         test    eax, eax
  282.         jz      .unlock_found
  283.         mov     [esp], eax
  284. .start:
  285.         xor     ecx, ecx
  286. .priority_loop:
  287.         mov     ebx, [scheduler_current+ecx*4]
  288.         test    ebx, ebx
  289.         jz      .priority_next
  290. .task_loop:
  291.         mov     ebx, [ebx+APPDATA.in_schedule.next]
  292.         mov     edi, ebx
  293.         shr     edi, 3
  294.         add     edi, TASK_TABLE - (SLOT_BASE shr 3)
  295.         mov     al, [edi+TASKDATA.state]
  296.         test    al, al
  297.         jz      .task_found     ; state == 0
  298.         cmp     al, 5
  299.         jne     .task_next      ; state == 1,2,3,4,9
  300.       ; state == 5
  301.         pushad  ; more freedom for [APPDATA.wait_test]
  302.         call    [ebx+APPDATA.wait_test]
  303.         mov     [esp+28], eax
  304.         popad
  305.         or      eax, eax
  306.         jnz     @f
  307.       ; testing for timeout
  308.         mov     eax, [timer_ticks]
  309.         sub     eax, [ebx+APPDATA.wait_begin]
  310.         cmp     eax, [ebx+APPDATA.wait_timeout]
  311.         jb      .task_next
  312.         xor     eax, eax
  313. @@:
  314.         mov     [ebx+APPDATA.wait_param], eax  ; retval for wait
  315.         mov     [edi+TASKDATA.state], TSTATE_RUNNING
  316. .task_found:
  317.         mov     [scheduler_current+ecx*4], ebx
  318. ; If we have selected a thread with higher priority
  319. ; AND rescheduling is due to IRQ,
  320. ; turn the current scheduler list one entry back,
  321. ; so the current thread will be next after high-priority thread is done.
  322.         mov     ecx, [esp]
  323.         cmp     ecx, NR_SCHED_QUEUES
  324.         jz      .unlock_found
  325.         mov     eax, [current_slot]
  326.         mov     eax, [eax+APPDATA.in_schedule.prev]
  327.         mov     [scheduler_current+ecx*4], eax
  328. .unlock_found:
  329.         pop     ecx
  330.         spin_unlock_irqrestore SchedulerLock
  331. .found:
  332.         ; the line below assumes APPDATA is 256 bytes long and SLOT_BASE is
  333.         ; aligned on 0x10000
  334.         mov     byte [current_slot_idx], bh
  335.         mov     [TASK_BASE], edi
  336.         rdtsc   ;call  _rdtsc
  337.         mov     [edi+TASKDATA.counter_add], eax; for next using update_counters
  338.         cmp     ebx, [current_slot]
  339.         ret
  340. .task_next:
  341.         cmp     ebx, [scheduler_current+ecx*4]
  342.         jnz     .task_loop
  343. .priority_next:
  344.         inc     ecx
  345.         cmp     ecx, [esp]
  346.         jb      .priority_loop
  347.         mov     ebx, [current_slot]
  348.         mov     edi, [TASK_BASE]
  349.         jmp     .unlock_found
  350. endp
  351.  
  352. if 0
  353.  
  354. struc TIMER
  355. {
  356.   .next      dd ?
  357.   .exp_time  dd ?
  358.   .func      dd ?
  359.   .arg       dd ?
  360. }
  361.  
  362.  
  363. uglobal
  364. rdy_head   rd 16
  365. endg
  366.  
  367. align 4
  368. pick_task:
  369.  
  370.         xor     eax, eax
  371.   .pick:
  372.         mov     ebx, [rdy_head+eax*4]
  373.         test    ebx, ebx
  374.         jz      .next
  375.  
  376.         mov     [next_task], ebx
  377.         test    [ebx+flags.billable]
  378.         jz      @F
  379.         mov     [bill_task], ebx
  380.   @@:
  381.         ret
  382.   .next:
  383.         inc     eax
  384.         jmp     .pick
  385.  
  386. ; param
  387. ;  eax= task
  388. ;
  389. ; retval
  390. ;  eax= task
  391. ;  ebx= queue
  392. ;  ecx= front if 1 or back if 0
  393. align 4
  394. shed:
  395.         cmp     [eax+.tics_left], 0;signed compare
  396.         mov     ebx, [eax+.priority]
  397.         setg    ecx
  398.         jg      @F
  399.  
  400.         mov     edx, [eax+.tics_quantum]
  401.         mov     [eax+.ticks_left], edx
  402.         cmp     ebx, (IDLE_PRIORITY-1)
  403.         je      @F
  404.         inc     ebx
  405.   @@:
  406.         ret
  407.  
  408. ; param
  409. ;  eax= task
  410. align 4
  411. enqueue:
  412.         call    shed;eax
  413.         cmp     [rdy_head+ebx*4], 0
  414.         jnz     @F
  415.  
  416.         mov     [rdy_head+ebx*4], eax
  417.         mov     [rdy_tail+ebx*4], eax
  418.         mov     [eax+.next_ready], 0
  419.         jmp     .pick
  420.   @@:
  421.         test    ecx, ecx
  422.         jz      .back
  423.  
  424.         mov     ecx, [rdy_head+ebx*4]
  425.         mov     [eax+.next_ready], ecx
  426.         mov     [rdy_head+ebx*4], eax
  427.         jmp     .pick
  428.   .back:
  429.         mov     ecx, [rdy_tail+ebx*4]
  430.         mov     [ecx+.next_ready], eax
  431.         mov     [rdy_tail+ebx*4], eax
  432.         mov     [eax+.next_ready], 0
  433.   .pick:
  434.         call    pick_proc;select next task
  435.         ret
  436.  
  437. end if
  438.