目前了解的中断分为:
本文主要讲解设备中断。
处理器上包含cpu、高速缓存、寄存器、boot rom、中断控制器等。PLIC(Platform-Level Interrupt Control中断控制器),用来管理设备中断,并将中断路由给指定的cpu核进行响应,CLINT是定时器中断。处理中断的具体流程如下:
分三种权限模式:
寄存器:
定时器中断在Machine Mode处理,handler是timervec,不管发生时CPU在执行Supervisor or User code,都必须立刻响应timer interrupt,会破环kernel 临界操作(关中断时进行的操作)。所以,采用的策略是timervec中触发一个software interrupt,在Supervisor Mode下响应定时器中断,yield进程,此时不会破坏临界区。
.globl timervec
.align 4
timervec:
# start.c has set up the memory that mscratch points to:
# scratch[0,8,16] : register save area.
# scratch[24] : address of CLINT's MTIMECMP register.
# scratch[32] : desired interval between interrupts.
csrrw a0, mscratch, a0
sd a1, 0(a0)
sd a2, 8(a0)
sd a3, 16(a0)
# schedule the next timer interrupt
# by adding interval to mtimecmp.
ld a1, 24(a0) # CLINT_MTIMECMP(hart)
ld a2, 32(a0) # interval
ld a3, 0(a1)
add a3, a3, a2
sd a3, 0(a1)
# raise a supervisor software interrupt.
//触发software interrupt来处理timer interrupt,直接处理会打破关中断的限制。
li a1, 2
csrw sip, a1
ld a3, 16(a0)
ld a2, 8(a0)
ld a1, 0(a0)
csrrw a0, mscratch, a0
mret
通用非同步收发传输器(Universal Asynchronous Receiver/Transmitter,通常称为UART)是一种异步收发传输器,是电脑硬体的一部分,比如:主机和键盘通过一根线连接,线的两端各有一个UART芯片,键盘输入字符,先到达UART芯片中控制寄存器,然后UART发起中断,等待CPU响应。
内存映射IO,对设备进行统一编址。
UART是每次读写1B,效率较慢,内存统一编址,UART地址是0x10000000L,涉及THR(发送寄存器)、RHR(接收寄存器)、IER(中断控制寄存器)、LCR(行控制寄存器,console是按行读取的),以及环形队列,用于将CPU和外设解耦。
top:应用进程通过read/write从buffer中读写数据是top部分。
bottom:CPU响应external interrupt,将寄存器数据放入到cons.buf中或者将uart_tx_buf数据发送出去。这个流程是通过interrupt的形式处理的,可以在任意CPU上响应,和当前进程没有关系。
//
// low-level driver routines for 16550a UART.
//
#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "riscv.h"
#include "spinlock.h"
#include "proc.h"
#include "defs.h"
// the UART control registers are memory-mapped
// at address UART0. this macro returns the
// address of one of the registers.
// #define UART0 0x10000000L
//内存映射IO,这是设备寄存器的内存地址
#define Reg(reg) ((volatile unsigned char *)(UART0 + reg))
// the UART control registers.
// some have different meanings for
// read vs write.
// see http://byterunner.com/16550.html
#define RHR 0 // receive holding register (for input bytes)
#define THR 0 // transmit holding register (for output bytes)
#define IER 1 // interrupt enable register
#define IER_RX_ENABLE (1<<0)
#define IER_TX_ENABLE (1<<1)
#define FCR 2 // FIFO control register
#define FCR_FIFO_ENABLE (1<<0)
#define FCR_FIFO_CLEAR (3<<1) // clear the content of the two FIFOs
#define ISR 2 // interrupt status register
#define LCR 3 // line control register
#define LCR_EIGHT_BITS (3<<0)
#define LCR_BAUD_LATCH (1<<7) // special mode to set baud rate
#define LSR 5 // line status register
#define LSR_RX_READY (1<<0) // input is waiting to be read from RHR
#define LSR_TX_IDLE (1<<5) // THR can accept another character to send
#define ReadReg(reg) (*(Reg(reg)))
#define WriteReg(reg, v) (*(Reg(reg)) = (v))
// the transmit output buffer.
struct spinlock uart_tx_lock;
#define UART_TX_BUF_SIZE 32
char uart_tx_buf[UART_TX_BUF_SIZE];
uint64 uart_tx_w; // write next to uart_tx_buf[uart_tx_w % UART_TX_BUF_SIZE]
uint64 uart_tx_r; // read next from uart_tx_buf[uart_tx_r % UART_TX_BUF_SIZE]
extern volatile int panicked; // from printf.c
void uartstart();
void
uartinit(void)
{
// disable interrupts.
//关中断
WriteReg(IER, 0x00);
//设置波特率
// special mode to set baud rate.
WriteReg(LCR, LCR_BAUD_LATCH);
// LSB for baud rate of 38.4K.
WriteReg(0, 0x03);
// MSB for baud rate of 38.4K.
WriteReg(1, 0x00);
//设置字符长度为8bit
WriteReg(LCR, LCR_EIGHT_BITS);
// reset and enable FIFOs.
WriteReg(FCR, FCR_FIFO_ENABLE | FCR_FIFO_CLEAR);
// enable transmit and receive interrupts.
WriteReg(IER, IER_TX_ENABLE | IER_RX_ENABLE);
initlock(&uart_tx_lock, "uart");
}
// add a character to the output buffer and tell the
// UART to start sending if it isn't already.
// blocks if the output buffer is full.
// because it may block, it can't be called
// from interrupts; it's only suitable for use
// by write().
void
uartputc(int c)
{
acquire(&uart_tx_lock);
if(panicked){
for(;;)
;
}
while(1){
if(uart_tx_w == uart_tx_r + UART_TX_BUF_SIZE){
// buffer is full.
// wait for uartstart() to open up space in the buffer.
sleep(&uart_tx_r, &uart_tx_lock);
} else {
uart_tx_buf[uart_tx_w % UART_TX_BUF_SIZE] = c;
uart_tx_w += 1;
//非同步发送,如果发送寄存器满,就会return掉,下次响应中断。
uartstart();
release(&uart_tx_lock);
return;
}
}
}
// alternate version of uartputc() that doesn't
// use interrupts, for use by kernel printf() and
// to echo characters. it spins waiting for the uart's
// output register to be empty.
void
uartputc_sync(int c)
{
push_off();
if(panicked){
for(;;)
;
}
// wait for Transmit Holding Empty to be set in LSR.
//interrupt使得CPU和设备可以并行和异步,如果不用interrupt的话,那么就必须同步发送数据,
while((ReadReg(LSR) & LSR_TX_IDLE) == 0)
;
WriteReg(THR, c);
pop_off();
}
// if the UART is idle, and a character is waiting
// in the transmit buffer, send it.
// caller must hold uart_tx_lock.
// called from both the top- and bottom-half.
void
uartstart()
{
while(1){
if(uart_tx_w == uart_tx_r){
// transmit buffer is empty.
return;
}
if((ReadReg(LSR) & LSR_TX_IDLE) == 0){
// the UART transmit holding register is full,
// so we cannot give it another byte.
//当发送寄存器空闲并准备好接收新字符时,会主动中断,cpu然后响应并在devintr()中会执行。
// it will interrupt when it's ready for a new byte.
return;
}
int c = uart_tx_buf[uart_tx_r % UART_TX_BUF_SIZE];
uart_tx_r += 1;
// maybe uartputc() is waiting for space in the buffer.
wakeup(&uart_tx_r);
WriteReg(THR, c);
}
}
// read one input character from the UART.
// return -1 if none is waiting.
int
uartgetc(void)
{
if(ReadReg(LSR) & 0x01){
// input data is ready.
return ReadReg(RHR);
} else {
return -1;
}
}
// handle a uart interrupt, raised because input has
// arrived, or the uart is ready for more output, or
// both. called from trap.c.
void
uartintr(void)
{
// read and process incoming characters.
while(1){
int c = uartgetc();
if(c == -1)
break;
consoleintr(c);
}
// send buffered characters.
acquire(&uart_tx_lock);
uartstart();
release(&uart_tx_lock);
}
buffer是一个生产者-消费者对列,将两者解耦,使得CPU和设备可以异步,异步是通过interrupt来实现的。
//一旦THR或者RHR处理结束,就会触发一个中断来继续处理数据,此时
//usertrap/kerneltrap-->devintr-->uartintr来发送和接收数据。
int
devintr()
{
uint64 scause = r_scause();
if((scause & 0x8000000000000000L) &&
(scause & 0xff) == 9){
// this is a supervisor external interrupt, via PLIC.
// irq indicates which device interrupted.
int irq = plic_claim();
if(irq == UART0_IRQ){
uartintr();
} else if(irq == VIRTIO0_IRQ){
virtio_disk_intr();
} else if(irq){
printf("unexpected interrupt irq=%d\n", irq);
}
// the PLIC allows each device to raise at most one
// interrupt at a time; tell the PLIC the device is
// now allowed to interrupt again.
if(irq)
plic_complete(irq);
return 1;
} else if(scause == 0x8000000000000001L){
// software interrupt from a machine-mode timer interrupt,
// forwarded by timervec in kernelvec.S.
if(cpuid() == 0){
clockintr();
}
// acknowledge the software interrupt by clearing
// the SSIP bit in sip.
w_sip(r_sip() & ~2);
return 2;
} else {
return 0;
}
}
不使用interrupt就意味着CPU和设备不能并行了,可以使用uartputc_sync来同步发送数据,并且也同步接收数据,效率会比较低。
高效设备会使用DMA硬件来实现批量发送数据,一个字符一次中断代价较大,一批数据一次中断代价较为合理,效率更高。
LSR寄存器有一位可以表示是否有输入字符等待被读,一旦被读,UART硬件就会从内部的FIFO上删除这个字符,并清除LSR。
控制台设备通过UART每次读写数据,每次读写一行,console对用户层表现的是文件描述符,是设备类型的。
consoleinit初始化了uart,每接收一个字节会生成一个receive interrupt,每发送一个字节就生成一个transmit complete interrupt。
当用户通过键盘输入一个字符后,UART芯片会生成一个interrupt,然后cpu调用trap handler响应interrupt,trap handler调用devintr(),它根据scause寄存器得知interrupt来自外部设备,然后询问PLIC哪一个设备中断,如果是UART,那么就调用uartintr。uartintr读取input character并调用consoleintr,不阻塞。consoleintr缓存input character到cons.buf直到一个完整行,然后唤醒consoleread等进程读取。
struct {
struct spinlock lock;
// input
#define INPUT_BUF 128
char buf[INPUT_BUF];
uint r; // Read index
uint w; // Write index
uint e; // Edit index
} cons;
console是一个虚拟设备,连接了键盘和显示器,内核启动时会初始化一个控制台进程init,支持用户进程读写控制台,创建了标准输入流、标准输出流、标准错误流,其实是同一个文件,都可以读写。
void
consoleinit(void)
{
initlock(&cons.lock, "cons");
uartinit();
// connect read and write system calls
// to consoleread and consolewrite.
devsw[CONSOLE].read = consoleread;
devsw[CONSOLE].write = consolewrite;
}
//初始化init进程
void
userinit(void)
{
struct proc *p;
p = allocproc();
initproc = p;
// allocate one user page and copy init's instructions
// and data into it.
//初始化init代码段和页表,第一次返回用户态后就会执行initcode,相当于exec("/init")
uvminit(p->pagetable, initcode, sizeof(initcode));
p->sz = PGSIZE;
// prepare for the very first "return" from kernel to user.
p->trapframe->epc = 0; // user program counter
p->trapframe->sp = PGSIZE; // user stack pointer
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
p->state = RUNNABLE;
release(&p->lock);
}
// Load the user initcode into address 0 of pagetable,
// for the very first process.
// sz must be less than a page.
void
uvminit(pagetable_t pagetable, uchar *src, uint sz)
{
char *mem;
if(sz >= PGSIZE)
panic("inituvm: more than a page");
mem = kalloc();
memset(mem, 0, PGSIZE);
mappages(pagetable, 0, PGSIZE, (uint64)mem, PTE_W|PTE_R|PTE_X|PTE_U);
memmove(mem, src, sz);
}
uchar initcode[] = {
0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02,
0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02,
0x93, 0x08, 0x70, 0x00, 0x73, 0x00, 0x00, 0x00,
0x93, 0x08, 0x20, 0x00, 0x73, 0x00, 0x00, 0x00,
0xef, 0xf0, 0x9f, 0xff, 0x2f, 0x69, 0x6e, 0x69,
0x74, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
init进程即是控制台进程,每个内核只有一个,而终端是sh进程,由init进程fork得到,可以有多终端。
char *argv[] = { "sh", 0 };
int
main(void)
{
int pid, wpid;
if(open("console", O_RDWR) < 0){
//创建控制台设备
mknod("console", CONSOLE, 0);
open("console", O_RDWR);
}
//复制多个
dup(0); // stdout
dup(0); // stderr
for(;;){
printf("init: starting sh\n");
pid = fork();
if(pid < 0){
printf("init: fork failed\n");
exit(1);
}
if(pid == 0){
//子进程是sh进程,也就是终端
exec("sh", argv);
//正常情况替换可执行文件后就切换到新执行流了,不会再回到这个位置
printf("init: exec sh failed\n");
exit(1);
}
for(;;){
// this call to wait() returns if the shell exits,
// or if a parentless process exits.
wpid = wait((int *) 0);
if(wpid == pid){
// the shell exited; restart it.
break;
} else if(wpid < 0){
printf("init: wait returned an error\n");
exit(1);
} else {
// it was a parentless process; do nothing.
}
}
}
}
int
main(void)
{
static char buf[100];
int fd;
// Ensure that three file descriptors are open.
while((fd = open("console", O_RDWR)) >= 0){
if(fd >= 3){
close(fd);
break;
}
}
// Read and run input commands.
while(getcmd(buf, sizeof(buf)) >= 0){
//如果是cd
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
// Chdir must be called by the parent, not the child.
buf[strlen(buf)-1] = 0; // chop \n
if(chdir(buf+3) < 0)
fprintf(2, "cannot cd %s\n", buf+3);
continue;
}
//fork进程执行命令
if(fork1() == 0)
runcmd(parsecmd(buf));
wait(0);
}
exit(0);
}
int
consolewrite(int user_src, uint64 src, int n)
{
int i;
for(i = 0; i < n; i++){
char c;
//读入输出字符
if(either_copyin(&c, user_src, src+i, 1) == -1)
break;
//写入uart_tx_buf,异步发送出去
uartputc(c);
}
return i;
}
//
// user read()s from the console go here.
// copy (up to) a whole input line to dst.
// user_dist indicates whether dst is a user
// or kernel address.
//
int
consoleread(int user_dst, uint64 dst, int n)
{
uint target;
int c;
char cbuf;
target = n;
acquire(&cons.lock);
while(n > 0){
// wait until interrupt handler has put some
// input into cons.buffer.
//buf为空就阻塞
while(cons.r == cons.w){
if(myproc()->killed){
release(&cons.lock);
return -1;
}
//键盘每输入一个字符就会唤醒一次
sleep(&cons.r, &cons.lock);
}
//读取一个字符
c = cons.buf[cons.r++ % INPUT_BUF];
// ctrl + D,终止读取,从阻塞中唤醒
if(c == C('D')){ // end-of-file
if(n < target){
// Save ^D for next time, to make sure
// caller gets a 0-byte result.
cons.r--;
}
break;
}
// copy the input byte to the user-space buffer.
cbuf = c;
if(either_copyout(user_dst, dst, &cbuf, 1) == -1)
break;
dst++;
--n;
//读取整行就退出
if(c == '\n'){
// a whole line has arrived, return to
// the user-level read().
break;
}
}
release(&cons.lock);
//读取的字符数
return target - n;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。