task_stack.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef _LINUX_SCHED_TASK_STACK_H
  2. #define _LINUX_SCHED_TASK_STACK_H
  3. /*
  4. * task->stack (kernel stack) handling interfaces:
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/magic.h>
  8. #ifdef CONFIG_THREAD_INFO_IN_TASK
  9. /*
  10. * When accessing the stack of a non-current task that might exit, use
  11. * try_get_task_stack() instead. task_stack_page will return a pointer
  12. * that could get freed out from under you.
  13. */
  14. static inline void *task_stack_page(const struct task_struct *task)
  15. {
  16. return task->stack;
  17. }
  18. #define setup_thread_stack(new,old) do { } while(0)
  19. static inline unsigned long *end_of_stack(const struct task_struct *task)
  20. {
  21. return task->stack;
  22. }
  23. #elif !defined(__HAVE_THREAD_FUNCTIONS)
  24. #define task_stack_page(task) ((void *)(task)->stack)
  25. static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
  26. {
  27. *task_thread_info(p) = *task_thread_info(org);
  28. task_thread_info(p)->task = p;
  29. }
  30. /*
  31. * Return the address of the last usable long on the stack.
  32. *
  33. * When the stack grows down, this is just above the thread
  34. * info struct. Going any lower will corrupt the threadinfo.
  35. *
  36. * When the stack grows up, this is the highest address.
  37. * Beyond that position, we corrupt data on the next page.
  38. */
  39. static inline unsigned long *end_of_stack(struct task_struct *p)
  40. {
  41. #ifdef CONFIG_STACK_GROWSUP
  42. return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
  43. #else
  44. return (unsigned long *)(task_thread_info(p) + 1);
  45. #endif
  46. }
  47. #endif
  48. #ifdef CONFIG_THREAD_INFO_IN_TASK
  49. static inline void *try_get_task_stack(struct task_struct *tsk)
  50. {
  51. return atomic_inc_not_zero(&tsk->stack_refcount) ?
  52. task_stack_page(tsk) : NULL;
  53. }
  54. extern void put_task_stack(struct task_struct *tsk);
  55. #else
  56. static inline void *try_get_task_stack(struct task_struct *tsk)
  57. {
  58. return task_stack_page(tsk);
  59. }
  60. static inline void put_task_stack(struct task_struct *tsk) {}
  61. #endif
  62. #define task_stack_end_corrupted(task) \
  63. (*(end_of_stack(task)) != STACK_END_MAGIC)
  64. static inline int object_is_on_stack(void *obj)
  65. {
  66. void *stack = task_stack_page(current);
  67. return (obj >= stack) && (obj < (stack + THREAD_SIZE));
  68. }
  69. extern void thread_stack_cache_init(void);
  70. #ifdef CONFIG_DEBUG_STACK_USAGE
  71. static inline unsigned long stack_not_used(struct task_struct *p)
  72. {
  73. unsigned long *n = end_of_stack(p);
  74. do { /* Skip over canary */
  75. # ifdef CONFIG_STACK_GROWSUP
  76. n--;
  77. # else
  78. n++;
  79. # endif
  80. } while (!*n);
  81. # ifdef CONFIG_STACK_GROWSUP
  82. return (unsigned long)end_of_stack(p) - (unsigned long)n;
  83. # else
  84. return (unsigned long)n - (unsigned long)end_of_stack(p);
  85. # endif
  86. }
  87. #endif
  88. extern void set_task_stack_end_magic(struct task_struct *tsk);
  89. #endif /* _LINUX_SCHED_TASK_STACK_H */