start.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. return udbg_getc_poll();
  58. }
  59. FILE *xmon_stdin;
  60. FILE *xmon_stdout;
  61. int
  62. xmon_putc(int c, void *f)
  63. {
  64. char ch = c;
  65. if (c == '\n')
  66. xmon_putc('\r', f);
  67. return xmon_write(f, &ch, 1) == 1? c: -1;
  68. }
  69. int
  70. xmon_putchar(int c)
  71. {
  72. return xmon_putc(c, xmon_stdout);
  73. }
  74. int
  75. xmon_fputs(char *str, void *f)
  76. {
  77. int n = strlen(str);
  78. return xmon_write(f, str, n) == n? 0: -1;
  79. }
  80. int
  81. xmon_readchar(void)
  82. {
  83. char ch;
  84. for (;;) {
  85. switch (xmon_read(xmon_stdin, &ch, 1)) {
  86. case 1:
  87. return ch;
  88. case -1:
  89. xmon_printf("read(stdin) returned -1\r\n", 0, 0);
  90. return -1;
  91. }
  92. }
  93. }
  94. static char line[256];
  95. static char *lineptr;
  96. static int lineleft;
  97. int
  98. xmon_getchar(void)
  99. {
  100. int c;
  101. if (lineleft == 0) {
  102. lineptr = line;
  103. for (;;) {
  104. c = xmon_readchar();
  105. if (c == -1 || c == 4)
  106. break;
  107. if (c == '\r' || c == '\n') {
  108. *lineptr++ = '\n';
  109. xmon_putchar('\n');
  110. break;
  111. }
  112. switch (c) {
  113. case 0177:
  114. case '\b':
  115. if (lineptr > line) {
  116. xmon_putchar('\b');
  117. xmon_putchar(' ');
  118. xmon_putchar('\b');
  119. --lineptr;
  120. }
  121. break;
  122. case 'U' & 0x1F:
  123. while (lineptr > line) {
  124. xmon_putchar('\b');
  125. xmon_putchar(' ');
  126. xmon_putchar('\b');
  127. --lineptr;
  128. }
  129. break;
  130. default:
  131. if (lineptr >= &line[sizeof(line) - 1])
  132. xmon_putchar('\a');
  133. else {
  134. xmon_putchar(c);
  135. *lineptr++ = c;
  136. }
  137. }
  138. }
  139. lineleft = lineptr - line;
  140. lineptr = line;
  141. }
  142. if (lineleft == 0)
  143. return -1;
  144. --lineleft;
  145. return *lineptr++;
  146. }
  147. char *
  148. xmon_fgets(char *str, int nb, void *f)
  149. {
  150. char *p;
  151. int c;
  152. for (p = str; p < str + nb - 1; ) {
  153. c = xmon_getchar();
  154. if (c == -1) {
  155. if (p == str)
  156. return NULL;
  157. break;
  158. }
  159. *p++ = c;
  160. if (c == '\n')
  161. break;
  162. }
  163. *p = 0;
  164. return str;
  165. }