console.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Machine (and hardware) independent console driver.
  31. *
  32. * We expose a simple interface to the rest of the kernel: "putch" to
  33. * print a character, "getch" to read one.
  34. *
  35. * As long as the device we're connected to does, we allow printing in
  36. * an interrupt handler or with interrupts off (by polling),
  37. * transparently to the caller. Note that getch by polling is not
  38. * supported, although such support could be added without undue
  39. * difficulty.
  40. *
  41. * Note that nothing happens until we have a device to write to. A
  42. * buffer of size DELAYBUFSIZE is used to hold output that is
  43. * generated before this point. This means that (1) using kprintf for
  44. * debugging problems that occur early in initialization is awkward,
  45. * and (2) if the system crashes before we find a console, no output
  46. * at all may appear.
  47. *
  48. * Note that we have no input buffering; characters typed too rapidly
  49. * will be lost.
  50. */
  51. #include <types.h>
  52. #include <kern/errno.h>
  53. #include <lib.h>
  54. #include <uio.h>
  55. #include <thread.h>
  56. #include <current.h>
  57. #include <synch.h>
  58. #include <generic/console.h>
  59. #include <vfs.h>
  60. #include <device.h>
  61. #include "autoconf.h"
  62. /*
  63. * The console device.
  64. */
  65. static struct con_softc *the_console = NULL;
  66. /*
  67. * Lock so user I/Os are atomic.
  68. * We use two locks so readers waiting for input don't lock out writers.
  69. */
  70. static struct lock *con_userlock_read = NULL;
  71. static struct lock *con_userlock_write = NULL;
  72. //////////////////////////////////////////////////
  73. /*
  74. * This is for accumulating characters printed before the
  75. * console is set up. Upon console setup they are dumped
  76. * to the actual console; thenceforth this space is unused.
  77. */
  78. #define DELAYBUFSIZE 1024
  79. static char delayed_outbuf[DELAYBUFSIZE];
  80. static size_t delayed_outbuf_pos=0;
  81. static
  82. void
  83. putch_delayed(int ch)
  84. {
  85. /*
  86. * No synchronization needed: called only during system startup
  87. * by main thread.
  88. */
  89. KASSERT(delayed_outbuf_pos < sizeof(delayed_outbuf));
  90. delayed_outbuf[delayed_outbuf_pos++] = ch;
  91. }
  92. static
  93. void
  94. flush_delay_buf(void)
  95. {
  96. size_t i;
  97. for (i=0; i<delayed_outbuf_pos; i++) {
  98. putch(delayed_outbuf[i]);
  99. }
  100. delayed_outbuf_pos = 0;
  101. }
  102. //////////////////////////////////////////////////
  103. /*
  104. * Print a character, using polling instead of interrupts to wait for
  105. * I/O completion.
  106. */
  107. static
  108. void
  109. putch_polled(struct con_softc *cs, int ch)
  110. {
  111. cs->cs_sendpolled(cs->cs_devdata, ch);
  112. }
  113. static
  114. void
  115. putch_prepare_polled(struct con_softc *cs)
  116. {
  117. if (cs->cs_startpolling != NULL) {
  118. cs->cs_startpolling(cs->cs_devdata);
  119. }
  120. }
  121. static
  122. void
  123. putch_complete_polled(struct con_softc *cs)
  124. {
  125. if (cs->cs_endpolling != NULL) {
  126. cs->cs_endpolling(cs->cs_devdata);
  127. }
  128. }
  129. //////////////////////////////////////////////////
  130. /*
  131. * Print a character, using interrupts to wait for I/O completion.
  132. */
  133. static
  134. void
  135. putch_intr(struct con_softc *cs, int ch)
  136. {
  137. P(cs->cs_wsem);
  138. cs->cs_send(cs->cs_devdata, ch);
  139. }
  140. /*
  141. * Read a character, using interrupts to wait for I/O completion.
  142. */
  143. static
  144. int
  145. getch_intr(struct con_softc *cs)
  146. {
  147. unsigned char ret;
  148. P(cs->cs_rsem);
  149. ret = cs->cs_gotchars[cs->cs_gotchars_tail];
  150. cs->cs_gotchars_tail =
  151. (cs->cs_gotchars_tail + 1) % CONSOLE_INPUT_BUFFER_SIZE;
  152. return ret;
  153. }
  154. /*
  155. * Called from underlying device when a read-ready interrupt occurs.
  156. *
  157. * Note: if gotchars_head == gotchars_tail, the buffer is empty. Thus
  158. * if gotchars_head+1 == gotchars_tail, the buffer is full. A slightly
  159. * tidier way to implement this check (that avoids wasting a slot,
  160. * too) would be with a second semaphore used with a nonblocking P,
  161. * but we don't have that in OS/161.
  162. */
  163. void
  164. con_input(void *vcs, int ch)
  165. {
  166. struct con_softc *cs = vcs;
  167. unsigned nexthead;
  168. nexthead = (cs->cs_gotchars_head + 1) % CONSOLE_INPUT_BUFFER_SIZE;
  169. if (nexthead == cs->cs_gotchars_tail) {
  170. /* overflow; drop character */
  171. return;
  172. }
  173. cs->cs_gotchars[cs->cs_gotchars_head] = ch;
  174. cs->cs_gotchars_head = nexthead;
  175. V(cs->cs_rsem);
  176. }
  177. /*
  178. * Called from underlying device when a write-done interrupt occurs.
  179. */
  180. void
  181. con_start(void *vcs)
  182. {
  183. struct con_softc *cs = vcs;
  184. V(cs->cs_wsem);
  185. }
  186. //////////////////////////////////////////////////
  187. /*
  188. * Exported interface.
  189. *
  190. * Warning: putch must work even in an interrupt handler or with
  191. * interrupts disabled, and before the console is probed. getch need
  192. * not, and does not.
  193. */
  194. void
  195. putch(int ch)
  196. {
  197. struct con_softc *cs = the_console;
  198. if (cs==NULL) {
  199. putch_delayed(ch);
  200. }
  201. else if (curthread->t_in_interrupt || curthread->t_iplhigh_count > 0) {
  202. putch_polled(cs, ch);
  203. }
  204. else {
  205. putch_intr(cs, ch);
  206. }
  207. }
  208. void
  209. putch_prepare(void)
  210. {
  211. struct con_softc *cs = the_console;
  212. if (cs == NULL) {
  213. /* nothing */
  214. }
  215. else if (curthread->t_in_interrupt || curthread->t_iplhigh_count > 0) {
  216. putch_prepare_polled(cs);
  217. }
  218. else {
  219. /* nothing */
  220. }
  221. }
  222. void
  223. putch_complete(void)
  224. {
  225. struct con_softc *cs = the_console;
  226. if (cs == NULL) {
  227. /* nothing */
  228. }
  229. else if (curthread->t_in_interrupt || curthread->t_iplhigh_count > 0) {
  230. putch_complete_polled(cs);
  231. }
  232. else {
  233. /* nothing */
  234. }
  235. }
  236. int
  237. getch(void)
  238. {
  239. struct con_softc *cs = the_console;
  240. KASSERT(cs != NULL);
  241. KASSERT(!curthread->t_in_interrupt && curthread->t_iplhigh_count == 0);
  242. return getch_intr(cs);
  243. }
  244. ////////////////////////////////////////////////////////////
  245. /*
  246. * VFS interface functions
  247. */
  248. static
  249. int
  250. con_open(struct device *dev, int openflags)
  251. {
  252. (void)dev;
  253. (void)openflags;
  254. return 0;
  255. }
  256. static
  257. int
  258. con_close(struct device *dev)
  259. {
  260. (void)dev;
  261. return 0;
  262. }
  263. static
  264. int
  265. con_io(struct device *dev, struct uio *uio)
  266. {
  267. int result;
  268. char ch;
  269. struct lock *lk;
  270. (void)dev; // unused
  271. if (uio->uio_rw==UIO_READ) {
  272. lk = con_userlock_read;
  273. }
  274. else {
  275. lk = con_userlock_write;
  276. }
  277. KASSERT(lk != NULL);
  278. lock_acquire(lk);
  279. while (uio->uio_resid > 0) {
  280. if (uio->uio_rw==UIO_READ) {
  281. ch = getch();
  282. if (ch=='\r') {
  283. ch = '\n';
  284. }
  285. result = uiomove(&ch, 1, uio);
  286. if (result) {
  287. lock_release(lk);
  288. return result;
  289. }
  290. if (ch=='\n') {
  291. break;
  292. }
  293. }
  294. else {
  295. result = uiomove(&ch, 1, uio);
  296. if (result) {
  297. lock_release(lk);
  298. return result;
  299. }
  300. if (ch=='\n') {
  301. putch('\r');
  302. }
  303. putch(ch);
  304. }
  305. }
  306. lock_release(lk);
  307. return 0;
  308. }
  309. static
  310. int
  311. con_ioctl(struct device *dev, int op, userptr_t data)
  312. {
  313. /* No ioctls. */
  314. (void)dev;
  315. (void)op;
  316. (void)data;
  317. return EINVAL;
  318. }
  319. static
  320. int
  321. attach_console_to_vfs(struct con_softc *cs)
  322. {
  323. struct device *dev;
  324. int result;
  325. dev = kmalloc(sizeof(*dev));
  326. if (dev==NULL) {
  327. return ENOMEM;
  328. }
  329. dev->d_open = con_open;
  330. dev->d_close = con_close;
  331. dev->d_io = con_io;
  332. dev->d_ioctl = con_ioctl;
  333. dev->d_blocks = 0;
  334. dev->d_blocksize = 1;
  335. dev->d_data = cs;
  336. result = vfs_adddev("con", dev, 0);
  337. if (result) {
  338. kfree(dev);
  339. return result;
  340. }
  341. return 0;
  342. }
  343. ////////////////////////////////////////////////////////////
  344. /*
  345. * Config routine called by autoconf.c after we are attached to something.
  346. */
  347. int
  348. config_con(struct con_softc *cs, int unit)
  349. {
  350. struct semaphore *rsem, *wsem;
  351. struct lock *rlk, *wlk;
  352. /*
  353. * Only allow one system console.
  354. * Further devices that could be the system console are ignored.
  355. *
  356. * Do not hardwire the console to be "con1" instead of "con0",
  357. * or these asserts will go off.
  358. */
  359. if (unit>0) {
  360. KASSERT(the_console!=NULL);
  361. return ENODEV;
  362. }
  363. KASSERT(the_console==NULL);
  364. rsem = sem_create("console read", 0);
  365. if (rsem == NULL) {
  366. return ENOMEM;
  367. }
  368. wsem = sem_create("console write", 1);
  369. if (wsem == NULL) {
  370. sem_destroy(rsem);
  371. return ENOMEM;
  372. }
  373. rlk = lock_create("console-lock-read");
  374. if (rlk == NULL) {
  375. sem_destroy(rsem);
  376. sem_destroy(wsem);
  377. return ENOMEM;
  378. }
  379. wlk = lock_create("console-lock-write");
  380. if (wlk == NULL) {
  381. lock_destroy(rlk);
  382. sem_destroy(rsem);
  383. sem_destroy(wsem);
  384. return ENOMEM;
  385. }
  386. cs->cs_rsem = rsem;
  387. cs->cs_wsem = wsem;
  388. cs->cs_gotchars_head = 0;
  389. cs->cs_gotchars_tail = 0;
  390. the_console = cs;
  391. con_userlock_read = rlk;
  392. con_userlock_write = wlk;
  393. flush_delay_buf();
  394. return attach_console_to_vfs(cs);
  395. }