Subversion Repositories HelenOS-historic

Rev

Rev 1248 | Rev 1460 | Go to most recent revision | 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. /**
  30.  * @file    futex.c
  31.  * @brief   Kernel backend for futexes.
  32.  *
  33.  * @todo Deallocation of orphaned kernel-side futex structures is not currently implemented.
  34.  */
  35.  
  36. #include <synch/futex.h>
  37. #include <synch/rwlock.h>
  38. #include <synch/spinlock.h>
  39. #include <synch/synch.h>
  40. #include <mm/frame.h>
  41. #include <mm/page.h>
  42. #include <mm/slab.h>
  43. #include <proc/thread.h>
  44. #include <genarch/mm/page_pt.h>
  45. #include <genarch/mm/page_ht.h>
  46. #include <adt/hash_table.h>
  47. #include <adt/list.h>
  48. #include <arch.h>
  49. #include <align.h>
  50. #include <panic.h>
  51. #include <errno.h>
  52. #include <print.h>
  53.  
  54. #define FUTEX_HT_SIZE   1024    /* keep it a power of 2 */
  55.  
  56. static void futex_initialize(futex_t *futex);
  57.  
  58. static futex_t *futex_find(__address paddr);
  59. static index_t futex_ht_hash(__native *key);
  60. static bool futex_ht_compare(__native *key, count_t keys, link_t *item);
  61. static void futex_ht_remove_callback(link_t *item);
  62.  
  63. /** Read-write lock protecting global futex hash table. */
  64. static rwlock_t futex_ht_lock;
  65.  
  66. /** Futex hash table. */
  67. static hash_table_t futex_ht;
  68.  
  69. /** Futex hash table operations. */
  70. static hash_table_operations_t futex_ht_ops = {
  71.     .hash = futex_ht_hash,
  72.     .compare = futex_ht_compare,
  73.     .remove_callback = futex_ht_remove_callback
  74. };
  75.  
  76. /** Initialize futex subsystem. */
  77. void futex_init(void)
  78. {
  79.     rwlock_initialize(&futex_ht_lock);
  80.     hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
  81. }
  82.  
  83. /** Initialize kernel futex structure.
  84.  *
  85.  * @param futex Kernel futex structure.
  86.  */
  87. void futex_initialize(futex_t *futex)
  88. {
  89.     waitq_initialize(&futex->wq);
  90.     link_initialize(&futex->ht_link);
  91.     futex->paddr = 0;
  92. }
  93.  
  94. /** Sleep in futex wait queue.
  95.  *
  96.  * @param uaddr Userspace address of the futex counter.
  97.  * @param usec If non-zero, number of microseconds this thread is willing to sleep.
  98.  * @param trydown If usec is zero and trydown is non-zero, conditional operation will be attempted.
  99.  *
  100.  * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See synch.h.
  101.  *     If there is no physical mapping for uaddr ENOENT is returned.
  102.  */
  103. __native sys_futex_sleep_timeout(__address uaddr, __u32 usec, int trydown)
  104. {
  105.     futex_t *futex;
  106.     __address paddr;
  107.     pte_t *t;
  108.     ipl_t ipl;
  109.    
  110.     ipl = interrupts_disable();
  111.  
  112.     /*
  113.      * Find physical address of futex counter.
  114.      */
  115.     page_table_lock(AS, true);
  116.     t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
  117.     if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
  118.         page_table_unlock(AS, true);
  119.         interrupts_restore(ipl);
  120.         return (__native) ENOENT;
  121.     }
  122.     paddr = PFN2ADDR(PTE_GET_FRAME(t)) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
  123.     page_table_unlock(AS, true);
  124.    
  125.     interrupts_restore(ipl);   
  126.  
  127.     futex = futex_find(paddr);
  128.    
  129.     return (__native) waitq_sleep_timeout(&futex->wq, usec, trydown);
  130. }
  131.  
  132. /** Wakeup one thread waiting in futex wait queue.
  133.  *
  134.  * @param uaddr Userspace address of the futex counter.
  135.  *
  136.  * @return ENOENT if there is no futex associated with the address
  137.  *     or if there is no physical mapping for uaddr.
  138.  */
  139. __native sys_futex_wakeup(__address uaddr)
  140. {
  141.     futex_t *futex;
  142.     __address paddr;
  143.     pte_t *t;
  144.     ipl_t ipl;
  145.    
  146.     ipl = interrupts_disable();
  147.    
  148.     /*
  149.      * Find physical address of futex counter.
  150.      */
  151.     page_table_lock(AS, true);
  152.     t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
  153.     if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
  154.         page_table_unlock(AS, true);
  155.         interrupts_restore(ipl);
  156.         return (__native) ENOENT;
  157.     }
  158.     paddr = PFN2ADDR(PTE_GET_FRAME(t)) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
  159.     page_table_unlock(AS, true);
  160.    
  161.     interrupts_restore(ipl);
  162.  
  163.     futex = futex_find(paddr);
  164.        
  165.     waitq_wakeup(&futex->wq, WAKEUP_FIRST);
  166.    
  167.     return 0;
  168. }
  169.  
  170. /** Find kernel address of the futex structure corresponding to paddr.
  171.  *
  172.  * If the structure does not exist alreay, a new one is created.
  173.  *
  174.  * @param paddr Physical address of the userspace futex counter.
  175.  *
  176.  * @return Address of the kernel futex structure.
  177.  */
  178. futex_t *futex_find(__address paddr)
  179. {
  180.     link_t *item;
  181.     futex_t *futex;
  182.    
  183.     /*
  184.      * Find the respective futex structure
  185.      * or allocate new one if it does not exist already.
  186.      */
  187.     rwlock_read_lock(&futex_ht_lock);
  188.     item = hash_table_find(&futex_ht, &paddr);
  189.     if (item) {
  190.         futex = hash_table_get_instance(item, futex_t, ht_link);
  191.         rwlock_read_unlock(&futex_ht_lock);
  192.     } else {
  193.         /*
  194.          * Upgrade to writer is not currently supported,
  195.          * Therefore, it is necessary to release the read lock
  196.          * and reacquire it as a writer.
  197.          */
  198.         rwlock_read_unlock(&futex_ht_lock);
  199.  
  200.         rwlock_write_lock(&futex_ht_lock);
  201.         /*
  202.          * Avoid possible race condition by searching
  203.          * the hash table once again with write access.
  204.          */
  205.         item = hash_table_find(&futex_ht, &paddr);
  206.         if (item) {
  207.             futex = hash_table_get_instance(item, futex_t, ht_link);
  208.             rwlock_write_unlock(&futex_ht_lock);
  209.         } else {
  210.             futex = (futex_t *) malloc(sizeof(futex_t), 0);
  211.             futex_initialize(futex);
  212.             futex->paddr = paddr;
  213.             hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
  214.             rwlock_write_unlock(&futex_ht_lock);
  215.         }
  216.     }
  217.    
  218.     return futex;
  219. }
  220.  
  221. /** Compute hash index into futex hash table.
  222.  *
  223.  * @param key Address where the key (i.e. physical address of futex counter) is stored.
  224.  *
  225.  * @return Index into futex hash table.
  226.  */
  227. index_t futex_ht_hash(__native *key)
  228. {
  229.     return *key & (FUTEX_HT_SIZE-1);
  230. }
  231.  
  232. /** Compare futex hash table item with a key.
  233.  *
  234.  * @param key Address where the key (i.e. physical address of futex counter) is stored.
  235.  *
  236.  * @return True if the item matches the key. False otherwise.
  237.  */
  238. bool futex_ht_compare(__native *key, count_t keys, link_t *item)
  239. {
  240.     futex_t *futex;
  241.  
  242.     ASSERT(keys == 1);
  243.  
  244.     futex = hash_table_get_instance(item, futex_t, ht_link);
  245.     return *key == futex->paddr;
  246. }
  247.  
  248. /** Callback for removal items from futex hash table.
  249.  *
  250.  * @param item Item removed from the hash table.
  251.  */
  252. void futex_ht_remove_callback(link_t *item)
  253. {
  254.     futex_t *futex;
  255.  
  256.     futex = hash_table_get_instance(item, futex_t, ht_link);
  257.     free(futex);
  258. }
  259.