romlib-design.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. Library at ROM
  2. ==============
  3. This document provides an overview of the "library at ROM" implementation in
  4. Trusted Firmware-A (TF-A).
  5. Introduction
  6. ~~~~~~~~~~~~
  7. The "library at ROM" feature allows platforms to build a library of functions to
  8. be placed in ROM. This reduces SRAM usage by utilising the available space in
  9. ROM. The "library at ROM" contains a jump table with the list of functions that
  10. are placed in ROM. The capabilities of the "library at ROM" are:
  11. 1. Functions can be from one or several libraries.
  12. 2. Functions can be patched after they have been programmed into ROM.
  13. 3. Platform-specific libraries can be placed in ROM.
  14. 4. Functions can be accessed by one or more BL images.
  15. Index file
  16. ~~~~~~~~~~
  17. .. image:: ../resources/diagrams/romlib_design.png
  18. :width: 600
  19. Library at ROM is described by an index file with the list of functions to be
  20. placed in ROM. The index file is platform specific and its format is:
  21. ::
  22. lib function [patch]
  23. lib -- Name of the library the function belongs to
  24. function -- Name of the function to be placed in library at ROM
  25. [patch] -- Option to patch the function
  26. It is also possible to insert reserved spaces in the list by using the keyword
  27. "reserved" rather than the "lib" and "function" names as shown below:
  28. ::
  29. reserved
  30. The reserved spaces can be used to add more functions in the future without
  31. affecting the order and location of functions already existing in the jump
  32. table. Also, for additional flexibility and modularity, the index file can
  33. include other index files.
  34. For an index file example, refer to ``lib/romlib/jmptbl.i``.
  35. Wrapper functions
  36. ~~~~~~~~~~~~~~~~~
  37. .. image:: ../resources/diagrams/romlib_wrapper.png
  38. :width: 600
  39. When invoking a function of the "library at ROM", the calling sequence is as
  40. follows:
  41. BL image --> wrapper function --> jump table entry --> library at ROM
  42. The index file is used to create a jump table which is placed in ROM. Then, the
  43. wrappers refer to the jump table to call the "library at ROM" functions. The
  44. wrappers essentially contain a branch instruction to the jump table entry
  45. corresponding to the original function. Finally, the original function in the BL
  46. image(s) is replaced with the wrapper function.
  47. The "library at ROM" contains a necessary init function that initialises the
  48. global variables defined by the functions inside "library at ROM".
  49. Wrapper functions are specified at the link stage of compilation and cannot
  50. interpose uppon functions within the same translation unit. For example, if
  51. function ``fn_a`` calls ``fn_b`` within translation unit ``functions.c`` and
  52. the romlib jump table includes an entry for ``fn_b``, ``fn_a`` will include
  53. a reference to ``fn_b``'s original program text instead of the wrapper. Thus
  54. the jumptable author must take care to include public entry points into
  55. translation units to avoid paying the program text cost twice, once in the
  56. original executable and once in romlib.
  57. Script
  58. ~~~~~~
  59. There is a ``romlib_generator.py`` Python script that generates the necessary
  60. files for the "library at ROM" to work. It implements multiple functions:
  61. 1. ``romlib_generator.py gentbl [args]`` - Generates the jump table by parsing
  62. the index file.
  63. 2. ``romlib_generator.py genvar [args]`` - Generates the jump table global
  64. variable (**not** the jump table itself) with the absolute address in ROM.
  65. This global variable is, basically, a pointer to the jump table.
  66. 3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for
  67. each entry in the index file except for the ones that contain the keyword
  68. ``patch``. The generated wrapper file is called ``wrappers.s``.
  69. 4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means
  70. it resolves all the include commands in the file recursively. It can also
  71. generate a dependency file of the included index files which can be directly
  72. used in makefiles.
  73. Each ``romlib_generator.py`` function has its own manual which is accessible by
  74. runing ``romlib_generator.py [function] --help``.
  75. ``romlib_generator.py`` requires Python 3 environment.
  76. Patching of functions in library at ROM
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. The ``romlib_generator.py genwrappers`` does not generate wrappers for the
  79. entries in the index file that contain the keyword ``patch``. Thus, it allows
  80. calling the function from the actual library by breaking the link to the
  81. "library at ROM" version of this function.
  82. The calling sequence for a patched function is as follows:
  83. BL image --> function
  84. Memory impact
  85. ~~~~~~~~~~~~~
  86. Using library at ROM will modify the memory layout of the BL images:
  87. - The ROM library needs a page aligned RAM section to hold the RW data. This
  88. section is defined by the ROMLIB_RW_BASE and ROMLIB_RW_END macros.
  89. On Arm platforms a section of 1 page (0x1000) is allocated at the top of SRAM.
  90. This will have for effect to shift down all the BL images by 1 page.
  91. - Depending on the functions moved to the ROM library, the size of the BL images
  92. will be reduced.
  93. For example: moving MbedTLS function into the ROM library reduces BL1 and
  94. BL2, but not BL31.
  95. - This change in BL images size can be taken into consideration to optimize the
  96. memory layout when defining the BLx_BASE macros.
  97. Build library at ROM
  98. ~~~~~~~~~~~~~~~~~~~~~
  99. The environment variable ``CROSS_COMPILE`` must be set appropriately. Refer to
  100. :ref:`Performing an Initial Build` for more information about setting this
  101. variable.
  102. In the below example the usage of ROMLIB together with mbed TLS is demonstrated
  103. to showcase the benefits of library at ROM - it's not mandatory.
  104. .. code:: shell
  105. make PLAT=fvp \
  106. MBEDTLS_DIR=</path/to/mbedtls/> \
  107. TRUSTED_BOARD_BOOT=1 GENERATE_COT=1 \
  108. ARM_ROTPK_LOCATION=devel_rsa \
  109. ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem \
  110. BL33=</path/to/bl33.bin> \
  111. USE_ROMLIB=1 \
  112. all fip
  113. --------------
  114. *Copyright (c) 2019, Arm Limited. All rights reserved.*