panic.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/console.h>
  23. #include <kernel/panic.h>
  24. #include <arch/x86/int.h>
  25. #include <arch/io.h>
  26. #include <arch/sys.h>
  27. static int syslog_index = 0;
  28. void panic(const char *s) {
  29. printk(red, "kernel panic: %s\n", s);
  30. trace_syslog();
  31. for (;;) hlt();
  32. }
  33. void syslog(const char *s) {
  34. /* we can't use schk in this function, */
  35. /* because this will make a die-loop (schk_init calls this function) */
  36. int i;
  37. for (i = 0; s[i]; i++) {
  38. if (syslog_index == 32768)
  39. syslog_index = 0;
  40. *(syslog_index + SYSLOG_BUFFER) = s[i];
  41. syslog_index++;
  42. }
  43. return;
  44. }
  45. void trace_syslog(void) {
  46. printk(yellow, "tracing system log: \n");
  47. #if DEBUG_LEVEL != TEST_DEBUG_LEVEL
  48. printk(yellow, "system log disabled. \n");
  49. return;
  50. #else
  51. /* put last 8 calls to the screen */
  52. int i;
  53. char *fname = SYSLOG_BUFFER + syslog_index - 1;
  54. for (i = 0; i < TRACE_SYSLOG_NR; i++) {
  55. while (*--fname != '\t');
  56. char *fn = fname + 1;
  57. if (!(i % 3))
  58. putc('\n', yellow);
  59. print(yellow, "+ ");
  60. while (*fn != '\t')
  61. putc(*fn++, yellow);
  62. putc('\t', white);
  63. }
  64. putc('\n', yellow);
  65. return;
  66. #endif
  67. }