123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
- /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
- /* Copyright (C) 2014 OSCAR lab, Stony Brook University
- This file is part of Graphene Library OS.
- Graphene Library OS is free software: you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- Graphene Library OS is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>. */
- /*
- * shim_futex.c
- *
- * Implementation of system call "futex", "set_robust_list" and
- * "get_robust_list".
- */
- #include <shim_internal.h>
- #include <shim_table.h>
- #include <shim_thread.h>
- #include <shim_checkpoint.h>
- #include <shim_utils.h>
- #include <pal.h>
- #include <pal_error.h>
- #include <linux_list.h>
- #include <sys/syscall.h>
- #include <sys/mman.h>
- #include <asm/prctl.h>
- #include <linux/futex.h>
- #include <errno.h>
- #define FUTEX_MIN_VALUE 0
- #define FUTEX_MAX_VALUE 255
- static LIST_HEAD(futex_list);
- static LOCKTYPE futex_list_lock;
- int shim_do_futex (unsigned int * uaddr, int op, int val, void * utime,
- unsigned int * uaddr2, int val3)
- {
- struct shim_futex_handle * tmp = NULL, * futex = NULL;
- struct shim_handle * hdl;
- int ret = 0;
- if (!uaddr || ((uintptr_t) uaddr % sizeof(unsigned int)))
- return -EINVAL;
- create_lock_runtime(&futex_list_lock);
- lock(futex_list_lock);
- list_for_each_entry(tmp, &futex_list, list)
- if (tmp->uaddr == uaddr) {
- futex = tmp;
- break;
- }
- if (futex) {
- hdl = container_of(tmp, struct shim_handle, info.futex);
- get_handle(hdl);
- } else {
- struct shim_vma * vma_addr = NULL;
- if (!(hdl = get_new_handle())) {
- unlock(futex_list_lock);
- return -ENOMEM;
- }
- hdl->type = TYPE_FUTEX;
- futex = &hdl->info.futex;
- futex->uaddr = uaddr;
- futex->vma = vma_addr;
- futex->event = DkSynchronizationEventCreate(0);
- if (futex->event == NULL) {
- ret = -PAL_ERRNO;
- unlock(futex_list_lock);
- goto out;
- }
- get_handle(hdl);
- INIT_LIST_HEAD(&futex->waiters);
- INIT_LIST_HEAD(&futex->list);
- list_add_tail(&futex->list, &futex_list);
- }
- unlock(futex_list_lock);
- lock(hdl->lock);
- struct futex_waiter {
- struct shim_thread * thread;
- struct list_head list;
- };
- switch (op & FUTEX_CMD_MASK) {
- case FUTEX_WAIT:
- if (*uaddr != val)
- break;
- struct futex_waiter waiter;
- thread_setwait(&waiter.thread, NULL);
- INIT_LIST_HEAD(&waiter.list);
- list_add_tail(&waiter.list, &futex->waiters);
- unlock(hdl->lock);
- thread_sleep();
- lock(hdl->lock);
- break;
- case FUTEX_WAKE: {
- for (int cnt = 0 ; cnt < val ; cnt++) {
- if (list_empty(&futex->waiters))
- break;
- struct futex_waiter * waiter = list_entry(futex->waiters.next,
- struct futex_waiter,
- list);
- list_del(&waiter->list);
- thread_wakeup(waiter->thread);
- }
- break;
- }
- case FUTEX_FD:
- ret = set_new_fd_handle(hdl, 0, NULL);
- break;
- default:
- ret = -ENOSYS;
- break;
- }
- unlock(hdl->lock);
- out:
- put_handle(hdl);
- return ret;
- }
- int shim_do_set_robust_list (struct robust_list_head * head, size_t len)
- {
- struct shim_thread * self = get_cur_thread();
- assert(self);
- if (len != sizeof(struct robust_list_head))
- return -EINVAL;
- self->robust_list = head;
- return 0;
- }
- int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
- size_t * len)
- {
- if (!head)
- return -EFAULT;
- struct shim_thread * thread;
- if (pid) {
- thread = lookup_thread(pid);
- if (!thread)
- return -ESRCH;
- } else {
- thread = get_cur_thread();
- }
- *head = (struct robust_list_head *) thread->robust_list;
- *len = sizeof(struct robust_list_head);
- return 0;
- }
- void release_robust_list (struct robust_list_head * head)
- {
- long futex_offset = head->futex_offset;
- struct robust_list * robust, * prev = &head->list;
- create_lock_runtime(&futex_list_lock);
- for (robust = prev->next ; robust && robust != prev ;
- prev = robust, robust = robust->next) {
- void * futex_addr = (void *) robust + futex_offset;
- struct shim_futex_handle * tmp, * futex = NULL;
- lock(futex_list_lock);
- list_for_each_entry(tmp, &futex_list, list)
- if (tmp->uaddr == futex_addr) {
- futex = tmp;
- break;
- }
- unlock(futex_list_lock);
- if (!futex)
- continue;
- DkEventSet(futex->event);
- }
- }
- void release_clear_child_id (int * clear_child_tid)
- {
- debug("clear child tid at %p\n", clear_child_tid);
- *clear_child_tid = 0;
- create_lock_runtime(&futex_list_lock);
- struct shim_futex_handle * tmp, * futex = NULL;
- lock(futex_list_lock);
- list_for_each_entry(tmp, &futex_list, list)
- if (tmp->uaddr == (void *) clear_child_tid) {
- futex = tmp;
- break;
- }
- unlock(futex_list_lock);
- if (!futex)
- return;
- debug("release futex at %p\n", clear_child_tid);
- DkEventSet(futex->event);
- }
|