arm exception handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. case...

33
ARM Exception Handling CS2253 Owen Kaser, UNBSJ

Upload: others

Post on 07-Sep-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

ARM Exception HandlingCS2253

Owen Kaser, UNBSJ

Page 2: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Overview● Warning: hardest parts of CS2253.

● Back to Chapter 1: Processor Modes & Vector Table

● Concept of Exceptions

– Interrupt Handlers

– Priority Levels● Software Interrupts

● Memory-Mapped Input/Output

● See Textbook Chapter 14, 16

Page 3: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Exceptions

● Sometimes, the normal flow of control needs to be unexpectedly modified– A character is received on the keyboard

– An access is made to a memory location with no memory/device there

– The processor is asked to execute a bit pattern that is not a valid machine code

● Two main cases: interrupts and errors

Page 4: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Interrupts

● An interrupt can occur because a hardware device wants service...now.

● Device: “I have just received a character from the keyboard, and my buffer is only 1 character deep. Please stop whatever you are doing and remove/process this character ASAP, so that I will be able to accept the next character the keyboard sends”.

● CPU: “ok, I see your interrupt request. I am interrupting my normal execution to switch to handler code for you. When I finish, I will return to my normal execution, where I left off.”

Page 5: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Why Interrupts?

● There can be a lot of asynchronous things happening in a computer system. Having one program keeping track of them would be hard.

● (Eg, every loop that ran more than a few microseconds would have to contain code to check for input/output)

● System running one dedicated program can use this approach. Maybe it even waits, looping, while I/O happens.

● This is called polling I/O. It is generally viewed as unweildy. ● Better to just have the I/O device interrupt.

Page 6: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

IRQ and FIQ interrupts

● The ARM ISA defines regular (IRQ) interrupts and fast (FIQ) interrupts.

● Stay tuned.

Page 7: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Software Interrupts

● Some ISAs, including ARMv4, have a special SWI instruction that, when executed, causes the system to act like a hardware device requested an interrupt.

● A hardware interrupt is like an unscheduled subroutine call that also puts the processor into an more privileged mode. Handler code is trusted and part of the operating system.

● So an SWI instruction is often used to invoke an operating system service subroutine.

● Book refers to the SWI instruction under its new name, SVC, but Crossware still uses SWI.

Page 8: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Error Exceptions

● Undefined instruction– Can be intentional (emulate a “missing” instruction)

● Prefetch abort– An attempt to fetch instruction fails (eg, PC is not a valid memory

location)

● Data abort– A LD or ST with an illegal address

– A store to a read-only address

● Sometimes, the response should be to die gracefully. But other times, we may be able to recover and continue.

Page 9: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Overall Approach

● For an exception, we need to – save the current state (including CPSR)

– Reset the PC to the handler code [ & change mode]

– Execute the handler

– Restore the saved state, including the PC & mode

● State saving and PC resetting are done by hardware. Handler and restoring done by software.

Page 10: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Modes

● See textbook Sections 2.3.1, 2.3.2.● Normal code executes in User mode. In processing

exceptions, modes are System, Undef, Abort, IRQ, FIQ, Supervisor.

● In some of these modes, some of the registers are banked out. The User version of R11 is hidden from use, replaced by another R11 when in FIQ mode. (So the User version of R11 is safe from modifications.)

Page 11: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Processor Modes (Book Fig 2.1)

Page 12: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Registers in Different Modes

Page 13: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Recognizing Your Mood Mode

● The CPSR stores more than your status flags. A 5-bit field M4:M0 stores a flag indicating mode (Table 2.1) – eg 10000 for User, 11011 for Undefined, etc.

● I bit enables or disables IRQ interrupts● F bit enables or disables FIQ (fast) interrupts● T is status: are you in Thumb mode?

Page 14: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Details Invoking Exception

● See textbook 14.4.● First, CPSR copied to SPSR_<mode>● Adjust CPSR (mode bits, disable IRQ, maybe

disable FIQ)● Store return address to LR_<mode>● Set PC to start of relevant handler

Page 15: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Finding the Right Handler● For the different kinds of exceptions, there are

different handlers. When an exception occurs, the hardware determines the source of the exception as a 3-bit number, which it uses to index the vector table (which starts in memory at address 0).

Page 16: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Textbook Figure 14.3

Page 17: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Returning After Exception

● When the handler has finished its task, it returns to the caller (in software)● The mode needs to be put back to its pre-interrupt value. And the PC needs to

be put back to the correct instruction.– Either to the instruction that had the exception (and did not successfully finish) or to the

next instruction. Case depends on the kind of exception

● SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR restore when PC is the destination and the S flag set

● Or on entry to handler – adjust LR (eg subtract 4)

– STMDB sp!,{some regs, lr}

● then use a

LDMIA sp!, {some regs, pc}^ to return.● ^ means to restore CPSR also.

Page 18: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Multiple Stacks

● Interrupt code typically uses stacks. And there is a separate R13 for each mode (except one). So there is a separate stack per mode...and at machine startup, it needs to be initialized.

● Initialization via a MSR (move into status register) instruction to change mode.

● Then store a value to (that) SP.● Then use a MSR to put mode back

Page 19: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Mrs. MRS

● MSR moves to a status register.– Status registers are CPSR, SPSR

– Underscores after (eg CPSR_cf) indicate which sub-parts of the status register are affected. Used in book code but not described… _cxsf is all of it?.

● MRS moves a status register into a regular register.

Page 20: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Implementing ADDSHIFT

● See Example 14.1 for the hairiest program of CS2253.

Page 21: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Priorities

● In order of decreasing priority, we have– Reset

– Data abort

– FIQ

– IRQ

– Prefetch abort

– SVC and Undefined Instruction

● A higher priority exception can interrupt the handling of a lower priority exception, but usually not the other way.

Page 22: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Priorities in IRQ

● Even within a given exception (eg IRQ), some hardware units (eg disk) are more urgent than others (eg keyboard).

● To prioritize, could OR together all interrupt request inputs. Then software can check each possible device to see who’s knocking...starting with the most urgent.

● Or a special priority device, a VIC, can take care of this.– Devices' IRQ lines go to VIC

– Only VIC actually interrupts CPU

– CPU can ask VIC for the handler address of the highest priority active interrupt request. [talking to devices: stay tuned!]

Page 23: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Vectoring IRQ InterruptsTextbook Figure 14.5

Page 24: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Software Interrupts

● Software can generate an exception. Use SWI to request an operating-system service.

● SWI handler has to use the value in R14 to find the actual instruction, in order to extract the “SVC number” field and thus know which OS service was requested.

● Assembler example: SWI 234

Page 25: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Talking to Devices (Ch 16)

● Not dealing with co-processors in CS2253.● Instead, we are dealing with attached devices

such as– UART (“serial port”)

– Timer

– Analogue to Digital (A-D) and Digital to Analogue converters

– Disk controllers

– General Purpose I/O (GPIO) connections that can control electronic devices (LEDs, motors, ...)

Page 26: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Special I/O Instructions

● Some ISAs (not ARM) have special instructions to access devices

● Intel x86: IN and OUT instructions. ● Devices are assigned 1+ numeric “port addresses”. ● CPU does an OUT instruction to the port address of the

UART to give it a command● CPU does an IN instruction to the port address to get a byte

of data from the UART, or check its status.● Frequently, a device has several (usually consecutive) port

addresses:– 1+ status port, 1+ control port, 1+ data output port, 1+ data input

port.

Page 27: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Memory-Mapped I/O

● Alternative: memory-mapped I/O, where certain “memory” addresses have no RAM.

● Instead, they are assigned to devices. No distinction between “port addresses” and “memory addresses”

● Now, ordinary LD and ST instructions (to the right “memory” addresses) can talk to devices.

● No IN or OUT instructions: RISC-ish.

Page 28: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Book Example

● LPC2104 System-on-Chip has an ARM processor core and a bunch of memory-mapped devices (peripherals)

● Some are internally connected via an “AHB” bus and some via an “VPB” bus.

● All VPB bus peripherals are in one range of addresses (0xE000000 to 0xEFFFFFF)

● UART0 device occupies 0xE000C000 to 0xE000C01C

● Every 4 addresses, we have 8 bits of data. Use LDRB or STRB to access, avoid endian-ness issue.

Page 29: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

SoC's Memory Map

Page 30: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Zoom Into 0xE000C000 area

Page 31: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

UARTs are Fiddley

● Serial communication is quite hairy. Lots of communications parameters to be set. You're lucky this era is largely past us.

● After setup, sending characters is pretty easy.● Bit 5 of the Line Status Register (memory mapped

to 0xE000C014) tells us if the transmitter buffer can accept another character now. (0 means “yes”)

● Transmit Holding Register (0xE000C000) is where we can put the next character.

Page 32: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Polling I/O Example (p 347, mod)

; ASCII code to send is in R0

LDR R5, =0xE000C000wait LDRB R6, [R5, #0x14] ; Line Status Reg CMP R6, #0x20 ; Risky check of Bit 5 BEQ wait ; spin until ready

; if we get here, bit 5 = 0

STRB R0,[R5] ; write to transmitter buff.

Page 33: ARM Exception Handlingowen/courses/2253-2017/slides/08-interrupts.pdf · next instruction. Case depends on the kind of exception SUBS PC, LR, #4 or SUBS PC, LR, #8 ← magic CPSR

Polling vs Interrupt Driven I/O

● Note how the “wait” loop repeated asks (polls) the device to see if it is ready.

● If not, it just tries again...and again...and....● No other work in the system can be done...tight polling loops

waste system resources.● Better to flip some bits in 0xE000C004 and get the transmitter

to give us an interrupt when its buffer gets some space. In the meantime, we can be doing other things (eg, getting input, doing calculations).

● This “Interrupt-driven I/O” is generally better, though more complicated.