嵌入式处理器架构与程式设计.ppt_第1页
嵌入式处理器架构与程式设计.ppt_第2页
嵌入式处理器架构与程式设计.ppt_第3页
嵌入式处理器架构与程式设计.ppt_第4页
嵌入式处理器架构与程式设计.ppt_第5页
已阅读5页,还剩67页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、嵌入式處理器架構與程式設計,王建民 中央研究院 資訊所 2008年 7月,2,Contents,Introduction Computer Architecture ARM Architecture Development Tools GNU Development Tools ARM Instruction Set ARM Assembly Language ARM Assembly Programming GNU ARM ToolChain Interrupts and Monitor,Lecture 10Interrupts and Monitor,4,Outline,Exception

2、 Handling and Software Interrupts ELF: Executable and Linking Format ARM Monitor and Program Loading,5,Normal Program Flow vs. Exception,Normally, programs execute sequentially (with a few branches to make life interesting) Normally, programs execute in user mode Exceptions and interrupts break the

3、sequential flow of a program, jumping to architecturallydefined memory locations In ARM, SoftWare Interrupt (SWI) is the “system call” exception,6,ARM Exceptions,Types of ARM exceptions Reset: when CPU reset pin is asserted undefined instruction: when CPU tries to execute an undefined op-code softwa

4、re interrupt: when CPU executes the SWI instruction prefetch abort: when CPU tries to execute an instruction pre-fetched from an illegal address data abort: when data transfer instruction tries to read or write at an illegal address IRQ: when CPUs external interrupt request pin is asserted FIQ: when

5、 CPUs external fast interrupt request pin is asserted,7,The Programmers Model,Processor Modes (of interest) User: the “normal” program execution mode. IRQ: used for general-purpose interrupt handling. Supervisor: a protected mode for the operating system. The Register Set Registers R0-R15 + CPSR R13

6、: Stack Pointer (by convention) R14: Link Register (hardwired) R15: Program Counter where bits 0:1 are ignored (hardwired),8,Terminology,The terms exception and interrupt are often confused Exception usually refers to an internal CPU event floating point overflow MMU fault (e.g., page fault) trap (S

7、WI) Interrupt usually refers to an external I/O event I/O device request reset In the ARM architecture manuals, the two terms are mixed together,9,What do SWIs do?,SWIs (often called software traps) allow a user program to “call” the OS that is, SWIs are how system calls are implemented. When SWIs e

8、xecute, the processor changes modes (from User to Supervisor mode on the ARM) and disables interrupts.,10,SWI Example,Types of SWIs in ARM Angel (axd or armsd) SWI_WriteC(SWI 0) Write a byte to the debug channel SWI_Write0(SWI 2) Write the nullterminated string to debug channel SWI_ReadC(SWI 4) Read

9、 a byte from the debug channel SWI_Exit(SWI 0 x11) Halt emulation this is how a program exits SWI_EnterOS(SWI 0 x16) Put the processor in supervisor mode SWI_Clock(SWI 0 x61) Return the number of centiseconds SWI_Time(SWI 0 x63) Return the number of secs since Jan. 1, 1970,11,What happens on an SWI?

10、1,The ARM architecture defines a Vector Table indexed by exception type One SWI, CPU does the following: PC 0 x08 Also, sets LR_svc, SPSR_svc, CPSR (supervisor mode, no IRQ),12,What happens on an SWI?2,Not enough space in the table (only one instruction per entry) to hold all of the code for the SWI

11、 handler function This one instruction must transfer control to appropriate SWI Handler Several options are presented in the next slide,13,“Vectoring” Exceptions to Handlers,Option of choice: Load PC from jump table (shown below) Another option: Direct branch (limited range),14,What happens on SWI c

12、ompletion?,Vectoring to the S_Handler starts executing the SWI handler When the handler is done, it returns to the program at the instruction following the SWI MOVS restores the original CPSR as well as changing pc,15,How to determine the SWI number?,All SWIs go to 0 x08,16,SWI Instruction Format,Ex

13、ample: SWI 0 x18,17,Executing SWI Instruction,On SWI, the processor (1) copies CPSR to SPSR_SVC (2) set 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 0 x08,LDR r0,lr,#4 BIC r0,r0,#0 xff000000 R0 holds SWI number,MOVS

14、 pc, lr,SWI Handler (S_Handler),18,Jump to “Service Routine”,LDR r0,lr,#4 BIC r0,r0,#0 xff000000 switch (r0) case 0 x00: service_SWI1(); case 0 x01: service_SWI2(); case 0 x02: service_SWI3(); ,MOVS pc, lr,SWI Handler (S_Handler),On SWI, the processor (1) copies CPSR to SPSR_SVC (2) set the CPSR mod

15、e bits to supervisor mode (3) sets the CPSR IRQ to disable (4) stores the value (PC + 4) into LR_SVC (5) forces PC to 0 x08,19,Problem with The Current Handler,On SWI, the processor (1) copies CPSR to SPSR_SVC (2) set the CPSR mode bits to supervisor mode (3) sets the CPSR IRQ to disable (4) stores

16、the value (PC + 4) into LR_SVC (5) forces PC to 0 x08,LDR r0,lr,#4 BIC r0,r0,#0 xff000000 switch (r0) case 0 x00: service_SWI1(); case 0 x01: service_SWI2(); case 0 x02: service_SWI3(); ,MOVS pc, lr,SWI Handler (S_Handler),What was in R0? User program may have been using this register. Therefore, ca

17、nnot just use it must first save it,20,Full SWI Handler,S_Handler: SUB sp, sp, #4 leave room on stack for SPSR STMFD sp!, r0r12, lr store users gp registers MRS r2, spsr get SPSR into gp registers STR r2, sp, #14*4 store SPSR above gp registers MOV r1, sp pointer to parameters on stack LDR r0, lr, #

18、4 extract the SWI number BIC r0,r0,#0 xff000000 get SWI # by bit-masking BL C_SWI_handler go to handler (see next slide) LDR r2, sp, #14*4 restore SPSR (NOT “sp!”) MSR spsr_csxf, r2 csxf flags LDMFD sp!, r0r12, lr unstack users registers ADD sp, sp, #4 remove space used to store SPSR MOVS pc, lr ret

19、urn from handler,gp = general-purpose,SPSR is stored above gp registers since the registers may contain system call parameters (sp in r1),21,C_SWI_Handler,void C_SWI_handler(unsigned number, unsigned *regs) switch (number) case 0: /* SWI number 0 code */ break; case 1: /* SWI number 1 code */ break;

20、 . case 0 x100: puts(“SWI 0 x100 trigged!n”); break; . case XXX: /* SWI number XXX code */ break; default: /* end switch */ /* end C_SWI_handler() */,22,Loading the Vector Table,/* For 18-349, the Vector Table will use the LDR PC, PC, * offset springboard approach */ unsigned Install_Handler(unsigne

21、d int routine, unsigned int *vector) unsigned int pcload_instr, old_handler, *soft_vector; pcload_instr = *vector; /* read the Vector Table instr (LDR .) */ pcload_instr ,23,.text .align 2 .global trigger trigger: STMFD sp!, lr SWI #0 x100 LDMFD sp!, pc,extern void S_Handler(); extern void trigger()

22、; int main() unsigned *swivec = (unsigned *) 0 x08; unsigned backup; backup = Install_Handler (unsigned) S_Handler, swivec); trigger(); Install_Handler (backup, swivec); ,Example: SWI Application,24,Exercise #3,Write a service routine that receives a file name from a trigger and display the first li

23、nes of the file on the screen. Void service101(char *filename); Write a trigger that pass a file name as an argument to the above service routine through SWI #0 x101. void trigger101(char *filename); Write a main program to perform a demonstration.,25,Outline,Exception Handling and Software Interrup

24、ts ELF: Executable and Linking Format ARM Monitor and Program Loading,26,Introduction to ELF,Executable and Linking Format Developed by Unix System Lab. Default binary format on Linux, Solaris 2.x, etc Some of the capabilities of ELF are dynamic linking, dynamic loading, imposing runtime control on

25、a program, and an improved method for creating shared libraries. The ELF representation of control data in an object file is platform independent.,27,Three Types of ELF Files,Relocatable file describes how it should be linked with other object files to create an executable file or shared library. Ex

26、ecutable file supplies information necessary for the operating system to create a process image suitable for executing the code and accessing the data contained within the file. Shared object file contains information needed in both static and dynamic linking.,28,ELF File Format,Two views for each o

27、f the three file types. Linking view and execution view These views support both the linking and execution of a program. Linking view is partitioned by sections. Execution view is partitioned by segments. The ELF access library, libelf, provides tools to extract and manipulate ELF object files.,29,E

28、LF File Format (cont.),Linking View Execution View,30,Example: readelf,We can use “readelf” to output ELF information Example use “-e” option to read all header from the executable file of “hello.c”,$ cat hello.c /* hello.c, a simple example program */ #define GREETING Hello, World!n int main() puts

29、(GREETING); $ arm-elf-gcc o hello.elf hello.c $ arm-elf-readelf e hello.elf,31,Example: ELF Header,ELF Header: Magic: 7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2s complement, little endian Version: 1 (current) OS/ABI: ARM ABI Version: 0 Type: EXEC (Executable file) Machine:

30、ARM Version: 0 x1 Entry point address: 0 x8100 Start of program headers: 52 (bytes into file) Start of section headers: 168152 (bytes into file) Flags: 0 x202, has entry point, GNU EABI, software FP Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 1 Size

31、 of section headers: 40 (bytes) Number of section headers: 25 Section header string table index: 22,32,Example: Section Header,Section Headers: Nr Name Type Addr Off Size ES Flg Lk Inf Al 0 NULL 00000000 000000 000000 00 0 0 0 1 .init PROGBITS 00008000 008000 000020 00 AX 0 0 4 2 .text PROGBITS 0000

32、8020 008020 0030e8 00 AX 0 0 4 3 .fini PROGBITS 0000b108 00b108 00001c 00 AX 0 0 4 4 .rodata PROGBITS 0000b124 00b124 000020 00 A 0 0 4 5 .data PROGBITS 0000b244 00b244 00092c 00 WA 0 0 4 6 .eh_frame PROGBITS 0000bb70 00bb70 000004 00 A 0 0 4 7 .ctors PROGBITS 0000bb74 00bb74 000008 00 WA 0 0 4 8 .d

33、tors PROGBITS 0000bb7c 00bb7c 000008 00 WA 0 0 4 9 .jcr PROGBITS 0000bb84 00bb84 000004 00 WA 0 0 4 10 .bss NOBITS 0000bb88 00bb88 00010c 00 WA 0 0 4 11 .comment PROGBITS 00000000 00bb88 000288 00 0 0 1 12 .debug_aranges PROGBITS 00000000 00be10 000420 00 0 0 8 13 .debug_pubnames PROGBITS 00000000 0

34、0c230 000726 00 0 0 1 14 .debug_info PROGBITS 00000000 00c956 011f48 00 0 0 1 15 .debug_abbrev PROGBITS 00000000 01e89e 0031f4 00 0 0 1 16 .debug_line PROGBITS 00000000 021a92 002a14 00 0 0 1 17 .debug_frame PROGBITS 00000000 0244a8 000a14 00 0 0 4 18 .debug_str PROGBITS 00000000 024ebc 001406 01 MS

35、 0 0 1 19 .debug_loc PROGBITS 00000000 0262c2 002be0 00 0 0 1 20 .stack PROGBITS 00080000 028ea2 000000 00 W 0 0 1 21 .debug_ranges PROGBITS 00000000 028ea2 000150 00 0 0 1 22 .shstrtab STRTAB 00000000 028ff2 0000e3 00 0 0 1 23 .symtab SYMTAB 00000000 0294c0 001590 10 24 ef 4 24 .strtab STRTAB 00000

36、000 02aa50 0007f9 00 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific),33,Example: Program Header,Program Headers: Type Offset VirtAddr PhysAddr FileS

37、iz MemSiz Flg Align LOAD 0 x008000 0 x00008000 0 x00008000 0 x03b88 0 x03c94 RWE 0 x8000 Section to Segment mapping: Segment Sections. 00 .init .text .fini .rodata .data .eh_frame .ctors .dtors .jcr .bss,34,Data Representation,Support various processors with 8-bit bytes and 32-bit architectures. Int

38、ended to be extensible to larger or smaller architecture.,35,ELF Header1,It is always the first section of the file. Describes the type of the object file . Its target architecture, and the version of ELF it is using. The location of the Program Header table, Section Header table, and String table a

39、long with associated number and size of entries for each table are also given. Contains the location of the first executable instruction.,36,ELF Header2,#define EI_NIDENT 16 typedef struct unsigned chare_identEI_NIDENT;/ file ID, interpretation Elf32_Halfe_type; / object file type Elf32_Halfe_machin

40、e; / target architecture Elf32_Worde_version; / ELF version Elf32_Addre_entry; / starting virtual address Elf32_Offe_phoff; / file offset to program header Elf32_Offe_shoff; / file offset to section header Elf32_Worde_flags; / processor-specific flags Elf32_Halfe_ehsize; / the ELF headers size Elf32

41、_Halfe_phentsize; / program header entry size Elf32_Halfe_phnum; / program header entry number Elf32_Halfe_shentsize; / section header entry size Elf32_Halfe_shnum; / section header entry number Elf32_Halfe_shtrndx; / section header index for string Elf32_Ehdr;,37,Section Header,The section header t

42、able is an array of structures. A section header table index is a subscript into this array. Each entry correlates to a section in the file. The entry provides the name, type, memory image starting address, file offset, the sections size in bytes, alignment.,38,The Section Header Table,typedef struc

43、t Elf32_Wordsh_name;/ name of section, an index Elf32_Wordsh_type;/ type of section Elf32_Wordsh_flags;/ section-specific attributes Elf32_Addrsh_addr;/ memory location of section Elf32_Offsh_offset;/ file offset to section Elf32_Wordsh_size;/ size of section Elf32_Wordsh_link;/ section type, depend

44、ent Elf32_Wordsh_info;/ extra information, dependent Elf32_Wordsh_addralign;/ address alignment Elf32_Wordsh_entsize;/ size of an entry in section Elf32_Shdr;,39,ELF Sections,A number of types of sections described by entries in the section header table. Sections can hold executable code, data, dyna

45、mic linking information, debugging data, symbol tables, relocation information, comments, string tables, and notes.,40,Special Sections1,Various sections in ELF are pre-defined. A list of special sections .bssun-initialized data .commentversion control information .data and .data1initialized data pr

46、esent .debuginformation for symbolic debugging .dynamicdynamic linking information .dynstrstrings needed for dynamic linking .hashsymbol hash table .lineline number information for debugging,41,Special Sections2,A list of special sections (cont.) .notefile notes .relname and .relaname relocation dat

47、a .rodata and .rodata1 read-only data .shstrtabsection names .strtabthe strings that represent the names associated with symbol table entries .symtabsymbol table .textexecutable instructions,42,String Table,The object file uses these strings to represent symbol and section names. The first and last

48、byte is defined to hold a null character. An empty string table section is permitted. Ex:,43,Symbol Table,Holds information needed to locate and relocate a programs symbolic definitions and references. A symbol table entry,typedef struct Elf32_Wordst_name; / symbol name, an index Elf32_Addrst_value;

49、 / symbol value Elf32_Wordst_size; / symbol size unsigned charst_info; / symbols type and binding attributes unsigned charst_other; / symbol visibility Elf32_Halfst_shndx; / relevant section header table index Elf32_Sym;,44,Program Header,Program headers are meaningful only for executable and shared

50、 object files. The program header table is an array of structures, each describing a segment or other information. An object file segment contains one or more sections. A file specifies its own program header size with the ELF headers e_phentsize / type of the segment Elf32_Offp_offset;/ file offset

51、 to segment Elf32_Addrp_vaddr;/ virtual address of first byte Elf32_Addrp_paddr;/ segments physical address Elf32_Wordp_filesz; / size of file image of segment Elf32_Wordp_memsz;/ size of memory image of segment Elf32_Wordp_flags;/ segment-specific flags Elf32_Wordp_align;/ alignment requirements El

52、f32_Phdr;,46,Executable Programs,A program to be loaded by the system must have at least one loadable segment. Segments are a way of grouping related sections. A process image is created by loading and interpreting segments. Segment contents A segment comprises one or more sections. Text segments co

53、ntain read-only instructions and data. Data segments contain writable data and instructions,47,ELF Segments,Text segment example,Data segment example,48,Exercise #4,Write a service routine that receives the name of an ELF executable file as a parameter and display the offset of program header on the

54、 screen. Void service102(char *filename); Write a trigger that pass a file name to the above service routine through SWI #102. void trigger102(char *filename); Write a main program to perform a demonstration.,49,Outline,Exception Handling and Software Interrupts ELF: Executable and Linking Format AR

55、M Monitor and Program Loading,50,Overview of ARM Debug Monitor,The ARM Debug Monitor is called “Angel” (earlier versions called it the “Demon” get it?) Provides lowlevel programming C library and debugging environment When the X-board first boots, they load the demon from flash memory (emulator pret

56、ends that this happens) This activity is called “bootstrapping”,51,Memory Map of Demon,0 x0000CPU reset vector 0 x0004.0 x1c CPU undefined instruction . CPU Fast Interrupt Vector 0 x00201K Bytes for FIQ and FIQ mode stack 0 x0400256 bytes for IRQ mode stack 0 x0500256 bytes for Undefined mode stack

57、0 x0600256 bytes for Abort mode stack 0 x0700256 bytes for SVC mode stack 0 x0800Debug monitor private workspace 0 x1000Free for user-supplied Debug Monitor 0 x2000Floating Point Emulation Space 0 x8000Application Space top of memory SWI_Getenv returns top of memory = 0 x08000000,52,Monitor Program,

58、Provide Capability to Setup Hardware on startup Load and run programs Debug code Minimal OS functionality Many embedded systems are just Monitor + application Monitor still handles other types of interrupts (well cover this later) l timer, I/O (e.g., keypad, switches, LED, LCD),53,Example System,Int

59、errupt from external devices keyboards, timers, disk drives,We refer to each piece of software as a process Codes Program counter Registers Stack Other terms Task Thread,54,Debug Monitor SWIs,Angel provides a number of SWIs that you can use SWI_WriteC (0) Write a byte to the debug channel SWI_Write0(2) Write the null-terminated st

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论