Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * Copyright (C) 2007 Vojtech Mencl
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. /** @addtogroup time
  31.  * @{
  32.  */
  33.  
  34. /**
  35.  * @file
  36.  * @brief   High-level clock interrupt handler.
  37.  *
  38.  * This file contains the clock() function which is the source
  39.  * of preemption. It is also responsible for executing expired
  40.  * timeouts.
  41.  */
  42.  
  43. #include <time/clock.h>
  44. #include <time/timeout.h>
  45. #include <config.h>
  46. #include <synch/spinlock.h>
  47. #include <synch/waitq.h>
  48. #include <func.h>
  49. #include <proc/scheduler.h>
  50. #include <cpu.h>
  51. #include <arch.h>
  52. #include <adt/list.h>
  53. #include <atomic.h>
  54. #include <proc/thread.h>
  55. #include <sysinfo/sysinfo.h>
  56. #include <arch/barrier.h>
  57. #include <mm/frame.h>
  58. #include <ddi/ddi.h>
  59. #if defined CONFIG_TIMEOUT_AVL_TREE || defined CONFIG_TIMEOUT_EXTAVL_TREE
  60. #include <arch/asm.h>
  61. #include <arch/types.h>
  62. #include <panic.h>
  63. #endif
  64. /* Pointer to variable with uptime */
  65. uptime_t *uptime;
  66.  
  67. /** Physical memory area of the real time clock */
  68. static parea_t clock_parea;
  69.  
  70. /* Variable holding fragment of second, so that we would update
  71.  * seconds correctly
  72.  */
  73. static unative_t secfrag = 0;
  74.  
  75. /** Initialize realtime clock counter
  76.  *
  77.  * The applications (and sometimes kernel) need to access accurate
  78.  * information about realtime data. We allocate 1 page with these
  79.  * data and update it periodically.
  80.  */
  81. void clock_counter_init(void)
  82. {
  83.     void *faddr;
  84.  
  85.     faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
  86.     if (!faddr)
  87.         panic("Cannot allocate page for clock");
  88.    
  89.     uptime = (uptime_t *) PA2KA(faddr);
  90.    
  91.     uptime->seconds1 = 0;
  92.     uptime->seconds2 = 0;
  93.     uptime->useconds = 0;
  94.  
  95.     clock_parea.pbase = (uintptr_t) faddr;
  96.     clock_parea.vbase = (uintptr_t) uptime;
  97.     clock_parea.frames = 1;
  98.     clock_parea.cacheable = true;
  99.     ddi_parea_register(&clock_parea);
  100.  
  101.     /*
  102.      * Prepare information for the userspace so that it can successfully
  103.      * physmem_map() the clock_parea.
  104.      */
  105.     sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true);
  106.     sysinfo_set_item_val("clock.faddr", NULL, (unative_t) faddr);
  107. }
  108.  
  109.  
  110. /** Update public counters
  111.  *
  112.  * Update it only on first processor
  113.  * TODO: Do we really need so many write barriers?
  114.  */
  115. static void clock_update_counters(void)
  116. {
  117.     if (CPU->id == 0) {
  118.         secfrag += 1000000 / HZ;
  119.         if (secfrag >= 1000000) {
  120.             secfrag -= 1000000;
  121.             uptime->seconds1++;
  122.             write_barrier();
  123.             uptime->useconds = secfrag;
  124.             write_barrier();
  125.             uptime->seconds2 = uptime->seconds1;
  126.         } else
  127.             uptime->useconds += 1000000 / HZ;
  128.     }
  129. }
  130.  
  131. #if defined CONFIG_TIMEOUT_AVL_TREE
  132.  
  133. /** Clock routine
  134.  *
  135.  * Clock routine executed from clock interrupt handler
  136.  * (assuming interrupts_disable()'d). Runs expired timeouts
  137.  * and preemptive scheduling.
  138.  *
  139.  */
  140. void clock(void)
  141. {
  142.     timeout_t *h;
  143.     timeout_handler_t f;
  144.     void *arg;
  145.     count_t missed_clock_ticks = CPU->missed_clock_ticks;
  146.     uint64_t i = CPU->timeout_active_tree.base;
  147.     uint64_t last_clock_tick = i + missed_clock_ticks;
  148.     avltree_node_t *expnode;
  149.  
  150.     /*
  151.      * To avoid lock ordering problems,
  152.      * run all expired timeouts as you visit them.
  153.      */
  154.  
  155.    
  156.     for (; i <= last_clock_tick; i++) {
  157.         clock_update_counters();
  158.         spinlock_lock(&CPU->timeoutlock);
  159.    
  160.         /*
  161.          * Check whether first timeout (with the smallest key in the tree) time out. If so perform
  162.          * callback function and try next timeout (more timeouts can have same timeout).
  163.          */
  164.         while ((expnode = avltree_find_min(&CPU->timeout_active_tree)) != NULL) {
  165.             h = avltree_get_instance(expnode,timeout_t,node);
  166.             spinlock_lock(&h->lock);
  167.             if (expnode->key != i) {
  168.                 /*
  169.                  * Base is increased every for cycle.
  170.                  */
  171.                 (CPU->timeout_active_tree.base)++;
  172.                 spinlock_unlock(&h->lock);
  173.                 break;
  174.             }
  175.            
  176.             /*
  177.              * Delete minimal key from the tree and repair tree structure in
  178.              * logarithmic time.
  179.              */
  180.             avltree_delete_min(&CPU->timeout_active_tree);
  181.  
  182.             f = h->handler;
  183.             arg = h->arg;
  184.             timeout_reinitialize(h);
  185.             spinlock_unlock(&h->lock); 
  186.             spinlock_unlock(&CPU->timeoutlock);
  187.  
  188.             f(arg);
  189.  
  190.             spinlock_lock(&CPU->timeoutlock);
  191.         }
  192.         spinlock_unlock(&CPU->timeoutlock);
  193.     }
  194.  
  195.     CPU->missed_clock_ticks = 0;
  196.  
  197.     /*
  198.      * Do CPU usage accounting and find out whether to preempt THREAD.
  199.      */
  200.     if (THREAD) {
  201.         uint64_t ticks;
  202.        
  203.         spinlock_lock(&CPU->lock);
  204.         CPU->needs_relink += 1 + missed_clock_ticks;
  205.         spinlock_unlock(&CPU->lock);   
  206.    
  207.         spinlock_lock(&THREAD->lock);
  208.         if ((ticks = THREAD->ticks)) {
  209.             if (ticks >= 1 + missed_clock_ticks)
  210.                 THREAD->ticks -= 1 + missed_clock_ticks;
  211.             else
  212.                 THREAD->ticks = 0;
  213.         }
  214.         spinlock_unlock(&THREAD->lock);
  215.        
  216.         if (!ticks && !PREEMPTION_DISABLED) {
  217.             scheduler();
  218.         }
  219.     }
  220. }
  221.  
  222. #elif defined CONFIG_TIMEOUT_EXTAVL_TREE
  223.  
  224. /** Clock routine
  225.  *
  226.  * Clock routine executed from clock interrupt handler
  227.  * (assuming interrupts_disable()'d). Runs expired timeouts
  228.  * and preemptive scheduling.
  229.  *
  230.  */
  231. void clock(void)
  232. {
  233.     timeout_t *h;
  234.     timeout_handler_t f;
  235.     void *arg;
  236.     count_t missed_clock_ticks = CPU->missed_clock_ticks;
  237.     uint64_t i = CPU->timeout_active_tree.base;
  238.     uint64_t last_clock_tick = i + missed_clock_ticks;
  239.     extavltree_node_t *expnode;
  240.     //ipl_t ipl;
  241.  
  242.     /*
  243.      * To avoid lock ordering problems,
  244.      * run all expired timeouts as you visit them.
  245.      */
  246.  
  247.     for (; i <= last_clock_tick; i++) {
  248.         clock_update_counters();
  249.         spinlock_lock(&CPU->timeoutlock);
  250.        
  251.         /*
  252.          * Check whether first timeout in list time out. If so perform callback function and try
  253.          * next timeout (more timeouts can have same timeout).
  254.          */
  255.         while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) {
  256.             h = extavltree_get_instance(expnode,timeout_t,node);
  257.             spinlock_lock(&h->lock);
  258.             if (expnode->key != i) {
  259.                 /*
  260.                  * Base is increased every for cycle.
  261.                  */
  262.                 (CPU->timeout_active_tree.base)++;
  263.                 spinlock_unlock(&h->lock);
  264.                 break;
  265.             }
  266.            
  267.             /*
  268.              * Delete first node in the list and repair tree structure in
  269.              * constant time.
  270.              */
  271.             extavltree_delete_min(&CPU->timeout_active_tree);
  272.  
  273.             f = h->handler;
  274.             arg = h->arg;
  275.             timeout_reinitialize(h);
  276.             spinlock_unlock(&h->lock); 
  277.             spinlock_unlock(&CPU->timeoutlock);
  278.  
  279.             f(arg);
  280.  
  281.             spinlock_lock(&CPU->timeoutlock);
  282.         }
  283.         spinlock_unlock(&CPU->timeoutlock);
  284.     }
  285.  
  286.     CPU->missed_clock_ticks = 0;
  287.  
  288.     /*
  289.      * Do CPU usage accounting and find out whether to preempt THREAD.
  290.      */
  291.     if (THREAD) {
  292.         uint64_t ticks;
  293.        
  294.         spinlock_lock(&CPU->lock);
  295.         CPU->needs_relink += 1 + missed_clock_ticks;
  296.         spinlock_unlock(&CPU->lock);   
  297.    
  298.         spinlock_lock(&THREAD->lock);
  299.         if ((ticks = THREAD->ticks)) {
  300.             if (ticks >= 1 + missed_clock_ticks)
  301.                 THREAD->ticks -= 1 + missed_clock_ticks;
  302.             else
  303.                 THREAD->ticks = 0;
  304.         }
  305.         spinlock_unlock(&THREAD->lock);
  306.        
  307.         if (!ticks && !PREEMPTION_DISABLED) {
  308.             scheduler();
  309.         }
  310.     }
  311. }
  312.  
  313. #elif defined CONFIG_TIMEOUT_EXTAVLREL_TREE
  314.  
  315. /** Clock routine
  316.  *
  317.  * Clock routine executed from clock interrupt handler
  318.  * (assuming interrupts_disable()'d). Runs expired timeouts
  319.  * and preemptive scheduling.
  320.  *
  321.  */
  322. void clock(void)
  323. {
  324.     extavlreltree_node_t *expnode;
  325.     timeout_t *h;
  326.     timeout_handler_t f;
  327.     void *arg;
  328.     count_t missed_clock_ticks = CPU->missed_clock_ticks;
  329.     int i;
  330.  
  331.     /*
  332.      * To avoid lock ordering problems,
  333.      * run all expired timeouts as you visit them.
  334.      */
  335.     for (i = 0; i <= missed_clock_ticks; i++) {
  336.         clock_update_counters();
  337.         spinlock_lock(&CPU->timeoutlock);
  338.  
  339.         /*
  340.          * Check whether first timeout in list time out. If so perform callback function and try
  341.          * next timeout (more timeouts can have same timeout).
  342.          */
  343.         while ((expnode = CPU->timeout_active_tree.head.next) != &(CPU->timeout_active_tree.head)) {
  344.             h = extavlreltree_get_instance(expnode,timeout_t,node);
  345.             spinlock_lock(&h->lock);
  346.             if (expnode->key != 0) {
  347.                 expnode->key--;
  348.                 spinlock_unlock(&h->lock);
  349.                 break;
  350.             }
  351.            
  352.             /*
  353.              * Delete first node in the list and repair tree structure in
  354.              * constant time. Be careful of expnode's key, it must be 0!
  355.              */
  356.             extavlreltree_delete_min(&CPU->timeout_active_tree);
  357.            
  358.             f = h->handler;
  359.             arg = h->arg;
  360.             timeout_reinitialize(h);
  361.             spinlock_unlock(&h->lock); 
  362.             spinlock_unlock(&CPU->timeoutlock);
  363.  
  364.             f(arg);
  365.  
  366.             spinlock_lock(&CPU->timeoutlock);
  367.         }
  368.         spinlock_unlock(&CPU->timeoutlock);
  369.     }
  370.     CPU->missed_clock_ticks = 0;
  371.  
  372.     /*
  373.      * Do CPU usage accounting and find out whether to preempt THREAD.
  374.      */
  375.  
  376.     if (THREAD) {
  377.         uint64_t ticks;
  378.        
  379.         spinlock_lock(&CPU->lock);
  380.         CPU->needs_relink += 1 + missed_clock_ticks;
  381.         spinlock_unlock(&CPU->lock);   
  382.    
  383.         spinlock_lock(&THREAD->lock);
  384.         if ((ticks = THREAD->ticks)) {
  385.             if (ticks >= 1 + missed_clock_ticks)
  386.                 THREAD->ticks -= 1 + missed_clock_ticks;
  387.             else
  388.                 THREAD->ticks = 0;
  389.         }
  390.         spinlock_unlock(&THREAD->lock);
  391.        
  392.         if (!ticks && !PREEMPTION_DISABLED) {
  393.             scheduler();
  394.         }
  395.     }
  396. }
  397.  
  398.  
  399.  
  400. #else
  401.  
  402.  
  403. /** Clock routine
  404.  *
  405.  * Clock routine executed from clock interrupt handler
  406.  * (assuming interrupts_disable()'d). Runs expired timeouts
  407.  * and preemptive scheduling.
  408.  *
  409.  */
  410. void clock(void)
  411. {
  412.     link_t *l;
  413.     timeout_t *h;
  414.     timeout_handler_t f;
  415.     void *arg;
  416.     count_t missed_clock_ticks = CPU->missed_clock_ticks;
  417.     int i;
  418.  
  419.     /*
  420.      * To avoid lock ordering problems,
  421.      * run all expired timeouts as you visit them.
  422.      */
  423.     for (i = 0; i <= missed_clock_ticks; i++) {
  424.         clock_update_counters();
  425.         spinlock_lock(&CPU->timeoutlock);
  426.         while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
  427.             h = list_get_instance(l, timeout_t, link);
  428.             spinlock_lock(&h->lock);
  429.             if (h->ticks-- != 0) {
  430.                 spinlock_unlock(&h->lock);
  431.                 break;
  432.             }
  433.             list_remove(l);
  434.             f = h->handler;
  435.             arg = h->arg;
  436.             timeout_reinitialize(h);
  437.             spinlock_unlock(&h->lock); 
  438.             spinlock_unlock(&CPU->timeoutlock);
  439.  
  440.             f(arg);
  441.  
  442.             spinlock_lock(&CPU->timeoutlock);
  443.         }
  444.         spinlock_unlock(&CPU->timeoutlock);
  445.     }
  446.     CPU->missed_clock_ticks = 0;
  447.  
  448.     /*
  449.      * Do CPU usage accounting and find out whether to preempt THREAD.
  450.      */
  451.  
  452.     if (THREAD) {
  453.         uint64_t ticks;
  454.        
  455.         spinlock_lock(&CPU->lock);
  456.         CPU->needs_relink += 1 + missed_clock_ticks;
  457.         spinlock_unlock(&CPU->lock);   
  458.    
  459.         spinlock_lock(&THREAD->lock);
  460.         if ((ticks = THREAD->ticks)) {
  461.             if (ticks >= 1 + missed_clock_ticks)
  462.                 THREAD->ticks -= 1 + missed_clock_ticks;
  463.             else
  464.                 THREAD->ticks = 0;
  465.         }
  466.         spinlock_unlock(&THREAD->lock);
  467.        
  468.         if (!ticks && !PREEMPTION_DISABLED) {
  469.             scheduler();
  470.         }
  471.     }
  472. }
  473.  
  474. #endif
  475. /** @}
  476.  */
  477.