Subversion Repositories HelenOS

Rev

Rev 3665 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup sync
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief   Kernel backend for futexes.
  36.  */
  37.  
  38. #include <synch/futex.h>
  39. #include <synch/rwlock.h>
  40. #include <synch/spinlock.h>
  41. #include <synch/synch.h>
  42. #include <mm/frame.h>
  43. #include <mm/page.h>
  44. #include <mm/slab.h>
  45. #include <proc/thread.h>
  46. #include <proc/task.h>
  47. #include <genarch/mm/page_pt.h>
  48. #include <genarch/mm/page_ht.h>
  49. #include <adt/hash_table.h>
  50. #include <adt/list.h>
  51. #include <arch.h>
  52. #include <align.h>
  53. #include <panic.h>
  54. #include <errno.h>
  55. #include <print.h>
  56. #include <arch/asm.h>
  57.  
  58. #define FUTEX_HT_SIZE   1024    /* keep it a power of 2 */
  59.  
  60. static void futex_initialize(futex_t *futex);
  61.  
  62. static futex_t *futex_find(uintptr_t paddr);
  63. static index_t futex_ht_hash(unative_t *key);
  64. static bool futex_ht_compare(unative_t *key, count_t keys, link_t *item);
  65. static void futex_ht_remove_callback(link_t *item);
  66.  
  67. /**
  68.  * Read-write lock protecting global futex hash table.
  69.  * It is also used to serialize access to all futex_t structures.
  70.  * Must be acquired before the task futex B+tree lock.
  71.  */
  72. static rwlock_t futex_ht_lock;
  73.  
  74. /** Futex hash table. */
  75. static hash_table_t futex_ht;
  76.  
  77. /** Futex hash table operations. */
  78. static hash_table_operations_t futex_ht_ops = {
  79.     .hash = futex_ht_hash,
  80.     .compare = futex_ht_compare,
  81.     .remove_callback = futex_ht_remove_callback
  82. };
  83.  
  84. /** Initialize futex subsystem. */
  85. void futex_init(void)
  86. {
  87.     rwlock_initialize(&futex_ht_lock);
  88.     hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
  89. }
  90.  
  91. /** Initialize kernel futex structure.
  92.  *
  93.  * @param futex Kernel futex structure.
  94.  */
  95. void futex_initialize(futex_t *futex)
  96. {
  97.     waitq_initialize(&futex->wq);
  98.     link_initialize(&futex->ht_link);
  99.     futex->paddr = 0;
  100.     futex->refcount = 1;
  101. }
  102.  
  103. /** Sleep in futex wait queue.
  104.  *
  105.  * @param uaddr Userspace address of the futex counter.
  106.  * @param usec If non-zero, number of microseconds this thread is willing to
  107.  *     sleep.
  108.  * @param flags Select mode of operation.
  109.  *
  110.  * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See
  111.  *     synch.h. If there is no physical mapping for uaddr ENOENT is returned.
  112.  */
  113. unative_t sys_futex_sleep_timeout(uintptr_t uaddr, uint32_t usec, int flags)
  114. {
  115.     futex_t *futex;
  116.     uintptr_t paddr;
  117.     pte_t *t;
  118.     ipl_t ipl;
  119.     int rc;
  120.    
  121.     ipl = interrupts_disable();
  122.  
  123.     /*
  124.      * Find physical address of futex counter.
  125.      */
  126.     page_table_lock(AS, true);
  127.     t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
  128.     if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
  129.         page_table_unlock(AS, true);
  130.         interrupts_restore(ipl);
  131.         return (unative_t) ENOENT;
  132.     }
  133.     paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
  134.     page_table_unlock(AS, true);
  135.    
  136.     interrupts_restore(ipl);   
  137.  
  138.     futex = futex_find(paddr);
  139.  
  140. #ifdef CONFIG_UDEBUG
  141.     udebug_stoppable_begin();
  142. #endif
  143.     rc = waitq_sleep_timeout(&futex->wq, usec, flags |
  144.         SYNCH_FLAGS_INTERRUPTIBLE);
  145.  
  146. #ifdef CONFIG_UDEBUG
  147.     udebug_stoppable_end();
  148. #endif
  149.     return (unative_t) rc;
  150. }
  151.  
  152. /** Wakeup one thread waiting in futex wait queue.
  153.  *
  154.  * @param uaddr Userspace address of the futex counter.
  155.  *
  156.  * @return ENOENT if there is no physical mapping for uaddr.
  157.  */
  158. unative_t sys_futex_wakeup(uintptr_t uaddr)
  159. {
  160.     futex_t *futex;
  161.     uintptr_t paddr;
  162.     pte_t *t;
  163.     ipl_t ipl;
  164.    
  165.     ipl = interrupts_disable();
  166.    
  167.     /*
  168.      * Find physical address of futex counter.
  169.      */
  170.     page_table_lock(AS, true);
  171.     t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
  172.     if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
  173.         page_table_unlock(AS, true);
  174.         interrupts_restore(ipl);
  175.         return (unative_t) ENOENT;
  176.     }
  177.     paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
  178.     page_table_unlock(AS, true);
  179.    
  180.     interrupts_restore(ipl);
  181.  
  182.     futex = futex_find(paddr);
  183.        
  184.     waitq_wakeup(&futex->wq, WAKEUP_FIRST);
  185.    
  186.     return 0;
  187. }
  188.  
  189. /** Find kernel address of the futex structure corresponding to paddr.
  190.  *
  191.  * If the structure does not exist already, a new one is created.
  192.  *
  193.  * @param paddr Physical address of the userspace futex counter.
  194.  *
  195.  * @return Address of the kernel futex structure.
  196.  */
  197. futex_t *futex_find(uintptr_t paddr)
  198. {
  199.     link_t *item;
  200.     futex_t *futex;
  201.     btree_node_t *leaf;
  202.    
  203.     /*
  204.      * Find the respective futex structure
  205.      * or allocate new one if it does not exist already.
  206.      */
  207.     rwlock_read_lock(&futex_ht_lock);
  208.     item = hash_table_find(&futex_ht, &paddr);
  209.     if (item) {
  210.         futex = hash_table_get_instance(item, futex_t, ht_link);
  211.  
  212.         /*
  213.          * See if the current task knows this futex.
  214.          */
  215.         mutex_lock(&TASK->futexes_lock);
  216.         if (!btree_search(&TASK->futexes, paddr, &leaf)) {
  217.             /*
  218.              * The futex is new to the current task.
  219.              * However, we only have read access.
  220.              * Gain write access and try again.
  221.              */
  222.             mutex_unlock(&TASK->futexes_lock);
  223.             goto gain_write_access;
  224.         }
  225.         mutex_unlock(&TASK->futexes_lock);
  226.  
  227.         rwlock_read_unlock(&futex_ht_lock);
  228.     } else {
  229. gain_write_access:
  230.         /*
  231.          * Upgrade to writer is not currently supported,
  232.          * therefore, it is necessary to release the read lock
  233.          * and reacquire it as a writer.
  234.          */
  235.         rwlock_read_unlock(&futex_ht_lock);
  236.  
  237.         rwlock_write_lock(&futex_ht_lock);
  238.         /*
  239.          * Avoid possible race condition by searching
  240.          * the hash table once again with write access.
  241.          */
  242.         item = hash_table_find(&futex_ht, &paddr);
  243.         if (item) {
  244.             futex = hash_table_get_instance(item, futex_t, ht_link);
  245.            
  246.             /*
  247.              * See if this futex is known to the current task.
  248.              */
  249.             mutex_lock(&TASK->futexes_lock);
  250.             if (!btree_search(&TASK->futexes, paddr, &leaf)) {
  251.                 /*
  252.                  * The futex is new to the current task.
  253.                  * Upgrade its reference count and put it to the
  254.                  * current task's B+tree of known futexes.
  255.                  */
  256.                 futex->refcount++;
  257.                 btree_insert(&TASK->futexes, paddr, futex,
  258.                     leaf);
  259.             }
  260.             mutex_unlock(&TASK->futexes_lock);
  261.    
  262.             rwlock_write_unlock(&futex_ht_lock);
  263.         } else {
  264.             futex = (futex_t *) malloc(sizeof(futex_t), 0);
  265.             futex_initialize(futex);
  266.             futex->paddr = paddr;
  267.             hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
  268.            
  269.             /*
  270.              * This is the first task referencing the futex.
  271.              * It can be directly inserted into its
  272.              * B+tree of known futexes.
  273.              */
  274.             mutex_lock(&TASK->futexes_lock);
  275.             btree_insert(&TASK->futexes, paddr, futex, NULL);
  276.             mutex_unlock(&TASK->futexes_lock);
  277.            
  278.             rwlock_write_unlock(&futex_ht_lock);
  279.         }
  280.     }
  281.    
  282.     return futex;
  283. }
  284.  
  285. /** Compute hash index into futex hash table.
  286.  *
  287.  * @param key Address where the key (i.e. physical address of futex counter) is
  288.  *     stored.
  289.  *
  290.  * @return Index into futex hash table.
  291.  */
  292. index_t futex_ht_hash(unative_t *key)
  293. {
  294.     return *key & (FUTEX_HT_SIZE-1);
  295. }
  296.  
  297. /** Compare futex hash table item with a key.
  298.  *
  299.  * @param key Address where the key (i.e. physical address of futex counter) is
  300.  *     stored.
  301.  *
  302.  * @return True if the item matches the key. False otherwise.
  303.  */
  304. bool futex_ht_compare(unative_t *key, count_t keys, link_t *item)
  305. {
  306.     futex_t *futex;
  307.  
  308.     ASSERT(keys == 1);
  309.  
  310.     futex = hash_table_get_instance(item, futex_t, ht_link);
  311.     return *key == futex->paddr;
  312. }
  313.  
  314. /** Callback for removal items from futex hash table.
  315.  *
  316.  * @param item Item removed from the hash table.
  317.  */
  318. void futex_ht_remove_callback(link_t *item)
  319. {
  320.     futex_t *futex;
  321.  
  322.     futex = hash_table_get_instance(item, futex_t, ht_link);
  323.     free(futex);
  324. }
  325.  
  326. /** Remove references from futexes known to the current task. */
  327. void futex_cleanup(void)
  328. {
  329.     link_t *cur;
  330.    
  331.     rwlock_write_lock(&futex_ht_lock);
  332.     mutex_lock(&TASK->futexes_lock);
  333.  
  334.     for (cur = TASK->futexes.leaf_head.next;
  335.         cur != &TASK->futexes.leaf_head; cur = cur->next) {
  336.         btree_node_t *node;
  337.         unsigned int i;
  338.        
  339.         node = list_get_instance(cur, btree_node_t, leaf_link);
  340.         for (i = 0; i < node->keys; i++) {
  341.             futex_t *ftx;
  342.             uintptr_t paddr = node->key[i];
  343.            
  344.             ftx = (futex_t *) node->value[i];
  345.             if (--ftx->refcount == 0)
  346.                 hash_table_remove(&futex_ht, &paddr, 1);
  347.         }
  348.     }
  349.    
  350.     mutex_unlock(&TASK->futexes_lock);
  351.     rwlock_write_unlock(&futex_ht_lock);
  352. }
  353.  
  354. /** @}
  355.  */
  356.