sched.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Process.h>
  27. namespace Kernel {
  28. int Process::sys$yield()
  29. {
  30. REQUIRE_PROMISE(stdio);
  31. Thread::current()->yield_without_holding_big_lock();
  32. return 0;
  33. }
  34. int Process::sys$donate(pid_t tid)
  35. {
  36. REQUIRE_PROMISE(stdio);
  37. if (tid < 0)
  38. return -EINVAL;
  39. ScopedCritical critical;
  40. auto thread = Thread::from_tid(tid);
  41. if (!thread || thread->pid() != pid())
  42. return -ESRCH;
  43. Thread::current()->donate_without_holding_big_lock(thread, "sys$donate");
  44. return 0;
  45. }
  46. int Process::sys$sched_setparam(int pid, Userspace<const struct sched_param*> user_param)
  47. {
  48. REQUIRE_PROMISE(proc);
  49. struct sched_param desired_param;
  50. if (!copy_from_user(&desired_param, user_param))
  51. return -EFAULT;
  52. if (desired_param.sched_priority < THREAD_PRIORITY_MIN || desired_param.sched_priority > THREAD_PRIORITY_MAX)
  53. return -EINVAL;
  54. auto* peer = Thread::current();
  55. ScopedSpinLock lock(g_scheduler_lock);
  56. if (pid != 0)
  57. peer = Thread::from_tid(pid);
  58. if (!peer)
  59. return -ESRCH;
  60. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  61. return -EPERM;
  62. peer->set_priority((u32)desired_param.sched_priority);
  63. return 0;
  64. }
  65. int Process::sys$sched_getparam(pid_t pid, Userspace<struct sched_param*> user_param)
  66. {
  67. REQUIRE_PROMISE(proc);
  68. int priority;
  69. {
  70. auto* peer = Thread::current();
  71. ScopedSpinLock lock(g_scheduler_lock);
  72. if (pid != 0) {
  73. // FIXME: PID/TID BUG
  74. // The entire process is supposed to be affected.
  75. peer = Thread::from_tid(pid);
  76. }
  77. if (!peer)
  78. return -ESRCH;
  79. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  80. return -EPERM;
  81. priority = (int)peer->priority();
  82. }
  83. struct sched_param param {
  84. priority
  85. };
  86. if (!copy_to_user(user_param, &param))
  87. return -EFAULT;
  88. return 0;
  89. }
  90. int Process::sys$set_thread_boost(pid_t tid, int amount)
  91. {
  92. REQUIRE_PROMISE(proc);
  93. if (amount < 0 || amount > 20)
  94. return -EINVAL;
  95. ScopedSpinLock lock(g_scheduler_lock);
  96. auto thread = Thread::from_tid(tid);
  97. if (!thread)
  98. return -ESRCH;
  99. if (thread->state() == Thread::State::Dead || thread->state() == Thread::State::Dying)
  100. return -ESRCH;
  101. if (!is_superuser() && thread->process().uid() != euid())
  102. return -EPERM;
  103. thread->set_priority_boost(amount);
  104. return 0;
  105. }
  106. int Process::sys$set_process_boost(pid_t pid, int amount)
  107. {
  108. REQUIRE_PROMISE(proc);
  109. if (amount < 0 || amount > 20)
  110. return -EINVAL;
  111. ScopedSpinLock lock(g_processes_lock);
  112. auto process = Process::from_pid(pid);
  113. if (!process || process->is_dead())
  114. return -ESRCH;
  115. if (!is_superuser() && process->uid() != euid())
  116. return -EPERM;
  117. process->m_priority_boost = amount;
  118. return 0;
  119. }
  120. }