Chapter 12: Software interrupts (SWI) and exceptions

Slides:



Advertisements
Similar presentations
CPU Structure and Function
Advertisements

ECE 353 Introduction to Microprocessor Systems
Lab III Real-Time Embedded Operating System for a SoC System.
Exceptions. Exception Types Exception Handling Vectoring Interrupts Interrupt Handlers Interrupt Priorities Interrupt applications 6-2.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Computertechniek Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 1.
ARM Microprocessor “MIPS for the Masses”.
EET 2261 Unit 9 Interrupts  Read Almy, Chapters 17 – 19.  Homework #9 and Lab #9 due next week.  Quiz next week.
Chapter 11 Multiple interrupts CEG Microcomputer Systems CEG2400 Ch11 Multiple Interrupts V4c
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
OS Spring’03 Introduction Operating Systems Spring 2003.
ARM 7 Datapath. Has “BIGEND” input bit, which defines whether the memory is big or little endian Modes: ARM7 supports six modes of operation: (1) User.
COMP3221 lec28-exception-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 28: Exceptions & Interrupts - II
COMP3221 lec27-exception-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 27: Exceptions & Interrupts - I
Chapter 9: Hardware Interrupts -- IRQ=External Interrupt Request
COMP3221 lec40-exception-review.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 40: Review of Exception
Midterm Wednesday 11/19 Overview: 25% First Midterm material - Number/character representation and conversion, number arithmetic - DeMorgan’s Law, Combinational.
The 8051 Microcontroller and Embedded Systems
CH12 CPU Structure and Function
The ARM Programmer’s Model
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Thumb Instruction Set.
Embedded System Design Center
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
Exception and Interrupt Handling
Introduction to Embedded Systems
System Calls 1.
Exceptions and Interrupts 2
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Introduction to Embedded Systems Rabie A. Ramadan 6.
Chap. 3 ARM CPU Architecture. 2 Outline 3.1 Registers 3.2 Memory 3.3 Exceptions.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
AT91 Interrupt Handling. 2 Stops the execution of main software Redirects the program flow, based on an event, to execute a different software subroutine.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
AT91 C-startup. 2 For reasons of modularity and portability most application code for an embedded application is written in C The application entry point.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
بسم الله الرحمن الرحيم MEMORY AND I/O.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
1 TM 1 Embedded Systems Lab./Honam University r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 (sp) r14 (lr) r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp)
Introduction to ARM processor. Intro.. ARM founded in November 1990 Advanced RISC Machines Company headquarters in Cambridge, UK Processor design centers.
ARM7 TDMI INTRODUCTION.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Ch 5. ARM Instruction Set  Data Type: ARM processors supports six data types  8-bit signed and unsigned bytes  16-bit signed and unsigned half-words.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Chapter 9: Hardware Interrupts -- IRQ=External Interrupt Request
Chapter 12: Software interrupts (SWI) and exceptions
Chapter 4: Introduction to Assembly Language Programming
Chapter 9: Hardware Interrupts -- IRQ=External Interrupt Request
Microprocessor and Assembly Language
68HC11 Interrupts & Resets.
Microprocessor Systems Design I
Embedded System Design Center
Timer and Interrupts.
Introduction of microprocessor
May 2006 Saeid Nooshabadi ELEC2041 Microprocessors and Interfacing Lectures 27: Exceptions & Interrupts - I
Chapter 4 Addressing modes
Chapter 11 Multiple interrupts
Arithmetic using a stack
Chapter 12: Software interrupts (SWI) and exceptions
Chapter 10 And, Finally... The Stack
ARM Introduction.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Multiply Instructions
Agenda Programmers Model Instruction Sets System Design
May, 2004 Modified from notes by Saeid Nooshabadi
Presentation transcript:

Chapter 12: Software interrupts (SWI) and exceptions CEG2400 - Microcomputer Systems [1] ARM7TDMI, Revision: r4p1, Technical Reference Manual http://infocenter.arm.com/help/topic/com.arm.doc.ddi0210c/DDI0210B.pdf CEG2400 Ch12 . SWI v4a

A brief introduction (revision) of interrupt/exception CEG2400 Ch12 . SWI v4a

What is interrupt/exception? Main () { : Doing something (e.g. browsing) } ring Phone rings Can happen anytime Depends on types of interrupts _isr() //Interrupt service routine { some tasks (e.g. answer telephone) }//when finished, //goes back to main Phone rings CEG2400 Ch12 . SWI v4a

Examples When your computer is running, a key press will trigger an interrupt to input a character to your system The dispatcher in the operating system is implemented by timer interrupt. Timer interrupts the CPU at a rate of 1KHz At each interrupt the system determines which task to run next. CEG2400 Ch12 . SWI v4a

Important interrupts Interrupts Reset (or power up) Software Interrupt SWI-XX Hardware FIQ,IRQ Timer ADC External EINT SWI Triggered by the software instruction SWI x Triggered by power_up/ reset_key Triggered by hardware sources CEG2400 Ch12 . SWI v4a

Interrupt and exception The terms are used differently by various manufacturers Traditionally exception means The normal operation of a program is interrupted and the processor will execute another piece of software (exception handling) somewhere. Interrupt (hardware interrupt) is an exception caused by some hardware condition happening outside the processor (e.g. external hard interrupt, IRQ FIQ). Software interrupt (SWI) is an exception caused by an assembly software instruction (SWI 0x?? exception call instruction) written in the software code. Trap is an exception caused by a failure condition of the processor (e.g. abort “pre-fetch , data” , undefined instruction, divided_by_zero, or stack overflow etc) CEG2400 Ch12 . SWI v4a

Important interrupts in words Reset, a special interrupt to start the system– happens at power up , or reset button depressed) Software interrupt SWI: similar to subroutine – happens when “SWI 0x??” is written in the program Hardware interrupt FIQ (fast interrupt) or IRQ (external interrupt), when the external interrupt request pin is pulled low, or an analogue to digital conversion is completed, or A timer/counter has made a regular request Interrupt handling hardware IRQ FIQ Timer/Counter ADC UART End of conversion Counter overflow End of transmission Interrupt request generated Inside LPC2131 CEG2400 Ch12 . SWI v4a

Software Interrupt (SWI) Introduction to Software Interrupt (SWI) CEG2400 Ch12 . SWI v4a

Compare hardware and software interrupt Hardware interrupt, e.g. Software interrupt A falling edge at EINT3 will trigger the execution of the interrupt service routine __irq IRQ_Eint1() IRQ_Eint1() { :: } Computer EINT3 Computer Main{ : SWI N } An instruction “SWI N” in the program will trigger the execution of the N-th-sys-routine (system routine) N-th-sys-routine() { :: } CEG2400 Ch12 . SWI v4a

Exception (interrupt) Modes ARM supports 7 types of exceptions and has a privileged processor mode for each type of exception. ARM Exception (interrupt) vectors SWI IRQ CEG2400 Ch12 . SWI v4a

Different types of exceptions Reset (supervisor model, at power up , or reset button depressed) Undefined Instruction (for co-processors *) *Prefetch Abort for instruction fetch memory fault *Data Abort : for data access memory fault Software Interrupt (SWI) : supervisor mode, operating sys. calls FIQ (Fast interrupt request) IRQ (interrupt request) * not discussed here , refer to http://infocenter.arm.com/help/topic/com.arm.doc.ddi0210c/DDI0210B.pdf CEG2400 Ch12 . SWI v4a

What should a CPU do when it enters interrupt? Exercise 12.1 Student ID: ___________,Date:_____________ Name: __________________________________ What should a CPU do when it enters interrupt? Answer: __________________________________ What is the difference between software and hardware interrupt? What is the meaning of Trap? Name all interrupts of LPC2131 and their entry addresses. CEG2400 Ch12 . SWI v4a

common usage of exceptions For building operating systems Reset (supervisor model, at power up , or reset button depressed) Undefined Instruction (for co-processors *) *Prefetch Abort for instruction fetch memory fault *Data Abort: for data access memory fault Software Interrupt (SWI) : supervisor mode, operating sys. calls For embedded systems, hardware systems FIQ (Fast interrupt request) IRQ (interrupt request) * not discussed here , refer to http://infocenter.arm.com/help/topic/com.arm.doc.ddi0210c/DDI0210B.pdf CEG2400 Ch12 . SWI v4a

ARM Registers 32-bit CEG2400 Ch12 . SWI v4a

Recall program status regs Mode of interrupt Mode N Z C V 28 31 8 4 I F T Copies of the ALU status flags (latched if the instruction has the "S" bit set). * Condition Code Flags * Interrupt Disable bits. I = 1, disables the IRQ. F = 1, disables the FIQ. * T Bit (Architecture v4T only) T = 0, Processor in ARM state T = 1, Processor in Thumb state N = Negative result from ALU flag. Z = Zero result from ALU flag. C = ALU operation Carried out V = ALU operation oVerflowed CEG2400 Ch12 . SWI v4a

Recall: registers Shaded registers are extra Registers for different General registers and Program Counter Program Status Registers r15 (pc) r14 (lr) r13 (sp) r14_svc r13_svc r14_irq r13_irq r14_abt r13_abt r14_undef r13_undef User32 / System FIQ32 Supervisor32 Abort32 IRQ32 Undefined32 cpsr sprsr_fiq spsr_abt spsr_svc spsr_fiq spsr_irq r12 r10 r11 r9 r8 r7 r4 r5 r2 r1 r0 r3 r6 r14_fiq r13_fiq r12_fiq r10_fiq r11_fiq r9_fiq r8_fiq spsr_undef Shaded registers are extra Registers for different modes SPSR= Saved Process Status Reg CEG2400 Ch12 . SWI v4a

Mode bits M[0:4] : bit0->bit4 of CPSR N Z C V 28 31 8 4 I F T Mode bits M[0:4] : bit0->bit4 of CPSR http://infocenter.arm.com/help/topic/com.arm.doc.ddi0210c/DDI0210B.pdf CEG2400 Ch12 . SWI v4a

We will study Software Interrupt (SWI) CEG2400 Ch12 . SWI v4a

Why Software interrupt SWI Why Software interrupt SWI? “Similar to a subroutine call but more efficient and organized” Make a list of often used routines To build system calls in Linux or Windows. E.g. print character , read keyboard etc.. CEG2400 Ch12 . SWI v4a

SWI software interrupt For operating sys. (OS) developers to write often used routines E.g. SWI 0x12 is for “write a character to screen” So you may have a table of all routines and called by users or OS programs. SWI table 0x01= reset system 0x02= init timer : 0x12 = write a charter to screen 0x13= make a beep sound for 0.5 seconds… CEG2400 Ch12 . SWI v4a

Example E.g.1 For Building OS Operating system calls SWI software interrupt for writing OS calls An efficient way for user to make OS calls Examples, SWI table SWI 20 = Print text on screen SWI 23 =Read real time clock SWI 35 =Keyboard read -…… Main() { : } Supervisor mode SWI x ;software interrupt Program for SWI x Print a character to screen CEG2400 Ch12 . SWI v4a

Code Example When SWI is in your code: E.g. SWI vector=SWI 0x11, vector =0x11 CEG2400 Ch12 . SWI v4a

SWI interrupt procedures (enter the supervisor mode) SWI (software interrupt ) Caused by “SWI 0x??” in your program Arm completes the current instruction. Goto SWI exception address 0x08 (short form for 0x000 0008) Exception entry, execution procedure (see next slide) {Change to supervisor op. mode :CPSR (bit0-4) : Return from interrupt MOVS pc, lr return to main} CEG2400 Ch12 . SWI v4a

Details of entering an interrupt (exception) Preserves the address of the next instruction in the appropriate Link Register (e.g. r14_svc  r14 of supervisor mode) Copies the CPSR (Current Program Status Register ) into the appropriate SPSR (Saved Process Status Reg. e.g. SPSR_svc) Forces the CPSR mode bits to a value which depends on the exception (supervisor, interrupt etc) Forces the PC (program counter r15) to fetch the next instruction from the relevant exception vector It may also set the interrupt disable flags to prevent otherwise unmanageable nesting of exceptions. http://infocenter.arm.com/help/topic/com.arm.doc.ddi0210c/DDI0210B.pdf CEG2400 Ch12 . SWI v4a

SWI Software interrupt Warning: What was in R0? User program may have been using this register. Therefore, cannot just use it ­ must first save it first (push stack) On SWI, the processor (1) copies CPSR to SPSR_SVC (2) sets the CPSR mode bits to supervisor mode (3) sets the CPSR IRQ to disable (4) stores the value (PC + 4) into LR_SVC (5) forces PC to 0x08 Usage of BIC (Bit Clear) E.g. BIC R0, R0, #%1011 ; Clear bit-0, bit-1, bit-3 in R0. to R_Handler to U_Handler to S_Handler to P_Handler to D_Handler ... to I_Handler to F_Handler Vector Table (spring board) starting at 0x00 in memory 0x00 0x04 0x08 0x0c 0x10 0x14 0x18 0x1c (Reset (Undef instr.) (SWI) (Prefetch abort) (Data abort) (Reserved) (IRQ) (FIQ) SWI Handler:0x40001000 (S_Handler) ADD r0,r0,r1 SWI 0x02 SUB r2,r2,r0 USER Program:0x40002000 LDR r0,[lr,#­-4] BIC r0,r0,#0xff000000 // now the vector is in r0 switch (r0){ case 0x00: service_SWI1(); case 0x01: service_SWI2(); case 0x02: service_SWI3(); … } MOVS pc, lr From www.cs.ucr.edu/~amitra/context_switch/extra/04_swi.ppt CEG2400 Ch12 . SWI v4a

Exercise 12.2: SWI handler : Assume the SWI handler is at 0x40001000 i) What is the content of address 0x08? Why? Answer:?__________________________________________ ii) What will the Processor do when it runs SWI 0x02 ANSWER: On SWI, the processor will execute ?____________________________ (2) ?____________________________ (3) ?____________________________ (4) ?____________________________ (5) ?______________________________ SWI handler (from 0x40001000) :push registers onto stack LDR r0,[lr,#-­4] BIC r0,r0,#0xff000000 :swtch(r0) .. etc : :pop registers from stack MOVS pc, lr; return from interrupt User main program (from 0x40002000) Address instruction 0x40002000 ADD r0,r0,r1 0x40002004 SWI 0x02 0x40002008 SUB r2,r2,r0 CEG2400 Ch12 . SWI v4a

Exercise 12.3 SWI handler Inside the SWI handler i) What are the mode bits M[0:4] : bit0->bit4 of CPSR? Answer: ?______________ Ii) What is the running mode inside the SWI handler: supervisor or user32? ANSWER: ?___________________________________ III) When the link register lr is used, which lr the processor is using :r14(lr) or r14_svc or r14_irq? Answer?__________________________________ The Machine code of SWI 0x02 is 0xea000002 . List the values of r0 after the first and second instruction of the SWI handler. LDR r0,[lr,#­-4] BIC r0,r0,#0xff000000; clear the most significant 2 bytes of r0 // now the vector is in r0 switch (r0){ case 0x00: service_SWI1(); case 0x01: service_SWI2(); case 0x02: service_SWI3(); … } CEG2400 Ch12 . SWI v4a

Details of leaving an interrupt ARM7TDMI tech. ref. (section 2 Details of leaving an interrupt ARM7TDMI tech. ref. (section 2.8 exception)[1] At the end of the SWI handler: Movs pc,lr Move the r14, minus an offset to the PC. The offset varies according to the type (auto) SPSR_svc CPSR. (auto) Clear the interrupt disable flags that were set on entry Bye Bye! CEG2400 Ch12 . SWI v4a

Summary Learned the basic concept of exceptions and interrupts SWI (Software interrupt) CEG2400 Ch12 . SWI v4a

Appendix Alternative set bit method in “C” Y=0x1<<21;//left shift 21 bits, this sets bit21=1 and other bits= 0 Before shift Y=0x1=0000 0000 0000 0000 0000 0000 0000 0001 (Binary) After shift Y= 0000 0000 0010 0000 0000 0000 0000 0000 (Binary) bit 31 bit 21 bit0 Exercise: set bit 9 of register R to be 1, other bits to be 0. Answer=0x1<<9; So R=0000 0000 0000 0000 0000 0010 0000 0000 (Binary) =0x200 Bit9 =1 CEG2400 Ch12 . SWI v4a