start.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 1996 Paul Mackerras.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/config.h>
  10. #include <linux/string.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/sysrq.h>
  14. #include <linux/init.h>
  15. #include <asm/machdep.h>
  16. #include <asm/io.h>
  17. #include <asm/page.h>
  18. #include <asm/prom.h>
  19. #include <asm/processor.h>
  20. #include <asm/udbg.h>
  21. #include <asm/system.h>
  22. #include "nonstdio.h"
  23. #ifdef CONFIG_MAGIC_SYSRQ
  24. static void sysrq_handle_xmon(int key, struct pt_regs *pt_regs,
  25. struct tty_struct *tty)
  26. {
  27. /* ensure xmon is enabled */
  28. xmon_init(1);
  29. debugger(pt_regs);
  30. }
  31. static struct sysrq_key_op sysrq_xmon_op =
  32. {
  33. .handler = sysrq_handle_xmon,
  34. .help_msg = "Xmon",
  35. .action_msg = "Entering xmon",
  36. };
  37. static int __init setup_xmon_sysrq(void)
  38. {
  39. register_sysrq_key('x', &sysrq_xmon_op);
  40. return 0;
  41. }
  42. __initcall(setup_xmon_sysrq);
  43. #endif /* CONFIG_MAGIC_SYSRQ */
  44. int
  45. xmon_write(void *handle, void *ptr, int nb)
  46. {
  47. return udbg_write(ptr, nb);
  48. }
  49. int
  50. xmon_read(void *handle, void *ptr, int nb)
  51. {
  52. return udbg_read(ptr, nb);
  53. }
  54. int
  55. xmon_read_poll(void)
  56. {
  57. if (udbg_getc_poll)
  58. return udbg_getc_poll();
  59. return -1;
  60. }
  61. FILE *xmon_stdin;
  62. FILE *xmon_stdout;
  63. int
  64. xmon_putc(int c, void *f)
  65. {
  66. char ch = c;
  67. if (c == '\n')
  68. xmon_putc('\r', f);
  69. return xmon_write(f, &ch, 1) == 1? c: -1;
  70. }
  71. int
  72. xmon_putchar(int c)
  73. {
  74. return xmon_putc(c, xmon_stdout);
  75. }
  76. int
  77. xmon_fputs(char *str, void *f)
  78. {
  79. int n = strlen(str);
  80. return xmon_write(f, str, n) == n? 0: -1;
  81. }
  82. int
  83. xmon_readchar(void)
  84. {
  85. char ch;
  86. for (;;) {
  87. switch (xmon_read(xmon_stdin, &ch, 1)) {
  88. case 1:
  89. return ch;
  90. case -1:
  91. xmon_printf("read(stdin) returned -1\r\n", 0, 0);
  92. return -1;
  93. }
  94. }
  95. }
  96. static char line[256];
  97. static char *lineptr;
  98. static int lineleft;
  99. int
  100. xmon_getchar(void)
  101. {
  102. int c;
  103. if (lineleft == 0) {
  104. lineptr = line;
  105. for (;;) {
  106. c = xmon_readchar();
  107. if (c == -1 || c == 4)
  108. break;
  109. if (c == '\r' || c == '\n') {
  110. *lineptr++ = '\n';
  111. xmon_putchar('\n');
  112. break;
  113. }
  114. switch (c) {
  115. case 0177:
  116. case '\b':
  117. if (lineptr > line) {
  118. xmon_putchar('\b');
  119. xmon_putchar(' ');
  120. xmon_putchar('\b');
  121. --lineptr;
  122. }
  123. break;
  124. case 'U' & 0x1F:
  125. while (lineptr > line) {
  126. xmon_putchar('\b');
  127. xmon_putchar(' ');
  128. xmon_putchar('\b');
  129. --lineptr;
  130. }
  131. break;
  132. default:
  133. if (lineptr >= &line[sizeof(line) - 1])
  134. xmon_putchar('\a');
  135. else {
  136. xmon_putchar(c);
  137. *lineptr++ = c;
  138. }
  139. }
  140. }
  141. lineleft = lineptr - line;
  142. lineptr = line;
  143. }
  144. if (lineleft == 0)
  145. return -1;
  146. --lineleft;
  147. return *lineptr++;
  148. }
  149. char *
  150. xmon_fgets(char *str, int nb, void *f)
  151. {
  152. char *p;
  153. int c;
  154. for (p = str; p < str + nb - 1; ) {
  155. c = xmon_getchar();
  156. if (c == -1) {
  157. if (p == str)
  158. return NULL;
  159. break;
  160. }
  161. *p++ = c;
  162. if (c == '\n')
  163. break;
  164. }
  165. *p = 0;
  166. return str;
  167. }