schk.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _SCHK_H
  20. #define _SCHK_H
  21. #include <kernel/init.h>
  22. #include <kernel/types.h>
  23. #include <kernel/errno.h>
  24. #include <kernel/panic.h>
  25. #include <kernel/graphic.h>
  26. #include <arch/sys.h>
  27. void __stack_chk_fail(void);
  28. #if DEBUG_LEVEL == TEST_DEBUG_LEVEL
  29. #define schk_init() \
  30. __uint64_t old_sp = get_rsp(); \
  31. syslog(" entering function "); \
  32. syslog(__FUNCTION__); \
  33. if (old_sp < 0x3000) { \
  34. syslog("#1#PANIC: SP < 0x3000\n"); \
  35. __stack_chk_fail(); \
  36. } \
  37. if (old_sp < 0x4000) \
  38. syslog("#2#WARNING: SP < 0x4000 now"); \
  39. else if (old_sp < 0x5000) \
  40. syslog("#3#WARNING: SP < 0x5000 now"); \
  41. else if (old_sp < 0x6000) \
  42. syslog("#4#WARNING: SP < 0x6000 now"); \
  43. #elif DEBUG_LEVEL == DEV_DEBUG_LEVEL
  44. #define schk_init() __uint64_t old_sp = get_rsp();
  45. #elif DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
  46. #define schk_init()
  47. #else
  48. #error "unknow debug level (in init.h)"
  49. #endif
  50. #if DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
  51. #define schk_leave()
  52. #else
  53. #define schk_leave() \
  54. if (old_sp != get_rsp()) \
  55. __stack_chk_fail()
  56. #endif
  57. #endif