sched.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(int tid)
  35. {
  36. REQUIRE_PROMISE(stdio);
  37. if (tid < 0)
  38. return -EINVAL;
  39. InterruptDisabler disabler;
  40. auto* thread = Thread::from_tid(tid);
  41. if (!thread || thread->pid() != pid())
  42. return -ESRCH;
  43. Scheduler::donate_to(thread, "sys$donate");
  44. return 0;
  45. }
  46. int Process::sys$sched_setparam(int tid, Userspace<const struct sched_param*> user_param)
  47. {
  48. REQUIRE_PROMISE(proc);
  49. if (!validate_read_typed(user_param))
  50. return -EFAULT;
  51. struct sched_param desired_param;
  52. copy_from_user(&desired_param, user_param);
  53. InterruptDisabler disabler;
  54. auto* peer = Thread::current();
  55. if (tid != 0)
  56. peer = Thread::from_tid(tid);
  57. if (!peer)
  58. return -ESRCH;
  59. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  60. return -EPERM;
  61. if (desired_param.sched_priority < THREAD_PRIORITY_MIN ||
  62. desired_param.sched_priority > THREAD_PRIORITY_MAX)
  63. return -EINVAL;
  64. peer->set_priority((u32)desired_param.sched_priority);
  65. return 0;
  66. }
  67. int Process::sys$sched_getparam(pid_t pid, struct sched_param* param)
  68. {
  69. REQUIRE_PROMISE(proc);
  70. if (!validate_write_typed(param))
  71. return -EFAULT;
  72. InterruptDisabler disabler;
  73. auto* peer = Thread::current();
  74. if (pid != 0)
  75. peer = Thread::from_tid(pid);
  76. if (!peer)
  77. return -ESRCH;
  78. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  79. return -EPERM;
  80. int priority = peer->priority();
  81. copy_to_user(&param->sched_priority, &priority);
  82. return 0;
  83. }
  84. int Process::sys$set_thread_boost(int tid, int amount)
  85. {
  86. REQUIRE_PROMISE(proc);
  87. if (amount < 0 || amount > 20)
  88. return -EINVAL;
  89. InterruptDisabler disabler;
  90. auto* thread = Thread::from_tid(tid);
  91. if (!thread)
  92. return -ESRCH;
  93. if (thread->state() == Thread::State::Dead || thread->state() == Thread::State::Dying)
  94. return -ESRCH;
  95. if (!is_superuser() && thread->process().uid() != euid())
  96. return -EPERM;
  97. thread->set_priority_boost(amount);
  98. return 0;
  99. }
  100. int Process::sys$set_process_boost(pid_t pid, int amount)
  101. {
  102. REQUIRE_PROMISE(proc);
  103. if (amount < 0 || amount > 20)
  104. return -EINVAL;
  105. ScopedSpinLock lock(g_processes_lock);
  106. auto* process = Process::from_pid(pid);
  107. if (!process || process->is_dead())
  108. return -ESRCH;
  109. if (!is_superuser() && process->uid() != euid())
  110. return -EPERM;
  111. process->m_priority_boost = amount;
  112. return 0;
  113. }
  114. }