aos lab 8: interrupts and device drivers
DESCRIPTION
TRANSCRIPT
![Page 1: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/1.jpg)
Lab 8: Interrupts and Device DriversAdvanced Operating Systems
Zubair Nabi
March 28, 2013
![Page 2: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/2.jpg)
Recap of Lab 5: Extraordinary events
Events that break normal processor flow, to return control to thekernel:
1 A device signals that it needs attention (e.g. Timer): Interrupt
2 A user program does something illegal (e.g. divide by zero):Exception
3 A user program asks the kernel for a service: System call
![Page 3: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/3.jpg)
Recap of Lab 5: Extraordinary events
Events that break normal processor flow, to return control to thekernel:
1 A device signals that it needs attention (e.g. Timer): Interrupt
2 A user program does something illegal (e.g. divide by zero):Exception
3 A user program asks the kernel for a service: System call
![Page 4: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/4.jpg)
Recap of Lab 5: Extraordinary events
Events that break normal processor flow, to return control to thekernel:
1 A device signals that it needs attention (e.g. Timer): Interrupt
2 A user program does something illegal (e.g. divide by zero):Exception
3 A user program asks the kernel for a service: System call
![Page 5: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/5.jpg)
Recap of Lab 5: Extraordinary events
Events that break normal processor flow, to return control to thekernel:
1 A device signals that it needs attention (e.g. Timer): Interrupt
2 A user program does something illegal (e.g. divide by zero):Exception
3 A user program asks the kernel for a service: System call
![Page 6: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/6.jpg)
Recap of Lab 5: Handling extraordinary events
The operating system must:
1 Save the processor’s registers for future resumption
2 Set up system for execution in the kernel
3 Choose a place for the kernel to start execution
4 Retrieve information about the event and call correspondinginterrupt handler
5 All the while, maintain isolation between user processes and thekernel
![Page 7: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/7.jpg)
Recap of Lab 5: Handling extraordinary events
The operating system must:
1 Save the processor’s registers for future resumption
2 Set up system for execution in the kernel
3 Choose a place for the kernel to start execution
4 Retrieve information about the event and call correspondinginterrupt handler
5 All the while, maintain isolation between user processes and thekernel
![Page 8: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/8.jpg)
Recap of Lab 5: Handling extraordinary events
The operating system must:
1 Save the processor’s registers for future resumption
2 Set up system for execution in the kernel
3 Choose a place for the kernel to start execution
4 Retrieve information about the event and call correspondinginterrupt handler
5 All the while, maintain isolation between user processes and thekernel
![Page 9: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/9.jpg)
Recap of Lab 5: Handling extraordinary events
The operating system must:
1 Save the processor’s registers for future resumption
2 Set up system for execution in the kernel
3 Choose a place for the kernel to start execution
4 Retrieve information about the event and call correspondinginterrupt handler
5 All the while, maintain isolation between user processes and thekernel
![Page 10: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/10.jpg)
Recap of Lab 5: Handling extraordinary events
The operating system must:
1 Save the processor’s registers for future resumption
2 Set up system for execution in the kernel
3 Choose a place for the kernel to start execution
4 Retrieve information about the event and call correspondinginterrupt handler
5 All the while, maintain isolation between user processes and thekernel
![Page 11: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/11.jpg)
Recap of Lab 5: Handling extraordinary events
The operating system must:
1 Save the processor’s registers for future resumption
2 Set up system for execution in the kernel
3 Choose a place for the kernel to start execution
4 Retrieve information about the event and call correspondinginterrupt handler
5 All the while, maintain isolation between user processes and thekernel
![Page 12: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/12.jpg)
Recap of Lab 5: Handling extraordinary events (2)
• Need hardware support
• On the x86, system calls generate an interrupt via the intinstruction
• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions
• Traps are caused by the current running process• Interrupts are caused by devices
• Can happen concurrently
![Page 13: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/13.jpg)
Recap of Lab 5: Handling extraordinary events (2)
• Need hardware support
• On the x86, system calls generate an interrupt via the intinstruction
• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions
• Traps are caused by the current running process• Interrupts are caused by devices
• Can happen concurrently
![Page 14: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/14.jpg)
Recap of Lab 5: Handling extraordinary events (2)
• Need hardware support
• On the x86, system calls generate an interrupt via the intinstruction
• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions
• Traps are caused by the current running process• Interrupts are caused by devices
• Can happen concurrently
![Page 15: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/15.jpg)
Recap of Lab 5: Handling extraordinary events (2)
• Need hardware support
• On the x86, system calls generate an interrupt via the intinstruction
• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions
• Traps are caused by the current running process• Interrupts are caused by devices
• Can happen concurrently
![Page 16: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/16.jpg)
Recap of Lab 5: Handling extraordinary events (2)
• Need hardware support
• On the x86, system calls generate an interrupt via the intinstruction
• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions
• Traps are caused by the current running process• Interrupts are caused by devices
• Can happen concurrently
![Page 17: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/17.jpg)
Recap of Lab 5: Handling extraordinary events (2)
• Need hardware support
• On the x86, system calls generate an interrupt via the intinstruction
• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions
• Traps are caused by the current running process• Interrupts are caused by devices
• Can happen concurrently
![Page 18: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/18.jpg)
Recap of Lab 5: trap
• Gets passed struct trapframe *tf
• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception
• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process
![Page 19: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/19.jpg)
Recap of Lab 5: trap
• Gets passed struct trapframe *tf
• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception
• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process
![Page 20: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/20.jpg)
Recap of Lab 5: trap
• Gets passed struct trapframe *tf
• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception
• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process
![Page 21: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/21.jpg)
Recap of Lab 5: trap
• Gets passed struct trapframe *tf
• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception
• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process
![Page 22: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/22.jpg)
Recap of Lab 5: trap
• Gets passed struct trapframe *tf
• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception
• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process
![Page 23: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/23.jpg)
Interrupts
• Examples of interrupts: Pressing a key on the keyboard, storingdata on disk etc.
• Hardware on the motherboard signals the CPU whenever adevice needs attention
• Devices need to be programmed to generate interrupts
• Interrupts must be assigned to a CPU for handling
![Page 24: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/24.jpg)
Interrupts
• Examples of interrupts: Pressing a key on the keyboard, storingdata on disk etc.
• Hardware on the motherboard signals the CPU whenever adevice needs attention
• Devices need to be programmed to generate interrupts
• Interrupts must be assigned to a CPU for handling
![Page 25: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/25.jpg)
Interrupts
• Examples of interrupts: Pressing a key on the keyboard, storingdata on disk etc.
• Hardware on the motherboard signals the CPU whenever adevice needs attention
• Devices need to be programmed to generate interrupts
• Interrupts must be assigned to a CPU for handling
![Page 26: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/26.jpg)
Interrupts
• Examples of interrupts: Pressing a key on the keyboard, storingdata on disk etc.
• Hardware on the motherboard signals the CPU whenever adevice needs attention
• Devices need to be programmed to generate interrupts
• Interrupts must be assigned to a CPU for handling
![Page 27: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/27.jpg)
Handing interrupts on multi-processors
Two parts of handling interrupts on multi-processors
1 A method to route interrupts to processors (I/O APIC – AdvancedProgrammable Interrupt Controller, ioapic.c)
2 A per CPU interrupt controller to handle interrupts (Local APIC,lapic.c)
The OS needs to program both the IOAPIC and the LAPIC
![Page 28: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/28.jpg)
Handing interrupts on multi-processors
Two parts of handling interrupts on multi-processors
1 A method to route interrupts to processors (I/O APIC – AdvancedProgrammable Interrupt Controller, ioapic.c)
2 A per CPU interrupt controller to handle interrupts (Local APIC,lapic.c)
The OS needs to program both the IOAPIC and the LAPIC
![Page 29: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/29.jpg)
Handing interrupts on multi-processors
Two parts of handling interrupts on multi-processors
1 A method to route interrupts to processors (I/O APIC – AdvancedProgrammable Interrupt Controller, ioapic.c)
2 A per CPU interrupt controller to handle interrupts (Local APIC,lapic.c)
The OS needs to program both the IOAPIC and the LAPIC
![Page 30: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/30.jpg)
IOAPIC
• Interrupts are generated by the APIC labelled from IRQ0 toIRQ16 or IRQ24
• A device enables particular interrupts within the IOAPIC table andspecifies which processor they should be routed to
• For instance, the keyboard interrupt is routed to CPU0 while diskinterrupts are routed to CPU(n−1), where n is the number ofCPUs
• Every CPU decides whether it wants to receive interrupts. Forinstance, scheduler() disables all interrupts
![Page 31: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/31.jpg)
IOAPIC
• Interrupts are generated by the APIC labelled from IRQ0 toIRQ16 or IRQ24
• A device enables particular interrupts within the IOAPIC table andspecifies which processor they should be routed to
• For instance, the keyboard interrupt is routed to CPU0 while diskinterrupts are routed to CPU(n−1), where n is the number ofCPUs
• Every CPU decides whether it wants to receive interrupts. Forinstance, scheduler() disables all interrupts
![Page 32: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/32.jpg)
IOAPIC
• Interrupts are generated by the APIC labelled from IRQ0 toIRQ16 or IRQ24
• A device enables particular interrupts within the IOAPIC table andspecifies which processor they should be routed to
• For instance, the keyboard interrupt is routed to CPU0 while diskinterrupts are routed to CPU(n−1), where n is the number ofCPUs
• Every CPU decides whether it wants to receive interrupts. Forinstance, scheduler() disables all interrupts
![Page 33: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/33.jpg)
IOAPIC
• Interrupts are generated by the APIC labelled from IRQ0 toIRQ16 or IRQ24
• A device enables particular interrupts within the IOAPIC table andspecifies which processor they should be routed to
• For instance, the keyboard interrupt is routed to CPU0 while diskinterrupts are routed to CPU(n−1), where n is the number ofCPUs
• Every CPU decides whether it wants to receive interrupts. Forinstance, scheduler() disables all interrupts
![Page 34: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/34.jpg)
LAPIC
• Each CPU has its own LAPIC
• xv6 programs the LAPIC using lapicinit()
![Page 35: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/35.jpg)
LAPIC
• Each CPU has its own LAPIC
• xv6 programs the LAPIC using lapicinit()
![Page 36: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/36.jpg)
Example: Timer interrupt
• The kernel uses timer interrupts to get a notion of time and tomake scheduling decisions
• A value of 100 timer ticks is used by default which is high enoughto allow interactivity and low enough not to lead to a live lock
• This value is set in lapicinit() and IRQ_TIMER is mappedto IRQ0
• IRQ_TIMER corresponds to interrupt number 32
• In trap(), for interrupt number 32, the timer tick is incrementedand processes sleeping on it are woken up
![Page 37: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/37.jpg)
Example: Timer interrupt
• The kernel uses timer interrupts to get a notion of time and tomake scheduling decisions
• A value of 100 timer ticks is used by default which is high enoughto allow interactivity and low enough not to lead to a live lock
• This value is set in lapicinit() and IRQ_TIMER is mappedto IRQ0
• IRQ_TIMER corresponds to interrupt number 32
• In trap(), for interrupt number 32, the timer tick is incrementedand processes sleeping on it are woken up
![Page 38: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/38.jpg)
Example: Timer interrupt
• The kernel uses timer interrupts to get a notion of time and tomake scheduling decisions
• A value of 100 timer ticks is used by default which is high enoughto allow interactivity and low enough not to lead to a live lock
• This value is set in lapicinit() and IRQ_TIMER is mappedto IRQ0
• IRQ_TIMER corresponds to interrupt number 32
• In trap(), for interrupt number 32, the timer tick is incrementedand processes sleeping on it are woken up
![Page 39: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/39.jpg)
Example: Timer interrupt
• The kernel uses timer interrupts to get a notion of time and tomake scheduling decisions
• A value of 100 timer ticks is used by default which is high enoughto allow interactivity and low enough not to lead to a live lock
• This value is set in lapicinit() and IRQ_TIMER is mappedto IRQ0
• IRQ_TIMER corresponds to interrupt number 32
• In trap(), for interrupt number 32, the timer tick is incrementedand processes sleeping on it are woken up
![Page 40: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/40.jpg)
Example: Timer interrupt
• The kernel uses timer interrupts to get a notion of time and tomake scheduling decisions
• A value of 100 timer ticks is used by default which is high enoughto allow interactivity and low enough not to lead to a live lock
• This value is set in lapicinit() and IRQ_TIMER is mappedto IRQ0
• IRQ_TIMER corresponds to interrupt number 32
• In trap(), for interrupt number 32, the timer tick is incrementedand processes sleeping on it are woken up
![Page 41: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/41.jpg)
Device Driver
• A piece of code in the OS that manages a particular device• Provides interrupt handlers for a device• Causes a device to perform operations• Causes a device to generate interrupts
• Non-trivial to write as a driver executes concurrently with thedevice that it manages
• Driver also needs to understand the hardware interface of thedevice
![Page 42: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/42.jpg)
Device Driver
• A piece of code in the OS that manages a particular device• Provides interrupt handlers for a device• Causes a device to perform operations• Causes a device to generate interrupts
• Non-trivial to write as a driver executes concurrently with thedevice that it manages
• Driver also needs to understand the hardware interface of thedevice
![Page 43: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/43.jpg)
Device Driver
• A piece of code in the OS that manages a particular device• Provides interrupt handlers for a device• Causes a device to perform operations• Causes a device to generate interrupts
• Non-trivial to write as a driver executes concurrently with thedevice that it manages
• Driver also needs to understand the hardware interface of thedevice
![Page 44: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/44.jpg)
Device Driver
• A piece of code in the OS that manages a particular device• Provides interrupt handlers for a device• Causes a device to perform operations• Causes a device to generate interrupts
• Non-trivial to write as a driver executes concurrently with thedevice that it manages
• Driver also needs to understand the hardware interface of thedevice
![Page 45: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/45.jpg)
Device Driver
• A piece of code in the OS that manages a particular device• Provides interrupt handlers for a device• Causes a device to perform operations• Causes a device to generate interrupts
• Non-trivial to write as a driver executes concurrently with thedevice that it manages
• Driver also needs to understand the hardware interface of thedevice
![Page 46: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/46.jpg)
Device Driver
• A piece of code in the OS that manages a particular device• Provides interrupt handlers for a device• Causes a device to perform operations• Causes a device to generate interrupts
• Non-trivial to write as a driver executes concurrently with thedevice that it manages
• Driver also needs to understand the hardware interface of thedevice
![Page 47: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/47.jpg)
Example: Disk driver
• In charge of copying data back and forth from the disk• Data is represented on the disk as a sequence of sectors, each
containing 512-byte blocks• Sector 0 represents the first 512 bytes, sector 1 the next 512, and
so on
• The OS has a corresponding structure (struct buf) thatmaps to one sector
• The data within the OS sector structure is often out of sync withthe disk
• xv6 implements an IDE driver
![Page 48: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/48.jpg)
Example: Disk driver
• In charge of copying data back and forth from the disk• Data is represented on the disk as a sequence of sectors, each
containing 512-byte blocks• Sector 0 represents the first 512 bytes, sector 1 the next 512, and
so on
• The OS has a corresponding structure (struct buf) thatmaps to one sector
• The data within the OS sector structure is often out of sync withthe disk
• xv6 implements an IDE driver
![Page 49: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/49.jpg)
Example: Disk driver
• In charge of copying data back and forth from the disk• Data is represented on the disk as a sequence of sectors, each
containing 512-byte blocks• Sector 0 represents the first 512 bytes, sector 1 the next 512, and
so on
• The OS has a corresponding structure (struct buf) thatmaps to one sector
• The data within the OS sector structure is often out of sync withthe disk
• xv6 implements an IDE driver
![Page 50: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/50.jpg)
Example: Disk driver
• In charge of copying data back and forth from the disk• Data is represented on the disk as a sequence of sectors, each
containing 512-byte blocks• Sector 0 represents the first 512 bytes, sector 1 the next 512, and
so on
• The OS has a corresponding structure (struct buf) thatmaps to one sector
• The data within the OS sector structure is often out of sync withthe disk
• xv6 implements an IDE driver
![Page 51: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/51.jpg)
Example: Disk driver
• In charge of copying data back and forth from the disk• Data is represented on the disk as a sequence of sectors, each
containing 512-byte blocks• Sector 0 represents the first 512 bytes, sector 1 the next 512, and
so on
• The OS has a corresponding structure (struct buf) thatmaps to one sector
• The data within the OS sector structure is often out of sync withthe disk
• xv6 implements an IDE driver
![Page 52: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/52.jpg)
Example: Disk driver
• In charge of copying data back and forth from the disk• Data is represented on the disk as a sequence of sectors, each
containing 512-byte blocks• Sector 0 represents the first 512 bytes, sector 1 the next 512, and
so on
• The OS has a corresponding structure (struct buf) thatmaps to one sector
• The data within the OS sector structure is often out of sync withthe disk
• xv6 implements an IDE driver
![Page 53: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/53.jpg)
Code: struct buf
struct buf {int flags;uint dev;uint sector;struct buf *prev;struct buf *next;struct buf *qnext;uchar data[512];
};#define B_BUSY 0x1#define B_VALID 0x2#define B_DIRTY 0x4
![Page 54: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/54.jpg)
Example: Disk driver initialization
• ideinit() is invoked by main(), which initializes the diskdriver
1 ideinit() sets up CPU(n−1) to handle disk interrupts2 It then makes a call to idewait() to wait for disk 0 to initialize
• It assumes that at least disk 0 is present because the boot loaderand the kernel were obviously loaded from it
3 idewait() polls the status bits of the disk hardware, untilIDE_BSY is clear and IDE_DRDY is set
4 It then checks whether another disk is present
![Page 55: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/55.jpg)
Example: Disk driver initialization
• ideinit() is invoked by main(), which initializes the diskdriver
1 ideinit() sets up CPU(n−1) to handle disk interrupts2 It then makes a call to idewait() to wait for disk 0 to initialize
• It assumes that at least disk 0 is present because the boot loaderand the kernel were obviously loaded from it
3 idewait() polls the status bits of the disk hardware, untilIDE_BSY is clear and IDE_DRDY is set
4 It then checks whether another disk is present
![Page 56: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/56.jpg)
Example: Disk driver initialization
• ideinit() is invoked by main(), which initializes the diskdriver
1 ideinit() sets up CPU(n−1) to handle disk interrupts2 It then makes a call to idewait() to wait for disk 0 to initialize
• It assumes that at least disk 0 is present because the boot loaderand the kernel were obviously loaded from it
3 idewait() polls the status bits of the disk hardware, untilIDE_BSY is clear and IDE_DRDY is set
4 It then checks whether another disk is present
![Page 57: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/57.jpg)
Example: Disk driver initialization
• ideinit() is invoked by main(), which initializes the diskdriver
1 ideinit() sets up CPU(n−1) to handle disk interrupts2 It then makes a call to idewait() to wait for disk 0 to initialize
• It assumes that at least disk 0 is present because the boot loaderand the kernel were obviously loaded from it
3 idewait() polls the status bits of the disk hardware, untilIDE_BSY is clear and IDE_DRDY is set
4 It then checks whether another disk is present
![Page 58: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/58.jpg)
Example: Disk driver initialization
• ideinit() is invoked by main(), which initializes the diskdriver
1 ideinit() sets up CPU(n−1) to handle disk interrupts2 It then makes a call to idewait() to wait for disk 0 to initialize
• It assumes that at least disk 0 is present because the boot loaderand the kernel were obviously loaded from it
3 idewait() polls the status bits of the disk hardware, untilIDE_BSY is clear and IDE_DRDY is set
4 It then checks whether another disk is present
![Page 59: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/59.jpg)
Example: Disk driver initialization
• ideinit() is invoked by main(), which initializes the diskdriver
1 ideinit() sets up CPU(n−1) to handle disk interrupts2 It then makes a call to idewait() to wait for disk 0 to initialize
• It assumes that at least disk 0 is present because the boot loaderand the kernel were obviously loaded from it
3 idewait() polls the status bits of the disk hardware, untilIDE_BSY is clear and IDE_DRDY is set
4 It then checks whether another disk is present
![Page 60: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/60.jpg)
Buffer cache
• The buffer cache is a linked list of struct buf in memory
• It holds cached copies of disk blocks• Uses iderw(struct buf *b) to read/write a buffer from/to
the disk• If B_DIRTY is set, it writes b to disk• If B_VALID is not set, it reads b from disk
![Page 61: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/61.jpg)
Buffer cache
• The buffer cache is a linked list of struct buf in memory
• It holds cached copies of disk blocks• Uses iderw(struct buf *b) to read/write a buffer from/to
the disk• If B_DIRTY is set, it writes b to disk• If B_VALID is not set, it reads b from disk
![Page 62: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/62.jpg)
Buffer cache
• The buffer cache is a linked list of struct buf in memory
• It holds cached copies of disk blocks• Uses iderw(struct buf *b) to read/write a buffer from/to
the disk• If B_DIRTY is set, it writes b to disk• If B_VALID is not set, it reads b from disk
![Page 63: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/63.jpg)
Buffer cache
• The buffer cache is a linked list of struct buf in memory
• It holds cached copies of disk blocks• Uses iderw(struct buf *b) to read/write a buffer from/to
the disk• If B_DIRTY is set, it writes b to disk• If B_VALID is not set, it reads b from disk
![Page 64: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/64.jpg)
Buffer cache
• The buffer cache is a linked list of struct buf in memory
• It holds cached copies of disk blocks• Uses iderw(struct buf *b) to read/write a buffer from/to
the disk• If B_DIRTY is set, it writes b to disk• If B_VALID is not set, it reads b from disk
![Page 65: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/65.jpg)
iderw
• Disk accesses take milliseconds of time therefore polling is not anoption
• iderw keeps the list of pending disk requests in a queue(idequeue) and uses interrupts to signal whenever a requesthas finished
• It adds b to the end of idequeue• If b is the only buffer in idequeue, it instantly dispatches it to the
disk hardware by calling idestart() which either issues aread or a write
• The current process is then put to sleep waiting for the operationon b to be completed
• Once the hardware has completed the operation, it will generatean interrupt (IRQ_IDE) which will be handled by trap byinvoking ideintr()
![Page 66: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/66.jpg)
iderw
• Disk accesses take milliseconds of time therefore polling is not anoption
• iderw keeps the list of pending disk requests in a queue(idequeue) and uses interrupts to signal whenever a requesthas finished
• It adds b to the end of idequeue• If b is the only buffer in idequeue, it instantly dispatches it to the
disk hardware by calling idestart() which either issues aread or a write
• The current process is then put to sleep waiting for the operationon b to be completed
• Once the hardware has completed the operation, it will generatean interrupt (IRQ_IDE) which will be handled by trap byinvoking ideintr()
![Page 67: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/67.jpg)
iderw
• Disk accesses take milliseconds of time therefore polling is not anoption
• iderw keeps the list of pending disk requests in a queue(idequeue) and uses interrupts to signal whenever a requesthas finished
• It adds b to the end of idequeue• If b is the only buffer in idequeue, it instantly dispatches it to the
disk hardware by calling idestart() which either issues aread or a write
• The current process is then put to sleep waiting for the operationon b to be completed
• Once the hardware has completed the operation, it will generatean interrupt (IRQ_IDE) which will be handled by trap byinvoking ideintr()
![Page 68: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/68.jpg)
iderw
• Disk accesses take milliseconds of time therefore polling is not anoption
• iderw keeps the list of pending disk requests in a queue(idequeue) and uses interrupts to signal whenever a requesthas finished
• It adds b to the end of idequeue• If b is the only buffer in idequeue, it instantly dispatches it to the
disk hardware by calling idestart() which either issues aread or a write
• The current process is then put to sleep waiting for the operationon b to be completed
• Once the hardware has completed the operation, it will generatean interrupt (IRQ_IDE) which will be handled by trap byinvoking ideintr()
![Page 69: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/69.jpg)
iderw
• Disk accesses take milliseconds of time therefore polling is not anoption
• iderw keeps the list of pending disk requests in a queue(idequeue) and uses interrupts to signal whenever a requesthas finished
• It adds b to the end of idequeue• If b is the only buffer in idequeue, it instantly dispatches it to the
disk hardware by calling idestart() which either issues aread or a write
• The current process is then put to sleep waiting for the operationon b to be completed
• Once the hardware has completed the operation, it will generatean interrupt (IRQ_IDE) which will be handled by trap byinvoking ideintr()
![Page 70: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/70.jpg)
iderw
• Disk accesses take milliseconds of time therefore polling is not anoption
• iderw keeps the list of pending disk requests in a queue(idequeue) and uses interrupts to signal whenever a requesthas finished
• It adds b to the end of idequeue• If b is the only buffer in idequeue, it instantly dispatches it to the
disk hardware by calling idestart() which either issues aread or a write
• The current process is then put to sleep waiting for the operationon b to be completed
• Once the hardware has completed the operation, it will generatean interrupt (IRQ_IDE) which will be handled by trap byinvoking ideintr()
![Page 71: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/71.jpg)
Code: iderw
void iderw(struct buf *b){
struct buf **pp;acquire(&idelock);b->qnext = 0;for(pp=&idequeue; *pp; pp=&(*pp)->qnext)
;
*pp = b;if(idequeue == b)
idestart(b);while((b->flags & (B_VALID|B_DIRTY)) != B_VALID){
sleep(b, &idelock);}release(&idelock);
}
![Page 72: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/72.jpg)
ideintr
1 Checks the first buffer in idequeue to find the type of theoperation (read or write)
2 If the operation was a read, it reads the data into the buffer viainsl()
3 It then sets B_VALID and clears B_DIRTY and wakes up anyprocesses sleeping on b
4 Finally, it dispatches the next buffer from idequeue
![Page 73: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/73.jpg)
ideintr
1 Checks the first buffer in idequeue to find the type of theoperation (read or write)
2 If the operation was a read, it reads the data into the buffer viainsl()
3 It then sets B_VALID and clears B_DIRTY and wakes up anyprocesses sleeping on b
4 Finally, it dispatches the next buffer from idequeue
![Page 74: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/74.jpg)
ideintr
1 Checks the first buffer in idequeue to find the type of theoperation (read or write)
2 If the operation was a read, it reads the data into the buffer viainsl()
3 It then sets B_VALID and clears B_DIRTY and wakes up anyprocesses sleeping on b
4 Finally, it dispatches the next buffer from idequeue
![Page 75: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/75.jpg)
ideintr
1 Checks the first buffer in idequeue to find the type of theoperation (read or write)
2 If the operation was a read, it reads the data into the buffer viainsl()
3 It then sets B_VALID and clears B_DIRTY and wakes up anyprocesses sleeping on b
4 Finally, it dispatches the next buffer from idequeue
![Page 76: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/76.jpg)
Modern disk controllers in a real OS
• Most disk controllers typically accept multiple outstanding diskrequests at a time
• They even reorder them to make efficient use of the disk arm• Conceptually other hardware is very similar to disks
• Network buffers hold packets• Audio device buffers hold sound samples• Graphics card buffers hold video data
![Page 77: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/77.jpg)
Modern disk controllers in a real OS
• Most disk controllers typically accept multiple outstanding diskrequests at a time
• They even reorder them to make efficient use of the disk arm• Conceptually other hardware is very similar to disks
• Network buffers hold packets• Audio device buffers hold sound samples• Graphics card buffers hold video data
![Page 78: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/78.jpg)
Modern disk controllers in a real OS
• Most disk controllers typically accept multiple outstanding diskrequests at a time
• They even reorder them to make efficient use of the disk arm• Conceptually other hardware is very similar to disks
• Network buffers hold packets• Audio device buffers hold sound samples• Graphics card buffers hold video data
![Page 79: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/79.jpg)
Modern disk controllers in a real OS
• Most disk controllers typically accept multiple outstanding diskrequests at a time
• They even reorder them to make efficient use of the disk arm• Conceptually other hardware is very similar to disks
• Network buffers hold packets• Audio device buffers hold sound samples• Graphics card buffers hold video data
![Page 80: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/80.jpg)
Modern disk controllers in a real OS
• Most disk controllers typically accept multiple outstanding diskrequests at a time
• They even reorder them to make efficient use of the disk arm• Conceptually other hardware is very similar to disks
• Network buffers hold packets• Audio device buffers hold sound samples• Graphics card buffers hold video data
![Page 81: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/81.jpg)
Modern disk controllers in a real OS
• Most disk controllers typically accept multiple outstanding diskrequests at a time
• They even reorder them to make efficient use of the disk arm• Conceptually other hardware is very similar to disks
• Network buffers hold packets• Audio device buffers hold sound samples• Graphics card buffers hold video data
![Page 82: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/82.jpg)
DMA
• High-bandwidth I/O devices, such as disks, graphics cards,network cards, etc. often use direct memory access (DMA)instead of explicit I/O (insl and outsl)
• Via DMA, controllers get direct access to physical memory
• The device driver passes the physical address of the buffer’s datafield to the device, which copies it from/to the main memory
• Once it is done, it generates an interrupt
• As a result of DMA, the CPU is not involved, hence making theentire process more efficient
![Page 83: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/83.jpg)
DMA
• High-bandwidth I/O devices, such as disks, graphics cards,network cards, etc. often use direct memory access (DMA)instead of explicit I/O (insl and outsl)
• Via DMA, controllers get direct access to physical memory
• The device driver passes the physical address of the buffer’s datafield to the device, which copies it from/to the main memory
• Once it is done, it generates an interrupt
• As a result of DMA, the CPU is not involved, hence making theentire process more efficient
![Page 84: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/84.jpg)
DMA
• High-bandwidth I/O devices, such as disks, graphics cards,network cards, etc. often use direct memory access (DMA)instead of explicit I/O (insl and outsl)
• Via DMA, controllers get direct access to physical memory
• The device driver passes the physical address of the buffer’s datafield to the device, which copies it from/to the main memory
• Once it is done, it generates an interrupt
• As a result of DMA, the CPU is not involved, hence making theentire process more efficient
![Page 85: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/85.jpg)
DMA
• High-bandwidth I/O devices, such as disks, graphics cards,network cards, etc. often use direct memory access (DMA)instead of explicit I/O (insl and outsl)
• Via DMA, controllers get direct access to physical memory
• The device driver passes the physical address of the buffer’s datafield to the device, which copies it from/to the main memory
• Once it is done, it generates an interrupt
• As a result of DMA, the CPU is not involved, hence making theentire process more efficient
![Page 86: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/86.jpg)
DMA
• High-bandwidth I/O devices, such as disks, graphics cards,network cards, etc. often use direct memory access (DMA)instead of explicit I/O (insl and outsl)
• Via DMA, controllers get direct access to physical memory
• The device driver passes the physical address of the buffer’s datafield to the device, which copies it from/to the main memory
• Once it is done, it generates an interrupt
• As a result of DMA, the CPU is not involved, hence making theentire process more efficient
![Page 87: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/87.jpg)
Dynamic interrupt routing
• In case of xv6 all disk interrupts are routed to CPU(n−1)• Real OSes use sophisticated algorithms to dynamically route
interrupts to processors• The goal is to achieve optimum load balancing while maintaining
good locality
![Page 88: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/88.jpg)
Dynamic interrupt routing
• In case of xv6 all disk interrupts are routed to CPU(n−1)• Real OSes use sophisticated algorithms to dynamically route
interrupts to processors• The goal is to achieve optimum load balancing while maintaining
good locality
![Page 89: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/89.jpg)
Dynamic interrupt routing
• In case of xv6 all disk interrupts are routed to CPU(n−1)• Real OSes use sophisticated algorithms to dynamically route
interrupts to processors• The goal is to achieve optimum load balancing while maintaining
good locality
![Page 90: AOS Lab 8: Interrupts and Device Drivers](https://reader034.vdocuments.mx/reader034/viewer/2022051312/546c91d8b4af9f932c8b5030/html5/thumbnails/90.jpg)
Reading(s)
• Chapter 3, “Traps, interrupts, and drivers”, section “Code:Interrupts" onwards from “xv6: a simple, Unix-like teachingoperating system”