shim_sched.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_sched.c
  15. *
  16. * Implementation of system call "sched_yield".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <api.h>
  21. #include <pal.h>
  22. #include <errno.h>
  23. int shim_do_sched_yield (void)
  24. {
  25. DkThreadYieldExecution();
  26. return 0;
  27. }
  28. int shim_do_sched_getaffinity (pid_t pid, size_t len,
  29. __kernel_cpu_set_t * user_mask_ptr)
  30. {
  31. __UNUSED(pid);
  32. int ncpus = PAL_CB(cpu_info.cpu_num);
  33. /* Check that user_mask_ptr is valid; if not, should return -EFAULT */
  34. if (test_user_memory(user_mask_ptr, len, 1))
  35. return -EFAULT;
  36. /* Linux kernel bitmap is based on long. So according to its
  37. * implementation, round up the result to sizeof(long) */
  38. size_t bitmask_long_count = (ncpus + sizeof(long) * 8 - 1) /
  39. (sizeof(long) * 8);
  40. size_t bitmask_size_in_bytes = bitmask_long_count * sizeof(long);
  41. if (len < bitmask_size_in_bytes)
  42. return -EINVAL;
  43. /* Linux kernel also rejects non-natural size */
  44. if (len & (sizeof(long) - 1))
  45. return -EINVAL;
  46. memset(user_mask_ptr, 0, len);
  47. for (int i = 0 ; i < ncpus ; i++)
  48. ((uint8_t *) user_mask_ptr)[i / 8] |= 1 << (i % 8);
  49. /* imitate the Linux kernel implementation
  50. * See SYSCALL_DEFINE3(sched_getaffinity) */
  51. return bitmask_size_in_bytes;
  52. }