console.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/types.h>
  21. #include <kernel/errno.h>
  22. #include <kernel/console.h>
  23. #include <kernel/vsprintf.h>
  24. #include <kernel/schk.h>
  25. #include <lib/string.h>
  26. #include <arch/io.h>
  27. #include <stdarg.h>
  28. static char buffer[256];
  29. static int syslog_index = 0;
  30. static __uint16_t *fb = FB_ADDR;
  31. __uint8_t x, y;
  32. struct tty tty0;
  33. struct tty stdin;
  34. struct tty stdout;
  35. struct tty stderr;
  36. extern struct fifo kbd_fifo;
  37. int nc_putc(struct tty *, char);
  38. errno_t console_init(void) {
  39. x = bootarg->x, y = bootarg->y;
  40. /* setup the frame buffer address fb */
  41. /* note that `fb` is a 16-bits pointer, so we don't need to '<< 1' */
  42. fb += y * VGA_X + x;
  43. tty0.input_fifo = NULL;
  44. tty0.mode = T_COOKED | T_NORD;
  45. tty0.ctl = 0;
  46. /* this is virtual */
  47. tty0.vx = x;
  48. tty0.vy = y;
  49. tty0.putc = nc_putc;
  50. stdin.input_fifo = &kbd_fifo;
  51. stdin.mode = T_COOKED;
  52. stdin.ctl = 0;
  53. stdin.vx = x;
  54. stdin.vy = y;
  55. memcpy(&stdout, &tty0, sizeof(struct tty));
  56. memcpy(&stdout, &tty0, sizeof(struct tty));
  57. /* well, this is the end */
  58. return 0;
  59. }
  60. int nc_putc(struct tty *tty, char c) {
  61. if (c == '\n') {
  62. tty->vy++;
  63. tty->vx = 0;
  64. /* no roll screen for tty->vy */
  65. putc('\n', white);
  66. return 0;
  67. }
  68. else if (c == '\b') {
  69. if (tty->vx)
  70. putc('\b', white);
  71. else
  72. return 0;
  73. }
  74. else if (c == '\t') {
  75. __uint64_t ax = (tty->vx + 3) & ~3;
  76. if (ax == tty->vx)
  77. tty->vx += TAB_SIZE;
  78. else
  79. tty->vx = ax;
  80. putc('\t', white);
  81. return 0;
  82. }
  83. else if (c == '\r') {
  84. tty->vx = 0;
  85. putc('\r', white);
  86. return 0;
  87. }
  88. putc(c, white);
  89. tty->vx++;
  90. return 0;
  91. }
  92. void syslog(const char *s) {
  93. /* we can't use schk in this function, */
  94. /* because this will make a die-loop (schk_init calls this function) */
  95. int i;
  96. for (i = 0; s[i]; i++) {
  97. if (syslog_index == 32768)
  98. syslog_index = 0;
  99. *(syslog_index + SYSLOG_BUFFER) = s[i];
  100. syslog_index++;
  101. }
  102. return;
  103. }
  104. int printk(__uint8_t color, char *s, ...) {
  105. schk_init();
  106. va_list args;
  107. int i;
  108. /* I think 256 is enough in kernel */
  109. /* too long, return it. */
  110. va_start(args, s);
  111. /* it maybe overflow, but that's in .data section and we did check. */
  112. i = vsprintf(buffer, s, args);
  113. print(color, buffer);
  114. memset(buffer, 0, 256);
  115. va_end(args);
  116. schk_leave();
  117. return i;
  118. }
  119. void print(__uint8_t color, char *s) {
  120. while (*s)
  121. putc(*s++, color);
  122. return;
  123. }
  124. void putc(char c, __uint8_t color) {
  125. if (c == '\n') {
  126. /* we do it like \r\n */
  127. x = 0;
  128. if (++y == VGA_Y) {
  129. roll_screen();
  130. y = 24;
  131. }
  132. update_cursor();
  133. return;
  134. }
  135. else if (c == '\r') {
  136. x = 0;
  137. update_cursor();
  138. return;
  139. }
  140. if (c == '\b') {
  141. if (x) {
  142. x--;
  143. update_cursor();
  144. *fb = 0x0720;
  145. }
  146. return;
  147. }
  148. else if (c == '\t') {
  149. __uint64_t ax = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
  150. if (ax == x)
  151. x += TAB_SIZE;
  152. else
  153. x = ax;
  154. update_cursor();
  155. return;
  156. }
  157. __asm__ __volatile__ (
  158. /* rdi is the frame buffer, and rax is the */
  159. "cld\n\t"
  160. "movb %%cl, %%ah\n\t"
  161. "stosw\n\t"
  162. : "+D" (fb)
  163. : "a" (c), "c" (color)
  164. );
  165. if (++x == VGA_X) {
  166. x = 0;
  167. if (++y == VGA_Y) {
  168. roll_screen();
  169. y = 24;
  170. }
  171. }
  172. update_cursor();
  173. return;
  174. }
  175. __uint16_t read_cursor(void) {
  176. __uint16_t pos = 0;
  177. outb(CMD_POS_LO, CURSOR_INDEX);
  178. pos |= inb(CURSOR_DATA);
  179. outb(CMD_POS_HI, CURSOR_INDEX);
  180. pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
  181. return pos;
  182. }
  183. void update_cursor(void) {
  184. if (y > VGA_Y) {
  185. roll_screen();
  186. y = 24;
  187. }
  188. __uint16_t pos = y * bootarg->vga_x + x;
  189. fb = FB_ADDR + pos;
  190. outb(CMD_POS_LO, CURSOR_INDEX);
  191. outb(pos & 0xff, CURSOR_DATA);
  192. outb(CMD_POS_HI, CURSOR_INDEX);
  193. outb((pos >> 8) & 0xff, CURSOR_DATA);
  194. return;
  195. }
  196. void roll_screen(void) {
  197. /* roll down the screen */
  198. __asm__ __volatile__ (
  199. "cld\n\t"
  200. "movq $960, %%rcx\n\t"
  201. "movq $0xb80a0, %%rsi\n\t"
  202. "movq $0xb8000, %%rdi\n\t"
  203. "rep\n\t"
  204. "movsq\n\t"
  205. "movq $0xb8f00, %%rdi\n\t"
  206. "movq $0x0720, %%rax\n\t"
  207. "movq $80, %%rcx\n\t"
  208. "rep\n\t"
  209. "stosw\n\t"
  210. :::
  211. );
  212. return;
  213. }