【问题标题】:Why the keyboard input is written back to IO bus in xv6?为什么键盘输入在 xv6 中被写回 IO 总线?
【发布时间】:2020-08-19 06:10:46
【问题描述】:

我不明白为什么从键盘中断得到的输入又被写回io总线:

这是键盘中断处理程序:

void consoleintr(int (*getc)(void)) {
    // I skipped some code for simplicity

    default:
      if(c != 0 && input.e-input.r < INPUT_BUF){
        c = (c == '\r') ? '\n' : c;

        input.buf[input.e++ % INPUT_BUF] = c;
----->  consputc(c);

        if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
          input.w = input.e;
          wakeup(&input.r);
        }
      }

      break;
    }
  }

输入c 取自getc 函数并写入input.buf 环形缓冲区,然后立即传递给consputc 函数。

consputc 函数调用uartputc,后者调用outb(COM1+0, c)

uartgetc 函数也注册为consoleintr 函数的输入:

static int uartgetc(void) {
    if (!uart) {
        return -1;
    }

    if (!(inb(COM1 + 5) & 0x01)) {
        return -1;
    }

    return inb(COM1+0);
}

void uartintr(void) {
    consoleintr(uartgetc);
}

代码链接:

  1. 控制台:https://github.com/mit-pdos/xv6-public/blob/master/console.c#L192

  2. consputc:https://github.com/mit-pdos/xv6-public/blob/master/console.c#L166

  3. uartputc:https://github.com/mit-pdos/xv6-public/blob/master/uart.c#L52

【问题讨论】:

    标签: unix operating-system xv6


    【解决方案1】:

    从键盘按下的键不会重新发送以再次处理,而是发送到输出:

    uart是某种串行线,它有两种使用方式:输入和输出。

    只有输入设备中的键盘。

    当你按下一个键时,按下的键将被发送到串口输出。

    一个小示意图可能会更好地解释:

    +-------------+                                   
    |             |                                   +--------------+
    | Keyboard    +-----+                       +---->+ Screen (term)|
    |             |     |                       |     +--------------+
    +-------------+     |    +------------+     |     
                        |    |            |     |      +----------------+
                        +--->| Char to    +-----+----->+ Input (process)|----> ....
                        |    |   process  |     |      +----------------+   
    +--------------+    |    +------------+     |     +--------------+
    |              |    |                       +---->+ Serial: out  |
    | Serial: in   +----+                             |  outb(...)   |
    |  inb(...)    |                                  +--------------+
    +--------------+
    
    

    【讨论】:

    • Mathieu,为什么需要将按键从键盘发送到串行输出?把它保存在缓冲区中然后发送到屏幕上还不够吗?
    • 我的假设是否正确,即从键盘传递的字符以后可以被其他软件使用,而 consoleintr 只是其中之一?
    • @maksadbek 串行线路实现用于远程访问。它对于调试目的非常有用。
    猜你喜欢
    • 2015-04-12
    • 1970-01-01
    • 2015-07-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多