schk.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <arch/sys.h>
  26. void __stack_chk_fail(void);
  27. #if DEBUG_LEVEL == TEST_DEBUG_LEVEL
  28. #define schk_init() \
  29. __uint64_t old_sp = get_rsp(); \
  30. syslog(__FUNCTION__); \
  31. syslog("\t"); \
  32. if (old_sp < 0x3000) { \
  33. syslog("#1#PANIC: SP < 0x3000\n"); \
  34. __stack_chk_fail(); \
  35. } \
  36. if (old_sp < 0x4000) \
  37. syslog("#2#WARNING: SP < 0x4000 now"); \
  38. else if (old_sp < 0x5000) \
  39. syslog("#3#WARNING: SP < 0x5000 now"); \
  40. else if (old_sp < 0x6000) \
  41. syslog("#4#WARNING: SP < 0x6000 now"); \
  42. #elif DEBUG_LEVEL == DEV_DEBUG_LEVEL
  43. #define schk_init() __uint64_t old_sp = get_rsp();
  44. #elif DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
  45. #define schk_init()
  46. #else
  47. #error "unknow debug level (in init.h)"
  48. #endif
  49. #if DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
  50. #define schk_leave()
  51. #else
  52. #define schk_leave() \
  53. if (old_sp != get_rsp()) \
  54. __stack_chk_fail()
  55. #endif
  56. #endif