shim_sched.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <api.h>
  19. #include <errno.h>
  20. #include <pal.h>
  21. #include <shim_internal.h>
  22. #include <shim_table.h>
  23. int shim_do_sched_yield(void) {
  24. DkThreadYieldExecution();
  25. return 0;
  26. }
  27. int shim_do_sched_getaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr) {
  28. __UNUSED(pid);
  29. int ncpus = PAL_CB(cpu_info.cpu_num);
  30. /* Check that user_mask_ptr is valid; if not, should return -EFAULT */
  31. if (test_user_memory(user_mask_ptr, len, 1))
  32. return -EFAULT;
  33. /* Linux kernel bitmap is based on long. So according to its
  34. * implementation, round up the result to sizeof(long) */
  35. size_t bitmask_long_count = (ncpus + sizeof(long) * 8 - 1) / (sizeof(long) * 8);
  36. size_t bitmask_size_in_bytes = bitmask_long_count * sizeof(long);
  37. if (len < bitmask_size_in_bytes)
  38. return -EINVAL;
  39. /* Linux kernel also rejects non-natural size */
  40. if (len & (sizeof(long) - 1))
  41. return -EINVAL;
  42. memset(user_mask_ptr, 0, len);
  43. for (int i = 0; i < ncpus; i++) {
  44. ((uint8_t*)user_mask_ptr)[i / 8] |= 1 << (i % 8);
  45. }
  46. /* imitate the Linux kernel implementation
  47. * See SYSCALL_DEFINE3(sched_getaffinity) */
  48. return bitmask_size_in_bytes;
  49. }