printf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2010-2019 Richard Braun.
  3. *
  4. * This program 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) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <kern/console.h>
  19. #include <kern/fmt.h>
  20. #include <kern/init.h>
  21. #include <kern/spinlock.h>
  22. #include <machine/boot.h>
  23. #include <machine/cpu.h>
  24. /*
  25. * Size of the static buffer.
  26. */
  27. #define PRINTF_BUFSIZE 1024
  28. static char printf_buffer[PRINTF_BUFSIZE];
  29. static struct spinlock printf_lock;
  30. static int
  31. vprintf_common(const char *format, va_list ap)
  32. {
  33. int length;
  34. length = fmt_vsnprintf(printf_buffer, sizeof(printf_buffer), format, ap);
  35. for (char *ptr = printf_buffer; *ptr != '\0'; ptr++) {
  36. console_putchar(*ptr);
  37. }
  38. return length;
  39. }
  40. int
  41. printf(const char *format, ...)
  42. {
  43. va_list ap;
  44. int length;
  45. va_start(ap, format);
  46. length = vprintf(format, ap);
  47. va_end(ap);
  48. return length;
  49. }
  50. int
  51. vprintf(const char *format, va_list ap)
  52. {
  53. unsigned long flags;
  54. int length;
  55. spinlock_lock_intr_save(&printf_lock, &flags);
  56. length = vprintf_common(format, ap);
  57. spinlock_unlock_intr_restore(&printf_lock, flags);
  58. return length;
  59. }
  60. int
  61. printf_ln(const char *format, ...)
  62. {
  63. va_list ap;
  64. int length;
  65. va_start(ap, format);
  66. length = vprintf_ln(format, ap);
  67. va_end(ap);
  68. return length;
  69. }
  70. int
  71. vprintf_ln(const char *format, va_list ap)
  72. {
  73. unsigned long flags;
  74. int length;
  75. spinlock_lock_intr_save(&printf_lock, &flags);
  76. length = vprintf_common(format, ap);
  77. console_putchar('\n');
  78. spinlock_unlock_intr_restore(&printf_lock, flags);
  79. return length;
  80. }
  81. static int __init
  82. printf_setup(void)
  83. {
  84. spinlock_init(&printf_lock);
  85. return 0;
  86. }
  87. INIT_OP_DEFINE(printf_setup,
  88. INIT_OP_DEP(boot_bootstrap_console, true),
  89. INIT_OP_DEP(console_bootstrap, true),
  90. INIT_OP_DEP(spinlock_setup, true));