romlib-design.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. Script
  50. ~~~~~~
  51. There is a ``romlib_generate.py`` Python script that generates the necessary
  52. files for the "library at ROM" to work. It implements multiple functions:
  53. 1. ``romlib_generate.py gentbl [args]`` - Generates the jump table by parsing
  54. the index file.
  55. 2. ``romlib_generator.py genvar [args]`` - Generates the jump table global
  56. variable (**not** the jump table itself) with the absolute address in ROM.
  57. This global variable is, basically, a pointer to the jump table.
  58. 3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for
  59. each entry in the index file except for the ones that contain the keyword
  60. ``patch``. The generated wrapper file is called ``<fn_name>.s``.
  61. 4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means
  62. it resolves all the include commands in the file recursively. It can also
  63. generate a dependency file of the included index files which can be directly
  64. used in makefiles.
  65. Each ``romlib_generate.py`` function has its own manual which is accessible by
  66. runing ``romlib_generator.py [function] --help``.
  67. ``romlib_generate.py`` requires Python 3 environment.
  68. Patching of functions in library at ROM
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. The ``romlib_generator.py genwrappers`` does not generate wrappers for the
  71. entries in the index file that contain the keyword ``patch``. Thus, it allows
  72. calling the function from the actual library by breaking the link to the
  73. "library at ROM" version of this function.
  74. The calling sequence for a patched function is as follows:
  75. BL image --> function
  76. Memory impact
  77. ~~~~~~~~~~~~~
  78. Using library at ROM will modify the memory layout of the BL images:
  79. - The ROM library needs a page aligned RAM section to hold the RW data. This
  80. section is defined by the ROMLIB_RW_BASE and ROMLIB_RW_END macros.
  81. On Arm platforms a section of 1 page (0x1000) is allocated at the top of SRAM.
  82. This will have for effect to shift down all the BL images by 1 page.
  83. - Depending on the functions moved to the ROM library, the size of the BL images
  84. will be reduced.
  85. For example: moving MbedTLS function into the ROM library reduces BL1 and
  86. BL2, but not BL31.
  87. - This change in BL images size can be taken into consideration to optimize the
  88. memory layout when defining the BLx_BASE macros.
  89. Build library at ROM
  90. ~~~~~~~~~~~~~~~~~~~~~
  91. The environment variable ``CROSS_COMPILE`` must be set appropriately. Refer to
  92. :ref:`Performing an Initial Build` for more information about setting this
  93. variable.
  94. In the below example the usage of ROMLIB together with mbed TLS is demonstrated
  95. to showcase the benefits of library at ROM - it's not mandatory.
  96. .. code:: shell
  97. make PLAT=fvp \
  98. MBEDTLS_DIR=</path/to/mbedtls/> \
  99. TRUSTED_BOARD_BOOT=1 GENERATE_COT=1 \
  100. ARM_ROTPK_LOCATION=devel_rsa \
  101. ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem \
  102. BL33=</path/to/bl33.bin> \
  103. USE_ROMLIB=1 \
  104. all fip
  105. --------------
  106. *Copyright (c) 2019, Arm Limited. All rights reserved.*