《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
12 KiB

10 years ago
  1. #include <defs.h>
  2. #include <x86.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <kbdreg.h>
  6. #include <picirq.h>
  7. #include <trap.h>
  8. #include <memlayout.h>
  9. #include <sync.h>
  10. /* stupid I/O delay routine necessitated by historical PC design flaws */
  11. static void
  12. delay(void) {
  13. inb(0x84);
  14. inb(0x84);
  15. inb(0x84);
  16. inb(0x84);
  17. }
  18. /***** Serial I/O code *****/
  19. #define COM1 0x3F8
  20. #define COM_RX 0 // In: Receive buffer (DLAB=0)
  21. #define COM_TX 0 // Out: Transmit buffer (DLAB=0)
  22. #define COM_DLL 0 // Out: Divisor Latch Low (DLAB=1)
  23. #define COM_DLM 1 // Out: Divisor Latch High (DLAB=1)
  24. #define COM_IER 1 // Out: Interrupt Enable Register
  25. #define COM_IER_RDI 0x01 // Enable receiver data interrupt
  26. #define COM_IIR 2 // In: Interrupt ID Register
  27. #define COM_FCR 2 // Out: FIFO Control Register
  28. #define COM_LCR 3 // Out: Line Control Register
  29. #define COM_LCR_DLAB 0x80 // Divisor latch access bit
  30. #define COM_LCR_WLEN8 0x03 // Wordlength: 8 bits
  31. #define COM_MCR 4 // Out: Modem Control Register
  32. #define COM_MCR_RTS 0x02 // RTS complement
  33. #define COM_MCR_DTR 0x01 // DTR complement
  34. #define COM_MCR_OUT2 0x08 // Out2 complement
  35. #define COM_LSR 5 // In: Line Status Register
  36. #define COM_LSR_DATA 0x01 // Data available
  37. #define COM_LSR_TXRDY 0x20 // Transmit buffer avail
  38. #define COM_LSR_TSRE 0x40 // Transmitter off
  39. #define MONO_BASE 0x3B4
  40. #define MONO_BUF 0xB0000
  41. #define CGA_BASE 0x3D4
  42. #define CGA_BUF 0xB8000
  43. #define CRT_ROWS 25
  44. #define CRT_COLS 80
  45. #define CRT_SIZE (CRT_ROWS * CRT_COLS)
  46. #define LPTPORT 0x378
  47. static uint16_t *crt_buf;
  48. static uint16_t crt_pos;
  49. static uint16_t addr_6845;
  50. /* TEXT-mode CGA/VGA display output */
  51. static void
  52. cga_init(void) {
  53. volatile uint16_t *cp = (uint16_t *)(CGA_BUF + KERNBASE);
  54. uint16_t was = *cp;
  55. *cp = (uint16_t) 0xA55A;
  56. if (*cp != 0xA55A) {
  57. cp = (uint16_t*)(MONO_BUF + KERNBASE);
  58. addr_6845 = MONO_BASE;
  59. } else {
  60. *cp = was;
  61. addr_6845 = CGA_BASE;
  62. }
  63. // Extract cursor location
  64. uint32_t pos;
  65. outb(addr_6845, 14);
  66. pos = inb(addr_6845 + 1) << 8;
  67. outb(addr_6845, 15);
  68. pos |= inb(addr_6845 + 1);
  69. crt_buf = (uint16_t*) cp;
  70. crt_pos = pos;
  71. }
  72. static bool serial_exists = 0;
  73. static void
  74. serial_init(void) {
  75. // Turn off the FIFO
  76. outb(COM1 + COM_FCR, 0);
  77. // Set speed; requires DLAB latch
  78. outb(COM1 + COM_LCR, COM_LCR_DLAB);
  79. outb(COM1 + COM_DLL, (uint8_t) (115200 / 9600));
  80. outb(COM1 + COM_DLM, 0);
  81. // 8 data bits, 1 stop bit, parity off; turn off DLAB latch
  82. outb(COM1 + COM_LCR, COM_LCR_WLEN8 & ~COM_LCR_DLAB);
  83. // No modem controls
  84. outb(COM1 + COM_MCR, 0);
  85. // Enable rcv interrupts
  86. outb(COM1 + COM_IER, COM_IER_RDI);
  87. // Clear any preexisting overrun indications and interrupts
  88. // Serial port doesn't exist if COM_LSR returns 0xFF
  89. serial_exists = (inb(COM1 + COM_LSR) != 0xFF);
  90. (void) inb(COM1+COM_IIR);
  91. (void) inb(COM1+COM_RX);
  92. if (serial_exists) {
  93. pic_enable(IRQ_COM1);
  94. }
  95. }
  96. static void
  97. lpt_putc_sub(int c) {
  98. int i;
  99. for (i = 0; !(inb(LPTPORT + 1) & 0x80) && i < 12800; i ++) {
  100. delay();
  101. }
  102. outb(LPTPORT + 0, c);
  103. outb(LPTPORT + 2, 0x08 | 0x04 | 0x01);
  104. outb(LPTPORT + 2, 0x08);
  105. }
  106. /* lpt_putc - copy console output to parallel port */
  107. static void
  108. lpt_putc(int c) {
  109. if (c != '\b') {
  110. lpt_putc_sub(c);
  111. }
  112. else {
  113. lpt_putc_sub('\b');
  114. lpt_putc_sub(' ');
  115. lpt_putc_sub('\b');
  116. }
  117. }
  118. /* cga_putc - print character to console */
  119. static void
  120. cga_putc(int c) {
  121. // set black on white
  122. if (!(c & ~0xFF)) {
  123. c |= 0x0700;
  124. }
  125. switch (c & 0xff) {
  126. case '\b':
  127. if (crt_pos > 0) {
  128. crt_pos --;
  129. crt_buf[crt_pos] = (c & ~0xff) | ' ';
  130. }
  131. break;
  132. case '\n':
  133. crt_pos += CRT_COLS;
  134. case '\r':
  135. crt_pos -= (crt_pos % CRT_COLS);
  136. break;
  137. default:
  138. crt_buf[crt_pos ++] = c; // write the character
  139. break;
  140. }
  141. // What is the purpose of this?
  142. if (crt_pos >= CRT_SIZE) {
  143. int i;
  144. memmove(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) * sizeof(uint16_t));
  145. for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i ++) {
  146. crt_buf[i] = 0x0700 | ' ';
  147. }
  148. crt_pos -= CRT_COLS;
  149. }
  150. // move that little blinky thing
  151. outb(addr_6845, 14);
  152. outb(addr_6845 + 1, crt_pos >> 8);
  153. outb(addr_6845, 15);
  154. outb(addr_6845 + 1, crt_pos);
  155. }
  156. static void
  157. serial_putc_sub(int c) {
  158. int i;
  159. for (i = 0; !(inb(COM1 + COM_LSR) & COM_LSR_TXRDY) && i < 12800; i ++) {
  160. delay();
  161. }
  162. outb(COM1 + COM_TX, c);
  163. }
  164. /* serial_putc - print character to serial port */
  165. static void
  166. serial_putc(int c) {
  167. if (c != '\b') {
  168. serial_putc_sub(c);
  169. }
  170. else {
  171. serial_putc_sub('\b');
  172. serial_putc_sub(' ');
  173. serial_putc_sub('\b');
  174. }
  175. }
  176. /* *
  177. * Here we manage the console input buffer, where we stash characters
  178. * received from the keyboard or serial port whenever the corresponding
  179. * interrupt occurs.
  180. * */
  181. #define CONSBUFSIZE 512
  182. static struct {
  183. uint8_t buf[CONSBUFSIZE];
  184. uint32_t rpos;
  185. uint32_t wpos;
  186. } cons;
  187. /* *
  188. * cons_intr - called by device interrupt routines to feed input
  189. * characters into the circular console input buffer.
  190. * */
  191. static void
  192. cons_intr(int (*proc)(void)) {
  193. int c;
  194. while ((c = (*proc)()) != -1) {
  195. if (c != 0) {
  196. cons.buf[cons.wpos ++] = c;
  197. if (cons.wpos == CONSBUFSIZE) {
  198. cons.wpos = 0;
  199. }
  200. }
  201. }
  202. }
  203. /* serial_proc_data - get data from serial port */
  204. static int
  205. serial_proc_data(void) {
  206. if (!(inb(COM1 + COM_LSR) & COM_LSR_DATA)) {
  207. return -1;
  208. }
  209. int c = inb(COM1 + COM_RX);
  210. if (c == 127) {
  211. c = '\b';
  212. }
  213. return c;
  214. }
  215. /* serial_intr - try to feed input characters from serial port */
  216. void
  217. serial_intr(void) {
  218. if (serial_exists) {
  219. cons_intr(serial_proc_data);
  220. }
  221. }
  222. /***** Keyboard input code *****/
  223. #define NO 0
  224. #define SHIFT (1<<0)
  225. #define CTL (1<<1)
  226. #define ALT (1<<2)
  227. #define CAPSLOCK (1<<3)
  228. #define NUMLOCK (1<<4)
  229. #define SCROLLLOCK (1<<5)
  230. #define E0ESC (1<<6)
  231. static uint8_t shiftcode[256] = {
  232. [0x1D] CTL,
  233. [0x2A] SHIFT,
  234. [0x36] SHIFT,
  235. [0x38] ALT,
  236. [0x9D] CTL,
  237. [0xB8] ALT
  238. };
  239. static uint8_t togglecode[256] = {
  240. [0x3A] CAPSLOCK,
  241. [0x45] NUMLOCK,
  242. [0x46] SCROLLLOCK
  243. };
  244. static uint8_t normalmap[256] = {
  245. NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
  246. '7', '8', '9', '0', '-', '=', '\b', '\t',
  247. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10
  248. 'o', 'p', '[', ']', '\n', NO, 'a', 's',
  249. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20
  250. '\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
  251. 'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30
  252. NO, ' ', NO, NO, NO, NO, NO, NO,
  253. NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
  254. '8', '9', '-', '4', '5', '6', '+', '1',
  255. '2', '3', '0', '.', NO, NO, NO, NO, // 0x50
  256. [0xC7] KEY_HOME, [0x9C] '\n' /*KP_Enter*/,
  257. [0xB5] '/' /*KP_Div*/, [0xC8] KEY_UP,
  258. [0xC9] KEY_PGUP, [0xCB] KEY_LF,
  259. [0xCD] KEY_RT, [0xCF] KEY_END,
  260. [0xD0] KEY_DN, [0xD1] KEY_PGDN,
  261. [0xD2] KEY_INS, [0xD3] KEY_DEL
  262. };
  263. static uint8_t shiftmap[256] = {
  264. NO, 033, '!', '@', '#', '$', '%', '^', // 0x00
  265. '&', '*', '(', ')', '_', '+', '\b', '\t',
  266. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10
  267. 'O', 'P', '{', '}', '\n', NO, 'A', 'S',
  268. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20
  269. '"', '~', NO, '|', 'Z', 'X', 'C', 'V',
  270. 'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30
  271. NO, ' ', NO, NO, NO, NO, NO, NO,
  272. NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
  273. '8', '9', '-', '4', '5', '6', '+', '1',
  274. '2', '3', '0', '.', NO, NO, NO, NO, // 0x50
  275. [0xC7] KEY_HOME, [0x9C] '\n' /*KP_Enter*/,
  276. [0xB5] '/' /*KP_Div*/, [0xC8] KEY_UP,
  277. [0xC9] KEY_PGUP, [0xCB] KEY_LF,
  278. [0xCD] KEY_RT, [0xCF] KEY_END,
  279. [0xD0] KEY_DN, [0xD1] KEY_PGDN,
  280. [0xD2] KEY_INS, [0xD3] KEY_DEL
  281. };
  282. #define C(x) (x - '@')
  283. static uint8_t ctlmap[256] = {
  284. NO, NO, NO, NO, NO, NO, NO, NO,
  285. NO, NO, NO, NO, NO, NO, NO, NO,
  286. C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
  287. C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
  288. C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
  289. NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
  290. C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
  291. [0x97] KEY_HOME,
  292. [0xB5] C('/'), [0xC8] KEY_UP,
  293. [0xC9] KEY_PGUP, [0xCB] KEY_LF,
  294. [0xCD] KEY_RT, [0xCF] KEY_END,
  295. [0xD0] KEY_DN, [0xD1] KEY_PGDN,
  296. [0xD2] KEY_INS, [0xD3] KEY_DEL
  297. };
  298. static uint8_t *charcode[4] = {
  299. normalmap,
  300. shiftmap,
  301. ctlmap,
  302. ctlmap
  303. };
  304. /* *
  305. * kbd_proc_data - get data from keyboard
  306. *
  307. * The kbd_proc_data() function gets data from the keyboard.
  308. * If we finish a character, return it, else 0. And return -1 if no data.
  309. * */
  310. static int
  311. kbd_proc_data(void) {
  312. int c;
  313. uint8_t data;
  314. static uint32_t shift;
  315. if ((inb(KBSTATP) & KBS_DIB) == 0) {
  316. return -1;
  317. }
  318. data = inb(KBDATAP);
  319. if (data == 0xE0) {
  320. // E0 escape character
  321. shift |= E0ESC;
  322. return 0;
  323. } else if (data & 0x80) {
  324. // Key released
  325. data = (shift & E0ESC ? data : data & 0x7F);
  326. shift &= ~(shiftcode[data] | E0ESC);
  327. return 0;
  328. } else if (shift & E0ESC) {
  329. // Last character was an E0 escape; or with 0x80
  330. data |= 0x80;
  331. shift &= ~E0ESC;
  332. }
  333. shift |= shiftcode[data];
  334. shift ^= togglecode[data];
  335. c = charcode[shift & (CTL | SHIFT)][data];
  336. if (shift & CAPSLOCK) {
  337. if ('a' <= c && c <= 'z')
  338. c += 'A' - 'a';
  339. else if ('A' <= c && c <= 'Z')
  340. c += 'a' - 'A';
  341. }
  342. // Process special keys
  343. // Ctrl-Alt-Del: reboot
  344. if (!(~shift & (CTL | ALT)) && c == KEY_DEL) {
  345. cprintf("Rebooting!\n");
  346. outb(0x92, 0x3); // courtesy of Chris Frost
  347. }
  348. return c;
  349. }
  350. /* kbd_intr - try to feed input characters from keyboard */
  351. static void
  352. kbd_intr(void) {
  353. cons_intr(kbd_proc_data);
  354. }
  355. static void
  356. kbd_init(void) {
  357. // drain the kbd buffer
  358. kbd_intr();
  359. pic_enable(IRQ_KBD);
  360. }
  361. /* cons_init - initializes the console devices */
  362. void
  363. cons_init(void) {
  364. cga_init();
  365. serial_init();
  366. kbd_init();
  367. if (!serial_exists) {
  368. cprintf("serial port does not exist!!\n");
  369. }
  370. }
  371. /* cons_putc - print a single character @c to console devices */
  372. void
  373. cons_putc(int c) {
  374. bool intr_flag;
  375. local_intr_save(intr_flag);
  376. {
  377. lpt_putc(c);
  378. cga_putc(c);
  379. serial_putc(c);
  380. }
  381. local_intr_restore(intr_flag);
  382. }
  383. /* *
  384. * cons_getc - return the next input character from console,
  385. * or 0 if none waiting.
  386. * */
  387. int
  388. cons_getc(void) {
  389. int c = 0;
  390. bool intr_flag;
  391. local_intr_save(intr_flag);
  392. {
  393. // poll for any pending input characters,
  394. // so that this function works even when interrupts are disabled
  395. // (e.g., when called from the kernel monitor).
  396. serial_intr();
  397. kbd_intr();
  398. // grab the next character from the input buffer.
  399. if (cons.rpos != cons.wpos) {
  400. c = cons.buf[cons.rpos ++];
  401. if (cons.rpos == CONSBUFSIZE) {
  402. cons.rpos = 0;
  403. }
  404. }
  405. }
  406. local_intr_restore(intr_flag);
  407. return c;
  408. }