home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / progutils / armrules
Encoding:
Text File  |  1990-10-03  |  21.1 KB  |  450 lines

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