arm software development

60
1 Embedded System:ARM Software Development ARM Software Development

Upload: nuri

Post on 31-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

ARM Software Development. ARM Software Development Platform. ARMulator An instruction set simulator for various ARM processors. Support a full ANSI C library to allow complete C programs to run on the simulated system. The main modules are A model of the ARM Processor core - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ARM Software Development

1

Embedded System:ARM Software Development

ARM Software Development

Page 2: ARM Software Development

2

Embedded System:ARM Software Development

ARM Software Development Platform• ARMulator

– An instruction set simulator for various ARM processors.– Support a full ANSI C library to allow complete C programs

to run on the simulated system.– The main modules are

• A model of the ARM Processor core• A model of the memory used by the processor (armmap)

– Some other predefined modules• Model additional hardware, such as a coprocessor or

peripherals.• Model pre-installed software, such as a C library,

semihosting SWI handler, or an operating system.• Provide debugging or benchmarking information

– You can write your own modules for the above tasks.• ARM Development Boards

Page 3: ARM Software Development

3

Embedded System:ARM Software Development

Configuring ARMulator

• Related configuration files– default.ami

– peripherals.ami

– example.ami

• What you can do?– Configuring tracer

– Configuring profiler

– Configuring memory model

– Configuring ARMulator to use the peripheral models

• Chapter 2 of Debug Target Guide

Page 4: ARM Software Development

4

Embedded System:ARM Software Development

Writing ARMulator Models

• Supplied models– Basic models

• tracer.c, profiler.c, pagetab.c, stackuse.c, nothing.c, semihost.c, dcc.c, mapfile.c

– Peripheral models

• intc.c, timer.c, millisec.c, watchdog.c, tube.c

• Writing your own models– Using the API provided in Chapter 4 of Debug Target Guide

to develop the new model.

– Configuring ARMulator to use new models

• Chapter 3.4 of Debug Target Guide

Page 5: ARM Software Development

5

Embedded System:ARM Software Development

Semihosting

• Provides code running on an ARM target use of facilities on a host computer that is running an ARM debugger.– These include the keyboard input, screen output, and disk I/O.

– To allow functions in the C library, such as printf() and scanf(), to use the screen and keyboard of the host.

• Is implemented by a set of defined software interrupt (SWI) operations. – In many case, the semihosting SWI will be invoked by code

within library functions.

Page 6: ARM Software Development

6

Embedded System:ARM Software Development

Overview of Semihosting

Page 7: ARM Software Development

7

Embedded System:ARM Software Development

SWI Interface

• SWI instructions contain a field that encode the SWI number used by application code.

• The SWI number used for semihosting– 0x123456 in ARM state– 0xAB in Thumb state

• To distinguish between operations, operation type is passed in r0.– 0x00 to 0x31 used by ARM– 0x32 to 0xFF reserved for future use by ARM– 0x100 to 0x1FF reserved for user applications– Example

• 0x01 SYS_OPEN• 0x02 SYS_CLOSE

Page 8: ARM Software Development

8

Embedded System:ARM Software Development

ARM-Thumb Procedure Call Standard(ATPCS)

• It defines register usage and stack conventions that must be followed in order to enable separately compiled or assembled subroutines to work together.

• It describes a contract between a calling and a called routine:– Obligations on the caller to create a memory state in which the

called routine may start to execute.

– Obligations on the called routine to preserve the memory-state of the caller across the call.

– The rights of the called routine to alter the memory-state of its caller.

Page 9: ARM Software Development

9

Embedded System:ARM Software Development

ATPCS (2)

• Specifies a family of Procedure Call Standard (PCS) Variants, generated by a cross product of user choices that reflect alternative priorities among:– Code size

– Performance

– Functionality (Ex. Ease of debugging, run-time checking, …)

• Contains– A machine-level base standard.

– A set of machine-level variants.

– Constraints on the layout of activation records and function entry sequences to support stack unwinding.

– The representation of externally visible C-language and C++ extern “C”{…} entities.

Page 10: ARM Software Development

10

Embedded System:ARM Software Development

Base Standard (1)

• Defines– a machine-level, integer-only calling standard common to

ARM and Thumb, and

– a machine-level floating-point standard for ARM.

• The base standard does not provide for– Interworking between ARM state and Thumb state.

– Position independence of either data or code.

– Re-entry to routines with independent data for each invocation.

– Stack checking.

• They are specified by the PCS variants.

Page 11: ARM Software Development

11

Embedded System:ARM Software Development

Base Standard (2)• Machine registers

Page 12: ARM Software Development

12

Embedded System:ARM Software Development

Base Standard (3)

• Floating point registers– FPA architecture

• Eight floating-point registers, f0-f7

• Each may hold a value of single, double or extended precision.

– VFP architecture

• Sixteen double-precision registers, d0-d15

• each may also be used to hold two single precision numbers.

• No extended precision

Page 13: ARM Software Development

13

Embedded System:ARM Software Development

Base Standard (4)

• Routine call can be implemented by any instruction sequence that has the effect:

LR <--- Return address

PC <--- Destination address

– A subroutine call preserves the values of r4-r11 and SP

• Parameter Passing– Types

• 32-bit integers

• Single-precision floating-point numbers

• Double-precision floating-point numbers

Page 14: ARM Software Development

14

Embedded System:ARM Software Development

Base Standard (4)

• Parameter Passing (cont.)– A source language parameter value is converted to a machine

parameter value as follows:

• An integer value narrower than 32 bits: widen to 32 bits

• A 64-bit integer: treated as two 32-bit integer values

• A floating-point value

– One or more floating-point values of the corresponding machine types if floating point hardware exists.

– A sequence of integer machine words if floating-point operation is simulated.

• Others

– Converted to a sequence of 32-bit integers

Page 15: ARM Software Development

15

Embedded System:ARM Software Development

Base Standard (5)

• Parameter passing (cont.)– Variadic routines (passing variable number of parameters)

• Loading the first 4 words into integers a1-a4 (lowest addressed into a1).

• Pushing remaining words onto the stack in reverse order.

– A floating point value can be passed in integer registers, or can be split between an integer register and memory.

– Non-variadic routines

• The first N floating-point values are assigned to floating-point argument registers.

• The rest is the same as Variadic routine.

Page 16: ARM Software Development

16

Embedded System:ARM Software Development

Base Standard (6)

• Result Return– A procedure returns no result.– A function returns a single value; a value occupies 1 or more

words• Integer function

– Must return a 1-word value in a1– May return a value of length 2-4 words in a1-a2, a1-a3,

a1-a4.– Must return a longer value indirectly in memory.

• Floating-point function

• FPA procedure call standard• VFP procedure call standard• No floating-point hardware procedure call standard

Page 17: ARM Software Development

17

Embedded System:ARM Software Development

Standard Variants (1)

• ARM and Thumb interworking

• Read-only position-independence (ROPI)– A read-only segment of a program can be loaded and used at

any address

• Read-write position-independence (RWPI)– A read-write segment of a program can be loaded and

executed at any address.

• Re-entrant code– A program which is RWPI is also re-entrant.

• Shared libraries– A shared library is a re-entrant program to which other

programs may link at execution time.

Page 18: ARM Software Development

18

Embedded System:ARM Software Development

Standard Variants (2)

• Stack limit checking– SL (Stack limit) must point at least 256 bytes above the lowest

usable address in the stack.

– SL must not be altered by code compiled or assembled with stack limit checking selected.

– The value held SP (stack pointer) must always greater than or equal to the value in SL.

Page 19: ARM Software Development

19

Embedded System:ARM Software Development

Interworking ARM and Thumb (1)

• Interworking– Mixing ARM and Thumb code as you wish.– The ARM linker alters call and return instructions, or insert

small code sections called veneers, to change processor states as necessary when it detects a state change exist.

– Why interworking?• Thumb state provides good code density, but we need the

application running in ARM state because of – Speed– Functionality (enable or disable interrupts can only be

done in ARM state)• Exception handling• A Thumb-capable ARM processor always starts in ARM

state

Page 20: ARM Software Development

20

Embedded System:ARM Software Development

Interworking ARM and Thumb (2)

• Use –apcs/interwork options for assembler and compilers– The compiler or assembler records an interworking attribute in the

object file.

– The linker provides interworking veneers for subroutine entry.

– In assembly language, you must write function exit code that returns to the instruction set state of the caller.

– In C or C++, the compiler creates function exit code.

• When to use? If your object file contains:– Thumb subroutines that might need to return to ARM state.

– ARM subroutines that might need to return to Thumb state.

– Thumb subroutines that might make indirect or virtual calls to ARM state.

– ARM subroutines that might make indirect or virtual calls to Thumb state.

Page 21: ARM Software Development

21

Embedded System:ARM Software Development

Assembly Language interworking

• Instructions perform processor state change– BX Rn

• The value of bit 0 of the branch address determines the processor state

– If bit 0 is set, the instructions at the branch address are executed in Thumb state.

– If not, the instructions at the branch address are executed in ARM state

– BLX, LDR, LDM, and POP (ARM architecture v5 only)

• Assembly directives– CODE16

– CODE32

Page 22: ARM Software Development

22

Embedded System:ARM Software Development

Assembly Language interworking-Example

Page 23: ARM Software Development

23

Embedded System:ARM Software Development

C and C++ Interworking-Example

Page 24: ARM Software Development

24

Embedded System:ARM Software Development

Veneer Creation by Compiler

Page 25: ARM Software Development

25

Embedded System:ARM Software Development

Assembly Language interworking Using Veneers - Example

Page 26: ARM Software Development

26

Embedded System:ARM Software Development

Veneers Creation by Linker

Page 27: ARM Software Development

27

Embedded System:ARM Software Development

Mixing C, C++ and Assembly Language

• In-line assembler

• Accessing C global variables

• Using C header files from C++

• Calling between C, C++, and ARM assembly language

Page 28: ARM Software Development

28

Embedded System:ARM Software Development

In-line Assembler

Page 29: ARM Software Development

29

Embedded System:ARM Software Development

Accessing C global variables

• To access a global variable, use the IMPORT directive to import the global and then load the address into a register.

Page 30: ARM Software Development

30

Embedded System:ARM Software Development

Handling Processor Exceptions

• Why normal processor execution interrupted?– To handle some events such as

• Externally generated interrupts

• An attempt by the processor to execute an undefined instruction

• Accessing privileged operating system functions

• Others

– Basic rules of handling exceptions

• After handling the exception, the processor execution can be resumed with a previous processor status.

Page 31: ARM Software Development

31

Embedded System:ARM Software Development

Exception Types

Page 32: ARM Software Development

32

Embedded System:ARM Software Development

Exception Priorities

Page 33: ARM Software Development

33

Embedded System:ARM Software Development

Entering an Exception

• When an exception is generated, the processor takes the following actions:– Copies the CPSR into SPSR for the mode in which the

exception is to be handled.

– Change the appropriate CPSR mode bits in order to

• Change to the appropriate mode, and map in the appropriate banked registers for that mode.

• Disable interrupts.

– IRQs are disabled when any exception occurs.

– FIQs are disabled when a FIQ occurs, and on reset.

• Set lr_mode to the return address

• Set PC to the vector address for the exception.

Page 34: ARM Software Development

34

Embedded System:ARM Software Development

Returning from an Exception

• The actions taken by the processor– Restore the CPSR from SPSR_mode

– Restore the PC using return address stored in lr_mode

• The way to return depends on whether a stack is used during entering the subroutine– Without a stack

• Performing a data processing instruction with S flag set and the PC as the destination register.

– With a stack

• Restoring the saved registers by performing

– LDMFD sp!, {r0-r12,pc}^

– ^ indicates that the CPSR is restored from the SPSR

Page 35: ARM Software Development

35

Embedded System:ARM Software Development

Returning from SWI and Undefined Instruction Handlers

• SWI and UI exceptions are generated by the instruction itself, so the PC is not updated when the exception is taken. Thus, storing (PC-4) in lr_mode makes lr_mode points to the next instruction be executed.

SWI xxx being executed PC-8INST-1 being decoded PC-4INST-2 being fetched PC

• Restoring the PC from lr – Without a stack

MOVS pc, lr– With a stack

STMFD sp!, {reglist,lr}…LDMFD sp!, {reglist, pc}^

Page 36: ARM Software Development

36

Embedded System:ARM Software Development

Returning from FIQ and IRQ

• Check IRQ and FIQ at the end of executing each instruction.INST-1 being executed PC-12 , IRQ or FIQ checked

INST-2 being decoded PC-8

INST-3 being fetched PC-4

INST-4 PC

• Restoring PC from lr– Without a stack

SUBS pc, lr, #4

– With a stack

SUB lr, lr, #4

STMFD sp!, {reglist, lr}

LDMFD sp!, {reglist, pc}^

Page 37: ARM Software Development

37

Embedded System:ARM Software Development

Returning from Prefetch Abort

• Prefetch abort is generated when it reaches the execution stage.INST-1 being executed PC-8 , AbortedINST-2 being decoded PC-4INST-3 being fetched PC

• Restoring PC from lr– Without a stack

SUBS pc, lr, #4– With a stack

SUB lr, lr, #4STMFD sp!, {reglist, lr}…LDMFD sp!, {reglist, pc}^

Page 38: ARM Software Development

38

Embedded System:ARM Software Development

Returning from Data Abort• When a data abort occurs, the program counter has been updated.

INST-1 being executed PC-12 , AbortedINST-2 being decoded PC-8INST-3 being fetched PC-4INST-4 PC

• Restoring PC from lr– Without a stack

SUBS pc, lr, #8– With a stack

SUB lr, lr, #8STMFD sp!, {reglist, lr}…LDMFD sp!, {reglist, pc}^

Page 39: ARM Software Development

39

Embedded System:ARM Software Development

Install an Exception Handler

• Exception handlers can be reached by the instruction contained in each entry in the vector table:– A branch instruction

• PC relative

– A load PC instruction

• The absolute address of a handler must be stored in a suitable memory location.

Page 40: ARM Software Development

40

Embedded System:ARM Software Development

Installing the Handlers at Reset (1)• If the ROM is at location 0x0 in memory (load PC can

be replaced by branch)

Page 41: ARM Software Development

41

Embedded System:ARM Software Development

Installing the Handlers at Reset (2)

• If the RAM is at location 0x0 in memory

Page 42: ARM Software Development

42

Embedded System:ARM Software Development

Installing the Handler from C (1)

• Branch Method1. Take the address of the exception handler.

2. Subtract the address of the corresponding vector.

3. Subtract 0x8 to allow for prefetching.

4. Shift the result to the right by two to give a word offset, rather than a byte offset.

5. Test the top eight bits of this are clear.

6. Logically OR this with 0xEA000000 (the Opcode for the branch instruction) to produce the value to be placed in the vector.

Page 43: ARM Software Development

43

Embedded System:ARM Software Development

Installing the Handler from C (2)• Example for branch method

Page 44: ARM Software Development

44

Embedded System:ARM Software Development

Installing the Handler from C (3)• Load PC method

Steps 1, 2, and 3 are the same as branch method.

4. Logically OR this with 0xE59FF000 (the Opcode for LDR pc, [pc, #offset]) to produce the value to be placed in the vector.

5. Push the address of the handler into the storage location

• Example

Page 45: ARM Software Development

45

Embedded System:ARM Software Development

SWI Handlers

• ARM SWI instruction

• Work to be done by the handler– First load the SWI instruction into a register by LDR r0, [lr, # -

4]• Why?

SWI being executed PC-8INST-1 decoded PC-4INST-2 fetched PC

– Obtain the SWI number by BIC r0, r0, #0xFF000000

Page 46: ARM Software Development

46

Embedded System:ARM Software Development

SWI Handlers - Example

Page 47: ARM Software Development

47

Embedded System:ARM Software Development

SWI Handlers in Assembly Language

• According to the value in r0, jump to the corresponding handler.

Page 48: ARM Software Development

48

Embedded System:ARM Software Development

SWI Handlers in C and Assembly Lang.

• Add the following line to the SWI_handler routine in page 46.BL C_SWI_handler

Page 49: ARM Software Development

49

Embedded System:ARM Software Development

Using SWIs in Supervisor Mode• Must store lr_svc and spsr_svc

Page 50: ARM Software Development

50

Embedded System:ARM Software Development

Calling SWIs from an Application (1)

• Use __swi directive. This allows a SWI to be compiled inline, without overhead, if– Any arguments are passed in r0-r3 only, and

– Any results are returned in r0-r3 only.

• If more than four results are returned, we have to use a struct to store the results and use the _ _value_in_regs directive.

Page 51: ARM Software Development

51

Embedded System:ARM Software Development

Calling SWIs from an Application (2)

Page 52: ARM Software Development

52

Embedded System:ARM Software Development

Calling SWIs from an Application (3)

Page 53: ARM Software Development

53

Embedded System:ARM Software Development

Simple Interrupt Handlers in C (1)

• IRQ and FIQ handlers

• Use _ _ irq keyword that– Preserves all APCS corruptible registers

– Preserves all other registers used by the function

– Exits the function by setting the program counter to lr-4 and restoring the CPSR to its original value.

Page 54: ARM Software Development

54

Embedded System:ARM Software Development

Simple Interrupt Handlers in C (2)

• After compilation

Page 55: ARM Software Development

55

Embedded System:ARM Software Development

Reentrant Interrupt Handlers (1)

• The following steps are needed to safely re-enable interrupts in an IRQ (FIQ) handler:

Page 56: ARM Software Development

56

Embedded System:ARM Software Development

Reentrant Interrupt Handlers (2)

Page 57: ARM Software Development

57

Embedded System:ARM Software Development

Operations Done by Reset Handlers

• For example– Set up exception vectors.

– Initialize stacks and registers.

– Initialize the memory system, if using MMU.

– Initialize any critical I/O devices.

– Enable interrupts.

– Change processor mode and/or state.

– Initialize variables required by C and call the main application.

Page 58: ARM Software Development

58

Embedded System:ARM Software Development

Undefined Instruction Handlers

• It is often used to emulate a coprocessor. Such an emulator must:– Attach itself to the Undefined Instruction vector.

– Examine the undefined instruction to see if it should be emulated.

– Otherwise the emulator must pass the exception onto the original handler using the vector stored when the emulator was installed.

Page 59: ARM Software Development

59

Embedded System:ARM Software Development

Prefetch Abort Handler or Data Abort

• If no MMU, report error. Otherwise, the related handler is executed to deal with the virtual memory fault.

• Depending on whether the instruction that causes abort is re-executed or not, the return address should be properly set. – Prefetch abort

• INST-aborted executed PC-8

• INST-1 decoded PC-4

• INST-2 fetched PC

Page 60: ARM Software Development

60

Embedded System:ARM Software Development

Handling Exceptions on Thumb-capable Processors