thread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * thread.h
  3. *
  4. * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __MONOLITHIUM_THREAD_H__
  20. #define __MONOLITHIUM_THREAD_H__
  21. #define THREAD_CREATE_FROZEN (1 << 0)
  22. #include "defs.h"
  23. #include "cpu.h"
  24. typedef dword_t (*thread_procedure_t)(void*);
  25. typedef qword_t affinity_t;
  26. typedef enum
  27. {
  28. THREAD_TID_INFO,
  29. THREAD_FROZEN_INFO,
  30. THREAD_CPU_STATE_INFO,
  31. THREAD_PRIORITY_INFO,
  32. THREAD_AFFINITY_INFO,
  33. } thread_info_t;
  34. typedef enum
  35. {
  36. THREAD_PRIORITY_HIGH,
  37. THREAD_PRIORITY_MID,
  38. THREAD_PRIORITY_LOW,
  39. THREAD_PRIORITY_IDLE,
  40. THREAD_PRIORITY_MAX
  41. } priority_t;
  42. typedef struct
  43. {
  44. registers_t regs;
  45. byte_t fpu_state[512];
  46. } thread_state_t;
  47. static inline void init_thread_stack(uintptr_t *stack_pointer, void *parameter)
  48. {
  49. static const byte_t thread_end_code[] = {
  50. /* push eax */
  51. 0x50,
  52. /* push INVALID_HANDLE */
  53. 0x68,
  54. INVALID_HANDLE & 0xFF,
  55. (INVALID_HANDLE >> 8) & 0xFF,
  56. (INVALID_HANDLE >> 16) & 0xFF,
  57. (INVALID_HANDLE >> 24) & 0xFF,
  58. /* mov eax, SYSCALL_TERMINATE_THREAD */
  59. 0xB8,
  60. SYSCALL_TERMINATE_THREAD & 0xFF,
  61. (SYSCALL_TERMINATE_THREAD >> 8) & 0xFF,
  62. (SYSCALL_TERMINATE_THREAD >> 16) & 0xFF,
  63. SYSCALL_TERMINATE_THREAD >> 24,
  64. /* mov edx, esp */
  65. 0x8B, 0xD4,
  66. /* int SYSCALL_INTERRUPT */
  67. 0xCD, SYSCALL_INTERRUPT
  68. };
  69. *stack_pointer -= (sizeof(thread_end_code) + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
  70. __builtin_memcpy((void*)*stack_pointer, thread_end_code, sizeof(thread_end_code));
  71. uintptr_t end_code_addr = *stack_pointer;
  72. push_to_stack(stack_pointer, (uintptr_t)parameter);
  73. push_to_stack(stack_pointer, end_code_addr);
  74. }
  75. sysret_t syscall_open_thread(dword_t tid, handle_t *handle);
  76. sysret_t syscall_create_thread(handle_t process, thread_state_t *initial_state, dword_t flags, priority_t priority, handle_t *new_thread);
  77. sysret_t syscall_terminate_thread(handle_t thread, dword_t return_value);
  78. sysret_t syscall_freeze_thread(handle_t handle);
  79. sysret_t syscall_thaw_thread(handle_t handle);
  80. sysret_t syscall_sleep(qword_t milliseconds);
  81. sysret_t syscall_yield_quantum(void);
  82. sysret_t syscall_query_thread(handle_t handle, thread_info_t info_type, void *buffer, size_t size);
  83. sysret_t syscall_set_thread(handle_t handle, thread_info_t info_type, const void *buffer, size_t size);
  84. #endif