shim_sched.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. int ncpus = PAL_CB(cpu_info.cpu_num);
  32. /* Check that user_mask_ptr is valid; if not, should return -EFAULT */
  33. if (test_user_memory(user_mask_ptr, len, 1))
  34. return -EFAULT;
  35. /* Linux kernel bitmap is based on long. So according to its
  36. * implementation, round up the result to sizeof(long) */
  37. int bitmask_long_count = (ncpus + sizeof(long) * 8 - 1) /
  38. (sizeof(long) * 8);
  39. int bitmask_size_in_bytes = bitmask_long_count * sizeof(long);
  40. if (len < bitmask_size_in_bytes)
  41. return -EINVAL;
  42. /* Linux kernel also rejects non-natural size */
  43. if (len & (sizeof(long) - 1))
  44. return -EINVAL;
  45. memset(user_mask_ptr, 0, len);
  46. for (int i = 0 ; i < ncpus ; i++)
  47. ((uint8_t *) user_mask_ptr)[i / 8] |= 1 << (i % 8);
  48. /* imitate the Linux kernel implementation
  49. * See SYSCALL_DEFINE3(sched_getaffinity) */
  50. return bitmask_size_in_bytes;
  51. }