6. Learn ARM from scratch - exception and interrupt handling, exception vector table, swi

Publisher:喜悦的38号Latest update time:2021-08-04 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Exception

Exception is the most important knowledge point to understand the operation of CPU. Almost every processor supports specific exception handling, and interrupt is one of the exceptions.
Sometimes when we measure the real-time performance of an operating system, we look at the shortest interrupt response time of the OS and the number of interrupt responses per unit time.

2. Abnormal Source

In the ARM architecture, there are 7 types of exception handling. When an exception occurs, the processor sets the PC to a specific memory address. This address is placed in a specific address range called a vector table. The entry of the vector table is some jump instructions that jump to a subroutine that specifically handles a certain exception or interrupt.

1. Abnormal source classification

To enter the exception mode, there must be an exception source. ARM stipulates 7 exception sources:

Abnormal sourcedescribe
ResetExecute at power on
UndefExecuted when an illegal instruction in the pipeline reaches the execution state
SWIExecuted when a soft interrupt instruction is executed
PrefetchThis exception is generated when an instruction is prefetched from memory and fails for some reason. If it can reach the execution state,
DataIf a prefetch instruction attempts to access an illegal memory location, an exception is generated.
IRQUsual interruptions
FIQFast interrupt
  1. reset abnormal

This exception is entered when the CPU is just powered on or after the reset button is pressed. This exception is handled in management mode.

  1. irq/fiq general/fast interrupt request

The CPU and external devices are independent hardware execution units. The CPU manages all devices and schedules resources. If the CPU wants to know the operating status of external devices, it either checks the specific registers of the external devices at regular intervals, or allows the external devices to "interrupt" the CPU when CPU intervention is required, so that it can handle the requests of the external devices. There is no doubt that the second method is more reasonable, allowing the CPU to "concentrate" on its work. The "interrupt" operation here is called an interrupt request. According to the urgency of the request, interrupt requests are divided into general interrupts and fast interrupts. Fast interrupts have the highest interrupt priority and the smallest interrupt delay. They are usually used to process high-speed data transmission and channel data recovery processing, such as DMA. Most peripherals use general interrupt requests.

  1. Prefetch instruction abort exception

This exception occurs in the instruction fetch stage of the CPU pipeline. If the target instruction address is an illegal address, the exception is entered and the exception is handled in the abort exception mode.

  1. Undefined instruction exception

This exception occurs in the decoding stage of the pipeline technology. If the current instruction cannot be recognized as a valid instruction, an undefined instruction exception is generated. This exception is handled in the undefined exception mode.

  1. Software interrupt instruction (swi) exception

This exception is generated when the application calls itself, and is used when the user program requests access to hardware resources, for example: printf() printing function, to print user data to the display, the user program must apply to use the display to achieve printing, and the user program does not have the right to use the peripheral hardware, and can only switch to the kernel state by using the software interrupt instruction, and access the peripheral hardware through the operating system kernel code. The kernel state works in privileged mode, and the operating system completes the printing of user data to the display in privileged mode. The purpose of doing this is nothing more than to protect the security of the operating system and the rational use of hardware resources. This exception is handled in management mode.

  1. Data abort access exception
    This exception occurs when the data address to be accessed does not exist or is an illegal address. This exception is handled in abort exception mode.

2. ARM exception priority

Reset→

Data abort→

FIQ→

IRQ→

Prefetch abort→

Undefined instruction/SWI.


3. Why FIQ is faster than IRQ

  1. fiq has higher priority than irq

  2. The FIQ vector is at the end of the vector table, and exception handling does not require a jump

  3. FIQ has 5 more private registers (r8-r12) than IRQ, and fewer stack push and pop operations are performed during interrupt operations.

3. Hardware Operations That Occur Abnormally

After an exception occurs, the operation steps of the ARM core can be summarized into 4 big steps and 3 small steps.

1. 4 big steps and 3 small steps

  1. Save execution state: copy CPSR to SPSR in the exception mode that occurred;

  2. Mode switch:

  • The CPSR mode bits are forced to the value corresponding to the exception type,

  • The processor enters ARM execution mode.

  • Disable all IRQ interrupts, and disable FIQ interrupts when entering FIQ fast interrupt mode;

  1. Save the return address: Save the address of the next instruction (interrupted program) in LR (LR_excep in exception mode).

  2. Jump into the exception vector table: force the PC value to be set to the corresponding exception vector address and jump to the exception handler.

2. Detailed steps

  1. Save execution status

The execution status of the current program is saved in the CPSR. When an exception occurs, the execution status in the current CPSR is saved in the SPSR in the exception mode. When the exception returns in the future, the CPSR is restored to the execution status.

  1. Mode Switching

The hardware automatically writes the exception code to the M[4:0] mode bit in the CPSR according to the current exception type, so that the CPU enters the corresponding exception mode. Regardless of whether an exception occurs in the ARM state or the THUMB state, it will automatically switch to the ARM state for exception processing. This is done automatically by the hardware, setting CPSR[5] to 0. At the same time, the CPU will turn off the interrupt IRQ (set the I bit of the CPSR register) to prevent interrupts from entering. If the current exception is a fast interrupt FIQ, turn off the fast interrupt (set the F bit of the CPSR register).

  1. Save return address

The current program is interrupted by an exception and switches to the exception handler. After the exception is handled, it returns to the current interrupted mode to continue execution. Therefore, the address of the next instruction of the currently executed instruction must be saved to LR_excep (LR in exception mode, there is no LR_excep register, _excep is added for the convenience of readers to understand, the same principle applies below). Due to different exception modes and the use of pipeline technology in the ARM core, the exception handler must calculate the return address according to the exception mode.

  1. Jump into the exception vector table

This operation is automatically performed by the CPU hardware. When an exception occurs, the CPU forces the value of PC to be modified to a fixed memory address. This fixed address is called the exception vector.

4. Exception Vector Table

The exception vector table is a specific memory address space. Each ARM exception corresponds to a word length space (4Bytes), which is exactly the length of a 32-bit instruction. When an exception occurs, the CPU forces the value of PC to be set to the fixed memory address corresponding to the current exception.

1. Exception vector table:

Exception Vector Table

The operation of jumping into the exception vector table is automatically completed by the hardware when an exception occurs, and the remaining exception handling tasks are completely handed over to the programmer. As can be seen from the above table, the exception vector is a fixed memory address. We can complete the exception handling by writing a jump instruction to this address and letting it jump to the entry of the exception handler we define ourselves.

Exception Vector Table

It is because of the existence of the exception vector table that the hardware exception handling and the programmer's custom handler are organically linked. The address 0x00000000 in the exception vector table is the reset exception. The reason why it is address 0 is because the CPU automatically loads instructions from address 0 when it is powered on. It can be seen that installing the reset exception at this address is also designed to be connected before and after. I have to sigh at the greatness of the CPU designer. Behind it are the other 7 exception vectors. Each exception vector occupies four bytes, which is exactly the size of an instruction. The last exception is the fast interrupt exception. Installing it here also has its significance. The fast interrupt handler can be directly stored at the address 0x0000001C without setting a jump instruction. This can save a clock cycle and speed up the fast interrupt processing time.

The memory mapping address 0x00000000 is reserved for the vector table. In some processors, the vector table can be located at the high address 0xFFFF0000 [configurable through coprocessor instructions]. In order to control memory access rights, today's operating systems usually enable virtual memory. After enabling virtual memory, the starting space of the memory is usually the kernel process space and the page table space. The exception vector table can no longer be installed at address 0.

For example, the Cortex-A8 system supports placing the first address of the exception vector table at any address by setting the C12 register of CP15.

2. Install the exception vector table

We can install the exception vector table by simply using the following command:

b reset ; jump into the reset handler

b HandleUndef ; jump into the undefined handler

b HandSWI ; Jump into the soft interrupt handler

b HandPrefetchAbt ; Jump into the prefetch instruction handler

b HandDataAbt ; Jump into the data access abort handler

b HandNoUsed ; Jump into unused program

b HandleIRQ ; Jump into interrupt handler

b HandleFIQ ; Jump into the fast interrupt handler

[1] [2] [3] [4]
Keywords:ARM Reference address:6. Learn ARM from scratch - exception and interrupt handling, exception vector table, swi

Previous article:7. Learn ARM-GNU pseudo instructions, code compilation, and lds usage from scratch
Next article:5. Learn ARM-MRS, MSR, addressing operation, and atomic operation principles from scratch

Recommended ReadingLatest update time:2024-11-16 21:33

Program Status Register (CPSR) in ARM
31 30 29 28 27 ~ 8 7 6 5 4 3 2 1 0 N WITH C IN reserve I F T M4 M3 M2 M1 M0 N Negative/Less Than I IRQ disable WITH Zero F FIQ disable C Carry/Borrow/Extend T State bit IN Overflow M0~4 Mode bits 1. Condition code flag   N,
[Microcontroller]
The management team of Arm China published a joint letter on its internal website, explaining the controversy surrounding Allen's dismissal
The management team of ARM China published a joint letter on the company’s intranet. The following is the content of the joint letter.   Stars, mountains and seas live up to the youth and work hard to forge ahead   Hello everyone, this is a joint letter from the ARM China management team.   As the company's management
[Internet of Things]
The management team of Arm China published a joint letter on its internal website, explaining the controversy surrounding Allen's dismissal
Differences between ARM7 and ARM9
1. ARM7 is a von Neumann structure, and ARM9 is a Harvard structure. The so-called von Neumann structure and Harvard structure mean that the von Neumann structure program (instructions) and data are stored in the same memory, while the Harvard structure program (instructions) and data have their own independent memory.
[Microcontroller]
Technical differences between ARM (RISC) and x86 (CISC)
RISC and CISC, this pair of enemies, have been in constant entanglement since the day they were born. Until today, after years of development, both have opened up a world in their respective fields and penetrated each other. RISC focuses on high performance, high performance power consumption, small size and mobile de
[Microcontroller]
Technical differences between ARM (RISC) and x86 (CISC)
FPGA slave serial configuration scheme design based on ARM is suitable for embedded system applications
1 Introduction ARM (Advanced RISC Machines) can be considered a company, a general term for a type of microprocessor, or a technology. Microprocessor applications based on ARM technology account for more than 75% of the market share of 32-bit RISC microprocessors, and ARM technology is gradually penetrating into all a
[Microcontroller]
FPGA slave serial configuration scheme design based on ARM is suitable for embedded system applications
Realization of embedded CNC system based on ARM and FPGA hardware platform
introduction In the existing CNC systems, the motion controller is designed by using the computer numerical control system solution of industrial control computer plus motion control card. As the overall functions of industrial control computers become more and more complex, the requirements for the size, cost, power
[Microcontroller]
Realization of embedded CNC system based on ARM and FPGA hardware platform
ARM7 LPC2378 remote upgrade ---- ISP and IAP
       Recently I have been learning the remote upgrade of ARM7. Here I would like to share my learning process with you. If there are any mistakes, please point them out so that I can correct them!          ISP (In-System Programming) means that blank devices on a circuit board can be programmed with end-user code wi
[Microcontroller]
ARM7 LPC2378 remote upgrade ---- ISP and IAP
Research and design of monitoring system based on ARM architecture
1 Introduction With the rapid development of my country's economy, the increasing acceleration of urbanization, and the ever-increasing pace of people's lives, more and more people are beginning to feel that their health is getting worse, and many people do not realize it until the disease breaks out. It is
[Medical Electronics]
Research and design of monitoring system based on ARM architecture
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号