|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Jean-Pierre Miceli <jean-pierre.miceli@heig-vd.ch> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License version 2 as |
| 6 | + * published by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program; if not, write to the Free Software |
| 15 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include <process.h> |
| 20 | +#include <heap.h> |
| 21 | +#include <errno.h> |
| 22 | +#include <futex.h> |
| 23 | + |
| 24 | +/** |
| 25 | + * do_futex_wait - block on futex_w |
| 26 | + * |
| 27 | + * @param futex_w address of the futex word |
| 28 | + * @param val expected value of the futex word |
| 29 | + * @return 0 on success or error value |
| 30 | + */ |
| 31 | +static int do_futex_wait(uint32_t *futex_w, uint32_t val) |
| 32 | +{ |
| 33 | + unsigned long flags; |
| 34 | + pcb_t *pcb = current()->pcb; |
| 35 | + struct list_head *pos; |
| 36 | + futex_t *futex; |
| 37 | + queue_thread_t f_element; |
| 38 | + |
| 39 | + flags = spin_lock_irqsave(&pcb->futex_lock); |
| 40 | + |
| 41 | + if (*futex_w != val) { |
| 42 | + spin_unlock_irqrestore(&pcb->futex_lock, flags); |
| 43 | + return -EAGAIN; |
| 44 | + } |
| 45 | + |
| 46 | + /* look if a futex_w already exists */ |
| 47 | + list_for_each(pos, &pcb->futex) { |
| 48 | + futex = list_entry(pos, futex_t, list); |
| 49 | + |
| 50 | + if ((uintptr_t) futex_w == futex->key) |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + if (list_is_head(pos, &pcb->futex)) { |
| 55 | + /* no futex on futex_w */ |
| 56 | + futex = (futex_t *) calloc(1, sizeof(futex_t)); |
| 57 | + if (futex == NULL) |
| 58 | + BUG(); |
| 59 | + |
| 60 | + futex->key = (uintptr_t) futex_w; |
| 61 | + |
| 62 | + INIT_LIST_HEAD(&futex->f_element); |
| 63 | + list_add_tail(&futex->list, &pcb->futex); |
| 64 | + } |
| 65 | + |
| 66 | + f_element.tcb = current(); |
| 67 | + |
| 68 | + list_add_tail(&f_element.list, &futex->f_element); |
| 69 | + |
| 70 | + /* go to sleep. */ |
| 71 | + spin_unlock(&pcb->futex_lock); |
| 72 | + waiting(); |
| 73 | + |
| 74 | + BUG_ON(local_irq_is_enabled()); |
| 75 | + |
| 76 | + spin_lock(&pcb->futex_lock); |
| 77 | + |
| 78 | + if (list_empty(&futex->f_element)) |
| 79 | + free(futex); |
| 80 | + |
| 81 | + spin_unlock_irqrestore(&pcb->futex_lock, flags); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * do_futex_wake - wake one or more tasks blocked on uaddr |
| 88 | + * |
| 89 | + * @nr_wake wake up to this many tasks |
| 90 | + * @return the number of waiters that were woken up |
| 91 | + */ |
| 92 | +static int do_futex_wake(uint32_t *futex_w, uint32_t nr_wake) |
| 93 | +{ |
| 94 | + unsigned long flags; |
| 95 | + pcb_t *pcb = current()->pcb; |
| 96 | + struct list_head *pos, *p; |
| 97 | + futex_t *futex; |
| 98 | + queue_thread_t *f_element; |
| 99 | + unsigned idx = 0; |
| 100 | + |
| 101 | + flags = spin_lock_irqsave(&pcb->futex_lock); |
| 102 | + |
| 103 | + /* Search for the futex element with futex_w as key */ |
| 104 | + list_for_each(pos, &pcb->futex) { |
| 105 | + futex = list_entry(pos, futex_t, list); |
| 106 | + |
| 107 | + if ((uintptr_t) futex_w == futex->key) |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + /* Check if the wanted key was found in the list */ |
| 112 | + if (list_is_head(pos, &pcb->futex)) { |
| 113 | + /* key does not exists in futex - Error */ |
| 114 | + spin_unlock_irqrestore(&pcb->futex_lock, flags); |
| 115 | + return -EINVAL; |
| 116 | + } |
| 117 | + |
| 118 | + /* wakes at most nr_wake of the waiters that are waiting */ |
| 119 | + list_for_each_safe(pos, p, &futex->list) { |
| 120 | + f_element = list_entry(pos, queue_thread_t, list); |
| 121 | + |
| 122 | + if (idx == nr_wake) |
| 123 | + break; |
| 124 | + |
| 125 | + list_del(&f_element->list); |
| 126 | + ready(f_element->tcb); |
| 127 | + |
| 128 | + idx++; |
| 129 | + } |
| 130 | + |
| 131 | + spin_unlock_irqrestore(&pcb->futex_lock, flags); |
| 132 | + |
| 133 | + return idx; |
| 134 | +} |
| 135 | + |
| 136 | +SYSCALL_DEFINE6(futex, uint32_t *, uaddr, int, op, uint32_t, val, const struct timespec *, utime, uint32_t *, uaddr2, uint32_t, |
| 137 | + val3) |
| 138 | +{ |
| 139 | + int cmd = op & FUTEX_CMD_MASK; |
| 140 | + |
| 141 | + if (utime) |
| 142 | + printk("[futex] utime parameter is not used in current implementation\n"); |
| 143 | + |
| 144 | + switch (cmd) { |
| 145 | + case FUTEX_WAIT: |
| 146 | + return do_futex_wait(uaddr, val); |
| 147 | + case FUTEX_WAKE: |
| 148 | + return do_futex_wake(uaddr, val); |
| 149 | + case FUTEX_FD: |
| 150 | + case FUTEX_REQUEUE: |
| 151 | + case FUTEX_CMP_REQUEUE: |
| 152 | + case FUTEX_WAKE_OP: |
| 153 | + case FUTEX_LOCK_PI: |
| 154 | + case FUTEX_UNLOCK_PI: |
| 155 | + case FUTEX_TRYLOCK_PI: |
| 156 | + case FUTEX_WAIT_BITSET: |
| 157 | + printk("Futex cmd '%d' not supported !\n"); |
| 158 | + return -EINVAL; |
| 159 | + } |
| 160 | + |
| 161 | + return -ENOSYS; |
| 162 | +} |
0 commit comments