Subversion Repositories HelenOS-historic

Rev

Rev 1264 | Rev 1502 | 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 = 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 physical mapping for uaddr.
  137.  */
  138. __native sys_futex_wakeup(__address uaddr)
  139. {
  140.     futex_t *futex;
  141.     __address paddr;
  142.     pte_t *t;
  143.     ipl_t ipl;
  144.    
  145.     ipl = interrupts_disable();
  146.    
  147.     /*
  148.      * Find physical address of futex counter.
  149.      */
  150.     page_table_lock(AS, true);
  151.     t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
  152.     if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
  153.         page_table_unlock(AS, true);
  154.         interrupts_restore(ipl);
  155.         return (__native) ENOENT;
  156.     }
  157.     paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
  158.     page_table_unlock(AS, true);
  159.    
  160.     interrupts_restore(ipl);
  161.  
  162.     futex = futex_find(paddr);
  163.        
  164.     waitq_wakeup(&futex->wq, WAKEUP_FIRST);
  165.    
  166.     return 0;
  167. }
  168.  
  169. /** Find kernel address of the futex structure corresponding to paddr.
  170.  *
  171.  * If the structure does not exist alreay, a new one is created.
  172.  *
  173.  * @param paddr Physical address of the userspace futex counter.
  174.  *
  175.  * @return Address of the kernel futex structure.
  176.  */
  177. futex_t *futex_find(__address paddr)
  178. {
  179.     link_t *item;
  180.     futex_t *futex;
  181.    
  182.     /*
  183.      * Find the respective futex structure
  184.      * or allocate new one if it does not exist already.
  185.      */
  186.     rwlock_read_lock(&futex_ht_lock);
  187.     item = hash_table_find(&futex_ht, &paddr);
  188.     if (item) {
  189.         futex = hash_table_get_instance(item, futex_t, ht_link);
  190.         rwlock_read_unlock(&futex_ht_lock);
  191.     } else {
  192.         /*
  193.          * Upgrade to writer is not currently supported,
  194.          * therefore, it is necessary to release the read lock
  195.          * and reacquire it as a writer.
  196.          */
  197.         rwlock_read_unlock(&futex_ht_lock);
  198.  
  199.         rwlock_write_lock(&futex_ht_lock);
  200.         /*
  201.          * Avoid possible race condition by searching
  202.          * the hash table once again with write access.
  203.          */
  204.         item = hash_table_find(&futex_ht, &paddr);
  205.         if (item) {
  206.             futex = hash_table_get_instance(item, futex_t, ht_link);
  207.             rwlock_write_unlock(&futex_ht_lock);
  208.         } else {
  209.             futex = (futex_t *) malloc(sizeof(futex_t), 0);
  210.             futex_initialize(futex);
  211.             futex->paddr = paddr;
  212.             hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
  213.             rwlock_write_unlock(&futex_ht_lock);
  214.         }
  215.     }
  216.    
  217.     return futex;
  218. }
  219.  
  220. /** Compute hash index into futex hash table.
  221.  *
  222.  * @param key Address where the key (i.e. physical address of futex counter) is stored.
  223.  *
  224.  * @return Index into futex hash table.
  225.  */
  226. index_t futex_ht_hash(__native *key)
  227. {
  228.     return *key & (FUTEX_HT_SIZE-1);
  229. }
  230.  
  231. /** Compare futex hash table item with a key.
  232.  *
  233.  * @param key Address where the key (i.e. physical address of futex counter) is stored.
  234.  *
  235.  * @return True if the item matches the key. False otherwise.
  236.  */
  237. bool futex_ht_compare(__native *key, count_t keys, link_t *item)
  238. {
  239.     futex_t *futex;
  240.  
  241.     ASSERT(keys == 1);
  242.  
  243.     futex = hash_table_get_instance(item, futex_t, ht_link);
  244.     return *key == futex->paddr;
  245. }
  246.  
  247. /** Callback for removal items from futex hash table.
  248.  *
  249.  * @param item Item removed from the hash table.
  250.  */
  251. void futex_ht_remove_callback(link_t *item)
  252. {
  253.     futex_t *futex;
  254.  
  255.     futex = hash_table_get_instance(item, futex_t, ht_link);
  256.     free(futex);
  257. }
  258.