home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / documentation / documents / a252armc < prev    next >
Internet Message Format  |  1999-04-27  |  22KB

  1. From: athomas (Alasdair Thomas)
  2. Subject: Re: 32bit immediate load in ARM code
  3. Date: 17 May 91 10:37:14 GMT
  4. Sender: athomas@armltd.uucp
  5. Organization: A.R.M. Ltd, Swaffham Bulbeck, Cambs, UK
  6.  
  7.  
  8.  
  9.                  IMPORTANT RULES FOR ARM CODE WRITERS
  10.                   ====================================
  11.   Date: 17/5/91
  12.  Issue: 2.5
  13.  
  14.  Every effort has been made to ensure that the information in this document
  15. is true and correct at the date of issue. Products described in this
  16. document, however, are subject to continuous development and improvements
  17. and Advanced RISC Machines Ltd (and other contributors) reserve the right to
  18. change their specifications at any time. Advanced RISC Machines Ltd cannot
  19. accept liability for any loss or damage arising from the use of any
  20. information or particulars in this document.
  21.  
  22.  
  23.                           ================
  24.                           = Introduction =
  25.                           ================
  26.  The ARM processor family uses Reduced Instruction Set (RISC) techniques to
  27. maximise performance; as such, the instruction set allows some instructions
  28. and code sequences to be constructed that will give rise to unexpected (and
  29. potentially erroneous) results. These cases must be avoided by all machine
  30. code writers and generators if correct program operation across the whole
  31. range of ARM processors is to be obtained.
  32.  
  33.  In order to be upwards compatible with future versions of the ARM processor
  34. family NEVER use any of the undefined instruction formats: both those shown
  35. in the manual as "Undefined" which the processor traps AND those which are
  36. not shown in the manual and which don't trap (for example a Multiply
  37. instruction where bit 5 or 6 of the instruction is set). In addition the
  38. "NV" (never executed) instruction class should not be used [It is
  39. recommended that the instruction "MOV R0,R0" be used as a general purpose
  40. NOP].
  41.  
  42.  This document lists the instruction code sequences to be avoided. It is
  43. *STRONGLY* recommended that you take the time to familiarise yourself with
  44. these cases because some will only fail under particular circumstances which
  45. may not arise during testing.
  46.  
  47.  
  48.               ============================================
  49.               = Instructions and code sequences to avoid =
  50.               ============================================
  51.  The instructions and code sequences are split into a number of categories.
  52. Each category starts with a recommendation or warning, and indicates which
  53. of the two main ARM variants (ARM2, ARM3) it applies to. The text then goes
  54. on to explain the conditions in more detail and to supply examples where
  55. appropriate.
  56.  
  57.  Unless a program is being targeted SPECIFICALLY for a single version of the
  58. ARM processor family, all of these recommendations should be adhered to.
  59.  
  60.  
  61. 1) TSTP/TEQP/CMPP/CMNP: Changing mode
  62. -------------------------------------
  63.   ####################################################################
  64.   # When the processor's mode is changed by altering the mode bits   #
  65.   # in the PSR using a data processing operation, care must be taken #
  66.   # not to access a banked register (R8-R14) in the following        #
  67.   # instruction. Accesses to the unbanked registers (R0-R7,R15) are  #
  68.   # safe.                                                            #
  69.   ####################################################################
  70.   # Applicability: ARM2                                              #
  71.   ####################################################################
  72.  
  73.  The following instructions are affected, but note that mode changes can
  74. only be made when the processor is in a non-user mode:-
  75.  
  76.    TSTP Rn,<Op2>
  77.    TEQP Rn,<Op2>
  78.    CMPP Rn,<Op2>
  79.    CMNP Rn,<Op2>
  80.  
  81.  These are the only operations that change all the bits in the PSR
  82. (including the mode bits) without affecting the PC (thereby forcing a
  83. pipeline refill during which time the register bank select logic settles).
  84.  
  85. e.g. Assume processor starts in Supervisor mode in each case:-
  86.  
  87.  a)  TEQP PC,#0
  88.      MOV  R0,R0            SAFE: NOP added between mode change and access
  89.      ADD  R0,R1,R13_usr          to a banked register (R13_usr).
  90.  
  91.  b)  TEQP PC,#0
  92.      ADD  R0,R1,R2         SAFE: No access made to a banked register
  93.  
  94.  c)  TEQP PC,#0
  95.      ADD  R0,R1,R13_usr    *FAILS*: Data NOT read from Register R13_usr!
  96.  
  97.  The safest default is always to add a NOP (e.g. MOV R0,R0) after a mode
  98. changing instruction; this will guarantee correct operation regardless of
  99. the code sequence that follows it.
  100.  
  101.  
  102.  
  103. 2) LDM/STM: Forcing transfer of the user bank (Part 1)
  104. ------------------------------------------------------
  105.   ###################################################################
  106.   # Don't use write back when forcing user bank transfer in LDM/STM #
  107.   ###################################################################
  108.   # Applicability: ARM2,ARM3                                        #
  109.   ###################################################################
  110.  
  111.  For STM instructions the S bit is redundant as the PSR is always stored
  112. with the PC whenever R15 is in the transfer list. In user mode programs the
  113. S bit is ignored, but in other modes it has a second interpretation. S=1 is
  114. used to force transfers to take values from the user register bank instead
  115. of the current register bank. This is useful for saving the user state on
  116. process switches.
  117.  Similarly, in LDM instructions the S bit is redundant if R15 is not in the
  118. transfer list. In user mode programs, the S bit is ignored, but in non-user
  119. mode programs where R15 is not in the transfer list, S=1 is used to force
  120. loaded values to go to the user registers instead of the current register
  121. bank.
  122.  In both cases where the processor is in a non-user mode and transfer
  123. to/from the user bank is forced by setting the S bit, write back of the base
  124. will also be to the user bank though the base will be fetched from the
  125. current bank. Therefore don't use write back when forcing user bank transfer
  126. in LDM/STM.
  127.  
  128. e.g. In all cases, the processor is assumed to be in a non-user mode and
  129.      <Rlist> is assumed not to include R15:-
  130.  
  131.      STMxx Rn!,<Rlist>   SAFE: Storing non-user registers with write back to
  132.                                the non-user base register
  133.  
  134.      LDMxx Rn!,<Rlist>   SAFE: Loading non-user registers with write back to
  135.                                the non-user base register
  136.  
  137.      STMxx Rn,<Rlist>^   SAFE: Storing user registers, but no base
  138.                                write-back
  139.  
  140.      STMxx Rn!,<Rlist>^  *FAILS*: Base fetched from non-user register, but
  141.                                    written back into user register
  142.  
  143.      LDMxx Rn!,<Rlist>^  *FAILS*: Base fetched from non-user register, but
  144.                                    written back into user register
  145.  
  146.  
  147.  
  148.  
  149. 3) LDM: Forcing transfer of the user bank (Part 2)
  150. --------------------------------------------------
  151.   ######################################################################
  152.   # When loading user bank registers with an LDM in a non-user mode,   #
  153.   # care must be taken not to access a banked register (R8-R14) in the #
  154.   # following instruction. Accesses to the unbanked registers          #
  155.   # (R0-R7,R15) are safe.                                              #
  156.   ######################################################################
  157.   # Applicability: ARM2,ARM3                                           #
  158.   ######################################################################
  159.  
  160.  Because the register bank switches from user mode to non-user mode during
  161. the first cycle of the instruction following an "LDM Rn,<Rlist>^", an
  162. attempt to access a banked register in that cycle may cause the wrong
  163. register to be accessed.
  164.  
  165. e.g. In all cases, the processor is assumed to be in a non-user mode and
  166.      <Rlist> is assumed not to include R15:-
  167.  
  168.    LDM Rn,<Rlist>^
  169.    ADD R0,R1,R2             SAFE: Access to unbanked registers after LDM^
  170.  
  171.    LDM Rn,<Rlist>^
  172.    MOV R0,R0                SAFE: NOP inserted before banked register used
  173.    ADD R0,R1,R13_svc              following an LDM^
  174.  
  175.    LDM Rn,<Rlist>^
  176.    ADD R0,R1,R13_svc        *FAILS*: Accessing a banked register immediately
  177.                                      after an LDM^ returns the wrong data!
  178.  
  179.    ADR   R14_svc, saveblock
  180.    LDMIA R14_svc, {R0 - R14_usr}^
  181.    LDR   R14_svc, [R14_svc,#15*4]   *FAILS*: Banked base register (R14_svc)
  182.    MOVS   PC, R14_svc                        used immediately after the LDM^
  183.  
  184.    ADR   R14_svc, saveblock
  185.    LDMIA R14_svc, {R0 - R14_usr}^
  186.    MOV   R0,R0                      SAFE: NOP inserted before banked
  187.    LDR   R14_svc, [R14_svc,#15*4]         register (R14_svc) used
  188.    MOVS   PC, R14_svc
  189.  
  190.  
  191. NOTE:
  192.  The ARM2 and ARM3 processors *usually* give the expected result, but cannot
  193. be guaranteed to do so under all circumstances. Therefore this code sequence
  194. should be avoided in future.
  195.  
  196.  
  197.  
  198. 4) SWI/Undefined Instruction trap interaction
  199. ---------------------------------------------
  200.   ######################################################################
  201.   # Care must be taken when writing an undefined instruction handler   #
  202.   # to allow for an unexpected call from a SWI instruction.            #
  203.   # The erroneous SWI call should be intercepted and redirected to the #
  204.   # software interrupt handler                                         #
  205.   ######################################################################
  206.   # Applicability: ARM2                                                #
  207.   ######################################################################
  208.  
  209.  The implementation of the CDP instruction on ARM2 causes a Software
  210. Interrupt (SWI) to take the Undefined Instruction trap if the SWI was the
  211. next instruction after the CDP.
  212. e.g.
  213.     SIN F0,F1
  214.     SWI &11        *FAILS*: ARM2 will take the undefined instruction trap
  215.                             instead of software interrupt trap.
  216.  
  217.  All Undefined Instruction handler code should check the failed instruction
  218. to see if it is a SWI, and if so pass it over to the software interrupt
  219. handler.
  220.  
  221.  
  222.  
  223.  
  224. 5) Undefined instruction/Prefetch abort trap interaction
  225. --------------------------------------------------------
  226.   ######################################################################
  227.   # Care must be taken when writing the Prefetch abort trap handler to #
  228.   # allow for an unexpected call due to an undefined instruction       #
  229.   ######################################################################
  230.   # Applicability: ARM2,ARM3                                           #
  231.   ######################################################################
  232.  
  233.  When an undefined instruction is fetched from the last word of a page,
  234. where the next page is absent from memory, the undefined instruction will
  235. cause the undefined instruction trap to be taken, and the following
  236. (aborted) instructions will cause a prefetch abort trap. One might expect
  237. the undefined instruction trap to be taken first, then the return to the
  238. succeeding code will cause the abort trap. In fact the prefetch abort has a
  239. higher priority than the undefined instruction trap, so the prefetch abort
  240. handler is entered _before_ the undefined instruction trap, indicating a
  241. fault at the address of the undefined instruction (which is in a page which
  242. is actually present). A normal return from the prefetch abort handler (after
  243. loading the absent page) will cause the undefined instruction to execute and
  244. take the trap correctly. However the indicated page is already present, so
  245. the prefetch abort handler may simply return control, causing an infinite
  246. loop to be entered.
  247.  Therefore, the prefetch abort handler should check whether the indicated
  248. fault is in a page which is actually present. If so, the above condition
  249. must be present and so control should be passed to the undefined instruction
  250. handler. This will restore the expected sequential nature of the execution
  251. sequence; a normal return from the undefined instruction handler will cause
  252. the next instruction to be fetched (which will abort), the prefetch abort
  253. handler will be reentered (with an address pointing to the absent page), and
  254. execution can proceed normally.
  255.  
  256.  
  257.  
  258.                        ========================
  259.                        = Other points to note =
  260.                        ========================
  261.  
  262.  This section highlights some obscure cases of ARM operation which should be
  263. borne in mind when writing code.
  264.  
  265. 1) Use of R15
  266. -------------
  267.  *************************************************************************
  268.  * WARNING: When the PC is used as a destination, operand, base or shift *
  269.  *          register, different results will be obtained depending on    *
  270.  *          the instruction and the exact usage of R15                   *
  271.  *************************************************************************
  272.  * Applicability: ARM2,ARM3                                              *
  273.  *************************************************************************
  274.  
  275.   Full details of the value derived from or written into R15+PSR for each
  276. instruction class is given in the datasheet. Care must be taken when using
  277. R15 because small changes in the instruction can yield significantly
  278. different results.
  279.  
  280. e.g. Consider data operations of the type:-
  281.                 <opcode>{cond}{S} Rd,Rn,Rm
  282.            or   <opcode>{cond}{S} Rd,Rn,Rm,<shiftname> Rs
  283.     a) When R15 is used in the Rm position, it will give the value of the PC
  284.        together with the PSR flags.
  285.     b) When R15 is used in the Rn or Rs positions, it will give the value of
  286.        the PC without the PSR flags (PSR bits replaced by zeros).
  287.  
  288.     MOV R0,#0
  289.     ORR R1,R0,R15   ; R1:=PC+PSR  (bits 31:26,1:0 reflect PSR flags)
  290.     ORR R2,R15,R0   ; R2:=PC      (bits 31:26,1:0 set to zero)
  291.  
  292. NOTE:
  293.  The relevant instruction description in the ARM datasheets should be
  294. consulted for full details of the behaviour of R15.
  295.  
  296.  
  297. 2) STM: Inclusion of the base in the register list
  298. --------------------------------------------------
  299.  ***********************************************************************
  300.  * WARNING: In the case of a STM with writeback that includes the base *
  301.  *          register in the register list, the value of the base       *
  302.  *          register stored depends upon its position in the register  *
  303.  *          list                                                       *
  304.  ***********************************************************************
  305.  * Applicability: ARM2,ARM3                                            *
  306.  ***********************************************************************
  307.  
  308.  During a STM, the first register is written out at the start of the second
  309. cycle of the instruction. When writeback is specified, the base is written
  310. back at the end of the second cycle. A STM which includes storing the base
  311. with the base as the first register to be stored will therefore store the
  312. unchanged value, whereas with the base second or later in the transfer
  313. order, it will store the modified value.
  314.  
  315.  e.g.
  316.     MOV   R5,#&1000
  317.     STMIA R5!,{R5-R6}  ;  Stores value of R5=&1000
  318.  
  319.     MOV   R5,#&1000
  320.     STMIA R5!,{R4-R5}  ;  Stores value of R5=&1008
  321.  
  322.  
  323.  
  324.  
  325. 3) MUL/MLA: Register restrictions
  326. ---------------------------------
  327.   ****************************************************
  328.   *   Given  MUL Rd,Rm,Rs                            *
  329.   *      or  MLA Rd,Rm,Rs,Rn                         *
  330.   *                                                  *
  331.   *   Then   Rd & Rm must be different registers     *
  332.   *               Rd must not be R15                 *
  333.   ****************************************************
  334.   * Applicability: ARM2,ARM3                         *
  335.   ****************************************************
  336.  
  337.  Due to the way that Booth's algorithm has been implemented, certain
  338. combinations of operand registers should be avoided. (The assembler will
  339. issue a warning if these restrictions are overlooked.)
  340.  The destination register (Rd) should not be the same as the Rm operand
  341. register, as Rd is used to hold intermediate values and Rm is used
  342. repeatedly during the multiply. A MUL will give a zero result if Rm=Rd, and
  343. a MLA will give a meaningless result.
  344.  The destination register (Rd) should also not be R15. R15 is protected from
  345. modification by these instructions, so the instruction will have no effect,
  346. except that it will put meaningless values in the PSR flags if the S bit is
  347. set.
  348.  All other register combinations will give correct results, and Rd, Rn and
  349. Rs may use the same register when required.
  350.  
  351.  
  352.  
  353. 4) LDM/STM: Address Exceptions
  354. ------------------------------
  355.   ************************************************************************
  356.   * WARNING: Illegal addresses formed during a LDM or STM operation will *
  357.   *          not cause an address exception                              *
  358.   ************************************************************************
  359.   * Applicability: ARM2,ARM3                                             *
  360.   ************************************************************************
  361.  
  362.  Only the address of the first transfer of a LDM or STM is checked for an
  363. address exception; if subsequent addresses over- or under-flow into illegal
  364. address space they will be truncated to 26 bits but will not cause an
  365. address exception trap.
  366.  
  367. e.g. Assume processor is in a non-user mode & MEMC being accessed:-
  368.        {these examples are very contrived}
  369.  
  370.     MOV R0,#&04000000  ; R0=&04000000
  371.     STMIA R0,{R1-R2}   ; Address exception reported (base address illegal)
  372.  
  373.     MOV R0,#&04000000
  374.     SUB R0,R0,#4       ; R0=&03FFFFFC
  375.     STMIA R0,{R1-R2}   ; No address exception reported (base address legal)
  376.                        ; code will overwrite data at address &00000000
  377.  
  378. NOTE:
  379.  The exact behaviour of the system depends upon the memory manager to which
  380. the processor is attached; in some cases, the wraparound may be detected and
  381. the processor aborted.
  382.  
  383.  
  384.  
  385. 5) LDC/STC: Address Exceptions
  386. ------------------------------
  387.   ************************************************************************
  388.   * WARNING: Illegal addresses formed during a LDC or STC operation will *
  389.   *          not cause an address exception (affects LDF/STF)            *
  390.   ************************************************************************
  391.   * Applicability: ARM2,ARM3                                             *
  392.   ************************************************************************
  393.  
  394.  The coprocessor data transfer operations act like STM and LDM with the
  395. processor generating the addresses and the coprocessor supplying/reading the
  396. data. As with LDM/STM, only the address of the first transfer of a LDM or
  397. STM is checked for an address exception; if subsequent addresses over- or
  398. under-flow into illegal address space they will be truncated to 26 bits but
  399. will not cause an address exception trap.
  400.  Note that the floating point LDF/STF instructions are forms of LDC & STC!
  401.  
  402. e.g. Assume processor is in a non-user mode & MEMC being accessed:-
  403.        {these examples are very contrived}
  404.  
  405.     MOV R0,#&04000000  ; R0=&04000000
  406.     STC CP1,CR0,[R0]   ; Address exception reported (base address illegal)
  407.  
  408.     MOV R0,#&04000000
  409.     SUB R0,R0,#4       ; R0=&03FFFFFC
  410.     STFD F0,[R0]       ; No address exception reported (base address legal)
  411.                        ; code will overwrite data at address &00000000
  412.  
  413. NOTE:
  414.  The exact behaviour of the system depends upon the memory manager to which
  415. the processor is attached; in some cases, the wraparound may be detected and
  416. the processor aborted.
  417.  
  418.  
  419.  
  420. 6) LDC: Data transfers to a coprocessor fetch more data than expected
  421. ---------------------------------------------------------------------
  422.  ***************************************************************************
  423.  * Data to be transferred to a coprocessor with the LDC instruction should *
  424.  * never be placed in the last word of an addressable chunk of memory, nor *
  425.  * in the word of memory immediately preceding a read-sensitive memory     *
  426.  * location                                                                *
  427.  ***************************************************************************
  428.  * Applicability: ARM3                                                     *
  429.  ***************************************************************************
  430.  
  431.  Due to the pipelining introduced into the ARM3 coprocessor interface, an
  432. LDC operation will cause one extra word of data to be fetched from the
  433. internal cache or external memory by ARM3 and then discarded; if the extra
  434. data is fetched from an area of external memory marked as cacheable, a whole
  435. line of data will be fetched and placed in the cache.
  436.  A particular case in point is that an LDC whose data ends at the last word
  437. of a memory page will load and then discard the first word (and hence the
  438. first cache line) of the next page. A minor effect of this is that it may
  439. occasionally cause an unnecessary page swap in a virtual memory system. The
  440. major effect of it is that (whether in a virtual memory system or not), the
  441. data for an LDC should never be placed in the last word of an addressable
  442. chunk of memory: the LDC will attempt to read the immediately following
  443. non-existent location and thus produce a memory fault.
  444.  
  445. e.g. Assume processor is in a non-user mode, FPU hardware attached and MEMC
  446.      being accessed:-
  447.        {this example is very contrived}
  448.  
  449.     MOV  R13,#&03000000 ; R13=Address of I/O space
  450.     STFD F0,[R13,#-8]!  ; Store F.P. register 0 at top of physical memory
  451.                         ; (two words of data transferred)
  452.     LDFD F1,[R13],#8    ; Load F.P. register 1 from top of physical memory
  453.                         ; but THREE words of data are transferred, and the
  454.                         ; third access will read from I/O space which may be
  455.                         ; read sensitive!  *** BEWARE ***
  456.  
  457.  
  458.